fixes
[oweals/gnunet.git] / src / testbed / gnunet-service-testbed.c
index 8ca7275c3fad3568d1c4abf7aa9372f69fa311d6..ea28af8d9bc2631ddb981d742f65f7ae7255aab4 100644 (file)
@@ -639,10 +639,16 @@ static struct Context *master_context;
  */
 static char *hostname;
 
+
 /***********/
 /* Handles */
 /***********/
 
+/**
+ * Our configuration
+ */
+static struct GNUNET_CONFIGURATION_Handle *our_config;
+
 /**
  * Current Transmit Handle; NULL if no notify transmit exists currently
  */
@@ -1436,7 +1442,9 @@ handle_init (void *cls, struct GNUNET_SERVER_Client *client,
   master_context->system =
       GNUNET_TESTING_system_create ("testbed", master_context->master_ip, hostname);
   host =
-      GNUNET_TESTBED_host_create_with_id (master_context->host_id, NULL, NULL,
+      GNUNET_TESTBED_host_create_with_id (master_context->host_id,
+                                          master_context->master_ip,
+                                          NULL,
                                           0);
   host_list_add (host);
   GNUNET_SERVER_client_keep (client);
@@ -1471,12 +1479,15 @@ handle_add_host (void *cls, struct GNUNET_SERVER_Client *client,
 
   msg = (const struct GNUNET_TESTBED_AddHostMessage *) message;
   msize = ntohs (msg->header.size);
-  username = (char *) &(msg[1]);
+  username = (char *) &msg[1];
   username_length = ntohs (msg->user_name_length);
-  GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length + 1));        /* msg must contain hostname */
   if (0 != username_length)
-    GNUNET_assert ('\0' == username[username_length]);
-  username_length = (0 == username_length) ? 0 : username_length + 1;
+    username_length++;
+  /* msg must contain hostname */
+  GNUNET_assert (msize > (sizeof (struct GNUNET_TESTBED_AddHostMessage) +
+                         username_length + 1));
+  if (0 != username_length)
+    GNUNET_assert ('\0' == username[username_length - 1]);
   hostname = username + username_length;
   hostname_length =
       msize - (sizeof (struct GNUNET_TESTBED_AddHostMessage) + username_length);
@@ -1662,7 +1673,8 @@ handle_link_controllers (void *cls, struct GNUNET_SERVER_Client *client,
   slave_host_id = ntohl (msg->slave_host_id);
   if ((slave_host_id >= host_list_size) || (NULL == host_list[slave_host_id]))
   {
-    LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host not registered with us\n");
+    LOG (GNUNET_ERROR_TYPE_WARNING, "Slave host %u not registered with us\n",
+        slave_host_id);
     GNUNET_SERVER_receive_done (client, GNUNET_SYSERR);
     return;
   }
@@ -2613,21 +2625,25 @@ static void
 focc_reg_completion_cc (void *cls, const char *emsg)
 {
   struct ForwardedOverlayConnectContext *focc = cls;
-  
+  struct GNUNET_CONFIGURATION_Handle *cfg;
+
   GNUNET_assert (FOCC_REGISTER == focc->state);
   focc->rhandle = NULL;
   GNUNET_assert (NULL == focc->sub_op);
   LOG_DEBUG ("Registering peer2's host successful\n");
-  if ((focc->peer2_host_id < slave_list_size) /* Check if we have the needed config */
-      && (NULL != slave_list[focc->peer2_host_id]))
+  if ((NULL == focc->gateway2)
+      || ((focc->peer2_host_id < slave_list_size) /* Check if we have the needed config */
+          && (NULL != slave_list[focc->peer2_host_id])))
   {
     focc->state = FOCC_LINK;
+    cfg = (NULL == focc->gateway2) ? 
+        our_config : slave_list[focc->peer2_host_id]->cfg;
     focc->sub_op = 
         GNUNET_TESTBED_controller_link_ (focc,
                                          focc->gateway,
                                          focc->peer2_host_id,
                                          peer_list[focc->peer1]->details.remote.remote_host_id,
-                                         slave_list[focc->peer2_host_id]->cfg,
+                                         cfg,
                                          GNUNET_NO);
     return;
   }
@@ -2708,7 +2724,6 @@ handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
   uint32_t p2; 
   uint32_t peer2_host_id;
 
-  
   msg = (const struct GNUNET_TESTBED_OverlayConnectMessage *) message;
   p1 = ntohl (msg->peer1);
   p2 = ntohl (msg->peer2);
@@ -2731,12 +2746,14 @@ handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
     route_to_peer2_host = NULL;
     route_to_peer1_host = NULL;
     route_to_peer2_host = find_dest_route (peer2_host_id);
-    if (NULL != route_to_peer2_host)
+    if ((NULL != route_to_peer2_host) 
+        || (peer2_host_id == master_context->host_id))
     {
       route_to_peer1_host = 
           find_dest_route (peer_list[p1]->details.remote.remote_host_id);
       GNUNET_assert (NULL != route_to_peer1_host);
-      if (route_to_peer2_host->dest != route_to_peer1_host->dest)
+      if ((peer2_host_id == master_context->host_id) 
+          || (route_to_peer2_host->dest != route_to_peer1_host->dest))
       {
         struct ForwardedOverlayConnectContext *focc;
         uint16_t msize;
@@ -2744,7 +2761,8 @@ handle_overlay_connect (void *cls, struct GNUNET_SERVER_Client *client,
         msize = sizeof (struct GNUNET_TESTBED_OverlayConnectMessage);
         focc = GNUNET_malloc (sizeof (struct ForwardedOverlayConnectContext));
         focc->gateway = peer->details.remote.controller;
-        focc->gateway2 = slave_list[route_to_peer2_host->dest]->controller;
+        focc->gateway2 = (NULL == route_to_peer2_host) ? NULL :
+            slave_list[route_to_peer2_host->dest]->controller;
         focc->peer1 = p1;
         focc->peer2 = p2;
         focc->peer2_host_id = peer2_host_id;
@@ -3179,6 +3197,7 @@ shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
     master_context = NULL;
   }
   GNUNET_free_non_null (hostname);
+  GNUNET_CONFIGURATION_destroy (our_config);
 }
 
 
@@ -3245,6 +3264,7 @@ testbed_run (void *cls, struct GNUNET_SERVER_Handle *server,
 
   GNUNET_assert (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string 
                 (cfg, "testbed", "HOSTNAME", &hostname));
+  our_config = GNUNET_CONFIGURATION_dup (cfg);
   GNUNET_SERVER_add_handlers (server, message_handlers);
   GNUNET_SERVER_disconnect_notify (server, &client_disconnect_cb, NULL);
   ss_map = GNUNET_CONTAINER_multihashmap_create (5, GNUNET_NO);