fixes, comments
[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 2, 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_NO
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   if (daemon == NULL)
69     return;
70
71   if (daemon->server != NULL)
72     {
73       GNUNET_CORE_disconnect(daemon->server);
74       daemon->server = NULL;
75     }
76
77   GNUNET_assert (message != NULL);
78   if (daemon->th != NULL)
79     {
80       GNUNET_TRANSPORT_get_hello_cancel(daemon->th, &process_hello, daemon);
81     }
82 #if DEBUG_TESTING
83   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
84               "Received `%s' from transport service of `%4s'\n",
85               "HELLO", GNUNET_i2s (&daemon->id));
86 #endif
87
88   GNUNET_free_non_null(daemon->hello);
89   daemon->hello = GNUNET_malloc(ntohs(message->size));
90   memcpy(daemon->hello, message, ntohs(message->size));
91
92   if (daemon->th != NULL)
93     {
94       GNUNET_TRANSPORT_disconnect(daemon->th);
95       daemon->th = NULL;
96     }
97
98 }
99
100 /**
101  * Function called after GNUNET_CORE_connect has succeeded
102  * (or failed for good).  Note that the private key of the
103  * peer is intentionally not exposed here; if you need it,
104  * your process should try to read the private key file
105  * directly (which should work if you are authorized...).
106  *
107  * @param cls closure
108  * @param server handle to the server, NULL if we failed
109  * @param my_identity ID of this peer, NULL if we failed
110  * @param publicKey public key of this peer, NULL if we failed
111  */
112 static void
113 testing_init (void *cls,
114               struct GNUNET_CORE_Handle *server,
115               const struct GNUNET_PeerIdentity *my_identity,
116               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
117 {
118   struct GNUNET_TESTING_Daemon *d = cls;
119   GNUNET_TESTING_NotifyDaemonRunning cb;
120
121   GNUNET_assert (d->phase == SP_START_CORE);
122   d->phase = SP_START_DONE;
123   cb = d->cb;
124   d->cb = NULL;
125   if (server == NULL)
126     {
127       d->server = NULL;
128       if (GNUNET_YES == d->dead)
129         GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
130       else if (NULL != cb)
131         cb (d->cb_cls, NULL, d->cfg, d,
132             _("Failed to connect to core service\n"));
133       return;
134     }
135 #if DEBUG_TESTING
136   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
137               "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
138 #endif
139   d->id = *my_identity;
140   d->shortname = strdup (GNUNET_i2s (my_identity));
141   d->server = server;
142   d->running = GNUNET_YES;
143   if (GNUNET_YES == d->dead)
144     GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
145   else if (NULL != cb)
146     cb (d->cb_cls, my_identity, d->cfg, d, NULL);
147 #if DEBUG_TESTING
148   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149               "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
150 #endif
151
152
153   d->th = GNUNET_TRANSPORT_connect (d->sched,
154                                     d->cfg, d, NULL, NULL, NULL);
155   if (d->th == NULL)
156     {
157       if (GNUNET_YES == d->dead)
158         GNUNET_TESTING_daemon_stop (d, GNUNET_TIME_absolute_get_remaining(d->max_timeout), d->dead_cb, d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
159       else if (NULL != d->cb)
160         d->cb (d->cb_cls, &d->id, d->cfg, d,
161             _("Failed to connect to transport service!\n"));
162       return;
163     }
164
165   GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
166 }
167
168
169 /**
170  * Finite-state machine for starting GNUnet.
171  *
172  * @param cls our "struct GNUNET_TESTING_Daemon"
173  * @param tc unused
174  */
175 static void
176 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177 {
178   struct GNUNET_TESTING_Daemon *d = cls;
179   GNUNET_TESTING_NotifyDaemonRunning cb;
180   enum GNUNET_OS_ProcessStatusType type;
181   unsigned long code;
182   char *dst;
183   int bytes_read;
184   static char hostkeybuf[105];
185   static const char temphostkey[104];
186
187 #if DEBUG_TESTING
188   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
189               "Peer FSM is in phase %u.\n", d->phase);
190 #endif
191
192   d->task = GNUNET_SCHEDULER_NO_TASK;
193   switch (d->phase)
194     {
195     case SP_COPYING:
196       /* confirm copying complete */
197       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
198         {
199           if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
200             {
201               cb = d->cb;
202               d->cb = NULL;
203               if (NULL != cb)
204                 cb (d->cb_cls,
205                     NULL,
206                     d->cfg, d, _("`scp' does not seem to terminate (timeout copying config).\n"));
207               return;
208             }
209           /* wait some more */
210           d->task
211             = GNUNET_SCHEDULER_add_delayed (d->sched,
212                                             GNUNET_CONSTANTS_EXEC_WAIT,
213                                             &start_fsm, d);
214           return;
215         }
216       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
217         {
218           cb = d->cb;
219           d->cb = NULL;
220           if (NULL != cb)
221             cb (d->cb_cls,
222                 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
223           return;
224         }
225 #if DEBUG_TESTING
226       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227                   "Successfully copied configuration file.\n");
228 #endif
229       d->phase = SP_COPIED;
230       /* fall-through */
231     case SP_COPIED:
232       /* Start create hostkey process */
233       d->pipe_stdout = GNUNET_DISK_pipe(GNUNET_NO);
234       if (d->pipe_stdout == NULL)
235         {
236           cb = d->cb;
237           d->cb = NULL;
238           if (NULL != cb)
239             cb (d->cb_cls,
240                 NULL,
241                 d->cfg,
242                 d,
243                 (NULL == d->hostname)
244                 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
245                 : _("Failed to create pipe for `ssh' process.\n"));
246           return;
247         }
248       if (NULL == d->hostname)
249         {
250 #if DEBUG_TESTING
251           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252                       "Starting `%s', with command `%s %s %s %s'.\n",
253                       "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
254                       "-sq");
255 #endif
256           d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
257                                             "gnunet-peerinfo",
258                                             "-c", d->cfgfile,
259                                             "-sq", NULL);
260           GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
261         }
262       else
263         {
264           if (d->username != NULL)
265             GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
266           else
267             dst = GNUNET_strdup (d->hostname);
268
269 #if DEBUG_TESTING
270           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
271                       "Starting `%s', with command `%s %s %s %s %s %s'.\n",
272                       "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c", d->cfgfile,
273                       "-sq");
274 #endif
275           d->pid = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
276                                             "ssh",
277                                             dst,
278                                             "gnunet-peerinfo",
279                                             "-c", d->cfgfile, "-sq", NULL);
280           GNUNET_DISK_pipe_close_end(d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
281           GNUNET_free (dst);
282         }
283       if (-1 == d->pid)
284         {
285           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286                       _("Could not start `%s' process to create hostkey.\n"),
287                       (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
288           cb = d->cb;
289           d->cb = NULL;
290           if (NULL != cb)
291             cb (d->cb_cls,
292                 NULL,
293                 d->cfg,
294                 d,
295                 (NULL == d->hostname)
296                 ? _("Failed to start `gnunet-peerinfo' process.\n")
297                 : _("Failed to start `ssh' process.\n"));
298           GNUNET_DISK_pipe_close(d->pipe_stdout);
299           return;
300         }
301 #if DEBUG_TESTING
302       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
303                   "Started `%s', waiting for hostkey.\n",
304                   "gnunet-peerinfo");
305 #endif
306       d->phase = SP_HOSTKEY_CREATE;
307       d->task
308         = GNUNET_SCHEDULER_add_delayed (d->sched,
309                                         GNUNET_CONSTANTS_EXEC_WAIT,
310                                         &start_fsm, d);
311       break;
312     case SP_HOSTKEY_CREATE:
313
314       bytes_read = GNUNET_DISK_file_read(GNUNET_DISK_pipe_handle(d->pipe_stdout, GNUNET_DISK_PIPE_END_READ), &hostkeybuf, sizeof(hostkeybuf));
315       if (bytes_read == 104) /* Success, we have read in the hostkey */
316         {
317           if (hostkeybuf[103] == '\n')
318             hostkeybuf[103] = '\0';
319           else
320             GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Malformed output from gnunet-peerinfo!\n");
321           memcpy(&temphostkey, &hostkeybuf, bytes_read);
322
323           if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (&temphostkey[0],
324                                                            &d->id.hashPubKey))
325             {
326               GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Failed to convert string to peer identity!\n");
327             }
328           else
329             {
330               GNUNET_DISK_pipe_close(d->pipe_stdout);
331               d->pipe_stdout = NULL;
332             }
333         }
334
335       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
336         {
337           if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
338             {
339               cb = d->cb;
340               d->cb = NULL;
341               if (NULL != cb)
342                 cb (d->cb_cls,
343                     NULL,
344                     d->cfg,
345                     d,
346                     (NULL == d->hostname)
347                     ? _("`gnunet-peerinfo' does not seem to terminate.\n")
348                     : _("`ssh' does not seem to terminate.\n"));
349
350               GNUNET_DISK_pipe_close(d->pipe_stdout);
351               return;
352             }
353           /* wait some more */
354           d->task
355             = GNUNET_SCHEDULER_add_delayed (d->sched,
356                                             GNUNET_CONSTANTS_EXEC_WAIT,
357                                             &start_fsm, d);
358           return;
359         }
360 #if DEBUG_TESTING
361       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362                   "Successfully got hostkey!\n");
363 #endif
364       if (d->pipe_stdout != NULL)
365         {
366           cb = d->cb;
367           d->cb = NULL;
368           if (NULL != cb)
369             cb (d->cb_cls,
370                 NULL,
371                 d->cfg,
372                 d,
373                 _("`Failed to get hostkey!\n"));
374           GNUNET_DISK_pipe_close(d->pipe_stdout);
375           return;
376         }
377
378       if (d->hostkey_callback != NULL)
379         {
380           d->hostkey_callback(d->hostkey_cls, &d->id, d, NULL);
381           d->phase = SP_HOSTKEY_CREATED;
382         }
383       else
384         {
385           d->phase = SP_TOPOLOGY_SETUP;
386         }
387
388       /* Fall through */
389     case SP_HOSTKEY_CREATED:
390       /* wait for topology finished */
391       if ((GNUNET_YES == d->dead) || (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0))
392         {
393           cb = d->cb;
394           d->cb = NULL;
395           if (NULL != cb)
396             cb (d->cb_cls,
397                 NULL,
398                 d->cfg,
399                 d,
400                 _("`Failed while waiting for topology setup!\n"));
401           return;
402         }
403
404       d->task
405         = GNUNET_SCHEDULER_add_delayed (d->sched,
406                                         GNUNET_CONSTANTS_EXEC_WAIT,
407                                         &start_fsm, d);
408       break;
409     case SP_TOPOLOGY_SETUP:
410       /* start GNUnet on remote host */
411       if (NULL == d->hostname)
412         {
413 #if DEBUG_TESTING
414           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
415                       "Starting `%s', with command `%s %s %s %s %s %s'.\n",
416                       "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
417                       "-L", "DEBUG",
418                       "-s");
419 #endif
420           d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
421                                             "gnunet-arm",
422                                             "-c", d->cfgfile,
423 #if DEBUG_TESTING
424                                             "-L", "DEBUG",
425 #endif
426                                             "-s", "-q", NULL);
427         }
428       else
429         {
430           if (d->username != NULL)
431             GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
432           else
433             dst = GNUNET_strdup (d->hostname);
434
435 #if DEBUG_TESTING
436           GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
437                       "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
438                       "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
439                       "-L", "DEBUG", "-s", "-q");
440 #endif
441           d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
442                                             "ssh",
443                                             dst,
444                                             "gnunet-arm",
445 #if DEBUG_TESTING
446                                             "-L", "DEBUG",
447 #endif
448                                             "-c", d->cfgfile, "-s", "-q", NULL);
449           GNUNET_free (dst);
450         }
451       if (-1 == d->pid)
452         {
453           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
454                       _("Could not start `%s' process to start GNUnet.\n"),
455                       (NULL == d->hostname) ? "gnunet-arm" : "ssh");
456           cb = d->cb;
457           d->cb = NULL;
458           if (NULL != cb)
459             cb (d->cb_cls,
460                 NULL,
461                 d->cfg,
462                 d,
463                 (NULL == d->hostname)
464                 ? _("Failed to start `gnunet-arm' process.\n")
465                 : _("Failed to start `ssh' process.\n"));
466           return;
467         }
468 #if DEBUG_TESTING
469       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470                   "Started `%s', waiting for `%s' to be up.\n",
471                   "gnunet-arm", "gnunet-service-core");
472 #endif
473       d->phase = SP_START_ARMING;
474       d->task
475         = GNUNET_SCHEDULER_add_delayed (d->sched,
476                                         GNUNET_CONSTANTS_EXEC_WAIT,
477                                         &start_fsm, d);
478       break;
479     case SP_START_ARMING:
480       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
481         {
482           if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
483             {
484               cb = d->cb;
485               d->cb = NULL;
486               if (NULL != cb)
487                 cb (d->cb_cls,
488                     NULL,
489                     d->cfg,
490                     d,
491                     (NULL == d->hostname)
492                     ? _("`gnunet-arm' does not seem to terminate.\n")
493                     : _("`ssh' does not seem to terminate.\n"));
494               return;
495             }
496           /* wait some more */
497           d->task
498             = GNUNET_SCHEDULER_add_delayed (d->sched,
499                                             GNUNET_CONSTANTS_EXEC_WAIT,
500                                             &start_fsm, d);
501           return;
502         }
503 #if DEBUG_TESTING
504       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
505                   "Successfully started `%s'.\n", "gnunet-arm");
506 #endif
507       d->phase = SP_START_CORE;
508       d->server = GNUNET_CORE_connect (d->sched,
509                                        d->cfg,
510                                        ARM_START_WAIT,
511                                        d,
512                                        &testing_init,
513                                        NULL, NULL,
514                                        NULL, GNUNET_NO,
515                                        NULL, GNUNET_NO, no_handlers);
516       break;
517     case SP_START_CORE:
518       GNUNET_break (0);
519       break;
520     case SP_START_DONE:
521       GNUNET_break (0);
522       break;
523     case SP_SHUTDOWN_START:
524       /* confirm copying complete */
525       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
526         {
527           if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0)
528             {
529               d->dead_cb (d->dead_cb_cls,
530                           _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
531               if (d->th != NULL)
532                 {
533                   GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
534                   GNUNET_TRANSPORT_disconnect(d->th);
535                   d->th = NULL;
536                 }
537               GNUNET_CONFIGURATION_destroy (d->cfg);
538               GNUNET_free (d->cfgfile);
539               GNUNET_free_non_null(d->hello);
540               GNUNET_free_non_null (d->hostname);
541               GNUNET_free_non_null (d->username);
542               GNUNET_free_non_null (d->shortname);
543               GNUNET_free (d);
544               return;
545             }
546           /* wait some more */
547           d->task
548             = GNUNET_SCHEDULER_add_delayed (d->sched,
549                                             GNUNET_CONSTANTS_EXEC_WAIT,
550                                             &start_fsm, d);
551           return;
552         }
553       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
554         {
555           if (NULL != d->dead_cb)
556             d->dead_cb (d->dead_cb_cls,
557                         _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
558           if (d->th != NULL)
559             {
560               GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
561               GNUNET_TRANSPORT_disconnect(d->th);
562               d->th = NULL;
563             }
564           GNUNET_CONFIGURATION_destroy (d->cfg);
565           GNUNET_free (d->cfgfile);
566           GNUNET_free_non_null(d->hello);
567           GNUNET_free_non_null (d->hostname);
568           GNUNET_free_non_null (d->username);
569           GNUNET_free_non_null (d->shortname);
570           GNUNET_free (d);
571           return;
572         }
573 #if DEBUG_TESTING
574       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
575 #endif
576       if (d->th != NULL)
577         {
578           GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
579           GNUNET_TRANSPORT_disconnect(d->th);
580           d->th = NULL;
581         }
582       /* state clean up and notifications */
583       if (d->churn == GNUNET_NO)
584         {
585           GNUNET_CONFIGURATION_destroy (d->cfg);
586           GNUNET_free (d->cfgfile);
587           GNUNET_free_non_null (d->hostname);
588           GNUNET_free_non_null (d->username);
589         }
590
591       GNUNET_free_non_null(d->hello);
592       d->hello = NULL;
593       GNUNET_free_non_null (d->shortname);
594       d->shortname = NULL;
595       if (NULL != d->dead_cb)
596         d->dead_cb (d->dead_cb_cls, NULL);
597
598       if (d->churn == GNUNET_NO)
599         GNUNET_free (d);
600
601       break;
602     case SP_CONFIG_UPDATE:
603       /* confirm copying complete */
604       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
605         {
606           if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0) /* FIXME: config update should take timeout parameter! */
607             {
608               cb = d->cb;
609               d->cb = NULL;
610               if (NULL != cb)
611                 cb (d->cb_cls,
612                     NULL,
613                     d->cfg, d, _("`scp' does not seem to terminate.\n"));
614               return;
615             }
616           /* wait some more */
617           d->task
618             = GNUNET_SCHEDULER_add_delayed (d->sched,
619                                             GNUNET_CONSTANTS_EXEC_WAIT,
620                                             &start_fsm, d);
621           return;
622         }
623       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
624         {
625           if (NULL != d->update_cb)
626             d->update_cb (d->update_cb_cls,
627                           _("`scp' did not complete cleanly.\n"));
628           return;
629         }
630 #if DEBUG_TESTING
631       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
632                   "Successfully copied configuration file.\n");
633 #endif
634       if (NULL != d->update_cb)
635         d->update_cb (d->update_cb_cls, NULL);
636       d->phase = SP_START_DONE;
637       break;
638     }
639 }
640
641 /**
642  * Continues GNUnet daemon startup when user wanted to be notified
643  * once a hostkey was generated (for creating friends files, blacklists,
644  * etc.).
645  *
646  * @param daemon the daemon to finish starting
647  */
648 void
649 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
650 {
651   GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
652   daemon->phase = SP_TOPOLOGY_SETUP;
653 }
654
655
656 /**
657  * Start a peer that has previously been stopped using the daemon_stop
658  * call (and files weren't deleted and the allow restart flag)
659  *
660  * @param daemon the daemon to start (has been previously stopped)
661  * @param timeout how long to wait for restart
662  * @param cb the callback for notification when the peer is running
663  * @param cb_cls closure for the callback
664  */
665 void
666 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
667                                      struct GNUNET_TIME_Relative timeout,
668                                      GNUNET_TESTING_NotifyDaemonRunning cb,
669                                      void *cb_cls)
670 {
671   if (daemon->running == GNUNET_YES)
672   {
673     cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
674     return;
675   }
676
677   daemon->cb = cb;
678   daemon->cb_cls = cb_cls;
679   daemon->phase = SP_TOPOLOGY_SETUP;
680   daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
681
682   GNUNET_SCHEDULER_add_continuation (daemon->sched,
683                                      &start_fsm,
684                                      daemon,
685                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
686 }
687
688 /**
689  * Starts a GNUnet daemon.  GNUnet must be installed on the target
690  * system and available in the PATH.  The machine must furthermore be
691  * reachable via "ssh" (unless the hostname is "NULL") without the
692  * need to enter a password.
693  *
694  * @param sched scheduler to use
695  * @param cfg configuration to use
696  * @param timeout how long to wait starting up peers
697  * @param hostname name of the machine where to run GNUnet
698  *        (use NULL for localhost).
699  * @param hostkey_callback function to call once the hostkey has been
700  *        generated for this peer, but it hasn't yet been started
701  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
702  * @param hostkey_cls closure for hostkey callback
703  * @param cb function to call with the result
704  * @param cb_cls closure for cb
705  * @return handle to the daemon (actual start will be completed asynchronously)
706  */
707 struct GNUNET_TESTING_Daemon *
708 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
709                              const struct GNUNET_CONFIGURATION_Handle *cfg,
710                              struct GNUNET_TIME_Relative timeout,
711                              const char *hostname,
712                              GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
713                              void *hostkey_cls,
714                              GNUNET_TESTING_NotifyDaemonRunning cb,
715                              void *cb_cls)
716 {
717   struct GNUNET_TESTING_Daemon *ret;
718   char *arg;
719   char *username;
720
721   ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
722   ret->sched = sched;
723   ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
724   ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
725 #if DEBUG_TESTING
726   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727               "Setting up peer with configuration file `%s'.\n",
728               ret->cfgfile);
729 #endif
730   if (NULL == ret->cfgfile)
731     {
732       GNUNET_free_non_null (ret->hostname);
733       GNUNET_free (ret);
734       return NULL;
735     }
736   ret->hostkey_callback = hostkey_callback;
737   ret->hostkey_cls = hostkey_cls;
738   ret->cb = cb;
739   ret->cb_cls = cb_cls;
740   ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
741   ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
742   GNUNET_CONFIGURATION_set_value_string (ret->cfg,
743                                          "PATHS",
744                                          "DEFAULTCONFIG", ret->cfgfile);
745   /* 1) write configuration to temporary file */
746   if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
747     {
748       if (0 != UNLINK (ret->cfgfile))
749         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
750                                   "unlink", ret->cfgfile);
751       GNUNET_CONFIGURATION_destroy (ret->cfg);
752       GNUNET_free_non_null (ret->hostname);
753       GNUNET_free (ret->cfgfile);
754       GNUNET_free (ret);
755       return NULL;
756     }
757   if (GNUNET_OK !=
758       GNUNET_CONFIGURATION_get_value_string (cfg,
759                                              "TESTING",
760                                              "USERNAME", &username))
761     {
762       if (NULL != getenv ("USER"))
763         username = GNUNET_strdup (getenv ("USER"));
764       else
765         username = NULL;
766     }
767   ret->username = username;
768
769   /* 2) copy file to remote host */
770   if (NULL != hostname)
771     {
772 #if DEBUG_TESTING
773       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
774                   "Copying configuration file to host `%s'.\n", hostname);
775 #endif
776       ret->phase = SP_COPYING;
777       if (NULL != username)
778         GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
779       else
780         GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
781       ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
782                                           "scp", ret->cfgfile, arg, NULL);
783       GNUNET_free (arg);
784       if (-1 == ret->pid)
785         {
786           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
787                       _
788                       ("Could not start `%s' process to copy configuration file.\n"),
789                       "scp");
790           if (0 != UNLINK (ret->cfgfile))
791             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
792                                       "unlink", ret->cfgfile);
793           GNUNET_CONFIGURATION_destroy (ret->cfg);
794           GNUNET_free_non_null (ret->hostname);
795           GNUNET_free_non_null (ret->username);
796           GNUNET_free (ret->cfgfile);
797           GNUNET_free (ret);
798           return NULL;
799         }
800       ret->task
801         = GNUNET_SCHEDULER_add_delayed (sched,
802                                         GNUNET_CONSTANTS_EXEC_WAIT,
803                                         &start_fsm, ret);
804       return ret;
805     }
806 #if DEBUG_TESTING
807   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808               "No need to copy configuration file since we are running locally.\n");
809 #endif
810   ret->phase = SP_COPIED;
811   GNUNET_SCHEDULER_add_continuation (sched,
812                                      &start_fsm,
813                                      ret,
814                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
815   return ret;
816 }
817
818
819 /**
820  * Restart (stop and start) a GNUnet daemon.
821  *
822  * @param d the daemon that should be restarted
823  * @param cb function called once the daemon is (re)started
824  * @param cb_cls closure for cb
825  */
826 void
827 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
828                                GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
829 {
830   char *arg;
831   char *del_arg;
832
833   del_arg = NULL;
834   if (NULL != d->cb)
835     {
836       d->dead = GNUNET_YES;
837       return;
838     }
839
840   d->cb = cb;
841   d->cb_cls = cb_cls;
842
843   if (d->phase == SP_CONFIG_UPDATE)
844     {
845       GNUNET_SCHEDULER_cancel (d->sched, d->task);
846       d->phase = SP_START_DONE;
847     }
848   if (d->server != NULL)
849     {
850       GNUNET_CORE_disconnect (d->server);
851       d->server = NULL;
852     }
853
854   if (d->th != NULL)
855     {
856       GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
857       GNUNET_TRANSPORT_disconnect(d->th);
858       d->th = NULL;
859     }
860   /* state clean up and notifications */
861   GNUNET_free_non_null(d->hello);
862
863 #if DEBUG_TESTING
864     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
865                 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
866 #endif
867
868    d->phase = SP_START_ARMING;
869
870     /* Check if this is a local or remote process */
871   if (NULL != d->hostname)
872     {
873 #if DEBUG_TESTING
874       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
875                   "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
876 #endif
877
878       if (d->username != NULL)
879         GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
880       else
881         arg = GNUNET_strdup (d->hostname);
882
883       d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
884                                         arg, "gnunet-arm",
885 #if DEBUG_TESTING
886                                         "-L", "DEBUG",
887 #endif
888                                         "-c", d->cfgfile, "-e", "-r", NULL);
889       /* Use -r to restart arm and all services */
890
891       GNUNET_free (arg);
892     }
893   else
894     {
895 #if DEBUG_TESTING
896       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
897                   "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
898 #endif
899       d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
900                                         "gnunet-arm",
901 #if DEBUG_TESTING
902                                         "-L", "DEBUG",
903 #endif
904                                         "-c", d->cfgfile, "-e", "-r", NULL);
905     }
906
907     GNUNET_free_non_null(del_arg);
908     d->task
909       = GNUNET_SCHEDULER_add_delayed (d->sched,
910                                       GNUNET_CONSTANTS_EXEC_WAIT,
911                                       &start_fsm, d);
912
913 }
914
915
916 /**
917  * Stops a GNUnet daemon.
918  *
919  * @param d the daemon that should be stopped
920  * @param timeout how long to wait for process for shutdown to complete
921  * @param cb function called once the daemon was stopped
922  * @param cb_cls closure for cb
923  * @param delete_files GNUNET_YES to remove files, GNUNET_NO
924  *        to leave them
925  * @param allow_restart GNUNET_YES to restart peer later (using this API)
926  *        GNUNET_NO to kill off and clean up for good
927  */
928 void
929 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
930                             struct GNUNET_TIME_Relative timeout,
931                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
932                             int delete_files, int allow_restart)
933 {
934   char *arg;
935   char *del_arg;
936   d->dead_cb = cb;
937   d->dead_cb_cls = cb_cls;
938
939   if (NULL != d->cb)
940     {
941 #if DEBUG_TESTING
942       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943                  _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
944 #endif
945       d->dead = GNUNET_YES;
946       return;
947     }
948
949   del_arg = NULL;
950   if (delete_files == GNUNET_YES)
951     {
952       GNUNET_asprintf(&del_arg, "-d");
953     }
954
955   if (d->phase == SP_CONFIG_UPDATE)
956     {
957       GNUNET_SCHEDULER_cancel (d->sched, d->task);
958       d->phase = SP_START_DONE;
959     }
960   if (d->server != NULL)
961     {
962       GNUNET_CORE_disconnect (d->server);
963       d->server = NULL;
964     }
965   /* shutdown ARM process (will terminate others) */
966 #if DEBUG_TESTING
967   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968               _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
969 #endif
970
971   d->phase = SP_SHUTDOWN_START;
972   d->running = GNUNET_NO;
973
974   if (allow_restart == GNUNET_YES)
975     d->churn = GNUNET_YES;
976
977   if (d->th != NULL)
978     {
979       GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
980       GNUNET_TRANSPORT_disconnect(d->th);
981       d->th = NULL;
982     }
983   /* Check if this is a local or remote process */
984   if (NULL != d->hostname)
985     {
986 #if DEBUG_TESTING
987       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988                   "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
989 #endif
990
991       if (d->username != NULL)
992         GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
993       else
994         arg = GNUNET_strdup (d->hostname);
995
996       d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
997                                         arg, "gnunet-arm",
998 #if DEBUG_TESTING
999                                         "-L", "DEBUG",
1000 #endif
1001                                         "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1002       /* Use -e to end arm, and -d to remove temp files */
1003
1004       GNUNET_free (arg);
1005     }
1006   else
1007     {
1008 #if DEBUG_TESTING
1009       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1010                   "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1011 #endif
1012       d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1013                                         "gnunet-arm",
1014 #if DEBUG_TESTING
1015                                         "-L", "DEBUG",
1016 #endif
1017                                         "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1018     }
1019
1020   GNUNET_free_non_null(del_arg);
1021   d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1022   d->task
1023     = GNUNET_SCHEDULER_add_now (d->sched,
1024                                 &start_fsm, d);
1025 }
1026
1027
1028 /**
1029  * Changes the configuration of a GNUnet daemon.
1030  *
1031  * @param d the daemon that should be modified
1032  * @param cfg the new configuration for the daemon
1033  * @param cb function called once the configuration was changed
1034  * @param cb_cls closure for cb
1035  */
1036 void
1037 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1038                                    struct GNUNET_CONFIGURATION_Handle *cfg,
1039                                    GNUNET_TESTING_NotifyCompletion cb,
1040                                    void *cb_cls)
1041 {
1042   char *arg;
1043
1044   if (d->phase != SP_START_DONE)
1045     {
1046       if (NULL != cb)
1047         cb (cb_cls,
1048             _
1049             ("Peer not yet running, can not change configuration at this point."));
1050       return;
1051     }
1052
1053   /* 1) write configuration to temporary file */
1054   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1055     {
1056       if (NULL != cb)
1057         cb (cb_cls, _("Failed to write new configuration to disk."));
1058       return;
1059     }
1060
1061   /* 2) copy file to remote host (if necessary) */
1062   if (NULL == d->hostname)
1063     {
1064       /* signal success */
1065       if (NULL != cb)
1066         cb (cb_cls, NULL);
1067       return;
1068     }
1069 #if DEBUG_TESTING
1070   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1071               "Copying updated configuration file to remote host `%s'.\n",
1072               d->hostname);
1073 #endif
1074   d->phase = SP_CONFIG_UPDATE;
1075   if (NULL != d->username)
1076     GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1077   else
1078     GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1079   d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
1080   GNUNET_free (arg);
1081   if (-1 == d->pid)
1082     {
1083       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1084                   _
1085                   ("Could not start `%s' process to copy configuration file.\n"),
1086                   "scp");
1087       if (NULL != cb)
1088         cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1089       d->phase = SP_START_DONE;
1090       return;
1091     }
1092   d->update_cb = cb;
1093   d->update_cb_cls = cb_cls;
1094   d->task
1095     = GNUNET_SCHEDULER_add_delayed (d->sched,
1096                                     GNUNET_CONSTANTS_EXEC_WAIT,
1097                                     &start_fsm, d);
1098 }
1099
1100
1101 /**
1102  * Data kept for each pair of peers that we try
1103  * to connect.
1104  */
1105 struct ConnectContext
1106 {
1107   /**
1108    * Testing handle to the first daemon.
1109    */
1110   struct GNUNET_TESTING_Daemon *d1;
1111
1112   /**
1113    * Handle to core of first daemon (to check connect)
1114    */
1115   struct GNUNET_CORE_Handle * d1core;
1116
1117   /**
1118    * Testing handle to the second daemon.
1119    */
1120   struct GNUNET_TESTING_Daemon *d2;
1121
1122   /**
1123    * Transport handle to the second daemon.
1124    */
1125   struct GNUNET_TRANSPORT_Handle *d2th;
1126
1127   /**
1128    * Function to call once we are done (or have timed out).
1129    */
1130   GNUNET_TESTING_NotifyConnection cb;
1131
1132   /**
1133    * Closure for "nb".
1134    */
1135   void *cb_cls;
1136
1137   /**
1138    * When should this operation be complete (or we must trigger
1139    * a timeout).
1140    */
1141   struct GNUNET_TIME_Absolute timeout;
1142
1143   /**
1144    * The relative timeout from whence this connect attempt was
1145    * started.  Allows for reconnect attempts.
1146    */
1147   struct GNUNET_TIME_Relative relative_timeout;
1148
1149   /**
1150    * Maximum number of connect attempts, will retry connection
1151    * this number of times on failures.
1152    */
1153   unsigned int max_connect_attempts;
1154
1155   /**
1156    * Hello timeout task
1157    */
1158   GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1159
1160   /**
1161    * Connect timeout task
1162    */
1163   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1164
1165   /**
1166    * When should this operation be complete (or we must trigger
1167    * a timeout).
1168    */
1169   struct GNUNET_TIME_Relative timeout_hello;
1170
1171
1172   /**
1173    * Was the connection attempt successful?
1174    */
1175   int connected;
1176 };
1177
1178
1179 /** Forward declaration **/
1180 static void
1181 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1182
1183
1184 /**
1185  * Notify callback about success or failure of the attempt
1186  * to connect the two peers
1187  *
1188  * @param cls our "struct ConnectContext" (freed)
1189  * @param tc reason tells us if we succeeded or failed
1190  */
1191 static void
1192 notify_connect_result (void *cls,
1193                        const struct GNUNET_SCHEDULER_TaskContext *tc)
1194 {
1195   struct ConnectContext *ctx = cls;
1196   struct GNUNET_TIME_Relative remaining;
1197
1198   ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1199   if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1200     {
1201       GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1202       ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1203     }
1204   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1205     {
1206       if (ctx->d2th != NULL)
1207         GNUNET_TRANSPORT_disconnect (ctx->d2th);
1208       ctx->d2th = NULL;
1209       if (ctx->d1core != NULL)
1210         GNUNET_CORE_disconnect (ctx->d1core);
1211
1212       ctx->d1core = NULL;
1213       GNUNET_free (ctx);
1214       return;
1215     }
1216
1217   remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1218
1219   if (ctx->connected == GNUNET_YES)
1220     {
1221       if (ctx->cb != NULL)
1222         {
1223           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1224                    ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1225         }
1226     }
1227   else if (remaining.value > 0)
1228     {
1229       if (ctx->d1core != NULL)
1230         {
1231           GNUNET_CORE_disconnect(ctx->d1core);
1232           ctx->d1core = NULL;
1233         }
1234
1235       if (ctx->d2th != NULL)
1236         {
1237           GNUNET_TRANSPORT_disconnect(ctx->d2th);
1238           ctx->d2th = NULL;
1239         }
1240       GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1241       return;
1242     }
1243   else
1244     {
1245       if (ctx->cb != NULL)
1246         {
1247           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1248                    ctx->d2->cfg, ctx->d1, ctx->d2,
1249                    _("Peers failed to connect"));
1250         }
1251     }
1252
1253   GNUNET_TRANSPORT_disconnect (ctx->d2th);
1254   ctx->d2th = NULL;
1255   GNUNET_CORE_disconnect (ctx->d1core);
1256   ctx->d1core = NULL;
1257   GNUNET_free (ctx);
1258 }
1259
1260
1261 /**
1262  * Success, connection is up.  Signal client our success.
1263  *
1264  * @param cls our "struct ConnectContext"
1265  * @param peer identity of the peer that has connected
1266  * @param latency the round trip latency of the connection to this peer
1267  * @param distance distance the transport level distance to this peer
1268  *
1269  */
1270 static void
1271 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1272                 uint32_t distance)
1273 {
1274   struct ConnectContext *ctx = cls;
1275
1276   if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1277     {
1278       ctx->connected = GNUNET_YES;
1279       GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1280       ctx->timeout_task = GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1281                                                     &notify_connect_result,
1282                                                     ctx);
1283     }
1284
1285 }
1286
1287 static void
1288 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1289 {
1290   struct ConnectContext *ctx = cls;
1291
1292   ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1293   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1294     return;
1295   if (ctx->d1->hello != NULL)
1296     {
1297       GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
1298       ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1299                                                     GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1300                                                                                   500));
1301     }
1302   ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1303                                                       ctx->timeout_hello,
1304                                                       &send_hello, ctx);
1305 }
1306
1307 /**
1308  * Establish a connection between two GNUnet daemons.
1309  *
1310  * @param d1 handle for the first daemon
1311  * @param d2 handle for the second daemon
1312  * @param timeout how long is the connection attempt
1313  *        allowed to take?
1314  * @param max_connect_attempts how many times should we try to reconnect
1315  *        (within timeout)
1316  * @param cb function to call at the end
1317  * @param cb_cls closure for cb
1318  */
1319 void
1320 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1321                                 struct GNUNET_TESTING_Daemon *d2,
1322                                 struct GNUNET_TIME_Relative timeout,
1323                                 unsigned int max_connect_attempts,
1324                                 GNUNET_TESTING_NotifyConnection cb,
1325                                 void *cb_cls)
1326 {
1327   struct ConnectContext *ctx;
1328
1329   if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1330     {
1331       if (NULL != cb)
1332         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1333             _("Peers are not fully running yet, can not connect!\n"));
1334       return;
1335     }
1336   ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1337   ctx->d1 = d1;
1338   ctx->d2 = d2;
1339   ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1340   ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1341   ctx->relative_timeout = timeout;
1342   ctx->cb = cb;
1343   ctx->cb_cls = cb_cls;
1344   ctx->max_connect_attempts = max_connect_attempts;
1345   ctx->connected = GNUNET_NO;
1346 #if DEBUG_TESTING
1347   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348               "Asked to connect peer %s to peer %s\n",
1349               d1->shortname, d2->shortname);
1350 #endif
1351
1352   ctx->d1core = GNUNET_CORE_connect (d1->sched,
1353                                      d1->cfg,
1354                                      timeout,
1355                                      ctx,
1356                                      NULL,
1357                                      &connect_notify, NULL,
1358                                      NULL, GNUNET_NO,
1359                                      NULL, GNUNET_NO, no_handlers);
1360   if (ctx->d1core == NULL)
1361     {
1362       GNUNET_free (ctx);
1363       if (NULL != cb)
1364         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1365             _("Failed to connect to core service of first peer!\n"));
1366       return;
1367     }
1368
1369 #if DEBUG_TESTING > 2
1370   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371               "Asked to connect peer %s to peer %s\n",
1372               d1->shortname, d2->shortname);
1373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374               "Connecting to transport service of peer %s\n", d2->shortname);
1375
1376 #endif
1377
1378   ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1379                                         d2->cfg, d2, NULL, NULL, NULL);
1380   if (ctx->d2th == NULL)
1381     {
1382       GNUNET_CORE_disconnect(ctx->d1core);
1383       GNUNET_free (ctx);
1384       if (NULL != cb)
1385         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1386             _("Failed to connect to transport service!\n"));
1387       return;
1388     }
1389
1390   ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1391                                                     GNUNET_TIME_relative_divide(ctx->relative_timeout, 
1392                                                                                 max_connect_attempts), 
1393                                                     &notify_connect_result, ctx);
1394
1395   ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1396 }
1397
1398 static void
1399 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1400 {
1401
1402   struct ConnectContext *ctx = cls;
1403   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1404     {
1405       return;
1406     }
1407 #if DEBUG_TESTING_RECONNECT
1408   GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "re-attempting connect of peer %s to peer %s\n",
1409               ctx->d1->shortname, ctx->d2->shortname);
1410 #endif
1411
1412   GNUNET_assert(ctx->d1core == NULL);
1413
1414   ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1415                                      ctx->d1->cfg,
1416                                      GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1417                                      ctx,
1418                                      NULL,
1419                                      &connect_notify, NULL,
1420                                      NULL, GNUNET_NO,
1421                                      NULL, GNUNET_NO, no_handlers);
1422   if (ctx->d1core == NULL)
1423     {
1424       if (NULL != ctx->cb)
1425         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1426                  _("Failed to connect to core service of first peer!\n"));
1427       GNUNET_free (ctx);
1428       return;
1429     }
1430
1431   ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1432                                         ctx->d2->cfg, ctx->d2, NULL, NULL, NULL);
1433   if (ctx->d2th == NULL)
1434     {
1435       GNUNET_CORE_disconnect(ctx->d1core);
1436       GNUNET_free (ctx);
1437       if (NULL != ctx->cb)
1438         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1439             _("Failed to connect to transport service!\n"));
1440       return;
1441     }
1442
1443   ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1444                                                     GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1445                                                     &notify_connect_result, ctx);
1446
1447   ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1448 }
1449
1450 /* end of testing.c */