2016-10-07 09:16:03 +00:00
|
|
|
/* $Id: minissdpc.c,v 1.32 2016/10/07 09:04:36 nanard Exp $ */
|
2016-01-17 17:49:41 +00:00
|
|
|
/* vim: tabstop=4 shiftwidth=4 noexpandtab
|
|
|
|
* 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
|
2019-01-07 08:35:21 +00:00
|
|
|
* copyright (c) 2005-2019 Thomas Bernard
|
2011-09-27 20:25:35 +00:00
|
|
|
* This software is subjet to the conditions detailed in the
|
|
|
|
* provided LICENCE file. */
|
|
|
|
/*#include <syslog.h>*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2016-09-03 07:34:15 +00:00
|
|
|
#if defined (__NetBSD__)
|
|
|
|
#include <net/if.h>
|
|
|
|
#endif
|
2012-01-21 13:38:38 +00:00
|
|
|
#if defined(_WIN32) || defined(__amigaos__) || defined(__amigaos4__)
|
|
|
|
#ifdef _WIN32
|
2011-09-27 20:25:35 +00:00
|
|
|
#include <winsock2.h>
|
|
|
|
#include <ws2tcpip.h>
|
|
|
|
#include <io.h>
|
2015-09-15 22:19:43 +00:00
|
|
|
#include <iphlpapi.h>
|
2015-09-18 16:11:56 +00:00
|
|
|
#define snprintf _snprintf
|
|
|
|
#if !defined(_MSC_VER)
|
2011-09-27 20:25:35 +00:00
|
|
|
#include <stdint.h>
|
2015-09-18 16:11:56 +00:00
|
|
|
#else /* !defined(_MSC_VER) */
|
|
|
|
typedef unsigned short uint16_t;
|
|
|
|
#endif /* !defined(_MSC_VER) */
|
2015-08-28 12:29:20 +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 */
|
2015-08-28 12:32:14 +00:00
|
|
|
#endif /* _WIN32 */
|
2011-09-27 20:25:35 +00:00
|
|
|
#if defined(__amigaos__) || defined(__amigaos4__)
|
|
|
|
#include <sys/socket.h>
|
2015-08-28 12:32:14 +00:00
|
|
|
#endif /* defined(__amigaos__) || defined(__amigaos4__) */
|
2011-09-27 20:25:35 +00:00
|
|
|
#if defined(__amigaos__)
|
|
|
|
#define uint16_t unsigned short
|
2015-08-28 12:32:14 +00:00
|
|
|
#endif /* defined(__amigaos__) */
|
2011-09-27 20:25:35 +00:00
|
|
|
/* Hack */
|
|
|
|
#define UNIX_PATH_LEN 108
|
|
|
|
struct sockaddr_un {
|
|
|
|
uint16_t sun_family;
|
|
|
|
char sun_path[UNIX_PATH_LEN];
|
|
|
|
};
|
2015-08-28 12:32:14 +00:00
|
|
|
#else /* defined(_WIN32) || defined(__amigaos__) || defined(__amigaos4__) */
|
2015-09-18 16:11:56 +00:00
|
|
|
#include <strings.h>
|
|
|
|
#include <unistd.h>
|
2011-09-27 20:25:35 +00:00
|
|
|
#include <sys/socket.h>
|
2015-08-28 12:32:14 +00:00
|
|
|
#include <sys/param.h>
|
2015-08-29 09:05:32 +00:00
|
|
|
#include <sys/time.h>
|
2011-09-27 20:25:35 +00:00
|
|
|
#include <sys/un.h>
|
2015-08-03 16:49:55 +00:00
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#define closesocket close
|
|
|
|
#endif
|
|
|
|
|
2018-04-06 10:21:36 +00:00
|
|
|
#include "miniupnpc_socketdef.h"
|
2015-08-03 16:49:55 +00:00
|
|
|
|
|
|
|
#if !defined(__DragonFly__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__APPLE__) && !defined(_WIN32) && !defined(__CYGWIN__) && !defined(__sun) && !defined(__GNU__) && !defined(__FreeBSD_kernel__)
|
|
|
|
#define HAS_IP_MREQN
|
|
|
|
#endif
|
|
|
|
|
2016-01-17 18:32:05 +00:00
|
|
|
#if !defined(HAS_IP_MREQN) && !defined(_WIN32)
|
2016-01-17 17:49:41 +00:00
|
|
|
#include <sys/ioctl.h>
|
2016-11-10 20:55:33 +00:00
|
|
|
#if defined(__sun)
|
|
|
|
#include <sys/sockio.h>
|
|
|
|
#endif
|
2016-01-17 17:49:41 +00:00
|
|
|
#endif
|
|
|
|
|
2015-08-03 16:49:55 +00:00
|
|
|
#if defined(HAS_IP_MREQN) && defined(NEED_STRUCT_IP_MREQN)
|
|
|
|
/* Several versions of glibc don't define this structure,
|
|
|
|
* define it here and compile with CFLAGS NEED_STRUCT_IP_MREQN */
|
|
|
|
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
|
|
|
|
|
|
|
|
#if defined(__amigaos__) || defined(__amigaos4__)
|
|
|
|
/* Amiga OS specific stuff */
|
|
|
|
#define TIMEVAL struct timeval
|
2011-09-27 20:25:35 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "minissdpc.h"
|
|
|
|
#include "miniupnpc.h"
|
2015-09-18 16:11:56 +00:00
|
|
|
#include "receivedata.h"
|
|
|
|
|
|
|
|
#if !(defined(_WIN32) || defined(__amigaos__) || defined(__amigaos4__))
|
2011-09-27 20:25:35 +00:00
|
|
|
|
|
|
|
#include "codelength.h"
|
|
|
|
|
|
|
|
struct UPNPDev *
|
2015-07-23 13:22:23 +00:00
|
|
|
getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath, int * error)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
|
|
|
struct UPNPDev * devlist = NULL;
|
|
|
|
int s;
|
2015-07-23 13:22:23 +00:00
|
|
|
int res;
|
|
|
|
|
|
|
|
s = connectToMiniSSDPD(socketpath);
|
|
|
|
if (s < 0) {
|
|
|
|
if (error)
|
|
|
|
*error = s;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
res = requestDevicesFromMiniSSDPD(s, devtype);
|
|
|
|
if (res < 0) {
|
|
|
|
if (error)
|
|
|
|
*error = res;
|
2015-08-27 08:03:22 +00:00
|
|
|
} else {
|
|
|
|
devlist = receiveDevicesFromMiniSSDPD(s, error);
|
2015-07-23 13:22:23 +00:00
|
|
|
}
|
|
|
|
disconnectFromMiniSSDPD(s);
|
|
|
|
return devlist;
|
|
|
|
}
|
2015-06-16 15:33:59 +00:00
|
|
|
|
|
|
|
/* macros used to read from unix socket */
|
|
|
|
#define READ_BYTE_BUFFER(c) \
|
2015-09-16 12:55:42 +00:00
|
|
|
if((int)bufferindex >= n) { \
|
2015-06-16 15:33:59 +00:00
|
|
|
n = read(s, buffer, sizeof(buffer)); \
|
|
|
|
if(n<=0) break; \
|
|
|
|
bufferindex = 0; \
|
|
|
|
} \
|
|
|
|
c = buffer[bufferindex++];
|
|
|
|
|
|
|
|
#ifndef MIN
|
|
|
|
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
|
|
|
|
#endif /* MIN */
|
|
|
|
|
|
|
|
#define READ_COPY_BUFFER(dst, len) \
|
|
|
|
for(l = len, p = (unsigned char *)dst; l > 0; ) { \
|
|
|
|
unsigned int lcopy; \
|
2015-09-16 12:55:42 +00:00
|
|
|
if((int)bufferindex >= n) { \
|
2015-06-16 15:33:59 +00:00
|
|
|
n = read(s, buffer, sizeof(buffer)); \
|
|
|
|
if(n<=0) break; \
|
|
|
|
bufferindex = 0; \
|
|
|
|
} \
|
|
|
|
lcopy = MIN(l, (n - bufferindex)); \
|
|
|
|
memcpy(p, buffer + bufferindex, lcopy); \
|
|
|
|
l -= lcopy; \
|
|
|
|
p += lcopy; \
|
|
|
|
bufferindex += lcopy; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define READ_DISCARD_BUFFER(len) \
|
|
|
|
for(l = len; l > 0; ) { \
|
|
|
|
unsigned int lcopy; \
|
|
|
|
if(bufferindex >= n) { \
|
|
|
|
n = read(s, buffer, sizeof(buffer)); \
|
|
|
|
if(n<=0) break; \
|
|
|
|
bufferindex = 0; \
|
|
|
|
} \
|
|
|
|
lcopy = MIN(l, (n - bufferindex)); \
|
|
|
|
l -= lcopy; \
|
|
|
|
bufferindex += lcopy; \
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
|
2015-07-23 13:22:23 +00:00
|
|
|
int
|
|
|
|
connectToMiniSSDPD(const char * socketpath)
|
|
|
|
{
|
2011-09-27 20:25:35 +00:00
|
|
|
int s;
|
|
|
|
struct sockaddr_un addr;
|
2016-11-11 15:14:08 +00:00
|
|
|
#if defined(MINIUPNPC_SET_SOCKET_TIMEOUT) && !defined(__sun)
|
2015-07-23 13:22:23 +00:00
|
|
|
struct timeval timeout;
|
|
|
|
#endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
|
2011-09-27 20:25:35 +00:00
|
|
|
|
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if(s < 0)
|
|
|
|
{
|
|
|
|
/*syslog(LOG_ERR, "socket(unix): %m");*/
|
|
|
|
perror("socket(unix)");
|
2015-07-23 13:22:23 +00:00
|
|
|
return MINISSDPC_SOCKET_ERROR;
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
2016-11-11 15:14:08 +00:00
|
|
|
#if defined(MINIUPNPC_SET_SOCKET_TIMEOUT) && !defined(__sun)
|
2015-06-16 15:33:59 +00:00
|
|
|
/* setting a 3 seconds timeout */
|
2016-11-11 15:14:08 +00:00
|
|
|
/* not supported for AF_UNIX sockets under Solaris */
|
2015-06-16 15:33:59 +00:00
|
|
|
timeout.tv_sec = 3;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
|
|
|
|
{
|
2016-11-11 15:13:48 +00:00
|
|
|
perror("setsockopt SO_RCVTIMEO unix");
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
|
|
|
timeout.tv_sec = 3;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0)
|
|
|
|
{
|
2016-11-11 15:13:48 +00:00
|
|
|
perror("setsockopt SO_SNDTIMEO unix");
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
|
|
|
#endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
|
2015-07-23 13:22:54 +00:00
|
|
|
if(!socketpath)
|
|
|
|
socketpath = "/var/run/minissdpd.sock";
|
2017-01-09 09:16:29 +00:00
|
|
|
memset(&addr, 0, sizeof(addr));
|
2011-09-27 20:25:35 +00:00
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, socketpath, sizeof(addr.sun_path));
|
|
|
|
/* TODO : check if we need to handle the EINTR */
|
|
|
|
if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
|
|
|
|
{
|
|
|
|
/*syslog(LOG_WARNING, "connect(\"%s\"): %m", socketpath);*/
|
|
|
|
close(s);
|
2015-07-23 13:22:23 +00:00
|
|
|
return MINISSDPC_SOCKET_ERROR;
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
2015-07-23 13:22:23 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
disconnectFromMiniSSDPD(int s)
|
|
|
|
{
|
|
|
|
if (close(s) < 0)
|
|
|
|
return MINISSDPC_SOCKET_ERROR;
|
|
|
|
return MINISSDPC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
requestDevicesFromMiniSSDPD(int s, const char * devtype)
|
|
|
|
{
|
|
|
|
unsigned char buffer[256];
|
|
|
|
unsigned char * p;
|
|
|
|
unsigned int stsize, l;
|
|
|
|
|
2011-09-27 20:25:35 +00:00
|
|
|
stsize = strlen(devtype);
|
2014-02-03 15:40:42 +00:00
|
|
|
if(stsize == 8 && 0 == memcmp(devtype, "ssdp:all", 8))
|
|
|
|
{
|
|
|
|
buffer[0] = 3; /* request type 3 : everything */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer[0] = 1; /* request type 1 : request devices/services by type */
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
p = buffer + 1;
|
|
|
|
l = stsize; CODELENGTH(l, p);
|
|
|
|
if(p + stsize > buffer + sizeof(buffer))
|
|
|
|
{
|
|
|
|
/* devtype is too long ! */
|
2015-06-16 15:33:59 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
fprintf(stderr, "devtype is too long ! stsize=%u sizeof(buffer)=%u\n",
|
|
|
|
stsize, (unsigned)sizeof(buffer));
|
|
|
|
#endif /* DEBUG */
|
2015-07-23 13:22:23 +00:00
|
|
|
return MINISSDPC_INVALID_INPUT;
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
|
|
|
memcpy(p, devtype, stsize);
|
|
|
|
p += stsize;
|
|
|
|
if(write(s, buffer, p - buffer) < 0)
|
|
|
|
{
|
|
|
|
/*syslog(LOG_ERR, "write(): %m");*/
|
|
|
|
perror("minissdpc.c: write()");
|
2015-07-23 13:22:23 +00:00
|
|
|
return MINISSDPC_SOCKET_ERROR;
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
2015-07-23 13:22:23 +00:00
|
|
|
return MINISSDPC_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct UPNPDev *
|
|
|
|
receiveDevicesFromMiniSSDPD(int s, int * error)
|
|
|
|
{
|
|
|
|
struct UPNPDev * tmp;
|
|
|
|
struct UPNPDev * devlist = NULL;
|
|
|
|
unsigned char buffer[256];
|
|
|
|
ssize_t n;
|
|
|
|
unsigned char * p;
|
|
|
|
unsigned char * url;
|
|
|
|
unsigned char * st;
|
|
|
|
unsigned int bufferindex;
|
|
|
|
unsigned int i, ndev;
|
|
|
|
unsigned int urlsize, stsize, usnsize, l;
|
|
|
|
|
2011-09-27 20:25:35 +00:00
|
|
|
n = read(s, buffer, sizeof(buffer));
|
|
|
|
if(n<=0)
|
|
|
|
{
|
|
|
|
perror("minissdpc.c: read()");
|
2015-07-23 13:22:23 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_SOCKET_ERROR;
|
2011-09-27 20:25:35 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2015-06-11 12:28:35 +00:00
|
|
|
ndev = buffer[0];
|
2015-06-16 15:33:59 +00:00
|
|
|
bufferindex = 1;
|
2015-06-11 12:28:35 +00:00
|
|
|
for(i = 0; i < ndev; i++)
|
2011-09-27 20:25:35 +00:00
|
|
|
{
|
2015-06-16 15:33:59 +00:00
|
|
|
DECODELENGTH_READ(urlsize, READ_BYTE_BUFFER);
|
|
|
|
if(n<=0) {
|
2015-07-23 13:23:39 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_INVALID_SERVER_REPLY;
|
|
|
|
return devlist;
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" urlsize=%u", urlsize);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
url = malloc(urlsize);
|
|
|
|
if(url == NULL) {
|
2015-07-23 13:23:39 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_MEMORY_ERROR;
|
|
|
|
return devlist;
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
|
|
|
READ_COPY_BUFFER(url, urlsize);
|
|
|
|
if(n<=0) {
|
2015-07-23 13:23:39 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_INVALID_SERVER_REPLY;
|
|
|
|
goto free_url_and_return;
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
|
|
|
DECODELENGTH_READ(stsize, READ_BYTE_BUFFER);
|
|
|
|
if(n<=0) {
|
2015-07-23 13:23:39 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_INVALID_SERVER_REPLY;
|
|
|
|
goto free_url_and_return;
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" stsize=%u", stsize);
|
|
|
|
#endif /* DEBUG */
|
2015-07-22 09:08:52 +00:00
|
|
|
st = malloc(stsize);
|
|
|
|
if (st == NULL) {
|
2015-07-23 13:23:39 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_MEMORY_ERROR;
|
|
|
|
goto free_url_and_return;
|
2015-07-22 09:08:52 +00:00
|
|
|
}
|
|
|
|
READ_COPY_BUFFER(st, stsize);
|
|
|
|
if(n<=0) {
|
2015-07-23 13:23:39 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_INVALID_SERVER_REPLY;
|
|
|
|
goto free_url_and_st_and_return;
|
2015-07-22 09:08:52 +00:00
|
|
|
}
|
|
|
|
DECODELENGTH_READ(usnsize, READ_BYTE_BUFFER);
|
|
|
|
if(n<=0) {
|
2015-07-23 13:23:39 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_INVALID_SERVER_REPLY;
|
|
|
|
goto free_url_and_st_and_return;
|
2015-07-22 09:08:52 +00:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" usnsize=%u\n", usnsize);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize+usnsize);
|
2015-06-16 15:33:59 +00:00
|
|
|
if(tmp == NULL) {
|
2015-07-23 13:23:39 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_MEMORY_ERROR;
|
|
|
|
goto free_url_and_st_and_return;
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
tmp->pNext = devlist;
|
|
|
|
tmp->descURL = tmp->buffer;
|
|
|
|
tmp->st = tmp->buffer + 1 + urlsize;
|
|
|
|
memcpy(tmp->buffer, url, urlsize);
|
|
|
|
tmp->buffer[urlsize] = '\0';
|
2015-07-22 09:08:52 +00:00
|
|
|
memcpy(tmp->st, st, stsize);
|
2011-09-27 20:25:35 +00:00
|
|
|
tmp->buffer[urlsize+1+stsize] = '\0';
|
2015-06-16 15:33:59 +00:00
|
|
|
free(url);
|
2015-07-22 09:08:52 +00:00
|
|
|
free(st);
|
2015-06-16 15:33:59 +00:00
|
|
|
url = NULL;
|
2015-07-22 09:08:52 +00:00
|
|
|
st = NULL;
|
|
|
|
tmp->usn = tmp->buffer + 1 + urlsize + 1 + stsize;
|
|
|
|
READ_COPY_BUFFER(tmp->usn, usnsize);
|
2015-06-16 15:33:59 +00:00
|
|
|
if(n<=0) {
|
2015-07-23 13:23:39 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_INVALID_SERVER_REPLY;
|
|
|
|
goto free_tmp_and_return;
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
2015-07-22 09:08:52 +00:00
|
|
|
tmp->buffer[urlsize+1+stsize+1+usnsize] = '\0';
|
2015-04-24 20:58:46 +00:00
|
|
|
tmp->scope_id = 0; /* default value. scope_id is not available with MiniSSDPd */
|
2011-09-27 20:25:35 +00:00
|
|
|
devlist = tmp;
|
|
|
|
}
|
2015-07-23 13:22:23 +00:00
|
|
|
if (error)
|
|
|
|
*error = MINISSDPC_SUCCESS;
|
2011-09-27 20:25:35 +00:00
|
|
|
return devlist;
|
2015-07-23 13:23:39 +00:00
|
|
|
|
|
|
|
free_url_and_st_and_return:
|
|
|
|
free(st);
|
|
|
|
free_url_and_return:
|
|
|
|
free(url);
|
|
|
|
return devlist;
|
|
|
|
|
|
|
|
free_tmp_and_return:
|
|
|
|
free(tmp);
|
|
|
|
return devlist;
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
|
|
|
|
2015-09-18 16:11:56 +00:00
|
|
|
#endif /* !(defined(_WIN32) || defined(__amigaos__) || defined(__amigaos4__)) */
|
|
|
|
|
2015-08-03 16:49:55 +00:00
|
|
|
/* 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.
|
2019-01-07 08:35:21 +00:00
|
|
|
* - usn/usnsize : "usn:" filed of the SSDP reply packet
|
2015-08-03 16:49:55 +00:00
|
|
|
* The strings are NOT null terminated */
|
|
|
|
static void
|
|
|
|
parseMSEARCHReply(const char * reply, int size,
|
|
|
|
const char * * location, int * locationsize,
|
|
|
|
const char * * st, int * stsize,
|
|
|
|
const char * * usn, int * usnsize)
|
|
|
|
{
|
|
|
|
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]==' ');
|
2019-01-05 20:24:46 +00:00
|
|
|
if(0==strncasecmp(reply+a, "location:", 9))
|
2015-08-03 16:49:55 +00:00
|
|
|
{
|
|
|
|
*location = reply+b;
|
|
|
|
*locationsize = i-b;
|
|
|
|
}
|
2019-01-05 20:24:46 +00:00
|
|
|
else if(0==strncasecmp(reply+a, "st:", 3))
|
2015-08-03 16:49:55 +00:00
|
|
|
{
|
|
|
|
*st = reply+b;
|
|
|
|
*stsize = i-b;
|
|
|
|
}
|
2019-01-05 20:24:46 +00:00
|
|
|
else if(0==strncasecmp(reply+a, "usn:", 4))
|
2015-08-03 16:49:55 +00:00
|
|
|
{
|
|
|
|
*usn = reply+b;
|
|
|
|
*usnsize = i-b;
|
|
|
|
}
|
|
|
|
b = 0;
|
|
|
|
}
|
|
|
|
a = i+1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* port upnp discover : SSDP protocol */
|
2015-10-08 16:08:43 +00:00
|
|
|
#define SSDP_PORT 1900
|
2015-08-03 16:49:55 +00:00
|
|
|
#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 */
|
|
|
|
|
|
|
|
/* direct discovery if minissdpd responses are not sufficient */
|
|
|
|
/* ssdpDiscoverDevices() :
|
|
|
|
* 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
|
|
|
|
* delay is in millisecond (poll).
|
|
|
|
* UDA v1.1 says :
|
|
|
|
* The TTL for the IP packet SHOULD default to 2 and
|
|
|
|
* SHOULD be configurable. */
|
|
|
|
struct UPNPDev *
|
|
|
|
ssdpDiscoverDevices(const char * const deviceTypes[],
|
|
|
|
int delay, const char * multicastif,
|
2015-10-08 15:42:45 +00:00
|
|
|
int localport,
|
2015-08-03 16:49:55 +00:00
|
|
|
int ipv6, unsigned char ttl,
|
|
|
|
int * error,
|
|
|
|
int searchalltypes)
|
|
|
|
{
|
|
|
|
struct UPNPDev * tmp;
|
2019-02-12 13:23:36 +00:00
|
|
|
struct UPNPDev * devlist = NULL;
|
2015-08-03 16:49:55 +00:00
|
|
|
unsigned int scope_id = 0;
|
|
|
|
int opt = 1;
|
|
|
|
static const char MSearchMsgFmt[] =
|
|
|
|
"M-SEARCH * HTTP/1.1\r\n"
|
2015-10-08 16:08:43 +00:00
|
|
|
"HOST: %s:" XSTR(SSDP_PORT) "\r\n"
|
2015-08-03 16:49:55 +00:00
|
|
|
"ST: %s\r\n"
|
|
|
|
"MAN: \"ssdp:discover\"\r\n"
|
|
|
|
"MX: %u\r\n"
|
|
|
|
"\r\n";
|
|
|
|
int deviceIndex;
|
|
|
|
char bufr[1536]; /* reception and emission buffer */
|
2018-04-06 10:21:36 +00:00
|
|
|
SOCKET sudp;
|
2015-08-03 16:49:55 +00:00
|
|
|
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
|
|
|
|
#ifdef _WIN32
|
2015-08-27 20:29:53 +00:00
|
|
|
unsigned long _ttl = (unsigned long)ttl;
|
2015-08-03 16:49:55 +00:00
|
|
|
#endif
|
|
|
|
int linklocal = 1;
|
2017-03-13 14:33:38 +00:00
|
|
|
int sentok;
|
2015-08-03 16:49:55 +00:00
|
|
|
|
|
|
|
if(error)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_UNKNOWN_ERROR;
|
2015-08-03 16:49:55 +00:00
|
|
|
|
2015-10-08 15:42:45 +00:00
|
|
|
if(localport==UPNP_LOCAL_PORT_SAME)
|
2015-10-08 16:08:43 +00:00
|
|
|
localport = SSDP_PORT;
|
2015-10-08 15:42:45 +00:00
|
|
|
|
2015-08-03 16:49:55 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
sudp = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
#else
|
|
|
|
sudp = socket(ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, 0);
|
|
|
|
#endif
|
2018-04-06 10:21:36 +00:00
|
|
|
if(ISINVALID(sudp))
|
2015-08-03 16:49:55 +00:00
|
|
|
{
|
|
|
|
if(error)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_SOCKET_ERROR;
|
2015-08-03 16:49:55 +00:00
|
|
|
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;
|
2015-10-08 15:42:45 +00:00
|
|
|
if(localport > 0 && localport < 65536)
|
|
|
|
p->sin6_port = htons((unsigned short)localport);
|
2015-08-03 16:49:55 +00:00
|
|
|
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;
|
2015-10-08 15:42:45 +00:00
|
|
|
if(localport > 0 && localport < 65536)
|
|
|
|
p->sin_port = htons((unsigned short)localport);
|
2015-08-03 16:49:55 +00:00
|
|
|
p->sin_addr.s_addr = INADDR_ANY;
|
|
|
|
}
|
|
|
|
#ifdef _WIN32
|
|
|
|
/* This code could help us to use the right Network interface for
|
|
|
|
* 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) */
|
2018-07-14 10:53:42 +00:00
|
|
|
if(!ipv6) {
|
2018-09-21 12:56:59 +00:00
|
|
|
DWORD ifbestidx;
|
|
|
|
SOCKADDR_IN destAddr;
|
|
|
|
memset(&destAddr, 0, sizeof(destAddr));
|
|
|
|
destAddr.sin_family = AF_INET;
|
|
|
|
destAddr.sin_addr.s_addr = inet_addr("223.255.255.255");
|
|
|
|
destAddr.sin_port = 0;
|
|
|
|
if (GetBestInterfaceEx((struct sockaddr *)&destAddr, &ifbestidx) == NO_ERROR) {
|
|
|
|
DWORD dwRetVal = 0;
|
|
|
|
PIP_ADAPTER_ADDRESSES pAddresses = NULL;
|
|
|
|
ULONG outBufLen = 0;
|
|
|
|
ULONG Iterations = 0;
|
|
|
|
PIP_ADAPTER_ADDRESSES pCurrAddresses = NULL;
|
|
|
|
PIP_ADAPTER_UNICAST_ADDRESS pUnicast = NULL;
|
|
|
|
|
|
|
|
outBufLen = 15360;
|
|
|
|
do {
|
|
|
|
pAddresses = (IP_ADAPTER_ADDRESSES *) HeapAlloc(GetProcessHeap(), 0, outBufLen);
|
|
|
|
if (pAddresses == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
dwRetVal = GetAdaptersAddresses(AF_INET, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufLen);
|
|
|
|
|
|
|
|
if (dwRetVal == ERROR_BUFFER_OVERFLOW) {
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAddresses);
|
|
|
|
pAddresses = NULL;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Iterations++;
|
|
|
|
} while ((dwRetVal == ERROR_BUFFER_OVERFLOW) && (Iterations < 3));
|
|
|
|
|
2015-09-15 22:31:34 +00:00
|
|
|
if (dwRetVal == NO_ERROR) {
|
2018-09-21 12:56:59 +00:00
|
|
|
pCurrAddresses = pAddresses;
|
|
|
|
while (pCurrAddresses) {
|
2015-08-03 16:49:55 +00:00
|
|
|
#ifdef DEBUG
|
2018-10-31 15:26:01 +00:00
|
|
|
int i;
|
|
|
|
PIP_ADAPTER_MULTICAST_ADDRESS pMulticast = NULL;
|
|
|
|
PIP_ADAPTER_ANYCAST_ADDRESS pAnycast = NULL;
|
|
|
|
|
2018-09-21 12:56:59 +00:00
|
|
|
printf("\tIfIndex (IPv4 interface): %u\n", pCurrAddresses->IfIndex);
|
|
|
|
printf("\tAdapter name: %s\n", pCurrAddresses->AdapterName);
|
|
|
|
pUnicast = pCurrAddresses->FirstUnicastAddress;
|
|
|
|
if (pUnicast != NULL) {
|
|
|
|
for (i = 0; pUnicast != NULL; i++) {
|
2019-02-11 15:08:55 +00:00
|
|
|
printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(((PSOCKADDR_IN)pUnicast->Address.lpSockaddr)->sin_addr) );
|
2018-09-21 12:56:59 +00:00
|
|
|
pUnicast = pUnicast->Next;
|
|
|
|
}
|
|
|
|
printf("\tNumber of Unicast Addresses: %d\n", i);
|
|
|
|
}
|
|
|
|
pAnycast = pCurrAddresses->FirstAnycastAddress;
|
|
|
|
if (pAnycast) {
|
|
|
|
for (i = 0; pAnycast != NULL; i++) {
|
2019-02-11 15:08:55 +00:00
|
|
|
printf("\tAnycast Address[%d]: \t%s\n", i, inet_ntoa(((PSOCKADDR_IN)pAnycast->Address.lpSockaddr)->sin_addr) );
|
2018-09-21 12:56:59 +00:00
|
|
|
pAnycast = pAnycast->Next;
|
|
|
|
}
|
|
|
|
printf("\tNumber of Anycast Addresses: %d\n", i);
|
|
|
|
}
|
|
|
|
pMulticast = pCurrAddresses->FirstMulticastAddress;
|
|
|
|
if (pMulticast) {
|
|
|
|
for (i = 0; pMulticast != NULL; i++) {
|
2019-02-11 15:08:55 +00:00
|
|
|
printf("\tMulticast Address[%d]: \t%s\n", i, inet_ntoa(((PSOCKADDR_IN)pMulticast->Address.lpSockaddr)->sin_addr) );
|
|
|
|
pMulticast = pMulticast->Next;
|
2018-09-21 12:56:59 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-15 22:31:34 +00:00
|
|
|
printf("\n");
|
2015-08-03 16:49:55 +00:00
|
|
|
#endif
|
2018-09-21 12:56:59 +00:00
|
|
|
pUnicast = pCurrAddresses->FirstUnicastAddress;
|
|
|
|
if (pCurrAddresses->IfIndex == ifbestidx && pUnicast != NULL) {
|
|
|
|
SOCKADDR_IN *ipv4 = (SOCKADDR_IN *)(pUnicast->Address.lpSockaddr);
|
2015-09-15 22:31:34 +00:00
|
|
|
/* Set the address of this interface to be used */
|
|
|
|
struct in_addr mc_if;
|
|
|
|
memset(&mc_if, 0, sizeof(mc_if));
|
2018-09-21 12:56:59 +00:00
|
|
|
mc_if.s_addr = ipv4->sin_addr.s_addr;
|
2015-09-15 22:31:34 +00:00
|
|
|
if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0) {
|
|
|
|
PRINT_SOCKET_ERROR("setsockopt");
|
|
|
|
}
|
2018-09-21 12:56:59 +00:00
|
|
|
((struct sockaddr_in *)&sockudp_r)->sin_addr.s_addr = ipv4->sin_addr.s_addr;
|
2015-08-03 16:49:55 +00:00
|
|
|
#ifndef DEBUG
|
2015-09-15 22:31:34 +00:00
|
|
|
break;
|
2015-08-03 16:49:55 +00:00
|
|
|
#endif
|
2015-09-15 22:31:34 +00:00
|
|
|
}
|
2018-09-21 12:56:59 +00:00
|
|
|
pCurrAddresses = pCurrAddresses->Next;
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
2018-09-21 12:56:59 +00:00
|
|
|
if (pAddresses != NULL) {
|
|
|
|
HeapFree(GetProcessHeap(), 0, pAddresses);
|
|
|
|
pAddresses = NULL;
|
|
|
|
}
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* _WIN32 */
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
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)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_SOCKET_ERROR;
|
2015-08-03 16:49:55 +00:00
|
|
|
PRINT_SOCKET_ERROR("setsockopt(SO_REUSEADDR,...)");
|
2019-02-12 13:23:36 +00:00
|
|
|
goto error;
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
|
2017-03-13 14:33:38 +00:00
|
|
|
if(ipv6) {
|
2017-09-18 17:27:10 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
DWORD mcastHops = ttl;
|
|
|
|
if(setsockopt(sudp, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, (const char *)&mcastHops, sizeof(mcastHops)) < 0)
|
|
|
|
#else /* _WIN32 */
|
2017-03-13 14:33:38 +00:00
|
|
|
int mcastHops = ttl;
|
|
|
|
if(setsockopt(sudp, IPPROTO_IPV6, IPV6_MULTICAST_HOPS, &mcastHops, sizeof(mcastHops)) < 0)
|
2017-09-18 17:27:10 +00:00
|
|
|
#endif /* _WIN32 */
|
2017-03-13 14:33:38 +00:00
|
|
|
{
|
|
|
|
PRINT_SOCKET_ERROR("setsockopt(IPV6_MULTICAST_HOPS,...)");
|
|
|
|
}
|
|
|
|
} else {
|
2015-08-27 20:29:53 +00:00
|
|
|
#ifdef _WIN32
|
2017-03-13 14:33:38 +00:00
|
|
|
if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_TTL, (const char *)&_ttl, sizeof(_ttl)) < 0)
|
2015-08-27 20:29:53 +00:00
|
|
|
#else /* _WIN32 */
|
2017-03-13 14:33:38 +00:00
|
|
|
if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_TTL, &ttl, sizeof(ttl)) < 0)
|
2015-08-27 20:29:53 +00:00
|
|
|
#endif /* _WIN32 */
|
2017-03-13 14:33:38 +00:00
|
|
|
{
|
|
|
|
/* not a fatal error */
|
|
|
|
PRINT_SOCKET_ERROR("setsockopt(IP_MULTICAST_TTL,...)");
|
|
|
|
}
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if(multicastif)
|
|
|
|
{
|
|
|
|
if(ipv6) {
|
|
|
|
#if !defined(_WIN32)
|
|
|
|
/* 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. */
|
|
|
|
if(setsockopt(sudp, IPPROTO_IPV6, IPV6_MULTICAST_IF, &ifindex, sizeof(ifindex)) < 0)
|
|
|
|
{
|
2016-11-11 15:13:48 +00:00
|
|
|
PRINT_SOCKET_ERROR("setsockopt IPV6_MULTICAST_IF");
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Setting of multicast interface not supported in IPv6 under Windows.\n");
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
} else {
|
|
|
|
struct in_addr mc_if;
|
2018-08-05 12:20:44 +00:00
|
|
|
#if defined(_WIN32) && (_WIN32_WINNT >= _WIN32_WINNT_VISTA)
|
2018-07-14 10:53:42 +00:00
|
|
|
InetPtonA(AF_INET, multicastif, &mc_if);
|
|
|
|
#else
|
2015-08-03 16:49:55 +00:00
|
|
|
mc_if.s_addr = inet_addr(multicastif); /* ex: 192.168.x.x */
|
2018-07-14 10:53:42 +00:00
|
|
|
#endif
|
2015-08-03 16:49:55 +00:00
|
|
|
if(mc_if.s_addr != INADDR_NONE)
|
|
|
|
{
|
|
|
|
((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)
|
|
|
|
{
|
2016-11-11 15:13:48 +00:00
|
|
|
PRINT_SOCKET_ERROR("setsockopt IP_MULTICAST_IF");
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
} 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)
|
|
|
|
{
|
2016-11-11 15:13:48 +00:00
|
|
|
PRINT_SOCKET_ERROR("setsockopt IP_MULTICAST_IF");
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
2016-01-17 18:32:05 +00:00
|
|
|
#elif !defined(_WIN32)
|
2016-01-17 17:49:41 +00:00
|
|
|
struct ifreq ifr;
|
|
|
|
int ifrlen = sizeof(ifr);
|
|
|
|
strncpy(ifr.ifr_name, multicastif, IFNAMSIZ);
|
|
|
|
ifr.ifr_name[IFNAMSIZ-1] = '\0';
|
|
|
|
if(ioctl(sudp, SIOCGIFADDR, &ifr, &ifrlen) < 0)
|
|
|
|
{
|
|
|
|
PRINT_SOCKET_ERROR("ioctl(...SIOCGIFADDR...)");
|
|
|
|
}
|
|
|
|
mc_if.s_addr = ((struct sockaddr_in *)&ifr.ifr_addr)->sin_addr.s_addr;
|
|
|
|
if(setsockopt(sudp, IPPROTO_IP, IP_MULTICAST_IF, (const char *)&mc_if, sizeof(mc_if)) < 0)
|
|
|
|
{
|
2016-11-11 15:13:48 +00:00
|
|
|
PRINT_SOCKET_ERROR("setsockopt IP_MULTICAST_IF");
|
2016-01-17 17:49:41 +00:00
|
|
|
}
|
2016-01-17 18:32:05 +00:00
|
|
|
#else /* _WIN32 */
|
|
|
|
#ifdef DEBUG
|
2015-08-03 16:49:55 +00:00
|
|
|
printf("Setting of multicast interface not supported with interface name.\n");
|
|
|
|
#endif
|
2016-01-17 18:32:05 +00:00
|
|
|
#endif /* #ifdef HAS_IP_MREQN / !defined(_WIN32) */
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Before sending the packed, we first "bind" in order to be able
|
|
|
|
* to receive the response */
|
|
|
|
if (bind(sudp, (const struct sockaddr *)&sockudp_r,
|
|
|
|
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) != 0)
|
|
|
|
{
|
|
|
|
if(error)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_SOCKET_ERROR;
|
2015-08-03 16:49:55 +00:00
|
|
|
PRINT_SOCKET_ERROR("bind");
|
|
|
|
closesocket(sudp);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(error)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_SUCCESS;
|
2015-08-03 16:49:55 +00:00
|
|
|
/* Calculating maximum response time in seconds */
|
|
|
|
mx = ((unsigned int)delay) / 1000u;
|
|
|
|
if(mx == 0) {
|
|
|
|
mx = 1;
|
|
|
|
delay = 1000;
|
|
|
|
}
|
|
|
|
/* receiving SSDP response packet */
|
|
|
|
for(deviceIndex = 0; deviceTypes[deviceIndex]; deviceIndex++) {
|
2017-03-13 14:33:38 +00:00
|
|
|
sentok = 0;
|
2015-08-03 16:49:55 +00:00
|
|
|
/* 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);
|
2015-10-26 10:35:09 +00:00
|
|
|
if ((unsigned int)n >= sizeof(bufr)) {
|
|
|
|
if(error)
|
|
|
|
*error = MINISSDPC_MEMORY_ERROR;
|
|
|
|
goto error;
|
2015-10-25 20:05:14 +00:00
|
|
|
}
|
2015-08-03 16:49:55 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
/*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]);
|
|
|
|
#endif
|
|
|
|
#ifdef NO_GETADDRINFO
|
|
|
|
/* 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;
|
2015-10-08 16:08:43 +00:00
|
|
|
p->sin6_port = htons(SSDP_PORT);
|
2015-08-03 16:49:55 +00:00
|
|
|
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;
|
2015-10-08 16:08:43 +00:00
|
|
|
p->sin_port = htons(SSDP_PORT);
|
2015-08-03 16:49:55 +00:00
|
|
|
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)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_SOCKET_ERROR;
|
2015-08-03 16:49:55 +00:00
|
|
|
PRINT_SOCKET_ERROR("sendto");
|
2017-03-13 14:33:38 +00:00
|
|
|
} else {
|
|
|
|
sentok = 1;
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
#else /* #ifdef NO_GETADDRINFO */
|
|
|
|
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,
|
2015-10-08 16:08:43 +00:00
|
|
|
XSTR(SSDP_PORT), &hints, &servinfo)) != 0) {
|
2015-08-03 16:49:55 +00:00
|
|
|
if(error)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_SOCKET_ERROR;
|
2015-08-03 16:49:55 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
fprintf(stderr, "getaddrinfo() failed: %d\n", rv);
|
|
|
|
#else
|
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
|
|
|
#endif
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
for(p = servinfo; p; p = p->ai_next) {
|
2018-06-18 20:35:59 +00:00
|
|
|
n = sendto(sudp, bufr, n, 0, p->ai_addr, MSC_CAST_INT p->ai_addrlen);
|
2015-08-03 16:49:55 +00:00
|
|
|
if (n < 0) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
|
2019-02-12 13:23:36 +00:00
|
|
|
if (getnameinfo(p->ai_addr, (socklen_t)p->ai_addrlen, hbuf, sizeof(hbuf), sbuf,
|
2015-08-03 16:49:55 +00:00
|
|
|
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
|
|
|
|
fprintf(stderr, "host:%s port:%s\n", hbuf, sbuf);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
PRINT_SOCKET_ERROR("sendto");
|
|
|
|
continue;
|
2017-03-13 14:33:38 +00:00
|
|
|
} else {
|
|
|
|
sentok = 1;
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
freeaddrinfo(servinfo);
|
2017-03-13 14:33:38 +00:00
|
|
|
if(!sentok) {
|
2015-08-03 16:49:55 +00:00
|
|
|
if(error)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_SOCKET_ERROR;
|
2015-08-03 16:49:55 +00:00
|
|
|
}
|
|
|
|
#endif /* #ifdef NO_GETADDRINFO */
|
|
|
|
/* Waiting for SSDP REPLY packet to M-SEARCH
|
|
|
|
* if searchalltypes is set, enter the loop only
|
|
|
|
* when the last deviceType is reached */
|
2017-03-13 14:33:38 +00:00
|
|
|
if((sentok && !searchalltypes) || !deviceTypes[deviceIndex + 1]) do {
|
2015-08-03 16:49:55 +00:00
|
|
|
n = receivedata(sudp, bufr, sizeof(bufr), delay, &scope_id);
|
|
|
|
if (n < 0) {
|
|
|
|
/* error */
|
|
|
|
if(error)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_SOCKET_ERROR;
|
2015-08-03 16:49:55 +00:00
|
|
|
goto error;
|
|
|
|
} else if (n == 0) {
|
|
|
|
/* no data or Time Out */
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("NODATA or TIMEOUT\n");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
if (devlist && !searchalltypes) {
|
|
|
|
/* found some devices, stop now*/
|
|
|
|
if(error)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_SUCCESS;
|
2015-08-03 16:49:55 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
const char * descURL=NULL;
|
|
|
|
int urlsize=0;
|
|
|
|
const char * st=NULL;
|
|
|
|
int stsize=0;
|
|
|
|
const char * usn=NULL;
|
|
|
|
int usnsize=0;
|
|
|
|
parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize, &usn, &usnsize);
|
|
|
|
if(st&&descURL) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("M-SEARCH Reply:\n ST: %.*s\n USN: %.*s\n Location: %.*s\n",
|
|
|
|
stsize, st, usnsize, (usn?usn:""), urlsize, descURL);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
for(tmp=devlist; tmp; tmp = tmp->pNext) {
|
|
|
|
if(memcmp(tmp->descURL, descURL, urlsize) == 0 &&
|
|
|
|
tmp->descURL[urlsize] == '\0' &&
|
|
|
|
memcmp(tmp->st, st, stsize) == 0 &&
|
|
|
|
tmp->st[stsize] == '\0' &&
|
|
|
|
(usnsize == 0 || memcmp(tmp->usn, usn, usnsize) == 0) &&
|
|
|
|
tmp->usn[usnsize] == '\0')
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/* at the exit of the loop above, tmp is null if
|
|
|
|
* no duplicate device was found */
|
|
|
|
if(tmp)
|
|
|
|
continue;
|
|
|
|
tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize+usnsize);
|
|
|
|
if(!tmp) {
|
|
|
|
/* memory allocation error */
|
|
|
|
if(error)
|
2015-08-04 11:15:56 +00:00
|
|
|
*error = MINISSDPC_MEMORY_ERROR;
|
2015-08-03 16:49:55 +00:00
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
tmp->pNext = devlist;
|
|
|
|
tmp->descURL = tmp->buffer;
|
|
|
|
tmp->st = tmp->buffer + 1 + urlsize;
|
|
|
|
tmp->usn = tmp->st + 1 + stsize;
|
|
|
|
memcpy(tmp->buffer, descURL, urlsize);
|
|
|
|
tmp->buffer[urlsize] = '\0';
|
|
|
|
memcpy(tmp->st, st, stsize);
|
|
|
|
tmp->buffer[urlsize+1+stsize] = '\0';
|
|
|
|
if(usn != NULL)
|
|
|
|
memcpy(tmp->usn, usn, usnsize);
|
|
|
|
tmp->buffer[urlsize+1+stsize+1+usnsize] = '\0';
|
|
|
|
tmp->scope_id = scope_id;
|
|
|
|
devlist = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} while(n > 0);
|
|
|
|
if(ipv6) {
|
|
|
|
/* switch linklocal flag */
|
|
|
|
if(linklocal) {
|
|
|
|
linklocal = 0;
|
|
|
|
--deviceIndex;
|
|
|
|
} else {
|
|
|
|
linklocal = 1;
|
|
|
|
}
|
|
|
|
}
|
2011-09-27 20:25:35 +00:00
|
|
|
}
|
2015-08-03 16:49:55 +00:00
|
|
|
error:
|
|
|
|
closesocket(sudp);
|
2011-09-27 20:25:35 +00:00
|
|
|
return devlist;
|
|
|
|
}
|
|
|
|
|