miniupnpc.c: change upnpDiscoverDevice() to stop when devices are

This commit is contained in:
Thomas Bernard 2013-02-20 17:07:18 +01:00
parent cb054ef580
commit 91602e129d
1 changed files with 81 additions and 87 deletions

View File

@ -338,7 +338,7 @@ upnpDiscoverDevices(const char * const deviceTypes[],
"MAN: \"ssdp:discover\"\r\n" "MAN: \"ssdp:discover\"\r\n"
"MX: %u\r\n" "MX: %u\r\n"
"\r\n"; "\r\n";
int deviceIndex = 0; int deviceIndex;
char bufr[1536]; /* reception and emission buffer */ char bufr[1536]; /* reception and emission buffer */
int sudp; int sudp;
int n; int n;
@ -361,7 +361,7 @@ upnpDiscoverDevices(const char * const deviceTypes[],
/* first try to get infos from minissdpd ! */ /* first try to get infos from minissdpd ! */
if(!minissdpdsock) if(!minissdpdsock)
minissdpdsock = "/var/run/minissdpd.sock"; minissdpdsock = "/var/run/minissdpd.sock";
while(!devlist && deviceTypes[deviceIndex]) { for(deviceIndex = 0; !devlist && deviceTypes[deviceIndex]; deviceIndex++) {
devlist = getDevicesFromMiniSSDPD(deviceTypes[deviceIndex], devlist = getDevicesFromMiniSSDPD(deviceTypes[deviceIndex],
minissdpdsock); minissdpdsock);
/* We return what we have found if it was not only a rootdevice */ /* We return what we have found if it was not only a rootdevice */
@ -370,9 +370,7 @@ upnpDiscoverDevices(const char * const deviceTypes[],
*error = UPNPDISCOVER_SUCCESS; *error = UPNPDISCOVER_SUCCESS;
return devlist; return devlist;
} }
deviceIndex++;
} }
deviceIndex = 0;
#endif #endif
/* fallback to direct discovery */ /* fallback to direct discovery */
#ifdef _WIN32 #ifdef _WIN32
@ -536,8 +534,7 @@ upnpDiscoverDevices(const char * const deviceTypes[],
/* Calculating maximum response time in seconds */ /* Calculating maximum response time in seconds */
mx = ((unsigned int)delay) / 1000u; mx = ((unsigned int)delay) / 1000u;
/* receiving SSDP response packet */ /* receiving SSDP response packet */
for(n = 0; deviceTypes[deviceIndex]; deviceIndex++) { for(deviceIndex = 0; deviceTypes[deviceIndex]; deviceIndex++) {
if(n == 0) {
/* sending the SSDP M-SEARCH packet */ /* sending the SSDP M-SEARCH packet */
n = snprintf(bufr, sizeof(bufr), n = snprintf(bufr, sizeof(bufr),
MSearchMsgFmt, MSearchMsgFmt,
@ -617,7 +614,6 @@ upnpDiscoverDevices(const char * const deviceTypes[],
break; break;
} }
#endif /* #ifdef NO_GETADDRINFO */ #endif /* #ifdef NO_GETADDRINFO */
}
/* Waiting for SSDP REPLY packet to M-SEARCH */ /* Waiting for SSDP REPLY packet to M-SEARCH */
do { do {
n = receivedata(sudp, bufr, sizeof(bufr), delay, &scope_id); n = receivedata(sudp, bufr, sizeof(bufr), delay, &scope_id);
@ -629,14 +625,13 @@ upnpDiscoverDevices(const char * const deviceTypes[],
} else if (n == 0) { } else if (n == 0) {
/* no data or Time Out */ /* no data or Time Out */
if (devlist) { if (devlist) {
/* no more device type to look for... */ /* found some devices, stop now*/
if(error) if(error)
*error = UPNPDISCOVER_SUCCESS; *error = UPNPDISCOVER_SUCCESS;
#if 0
goto error; goto error;
#endif
} }
if(ipv6) { if(ipv6) {
/* switch linklocal flag */
if(linklocal) { if(linklocal) {
linklocal = 0; linklocal = 0;
--deviceIndex; --deviceIndex;
@ -649,7 +644,6 @@ 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;
/*printf("%d byte(s) :\n%s\n", n, bufr);*/ /* affichage du message */
parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize); parseMSEARCHReply(bufr, n, &descURL, &urlsize, &st, &stsize);
if(st&&descURL) { if(st&&descURL) {
#ifdef DEBUG #ifdef DEBUG
@ -672,7 +666,7 @@ upnpDiscoverDevices(const char * const deviceTypes[],
/* memory allocation error */ /* memory allocation error */
if(error) if(error)
*error = UPNPDISCOVER_MEMORY_ERROR; *error = UPNPDISCOVER_MEMORY_ERROR;
break; goto error;
} }
tmp->pNext = devlist; tmp->pNext = devlist;
tmp->descURL = tmp->buffer; tmp->descURL = tmp->buffer;