-avoid side-effect in assertion
[oweals/gnunet.git] / src / testbed / testbed_api_hosts.c
index e5b6d0a0b1807820d9793b3ad51a8c117678eb45..2593f5ff80383956f9dc8bfb0a013df3715bcd40 100644 (file)
@@ -126,16 +126,6 @@ struct TimeSlot
 struct GNUNET_TESTBED_Host
 {
 
-  /**
-   * The next pointer for DLL
-   */
-  struct GNUNET_TESTBED_Host *next;
-
-  /**
-   * The prev pointer for DLL
-   */
-  struct GNUNET_TESTBED_Host *prev;
-
   /**
    * The hostname of the host; NULL for localhost
    */
@@ -496,13 +486,13 @@ GNUNET_TESTBED_hosts_load_from_file (const char *filename,
     {
       data[offset] = '\0';
       ret =
-          SSCANF (buf, "%255[a-zA-Z0-9_]@%255[a-zA-Z0-9.]:%5hd", username,
+          SSCANF (buf, "%255[a-zA-Z0-9_]@%255[.a-zA-Z0-9-]:%5hd", username,
                   hostname, &port);
       if (3 == ret)
       {
-        GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                    "Successfully read host %s, port %d and user %s from file\n",
-                    hostname, port, username);
+        LOG (GNUNET_ERROR_TYPE_DEBUG,
+             "Successfully read host %s, port %d and user %s from file\n",
+             hostname, port, username);
         /* We store hosts in a static list; hence we only require the starting
          * host pointer in that list to access the newly created list of hosts */
         if (NULL == starting_host)
@@ -566,10 +556,22 @@ simple_resolve (const char *host)
   in_addr = (const struct sockaddr_in *) res->ai_addr;
   hostip = inet_ntoa (in_addr->sin_addr);
   GNUNET_assert (NULL != hostip);
+  freeaddrinfo (res);
   LOG_DEBUG ("Resolved [%s] to [%s]\n", host, hostip);
   return hostip;
 }
 
+#if ENABLE_LL
+static int
+cmpstringp(const void *p1, const void *p2)
+{
+  /* The actual arguments to this function are "pointers to
+     pointers to char", but strcmp(3) arguments are "pointers
+     to char", hence the following cast plus dereference */
+  
+  return strcmp(* (char * const *) p1, * (char * const *) p2);
+}
+#endif
 
 /**
  * Loads the set of host allocated by the LoadLeveler Job Scheduler.  This
@@ -598,8 +600,6 @@ GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
   char *buf;
   char *hostname;
   char **hostnames;
-  char **hostaddrs;
-  const char *hostip;
   struct GNUNET_TESTBED_Host **host_list;
   ssize_t rsize;
   uint64_t size;
@@ -612,7 +612,6 @@ GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
   } pstep;
   unsigned int host;
   unsigned int nhosts;
-  unsigned int nhostaddrs;
   
   if (NULL == (hostfile = getenv ("MP_SAVEHOSTFILE")))
   {
@@ -643,9 +642,7 @@ GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
   pstep = SCAN;
   hostname = NULL;
   hostnames = NULL;
-  hostaddrs = NULL;
   nhosts = 0;
-  nhostaddrs = 0;
   while (offset < size)
   {
     switch (pstep)
@@ -701,31 +698,18 @@ GNUNET_TESTBED_hosts_load_from_loadleveler (const struct
   GNUNET_free_non_null (buf);
   if (NULL == hostnames)
     return 0;
-  for (host = 0; host < nhosts; host++)
-  {
-    hostip = simple_resolve (hostnames[host]);
-    if (NULL == hostip)
-    {
-      nhosts = 0;
-      goto cleanup;
-    }
-    GNUNET_array_append (hostaddrs, nhostaddrs, GNUNET_strdup (hostip));
-  }
-  GNUNET_assert (nhostaddrs == nhosts);
   if (NULL == hosts)
     goto cleanup;
-  host_list = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * nhostaddrs);
+  qsort (hostnames, nhosts, sizeof (hostnames[0]), cmpstringp);
+  host_list = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Host *) * nhosts);
   for (host = 0; host < nhosts; host++)
-    host_list[host] = GNUNET_TESTBED_host_create (hostaddrs[host], NULL, cfg, 0);
+    host_list[host] = GNUNET_TESTBED_host_create (hostnames[host], NULL, cfg, 0);
   *hosts = host_list;
 
  cleanup:
   for (host = 0; host < nhosts; host++)
     GNUNET_free (hostnames[host]);
   GNUNET_free(hostnames);
-  for (host = 0; (NULL != hostaddrs) && (host < nhostaddrs); host++)
-    GNUNET_free (hostaddrs[host]);
-  GNUNET_free (hostaddrs);
   return nhosts;
 #endif
 }
@@ -805,6 +789,32 @@ GNUNET_TESTBED_mark_host_registered_at_ (struct GNUNET_TESTBED_Host *host,
 }
 
 
+/**
+ * Unmarks a host registered at a controller
+ *
+ * @param host the host to unmark
+ * @param controller the controller at which this host has to be unmarked
+ */
+void
+GNUNET_TESTBED_deregister_host_at_ (struct GNUNET_TESTBED_Host *host,
+                                    const struct GNUNET_TESTBED_Controller
+                                    *const controller)
+{
+  struct RegisteredController *rc;
+
+  for (rc = host->rc_head; NULL != rc; rc=rc->next)
+    if (controller == rc->controller)
+      break;
+  if (NULL == rc)
+  {
+    GNUNET_break (0);
+    return;
+  }
+  GNUNET_CONTAINER_DLL_remove (host->rc_head, host->rc_tail, rc);
+  GNUNET_free (rc);
+}
+
+
 /**
  * Checks whether a host has been registered
  *
@@ -1245,20 +1255,33 @@ GNUNET_TESTBED_controller_start (const char *trusted_ip,
 
 
 /**
- * Stop the controller process (also will terminate all peers and controllers
- * dependent on this controller).  This function blocks until the testbed has
- * been fully terminated (!). The controller status cb from
- * GNUNET_TESTBED_controller_start() will not be called.
+ * Sends termination signal to the controller's helper process
  *
- * @param cproc the controller process handle
+ * @param cproc the handle to the controller's helper process
  */
 void
-GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
+GNUNET_TESTBED_controller_kill_ (struct GNUNET_TESTBED_ControllerProc *cproc)
 {
   if (NULL != cproc->shandle)
     GNUNET_HELPER_send_cancel (cproc->shandle);
   if (NULL != cproc->helper)
-    GNUNET_HELPER_soft_stop (cproc->helper);
+    GNUNET_HELPER_kill (cproc->helper, GNUNET_YES);
+}
+
+
+/**
+ * Cleans-up the controller's helper process handle
+ *
+ * @param cproc the handle to the controller's helper process
+ */
+void
+GNUNET_TESTBED_controller_destroy_ (struct GNUNET_TESTBED_ControllerProc *cproc)
+{
+  if (NULL != cproc->helper)
+  {
+    GNUNET_break (GNUNET_OK == GNUNET_HELPER_wait (cproc->helper));
+    GNUNET_HELPER_destroy (cproc->helper);
+  }
   if (NULL != cproc->helper_argv)
     free_argv (cproc->helper_argv);
   cproc->host->controller_started = GNUNET_NO;
@@ -1267,6 +1290,22 @@ GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
 }
 
 
+/**
+ * Stop the controller process (also will terminate all peers and controllers
+ * dependent on this controller).  This function blocks until the testbed has
+ * been fully terminated (!). The controller status cb from
+ * GNUNET_TESTBED_controller_start() will not be called.
+ *
+ * @param cproc the controller process handle
+ */
+void
+GNUNET_TESTBED_controller_stop (struct GNUNET_TESTBED_ControllerProc *cproc)
+{
+  GNUNET_TESTBED_controller_kill_ (cproc);
+  GNUNET_TESTBED_controller_destroy_ (cproc);
+}
+
+
 /**
  * The handle for whether a host is habitable or not
  */
@@ -1353,6 +1392,7 @@ call_cb:
   cb = h->cb;
   cb_cls = h->cb_cls;
   host = h->host;
+  free_argv (h->helper_argv);
   GNUNET_free (h);
   if (NULL != cb)
     cb (cb_cls, host, ret);
@@ -1409,10 +1449,11 @@ GNUNET_TESTBED_is_host_habitable (const struct GNUNET_TESTBED_Host *host,
   stat_args[0] = "stat";
   stat_args[2] = NULL;
   rsh_suffix_args = gen_rsh_suffix_args ((const char **) stat_args);
+  GNUNET_free (stat_args[1]);
   h->helper_argv = join_argv ((const char **) rsh_args,
                               (const char **) rsh_suffix_args);
-  GNUNET_free (rsh_suffix_args);
-  GNUNET_free (rsh_args);
+  free_argv (rsh_suffix_args);
+  free_argv (rsh_args);
   h->auxp =
       GNUNET_OS_start_process_vap (GNUNET_NO, GNUNET_OS_INHERIT_STD_ERR, NULL,
                                    NULL, h->helper_argv[0], h->helper_argv);
@@ -1842,4 +1883,27 @@ GNUNET_TESTBED_host_handle_addhostconfirm_ (struct GNUNET_TESTBED_Controller *c,
   return GNUNET_OK;
 }
 
+
+/**
+ * Resolves the hostname of the host to an ip address
+ *
+ * @param host the host whose hostname is to be resolved
+ */
+void
+GNUNET_TESTBED_host_resolve_ (struct GNUNET_TESTBED_Host *host)
+{
+  char *hostname;
+
+  hostname = (char *) host->hostname;
+  host->hostname = simple_resolve (hostname);
+  if (NULL == host->hostname)
+  {
+    GNUNET_break (0);
+    host->hostname = hostname;
+    return;
+  }
+  GNUNET_free (hostname);
+  host->hostname = GNUNET_strdup (host->hostname);
+}
+
 /* end of testbed_api_hosts.c */