Read USN from SSDP messages.

This commit is contained in:
Konstantin Tokarev 2015-07-22 12:08:52 +03:00
parent ed4a01d2d6
commit eb74f5c886
4 changed files with 54 additions and 26 deletions

View File

@ -87,7 +87,7 @@ int main(int argc, char * * argv)
}
if(devlist) {
for(dev = devlist; dev != NULL; dev = dev->pNext) {
printf("%-48s\t%s\n", dev->st, dev->descURL);
printf("%-100s\t%s\n", dev->usn, dev->descURL);
}
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;
@ -692,24 +698,28 @@ upnpDiscoverDevices(const char * const deviceTypes[],
int urlsize=0;
const char * st=NULL;
int stsize=0;
parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize);
if(st&&descURL) {
const char * usn=NULL;
int usnsize=0;
parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize, &usn, &usnsize);
if(st&&descURL&&usn) {
#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, 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' &&
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)
@ -719,10 +729,13 @@ 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';
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()