clique topology optimization, progress meter for connecting
[oweals/gnunet.git] / src / testing / testing.c
index c5ff0eb7bd0fe705231fba10666df7aee81dcfa1..4a321c6e640836cc827bf4cc2d829c875d760cc6 100644 (file)
@@ -37,7 +37,7 @@
 #include "gnunet_transport_service.h"
 #include "gnunet_hello_lib.h"
 
-#define DEBUG_TESTING GNUNET_YES
+#define DEBUG_TESTING GNUNET_NO
 #define DEBUG_TESTING_RECONNECT GNUNET_YES
 
 /**
@@ -65,10 +65,19 @@ static void
 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
 {
   struct GNUNET_TESTING_Daemon *daemon = cls;
+  GNUNET_TESTING_NotifyDaemonRunning cb;
+
   int msize;
   if (daemon == NULL)
     return;
 
+  GNUNET_assert (daemon->phase == SP_GET_HELLO);
+
+  cb = daemon->cb;
+  daemon->cb = NULL;
+  if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
+    GNUNET_SCHEDULER_cancel(daemon->task);
+
   if (daemon->server != NULL)
     {
 #if DEBUG_TESTING
@@ -105,9 +114,15 @@ process_hello (void *cls, const struct GNUNET_MessageHeader *message)
       GNUNET_TRANSPORT_disconnect (daemon->th);
       daemon->th = NULL;
     }
+  daemon->phase = SP_START_DONE;
 
+  if (NULL != cb) /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
+    cb (daemon->cb_cls, &daemon->id, daemon->cfg, daemon, NULL);
 }
 
+static void
+start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
 /**
  * Function called after GNUNET_CORE_connect has succeeded
  * (or failed for good).  Note that the private key of the
@@ -127,12 +142,10 @@ testing_init (void *cls,
               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
 {
   struct GNUNET_TESTING_Daemon *d = cls;
-  GNUNET_TESTING_NotifyDaemonRunning cb;
 
   GNUNET_assert (d->phase == SP_START_CORE);
-  d->phase = SP_START_DONE;
-  cb = d->cb;
-  d->cb = NULL;
+  d->phase = SP_GET_HELLO;
+
   if (server == NULL)
     {
       d->server = NULL;
@@ -141,8 +154,8 @@ testing_init (void *cls,
                                     GNUNET_TIME_absolute_get_remaining
                                     (d->max_timeout), d->dead_cb,
                                     d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
-      else if (NULL != cb)
-        cb (d->cb_cls, NULL, d->cfg, d,
+      else if (NULL != d->cb)
+        d->cb (d->cb_cls, NULL, d->cfg, d,
             _("Failed to connect to core service\n"));
       return;
     }
@@ -155,9 +168,6 @@ testing_init (void *cls,
   d->server = server;
   d->running = GNUNET_YES;
 
-  if (NULL != cb)               /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
-    cb (d->cb_cls, my_identity, d->cfg, d, NULL);
-
   if (GNUNET_NO == d->running)
     {
 #if DEBUG_TESTING
@@ -192,6 +202,12 @@ testing_init (void *cls,
 #endif
 
   GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
+  /* wait some more */
+  if (d->task != GNUNET_SCHEDULER_NO_TASK)
+    GNUNET_SCHEDULER_cancel(d->task);
+  d->task
+    = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
+                                    &start_fsm, d);
 }
 
 
@@ -385,6 +401,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           return;
         }
       d->hostkeybuf[103] = '\0';
+
       if ((bytes_read < 0) ||
           (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
                                                         &d->id.hashPubKey)))
@@ -452,7 +469,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       if (NULL == d->hostname)
         {
 #if DEBUG_TESTING
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                       "Starting `%s', with command `%s %s %s %s %s %s'.\n",
                       "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
                       "-L", "DEBUG", "-s");
@@ -472,7 +489,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
             dst = GNUNET_strdup (d->hostname);
 
 #if DEBUG_TESTING
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+          GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                       "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
                       "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
                       d->cfgfile, "-L", "DEBUG", "-s", "-q");
@@ -525,7 +542,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           return;
         }
 #if DEBUG_TESTING
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                   "Started `%s', waiting for `%s' to be up.\n",
                   "gnunet-arm", "gnunet-service-core");
 #endif
@@ -550,6 +567,12 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                     (NULL == d->hostname)
                     ? _("`gnunet-arm' does not seem to terminate.\n")
                     : _("`ssh' does not seem to terminate.\n"));
+              GNUNET_CONFIGURATION_destroy (d->cfg);
+              GNUNET_free (d->cfgfile);
+              GNUNET_free_non_null (d->hostname);
+              GNUNET_free_non_null (d->username);
+              GNUNET_free(d->proc);
+              GNUNET_free(d);
               return;
             }
           /* wait some more */
@@ -559,22 +582,77 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           return;
         }
 #if DEBUG_TESTING
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                   "Successfully started `%s'.\n", "gnunet-arm");
 #endif
+      GNUNET_free(d->proc);
       d->phase = SP_START_CORE;
-      d->server = GNUNET_CORE_connect (d->cfg, 1,
-#if NO_MORE_TIMEOUT_FIXME
-                                       ARM_START_WAIT,
+#if DEBUG_TESTING
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                  "Calling CORE_connect\n");
 #endif
+      /* Fall through */
+    case SP_START_CORE:
+      if (d->server != NULL)
+        GNUNET_CORE_disconnect(d->server);
+
+      if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
+          0)
+        {
+          cb = d->cb;
+          d->cb = NULL;
+          if (NULL != cb)
+            cb (d->cb_cls,
+                NULL,
+                d->cfg,
+                d,
+                _("Unable to connect to CORE service for peer!\n"));
+          GNUNET_CONFIGURATION_destroy (d->cfg);
+          GNUNET_free (d->cfgfile);
+          GNUNET_free_non_null (d->hostname);
+          GNUNET_free_non_null (d->username);
+          GNUNET_free (d);
+          return;
+        }
+      d->server = GNUNET_CORE_connect (d->cfg, 1,
                                        d,
                                        &testing_init,
                                        NULL, NULL, NULL,
                                        NULL, GNUNET_NO,
                                        NULL, GNUNET_NO, no_handlers);
+      d->task
+        = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
+                                        &start_fsm, d);
       break;
-    case SP_START_CORE:
-      GNUNET_break (0);
+    case SP_GET_HELLO:
+      if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
+          0)
+        {
+          if (d->server != NULL)
+            GNUNET_CORE_disconnect(d->server);
+          if (d->th != NULL)
+            GNUNET_TRANSPORT_disconnect(d->th);
+          cb = d->cb;
+          d->cb = NULL;
+          if (NULL != cb)
+            cb (d->cb_cls,
+                NULL,
+                d->cfg,
+                d,
+                _("Unable to get HELLO for peer!\n"));
+          GNUNET_CONFIGURATION_destroy (d->cfg);
+          GNUNET_free (d->cfgfile);
+          GNUNET_free_non_null (d->hostname);
+          GNUNET_free_non_null (d->username);
+          GNUNET_free (d);
+          return;
+        }
+      if (d->hello != NULL)
+        return;
+      GNUNET_assert(d->task == GNUNET_SCHEDULER_NO_TASK);
+      d->task
+        = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
+                                        &start_fsm, d);
       break;
     case SP_START_DONE:
       GNUNET_break (0);
@@ -603,6 +681,8 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
               GNUNET_free_non_null (d->hostname);
               GNUNET_free_non_null (d->username);
               GNUNET_free_non_null (d->shortname);
+              GNUNET_free_non_null (d->proc);
+              d->proc = NULL;
               GNUNET_free (d);
               return;
             }
@@ -635,6 +715,8 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           GNUNET_free_non_null (d->hostname);
           GNUNET_free_non_null (d->username);
           GNUNET_free_non_null (d->shortname);
+          GNUNET_free_non_null (d->proc);
+          d->proc = NULL;
           GNUNET_free (d);
           return;
         }
@@ -665,6 +747,8 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       GNUNET_free_non_null (d->hello);
       d->hello = NULL;
       GNUNET_free_non_null (d->shortname);
+      GNUNET_free_non_null (d->proc);
+      d->proc = NULL;
       d->shortname = NULL;
       if (NULL != d->dead_cb)
         d->dead_cb (d->dead_cb_cls, NULL);
@@ -789,6 +873,7 @@ GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
  *        (use NULL for localhost).
  * @param ssh_username ssh username to use when connecting to hostname
  * @param sshport port to pass to ssh process when connecting to hostname
+ * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
  * @param hostkey_callback function to call once the hostkey has been
  *        generated for this peer, but it hasn't yet been started
  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
@@ -803,6 +888,7 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
                              const char *hostname,
                              const char *ssh_username,
                              uint16_t sshport,
+                             const char *hostkey,
                              GNUNET_TESTING_NotifyHostkeyCreated
                              hostkey_callback, void *hostkey_cls,
                              GNUNET_TESTING_NotifyDaemonRunning cb,
@@ -811,6 +897,12 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   struct GNUNET_TESTING_Daemon *ret;
   char *arg;
   char *username;
+  char *servicehome;
+  char *baseservicehome;
+  char *slash;
+  char *hostkeyfile;
+  char *temp_file_name;
+  struct GNUNET_DISK_FileHandle *fn;
 
   ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
   ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
@@ -820,7 +912,18 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
     }
   else
     ret->ssh_port_str = NULL;
-  ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
+
+  /* Find service home and base service home directories, create it if it doesn't exist */
+  GNUNET_assert(GNUNET_OK ==
+                GNUNET_CONFIGURATION_get_value_string (cfg,
+                                                       "PATHS",
+                                                       "SERVICEHOME",
+                                                       &servicehome));
+
+  GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
+  GNUNET_asprintf(&temp_file_name, "%s/gnunet-testing-config", servicehome);
+  ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
+  GNUNET_free(temp_file_name);
 #if DEBUG_TESTING
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Setting up peer with configuration file `%s'.\n",
@@ -842,7 +945,24 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   GNUNET_CONFIGURATION_set_value_string (ret->cfg,
                                          "PATHS",
                                          "DEFAULTCONFIG", ret->cfgfile);
-  /* 1) write configuration to temporary file */
+
+  /* Write hostkey to file, if we were given one */
+  hostkeyfile = NULL;
+  if (hostkey != NULL)
+    {
+      GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome);
+      fn =
+      GNUNET_DISK_file_open (hostkeyfile,
+                             GNUNET_DISK_OPEN_READWRITE
+                             | GNUNET_DISK_OPEN_CREATE,
+                             GNUNET_DISK_PERM_USER_READ |
+                             GNUNET_DISK_PERM_USER_WRITE);
+      GNUNET_assert(fn != NULL);
+      GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE));
+      GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn));
+    }
+
+  /* write configuration to temporary file */
   if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
     {
       if (0 != UNLINK (ret->cfgfile))
@@ -869,42 +989,54 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
     }
   ret->username = username;
 
-  /* 2) copy file to remote host */
+  /* copy directory to remote host */
   if (NULL != hostname)
     {
 #if DEBUG_TESTING
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Copying configuration file to host `%s'.\n", hostname);
+                  "Copying configuration directory to host `%s'.\n", hostname);
 #endif
+      baseservicehome = GNUNET_strdup(servicehome);
+      /* Remove trailing /'s */
+      while (baseservicehome[strlen(baseservicehome) - 1] == '/')
+        baseservicehome[strlen(baseservicehome) - 1] = '\0';
+      /* Find next directory /, jump one ahead */
+      slash = strrchr(baseservicehome, '/');
+      if (slash != NULL)
+        *(++slash) = '\0';
+
       ret->phase = SP_COPYING;
       if (NULL != username)
-        GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
+        GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
       else
-        GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
+        GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
 
       if (ret->ssh_port_str == NULL)
         {
-          ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
+          ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
 #if !DEBUG_TESTING
                                                "-q",
 #endif
-                                               ret->cfgfile, arg, NULL);
+                                               servicehome, arg, NULL);
+#if DEBUG_TESTING
+          GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", servicehome, arg);
+#endif
         }
       else
         {
           ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
-                                               "scp", "-P", ret->ssh_port_str,
+                                               "scp", "-r", "-P", ret->ssh_port_str,
 #if !DEBUG_TESTING
                                                "-q",
 #endif
-                                               ret->cfgfile, arg, NULL);
+                                               servicehome, arg, NULL);
         }
       GNUNET_free (arg);
       if (NULL == ret->proc)
         {
           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
                       _
-                      ("Could not start `%s' process to copy configuration file.\n"),
+                      ("Could not start `%s' process to copy configuration directory.\n"),
                       "scp");
           if (0 != UNLINK (ret->cfgfile))
             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
@@ -914,11 +1046,19 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
           GNUNET_free_non_null (ret->username);
           GNUNET_free (ret->cfgfile);
           GNUNET_free (ret);
+          if ((hostkey != NULL) && (0 != UNLINK(hostkeyfile)))
+            GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                                      "unlink", hostkeyfile);
+          GNUNET_free_non_null(hostkeyfile);
+          GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
+          GNUNET_free(servicehome);
           return NULL;
         }
       ret->task
         = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
                                         &start_fsm, ret);
+      GNUNET_free_non_null(hostkeyfile);
+      GNUNET_free(servicehome);
       return ret;
     }
 #if DEBUG_TESTING
@@ -929,6 +1069,8 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   GNUNET_SCHEDULER_add_continuation (&start_fsm,
                                      ret,
                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+  GNUNET_free_non_null(hostkeyfile);
+  GNUNET_free(servicehome);
   return ret;
 }
 
@@ -1266,6 +1408,11 @@ struct ConnectContext
    */
   struct GNUNET_CORE_Handle *d1core;
 
+  /**
+   * Have we actually connected to the core of the first daemon yet?
+   */
+  int d1core_ready;
+
   /**
    * Testing handle to the second daemon.
    */
@@ -1291,12 +1438,6 @@ struct ConnectContext
    */
   void *cb_cls;
 
-  /**
-   * When should this operation be complete (or we must trigger
-   * a timeout).
-   */
-  struct GNUNET_TIME_Absolute timeout;
-
   /**
    * The relative timeout from whence this connect attempt was
    * started.  Allows for reconnect attempts.
@@ -1307,7 +1448,7 @@ struct ConnectContext
    * Maximum number of connect attempts, will retry connection
    * this number of times on failures.
    */
-  unsigned int max_connect_attempts;
+  unsigned int connect_attempts;
 
   /**
    * Hello timeout task
@@ -1330,6 +1471,11 @@ struct ConnectContext
    */
   int connected;
 
+  /**
+   * When connecting, do we need to send the HELLO?
+   */
+  int send_hello;
+
   /**
    * The distance between the two connected peers
    */
@@ -1355,8 +1501,6 @@ notify_connect_result (void *cls,
                        const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
   struct ConnectContext *ctx = cls;
-  struct GNUNET_TIME_Relative remaining;
-
   ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
   if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
     {
@@ -1369,6 +1513,7 @@ notify_connect_result (void *cls,
       GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
       ctx->connect_request_handle = NULL;
     }
+
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     {
       if (ctx->d1th != NULL)
@@ -1382,12 +1527,16 @@ notify_connect_result (void *cls,
       ctx->d2core = NULL;
 #endif
       ctx->d1core = NULL;
-
       GNUNET_free (ctx);
       return;
     }
 
-  remaining = GNUNET_TIME_absolute_get_remaining (ctx->timeout);
+  if (ctx->d1th != NULL)
+    GNUNET_TRANSPORT_disconnect (ctx->d1th);
+  ctx->d1th = NULL;
+  if (ctx->d1core != NULL)
+    GNUNET_CORE_disconnect (ctx->d1core);
+  ctx->d1core = NULL;
 
   if (ctx->connected == GNUNET_YES)
     {
@@ -1400,13 +1549,9 @@ notify_connect_result (void *cls,
                    ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
         }
     }
-  else if (remaining.rel_value > 0)
+  else if (ctx->connect_attempts > 0)
     {
-      if (ctx->d1core != NULL)
-        {
-          GNUNET_CORE_disconnect (ctx->d1core);
-          ctx->d1core = NULL;
-        }
+      ctx->d1core_ready = GNUNET_NO;
 #if CONNECT_CORE2
       if (ctx->d2core != NULL)
         {
@@ -1414,12 +1559,6 @@ notify_connect_result (void *cls,
           ctx->d2core = NULL;
         }
 #endif
-
-      if (ctx->d1th != NULL)
-        {
-          GNUNET_TRANSPORT_disconnect (ctx->d1th);
-          ctx->d1th = NULL;
-        }
       GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
       return;
     }
@@ -1433,10 +1572,6 @@ notify_connect_result (void *cls,
         }
     }
 
-  GNUNET_TRANSPORT_disconnect (ctx->d1th);
-  ctx->d1th = NULL;
-  GNUNET_CORE_disconnect (ctx->d1core);
-  ctx->d1core = NULL;
   GNUNET_free (ctx);
 }
 
@@ -1456,10 +1591,22 @@ connect_notify (void *cls,
 {
   struct ConnectContext *ctx = cls;
 
-  if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
+#if DEBUG_TESTING
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+                "Connected peer %s to peer %s\n",
+                ctx->d1->shortname, GNUNET_i2s(peer));
+#endif
+
+  if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
     {
+
       ctx->connected = GNUNET_YES;
       ctx->distance = 0;        /* FIXME: distance */
+      if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
+        {
+          GNUNET_SCHEDULER_cancel(ctx->hello_send_task);
+          ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
+        }
       GNUNET_SCHEDULER_cancel (ctx->timeout_task);
       ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result,
                                                     ctx);
@@ -1498,17 +1645,15 @@ connect_notify_core2 (void *cls,
  * Task called once a core connect request has been transmitted.
  *
  * @param cls struct ConnectContext
- * @param tc context information (why was this task triggered now)
+ * @param success was the request successful?
  */
 void
 core_connect_request_cont (void *cls,
-                           const struct GNUNET_SCHEDULER_TaskContext *tc)
+                           int success)
 {
   struct ConnectContext *ctx = cls;
 
-  if (tc->reason == GNUNET_SCHEDULER_REASON_PREREQ_DONE)
-    ctx->connect_request_handle = NULL;
-  return;
+  ctx->connect_request_handle = NULL;
 }
 
 static void
@@ -1519,22 +1664,21 @@ send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     return;
-  if ((ctx->d2->hello != NULL)
+  if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
       && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
     {
       hello = GNUNET_HELLO_get_header (ctx->d2->hello);
       GNUNET_assert (hello != NULL);
-      GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello);
+      GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
       GNUNET_assert (ctx->d1core != NULL);
       ctx->connect_request_handle =
         GNUNET_CORE_peer_request_connect (ctx->d1core,
-                                          GNUNET_TIME_relative_divide
-                                          (ctx->relative_timeout,
-                                           ctx->max_connect_attempts + 1),
+                                          ctx->relative_timeout,
                                           &ctx->d2->id,
                                           &core_connect_request_cont, ctx);
+
 #if DEBUG_TESTING
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+      GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
                   "Sending connect request to CORE of %s for peer %s\n",
                   GNUNET_i2s (&ctx->d1->id),
                   GNUNET_h2s (&ctx->d2->id.hashPubKey));
@@ -1549,124 +1693,52 @@ send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
 }
 
 /**
- * Establish a connection between two GNUnet daemons.
+ * Notify of a successful connection to the core service.
  *
- * @param d1 handle for the first daemon
- * @param d2 handle for the second daemon
- * @param timeout how long is the connection attempt
- *        allowed to take?
- * @param max_connect_attempts how many times should we try to reconnect
- *        (within timeout)
- * @param cb function to call at the end
- * @param cb_cls closure for cb
+ * @param cls a ConnectContext
+ * @param server handle to the core service
+ * @param my_identity the peer identity of this peer
+ * @param publicKey the public key of the peer
  */
 void
-GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
-                                struct GNUNET_TESTING_Daemon *d2,
-                                struct GNUNET_TIME_Relative timeout,
-                                unsigned int max_connect_attempts,
-                                GNUNET_TESTING_NotifyConnection cb,
-                                void *cb_cls)
+core_init_notify (void *cls,
+                  struct GNUNET_CORE_Handle * server,
+                  const struct GNUNET_PeerIdentity *
+                  my_identity,
+                  const struct
+                  GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
+                  publicKey)
 {
-  struct ConnectContext *ctx;
+  struct ConnectContext *connect_ctx = cls;
+  connect_ctx->d1core_ready = GNUNET_YES;
 
-  if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
+  if (connect_ctx->send_hello == GNUNET_NO)
     {
-      if (NULL != cb)
-        cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
-            _("Peers are not fully running yet, can not connect!\n"));
-      return;
-    }
-  ctx = GNUNET_malloc (sizeof (struct ConnectContext));
-  ctx->d1 = d1;
-  ctx->d2 = d2;
-  ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
-  ctx->timeout_hello =
-    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
-  ctx->relative_timeout = timeout;
-  ctx->cb = cb;
-  ctx->cb_cls = cb_cls;
-  ctx->max_connect_attempts = max_connect_attempts;
-  ctx->connected = GNUNET_NO;
+      connect_ctx->connect_request_handle =
+          GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
+                                            connect_ctx->relative_timeout,
+                                            &connect_ctx->d2->id,
+                                            &core_connect_request_cont, connect_ctx);
+      GNUNET_assert(connect_ctx->connect_request_handle != NULL);
 #if DEBUG_TESTING
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Asked to connect peer %s to peer %s\n",
-              d1->shortname, d2->shortname);
-#endif
-
-  ctx->d1core = GNUNET_CORE_connect (d1->cfg, 1,
-                                     ctx,
-                                     NULL,
-                                     &connect_notify, NULL, NULL,
-                                     NULL, GNUNET_NO,
-                                     NULL, GNUNET_NO, no_handlers);
-  if (ctx->d1core == NULL)
-    {
-      GNUNET_free (ctx);
-      if (NULL != cb)
-        cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
-            _("Failed to connect to core service of first peer!\n"));
-      return;
-    }
-
-#if CONNECT_CORE2
-  ctx->d2core = GNUNET_CORE_connect (d2->cfg, 1,
-#if NO_MORE_TIMEOUT_FIXME
-                                     timeout,
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Sending connect request to CORE of %s for peer %s\n",
+                  connect_ctx->d1->shortname,
+                  connect_ctx->d2->shortname);
 #endif
-                                     ctx,
-                                     NULL,
-                                     NULL, NULL, NULL,
-                                     NULL, GNUNET_NO,
-                                     NULL, GNUNET_NO, no_handlers);
-  if (ctx->d2core == NULL)
-    {
-      GNUNET_free (ctx);
-      if (NULL != cb)
-        cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
-            _("Failed to connect to core service of second peer!\n"));
-      return;
     }
-#endif
 
-#if DEBUG_TESTING > 2
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Asked to connect peer %s to peer %s\n",
-              d1->shortname, d2->shortname);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Connecting to transport service of peer %s\n", d2->shortname);
-
-#endif
-
-  ctx->d1th = GNUNET_TRANSPORT_connect (d1->cfg,
-                                        &d1->id, d1, NULL, NULL, NULL);
-  if (ctx->d1th == NULL)
-    {
-      GNUNET_CORE_disconnect (ctx->d1core);
-      GNUNET_free (ctx);
-      if (NULL != cb)
-        cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
-            _("Failed to connect to transport service!\n"));
-      return;
-    }
-
-  ctx->timeout_task =
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
-                                  (ctx->relative_timeout,
-                                   ctx->max_connect_attempts),
-                                   &notify_connect_result, ctx);
-
-  ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
 }
 
+
 static void
 reattempt_daemons_connect (void *cls,
                            const struct GNUNET_SCHEDULER_TaskContext *tc)
 {
-
   struct ConnectContext *ctx = cls;
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     {
+      GNUNET_free(ctx);
       return;
     }
 #if DEBUG_TESTING_RECONNECT
@@ -1674,16 +1746,12 @@ reattempt_daemons_connect (void *cls,
               "re-attempting connect of peer %s to peer %s\n",
               ctx->d1->shortname, ctx->d2->shortname);
 #endif
-
+  ctx->connect_attempts--;
   GNUNET_assert (ctx->d1core == NULL);
-
+  ctx->d1core_ready = GNUNET_NO;
   ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
-#if NO_MORE_TIMEOUT_FIXME
-                                     GNUNET_TIME_absolute_get_remaining
-                                     (ctx->timeout),
-#endif
                                      ctx,
-                                     NULL,
+                                     &core_init_notify,
                                      &connect_notify, NULL, NULL,
                                      NULL, GNUNET_NO,
                                      NULL, GNUNET_NO, no_handlers);
@@ -1697,27 +1765,170 @@ reattempt_daemons_connect (void *cls,
       return;
     }
 
-  ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
-                                        &ctx->d1->id,
-                                        ctx->d1, NULL, NULL, NULL);
-  if (ctx->d1th == NULL)
+  if (ctx->send_hello == GNUNET_YES)
     {
-      GNUNET_CORE_disconnect (ctx->d1core);
-      GNUNET_free (ctx);
-      if (NULL != ctx->cb)
-        ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
-                 ctx->d2->cfg, ctx->d1, ctx->d2,
-                 _("Failed to connect to transport service!\n"));
-      return;
+      ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
+                                            &ctx->d1->id,
+                                            ctx->d1, NULL, NULL, NULL);
+      if (ctx->d1th == NULL)
+        {
+          GNUNET_CORE_disconnect (ctx->d1core);
+          GNUNET_free (ctx);
+          if (NULL != ctx->cb)
+            ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
+                     ctx->d2->cfg, ctx->d1, ctx->d2,
+                     _("Failed to connect to transport service!\n"));
+          return;
+        }
+      ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
+    }
+  else
+    {
+      ctx->connect_request_handle =
+        GNUNET_CORE_peer_request_connect (ctx->d1core,
+                                          ctx->relative_timeout,
+                                          &ctx->d2->id,
+                                          &core_connect_request_cont, ctx);
     }
-
   ctx->timeout_task =
-    GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
-                                  (ctx->relative_timeout,
-                                   ctx->max_connect_attempts),
+    GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
                                   &notify_connect_result, ctx);
+}
+
+/**
+ * Iterator for currently known peers, to ensure
+ * that we don't try to send duplicate connect
+ * requests to core.
+ *
+ * @param cls our "struct ConnectContext"
+ * @param peer identity of the peer that has connected,
+ *        NULL when iteration has finished
+ * @param atsi performance information
+ *
+ */
+static void
+core_initial_iteration (void *cls,
+                        const struct GNUNET_PeerIdentity *peer,
+                        const struct GNUNET_TRANSPORT_ATS_Information *atsi)
+{
+  struct ConnectContext *ctx = cls;
+
+  if ((peer != NULL) &&
+      (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
+    {
+      ctx->connected = GNUNET_YES;
+      ctx->distance = 0;        /* FIXME: distance */
+      return;
+    }
+  else if (peer == NULL) /* End of iteration over peers */
+    {
+      if (ctx->connected == GNUNET_YES)
+        {
+          ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result,
+                                                        ctx);
+          return;
+        }
+
+      /* Peer not already connected, need to schedule connect request! */
+      if (ctx->d1core == NULL)
+        {
+#if DEBUG_TESTING
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "Peers are NOT connected, connecting to core!\n");
+#endif
+          ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
+                                             ctx,
+                                             &core_init_notify,
+                                             &connect_notify, NULL, NULL,
+                                             NULL, GNUNET_NO,
+                                             NULL, GNUNET_NO, no_handlers);
+        }
+
+      if (ctx->d1core == NULL)
+        {
+          GNUNET_free (ctx);
+          if (NULL != ctx->cb)
+            ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
+                _("Failed to connect to core service of first peer!\n"));
+          return;
+        }
+
+      if (ctx->send_hello == GNUNET_YES)
+        {
+          ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
+                                                &ctx->d1->id, ctx->d1, NULL, NULL, NULL);
+          if (ctx->d1th == NULL)
+            {
+              GNUNET_CORE_disconnect (ctx->d1core);
+              GNUNET_free (ctx);
+              if (NULL != ctx->cb)
+                ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
+                    _("Failed to connect to transport service!\n"));
+              return;
+            }
+          ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
+        }
+
+      ctx->timeout_task =
+        GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
+                                      &notify_connect_result, ctx);
+    }
+}
+
+
+/**
+ * Establish a connection between two GNUnet daemons.
+ *
+ * @param d1 handle for the first daemon
+ * @param d2 handle for the second daemon
+ * @param timeout how long is the connection attempt
+ *        allowed to take?
+ * @param max_connect_attempts how many times should we try to reconnect
+ *        (within timeout)
+ * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
+ *                   the HELLO has already been exchanged
+ * @param cb function to call at the end
+ * @param cb_cls closure for cb
+ */
+void
+GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
+                                struct GNUNET_TESTING_Daemon *d2,
+                                struct GNUNET_TIME_Relative timeout,
+                                unsigned int max_connect_attempts,
+                                int send_hello,
+                                GNUNET_TESTING_NotifyConnection cb,
+                                void *cb_cls)
+{
+  struct ConnectContext *ctx;
+
+  if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
+    {
+      if (NULL != cb)
+        cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
+            _("Peers are not fully running yet, can not connect!\n"));
+      return;
+    }
+
+  ctx = GNUNET_malloc (sizeof (struct ConnectContext));
+  ctx->d1 = d1;
+  ctx->d2 = d2;
+  ctx->timeout_hello =
+    GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
+  ctx->relative_timeout = GNUNET_TIME_relative_divide(timeout, max_connect_attempts);
+  ctx->cb = cb;
+  ctx->cb_cls = cb_cls;
+  ctx->connect_attempts = max_connect_attempts;
+  ctx->connected = GNUNET_NO;
+  ctx->send_hello = send_hello;
+#if DEBUG_TESTING
+  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+              "Asked to connect peer %s to peer %s\n",
+              d1->shortname, d2->shortname);
+#endif
 
-  ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
+  /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
+  GNUNET_assert(GNUNET_OK == GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id, &core_initial_iteration, ctx));
+  /*GNUNET_assert(GNUNET_OK == GNUNET_CORE_iterate_peers (ctx->d1->cfg, &core_initial_iteration, ctx));*/
 }
 
 /* end of testing.c */