WPS ER: Add more debug info on initialization errors
[libeap.git] / src / wps / wps_upnp_ssdp.c
index 3ffff09..c1b2193 100644 (file)
@@ -130,14 +130,15 @@ static int str_starts(const char *str, const char *start)
  * Note: next_advertisement is shared code with msearchreply_* functions
  */
 static struct wpabuf *
-next_advertisement(struct advertisement_state_machine *a, int *islast)
+next_advertisement(struct upnp_wps_device_sm *sm,
+                  struct advertisement_state_machine *a, int *islast)
 {
        struct wpabuf *msg;
        char *NTString = "";
        char uuid_string[80];
 
        *islast = 0;
-       uuid_bin2str(a->sm->wps->uuid, uuid_string, sizeof(uuid_string));
+       uuid_bin2str(sm->wps->uuid, uuid_string, sizeof(uuid_string));
        msg = wpabuf_alloc(800); /* more than big enough */
        if (msg == NULL)
                goto fail;
@@ -171,7 +172,7 @@ next_advertisement(struct advertisement_state_machine *a, int *islast)
        if (a->type != ADVERTISE_DOWN) {
                /* Where others may get our XML files from */
                wpabuf_printf(msg, "LOCATION: http://%s:%d/%s\r\n",
-                             a->sm->ip_addr_text, a->sm->web_port,
+                             sm->ip_addr_text, sm->web_port,
                              UPNP_WPS_DEVICE_XML_FILE);
        }
 
@@ -228,10 +229,40 @@ static void advertisement_state_machine_handler(void *eloop_data,
 /**
  * advertisement_state_machine_stop - Stop SSDP advertisements
  * @sm: WPS UPnP state machine from upnp_wps_device_init()
+ * @send_byebye: Send byebye advertisement messages immediately
  */
-void advertisement_state_machine_stop(struct upnp_wps_device_sm *sm)
+void advertisement_state_machine_stop(struct upnp_wps_device_sm *sm,
+                                     int send_byebye)
 {
+       struct advertisement_state_machine *a = &sm->advertisement;
+       int islast = 0;
+       struct wpabuf *msg;
+       struct sockaddr_in dest;
+
        eloop_cancel_timeout(advertisement_state_machine_handler, NULL, sm);
+       if (!send_byebye || sm->multicast_sd < 0)
+               return;
+
+       a->type = ADVERTISE_DOWN;
+       a->state = 0;
+
+       os_memset(&dest, 0, sizeof(dest));
+       dest.sin_family = AF_INET;
+       dest.sin_addr.s_addr = inet_addr(UPNP_MULTICAST_ADDRESS);
+       dest.sin_port = htons(UPNP_MULTICAST_PORT);
+
+       while (!islast) {
+               msg = next_advertisement(sm, a, &islast);
+               if (msg == NULL)
+                       break;
+               if (sendto(sm->multicast_sd, wpabuf_head(msg), wpabuf_len(msg),
+                          0, (struct sockaddr *) &dest, sizeof(dest)) < 0) {
+                       wpa_printf(MSG_INFO, "WPS UPnP: Advertisement sendto "
+                                  "failed: %d (%s)", errno, strerror(errno));
+               }
+               wpabuf_free(msg);
+               a->state++;
+       }
 }
 
 
@@ -260,7 +291,7 @@ static void advertisement_state_machine_handler(void *eloop_data,
         */
 
        wpa_printf(MSG_MSGDUMP, "WPS UPnP: Advertisement state=%d", a->state);
-       msg = next_advertisement(a, &islast);
+       msg = next_advertisement(sm, a, &islast);
        if (msg == NULL)
                return;
 
@@ -317,7 +348,7 @@ int advertisement_state_machine_start(struct upnp_wps_device_sm *sm)
        struct advertisement_state_machine *a = &sm->advertisement;
        int next_timeout_msec;
 
-       advertisement_state_machine_stop(sm);
+       advertisement_state_machine_stop(sm, 0);
 
        /*
         * Start out advertising down, this automatically switches
@@ -325,7 +356,6 @@ int advertisement_state_machine_start(struct upnp_wps_device_sm *sm)
         */
        a->type = ADVERTISE_DOWN;
        a->state = 0;
-       a->sm = sm;
        /* (other fields not used here) */
 
        /* First timeout should be random interval < 100 msec */
@@ -344,28 +374,15 @@ int advertisement_state_machine_start(struct upnp_wps_device_sm *sm)
  * They are sent in response to a UDP M-SEARCH packet.
  **************************************************************************/
 
-static void msearchreply_state_machine_handler(void *eloop_data,
-                                              void *user_ctx);
-
-
 /**
  * msearchreply_state_machine_stop - Stop M-SEARCH reply state machine
  * @a: Selected advertisement/reply state
  */
 void msearchreply_state_machine_stop(struct advertisement_state_machine *a)
 {
-       struct upnp_wps_device_sm *sm = a->sm;
        wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH stop");
-       if (a->next == a) {
-               sm->msearch_replies = NULL;
-       } else {
-               if (sm->msearch_replies == a)
-                       sm->msearch_replies = a->next;
-               a->next->prev = a->prev;
-               a->prev->next = a->next;
-       }
+       dl_list_del(&a->list);
        os_free(a);
-       sm->n_msearch_replies--;
 }
 
 
@@ -373,7 +390,7 @@ static void msearchreply_state_machine_handler(void *eloop_data,
                                               void *user_ctx)
 {
        struct advertisement_state_machine *a = user_ctx;
-       struct upnp_wps_device_sm *sm = a->sm;
+       struct upnp_wps_device_sm *sm = eloop_data;
        struct wpabuf *msg;
        int next_timeout_msec = 100;
        int next_timeout_sec = 0;
@@ -391,7 +408,7 @@ static void msearchreply_state_machine_handler(void *eloop_data,
        wpa_printf(MSG_MSGDUMP, "WPS UPnP: M-SEARCH reply state=%d (%s:%d)",
                   a->state, inet_ntoa(a->client.sin_addr),
                   ntohs(a->client.sin_port));
-       msg = next_advertisement(a, &islast);
+       msg = next_advertisement(sm, a, &islast);
        if (msg == NULL)
                return;
 
@@ -445,10 +462,12 @@ static void msearchreply_state_machine_start(struct upnp_wps_device_sm *sm,
        struct advertisement_state_machine *a;
        int next_timeout_sec;
        int next_timeout_msec;
+       int replies;
 
+       replies = dl_list_len(&sm->msearch_replies);
        wpa_printf(MSG_DEBUG, "WPS UPnP: M-SEARCH reply start (%d "
-                  "outstanding)", sm->n_msearch_replies);
-       if (sm->n_msearch_replies >= MAX_MSEARCH) {
+                  "outstanding)", replies);
+       if (replies >= MAX_MSEARCH) {
                wpa_printf(MSG_INFO, "WPS UPnP: Too many outstanding "
                           "M-SEARCH replies");
                return;
@@ -459,7 +478,6 @@ static void msearchreply_state_machine_start(struct upnp_wps_device_sm *sm,
                return;
        a->type = MSEARCH_REPLY;
        a->state = 0;
-       a->sm = sm;
        os_memcpy(&a->client, client, sizeof(*client));
        /* Wait time depending on MX value */
        next_timeout_msec = (1000 * mx * (os_random() & 0xFF)) >> 8;
@@ -472,15 +490,7 @@ static void msearchreply_state_machine_start(struct upnp_wps_device_sm *sm,
                goto fail;
        }
        /* Remember for future cleanup */
-       if (sm->msearch_replies) {
-               a->next = sm->msearch_replies;
-               a->prev = a->next->prev;
-               a->prev->next = a;
-               a->next->prev = a;
-       } else {
-               sm->msearch_replies = a->next = a->prev = a;
-       }
-       sm->n_msearch_replies++;
+       dl_list_add(&sm->msearch_replies, &a->list);
        return;
 
 fail:
@@ -514,7 +524,9 @@ fail:
 static void ssdp_parse_msearch(struct upnp_wps_device_sm *sm,
                               struct sockaddr_in *client, const char *data)
 {
+#ifndef CONFIG_NO_STDOUT_DEBUG
        const char *start = data;
+#endif /* CONFIG_NO_STDOUT_DEBUG */
        const char *end;
        int got_host = 0;
        int got_st = 0, st_match = 0;
@@ -863,11 +875,17 @@ int ssdp_open_multicast_sock(u32 ip_addr)
 #endif
 
        if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_IF,
-                      &ip_addr, sizeof(ip_addr)))
+                      &ip_addr, sizeof(ip_addr))) {
+               wpa_printf(MSG_DEBUG, "WPS: setsockopt(IP_MULTICAST_IF) %x: "
+                          "%d (%s)", ip_addr, errno, strerror(errno));
                return -1;
+       }
        if (setsockopt(sd, IPPROTO_IP, IP_MULTICAST_TTL,
-                      &ttl, sizeof(ttl)))
+                      &ttl, sizeof(ttl))) {
+               wpa_printf(MSG_DEBUG, "WPS: setsockopt(IP_MULTICAST_TTL): "
+                          "%d (%s)", errno, strerror(errno));
                return -1;
+       }
 
 #if 0   /* not needed, because we don't receive using multicast_sd */
        {