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