-indentation
[oweals/gnunet.git] / src / nat / nat_test.c
index 30db008034fc0b5e97375fb434b043b80fdad083..2836730caafcaf02eafecf674d09afd6e97ae8df 100644 (file)
@@ -28,6 +28,9 @@
 #include "gnunet_nat_lib.h"
 #include "nat.h"
 
+#define LOG(kind,...) GNUNET_log_from (kind, "nat", __VA_ARGS__)
+
+#define NAT_SERVER_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
 
 /**
  * Entry we keep for each incoming connection.
@@ -101,7 +104,7 @@ struct GNUNET_NAT_Test
   GNUNET_NAT_TestCallback report;
 
   /**
-   * Closure for 'report'.
+   * Closure for @e report.
    */
   void *report_cls;
 
@@ -139,6 +142,11 @@ struct GNUNET_NAT_Test
    * Identity of task for the listen socket (if any)
    */
   GNUNET_SCHEDULER_TaskIdentifier ltask;
+  
+  /**
+   * Task identifier for the timeout (if any)
+   */
+  GNUNET_SCHEDULER_TaskIdentifier ttask;
 
   /**
    * GNUNET_YES if we're testing TCP
@@ -159,33 +167,32 @@ struct GNUNET_NAT_Test
 
 
 /**
- * Function called from GNUNET_NAT_register whenever someone asks us
+ * Function called from #GNUNET_NAT_register whenever someone asks us
  * to do connection reversal.
  *
- * @param cls closure, our 'struct GNUNET_NAT_Handle'
+ * @param cls closure, our `struct GNUNET_NAT_Handle`
  * @param addr public IP address of the other peer
  * @param addrlen actual lenght of the address
  */
 static void
-reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen)
+reversal_cb (void *cls,
+             const struct sockaddr *addr,
+             socklen_t addrlen)
 {
   struct GNUNET_NAT_Test *h = cls;
   const struct sockaddr_in *sa;
 
-  if (addrlen != sizeof (struct sockaddr_in))
+  if (sizeof (struct sockaddr_in) != addrlen)
     return;
   sa = (const struct sockaddr_in *) addr;
   if (h->data != sa->sin_port)
   {
-#if DEBUG_NAT
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
-                     "nat",
-                     "Received connection reversal request for wrong port\n");
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Received connection reversal request for wrong port\n");
     return;                     /* wrong port */
   }
   /* report success */
-  h->report (h->report_cls, GNUNET_OK);
+  h->report (h->report_cls, GNUNET_NAT_ERROR_SUCCESS);
 }
 
 
@@ -193,36 +200,34 @@ reversal_cb (void *cls, const struct sockaddr *addr, socklen_t addrlen)
  * Activity on our incoming socket.  Read data from the
  * incoming connection.
  *
- * @param cls the 'struct NatActivity'
+ * @param cls the `struct GNUNET_NAT_Test`
  * @param tc scheduler context
  */
 static void
-do_udp_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_udp_read (void *cls,
+             const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_NAT_Test *tst = cls;
   uint16_t data;
 
-  tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
-                                              tst->lsock, &do_udp_read, tst);
+  tst->ltask =
+      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
+                                     tst->lsock,
+                                     &do_udp_read, tst);
   if ((NULL != tc->write_ready) &&
-      (GNUNET_NETWORK_fdset_isset (tc->read_ready,
-                                   tst->lsock)) &&
+      (GNUNET_NETWORK_fdset_isset (tc->read_ready, tst->lsock)) &&
       (sizeof (data) ==
        GNUNET_NETWORK_socket_recv (tst->lsock, &data, sizeof (data))))
   {
     if (data == tst->data)
-      tst->report (tst->report_cls, GNUNET_OK);
-#if DEBUG_NAT
+      tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
     else
-      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
-                       "nat", "Received data mismatches expected value\n");
-#endif
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "Received data mismatches expected value\n");
   }
-#if DEBUG_NAT
   else
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
-                     "nat", "Failed to receive data from inbound connection\n");
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Failed to receive data from inbound connection\n");
 }
 
 
@@ -230,11 +235,12 @@ do_udp_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Activity on our incoming socket.  Read data from the
  * incoming connection.
  *
- * @param cls the 'struct NatActivity'
+ * @param cls the `struct NatActivity`
  * @param tc scheduler context
  */
 static void
-do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_read (void *cls,
+         const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct NatActivity *na = cls;
   struct GNUNET_NAT_Test *tst;
@@ -244,24 +250,19 @@ do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   tst = na->h;
   GNUNET_CONTAINER_DLL_remove (tst->na_head, tst->na_tail, na);
   if ((NULL != tc->write_ready) &&
-      (GNUNET_NETWORK_fdset_isset (tc->read_ready,
-                                   na->sock)) &&
+      (GNUNET_NETWORK_fdset_isset (tc->read_ready, na->sock)) &&
       (sizeof (data) ==
        GNUNET_NETWORK_socket_recv (na->sock, &data, sizeof (data))))
   {
     if (data == tst->data)
-      tst->report (tst->report_cls, GNUNET_OK);
-#if DEBUG_NAT
+      tst->report (tst->report_cls, GNUNET_NAT_ERROR_SUCCESS);
     else
-      GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
-                       "nat" "Received data mismatches expected value\n");
-#endif
+      LOG (GNUNET_ERROR_TYPE_DEBUG,
+           "Received data does not match expected value\n");
   }
-#if DEBUG_NAT
   else
-    GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
-                     "nat", "Failed to receive data from inbound connection\n");
-#endif
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+         "Failed to receive data from inbound connection\n");
   GNUNET_NETWORK_socket_close (na->sock);
   GNUNET_free (na);
 }
@@ -271,11 +272,12 @@ do_read (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Activity on our listen socket. Accept the
  * incoming connection.
  *
- * @param cls the 'struct GNUNET_NAT_Test'
+ * @param cls the `struct GNUNET_NAT_Test`
  * @param tc scheduler context
  */
 static void
-do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
+do_accept (void *cls,
+           const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct GNUNET_NAT_Test *tst = cls;
   struct GNUNET_NETWORK_Handle *s;
@@ -284,23 +286,24 @@ do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   tst->ltask = GNUNET_SCHEDULER_NO_TASK;
   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN))
     return;
-  tst->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
-                                              tst->lsock, &do_accept, tst);
+  tst->ltask =
+      GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL, tst->lsock,
+                                     &do_accept, tst);
   s = GNUNET_NETWORK_socket_accept (tst->lsock, NULL, NULL);
   if (NULL == s)
   {
     GNUNET_log_strerror (GNUNET_ERROR_TYPE_INFO, "accept");
     return;                     /* odd error */
   }
-#if DEBUG_NAT
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
-                   "nat", "Got an inbound connection, waiting for data\n");
-#endif
-  wl = GNUNET_malloc (sizeof (struct NatActivity));
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Got an inbound connection, waiting for data\n");
+  wl = GNUNET_new (struct NatActivity);
   wl->sock = s;
   wl->h = tst;
-  wl->rtask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
-                                             wl->sock, &do_read, wl);
+  wl->rtask =
+    GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
+                                   wl->sock,
+                                   &do_read, wl);
   GNUNET_CONTAINER_DLL_insert (tst->na_head, tst->na_tail, wl);
 }
 
@@ -309,14 +312,16 @@ do_accept (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
  * Address-callback, used to send message to gnunet-nat-server.
  *
  * @param cls closure
- * @param add_remove GNUNET_YES to mean the new public IP address, GNUNET_NO to mean
+ * @param add_remove #GNUNET_YES to mean the new public IP address, #GNUNET_NO to mean
  *     the previous (now invalid) one
  * @param addr either the previous or the new public IP address
- * @param addrlen actual lenght of the address
+ * @param addrlen actual length of the @a addr
  */
 static void
 addr_cb (void *cls,
-         int add_remove, const struct sockaddr *addr, socklen_t addrlen)
+         int add_remove,
+         const struct sockaddr *addr,
+         socklen_t addrlen)
 {
   struct GNUNET_NAT_Test *h = cls;
   struct ClientActivity *ca;
@@ -327,13 +332,15 @@ addr_cb (void *cls,
   if (GNUNET_YES != add_remove)
     return;
   if (addrlen != sizeof (struct sockaddr_in))
+  {
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "NAT test ignores IPv6 address `%s' returned from NAT library\n",
+        GNUNET_a2s (addr, addrlen));
     return;                     /* ignore IPv6 here */
-#if DEBUG_NAT
-  GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG,
-                   "nat",
-                   "Asking gnunet-nat-server to connect to `%s'\n",
-                   GNUNET_a2s (addr, addrlen));
-#endif
+  }
+  LOG (GNUNET_ERROR_TYPE_INFO,
+       "Asking gnunet-nat-server to connect to `%s'\n",
+       GNUNET_a2s (addr, addrlen));
   sa = (const struct sockaddr_in *) addr;
   msg.header.size = htons (sizeof (struct GNUNET_NAT_TestMessage));
   msg.header.type = htons (GNUNET_MESSAGE_TYPE_NAT_TEST);
@@ -349,28 +356,50 @@ addr_cb (void *cls,
                 _("Failed to connect to `gnunet-nat-server'\n"));
     return;
   }
-  ca = GNUNET_malloc (sizeof (struct ClientActivity));
+  ca = GNUNET_new (struct ClientActivity);
   ca->client = client;
   GNUNET_CONTAINER_DLL_insert (h->ca_head, h->ca_tail, ca);
   GNUNET_break (GNUNET_OK ==
-                GNUNET_CLIENT_transmit_and_get_response (client,
-                                                         &msg.header,
-                                                         GNUNET_TIME_UNIT_SECONDS,
-                                                         GNUNET_YES,
-                                                         NULL, NULL));
+                GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
+                                                         NAT_SERVER_TIMEOUT,
+                                                         GNUNET_YES, NULL,
+                                                         NULL));
+}
+
+
+/**
+ * Timeout task for a nat test. 
+ * Calls the report-callback with a timeout return value
+ * 
+ * @param cls handle to the timed out NAT test
+ * @param tc not used
+ */
+static void
+do_timeout (void *cls,
+                 const struct GNUNET_SCHEDULER_TaskContext * tc)
+{
+  struct GNUNET_NAT_Test *nh = (struct GNUNET_NAT_Test *) cls;
+  
+  nh->ttask = GNUNET_SCHEDULER_NO_TASK;
+  nh->report (nh->report_cls, GNUNET_NAT_ERROR_TIMEOUT);
+  
+  GNUNET_NAT_test_stop(nh);
 }
 
 
 /**
  * Start testing if NAT traversal works using the
  * given configuration (IPv4-only).
+ * 
+ * ALL failures are reported directly to the report callback
  *
  * @param cfg configuration for the NAT traversal
- * @param is_tcp GNUNET_YES to test TCP, GNUNET_NO to test UDP
+ * @param is_tcp #GNUNET_YES to test TCP, #GNUNET_NO to test UDP
  * @param bnd_port port to bind to, 0 for connection reversal
  * @param adv_port externally advertised port to use
+ * @param timeout delay after which the test should be aborted
  * @param report function to call with the result of the test
- * @param report_cls closure for report
+ * @param report_cls closure for @a report
  * @return handle to cancel NAT test
  */
 struct GNUNET_NAT_Test *
@@ -378,9 +407,11 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
                        int is_tcp,
                        uint16_t bnd_port,
                        uint16_t adv_port,
-                       GNUNET_NAT_TestCallback report, void *report_cls)
+                       struct GNUNET_TIME_Relative timeout,
+                       GNUNET_NAT_TestCallback report,
+                       void *report_cls)
 {
-  struct GNUNET_NAT_Test *ret;
+  struct GNUNET_NAT_Test *nh;
   struct sockaddr_in sa;
   const struct sockaddr *addrs[] = { (const struct sockaddr *) &sa };
   const socklen_t addrlens[] = { sizeof (sa) };
@@ -392,56 +423,74 @@ GNUNET_NAT_test_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   sa.sin_len = sizeof (sa);
 #endif
 
-  ret = GNUNET_malloc (sizeof (struct GNUNET_NAT_Test));
-  ret->cfg = cfg;
-  ret->is_tcp = is_tcp;
-  ret->data = bnd_port;
-  ret->adv_port = adv_port;
-  ret->report = report;
-  ret->report_cls = report_cls;
-  if (bnd_port == 0)
+  nh = GNUNET_new (struct GNUNET_NAT_Test);
+  nh->cfg = cfg;
+  nh->is_tcp = is_tcp;
+  nh->data = bnd_port;
+  nh->adv_port = adv_port;
+  nh->report = report;
+  nh->report_cls = report_cls;
+  nh->ttask = GNUNET_SCHEDULER_NO_TASK;
+  if (0 == bnd_port)
   {
-    ret->nat = GNUNET_NAT_register (cfg, is_tcp,
-                                    0,
-                                    0, NULL, NULL, &addr_cb, &reversal_cb, ret);
+    nh->nat =
+        GNUNET_NAT_register (cfg, is_tcp, 0, 0, NULL, NULL, &addr_cb,
+                             &reversal_cb, nh);
   }
   else
   {
-    ret->lsock = GNUNET_NETWORK_socket_create (AF_INET,
-                                               (is_tcp == GNUNET_YES)
-                                               ? SOCK_STREAM : SOCK_DGRAM, 0);
-    if ((ret->lsock == NULL) ||
-        (GNUNET_OK != GNUNET_NETWORK_socket_bind (ret->lsock,
-                                                  (const struct sockaddr *) &sa,
-                                                  sizeof (sa))))
+    nh->lsock =
+        GNUNET_NETWORK_socket_create (AF_INET,
+                                      (is_tcp ==
+                                       GNUNET_YES) ? SOCK_STREAM : SOCK_DGRAM,
+                                      0);
+    if ((nh->lsock == NULL) ||
+        (GNUNET_OK !=
+         GNUNET_NETWORK_socket_bind (nh->lsock, (const struct sockaddr *) &sa,
+                                     sizeof (sa))))
     {
       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                  _
-                  ("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
+                  _("Failed to create listen socket bound to `%s' for NAT test: %s\n"),
                   GNUNET_a2s ((const struct sockaddr *) &sa, sizeof (sa)),
                   STRERROR (errno));
-      if (NULL != ret->lsock)
-        GNUNET_NETWORK_socket_close (ret->lsock);
-      GNUNET_free (ret);
+      if (NULL != nh->lsock)
+        GNUNET_NETWORK_socket_close (nh->lsock);
+      GNUNET_free (nh);
       return NULL;
     }
     if (GNUNET_YES == is_tcp)
     {
-      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (ret->lsock, 5));
-      ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
-                                                  ret->lsock, &do_accept, ret);
+      GNUNET_break (GNUNET_OK == GNUNET_NETWORK_socket_listen (nh->lsock, 5));
+      nh->ltask =
+          GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
+                                         nh->lsock, &do_accept, nh);
     }
     else
     {
-      ret->ltask = GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
-                                                  ret->lsock,
-                                                  &do_udp_read, ret);
+      nh->ltask =
+          GNUNET_SCHEDULER_add_read_net (GNUNET_TIME_UNIT_FOREVER_REL,
+                                         nh->lsock, &do_udp_read, nh);
+    }
+    LOG (GNUNET_ERROR_TYPE_DEBUG,
+        "NAT test listens on port %u (%s)\n",
+        bnd_port,
+        (GNUNET_YES == is_tcp) ? "tcp" : "udp");
+    nh->nat = GNUNET_NAT_register (cfg, is_tcp, adv_port, 1, addrs, addrlens,
+                             &addr_cb, NULL, nh);
+    if (NULL == nh->nat)
+    {
+      LOG (GNUNET_ERROR_TYPE_ERROR,
+          _("NAT test failed to start NAT library\n"));
+      if (GNUNET_SCHEDULER_NO_TASK != nh->ltask)
+        GNUNET_SCHEDULER_cancel (nh->ltask);
+      if (NULL != nh->lsock)
+        GNUNET_NETWORK_socket_close (nh->lsock);
+      GNUNET_free (nh);
+      return NULL;
     }
-    ret->nat = GNUNET_NAT_register (cfg, is_tcp,
-                                    adv_port,
-                                    1, addrs, addrlens, &addr_cb, NULL, ret);
   }
-  return ret;
+  nh->ttask = GNUNET_SCHEDULER_add_delayed (timeout, &do_timeout, nh);
+  return nh;
 }
 
 
@@ -456,10 +505,12 @@ GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
   struct NatActivity *pos;
   struct ClientActivity *cpos;
 
+  LOG (GNUNET_ERROR_TYPE_DEBUG,
+       "Stopping NAT test\n");
   while (NULL != (cpos = tst->ca_head))
   {
     GNUNET_CONTAINER_DLL_remove (tst->ca_head, tst->ca_tail, cpos);
-    GNUNET_CLIENT_disconnect (cpos->client, GNUNET_NO);
+    GNUNET_CLIENT_disconnect (cpos->client);
     GNUNET_free (cpos);
   }
   while (NULL != (pos = tst->na_head))
@@ -469,6 +520,8 @@ GNUNET_NAT_test_stop (struct GNUNET_NAT_Test *tst)
     GNUNET_NETWORK_socket_close (pos->sock);
     GNUNET_free (pos);
   }
+  if (GNUNET_SCHEDULER_NO_TASK != tst->ttask)
+    GNUNET_SCHEDULER_cancel (tst->ttask);
   if (GNUNET_SCHEDULER_NO_TASK != tst->ltask)
     GNUNET_SCHEDULER_cancel (tst->ltask);
   if (NULL != tst->lsock)