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