1/* $Id: testiptcrdr.c,v 1.18 2012/04/24 22:41:53 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 "nftnlrdr.h" 14#include "../commonrdr.h" 15 16#ifndef PRIu64 17#define PRIu64 "llu" 18#endif 19 20static int 21add_filter_rule(int proto, const char * rhost, 22 const char * iaddr, unsigned short iport) 23{ 24 return add_filter_rule2(NULL, rhost, iaddr, 0, iport, proto, NULL); 25} 26 27static int 28addnatrule(int proto, unsigned short eport, 29 const char * iaddr, unsigned short iport, 30 const char * rhost) 31{ 32 return add_redirect_rule2(NULL, rhost, eport, iaddr, iport, proto, NULL, 0); 33} 34 35int 36main(int argc, char ** argv) 37{ 38 unsigned short eport, iport; 39 const char * iaddr; 40 41 if(argc<4) { 42 printf("Usage %s <ext_port> <internal_ip> <internal_port>\n", argv[0]); 43 return -1; 44 } 45 openlog("testnftnlrdr", LOG_PERROR|LOG_CONS, LOG_LOCAL0); 46 eport = (unsigned short)atoi(argv[1]); 47 iaddr = argv[2]; 48 iport = (unsigned short)atoi(argv[3]); 49 printf("trying to redirect port %hu to %s:%hu\n", eport, iaddr, iport); 50 if(addnatrule(IPPROTO_TCP, eport, iaddr, iport, NULL) < 0) { 51 printf("addnatrule() failed!\n"); 52 return -1; 53 } 54 if(add_filter_rule(IPPROTO_TCP, NULL, iaddr, iport) < 0) { 55 printf("add_filter_rule() failed!\n"); 56 return -1; 57 } 58 /* test */ 59 { 60 unsigned short p1, p2; 61 char addr[16]; 62 int proto2; 63 char desc[256]; 64 char rhost[256]; 65 unsigned int timestamp; 66 u_int64_t packets, bytes; 67 68 desc[0] = '\0'; 69 printf("test0\n"); 70 if(get_redirect_rule_by_index(0, "", &p1, 71 addr, sizeof(addr), &p2, 72 &proto2, desc, sizeof(desc), 73 rhost, sizeof(rhost), 74 ×tamp, 75 &packets, &bytes) < 0) 76 { 77 printf("rule not found\n"); 78 } 79 else 80 { 81 printf("redirected port %hu to %s:%hu proto %d packets=%" PRIu64 " bytes=%" PRIu64 "\n", 82 p1, addr, p2, proto2, packets, bytes); 83 } 84 printf("test\n"); 85 } 86 printf("trying to list nat rules :\n"); 87 list_redirect_rule(argv[1]); 88 printf("deleting\n"); 89 delete_redirect_and_filter_rules(eport, IPPROTO_TCP); 90 return 0; 91} 92