(no commit message)
[oweals/gnunet.git] / src / nat / upnp-discover.c
index 715f2fcee44df74866796ab07f73bf88f0800067..5e955db450ecb021b2c13b8a797dbe7ea2cdc6a1 100644 (file)
@@ -85,11 +85,6 @@ typedef void (*download_cb) (char *data, void *cls);
  */
 struct download_cls
 {
-  /**
-   * Scheduler used for the download task.
-   */
-  struct GNUNET_SCHEDULER_Handle *sched;
-
   /**
    * curl_easy handle.
    */
@@ -243,8 +238,7 @@ download_prepare (struct download_cls *cls)
   GNUNET_NETWORK_fdset_copy_native (grs, &rs, max + 1);
   GNUNET_NETWORK_fdset_copy_native (gws, &ws, max + 1);
 
-  GNUNET_SCHEDULER_add_select (cls->sched,
-                               GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+  GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
                                GNUNET_SCHEDULER_NO_TASK,
                                rtime,
                                grs,
@@ -280,7 +274,7 @@ task_download (struct download_cls *cls,
       download_clean_up (cls);
       return;
     }
-  if (GNUNET_TIME_absolute_get_remaining (cls->end_time).value == 0)
+  if (GNUNET_TIME_absolute_get_remaining (cls->end_time).rel_value == 0)
     {
       GNUNET_log_from (GNUNET_ERROR_TYPE_WARNING, "UPnP",
                        _
@@ -352,14 +346,12 @@ task_download (struct download_cls *cls,
 /**
  * Download description from devices.
  *
- * @param sched the scheduler to use for the download task
  * @param url URL of the file to download
  * @param caller_cb user function to call when done
  * @caller_cls closure to pass to caller_cb
  */
 void
-download_device_description (struct GNUNET_SCHEDULER_Handle *sched,
-                             char *url, download_cb caller_cb,
+download_device_description (char *url, download_cb caller_cb,
                              void *caller_cls)
 {
   CURL *curl;
@@ -425,7 +417,6 @@ download_device_description (struct GNUNET_SCHEDULER_Handle *sched,
                    url);
 #endif
 
-  cls->sched = sched;
   cls->curl = curl;
   cls->multi = multi;
   cls->url = url;
@@ -587,11 +578,6 @@ struct UPNP_Dev_
  */
 struct UPNP_discover_cls
 {
-  /**
-   * Scheduler to use for networking tasks.
-   */
-  struct GNUNET_SCHEDULER_Handle *sched;
-
   /**
    * Remote address used for multicast emission and reception.
    */
@@ -696,18 +682,20 @@ get_absolute_url (const char *ref_url, int is_desc_file, const char *raw_url)
 
 
 /**
- * Construct control URL for device from its description URL and
- * UPNP_IGD_Data_ information. This involves resolving relative paths
+ * Construct control URL and service type for device from its description URL
+ * and UPNP_IGD_Data_ information. This involves resolving relative paths
  * and choosing between Common Interface Config and interface-specific
  * paths.
  *
  * @param desc_url URL to the description file of the device
  * @param data IGD information obtained from the description file
- * @returns a URL to control the IGD device, or the empty string
- *   in case of failure
+ * @param control_url place to store a URL to control the IGD device (will be
+ *   the empty string in case of failure)
+ * @param service_type place to store the service type corresponding to control_url
+ *   (will be the empty string in case of failure)
  */
-static char *
-format_control_urls (const char *desc_url, struct UPNP_IGD_Data_ *data)
+static void
+format_control_urls (const char *desc_url, struct UPNP_IGD_Data_ *data, char **control_url, char **service_type)
 {
   const char *ref_url;
   int is_desc_file;
@@ -724,11 +712,22 @@ format_control_urls (const char *desc_url, struct UPNP_IGD_Data_ *data)
     }
 
   if (data->control_url[0] != '\0')
-    return get_absolute_url (ref_url, is_desc_file, data->control_url);
+    {
+      *control_url = get_absolute_url (ref_url, is_desc_file, data->control_url);
+      *service_type = GNUNET_strdup (data->service_type);
+    }
   else if (data->control_url_CIF[0] != '\0')
-    return get_absolute_url (ref_url, is_desc_file, data->control_url_CIF);
+    {
+      *control_url = get_absolute_url (ref_url, is_desc_file, data->control_url_CIF);
+      *service_type = GNUNET_strdup (data->service_type_CIF);
+    }
   else
-    return GNUNET_strdup ("");
+    {
+      /* If no suitable URL-service type pair was found, set both to empty
+       * to avoid pretending things will work */
+      *control_url = GNUNET_strdup ("");
+      *service_type = GNUNET_strdup ("");
+    }
 }
 
 static void get_valid_igd (struct UPNP_discover_cls *cls);
@@ -803,23 +802,15 @@ get_valid_igd_receive (char *desc, void *data)
   memset (igd_data, 0, sizeof (struct UPNP_IGD_Data_));
   UPNP_IGD_parse_desc_ (desc, strlen (desc), igd_data);
 
-  cls->current_dev->control_url =
-    format_control_urls (cls->current_dev->desc_url, igd_data);
-
-  if (igd_data->service_type != '\0')
-    cls->current_dev->service_type = GNUNET_strdup (igd_data->service_type);
-  else if (igd_data->service_type_CIF != '\0')
-    cls->current_dev->service_type =
-      GNUNET_strdup (igd_data->service_type_CIF);
-  else
-    cls->current_dev->service_type = GNUNET_strdup ("");
+  format_control_urls (cls->current_dev->desc_url, igd_data,
+                       &cls->current_dev->control_url,
+                       &cls->current_dev->service_type);
 
   cls->current_dev->data = igd_data;
 
   /* Check whether device is connected */
   buffer = GNUNET_malloc (UPNP_COMMAND_BUFSIZE);
-  UPNP_command_ (cls->sched,
-                 cls->current_dev->control_url,
+  UPNP_command_ (cls->current_dev->control_url,
                  cls->current_dev->data->service_type,
                  "GetStatusInfo", NULL, buffer, UPNP_COMMAND_BUFSIZE,
                  get_valid_igd_connected_cb, cls);
@@ -900,7 +891,7 @@ get_valid_igd (struct UPNP_discover_cls *cls)
     }
 
   /* There are still devices to ask, go on */
-  download_device_description (cls->sched, cls->current_dev->desc_url,
+  download_device_description (cls->current_dev->desc_url,
                                get_valid_igd_receive, cls);
 }
 
@@ -908,7 +899,6 @@ static const char *const discover_type_list[] = {
   "urn:schemas-upnp-org:device:InternetGatewayDevice:1",
   "urn:schemas-upnp-org:service:WANIPConnection:1",
   "urn:schemas-upnp-org:service:WANPPPConnection:1",
-  "upnp:rootdevice",
   NULL
 };
 
@@ -955,7 +945,8 @@ discover_recv (void *data, const struct GNUNET_SCHEDULER_TaskContext *tc)
                                     &addrlen);
   if (received == GNUNET_SYSERR)
     {
-      PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_recvfrom");
+      if (errno != EAGAIN)
+       PRINT_SOCKET_ERROR ("GNUNET_NETWORK_socket_recvfrom");
     }
 #if DEBUG_UPNP
   else
@@ -998,16 +989,14 @@ discover_recv (void *data, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_NETWORK_fdset_zero (cls->fdset);
       GNUNET_NETWORK_fdset_set (cls->fdset, cls->sudp);
 
-      task_w = GNUNET_SCHEDULER_add_select (cls->sched,
-                                            GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+      task_w = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
                                             GNUNET_SCHEDULER_NO_TASK,
                                             GNUNET_TIME_relative_multiply
                                             (GNUNET_TIME_UNIT_SECONDS, 15),
                                             NULL, cls->fdset, &discover_send,
                                             cls);
 
-      GNUNET_SCHEDULER_add_select (cls->sched,
-                                   GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+      GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
                                    task_w,
                                    GNUNET_TIME_relative_multiply
                                    (GNUNET_TIME_UNIT_SECONDS, 5), cls->fdset,
@@ -1078,15 +1067,13 @@ discover_send (void *data, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * If several devices are found, a device that is connected to the WAN
  * is returned first (if any).
  *
- * @param sched scheduler to use for network tasks
  * @param multicastif network interface to send discovery messages, or NULL
  * @param addr address used to send messages on multicastif, or NULL
  * @param caller_cb user function to call when done
  * @param caller_cls closure to pass to caller_cb
  */
 void
-UPNP_discover_ (struct GNUNET_SCHEDULER_Handle *sched,
-                const char *multicastif,
+UPNP_discover_ (const char *multicastif,
                 const struct sockaddr *addr,
                 UPNP_discover_cb_ caller_cb, void *caller_cls)
 {
@@ -1128,7 +1115,6 @@ UPNP_discover_ (struct GNUNET_SCHEDULER_Handle *sched,
 
 
   cls = GNUNET_malloc (sizeof (struct UPNP_discover_cls));
-  cls->sched = sched;
   cls->sudp = sudp;
   cls->type_index = 0;
   cls->dev_list = NULL;
@@ -1269,15 +1255,13 @@ UPNP_discover_ (struct GNUNET_SCHEDULER_Handle *sched,
   GNUNET_NETWORK_fdset_zero (cls->fdset);
   GNUNET_NETWORK_fdset_set (cls->fdset, sudp);
 
-  task_w = GNUNET_SCHEDULER_add_select (sched,
-                                        GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+  task_w = GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
                                         GNUNET_SCHEDULER_NO_TASK,
                                         GNUNET_TIME_relative_multiply
                                         (GNUNET_TIME_UNIT_SECONDS, 15), NULL,
                                         cls->fdset, &discover_send, cls);
 
-  GNUNET_SCHEDULER_add_select (sched,
-                               GNUNET_SCHEDULER_PRIORITY_DEFAULT,
+  GNUNET_SCHEDULER_add_select (GNUNET_SCHEDULER_PRIORITY_DEFAULT,
                                task_w,
                                GNUNET_TIME_relative_multiply
                                (GNUNET_TIME_UNIT_SECONDS, 15), cls->fdset,