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,88 +534,86 @@ 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, ipv6 ?
ipv6 ? (linklocal ? "[" UPNP_MCAST_LL_ADDR "]" : "[" UPNP_MCAST_SL_ADDR "]")
(linklocal ? "[" UPNP_MCAST_LL_ADDR "]" : "[" UPNP_MCAST_SL_ADDR "]") : UPNP_MCAST_ADDR,
: UPNP_MCAST_ADDR, deviceTypes[deviceIndex], mx);
deviceTypes[deviceIndex], mx);
#ifdef DEBUG #ifdef DEBUG
/*printf("Sending %s", bufr);*/ /*printf("Sending %s", bufr);*/
printf("Sending M-SEARCH request to %s with ST: %s\n", printf("Sending M-SEARCH request to %s with ST: %s\n",
ipv6 ? ipv6 ?
(linklocal ? "[" UPNP_MCAST_LL_ADDR "]" : "[" UPNP_MCAST_SL_ADDR "]") (linklocal ? "[" UPNP_MCAST_LL_ADDR "]" : "[" UPNP_MCAST_SL_ADDR "]")
: UPNP_MCAST_ADDR, : UPNP_MCAST_ADDR,
deviceTypes[deviceIndex]); deviceTypes[deviceIndex]);
#endif #endif
#ifdef NO_GETADDRINFO #ifdef NO_GETADDRINFO
/* the following code is not using getaddrinfo */ /* the following code is not using getaddrinfo */
/* emission */ /* emission */
memset(&sockudp_w, 0, sizeof(struct sockaddr_storage)); memset(&sockudp_w, 0, sizeof(struct sockaddr_storage));
if(ipv6) { if(ipv6) {
struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockudp_w; struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockudp_w;
p->sin6_family = AF_INET6; p->sin6_family = AF_INET6;
p->sin6_port = htons(PORT); p->sin6_port = htons(PORT);
inet_pton(AF_INET6, inet_pton(AF_INET6,
linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR, linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR,
&(p->sin6_addr)); &(p->sin6_addr));
} else { } else {
struct sockaddr_in * p = (struct sockaddr_in *)&sockudp_w; struct sockaddr_in * p = (struct sockaddr_in *)&sockudp_w;
p->sin_family = AF_INET; p->sin_family = AF_INET;
p->sin_port = htons(PORT); p->sin_port = htons(PORT);
p->sin_addr.s_addr = inet_addr(UPNP_MCAST_ADDR); p->sin_addr.s_addr = inet_addr(UPNP_MCAST_ADDR);
}
n = sendto(sudp, bufr, n, 0, &sockudp_w,
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
if (n < 0) {
if(error)
*error = UPNPDISCOVER_SOCKET_ERROR;
PRINT_SOCKET_ERROR("sendto");
break;
}
#else /* #ifdef NO_GETADDRINFO */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /* AF_INET6 or AF_INET */
hints.ai_socktype = SOCK_DGRAM;
/*hints.ai_flags = */
if ((rv = getaddrinfo(ipv6
? (linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR)
: UPNP_MCAST_ADDR,
XSTR(PORT), &hints, &servinfo)) != 0) {
if(error)
*error = UPNPDISCOVER_SOCKET_ERROR;
#ifdef _WIN32
fprintf(stderr, "getaddrinfo() failed: %d\n", rv);
#else
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
#endif
break;
}
for(p = servinfo; p; p = p->ai_next) {
n = sendto(sudp, bufr, n, 0, p->ai_addr, p->ai_addrlen);
if (n < 0) {
#ifdef DEBUG
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
if (getnameinfo(p->ai_addr, p->ai_addrlen, hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
fprintf(stderr, "host:%s port:%s\n", hbuf, sbuf);
}
#endif
PRINT_SOCKET_ERROR("sendto");
continue;
}
}
freeaddrinfo(servinfo);
if(n < 0) {
if(error)
*error = UPNPDISCOVER_SOCKET_ERROR;
break;
}
#endif /* #ifdef NO_GETADDRINFO */
} }
n = sendto(sudp, bufr, n, 0, &sockudp_w,
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in));
if (n < 0) {
if(error)
*error = UPNPDISCOVER_SOCKET_ERROR;
PRINT_SOCKET_ERROR("sendto");
break;
}
#else /* #ifdef NO_GETADDRINFO */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC; /* AF_INET6 or AF_INET */
hints.ai_socktype = SOCK_DGRAM;
/*hints.ai_flags = */
if ((rv = getaddrinfo(ipv6
? (linklocal ? UPNP_MCAST_LL_ADDR : UPNP_MCAST_SL_ADDR)
: UPNP_MCAST_ADDR,
XSTR(PORT), &hints, &servinfo)) != 0) {
if(error)
*error = UPNPDISCOVER_SOCKET_ERROR;
#ifdef _WIN32
fprintf(stderr, "getaddrinfo() failed: %d\n", rv);
#else
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
#endif
break;
}
for(p = servinfo; p; p = p->ai_next) {
n = sendto(sudp, bufr, n, 0, p->ai_addr, p->ai_addrlen);
if (n < 0) {
#ifdef DEBUG
char hbuf[NI_MAXHOST], sbuf[NI_MAXSERV];
if (getnameinfo(p->ai_addr, p->ai_addrlen, hbuf, sizeof(hbuf), sbuf,
sizeof(sbuf), NI_NUMERICHOST | NI_NUMERICSERV) == 0) {
fprintf(stderr, "host:%s port:%s\n", hbuf, sbuf);
}
#endif
PRINT_SOCKET_ERROR("sendto");
continue;
}
}
freeaddrinfo(servinfo);
if(n < 0) {
if(error)
*error = UPNPDISCOVER_SOCKET_ERROR;
break;
}
#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;