Merge branch 'read_usn'

This commit is contained in:
Thomas Bernard 2015-07-22 23:50:44 +02:00
commit af2ec81f14
4 changed files with 58 additions and 27 deletions

View File

@ -1,7 +1,7 @@
/* $Id: listdevices.c,v 1.4 2015/07/15 12:51:30 nanard Exp $ */
/* Project : miniupnp
* Author : Thomas Bernard
* Copyright (c) 2013-2014 Thomas Bernard
* Copyright (c) 2013-2015 Thomas Bernard
* This software is subject to the conditions detailed in the
* LICENCE file provided in this distribution. */
@ -86,8 +86,10 @@ int main(int argc, char * * argv)
0/*sameport*/, ipv6, &error);
}
if(devlist) {
for(dev = devlist; dev != NULL; dev = dev->pNext) {
printf("%-48s\t%s\n", dev->st, dev->descURL);
for(dev = devlist, i = 1; dev != NULL; dev = dev->pNext, i++) {
printf("%3d: %-48s\n", i, dev->st);
printf(" %s\n", dev->descURL);
printf(" %s\n", dev->usn);
}
freeUPNPDevlist(devlist);
} else {

View File

@ -50,6 +50,7 @@ getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath)
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;
@ -196,9 +197,30 @@ getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath)
#ifdef DEBUG
printf(" stsize=%u", stsize);
#endif /* DEBUG */
tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize);
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);
if(tmp == NULL) {
free(url);
free(st);
break;
}
tmp->pNext = devlist;
@ -206,29 +228,21 @@ getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath)
tmp->st = tmp->buffer + 1 + urlsize;
memcpy(tmp->buffer, url, urlsize);
tmp->buffer[urlsize] = '\0';
memcpy(tmp->st, st, stsize);
tmp->buffer[urlsize+1+stsize] = '\0';
free(url);
free(st);
url = NULL;
READ_COPY_BUFFER(tmp->buffer + urlsize + 1, stsize);
st = NULL;
tmp->usn = tmp->buffer + 1 + urlsize + 1 + stsize;
READ_COPY_BUFFER(tmp->usn, usnsize);
if(n<=0) {
free(tmp);
break;
}
tmp->buffer[urlsize+1+stsize] = '\0';
tmp->buffer[urlsize+1+stsize+1+usnsize] = '\0';
tmp->scope_id = 0; /* default value. scope_id is not available with MiniSSDPd */
devlist = tmp;
/* added for compatibility with recent versions of MiniSSDPd
* >= 2007/12/19 */
DECODELENGTH_READ(usnsize, READ_BYTE_BUFFER);
if(n<=0) {
break;
}
#ifdef DEBUG
printf(" usnsize=%u\n", usnsize);
#endif /* DEBUG */
READ_DISCARD_BUFFER(usnsize);
if(n<=0) {
break;
}
}
close(s);
return devlist;

View File

@ -272,7 +272,8 @@ char * simpleUPnPcommand(int s, const char * url, const char * service,
static void
parseMSEARCHReply(const char * reply, int size,
const char * * location, int * locationsize,
const char * * st, int * stsize)
const char * * st, int * stsize,
const char * * usn, int * usnsize)
{
int a, b, i;
i = 0;
@ -314,6 +315,11 @@ parseMSEARCHReply(const char * reply, int size,
*st = reply+b;
*stsize = i-b;
}
else if(0==strncasecmp(reply+a, "usn", 3))
{
*usn = reply+b;
*usnsize = i-b;
}
b = 0;
}
a = i+1;
@ -701,24 +707,28 @@ upnpDiscoverDevices(const char * const deviceTypes[],
int urlsize=0;
const char * st=NULL;
int stsize=0;
parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize);
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 Location: %.*s\n",
stsize, st, urlsize, descURL);
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')
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);
tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize+usnsize);
if(!tmp) {
/* memory allocation error */
if(error)
@ -728,10 +738,14 @@ upnpDiscoverDevices(const char * const deviceTypes[],
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->buffer + urlsize + 1, st, stsize);
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;
}

View File

@ -38,7 +38,8 @@ struct UPNPDev {
char * descURL;
char * st;
unsigned int scope_id;
char buffer[2];
char * usn;
char buffer[3];
};
/* upnpDiscover()