use gnunet-arm instead of gnunet-service-arm
[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  * TODO:
32  * - modify configuration to allow 2087-connections from
33  *   controlling host (otherwise shutdown won't work)
34  *
35  */
36 #include "platform.h"
37 #include "gnunet_arm_service.h"
38 #include "gnunet_core_service.h"
39 #include "gnunet_constants.h"
40 #include "gnunet_testing_lib.h"
41 #include "gnunet_transport_service.h"
42
43 #define DEBUG_TESTING GNUNET_NO
44
45 /**
46  * How long do we wait after starting gnunet-service-arm
47  * for the core service to be alive?
48  */
49 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 60)
50
51 /**
52  * How many times are we willing to try to wait for "scp" or
53  * "gnunet-service-arm" to complete (waitpid) before giving up?
54  */
55 #define MAX_EXEC_WAIT_RUNS 50
56
57
58 /**
59  * Function called after GNUNET_CORE_connect has succeeded
60  * (or failed for good).  Note that the private key of the
61  * peer is intentionally not exposed here; if you need it,
62  * your process should try to read the private key file
63  * directly (which should work if you are authorized...).
64  *
65  * @param cls closure
66  * @param server handle to the server, NULL if we failed
67  * @param my_identity ID of this peer, NULL if we failed
68  * @param publicKey public key of this peer, NULL if we failed
69  */
70 static void
71 testing_init (void *cls,
72               struct GNUNET_CORE_Handle *server,
73               const struct GNUNET_PeerIdentity *my_identity,
74               const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
75 {
76   struct GNUNET_TESTING_Daemon *d = cls;
77   GNUNET_TESTING_NotifyDaemonRunning cb;
78
79   GNUNET_assert (d->phase == SP_START_CORE);
80   d->phase = SP_START_DONE;
81   cb = d->cb;
82   d->cb = NULL;
83   if (server == NULL)
84     {
85       d->server = NULL;
86       if (GNUNET_YES == d->dead)
87         GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
88       else if (NULL != cb)
89         cb (d->cb_cls, NULL, d->cfg, d,
90             _("Failed to connect to core service\n"));
91       return;
92     }
93 #if DEBUG_TESTING
94   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95               "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
96 #endif
97   d->id = *my_identity;
98   d->shortname = strdup (GNUNET_i2s (my_identity));
99   d->server = server;
100   if (GNUNET_YES == d->dead)
101     GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
102   else if (NULL != cb)
103     cb (d->cb_cls, my_identity, d->cfg, d, NULL);
104 }
105
106
107 /**
108  * Finite-state machine for starting GNUnet.
109  *
110  * @param cls our "struct GNUNET_TESTING_Daemon"
111  * @param tc unused
112  */
113 static void
114 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
115 {
116   static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
117   struct GNUNET_TESTING_Daemon *d = cls;
118   GNUNET_TESTING_NotifyDaemonRunning cb;
119   enum GNUNET_OS_ProcessStatusType type;
120   unsigned long code;
121   char *dst;
122
123 #if DEBUG_TESTING
124   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
125               "Peer FSM is in phase %u.\n", d->phase);
126 #endif
127   d->task = GNUNET_SCHEDULER_NO_TASK;
128   switch (d->phase)
129     {
130     case SP_COPYING:
131       /* confirm copying complete */
132       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
133         {
134           d->wait_runs++;
135           if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
136             {
137               cb = d->cb;
138               d->cb = NULL;
139               if (NULL != cb)
140                 cb (d->cb_cls,
141                     NULL,
142                     d->cfg, d, _("`scp' does not seem to terminate.\n"));
143               return;
144             }
145           /* wait some more */
146           d->task
147             = GNUNET_SCHEDULER_add_delayed (d->sched,
148                                             GNUNET_CONSTANTS_EXEC_WAIT,
149                                             &start_fsm, d);
150           return;
151         }
152       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
153         {
154           cb = d->cb;
155           d->cb = NULL;
156           if (NULL != cb)
157             cb (d->cb_cls,
158                 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
159           return;
160         }
161 #if DEBUG_TESTING
162       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
163                   "Successfully copied configuration file.\n");
164 #endif
165       d->phase = SP_COPIED;
166       /* fall-through */
167     case SP_COPIED:
168       /* start GNUnet on remote host */
169       if (NULL == d->hostname)
170         {
171           d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-service-arm",
172                                             "gnunet-service-arm",
173                                             "-c", d->cfgfile,
174 #if DEBUG_TESTING
175                                             "-L", "DEBUG",
176 #endif
177                                             "-d", NULL);
178         }
179       else
180         {
181           if (d->username != NULL)
182             GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
183           else
184             dst = GNUNET_strdup (d->hostname);
185           d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
186                                             "ssh",
187                                             dst,
188                                             "gnunet-arm",
189                                             "-c", d->cfgfile, "-s", NULL);
190           GNUNET_free (dst);
191         }
192       if (-1 == d->pid)
193         {
194           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
195                       _("Could not start `%s' process to start GNUnet.\n"),
196                       (NULL == d->hostname) ? "gnunet-arm" : "ssh");
197           cb = d->cb;
198           d->cb = NULL;
199           if (NULL != cb)
200             cb (d->cb_cls,
201                 NULL,
202                 d->cfg,
203                 d,
204                 (NULL == d->hostname)
205                 ? _("Failed to start `gnunet-arm' process.\n")
206                 : _("Failed to start `ssh' process.\n"));
207         }
208 #if DEBUG_TESTING
209       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
210                   "Started `%s', waiting for `%s' to be up.\n",
211                   "gnunet-arm", "gnunet-service-core");
212 #endif
213       d->phase = SP_START_ARMING;
214       d->wait_runs = 0;
215       d->task
216         = GNUNET_SCHEDULER_add_delayed (d->sched,
217                                         GNUNET_CONSTANTS_EXEC_WAIT,
218                                         &start_fsm, d);
219       break;
220     case SP_START_ARMING:
221       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
222         {
223           d->wait_runs++;
224           if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
225             {
226               cb = d->cb;
227               d->cb = NULL;
228               if (NULL != cb)
229                 cb (d->cb_cls,
230                     NULL,
231                     d->cfg,
232                     d,
233                     (NULL == d->hostname)
234                     ? _("`gnunet-arm' does not seem to terminate.\n")
235                     : _("`ssh' does not seem to terminate.\n"));
236               return;
237             }
238           /* wait some more */
239           d->task
240             = GNUNET_SCHEDULER_add_delayed (d->sched,
241                                             GNUNET_CONSTANTS_EXEC_WAIT,
242                                             &start_fsm, d);
243           return;
244         }
245 #if DEBUG_TESTING
246       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247                   "Successfully started `%s'.\n", "gnunet-arm");
248 #endif
249       d->phase = SP_START_CORE;
250       d->server = GNUNET_CORE_connect (d->sched,
251                                        d->cfg,
252                                        ARM_START_WAIT,
253                                        d,
254                                        &testing_init,
255                                        NULL, NULL, NULL,
256                                        NULL, GNUNET_NO,
257                                        NULL, GNUNET_NO, no_handlers);
258       break;
259     case SP_START_CORE:
260       GNUNET_break (0);
261       break;
262     case SP_START_DONE:
263       GNUNET_break (0);
264       break;
265     case SP_SHUTDOWN_START:
266       /* confirm copying complete */
267       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
268         {
269           d->wait_runs++;
270           if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
271             {
272               d->dead_cb (d->dead_cb_cls,
273                           _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
274               GNUNET_CONFIGURATION_destroy (d->cfg);
275               GNUNET_free (d->cfgfile);
276               GNUNET_free_non_null (d->hostname);
277               GNUNET_free_non_null (d->username);
278               GNUNET_free_non_null (d->shortname);
279               GNUNET_free (d);
280               return;
281             }
282           /* wait some more */
283           d->task
284             = GNUNET_SCHEDULER_add_delayed (d->sched,
285                                             GNUNET_CONSTANTS_EXEC_WAIT,
286                                             &start_fsm, d);
287           return;
288         }
289       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
290         {
291           if (NULL != d->dead_cb)
292             d->dead_cb (d->dead_cb_cls,
293                         _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
294           GNUNET_CONFIGURATION_destroy (d->cfg);
295           GNUNET_free (d->cfgfile);
296           GNUNET_free_non_null (d->hostname);
297           GNUNET_free_non_null (d->username);
298           GNUNET_free_non_null (d->shortname);
299           GNUNET_free (d);
300           return;
301         }
302 #if DEBUG_TESTING
303       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
304 #endif
305       /* state clean up and notifications */
306       GNUNET_CONFIGURATION_destroy (d->cfg);
307       GNUNET_free (d->cfgfile);
308       GNUNET_free_non_null (d->hostname);
309       GNUNET_free_non_null (d->username);
310       GNUNET_free_non_null (d->shortname);
311       if (NULL != d->dead_cb)
312         d->dead_cb (d->dead_cb_cls, NULL);
313       GNUNET_free (d);
314       break;
315     case SP_CONFIG_UPDATE:
316       /* confirm copying complete */
317       if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
318         {
319           d->wait_runs++;
320           if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
321             {
322               cb = d->cb;
323               d->cb = NULL;
324               if (NULL != cb)
325                 cb (d->cb_cls,
326                     NULL,
327                     d->cfg, d, _("`scp' does not seem to terminate.\n"));
328               return;
329             }
330           /* wait some more */
331           d->task
332             = GNUNET_SCHEDULER_add_delayed (d->sched,
333                                             GNUNET_CONSTANTS_EXEC_WAIT,
334                                             &start_fsm, d);
335           return;
336         }
337       if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
338         {
339           if (NULL != d->update_cb)
340             d->update_cb (d->update_cb_cls,
341                           _("`scp' did not complete cleanly.\n"));
342           return;
343         }
344 #if DEBUG_TESTING
345       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346                   "Successfully copied configuration file.\n");
347 #endif
348       if (NULL != d->update_cb)
349         d->update_cb (d->update_cb_cls, NULL);
350       d->phase = SP_START_DONE;
351       break;
352     }
353 }
354
355
356 /**
357  * Starts a GNUnet daemon.  GNUnet must be installed on the target
358  * system and available in the PATH.  The machine must furthermore be
359  * reachable via "ssh" (unless the hostname is "NULL") without the
360  * need to enter a password.
361  *
362  * @param sched scheduler to use
363  * @param cfg configuration to use
364  * @param hostname name of the machine where to run GNUnet
365  *        (use NULL for localhost).
366  * @param cb function to call with the result
367  * @param cb_cls closure for cb
368  * @return handle to the daemon (actual start will be completed asynchronously)
369  */
370 struct GNUNET_TESTING_Daemon *
371 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
372                              const struct GNUNET_CONFIGURATION_Handle *cfg,
373                              const char *hostname,
374                              GNUNET_TESTING_NotifyDaemonRunning cb,
375                              void *cb_cls)
376 {
377   struct GNUNET_TESTING_Daemon *ret;
378   char *arg;
379   char *username;
380
381   ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
382   ret->sched = sched;
383   ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
384   ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
385 #if DEBUG_TESTING
386   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
387               "Setting up peer with configuration file `%s'.\n",
388               ret->cfgfile);
389 #endif
390   if (NULL == ret->cfgfile)
391     {
392       GNUNET_free_non_null (ret->hostname);
393       GNUNET_free (ret);
394       return NULL;
395     }
396   ret->cb = cb;
397   ret->cb_cls = cb_cls;
398   ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
399   GNUNET_CONFIGURATION_set_value_string (ret->cfg,
400                                          "PATHS",
401                                          "DEFAULTCONFIG", ret->cfgfile);
402   /* 1) write configuration to temporary file */
403   if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
404     {
405       if (0 != UNLINK (ret->cfgfile))
406         GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
407                                   "unlink", ret->cfgfile);
408       GNUNET_CONFIGURATION_destroy (ret->cfg);
409       GNUNET_free_non_null (ret->hostname);
410       GNUNET_free (ret->cfgfile);
411       GNUNET_free (ret);
412       return NULL;
413     }
414   if (GNUNET_OK !=
415       GNUNET_CONFIGURATION_get_value_string (cfg,
416                                              "TESTING",
417                                              "USERNAME", &username))
418     {
419       if (NULL != getenv ("USER"))
420         username = GNUNET_strdup (getenv ("USER"));
421       else
422         username = NULL;
423     }
424   ret->username = username;
425
426   /* 2) copy file to remote host */
427   if (NULL != hostname)
428     {
429 #if DEBUG_TESTING
430       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
431                   "Copying configuration file to host `%s'.\n", hostname);
432 #endif
433       ret->phase = SP_COPYING;
434       if (NULL != username)
435         GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
436       else
437         GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
438       ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
439                                           "scp", ret->cfgfile, arg, NULL);
440       GNUNET_free (arg);
441       if (-1 == ret->pid)
442         {
443           GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
444                       _
445                       ("Could not start `%s' process to copy configuration file.\n"),
446                       "scp");
447           if (0 != UNLINK (ret->cfgfile))
448             GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
449                                       "unlink", ret->cfgfile);
450           GNUNET_CONFIGURATION_destroy (ret->cfg);
451           GNUNET_free_non_null (ret->hostname);
452           GNUNET_free_non_null (ret->username);
453           GNUNET_free (ret->cfgfile);
454           GNUNET_free (ret);
455           return NULL;
456         }
457       ret->task
458         = GNUNET_SCHEDULER_add_delayed (sched,
459                                         GNUNET_CONSTANTS_EXEC_WAIT,
460                                         &start_fsm, ret);
461       return ret;
462     }
463 #if DEBUG_TESTING
464   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
465               "No need to copy configuration file since we are running locally.\n");
466 #endif
467   ret->phase = SP_COPIED;
468   GNUNET_SCHEDULER_add_continuation (sched,
469                                      &start_fsm,
470                                      ret,
471                                      GNUNET_SCHEDULER_REASON_PREREQ_DONE);
472   return ret;
473 }
474
475
476 /**
477  * Stops a GNUnet daemon.
478  *
479  * @param d the daemon that should be stopped
480  * @param cb function called once the daemon was stopped
481  * @param cb_cls closure for cb
482  */
483 void
484 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
485                             GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
486 {
487   char *arg;
488
489   d->dead_cb = cb;
490   d->dead_cb_cls = cb_cls;
491
492   if (NULL != d->cb)
493     {
494       d->dead = GNUNET_YES;
495       return;
496     }
497   if (d->phase == SP_CONFIG_UPDATE)
498     {
499       GNUNET_SCHEDULER_cancel (d->sched, d->task);
500       d->phase = SP_START_DONE;
501     }
502   if (d->server != NULL)
503     {
504       GNUNET_CORE_disconnect (d->server);
505       d->server = NULL;
506     }
507   /* shutdown ARM process (will terminate others) */
508 #if DEBUG_TESTING
509   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510               _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
511   /* sleep(15); Manual check for running */
512 #endif
513
514   d->phase = SP_SHUTDOWN_START;
515
516   /* Check if this is a local or remote process */
517   if (NULL != d->hostname)
518     {
519 #if DEBUG_TESTING
520       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
521                   "Stopping gnunet-arm on with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
522 #endif
523
524       if (d->username != NULL)
525         GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
526       else
527         arg = GNUNET_strdup (d->hostname);
528
529       d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
530                                               arg, "gnunet-arm",
531                                               "-c", d->cfgfile, "-e", "-d", NULL);
532       /* Use -e to end arm, and -d to remove temp files */
533
534       GNUNET_free (arg);
535     }
536   else
537   {
538 #if DEBUG_TESTING
539       GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
540                   "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
541 #endif
542     d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
543                                             "gnunet-arm",
544                                             "-c", d->cfgfile, "-e", NULL);
545   }
546
547   d->wait_runs = 0;
548   d->task
549     = GNUNET_SCHEDULER_add_delayed (d->sched,
550                                     GNUNET_CONSTANTS_EXEC_WAIT,
551                                     &start_fsm, d);
552   return;
553 }
554
555
556 /**
557  * Changes the configuration of a GNUnet daemon.
558  *
559  * @param d the daemon that should be modified
560  * @param cfg the new configuration for the daemon
561  * @param cb function called once the configuration was changed
562  * @param cb_cls closure for cb
563  */
564 void
565 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
566                                    struct GNUNET_CONFIGURATION_Handle *cfg,
567                                    GNUNET_TESTING_NotifyCompletion cb,
568                                    void *cb_cls)
569 {
570   char *arg;
571
572   if (d->phase != SP_START_DONE)
573     {
574       if (NULL != cb)
575         cb (cb_cls,
576             _
577             ("Peer not yet running, can not change configuration at this point."));
578       return;
579     }
580
581   /* 1) write configuration to temporary file */
582   if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
583     {
584       if (NULL != cb)
585         cb (cb_cls, _("Failed to write new configuration to disk."));
586       return;
587     }
588
589   /* 2) copy file to remote host (if necessary) */
590   if (NULL == d->hostname)
591     {
592       /* signal success */
593       if (NULL != cb)
594         cb (cb_cls, NULL);
595       return;
596     }
597 #if DEBUG_TESTING
598   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599               "Copying updated configuration file to remote host `%s'.\n",
600               d->hostname);
601 #endif
602   d->phase = SP_CONFIG_UPDATE;
603   if (NULL != d->username)
604     GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
605   else
606     GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
607   d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
608   GNUNET_free (arg);
609   if (-1 == d->pid)
610     {
611       GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
612                   _
613                   ("Could not start `%s' process to copy configuration file.\n"),
614                   "scp");
615       if (NULL != cb)
616         cb (cb_cls, _("Failed to copy new configuration to remote machine."));
617       d->phase = SP_START_DONE;
618       return;
619     }
620   d->update_cb = cb;
621   d->update_cb_cls = cb_cls;
622   d->task
623     = GNUNET_SCHEDULER_add_delayed (d->sched,
624                                     GNUNET_CONSTANTS_EXEC_WAIT,
625                                     &start_fsm, d);
626 }
627
628
629 /**
630  * Data kept for each pair of peers that we try
631  * to connect.
632  */
633 struct ConnectContext
634 {
635   /**
636    * Testing handle to the first daemon.
637    */
638   struct GNUNET_TESTING_Daemon *d1;
639
640   /**
641    * Handle to core of first daemon (to check connect)
642    */
643   struct GNUNET_CORE_Handle * d1core;
644
645   /**
646    * Testing handle to the second daemon.
647    */
648   struct GNUNET_TESTING_Daemon *d2;
649
650   /**
651    * Handle to core of second daemon (to check connect)
652    */
653   struct GNUNET_CORE_Handle * d2core;
654
655   /**
656    * Transport handle to the first daemon.
657    */
658   struct GNUNET_TRANSPORT_Handle *d1th;
659
660   /**
661    * Transport handle to the second daemon.
662    */
663   struct GNUNET_TRANSPORT_Handle *d2th;
664
665   /**
666    * Function to call once we are done (or have timed out).
667    */
668   GNUNET_TESTING_NotifyConnection cb;
669
670   /**
671    * Closure for "nb".
672    */
673   void *cb_cls;
674
675   /**
676    * Transmit handle for our request for transmission
677    * (as given to d2 asking to talk to d1).
678    */
679   struct GNUNET_CORE_TransmitHandle *ntr;
680
681   /**
682    * When should this operation be complete (or we must trigger
683    * a timeout).
684    */
685   struct GNUNET_TIME_Absolute timeout;
686
687   /**
688    * Hello timeout task
689    */
690   GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
691
692   /**
693    * Connect timeout task
694    */
695   GNUNET_SCHEDULER_TaskIdentifier timeout_task;
696
697   /**
698    * When should this operation be complete (or we must trigger
699    * a timeout).
700    */
701   struct GNUNET_TIME_Relative timeout_hello;
702
703   /**
704    * The current hello message we have (for d1)
705    */
706   struct GNUNET_MessageHeader *hello;
707
708   /**
709    * Was the connection successful?
710    */
711   int connected;
712 };
713
714
715 /**
716  * Receive the HELLO from one peer, give it to the other
717  * and ask them to connect.
718  *
719  * @param cls "struct ConnectContext"
720  * @param message HELLO message of peer
721  */
722 static void
723 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
724 {
725   struct ConnectContext *ctx = cls;
726
727 #if DEBUG_TESTING
728   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
729               "Received `%s' from transport service of `%4s'\n",
730               "HELLO", GNUNET_i2s (&ctx->d1->id));
731 #endif
732
733   GNUNET_assert (message != NULL);
734   GNUNET_free_non_null(ctx->hello);
735   ctx->hello = GNUNET_malloc(ntohs(message->size));
736   memcpy(ctx->hello, message, ntohs(message->size));
737
738 }
739
740
741 /**
742  * Notify callback about success or failure of the attempt
743  * to connect the two peers
744  *
745  * @param cls our "struct ConnectContext" (freed)
746  * @param tc reason tells us if we succeeded or failed
747  */
748 static void
749 notify_connect_result (void *cls,
750                        const struct GNUNET_SCHEDULER_TaskContext *tc)
751 {
752   struct ConnectContext *ctx = cls;
753
754   GNUNET_TRANSPORT_get_hello_cancel (ctx->d1th, &process_hello, ctx);
755   GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
756
757   if (ctx->cb != NULL)
758     {
759       if (ctx->connected == GNUNET_NO)
760         {
761           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
762                   ctx->d2->cfg, ctx->d1, ctx->d2,
763                   _("Peers failed to connect"));
764         }
765       else
766         {
767           ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
768                    ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
769           GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
770         }
771     }
772
773   ctx->ntr = NULL;
774   GNUNET_TRANSPORT_disconnect (ctx->d1th);
775   ctx->d1th = NULL;
776   GNUNET_TRANSPORT_disconnect (ctx->d2th);
777   ctx->d2th = NULL;
778   GNUNET_CORE_disconnect (ctx->d1core);
779   ctx->d1core = NULL;
780   GNUNET_free_non_null (ctx->hello);
781   GNUNET_free (ctx);
782 }
783
784
785 /**
786  * Success, connection is up.  Signal client our success.
787  *
788  * @param cls our "struct ConnectContext"
789  * @param peer identity of the peer that has connected
790  * @param latency the round trip latency of the connection to this peer
791  * @param distance distance the transport level distance to this peer
792  *
793  */
794 static void
795 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
796                 uint32_t distance)
797 {
798   struct ConnectContext *ctx = cls;
799
800 #if DEBUG_TESTING
801   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802               "Core notified us about connection to a peer\n");
803 #endif
804   if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
805     {
806 #if DEBUG_TESTING
807   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808               "Core notified us about connection to peer %s\n", GNUNET_i2s(peer));
809 #endif
810       /*
811        * If we disconnect here, then the hello may never get sent (if it was delayed!)
812        * However I'm sure there was a reason it was here... so I'm just commenting.
813        */
814       ctx->connected = GNUNET_YES;
815       GNUNET_SCHEDULER_add_now (ctx->d1->sched,
816                                 &notify_connect_result,
817                                 ctx);
818     }
819
820 }
821
822 static void
823 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
824 {
825   struct ConnectContext *ctx = cls;
826
827   if (ctx->hello != NULL)
828     {
829       GNUNET_TRANSPORT_offer_hello (ctx->d2th, ctx->hello);
830       ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
831                                                     GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
832                                                                                   200));
833     }
834   ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
835                                                       ctx->timeout_hello,
836                                                       &send_hello, ctx);
837 }
838
839
840 /**
841  * Establish a connection between two GNUnet daemons.
842  *
843  * @param d1 handle for the first daemon
844  * @param d2 handle for the second daemon
845  * @param timeout how long is the connection attempt
846  *        allowed to take?
847  * @param cb function to call at the end
848  * @param cb_cls closure for cb
849  */
850 void
851 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
852                                 struct GNUNET_TESTING_Daemon *d2,
853                                 struct GNUNET_TIME_Relative timeout,
854                                 GNUNET_TESTING_NotifyConnection cb,
855                                 void *cb_cls)
856 {
857   struct ConnectContext *ctx;
858   static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
859
860   if ((d1->server == NULL) || (d2->server == NULL))
861     {
862       if (NULL != cb)
863         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
864             _("Peers are not fully running yet, can not connect!\n"));
865       return;
866     }
867   ctx = GNUNET_malloc (sizeof (struct ConnectContext));
868   ctx->d1 = d1;
869   ctx->d2 = d2;
870   ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
871   ctx->cb = cb;
872   ctx->cb_cls = cb_cls;
873   ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 400);
874   ctx->connected = GNUNET_NO;
875 #if DEBUG_TESTING
876   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
877               "Asked to connect peer %s to peer %s\n",
878               d1->shortname, d2->shortname);
879   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
880               "Connecting to transport service of peer %s\n", d1->shortname);
881 #endif
882
883   ctx->d1core = GNUNET_CORE_connect (d1->sched,
884                                      d1->cfg,
885                                      timeout,
886                                      ctx,
887                                      NULL,
888                                      NULL, &connect_notify, NULL,
889                                      NULL, GNUNET_NO,
890                                      NULL, GNUNET_NO, no_handlers);
891   if (ctx->d1core == NULL)
892     {
893       GNUNET_free (ctx);
894       if (NULL != cb)
895         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
896             _("Failed to connect to core service of first peer!\n"));
897       return;
898     }
899
900   ctx->d1th = GNUNET_TRANSPORT_connect (d1->sched,
901                                         d1->cfg, d1, NULL, NULL, NULL);
902   if (ctx->d1th == NULL)
903     {
904       GNUNET_free (ctx);
905       if (NULL != cb)
906         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
907             _("Failed to connect to transport service!\n"));
908       return;
909     }
910 #if DEBUG_TESTING
911   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
912               "Asked to connect peer %s to peer %s\n",
913               d1->shortname, d2->shortname);
914   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
915               "Connecting to transport service of peer %s\n", d2->shortname);
916
917 #endif
918
919   ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
920                                         d2->cfg, d2, NULL, NULL, NULL);
921   if (ctx->d2th == NULL)
922     {
923       GNUNET_TRANSPORT_disconnect (ctx->d1th);
924       GNUNET_free (ctx);
925       if (NULL != cb)
926         cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
927             _("Failed to connect to transport service!\n"));
928       return;
929     }
930
931 #if DEBUG_TESTING
932   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
933               "Asking for HELLO from peer %s\n", GNUNET_i2s (&d1->id));
934 #endif
935
936   ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
937                                                     timeout,
938                                                     &notify_connect_result, ctx);
939
940   GNUNET_TRANSPORT_get_hello (ctx->d1th, &process_hello, ctx);
941   ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched, ctx->timeout_hello,
942                                                       &send_hello, ctx);
943 }
944
945
946 /* end of testing.c */