-indentation
[oweals/gnunet.git] / src / nat / nat_mini.c
index 7a2b08f46ea66f7711d88a6fd6acc612275007c9..ae15b5c13cfd4cde6d9c4cdda08139976159dd1e 100644 (file)
@@ -98,6 +98,10 @@ struct GNUNET_NAT_ExternalHandle
    */
   char buf[17];
 
+  /**
+   * Error code for better debugging and user feedback
+   */
+  enum GNUNET_NAT_FailureCode ret;
 };
 
 
@@ -115,15 +119,16 @@ read_external_ipv4 (void *cls,
   struct GNUNET_NAT_ExternalHandle *eh = cls;
   ssize_t ret;
   struct in_addr addr;
-  int iret;
 
   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);
-  else
+  else {
+    eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
     ret = -1;                   /* error reading, timeout, etc. */
+  }
   if (ret > 0)
   {
     /* try to read more */
@@ -134,25 +139,21 @@ read_external_ipv4 (void *cls,
                                         &read_external_ipv4, eh);
     return;
   }
-  iret = GNUNET_NO;
+  eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_OUTPUT_INVALID;
   if ((eh->off > 7) && (eh->buf[eh->off - 1] == '\n'))
   {
     eh->buf[eh->off - 1] = '\0';
     if (1 == inet_pton (AF_INET, eh->buf, &addr))
     {
-      if (0 == addr.s_addr)
-        iret = GNUNET_NO;       /* got 0.0.0.0 */
+      if (0 != addr.s_addr)
+        eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_ADDRESS_INVALID;       /* got 0.0.0.0 */
       else
-        iret = GNUNET_OK;
+        eh->ret = GNUNET_NAT_ERROR_SUCCESS;
     }
   }
   eh->cb (eh->cb_cls,
-          (GNUNET_OK == iret)
-          ? &addr :
-          NULL,
-          (GNUNET_OK == iret)
-          ? NULL
-          : _("no valid address was returned by `external-ip'"));
+          (GNUNET_NAT_ERROR_SUCCESS == eh->ret) ? &addr : NULL,
+          eh->ret);
   GNUNET_NAT_mini_get_external_ipv4_cancel (eh);
 }
 
@@ -172,7 +173,7 @@ signal_external_ip_error (void *cls,
   eh->task = GNUNET_SCHEDULER_NO_TASK;
   eh->cb (eh->cb_cls,
           NULL,
-          _("`external-ip' command not found"));
+          eh->ret);
   GNUNET_free (eh);
 }
 
@@ -183,7 +184,7 @@ signal_external_ip_error (void *cls,
  * @param timeout when to fail
  * @param cb function to call with result
  * @param cb_cls closure for @a cb
- * @return handle for cancellation (can only be used until @a cb is called), NULL on error
+ * @return handle for cancellation (can only be used until @a cb is called), never NULL
  */
 struct GNUNET_NAT_ExternalHandle *
 GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
@@ -194,11 +195,13 @@ GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
   eh = GNUNET_new (struct GNUNET_NAT_ExternalHandle);
   eh->cb = cb;
   eh->cb_cls = cb_cls;
+  eh->ret = GNUNET_NAT_ERROR_SUCCESS;
   if (GNUNET_SYSERR ==
       GNUNET_OS_check_helper_binary ("external-ip", GNUNET_NO, NULL))
   {
     LOG (GNUNET_ERROR_TYPE_INFO,
         _("`external-ip' command not found\n"));
+    eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_FOUND;
     eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
                                          eh);
     return eh;
@@ -208,6 +211,7 @@ GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
   eh->opipe = GNUNET_DISK_pipe (GNUNET_YES, GNUNET_YES, GNUNET_NO, GNUNET_YES);
   if (NULL == eh->opipe)
   {
+    eh->ret = GNUNET_NAT_ERROR_IPC_FAILURE;
     eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
                                          eh);
     return eh;
@@ -219,6 +223,7 @@ GNUNET_NAT_mini_get_external_ipv4 (struct GNUNET_TIME_Relative timeout,
   if (NULL == eh->eip)
   {
     GNUNET_DISK_pipe_close (eh->opipe);
+    eh->ret = GNUNET_NAT_ERROR_EXTERNAL_IP_UTILITY_NOT_EXECUTEABLE;
     eh->task = GNUNET_SCHEDULER_add_now (&signal_external_ip_error,
                                          eh);
     return eh;
@@ -363,7 +368,7 @@ run_upnpc_r (struct GNUNET_NAT_MiniHandle *mini)
     mini->ac (mini->ac_cls,
               GNUNET_SYSERR,
               NULL, 0,
-              _("Failed to run `upnpc` command"));
+              GNUNET_NAT_ERROR_UPNPC_FAILED);
     return;
   }
 }
@@ -398,7 +403,7 @@ process_refresh_output (void *cls, const char *line)
                   GNUNET_NO,
                   (const struct sockaddr *) &mini->current_addr,
                   sizeof (mini->current_addr),
-                  NULL);
+                  GNUNET_NAT_ERROR_SUCCESS);
         mini->did_map = GNUNET_NO;
       }
       run_upnpc_r (mini);
@@ -423,12 +428,12 @@ process_refresh_output (void *cls, const char *line)
     mini->ac (mini->ac_cls, GNUNET_NO,
               (const struct sockaddr *) &mini->current_addr,
               sizeof (mini->current_addr),
-              NULL);
+              GNUNET_NAT_ERROR_SUCCESS);
     mini->current_addr.sin_addr = exip;
     mini->ac (mini->ac_cls, GNUNET_YES,
               (const struct sockaddr *) &mini->current_addr,
               sizeof (mini->current_addr),
-              NULL);
+              GNUNET_NAT_ERROR_SUCCESS);
     return;
   }
   /*
@@ -460,12 +465,12 @@ process_refresh_output (void *cls, const char *line)
   mini->ac (mini->ac_cls, GNUNET_NO,
             (const struct sockaddr *) &mini->current_addr,
             sizeof (mini->current_addr),
-            NULL);
+            GNUNET_NAT_ERROR_SUCCESS);
   mini->current_addr.sin_port = htons ((uint16_t) nport);
   mini->ac (mini->ac_cls, GNUNET_YES,
             (const struct sockaddr *) &mini->current_addr,
             sizeof (mini->current_addr),
-            NULL);
+            GNUNET_NAT_ERROR_SUCCESS);
 }
 
 
@@ -509,7 +514,7 @@ do_refresh (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     mini->ac (mini->ac_cls,
               GNUNET_SYSERR,
               NULL, 0,
-              _("`upnpc' command took too long, process killed"));
+              GNUNET_NAT_ERROR_UPNPC_TIMEOUT);
 }
 
 
@@ -537,7 +542,7 @@ process_map_output (void *cls,
       mini->ac (mini->ac_cls,
                 GNUNET_SYSERR,
                 NULL, 0,
-                _("`upnpc' command failed to establish port mapping"));
+                GNUNET_NAT_ERROR_UPNPC_PORTMAP_FAILED);
     if (GNUNET_SCHEDULER_NO_TASK == mini->refresh_task)
       mini->refresh_task =
         GNUNET_SCHEDULER_add_delayed (MAP_REFRESH_FREQ, &do_refresh, mini);
@@ -572,7 +577,7 @@ process_map_output (void *cls,
   mini->ac (mini->ac_cls, GNUNET_YES,
             (const struct sockaddr *) &mini->current_addr,
             sizeof (mini->current_addr),
-            NULL);
+            GNUNET_NAT_ERROR_SUCCESS);
 }
 
 
@@ -605,7 +610,7 @@ GNUNET_NAT_mini_map_start (uint16_t port,
     ac (ac_cls,
         GNUNET_SYSERR,
         NULL, 0,
-        _("`upnpc` command not found"));
+        GNUNET_NAT_ERROR_UPNPC_NOT_FOUND);
     return NULL;
   }
   LOG (GNUNET_ERROR_TYPE_DEBUG,
@@ -625,7 +630,7 @@ GNUNET_NAT_mini_map_start (uint16_t port,
 /**
  * Process output from our 'unmap' command.
  *
- * @param cls the 'struct GNUNET_NAT_MiniHandle'
+ * @param cls the `struct GNUNET_NAT_MiniHandle`
  * @param line line of output, NULL at the end
  */
 static void
@@ -682,7 +687,7 @@ GNUNET_NAT_mini_map_stop (struct GNUNET_NAT_MiniHandle *mini)
   mini->ac (mini->ac_cls, GNUNET_NO,
             (const struct sockaddr *) &mini->current_addr,
             sizeof (mini->current_addr),
-            NULL);
+            GNUNET_NAT_ERROR_SUCCESS);
   /* 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... */