2 This file is part of GNUnet
3 (C) 2008, 2009 Christian Grothoff (and other contributing authors)
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.
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.
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.
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
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"
40 #define DEBUG_TESTING GNUNET_NO
42 #define DEBUG_TESTING_RECONNECT GNUNET_NO
44 #define WAIT_FOR_HELLO GNUNET_NO
47 * Hack to deal with initial HELLO's being often devoid of addresses.
48 * This hack causes 'process_hello' to ignore HELLOs without addresses.
49 * The correct implementation would continue with 'process_hello' until
50 * the connection could be established...
52 #define EMPTY_HACK GNUNET_YES
55 * How long do we wait after starting gnunet-service-arm
56 * for the core service to be alive?
58 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
61 * How many times are we willing to try to wait for "scp" or
62 * "gnunet-service-arm" to complete (waitpid) before giving up?
64 #define MAX_EXEC_WAIT_RUNS 250
66 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
70 test_address (void *cls, const char *tname,
71 struct GNUNET_TIME_Absolute expiration, const void *addr,
82 * Receive the HELLO from one peer, give it to the other
83 * and ask them to connect.
85 * @param cls "struct ConnectContext"
86 * @param message HELLO message of peer
89 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
91 struct GNUNET_TESTING_Daemon *daemon = cls;
95 GNUNET_TESTING_NotifyDaemonRunning cb;
101 GNUNET_assert (message != NULL);
102 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) message,
103 GNUNET_NO, &test_address, &empty);
104 if (GNUNET_YES == empty)
107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Skipping empty HELLO address\n");
115 GNUNET_assert (daemon->phase == SP_GET_HELLO ||
116 daemon->phase == SP_START_DONE);
121 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
122 GNUNET_SCHEDULER_cancel (daemon->task);
124 if (daemon->server != NULL)
127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
128 "Received `%s' from transport service of `%4s', disconnecting core!\n",
129 "HELLO", GNUNET_i2s (&daemon->id));
131 GNUNET_CORE_disconnect (daemon->server);
132 daemon->server = NULL;
135 msize = ntohs (message->size);
140 if (daemon->ghh != NULL)
142 GNUNET_TRANSPORT_get_hello_cancel (daemon->ghh);
145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
146 "Received `%s' from transport service of `%4s'\n", "HELLO",
147 GNUNET_i2s (&daemon->id));
150 GNUNET_free_non_null (daemon->hello);
151 daemon->hello = GNUNET_malloc (msize);
152 memcpy (daemon->hello, message, msize);
154 if (daemon->th != NULL)
156 GNUNET_TRANSPORT_disconnect (daemon->th);
159 daemon->phase = SP_START_DONE;
162 if (NULL != cb) /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
163 cb (daemon->cb_cls, &daemon->id, daemon->cfg, daemon, NULL);
168 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
172 * Function called after GNUNET_CORE_connect has succeeded
173 * (or failed for good). Note that the private key of the
174 * peer is intentionally not exposed here; if you need it,
175 * your process should try to read the private key file
176 * directly (which should work if you are authorized...).
179 * @param server handle to the server, NULL if we failed
180 * @param my_identity ID of this peer, NULL if we failed
181 * @param publicKey public key of this peer, NULL if we failed
184 testing_init (void *cls, struct GNUNET_CORE_Handle *server,
185 const struct GNUNET_PeerIdentity *my_identity,
186 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
188 struct GNUNET_TESTING_Daemon *d = cls;
190 GNUNET_assert (d->phase == SP_START_CORE);
191 d->phase = SP_GET_HELLO;
196 if (GNUNET_YES == d->dead)
197 GNUNET_TESTING_daemon_stop (d,
198 GNUNET_TIME_absolute_get_remaining
199 (d->max_timeout), d->dead_cb, d->dead_cb_cls,
200 GNUNET_YES, GNUNET_NO);
201 else if (NULL != d->cb)
202 d->cb (d->cb_cls, NULL, d->cfg, d,
203 _("Failed to connect to core service\n"));
207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully started peer `%4s'.\n",
208 GNUNET_i2s (my_identity));
210 d->id = *my_identity; /* FIXME: shouldn't we already have this from reading the hostkey file? */
211 if (d->shortname == NULL)
212 d->shortname = strdup (GNUNET_i2s (my_identity));
214 d->running = GNUNET_YES;
216 if (GNUNET_NO == d->running)
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Peer is dead (d->running == GNUNET_NO)\n");
225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
226 "Successfully started peer `%4s', connecting to transport service.\n",
227 GNUNET_i2s (my_identity));
230 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
233 if (GNUNET_YES == d->dead)
234 GNUNET_TESTING_daemon_stop (d,
235 GNUNET_TIME_absolute_get_remaining
236 (d->max_timeout), d->dead_cb, d->dead_cb_cls,
237 GNUNET_YES, GNUNET_NO);
238 else if (NULL != d->cb)
239 d->cb (d->cb_cls, &d->id, d->cfg, d,
240 _("Failed to connect to transport service!\n"));
244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
245 "Connected to transport service `%s', getting HELLO\n",
246 GNUNET_i2s (my_identity));
249 d->ghh = GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
251 if (d->task != GNUNET_SCHEDULER_NO_TASK)
252 GNUNET_SCHEDULER_cancel (d->task);
254 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
260 * Notify of a peer being up and running. Scheduled as a task
261 * so that variables which may need to be set are set before
262 * the connect callback can set up new operations.
264 * @param cls the testing daemon
265 * @param tc task scheduler context
268 notify_daemon_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
270 struct GNUNET_TESTING_Daemon *d = cls;
271 GNUNET_TESTING_NotifyDaemonRunning cb;
276 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
281 * Finite-state machine for starting GNUnet.
283 * @param cls our "struct GNUNET_TESTING_Daemon"
287 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
289 struct GNUNET_TESTING_Daemon *d = cls;
290 GNUNET_TESTING_NotifyDaemonRunning cb;
291 enum GNUNET_OS_ProcessStatusType type;
297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer FSM is in phase %u.\n", d->phase);
300 d->task = GNUNET_SCHEDULER_NO_TASK;
304 /* confirm copying complete */
305 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
307 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
312 cb (d->cb_cls, NULL, d->cfg, d,
314 ("`scp' does not seem to terminate (timeout copying config).\n"));
319 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
323 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
328 cb (d->cb_cls, NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
331 GNUNET_OS_process_close (d->proc);
333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
334 "Successfully copied configuration file.\n");
336 d->phase = SP_COPIED;
339 /* Start create hostkey process if we don't already know the peer identity! */
340 if (GNUNET_NO == d->have_hostkey)
342 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
343 if (d->pipe_stdout == NULL)
348 cb (d->cb_cls, NULL, d->cfg, d,
351 _("Failed to create pipe for `gnunet-peerinfo' process.\n") :
352 _("Failed to create pipe for `ssh' process.\n"));
355 if (NULL == d->hostname)
358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
359 "Starting `%s', with command `%s %s %s %s'.\n",
360 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
364 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
365 "gnunet-peerinfo", "-c", d->cfgfile, "-sq",
367 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
371 if (d->username != NULL)
372 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
374 dst = GNUNET_strdup (d->hostname);
377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
378 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
379 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
382 if (d->ssh_port_str == NULL)
384 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh",
388 dst, "gnunet-peerinfo", "-c",
389 d->cfgfile, "-sq", NULL);
394 GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh", "-p",
399 dst, "gnunet-peerinfo", "-c", d->cfgfile,
402 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
408 _("Could not start `%s' process to create hostkey.\n"),
409 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
413 cb (d->cb_cls, NULL, d->cfg, d,
415 d->hostname) ? _("Failed to start `gnunet-peerinfo' process.\n")
416 : _("Failed to start `ssh' process.\n"));
417 GNUNET_DISK_pipe_close (d->pipe_stdout);
421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
422 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
424 d->phase = SP_HOSTKEY_CREATE;
426 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
428 GNUNET_DISK_pipe_handle
430 GNUNET_DISK_PIPE_END_READ),
433 else /* Already have a hostkey! */
435 if (d->hostkey_callback != NULL)
437 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
438 d->hostkey_callback = NULL;
439 d->phase = SP_HOSTKEY_CREATED;
442 d->phase = SP_TOPOLOGY_SETUP;
445 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
448 case SP_HOSTKEY_CREATE:
450 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
451 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
452 &d->hostkeybuf[d->hostkeybufpos],
453 sizeof (d->hostkeybuf) - d->hostkeybufpos);
455 d->hostkeybufpos += bytes_read;
457 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
461 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
463 GNUNET_DISK_pipe_handle
465 GNUNET_DISK_PIPE_END_READ),
469 d->hostkeybuf[103] = '\0';
471 if ((bytes_read < 0) ||
473 GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey)))
477 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
478 _("Error reading from gnunet-peerinfo: %s\n"),
481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
482 _("Malformed output from gnunet-peerinfo!\n"));
485 GNUNET_DISK_pipe_close (d->pipe_stdout);
486 d->pipe_stdout = NULL;
487 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
488 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
489 GNUNET_OS_process_close (d->proc);
492 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
495 d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
496 GNUNET_DISK_pipe_close (d->pipe_stdout);
497 d->pipe_stdout = NULL;
498 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
499 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
500 GNUNET_OS_process_close (d->proc);
502 d->have_hostkey = GNUNET_YES;
503 if (d->hostkey_callback != NULL)
505 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
506 d->hostkey_callback = NULL;
507 d->phase = SP_HOSTKEY_CREATED;
511 d->phase = SP_TOPOLOGY_SETUP;
514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
517 case SP_HOSTKEY_CREATED:
518 /* wait for topology finished */
519 if ((GNUNET_YES == d->dead) ||
520 (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
525 cb (d->cb_cls, NULL, d->cfg, d,
526 _("`Failed while waiting for topology setup!\n"));
531 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
534 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
535 /* start GNUnet on remote host */
536 if (NULL == d->hostname)
539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
540 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
541 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-L", "DEBUG",
545 GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm", "-c",
551 GNUNET_TIME_relative_to_string
552 (GNUNET_TIME_absolute_get_remaining
553 (d->max_timeout)), NULL);
557 if (d->username != NULL)
558 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
560 dst = GNUNET_strdup (d->hostname);
563 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
564 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
565 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
566 "-L", "DEBUG", "-s", "-q");
568 if (d->ssh_port_str == NULL)
570 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
578 "-c", d->cfgfile, "-s", "-q", "-T",
579 GNUNET_TIME_relative_to_string
580 (GNUNET_TIME_absolute_get_remaining
581 (d->max_timeout)), NULL);
587 GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-p",
596 "-c", d->cfgfile, "-s", "-q", "-T",
597 GNUNET_TIME_relative_to_string
598 (GNUNET_TIME_absolute_get_remaining
599 (d->max_timeout)), NULL);
605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
606 _("Could not start `%s' process to start GNUnet.\n"),
607 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
611 cb (d->cb_cls, NULL, d->cfg, d,
613 d->hostname) ? _("Failed to start `gnunet-arm' process.\n") :
614 _("Failed to start `ssh' process.\n"));
618 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
619 "Started `%s', waiting for `%s' to be up.\n", "gnunet-arm",
620 "gnunet-service-core");
622 d->phase = SP_START_ARMING;
624 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
627 case SP_START_ARMING:
628 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
630 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
635 cb (d->cb_cls, NULL, d->cfg, d,
637 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
638 _("`ssh' does not seem to terminate.\n"));
639 GNUNET_CONFIGURATION_destroy (d->cfg);
640 GNUNET_free (d->cfgfile);
641 GNUNET_free_non_null (d->hostname);
642 GNUNET_free_non_null (d->username);
643 GNUNET_free (d->proc);
649 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
654 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Successfully started `%s'.\n",
657 GNUNET_free (d->proc);
658 d->phase = SP_START_CORE;
660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Calling CORE_connect\n");
664 if (d->server != NULL)
665 GNUNET_CORE_disconnect (d->server);
668 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
673 cb (d->cb_cls, NULL, d->cfg, d,
674 _("Unable to connect to CORE service for peer!\n"));
675 GNUNET_CONFIGURATION_destroy (d->cfg);
676 GNUNET_free (d->cfgfile);
677 GNUNET_free_non_null (d->hostname);
678 GNUNET_free_non_null (d->username);
683 GNUNET_CORE_connect (d->cfg, 1, d, &testing_init, NULL, NULL, NULL,
684 NULL, GNUNET_NO, NULL, GNUNET_NO, no_handlers);
686 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
687 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
690 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
693 if (GNUNET_YES == d->dead)
694 GNUNET_TESTING_daemon_stop (d,
695 GNUNET_TIME_absolute_get_remaining
696 (d->max_timeout), d->dead_cb,
697 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
698 else if (NULL != d->cb)
699 d->cb (d->cb_cls, &d->id, d->cfg, d,
700 _("Failed to connect to transport service!\n"));
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
705 "Connected to transport service `%s', getting HELLO\n",
706 GNUNET_i2s (&d->id));
709 d->ghh = GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
710 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
714 * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
715 d->running = GNUNET_YES;
716 d->phase = SP_GET_HELLO;
720 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
722 if (d->server != NULL)
723 GNUNET_CORE_disconnect (d->server);
725 GNUNET_TRANSPORT_disconnect (d->th);
729 cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
730 GNUNET_CONFIGURATION_destroy (d->cfg);
731 GNUNET_free (d->cfgfile);
732 GNUNET_free_non_null (d->hostname);
733 GNUNET_free_non_null (d->username);
737 if (d->hello != NULL)
739 GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
741 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
742 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
748 case SP_SERVICE_START:
749 /* confirm gnunet-arm exited */
750 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
752 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
757 cb (d->cb_cls, NULL, d->cfg, d,
759 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
760 _("`ssh' does not seem to terminate.\n"));
765 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
770 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
775 cb (d->cb_cls, NULL, d->cfg, d,
779 ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
780 : _("`ssh' does not seem to terminate.\n"));
785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
789 d->phase = SP_START_DONE;
791 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
793 case SP_SERVICE_SHUTDOWN_START:
794 /* confirm copying complete */
795 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
797 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
799 if (NULL != d->dead_cb)
800 d->dead_cb (d->dead_cb_cls,
802 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
807 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
812 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
814 if (NULL != d->dead_cb)
815 d->dead_cb (d->dead_cb_cls,
817 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
822 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
824 if (NULL != d->dead_cb)
825 d->dead_cb (d->dead_cb_cls, NULL);
827 case SP_SHUTDOWN_START:
828 /* confirm copying complete */
829 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
831 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
833 if (NULL != d->dead_cb)
834 d->dead_cb (d->dead_cb_cls,
836 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
839 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
841 GNUNET_TRANSPORT_disconnect (d->th);
844 GNUNET_CONFIGURATION_destroy (d->cfg);
845 GNUNET_free (d->cfgfile);
846 GNUNET_free_non_null (d->hello);
847 GNUNET_free_non_null (d->hostname);
848 GNUNET_free_non_null (d->username);
849 GNUNET_free_non_null (d->shortname);
850 GNUNET_free_non_null (d->proc);
857 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
861 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
863 if (NULL != d->dead_cb)
864 d->dead_cb (d->dead_cb_cls,
866 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
869 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
871 GNUNET_TRANSPORT_disconnect (d->th);
874 if (d->server != NULL)
876 GNUNET_CORE_disconnect (d->server);
879 GNUNET_CONFIGURATION_destroy (d->cfg);
880 GNUNET_free (d->cfgfile);
881 GNUNET_free_non_null (d->hello);
882 GNUNET_free_non_null (d->hostname);
883 GNUNET_free_non_null (d->username);
884 GNUNET_free_non_null (d->shortname);
885 GNUNET_free_non_null (d->proc);
891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
893 if (d->server != NULL)
895 GNUNET_CORE_disconnect (d->server);
901 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
903 GNUNET_TRANSPORT_disconnect (d->th);
907 if (NULL != d->dead_cb)
908 d->dead_cb (d->dead_cb_cls, NULL);
910 /* state clean up and notifications */
911 if (d->churn == GNUNET_NO)
913 GNUNET_CONFIGURATION_destroy (d->cfg);
914 GNUNET_free (d->cfgfile);
915 GNUNET_free_non_null (d->hostname);
916 GNUNET_free_non_null (d->username);
919 GNUNET_free_non_null (d->hello);
921 GNUNET_free_non_null (d->shortname);
922 GNUNET_free_non_null (d->proc);
925 if (d->churn == GNUNET_NO)
929 case SP_CONFIG_UPDATE:
930 /* confirm copying complete */
931 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
933 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
938 cb (d->cb_cls, NULL, d->cfg, d,
939 _("`scp' does not seem to terminate.\n"));
944 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
948 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
950 if (NULL != d->update_cb)
951 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
956 "Successfully copied configuration file.\n");
958 if (NULL != d->update_cb)
959 d->update_cb (d->update_cb_cls, NULL);
960 d->phase = SP_START_DONE;
966 * Continues GNUnet daemon startup when user wanted to be notified
967 * once a hostkey was generated (for creating friends files, blacklists,
970 * @param daemon the daemon to finish starting
973 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
975 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
976 daemon->phase = SP_TOPOLOGY_SETUP;
980 * Check whether the given daemon is running.
982 * @param daemon the daemon to check
984 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
985 * daemon is down, GNUNET_SYSERR on error.
988 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
991 return GNUNET_SYSERR;
993 if (daemon->running == GNUNET_YES)
1000 * Starts a GNUnet daemon service which has been previously stopped.
1002 * @param d the daemon for which the service should be started
1003 * @param service the name of the service to start
1004 * @param timeout how long to wait for process for shutdown to complete
1005 * @param cb function called once the service starts
1006 * @param cb_cls closure for cb
1009 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
1011 struct GNUNET_TIME_Relative
1013 GNUNET_TESTING_NotifyDaemonRunning
1021 GNUNET_assert (d->running == GNUNET_YES);
1023 if (d->phase == SP_CONFIG_UPDATE)
1025 GNUNET_SCHEDULER_cancel (d->task);
1026 d->phase = SP_START_DONE;
1029 if (d->churned_services == NULL)
1031 d->cb (d->cb_cls, &d->id, d->cfg, d,
1032 "No service has been churned off yet!!");
1035 d->phase = SP_SERVICE_START;
1036 GNUNET_free (d->churned_services);
1037 d->churned_services = NULL;
1038 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1039 /* Check if this is a local or remote process */
1040 if (NULL != d->hostname)
1043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1044 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1045 d->cfgfile, d->hostname);
1048 if (d->username != NULL)
1049 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1051 arg = GNUNET_strdup (d->hostname);
1053 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1061 "-c", d->cfgfile, "-i", service, "-q",
1063 GNUNET_TIME_relative_to_string (timeout),
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
1067 arg, "gnunet-arm", d->cfgfile, service);
1073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1074 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1076 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1080 "-c", d->cfgfile, "-i", service, "-q",
1082 GNUNET_TIME_relative_to_string (timeout),
1086 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1087 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1091 * Starts a GNUnet daemon's service.
1093 * @param d the daemon for which the service should be started
1094 * @param service the name of the service to start
1095 * @param timeout how long to wait for process for startup
1096 * @param cb function called once gnunet-arm returns
1097 * @param cb_cls closure for cb
1100 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
1102 struct GNUNET_TIME_Relative timeout,
1103 GNUNET_TESTING_NotifyDaemonRunning cb,
1111 GNUNET_assert (service != NULL);
1112 GNUNET_assert (d->running == GNUNET_YES);
1113 GNUNET_assert (d->phase == SP_START_DONE);
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 _("Starting service %s for peer `%4s'\n"), service,
1118 GNUNET_i2s (&d->id));
1121 d->phase = SP_SERVICE_START;
1122 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1123 /* Check if this is a local or remote process */
1124 if (NULL != d->hostname)
1127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1128 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1129 d->cfgfile, d->hostname);
1132 if (d->username != NULL)
1133 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1135 arg = GNUNET_strdup (d->hostname);
1137 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1145 "-c", d->cfgfile, "-i", service, "-q",
1147 GNUNET_TIME_relative_to_string (timeout),
1149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1150 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
1151 arg, "gnunet-arm", d->cfgfile, service,
1152 GNUNET_TIME_relative_to_string (timeout));
1158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1159 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1161 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1165 "-c", d->cfgfile, "-i", service, "-q",
1167 GNUNET_TIME_relative_to_string (timeout),
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1171 "gnunet-arm", d->cfgfile, service,
1172 GNUNET_TIME_relative_to_string (timeout));
1175 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1176 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1180 * Start a peer that has previously been stopped using the daemon_stop
1181 * call (and files weren't deleted and the allow restart flag)
1183 * @param daemon the daemon to start (has been previously stopped)
1184 * @param timeout how long to wait for restart
1185 * @param cb the callback for notification when the peer is running
1186 * @param cb_cls closure for the callback
1189 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1190 struct GNUNET_TIME_Relative timeout,
1191 GNUNET_TESTING_NotifyDaemonRunning cb,
1194 if (daemon->running == GNUNET_YES)
1196 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1197 "Daemon already running, can't restart!");
1202 daemon->cb_cls = cb_cls;
1203 daemon->phase = SP_TOPOLOGY_SETUP;
1204 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1206 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1207 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1211 * Starts a GNUnet daemon. GNUnet must be installed on the target
1212 * system and available in the PATH. The machine must furthermore be
1213 * reachable via "ssh" (unless the hostname is "NULL") without the
1214 * need to enter a password.
1216 * @param cfg configuration to use
1217 * @param timeout how long to wait starting up peers
1218 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1219 * to really start the peer (default)
1220 * @param hostname name of the machine where to run GNUnet
1221 * (use NULL for localhost).
1222 * @param ssh_username ssh username to use when connecting to hostname
1223 * @param sshport port to pass to ssh process when connecting to hostname
1224 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1225 * @param hostkey_callback function to call once the hostkey has been
1226 * generated for this peer, but it hasn't yet been started
1227 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1228 * @param hostkey_cls closure for hostkey callback
1229 * @param cb function to call once peer is up, or failed to start
1230 * @param cb_cls closure for cb
1231 * @return handle to the daemon (actual start will be completed asynchronously)
1233 struct GNUNET_TESTING_Daemon *
1234 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1235 struct GNUNET_TIME_Relative timeout, int pretend,
1236 const char *hostname, const char *ssh_username,
1237 uint16_t sshport, const char *hostkey,
1238 GNUNET_TESTING_NotifyHostkeyCreated
1239 hostkey_callback, void *hostkey_cls,
1240 GNUNET_TESTING_NotifyDaemonRunning cb,
1243 struct GNUNET_TESTING_Daemon *ret;
1247 char *baseservicehome;
1250 char *temp_file_name;
1251 struct GNUNET_DISK_FileHandle *fn;
1252 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1253 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1255 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1256 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1259 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1262 ret->ssh_port_str = NULL;
1264 /* Find service home and base service home directories, create it if it doesn't exist */
1265 GNUNET_assert (GNUNET_OK ==
1266 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1270 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1271 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1272 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1273 GNUNET_free (temp_file_name);
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1278 if (NULL == ret->cfgfile)
1280 GNUNET_free_non_null (ret->ssh_port_str);
1281 GNUNET_free_non_null (ret->hostname);
1285 ret->hostkey_callback = hostkey_callback;
1286 ret->hostkey_cls = hostkey_cls;
1288 ret->cb_cls = cb_cls;
1289 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1290 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1291 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1294 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1296 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1297 GNUNET_assert (private_key != NULL);
1298 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1299 GNUNET_CRYPTO_hash (&public_key,
1300 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1301 &ret->id.hashPubKey);
1302 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1303 ret->have_hostkey = GNUNET_YES;
1304 GNUNET_CRYPTO_rsa_key_free (private_key);
1307 /* Write hostkey to file, if we were given one */
1309 if (hostkey != NULL)
1311 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1312 fn = GNUNET_DISK_file_open (hostkeyfile,
1313 GNUNET_DISK_OPEN_READWRITE |
1314 GNUNET_DISK_OPEN_CREATE,
1315 GNUNET_DISK_PERM_USER_READ |
1316 GNUNET_DISK_PERM_USER_WRITE);
1317 GNUNET_assert (fn != NULL);
1318 GNUNET_assert (HOSTKEYFILESIZE ==
1319 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1320 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1323 /* write configuration to temporary file */
1324 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1326 if (0 != UNLINK (ret->cfgfile))
1327 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1329 GNUNET_CONFIGURATION_destroy (ret->cfg);
1330 GNUNET_free_non_null (ret->hostname);
1331 GNUNET_free (ret->cfgfile);
1335 if (ssh_username != NULL)
1336 username = GNUNET_strdup (ssh_username);
1337 if ((ssh_username == NULL) &&
1339 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1342 if (NULL != getenv ("USER"))
1343 username = GNUNET_strdup (getenv ("USER"));
1347 ret->username = username;
1349 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1351 /* copy directory to remote host */
1352 if (NULL != hostname)
1355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1356 "Copying configuration directory to host `%s'.\n", hostname);
1358 baseservicehome = GNUNET_strdup (servicehome);
1359 /* Remove trailing /'s */
1360 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1361 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1362 /* Find next directory /, jump one ahead */
1363 slash = strrchr (baseservicehome, '/');
1367 ret->phase = SP_COPYING;
1368 if (NULL != username)
1369 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1371 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1372 GNUNET_free (baseservicehome);
1373 if (ret->ssh_port_str == NULL)
1375 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1379 servicehome, arg, NULL);
1381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1382 "copying directory with command scp -r %s %s\n",
1389 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r", "-P",
1394 servicehome, arg, NULL);
1397 if (NULL == ret->proc)
1399 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1401 ("Could not start `%s' process to copy configuration directory.\n"),
1403 if (0 != UNLINK (ret->cfgfile))
1404 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1406 GNUNET_CONFIGURATION_destroy (ret->cfg);
1407 GNUNET_free_non_null (ret->hostname);
1408 GNUNET_free_non_null (ret->username);
1409 GNUNET_free (ret->cfgfile);
1411 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1412 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1414 GNUNET_free_non_null (hostkeyfile);
1415 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1416 GNUNET_free (servicehome);
1421 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1423 GNUNET_free_non_null (hostkeyfile);
1424 GNUNET_free (servicehome);
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "No need to copy configuration file since we are running locally.\n");
1431 ret->phase = SP_COPIED;
1432 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1433 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1435 GNUNET_free_non_null (hostkeyfile);
1436 GNUNET_free (servicehome);
1442 * Restart (stop and start) a GNUnet daemon.
1444 * @param d the daemon that should be restarted
1445 * @param cb function called once the daemon is (re)started
1446 * @param cb_cls closure for cb
1449 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1450 GNUNET_TESTING_NotifyDaemonRunning cb,
1459 d->dead = GNUNET_YES;
1466 if (d->phase == SP_CONFIG_UPDATE)
1468 GNUNET_SCHEDULER_cancel (d->task);
1469 d->phase = SP_START_DONE;
1471 if (d->server != NULL)
1473 GNUNET_CORE_disconnect (d->server);
1479 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1481 GNUNET_TRANSPORT_disconnect (d->th);
1484 /* state clean up and notifications */
1485 GNUNET_free_non_null (d->hello);
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1489 GNUNET_i2s (&d->id));
1492 d->phase = SP_START_ARMING;
1494 /* Check if this is a local or remote process */
1495 if (NULL != d->hostname)
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1499 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1500 d->cfgfile, d->hostname);
1503 if (d->username != NULL)
1504 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1506 arg = GNUNET_strdup (d->hostname);
1508 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1516 "-c", d->cfgfile, "-e", "-r", NULL);
1517 /* Use -r to restart arm and all services */
1524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1525 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1527 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1531 "-c", d->cfgfile, "-e", "-r", NULL);
1534 GNUNET_free_non_null (del_arg);
1536 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1542 * Stops a GNUnet daemon.
1544 * @param d the daemon that should be stopped
1545 * @param service the name of the service to stop
1546 * @param timeout how long to wait for process for shutdown to complete
1547 * @param cb function called once the daemon was stopped
1548 * @param cb_cls closure for cb
1549 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1551 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1552 * GNUNET_NO to kill off and clean up for good
1555 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1557 struct GNUNET_TIME_Relative timeout,
1558 GNUNET_TESTING_NotifyCompletion cb,
1564 d->dead_cb_cls = cb_cls;
1566 GNUNET_assert (d->running == GNUNET_YES);
1568 if (d->phase == SP_CONFIG_UPDATE)
1570 GNUNET_SCHEDULER_cancel (d->task);
1571 d->phase = SP_START_DONE;
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1576 GNUNET_i2s (&d->id));
1578 if (d->churned_services != NULL)
1580 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1583 d->phase = SP_SERVICE_SHUTDOWN_START;
1584 d->churned_services = GNUNET_strdup (service);
1585 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1586 /* Check if this is a local or remote process */
1587 if (NULL != d->hostname)
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1592 d->cfgfile, d->hostname);
1595 if (d->username != NULL)
1596 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1598 arg = GNUNET_strdup (d->hostname);
1600 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1608 "-c", d->cfgfile, "-k", service, "-q",
1610 GNUNET_TIME_relative_to_string (timeout),
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1613 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1614 arg, "gnunet-arm", d->cfgfile, service);
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1621 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1623 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1627 "-c", d->cfgfile, "-k", service, "-q",
1629 GNUNET_TIME_relative_to_string (timeout),
1633 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1634 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1639 * Stops a GNUnet daemon.
1641 * @param d the daemon that should be stopped
1642 * @param timeout how long to wait for process for shutdown to complete
1643 * @param cb function called once the daemon was stopped
1644 * @param cb_cls closure for cb
1645 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1647 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1648 * GNUNET_NO to kill off and clean up for good
1651 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1652 struct GNUNET_TIME_Relative timeout,
1653 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1654 int delete_files, int allow_restart)
1660 d->dead_cb_cls = cb_cls;
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1666 GNUNET_i2s (&d->id));
1668 d->dead = GNUNET_YES;
1672 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1674 /* Free what was left from churning! */
1675 GNUNET_assert (d->cfg != NULL);
1676 GNUNET_CONFIGURATION_destroy (d->cfg);
1677 if (delete_files == GNUNET_YES)
1679 if (0 != UNLINK (d->cfgfile))
1681 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1684 GNUNET_free (d->cfgfile);
1685 GNUNET_free_non_null (d->hostname);
1686 GNUNET_free_non_null (d->username);
1687 if (NULL != d->dead_cb)
1688 d->dead_cb (d->dead_cb_cls, NULL);
1694 if (delete_files == GNUNET_YES)
1696 GNUNET_asprintf (&del_arg, "-d");
1699 if (d->phase == SP_CONFIG_UPDATE)
1701 GNUNET_SCHEDULER_cancel (d->task);
1702 d->phase = SP_START_DONE;
1704 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1705 if (d->server != NULL)
1707 GNUNET_CORE_disconnect (d->server);
1711 /* shutdown ARM process (will terminate others) */
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1714 GNUNET_i2s (&d->id));
1716 d->phase = SP_SHUTDOWN_START;
1717 d->running = GNUNET_NO;
1718 if (allow_restart == GNUNET_YES)
1719 d->churn = GNUNET_YES;
1722 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1724 GNUNET_TRANSPORT_disconnect (d->th);
1727 /* Check if this is a local or remote process */
1728 if (NULL != d->hostname)
1731 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1732 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1733 d->cfgfile, d->hostname);
1736 if (d->username != NULL)
1737 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1739 arg = GNUNET_strdup (d->hostname);
1741 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1749 "-c", d->cfgfile, "-e", "-q", "-T",
1750 GNUNET_TIME_relative_to_string (timeout),
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1753 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1754 arg, "gnunet-arm", d->cfgfile, del_arg);
1755 /* Use -e to end arm, and -d to remove temp files */
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1764 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1768 "-c", d->cfgfile, "-e", "-q", "-T",
1769 GNUNET_TIME_relative_to_string (timeout),
1773 GNUNET_free_non_null (del_arg);
1774 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1775 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1780 * Changes the configuration of a GNUnet daemon.
1782 * @param d the daemon that should be modified
1783 * @param cfg the new configuration for the daemon
1784 * @param cb function called once the configuration was changed
1785 * @param cb_cls closure for cb
1788 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1789 struct GNUNET_CONFIGURATION_Handle *cfg,
1790 GNUNET_TESTING_NotifyCompletion cb,
1795 if (d->phase != SP_START_DONE)
1800 ("Peer not yet running, can not change configuration at this point."));
1804 /* 1) write configuration to temporary file */
1805 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1808 cb (cb_cls, _("Failed to write new configuration to disk."));
1812 /* 2) copy file to remote host (if necessary) */
1813 if (NULL == d->hostname)
1815 /* signal success */
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "Copying updated configuration file to remote host `%s'.\n",
1825 d->phase = SP_CONFIG_UPDATE;
1826 if (NULL != d->username)
1827 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1829 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1830 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1834 d->cfgfile, arg, NULL);
1836 if (NULL == d->proc)
1838 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1839 _("Could not start `%s' process to copy configuration file.\n"),
1842 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1843 d->phase = SP_START_DONE;
1847 d->update_cb_cls = cb_cls;
1849 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1854 * Data kept for each pair of peers that we try
1857 struct ConnectContext
1860 * Testing handle to the first daemon.
1862 struct GNUNET_TESTING_Daemon *d1;
1865 * Handle to core of first daemon (to check connect)
1867 struct GNUNET_CORE_Handle *d1core;
1870 * Have we actually connected to the core of the first daemon yet?
1875 * Testing handle to the second daemon.
1877 struct GNUNET_TESTING_Daemon *d2;
1880 * Handler for the request to core to connect to this peer.
1882 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1885 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1887 struct GNUNET_TRANSPORT_Handle *d1th;
1890 * Function to call once we are done (or have timed out).
1892 GNUNET_TESTING_NotifyConnection cb;
1900 * The relative timeout from whence this connect attempt was
1901 * started. Allows for reconnect attempts.
1903 struct GNUNET_TIME_Relative relative_timeout;
1906 * Maximum number of connect attempts, will retry connection
1907 * this number of times on failures.
1909 unsigned int connect_attempts;
1912 * Hello timeout task
1914 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1917 * Connect timeout task
1919 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1922 * When should this operation be complete (or we must trigger
1925 struct GNUNET_TIME_Relative timeout_hello;
1928 * Was the connection attempt successful?
1933 * When connecting, do we need to send the HELLO?
1938 * The distance between the two connected peers
1944 /** Forward declaration **/
1946 reattempt_daemons_connect (void *cls,
1947 const struct GNUNET_SCHEDULER_TaskContext *tc);
1951 * Notify callback about success or failure of the attempt
1952 * to connect the two peers
1954 * @param cls our "struct ConnectContext" (freed)
1955 * @param tc reason tells us if we succeeded or failed
1958 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1960 struct ConnectContext *ctx = cls;
1962 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1963 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1965 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1966 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1969 if (ctx->connect_request_handle != NULL)
1971 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1972 ctx->connect_request_handle = NULL;
1975 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1977 if (ctx->d1th != NULL)
1978 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1980 if (ctx->d1core != NULL)
1981 GNUNET_CORE_disconnect (ctx->d1core);
1983 if (ctx->d2core != NULL)
1984 GNUNET_CORE_disconnect (ctx->d2core);
1992 if (ctx->d1th != NULL)
1993 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1995 if (ctx->d1core != NULL)
1996 GNUNET_CORE_disconnect (ctx->d1core);
1999 if (ctx->connected == GNUNET_YES)
2001 if (ctx->cb != NULL)
2003 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
2004 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
2007 else if (ctx->connect_attempts > 0)
2009 ctx->d1core_ready = GNUNET_NO;
2011 if (ctx->d2core != NULL)
2013 GNUNET_CORE_disconnect (ctx->d2core);
2017 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
2022 if (ctx->cb != NULL)
2024 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2025 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
2034 * Success, connection is up. Signal client our success.
2036 * @param cls our "struct ConnectContext"
2037 * @param peer identity of the peer that has connected
2038 * @param atsi performance information
2042 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
2043 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2045 struct ConnectContext *ctx = cls;
2048 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Connected peer %s to peer %s\n",
2049 ctx->d1->shortname, GNUNET_i2s (peer));
2052 if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
2055 ctx->connected = GNUNET_YES;
2056 ctx->distance = 0; /* FIXME: distance */
2057 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
2059 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
2060 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2062 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
2063 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2069 * Success, connection is up. Signal client our success.
2071 * @param cls our "struct ConnectContext"
2072 * @param peer identity of the peer that has connected
2073 * @param atsi performance information
2077 connect_notify_core2 (void *cls, const struct GNUNET_PeerIdentity *peer,
2078 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2080 struct ConnectContext *ctx = cls;
2082 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
2084 ctx->connected = GNUNET_YES;
2085 ctx->distance = 0; /* FIXME: distance */
2086 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
2087 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2094 * Task called once a core connect request has been transmitted.
2096 * @param cls struct ConnectContext
2097 * @param success was the request successful?
2100 core_connect_request_cont (void *cls, int success)
2102 struct ConnectContext *ctx = cls;
2104 ctx->connect_request_handle = NULL;
2108 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2110 struct ConnectContext *ctx = cls;
2111 struct GNUNET_MessageHeader *hello;
2113 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2114 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2116 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
2117 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
2118 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
2120 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
2121 GNUNET_assert (hello != NULL);
2123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
2124 ctx->d2->shortname, ctx->d1->shortname);
2126 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
2127 GNUNET_assert (ctx->d1core != NULL);
2128 ctx->connect_request_handle =
2129 GNUNET_CORE_peer_request_connect (ctx->d1core, &ctx->d2->id,
2130 &core_connect_request_cont, ctx);
2133 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2134 "Sending connect request to CORE of %s for peer %s\n",
2135 GNUNET_i2s (&ctx->d1->id),
2136 GNUNET_h2s (&ctx->d2->id.hashPubKey));
2138 ctx->timeout_hello =
2139 GNUNET_TIME_relative_add (ctx->timeout_hello,
2140 GNUNET_TIME_relative_multiply
2141 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
2143 ctx->hello_send_task =
2144 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
2148 * Notify of a successful connection to the core service.
2150 * @param cls a ConnectContext
2151 * @param server handle to the core service
2152 * @param my_identity the peer identity of this peer
2153 * @param publicKey the public key of the peer
2156 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
2157 const struct GNUNET_PeerIdentity *my_identity,
2158 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
2161 struct ConnectContext *connect_ctx = cls;
2163 connect_ctx->d1core_ready = GNUNET_YES;
2165 if (connect_ctx->send_hello == GNUNET_NO)
2167 connect_ctx->connect_request_handle =
2168 GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
2169 &connect_ctx->d2->id,
2170 &core_connect_request_cont,
2172 GNUNET_assert (connect_ctx->connect_request_handle != NULL);
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 "Sending connect request to CORE of %s for peer %s\n",
2176 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
2184 reattempt_daemons_connect (void *cls,
2185 const struct GNUNET_SCHEDULER_TaskContext *tc)
2187 struct ConnectContext *ctx = cls;
2189 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2194 #if DEBUG_TESTING_RECONNECT
2195 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2196 "re-attempting connect of peer %s to peer %s\n",
2197 ctx->d1->shortname, ctx->d2->shortname);
2199 ctx->connect_attempts--;
2200 GNUNET_assert (ctx->d1core == NULL);
2201 ctx->d1core_ready = GNUNET_NO;
2203 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2204 &connect_notify, NULL, NULL, NULL, GNUNET_NO, NULL,
2205 GNUNET_NO, no_handlers);
2206 if (ctx->d1core == NULL)
2208 if (NULL != ctx->cb)
2209 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2210 ctx->d2->cfg, ctx->d1, ctx->d2,
2211 _("Failed to connect to core service of first peer!\n"));
2216 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
2217 if (NULL != ctx->d2->hello)
2219 GNUNET_free (ctx->d2->hello);
2220 ctx->d2->hello = NULL;
2221 if (NULL != ctx->d2->th)
2223 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->ghh);
2224 ctx->d2->ghh = NULL;
2225 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
2228 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2230 GNUNET_assert (ctx->d2->th != NULL);
2232 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2235 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
2238 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2240 if (ctx->d2->th == NULL)
2242 GNUNET_CORE_disconnect (ctx->d1core);
2244 if (NULL != ctx->cb)
2245 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2246 ctx->d2->cfg, ctx->d1, ctx->d2,
2247 _("Failed to connect to transport service!\n"));
2251 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2254 if (ctx->send_hello == GNUNET_YES)
2257 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2259 if (ctx->d1th == NULL)
2261 GNUNET_CORE_disconnect (ctx->d1core);
2263 if (NULL != ctx->cb)
2264 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2265 ctx->d2->cfg, ctx->d1, ctx->d2,
2266 _("Failed to connect to transport service!\n"));
2269 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2273 ctx->connect_request_handle =
2274 GNUNET_CORE_peer_request_connect (ctx->d1core, &ctx->d2->id,
2275 &core_connect_request_cont, ctx);
2278 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2279 ¬ify_connect_result, ctx);
2283 * Iterator for currently known peers, to ensure
2284 * that we don't try to send duplicate connect
2287 * @param cls our "struct ConnectContext"
2288 * @param peer identity of the peer that has connected,
2289 * NULL when iteration has finished
2290 * @param atsi performance information
2294 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2295 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2297 struct ConnectContext *ctx = cls;
2299 if ((peer != NULL) &&
2300 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2302 ctx->connected = GNUNET_YES;
2303 ctx->distance = 0; /* FIXME: distance */
2306 else if (peer == NULL) /* End of iteration over peers */
2308 if (ctx->connected == GNUNET_YES)
2311 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2315 /* Peer not already connected, need to schedule connect request! */
2316 if (ctx->d1core == NULL)
2319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2320 "Peers are NOT connected, connecting to core!\n");
2323 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2324 &connect_notify, NULL, NULL, NULL, GNUNET_NO,
2325 NULL, GNUNET_NO, no_handlers);
2328 if (ctx->d1core == NULL)
2331 if (NULL != ctx->cb)
2332 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2333 ctx->d2->cfg, ctx->d1, ctx->d2,
2334 _("Failed to connect to core service of first peer!\n"));
2338 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL)) /* Do not yet have the second peer's hello, set up a task to get it */
2341 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL,
2343 if (ctx->d2->th == NULL)
2345 GNUNET_CORE_disconnect (ctx->d1core);
2347 if (NULL != ctx->cb)
2348 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2349 ctx->d2->cfg, ctx->d1, ctx->d2,
2350 _("Failed to connect to transport service!\n"));
2354 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2357 if (ctx->send_hello == GNUNET_YES)
2360 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2362 if (ctx->d1th == NULL)
2364 GNUNET_CORE_disconnect (ctx->d1core);
2366 if (NULL != ctx->cb)
2367 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2368 ctx->d2->cfg, ctx->d1, ctx->d2,
2369 _("Failed to connect to transport service!\n"));
2372 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2376 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2377 ¬ify_connect_result, ctx);
2383 * Establish a connection between two GNUnet daemons.
2385 * @param d1 handle for the first daemon
2386 * @param d2 handle for the second daemon
2387 * @param timeout how long is the connection attempt
2389 * @param max_connect_attempts how many times should we try to reconnect
2391 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2392 * the HELLO has already been exchanged
2393 * @param cb function to call at the end
2394 * @param cb_cls closure for cb
2397 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2398 struct GNUNET_TESTING_Daemon *d2,
2399 struct GNUNET_TIME_Relative timeout,
2400 unsigned int max_connect_attempts,
2402 GNUNET_TESTING_NotifyConnection cb,
2405 struct ConnectContext *ctx;
2407 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2410 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2411 _("Peers are not fully running yet, can not connect!\n"));
2412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2416 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
2419 ctx->timeout_hello =
2420 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2421 ctx->relative_timeout =
2422 GNUNET_TIME_relative_divide (timeout, max_connect_attempts);
2424 ctx->cb_cls = cb_cls;
2425 ctx->connect_attempts = max_connect_attempts;
2426 ctx->connected = GNUNET_NO;
2427 ctx->send_hello = send_hello;
2429 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to connect peer %s to peer %s\n",
2430 d1->shortname, d2->shortname);
2433 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2434 GNUNET_assert (GNUNET_OK ==
2435 GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id,
2436 &core_initial_iteration, ctx));
2437 /*GNUNET_assert(GNUNET_OK == GNUNET_CORE_iterate_peers (ctx->d1->cfg, &core_initial_iteration, ctx)); */
2440 /* end of testing.c */