comment fix
[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_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   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       GNUNET_free_non_null (d->shortname);
593       if (NULL != d->dead_cb)
594         d->dead_cb (d->dead_cb_cls, NULL);
595
596       GNUNET_free (d);
597       break;
598     case SP_CONFIG_UPDATE:
599       /* confirm copying complete */
600       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
601         {
602           if (GNUNET_TIME_absolute_get_remaining(d->max_timeout).value == 0) /* FIXME: config update should take timeout parameter! */
603             {
604               cb = d->cb;
605               d->cb = NULL;
606               if (NULL != cb)
607                 cb (d->cb_cls,
608                     NULL,
609                     d->cfg, d, _("`scp' does not seem to terminate.\n"));
610               return;
611             }
612           /* wait some more */
613           d->task
614             = GNUNET_SCHEDULER_add_delayed (d->sched,
615                                             GNUNET_CONSTANTS_EXEC_WAIT,
616                                             &start_fsm, d);
617           return;
618         }
619       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
620         {
621           if (NULL != d->update_cb)
622             d->update_cb (d->update_cb_cls,
623                           _("`scp' did not complete cleanly.\n"));
624           return;
625         }
626 #if DEBUG_TESTING
627       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
628                   "Successfully copied configuration file.\n");
629 #endif
630       if (NULL != d->update_cb)
631         d->update_cb (d->update_cb_cls, NULL);
632       d->phase = SP_START_DONE;
633       break;
634     }
635 }
636
637 /**
638  * Continues GNUnet daemon startup when user wanted to be notified
639  * once a hostkey was generated (for creating friends files, blacklists,
640  * etc.).
641  *
642  * @param daemon the daemon to finish starting
643  */
644 void
645 GNUNET_TESTING_daemon_continue_startup(struct GNUNET_TESTING_Daemon *daemon)
646 {
647   GNUNET_assert(daemon->phase == SP_HOSTKEY_CREATED);
648   daemon->phase = SP_TOPOLOGY_SETUP;
649 }
650
651
652 /**
653  * Start a peer that has previously been stopped using the daemon_stop
654  * call (and files weren't deleted and the allow restart flag)
655  *
656  * @param daemon the daemon to start (has been previously stopped)
657  * @param timeout how long to wait for restart
658  * @param cb the callback for notification when the peer is running
659  * @param cb_cls closure for the callback
660  */
661 void
662 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
663                                      struct GNUNET_TIME_Relative timeout,
664                                      GNUNET_TESTING_NotifyDaemonRunning cb,
665                                      void *cb_cls)
666 {
667   if (daemon->running == GNUNET_YES)
668   {
669     cb(cb_cls, &daemon->id, daemon->cfg, daemon, "Daemon already running, can't restart!");
670     return;
671   }
672
673   daemon->cb = cb;
674   daemon->cb_cls = cb_cls;
675   daemon->phase = SP_TOPOLOGY_SETUP;
676   daemon->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
677
678   GNUNET_SCHEDULER_add_continuation (daemon->sched,
679                                      &start_fsm,
680                                      daemon,
681                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
682 }
683
684 /**
685  * Starts a GNUnet daemon.  GNUnet must be installed on the target
686  * system and available in the PATH.  The machine must furthermore be
687  * reachable via "ssh" (unless the hostname is "NULL") without the
688  * need to enter a password.
689  *
690  * @param sched scheduler to use
691  * @param cfg configuration to use
692  * @param timeout how long to wait starting up peers
693  * @param hostname name of the machine where to run GNUnet
694  *        (use NULL for localhost).
695  * @param hostkey_callback function to call once the hostkey has been
696  *        generated for this peer, but it hasn't yet been started
697  *        (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
698  * @param hostkey_cls closure for hostkey callback
699  * @param cb function to call with the result
700  * @param cb_cls closure for cb
701  * @return handle to the daemon (actual start will be completed asynchronously)
702  */
703 struct GNUNET_TESTING_Daemon *
704 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
705                              const struct GNUNET_CONFIGURATION_Handle *cfg,
706                              struct GNUNET_TIME_Relative timeout,
707                              const char *hostname,
708                              GNUNET_TESTING_NotifyHostkeyCreated hostkey_callback,
709                              void *hostkey_cls,
710                              GNUNET_TESTING_NotifyDaemonRunning cb,
711                              void *cb_cls)
712 {
713   struct GNUNET_TESTING_Daemon *ret;
714   char *arg;
715   char *username;
716
717   ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
718   ret->sched = sched;
719   ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
720   ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
721 #if DEBUG_TESTING
722   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
723               "Setting up peer with configuration file `%s'.\n",
724               ret->cfgfile);
725 #endif
726   if (NULL == ret->cfgfile)
727     {
728       GNUNET_free_non_null (ret->hostname);
729       GNUNET_free (ret);
730       return NULL;
731     }
732   ret->hostkey_callback = hostkey_callback;
733   ret->hostkey_cls = hostkey_cls;
734   ret->cb = cb;
735   ret->cb_cls = cb_cls;
736   ret->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
737   ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
738   GNUNET_CONFIGURATION_set_value_string (ret->cfg,
739                                          "PATHS",
740                                          "DEFAULTCONFIG", ret->cfgfile);
741   /* 1) write configuration to temporary file */
742   if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
743     {
744       if (0 != UNLINK (ret->cfgfile))
745         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
746                                   "unlink", ret->cfgfile);
747       GNUNET_CONFIGURATION_destroy (ret->cfg);
748       GNUNET_free_non_null (ret->hostname);
749       GNUNET_free (ret->cfgfile);
750       GNUNET_free (ret);
751       return NULL;
752     }
753   if (GNUNET_OK !=
754       GNUNET_CONFIGURATION_get_value_string (cfg,
755                                              "TESTING",
756                                              "USERNAME", &username))
757     {
758       if (NULL != getenv ("USER"))
759         username = GNUNET_strdup (getenv ("USER"));
760       else
761         username = NULL;
762     }
763   ret->username = username;
764
765   /* 2) copy file to remote host */
766   if (NULL != hostname)
767     {
768 #if DEBUG_TESTING
769       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
770                   "Copying configuration file to host `%s'.\n", hostname);
771 #endif
772       ret->phase = SP_COPYING;
773       if (NULL != username)
774         GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
775       else
776         GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
777       ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
778                                           "scp", ret->cfgfile, arg, NULL);
779       GNUNET_free (arg);
780       if (-1 == ret->pid)
781         {
782           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
783                       _
784                       ("Could not start `%s' process to copy configuration file.\n"),
785                       "scp");
786           if (0 != UNLINK (ret->cfgfile))
787             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
788                                       "unlink", ret->cfgfile);
789           GNUNET_CONFIGURATION_destroy (ret->cfg);
790           GNUNET_free_non_null (ret->hostname);
791           GNUNET_free_non_null (ret->username);
792           GNUNET_free (ret->cfgfile);
793           GNUNET_free (ret);
794           return NULL;
795         }
796       ret->task
797         = GNUNET_SCHEDULER_add_delayed (sched,
798                                         GNUNET_CONSTANTS_EXEC_WAIT,
799                                         &start_fsm, ret);
800       return ret;
801     }
802 #if DEBUG_TESTING
803   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
804               "No need to copy configuration file since we are running locally.\n");
805 #endif
806   ret->phase = SP_COPIED;
807   GNUNET_SCHEDULER_add_continuation (sched,
808                                      &start_fsm,
809                                      ret,
810                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
811   return ret;
812 }
813
814
815 /**
816  * Restart (stop and start) a GNUnet daemon.
817  *
818  * @param d the daemon that should be restarted
819  * @param cb function called once the daemon is (re)started
820  * @param cb_cls closure for cb
821  */
822 void
823 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
824                                GNUNET_TESTING_NotifyDaemonRunning cb, void *cb_cls)
825 {
826   char *arg;
827   char *del_arg;
828
829   del_arg = NULL;
830   if (NULL != d->cb)
831     {
832       d->dead = GNUNET_YES;
833       return;
834     }
835
836   d->cb = cb;
837   d->cb_cls = cb_cls;
838
839   if (d->phase == SP_CONFIG_UPDATE)
840     {
841       GNUNET_SCHEDULER_cancel (d->sched, d->task);
842       d->phase = SP_START_DONE;
843     }
844   if (d->server != NULL)
845     {
846       GNUNET_CORE_disconnect (d->server);
847       d->server = NULL;
848     }
849
850   if (d->th != NULL)
851     {
852       GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
853       GNUNET_TRANSPORT_disconnect(d->th);
854       d->th = NULL;
855     }
856   /* state clean up and notifications */
857   GNUNET_free_non_null(d->hello);
858
859 #if DEBUG_TESTING
860     GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861                 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
862 #endif
863
864    d->phase = SP_START_ARMING;
865
866     /* Check if this is a local or remote process */
867   if (NULL != d->hostname)
868     {
869 #if DEBUG_TESTING
870       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871                   "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
872 #endif
873
874       if (d->username != NULL)
875         GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
876       else
877         arg = GNUNET_strdup (d->hostname);
878
879       d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
880                                         arg, "gnunet-arm",
881 #if DEBUG_TESTING
882                                         "-L", "DEBUG",
883 #endif
884                                         "-c", d->cfgfile, "-e", "-r", NULL);
885       /* Use -r to restart arm and all services */
886
887       GNUNET_free (arg);
888     }
889   else
890     {
891 #if DEBUG_TESTING
892       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
893                   "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
894 #endif
895       d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
896                                         "gnunet-arm",
897 #if DEBUG_TESTING
898                                         "-L", "DEBUG",
899 #endif
900                                         "-c", d->cfgfile, "-e", "-r", NULL);
901     }
902
903     GNUNET_free_non_null(del_arg);
904     d->task
905       = GNUNET_SCHEDULER_add_delayed (d->sched,
906                                       GNUNET_CONSTANTS_EXEC_WAIT,
907                                       &start_fsm, d);
908
909 }
910
911
912 /**
913  * Stops a GNUnet daemon.
914  *
915  * @param d the daemon that should be stopped
916  * @param timeout how long to wait for process for shutdown to complete
917  * @param cb function called once the daemon was stopped
918  * @param cb_cls closure for cb
919  * @param delete_files GNUNET_YES to remove files, GNUNET_NO
920  *        to leave them
921  * @param allow_restart GNUNET_YES to restart peer later (using this API)
922  *        GNUNET_NO to kill off and clean up for good
923  */
924 void
925 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
926                             struct GNUNET_TIME_Relative timeout,
927                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
928                             int delete_files, int allow_restart)
929 {
930   char *arg;
931   char *del_arg;
932   d->dead_cb = cb;
933   d->dead_cb_cls = cb_cls;
934
935   if (NULL != d->cb)
936     {
937 #if DEBUG_TESTING
938       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
939                  _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
940 #endif
941       d->dead = GNUNET_YES;
942       return;
943     }
944
945   del_arg = NULL;
946   if (delete_files == GNUNET_YES)
947     {
948       GNUNET_asprintf(&del_arg, "-d");
949     }
950
951   if (d->phase == SP_CONFIG_UPDATE)
952     {
953       GNUNET_SCHEDULER_cancel (d->sched, d->task);
954       d->phase = SP_START_DONE;
955     }
956   if (d->server != NULL)
957     {
958       GNUNET_CORE_disconnect (d->server);
959       d->server = NULL;
960     }
961   /* shutdown ARM process (will terminate others) */
962 #if DEBUG_TESTING
963   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964               _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
965 #endif
966
967   d->phase = SP_SHUTDOWN_START;
968   d->running = GNUNET_NO;
969
970   if (allow_restart == GNUNET_YES)
971     d->churn = GNUNET_YES;
972   if (d->th != NULL)
973     {
974       GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
975       GNUNET_TRANSPORT_disconnect(d->th);
976       d->th = NULL;
977     }
978   /* Check if this is a local or remote process */
979   if (NULL != d->hostname)
980     {
981 #if DEBUG_TESTING
982       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983                   "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
984 #endif
985
986       if (d->username != NULL)
987         GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
988       else
989         arg = GNUNET_strdup (d->hostname);
990
991       d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
992                                         arg, "gnunet-arm",
993 #if DEBUG_TESTING
994                                         "-L", "DEBUG",
995 #endif
996                                         "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
997       /* Use -e to end arm, and -d to remove temp files */
998
999       GNUNET_free (arg);
1000     }
1001   else
1002     {
1003 #if DEBUG_TESTING
1004       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005                   "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1006 #endif
1007       d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1008                                         "gnunet-arm",
1009 #if DEBUG_TESTING
1010                                         "-L", "DEBUG",
1011 #endif
1012                                         "-c", d->cfgfile, "-e", "-q", del_arg, NULL);
1013     }
1014
1015   GNUNET_free_non_null(del_arg);
1016   d->max_timeout = GNUNET_TIME_relative_to_absolute(timeout);
1017   d->task
1018     = GNUNET_SCHEDULER_add_delayed (d->sched,
1019                                     GNUNET_CONSTANTS_EXEC_WAIT,
1020                                     &start_fsm, d);
1021 }
1022
1023
1024 /**
1025  * Changes the configuration of a GNUnet daemon.
1026  *
1027  * @param d the daemon that should be modified
1028  * @param cfg the new configuration for the daemon
1029  * @param cb function called once the configuration was changed
1030  * @param cb_cls closure for cb
1031  */
1032 void
1033 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1034                                    struct GNUNET_CONFIGURATION_Handle *cfg,
1035                                    GNUNET_TESTING_NotifyCompletion cb,
1036                                    void *cb_cls)
1037 {
1038   char *arg;
1039
1040   if (d->phase != SP_START_DONE)
1041     {
1042       if (NULL != cb)
1043         cb (cb_cls,
1044             _
1045             ("Peer not yet running, can not change configuration at this point."));
1046       return;
1047     }
1048
1049   /* 1) write configuration to temporary file */
1050   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1051     {
1052       if (NULL != cb)
1053         cb (cb_cls, _("Failed to write new configuration to disk."));
1054       return;
1055     }
1056
1057   /* 2) copy file to remote host (if necessary) */
1058   if (NULL == d->hostname)
1059     {
1060       /* signal success */
1061       if (NULL != cb)
1062         cb (cb_cls, NULL);
1063       return;
1064     }
1065 #if DEBUG_TESTING
1066   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1067               "Copying updated configuration file to remote host `%s'.\n",
1068               d->hostname);
1069 #endif
1070   d->phase = SP_CONFIG_UPDATE;
1071   if (NULL != d->username)
1072     GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1073   else
1074     GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1075   d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
1076   GNUNET_free (arg);
1077   if (-1 == d->pid)
1078     {
1079       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1080                   _
1081                   ("Could not start `%s' process to copy configuration file.\n"),
1082                   "scp");
1083       if (NULL != cb)
1084         cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1085       d->phase = SP_START_DONE;
1086       return;
1087     }
1088   d->update_cb = cb;
1089   d->update_cb_cls = cb_cls;
1090   d->task
1091     = GNUNET_SCHEDULER_add_delayed (d->sched,
1092                                     GNUNET_CONSTANTS_EXEC_WAIT,
1093                                     &start_fsm, d);
1094 }
1095
1096
1097 /**
1098  * Data kept for each pair of peers that we try
1099  * to connect.
1100  */
1101 struct ConnectContext
1102 {
1103   /**
1104    * Testing handle to the first daemon.
1105    */
1106   struct GNUNET_TESTING_Daemon *d1;
1107
1108   /**
1109    * Handle to core of first daemon (to check connect)
1110    */
1111   struct GNUNET_CORE_Handle * d1core;
1112
1113   /**
1114    * Testing handle to the second daemon.
1115    */
1116   struct GNUNET_TESTING_Daemon *d2;
1117
1118   /**
1119    * Transport handle to the second daemon.
1120    */
1121   struct GNUNET_TRANSPORT_Handle *d2th;
1122
1123   /**
1124    * Function to call once we are done (or have timed out).
1125    */
1126   GNUNET_TESTING_NotifyConnection cb;
1127
1128   /**
1129    * Closure for "nb".
1130    */
1131   void *cb_cls;
1132
1133   /**
1134    * When should this operation be complete (or we must trigger
1135    * a timeout).
1136    */
1137   struct GNUNET_TIME_Absolute timeout;
1138
1139   /**
1140    * The relative timeout from whence this connect attempt was
1141    * started.  Allows for reconnect attempts.
1142    */
1143   struct GNUNET_TIME_Relative relative_timeout;
1144
1145   /**
1146    * Maximum number of connect attempts, will retry connection
1147    * this number of times on failures.
1148    */
1149   unsigned int max_connect_attempts;
1150
1151   /**
1152    * Hello timeout task
1153    */
1154   GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1155
1156   /**
1157    * Connect timeout task
1158    */
1159   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1160
1161   /**
1162    * When should this operation be complete (or we must trigger
1163    * a timeout).
1164    */
1165   struct GNUNET_TIME_Relative timeout_hello;
1166
1167
1168   /**
1169    * Was the connection attempt successful?
1170    */
1171   int connected;
1172 };
1173
1174
1175 /** Forward declaration **/
1176 static void
1177 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
1178
1179
1180 /**
1181  * Notify callback about success or failure of the attempt
1182  * to connect the two peers
1183  *
1184  * @param cls our "struct ConnectContext" (freed)
1185  * @param tc reason tells us if we succeeded or failed
1186  */
1187 static void
1188 notify_connect_result (void *cls,
1189                        const struct GNUNET_SCHEDULER_TaskContext *tc)
1190 {
1191   struct ConnectContext *ctx = cls;
1192   struct GNUNET_TIME_Relative remaining;
1193
1194   if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1195     {
1196       GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
1197       ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1198     }
1199
1200   if ((ctx->timeout_task != GNUNET_SCHEDULER_NO_TASK) && (tc->reason != GNUNET_SCHEDULER_REASON_TIMEOUT))
1201     {
1202       GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1203       ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1204     }
1205
1206   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1207     {
1208       if (ctx->d2th != NULL)
1209         GNUNET_TRANSPORT_disconnect (ctx->d2th);
1210       ctx->d2th = NULL;
1211       if (ctx->d1core != NULL)
1212         GNUNET_CORE_disconnect (ctx->d1core);
1213
1214       ctx->d1core = NULL;
1215       GNUNET_free (ctx);
1216       return;
1217     }
1218
1219   remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
1220
1221   if (ctx->connected == GNUNET_YES)
1222     {
1223       if (ctx->cb != NULL)
1224         {
1225           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1226                    ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1227         }
1228     }
1229   else if (remaining.value > 0)
1230     {
1231       if (ctx->d1core != NULL)
1232         {
1233           GNUNET_CORE_disconnect(ctx->d1core);
1234           ctx->d1core = NULL;
1235         }
1236
1237       if (ctx->d2th != NULL)
1238         {
1239           GNUNET_TRANSPORT_disconnect(ctx->d2th);
1240           ctx->d2th = NULL;
1241         }
1242       GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
1243       return;
1244     }
1245   else
1246     {
1247       if (ctx->cb != NULL)
1248         {
1249           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
1250                    ctx->d2->cfg, ctx->d1, ctx->d2,
1251                    _("Peers failed to connect"));
1252         }
1253     }
1254
1255   GNUNET_TRANSPORT_disconnect (ctx->d2th);
1256   ctx->d2th = NULL;
1257   GNUNET_CORE_disconnect (ctx->d1core);
1258   ctx->d1core = NULL;
1259   GNUNET_free (ctx);
1260 }
1261
1262
1263 /**
1264  * Success, connection is up.  Signal client our success.
1265  *
1266  * @param cls our "struct ConnectContext"
1267  * @param peer identity of the peer that has connected
1268  * @param latency the round trip latency of the connection to this peer
1269  * @param distance distance the transport level distance to this peer
1270  *
1271  */
1272 static void
1273 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
1274                 uint32_t distance)
1275 {
1276   struct ConnectContext *ctx = cls;
1277
1278   if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
1279     {
1280       ctx->connected = GNUNET_YES;
1281       GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
1282       ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1283       GNUNET_SCHEDULER_add_now (ctx->d1->sched,
1284                                 &notify_connect_result,
1285                                 ctx);
1286     }
1287
1288 }
1289
1290 static void
1291 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1292 {
1293   struct ConnectContext *ctx = cls;
1294
1295   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1296     return;
1297
1298   if (ctx->d1->hello != NULL)
1299     {
1300       GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
1301       ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
1302                                                     GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
1303                                                                                   500));
1304     }
1305   ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
1306                                                       ctx->timeout_hello,
1307                                                       &send_hello, ctx);
1308 }
1309
1310 /**
1311  * Establish a connection between two GNUnet daemons.
1312  *
1313  * @param d1 handle for the first daemon
1314  * @param d2 handle for the second daemon
1315  * @param timeout how long is the connection attempt
1316  *        allowed to take?
1317  * @param max_connect_attempts how many times should we try to reconnect
1318  *        (within timeout)
1319  * @param cb function to call at the end
1320  * @param cb_cls closure for cb
1321  */
1322 void
1323 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1324                                 struct GNUNET_TESTING_Daemon *d2,
1325                                 struct GNUNET_TIME_Relative timeout,
1326                                 unsigned int max_connect_attempts,
1327                                 GNUNET_TESTING_NotifyConnection cb,
1328                                 void *cb_cls)
1329 {
1330   struct ConnectContext *ctx;
1331
1332   if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1333     {
1334       if (NULL != cb)
1335         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1336             _("Peers are not fully running yet, can not connect!\n"));
1337       return;
1338     }
1339   ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1340   ctx->d1 = d1;
1341   ctx->d2 = d2;
1342   ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1343   ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
1344   ctx->relative_timeout = timeout;
1345   ctx->cb = cb;
1346   ctx->cb_cls = cb_cls;
1347   ctx->max_connect_attempts = max_connect_attempts;
1348   ctx->connected = GNUNET_NO;
1349 #if DEBUG_TESTING
1350   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351               "Asked to connect peer %s to peer %s\n",
1352               d1->shortname, d2->shortname);
1353 #endif
1354
1355   ctx->d1core = GNUNET_CORE_connect (d1->sched,
1356                                      d1->cfg,
1357                                      timeout,
1358                                      ctx,
1359                                      NULL,
1360                                      &connect_notify, NULL,
1361                                      NULL, GNUNET_NO,
1362                                      NULL, GNUNET_NO, no_handlers);
1363   if (ctx->d1core == NULL)
1364     {
1365       GNUNET_free (ctx);
1366       if (NULL != cb)
1367         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1368             _("Failed to connect to core service of first peer!\n"));
1369       return;
1370     }
1371
1372 #if DEBUG_TESTING > 2
1373   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1374               "Asked to connect peer %s to peer %s\n",
1375               d1->shortname, d2->shortname);
1376   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1377               "Connecting to transport service of peer %s\n", d2->shortname);
1378
1379 #endif
1380
1381   ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
1382                                         d2->cfg, d2, NULL, NULL, NULL);
1383   if (ctx->d2th == NULL)
1384     {
1385       GNUNET_CORE_disconnect(ctx->d1core);
1386       GNUNET_free (ctx);
1387       if (NULL != cb)
1388         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
1389             _("Failed to connect to transport service!\n"));
1390       return;
1391     }
1392
1393   ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
1394                                                     GNUNET_TIME_relative_divide(ctx->relative_timeout, max_connect_attempts), /* Allow up to 8 reconnect attempts */
1395                                                     &notify_connect_result, ctx);
1396
1397   ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1398 }
1399
1400 static void
1401 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1402 {
1403
1404   struct ConnectContext *ctx = cls;
1405   if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1406     {
1407       return;
1408     }
1409 #if DEBUG_TESTING_RECONNECT
1410   GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "re-attempting connect of peer %s to peer %s\n",
1411               ctx->d1->shortname, ctx->d2->shortname);
1412 #endif
1413
1414   GNUNET_assert(ctx->d1core == NULL);
1415
1416   ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1417                                      ctx->d1->cfg,
1418                                      GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1419                                      ctx,
1420                                      NULL,
1421                                      &connect_notify, NULL,
1422                                      NULL, GNUNET_NO,
1423                                      NULL, GNUNET_NO, no_handlers);
1424   if (ctx->d1core == NULL)
1425     {
1426       if (NULL != ctx->cb)
1427         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1428                  _("Failed to connect to core service of first peer!\n"));
1429       GNUNET_free (ctx);
1430       return;
1431     }
1432
1433   ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1434                                         ctx->d2->cfg, ctx->d2, NULL, NULL, NULL);
1435   if (ctx->d2th == NULL)
1436     {
1437       GNUNET_CORE_disconnect(ctx->d1core);
1438       GNUNET_free (ctx);
1439       if (NULL != ctx->cb)
1440         ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1441             _("Failed to connect to transport service!\n"));
1442       return;
1443     }
1444
1445   ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1446                                                     GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1447                                                     &notify_connect_result, ctx);
1448
1449   ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1450 }
1451
1452 /* end of testing.c */