2012-01-21 13:38:38 +00:00
|
|
|
/* $Id: minissdpc.c,v 1.15 2012/01/21 13:30:31 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-06-11 12:28:35 +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 LICENCE file. */
|
|
|
|
/*#include <syslog.h>*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
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>
|
|
|
|
#include <winsock.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#endif
|
|
|
|
#if defined(__amigaos__) || defined(__amigaos4__)
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#endif
|
|
|
|
#if defined(__amigaos__)
|
|
|
|
#define uint16_t unsigned short
|
|
|
|
#endif
|
|
|
|
/* Hack */
|
|
|
|
#define UNIX_PATH_LEN 108
|
|
|
|
struct sockaddr_un {
|
|
|
|
uint16_t sun_family;
|
|
|
|
char sun_path[UNIX_PATH_LEN];
|
|
|
|
};
|
|
|
|
#else
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "minissdpc.h"
|
|
|
|
#include "miniupnpc.h"
|
|
|
|
|
|
|
|
#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
|
|
|
{
|
2015-07-23 13:22:23 +00:00
|
|
|
struct UPNPDev * devlist;
|
2011-09-27 20:25:35 +00:00
|
|
|
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;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
devlist = receiveDevicesFromMiniSSDPD(s, error);
|
|
|
|
disconnectFromMiniSSDPD(s);
|
|
|
|
return devlist;
|
|
|
|
}
|
2015-06-16 15:33:59 +00:00
|
|
|
|
|
|
|
/* macros used to read from unix socket */
|
|
|
|
#define READ_BYTE_BUFFER(c) \
|
|
|
|
if(bufferindex >= n) { \
|
|
|
|
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; \
|
|
|
|
if(bufferindex >= n) { \
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
int s;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
|
|
|
|
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
|
|
|
}
|
2015-06-16 15:33:59 +00:00
|
|
|
#ifdef MINIUPNPC_SET_SOCKET_TIMEOUT
|
|
|
|
/* setting a 3 seconds timeout */
|
|
|
|
timeout.tv_sec = 3;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
if(setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(struct timeval)) < 0)
|
|
|
|
{
|
|
|
|
perror("setsockopt");
|
|
|
|
}
|
|
|
|
timeout.tv_sec = 3;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
if(setsockopt(s, SOL_SOCKET, SO_SNDTIMEO, &timeout, sizeof(struct timeval)) < 0)
|
|
|
|
{
|
|
|
|
perror("setsockopt");
|
|
|
|
}
|
|
|
|
#endif /* #ifdef MINIUPNPC_SET_SOCKET_TIMEOUT */
|
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);*/
|
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) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf(" urlsize=%u", urlsize);
|
|
|
|
#endif /* DEBUG */
|
|
|
|
url = malloc(urlsize);
|
|
|
|
if(url == NULL) {
|
2011-09-27 20:25:35 +00:00
|
|
|
break;
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
|
|
|
READ_COPY_BUFFER(url, urlsize);
|
|
|
|
if(n<=0) {
|
|
|
|
free(url);
|
2011-09-27 20:25:35 +00:00
|
|
|
break;
|
2015-06-16 15:33:59 +00:00
|
|
|
}
|
|
|
|
DECODELENGTH_READ(stsize, READ_BYTE_BUFFER);
|
|
|
|
if(n<=0) {
|
|
|
|
free(url);
|
2011-09-27 20:25:35 +00:00
|
|
|
break;
|
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) {
|
|
|
|
free(url);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
READ_COPY_BUFFER(st, stsize);
|
|
|
|
if(n<=0) {
|
|
|
|
free(url);
|
|
|
|
free(st);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
DECODELENGTH_READ(usnsize, READ_BYTE_BUFFER);
|
|
|
|
if(n<=0) {
|
|
|
|
free(url);
|
|
|
|
free(st);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#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) {
|
|
|
|
free(url);
|
2015-07-22 09:08:52 +00:00
|
|
|
free(st);
|
2015-06-16 15:33:59 +00:00
|
|
|
break;
|
|
|
|
}
|
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);
|
|
|
|
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) {
|
|
|
|
free(tmp);
|
|
|
|
break;
|
|
|
|
}
|
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;
|
|
|
|
}
|
|
|
|
|