c211667757f2fdd32ee0216627bb4e9ff7e4fe6c
[oweals/gnunet.git] / src / testing / testing.c
1 /*
2       This file is part of GNUnet
3       (C) 2008, 2009 Christian Grothoff (and other contributing authors)
4
5       GNUnet is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published
7       by the Free Software Foundation; either version 3, or (at your
8       option) any later version.
9
10       GNUnet is distributed in the hope that it will be useful, but
11       WITHOUT ANY WARRANTY; without even the implied warranty of
12       MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13       General Public License for more details.
14
15       You should have received a copy of the GNU General Public License
16       along with GNUnet; see the file COPYING.  If not, write to the
17       Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18       Boston, MA 02111-1307, USA.
19  */
20
21 /**
22  * @file testing/testing.c
23  * @brief convenience API for writing testcases for GNUnet
24  *        Many testcases need to start and stop gnunetd,
25  *        and this library is supposed to make that easier
26  *        for TESTCASES.  Normal programs should always
27  *        use functions from gnunet_{util,arm}_lib.h.  This API is
28  *        ONLY for writing testcases!
29  * @author Christian Grothoff
30  *
31  */
32 #include "platform.h"
33 #include "gnunet_arm_service.h"
34 #include "gnunet_core_service.h"
35 #include "gnunet_constants.h"
36 #include "gnunet_testing_lib.h"
37 #include "gnunet_transport_service.h"
38 #include "gnunet_hello_lib.h"
39
40 #define DEBUG_TESTING GNUNET_NO
41
42 #define DEBUG_TESTING_RECONNECT GNUNET_NO
43
44 #define WAIT_FOR_HELLO GNUNET_NO
45
46 /**
47  * Hack to deal with initial HELLO's being often devoid of addresses.
48  * This hack causes 'process_hello' to ignore HELLOs without addresses.
49  * The correct implementation would continue with 'process_hello' until
50  * the connection could be established...
51  */
52 #define EMPTY_HACK GNUNET_YES
53
54 /**
55  * How long do we wait after starting gnunet-service-arm
56  * for the core service to be alive?
57  */
58 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
59
60 /**
61  * How many times are we willing to try to wait for "scp" or
62  * "gnunet-service-arm" to complete (waitpid) before giving up?
63  */
64 #define MAX_EXEC_WAIT_RUNS 250
65
66 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
67
68 #if EMPTY_HACK
69 static int
70 test_address (void *cls,
71               const char *tname,
72               struct GNUNET_TIME_Absolute expiration,
73               const void *addr, uint16_t addrlen)
74 {
75   int *empty = cls;
76
77   *empty = GNUNET_NO;
78   return GNUNET_OK;
79 }
80 #endif
81
82 /**
83  * Receive the HELLO from one peer, give it to the other
84  * and ask them to connect.
85  *
86  * @param cls "struct ConnectContext"
87  * @param message HELLO message of peer
88  */
89 static void
90 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
91 {
92   struct GNUNET_TESTING_Daemon *daemon = cls;
93   int msize;
94
95 #if WAIT_FOR_HELLO
96   GNUNET_TESTING_NotifyDaemonRunning cb;
97 #endif
98 #if EMPTY_HACK
99   int empty;
100
101   empty = GNUNET_YES;
102   GNUNET_assert (message != NULL);
103   GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) message,
104                                   GNUNET_NO, &test_address, &empty);
105   if (GNUNET_YES == empty)
106   {
107 #if DEBUG_TESTING
108     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping empty HELLO address\n");
109 #endif
110     return;
111   }
112 #endif
113   if (daemon == NULL)
114     return;
115
116   GNUNET_assert (daemon->phase == SP_GET_HELLO ||
117                  daemon->phase == SP_START_DONE);
118 #if WAIT_FOR_HELLO
119   cb = daemon->cb;
120 #endif
121   daemon->cb = NULL;
122   if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
123     GNUNET_SCHEDULER_cancel (daemon->task);
124
125   if (daemon->server != NULL)
126   {
127 #if DEBUG_TESTING
128     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
129                 "Received `%s' from transport service of `%4s', disconnecting core!\n",
130                 "HELLO", GNUNET_i2s (&daemon->id));
131 #endif
132     GNUNET_CORE_disconnect (daemon->server);
133     daemon->server = NULL;
134   }
135
136   msize = ntohs (message->size);
137   if (msize < 1)
138   {
139     return;
140   }
141   if (daemon->th != NULL)
142   {
143     GNUNET_TRANSPORT_get_hello_cancel (daemon->th, &process_hello, daemon);
144   }
145 #if DEBUG_TESTING
146   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
147               "Received `%s' from transport service of `%4s'\n",
148               "HELLO", GNUNET_i2s (&daemon->id));
149 #endif
150
151   GNUNET_free_non_null (daemon->hello);
152   daemon->hello = GNUNET_malloc (msize);
153   memcpy (daemon->hello, message, msize);
154
155   if (daemon->th != NULL)
156   {
157     GNUNET_TRANSPORT_disconnect (daemon->th);
158     daemon->th = NULL;
159   }
160   daemon->phase = SP_START_DONE;
161
162 #if WAIT_FOR_HELLO
163   if (NULL != cb)               /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
164     cb (daemon->cb_cls, &daemon->id, daemon->cfg, daemon, NULL);
165 #endif
166 }
167
168 static void
169 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
170
171 #if WAIT_FOR_HELLO
172 /**
173  * Function called after GNUNET_CORE_connect has succeeded
174  * (or failed for good).  Note that the private key of the
175  * peer is intentionally not exposed here; if you need it,
176  * your process should try to read the private key file
177  * directly (which should work if you are authorized...).
178  *
179  * @param cls closure
180  * @param server handle to the server, NULL if we failed
181  * @param my_identity ID of this peer, NULL if we failed
182  * @param publicKey public key of this peer, NULL if we failed
183  */
184 static void
185 testing_init (void *cls,
186               struct GNUNET_CORE_Handle *server,
187               const struct GNUNET_PeerIdentity *my_identity,
188               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
189 {
190   struct GNUNET_TESTING_Daemon *d = cls;
191
192   GNUNET_assert (d->phase == SP_START_CORE);
193   d->phase = SP_GET_HELLO;
194
195   if (server == NULL)
196   {
197     d->server = NULL;
198     if (GNUNET_YES == d->dead)
199       GNUNET_TESTING_daemon_stop (d,
200                                   GNUNET_TIME_absolute_get_remaining
201                                   (d->max_timeout), d->dead_cb,
202                                   d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
203     else if (NULL != d->cb)
204       d->cb (d->cb_cls, NULL, d->cfg, d,
205              _("Failed to connect to core service\n"));
206     return;
207   }
208 #if DEBUG_TESTING
209   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210               "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
211 #endif
212   d->id = *my_identity;         /* FIXME: shouldn't we already have this from reading the hostkey file? */
213   if (d->shortname == NULL)
214     d->shortname = strdup (GNUNET_i2s (my_identity));
215   d->server = server;
216   d->running = GNUNET_YES;
217
218   if (GNUNET_NO == d->running)
219   {
220 #if DEBUG_TESTING
221     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
222                 "Peer is dead (d->running == GNUNET_NO)\n");
223 #endif
224     return;
225   }
226 #if DEBUG_TESTING
227   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
228               "Successfully started peer `%4s', connecting to transport service.\n",
229               GNUNET_i2s (my_identity));
230 #endif
231
232   d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
233   if (d->th == NULL)
234   {
235     if (GNUNET_YES == d->dead)
236       GNUNET_TESTING_daemon_stop (d,
237                                   GNUNET_TIME_absolute_get_remaining
238                                   (d->max_timeout), d->dead_cb,
239                                   d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
240     else if (NULL != d->cb)
241       d->cb (d->cb_cls, &d->id, d->cfg, d,
242              _("Failed to connect to transport service!\n"));
243     return;
244   }
245 #if DEBUG_TESTING
246   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247               "Connected to transport service `%s', getting HELLO\n",
248               GNUNET_i2s (my_identity));
249 #endif
250
251   GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
252   /* wait some more */
253   if (d->task != GNUNET_SCHEDULER_NO_TASK)
254     GNUNET_SCHEDULER_cancel (d->task);
255   d->task
256       = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
257                                       &start_fsm, d);
258 }
259 #endif
260
261 #if !WAIT_FOR_HELLO
262 /**
263  * Notify of a peer being up and running.  Scheduled as a task
264  * so that variables which may need to be set are set before
265  * the connect callback can set up new operations.
266  *
267  * @param cls the testing daemon
268  * @param tc task scheduler context
269  */
270 static void
271 notify_daemon_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
272 {
273   struct GNUNET_TESTING_Daemon *d = cls;
274   GNUNET_TESTING_NotifyDaemonRunning cb;
275
276   cb = d->cb;
277   d->cb = NULL;
278   if (NULL != cb)
279     cb (d->cb_cls, &d->id, d->cfg, d, NULL);
280 }
281 #endif
282
283 /**
284  * Finite-state machine for starting GNUnet.
285  *
286  * @param cls our "struct GNUNET_TESTING_Daemon"
287  * @param tc unused
288  */
289 static void
290 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
291 {
292   struct GNUNET_TESTING_Daemon *d = cls;
293   GNUNET_TESTING_NotifyDaemonRunning cb;
294   enum GNUNET_OS_ProcessStatusType type;
295   unsigned long code;
296   char *dst;
297   int bytes_read;
298
299 #if DEBUG_TESTING
300   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer FSM is in phase %u.\n", d->phase);
301 #endif
302
303   d->task = GNUNET_SCHEDULER_NO_TASK;
304   switch (d->phase)
305   {
306   case SP_COPYING:
307     /* confirm copying complete */
308     if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
309     {
310       if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
311       {
312         cb = d->cb;
313         d->cb = NULL;
314         if (NULL != cb)
315           cb (d->cb_cls,
316               NULL,
317               d->cfg, d,
318               _
319               ("`scp' does not seem to terminate (timeout copying config).\n"));
320         return;
321       }
322       /* wait some more */
323       d->task
324           = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
325                                           &start_fsm, d);
326       return;
327     }
328     if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
329     {
330       cb = d->cb;
331       d->cb = NULL;
332       if (NULL != cb)
333         cb (d->cb_cls, NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
334       return;
335     }
336     GNUNET_OS_process_close (d->proc);
337 #if DEBUG_TESTING
338     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
339                 "Successfully copied configuration file.\n");
340 #endif
341     d->phase = SP_COPIED;
342     /* fall-through */
343   case SP_COPIED:
344     /* Start create hostkey process if we don't already know the peer identity! */
345     if (GNUNET_NO == d->have_hostkey)
346     {
347       d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
348       if (d->pipe_stdout == NULL)
349       {
350         cb = d->cb;
351         d->cb = NULL;
352         if (NULL != cb)
353           cb (d->cb_cls,
354               NULL,
355               d->cfg,
356               d,
357               (NULL == d->hostname)
358               ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
359               : _("Failed to create pipe for `ssh' process.\n"));
360         return;
361       }
362       if (NULL == d->hostname)
363       {
364 #if DEBUG_TESTING
365         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
366                     "Starting `%s', with command `%s %s %s %s'.\n",
367                     "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
368                     "-sq");
369 #endif
370         d->proc =
371             GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
372                                      "gnunet-peerinfo", "-c", d->cfgfile,
373                                      "-sq", NULL);
374         GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
375       }
376       else
377       {
378         if (d->username != NULL)
379           GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
380         else
381           dst = GNUNET_strdup (d->hostname);
382
383 #if DEBUG_TESTING
384         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
385                     "Starting `%s', with command `%s %s %s %s %s %s'.\n",
386                     "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
387                     d->cfgfile, "-sq");
388 #endif
389         if (d->ssh_port_str == NULL)
390         {
391           d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh",
392 #if !DEBUG_TESTING
393                                              "-q",
394 #endif
395                                              dst,
396                                              "gnunet-peerinfo",
397                                              "-c", d->cfgfile, "-sq", NULL);
398         }
399         else
400         {
401           d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
402                                              "ssh", "-p", d->ssh_port_str,
403 #if !DEBUG_TESTING
404                                              "-q",
405 #endif
406                                              dst,
407                                              "gnunet-peerinfo",
408                                              "-c", d->cfgfile, "-sq", NULL);
409         }
410         GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
411         GNUNET_free (dst);
412       }
413       if (NULL == d->proc)
414       {
415         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
416                     _("Could not start `%s' process to create hostkey.\n"),
417                     (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
418         cb = d->cb;
419         d->cb = NULL;
420         if (NULL != cb)
421           cb (d->cb_cls,
422               NULL,
423               d->cfg,
424               d,
425               (NULL == d->hostname)
426               ? _("Failed to start `gnunet-peerinfo' process.\n")
427               : _("Failed to start `ssh' process.\n"));
428         GNUNET_DISK_pipe_close (d->pipe_stdout);
429         return;
430       }
431 #if DEBUG_TESTING
432       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
433                   "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
434 #endif
435       d->phase = SP_HOSTKEY_CREATE;
436       d->task
437           =
438           GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
439                                           (d->max_timeout),
440                                           GNUNET_DISK_pipe_handle
441                                           (d->pipe_stdout,
442                                            GNUNET_DISK_PIPE_END_READ),
443                                           &start_fsm, d);
444     }
445     else                        /* Already have a hostkey! */
446     {
447       if (d->hostkey_callback != NULL)
448       {
449         d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
450         d->hostkey_callback = NULL;
451         d->phase = SP_HOSTKEY_CREATED;
452       }
453       else
454         d->phase = SP_TOPOLOGY_SETUP;
455
456       /* wait some more */
457       d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
458     }
459     break;
460   case SP_HOSTKEY_CREATE:
461     bytes_read =
462         GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
463                                (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
464                                &d->hostkeybuf[d->hostkeybufpos],
465                                sizeof (d->hostkeybuf) - d->hostkeybufpos);
466     if (bytes_read > 0)
467       d->hostkeybufpos += bytes_read;
468
469     if ((d->hostkeybufpos < 104) && (bytes_read > 0))
470     {
471       /* keep reading */
472       d->task
473           =
474           GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
475                                           (d->max_timeout),
476                                           GNUNET_DISK_pipe_handle
477                                           (d->pipe_stdout,
478                                            GNUNET_DISK_PIPE_END_READ),
479                                           &start_fsm, d);
480       return;
481     }
482     d->hostkeybuf[103] = '\0';
483
484     if ((bytes_read < 0) ||
485         (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
486                                                       &d->id.hashPubKey)))
487     {
488       /* error */
489       if (bytes_read < 0)
490         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
491                     _("Error reading from gnunet-peerinfo: %s\n"),
492                     STRERROR (errno));
493       else
494         GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
495                     _("Malformed output from gnunet-peerinfo!\n"));
496       cb = d->cb;
497       d->cb = NULL;
498       GNUNET_DISK_pipe_close (d->pipe_stdout);
499       d->pipe_stdout = NULL;
500       (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
501       GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
502       GNUNET_OS_process_close (d->proc);
503       d->proc = NULL;
504       if (NULL != cb)
505         cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
506       return;
507     }
508     d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
509     GNUNET_DISK_pipe_close (d->pipe_stdout);
510     d->pipe_stdout = NULL;
511     (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
512     GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
513     GNUNET_OS_process_close (d->proc);
514     d->proc = NULL;
515     d->have_hostkey = GNUNET_YES;
516     if (d->hostkey_callback != NULL)
517     {
518       d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
519       d->hostkey_callback = NULL;
520       d->phase = SP_HOSTKEY_CREATED;
521     }
522     else
523     {
524       d->phase = SP_TOPOLOGY_SETUP;
525     }
526 #if DEBUG_TESTING
527     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
528 #endif
529     /* Fall through */
530   case SP_HOSTKEY_CREATED:
531     /* wait for topology finished */
532     if ((GNUNET_YES == d->dead)
533         || (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
534     {
535       cb = d->cb;
536       d->cb = NULL;
537       if (NULL != cb)
538         cb (d->cb_cls,
539             NULL, d->cfg, d, _("`Failed while waiting for topology setup!\n"));
540       return;
541     }
542
543     d->task
544         = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
545                                         &start_fsm, d);
546     break;
547   case SP_TOPOLOGY_SETUP:      /* Indicates topology setup has completed! */
548     /* start GNUnet on remote host */
549     if (NULL == d->hostname)
550     {
551 #if DEBUG_TESTING
552       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
553                   "Starting `%s', with command `%s %s %s %s %s %s'.\n",
554                   "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
555                   "-L", "DEBUG", "-s");
556 #endif
557       d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
558                                          "gnunet-arm", "-c", d->cfgfile,
559 #if DEBUG_TESTING
560                                          "-L", "DEBUG",
561 #endif
562                                          "-s", "-q", "-T",
563                                          GNUNET_TIME_relative_to_string
564                                          (GNUNET_TIME_absolute_get_remaining
565                                           (d->max_timeout)), NULL);
566     }
567     else
568     {
569       if (d->username != NULL)
570         GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
571       else
572         dst = GNUNET_strdup (d->hostname);
573
574 #if DEBUG_TESTING
575       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
576                   "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
577                   "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
578                   d->cfgfile, "-L", "DEBUG", "-s", "-q");
579 #endif
580       if (d->ssh_port_str == NULL)
581       {
582         d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
583 #if !DEBUG_TESTING
584                                            "-q",
585 #endif
586                                            dst, "gnunet-arm",
587 #if DEBUG_TESTING
588                                            "-L", "DEBUG",
589 #endif
590                                            "-c", d->cfgfile, "-s", "-q",
591                                            "-T",
592                                            GNUNET_TIME_relative_to_string
593                                            (GNUNET_TIME_absolute_get_remaining
594                                             (d->max_timeout)), NULL);
595       }
596       else
597       {
598
599         d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
600                                            "ssh", "-p", d->ssh_port_str,
601 #if !DEBUG_TESTING
602                                            "-q",
603 #endif
604                                            dst, "gnunet-arm",
605 #if DEBUG_TESTING
606                                            "-L", "DEBUG",
607 #endif
608                                            "-c", d->cfgfile, "-s", "-q",
609                                            "-T",
610                                            GNUNET_TIME_relative_to_string
611                                            (GNUNET_TIME_absolute_get_remaining
612                                             (d->max_timeout)), NULL);
613       }
614       GNUNET_free (dst);
615     }
616     if (NULL == d->proc)
617     {
618       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
619                   _("Could not start `%s' process to start GNUnet.\n"),
620                   (NULL == d->hostname) ? "gnunet-arm" : "ssh");
621       cb = d->cb;
622       d->cb = NULL;
623       if (NULL != cb)
624         cb (d->cb_cls,
625             NULL,
626             d->cfg,
627             d,
628             (NULL == d->hostname)
629             ? _("Failed to start `gnunet-arm' process.\n")
630             : _("Failed to start `ssh' process.\n"));
631       return;
632     }
633 #if DEBUG_TESTING
634     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
635                 "Started `%s', waiting for `%s' to be up.\n",
636                 "gnunet-arm", "gnunet-service-core");
637 #endif
638     d->phase = SP_START_ARMING;
639     d->task
640         = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
641                                         &start_fsm, d);
642     break;
643   case SP_START_ARMING:
644     if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
645     {
646       if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
647       {
648         cb = d->cb;
649         d->cb = NULL;
650         if (NULL != cb)
651           cb (d->cb_cls,
652               NULL,
653               d->cfg,
654               d,
655               (NULL == d->hostname)
656               ? _("`gnunet-arm' does not seem to terminate.\n")
657               : _("`ssh' does not seem to terminate.\n"));
658         GNUNET_CONFIGURATION_destroy (d->cfg);
659         GNUNET_free (d->cfgfile);
660         GNUNET_free_non_null (d->hostname);
661         GNUNET_free_non_null (d->username);
662         GNUNET_free (d->proc);
663         GNUNET_free (d);
664         return;
665       }
666       /* wait some more */
667       d->task
668           = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
669                                           &start_fsm, d);
670       return;
671     }
672 #if DEBUG_TESTING
673     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
674                 "Successfully started `%s'.\n", "gnunet-arm");
675 #endif
676     GNUNET_free (d->proc);
677     d->phase = SP_START_CORE;
678 #if DEBUG_TESTING
679     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Calling CORE_connect\n");
680 #endif
681     /* Fall through */
682   case SP_START_CORE:
683     if (d->server != NULL)
684       GNUNET_CORE_disconnect (d->server);
685
686 #if WAIT_FOR_HELLO
687     if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
688     {
689       cb = d->cb;
690       d->cb = NULL;
691       if (NULL != cb)
692         cb (d->cb_cls,
693             NULL,
694             d->cfg, d, _("Unable to connect to CORE service for peer!\n"));
695       GNUNET_CONFIGURATION_destroy (d->cfg);
696       GNUNET_free (d->cfgfile);
697       GNUNET_free_non_null (d->hostname);
698       GNUNET_free_non_null (d->username);
699       GNUNET_free (d);
700       return;
701     }
702     d->server = GNUNET_CORE_connect (d->cfg, 1,
703                                      d,
704                                      &testing_init,
705                                      NULL, NULL, NULL,
706                                      NULL, GNUNET_NO,
707                                      NULL, GNUNET_NO, no_handlers);
708     d->task
709         =
710         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
711                                       (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
712                                       &start_fsm, d);
713 #else
714     d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
715     if (d->th == NULL)
716     {
717       if (GNUNET_YES == d->dead)
718         GNUNET_TESTING_daemon_stop (d,
719                                     GNUNET_TIME_absolute_get_remaining
720                                     (d->max_timeout), d->dead_cb,
721                                     d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
722       else if (NULL != d->cb)
723         d->cb (d->cb_cls, &d->id, d->cfg, d,
724                _("Failed to connect to transport service!\n"));
725       return;
726     }
727 #if DEBUG_TESTING
728     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729                 "Connected to transport service `%s', getting HELLO\n",
730                 GNUNET_i2s (&d->id));
731 #endif
732
733     GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
734     GNUNET_SCHEDULER_add_now (&notify_daemon_started, d);
735     /*cb = d->cb;
736      * d->cb = NULL;
737      * if (NULL != cb)
738      * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
739     d->running = GNUNET_YES;
740     d->phase = SP_GET_HELLO;
741 #endif
742     break;
743   case SP_GET_HELLO:
744     if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
745     {
746       if (d->server != NULL)
747         GNUNET_CORE_disconnect (d->server);
748       if (d->th != NULL)
749         GNUNET_TRANSPORT_disconnect (d->th);
750       cb = d->cb;
751       d->cb = NULL;
752       if (NULL != cb)
753         cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
754       GNUNET_CONFIGURATION_destroy (d->cfg);
755       GNUNET_free (d->cfgfile);
756       GNUNET_free_non_null (d->hostname);
757       GNUNET_free_non_null (d->username);
758       GNUNET_free (d);
759       return;
760     }
761     if (d->hello != NULL)
762       return;
763     GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
764     d->task
765         =
766         GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
767                                       (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
768                                       &start_fsm, d);
769     break;
770   case SP_START_DONE:
771     GNUNET_break (0);
772     break;
773   case SP_SERVICE_START:
774     /* confirm gnunet-arm exited */
775     if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
776     {
777       if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
778       {
779         cb = d->cb;
780         d->cb = NULL;
781         if (NULL != cb)
782           cb (d->cb_cls,
783               NULL,
784               d->cfg,
785               d,
786               (NULL == d->hostname)
787               ? _("`gnunet-arm' does not seem to terminate.\n")
788               : _("`ssh' does not seem to terminate.\n"));
789         return;
790       }
791       /* wait some more */
792       d->task
793           = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
794                                           &start_fsm, d);
795       return;
796     }
797 #if EXTRA_CHECKS
798     if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
799     {
800       cb = d->cb;
801       d->cb = NULL;
802       if (NULL != cb)
803         cb (d->cb_cls,
804             NULL,
805             d->cfg,
806             d,
807             (NULL == d->hostname)
808             ?
809             _
810             ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
811             : _("`ssh' does not seem to terminate.\n"));
812       return;
813     }
814 #endif
815 #if DEBUG_TESTING
816     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
817 #endif
818     cb = d->cb;
819     d->cb = NULL;
820     d->phase = SP_START_DONE;
821     if (NULL != cb)
822       cb (d->cb_cls, &d->id, d->cfg, d, NULL);
823     break;
824   case SP_SERVICE_SHUTDOWN_START:
825     /* confirm copying complete */
826     if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
827     {
828       if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
829       {
830         if (NULL != d->dead_cb)
831           d->dead_cb (d->dead_cb_cls,
832                       _
833                       ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
834         return;
835       }
836       /* wait some more */
837       d->task
838           = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
839                                           &start_fsm, d);
840       return;
841     }
842 #if EXTRA_CHECKS
843     if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
844     {
845       if (NULL != d->dead_cb)
846         d->dead_cb (d->dead_cb_cls,
847                     _
848                     ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
849       return;
850     }
851 #endif
852 #if DEBUG_TESTING
853     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
854 #endif
855     if (NULL != d->dead_cb)
856       d->dead_cb (d->dead_cb_cls, NULL);
857     break;
858   case SP_SHUTDOWN_START:
859     /* confirm copying complete */
860     if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
861     {
862       if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
863       {
864         if (NULL != d->dead_cb)
865           d->dead_cb (d->dead_cb_cls,
866                       _
867                       ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
868         if (d->th != NULL)
869         {
870           GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
871           GNUNET_TRANSPORT_disconnect (d->th);
872           d->th = NULL;
873         }
874         GNUNET_CONFIGURATION_destroy (d->cfg);
875         GNUNET_free (d->cfgfile);
876         GNUNET_free_non_null (d->hello);
877         GNUNET_free_non_null (d->hostname);
878         GNUNET_free_non_null (d->username);
879         GNUNET_free_non_null (d->shortname);
880         GNUNET_free_non_null (d->proc);
881         d->proc = NULL;
882         GNUNET_free (d);
883         return;
884       }
885       /* wait some more */
886       d->task
887           = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
888                                           &start_fsm, d);
889       return;
890     }
891     if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
892     {
893       if (NULL != d->dead_cb)
894         d->dead_cb (d->dead_cb_cls,
895                     _
896                     ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
897       if (d->th != NULL)
898       {
899         GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
900         GNUNET_TRANSPORT_disconnect (d->th);
901         d->th = NULL;
902       }
903       if (d->server != NULL)
904       {
905         GNUNET_CORE_disconnect (d->server);
906         d->server = NULL;
907       }
908       GNUNET_CONFIGURATION_destroy (d->cfg);
909       GNUNET_free (d->cfgfile);
910       GNUNET_free_non_null (d->hello);
911       GNUNET_free_non_null (d->hostname);
912       GNUNET_free_non_null (d->username);
913       GNUNET_free_non_null (d->shortname);
914       GNUNET_free_non_null (d->proc);
915       d->proc = NULL;
916       GNUNET_free (d);
917       return;
918     }
919 #if DEBUG_TESTING
920     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
921 #endif
922     if (d->server != NULL)
923     {
924       GNUNET_CORE_disconnect (d->server);
925       d->server = NULL;
926     }
927
928     if (d->th != NULL)
929     {
930       GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
931       GNUNET_TRANSPORT_disconnect (d->th);
932       d->th = NULL;
933     }
934
935     if (NULL != d->dead_cb)
936       d->dead_cb (d->dead_cb_cls, NULL);
937
938     /* state clean up and notifications */
939     if (d->churn == GNUNET_NO)
940     {
941       GNUNET_CONFIGURATION_destroy (d->cfg);
942       GNUNET_free (d->cfgfile);
943       GNUNET_free_non_null (d->hostname);
944       GNUNET_free_non_null (d->username);
945     }
946
947     GNUNET_free_non_null (d->hello);
948     d->hello = NULL;
949     GNUNET_free_non_null (d->shortname);
950     GNUNET_free_non_null (d->proc);
951     d->proc = NULL;
952     d->shortname = NULL;
953     if (d->churn == GNUNET_NO)
954       GNUNET_free (d);
955
956     break;
957   case SP_CONFIG_UPDATE:
958     /* confirm copying complete */
959     if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
960     {
961       if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)   /* FIXME: config update should take timeout parameter! */
962       {
963         cb = d->cb;
964         d->cb = NULL;
965         if (NULL != cb)
966           cb (d->cb_cls,
967               NULL, d->cfg, d, _("`scp' does not seem to terminate.\n"));
968         return;
969       }
970       /* wait some more */
971       d->task
972           = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
973                                           &start_fsm, d);
974       return;
975     }
976     if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
977     {
978       if (NULL != d->update_cb)
979         d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
980       return;
981     }
982 #if DEBUG_TESTING
983     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
984                 "Successfully copied configuration file.\n");
985 #endif
986     if (NULL != d->update_cb)
987       d->update_cb (d->update_cb_cls, NULL);
988     d->phase = SP_START_DONE;
989     break;
990   }
991 }
992
993 /**
994  * Continues GNUnet daemon startup when user wanted to be notified
995  * once a hostkey was generated (for creating friends files, blacklists,
996  * etc.).
997  *
998  * @param daemon the daemon to finish starting
999  */
1000 void
1001 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
1002 {
1003   GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
1004   daemon->phase = SP_TOPOLOGY_SETUP;
1005 }
1006
1007 /**
1008  * Check whether the given daemon is running.
1009  *
1010  * @param daemon the daemon to check
1011  *
1012  * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
1013  *         daemon is down, GNUNET_SYSERR on error.
1014  */
1015 int
1016 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
1017 {
1018   if (daemon == NULL)
1019     return GNUNET_SYSERR;
1020
1021   if (daemon->running == GNUNET_YES)
1022     return GNUNET_YES;
1023   return GNUNET_NO;
1024 }
1025
1026
1027 /**
1028  * Starts a GNUnet daemon service which has been previously stopped.
1029  *
1030  * @param d the daemon for which the service should be started
1031  * @param service the name of the service to start
1032  * @param timeout how long to wait for process for shutdown to complete
1033  * @param cb function called once the service starts
1034  * @param cb_cls closure for cb
1035  */
1036 void
1037 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
1038                                              char *service,
1039                                              struct GNUNET_TIME_Relative
1040                                              timeout,
1041                                              GNUNET_TESTING_NotifyDaemonRunning
1042                                              cb, void *cb_cls)
1043 {
1044   char *arg;
1045
1046   d->cb = cb;
1047   d->cb_cls = cb_cls;
1048
1049   GNUNET_assert (d->running == GNUNET_YES);
1050
1051   if (d->phase == SP_CONFIG_UPDATE)
1052   {
1053     GNUNET_SCHEDULER_cancel (d->task);
1054     d->phase = SP_START_DONE;
1055   }
1056
1057   if (d->churned_services == NULL)
1058   {
1059     d->cb (d->cb_cls, &d->id, d->cfg, d,
1060            "No service has been churned off yet!!");
1061     return;
1062   }
1063   d->phase = SP_SERVICE_START;
1064   GNUNET_free (d->churned_services);
1065   d->churned_services = NULL;
1066   d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1067   /* Check if this is a local or remote process */
1068   if (NULL != d->hostname)
1069   {
1070 #if DEBUG_TESTING
1071     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072                 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1073                 d->cfgfile, d->hostname);
1074 #endif
1075
1076     if (d->username != NULL)
1077       GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1078     else
1079       arg = GNUNET_strdup (d->hostname);
1080
1081     d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1082 #if !DEBUG_TESTING
1083                                        "-q",
1084 #endif
1085                                        arg, "gnunet-arm",
1086 #if DEBUG_TESTING
1087                                        "-L", "DEBUG",
1088 #endif
1089                                        "-c", d->cfgfile, "-i", service, "-q",
1090                                        "-T",
1091                                        GNUNET_TIME_relative_to_string (timeout),
1092                                        NULL);
1093     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094                 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
1095                 arg, "gnunet-arm", d->cfgfile, service);
1096     GNUNET_free (arg);
1097   }
1098   else
1099   {
1100 #if DEBUG_TESTING
1101     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1102                 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1103 #endif
1104     d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1105 #if DEBUG_TESTING
1106                                        "-L", "DEBUG",
1107 #endif
1108                                        "-c", d->cfgfile, "-i", service, "-q",
1109                                        "-T",
1110                                        GNUNET_TIME_relative_to_string (timeout),
1111                                        NULL);
1112   }
1113
1114   d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1115   d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1116 }
1117
1118 /**
1119  * Starts a GNUnet daemon's service.
1120  *
1121  * @param d the daemon for which the service should be started
1122  * @param service the name of the service to start
1123  * @param timeout how long to wait for process for startup
1124  * @param cb function called once gnunet-arm returns
1125  * @param cb_cls closure for cb
1126  */
1127 void
1128 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
1129                                      char *service,
1130                                      struct GNUNET_TIME_Relative timeout,
1131                                      GNUNET_TESTING_NotifyDaemonRunning cb,
1132                                      void *cb_cls)
1133 {
1134   char *arg;
1135
1136   d->cb = cb;
1137   d->cb_cls = cb_cls;
1138
1139   GNUNET_assert (service != NULL);
1140   GNUNET_assert (d->running == GNUNET_YES);
1141   GNUNET_assert (d->phase == SP_START_DONE);
1142
1143 #if DEBUG_TESTING
1144   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145               _("Starting service %s for peer `%4s'\n"), service,
1146               GNUNET_i2s (&d->id));
1147 #endif
1148
1149   d->phase = SP_SERVICE_START;
1150   d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1151   /* Check if this is a local or remote process */
1152   if (NULL != d->hostname)
1153   {
1154 #if DEBUG_TESTING
1155     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156                 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1157                 d->cfgfile, d->hostname);
1158 #endif
1159
1160     if (d->username != NULL)
1161       GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1162     else
1163       arg = GNUNET_strdup (d->hostname);
1164
1165     d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1166 #if !DEBUG_TESTING
1167                                        "-q",
1168 #endif
1169                                        arg, "gnunet-arm",
1170 #if DEBUG_TESTING
1171                                        "-L", "DEBUG",
1172 #endif
1173                                        "-c", d->cfgfile, "-i", service, "-q",
1174                                        "-T",
1175                                        GNUNET_TIME_relative_to_string (timeout),
1176                                        NULL);
1177     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1178                 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
1179                 arg, "gnunet-arm", d->cfgfile, service,
1180                 GNUNET_TIME_relative_to_string (timeout));
1181     GNUNET_free (arg);
1182   }
1183   else
1184   {
1185 #if DEBUG_TESTING
1186     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187                 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1188 #endif
1189     d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1190 #if DEBUG_TESTING
1191                                        "-L", "DEBUG",
1192 #endif
1193                                        "-c", d->cfgfile, "-i", service, "-q",
1194                                        "-T",
1195                                        GNUNET_TIME_relative_to_string (timeout),
1196                                        NULL);
1197     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1198                 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1199                 "gnunet-arm", d->cfgfile, service,
1200                 GNUNET_TIME_relative_to_string (timeout));
1201   }
1202
1203   d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1204   d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1205 }
1206
1207 /**
1208  * Start a peer that has previously been stopped using the daemon_stop
1209  * call (and files weren't deleted and the allow restart flag)
1210  *
1211  * @param daemon the daemon to start (has been previously stopped)
1212  * @param timeout how long to wait for restart
1213  * @param cb the callback for notification when the peer is running
1214  * @param cb_cls closure for the callback
1215  */
1216 void
1217 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1218                                      struct GNUNET_TIME_Relative timeout,
1219                                      GNUNET_TESTING_NotifyDaemonRunning cb,
1220                                      void *cb_cls)
1221 {
1222   if (daemon->running == GNUNET_YES)
1223   {
1224     cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1225         "Daemon already running, can't restart!");
1226     return;
1227   }
1228
1229   daemon->cb = cb;
1230   daemon->cb_cls = cb_cls;
1231   daemon->phase = SP_TOPOLOGY_SETUP;
1232   daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1233
1234   GNUNET_SCHEDULER_add_continuation (&start_fsm,
1235                                      daemon,
1236                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1237 }
1238
1239 /**
1240  * Starts a GNUnet daemon.  GNUnet must be installed on the target
1241  * system and available in the PATH.  The machine must furthermore be
1242  * reachable via "ssh" (unless the hostname is "NULL") without the
1243  * need to enter a password.
1244  *
1245  * @param cfg configuration to use
1246  * @param timeout how long to wait starting up peers
1247  * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1248  *                to really start the peer (default)
1249  * @param hostname name of the machine where to run GNUnet
1250  *        (use NULL for localhost).
1251  * @param ssh_username ssh username to use when connecting to hostname
1252  * @param sshport port to pass to ssh process when connecting to hostname
1253  * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1254  * @param hostkey_callback function to call once the hostkey has been
1255  *        generated for this peer, but it hasn't yet been started
1256  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1257  * @param hostkey_cls closure for hostkey callback
1258  * @param cb function to call once peer is up, or failed to start
1259  * @param cb_cls closure for cb
1260  * @return handle to the daemon (actual start will be completed asynchronously)
1261  */
1262 struct GNUNET_TESTING_Daemon *
1263 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1264                              struct GNUNET_TIME_Relative timeout,
1265                              int pretend,
1266                              const char *hostname,
1267                              const char *ssh_username,
1268                              uint16_t sshport,
1269                              const char *hostkey,
1270                              GNUNET_TESTING_NotifyHostkeyCreated
1271                              hostkey_callback, void *hostkey_cls,
1272                              GNUNET_TESTING_NotifyDaemonRunning cb,
1273                              void *cb_cls)
1274 {
1275   struct GNUNET_TESTING_Daemon *ret;
1276   char *arg;
1277   char *username;
1278   char *servicehome;
1279   char *baseservicehome;
1280   char *slash;
1281   char *hostkeyfile;
1282   char *temp_file_name;
1283   struct GNUNET_DISK_FileHandle *fn;
1284   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1285   struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1286
1287   ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1288   ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1289   if (sshport != 0)
1290   {
1291     GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1292   }
1293   else
1294     ret->ssh_port_str = NULL;
1295
1296   /* Find service home and base service home directories, create it if it doesn't exist */
1297   GNUNET_assert (GNUNET_OK ==
1298                  GNUNET_CONFIGURATION_get_value_string (cfg,
1299                                                         "PATHS",
1300                                                         "SERVICEHOME",
1301                                                         &servicehome));
1302
1303   GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1304   GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1305   ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1306   GNUNET_free (temp_file_name);
1307 #if DEBUG_TESTING
1308   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1309               "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1310 #endif
1311   if (NULL == ret->cfgfile)
1312   {
1313     GNUNET_free_non_null (ret->ssh_port_str);
1314     GNUNET_free_non_null (ret->hostname);
1315     GNUNET_free (ret);
1316     return NULL;
1317   }
1318   ret->hostkey_callback = hostkey_callback;
1319   ret->hostkey_cls = hostkey_cls;
1320   ret->cb = cb;
1321   ret->cb_cls = cb_cls;
1322   ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1323   ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1324   GNUNET_CONFIGURATION_set_value_string (ret->cfg,
1325                                          "PATHS",
1326                                          "DEFAULTCONFIG", ret->cfgfile);
1327
1328   if (hostkey != NULL)          /* Get the peer identity from the hostkey */
1329   {
1330     private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1331     GNUNET_assert (private_key != NULL);
1332     GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1333     GNUNET_CRYPTO_hash (&public_key,
1334                         sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1335                         &ret->id.hashPubKey);
1336     ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1337     ret->have_hostkey = GNUNET_YES;
1338     GNUNET_CRYPTO_rsa_key_free (private_key);
1339   }
1340
1341   /* Write hostkey to file, if we were given one */
1342   hostkeyfile = NULL;
1343   if (hostkey != NULL)
1344   {
1345     GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1346     fn = GNUNET_DISK_file_open (hostkeyfile,
1347                                 GNUNET_DISK_OPEN_READWRITE
1348                                 | GNUNET_DISK_OPEN_CREATE,
1349                                 GNUNET_DISK_PERM_USER_READ |
1350                                 GNUNET_DISK_PERM_USER_WRITE);
1351     GNUNET_assert (fn != NULL);
1352     GNUNET_assert (HOSTKEYFILESIZE ==
1353                    GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1354     GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1355   }
1356
1357   /* write configuration to temporary file */
1358   if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1359   {
1360     if (0 != UNLINK (ret->cfgfile))
1361       GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1362                                 "unlink", ret->cfgfile);
1363     GNUNET_CONFIGURATION_destroy (ret->cfg);
1364     GNUNET_free_non_null (ret->hostname);
1365     GNUNET_free (ret->cfgfile);
1366     GNUNET_free (ret);
1367     return NULL;
1368   }
1369   if (ssh_username != NULL)
1370     username = GNUNET_strdup (ssh_username);
1371   if ((ssh_username == NULL) && (GNUNET_OK !=
1372                                  GNUNET_CONFIGURATION_get_value_string (cfg,
1373                                                                         "TESTING",
1374                                                                         "USERNAME",
1375                                                                         &username)))
1376   {
1377     if (NULL != getenv ("USER"))
1378       username = GNUNET_strdup (getenv ("USER"));
1379     else
1380       username = NULL;
1381   }
1382   ret->username = username;
1383
1384   if (GNUNET_NO == pretend)     /* Copy files, enter finite state machine */
1385   {
1386     /* copy directory to remote host */
1387     if (NULL != hostname)
1388     {
1389 #if DEBUG_TESTING
1390       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1391                   "Copying configuration directory to host `%s'.\n", hostname);
1392 #endif
1393       baseservicehome = GNUNET_strdup (servicehome);
1394       /* Remove trailing /'s */
1395       while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1396         baseservicehome[strlen (baseservicehome) - 1] = '\0';
1397       /* Find next directory /, jump one ahead */
1398       slash = strrchr (baseservicehome, '/');
1399       if (slash != NULL)
1400         *(++slash) = '\0';
1401
1402       ret->phase = SP_COPYING;
1403       if (NULL != username)
1404         GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1405       else
1406         GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1407       GNUNET_free (baseservicehome);
1408       if (ret->ssh_port_str == NULL)
1409       {
1410         ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1411 #if !DEBUG_TESTING
1412                                              "-q",
1413 #endif
1414                                              servicehome, arg, NULL);
1415 #if DEBUG_TESTING
1416         GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1417                     "copying directory with command scp -r %s %s\n",
1418                     servicehome, arg);
1419 #endif
1420       }
1421       else
1422       {
1423         ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
1424                                              "scp", "-r", "-P",
1425                                              ret->ssh_port_str,
1426 #if !DEBUG_TESTING
1427                                              "-q",
1428 #endif
1429                                              servicehome, arg, NULL);
1430       }
1431       GNUNET_free (arg);
1432       if (NULL == ret->proc)
1433       {
1434         GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1435                     _
1436                     ("Could not start `%s' process to copy configuration directory.\n"),
1437                     "scp");
1438         if (0 != UNLINK (ret->cfgfile))
1439           GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1440                                     "unlink", ret->cfgfile);
1441         GNUNET_CONFIGURATION_destroy (ret->cfg);
1442         GNUNET_free_non_null (ret->hostname);
1443         GNUNET_free_non_null (ret->username);
1444         GNUNET_free (ret->cfgfile);
1445         GNUNET_free (ret);
1446         if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1447           GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1448                                     "unlink", hostkeyfile);
1449         GNUNET_free_non_null (hostkeyfile);
1450         GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1451         GNUNET_free (servicehome);
1452         return NULL;
1453       }
1454
1455       ret->task
1456           = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1457                                           &start_fsm, ret);
1458       GNUNET_free_non_null (hostkeyfile);
1459       GNUNET_free (servicehome);
1460       return ret;
1461     }
1462 #if DEBUG_TESTING
1463     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464                 "No need to copy configuration file since we are running locally.\n");
1465 #endif
1466     ret->phase = SP_COPIED;
1467     GNUNET_SCHEDULER_add_continuation (&start_fsm,
1468                                        ret,
1469                                        GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1470   }
1471   GNUNET_free_non_null (hostkeyfile);
1472   GNUNET_free (servicehome);
1473   return ret;
1474 }
1475
1476
1477 /**
1478  * Restart (stop and start) a GNUnet daemon.
1479  *
1480  * @param d the daemon that should be restarted
1481  * @param cb function called once the daemon is (re)started
1482  * @param cb_cls closure for cb
1483  */
1484 void
1485 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1486                                GNUNET_TESTING_NotifyDaemonRunning cb,
1487                                void *cb_cls)
1488 {
1489   char *arg;
1490   char *del_arg;
1491
1492   del_arg = NULL;
1493   if (NULL != d->cb)
1494   {
1495     d->dead = GNUNET_YES;
1496     return;
1497   }
1498
1499   d->cb = cb;
1500   d->cb_cls = cb_cls;
1501
1502   if (d->phase == SP_CONFIG_UPDATE)
1503   {
1504     GNUNET_SCHEDULER_cancel (d->task);
1505     d->phase = SP_START_DONE;
1506   }
1507   if (d->server != NULL)
1508   {
1509     GNUNET_CORE_disconnect (d->server);
1510     d->server = NULL;
1511   }
1512
1513   if (d->th != NULL)
1514   {
1515     GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1516     GNUNET_TRANSPORT_disconnect (d->th);
1517     d->th = NULL;
1518   }
1519   /* state clean up and notifications */
1520   GNUNET_free_non_null (d->hello);
1521
1522 #if DEBUG_TESTING
1523   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524               _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1525 #endif
1526
1527   d->phase = SP_START_ARMING;
1528
1529   /* Check if this is a local or remote process */
1530   if (NULL != d->hostname)
1531   {
1532 #if DEBUG_TESTING
1533     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534                 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1535                 d->cfgfile, d->hostname);
1536 #endif
1537
1538     if (d->username != NULL)
1539       GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1540     else
1541       arg = GNUNET_strdup (d->hostname);
1542
1543     d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1544 #if !DEBUG_TESTING
1545                                        "-q",
1546 #endif
1547                                        arg, "gnunet-arm",
1548 #if DEBUG_TESTING
1549                                        "-L", "DEBUG",
1550 #endif
1551                                        "-c", d->cfgfile, "-e", "-r", NULL);
1552     /* Use -r to restart arm and all services */
1553
1554     GNUNET_free (arg);
1555   }
1556   else
1557   {
1558 #if DEBUG_TESTING
1559     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560                 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1561 #endif
1562     d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1563 #if DEBUG_TESTING
1564                                        "-L", "DEBUG",
1565 #endif
1566                                        "-c", d->cfgfile, "-e", "-r", NULL);
1567   }
1568
1569   GNUNET_free_non_null (del_arg);
1570   d->task
1571       = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1572                                       &start_fsm, d);
1573
1574 }
1575
1576
1577 /**
1578  * Stops a GNUnet daemon.
1579  *
1580  * @param d the daemon that should be stopped
1581  * @param service the name of the service to stop
1582  * @param timeout how long to wait for process for shutdown to complete
1583  * @param cb function called once the daemon was stopped
1584  * @param cb_cls closure for cb
1585  * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1586  *        to leave them
1587  * @param allow_restart GNUNET_YES to restart peer later (using this API)
1588  *        GNUNET_NO to kill off and clean up for good
1589  */
1590 void
1591 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1592                                     char *service,
1593                                     struct GNUNET_TIME_Relative timeout,
1594                                     GNUNET_TESTING_NotifyCompletion cb,
1595                                     void *cb_cls)
1596 {
1597   char *arg;
1598
1599   d->dead_cb = cb;
1600   d->dead_cb_cls = cb_cls;
1601
1602   GNUNET_assert (d->running == GNUNET_YES);
1603
1604   if (d->phase == SP_CONFIG_UPDATE)
1605   {
1606     GNUNET_SCHEDULER_cancel (d->task);
1607     d->phase = SP_START_DONE;
1608   }
1609
1610 #if DEBUG_TESTING
1611   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612               _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1613 #endif
1614   if (d->churned_services != NULL)
1615   {
1616     d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1617     return;
1618   }
1619   d->phase = SP_SERVICE_SHUTDOWN_START;
1620   d->churned_services = GNUNET_strdup (service);
1621   d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1622   /* Check if this is a local or remote process */
1623   if (NULL != d->hostname)
1624   {
1625 #if DEBUG_TESTING
1626     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627                 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1628                 d->cfgfile, d->hostname);
1629 #endif
1630
1631     if (d->username != NULL)
1632       GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1633     else
1634       arg = GNUNET_strdup (d->hostname);
1635
1636     d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1637 #if !DEBUG_TESTING
1638                                        "-q",
1639 #endif
1640                                        arg, "gnunet-arm",
1641 #if DEBUG_TESTING
1642                                        "-L", "DEBUG",
1643 #endif
1644                                        "-c", d->cfgfile, "-k", service, "-q",
1645                                        "-T",
1646                                        GNUNET_TIME_relative_to_string (timeout),
1647                                        NULL);
1648     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1649                 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1650                 arg, "gnunet-arm", d->cfgfile, service);
1651     GNUNET_free (arg);
1652   }
1653   else
1654   {
1655 #if DEBUG_TESTING
1656     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1657                 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1658 #endif
1659     d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1660 #if DEBUG_TESTING
1661                                        "-L", "DEBUG",
1662 #endif
1663                                        "-c", d->cfgfile, "-k", service, "-q",
1664                                        "-T",
1665                                        GNUNET_TIME_relative_to_string (timeout),
1666                                        NULL);
1667   }
1668
1669   d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1670   d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1671 }
1672
1673
1674 /**
1675  * Stops a GNUnet daemon.
1676  *
1677  * @param d the daemon that should be stopped
1678  * @param timeout how long to wait for process for shutdown to complete
1679  * @param cb function called once the daemon was stopped
1680  * @param cb_cls closure for cb
1681  * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1682  *        to leave them
1683  * @param allow_restart GNUNET_YES to restart peer later (using this API)
1684  *        GNUNET_NO to kill off and clean up for good
1685  */
1686 void
1687 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1688                             struct GNUNET_TIME_Relative timeout,
1689                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1690                             int delete_files, int allow_restart)
1691 {
1692   char *arg;
1693   char *del_arg;
1694
1695   d->dead_cb = cb;
1696   d->dead_cb_cls = cb_cls;
1697
1698   if (NULL != d->cb)
1699   {
1700 #if DEBUG_TESTING
1701     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702                 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1703 #endif
1704     d->dead = GNUNET_YES;
1705     return;
1706   }
1707
1708   if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES))    /* Peer has already been stopped in churn context! */
1709   {
1710     /* Free what was left from churning! */
1711     GNUNET_assert (d->cfg != NULL);
1712     GNUNET_CONFIGURATION_destroy (d->cfg);
1713     if (delete_files == GNUNET_YES)
1714     {
1715       if (0 != UNLINK (d->cfgfile))
1716       {
1717         GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1718       }
1719     }
1720     GNUNET_free (d->cfgfile);
1721     GNUNET_free_non_null (d->hostname);
1722     GNUNET_free_non_null (d->username);
1723     if (NULL != d->dead_cb)
1724       d->dead_cb (d->dead_cb_cls, NULL);
1725     GNUNET_free (d);
1726     return;
1727   }
1728
1729   del_arg = NULL;
1730   if (delete_files == GNUNET_YES)
1731   {
1732     GNUNET_asprintf (&del_arg, "-d");
1733   }
1734
1735   if (d->phase == SP_CONFIG_UPDATE)
1736   {
1737     GNUNET_SCHEDULER_cancel (d->task);
1738     d->phase = SP_START_DONE;
1739   }
1740   /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1741   if (d->server != NULL)
1742     {
1743       GNUNET_CORE_disconnect (d->server);
1744       d->server = NULL;
1745     }
1746     */
1747   /* shutdown ARM process (will terminate others) */
1748 #if DEBUG_TESTING
1749   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1750               _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1751 #endif
1752   d->phase = SP_SHUTDOWN_START;
1753   d->running = GNUNET_NO;
1754   if (allow_restart == GNUNET_YES)
1755     d->churn = GNUNET_YES;
1756   if (d->th != NULL)
1757   {
1758     GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1759     GNUNET_TRANSPORT_disconnect (d->th);
1760     d->th = NULL;
1761   }
1762   /* Check if this is a local or remote process */
1763   if (NULL != d->hostname)
1764   {
1765 #if DEBUG_TESTING
1766     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767                 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1768                 d->cfgfile, d->hostname);
1769 #endif
1770
1771     if (d->username != NULL)
1772       GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1773     else
1774       arg = GNUNET_strdup (d->hostname);
1775
1776     d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1777 #if !DEBUG_TESTING
1778                                        "-q",
1779 #endif
1780                                        arg, "gnunet-arm",
1781 #if DEBUG_TESTING
1782                                        "-L", "DEBUG",
1783 #endif
1784                                        "-c", d->cfgfile, "-e", "-q",
1785                                        "-T",
1786                                        GNUNET_TIME_relative_to_string (timeout),
1787                                        del_arg, NULL);
1788     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789                 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1790                 arg, "gnunet-arm", d->cfgfile, del_arg);
1791     /* Use -e to end arm, and -d to remove temp files */
1792     GNUNET_free (arg);
1793   }
1794   else
1795   {
1796 #if DEBUG_TESTING
1797     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798                 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1799 #endif
1800     d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1801 #if DEBUG_TESTING
1802                                        "-L", "DEBUG",
1803 #endif
1804                                        "-c", d->cfgfile, "-e", "-q",
1805                                        "-T",
1806                                        GNUNET_TIME_relative_to_string (timeout),
1807                                        del_arg, NULL);
1808   }
1809
1810   GNUNET_free_non_null (del_arg);
1811   d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1812   d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1813 }
1814
1815
1816 /**
1817  * Changes the configuration of a GNUnet daemon.
1818  *
1819  * @param d the daemon that should be modified
1820  * @param cfg the new configuration for the daemon
1821  * @param cb function called once the configuration was changed
1822  * @param cb_cls closure for cb
1823  */
1824 void
1825 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1826                                    struct GNUNET_CONFIGURATION_Handle *cfg,
1827                                    GNUNET_TESTING_NotifyCompletion cb,
1828                                    void *cb_cls)
1829 {
1830   char *arg;
1831
1832   if (d->phase != SP_START_DONE)
1833   {
1834     if (NULL != cb)
1835       cb (cb_cls,
1836           _
1837           ("Peer not yet running, can not change configuration at this point."));
1838     return;
1839   }
1840
1841   /* 1) write configuration to temporary file */
1842   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1843   {
1844     if (NULL != cb)
1845       cb (cb_cls, _("Failed to write new configuration to disk."));
1846     return;
1847   }
1848
1849   /* 2) copy file to remote host (if necessary) */
1850   if (NULL == d->hostname)
1851   {
1852     /* signal success */
1853     if (NULL != cb)
1854       cb (cb_cls, NULL);
1855     return;
1856   }
1857 #if DEBUG_TESTING
1858   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1859               "Copying updated configuration file to remote host `%s'.\n",
1860               d->hostname);
1861 #endif
1862   d->phase = SP_CONFIG_UPDATE;
1863   if (NULL != d->username)
1864     GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1865   else
1866     GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1867   d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1868 #if !DEBUG_TESTING
1869                                      "-q",
1870 #endif
1871                                      d->cfgfile, arg, NULL);
1872   GNUNET_free (arg);
1873   if (NULL == d->proc)
1874   {
1875     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1876                 _
1877                 ("Could not start `%s' process to copy configuration file.\n"),
1878                 "scp");
1879     if (NULL != cb)
1880       cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1881     d->phase = SP_START_DONE;
1882     return;
1883   }
1884   d->update_cb = cb;
1885   d->update_cb_cls = cb_cls;
1886   d->task
1887       = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1888                                       &start_fsm, d);
1889 }
1890
1891
1892 /**
1893  * Data kept for each pair of peers that we try
1894  * to connect.
1895  */
1896 struct ConnectContext
1897 {
1898   /**
1899    * Testing handle to the first daemon.
1900    */
1901   struct GNUNET_TESTING_Daemon *d1;
1902
1903   /**
1904    * Handle to core of first daemon (to check connect)
1905    */
1906   struct GNUNET_CORE_Handle *d1core;
1907
1908   /**
1909    * Have we actually connected to the core of the first daemon yet?
1910    */
1911   int d1core_ready;
1912
1913   /**
1914    * Testing handle to the second daemon.
1915    */
1916   struct GNUNET_TESTING_Daemon *d2;
1917
1918   /**
1919    * Handler for the request to core to connect to this peer.
1920    */
1921   struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1922
1923   /**
1924    * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1925    */
1926   struct GNUNET_TRANSPORT_Handle *d1th;
1927
1928   /**
1929    * Function to call once we are done (or have timed out).
1930    */
1931   GNUNET_TESTING_NotifyConnection cb;
1932
1933   /**
1934    * Closure for "nb".
1935    */
1936   void *cb_cls;
1937
1938   /**
1939    * The relative timeout from whence this connect attempt was
1940    * started.  Allows for reconnect attempts.
1941    */
1942   struct GNUNET_TIME_Relative relative_timeout;
1943
1944   /**
1945    * Maximum number of connect attempts, will retry connection
1946    * this number of times on failures.
1947    */
1948   unsigned int connect_attempts;
1949
1950   /**
1951    * Hello timeout task
1952    */
1953   GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1954
1955   /**
1956    * Connect timeout task
1957    */
1958   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1959
1960   /**
1961    * When should this operation be complete (or we must trigger
1962    * a timeout).
1963    */
1964   struct GNUNET_TIME_Relative timeout_hello;
1965
1966   /**
1967    * Was the connection attempt successful?
1968    */
1969   int connected;
1970
1971   /**
1972    * When connecting, do we need to send the HELLO?
1973    */
1974   int send_hello;
1975
1976   /**
1977    * The distance between the two connected peers
1978    */
1979   uint32_t distance;
1980 };
1981
1982
1983 /** Forward declaration **/
1984 static void
1985 reattempt_daemons_connect (void *cls,
1986                            const struct GNUNET_SCHEDULER_TaskContext *tc);
1987
1988
1989 /**
1990  * Notify callback about success or failure of the attempt
1991  * to connect the two peers
1992  *
1993  * @param cls our "struct ConnectContext" (freed)
1994  * @param tc reason tells us if we succeeded or failed
1995  */
1996 static void
1997 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1998 {
1999   struct ConnectContext *ctx = cls;
2000
2001   ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2002   if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
2003   {
2004     GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
2005     ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2006   }
2007
2008   if (ctx->connect_request_handle != NULL)
2009   {
2010     GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
2011     ctx->connect_request_handle = NULL;
2012   }
2013
2014   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2015   {
2016     if (ctx->d1th != NULL)
2017       GNUNET_TRANSPORT_disconnect (ctx->d1th);
2018     ctx->d1th = NULL;
2019     if (ctx->d1core != NULL)
2020       GNUNET_CORE_disconnect (ctx->d1core);
2021 #if CONNECT_CORE2
2022     if (ctx->d2core != NULL)
2023       GNUNET_CORE_disconnect (ctx->d2core);
2024     ctx->d2core = NULL;
2025 #endif
2026     ctx->d1core = NULL;
2027     GNUNET_free (ctx);
2028     return;
2029   }
2030
2031   if (ctx->d1th != NULL)
2032     GNUNET_TRANSPORT_disconnect (ctx->d1th);
2033   ctx->d1th = NULL;
2034   if (ctx->d1core != NULL)
2035     GNUNET_CORE_disconnect (ctx->d1core);
2036   ctx->d1core = NULL;
2037
2038   if (ctx->connected == GNUNET_YES)
2039   {
2040     if (ctx->cb != NULL)
2041     {
2042       ctx->cb (ctx->cb_cls,
2043                &ctx->d1->id,
2044                &ctx->d2->id,
2045                ctx->distance,
2046                ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
2047     }
2048   }
2049   else if (ctx->connect_attempts > 0)
2050   {
2051     ctx->d1core_ready = GNUNET_NO;
2052 #if CONNECT_CORE2
2053     if (ctx->d2core != NULL)
2054     {
2055       GNUNET_CORE_disconnect (ctx->d2core);
2056       ctx->d2core = NULL;
2057     }
2058 #endif
2059     GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
2060     return;
2061   }
2062   else
2063   {
2064     if (ctx->cb != NULL)
2065     {
2066       ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2067                ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
2068     }
2069   }
2070
2071   GNUNET_free (ctx);
2072 }
2073
2074
2075 /**
2076  * Success, connection is up.  Signal client our success.
2077  *
2078  * @param cls our "struct ConnectContext"
2079  * @param peer identity of the peer that has connected
2080  * @param atsi performance information
2081  *
2082  */
2083 static void
2084 connect_notify (void *cls,
2085                 const struct GNUNET_PeerIdentity *peer,
2086                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2087 {
2088   struct ConnectContext *ctx = cls;
2089
2090 #if DEBUG_TESTING
2091   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2092               "Connected peer %s to peer %s\n",
2093               ctx->d1->shortname, GNUNET_i2s (peer));
2094 #endif
2095
2096   if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
2097   {
2098
2099     ctx->connected = GNUNET_YES;
2100     ctx->distance = 0;          /* FIXME: distance */
2101     if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
2102     {
2103       GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
2104       ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2105     }
2106     GNUNET_SCHEDULER_cancel (ctx->timeout_task);
2107     ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result, ctx);
2108   }
2109 }
2110
2111 #if CONNECT_CORE2
2112 /**
2113  * Success, connection is up.  Signal client our success.
2114  *
2115  * @param cls our "struct ConnectContext"
2116  * @param peer identity of the peer that has connected
2117  * @param atsi performance information
2118  *
2119  */
2120 static void
2121 connect_notify_core2 (void *cls,
2122                       const struct GNUNET_PeerIdentity *peer,
2123                       const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2124 {
2125   struct ConnectContext *ctx = cls;
2126
2127   if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
2128   {
2129     ctx->connected = GNUNET_YES;
2130     ctx->distance = 0;          /* FIXME: distance */
2131     GNUNET_SCHEDULER_cancel (ctx->timeout_task);
2132     ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result, ctx);
2133   }
2134
2135 }
2136 #endif
2137
2138 /**
2139  * Task called once a core connect request has been transmitted.
2140  *
2141  * @param cls struct ConnectContext
2142  * @param success was the request successful?
2143  */
2144 void
2145 core_connect_request_cont (void *cls, int success)
2146 {
2147   struct ConnectContext *ctx = cls;
2148
2149   ctx->connect_request_handle = NULL;
2150 }
2151
2152 static void
2153 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2154 {
2155   struct ConnectContext *ctx = cls;
2156   struct GNUNET_MessageHeader *hello;
2157
2158   ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2159   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2160     return;
2161   if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
2162       && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello))
2163       && (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
2164   {
2165     hello = GNUNET_HELLO_get_header (ctx->d2->hello);
2166     GNUNET_assert (hello != NULL);
2167 #if DEBUG_TESTING
2168     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
2169                 ctx->d2->shortname, ctx->d1->shortname);
2170 #endif
2171     GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
2172     GNUNET_assert (ctx->d1core != NULL);
2173     ctx->connect_request_handle =
2174         GNUNET_CORE_peer_request_connect (ctx->d1core,
2175                                           &ctx->d2->id,
2176                                           &core_connect_request_cont, ctx);
2177
2178 #if DEBUG_TESTING
2179     GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2180                 "Sending connect request to CORE of %s for peer %s\n",
2181                 GNUNET_i2s (&ctx->d1->id),
2182                 GNUNET_h2s (&ctx->d2->id.hashPubKey));
2183 #endif
2184     ctx->timeout_hello =
2185         GNUNET_TIME_relative_add (ctx->timeout_hello,
2186                                   GNUNET_TIME_relative_multiply
2187                                   (GNUNET_TIME_UNIT_MILLISECONDS, 500));
2188   }
2189   ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello,
2190                                                        &send_hello, ctx);
2191 }
2192
2193 /**
2194  * Notify of a successful connection to the core service.
2195  *
2196  * @param cls a ConnectContext
2197  * @param server handle to the core service
2198  * @param my_identity the peer identity of this peer
2199  * @param publicKey the public key of the peer
2200  */
2201 void
2202 core_init_notify (void *cls,
2203                   struct GNUNET_CORE_Handle *server,
2204                   const struct GNUNET_PeerIdentity *my_identity,
2205                   const struct
2206                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
2207 {
2208   struct ConnectContext *connect_ctx = cls;
2209
2210   connect_ctx->d1core_ready = GNUNET_YES;
2211
2212   if (connect_ctx->send_hello == GNUNET_NO)
2213   {
2214     connect_ctx->connect_request_handle =
2215         GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
2216                                           &connect_ctx->d2->id,
2217                                           &core_connect_request_cont,
2218                                           connect_ctx);
2219     GNUNET_assert (connect_ctx->connect_request_handle != NULL);
2220 #if DEBUG_TESTING
2221     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2222                 "Sending connect request to CORE of %s for peer %s\n",
2223                 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
2224 #endif
2225   }
2226
2227 }
2228
2229
2230 static void
2231 reattempt_daemons_connect (void *cls,
2232                            const struct GNUNET_SCHEDULER_TaskContext *tc)
2233 {
2234   struct ConnectContext *ctx = cls;
2235
2236   if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2237   {
2238     GNUNET_free (ctx);
2239     return;
2240   }
2241 #if DEBUG_TESTING_RECONNECT
2242   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2243               "re-attempting connect of peer %s to peer %s\n",
2244               ctx->d1->shortname, ctx->d2->shortname);
2245 #endif
2246   ctx->connect_attempts--;
2247   GNUNET_assert (ctx->d1core == NULL);
2248   ctx->d1core_ready = GNUNET_NO;
2249   ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
2250                                      ctx,
2251                                      &core_init_notify,
2252                                      &connect_notify, NULL, NULL,
2253                                      NULL, GNUNET_NO,
2254                                      NULL, GNUNET_NO, no_handlers);
2255   if (ctx->d1core == NULL)
2256   {
2257     if (NULL != ctx->cb)
2258       ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2259                ctx->d2->cfg, ctx->d1, ctx->d2,
2260                _("Failed to connect to core service of first peer!\n"));
2261     GNUNET_free (ctx);
2262     return;
2263   }
2264
2265   /* Don't know reason for initial connect failure, update the HELLO for the second peer */
2266   if (NULL != ctx->d2->hello)
2267   {
2268     GNUNET_free (ctx->d2->hello);
2269     ctx->d2->hello = NULL;
2270     if (NULL != ctx->d2->th)
2271     {
2272       GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->th, &process_hello, ctx->d2);
2273       GNUNET_TRANSPORT_disconnect (ctx->d2->th);
2274     }
2275     ctx->d2->th =
2276         GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2277                                   NULL);
2278     GNUNET_assert (ctx->d2->th != NULL);
2279     GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2280   }
2281
2282   if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
2283   {
2284     ctx->d2->th =
2285         GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2286                                   NULL);
2287     if (ctx->d2->th == NULL)
2288     {
2289       GNUNET_CORE_disconnect (ctx->d1core);
2290       GNUNET_free (ctx);
2291       if (NULL != ctx->cb)
2292         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2293                  ctx->d2->cfg, ctx->d1, ctx->d2,
2294                  _("Failed to connect to transport service!\n"));
2295       return;
2296     }
2297     GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2298   }
2299
2300   if (ctx->send_hello == GNUNET_YES)
2301   {
2302     ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
2303                                           &ctx->d1->id,
2304                                           ctx->d1, NULL, NULL, NULL);
2305     if (ctx->d1th == NULL)
2306     {
2307       GNUNET_CORE_disconnect (ctx->d1core);
2308       GNUNET_free (ctx);
2309       if (NULL != ctx->cb)
2310         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2311                  ctx->d2->cfg, ctx->d1, ctx->d2,
2312                  _("Failed to connect to transport service!\n"));
2313       return;
2314     }
2315     ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2316   }
2317   else
2318   {
2319     ctx->connect_request_handle =
2320         GNUNET_CORE_peer_request_connect (ctx->d1core,
2321                                           &ctx->d2->id,
2322                                           &core_connect_request_cont, ctx);
2323   }
2324   ctx->timeout_task =
2325       GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2326                                     &notify_connect_result, ctx);
2327 }
2328
2329 /**
2330  * Iterator for currently known peers, to ensure
2331  * that we don't try to send duplicate connect
2332  * requests to core.
2333  *
2334  * @param cls our "struct ConnectContext"
2335  * @param peer identity of the peer that has connected,
2336  *        NULL when iteration has finished
2337  * @param atsi performance information
2338  *
2339  */
2340 static void
2341 core_initial_iteration (void *cls,
2342                         const struct GNUNET_PeerIdentity *peer,
2343                         const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2344 {
2345   struct ConnectContext *ctx = cls;
2346
2347   if ((peer != NULL) &&
2348       (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2349   {
2350     ctx->connected = GNUNET_YES;
2351     ctx->distance = 0;          /* FIXME: distance */
2352     return;
2353   }
2354   else if (peer == NULL)        /* End of iteration over peers */
2355   {
2356     if (ctx->connected == GNUNET_YES)
2357     {
2358       ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result,
2359                                                     ctx);
2360       return;
2361     }
2362
2363     /* Peer not already connected, need to schedule connect request! */
2364     if (ctx->d1core == NULL)
2365     {
2366 #if DEBUG_TESTING
2367       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2368                   "Peers are NOT connected, connecting to core!\n");
2369 #endif
2370       ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
2371                                          ctx,
2372                                          &core_init_notify,
2373                                          &connect_notify, NULL, NULL,
2374                                          NULL, GNUNET_NO,
2375                                          NULL, GNUNET_NO, no_handlers);
2376     }
2377
2378     if (ctx->d1core == NULL)
2379     {
2380       GNUNET_free (ctx);
2381       if (NULL != ctx->cb)
2382         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2383                  ctx->d2->cfg, ctx->d1, ctx->d2,
2384                  _("Failed to connect to core service of first peer!\n"));
2385       return;
2386     }
2387
2388     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 */
2389     {
2390       ctx->d2->th =
2391           GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL,
2392                                     NULL, NULL);
2393       if (ctx->d2->th == NULL)
2394       {
2395         GNUNET_CORE_disconnect (ctx->d1core);
2396         GNUNET_free (ctx);
2397         if (NULL != ctx->cb)
2398           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2399                    ctx->d2->cfg, ctx->d1, ctx->d2,
2400                    _("Failed to connect to transport service!\n"));
2401         return;
2402       }
2403       GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2404     }
2405
2406     if (ctx->send_hello == GNUNET_YES)
2407     {
2408       ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
2409                                             &ctx->d1->id, ctx->d1, NULL, NULL,
2410                                             NULL);
2411       if (ctx->d1th == NULL)
2412       {
2413         GNUNET_CORE_disconnect (ctx->d1core);
2414         GNUNET_free (ctx);
2415         if (NULL != ctx->cb)
2416           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2417                    ctx->d2->cfg, ctx->d1, ctx->d2,
2418                    _("Failed to connect to transport service!\n"));
2419         return;
2420       }
2421       ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2422     }
2423
2424     ctx->timeout_task =
2425         GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2426                                       &notify_connect_result, ctx);
2427   }
2428 }
2429
2430
2431 /**
2432  * Establish a connection between two GNUnet daemons.
2433  *
2434  * @param d1 handle for the first daemon
2435  * @param d2 handle for the second daemon
2436  * @param timeout how long is the connection attempt
2437  *        allowed to take?
2438  * @param max_connect_attempts how many times should we try to reconnect
2439  *        (within timeout)
2440  * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2441  *                   the HELLO has already been exchanged
2442  * @param cb function to call at the end
2443  * @param cb_cls closure for cb
2444  */
2445 void
2446 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2447                                 struct GNUNET_TESTING_Daemon *d2,
2448                                 struct GNUNET_TIME_Relative timeout,
2449                                 unsigned int max_connect_attempts,
2450                                 int send_hello,
2451                                 GNUNET_TESTING_NotifyConnection cb,
2452                                 void *cb_cls)
2453 {
2454   struct ConnectContext *ctx;
2455
2456   if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2457   {
2458     if (NULL != cb)
2459       cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2460           _("Peers are not fully running yet, can not connect!\n"));
2461     GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2462     return;
2463   }
2464
2465   ctx = GNUNET_malloc (sizeof (struct ConnectContext));
2466   ctx->d1 = d1;
2467   ctx->d2 = d2;
2468   ctx->timeout_hello =
2469       GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2470   ctx->relative_timeout =
2471       GNUNET_TIME_relative_divide (timeout, max_connect_attempts);
2472   ctx->cb = cb;
2473   ctx->cb_cls = cb_cls;
2474   ctx->connect_attempts = max_connect_attempts;
2475   ctx->connected = GNUNET_NO;
2476   ctx->send_hello = send_hello;
2477 #if DEBUG_TESTING
2478   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479               "Asked to connect peer %s to peer %s\n",
2480               d1->shortname, d2->shortname);
2481 #endif
2482
2483   /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2484   GNUNET_assert (GNUNET_OK ==
2485                  GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id,
2486                                                 &core_initial_iteration, ctx));
2487   /*GNUNET_assert(GNUNET_OK == GNUNET_CORE_iterate_peers (ctx->d1->cfg, &core_initial_iteration, ctx)); */
2488 }
2489
2490 /* end of testing.c */