Annotation of embedaddon/miniupnpd/miniupnpc-async/testasync.c, revision 1.1.1.1

1.1       misho       1: /* $Id: testasync.c,v 1.14 2014/11/07 12:07:38 nanard Exp $ */
                      2: /* miniupnpc-async
                      3:  * Copyright (c) 2008-2017, Thomas BERNARD <miniupnp@free.fr>
                      4:  * http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
                      5:  *
                      6:  * Permission to use, copy, modify, and/or distribute this software for any
                      7:  * purpose with or without fee is hereby granted, provided that the above
                      8:  * copyright notice and this permission notice appear in all copies.
                      9:  *
                     10:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     11:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     12:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     13:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     14:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     15:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     16:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
                     17: #include <stdio.h>
                     18: #include <string.h>
                     19: #include <sys/select.h>
                     20: /* for getnameinfo() : */
                     21: #include <sys/types.h>
                     22: #include <sys/socket.h>
                     23: #include <netdb.h>
                     24: /* compile with -DUPNPC_USE_SELECT to enable upnpc_select_fds() function */
                     25: #include "miniupnpc-async.h"
                     26: #include "upnpreplyparse.h"
                     27: 
                     28: enum methods {
                     29:        EGetExternalIP,
                     30:        EGetRates,
                     31:        EAddPortMapping,
                     32:        ENothing
                     33: };
                     34: 
                     35: int main(int argc, char * * argv)
                     36: {
                     37:        char ip_address[64];
                     38:        int r, n;
                     39:        upnpc_t upnp;
                     40:        upnpc_device_t * device = NULL;
                     41:        const char * multicastif = NULL;
                     42:        enum methods next_method_to_call = EGetExternalIP;
                     43:        enum methods last_method = ENothing;
                     44:        if(argc>1)
                     45:                multicastif = argv[1];
                     46:        if((r = upnpc_init(&upnp, multicastif)) < 0) {
                     47:                fprintf(stderr, "upnpc_init failed : %d", r);
                     48:                return 1;
                     49:        }
                     50:        r = upnpc_process(&upnp);
                     51:        printf("upnpc_process returned %d\n", r);
                     52:        while(upnp.state != EUPnPError) {
                     53:                int nfds;
                     54:                fd_set readfds;
                     55:                fd_set writefds;
                     56:                /*struct timeval timeout;*/
                     57: 
                     58:                FD_ZERO(&readfds);
                     59:                FD_ZERO(&writefds);
                     60:                nfds = 0;
                     61:                n = upnpc_select_fds(&upnp, &nfds, &readfds, &writefds);
                     62:                if(n <= 0) {
                     63:                        printf("nothing to select()...\n");
                     64:                        break;
                     65:                }
                     66: #if 0
                     67:                timeout.tv_sec = 0;
                     68:                timeout.tv_usec = 0;
                     69: #endif
                     70: #if DEBUG
                     71:                printf("select(%d, ...);\n", nfds+1);
                     72: #endif /* DEBUG */
                     73:                if(select(nfds+1, &readfds, &writefds, NULL, NULL/*&timeout*/) < 0) {
                     74:                        perror("select");
                     75:                        return 1;
                     76:                }
                     77:                upnpc_check_select_fds(&upnp, &readfds, &writefds);
                     78:                r = upnpc_process(&upnp);
                     79: #if DEBUG
                     80:                printf("upnpc_process returned %d\n", r);
                     81: #endif /* DEBUG */
                     82:                if(r < 0)
                     83:                        break;
                     84:                if(upnp.state == EUPnPReady) {
                     85:                        char * p;
                     86:                        if(device == NULL) {
                     87:                                /* select one device */
                     88:                                device = upnp.device_list;      /* pick up the first one */
                     89:                        }
                     90:                        printf("Process UPnP IGD Method results : HTTP %d\n", device->http_response_code);
                     91:                        if(device->http_response_code == 200) {
                     92:                                switch(last_method) {
                     93:                                case EGetExternalIP:
                     94:                                        p = GetValueFromNameValueList(&device->soap_response_data, "NewExternalIPAddress");
                     95:                                        printf("ExternalIPAddress = %s\n", p);
                     96:        /*                              p = GetValueFromNameValueList(&pdata, "errorCode");*/
                     97:                                        break;
                     98:                                case EGetRates:
                     99:                                        p = GetValueFromNameValueList(&device->soap_response_data, "NewLayer1DownstreamMaxBitRate");
                    100:                                        printf("DownStream MaxBitRate = %s\t", p);
                    101:                                        p = GetValueFromNameValueList(&device->soap_response_data, "NewLayer1UpstreamMaxBitRate");
                    102:                                        printf("UpStream MaxBitRate = %s\n", p);
                    103:                                        break;
                    104:                                case EAddPortMapping:
                    105:                                        printf("OK\n");
                    106:                                        break;
                    107:                                case ENothing:
                    108:                                        break;
                    109:                                }
                    110:                        } else {
                    111:                                printf("SOAP error :\n");
                    112:                                printf("  faultcode='%s'\n", GetValueFromNameValueList(&device->soap_response_data, "faultcode"));
                    113:                                printf("  faultstring='%s'\n", GetValueFromNameValueList(&device->soap_response_data, "faultstring"));
                    114:                                printf("  errorCode=%s\n", GetValueFromNameValueList(&device->soap_response_data, "errorCode"));
                    115:                                printf("  errorDescription='%s'\n", GetValueFromNameValueList(&device->soap_response_data, "errorDescription"));
                    116:                        }
                    117:                        if(next_method_to_call == ENothing)
                    118:                                break;
                    119:                        printf("Ready to call UPnP IGD methods\n");
                    120:                        last_method = next_method_to_call;
                    121:                        switch(next_method_to_call) {
                    122:                        case EGetExternalIP:
                    123:                                printf("GetExternalIPAddress\n");
                    124:                                upnpc_get_external_ip_address(device);
                    125:                                next_method_to_call = EGetRates;
                    126:                                break;
                    127:                        case EGetRates:
                    128:                                printf("GetCommonLinkProperties\n");
                    129:                                upnpc_get_link_layer_max_rate(device);
                    130:                                next_method_to_call = EAddPortMapping;
                    131:                                break;
                    132:                        case EAddPortMapping:
                    133:                                if(getnameinfo((struct sockaddr *)&device->selfaddr, device->selfaddrlen,
                    134:                                               ip_address, sizeof(ip_address), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
                    135:                                        fprintf(stderr, "getnameinfo() failed\n");
                    136:                                }
                    137:                                printf("our IP address is %s\n", ip_address);
                    138:                                printf("AddPortMapping\n");
                    139:                                upnpc_add_port_mapping(device,
                    140:                            NULL /* remote_host */, 40002 /* ext_port */,
                    141:                            42042 /* int_port */, ip_address /* int_client */,
                    142:                            "TCP" /* proto */, "this is a test" /* description */,
                    143:                            0 /* lease duration */);
                    144:                                next_method_to_call = ENothing;
                    145:                        case ENothing:
                    146:                                break;
                    147:                        }
                    148:                }
                    149:        }
                    150:        upnpc_finalize(&upnp);
                    151:        return 0;
                    152: }
                    153: 

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