Nothing ever is right the first time.
[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 #define DEBUG_TESTING_RECONNECT GNUNET_YES
42
43 /**
44  * How long do we wait after starting gnunet-service-arm
45  * for the core service to be alive?
46  */
47 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
48
49 /**
50  * How many times are we willing to try to wait for "scp" or
51  * "gnunet-service-arm" to complete (waitpid) before giving up?
52  */
53 #define MAX_EXEC_WAIT_RUNS 250
54
55 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
56
57 /**
58  * Receive the HELLO from one peer, give it to the other
59  * and ask them to connect.
60  *
61  * @param cls "struct ConnectContext"
62  * @param message HELLO message of peer
63  */
64 static void
65 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
66 {
67   struct GNUNET_TESTING_Daemon *daemon = cls;
68   GNUNET_TESTING_NotifyDaemonRunning cb;
69
70   int msize;
71   if (daemon == NULL)
72     return;
73
74   GNUNET_assert (daemon->phase == SP_GET_HELLO);
75
76   cb = daemon->cb;
77   daemon->cb = NULL;
78   if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
79     GNUNET_SCHEDULER_cancel(daemon->task);
80
81   if (daemon->server != NULL)
82     {
83 #if DEBUG_TESTING
84       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85                   "Received `%s' from transport service of `%4s', disconnecting core!\n",
86                   "HELLO", GNUNET_i2s (&daemon->id));
87 #endif
88       GNUNET_CORE_disconnect (daemon->server);
89       daemon->server = NULL;
90     }
91
92   GNUNET_assert (message != NULL);
93   msize = ntohs (message->size);
94   if (msize < 1)
95     {
96       return;
97     }
98   if (daemon->th != NULL)
99     {
100       GNUNET_TRANSPORT_get_hello_cancel (daemon->th, &process_hello, daemon);
101     }
102 #if DEBUG_TESTING
103   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
104               "Received `%s' from transport service of `%4s'\n",
105               "HELLO", GNUNET_i2s (&daemon->id));
106 #endif
107
108   GNUNET_free_non_null (daemon->hello);
109   daemon->hello = GNUNET_malloc (msize);
110   memcpy (daemon->hello, message, msize);
111
112   if (daemon->th != NULL)
113     {
114       GNUNET_TRANSPORT_disconnect (daemon->th);
115       daemon->th = NULL;
116     }
117   daemon->phase = SP_START_DONE;
118
119   if (NULL != cb) /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
120     cb (daemon->cb_cls, &daemon->id, daemon->cfg, daemon, NULL);
121 }
122
123 static void
124 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
125
126 /**
127  * Function called after GNUNET_CORE_connect has succeeded
128  * (or failed for good).  Note that the private key of the
129  * peer is intentionally not exposed here; if you need it,
130  * your process should try to read the private key file
131  * directly (which should work if you are authorized...).
132  *
133  * @param cls closure
134  * @param server handle to the server, NULL if we failed
135  * @param my_identity ID of this peer, NULL if we failed
136  * @param publicKey public key of this peer, NULL if we failed
137  */
138 static void
139 testing_init (void *cls,
140               struct GNUNET_CORE_Handle *server,
141               const struct GNUNET_PeerIdentity *my_identity,
142               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
143 {
144   struct GNUNET_TESTING_Daemon *d = cls;
145
146   GNUNET_assert (d->phase == SP_START_CORE);
147   d->phase = SP_GET_HELLO;
148
149   if (server == NULL)
150     {
151       d->server = NULL;
152       if (GNUNET_YES == d->dead)
153         GNUNET_TESTING_daemon_stop (d,
154                                     GNUNET_TIME_absolute_get_remaining
155                                     (d->max_timeout), d->dead_cb,
156                                     d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
157       else if (NULL != d->cb)
158         d->cb (d->cb_cls, NULL, d->cfg, d,
159             _("Failed to connect to core service\n"));
160       return;
161     }
162 #if DEBUG_TESTING
163   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164               "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
165 #endif
166   d->id = *my_identity;
167   d->shortname = strdup (GNUNET_i2s (my_identity));
168   d->server = server;
169   d->running = GNUNET_YES;
170
171   if (GNUNET_NO == d->running)
172     {
173 #if DEBUG_TESTING
174       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175                   "Peer is dead (d->running == GNUNET_NO)\n");
176 #endif
177       return;
178     }
179 #if DEBUG_TESTING
180   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181               "Successfully started peer `%4s', connecting to transport service.\n",
182               GNUNET_i2s (my_identity));
183 #endif
184
185   d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
186   if (d->th == NULL)
187     {
188       if (GNUNET_YES == d->dead)
189         GNUNET_TESTING_daemon_stop (d,
190                                     GNUNET_TIME_absolute_get_remaining
191                                     (d->max_timeout), d->dead_cb,
192                                     d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
193       else if (NULL != d->cb)
194         d->cb (d->cb_cls, &d->id, d->cfg, d,
195                _("Failed to connect to transport service!\n"));
196       return;
197     }
198 #if DEBUG_TESTING
199   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200               "Connected to transport service `%s', getting HELLO\n",
201               GNUNET_i2s (my_identity));
202 #endif
203
204   GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
205   /* wait some more */
206   if (d->task != GNUNET_SCHEDULER_NO_TASK)
207     GNUNET_SCHEDULER_cancel(d->task);
208   d->task
209     = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
210                                     &start_fsm, d);
211 }
212
213
214 /**
215  * Finite-state machine for starting GNUnet.
216  *
217  * @param cls our "struct GNUNET_TESTING_Daemon"
218  * @param tc unused
219  */
220 static void
221 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
222 {
223   struct GNUNET_TESTING_Daemon *d = cls;
224   GNUNET_TESTING_NotifyDaemonRunning cb;
225   enum GNUNET_OS_ProcessStatusType type;
226   unsigned long code;
227   char *dst;
228   int bytes_read;
229
230 #if DEBUG_TESTING
231   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232               "Peer FSM is in phase %u.\n", d->phase);
233 #endif
234
235   d->task = GNUNET_SCHEDULER_NO_TASK;
236   switch (d->phase)
237     {
238     case SP_COPYING:
239       /* confirm copying complete */
240       if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
241         {
242           if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
243               0)
244             {
245               cb = d->cb;
246               d->cb = NULL;
247               if (NULL != cb)
248                 cb (d->cb_cls,
249                     NULL,
250                     d->cfg, d,
251                     _
252                     ("`scp' does not seem to terminate (timeout copying config).\n"));
253               return;
254             }
255           /* wait some more */
256           d->task
257             = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
258                                             &start_fsm, d);
259           return;
260         }
261       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
262         {
263           cb = d->cb;
264           d->cb = NULL;
265           if (NULL != cb)
266             cb (d->cb_cls,
267                 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
268           return;
269         }
270 #if DEBUG_TESTING
271       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272                   "Successfully copied configuration file.\n");
273 #endif
274       d->phase = SP_COPIED;
275       /* fall-through */
276     case SP_COPIED:
277       /* Start create hostkey process if we don't already know the peer identity!*/
278       if (GNUNET_NO == d->have_hostkey)
279         {
280           d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
281           if (d->pipe_stdout == NULL)
282             {
283               cb = d->cb;
284               d->cb = NULL;
285               if (NULL != cb)
286                 cb (d->cb_cls,
287                     NULL,
288                     d->cfg,
289                     d,
290                     (NULL == d->hostname)
291                     ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
292                     : _("Failed to create pipe for `ssh' process.\n"));
293               return;
294             }
295           if (NULL == d->hostname)
296             {
297     #if DEBUG_TESTING
298               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299                           "Starting `%s', with command `%s %s %s %s'.\n",
300                           "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
301                           "-sq");
302     #endif
303               d->proc =
304                 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
305                                          "gnunet-peerinfo", "-c", d->cfgfile,
306                                          "-sq", NULL);
307               GNUNET_DISK_pipe_close_end (d->pipe_stdout,
308                                           GNUNET_DISK_PIPE_END_WRITE);
309             }
310           else
311             {
312               if (d->username != NULL)
313                 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
314               else
315                 dst = GNUNET_strdup (d->hostname);
316
317     #if DEBUG_TESTING
318               GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319                           "Starting `%s', with command `%s %s %s %s %s %s'.\n",
320                           "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
321                           d->cfgfile, "-sq");
322     #endif
323               if (d->ssh_port_str == NULL)
324                 {
325                   d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
326                                                      "ssh",
327     #if !DEBUG_TESTING
328                                                      "-q",
329     #endif
330                                                      dst,
331                                                      "gnunet-peerinfo",
332                                                      "-c", d->cfgfile, "-sq",
333                                                      NULL);
334                 }
335               else
336                 {
337                   d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
338                                                      "ssh", "-p", d->ssh_port_str,
339     #if !DEBUG_TESTING
340                                                      "-q",
341     #endif
342                                                      dst,
343                                                      "gnunet-peerinfo",
344                                                      "-c", d->cfgfile, "-sq",
345                                                      NULL);
346                 }
347               GNUNET_DISK_pipe_close_end (d->pipe_stdout,
348                                           GNUNET_DISK_PIPE_END_WRITE);
349               GNUNET_free (dst);
350             }
351           if (NULL == d->proc)
352             {
353               GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
354                           _("Could not start `%s' process to create hostkey.\n"),
355                           (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
356               cb = d->cb;
357               d->cb = NULL;
358               if (NULL != cb)
359                 cb (d->cb_cls,
360                     NULL,
361                     d->cfg,
362                     d,
363                     (NULL == d->hostname)
364                     ? _("Failed to start `gnunet-peerinfo' process.\n")
365                     : _("Failed to start `ssh' process.\n"));
366               GNUNET_DISK_pipe_close (d->pipe_stdout);
367               return;
368             }
369     #if DEBUG_TESTING
370           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371                       "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
372     #endif
373           d->phase = SP_HOSTKEY_CREATE;
374           d->task
375             =
376             GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
377                                             (d->max_timeout),
378                                             GNUNET_DISK_pipe_handle
379                                             (d->pipe_stdout,
380                                              GNUNET_DISK_PIPE_END_READ),
381                                             &start_fsm, d);
382         }
383       else /* Already have a hostkey! */
384         {
385           d->phase = SP_HOSTKEY_CREATED;
386           /* wait some more */
387           d->task
388             = GNUNET_SCHEDULER_add_now (&start_fsm, d);
389         }
390       break;
391     case SP_HOSTKEY_CREATE:
392       bytes_read =
393         GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
394                                (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
395                                &d->hostkeybuf[d->hostkeybufpos],
396                                sizeof (d->hostkeybuf) - d->hostkeybufpos);
397       if (bytes_read > 0)
398         d->hostkeybufpos += bytes_read;
399
400       if ((d->hostkeybufpos < 104) && (bytes_read > 0))
401         {
402           /* keep reading */
403           d->task
404             =
405             GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
406                                             (d->max_timeout),
407                                             GNUNET_DISK_pipe_handle
408                                             (d->pipe_stdout,
409                                              GNUNET_DISK_PIPE_END_READ),
410                                             &start_fsm, d);
411           return;
412         }
413       d->hostkeybuf[103] = '\0';
414
415       if ((bytes_read < 0) ||
416           (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
417                                                         &d->id.hashPubKey)))
418         {
419           /* error */
420           if (bytes_read < 0)
421             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
422                         _("Error reading from gnunet-peerinfo: %s\n"),
423                         STRERROR (errno));
424           else
425             GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
426                         _("Malformed output from gnunet-peerinfo!\n"));
427           cb = d->cb;
428           d->cb = NULL;
429           GNUNET_DISK_pipe_close (d->pipe_stdout);
430           d->pipe_stdout = NULL;
431           (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
432           GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
433           GNUNET_OS_process_close (d->proc);
434           d->proc = NULL;
435           if (NULL != cb)
436             cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
437           return;
438         }
439       GNUNET_DISK_pipe_close (d->pipe_stdout);
440       d->pipe_stdout = NULL;
441       (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
442       GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
443       GNUNET_OS_process_close (d->proc);
444       d->proc = NULL;
445       d->have_hostkey = GNUNET_YES;
446 #if DEBUG_TESTING
447       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
448 #endif
449       /* Fall through */
450     case SP_HOSTKEY_CREATED:
451       GNUNET_assert(d->have_hostkey == GNUNET_YES);
452       if (d->hostkey_callback != NULL)
453         {
454           d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
455           d->hostkey_callback = NULL;
456           d->phase = SP_HOSTKEY_CREATED;
457         }
458       else
459         {
460           d->phase = SP_TOPOLOGY_SETUP;
461         }
462       /* wait for topology finished */
463       if ((GNUNET_YES == d->dead)
464           || (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
465               0))
466         {
467           cb = d->cb;
468           d->cb = NULL;
469           if (NULL != cb)
470             cb (d->cb_cls,
471                 NULL,
472                 d->cfg, d, _("`Failed while waiting for topology setup!\n"));
473           return;
474         }
475
476       d->task
477         = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
478                                         &start_fsm, d);
479       break;
480     case SP_TOPOLOGY_SETUP:
481       /* start GNUnet on remote host */
482       if (NULL == d->hostname)
483         {
484 #if DEBUG_TESTING
485           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
486                       "Starting `%s', with command `%s %s %s %s %s %s'.\n",
487                       "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
488                       "-L", "DEBUG", "-s");
489 #endif
490           d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
491                                              "gnunet-arm", "-c", d->cfgfile,
492 #if DEBUG_TESTING
493                                              "-L", "DEBUG",
494 #endif
495                                              "-s", "-q", NULL);
496         }
497       else
498         {
499           if (d->username != NULL)
500             GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
501           else
502             dst = GNUNET_strdup (d->hostname);
503
504 #if DEBUG_TESTING
505           GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
506                       "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
507                       "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
508                       d->cfgfile, "-L", "DEBUG", "-s", "-q");
509 #endif
510           if (d->ssh_port_str == NULL)
511             {
512               d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
513 #if !DEBUG_TESTING
514                                                  "-q",
515 #endif
516                                                  dst, "gnunet-arm",
517 #if DEBUG_TESTING
518                                                  "-L", "DEBUG",
519 #endif
520                                                  "-c", d->cfgfile, "-s", "-q",
521                                                  NULL);
522             }
523           else
524             {
525
526               d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
527                                                  "ssh", "-p", d->ssh_port_str,
528 #if !DEBUG_TESTING
529                                                  "-q",
530 #endif
531                                                  dst, "gnunet-arm",
532 #if DEBUG_TESTING
533                                                  "-L", "DEBUG",
534 #endif
535                                                  "-c", d->cfgfile, "-s", "-q",
536                                                  NULL);
537             }
538           GNUNET_free (dst);
539         }
540       if (NULL == d->proc)
541         {
542           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
543                       _("Could not start `%s' process to start GNUnet.\n"),
544                       (NULL == d->hostname) ? "gnunet-arm" : "ssh");
545           cb = d->cb;
546           d->cb = NULL;
547           if (NULL != cb)
548             cb (d->cb_cls,
549                 NULL,
550                 d->cfg,
551                 d,
552                 (NULL == d->hostname)
553                 ? _("Failed to start `gnunet-arm' process.\n")
554                 : _("Failed to start `ssh' process.\n"));
555           return;
556         }
557 #if DEBUG_TESTING
558       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
559                   "Started `%s', waiting for `%s' to be up.\n",
560                   "gnunet-arm", "gnunet-service-core");
561 #endif
562       d->phase = SP_START_ARMING;
563       d->task
564         = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
565                                         &start_fsm, d);
566       break;
567     case SP_START_ARMING:
568       if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
569         {
570           if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
571               0)
572             {
573               cb = d->cb;
574               d->cb = NULL;
575               if (NULL != cb)
576                 cb (d->cb_cls,
577                     NULL,
578                     d->cfg,
579                     d,
580                     (NULL == d->hostname)
581                     ? _("`gnunet-arm' does not seem to terminate.\n")
582                     : _("`ssh' does not seem to terminate.\n"));
583               GNUNET_CONFIGURATION_destroy (d->cfg);
584               GNUNET_free (d->cfgfile);
585               GNUNET_free_non_null (d->hostname);
586               GNUNET_free_non_null (d->username);
587               GNUNET_free(d->proc);
588               GNUNET_free(d);
589               return;
590             }
591           /* wait some more */
592           d->task
593             = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
594                                             &start_fsm, d);
595           return;
596         }
597 #if DEBUG_TESTING
598       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
599                   "Successfully started `%s'.\n", "gnunet-arm");
600 #endif
601       GNUNET_free(d->proc);
602       d->phase = SP_START_CORE;
603 #if DEBUG_TESTING
604       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
605                   "Calling CORE_connect\n");
606 #endif
607       /* Fall through */
608     case SP_START_CORE:
609       if (d->server != NULL)
610         GNUNET_CORE_disconnect(d->server);
611
612       if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
613           0)
614         {
615           cb = d->cb;
616           d->cb = NULL;
617           if (NULL != cb)
618             cb (d->cb_cls,
619                 NULL,
620                 d->cfg,
621                 d,
622                 _("Unable to connect to CORE service for peer!\n"));
623           GNUNET_CONFIGURATION_destroy (d->cfg);
624           GNUNET_free (d->cfgfile);
625           GNUNET_free_non_null (d->hostname);
626           GNUNET_free_non_null (d->username);
627           GNUNET_free (d);
628           return;
629         }
630       d->server = GNUNET_CORE_connect (d->cfg, 1,
631                                        d,
632                                        &testing_init,
633                                        NULL, NULL, NULL,
634                                        NULL, GNUNET_NO,
635                                        NULL, GNUNET_NO, no_handlers);
636       d->task
637         = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
638                                         &start_fsm, d);
639       break;
640     case SP_GET_HELLO:
641       if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
642           0)
643         {
644           if (d->server != NULL)
645             GNUNET_CORE_disconnect(d->server);
646           if (d->th != NULL)
647             GNUNET_TRANSPORT_disconnect(d->th);
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                 _("Unable to get HELLO for peer!\n"));
656           GNUNET_CONFIGURATION_destroy (d->cfg);
657           GNUNET_free (d->cfgfile);
658           GNUNET_free_non_null (d->hostname);
659           GNUNET_free_non_null (d->username);
660           GNUNET_free (d);
661           return;
662         }
663       if (d->hello != NULL)
664         return;
665       GNUNET_assert(d->task == GNUNET_SCHEDULER_NO_TASK);
666       d->task
667         = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
668                                         &start_fsm, d);
669       break;
670     case SP_START_DONE:
671       GNUNET_break (0);
672       break;
673     case SP_SHUTDOWN_START:
674       /* confirm copying complete */
675       if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
676         {
677           if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
678               0)
679             {
680               if (NULL != d->dead_cb)
681                 d->dead_cb (d->dead_cb_cls,
682                             _
683                             ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
684               if (d->th != NULL)
685                 {
686                   GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello,
687                                                      d);
688                   GNUNET_TRANSPORT_disconnect (d->th);
689                   d->th = NULL;
690                 }
691               GNUNET_CONFIGURATION_destroy (d->cfg);
692               GNUNET_free (d->cfgfile);
693               GNUNET_free_non_null (d->hello);
694               GNUNET_free_non_null (d->hostname);
695               GNUNET_free_non_null (d->username);
696               GNUNET_free_non_null (d->shortname);
697               GNUNET_free_non_null (d->proc);
698               d->proc = NULL;
699               GNUNET_free (d);
700               return;
701             }
702           /* wait some more */
703           d->task
704             = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
705                                             &start_fsm, d);
706           return;
707         }
708       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
709         {
710           if (NULL != d->dead_cb)
711             d->dead_cb (d->dead_cb_cls,
712                         _
713                         ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
714           if (d->th != NULL)
715             {
716               GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
717               GNUNET_TRANSPORT_disconnect (d->th);
718               d->th = NULL;
719             }
720           if (d->server != NULL)
721             {
722               GNUNET_CORE_disconnect (d->server);
723               d->server = NULL;
724             }
725           GNUNET_CONFIGURATION_destroy (d->cfg);
726           GNUNET_free (d->cfgfile);
727           GNUNET_free_non_null (d->hello);
728           GNUNET_free_non_null (d->hostname);
729           GNUNET_free_non_null (d->username);
730           GNUNET_free_non_null (d->shortname);
731           GNUNET_free_non_null (d->proc);
732           d->proc = NULL;
733           GNUNET_free (d);
734           return;
735         }
736 #if DEBUG_TESTING
737       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
738 #endif
739       if (d->server != NULL)
740         {
741           GNUNET_CORE_disconnect (d->server);
742           d->server = NULL;
743         }
744
745       if (d->th != NULL)
746         {
747           GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
748           GNUNET_TRANSPORT_disconnect (d->th);
749           d->th = NULL;
750         }
751       /* state clean up and notifications */
752       if (d->churn == GNUNET_NO)
753         {
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         }
759
760       GNUNET_free_non_null (d->hello);
761       d->hello = NULL;
762       GNUNET_free_non_null (d->shortname);
763       GNUNET_free_non_null (d->proc);
764       d->proc = NULL;
765       d->shortname = NULL;
766       if (NULL != d->dead_cb)
767         d->dead_cb (d->dead_cb_cls, NULL);
768
769       if (d->churn == GNUNET_NO)
770         GNUNET_free (d);
771
772       break;
773     case SP_CONFIG_UPDATE:
774       /* confirm copying complete */
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)       /* FIXME: config update should take timeout parameter! */
778             {
779               cb = d->cb;
780               d->cb = NULL;
781               if (NULL != cb)
782                 cb (d->cb_cls,
783                     NULL,
784                     d->cfg, d, _("`scp' does not seem to terminate.\n"));
785               return;
786             }
787           /* wait some more */
788           d->task
789             = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
790                                             &start_fsm, d);
791           return;
792         }
793       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
794         {
795           if (NULL != d->update_cb)
796             d->update_cb (d->update_cb_cls,
797                           _("`scp' did not complete cleanly.\n"));
798           return;
799         }
800 #if DEBUG_TESTING
801       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802                   "Successfully copied configuration file.\n");
803 #endif
804       if (NULL != d->update_cb)
805         d->update_cb (d->update_cb_cls, NULL);
806       d->phase = SP_START_DONE;
807       break;
808     }
809 }
810
811 /**
812  * Continues GNUnet daemon startup when user wanted to be notified
813  * once a hostkey was generated (for creating friends files, blacklists,
814  * etc.).
815  *
816  * @param daemon the daemon to finish starting
817  */
818 void
819 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
820 {
821   GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
822   daemon->phase = SP_TOPOLOGY_SETUP;
823 }
824
825 /**
826  * Check whether the given daemon is running.
827  *
828  * @param daemon the daemon to check
829  *
830  * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
831  *         daemon is down, GNUNET_SYSERR on error.
832  */
833 int
834 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
835 {
836   if (daemon == NULL)
837     return GNUNET_SYSERR;
838
839   if (daemon->running == GNUNET_YES)
840     return GNUNET_YES;
841   return GNUNET_NO;
842 }
843
844
845 /**
846  * Start a peer that has previously been stopped using the daemon_stop
847  * call (and files weren't deleted and the allow restart flag)
848  *
849  * @param daemon the daemon to start (has been previously stopped)
850  * @param timeout how long to wait for restart
851  * @param cb the callback for notification when the peer is running
852  * @param cb_cls closure for the callback
853  */
854 void
855 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
856                                      struct GNUNET_TIME_Relative timeout,
857                                      GNUNET_TESTING_NotifyDaemonRunning cb,
858                                      void *cb_cls)
859 {
860   if (daemon->running == GNUNET_YES)
861     {
862       cb (cb_cls, &daemon->id, daemon->cfg, daemon,
863           "Daemon already running, can't restart!");
864       return;
865     }
866
867   daemon->cb = cb;
868   daemon->cb_cls = cb_cls;
869   daemon->phase = SP_TOPOLOGY_SETUP;
870   daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
871
872   GNUNET_SCHEDULER_add_continuation (&start_fsm,
873                                      daemon,
874                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
875 }
876
877 /**
878  * Starts a GNUnet daemon.  GNUnet must be installed on the target
879  * system and available in the PATH.  The machine must furthermore be
880  * reachable via "ssh" (unless the hostname is "NULL") without the
881  * need to enter a password.
882  *
883  * @param cfg configuration to use
884  * @param timeout how long to wait starting up peers
885  * @param hostname name of the machine where to run GNUnet
886  *        (use NULL for localhost).
887  * @param ssh_username ssh username to use when connecting to hostname
888  * @param sshport port to pass to ssh process when connecting to hostname
889  * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
890  * @param hostkey_callback function to call once the hostkey has been
891  *        generated for this peer, but it hasn't yet been started
892  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
893  * @param hostkey_cls closure for hostkey callback
894  * @param cb function to call once peer is up, or failed to start
895  * @param cb_cls closure for cb
896  * @return handle to the daemon (actual start will be completed asynchronously)
897  */
898 struct GNUNET_TESTING_Daemon *
899 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
900                              struct GNUNET_TIME_Relative timeout,
901                              const char *hostname,
902                              const char *ssh_username,
903                              uint16_t sshport,
904                              const char *hostkey,
905                              GNUNET_TESTING_NotifyHostkeyCreated
906                              hostkey_callback, void *hostkey_cls,
907                              GNUNET_TESTING_NotifyDaemonRunning cb,
908                              void *cb_cls)
909 {
910   struct GNUNET_TESTING_Daemon *ret;
911   char *arg;
912   char *username;
913   char *servicehome;
914   char *baseservicehome;
915   char *slash;
916   char *hostkeyfile;
917   char *temp_file_name;
918   struct GNUNET_DISK_FileHandle *fn;
919   struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
920   struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
921
922   ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
923   ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
924   if (sshport != 0)
925     {
926       GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
927     }
928   else
929     ret->ssh_port_str = NULL;
930
931   /* Find service home and base service home directories, create it if it doesn't exist */
932   GNUNET_assert(GNUNET_OK ==
933                 GNUNET_CONFIGURATION_get_value_string (cfg,
934                                                        "PATHS",
935                                                        "SERVICEHOME",
936                                                        &servicehome));
937
938   GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
939   GNUNET_asprintf(&temp_file_name, "%s/gnunet-testing-config", servicehome);
940   ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
941   GNUNET_free(temp_file_name);
942 #if DEBUG_TESTING
943   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944               "Setting up peer with configuration file `%s'.\n",
945               ret->cfgfile);
946 #endif
947   if (NULL == ret->cfgfile)
948     {
949       GNUNET_free_non_null (ret->ssh_port_str);
950       GNUNET_free_non_null (ret->hostname);
951       GNUNET_free (ret);
952       return NULL;
953     }
954   ret->hostkey_callback = hostkey_callback;
955   ret->hostkey_cls = hostkey_cls;
956   ret->cb = cb;
957   ret->cb_cls = cb_cls;
958   ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
959   ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
960   GNUNET_CONFIGURATION_set_value_string (ret->cfg,
961                                          "PATHS",
962                                          "DEFAULTCONFIG", ret->cfgfile);
963
964   if (hostkey != NULL) /* Get the peer identity from the hostkey */
965     {
966       private_key = GNUNET_CRYPTO_rsa_decode_key(hostkey, HOSTKEYFILESIZE);
967       GNUNET_assert(private_key != NULL);
968       GNUNET_CRYPTO_rsa_key_get_public (private_key,
969                                         &public_key);
970       GNUNET_CRYPTO_hash(&public_key, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &ret->id.hashPubKey);
971       ret->shortname = GNUNET_strdup(GNUNET_i2s(&ret->id));
972       ret->have_hostkey = GNUNET_YES;
973       GNUNET_free(private_key);
974     }
975
976   /* Write hostkey to file, if we were given one */
977   hostkeyfile = NULL;
978   if (hostkey != NULL)
979     {
980       GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome);
981       fn =
982       GNUNET_DISK_file_open (hostkeyfile,
983                              GNUNET_DISK_OPEN_READWRITE
984                              | GNUNET_DISK_OPEN_CREATE,
985                              GNUNET_DISK_PERM_USER_READ |
986                              GNUNET_DISK_PERM_USER_WRITE);
987       GNUNET_assert(fn != NULL);
988       GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE));
989       GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn));
990     }
991
992   /* write configuration to temporary file */
993   if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
994     {
995       if (0 != UNLINK (ret->cfgfile))
996         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
997                                   "unlink", ret->cfgfile);
998       GNUNET_CONFIGURATION_destroy (ret->cfg);
999       GNUNET_free_non_null (ret->hostname);
1000       GNUNET_free (ret->cfgfile);
1001       GNUNET_free (ret);
1002       return NULL;
1003     }
1004   if (ssh_username != NULL)
1005     username = GNUNET_strdup (ssh_username);
1006   if ((ssh_username == NULL) && (GNUNET_OK !=
1007                                  GNUNET_CONFIGURATION_get_value_string (cfg,
1008                                                                         "TESTING",
1009                                                                         "USERNAME",
1010                                                                         &username)))
1011     {
1012       if (NULL != getenv ("USER"))
1013         username = GNUNET_strdup (getenv ("USER"));
1014       else
1015         username = NULL;
1016     }
1017   ret->username = username;
1018
1019   /* copy directory to remote host */
1020   if (NULL != hostname)
1021     {
1022 #if DEBUG_TESTING
1023       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024                   "Copying configuration directory to host `%s'.\n", hostname);
1025 #endif
1026       baseservicehome = GNUNET_strdup(servicehome);
1027       /* Remove trailing /'s */
1028       while (baseservicehome[strlen(baseservicehome) - 1] == '/')
1029         baseservicehome[strlen(baseservicehome) - 1] = '\0';
1030       /* Find next directory /, jump one ahead */
1031       slash = strrchr(baseservicehome, '/');
1032       if (slash != NULL)
1033         *(++slash) = '\0';
1034
1035       ret->phase = SP_COPYING;
1036       if (NULL != username)
1037         GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1038       else
1039         GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1040
1041       if (ret->ssh_port_str == NULL)
1042         {
1043           ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1044 #if !DEBUG_TESTING
1045                                                "-q",
1046 #endif
1047                                                servicehome, arg, NULL);
1048 #if DEBUG_TESTING
1049           GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", servicehome, arg);
1050 #endif
1051         }
1052       else
1053         {
1054           ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
1055                                                "scp", "-r", "-P", ret->ssh_port_str,
1056 #if !DEBUG_TESTING
1057                                                "-q",
1058 #endif
1059                                                servicehome, arg, NULL);
1060         }
1061       GNUNET_free (arg);
1062       if (NULL == ret->proc)
1063         {
1064           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1065                       _
1066                       ("Could not start `%s' process to copy configuration directory.\n"),
1067                       "scp");
1068           if (0 != UNLINK (ret->cfgfile))
1069             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1070                                       "unlink", ret->cfgfile);
1071           GNUNET_CONFIGURATION_destroy (ret->cfg);
1072           GNUNET_free_non_null (ret->hostname);
1073           GNUNET_free_non_null (ret->username);
1074           GNUNET_free (ret->cfgfile);
1075           GNUNET_free (ret);
1076           if ((hostkey != NULL) && (0 != UNLINK(hostkeyfile)))
1077             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1078                                       "unlink", hostkeyfile);
1079           GNUNET_free_non_null(hostkeyfile);
1080           GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1081           GNUNET_free(servicehome);
1082           return NULL;
1083         }
1084
1085       ret->task
1086         = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1087                                         &start_fsm, ret);
1088       GNUNET_free_non_null(hostkeyfile);
1089       GNUNET_free(servicehome);
1090       return ret;
1091     }
1092 #if DEBUG_TESTING
1093   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094               "No need to copy configuration file since we are running locally.\n");
1095 #endif
1096   ret->phase = SP_COPIED;
1097   GNUNET_SCHEDULER_add_continuation (&start_fsm,
1098                                      ret,
1099                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1100   GNUNET_free_non_null(hostkeyfile);
1101   GNUNET_free(servicehome);
1102   return ret;
1103 }
1104
1105
1106 /**
1107  * Restart (stop and start) a GNUnet daemon.
1108  *
1109  * @param d the daemon that should be restarted
1110  * @param cb function called once the daemon is (re)started
1111  * @param cb_cls closure for cb
1112  */
1113 void
1114 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1115                                GNUNET_TESTING_NotifyDaemonRunning cb,
1116                                void *cb_cls)
1117 {
1118   char *arg;
1119   char *del_arg;
1120
1121   del_arg = NULL;
1122   if (NULL != d->cb)
1123     {
1124       d->dead = GNUNET_YES;
1125       return;
1126     }
1127
1128   d->cb = cb;
1129   d->cb_cls = cb_cls;
1130
1131   if (d->phase == SP_CONFIG_UPDATE)
1132     {
1133       GNUNET_SCHEDULER_cancel (d->task);
1134       d->phase = SP_START_DONE;
1135     }
1136   if (d->server != NULL)
1137     {
1138       GNUNET_CORE_disconnect (d->server);
1139       d->server = NULL;
1140     }
1141
1142   if (d->th != NULL)
1143     {
1144       GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1145       GNUNET_TRANSPORT_disconnect (d->th);
1146       d->th = NULL;
1147     }
1148   /* state clean up and notifications */
1149   GNUNET_free_non_null (d->hello);
1150
1151 #if DEBUG_TESTING
1152   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153               _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1154 #endif
1155
1156   d->phase = SP_START_ARMING;
1157
1158   /* Check if this is a local or remote process */
1159   if (NULL != d->hostname)
1160     {
1161 #if DEBUG_TESTING
1162       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163                   "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1164                   d->cfgfile, d->hostname);
1165 #endif
1166
1167       if (d->username != NULL)
1168         GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1169       else
1170         arg = GNUNET_strdup (d->hostname);
1171
1172       d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1173 #if !DEBUG_TESTING
1174                                          "-q",
1175 #endif
1176                                          arg, "gnunet-arm",
1177 #if DEBUG_TESTING
1178                                          "-L", "DEBUG",
1179 #endif
1180                                          "-c", d->cfgfile, "-e", "-r", NULL);
1181       /* Use -r to restart arm and all services */
1182
1183       GNUNET_free (arg);
1184     }
1185   else
1186     {
1187 #if DEBUG_TESTING
1188       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189                   "Stopping gnunet-arm with config `%s' locally.\n",
1190                   d->cfgfile);
1191 #endif
1192       d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1193                                          "gnunet-arm",
1194 #if DEBUG_TESTING
1195                                          "-L", "DEBUG",
1196 #endif
1197                                          "-c", d->cfgfile, "-e", "-r", NULL);
1198     }
1199
1200   GNUNET_free_non_null (del_arg);
1201   d->task
1202     = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1203                                     &start_fsm, d);
1204
1205 }
1206
1207
1208 /**
1209  * Stops a GNUnet daemon.
1210  *
1211  * @param d the daemon that should be stopped
1212  * @param timeout how long to wait for process for shutdown to complete
1213  * @param cb function called once the daemon was stopped
1214  * @param cb_cls closure for cb
1215  * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1216  *        to leave them
1217  * @param allow_restart GNUNET_YES to restart peer later (using this API)
1218  *        GNUNET_NO to kill off and clean up for good
1219  */
1220 void
1221 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1222                             struct GNUNET_TIME_Relative timeout,
1223                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1224                             int delete_files, int allow_restart)
1225 {
1226   char *arg;
1227   char *del_arg;
1228   d->dead_cb = cb;
1229   d->dead_cb_cls = cb_cls;
1230
1231   if (NULL != d->cb)
1232     {
1233 #if DEBUG_TESTING
1234       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235                   _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1236 #endif
1237       d->dead = GNUNET_YES;
1238       return;
1239     }
1240
1241   if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES))    /* Peer has already been stopped in churn context! */
1242     {
1243       /* Free what was left from churning! */
1244       GNUNET_assert (d->cfg != NULL);
1245       GNUNET_CONFIGURATION_destroy (d->cfg);
1246       if (delete_files == GNUNET_YES)
1247         {
1248           if (0 != UNLINK (d->cfgfile))
1249             {
1250               GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1251             }
1252         }
1253       GNUNET_free (d->cfgfile);
1254       GNUNET_free_non_null (d->hostname);
1255       GNUNET_free_non_null (d->username);
1256       if (NULL != d->dead_cb)
1257         d->dead_cb (d->dead_cb_cls, NULL);
1258       GNUNET_free (d);
1259       return;
1260     }
1261
1262   del_arg = NULL;
1263   if (delete_files == GNUNET_YES)
1264     {
1265       GNUNET_asprintf (&del_arg, "-d");
1266     }
1267
1268   if (d->phase == SP_CONFIG_UPDATE)
1269     {
1270       GNUNET_SCHEDULER_cancel (d->task);
1271       d->phase = SP_START_DONE;
1272     }
1273   /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1274   if (d->server != NULL)
1275     {
1276       GNUNET_CORE_disconnect (d->server);
1277       d->server = NULL;
1278     }
1279     */
1280   /* shutdown ARM process (will terminate others) */
1281 #if DEBUG_TESTING
1282   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283               _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1284 #endif
1285   d->phase = SP_SHUTDOWN_START;
1286   d->running = GNUNET_NO;
1287   if (allow_restart == GNUNET_YES)
1288     d->churn = GNUNET_YES;
1289   if (d->th != NULL)
1290     {
1291       GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1292       GNUNET_TRANSPORT_disconnect (d->th);
1293       d->th = NULL;
1294     }
1295   /* Check if this is a local or remote process */
1296   if (NULL != d->hostname)
1297     {
1298 #if DEBUG_TESTING
1299       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300                   "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1301                   d->cfgfile, d->hostname);
1302 #endif
1303
1304       if (d->username != NULL)
1305         GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1306       else
1307         arg = GNUNET_strdup (d->hostname);
1308
1309       d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1310 #if !DEBUG_TESTING
1311                                          "-q",
1312 #endif
1313                                          arg, "gnunet-arm",
1314 #if DEBUG_TESTING
1315                                          "-L", "DEBUG",
1316 #endif
1317                                          "-c", d->cfgfile, "-e", "-q",
1318                                          del_arg, NULL);
1319       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320                   "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1321                   arg, "gnunet-arm", d->cfgfile, del_arg);
1322       /* Use -e to end arm, and -d to remove temp files */
1323       GNUNET_free (arg);
1324     }
1325   else
1326     {
1327 #if DEBUG_TESTING
1328       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329                   "Stopping gnunet-arm with config `%s' locally.\n",
1330                   d->cfgfile);
1331 #endif
1332       d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1333                                          "gnunet-arm",
1334 #if DEBUG_TESTING
1335                                          "-L", "DEBUG",
1336 #endif
1337                                          "-c", d->cfgfile, "-e", "-q",
1338                                          del_arg, NULL);
1339     }
1340
1341   GNUNET_free_non_null (del_arg);
1342   d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1343   d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1344 }
1345
1346
1347 /**
1348  * Changes the configuration of a GNUnet daemon.
1349  *
1350  * @param d the daemon that should be modified
1351  * @param cfg the new configuration for the daemon
1352  * @param cb function called once the configuration was changed
1353  * @param cb_cls closure for cb
1354  */
1355 void
1356 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1357                                    struct GNUNET_CONFIGURATION_Handle *cfg,
1358                                    GNUNET_TESTING_NotifyCompletion cb,
1359                                    void *cb_cls)
1360 {
1361   char *arg;
1362
1363   if (d->phase != SP_START_DONE)
1364     {
1365       if (NULL != cb)
1366         cb (cb_cls,
1367             _
1368             ("Peer not yet running, can not change configuration at this point."));
1369       return;
1370     }
1371
1372   /* 1) write configuration to temporary file */
1373   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1374     {
1375       if (NULL != cb)
1376         cb (cb_cls, _("Failed to write new configuration to disk."));
1377       return;
1378     }
1379
1380   /* 2) copy file to remote host (if necessary) */
1381   if (NULL == d->hostname)
1382     {
1383       /* signal success */
1384       if (NULL != cb)
1385         cb (cb_cls, NULL);
1386       return;
1387     }
1388 #if DEBUG_TESTING
1389   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390               "Copying updated configuration file to remote host `%s'.\n",
1391               d->hostname);
1392 #endif
1393   d->phase = SP_CONFIG_UPDATE;
1394   if (NULL != d->username)
1395     GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1396   else
1397     GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1398   d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1399 #if !DEBUG_TESTING
1400                                      "-q",
1401 #endif
1402                                      d->cfgfile, arg, NULL);
1403   GNUNET_free (arg);
1404   if (NULL == d->proc)
1405     {
1406       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1407                   _
1408                   ("Could not start `%s' process to copy configuration file.\n"),
1409                   "scp");
1410       if (NULL != cb)
1411         cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1412       d->phase = SP_START_DONE;
1413       return;
1414     }
1415   d->update_cb = cb;
1416   d->update_cb_cls = cb_cls;
1417   d->task
1418     = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1419                                     &start_fsm, d);
1420 }
1421
1422
1423 /**
1424  * Data kept for each pair of peers that we try
1425  * to connect.
1426  */
1427 struct ConnectContext
1428 {
1429   /**
1430    * Testing handle to the first daemon.
1431    */
1432   struct GNUNET_TESTING_Daemon *d1;
1433
1434   /**
1435    * Handle to core of first daemon (to check connect)
1436    */
1437   struct GNUNET_CORE_Handle *d1core;
1438
1439   /**
1440    * Have we actually connected to the core of the first daemon yet?
1441    */
1442   int d1core_ready;
1443
1444   /**
1445    * Testing handle to the second daemon.
1446    */
1447   struct GNUNET_TESTING_Daemon *d2;
1448
1449   /**
1450    * Handler for the request to core to connect to this peer.
1451    */
1452   struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1453
1454   /**
1455    * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1456    */
1457   struct GNUNET_TRANSPORT_Handle *d1th;
1458
1459   /**
1460    * Function to call once we are done (or have timed out).
1461    */
1462   GNUNET_TESTING_NotifyConnection cb;
1463
1464   /**
1465    * Closure for "nb".
1466    */
1467   void *cb_cls;
1468
1469   /**
1470    * The relative timeout from whence this connect attempt was
1471    * started.  Allows for reconnect attempts.
1472    */
1473   struct GNUNET_TIME_Relative relative_timeout;
1474
1475   /**
1476    * Maximum number of connect attempts, will retry connection
1477    * this number of times on failures.
1478    */
1479   unsigned int connect_attempts;
1480
1481   /**
1482    * Hello timeout task
1483    */
1484   GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1485
1486   /**
1487    * Connect timeout task
1488    */
1489   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1490
1491   /**
1492    * When should this operation be complete (or we must trigger
1493    * a timeout).
1494    */
1495   struct GNUNET_TIME_Relative timeout_hello;
1496
1497   /**
1498    * Was the connection attempt successful?
1499    */
1500   int connected;
1501
1502   /**
1503    * When connecting, do we need to send the HELLO?
1504    */
1505   int send_hello;
1506
1507   /**
1508    * The distance between the two connected peers
1509    */
1510   uint32_t distance;
1511 };
1512
1513
1514 /** Forward declaration **/
1515 static void
1516 reattempt_daemons_connect (void *cls,
1517                            const struct GNUNET_SCHEDULER_TaskContext *tc);
1518
1519
1520 /**
1521  * Notify callback about success or failure of the attempt
1522  * to connect the two peers
1523  *
1524  * @param cls our "struct ConnectContext" (freed)
1525  * @param tc reason tells us if we succeeded or failed
1526  */
1527 static void
1528 notify_connect_result (void *cls,
1529                        const struct GNUNET_SCHEDULER_TaskContext *tc)
1530 {
1531   struct ConnectContext *ctx = cls;
1532   ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1533   if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1534     {
1535       GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1536       ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1537     }
1538
1539   if (ctx->connect_request_handle != NULL)
1540     {
1541       GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1542       ctx->connect_request_handle = NULL;
1543     }
1544
1545   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1546     {
1547       if (ctx->d1th != NULL)
1548         GNUNET_TRANSPORT_disconnect (ctx->d1th);
1549       ctx->d1th = NULL;
1550       if (ctx->d1core != NULL)
1551         GNUNET_CORE_disconnect (ctx->d1core);
1552 #if CONNECT_CORE2
1553       if (ctx->d2core != NULL)
1554         GNUNET_CORE_disconnect (ctx->d2core);
1555       ctx->d2core = NULL;
1556 #endif
1557       ctx->d1core = NULL;
1558       GNUNET_free (ctx);
1559       return;
1560     }
1561
1562   if (ctx->d1th != NULL)
1563     GNUNET_TRANSPORT_disconnect (ctx->d1th);
1564   ctx->d1th = NULL;
1565   if (ctx->d1core != NULL)
1566     GNUNET_CORE_disconnect (ctx->d1core);
1567   ctx->d1core = NULL;
1568
1569   if (ctx->connected == GNUNET_YES)
1570     {
1571       if (ctx->cb != NULL)
1572         {
1573           ctx->cb (ctx->cb_cls,
1574                    &ctx->d1->id,
1575                    &ctx->d2->id,
1576                    ctx->distance,
1577                    ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1578         }
1579     }
1580   else if (ctx->connect_attempts > 0)
1581     {
1582       ctx->d1core_ready = GNUNET_NO;
1583 #if CONNECT_CORE2
1584       if (ctx->d2core != NULL)
1585         {
1586           GNUNET_CORE_disconnect (ctx->d2core);
1587           ctx->d2core = NULL;
1588         }
1589 #endif
1590       GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1591       return;
1592     }
1593   else
1594     {
1595       if (ctx->cb != NULL)
1596         {
1597           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1598                    ctx->d2->cfg, ctx->d1, ctx->d2,
1599                    _("Peers failed to connect"));
1600         }
1601     }
1602
1603   GNUNET_free (ctx);
1604 }
1605
1606
1607 /**
1608  * Success, connection is up.  Signal client our success.
1609  *
1610  * @param cls our "struct ConnectContext"
1611  * @param peer identity of the peer that has connected
1612  * @param atsi performance information
1613  *
1614  */
1615 static void
1616 connect_notify (void *cls,
1617                 const struct GNUNET_PeerIdentity *peer,
1618                 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1619 {
1620   struct ConnectContext *ctx = cls;
1621
1622 #if DEBUG_TESTING
1623   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1624                 "Connected peer %s to peer %s\n",
1625                 ctx->d1->shortname, GNUNET_i2s(peer));
1626 #endif
1627
1628   if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1629     {
1630
1631       ctx->connected = GNUNET_YES;
1632       ctx->distance = 0;        /* FIXME: distance */
1633       if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1634         {
1635           GNUNET_SCHEDULER_cancel(ctx->hello_send_task);
1636           ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1637         }
1638       GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1639       ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result,
1640                                                     ctx);
1641     }
1642 }
1643
1644 #if CONNECT_CORE2
1645 /**
1646  * Success, connection is up.  Signal client our success.
1647  *
1648  * @param cls our "struct ConnectContext"
1649  * @param peer identity of the peer that has connected
1650  * @param atsi performance information
1651  *
1652  */
1653 static void
1654 connect_notify_core2 (void *cls,
1655                       const struct GNUNET_PeerIdentity *peer,
1656                       const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1657 {
1658   struct ConnectContext *ctx = cls;
1659
1660   if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
1661     {
1662       ctx->connected = GNUNET_YES;
1663       ctx->distance = 0;        /* FIXME: distance */
1664       GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1665       ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result,
1666                                                     ctx);
1667     }
1668
1669 }
1670 #endif
1671
1672 /**
1673  * Task called once a core connect request has been transmitted.
1674  *
1675  * @param cls struct ConnectContext
1676  * @param success was the request successful?
1677  */
1678 void
1679 core_connect_request_cont (void *cls,
1680                            int success)
1681 {
1682   struct ConnectContext *ctx = cls;
1683
1684   ctx->connect_request_handle = NULL;
1685 }
1686
1687 static void
1688 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1689 {
1690   struct ConnectContext *ctx = cls;
1691   struct GNUNET_MessageHeader *hello;
1692   ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1693   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1694     return;
1695   if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
1696       && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
1697     {
1698       hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1699       GNUNET_assert (hello != NULL);
1700       GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1701       GNUNET_assert (ctx->d1core != NULL);
1702       ctx->connect_request_handle =
1703         GNUNET_CORE_peer_request_connect (ctx->d1core,
1704                                           ctx->relative_timeout,
1705                                           &ctx->d2->id,
1706                                           &core_connect_request_cont, ctx);
1707
1708 #if DEBUG_TESTING
1709       GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1710                   "Sending connect request to CORE of %s for peer %s\n",
1711                   GNUNET_i2s (&ctx->d1->id),
1712                   GNUNET_h2s (&ctx->d2->id.hashPubKey));
1713 #endif
1714       ctx->timeout_hello =
1715         GNUNET_TIME_relative_add (ctx->timeout_hello,
1716                                   GNUNET_TIME_relative_multiply
1717                                   (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1718     }
1719   ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello,
1720                                                        &send_hello, ctx);
1721 }
1722
1723 /**
1724  * Notify of a successful connection to the core service.
1725  *
1726  * @param cls a ConnectContext
1727  * @param server handle to the core service
1728  * @param my_identity the peer identity of this peer
1729  * @param publicKey the public key of the peer
1730  */
1731 void
1732 core_init_notify (void *cls,
1733                   struct GNUNET_CORE_Handle * server,
1734                   const struct GNUNET_PeerIdentity *
1735                   my_identity,
1736                   const struct
1737                   GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1738                   publicKey)
1739 {
1740   struct ConnectContext *connect_ctx = cls;
1741   connect_ctx->d1core_ready = GNUNET_YES;
1742
1743   if (connect_ctx->send_hello == GNUNET_NO)
1744     {
1745       connect_ctx->connect_request_handle =
1746           GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
1747                                             connect_ctx->relative_timeout,
1748                                             &connect_ctx->d2->id,
1749                                             &core_connect_request_cont, connect_ctx);
1750       GNUNET_assert(connect_ctx->connect_request_handle != NULL);
1751 #if DEBUG_TESTING
1752       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1753                   "Sending connect request to CORE of %s for peer %s\n",
1754                   connect_ctx->d1->shortname,
1755                   connect_ctx->d2->shortname);
1756 #endif
1757     }
1758
1759 }
1760
1761
1762 static void
1763 reattempt_daemons_connect (void *cls,
1764                            const struct GNUNET_SCHEDULER_TaskContext *tc)
1765 {
1766   struct ConnectContext *ctx = cls;
1767   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1768     {
1769       GNUNET_free(ctx);
1770       return;
1771     }
1772 #if DEBUG_TESTING_RECONNECT
1773   GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1774               "re-attempting connect of peer %s to peer %s\n",
1775               ctx->d1->shortname, ctx->d2->shortname);
1776 #endif
1777   ctx->connect_attempts--;
1778   GNUNET_assert (ctx->d1core == NULL);
1779   ctx->d1core_ready = GNUNET_NO;
1780   ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1781                                      ctx,
1782                                      &core_init_notify,
1783                                      &connect_notify, NULL, NULL,
1784                                      NULL, GNUNET_NO,
1785                                      NULL, GNUNET_NO, no_handlers);
1786   if (ctx->d1core == NULL)
1787     {
1788       if (NULL != ctx->cb)
1789         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1790                  ctx->d2->cfg, ctx->d1, ctx->d2,
1791                  _("Failed to connect to core service of first peer!\n"));
1792       GNUNET_free (ctx);
1793       return;
1794     }
1795
1796   if (ctx->send_hello == GNUNET_YES)
1797     {
1798       ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1799                                             &ctx->d1->id,
1800                                             ctx->d1, NULL, NULL, NULL);
1801       if (ctx->d1th == NULL)
1802         {
1803           GNUNET_CORE_disconnect (ctx->d1core);
1804           GNUNET_free (ctx);
1805           if (NULL != ctx->cb)
1806             ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1807                      ctx->d2->cfg, ctx->d1, ctx->d2,
1808                      _("Failed to connect to transport service!\n"));
1809           return;
1810         }
1811       ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1812     }
1813   else
1814     {
1815       ctx->connect_request_handle =
1816         GNUNET_CORE_peer_request_connect (ctx->d1core,
1817                                           ctx->relative_timeout,
1818                                           &ctx->d2->id,
1819                                           &core_connect_request_cont, ctx);
1820     }
1821   ctx->timeout_task =
1822     GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1823                                   &notify_connect_result, ctx);
1824 }
1825
1826 /**
1827  * Iterator for currently known peers, to ensure
1828  * that we don't try to send duplicate connect
1829  * requests to core.
1830  *
1831  * @param cls our "struct ConnectContext"
1832  * @param peer identity of the peer that has connected,
1833  *        NULL when iteration has finished
1834  * @param atsi performance information
1835  *
1836  */
1837 static void
1838 core_initial_iteration (void *cls,
1839                         const struct GNUNET_PeerIdentity *peer,
1840                         const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1841 {
1842   struct ConnectContext *ctx = cls;
1843
1844   if ((peer != NULL) &&
1845       (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
1846     {
1847       ctx->connected = GNUNET_YES;
1848       ctx->distance = 0;        /* FIXME: distance */
1849       return;
1850     }
1851   else if (peer == NULL) /* End of iteration over peers */
1852     {
1853       if (ctx->connected == GNUNET_YES)
1854         {
1855           ctx->timeout_task = GNUNET_SCHEDULER_add_now (&notify_connect_result,
1856                                                         ctx);
1857           return;
1858         }
1859
1860       /* Peer not already connected, need to schedule connect request! */
1861       if (ctx->d1core == NULL)
1862         {
1863 #if DEBUG_TESTING
1864           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1865                       "Peers are NOT connected, connecting to core!\n");
1866 #endif
1867           ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1868                                              ctx,
1869                                              &core_init_notify,
1870                                              &connect_notify, NULL, NULL,
1871                                              NULL, GNUNET_NO,
1872                                              NULL, GNUNET_NO, no_handlers);
1873         }
1874
1875       if (ctx->d1core == NULL)
1876         {
1877           GNUNET_free (ctx);
1878           if (NULL != ctx->cb)
1879             ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1880                 _("Failed to connect to core service of first peer!\n"));
1881           return;
1882         }
1883
1884       if (ctx->send_hello == GNUNET_YES)
1885         {
1886           ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1887                                                 &ctx->d1->id, ctx->d1, NULL, NULL, NULL);
1888           if (ctx->d1th == NULL)
1889             {
1890               GNUNET_CORE_disconnect (ctx->d1core);
1891               GNUNET_free (ctx);
1892               if (NULL != ctx->cb)
1893                 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1894                     _("Failed to connect to transport service!\n"));
1895               return;
1896             }
1897           ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1898         }
1899
1900       ctx->timeout_task =
1901         GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1902                                       &notify_connect_result, ctx);
1903     }
1904 }
1905
1906
1907 /**
1908  * Establish a connection between two GNUnet daemons.
1909  *
1910  * @param d1 handle for the first daemon
1911  * @param d2 handle for the second daemon
1912  * @param timeout how long is the connection attempt
1913  *        allowed to take?
1914  * @param max_connect_attempts how many times should we try to reconnect
1915  *        (within timeout)
1916  * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
1917  *                   the HELLO has already been exchanged
1918  * @param cb function to call at the end
1919  * @param cb_cls closure for cb
1920  */
1921 void
1922 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1923                                 struct GNUNET_TESTING_Daemon *d2,
1924                                 struct GNUNET_TIME_Relative timeout,
1925                                 unsigned int max_connect_attempts,
1926                                 int send_hello,
1927                                 GNUNET_TESTING_NotifyConnection cb,
1928                                 void *cb_cls)
1929 {
1930   struct ConnectContext *ctx;
1931
1932   if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1933     {
1934       if (NULL != cb)
1935         cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1936             _("Peers are not fully running yet, can not connect!\n"));
1937       return;
1938     }
1939
1940   ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1941   ctx->d1 = d1;
1942   ctx->d2 = d2;
1943   ctx->timeout_hello =
1944     GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
1945   ctx->relative_timeout = GNUNET_TIME_relative_divide(timeout, max_connect_attempts);
1946   ctx->cb = cb;
1947   ctx->cb_cls = cb_cls;
1948   ctx->connect_attempts = max_connect_attempts;
1949   ctx->connected = GNUNET_NO;
1950   ctx->send_hello = send_hello;
1951 #if DEBUG_TESTING
1952   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1953               "Asked to connect peer %s to peer %s\n",
1954               d1->shortname, d2->shortname);
1955 #endif
1956
1957   /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
1958   GNUNET_assert(GNUNET_OK == GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id, &core_initial_iteration, ctx));
1959   /*GNUNET_assert(GNUNET_OK == GNUNET_CORE_iterate_peers (ctx->d1->cfg, &core_initial_iteration, ctx));*/
1960 }
1961
1962 /* end of testing.c */