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