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