gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[GNUnet-SVN] r36464 - gnunet/src/nat


From: gnunet
Subject: [GNUnet-SVN] r36464 - gnunet/src/nat
Date: Wed, 7 Oct 2015 13:31:37 +0200

Author: grothoff
Date: 2015-10-07 13:31:37 +0200 (Wed, 07 Oct 2015)
New Revision: 36464

Modified:
   gnunet/src/nat/test_stun.c
Log:
-fix testcase to handle SUCCESS status reply, only recv UDP after STUN Request 
is out, etc.

Modified: gnunet/src/nat/test_stun.c
===================================================================
--- gnunet/src/nat/test_stun.c  2015-10-07 10:11:29 UTC (rev 36463)
+++ gnunet/src/nat/test_stun.c  2015-10-07 11:31:37 UTC (rev 36464)
@@ -24,8 +24,7 @@
  * @file nat/test_stun.c
  * @brief Testcase for STUN library
  * @author Bruno Souza Cabral
-
- *
+ * @author Christian Grothoff
  */
 
 
@@ -49,9 +48,11 @@
  * The port the test service is running on (default 7895)
  */
 static unsigned long port = 7895;
+
 static int ret = 1;
 
-static char *stun_server = "stun.ekiga.net";
+static const char *stun_server = "stun.gnunet.org";
+
 static int stun_port = 3478;
 
 /**
@@ -59,23 +60,27 @@
  */
 static struct GNUNET_NETWORK_Handle *lsock4;
 
-
 /**
  * The listen task ID for IPv4
  */
-static struct GNUNET_SCHEDULER_Task * ltask4;
+static struct GNUNET_SCHEDULER_Task *ltask4;
 
+/**
+ * Handle for the STUN request.
+ */
+static struct GNUNET_NAT_STUN_Handle *rh;
 
 
-
 static void
 print_answer(struct sockaddr_in* answer)
 {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,"External IP is: %s , with port %d\n", 
inet_ntoa(answer->sin_addr), ntohs(answer->sin_port));
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "External IP is: %s , with port %d\n",
+              inet_ntoa (answer->sin_addr),
+              ntohs (answer->sin_port));
 }
 
 
-
 /**
  * Function that terminates the test.
  */
@@ -82,22 +87,23 @@
 static void
 stop ()
 {
-  GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Stopping NAT and quitting...\n");
-
-  //Clean task
-  if(NULL != ltask4)
+  GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+              "Stopping NAT and quitting...\n");
+  if (NULL != ltask4)
   {
     GNUNET_SCHEDULER_cancel (ltask4);
     ltask4 = NULL;
   }
-
-  //Clean socket
   if(NULL != lsock4)
   {
     GNUNET_NETWORK_socket_close(lsock4);
     lsock4 = NULL;
   }
-
+  if (NULL != rh)
+  {
+    GNUNET_NAT_stun_make_request_cancel (rh);
+    rh = NULL;
+  }
 }
 
 
@@ -112,48 +118,41 @@
 do_udp_read (void *cls,
              const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-    //struct GNUNET_NAT_Test *tst = cls;
-       unsigned char reply_buf[1024];
-       ssize_t rlen;
-       struct sockaddr_in answer;
+  //struct GNUNET_NAT_Test *tst = cls;
+  unsigned char reply_buf[1024];
+  ssize_t rlen;
+  struct sockaddr_in answer;
 
-  if ((0 != (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) &&
-      (GNUNET_NETWORK_fdset_isset (tc->read_ready,
-                                   lsock4)))
-       {
-               rlen = GNUNET_NETWORK_socket_recv (lsock4, reply_buf, sizeof 
(reply_buf));
-
-
-               //Lets handle the packet
-               memset(&answer, 0, sizeof(struct sockaddr_in));
-
-    if(GNUNET_OK == GNUNET_NAT_stun_handle_packet(reply_buf, rlen, &answer))
-    {
-      //Print the answer
-      ret = 0;
-      print_answer(&answer);
-
-      //Destroy the connection
-      GNUNET_NETWORK_socket_close(lsock4);
-      lsock4 = NULL;
-
-    }
-    else
-    {
-      //Lets try again, its a invalid message
-      ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
-                                              lsock4, &do_udp_read, NULL);
-    }
-
-       }
-  else
+  ltask4 = NULL;
+  if ( (0 == (tc->reason & GNUNET_SCHEDULER_REASON_READ_READY)) ||
+       (! GNUNET_NETWORK_fdset_isset (tc->read_ready,
+                                      lsock4)) )
   {
-    //We got a timeout
-    ltask4 = NULL;
+    fprintf (stderr,
+             "Timeout waiting for STUN response\n");
     stop();
   }
-
-  ltask4 = NULL;
+  rlen = GNUNET_NETWORK_socket_recv (lsock4,
+                                     reply_buf,
+                                     sizeof (reply_buf));
+  memset (&answer,
+          0,
+          sizeof(struct sockaddr_in));
+  if (GNUNET_OK !=
+      GNUNET_NAT_stun_handle_packet (reply_buf,
+                                     rlen,
+                                     &answer))
+  {
+    fprintf (stderr,
+             "Unexpected UDP packet, trying to read more\n");
+    ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
+                                            lsock4,
+                                            &do_udp_read, NULL);
+    return;
+  }
+  ret = 0;
+  print_answer (&answer);
+  stop ();
 }
 
 
@@ -165,52 +164,67 @@
 static struct GNUNET_NETWORK_Handle *
 bind_v4 ()
 {
-    struct GNUNET_NETWORK_Handle *ls;
-    struct sockaddr_in sa4;
-    int eno;
+  struct GNUNET_NETWORK_Handle *ls;
+  struct sockaddr_in sa4;
+  int eno;
 
-    memset (&sa4, 0, sizeof (sa4));
-    sa4.sin_family = AF_INET;
-    sa4.sin_port = htons (port);
+  memset (&sa4, 0, sizeof (sa4));
+  sa4.sin_family = AF_INET;
+  sa4.sin_port = htons (port);
 #if HAVE_SOCKADDR_IN_SIN_LEN
-    sa4.sin_len = sizeof (sa4);
+  sa4.sin_len = sizeof (sa4);
 #endif
-    ls = GNUNET_NETWORK_socket_create (AF_INET,
-                                       SOCK_DGRAM,
-                                       0);
-    if (NULL == ls)
-        return NULL;
-    if (GNUNET_OK !=
-            GNUNET_NETWORK_socket_bind (ls, (const struct sockaddr *) &sa4,
-                                        sizeof (sa4)))
-    {
-        eno = errno;
-        GNUNET_NETWORK_socket_close (ls);
-        errno = eno;
-        return NULL;
-    }
-    return ls;
+  ls = GNUNET_NETWORK_socket_create (AF_INET,
+                                     SOCK_DGRAM,
+                                     0);
+  if (NULL == ls)
+    return NULL;
+  if (GNUNET_OK !=
+      GNUNET_NETWORK_socket_bind (ls,
+                                  (const struct sockaddr *) &sa4,
+                                  sizeof (sa4)))
+  {
+    eno = errno;
+    GNUNET_NETWORK_socket_close (ls);
+    errno = eno;
+    return NULL;
+  }
+  return ls;
 }
 
 
-
-static void request_callback(void *cls,
-enum GNUNET_NAT_StatusCode error)
+/**
+ * Function called with the result of the STUN request transmission attempt.
+ *
+ * @param cls unused
+ * @param error status code from STUN
+ */
+static void
+request_callback (void *cls,
+                  enum GNUNET_NAT_StatusCode error)
 {
-  if(error == GNUNET_NAT_ERROR_NOT_ONLINE)
+  rh = NULL;
+  if (GNUNET_NAT_ERROR_SUCCESS == error)
   {
-    //If we are not online, mark the test as success
-    ret = 0;
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
-                "test-stun detected as offline, cant make STUN request.\n"
-                );
+    /* all good, start to receive */
+    ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
+                                            lsock4,
+                                            &do_udp_read,
+                                            NULL);
+    return;
   }
+  if (error == GNUNET_NAT_ERROR_NOT_ONLINE)
+  {
+    ret = 77; /* report 'skip' */
+    fprintf (stderr,
+             "System is offline, cannot test STUN request.\n");
+  }
   else
   {
     ret = error;
   }
   stop();
-};
+}
 
 
 /**
@@ -217,35 +231,29 @@
  * Main function run with scheduler.
  */
 static void
-run (void *cls, char *const *args, const char *cfgfile,
+run (void *cls,
+     char *const *args,
+     const char *cfgfile,
      const struct GNUNET_CONFIGURATION_Handle *cfg)
 {
-
   //Lets create the socket
   lsock4 = bind_v4 ();
-  ltask4 = NULL;
   if (NULL == lsock4)
   {
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR, "bind");
-      GNUNET_SCHEDULER_shutdown ();
-      return;
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_ERROR,
+                         "bind");
+    GNUNET_SCHEDULER_shutdown ();
+    return;
   }
-  else
-  {
-      //Lets call our function now when it accepts
-      ltask4 = GNUNET_SCHEDULER_add_read_net (TIMEOUT,
-                                              lsock4, &do_udp_read, NULL );
-
-  }
-
   GNUNET_log (GNUNET_ERROR_TYPE_INFO,
               "Service listens on port %u\n",
               port);
-  GNUNET_NAT_stun_make_request (stun_server, stun_port, lsock4,
-                                &request_callback, NULL);
-
-  GNUNET_SCHEDULER_add_delayed (TIMEOUT, &stop, NULL);
-
+  rh = GNUNET_NAT_stun_make_request (stun_server,
+                                     stun_port,
+                                     lsock4,
+                                     &request_callback, NULL);
+  GNUNET_SCHEDULER_add_delayed (TIMEOUT,
+                                &stop, NULL);
 }
 
 
@@ -255,7 +263,6 @@
   struct GNUNET_GETOPT_CommandLineOption options[] = {
       GNUNET_GETOPT_OPTION_END
   };
-
   char *const argv_prog[] = {
       "test-stun",
       "-c",
@@ -262,14 +269,14 @@
       "test_stun.conf",
       NULL
   };
+  char *fn;
+  struct GNUNET_OS_Process *proc;
+
   GNUNET_log_setup ("test-stun",
                     "WARNING",
                     NULL);
 
   /* Lets start resolver */
-  char *fn;
-  struct GNUNET_OS_Process *proc;
-
   fn = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
   proc = GNUNET_OS_start_process (GNUNET_YES,
                                   GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
@@ -280,17 +287,20 @@
 
   if (NULL == proc)
   {
-    GNUNET_log (GNUNET_ERROR_TYPE_INFO, "This test was unable to start 
gnunet-service-resolver, and it is required to run ...\n");
+    GNUNET_log (GNUNET_ERROR_TYPE_INFO,
+                "This test was unable to start gnunet-service-resolver, and it 
is required to run ...\n");
     exit(1);
   }
 
-  GNUNET_PROGRAM_run (3, argv_prog, "test-stun", "nohelp", options, &run, 
NULL);
+  GNUNET_PROGRAM_run (3, argv_prog,
+                      "test-stun", "nohelp",
+                      options,
+                      &run, NULL);
 
-
   /* Now kill the resolver */
   if (0 != GNUNET_OS_process_kill (proc, GNUNET_TERM_SIG))
   {
-      GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
+    GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "kill");
   }
   GNUNET_OS_process_wait (proc);
   GNUNET_OS_process_destroy (proc);
@@ -297,8 +307,7 @@
   proc = NULL;
   GNUNET_free (fn);
 
-
-       return ret;
+  return ret;
 }
 
 /* end of test_stun.c */




reply via email to

[Prev in Thread] Current Thread [Next in Thread]