1/* $Id: testobsdrdr.c,v 1.28 2014/03/06 13:02:47 nanard Exp $ */
2/* MiniUPnP project
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 */
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;*/
18int runtime_flags = 0;
19const char * tag = 0;
20const char * anchor_name = "miniupnpd";
21const char * queue = NULL;
22
23void
24list_rules(void);
25
26void
27list_eports_tcp(void)
28{
29	unsigned short * port_list;
30	unsigned int number = 0;
31	unsigned int i;
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++)
35	{
36		printf(" %hu", port_list[i]);
37	}
38	printf("\n");
39	free(port_list);
40}
41
42void
43test_index(void)
44{
45	char ifname[16/*IFNAMSIZ*/];
46	char iaddr[32];
47	char desc[64];
48	char rhost[32];
49	unsigned short iport = 0;
50	unsigned short eport = 0;
51	int proto = 0;
52	unsigned int timestamp;
53	ifname[0] = '\0';
54	iaddr[0] = '\0';
55	rhost[0] = '\0';
56	if(get_redirect_rule_by_index(0, ifname, &eport, iaddr, sizeof(iaddr),
57	                              &iport, &proto, desc, sizeof(desc),
58	                              rhost, sizeof(rhost),
59                                  &timestamp, 0, 0) < 0)
60	{
61		printf("get.._by_index : no rule\n");
62	}
63	else
64	{
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);
68	}
69}
70
71int
72main(int argc, char * * argv)
73{
74	char buf[32];
75	char desc[64];
76	char rhost[64];
77	/*char rhost[32];*/
78	unsigned short iport;
79	unsigned int timestamp;
80	u_int64_t packets = 0;
81	u_int64_t bytes = 0;
82	int clear = 0;
83
84	if(argc > 1) {
85		if(0 == strcmp(argv[1], "--clear") || 0 == strcmp(argv[1], "-c"))
86			clear = 1;
87	}
88
89	openlog("testobsdrdr", LOG_PERROR, LOG_USER);
90	if(init_redirect() < 0)
91	{
92		fprintf(stderr, "init_redirect() failed\n");
93		return 1;
94	}
95#if 0
96	add_redirect_rule("ep0", 12123, "192.168.1.23", 1234);
97	add_redirect_rule2("ep0", 12155, "192.168.1.155", 1255, IPPROTO_TCP);
98#endif
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");
108
109	list_rules();
110	list_eports_tcp();
111
112
113	if(get_redirect_rule("xl1", 4662, IPPROTO_TCP,
114	                     buf, sizeof(buf), &iport, desc, sizeof(desc),
115	                     rhost, sizeof(rhost),
116	                     &timestamp,
117	                     &packets, &bytes) < 0)
118		printf("get_redirect_rule() failed\n");
119	else
120	{
121		printf("\n%s:%d '%s' packets=%llu bytes=%llu\n", buf, (int)iport, desc,
122		       packets, bytes);
123	}
124
125	if(delete_redirect_rule("ep0", 12123, IPPROTO_UDP) < 0)
126		printf("delete_redirect_rule() failed\n");
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("em0", 12123, IPPROTO_TCP) < 0)
132		printf("delete_redirect_and_filter_rules() failed\n");
133
134	test_index();
135
136	if(clear) {
137		clear_redirect_rules();
138		clear_filter_rules();
139	}
140	/*list_rules();*/
141
142	return 0;
143}
144
145
146