3 * Copyright (c) 2000-2003 Intel Corporation
4 * Copyright (c) 2006-2007 Sony Corporation
5 * Copyright (c) 2008-2009 Atheros Communications
6 * Copyright (c) 2009, Jouni Malinen <j@w1.fi>
8 * See wps_upnp.c for more details on licensing and code history.
14 #include <sys/ioctl.h>
15 #include <net/route.h>
22 #include "wps_upnp_i.h"
24 #define UPNP_CACHE_SEC (UPNP_CACHE_SEC_MIN + 1) /* cache time we use */
25 #define UPNP_CACHE_SEC_MIN 1800 /* min cachable time per UPnP standard */
26 #define UPNP_ADVERTISE_REPEAT 2 /* no more than 3 */
27 #define MAX_MSEARCH 20 /* max simultaneous M-SEARCH replies ongoing */
28 #define SSDP_TARGET "239.0.0.0"
29 #define SSDP_NETMASK "255.0.0.0"
32 /* Check tokens for equality, where tokens consist of letters, digits,
33 * underscore and hyphen, and are matched case insensitive.
35 static int token_eq(const char *s1, const char *s2)
44 if (isalpha(c1) && isupper(c1))
46 if (isalpha(c2) && isupper(c2))
48 end1 = !(isalnum(c1) || c1 == '_' || c1 == '-');
49 end2 = !(isalnum(c2) || c2 == '_' || c2 == '-');
50 if (end1 || end2 || c1 != c2)
53 return end1 && end2; /* reached end of both words? */
57 /* Return length of token (see above for definition of token) */
58 static int token_length(const char *s)
60 const char *begin = s;
63 int end = !(isalnum(c) || c == '_' || c == '-');
71 /* return length of interword separation.
72 * This accepts only spaces/tabs and thus will not traverse a line
75 static int word_separation_length(const char *s)
77 const char *begin = s;
80 if (c == ' ' || c == '\t')
88 /* No. of chars through (including) end of line */
89 static int line_length(const char *l)
92 while (*lp && *lp != '\n')
100 static int str_starts(const char *str, const char *start)
102 return os_strncmp(str, start, os_strlen(start)) == 0;
106 /***************************************************************************
108 * These are multicast to the world to tell them we are here.
109 * The individual packets are spread out in time to limit loss,
110 * and then after a much longer period of time the whole sequence
111 * is repeated again (for NOTIFYs only).
112 **************************************************************************/
115 * next_advertisement - Build next message and advance the state machine
116 * @a: Advertisement state
117 * @islast: Buffer for indicating whether this is the last message (= 1)
118 * Returns: The new message (caller is responsible for freeing this)
120 * Note: next_advertisement is shared code with msearchreply_* functions
122 static struct wpabuf *
123 next_advertisement(struct upnp_wps_device_sm *sm,
124 struct advertisement_state_machine *a, int *islast)
128 char uuid_string[80];
129 struct upnp_wps_device_interface *iface;
132 iface = dl_list_first(&sm->interfaces,
133 struct upnp_wps_device_interface, list);
134 uuid_bin2str(iface->wps->uuid, uuid_string, sizeof(uuid_string));
135 msg = wpabuf_alloc(800); /* more than big enough */
142 wpabuf_put_str(msg, "NOTIFY * HTTP/1.1\r\n");
143 wpabuf_printf(msg, "HOST: %s:%d\r\n",
144 UPNP_MULTICAST_ADDRESS, UPNP_MULTICAST_PORT);
145 wpabuf_printf(msg, "CACHE-CONTROL: max-age=%d\r\n",
147 wpabuf_printf(msg, "NTS: %s\r\n",
148 (a->type == ADVERTISE_UP ?
149 "ssdp:alive" : "ssdp:byebye"));
153 wpabuf_put_str(msg, "HTTP/1.1 200 OK\r\n");
154 wpabuf_printf(msg, "CACHE-CONTROL: max-age=%d\r\n",
157 wpabuf_put_str(msg, "DATE: ");
159 wpabuf_put_str(msg, "\r\n");
161 wpabuf_put_str(msg, "EXT:\r\n");
165 if (a->type != ADVERTISE_DOWN) {
166 /* Where others may get our XML files from */
167 wpabuf_printf(msg, "LOCATION: http://%s:%d/%s\r\n",
168 sm->ip_addr_text, sm->web_port,
169 UPNP_WPS_DEVICE_XML_FILE);
172 /* The SERVER line has three comma-separated fields:
173 * operating system / version
175 * software package / version
176 * However, only the UPnP version is really required, the
177 * others can be place holders... for security reasons
178 * it is better to NOT provide extra information.
180 wpabuf_put_str(msg, "SERVER: Unspecified, UPnP/1.0, Unspecified\r\n");
182 switch (a->state / UPNP_ADVERTISE_REPEAT) {
184 wpabuf_printf(msg, "%s: upnp:rootdevice\r\n", NTString);
185 wpabuf_printf(msg, "USN: uuid:%s::upnp:rootdevice\r\n",
189 wpabuf_printf(msg, "%s: uuid:%s\r\n", NTString, uuid_string);
190 wpabuf_printf(msg, "USN: uuid:%s\r\n", uuid_string);
193 wpabuf_printf(msg, "%s: urn:schemas-wifialliance-org:device:"
194 "WFADevice:1\r\n", NTString);
195 wpabuf_printf(msg, "USN: uuid:%s::urn:schemas-wifialliance-"
196 "org:device:WFADevice:1\r\n", uuid_string);
199 wpabuf_printf(msg, "%s: urn:schemas-wifialliance-org:service:"
200 "WFAWLANConfig:1\r\n", NTString);
201 wpabuf_printf(msg, "USN: uuid:%s::urn:schemas-wifialliance-"
202 "org:service:WFAWLANConfig:1\r\n", uuid_string);
205 wpabuf_put_str(msg, "\r\n");
207 if (a->state + 1 >= 4 * UPNP_ADVERTISE_REPEAT)
218 static void advertisement_state_machine_handler(void *eloop_data,
223 * advertisement_state_machine_stop - Stop SSDP advertisements
224 * @sm: WPS UPnP state machine from upnp_wps_device_init()
225 * @send_byebye: Send byebye advertisement messages immediately
227 void advertisement_state_machine_stop(struct upnp_wps_device_sm *sm,
230 struct advertisement_state_machine *a = &sm->advertisement;
233 struct sockaddr_in dest;
235 eloop_cancel_timeout(advertisement_state_machine_handler, NULL, sm);
236 if (!send_byebye || sm->multicast_sd < 0)
239 a->type = ADVERTISE_DOWN;
242 os_memset(&dest, 0, sizeof(dest));
243 dest.sin_family = AF_INET;
244 dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
245 dest.sin_port = htons(UPNP_MULTICAST_PORT);
248 msg = next_advertisement(sm, a, &islast);
251 if (sendto(sm->multicast_sd, wpabuf_head(msg), wpabuf_len(msg),
252 0, (struct sockaddr *) &dest, sizeof(dest)) < 0) {
253 wpa_printf(MSG_INFO, "WPS UPnP: Advertisement sendto "
254 "failed: %d (%s)", errno, strerror(errno));
262 static void advertisement_state_machine_handler(void *eloop_data,
265 struct upnp_wps_device_sm *sm = user_ctx;
266 struct advertisement_state_machine *a = &sm->advertisement;
268 int next_timeout_msec = 100;
269 int next_timeout_sec = 0;
270 struct sockaddr_in dest;
274 * Each is sent twice (in case lost) w/ 100 msec delay between;
275 * spec says no more than 3 times.
276 * One pair for rootdevice, one pair for uuid, and a pair each for
277 * each of the two urns.
278 * The entire sequence must be repeated before cache control timeout
279 * (which is min 1800 seconds),
280 * recommend random portion of half of the advertised cache control age
281 * to ensure against loss... perhaps 1800/4 + rand*1800/4 ?
282 * Delay random interval < 100 msec prior to initial sending.
286 wpa_printf(MSG_MSGDUMP, "WPS UPnP: Advertisement state=%d", a->state);
287 msg = next_advertisement(sm, a, &islast);
291 os_memset(&dest, 0, sizeof(dest));
292 dest.sin_family = AF_INET;
293 dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
294 dest.sin_port = htons(UPNP_MULTICAST_PORT);
296 if (sendto(sm->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0,
297 (struct sockaddr *) &dest, sizeof(dest)) == -1) {
298 wpa_printf(MSG_ERROR, "WPS UPnP: Advertisement sendto failed:"
299 "%d (%s)", errno, strerror(errno));
300 next_timeout_msec = 0;
301 next_timeout_sec = 10; /* ... later */
303 a->state = 0; /* wrap around */
304 if (a->type == ADVERTISE_DOWN) {
305 wpa_printf(MSG_DEBUG, "WPS UPnP: ADVERTISE_DOWN->UP");
306 a->type = ADVERTISE_UP;
307 /* do it all over again right away */
311 * Start over again after a long timeout
314 next_timeout_msec = 0;
315 os_get_random((void *) &r, sizeof(r));
316 next_timeout_sec = UPNP_CACHE_SEC / 4 +
317 (((UPNP_CACHE_SEC / 4) * r) >> 16);
318 sm->advertise_count++;
319 wpa_printf(MSG_DEBUG, "WPS UPnP: ADVERTISE_UP (#%u); "
321 sm->advertise_count, next_timeout_sec);
329 eloop_register_timeout(next_timeout_sec, next_timeout_msec,
330 advertisement_state_machine_handler, NULL, sm);
335 * advertisement_state_machine_start - Start SSDP advertisements
336 * @sm: WPS UPnP state machine from upnp_wps_device_init()
337 * Returns: 0 on success, -1 on failure
339 int advertisement_state_machine_start(struct upnp_wps_device_sm *sm)
341 struct advertisement_state_machine *a = &sm->advertisement;
342 int next_timeout_msec;
344 advertisement_state_machine_stop(sm, 0);
347 * Start out advertising down, this automatically switches
348 * to advertising up which signals our restart.
350 a->type = ADVERTISE_DOWN;
352 /* (other fields not used here) */
354 /* First timeout should be random interval < 100 msec */
355 next_timeout_msec = (100 * (os_random() & 0xFF)) >> 8;
356 return eloop_register_timeout(0, next_timeout_msec,
357 advertisement_state_machine_handler,
362 /***************************************************************************
364 * These are very similar to the multicast advertisements, with some
365 * small changes in data content; and they are sent (UDP) to a specific
366 * unicast address instead of multicast.
367 * They are sent in response to a UDP M-SEARCH packet.
368 **************************************************************************/
371 * msearchreply_state_machine_stop - Stop M-SEARCH reply state machine
372 * @a: Selected advertisement/reply state
374 void msearchreply_state_machine_stop(struct advertisement_state_machine *a)
376 wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH stop");
377 dl_list_del(&a->list);
382 static void msearchreply_state_machine_handler(void *eloop_data,
385 struct advertisement_state_machine *a = user_ctx;
386 struct upnp_wps_device_sm *sm = eloop_data;
388 int next_timeout_msec = 100;
389 int next_timeout_sec = 0;
393 * Each response is sent twice (in case lost) w/ 100 msec delay
394 * between; spec says no more than 3 times.
395 * One pair for rootdevice, one pair for uuid, and a pair each for
396 * each of the two urns.
399 /* TODO: should only send the requested response types */
401 wpa_printf(MSG_MSGDUMP, "WPS UPnP: M-SEARCH reply state=%d (%s:%d)",
402 a->state, inet_ntoa(a->client.sin_addr),
403 ntohs(a->client.sin_port));
404 msg = next_advertisement(sm, a, &islast);
409 * Send it on the multicast socket to avoid having to set up another
412 if (sendto(sm->multicast_sd, wpabuf_head(msg), wpabuf_len(msg), 0,
413 (struct sockaddr *) &a->client, sizeof(a->client)) < 0) {
414 wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH reply sendto "
415 "errno %d (%s) for %s:%d",
416 errno, strerror(errno),
417 inet_ntoa(a->client.sin_addr),
418 ntohs(a->client.sin_port));
419 /* Ignore error and hope for the best */
423 wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH reply done");
424 msearchreply_state_machine_stop(a);
429 wpa_printf(MSG_MSGDUMP, "WPS UPnP: M-SEARCH reply in %d.%03d sec",
430 next_timeout_sec, next_timeout_msec);
431 eloop_register_timeout(next_timeout_sec, next_timeout_msec,
432 msearchreply_state_machine_handler, sm, a);
437 * msearchreply_state_machine_start - Reply to M-SEARCH discovery request
438 * @sm: WPS UPnP state machine from upnp_wps_device_init()
439 * @client: Client address
440 * @mx: Maximum delay in seconds
442 * Use TTL of 4 (this was done when socket set up).
443 * A response should be given in randomized portion of min(MX,120) seconds
445 * UPnP-arch-DeviceArchitecture, 1.2.3:
446 * To be found, a device must send a UDP response to the source IP address and
447 * port that sent the request to the multicast channel. Devices respond if the
448 * ST header of the M-SEARCH request is "ssdp:all", "upnp:rootdevice", "uuid:"
449 * followed by a UUID that exactly matches one advertised by the device.
451 static void msearchreply_state_machine_start(struct upnp_wps_device_sm *sm,
452 struct sockaddr_in *client,
455 struct advertisement_state_machine *a;
456 int next_timeout_sec;
457 int next_timeout_msec;
460 replies = dl_list_len(&sm->msearch_replies);
461 wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH reply start (%d "
462 "outstanding)", replies);
463 if (replies >= MAX_MSEARCH) {
464 wpa_printf(MSG_INFO, "WPS UPnP: Too many outstanding "
469 a = os_zalloc(sizeof(*a));
472 a->type = MSEARCH_REPLY;
474 os_memcpy(&a->client, client, sizeof(*client));
475 /* Wait time depending on MX value */
476 next_timeout_msec = (1000 * mx * (os_random() & 0xFF)) >> 8;
477 next_timeout_sec = next_timeout_msec / 1000;
478 next_timeout_msec = next_timeout_msec % 1000;
479 if (eloop_register_timeout(next_timeout_sec, next_timeout_msec,
480 msearchreply_state_machine_handler, sm,
482 /* No way to recover (from malloc failure) */
485 /* Remember for future cleanup */
486 dl_list_add(&sm->msearch_replies, &a->list);
490 wpa_printf(MSG_INFO, "WPS UPnP: M-SEARCH reply failure!");
491 eloop_cancel_timeout(msearchreply_state_machine_handler, sm, a);
497 * ssdp_parse_msearch - Process a received M-SEARCH
498 * @sm: WPS UPnP state machine from upnp_wps_device_init()
499 * @client: Client address
500 * @data: NULL terminated M-SEARCH message
502 * Given that we have received a header w/ M-SEARCH, act upon it
504 * Format of M-SEARCH (case insensitive!):
506 * First line must be:
507 * M-SEARCH * HTTP/1.1
508 * Other lines in arbitrary order:
509 * HOST:239.255.255.250:1900
510 * ST:<varies -- must match>
511 * MAN:"ssdp:discover"
514 * It should be noted that when Microsoft Vista is still learning its IP
515 * address, it sends out host lines like: HOST:[FF02::C]:1900
517 static void ssdp_parse_msearch(struct upnp_wps_device_sm *sm,
518 struct sockaddr_in *client, const char *data)
520 #ifndef CONFIG_NO_STDOUT_DEBUG
521 const char *start = data;
522 #endif /* CONFIG_NO_STDOUT_DEBUG */
524 int got_st = 0, st_match = 0;
530 * Skip first line M-SEARCH * HTTP/1.1
531 * (perhaps we should check remainder of the line for syntax)
533 data += line_length(data);
535 /* Parse remaining lines */
536 for (; *data != '\0'; data += line_length(data)) {
537 if (token_eq(data, "host")) {
538 /* The host line indicates who the packet
539 * is addressed to... but do we really care?
540 * Note that Microsoft sometimes does funny
541 * stuff with the HOST: line.
544 data += token_length(data);
545 data += word_separation_length(data);
549 data += word_separation_length(data);
550 /* UPNP_MULTICAST_ADDRESS */
551 if (!str_starts(data, "239.255.255.250"))
553 data += os_strlen("239.255.255.250");
555 if (!str_starts(data, ":1900"))
558 #endif /* could be */
561 } else if (token_eq(data, "st")) {
562 /* There are a number of forms; we look
563 * for one that matches our case.
566 data += token_length(data);
567 data += word_separation_length(data);
571 data += word_separation_length(data);
572 if (str_starts(data, "ssdp:all")) {
576 if (str_starts(data, "upnp:rootdevice")) {
580 if (str_starts(data, "uuid:")) {
581 char uuid_string[80];
582 struct upnp_wps_device_interface *iface;
583 iface = dl_list_first(
585 struct upnp_wps_device_interface,
587 data += os_strlen("uuid:");
588 uuid_bin2str(iface->wps->uuid, uuid_string,
589 sizeof(uuid_string));
590 if (str_starts(data, uuid_string))
595 /* FIX: should we really reply to IGD string? */
596 if (str_starts(data, "urn:schemas-upnp-org:device:"
597 "InternetGatewayDevice:1")) {
602 if (str_starts(data, "urn:schemas-wifialliance-org:"
603 "service:WFAWLANConfig:1")) {
607 if (str_starts(data, "urn:schemas-wifialliance-org:"
608 "device:WFADevice:1")) {
613 } else if (token_eq(data, "man")) {
614 data += token_length(data);
615 data += word_separation_length(data);
619 data += word_separation_length(data);
620 if (!str_starts(data, "\"ssdp:discover\"")) {
621 wpa_printf(MSG_DEBUG, "WPS UPnP: Unexpected "
622 "M-SEARCH man-field");
627 } else if (token_eq(data, "mx")) {
628 data += token_length(data);
629 data += word_separation_length(data);
633 data += word_separation_length(data);
638 /* ignore anything else */
640 if (!got_host || !got_st || !got_man || !got_mx || mx < 0) {
641 wpa_printf(MSG_DEBUG, "WPS UPnP: Invalid M-SEARCH: %d %d %d "
642 "%d mx=%d", got_host, got_st, got_man, got_mx, mx);
646 wpa_printf(MSG_DEBUG, "WPS UPnP: Ignored M-SEARCH (no ST "
651 mx = 120; /* UPnP-arch-DeviceArchitecture, 1.2.3 */
652 msearchreply_state_machine_start(sm, client, mx);
656 wpa_printf(MSG_INFO, "WPS UPnP: Failed to parse M-SEARCH");
657 wpa_printf(MSG_MSGDUMP, "WPS UPnP: M-SEARCH data:\n%s", start);
661 /* Listening for (UDP) discovery (M-SEARCH) packets */
664 * ssdp_listener_stop - Stop SSDP listered
665 * @sm: WPS UPnP state machine from upnp_wps_device_init()
667 * This function stops the SSDP listener that was started by calling
668 * ssdp_listener_start().
670 void ssdp_listener_stop(struct upnp_wps_device_sm *sm)
672 if (sm->ssdp_sd_registered) {
673 eloop_unregister_sock(sm->ssdp_sd, EVENT_TYPE_READ);
674 sm->ssdp_sd_registered = 0;
677 if (sm->ssdp_sd != -1) {
682 eloop_cancel_timeout(msearchreply_state_machine_handler, sm,
687 static void ssdp_listener_handler(int sd, void *eloop_ctx, void *sock_ctx)
689 struct upnp_wps_device_sm *sm = sock_ctx;
690 struct sockaddr_in addr; /* client address */
693 char buf[MULTICAST_MAX_READ], *pos;
695 addr_len = sizeof(addr);
696 nread = recvfrom(sm->ssdp_sd, buf, sizeof(buf) - 1, 0,
697 (struct sockaddr *) &addr, &addr_len);
700 buf[nread] = '\0'; /* need null termination for algorithm */
702 if (str_starts(buf, "NOTIFY ")) {
704 * Silently ignore NOTIFYs to avoid filling debug log with
710 pos = os_strchr(buf, '\n');
713 wpa_printf(MSG_MSGDUMP, "WPS UPnP: Received SSDP packet from %s:%d: "
714 "%s", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port), buf);
718 /* Parse first line */
719 if (os_strncasecmp(buf, "M-SEARCH", os_strlen("M-SEARCH")) == 0 &&
720 !isgraph(buf[strlen("M-SEARCH")])) {
721 ssdp_parse_msearch(sm, &addr, buf);
725 /* Ignore anything else */
729 int ssdp_listener_open(void)
731 struct sockaddr_in addr;
732 struct ip_mreq mcast_addr;
734 /* per UPnP spec, keep IP packet time to live (TTL) small */
735 unsigned char ttl = 4;
738 sd = socket(AF_INET, SOCK_DGRAM, 0);
741 if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0)
743 if (setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)))
745 os_memset(&addr, 0, sizeof(addr));
746 addr.sin_family = AF_INET;
747 addr.sin_addr.s_addr = htonl(INADDR_ANY);
748 addr.sin_port = htons(UPNP_MULTICAST_PORT);
749 if (bind(sd, (struct sockaddr *) &addr, sizeof(addr)))
751 os_memset(&mcast_addr, 0, sizeof(mcast_addr));
752 mcast_addr.imr_interface.s_addr = htonl(INADDR_ANY);
753 mcast_addr.imr_multiaddr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
754 if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
755 (char *) &mcast_addr, sizeof(mcast_addr)))
757 if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL,
771 * ssdp_listener_start - Set up for receiving discovery (UDP) packets
772 * @sm: WPS UPnP state machine from upnp_wps_device_init()
773 * Returns: 0 on success, -1 on failure
775 * The SSDP listener is stopped by calling ssdp_listener_stop().
777 int ssdp_listener_start(struct upnp_wps_device_sm *sm)
779 sm->ssdp_sd = ssdp_listener_open();
781 if (eloop_register_sock(sm->ssdp_sd, EVENT_TYPE_READ,
782 ssdp_listener_handler, NULL, sm))
784 sm->ssdp_sd_registered = 1;
789 wpa_printf(MSG_ERROR, "WPS UPnP: ssdp_listener_start failed");
790 ssdp_listener_stop(sm);
796 * add_ssdp_network - Add routing entry for SSDP
797 * @net_if: Selected network interface name
798 * Returns: 0 on success, -1 on failure
800 * This function assures that the multicast address will be properly
801 * handled by Linux networking code (by a modification to routing tables).
802 * This must be done per network interface. It really only needs to be done
803 * once after booting up, but it does not hurt to call this more frequently
806 int add_ssdp_network(const char *net_if)
812 struct sockaddr_in *sin;
817 os_memset(&rt, 0, sizeof(rt));
818 sock = socket(AF_INET, SOCK_DGRAM, 0);
822 rt.rt_dev = (char *) net_if;
823 sin = aliasing_hide_typecast(&rt.rt_dst, struct sockaddr_in);
824 sin->sin_family = AF_INET;
826 sin->sin_addr.s_addr = inet_addr(SSDP_TARGET);
827 sin = aliasing_hide_typecast(&rt.rt_genmask, struct sockaddr_in);
828 sin->sin_family = AF_INET;
830 sin->sin_addr.s_addr = inet_addr(SSDP_NETMASK);
831 rt.rt_flags = RTF_UP;
832 if (ioctl(sock, SIOCADDRT, &rt) < 0) {
833 if (errno == EPERM) {
834 wpa_printf(MSG_DEBUG, "add_ssdp_network: No "
835 "permissions to add routing table entry");
836 /* Continue to allow testing as non-root */
837 } else if (errno != EEXIST) {
838 wpa_printf(MSG_INFO, "add_ssdp_network() ioctl errno "
839 "%d (%s)", errno, strerror(errno));
851 #else /* __linux__ */
853 #endif /* __linux__ */
857 int ssdp_open_multicast_sock(u32 ip_addr)
860 /* per UPnP-arch-DeviceArchitecture, 1. Discovery, keep IP packet
861 * time to live (TTL) small */
862 unsigned char ttl = 4;
864 sd = socket(AF_INET, SOCK_DGRAM, 0);
868 #if 0 /* maybe ok if we sometimes block on writes */
869 if (fcntl(sd, F_SETFL, O_NONBLOCK) != 0) {
875 if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF,
876 &ip_addr, sizeof(ip_addr))) {
877 wpa_printf(MSG_DEBUG, "WPS: setsockopt(IP_MULTICAST_IF) %x: "
878 "%d (%s)", ip_addr, errno, strerror(errno));
882 if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL,
883 &ttl, sizeof(ttl))) {
884 wpa_printf(MSG_DEBUG, "WPS: setsockopt(IP_MULTICAST_TTL): "
885 "%d (%s)", errno, strerror(errno));
890 #if 0 /* not needed, because we don't receive using multicast_sd */
893 mreq.imr_multiaddr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
894 mreq.imr_interface.s_addr = ip_addr;
895 wpa_printf(MSG_DEBUG, "WPS UPnP: Multicast addr 0x%x if addr "
897 mreq.imr_multiaddr.s_addr,
898 mreq.imr_interface.s_addr);
899 if (setsockopt(sd, IPPROTO_IP, IP_ADD_MEMBERSHIP, &mreq,
901 wpa_printf(MSG_ERROR,
902 "WPS UPnP: setsockopt "
903 "IP_ADD_MEMBERSHIP errno %d (%s)",
904 errno, strerror(errno));
909 #endif /* not needed */
912 * TODO: What about IP_MULTICAST_LOOP? It seems to be on by default?
913 * which aids debugging I suppose but isn't really necessary?
921 * ssdp_open_multicast - Open socket for sending multicast SSDP messages
922 * @sm: WPS UPnP state machine from upnp_wps_device_init()
923 * Returns: 0 on success, -1 on failure
925 int ssdp_open_multicast(struct upnp_wps_device_sm *sm)
927 sm->multicast_sd = ssdp_open_multicast_sock(sm->ip_addr);
928 if (sm->multicast_sd < 0)