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