1 /* $Id: testobsdrdr.c,v 1.28 2014/03/06 13:02:47 nanard Exp $ */
3 * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4 * (c) 2006-2014 Thomas Bernard
5 * This software is subject to the conditions detailed
6 * in the LICENCE file provided within the distribution */
11 #include <sys/types.h>
12 #include <netinet/in.h>
17 /*int logpackets = 1;*/
18 int runtime_flags
= 0;
20 const char * anchor_name
= "miniupnpd";
21 const char * queue
= NULL
;
29 unsigned short * port_list
;
30 unsigned int number
= 0;
32 port_list
= get_portmappings_in_range(0, 65535, IPPROTO_TCP
, &number
);
33 printf("%u ports redirected (TCP) :", number
);
34 for(i
= 0; i
< number
; i
++)
36 printf(" %hu", port_list
[i
]);
45 char ifname
[16/*IFNAMSIZ*/];
49 unsigned short iport
= 0;
50 unsigned short eport
= 0;
52 unsigned int timestamp
;
56 if(get_redirect_rule_by_index(0, ifname
, &eport
, iaddr
, sizeof(iaddr
),
57 &iport
, &proto
, desc
, sizeof(desc
),
59 ×tamp
, 0, 0) < 0)
61 printf("get.._by_index : no rule\n");
65 printf("%s %u -> %s:%u proto %d\n", ifname
, (unsigned int)eport
,
66 iaddr
, (unsigned int)iport
, proto
);
67 printf("description: \"%s\"\n", desc
);
72 main(int argc
, char * * argv
)
79 unsigned int timestamp
;
80 u_int64_t packets
= 0;
85 if(0 == strcmp(argv
[1], "--clear") || 0 == strcmp(argv
[1], "-c"))
89 openlog("testobsdrdr", LOG_PERROR
, LOG_USER
);
90 if(init_redirect() < 0)
92 fprintf(stderr
, "init_redirect() failed\n");
96 add_redirect_rule("ep0", 12123, "192.168.1.23", 1234);
97 add_redirect_rule2("ep0", 12155, "192.168.1.155", 1255, IPPROTO_TCP
);
99 if(add_redirect_rule2("ep0", "8.8.8.8", 12123, "192.168.1.125", 1234,
100 IPPROTO_UDP
, "test description", 0) < 0)
101 printf("add_redirect_rule2() #3 failed\n");
102 if(add_redirect_rule2("em0", NULL
, 12123, "127.1.2.3", 1234,
103 IPPROTO_TCP
, "test description tcp", 0) < 0)
104 printf("add_redirect_rule2() #4 failed\n");
105 if(add_filter_rule2("em0", NULL
, "127.1.2.3", 12123, 1234, IPPROTO_TCP
,
106 "test description tcp") < 0)
107 printf("add_filter_rule2() #1 failed\n");
113 if(get_redirect_rule("xl1", 4662, IPPROTO_TCP
,
114 buf
, sizeof(buf
), &iport
, desc
, sizeof(desc
),
115 rhost
, sizeof(rhost
),
117 &packets
, &bytes
) < 0)
118 printf("get_redirect_rule() failed\n");
121 printf("\n%s:%d '%s' packets=%llu bytes=%llu\n", buf
, (int)iport
, desc
,
125 if(delete_redirect_rule("ep0", 12123, IPPROTO_UDP
) < 0)
126 printf("delete_redirect_rule() failed\n");
128 if(delete_redirect_rule("ep0", 12123, IPPROTO_UDP
) < 0)
129 printf("delete_redirect_rule() failed\n");
131 if(delete_redirect_and_filter_rules("em0", 12123, IPPROTO_TCP
) < 0)
132 printf("delete_redirect_and_filter_rules() failed\n");
137 clear_redirect_rules();
138 clear_filter_rules();