1 /* $Id: testiptcrdr_peer.c,v 1.1 2013/12/13 13:10:48 nanard Exp $ */
2 /* MiniUPnP project
3  * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
4  * (c) 2006-2012 Thomas Bernard
5  * This software is subject to the conditions detailed
6  * in the LICENCE file provided within the distribution */
7 
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <netinet/in.h>
11 #include <syslog.h>
12 
13 #include "iptcrdr.h"
14 #include "../commonrdr.h"
15 #include "iptcrdr.c"
16 
17 #ifndef PRIu64
18 #define PRIu64 "llu"
19 #endif
20 
21 int
main(int argc,char ** argv)22 main(int argc, char ** argv)
23 {
24 	unsigned short eport, iport, rport;
25 	const char * eaddr, *iaddr, *rhost;
26 	printf("Usage %s <ext_ip> <ext_port> <internal_ip> <internal_port> <peer_ip> <peer_port>\n", argv[0]);
27 
28 	if(argc<6)
29 		return -1;
30 	openlog("testiptcrdr_peer", LOG_PERROR|LOG_CONS, LOG_LOCAL0);
31 	eaddr = argv[1];
32 	eport = (unsigned short)atoi(argv[2]);
33 	iaddr = argv[3];
34 	iport = (unsigned short)atoi(argv[4]);
35 	rhost = argv[5];
36 	rport = (unsigned short)atoi(argv[6]);
37 #if 1
38 	printf("trying to redirect port %hu to %s:%hu\n", eport, iaddr, iport);
39 	if(addpeernatrule(IPPROTO_TCP, eaddr, eport, iaddr, iport, rhost, rport) < 0)
40 		return -1;
41 #endif
42 	/* test */
43 	{
44 		unsigned short p1, p2;
45 		char addr[16];
46 		int proto2;
47 		char desc[256];
48 		char rhost[256];
49 		unsigned int timestamp;
50 		u_int64_t packets, bytes;
51 
52 		desc[0] = '\0';
53 		if(get_redirect_rule_by_index(0, "", &p1,
54 									  addr, sizeof(addr), &p2,
55 									  &proto2, desc, sizeof(desc),
56 									  rhost, sizeof(rhost),
57 									  &timestamp,
58 									  &packets, &bytes) < 0)
59 		{
60 			printf("rule not found\n");
61 		}
62 		else
63 		{
64 			printf("redirected port %hu to %s:%hu proto %d   packets=%" PRIu64 " bytes=%" PRIu64 "\n",
65 			       p1, addr, p2, proto2, packets, bytes);
66 		}
67 	}
68 	printf("trying to list nat rules :\n");
69 	list_redirect_rule(argv[1]);
70 	printf("deleting\n");
71 	delete_redirect_and_filter_rules(eport, IPPROTO_TCP);
72 	return 0;
73 }
74 
75