1 /* $Id: testobsdrdr.c,v 1.31 2020/05/29 22:29:13 nanard Exp $ */
2 /* MiniUPnP project
3  * http://miniupnp.free.fr/ or https://miniupnp.tuxfamily.org/
4  * (c) 2006-2020 Thomas Bernard
5  * This software is subject to the conditions detailed
6  * in the LICENCE file provided within the distribution */
7 
8 #include <string.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <sys/types.h>
12 #include <netinet/in.h>
13 #include <syslog.h>
14 
15 #include "obsdrdr.h"
16 
17 /*int logpackets = 1;*/
18 int runtime_flags = 0;
19 const char * tag = 0;
20 const char * anchor_name = "miniupnpd";
21 const char * queue = NULL;
22 const char * use_ext_ip_addr = "42.42.42.42";
23 
24 void
25 list_rules(void);
26 
27 void
list_eports_tcp(void)28 list_eports_tcp(void)
29 {
30 	unsigned short * port_list;
31 	unsigned int number = 0;
32 	unsigned int i;
33 	port_list = get_portmappings_in_range(0, 65535, IPPROTO_TCP, &number);
34 	printf("%u ports redirected (TCP) :", number);
35 	for(i = 0; i < number; i++)
36 	{
37 		printf(" %hu", port_list[i]);
38 	}
39 	printf("\n");
40 	free(port_list);
41 }
42 
43 void
test_index(void)44 test_index(void)
45 {
46 	char ifname[16/*IFNAMSIZ*/];
47 	char iaddr[32];
48 	char desc[64];
49 	char rhost[32];
50 	unsigned short iport = 0;
51 	unsigned short eport = 0;
52 	int proto = 0;
53 	unsigned int timestamp;
54 	ifname[0] = '\0';
55 	iaddr[0] = '\0';
56 	rhost[0] = '\0';
57 	if(get_redirect_rule_by_index(0, ifname, &eport, iaddr, sizeof(iaddr),
58 	                              &iport, &proto, desc, sizeof(desc),
59 	                              rhost, sizeof(rhost),
60                                   &timestamp, 0, 0) < 0)
61 	{
62 		printf("get.._by_index : no rule\n");
63 	}
64 	else
65 	{
66 		printf("%s %u -> %s:%u proto %d\n", ifname, (unsigned int)eport,
67 		       iaddr, (unsigned int)iport, proto);
68 		printf("description: \"%s\"\n", desc);
69 	}
70 }
71 
72 int
main(int argc,char ** argv)73 main(int argc, char * * argv)
74 {
75 	char buf[32];
76 	char desc[64];
77 	char rhost[64];
78 	/*char rhost[32];*/
79 	unsigned short iport;
80 	unsigned int timestamp;
81 	u_int64_t packets = 0;
82 	u_int64_t bytes = 0;
83 	int clear = 0;
84 
85 	if(argc > 1) {
86 		if(0 == strcmp(argv[1], "--clear") || 0 == strcmp(argv[1], "-c"))
87 			clear = 1;
88 	}
89 
90 	openlog("testobsdrdr", LOG_PERROR, LOG_USER);
91 	if(init_redirect() < 0)
92 	{
93 		fprintf(stderr, "init_redirect() failed\n");
94 		return 1;
95 	}
96 #if 0
97 	add_redirect_rule("ep0", 12123, "192.168.1.23", 1234);
98 	add_redirect_rule2("ep0", 12155, "192.168.1.155", 1255, IPPROTO_TCP);
99 #endif
100 	if(add_redirect_rule2("ep0", NULL/*"8.8.8.8"*/, 12123, "192.168.1.125", 1234,
101 	                   IPPROTO_UDP, "test description", 0) < 0)
102 		printf("add_redirect_rule2() #3 failed\n");
103 	use_ext_ip_addr = NULL;
104 	if(add_redirect_rule2("em0", NULL, 12123, "127.1.2.3", 1234,
105 	                   IPPROTO_TCP, "test description tcp", 0) < 0)
106 		printf("add_redirect_rule2() #4 failed\n");
107 	if(add_filter_rule2("em0", NULL, "127.1.2.3", 12123, 1234, IPPROTO_TCP,
108 	                 "test description tcp") < 0)
109 		printf("add_filter_rule2() #1 failed\n");
110 
111 	list_rules();
112 	list_eports_tcp();
113 
114 
115 	if(get_redirect_rule("xl1", 4662, IPPROTO_TCP,
116 	                     buf, sizeof(buf), &iport, desc, sizeof(desc),
117 	                     rhost, sizeof(rhost),
118 	                     &timestamp,
119 	                     &packets, &bytes) < 0)
120 		printf("get_redirect_rule() failed\n");
121 	else
122 	{
123 		printf("\n%s:%d '%s' packets=%" PRIu64 " bytes=%" PRIu64 "\n", buf, (int)iport, desc,
124 		       packets, bytes);
125 	}
126 
127 /*
128 	if(delete_redirect_rule("ep0", 12123, IPPROTO_UDP) < 0)
129 		printf("delete_redirect_rule() failed\n");
130 */
131 	if(delete_redirect_and_filter_rules("ep0", 12123, IPPROTO_UDP) < 0)
132 		printf("delete_redirect_rule() failed\n");
133 
134 	if(delete_redirect_rule("ep0", 12123, IPPROTO_UDP) < 0)
135 		printf("delete_redirect_rule() failed\n");
136 
137 	if(delete_redirect_and_filter_rules("em0", 12123, IPPROTO_TCP) < 0)
138 		printf("delete_redirect_and_filter_rules() failed\n");
139 
140 	test_index();
141 
142 	if(clear) {
143 		clear_redirect_rules();
144 		clear_filter_rules();
145 		clear_nat_rules();
146 	}
147 	/*list_rules();*/
148 
149 	return 0;
150 }
151 
152 
153