1/* $Id: testipfwrdr.c,v 1.7 2011/06/22 21:57:17 nanard Exp $ */
2/*
3 * MiniUPnP project
4 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
5 * (c) 2009-2011 Jardel Weyrich, Thomas Bernard
6 * This software is subject to the conditions detailed
7 * in the LICENCE file provided within the distribution
8 */
9
10#include <stdio.h>
11#include <syslog.h>
12#include <time.h>
13#include <netinet/in.h>
14#include "ipfwrdr.h"
15
16// test program for ipfwrdr.c
17static const char * ifname = "lo0";
18
19static void
20list_port_mappings(void)
21{
22	int i;
23	unsigned short eport;
24	char iaddr[16];
25	unsigned short iport;
26	int proto;
27	char desc[64];
28	char rhost[32];
29	unsigned int timestamp;
30	u_int64_t packets, bytes;
31
32	printf("== Port Mapping List ==\n");
33	for(i = 0;; i++) {
34		iaddr[0] = '\0';
35		desc[0] = '\0';
36		eport = iport = 0;
37		timestamp = 0;
38		packets = bytes = 0;
39		proto = -1;
40		if(get_redirect_rule_by_index(i, 0/*ifname*/, &eport, iaddr, sizeof(iaddr),
41		                              &iport, &proto, desc, sizeof(desc),
42		                              rhost, sizeof(rhost),
43		                              &timestamp, &packets, &bytes) < 0)
44			break;
45		printf("%2d - %5hu=>%15s:%5hu %d '%s' '%s' %u %" PRIu64 " %" PRIu64 "\n",
46		       i, eport, iaddr, iport, proto, desc, rhost, timestamp,
47		       packets, bytes);
48	}
49	printf("== %d Port Mapping%s ==\n", i, (i > 1)?"s":"");
50}
51
52int main(int argc, char * * argv) {
53	unsigned int timestamp;
54	char desc[64];
55	char addr[16];
56	char rhost[40];
57	unsigned short iport = 0;
58	const char * in_rhost = "8.8.8.8";
59
60	desc[0] = '\0';
61	addr[0] = '\0';
62	openlog("testipfwrdrd", LOG_CONS | LOG_PERROR, LOG_USER);
63	if(init_redirect() < 0) {
64		fprintf(stderr, "init_redirect() failed.\n");
65		return 1;
66	}
67	list_port_mappings();
68	delete_redirect_rule(ifname, 2222, IPPROTO_TCP);
69	delete_redirect_rule(ifname, 2223, IPPROTO_TCP);
70	add_redirect_rule2(ifname, "", 2223,
71	                   "10.1.1.17", 4445, IPPROTO_TCP,
72	                   "test miniupnpd", time(NULL) + 60);
73	add_redirect_rule2(ifname, in_rhost, 2222,
74	                   "10.1.1.16", 4444, IPPROTO_TCP,
75	                   "test miniupnpd", time(NULL) + 60);
76	get_redirect_rule(ifname, 2222, IPPROTO_TCP, addr, sizeof(addr), &iport,
77	                  desc, sizeof(desc), rhost, sizeof(rhost),
78	                  &timestamp, NULL, NULL);
79	printf("'%s' %s:%hu '%s' %u\n", rhost, addr, iport, desc, timestamp);
80	list_port_mappings();
81	delete_redirect_rule(ifname, 2222, IPPROTO_TCP);
82	delete_redirect_rule(ifname, 2223, IPPROTO_TCP);
83	list_port_mappings();
84	shutdown_redirect();
85	return 0;
86}
87
88