2015-07-23 20:44:37 +00:00
|
|
|
/* $Id: miniupnpc.c,v 1.135 2015/07/23 20:40:08 nanard Exp $ */
|
2011-09-27 20:25:35 +00:00
|
|
|
/* Project : miniupnp
|
2012-01-21 13:38:38 +00:00
|
|
|
* Web : http://miniupnp.free.fr/
|
2011-09-27 20:25:35 +00:00
|
|
|
* Author : Thomas BERNARD
|
2015-05-22 10:25:51 +00:00
|
|
|
* copyright (c) 2005-2015 Thomas Bernard
|
2011-09-27 20:25:35 +00:00
|
|
|
* This software is subjet to the conditions detailed in the
|
|
|
|
* provided LICENSE file. */
|
|
|
|
#define __EXTENSIONS__ 1
|
2014-06-15 01:14:43 +00:00
|
|
|
#if !defined(__APPLE__) && !defined(__sun)
|
2011-09-27 20:25:35 +00:00
|
|
|
#if !defined(_XOPEN_SOURCE) && !defined(__OpenBSD__) && !defined(__NetBSD__)
|
|
|
|
#ifndef __cplusplus
|
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifndef __BSD_VISIBLE
|
|
|
|
#define __BSD_VISIBLE 1
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2014-06-16 08:54:03 +00:00
|
|
|
#if !defined(__DragonFly__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__APPLE__) && !defined(_WIN32) && !defined(__CYGWIN__) && !defined(__sun) && !defined(__GNU__) && !defined(__FreeBSD_kernel__)
|
2012-01-07 10:40:50 +00:00
|
|
|
#define HAS_IP_MREQN
|
|
|
|
#endif
|
|
|
|
|
2011-09-27 20:25:35 +00:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-27 20:25:35 +00:00
|
|
|
/* Win32 Specific includes and defines */
|
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <io.h>
|
|
|
|
#include <iphlpapi.h>
|
|
|
|
#define snprintf _snprintf
|
2012-10-16 16:53:28 +00:00
|
|
|
#define strdup _strdup
|
2011-09-27 20:25:35 +00:00
|
|
|
#ifndef strncasecmp
|
|
|
|
#if defined(_MSC_VER) && (_MSC_VER >= 1400)
|
|
|
|
#define strncasecmp _memicmp
|
|
|
|
#else /* defined(_MSC_VER) && (_MSC_VER >= 1400) */
|
|
|
|
#define strncasecmp memicmp
|
|
|
|
#endif /* defined(_MSC_VER) && (_MSC_VER >= 1400) */
|
|
|
|
#endif /* #ifndef strncasecmp */
|
|
|
|
#define MAXHOSTNAMELEN 64
|
2012-01-21 13:38:38 +00:00
|
|
|
#else /* #ifdef _WIN32 */
|
2011-09-27 20:25:35 +00:00
|
|
|
/* Standard POSIX includes */
|
|
|
|
#include <unistd.h>
|
|
|
|
#if defined(__amigaos__) && !defined(__amigaos4__)
|
|
|
|
/* Amiga OS 3 specific stuff */
|
|
|
|
#define socklen_t int
|
|
|
|
#else
|
|
|
|
#include <sys/select.h>
|
|
|
|
#endif
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#if !defined(__amigaos__) && !defined(__amigaos4__)
|
|
|
|
#include <poll.h>
|
|
|
|
#endif
|
|
|
|
#include <strings.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#define closesocket close
|
2012-01-21 13:38:38 +00:00
|
|
|
#endif /* #else _WIN32 */
|
2011-09-27 20:25:35 +00:00
|
|
|
#if defined(__amigaos__) || defined(__amigaos4__)
|
|
|
|
/* Amiga OS specific stuff */
|
|
|
|
#define TIMEVAL struct timeval
|
|
|
|
#endif
|
2014-06-02 15:13:52 +00:00
|
|
|
#ifdef __GNU__
|
|
|
|
#define MAXHOSTNAMELEN 64
|
|
|
|
#endif
|
2011-09-27 20:25:35 +00:00
|
|
|
|
2013-11-15 09:42:27 +00:00
|
|
|
|
|
|
|
#if defined(HAS_IP_MREQN) && defined(NEED_STRUCT_IP_MREQN)
|
2015-05-22 10:27:38 +00:00
|
|
|
/* Several versions of glibc don't define this structure,
|
|
|
|
* define it here and compile with CFLAGS NEED_STRUCT_IP_MREQN */
|
2013-11-15 09:42:27 +00:00
|
|
|
struct ip_mreqn
|
|
|
|
{
|
|
|
|
struct in_addr imr_multiaddr; /* IP multicast address of group */
|
|
|
|
struct in_addr imr_address; /* local IP address of interface */
|
|
|
|
int imr_ifindex; /* Interface index */
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2011-09-27 20:25:35 +00:00
|
|
|
#include "miniupnpc.h"
|
|
|
|
#include "minissdpc.h"
|
|
|
|
#include "miniwget.h"
|
|
|
|
#include "minisoap.h"
|
|
|
|
#include "minixml.h"
|
|
|
|
#include "upnpcommands.h"
|
|
|
|
#include "connecthostport.h"
|
|
|
|
#include "receivedata.h"
|
|
|
|
|
2014-11-17 23:01:48 +00:00
|
|
|
/* compare the begining of a string with a constant string */
|
|
|
|
#define COMPARE(str, cstr) (0==memcmp(str, cstr, sizeof(cstr) - 1))
|
|
|
|
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-27 20:25:35 +00:00
|
|
|
#define PRINT_SOCKET_ERROR(x) printf("Socket error: %s, %d\n", x, WSAGetLastError());
|
|
|
|
#else
|
|
|
|
#define PRINT_SOCKET_ERROR(x) perror(x)
|
|
|
|
#endif
|
|
|
|
|
2013-12-09 08:24:28 +00:00
|
|
|
#ifndef MAXHOSTNAMELEN
|
|
|
|
#define MAXHOSTNAMELEN 64
|
|
|
|
#endif
|
|
|
|
|
2011-09-27 20:25:35 +00:00
|
|
|
#define SOAPPREFIX "s"
|
|
|
|
#define SERVICEPREFIX "u"
|
|
|
|
#define SERVICEPREFIX2 'u'
|
|
|
|
|
|
|
|
/* root description parsing */
|
2014-06-01 10:35:13 +00:00
|
|
|
MINIUPNP_LIBSPEC void parserootdesc(const char * buffer, int bufsize, struct IGDdatas * data)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
|
|
|
struct xmlparser parser;
|
|
|
|
/* xmlparser object */
|
|
|
|
parser.xmlstart = buffer;
|
|
|
|
parser.xmlsize = bufsize;
|
|
|
|
parser.data = data;
|
|
|
|
parser.starteltfunc = IGDstartelt;
|
|
|
|
parser.endeltfunc = IGDendelt;
|
|
|
|
parser.datafunc = IGDdata;
|
|
|
|
parser.attfunc = 0;
|
|
|
|
parsexml(&parser);
|
|
|
|
#ifdef DEBUG
|
|
|
|
printIGD(data);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* simpleUPnPcommand2 :
|
|
|
|
* not so simple !
|
|
|
|
* return values :
|
|
|
|
* pointer - OK
|
|
|
|
* NULL - error */
|
|
|
|
char * simpleUPnPcommand2(int s, const char * url, const char * service,
|
|
|
|
const char * action, struct UPNParg * args,
|
|
|
|
int * bufsize, const char * httpversion)
|
|
|
|
{
|
|
|
|
char hostname[MAXHOSTNAMELEN+1];
|
|
|
|
unsigned short port = 0;
|
|
|
|
char * path;
|
|
|
|
char soapact[128];
|
|
|
|
char soapbody[2048];
|
|
|
|
char * buf;
|
|
|
|
int n;
|
|
|
|
|
|
|
|
*bufsize = 0;
|
|
|
|
snprintf(soapact, sizeof(soapact), "%s#%s", service, action);
|
|
|
|
if(args==NULL)
|
|
|
|
{
|
|
|
|
/*soapbodylen = */snprintf(soapbody, sizeof(soapbody),
|
|
|
|
"<?xml version=\"1.0\"?>\r\n"
|
|
|
|
"<" SOAPPREFIX ":Envelope "
|
|
|
|
"xmlns:" SOAPPREFIX "=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
SOAPPREFIX ":encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<" SOAPPREFIX ":Body>"
|
|
|
|
"<" SERVICEPREFIX ":%s xmlns:" SERVICEPREFIX "=\"%s\">"
|
|
|
|
"</" SERVICEPREFIX ":%s>"
|
|
|
|
"</" SOAPPREFIX ":Body></" SOAPPREFIX ":Envelope>"
|
|
|
|
"\r\n", action, service, action);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
char * p;
|
|
|
|
const char * pe, * pv;
|
|
|
|
int soapbodylen;
|
|
|
|
soapbodylen = snprintf(soapbody, sizeof(soapbody),
|
|
|
|
"<?xml version=\"1.0\"?>\r\n"
|
|
|
|
"<" SOAPPREFIX ":Envelope "
|
|
|
|
"xmlns:" SOAPPREFIX "=\"http://schemas.xmlsoap.org/soap/envelope/\" "
|
|
|
|
SOAPPREFIX ":encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">"
|
|
|
|
"<" SOAPPREFIX ":Body>"
|
|
|
|
"<" SERVICEPREFIX ":%s xmlns:" SERVICEPREFIX "=\"%s\">",
|
|
|
|
action, service);
|
|
|
|
p = soapbody + soapbodylen;
|
|
|
|
while(args->elt)
|
|
|
|
{
|
|
|
|
/* check that we are never overflowing the string... */
|
|
|
|
if(soapbody + sizeof(soapbody) <= p + 100)
|
|
|
|
{
|
|
|
|
/* we keep a margin of at least 100 bytes */
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
*(p++) = '<';
|
|
|
|
pe = args->elt;
|
|
|
|
while(*pe)
|
|
|
|
*(p++) = *(pe++);
|
|
|
|
*(p++) = '>';
|
|
|
|
if((pv = args->val))
|
|
|
|
{
|
|
|
|
while(*pv)
|
|
|
|
*(p++) = *(pv++);
|
|
|
|
}
|
|
|
|
*(p++) = '<';
|
|
|
|
*(p++) = '/';
|
|
|
|
pe = args->elt;
|
|
|
|
while(*pe)
|
|
|
|
*(p++) = *(pe++);
|
|
|
|
*(p++) = '>';
|
|
|
|
args++;
|
|
|
|
}
|
|
|
|
*(p++) = '<';
|
|
|
|
*(p++) = '/';
|
|
|
|
*(p++) = SERVICEPREFIX2;
|
|
|
|
*(p++) = ':';
|
|
|
|
pe = action;
|
|
|
|
while(*pe)
|
|
|
|
*(p++) = *(pe++);
|
|
|
|
strncpy(p, "></" SOAPPREFIX ":Body></" SOAPPREFIX ":Envelope>\r\n",
|
|
|
|
soapbody + sizeof(soapbody) - p);
|
|
|
|
}
|
2012-06-23 22:55:31 +00:00
|
|
|
if(!parseURL(url, hostname, &port, &path, NULL)) return NULL;
|
|
|
|
if(s < 0) {
|
|
|
|
s = connecthostport(hostname, port, 0);
|
|
|
|
if(s < 0) {
|
|
|
|
/* failed to connect */
|
2011-09-27 20:25:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
n = soapPostSubmit(s, path, hostname, port, soapact, soapbody, httpversion);
|
|
|
|
if(n<=0) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Error sending SOAP request\n");
|
|
|
|
#endif
|
|
|
|
closesocket(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf = getHTTPResponse(s, bufsize);
|
|
|
|
#ifdef DEBUG
|
|
|
|
if(*bufsize > 0 && buf)
|
|
|
|
{
|
|
|
|
printf("SOAP Response :\n%.*s\n", *bufsize, buf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
closesocket(s);
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* simpleUPnPcommand :
|
|
|
|
* not so simple !
|
|
|
|
* return values :
|
|
|
|
* pointer - OK
|
|
|
|
* NULL - error */
|
|
|
|
char * simpleUPnPcommand(int s, const char * url, const char * service,
|
|
|
|
const char * action, struct UPNParg * args,
|
|
|
|
int * bufsize)
|
|
|
|
{
|
|
|
|
char * buf;
|
|
|
|
|
2012-06-23 22:56:24 +00:00
|
|
|
#if 1
|
2011-09-27 20:25:35 +00:00
|
|
|
buf = simpleUPnPcommand2(s, url, service, action, args, bufsize, "1.1");
|
2012-06-23 22:56:24 +00:00
|
|
|
#else
|
2011-09-27 20:25:35 +00:00
|
|
|
buf = simpleUPnPcommand2(s, url, service, action, args, bufsize, "1.0");
|
|
|
|
if (!buf || *bufsize == 0)
|
|
|
|
{
|
|
|
|
#if DEBUG
|
|
|
|
printf("Error or no result from SOAP request; retrying with HTTP/1.1\n");
|
|
|
|
#endif
|
|
|
|
buf = simpleUPnPcommand2(s, url, service, action, args, bufsize, "1.1");
|
|
|
|
}
|
2012-06-23 22:56:24 +00:00
|
|
|
#endif
|
2011-09-27 20:25:35 +00:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parseMSEARCHReply()
|
|
|
|
* the last 4 arguments are filled during the parsing :
|
|
|
|
* - location/locationsize : "location:" field of the SSDP reply packet
|
|
|
|
* - st/stsize : "st:" field of the SSDP reply packet.
|
|
|
|
* The strings are NOT null terminated */
|
|
|
|
static void
|
|
|
|
parseMSEARCHReply(const char * reply, int size,
|
|
|
|
const char * * location, int * locationsize,
|
2015-07-22 09:08:52 +00:00
|
|
|
const char * * st, int * stsize,
|
|
|
|
const char * * usn, int * usnsize)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
|
|
|
int a, b, i;
|
|
|
|
i = 0;
|
|
|
|
a = i; /* start of the line */
|
|
|
|
b = 0; /* end of the "header" (position of the colon) */
|
|
|
|
while(i<size)
|
|
|
|
{
|
|
|
|
switch(reply[i])
|
|
|
|
{
|
|
|
|
case ':':
|
|
|
|
if(b==0)
|
|
|
|
{
|
|
|
|
b = i; /* end of the "header" */
|
|
|
|
/*for(j=a; j<b; j++)
|
|
|
|
{
|
|
|
|
putchar(reply[j]);
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case '\x0a':
|
|
|
|
case '\x0d':
|
|
|
|
if(b!=0)
|
|
|
|
{
|
|
|
|
/*for(j=b+1; j<i; j++)
|
|
|
|
{
|
|
|
|
putchar(reply[j]);
|
|
|
|
}
|
|
|
|
putchar('\n');*/
|
|
|
|
/* skip the colon and white spaces */
|
|
|
|
do { b++; } while(reply[b]==' ');
|
|
|
|
if(0==strncasecmp(reply+a, "location", 8))
|
|
|
|
{
|
|
|
|
*location = reply+b;
|
|
|
|
*locationsize = i-b;
|
|
|
|
}
|
|
|
|
else if(0==strncasecmp(reply+a, "st", 2))
|
|
|
|
{
|
|
|
|
*st = reply+b;
|
|
|
|
*stsize = i-b;
|
|
|
|
}
|
2015-07-22 09:08:52 +00:00
|
|
|
else if(0==strncasecmp(reply+a, "usn", 3))
|
|
|
|
{
|
|
|
|
*usn = reply+b;
|
|
|
|
*usnsize = i-b;
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
b = 0;
|
|
|
|
}
|
|
|
|
a = i+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* port upnp discover : SSDP protocol */
|
|
|
|
#define PORT 1900
|
|
|
|
#define XSTR(s) STR(s)
|
|
|
|
#define STR(s) #s
|
|
|
|
#define UPNP_MCAST_ADDR "239.255.255.250"
|
|
|
|
/* for IPv6 */
|
|
|
|
#define UPNP_MCAST_LL_ADDR "FF02::C" /* link-local */
|
|
|
|
#define UPNP_MCAST_SL_ADDR "FF05::C" /* site-local */
|
|
|
|
|
2014-01-31 13:59:21 +00:00
|
|
|
/* upnpDiscoverDevices() :
|
2011-09-27 20:25:35 +00:00
|
|
|
* return a chained list of all devices found or NULL if
|
|
|
|
* no devices was found.
|
|
|
|
* It is up to the caller to free the chained list
|
2015-07-23 20:44:37 +00:00
|
|
|
* delay is in millisecond (poll).
|
|
|
|
* UDA v1.1 says :
|
|
|
|
* The TTL for the IP packet SHOULD default to 2 and
|
|
|
|
* SHOULD be configurable. */
|
2014-06-01 10:35:13 +00:00
|
|
|
MINIUPNP_LIBSPEC struct UPNPDev *
|
2013-02-17 23:46:09 +00:00
|
|
|
upnpDiscoverDevices(const char * const deviceTypes[],
|
|
|
|
int delay, const char * multicastif,
|
|
|
|
const char * minissdpdsock, int sameport,
|
2015-07-23 20:44:37 +00:00
|
|
|
int ipv6, unsigned char ttl,
|
2015-05-22 10:25:51 +00:00
|
|
|
int * error,
|
|
|
|
int searchalltypes)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
|
|
|
struct UPNPDev * tmp;
|
|
|
|
struct UPNPDev * devlist = 0;
|
2012-06-23 22:52:51 +00:00
|
|
|
unsigned int scope_id = 0;
|
2011-09-27 20:25:35 +00:00
|
|
|
int opt = 1;
|
2012-03-01 01:51:24 +00:00
|
|
|
static const char MSearchMsgFmt[] =
|
2011-09-27 20:25:35 +00:00
|
|
|
"M-SEARCH * HTTP/1.1\r\n"
|
|
|
|
"HOST: %s:" XSTR(PORT) "\r\n"
|
|
|
|
"ST: %s\r\n"
|
|
|
|
"MAN: \"ssdp:discover\"\r\n"
|
|
|
|
"MX: %u\r\n"
|
|
|
|
"\r\n";
|
2013-02-20 16:07:18 +00:00
|
|
|
int deviceIndex;
|
2011-09-27 20:25:35 +00:00
|
|
|
char bufr[1536]; /* reception and emission buffer */
|
|
|
|
int sudp;
|
|
|
|
int n;
|
|
|
|
struct sockaddr_storage sockudp_r;
|
|
|
|
unsigned int mx;
|
|
|
|
#ifdef NO_GETADDRINFO
|
|
|
|
struct sockaddr_storage sockudp_w;
|
|
|
|
#else
|
|
|
|
int rv;
|
|
|
|
struct addrinfo hints, *servinfo, *p;
|
|
|
|
#endif
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-27 20:25:35 +00:00
|
|
|
MIB_IPFORWARDROW ip_forward;
|
|
|
|
#endif
|
|
|
|
int linklocal = 1;
|
|
|
|
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_UNKNOWN_ERROR;
|
2012-01-21 13:38:38 +00:00
|
|
|
#if !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__)
|
2011-09-27 20:25:35 +00:00
|
|
|
/* first try to get infos from minissdpd ! */
|
|
|
|
if(!minissdpdsock)
|
|
|
|
minissdpdsock = "/var/run/minissdpd.sock";
|
2015-05-22 10:25:51 +00:00
|
|
|
for(deviceIndex = 0; deviceTypes[deviceIndex]; deviceIndex++) {
|
2015-06-09 15:20:30 +00:00
|
|
|
struct UPNPDev * minissdpd_devlist;
|
|
|
|
int only_rootdevice = 1;
|
|
|
|
minissdpd_devlist = getDevicesFromMiniSSDPD(deviceTypes[deviceIndex],
|
2015-07-23 13:22:23 +00:00
|
|
|
minissdpdsock, 0);
|
2015-06-09 15:20:30 +00:00
|
|
|
if(minissdpd_devlist) {
|
2015-05-22 10:25:51 +00:00
|
|
|
#ifdef DEBUG
|
2015-06-11 12:28:35 +00:00
|
|
|
printf("returned by MiniSSDPD: %s\t%s\n",
|
|
|
|
minissdpd_devlist->st, minissdpd_devlist->descURL);
|
|
|
|
#endif /* DEBUG */
|
2015-06-09 15:20:30 +00:00
|
|
|
if(!strstr(minissdpd_devlist->st, "rootdevice"))
|
|
|
|
only_rootdevice = 0;
|
|
|
|
for(tmp = minissdpd_devlist; tmp->pNext != NULL; tmp = tmp->pNext) {
|
|
|
|
#ifdef DEBUG
|
2015-06-11 12:28:35 +00:00
|
|
|
printf("returned by MiniSSDPD: %s\t%s\n",
|
|
|
|
tmp->pNext->st, tmp->pNext->descURL);
|
2015-06-09 15:20:30 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
if(!strstr(tmp->st, "rootdevice"))
|
|
|
|
only_rootdevice = 0;
|
|
|
|
}
|
2015-05-22 10:25:51 +00:00
|
|
|
tmp->pNext = devlist;
|
2015-06-09 15:20:30 +00:00
|
|
|
devlist = minissdpd_devlist;
|
|
|
|
if(!searchalltypes && !only_rootdevice)
|
2015-05-22 10:25:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(tmp = devlist; tmp != NULL; tmp = tmp->pNext) {
|
2011-09-27 20:25:35 +00:00
|
|
|
/* We return what we have found if it was not only a rootdevice */
|
2015-05-22 10:25:51 +00:00
|
|
|
if(!strstr(tmp->st, "rootdevice")) {
|
2011-09-27 20:25:35 +00:00
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SUCCESS;
|
|
|
|
return devlist;
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 12:28:35 +00:00
|
|
|
#endif /* !defined(_WIN32) && !defined(__amigaos__) && !defined(__amigaos4__) */
|
|
|
|
|
|
|
|
/* direct discovery if minissdpd responses are not sufficient */
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-27 20:25:35 +00:00
|
|
|
sudp = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
#else
|
|
|
|
sudp = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, 0);
|
|
|
|
#endif
|
|
|
|
if(sudp < 0)
|
|
|
|
{
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SOCKET_ERROR;
|
|
|
|
PRINT_SOCKET_ERROR("socket");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* reception */
|
|
|
|
memset(&sockudp_r, 0, sizeof(struct sockaddr_storage));
|
|
|
|
if(ipv6) {
|
|
|
|
struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockudp_r;
|
|
|
|
p->sin6_family = AF_INET6;
|
|
|
|
if(sameport)
|
|
|
|
p->sin6_port = htons(PORT);
|
|
|
|
p->sin6_addr = in6addr_any; /* in6addr_any is not available with MinGW32 3.4.2 */
|
|
|
|
} else {
|
|
|
|
struct sockaddr_in * p = (struct sockaddr_in *)&sockudp_r;
|
|
|
|
p->sin_family = AF_INET;
|
|
|
|
if(sameport)
|
|
|
|
p->sin_port = htons(PORT);
|
|
|
|
p->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
}
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2012-03-01 01:51:24 +00:00
|
|
|
/* This code could help us to use the right Network interface for
|
2011-09-27 20:25:35 +00:00
|
|
|
* SSDP multicast traffic */
|
|
|
|
/* Get IP associated with the index given in the ip_forward struct
|
|
|
|
* in order to give this ip to setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF) */
|
|
|
|
if(!ipv6
|
|
|
|
&& (GetBestRoute(inet_addr("223.255.255.255"), 0, &ip_forward) == NO_ERROR)) {
|
|
|
|
DWORD dwRetVal = 0;
|
|
|
|
PMIB_IPADDRTABLE pIPAddrTable;
|
|
|
|
DWORD dwSize = 0;
|
|
|
|
#ifdef DEBUG
|
|
|
|
IN_ADDR IPAddr;
|
|
|
|
#endif
|
|
|
|
int i;
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("ifIndex=%lu nextHop=%lx \n", ip_forward.dwForwardIfIndex, ip_forward.dwForwardNextHop);
|
|
|
|
#endif
|
|
|
|
pIPAddrTable = (MIB_IPADDRTABLE *) malloc(sizeof (MIB_IPADDRTABLE));
|
2015-07-15 12:48:57 +00:00
|
|
|
if(pIPAddrTable) {
|
|
|
|
if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) {
|
|
|
|
free(pIPAddrTable);
|
|
|
|
pIPAddrTable = (MIB_IPADDRTABLE *) malloc(dwSize);
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
|
|
|
if(pIPAddrTable) {
|
|
|
|
dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 );
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("\tNum Entries: %ld\n", pIPAddrTable->dwNumEntries);
|
|
|
|
#endif
|
|
|
|
for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("\n\tInterface Index[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwIndex);
|
|
|
|
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr;
|
|
|
|
printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
|
|
|
|
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask;
|
|
|
|
printf("\tSubnet Mask[%d]: \t%s\n", i, inet_ntoa(IPAddr) );
|
|
|
|
IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr;
|
|
|
|
printf("\tBroadCast[%d]: \t%s (%ld)\n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr);
|
|
|
|
printf("\tReassembly size[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwReasmSize);
|
|
|
|
printf("\tType and State[%d]:", i);
|
|
|
|
printf("\n");
|
|
|
|
#endif
|
|
|
|
if (pIPAddrTable->table[i].dwIndex == ip_forward.dwForwardIfIndex) {
|
|
|
|
/* Set the address of this interface to be used */
|
|
|
|
struct in_addr mc_if;
|
|
|
|
memset(&mc_if, 0, sizeof(mc_if));
|
|
|
|
mc_if.s_addr = pIPAddrTable->table[i].dwAddr;
|
|
|
|
if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0) {
|
|
|
|
PRINT_SOCKET_ERROR("setsockopt");
|
|
|
|
}
|
|
|
|
((struct sockaddr_in *)&sockudp_r)->sin_addr.s_addr = pIPAddrTable->table[i].dwAddr;
|
|
|
|
#ifndef DEBUG
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(pIPAddrTable);
|
|
|
|
pIPAddrTable = NULL;
|
|
|
|
}
|
|
|
|
}
|
2015-06-11 12:28:35 +00:00
|
|
|
#endif /* _WIN32 */
|
2011-09-27 20:25:35 +00:00
|
|
|
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2011-09-27 20:25:35 +00:00
|
|
|
if (setsockopt(sudp, SOL_SOCKET, SO_REUSEADDR, (const char *)&opt, sizeof (opt)) < 0)
|
|
|
|
#else
|
|
|
|
if (setsockopt(sudp, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof (opt)) < 0)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SOCKET_ERROR;
|
2015-07-22 13:00:01 +00:00
|
|
|
PRINT_SOCKET_ERROR("setsockopt(SO_REUSEADDR,...)");
|
2011-09-27 20:25:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-23 09:40:42 +00:00
|
|
|
if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
|
2015-07-22 13:00:01 +00:00
|
|
|
{
|
|
|
|
/* not a fatal error */
|
|
|
|
PRINT_SOCKET_ERROR("setsockopt(IP_MULTICAST_TTL,...)");
|
|
|
|
}
|
|
|
|
|
2011-09-27 20:25:35 +00:00
|
|
|
if(multicastif)
|
|
|
|
{
|
|
|
|
if(ipv6) {
|
2012-01-21 13:38:38 +00:00
|
|
|
#if !defined(_WIN32)
|
2011-09-27 20:25:35 +00:00
|
|
|
/* according to MSDN, if_nametoindex() is supported since
|
|
|
|
* MS Windows Vista and MS Windows Server 2008.
|
|
|
|
* http://msdn.microsoft.com/en-us/library/bb408409%28v=vs.85%29.aspx */
|
|
|
|
unsigned int ifindex = if_nametoindex(multicastif); /* eth0, etc. */
|
2015-01-21 13:31:57 +00:00
|
|
|
if(setsockopt(sudp, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifindex, sizeof(ifindex)) < 0)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
|
|
|
PRINT_SOCKET_ERROR("setsockopt");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Setting of multicast interface not supported in IPv6 under Windows.\n");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
struct in_addr mc_if;
|
|
|
|
mc_if.s_addr = inet_addr(multicastif); /* ex: 192.168.x.x */
|
2012-01-07 10:40:50 +00:00
|
|
|
if(mc_if.s_addr != INADDR_NONE)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
2012-01-07 10:40:50 +00:00
|
|
|
((struct sockaddr_in *)&sockudp_r)->sin_addr.s_addr = mc_if.s_addr;
|
|
|
|
if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0)
|
|
|
|
{
|
|
|
|
PRINT_SOCKET_ERROR("setsockopt");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
#ifdef HAS_IP_MREQN
|
|
|
|
/* was not an ip address, try with an interface name */
|
|
|
|
struct ip_mreqn reqn; /* only defined with -D_BSD_SOURCE or -D_GNU_SOURCE */
|
|
|
|
memset(&reqn, 0, sizeof(struct ip_mreqn));
|
|
|
|
reqn.imr_ifindex = if_nametoindex(multicastif);
|
|
|
|
if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&reqn, sizeof(reqn)) < 0)
|
|
|
|
{
|
|
|
|
PRINT_SOCKET_ERROR("setsockopt");
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Setting of multicast interface not supported with interface name.\n");
|
|
|
|
#endif
|
|
|
|
#endif
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-31 14:20:15 +00:00
|
|
|
/* Before sending the packed, we first "bind" in order to be able
|
|
|
|
* to receive the response */
|
2013-02-20 15:29:28 +00:00
|
|
|
if (bind(sudp, (const struct sockaddr *)&sockudp_r,
|
2011-09-27 20:25:35 +00:00
|
|
|
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) != 0)
|
|
|
|
{
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SOCKET_ERROR;
|
2013-02-20 15:29:28 +00:00
|
|
|
PRINT_SOCKET_ERROR("bind");
|
2011-09-27 20:25:35 +00:00
|
|
|
closesocket(sudp);
|
|
|
|
return NULL;
|
2013-02-20 15:29:28 +00:00
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SUCCESS;
|
|
|
|
/* Calculating maximum response time in seconds */
|
|
|
|
mx = ((unsigned int)delay) / 1000u;
|
2013-02-20 16:08:07 +00:00
|
|
|
if(mx == 0) {
|
|
|
|
mx = 1;
|
|
|
|
delay = 1000;
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
/* receiving SSDP response packet */
|
2013-02-20 16:07:18 +00:00
|
|
|
for(deviceIndex = 0; deviceTypes[deviceIndex]; deviceIndex++) {
|
|
|
|
/* sending the SSDP M-SEARCH packet */
|
|
|
|
n = snprintf(bufr, sizeof(bufr),
|
|
|
|
MSearchMsgFmt,
|
|
|
|
ipv6 ?
|
|
|
|
(linklocal ? "[" UPNP_MCAST_LL_ADDR "]" : "[" UPNP_MCAST_SL_ADDR "]")
|
|
|
|
: UPNP_MCAST_ADDR,
|
|
|
|
deviceTypes[deviceIndex], mx);
|
2011-09-27 20:25:35 +00:00
|
|
|
#ifdef DEBUG
|
2013-02-20 16:07:18 +00:00
|
|
|
/*printf("Sending %s", bufr);*/
|
|
|
|
printf("Sending M-SEARCH request to %s with ST: %s\n",
|
|
|
|
ipv6 ?
|
|
|
|
(linklocal ? "[" UPNP_MCAST_LL_ADDR "]" : "[" UPNP_MCAST_SL_ADDR "]")
|
|
|
|
: UPNP_MCAST_ADDR,
|
|
|
|
deviceTypes[deviceIndex]);
|
2011-09-27 20:25:35 +00:00
|
|
|
#endif
|
|
|
|
#ifdef NO_GETADDRINFO
|
2013-02-20 16:07:18 +00:00
|
|
|
/* the following code is not using getaddrinfo */
|
|
|
|
/* emission */
|
|
|
|
memset(&sockudp_w, 0, sizeof(struct sockaddr_storage));
|
|
|
|
if(ipv6) {
|
|
|
|
struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockudp_w;
|
|
|
|
p->sin6_family = AF_INET6;
|
|
|
|
p->sin6_port = htons(PORT);
|
|
|
|
inet_pton(AF_INET6,
|
|
|
|
linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR,
|
|
|
|
&(p->sin6_addr));
|
|
|
|
} else {
|
|
|
|
struct sockaddr_in * p = (struct sockaddr_in *)&sockudp_w;
|
|
|
|
p->sin_family = AF_INET;
|
|
|
|
p->sin_port = htons(PORT);
|
|
|
|
p->sin_addr.s_addr = inet_addr(UPNP_MCAST_ADDR);
|
|
|
|
}
|
|
|
|
n = sendto(sudp, bufr, n, 0, &sockudp_w,
|
|
|
|
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
|
|
|
|
if (n < 0) {
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SOCKET_ERROR;
|
|
|
|
PRINT_SOCKET_ERROR("sendto");
|
|
|
|
break;
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
#else /* #ifdef NO_GETADDRINFO */
|
2013-02-20 16:07:18 +00:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC; /* AF_INET6 or AF_INET */
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
/*hints.ai_flags = */
|
|
|
|
if ((rv = getaddrinfo(ipv6
|
|
|
|
? (linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR)
|
|
|
|
: UPNP_MCAST_ADDR,
|
|
|
|
XSTR(PORT), &hints, &servinfo)) != 0) {
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SOCKET_ERROR;
|
2012-01-21 13:38:38 +00:00
|
|
|
#ifdef _WIN32
|
2013-02-20 16:07:18 +00:00
|
|
|
fprintf(stderr, "getaddrinfo() failed: %d\n", rv);
|
2011-09-27 20:25:35 +00:00
|
|
|
#else
|
2013-02-20 16:07:18 +00:00
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
2011-09-27 20:25:35 +00:00
|
|
|
#endif
|
2013-02-20 16:07:18 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(p = servinfo; p; p = p->ai_next) {
|
|
|
|
n = sendto(sudp, bufr, n, 0, p->ai_addr, p->ai_addrlen);
|
|
|
|
if (n < 0) {
|
2012-04-09 22:11:28 +00:00
|
|
|
#ifdef DEBUG
|
2013-02-20 16:07:18 +00:00
|
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
|
|
|
if (getnameinfo(p->ai_addr, p->ai_addrlen, hbuf, sizeof(hbuf), sbuf,
|
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
|
|
|
|
fprintf(stderr, "host:%s port:%s\n", hbuf, sbuf);
|
2013-02-20 15:29:28 +00:00
|
|
|
}
|
2013-02-20 16:07:18 +00:00
|
|
|
#endif
|
|
|
|
PRINT_SOCKET_ERROR("sendto");
|
|
|
|
continue;
|
2013-02-20 15:29:28 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-20 16:07:18 +00:00
|
|
|
freeaddrinfo(servinfo);
|
|
|
|
if(n < 0) {
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SOCKET_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* #ifdef NO_GETADDRINFO */
|
2015-05-22 10:44:38 +00:00
|
|
|
/* Waiting for SSDP REPLY packet to M-SEARCH
|
|
|
|
* if searchalltypes is set, enter the loop only
|
|
|
|
* when the last deviceType is reached */
|
|
|
|
if(!searchalltypes || !deviceTypes[deviceIndex + 1]) do {
|
2013-02-20 15:29:28 +00:00
|
|
|
n = receivedata(sudp, bufr, sizeof(bufr), delay, &scope_id);
|
|
|
|
if (n < 0) {
|
|
|
|
/* error */
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SOCKET_ERROR;
|
|
|
|
goto error;
|
|
|
|
} else if (n == 0) {
|
|
|
|
/* no data or Time Out */
|
2015-05-22 10:44:38 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("NODATA or TIMEOUT\n");
|
|
|
|
#endif /* DEBUG */
|
2015-05-22 10:25:51 +00:00
|
|
|
if (devlist && !searchalltypes) {
|
2013-02-20 16:07:18 +00:00
|
|
|
/* found some devices, stop now*/
|
2013-02-20 15:29:28 +00:00
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_SUCCESS;
|
|
|
|
goto error;
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
} else {
|
2013-02-20 15:29:28 +00:00
|
|
|
const char * descURL=NULL;
|
|
|
|
int urlsize=0;
|
|
|
|
const char * st=NULL;
|
|
|
|
int stsize=0;
|
2015-07-22 09:08:52 +00:00
|
|
|
const char * usn=NULL;
|
|
|
|
int usnsize=0;
|
|
|
|
parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize, &usn, &usnsize);
|
2013-02-20 15:29:28 +00:00
|
|
|
if(st&&descURL) {
|
2011-09-27 20:25:35 +00:00
|
|
|
#ifdef DEBUG
|
2015-07-22 09:08:52 +00:00
|
|
|
printf("M-SEARCH Reply:\n ST: %.*s\n USN: %.*s\n Location: %.*s\n",
|
2015-07-22 21:46:44 +00:00
|
|
|
stsize, st, usnsize, (usn?usn:""), urlsize, descURL);
|
2015-05-22 10:44:38 +00:00
|
|
|
#endif /* DEBUG */
|
2013-02-20 15:29:28 +00:00
|
|
|
for(tmp=devlist; tmp; tmp = tmp->pNext) {
|
|
|
|
if(memcmp(tmp->descURL, descURL, urlsize) == 0 &&
|
|
|
|
tmp->descURL[urlsize] == '\0' &&
|
|
|
|
memcmp(tmp->st, st, stsize) == 0 &&
|
2015-07-22 09:08:52 +00:00
|
|
|
tmp->st[stsize] == '\0' &&
|
2015-07-22 21:46:44 +00:00
|
|
|
(usnsize == 0 || memcmp(tmp->usn, usn, usnsize) == 0) &&
|
2015-07-22 09:08:52 +00:00
|
|
|
tmp->usn[usnsize] == '\0')
|
2013-02-20 15:29:28 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* at the exit of the loop above, tmp is null if
|
|
|
|
* no duplicate device was found */
|
|
|
|
if(tmp)
|
|
|
|
continue;
|
2015-07-22 09:08:52 +00:00
|
|
|
tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize+usnsize);
|
2013-02-20 15:29:28 +00:00
|
|
|
if(!tmp) {
|
|
|
|
/* memory allocation error */
|
|
|
|
if(error)
|
|
|
|
*error = UPNPDISCOVER_MEMORY_ERROR;
|
2013-02-20 16:07:18 +00:00
|
|
|
goto error;
|
2013-02-20 15:29:28 +00:00
|
|
|
}
|
|
|
|
tmp->pNext = devlist;
|
|
|
|
tmp->descURL = tmp->buffer;
|
|
|
|
tmp->st = tmp->buffer + 1 + urlsize;
|
2015-07-22 09:08:52 +00:00
|
|
|
tmp->usn = tmp->st + 1 + stsize;
|
2013-02-20 15:29:28 +00:00
|
|
|
memcpy(tmp->buffer, descURL, urlsize);
|
|
|
|
tmp->buffer[urlsize] = '\0';
|
2015-07-22 09:08:52 +00:00
|
|
|
memcpy(tmp->st, st, stsize);
|
2013-02-20 15:29:28 +00:00
|
|
|
tmp->buffer[urlsize+1+stsize] = '\0';
|
2015-07-22 21:46:44 +00:00
|
|
|
if(usn != NULL)
|
|
|
|
memcpy(tmp->usn, usn, usnsize);
|
2015-07-22 09:08:52 +00:00
|
|
|
tmp->buffer[urlsize+1+stsize+1+usnsize] = '\0';
|
2013-02-20 15:29:28 +00:00
|
|
|
tmp->scope_id = scope_id;
|
|
|
|
devlist = tmp;
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
2013-02-20 15:29:28 +00:00
|
|
|
} while(n > 0);
|
2015-05-22 10:44:38 +00:00
|
|
|
if(ipv6) {
|
|
|
|
/* switch linklocal flag */
|
|
|
|
if(linklocal) {
|
|
|
|
linklocal = 0;
|
|
|
|
--deviceIndex;
|
|
|
|
} else {
|
|
|
|
linklocal = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
2013-02-16 09:48:56 +00:00
|
|
|
error:
|
2011-09-27 20:25:35 +00:00
|
|
|
closesocket(sudp);
|
|
|
|
return devlist;
|
|
|
|
}
|
|
|
|
|
2014-01-31 13:59:21 +00:00
|
|
|
/* upnpDiscover() Discover IGD device */
|
2014-11-13 10:05:09 +00:00
|
|
|
MINIUPNP_LIBSPEC struct UPNPDev *
|
2013-02-17 23:46:09 +00:00
|
|
|
upnpDiscover(int delay, const char * multicastif,
|
|
|
|
const char * minissdpdsock, int sameport,
|
2015-07-23 20:44:37 +00:00
|
|
|
int ipv6, unsigned char ttl,
|
2013-02-17 23:46:09 +00:00
|
|
|
int * error)
|
|
|
|
{
|
|
|
|
static const char * const deviceList[] = {
|
|
|
|
#if 0
|
|
|
|
"urn:schemas-upnp-org:device:InternetGatewayDevice:2",
|
|
|
|
"urn:schemas-upnp-org:service:WANIPConnection:2",
|
|
|
|
#endif
|
|
|
|
"urn:schemas-upnp-org:device:InternetGatewayDevice:1",
|
|
|
|
"urn:schemas-upnp-org:service:WANIPConnection:1",
|
|
|
|
"urn:schemas-upnp-org:service:WANPPPConnection:1",
|
|
|
|
"upnp:rootdevice",
|
2013-02-20 16:04:53 +00:00
|
|
|
/*"ssdp:all",*/
|
|
|
|
0
|
|
|
|
};
|
|
|
|
return upnpDiscoverDevices(deviceList,
|
|
|
|
delay, multicastif, minissdpdsock, sameport,
|
2015-07-23 20:44:37 +00:00
|
|
|
ipv6, ttl, error, 0);
|
2013-02-20 16:04:53 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 13:59:21 +00:00
|
|
|
/* upnpDiscoverAll() Discover all UPnP devices */
|
2014-11-13 10:05:09 +00:00
|
|
|
MINIUPNP_LIBSPEC struct UPNPDev *
|
2013-02-20 16:04:53 +00:00
|
|
|
upnpDiscoverAll(int delay, const char * multicastif,
|
|
|
|
const char * minissdpdsock, int sameport,
|
2015-07-23 20:44:37 +00:00
|
|
|
int ipv6, unsigned char ttl,
|
2013-02-20 16:04:53 +00:00
|
|
|
int * error)
|
|
|
|
{
|
|
|
|
static const char * const deviceList[] = {
|
|
|
|
/*"upnp:rootdevice",*/
|
2013-02-17 23:46:09 +00:00
|
|
|
"ssdp:all",
|
|
|
|
0
|
|
|
|
};
|
|
|
|
return upnpDiscoverDevices(deviceList,
|
|
|
|
delay, multicastif, minissdpdsock, sameport,
|
2015-07-23 20:44:37 +00:00
|
|
|
ipv6, ttl, error, 0);
|
2013-02-17 23:46:09 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 13:59:21 +00:00
|
|
|
/* upnpDiscoverDevice() Discover a specific device */
|
2014-11-13 10:05:09 +00:00
|
|
|
MINIUPNP_LIBSPEC struct UPNPDev *
|
2014-01-31 13:59:21 +00:00
|
|
|
upnpDiscoverDevice(const char * device, int delay, const char * multicastif,
|
|
|
|
const char * minissdpdsock, int sameport,
|
2015-07-23 20:44:37 +00:00
|
|
|
int ipv6, unsigned char ttl,
|
2014-01-31 13:59:21 +00:00
|
|
|
int * error)
|
|
|
|
{
|
|
|
|
const char * const deviceList[] = {
|
|
|
|
device,
|
|
|
|
0
|
|
|
|
};
|
|
|
|
return upnpDiscoverDevices(deviceList,
|
|
|
|
delay, multicastif, minissdpdsock, sameport,
|
2015-07-23 20:44:37 +00:00
|
|
|
ipv6, ttl, error, 0);
|
2014-01-31 13:59:21 +00:00
|
|
|
}
|
|
|
|
|
2011-09-27 20:25:35 +00:00
|
|
|
/* freeUPNPDevlist() should be used to
|
|
|
|
* free the chained list returned by upnpDiscover() */
|
2014-06-01 10:35:13 +00:00
|
|
|
MINIUPNP_LIBSPEC void freeUPNPDevlist(struct UPNPDev * devlist)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
|
|
|
struct UPNPDev * next;
|
|
|
|
while(devlist)
|
|
|
|
{
|
|
|
|
next = devlist->pNext;
|
|
|
|
free(devlist);
|
|
|
|
devlist = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 06:09:42 +00:00
|
|
|
static char *
|
|
|
|
build_absolute_url(const char * baseurl, const char * descURL,
|
|
|
|
const char * url, unsigned int scope_id)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
2014-11-05 06:09:42 +00:00
|
|
|
int l, n;
|
|
|
|
char * s;
|
|
|
|
const char * base;
|
2011-09-27 20:25:35 +00:00
|
|
|
char * p;
|
2014-11-27 11:59:12 +00:00
|
|
|
#if defined(IF_NAMESIZE) && !defined(_WIN32)
|
2012-06-28 19:05:50 +00:00
|
|
|
char ifname[IF_NAMESIZE];
|
2014-11-27 11:59:12 +00:00
|
|
|
#else /* defined(IF_NAMESIZE) && !defined(_WIN32) */
|
2012-08-29 07:58:39 +00:00
|
|
|
char scope_str[8];
|
2014-11-27 11:59:12 +00:00
|
|
|
#endif /* defined(IF_NAMESIZE) && !defined(_WIN32) */
|
2011-09-27 20:25:35 +00:00
|
|
|
|
2014-11-05 06:09:42 +00:00
|
|
|
if( (url[0] == 'h')
|
|
|
|
&&(url[1] == 't')
|
|
|
|
&&(url[2] == 't')
|
|
|
|
&&(url[3] == 'p')
|
|
|
|
&&(url[4] == ':')
|
|
|
|
&&(url[5] == '/')
|
|
|
|
&&(url[6] == '/'))
|
|
|
|
return strdup(url);
|
|
|
|
base = (baseurl[0] == '\0') ? descURL : baseurl;
|
|
|
|
n = strlen(base);
|
|
|
|
if(n > 7) {
|
|
|
|
p = strchr(base + 7, '/');
|
|
|
|
if(p)
|
|
|
|
n = p - base;
|
|
|
|
}
|
|
|
|
l = n + strlen(url) + 1;
|
|
|
|
if(url[0] != '/')
|
|
|
|
l++;
|
2012-06-28 19:05:50 +00:00
|
|
|
if(scope_id != 0) {
|
2014-11-27 11:59:12 +00:00
|
|
|
#if defined(IF_NAMESIZE) && !defined(_WIN32)
|
2012-06-28 19:05:50 +00:00
|
|
|
if(if_indextoname(scope_id, ifname)) {
|
2014-11-05 06:09:42 +00:00
|
|
|
l += 3 + strlen(ifname); /* 3 == strlen(%25) */
|
2012-06-28 19:05:50 +00:00
|
|
|
}
|
2014-11-27 11:59:12 +00:00
|
|
|
#else /* defined(IF_NAMESIZE) && !defined(_WIN32) */
|
2014-11-05 06:09:42 +00:00
|
|
|
/* under windows, scope is numerical */
|
|
|
|
l += 3 + snprintf(scope_str, sizeof(scope_str), "%u", scope_id);
|
2014-11-27 11:59:12 +00:00
|
|
|
#endif /* defined(IF_NAMESIZE) && !defined(_WIN32) */
|
2012-06-28 19:05:50 +00:00
|
|
|
}
|
2014-11-05 06:09:42 +00:00
|
|
|
s = malloc(l);
|
|
|
|
if(s == NULL) return NULL;
|
|
|
|
memcpy(s, base, n);
|
2012-06-28 19:05:50 +00:00
|
|
|
if(scope_id != 0) {
|
2014-11-05 06:09:42 +00:00
|
|
|
s[n] = '\0';
|
|
|
|
if(0 == memcmp(s, "http://[fe80:", 13)) {
|
2012-06-28 19:05:50 +00:00
|
|
|
/* this is a linklocal IPv6 address */
|
2014-11-05 06:09:42 +00:00
|
|
|
p = strchr(s, ']');
|
2012-06-28 19:05:50 +00:00
|
|
|
if(p) {
|
|
|
|
/* insert %25<scope> into URL */
|
2014-12-01 09:31:50 +00:00
|
|
|
#if defined(IF_NAMESIZE) && !defined(_WIN32)
|
2012-06-28 19:05:50 +00:00
|
|
|
memmove(p + 3 + strlen(ifname), p, strlen(p) + 1);
|
|
|
|
memcpy(p, "%25", 3);
|
|
|
|
memcpy(p + 3, ifname, strlen(ifname));
|
2014-11-05 06:09:42 +00:00
|
|
|
n += 3 + strlen(ifname);
|
2014-12-01 09:31:50 +00:00
|
|
|
#else /* defined(IF_NAMESIZE) && !defined(_WIN32) */
|
2012-08-29 07:58:39 +00:00
|
|
|
memmove(p + 3 + strlen(scope_str), p, strlen(p) + 1);
|
|
|
|
memcpy(p, "%25", 3);
|
|
|
|
memcpy(p + 3, scope_str, strlen(scope_str));
|
2014-11-05 06:09:42 +00:00
|
|
|
n += 3 + strlen(scope_str);
|
2014-12-01 09:31:50 +00:00
|
|
|
#endif /* defined(IF_NAMESIZE) && !defined(_WIN32) */
|
2012-06-28 19:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-11-05 06:09:42 +00:00
|
|
|
if(url[0] != '/')
|
|
|
|
s[n++] = '/';
|
|
|
|
memcpy(s + n, url, l - n);
|
|
|
|
return s;
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
|
2014-11-05 06:09:42 +00:00
|
|
|
/* Prepare the Urls for usage...
|
|
|
|
*/
|
|
|
|
MINIUPNP_LIBSPEC void
|
|
|
|
GetUPNPUrls(struct UPNPUrls * urls, struct IGDdatas * data,
|
|
|
|
const char * descURL, unsigned int scope_id)
|
|
|
|
{
|
|
|
|
/* strdup descURL */
|
|
|
|
urls->rootdescURL = strdup(descURL);
|
2011-09-27 20:25:35 +00:00
|
|
|
|
2014-11-05 06:09:42 +00:00
|
|
|
/* get description of WANIPConnection */
|
|
|
|
urls->ipcondescURL = build_absolute_url(data->urlbase, descURL,
|
|
|
|
data->first.scpdurl, scope_id);
|
|
|
|
urls->controlURL = build_absolute_url(data->urlbase, descURL,
|
|
|
|
data->first.controlurl, scope_id);
|
|
|
|
urls->controlURL_CIF = build_absolute_url(data->urlbase, descURL,
|
|
|
|
data->CIF.controlurl, scope_id);
|
|
|
|
urls->controlURL_6FC = build_absolute_url(data->urlbase, descURL,
|
|
|
|
data->IPv6FC.controlurl, scope_id);
|
2011-09-27 20:25:35 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
2014-11-05 06:09:42 +00:00
|
|
|
printf("urls->ipcondescURL='%s'\n", urls->ipcondescURL);
|
|
|
|
printf("urls->controlURL='%s'\n", urls->controlURL);
|
|
|
|
printf("urls->controlURL_CIF='%s'\n", urls->controlURL_CIF);
|
|
|
|
printf("urls->controlURL_6FC='%s'\n", urls->controlURL_6FC);
|
2011-09-27 20:25:35 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-06-01 10:35:13 +00:00
|
|
|
MINIUPNP_LIBSPEC void
|
2011-09-27 20:25:35 +00:00
|
|
|
FreeUPNPUrls(struct UPNPUrls * urls)
|
|
|
|
{
|
|
|
|
if(!urls)
|
|
|
|
return;
|
|
|
|
free(urls->controlURL);
|
|
|
|
urls->controlURL = 0;
|
|
|
|
free(urls->ipcondescURL);
|
|
|
|
urls->ipcondescURL = 0;
|
|
|
|
free(urls->controlURL_CIF);
|
|
|
|
urls->controlURL_CIF = 0;
|
|
|
|
free(urls->controlURL_6FC);
|
|
|
|
urls->controlURL_6FC = 0;
|
|
|
|
free(urls->rootdescURL);
|
|
|
|
urls->rootdescURL = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
UPNPIGD_IsConnected(struct UPNPUrls * urls, struct IGDdatas * data)
|
|
|
|
{
|
|
|
|
char status[64];
|
|
|
|
unsigned int uptime;
|
|
|
|
status[0] = '\0';
|
|
|
|
UPNP_GetStatusInfo(urls->controlURL, data->first.servicetype,
|
|
|
|
status, &uptime, NULL);
|
|
|
|
if(0 == strcmp("Connected", status))
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* UPNP_GetValidIGD() :
|
|
|
|
* return values :
|
2012-04-09 22:10:52 +00:00
|
|
|
* -1 = Internal error
|
2011-09-27 20:25:35 +00:00
|
|
|
* 0 = NO IGD found
|
|
|
|
* 1 = A valid connected IGD has been found
|
|
|
|
* 2 = A valid IGD has been found but it reported as
|
|
|
|
* not connected
|
|
|
|
* 3 = an UPnP device has been found but was not recognized as an IGD
|
|
|
|
*
|
2013-12-09 08:29:08 +00:00
|
|
|
* In any positive non zero return case, the urls and data structures
|
2011-09-27 20:25:35 +00:00
|
|
|
* passed as parameters are set. Donc forget to call FreeUPNPUrls(urls) to
|
|
|
|
* free allocated memory.
|
|
|
|
*/
|
2014-06-01 10:35:13 +00:00
|
|
|
MINIUPNP_LIBSPEC int
|
2011-09-27 20:25:35 +00:00
|
|
|
UPNP_GetValidIGD(struct UPNPDev * devlist,
|
|
|
|
struct UPNPUrls * urls,
|
|
|
|
struct IGDdatas * data,
|
|
|
|
char * lanaddr, int lanaddrlen)
|
|
|
|
{
|
2012-04-09 22:10:52 +00:00
|
|
|
struct xml_desc {
|
|
|
|
char * xml;
|
|
|
|
int size;
|
2013-12-09 08:29:08 +00:00
|
|
|
int is_igd;
|
2012-04-09 22:10:52 +00:00
|
|
|
} * desc = NULL;
|
2011-09-27 20:25:35 +00:00
|
|
|
struct UPNPDev * dev;
|
|
|
|
int ndev = 0;
|
2012-04-09 22:10:52 +00:00
|
|
|
int i;
|
|
|
|
int state = -1; /* state 1 : IGD connected. State 2 : IGD. State 3 : anything */
|
2013-12-09 08:29:08 +00:00
|
|
|
int n_igd = 0;
|
|
|
|
char extIpAddr[16];
|
2011-09-27 20:25:35 +00:00
|
|
|
if(!devlist)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Empty devlist\n");
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
2013-12-09 08:29:08 +00:00
|
|
|
/* counting total number of devices in the list */
|
2012-04-09 22:10:52 +00:00
|
|
|
for(dev = devlist; dev; dev = dev->pNext)
|
|
|
|
ndev++;
|
|
|
|
if(ndev > 0)
|
|
|
|
{
|
|
|
|
desc = calloc(ndev, sizeof(struct xml_desc));
|
|
|
|
if(!desc)
|
|
|
|
return -1; /* memory allocation error */
|
|
|
|
}
|
2013-12-09 08:29:08 +00:00
|
|
|
/* Step 1 : downloading descriptions and testing type */
|
|
|
|
for(dev = devlist, i = 0; dev; dev = dev->pNext, i++)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
2013-12-09 08:29:08 +00:00
|
|
|
/* we should choose an internet gateway device.
|
|
|
|
* with st == urn:schemas-upnp-org:device:InternetGatewayDevice:1 */
|
|
|
|
desc[i].xml = miniwget_getaddr(dev->descURL, &(desc[i].size),
|
|
|
|
lanaddr, lanaddrlen,
|
|
|
|
dev->scope_id);
|
2012-04-09 22:10:52 +00:00
|
|
|
#ifdef DEBUG
|
2013-12-09 08:29:08 +00:00
|
|
|
if(!desc[i].xml)
|
|
|
|
{
|
|
|
|
printf("error getting XML description %s\n", dev->descURL);
|
|
|
|
}
|
2012-04-09 22:10:52 +00:00
|
|
|
#endif
|
2013-12-09 08:29:08 +00:00
|
|
|
if(desc[i].xml)
|
|
|
|
{
|
|
|
|
memset(data, 0, sizeof(struct IGDdatas));
|
|
|
|
memset(urls, 0, sizeof(struct UPNPUrls));
|
|
|
|
parserootdesc(desc[i].xml, desc[i].size, data);
|
2014-11-17 23:01:48 +00:00
|
|
|
if(COMPARE(data->CIF.servicetype,
|
|
|
|
"urn:schemas-upnp-org:service:WANCommonInterfaceConfig:"))
|
2013-12-09 08:29:08 +00:00
|
|
|
{
|
|
|
|
desc[i].is_igd = 1;
|
|
|
|
n_igd++;
|
2012-04-09 22:10:52 +00:00
|
|
|
}
|
2013-12-09 08:29:08 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* iterate the list to find a device depending on state */
|
|
|
|
for(state = 1; state <= 3; state++)
|
|
|
|
{
|
|
|
|
for(dev = devlist, i = 0; dev; dev = dev->pNext, i++)
|
|
|
|
{
|
2012-04-09 22:10:52 +00:00
|
|
|
if(desc[i].xml)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
|
|
|
memset(data, 0, sizeof(struct IGDdatas));
|
|
|
|
memset(urls, 0, sizeof(struct UPNPUrls));
|
2012-04-09 22:10:52 +00:00
|
|
|
parserootdesc(desc[i].xml, desc[i].size, data);
|
2013-12-09 08:29:08 +00:00
|
|
|
if(desc[i].is_igd || state >= 3 )
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
2012-06-28 19:05:50 +00:00
|
|
|
GetUPNPUrls(urls, data, dev->descURL, dev->scope_id);
|
2011-09-27 20:25:35 +00:00
|
|
|
|
2013-12-09 08:29:08 +00:00
|
|
|
/* in state 2 and 3 we dont test if device is connected ! */
|
|
|
|
if(state >= 2)
|
|
|
|
goto free_and_return;
|
2011-09-27 20:25:35 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("UPNPIGD_IsConnected(%s) = %d\n",
|
|
|
|
urls->controlURL,
|
|
|
|
UPNPIGD_IsConnected(urls, data));
|
|
|
|
#endif
|
2013-12-09 08:29:08 +00:00
|
|
|
/* checks that status is connected AND there is a external IP address assigned */
|
|
|
|
if(UPNPIGD_IsConnected(urls, data)
|
|
|
|
&& (UPNP_GetExternalIPAddress(urls->controlURL, data->first.servicetype, extIpAddr) == 0))
|
2012-04-09 22:10:52 +00:00
|
|
|
goto free_and_return;
|
2011-09-27 20:25:35 +00:00
|
|
|
FreeUPNPUrls(urls);
|
|
|
|
if(data->second.servicetype[0] != '\0') {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("We tried %s, now we try %s !\n",
|
|
|
|
data->first.servicetype, data->second.servicetype);
|
|
|
|
#endif
|
|
|
|
/* swaping WANPPPConnection and WANIPConnection ! */
|
|
|
|
memcpy(&data->tmp, &data->first, sizeof(struct IGDdatas_service));
|
|
|
|
memcpy(&data->first, &data->second, sizeof(struct IGDdatas_service));
|
|
|
|
memcpy(&data->second, &data->tmp, sizeof(struct IGDdatas_service));
|
2012-06-28 19:05:50 +00:00
|
|
|
GetUPNPUrls(urls, data, dev->descURL, dev->scope_id);
|
2011-09-27 20:25:35 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("UPNPIGD_IsConnected(%s) = %d\n",
|
|
|
|
urls->controlURL,
|
|
|
|
UPNPIGD_IsConnected(urls, data));
|
|
|
|
#endif
|
2013-12-09 08:29:08 +00:00
|
|
|
if(UPNPIGD_IsConnected(urls, data)
|
|
|
|
&& (UPNP_GetExternalIPAddress(urls->controlURL, data->first.servicetype, extIpAddr) == 0))
|
2012-04-09 22:10:52 +00:00
|
|
|
goto free_and_return;
|
2011-09-27 20:25:35 +00:00
|
|
|
FreeUPNPUrls(urls);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
memset(data, 0, sizeof(struct IGDdatas));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-04-09 22:10:52 +00:00
|
|
|
state = 0;
|
|
|
|
free_and_return:
|
2012-08-29 07:57:38 +00:00
|
|
|
if(desc) {
|
|
|
|
for(i = 0; i < ndev; i++) {
|
|
|
|
if(desc[i].xml) {
|
|
|
|
free(desc[i].xml);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(desc);
|
|
|
|
}
|
2012-04-09 22:10:52 +00:00
|
|
|
return state;
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* UPNP_GetIGDFromUrl()
|
|
|
|
* Used when skipping the discovery process.
|
|
|
|
* return value :
|
|
|
|
* 0 - Not ok
|
|
|
|
* 1 - OK */
|
|
|
|
int
|
|
|
|
UPNP_GetIGDFromUrl(const char * rootdescurl,
|
|
|
|
struct UPNPUrls * urls,
|
|
|
|
struct IGDdatas * data,
|
|
|
|
char * lanaddr, int lanaddrlen)
|
|
|
|
{
|
|
|
|
char * descXML;
|
|
|
|
int descXMLsize = 0;
|
|
|
|
descXML = miniwget_getaddr(rootdescurl, &descXMLsize,
|
2012-06-23 22:55:31 +00:00
|
|
|
lanaddr, lanaddrlen, 0);
|
2011-09-27 20:25:35 +00:00
|
|
|
if(descXML) {
|
|
|
|
memset(data, 0, sizeof(struct IGDdatas));
|
|
|
|
memset(urls, 0, sizeof(struct UPNPUrls));
|
|
|
|
parserootdesc(descXML, descXMLsize, data);
|
|
|
|
free(descXML);
|
|
|
|
descXML = NULL;
|
2012-06-28 19:05:50 +00:00
|
|
|
GetUPNPUrls(urls, data, rootdescurl, 0);
|
2011-09-27 20:25:35 +00:00
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|