2007-12-20 20:18:22 +00:00
|
|
|
/* $Id: minissdpc.c,v 1.4 2007/12/19 14:56:58 nanard Exp $ */
|
2007-11-06 16:02:50 +00:00
|
|
|
/* Project : miniupnp
|
|
|
|
* Author : Thomas BERNARD
|
|
|
|
* copyright (c) 2005-2007 Thomas Bernard
|
|
|
|
* 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>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/un.h>
|
|
|
|
|
|
|
|
#include "minissdpc.h"
|
|
|
|
#include "miniupnpc.h"
|
|
|
|
|
2007-12-20 20:18:22 +00:00
|
|
|
#define DECODELENGTH(n, p) n = 0; \
|
|
|
|
do { n = (n << 7) | (*p & 0x7f); } \
|
|
|
|
while(*(p++)&0x80);
|
|
|
|
#define CODELENGTH(n, p) do { *p = (n & 0x7f) | ((n > 0x7f) ? 0x80 : 0); \
|
|
|
|
p++; n >>= 7; } while(n);
|
|
|
|
|
2007-11-06 16:02:50 +00:00
|
|
|
struct UPNPDev *
|
|
|
|
getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath)
|
|
|
|
{
|
|
|
|
struct UPNPDev * tmp;
|
|
|
|
struct UPNPDev * devlist = NULL;
|
2007-12-20 20:18:22 +00:00
|
|
|
unsigned char buffer[2048];
|
2007-11-06 16:02:50 +00:00
|
|
|
ssize_t n;
|
|
|
|
unsigned char * p;
|
2007-12-20 20:18:22 +00:00
|
|
|
unsigned char * url;
|
2007-11-06 16:02:50 +00:00
|
|
|
unsigned int i;
|
2007-12-20 20:18:22 +00:00
|
|
|
unsigned int urlsize, stsize, usnsize, l;
|
2007-11-06 16:02:50 +00:00
|
|
|
int s;
|
|
|
|
struct sockaddr_un addr;
|
|
|
|
|
|
|
|
s = socket(AF_UNIX, SOCK_STREAM, 0);
|
|
|
|
if(s < 0)
|
|
|
|
{
|
|
|
|
/*syslog(LOG_ERR, "socket(unix): %m");*/
|
|
|
|
perror("socket(unix)");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
addr.sun_family = AF_UNIX;
|
|
|
|
strncpy(addr.sun_path, socketpath, sizeof(addr.sun_path));
|
|
|
|
if(connect(s, (struct sockaddr *)&addr, sizeof(struct sockaddr_un)) < 0)
|
|
|
|
{
|
|
|
|
/*syslog(LOG_WARNING, "connect(\"%s\"): %m", socketpath);*/
|
|
|
|
close(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
stsize = strlen(devtype);
|
|
|
|
buffer[0] = 1;
|
2007-12-20 20:18:22 +00:00
|
|
|
p = buffer + 1;
|
|
|
|
l = stsize; CODELENGTH(l, p);
|
|
|
|
memcpy(p, devtype, stsize);
|
|
|
|
p += stsize;
|
|
|
|
if(write(s, buffer, p - buffer) < 0)
|
2007-11-06 16:02:50 +00:00
|
|
|
{
|
|
|
|
/*syslog(LOG_ERR, "write(): %m");*/
|
2007-12-20 20:18:22 +00:00
|
|
|
perror("minissdpc.c: write()");
|
2007-11-06 16:02:50 +00:00
|
|
|
close(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
n = read(s, buffer, sizeof(buffer));
|
|
|
|
if(n<=0)
|
|
|
|
{
|
2007-12-20 20:18:22 +00:00
|
|
|
perror("minissdpc.c: read()");
|
2007-11-06 16:02:50 +00:00
|
|
|
close(s);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
p = buffer + 1;
|
|
|
|
for(i = 0; i < buffer[0]; i++)
|
|
|
|
{
|
2007-12-20 20:18:22 +00:00
|
|
|
if(p+2>=buffer+sizeof(buffer))
|
|
|
|
break;
|
|
|
|
DECODELENGTH(urlsize, p);
|
|
|
|
if(p+urlsize+2>=buffer+sizeof(buffer))
|
|
|
|
break;
|
|
|
|
url = p;
|
|
|
|
p += urlsize;
|
|
|
|
DECODELENGTH(stsize, p);
|
|
|
|
if(p+stsize+2>=buffer+sizeof(buffer))
|
|
|
|
break;
|
2007-11-06 16:02:50 +00:00
|
|
|
tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize);
|
|
|
|
tmp->pNext = devlist;
|
|
|
|
tmp->descURL = tmp->buffer;
|
|
|
|
tmp->st = tmp->buffer + 1 + urlsize;
|
2007-12-20 20:18:22 +00:00
|
|
|
memcpy(tmp->buffer, url, urlsize);
|
2007-11-06 16:02:50 +00:00
|
|
|
tmp->buffer[urlsize] = '\0';
|
|
|
|
memcpy(tmp->buffer + urlsize + 1, p, stsize);
|
2007-12-20 20:18:22 +00:00
|
|
|
p += stsize;
|
2007-11-06 16:02:50 +00:00
|
|
|
tmp->buffer[urlsize+1+stsize] = '\0';
|
|
|
|
devlist = tmp;
|
2007-12-20 20:18:22 +00:00
|
|
|
/* added for compatibility with recent versions of MiniSSDPd
|
|
|
|
* >= 2007/12/19 */
|
|
|
|
DECODELENGTH(usnsize, p);
|
|
|
|
p += usnsize;
|
|
|
|
if(p>buffer + sizeof(buffer))
|
|
|
|
break;
|
2007-11-06 16:02:50 +00:00
|
|
|
}
|
|
|
|
close(s);
|
|
|
|
return devlist;
|
|
|
|
}
|
|
|
|
|