Miniupnpd v. 1.5 (20110618)
[tomato.git] / release / src / router / miniupnpd / ipfw / testipfwrdr.c
blob4eb67e52974e6b17539d2fbd462cd8d7800ab474
1 /* $Id: testipfwrdr.c,v 1.6 2011/06/04 15:47:18 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 */
10 #include <stdio.h>
11 #include <syslog.h>
12 #include <time.h>
13 #include <netinet/in.h>
14 #include "ipfwrdr.h"
16 // test program for ipfwrdr.c
17 static const char * ifname = "lo0";
19 static void
20 list_port_mappings(void)
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;
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' %u %" PRIu64 " %" PRIu64 "\n",
46 i, eport, iaddr, iport, proto, desc, timestamp,
47 packets, bytes);
49 printf("== %d Port Mapping%s ==\n", i, (i > 1)?"s":"");
52 int main(int argc, char * * argv) {
53 unsigned int timestamp;
54 char desc[64];
55 char addr[16];
56 unsigned short iport = 0;
57 const char * rhost = "8.8.8.8";
59 desc[0] = '\0';
60 addr[0] = '\0';
61 openlog("testipfwrdrd", LOG_CONS | LOG_PERROR, LOG_USER);
62 if(init_redirect() < 0) {
63 fprintf(stderr, "init_redirect() failed.\n");
64 return 1;
66 list_port_mappings();
67 delete_redirect_rule(ifname, 2222, IPPROTO_TCP);
68 add_redirect_rule2(ifname, rhost, 2222,
69 "10.1.1.16", 4444, IPPROTO_TCP,
70 "test miniupnpd", time(NULL) + 60);
71 get_redirect_rule(ifname, 2222, IPPROTO_TCP, addr, sizeof(addr), &iport,
72 desc, sizeof(desc), &timestamp, NULL, NULL);
73 printf("%s:%hu '%s' %u\n", addr, iport, desc, timestamp);
74 list_port_mappings();
75 delete_redirect_rule(ifname, 2222, IPPROTO_TCP);
76 list_port_mappings();
77 shutdown_redirect();
78 return 0;