fix for over-reporting number of necessary connections
[oweals/gnunet.git] / src / testing / testing.c
index f67d853917ee9b3e014bd617b086f5b4430484cc..024be6ff5561cf9fcbe1fe82d0184c16b6afbf74 100644 (file)
@@ -38,7 +38,8 @@
 #include "gnunet_hello_lib.h"
 
 #define DEBUG_TESTING GNUNET_NO
-#define DEBUG_TESTING_RECONNECT GNUNET_NO
+#define DEBUG_TESTING_RECONNECT GNUNET_YES
+#define WAIT_FOR_HELLO GNUNET_NO
 
 /**
  * How long do we wait after starting gnunet-service-arm
@@ -65,10 +66,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 || daemon->phase == SP_START_DONE);
+
+  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 +115,18 @@ process_hello (void *cls, const struct GNUNET_MessageHeader *message)
       GNUNET_TRANSPORT_disconnect (daemon->th);
       daemon->th = NULL;
     }
+  daemon->phase = SP_START_DONE;
 
+#if WAIT_FOR_HELLO
+  if (NULL != cb) /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
+    cb (daemon->cb_cls, &daemon->id, daemon->cfg, daemon, NULL);
+#endif
 }
 
+static void
+start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
+
+#if WAIT_FOR_HELLO
 /**
  * Function called after GNUNET_CORE_connect has succeeded
  * (or failed for good).  Note that the private key of the
@@ -127,12 +146,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 +158,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;
     }
@@ -150,14 +167,12 @@ testing_init (void *cls,
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
 #endif
-  d->id = *my_identity;
-  d->shortname = strdup (GNUNET_i2s (my_identity));
+  d->id = *my_identity; /* FIXME: shouldn't we already have this from reading the hostkey file? */
+  if (d->shortname == NULL)
+    d->shortname = strdup (GNUNET_i2s (my_identity));
   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,8 +207,37 @@ 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);
 }
+#endif
 
+#if !WAIT_FOR_HELLO
+/**
+ * Notify of a peer being up and running.  Scheduled as a task
+ * so that variables which may need to be set are set before
+ * the connect callback can set up new operations.
+ *
+ * @param cls the testing daemon
+ * @param tc task scheduler context
+ */
+static void
+notify_daemon_started (void *cls,
+                       const struct GNUNET_SCHEDULER_TaskContext *tc)
+{
+  struct GNUNET_TESTING_Daemon *d = cls;
+  GNUNET_TESTING_NotifyDaemonRunning cb;
+
+  cb = d->cb;
+  d->cb = NULL;
+  if (NULL != cb)
+    cb (d->cb_cls, &d->id, d->cfg, d, NULL);
+}
+#endif
 
 /**
  * Finite-state machine for starting GNUnet.
@@ -251,6 +295,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
                 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
           return;
         }
+      GNUNET_OS_process_close(d->proc);
 #if DEBUG_TESTING
       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
                   "Successfully copied configuration file.\n");
@@ -258,109 +303,127 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       d->phase = SP_COPIED;
       /* fall-through */
     case SP_COPIED:
-      /* Start create hostkey process */
-      d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
-      if (d->pipe_stdout == NULL)
+      /* Start create hostkey process if we don't already know the peer identity!*/
+      if (GNUNET_NO == d->have_hostkey)
         {
-          cb = d->cb;
-          d->cb = NULL;
-          if (NULL != cb)
-            cb (d->cb_cls,
-                NULL,
-                d->cfg,
-                d,
-                (NULL == d->hostname)
-                ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
-                : _("Failed to create pipe for `ssh' process.\n"));
-          return;
-        }
-      if (NULL == d->hostname)
-        {
-#if DEBUG_TESTING
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "Starting `%s', with command `%s %s %s %s'.\n",
-                      "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
-                      "-sq");
-#endif
-          d->proc =
-            GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
-                                     "gnunet-peerinfo", "-c", d->cfgfile,
-                                     "-sq", NULL);
-          GNUNET_DISK_pipe_close_end (d->pipe_stdout,
-                                      GNUNET_DISK_PIPE_END_WRITE);
-        }
-      else
-        {
-          if (d->username != NULL)
-            GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
-          else
-            dst = GNUNET_strdup (d->hostname);
-
-#if DEBUG_TESTING
-          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                      "Starting `%s', with command `%s %s %s %s %s %s'.\n",
-                      "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
-                      d->cfgfile, "-sq");
-#endif
-          if (d->ssh_port_str == NULL)
+          d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
+          if (d->pipe_stdout == NULL)
             {
-              d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
-                                                 "ssh",
-#if !DEBUG_TESTING
-                                                 "-q",
-#endif
-                                                 dst,
-                                                 "gnunet-peerinfo",
-                                                 "-c", d->cfgfile, "-sq",
-                                                 NULL);
+              cb = d->cb;
+              d->cb = NULL;
+              if (NULL != cb)
+                cb (d->cb_cls,
+                    NULL,
+                    d->cfg,
+                    d,
+                    (NULL == d->hostname)
+                    ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
+                    : _("Failed to create pipe for `ssh' process.\n"));
+              return;
+            }
+          if (NULL == d->hostname)
+            {
+    #if DEBUG_TESTING
+              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                          "Starting `%s', with command `%s %s %s %s'.\n",
+                          "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
+                          "-sq");
+    #endif
+              d->proc =
+                GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
+                                         "gnunet-peerinfo", "-c", d->cfgfile,
+                                         "-sq", NULL);
+              GNUNET_DISK_pipe_close_end (d->pipe_stdout,
+                                          GNUNET_DISK_PIPE_END_WRITE);
             }
           else
             {
-              d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
-                                                 "ssh", "-p", d->ssh_port_str,
-#if !DEBUG_TESTING
-                                                 "-q",
-#endif
-                                                 dst,
-                                                 "gnunet-peerinfo",
-                                                 "-c", d->cfgfile, "-sq",
-                                                 NULL);
+              if (d->username != NULL)
+                GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
+              else
+                dst = GNUNET_strdup (d->hostname);
+
+    #if DEBUG_TESTING
+              GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                          "Starting `%s', with command `%s %s %s %s %s %s'.\n",
+                          "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
+                          d->cfgfile, "-sq");
+    #endif
+              if (d->ssh_port_str == NULL)
+                {
+                  d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
+                                                     "ssh",
+    #if !DEBUG_TESTING
+                                                     "-q",
+    #endif
+                                                     dst,
+                                                     "gnunet-peerinfo",
+                                                     "-c", d->cfgfile, "-sq",
+                                                     NULL);
+                }
+              else
+                {
+                  d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
+                                                     "ssh", "-p", d->ssh_port_str,
+    #if !DEBUG_TESTING
+                                                     "-q",
+    #endif
+                                                     dst,
+                                                     "gnunet-peerinfo",
+                                                     "-c", d->cfgfile, "-sq",
+                                                     NULL);
+                }
+              GNUNET_DISK_pipe_close_end (d->pipe_stdout,
+                                          GNUNET_DISK_PIPE_END_WRITE);
+              GNUNET_free (dst);
             }
-          GNUNET_DISK_pipe_close_end (d->pipe_stdout,
-                                      GNUNET_DISK_PIPE_END_WRITE);
-          GNUNET_free (dst);
+          if (NULL == d->proc)
+            {
+              GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                          _("Could not start `%s' process to create hostkey.\n"),
+                          (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
+              cb = d->cb;
+              d->cb = NULL;
+              if (NULL != cb)
+                cb (d->cb_cls,
+                    NULL,
+                    d->cfg,
+                    d,
+                    (NULL == d->hostname)
+                    ? _("Failed to start `gnunet-peerinfo' process.\n")
+                    : _("Failed to start `ssh' process.\n"));
+              GNUNET_DISK_pipe_close (d->pipe_stdout);
+              return;
+            }
+    #if DEBUG_TESTING
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
+    #endif
+          d->phase = SP_HOSTKEY_CREATE;
+          d->task
+            =
+            GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
+                                            (d->max_timeout),
+                                            GNUNET_DISK_pipe_handle
+                                            (d->pipe_stdout,
+                                             GNUNET_DISK_PIPE_END_READ),
+                                            &start_fsm, d);
         }
-      if (NULL == d->proc)
+      else /* Already have a hostkey! */
         {
-          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                      _("Could not start `%s' process to create hostkey.\n"),
-                      (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
-          cb = d->cb;
-          d->cb = NULL;
-          if (NULL != cb)
-            cb (d->cb_cls,
-                NULL,
-                d->cfg,
-                d,
-                (NULL == d->hostname)
-                ? _("Failed to start `gnunet-peerinfo' process.\n")
-                : _("Failed to start `ssh' process.\n"));
-          GNUNET_DISK_pipe_close (d->pipe_stdout);
-          return;
+          if (d->hostkey_callback != NULL)
+            {
+              d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
+              d->hostkey_callback = NULL;
+              d->phase = SP_HOSTKEY_CREATED;
+            }
+          else
+            d->phase = SP_TOPOLOGY_SETUP;
+
+          /* wait some more */
+          d->task
+            = GNUNET_SCHEDULER_add_now (&start_fsm, d);
         }
-#if DEBUG_TESTING
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
-#endif
-      d->phase = SP_HOSTKEY_CREATE;
-      d->task
-        =
-        GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
-                                        (d->max_timeout),
-                                        GNUNET_DISK_pipe_handle
-                                        (d->pipe_stdout,
-                                         GNUNET_DISK_PIPE_END_READ),
-                                        &start_fsm, d);
       break;
     case SP_HOSTKEY_CREATE:
       bytes_read =
@@ -410,24 +473,27 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
             cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
           return;
         }
+      d->shortname = GNUNET_strdup(GNUNET_i2s(&d->id));
       GNUNET_DISK_pipe_close (d->pipe_stdout);
       d->pipe_stdout = NULL;
       (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
       GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
       GNUNET_OS_process_close (d->proc);
       d->proc = NULL;
-#if DEBUG_TESTING
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
-#endif
+      d->have_hostkey = GNUNET_YES;
       if (d->hostkey_callback != NULL)
         {
           d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
+          d->hostkey_callback = NULL;
           d->phase = SP_HOSTKEY_CREATED;
         }
       else
         {
           d->phase = SP_TOPOLOGY_SETUP;
         }
+#if DEBUG_TESTING
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
+#endif
       /* Fall through */
     case SP_HOSTKEY_CREATED:
       /* wait for topology finished */
@@ -448,7 +514,7 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
         = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
                                         &start_fsm, d);
       break;
-    case SP_TOPOLOGY_SETUP:
+    case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
       /* start GNUnet on remote host */
       if (NULL == d->hostname)
         {
@@ -551,7 +617,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 */
@@ -570,18 +641,98 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       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 WAIT_FOR_HELLO
+      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,
-#if NO_MORE_TIMEOUT_FIXME
-                                       ARM_START_WAIT,
-#endif
                                        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);
+#else
+      d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
+      if (d->th == NULL)
+        {
+          if (GNUNET_YES == d->dead)
+            GNUNET_TESTING_daemon_stop (d,
+                                        GNUNET_TIME_absolute_get_remaining
+                                        (d->max_timeout), d->dead_cb,
+                                        d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
+          else if (NULL != d->cb)
+            d->cb (d->cb_cls, &d->id, d->cfg, d,
+                   _("Failed to connect to transport service!\n"));
+          return;
+        }
+#if DEBUG_TESTING
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                  "Connected to transport service `%s', getting HELLO\n",
+                  GNUNET_i2s (&d->id));
+#endif
+
+      GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
+      GNUNET_SCHEDULER_add_now(&notify_daemon_started, d);
+      /*cb = d->cb;
+      d->cb = NULL;
+      if (NULL != cb)
+        cb (d->cb_cls, &d->id, d->cfg, d, NULL);*/
+      d->running = GNUNET_YES;
+      d->phase = SP_GET_HELLO;
+#endif
       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);
@@ -664,6 +815,10 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
           GNUNET_TRANSPORT_disconnect (d->th);
           d->th = NULL;
         }
+
+      if (NULL != d->dead_cb)
+        d->dead_cb (d->dead_cb_cls, NULL);
+
       /* state clean up and notifications */
       if (d->churn == GNUNET_NO)
         {
@@ -679,9 +834,6 @@ start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
       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);
-
       if (d->churn == GNUNET_NO)
         GNUNET_free (d);
 
@@ -798,6 +950,8 @@ GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
  *
  * @param cfg configuration to use
  * @param timeout how long to wait starting up peers
+ * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
+ *                to really start the peer (default)
  * @param hostname name of the machine where to run GNUnet
  *        (use NULL for localhost).
  * @param ssh_username ssh username to use when connecting to hostname
@@ -814,6 +968,7 @@ GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
 struct GNUNET_TESTING_Daemon *
 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
                              struct GNUNET_TIME_Relative timeout,
+                             int pretend,
                              const char *hostname,
                              const char *ssh_username,
                              uint16_t sshport,
@@ -832,6 +987,8 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
   char *hostkeyfile;
   char *temp_file_name;
   struct GNUNET_DISK_FileHandle *fn;
+  struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
+  struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
 
   ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
   ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
@@ -875,6 +1032,18 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
                                          "PATHS",
                                          "DEFAULTCONFIG", ret->cfgfile);
 
+  if (hostkey != NULL) /* Get the peer identity from the hostkey */
+    {
+      private_key = GNUNET_CRYPTO_rsa_decode_key(hostkey, HOSTKEYFILESIZE);
+      GNUNET_assert(private_key != NULL);
+      GNUNET_CRYPTO_rsa_key_get_public (private_key,
+                                        &public_key);
+      GNUNET_CRYPTO_hash(&public_key, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &ret->id.hashPubKey);
+      ret->shortname = GNUNET_strdup(GNUNET_i2s(&ret->id));
+      ret->have_hostkey = GNUNET_YES;
+      GNUNET_CRYPTO_rsa_key_free(private_key);
+    }
+
   /* Write hostkey to file, if we were given one */
   hostkeyfile = NULL;
   if (hostkey != NULL)
@@ -918,86 +1087,91 @@ GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
     }
   ret->username = username;
 
-  /* copy directory to remote host */
-  if (NULL != hostname)
+  if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
     {
+      /* copy directory to remote host */
+      if (NULL != hostname)
+        {
 #if DEBUG_TESTING
-      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-                  "Copying configuration directory to host `%s'.\n", hostname);
+          GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
+                      "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, baseservicehome);
-      else
-        GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
+          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, baseservicehome);
+          else
+            GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
 
-      if (ret->ssh_port_str == NULL)
-        {
-          ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
+          if (ret->ssh_port_str == NULL)
+            {
+              ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
 #if !DEBUG_TESTING
-                                               "-q",
+                                                   "-q",
 #endif
-                                               servicehome, 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);
+              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", "-r", "-P", ret->ssh_port_str,
-#if !DEBUG_TESTING
-                                               "-q",
-#endif
-                                               servicehome, arg, NULL);
-        }
-      GNUNET_free (arg);
-      if (NULL == ret->proc)
-        {
-          GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
-                      _
-                      ("Could not start `%s' process to copy configuration directory.\n"),
-                      "scp");
-          if (0 != UNLINK (ret->cfgfile))
-            GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
-                                      "unlink", ret->cfgfile);
-          GNUNET_CONFIGURATION_destroy (ret->cfg);
-          GNUNET_free_non_null (ret->hostname);
-          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);
+            }
+          else
+            {
+              ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
+                                                   "scp", "-r", "-P", ret->ssh_port_str,
+    #if !DEBUG_TESTING
+                                                   "-q",
+    #endif
+                                                   servicehome, arg, NULL);
+            }
+          GNUNET_free (arg);
+          if (NULL == ret->proc)
+            {
+              GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
+                          _
+                          ("Could not start `%s' process to copy configuration directory.\n"),
+                          "scp");
+              if (0 != UNLINK (ret->cfgfile))
+                GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
+                                          "unlink", ret->cfgfile);
+              GNUNET_CONFIGURATION_destroy (ret->cfg);
+              GNUNET_free_non_null (ret->hostname);
+              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_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
+          GNUNET_free(baseservicehome);
           GNUNET_free(servicehome);
-          return NULL;
+          return ret;
         }
-      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
   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
               "No need to copy configuration file since we are running locally.\n");
 #endif
-  ret->phase = SP_COPIED;
-  GNUNET_SCHEDULER_add_continuation (&start_fsm,
-                                     ret,
-                                     GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+      ret->phase = SP_COPIED;
+      GNUNET_SCHEDULER_add_continuation (&start_fsm,
+                                         ret,
+                                         GNUNET_SCHEDULER_REASON_PREREQ_DONE);
+    }
   GNUNET_free_non_null(hostkeyfile);
   GNUNET_free(servicehome);
   return ret;
@@ -1367,12 +1541,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.
@@ -1383,7 +1551,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
@@ -1406,6 +1574,11 @@ struct ConnectContext
    */
   int connected;
 
+  /**
+   * When connecting, do we need to send the HELLO?
+   */
+  int send_hello;
+
   /**
    * The distance between the two connected peers
    */
@@ -1431,8 +1604,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)
     {
@@ -1445,6 +1616,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)
@@ -1458,12 +1630,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)
     {
@@ -1476,13 +1652,8 @@ 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)
@@ -1491,12 +1662,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;
     }
@@ -1510,10 +1675,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);
 }
 
@@ -1533,10 +1694,22 @@ connect_notify (void *cls,
 {
   struct ConnectContext *ctx = cls;
 
+#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);
@@ -1595,21 +1768,25 @@ send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
     return;
   if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
-      && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
+      && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello))
+      && (ctx->d1->phase == SP_START_DONE)
+      && (ctx->d2->phase == SP_START_DONE))
     {
       hello = GNUNET_HELLO_get_header (ctx->d2->hello);
       GNUNET_assert (hello != NULL);
-      GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello);
+#if DEBUG_TESTING
+      GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n", ctx->d2->shortname, ctx->d1->shortname);
+#endif
+      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->d2->id,
-                                           &core_connect_request_cont, ctx);
+                                          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));
@@ -1642,6 +1819,118 @@ core_init_notify (void *cls,
 {
   struct ConnectContext *connect_ctx = cls;
   connect_ctx->d1core_ready = GNUNET_YES;
+
+  if (connect_ctx->send_hello == 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,
+                  "Sending connect request to CORE of %s for peer %s\n",
+                  connect_ctx->d1->shortname,
+                  connect_ctx->d2->shortname);
+#endif
+    }
+
+}
+
+
+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
+  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
+              "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,
+                                     ctx,
+                                     &core_init_notify,
+                                     &connect_notify, NULL, NULL,
+                                     NULL, GNUNET_NO,
+                                     NULL, GNUNET_NO, no_handlers);
+  if (ctx->d1core == NULL)
+    {
+      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"));
+      GNUNET_free (ctx);
+      return;
+    }
+
+  /* Don't know reason for initial connect failure, update the HELLO for the second peer */
+  if (NULL != ctx->d2->hello)
+    {
+      GNUNET_free(ctx->d2->hello);
+      ctx->d2->hello = NULL;
+      if (NULL != ctx->d2->th)
+        {
+          GNUNET_TRANSPORT_get_hello_cancel(ctx->d2->th, &process_hello, ctx->d2);
+          GNUNET_TRANSPORT_disconnect(ctx->d2->th);
+        }
+      ctx->d2->th = GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL, NULL);
+      GNUNET_assert(ctx->d2->th != NULL);
+      GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
+    }
+
+  if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
+    {
+      ctx->d2->th = GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL, NULL);
+      if (ctx->d2->th == 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;
+        }
+      GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
+    }
+
+  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);
+    }
+  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 (ctx->relative_timeout,
+                                  &notify_connect_result, ctx);
 }
 
 /**
@@ -1667,54 +1956,75 @@ core_initial_iteration (void *cls,
     {
       ctx->connected = GNUNET_YES;
       ctx->distance = 0;        /* FIXME: distance */
-      ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result,
-                                                    ctx);
+      return;
     }
-  else if (peer == NULL) /* Peer not already connected, need to schedule connect request! */
+  else if (peer == NULL) /* End of iteration over peers */
     {
-      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)
+      if (ctx->connected == GNUNET_YES)
         {
-          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"));
+          ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result,
+                                                        ctx);
           return;
         }
 
-#if DEBUG_TESTING > 2
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Asked to connect peer %s to peer %s\n",
-              ctx->d1->shortname, ctx->d2->shortname);
-  GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
-              "Connecting to transport service of peer %s\n", ctx->d2->shortname);
-
+      /* 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);
+        }
 
-      ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
-                                            &ctx->d1->id, ctx->d1, NULL, NULL, NULL);
-      if (ctx->d1th == NULL)
+      if (ctx->d1core == 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"));
+                _("Failed to connect to core service of first peer!\n"));
           return;
         }
 
-      ctx->timeout_task =
-        GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
-                                      (ctx->relative_timeout,
-                                       ctx->max_connect_attempts),
-                                       &notify_connect_result, ctx);
+      if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL)) /* Do not yet have the second peer's hello, set up a task to get it */
+        {
+          ctx->d2->th = GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL, NULL);
+          if (ctx->d2->th == 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;
+            }
+          GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
+        }
 
-      ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
+      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);
     }
 }
 
@@ -1728,6 +2038,8 @@ core_initial_iteration (void *cls,
  *        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
  */
@@ -1736,6 +2048,7 @@ 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)
 {
@@ -1746,19 +2059,21 @@ GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
       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"));
+      GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\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->relative_timeout = GNUNET_TIME_relative_divide(timeout, max_connect_attempts);
   ctx->cb = cb;
   ctx->cb_cls = cb_cls;
-  ctx->max_connect_attempts = max_connect_attempts;
+  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",
@@ -1766,65 +2081,8 @@ GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
 #endif
 
   /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
-  GNUNET_CORE_iterate_peers(ctx->d1->cfg, &core_initial_iteration, 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)
-    {
-      return;
-    }
-#if DEBUG_TESTING_RECONNECT
-  GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
-              "re-attempting connect of peer %s to peer %s\n",
-              ctx->d1->shortname, ctx->d2->shortname);
-#endif
-
-  GNUNET_assert (ctx->d1core == NULL);
-  ctx->d1core_ready = GNUNET_NO;
-  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)
-    {
-      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"));
-      GNUNET_free (ctx);
-      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->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);
+  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 */