From 165aeef1290627ddd8c6634837a515881a55a0dd Mon Sep 17 00:00:00 2001 From: Thomas Bernard Date: Thu, 22 May 2014 10:12:26 +0200 Subject: [PATCH] miniupnpd/minissdp.c: clean SendSSDPNotify() code --- miniupnpd/minissdp.c | 56 +++++++++++++++++++++----------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/miniupnpd/minissdp.c b/miniupnpd/minissdp.c index b89f821..a94b9eb 100644 --- a/miniupnpd/minissdp.c +++ b/miniupnpd/minissdp.c @@ -76,12 +76,12 @@ AddMulticastMembershipIPv6(int s) /*unsigned int ifindex;*/ memset(&mr, 0, sizeof(mr)); - inet_pton(AF_INET6, LL_SSDP_MCAST_ADDR, &mr.ipv6mr_multiaddr); /*mr.ipv6mr_interface = ifindex;*/ mr.ipv6mr_interface = 0; /* 0 : all interfaces */ #ifndef IPV6_ADD_MEMBERSHIP #define IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP #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) { syslog(LOG_ERR, "setsockopt(udp, IPV6_ADD_MEMBERSHIP): %m"); @@ -483,14 +483,15 @@ static struct { }; 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, #ifdef ENABLE_HTTPS unsigned short https_port, #endif const char * nt, const char * suffix, const char * usn1, const char * usn2, const char * usn3, - unsigned int lifetime, int ipv6) + unsigned int lifetime) { char bufr[SSDP_PACKET_MAX_LEN]; int n, l; @@ -512,16 +513,17 @@ SendSSDPNotify(int s, const struct sockaddr * dest, "BOOTID.UPNP.ORG: %u\r\n" /* UDA v1.1 */ "CONFIGID.UPNP.ORG: %u\r\n" /* UDA v1.1 */ "\r\n", - ipv6 ? "[" LL_SSDP_MCAST_ADDR "]" : SSDP_MCAST_ADDR, - SSDP_PORT, - lifetime, - host, (unsigned int)http_port, + dest_str, SSDP_PORT, /* HOST: */ + lifetime, /* CACHE-CONTROL: */ + host, (unsigned int)http_port, /* LOCATION: */ #ifdef ENABLE_HTTPS - host, (unsigned int)https_port, + host, (unsigned int)https_port, /* SECURE-LOCATION: */ #endif - nt, suffix, /* NT: */ - usn1, usn2, usn3, suffix, /* USN: */ - upnp_bootid, upnp_bootid, upnp_configid ); + nt, suffix, /* NT: */ + usn1, usn2, usn3, suffix, /* USN: */ + upnp_bootid, /* 01-NLS: */ + upnp_bootid, /* BOOTID.UPNP.ORG: */ + upnp_configid ); /* CONFIGID.UPNP.ORG: */ if(l<0) { syslog(LOG_ERR, "%s: snprintf error", "SendSSDPNotify()"); @@ -533,13 +535,7 @@ SendSSDPNotify(int s, const struct sockaddr * dest, "SendSSDPNotify()", (unsigned)l, (unsigned)sizeof(bufr)); l = sizeof(bufr) - 1; } - n = sendto_or_schedule(s, bufr, l, 0, dest, -#ifdef ENABLE_IPV6 - ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in) -#else - sizeof(struct sockaddr_in) -#endif - ); + n = sendto_or_schedule(s, bufr, l, 0, dest, dest_len); if(n < 0) { 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 * sets e.g. a few hundred milliseconds. To avoid network congestion * discovery messages SHOULD NOT be sent more than three times. */ - n = sendto_schedule(s, bufr, l, 0, dest, -#ifdef ENABLE_IPV6 - ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in), -#else - sizeof(struct sockaddr_in), -#endif - 250); + n = sendto_schedule(s, bufr, l, 0, dest, dest_len, 250); if(n < 0) { 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 struct sockaddr_in sockname; #endif + socklen_t sockname_len; + const char * dest_str; int i=0; char ver_str[4]; @@ -591,9 +583,11 @@ SendSSDPNotifies(int s, const char * host, unsigned short http_port, if(ipv6) { struct sockaddr_in6 * p = (struct sockaddr_in6 *)&sockname; + sockname_len = sizeof(struct sockaddr_in6); p->sin6_family = AF_INET6; p->sin6_port = htons(SSDP_PORT); 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 ? */ /* UPnP Device Architecture 1.1 : * 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 { struct sockaddr_in *p = (struct sockaddr_in *)&sockname; + sockname_len = sizeof(struct sockaddr_in); p->sin_family = AF_INET; p->sin_port = htons(SSDP_PORT); p->sin_addr.s_addr = inet_addr(SSDP_MCAST_ADDR); + dest_str = SSDP_MCAST_ADDR; } while(known_service_types[i].s) @@ -619,24 +615,26 @@ SendSSDPNotifies(int s, const char * host, unsigned short http_port, ver_str[0] = '\0'; else 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 https_port, #endif known_service_types[i].s, ver_str, /* NT: */ known_service_types[i].uuid, "::", known_service_types[i].s, /* ver_str, USN: */ - lifetime, ipv6); + lifetime); if(0==memcmp(known_service_types[i].s, "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 https_port, #endif known_service_types[i].uuid, "", /* NT: */ known_service_types[i].uuid, "", "", /* ver_str, USN: */ - lifetime, ipv6); + lifetime); } i++; }