miniupnpd/minissdp.c: clean SendSSDPNotify() code

This commit is contained in:
Thomas Bernard 2014-05-22 10:12:26 +02:00
parent 0d32445f57
commit 165aeef129
1 changed files with 27 additions and 29 deletions

View File

@ -76,12 +76,12 @@ AddMulticastMembershipIPv6(int s)
/*unsigned int ifindex;*/ /*unsigned int ifindex;*/
memset(&mr, 0, sizeof(mr)); memset(&mr, 0, sizeof(mr));
inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &mr.ipv6mr_multiaddr);
/*mr.ipv6mr_interface = ifindex;*/ /*mr.ipv6mr_interface = ifindex;*/
mr.ipv6mr_interface = 0; /* 0 : all interfaces */ mr.ipv6mr_interface = 0; /* 0 : all interfaces */
#ifndef IPV6_ADD_MEMBERSHIP #ifndef IPV6_ADD_MEMBERSHIP
#define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
#endif #endif
inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &mr.ipv6mr_multiaddr);
if(setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mr, sizeof(struct ipv6_mreq)) < 0) if(setsockopt(s, IPPROTO_IPV6, IPV6_ADD_MEMBERSHIP, &mr, sizeof(struct ipv6_mreq)) < 0)
{ {
syslog(LOG_ERR, "setsockopt(udp, IPV6_ADD_MEMBERSHIP): %m"); syslog(LOG_ERR, "setsockopt(udp, IPV6_ADD_MEMBERSHIP): %m");
@ -483,14 +483,15 @@ static struct {
}; };
static void static void
SendSSDPNotify(int s, const struct sockaddr * dest, SendSSDPNotify(int s, const struct sockaddr * dest, socklen_t dest_len,
const char * dest_str,
const char * host, unsigned short http_port, const char * host, unsigned short http_port,
#ifdef ENABLE_HTTPS #ifdef ENABLE_HTTPS
unsigned short https_port, unsigned short https_port,
#endif #endif
const char * nt, const char * suffix, const char * nt, const char * suffix,
const char * usn1, const char * usn2, const char * usn3, const char * usn1, const char * usn2, const char * usn3,
unsigned int lifetime, int ipv6) unsigned int lifetime)
{ {
char bufr[SSDP_PACKET_MAX_LEN]; char bufr[SSDP_PACKET_MAX_LEN];
int n, l; int n, l;
@ -512,16 +513,17 @@ SendSSDPNotify(int s, const struct sockaddr * dest,
"BOOTID.UPNP.ORG: %u\r\n" /* UDA v1.1 */ "BOOTID.UPNP.ORG: %u\r\n" /* UDA v1.1 */
"CONFIGID.UPNP.ORG: %u\r\n" /* UDA v1.1 */ "CONFIGID.UPNP.ORG: %u\r\n" /* UDA v1.1 */
"\r\n", "\r\n",
ipv6 ? "[" LL_SSDP_MCAST_ADDR "]" : SSDP_MCAST_ADDR, dest_str, SSDP_PORT, /* HOST: */
SSDP_PORT, lifetime, /* CACHE-CONTROL: */
lifetime, host, (unsigned int)http_port, /* LOCATION: */
host, (unsigned int)http_port,
#ifdef ENABLE_HTTPS #ifdef ENABLE_HTTPS
host, (unsigned int)https_port, host, (unsigned int)https_port, /* SECURE-LOCATION: */
#endif #endif
nt, suffix, /* NT: */ nt, suffix, /* NT: */
usn1, usn2, usn3, suffix, /* USN: */ usn1, usn2, usn3, suffix, /* USN: */
upnp_bootid, upnp_bootid, upnp_configid ); upnp_bootid, /* 01-NLS: */
upnp_bootid, /* BOOTID.UPNP.ORG: */
upnp_configid ); /* CONFIGID.UPNP.ORG: */
if(l<0) if(l<0)
{ {
syslog(LOG_ERR, "%s: snprintf error", "SendSSDPNotify()"); syslog(LOG_ERR, "%s: snprintf error", "SendSSDPNotify()");
@ -533,13 +535,7 @@ SendSSDPNotify(int s, const struct sockaddr * dest,
"SendSSDPNotify()", (unsigned)l, (unsigned)sizeof(bufr)); "SendSSDPNotify()", (unsigned)l, (unsigned)sizeof(bufr));
l = sizeof(bufr) - 1; l = sizeof(bufr) - 1;
} }
n = sendto_or_schedule(s, bufr, l, 0, dest, n = sendto_or_schedule(s, bufr, l, 0, dest, dest_len);
#ifdef ENABLE_IPV6
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)
#else
sizeof(struct sockaddr_in)
#endif
);
if(n < 0) if(n < 0)
{ {
syslog(LOG_ERR, "sendto(udp_notify=%d, %s): %m", s, syslog(LOG_ERR, "sendto(udp_notify=%d, %s): %m", s,
@ -553,13 +549,7 @@ SendSSDPNotify(int s, const struct sockaddr * dest,
* set of discovery messages more than once with some delay between * set of discovery messages more than once with some delay between
* sets e.g. a few hundred milliseconds. To avoid network congestion * sets e.g. a few hundred milliseconds. To avoid network congestion
* discovery messages SHOULD NOT be sent more than three times. */ * discovery messages SHOULD NOT be sent more than three times. */
n = sendto_schedule(s, bufr, l, 0, dest, n = sendto_schedule(s, bufr, l, 0, dest, dest_len, 250);
#ifdef ENABLE_IPV6
ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in),
#else
sizeof(struct sockaddr_in),
#endif
250);
if(n < 0) if(n < 0)
{ {
syslog(LOG_ERR, "sendto(udp_notify=%d, %s): %m", s, syslog(LOG_ERR, "sendto(udp_notify=%d, %s): %m", s,
@ -583,6 +573,8 @@ SendSSDPNotifies(int s, const char * host, unsigned short http_port,
#else #else
struct sockaddr_in sockname; struct sockaddr_in sockname;
#endif #endif
socklen_t sockname_len;
const char * dest_str;
int i=0; int i=0;
char ver_str[4]; char ver_str[4];
@ -591,9 +583,11 @@ SendSSDPNotifies(int s, const char * host, unsigned short http_port,
if(ipv6) if(ipv6)
{ {
struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockname; struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockname;
sockname_len = sizeof(struct sockaddr_in6);
p->sin6_family = AF_INET6; p->sin6_family = AF_INET6;
p->sin6_port = htons(SSDP_PORT); p->sin6_port = htons(SSDP_PORT);
inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &(p->sin6_addr)); inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &(p->sin6_addr));
dest_str = "[" LL_SSDP_MCAST_ADDR "]";
/* TODO : also send to SL_SSDP_MCAST_ADDR and GL_SSDP_MCAST_ADDR ? */ /* TODO : also send to SL_SSDP_MCAST_ADDR and GL_SSDP_MCAST_ADDR ? */
/* UPnP Device Architecture 1.1 : /* UPnP Device Architecture 1.1 :
* Devices MUST multicast SSDP messages for each of the UPnP-enabled * Devices MUST multicast SSDP messages for each of the UPnP-enabled
@ -608,9 +602,11 @@ SendSSDPNotifies(int s, const char * host, unsigned short http_port,
#endif #endif
{ {
struct sockaddr_in *p = (struct sockaddr_in *)&sockname; struct sockaddr_in *p = (struct sockaddr_in *)&sockname;
sockname_len = sizeof(struct sockaddr_in);
p->sin_family = AF_INET; p->sin_family = AF_INET;
p->sin_port = htons(SSDP_PORT); p->sin_port = htons(SSDP_PORT);
p->sin_addr.s_addr = inet_addr(SSDP_MCAST_ADDR); p->sin_addr.s_addr = inet_addr(SSDP_MCAST_ADDR);
dest_str = SSDP_MCAST_ADDR;
} }
while(known_service_types[i].s) while(known_service_types[i].s)
@ -619,24 +615,26 @@ SendSSDPNotifies(int s, const char * host, unsigned short http_port,
ver_str[0] = '\0'; ver_str[0] = '\0';
else else
snprintf(ver_str, sizeof(ver_str), "%d", known_service_types[i].version); snprintf(ver_str, sizeof(ver_str), "%d", known_service_types[i].version);
SendSSDPNotify(s, (struct sockaddr *)&sockname, host, http_port, SendSSDPNotify(s, (struct sockaddr *)&sockname, sockname_len, dest_str,
host, http_port,
#ifdef ENABLE_HTTPS #ifdef ENABLE_HTTPS
https_port, https_port,
#endif #endif
known_service_types[i].s, ver_str, /* NT: */ known_service_types[i].s, ver_str, /* NT: */
known_service_types[i].uuid, "::", known_service_types[i].uuid, "::",
known_service_types[i].s, /* ver_str, USN: */ known_service_types[i].s, /* ver_str, USN: */
lifetime, ipv6); lifetime);
if(0==memcmp(known_service_types[i].s, if(0==memcmp(known_service_types[i].s,
"urn:schemas-upnp-org:device", sizeof("urn:schemas-upnp-org:device")-1)) "urn:schemas-upnp-org:device", sizeof("urn:schemas-upnp-org:device")-1))
{ {
SendSSDPNotify(s, (struct sockaddr *)&sockname, host, http_port, SendSSDPNotify(s, (struct sockaddr *)&sockname, sockname_len, dest_str,
host, http_port,
#ifdef ENABLE_HTTPS #ifdef ENABLE_HTTPS
https_port, https_port,
#endif #endif
known_service_types[i].uuid, "", /* NT: */ known_service_types[i].uuid, "", /* NT: */
known_service_types[i].uuid, "", "", /* ver_str, USN: */ known_service_types[i].uuid, "", "", /* ver_str, USN: */
lifetime, ipv6); lifetime);
} }
i++; i++;
} }