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

View File

@ -50,6 +50,7 @@ getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath)
ssize_t n; ssize_t n;
unsigned char * p; unsigned char * p;
unsigned char * url; unsigned char * url;
unsigned char * st;
unsigned int bufferindex; unsigned int bufferindex;
unsigned int i, ndev; unsigned int i, ndev;
unsigned int urlsize, stsize, usnsize, l; unsigned int urlsize, stsize, usnsize, l;
@ -196,9 +197,30 @@ getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath)
#ifdef DEBUG #ifdef DEBUG
printf(" stsize=%u", stsize); printf(" stsize=%u", stsize);
#endif /* DEBUG */ #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) { if(tmp == NULL) {
free(url); free(url);
free(st);
break; break;
} }
tmp->pNext = devlist; tmp->pNext = devlist;
@ -206,29 +228,21 @@ getDevicesFromMiniSSDPD(const char * devtype, const char * socketpath)
tmp->st = tmp->buffer + 1 + urlsize; tmp->st = tmp->buffer + 1 + urlsize;
memcpy(tmp->buffer, url, urlsize); memcpy(tmp->buffer, url, urlsize);
tmp->buffer[urlsize] = '\0'; tmp->buffer[urlsize] = '\0';
memcpy(tmp->st, st, stsize);
tmp->buffer[urlsize+1+stsize] = '\0';
free(url); free(url);
free(st);
url = NULL; 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) { if(n<=0) {
free(tmp); free(tmp);
break; 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 */ tmp->scope_id = 0; /* default value. scope_id is not available with MiniSSDPd */
devlist = tmp; 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); close(s);
return devlist; return devlist;

View File

@ -272,7 +272,8 @@ char * simpleUPnPcommand(int s, const char * url, const char * service,
static void static void
parseMSEARCHReply(const char * reply, int size, parseMSEARCHReply(const char * reply, int size,
const char * * location, int * locationsize, const char * * location, int * locationsize,
const char * * st, int * stsize) const char * * st, int * stsize,
const char * * usn, int * usnsize)
{ {
int a, b, i; int a, b, i;
i = 0; i = 0;
@ -314,6 +315,11 @@ parseMSEARCHReply(const char * reply, int size,
*st = reply+b; *st = reply+b;
*stsize = i-b; *stsize = i-b;
} }
else if(0==strncasecmp(reply+a, "usn", 3))
{
*usn = reply+b;
*usnsize = i-b;
}
b = 0; b = 0;
} }
a = i+1; a = i+1;
@ -701,24 +707,28 @@ upnpDiscoverDevices(const char * const deviceTypes[],
int urlsize=0; int urlsize=0;
const char * st=NULL; const char * st=NULL;
int stsize=0; 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) { if(st&&descURL) {
#ifdef DEBUG #ifdef DEBUG
printf("M-SEARCH Reply:\n ST: %.*s\n Location: %.*s\n", printf("M-SEARCH Reply:\n ST: %.*s\n USN: %.*s\n Location: %.*s\n",
stsize, st, urlsize, descURL); stsize, st, usnsize, (usn?usn:""), urlsize, descURL);
#endif /* DEBUG */ #endif /* DEBUG */
for(tmp=devlist; tmp; tmp = tmp->pNext) { for(tmp=devlist; tmp; tmp = tmp->pNext) {
if(memcmp(tmp->descURL, descURL, urlsize) == 0 && if(memcmp(tmp->descURL, descURL, urlsize) == 0 &&
tmp->descURL[urlsize] == '\0' && tmp->descURL[urlsize] == '\0' &&
memcmp(tmp->st, st, stsize) == 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; break;
} }
/* at the exit of the loop above, tmp is null if /* at the exit of the loop above, tmp is null if
* no duplicate device was found */ * no duplicate device was found */
if(tmp) if(tmp)
continue; continue;
tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize); tmp = (struct UPNPDev *)malloc(sizeof(struct UPNPDev)+urlsize+stsize+usnsize);
if(!tmp) { if(!tmp) {
/* memory allocation error */ /* memory allocation error */
if(error) if(error)
@ -728,10 +738,14 @@ upnpDiscoverDevices(const char * const deviceTypes[],
tmp->pNext = devlist; tmp->pNext = devlist;
tmp->descURL = tmp->buffer; tmp->descURL = tmp->buffer;
tmp->st = tmp->buffer + 1 + urlsize; tmp->st = tmp->buffer + 1 + urlsize;
tmp->usn = tmp->st + 1 + stsize;
memcpy(tmp->buffer, descURL, urlsize); memcpy(tmp->buffer, descURL, urlsize);
tmp->buffer[urlsize] = '\0'; tmp->buffer[urlsize] = '\0';
memcpy(tmp->buffer + urlsize + 1, st, stsize); memcpy(tmp->st, st, stsize);
tmp->buffer[urlsize+1+stsize] = '\0'; 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; tmp->scope_id = scope_id;
devlist = tmp; devlist = tmp;
} }

View File

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