miniupnp/miniupnpc-async/testasync.c

154 lines
5.0 KiB
C
Raw Permalink Normal View History

/* $Id: testasync.c,v 1.14 2014/11/07 12:07:38 nanard Exp $ */
2014-11-07 11:32:20 +00:00
/* miniupnpc-async
* Copyright (c) 2008-2017, Thomas BERNARD <miniupnp@free.fr>
2014-11-07 11:32:20 +00:00
* http://miniupnp.free.fr/ or http://miniupnp.tuxfamily.org/
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
/* for getnameinfo() : */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
2014-11-07 11:32:20 +00:00
/* compile with -DUPNPC_USE_SELECT to enable upnpc_select_fds() function */
#include "miniupnpc-async.h"
#include "upnpreplyparse.h"
enum methods {
EGetExternalIP,
EGetRates,
EAddPortMapping,
ENothing
};
int main(int argc, char * * argv)
{
char ip_address[64];
2014-11-07 11:32:20 +00:00
int r, n;
upnpc_t upnp;
2017-05-26 10:14:59 +00:00
upnpc_device_t * device = NULL;
2014-11-07 11:32:20 +00:00
const char * multicastif = NULL;
enum methods next_method_to_call = EGetExternalIP;
enum methods last_method = ENothing;
if(argc>1)
multicastif = argv[1];
if((r = upnpc_init(&upnp, multicastif)) < 0) {
fprintf(stderr, "upnpc_init failed : %d", r);
return 1;
}
r = upnpc_process(&upnp);
printf("upnpc_process returned %d\n", r);
while(upnp.state != EUPnPError) {
2014-11-07 11:32:20 +00:00
int nfds;
fd_set readfds;
fd_set writefds;
/*struct timeval timeout;*/
FD_ZERO(&readfds);
FD_ZERO(&writefds);
nfds = 0;
n = upnpc_select_fds(&upnp, &nfds, &readfds, &writefds);
if(n <= 0) {
printf("nothing to select()...\n");
break;
}
#if 0
timeout.tv_sec = 0;
timeout.tv_usec = 0;
#endif
#if DEBUG
2014-11-07 11:32:20 +00:00
printf("select(%d, ...);\n", nfds+1);
#endif /* DEBUG */
2014-11-07 11:32:20 +00:00
if(select(nfds+1, &readfds, &writefds, NULL, NULL/*&timeout*/) < 0) {
perror("select");
return 1;
}
upnpc_check_select_fds(&upnp, &readfds, &writefds);
2014-11-07 11:32:20 +00:00
r = upnpc_process(&upnp);
#if DEBUG
2014-11-07 11:32:20 +00:00
printf("upnpc_process returned %d\n", r);
#endif /* DEBUG */
if(r < 0)
break;
if(upnp.state == EUPnPReady) {
2014-11-07 11:32:20 +00:00
char * p;
2017-05-26 10:14:59 +00:00
if(device == NULL) {
/* select one device */
device = upnp.device_list; /* pick up the first one */
}
printf("Process UPnP IGD Method results : HTTP %d\n", device->http_response_code);
if(device->http_response_code == 200) {
2014-11-07 11:32:20 +00:00
switch(last_method) {
case EGetExternalIP:
2017-05-26 10:14:59 +00:00
p = GetValueFromNameValueList(&device->soap_response_data, "NewExternalIPAddress");
2014-11-07 11:32:20 +00:00
printf("ExternalIPAddress = %s\n", p);
/* p = GetValueFromNameValueList(&pdata, "errorCode");*/
break;
case EGetRates:
2017-05-26 10:14:59 +00:00
p = GetValueFromNameValueList(&device->soap_response_data, "NewLayer1DownstreamMaxBitRate");
2014-11-07 11:32:20 +00:00
printf("DownStream MaxBitRate = %s\t", p);
2017-05-26 10:14:59 +00:00
p = GetValueFromNameValueList(&device->soap_response_data, "NewLayer1UpstreamMaxBitRate");
2014-11-07 11:32:20 +00:00
printf("UpStream MaxBitRate = %s\n", p);
break;
case EAddPortMapping:
printf("OK\n");
break;
2014-11-07 11:32:20 +00:00
case ENothing:
break;
}
} else {
printf("SOAP error :\n");
2017-05-26 10:14:59 +00:00
printf(" faultcode='%s'\n", GetValueFromNameValueList(&device->soap_response_data, "faultcode"));
printf(" faultstring='%s'\n", GetValueFromNameValueList(&device->soap_response_data, "faultstring"));
printf(" errorCode=%s\n", GetValueFromNameValueList(&device->soap_response_data, "errorCode"));
printf(" errorDescription='%s'\n", GetValueFromNameValueList(&device->soap_response_data, "errorDescription"));
2014-11-07 11:32:20 +00:00
}
if(next_method_to_call == ENothing)
break;
printf("Ready to call UPnP IGD methods\n");
last_method = next_method_to_call;
switch(next_method_to_call) {
case EGetExternalIP:
printf("GetExternalIPAddress\n");
2017-05-26 10:14:59 +00:00
upnpc_get_external_ip_address(device);
2014-11-07 11:32:20 +00:00
next_method_to_call = EGetRates;
break;
case EGetRates:
printf("GetCommonLinkProperties\n");
2017-05-26 10:14:59 +00:00
upnpc_get_link_layer_max_rate(device);
2014-11-07 11:32:20 +00:00
next_method_to_call = EAddPortMapping;
break;
2014-11-07 11:32:20 +00:00
case EAddPortMapping:
2017-05-26 10:14:59 +00:00
if(getnameinfo((struct sockaddr *)&device->selfaddr, device->selfaddrlen,
ip_address, sizeof(ip_address), NULL, 0, NI_NUMERICHOST | NI_NUMERICSERV) < 0) {
fprintf(stderr, "getnameinfo() failed\n");
}
printf("our IP address is %s\n", ip_address);
2014-11-07 11:32:20 +00:00
printf("AddPortMapping\n");
2017-05-26 10:14:59 +00:00
upnpc_add_port_mapping(device,
2014-11-07 11:32:20 +00:00
NULL /* remote_host */, 40002 /* ext_port */,
42042 /* int_port */, ip_address /* int_client */,
2014-11-07 11:32:20 +00:00
"TCP" /* proto */, "this is a test" /* description */,
0 /* lease duration */);
next_method_to_call = ENothing;
case ENothing:
break;
}
}
}
upnpc_finalize(&upnp);
return 0;
}