checkpoint save
[oweals/gnunet.git] / src / nat / nat_mini.c
index c40231067210a264faa3b7dae4430c7b69c54cff..5dffa8120baba31b38029769a2d0d0db52320bed 100644 (file)
@@ -28,7 +28,7 @@
 #include "gnunet_nat_lib.h"
 #include "nat.h"
 
-#define DEBUG_NAT GNUNET_NO
+#define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
 
 /**
  * How long do we give upnpc to create a mapping?
@@ -118,8 +118,9 @@ read_external_ipv4 (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   eh->task = GNUNET_SCHEDULER_NO_TASK;
   if (GNUNET_YES == GNUNET_NETWORK_fdset_handle_isset (tc->read_ready, eh->r))
-    ret = GNUNET_DISK_file_read (eh->r,
-                                 &eh->buf[eh->off], sizeof (eh->buf) - eh->off);
+    ret =
+        GNUNET_DISK_file_read (eh->r, &eh->buf[eh->off],
+                               sizeof (eh->buf) - eh->off);
   else
     ret = -1;                   /* error reading, timeout, etc. */
   if (ret > 0)
@@ -163,18 +164,20 @@ GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
 {
   struct GNUNET_NAT_ExternalHandle *eh;
 
+  if (GNUNET_SYSERR == GNUNET_OS_check_helper_binary ("external-ip"))
+    return NULL;
   eh = GNUNET_malloc (sizeof (struct GNUNET_NAT_ExternalHandle));
   eh->cb = cb;
   eh->cb_cls = cb_cls;
-  eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_NO, GNUNET_YES);
+  eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
   if (NULL == eh->opipe)
   {
     GNUNET_free (eh);
     return NULL;
   }
-  eh->eip = GNUNET_OS_start_process (NULL,
-                                     eh->opipe,
-                                     "external-ip", "external-ip", NULL);
+  eh->eip =
+    GNUNET_OS_start_process (GNUNET_NO, 0, NULL, eh->opipe, "external-ip", "external-ip",
+                               NULL);
   if (NULL == eh->eip)
   {
     GNUNET_DISK_pipe_close (eh->opipe);
@@ -184,8 +187,8 @@ GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
   GNUNET_DISK_pipe_close_end (eh->opipe, GNUNET_DISK_PIPE_END_WRITE);
   eh->timeout = GNUNET_TIME_relative_to_absolute (timeout);
   eh->r = GNUNET_DISK_pipe_handle (eh->opipe, GNUNET_DISK_PIPE_END_READ);
-  eh->task = GNUNET_SCHEDULER_add_read_file (timeout,
-                                             eh->r, &read_external_ipv4, eh);
+  eh->task =
+      GNUNET_SCHEDULER_add_read_file (timeout, eh->r, &read_external_ipv4, eh);
   return eh;
 }
 
@@ -199,7 +202,7 @@ void
 GNUNET_NAT_mini_get_external_ipv4_cancel (struct GNUNET_NAT_ExternalHandle *eh)
 {
   (void) GNUNET_OS_process_kill (eh->eip, SIGKILL);
-  GNUNET_OS_process_close (eh->eip);
+  GNUNET_OS_process_destroy (eh->eip);
   GNUNET_DISK_pipe_close (eh->opipe);
   if (GNUNET_SCHEDULER_NO_TASK != eh->task)
     GNUNET_SCHEDULER_cancel (eh->task);
@@ -288,7 +291,8 @@ do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
  * @param cls the 'struct GNUNET_NAT_MiniHandle'
  * @param line line of output, NULL at the end
  */
-static void process_map_output (void *cls, const char *line);
+static void
+process_map_output (void *cls, const char *line);
 
 
 /**
@@ -322,19 +326,15 @@ process_refresh_output (void *cls, const char *line)
         mini->did_map = GNUNET_NO;
       }
       GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) mini->port);
-      mini->map_cmd = GNUNET_OS_command_run (&process_map_output,
-                                             mini,
-                                             MAP_TIMEOUT,
-                                             "upnpc",
-                                             "upnpc",
-                                             "-r", pstr,
-                                             mini->is_tcp ? "tcp" : "udp",
-                                             NULL);
+      mini->map_cmd =
+          GNUNET_OS_command_run (&process_map_output, mini, MAP_TIMEOUT,
+                                 "upnpc", "upnpc", "-r", pstr,
+                                 mini->is_tcp ? "tcp" : "udp", NULL);
       if (NULL != mini->map_cmd)
         return;
     }
-    mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
-                                                       &do_refresh, mini);
+    mini->refresh_task =
+        GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
     return;
   }
   if (!mini->did_map)
@@ -363,12 +363,12 @@ process_refresh_output (void *cls, const char *line)
   }
   /*
    * we're looking for output of the form:
-   * 
+   *
    * "0 TCP  3000->192.168.2.150:3000  'libminiupnpc' ''"
    * "1 UDP  3001->192.168.2.150:3001  'libminiupnpc' ''"
-   * 
+   *
    * the pattern we look for is:
-   * 
+   *
    * "%s TCP  PORT->STRING:OURPORT *" or
    * "%s UDP  PORT->STRING:OURPORT *"
    */
@@ -377,10 +377,10 @@ process_refresh_output (void *cls, const char *line)
     return;                     /* skip */
   if (NULL == strstr (s, pstr))
     return;                     /* skip */
-  if (1 != sscanf (line,
-                   (mini->is_tcp)
-                   ? "%*u TCP  %u->%*s:%*u %*s"
-                   : "%*u UDP  %u->%*s:%*u %*s", &nport))
+  if (1 !=
+      SSCANF (line,
+              (mini->is_tcp) ? "%*u TCP  %u->%*s:%*u %*s" :
+              "%*u UDP  %u->%*s:%*u %*s", &nport))
     return;                     /* skip */
   mini->found = GNUNET_YES;
   if (nport == ntohs (mini->current_addr.sin_port))
@@ -410,10 +410,9 @@ do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 
   mini->refresh_task = GNUNET_SCHEDULER_NO_TASK;
   mini->found = GNUNET_NO;
-  mini->refresh_cmd = GNUNET_OS_command_run (&process_refresh_output,
-                                             mini,
-                                             MAP_TIMEOUT,
-                                             "upnpc", "upnpc", "-l", NULL);
+  mini->refresh_cmd =
+      GNUNET_OS_command_run (&process_refresh_output, mini, MAP_TIMEOUT,
+                             "upnpc", "upnpc", "-l", NULL);
 }
 
 
@@ -436,18 +435,18 @@ process_map_output (void *cls, const char *line)
   {
     GNUNET_OS_command_stop (mini->map_cmd);
     mini->map_cmd = NULL;
-    mini->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
-                                                       &do_refresh, mini);
+    mini->refresh_task =
+        GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
     return;
   }
   /*
    * The upnpc output we're after looks like this:
-   * 
+   *
    * "external 87.123.42.204:3000 TCP is redirected to internal 192.168.2.150:3000"
    */
   if ((NULL == (ipaddr = strstr (line, " "))) ||
       (NULL == (pstr = strstr (ipaddr, ":"))) ||
-      (1 != sscanf (pstr + 1, "%u", &port)))
+      (1 != SSCANF (pstr + 1, "%u", &port)))
   {
     return;                     /* skip line */
   }
@@ -478,7 +477,7 @@ process_map_output (void *cls, const char *line)
  * general-purpose 'GNUNET_NAT_register' code).  However, it can be
  * used if specifically UPnP-based NAT traversal is to be used or
  * tested.
- * 
+ *
  * @param port port to map
  * @param is_tcp GNUNET_YES to map TCP, GNUNET_NO for UDP
  * @param ac function to call with mapping result
@@ -486,8 +485,7 @@ process_map_output (void *cls, const char *line)
  * @return NULL on error (no 'upnpc' installed)
  */
 struct GNUNET_NAT_MiniHandle *
-GNUNET_NAT_mini_map_start (uint16_t port,
-                           int is_tcp,
+GNUNET_NAT_mini_map_start (uint16_t port, int is_tcp,
                            GNUNET_NAT_AddressCallback ac, void *ac_cls)
 {
   struct GNUNET_NAT_MiniHandle *ret;
@@ -501,17 +499,13 @@ GNUNET_NAT_mini_map_start (uint16_t port,
   ret->is_tcp = is_tcp;
   ret->port = port;
   GNUNET_snprintf (pstr, sizeof (pstr), "%u", (unsigned int) port);
-  ret->map_cmd = GNUNET_OS_command_run (&process_map_output,
-                                        ret,
-                                        MAP_TIMEOUT,
-                                        "upnpc",
-                                        "upnpc",
-                                        "-r", pstr,
-                                        is_tcp ? "tcp" : "udp", NULL);
+  ret->map_cmd =
+      GNUNET_OS_command_run (&process_map_output, ret, MAP_TIMEOUT, "upnpc",
+                             "upnpc", "-r", pstr, is_tcp ? "tcp" : "udp", NULL);
   if (NULL != ret->map_cmd)
     return ret;
-  ret->refresh_task = GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ,
-                                                    &do_refresh, ret);
+  ret->refresh_task =
+      GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, ret);
 
   return ret;
 }
@@ -530,9 +524,7 @@ process_unmap_output (void *cls, const char *line)
 
   if (NULL == line)
   {
-#if DEBUG_NAT
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG, "nat", "UPnP unmap done\n");
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG, "UPnP unmap done\n");
     GNUNET_OS_command_stop (mini->unmap_cmd);
     mini->unmap_cmd = NULL;
     GNUNET_free (mini);
@@ -547,7 +539,7 @@ process_unmap_output (void *cls, const char *line)
  * this function will give 'upnpc' 1s to remove tha mapping,
  * so while this function is non-blocking, a task will be
  * left with the scheduler for up to 1s past this call.
- * 
+ *
  * @param mini the handle
  */
 void
@@ -581,21 +573,14 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
   /* Note: oddly enough, deletion uses the external port whereas
    * addition uses the internal port; this rarely matters since they
    * often are the same, but it might... */
-  GNUNET_snprintf (pstr, sizeof (pstr),
-                   "%u", (unsigned int) ntohs (mini->current_addr.sin_port));
-#if DEBUG_NAT
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
-                   "nat",
-                   "Unmapping port %u with UPnP\n",
-                   ntohs (mini->current_addr.sin_port));
-#endif
-  mini->unmap_cmd = GNUNET_OS_command_run (&process_unmap_output,
-                                           mini,
-                                           UNMAP_TIMEOUT,
-                                           "upnpc",
-                                           "upnpc",
-                                           "-d", pstr,
-                                           mini->is_tcp ? "tcp" : "udp", NULL);
+  GNUNET_snprintf (pstr, sizeof (pstr), "%u",
+                   (unsigned int) ntohs (mini->current_addr.sin_port));
+  LOG (GNUNET_ERROR_TYPE_DEBUG, "Unmapping port %u with UPnP\n",
+       ntohs (mini->current_addr.sin_port));
+  mini->unmap_cmd =
+      GNUNET_OS_command_run (&process_unmap_output, mini, UNMAP_TIMEOUT,
+                             "upnpc", "upnpc", "-d", pstr,
+                             mini->is_tcp ? "tcp" : "udp", NULL);
 }