miniupnpc-async: retrieve our ip address

This commit is contained in:
Thomas Bernard 2017-05-26 12:08:03 +02:00
parent 3e89381fba
commit bbb27ab97d
3 changed files with 23 additions and 1 deletions

View File

@ -402,6 +402,13 @@ static int upnpc_send_request(upnpc_device_t * p)
"Connection: Close\r\n"
"User-Agent: MiniUPnPc-async\r\n"
"\r\n";
/* retrieve "our" IP address used to connect to the UPnP device */
p->selfaddrlen = sizeof(struct sockaddr_storage);
if(getsockname(p->http_socket, (struct sockaddr *)&p->selfaddr, &p->selfaddrlen) < 0) {
PRINT_SOCKET_ERROR("getsockname()");
}
if(p->http_request == NULL) {
char hostname[MAXHOSTNAMELEN+1];
unsigned short port;

View File

@ -17,6 +17,9 @@
#ifndef MINIUPNPC_ASYNC_H_INCLUDED
#define MINIUPNPC_ASYNC_H_INCLUDED
/* for struct sockaddr_storage */
#include <netinet/in.h>
#include "declspec.h"
#include "upnpreplyparse.h"
@ -64,6 +67,8 @@ typedef struct upnpc_device {
int http_response_chunked;
int http_response_code;
struct NameValueParserData soap_response_data;
struct sockaddr_storage selfaddr;
socklen_t selfaddrlen;
} upnpc_device_t;
typedef struct {

View File

@ -17,6 +17,10 @@
#include <stdio.h>
#include <string.h>
#include <sys/select.h>
/* for getnameinfo() : */
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
/* compile with -DUPNPC_USE_SELECT to enable upnpc_select_fds() function */
#include "miniupnpc-async.h"
#include "upnpreplyparse.h"
@ -30,6 +34,7 @@ enum methods {
int main(int argc, char * * argv)
{
char ip_address[64];
int r, n;
upnpc_t upnp;
const char * multicastif = NULL;
@ -120,10 +125,15 @@ int main(int argc, char * * argv)
next_method_to_call = EAddPortMapping;
break;
case EAddPortMapping:
if(getnameinfo((struct sockaddr *)&upnp.device_list->selfaddr, upnp.device_list->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);
printf("AddPortMapping\n");
upnpc_add_port_mapping(upnp.device_list, /* XXX */
NULL /* remote_host */, 40002 /* ext_port */,
42042 /* int_port */, "192.168.1.202" /* int_client */,
42042 /* int_port */, ip_address /* int_client */,
"TCP" /* proto */, "this is a test" /* description */,
0 /* lease duration */);
next_method_to_call = ENothing;