Annotation of embedaddon/miniupnpd/pf/testobsdrdr.c, revision 1.1.1.3

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

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>