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