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->th != NULL)
142 GNUNET_TRANSPORT_get_hello_cancel (daemon->th, &process_hello, daemon);
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);
167 static void start_fsm (void *cls,
168 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 (d->
200 d->dead_cb, d->dead_cb_cls, GNUNET_YES,
202 else if (NULL != d->cb)
203 d->cb (d->cb_cls, NULL, d->cfg, d,
204 _("Failed to connect to core service\n"));
208 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully started peer `%4s'.\n",
209 GNUNET_i2s (my_identity));
211 d->id = *my_identity; /* FIXME: shouldn't we already have this from reading the hostkey file? */
212 if (d->shortname == NULL)
213 d->shortname = strdup (GNUNET_i2s (my_identity));
215 d->running = GNUNET_YES;
217 if (GNUNET_NO == d->running)
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Peer is dead (d->running == GNUNET_NO)\n");
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Successfully started peer `%4s', connecting to transport service.\n",
228 GNUNET_i2s (my_identity));
231 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
234 if (GNUNET_YES == d->dead)
235 GNUNET_TESTING_daemon_stop (d,
236 GNUNET_TIME_absolute_get_remaining (d->
238 d->dead_cb, d->dead_cb_cls, GNUNET_YES,
240 else if (NULL != d->cb)
241 d->cb (d->cb_cls, &d->id, d->cfg, d,
242 _("Failed to connect to transport service!\n"));
246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
247 "Connected to transport service `%s', getting HELLO\n",
248 GNUNET_i2s (my_identity));
251 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
253 if (d->task != GNUNET_SCHEDULER_NO_TASK)
254 GNUNET_SCHEDULER_cancel (d->task);
256 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
262 * Notify of a peer being up and running. Scheduled as a task
263 * so that variables which may need to be set are set before
264 * the connect callback can set up new operations.
266 * @param cls the testing daemon
267 * @param tc task scheduler context
270 notify_daemon_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
272 struct GNUNET_TESTING_Daemon *d = cls;
273 GNUNET_TESTING_NotifyDaemonRunning cb;
278 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
283 * Finite-state machine for starting GNUnet.
285 * @param cls our "struct GNUNET_TESTING_Daemon"
289 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
291 struct GNUNET_TESTING_Daemon *d = cls;
292 GNUNET_TESTING_NotifyDaemonRunning cb;
293 enum GNUNET_OS_ProcessStatusType type;
299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer FSM is in phase %u.\n", d->phase);
302 d->task = GNUNET_SCHEDULER_NO_TASK;
306 /* confirm copying complete */
307 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
309 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
314 cb (d->cb_cls, NULL, d->cfg, d,
316 ("`scp' does not seem to terminate (timeout copying config).\n"));
321 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
325 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
330 cb (d->cb_cls, NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
333 GNUNET_OS_process_close (d->proc);
335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
336 "Successfully copied configuration file.\n");
338 d->phase = SP_COPIED;
341 /* Start create hostkey process if we don't already know the peer identity! */
342 if (GNUNET_NO == d->have_hostkey)
344 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
345 if (d->pipe_stdout == NULL)
350 cb (d->cb_cls, NULL, d->cfg, d,
354 _("Failed to create pipe for `gnunet-peerinfo' process.\n") :
355 _("Failed to create pipe for `ssh' process.\n"));
358 if (NULL == d->hostname)
361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
362 "Starting `%s', with command `%s %s %s %s'.\n",
363 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
367 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
368 "gnunet-peerinfo", "-c", d->cfgfile, "-sq",
370 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
374 if (d->username != NULL)
375 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
377 dst = GNUNET_strdup (d->hostname);
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
381 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
382 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
385 if (d->ssh_port_str == NULL)
387 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh",
391 dst, "gnunet-peerinfo", "-c",
392 d->cfgfile, "-sq", NULL);
397 GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh", "-p",
402 dst, "gnunet-peerinfo", "-c", d->cfgfile,
405 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
411 _("Could not start `%s' process to create hostkey.\n"),
412 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
416 cb (d->cb_cls, NULL, d->cfg, d,
419 hostname) ? _("Failed to start `gnunet-peerinfo' process.\n") :
420 _("Failed to start `ssh' process.\n"));
421 GNUNET_DISK_pipe_close (d->pipe_stdout);
425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
426 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
428 d->phase = SP_HOSTKEY_CREATE;
430 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
432 GNUNET_DISK_pipe_handle (d->
434 GNUNET_DISK_PIPE_END_READ),
437 else /* Already have a hostkey! */
439 if (d->hostkey_callback != NULL)
441 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
442 d->hostkey_callback = NULL;
443 d->phase = SP_HOSTKEY_CREATED;
446 d->phase = SP_TOPOLOGY_SETUP;
449 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
452 case SP_HOSTKEY_CREATE:
454 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
455 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
456 &d->hostkeybuf[d->hostkeybufpos],
457 sizeof (d->hostkeybuf) - d->hostkeybufpos);
459 d->hostkeybufpos += bytes_read;
461 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
465 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
467 GNUNET_DISK_pipe_handle (d->
469 GNUNET_DISK_PIPE_END_READ),
473 d->hostkeybuf[103] = '\0';
475 if ((bytes_read < 0) ||
477 GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey)))
481 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
482 _("Error reading from gnunet-peerinfo: %s\n"),
485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
486 _("Malformed output from gnunet-peerinfo!\n"));
489 GNUNET_DISK_pipe_close (d->pipe_stdout);
490 d->pipe_stdout = NULL;
491 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
492 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
493 GNUNET_OS_process_close (d->proc);
496 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
499 d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
500 GNUNET_DISK_pipe_close (d->pipe_stdout);
501 d->pipe_stdout = NULL;
502 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
503 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
504 GNUNET_OS_process_close (d->proc);
506 d->have_hostkey = GNUNET_YES;
507 if (d->hostkey_callback != NULL)
509 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
510 d->hostkey_callback = NULL;
511 d->phase = SP_HOSTKEY_CREATED;
515 d->phase = SP_TOPOLOGY_SETUP;
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
521 case SP_HOSTKEY_CREATED:
522 /* wait for topology finished */
523 if ((GNUNET_YES == d->dead) ||
524 (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
529 cb (d->cb_cls, NULL, d->cfg, d,
530 _("`Failed while waiting for topology setup!\n"));
535 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
538 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
539 /* start GNUnet on remote host */
540 if (NULL == d->hostname)
543 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
544 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
545 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-L", "DEBUG",
549 GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm", "-c",
555 GNUNET_TIME_relative_to_string
556 (GNUNET_TIME_absolute_get_remaining
557 (d->max_timeout)), NULL);
561 if (d->username != NULL)
562 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
564 dst = GNUNET_strdup (d->hostname);
567 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
568 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
569 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
570 "-L", "DEBUG", "-s", "-q");
572 if (d->ssh_port_str == NULL)
574 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
582 "-c", d->cfgfile, "-s", "-q", "-T",
583 GNUNET_TIME_relative_to_string
584 (GNUNET_TIME_absolute_get_remaining
585 (d->max_timeout)), NULL);
591 GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-p",
600 "-c", d->cfgfile, "-s", "-q", "-T",
601 GNUNET_TIME_relative_to_string
602 (GNUNET_TIME_absolute_get_remaining
603 (d->max_timeout)), NULL);
609 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
610 _("Could not start `%s' process to start GNUnet.\n"),
611 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
615 cb (d->cb_cls, NULL, d->cfg, d,
618 hostname) ? _("Failed to start `gnunet-arm' process.\n") :
619 _("Failed to start `ssh' process.\n"));
623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
624 "Started `%s', waiting for `%s' to be up.\n", "gnunet-arm",
625 "gnunet-service-core");
627 d->phase = SP_START_ARMING;
629 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
632 case SP_START_ARMING:
633 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
635 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
640 cb (d->cb_cls, NULL, d->cfg, d,
643 hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
644 _("`ssh' does not seem to terminate.\n"));
645 GNUNET_CONFIGURATION_destroy (d->cfg);
646 GNUNET_free (d->cfgfile);
647 GNUNET_free_non_null (d->hostname);
648 GNUNET_free_non_null (d->username);
649 GNUNET_free (d->proc);
655 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Successfully started `%s'.\n",
663 GNUNET_free (d->proc);
664 d->phase = SP_START_CORE;
666 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Calling CORE_connect\n");
670 if (d->server != NULL)
671 GNUNET_CORE_disconnect (d->server);
674 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
679 cb (d->cb_cls, NULL, d->cfg, d,
680 _("Unable to connect to CORE service for peer!\n"));
681 GNUNET_CONFIGURATION_destroy (d->cfg);
682 GNUNET_free (d->cfgfile);
683 GNUNET_free_non_null (d->hostname);
684 GNUNET_free_non_null (d->username);
689 GNUNET_CORE_connect (d->cfg, 1, d, &testing_init, NULL, NULL, NULL,
690 NULL, GNUNET_NO, NULL, GNUNET_NO, no_handlers);
692 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
693 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
696 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
699 if (GNUNET_YES == d->dead)
700 GNUNET_TESTING_daemon_stop (d,
701 GNUNET_TIME_absolute_get_remaining (d->
703 d->dead_cb, d->dead_cb_cls, GNUNET_YES,
705 else if (NULL != d->cb)
706 d->cb (d->cb_cls, &d->id, d->cfg, d,
707 _("Failed to connect to transport service!\n"));
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Connected to transport service `%s', getting HELLO\n",
713 GNUNET_i2s (&d->id));
716 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
717 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
721 * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
722 d->running = GNUNET_YES;
723 d->phase = SP_GET_HELLO;
727 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
729 if (d->server != NULL)
730 GNUNET_CORE_disconnect (d->server);
732 GNUNET_TRANSPORT_disconnect (d->th);
736 cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
737 GNUNET_CONFIGURATION_destroy (d->cfg);
738 GNUNET_free (d->cfgfile);
739 GNUNET_free_non_null (d->hostname);
740 GNUNET_free_non_null (d->username);
744 if (d->hello != NULL)
746 GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
748 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
749 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
755 case SP_SERVICE_START:
756 /* confirm gnunet-arm exited */
757 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
759 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
764 cb (d->cb_cls, NULL, d->cfg, d,
767 hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
768 _("`ssh' does not seem to terminate.\n"));
773 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
778 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
783 cb (d->cb_cls, NULL, d->cfg, d,
788 ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
789 : _("`ssh' does not seem to terminate.\n"));
794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
798 d->phase = SP_START_DONE;
800 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
802 case SP_SERVICE_SHUTDOWN_START:
803 /* confirm copying complete */
804 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
806 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
808 if (NULL != d->dead_cb)
809 d->dead_cb (d->dead_cb_cls,
811 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
816 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
821 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
823 if (NULL != d->dead_cb)
824 d->dead_cb (d->dead_cb_cls,
826 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
833 if (NULL != d->dead_cb)
834 d->dead_cb (d->dead_cb_cls, NULL);
836 case SP_SHUTDOWN_START:
837 /* confirm copying complete */
838 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
840 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
842 if (NULL != d->dead_cb)
843 d->dead_cb (d->dead_cb_cls,
845 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
848 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
849 GNUNET_TRANSPORT_disconnect (d->th);
852 GNUNET_CONFIGURATION_destroy (d->cfg);
853 GNUNET_free (d->cfgfile);
854 GNUNET_free_non_null (d->hello);
855 GNUNET_free_non_null (d->hostname);
856 GNUNET_free_non_null (d->username);
857 GNUNET_free_non_null (d->shortname);
858 GNUNET_free_non_null (d->proc);
865 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
869 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
871 if (NULL != d->dead_cb)
872 d->dead_cb (d->dead_cb_cls,
874 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
877 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
878 GNUNET_TRANSPORT_disconnect (d->th);
881 if (d->server != NULL)
883 GNUNET_CORE_disconnect (d->server);
886 GNUNET_CONFIGURATION_destroy (d->cfg);
887 GNUNET_free (d->cfgfile);
888 GNUNET_free_non_null (d->hello);
889 GNUNET_free_non_null (d->hostname);
890 GNUNET_free_non_null (d->username);
891 GNUNET_free_non_null (d->shortname);
892 GNUNET_free_non_null (d->proc);
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
900 if (d->server != NULL)
902 GNUNET_CORE_disconnect (d->server);
908 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
909 GNUNET_TRANSPORT_disconnect (d->th);
913 if (NULL != d->dead_cb)
914 d->dead_cb (d->dead_cb_cls, NULL);
916 /* state clean up and notifications */
917 if (d->churn == GNUNET_NO)
919 GNUNET_CONFIGURATION_destroy (d->cfg);
920 GNUNET_free (d->cfgfile);
921 GNUNET_free_non_null (d->hostname);
922 GNUNET_free_non_null (d->username);
925 GNUNET_free_non_null (d->hello);
927 GNUNET_free_non_null (d->shortname);
928 GNUNET_free_non_null (d->proc);
931 if (d->churn == GNUNET_NO)
935 case SP_CONFIG_UPDATE:
936 /* confirm copying complete */
937 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
939 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
944 cb (d->cb_cls, NULL, d->cfg, d,
945 _("`scp' does not seem to terminate.\n"));
950 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
954 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
956 if (NULL != d->update_cb)
957 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Successfully copied configuration file.\n");
964 if (NULL != d->update_cb)
965 d->update_cb (d->update_cb_cls, NULL);
966 d->phase = SP_START_DONE;
972 * Continues GNUnet daemon startup when user wanted to be notified
973 * once a hostkey was generated (for creating friends files, blacklists,
976 * @param daemon the daemon to finish starting
979 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
981 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
982 daemon->phase = SP_TOPOLOGY_SETUP;
986 * Check whether the given daemon is running.
988 * @param daemon the daemon to check
990 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
991 * daemon is down, GNUNET_SYSERR on error.
994 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
997 return GNUNET_SYSERR;
999 if (daemon->running == GNUNET_YES)
1006 * Starts a GNUnet daemon service which has been previously stopped.
1008 * @param d the daemon for which the service should be started
1009 * @param service the name of the service to start
1010 * @param timeout how long to wait for process for shutdown to complete
1011 * @param cb function called once the service starts
1012 * @param cb_cls closure for cb
1015 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
1017 struct GNUNET_TIME_Relative
1019 GNUNET_TESTING_NotifyDaemonRunning
1027 GNUNET_assert (d->running == GNUNET_YES);
1029 if (d->phase == SP_CONFIG_UPDATE)
1031 GNUNET_SCHEDULER_cancel (d->task);
1032 d->phase = SP_START_DONE;
1035 if (d->churned_services == NULL)
1037 d->cb (d->cb_cls, &d->id, d->cfg, d,
1038 "No service has been churned off yet!!");
1041 d->phase = SP_SERVICE_START;
1042 GNUNET_free (d->churned_services);
1043 d->churned_services = NULL;
1044 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1045 /* Check if this is a local or remote process */
1046 if (NULL != d->hostname)
1049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1050 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1051 d->cfgfile, d->hostname);
1054 if (d->username != NULL)
1055 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1057 arg = GNUNET_strdup (d->hostname);
1059 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1067 "-c", d->cfgfile, "-i", service, "-q",
1069 GNUNET_TIME_relative_to_string (timeout),
1071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1072 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
1073 arg, "gnunet-arm", d->cfgfile, service);
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1082 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1086 "-c", d->cfgfile, "-i", service, "-q",
1088 GNUNET_TIME_relative_to_string (timeout),
1092 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1093 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1097 * Starts a GNUnet daemon's service.
1099 * @param d the daemon for which the service should be started
1100 * @param service the name of the service to start
1101 * @param timeout how long to wait for process for startup
1102 * @param cb function called once gnunet-arm returns
1103 * @param cb_cls closure for cb
1106 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
1108 struct GNUNET_TIME_Relative timeout,
1109 GNUNET_TESTING_NotifyDaemonRunning cb,
1117 GNUNET_assert (service != NULL);
1118 GNUNET_assert (d->running == GNUNET_YES);
1119 GNUNET_assert (d->phase == SP_START_DONE);
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123 _("Starting service %s for peer `%4s'\n"), service,
1124 GNUNET_i2s (&d->id));
1127 d->phase = SP_SERVICE_START;
1128 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1129 /* Check if this is a local or remote process */
1130 if (NULL != d->hostname)
1133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1134 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1135 d->cfgfile, d->hostname);
1138 if (d->username != NULL)
1139 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1141 arg = GNUNET_strdup (d->hostname);
1143 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1151 "-c", d->cfgfile, "-i", service, "-q",
1153 GNUNET_TIME_relative_to_string (timeout),
1155 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1156 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
1157 arg, "gnunet-arm", d->cfgfile, service,
1158 GNUNET_TIME_relative_to_string (timeout));
1164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1165 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1167 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1171 "-c", d->cfgfile, "-i", service, "-q",
1173 GNUNET_TIME_relative_to_string (timeout),
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1177 "gnunet-arm", d->cfgfile, service,
1178 GNUNET_TIME_relative_to_string (timeout));
1181 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1182 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1186 * Start a peer that has previously been stopped using the daemon_stop
1187 * call (and files weren't deleted and the allow restart flag)
1189 * @param daemon the daemon to start (has been previously stopped)
1190 * @param timeout how long to wait for restart
1191 * @param cb the callback for notification when the peer is running
1192 * @param cb_cls closure for the callback
1195 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1196 struct GNUNET_TIME_Relative timeout,
1197 GNUNET_TESTING_NotifyDaemonRunning cb,
1200 if (daemon->running == GNUNET_YES)
1202 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1203 "Daemon already running, can't restart!");
1208 daemon->cb_cls = cb_cls;
1209 daemon->phase = SP_TOPOLOGY_SETUP;
1210 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1212 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1213 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1217 * Starts a GNUnet daemon. GNUnet must be installed on the target
1218 * system and available in the PATH. The machine must furthermore be
1219 * reachable via "ssh" (unless the hostname is "NULL") without the
1220 * need to enter a password.
1222 * @param cfg configuration to use
1223 * @param timeout how long to wait starting up peers
1224 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1225 * to really start the peer (default)
1226 * @param hostname name of the machine where to run GNUnet
1227 * (use NULL for localhost).
1228 * @param ssh_username ssh username to use when connecting to hostname
1229 * @param sshport port to pass to ssh process when connecting to hostname
1230 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1231 * @param hostkey_callback function to call once the hostkey has been
1232 * generated for this peer, but it hasn't yet been started
1233 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1234 * @param hostkey_cls closure for hostkey callback
1235 * @param cb function to call once peer is up, or failed to start
1236 * @param cb_cls closure for cb
1237 * @return handle to the daemon (actual start will be completed asynchronously)
1239 struct GNUNET_TESTING_Daemon *
1240 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1241 struct GNUNET_TIME_Relative timeout, int pretend,
1242 const char *hostname, const char *ssh_username,
1243 uint16_t sshport, const char *hostkey,
1244 GNUNET_TESTING_NotifyHostkeyCreated
1245 hostkey_callback, void *hostkey_cls,
1246 GNUNET_TESTING_NotifyDaemonRunning cb,
1249 struct GNUNET_TESTING_Daemon *ret;
1253 char *baseservicehome;
1256 char *temp_file_name;
1257 struct GNUNET_DISK_FileHandle *fn;
1258 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1259 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1261 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1262 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1265 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1268 ret->ssh_port_str = NULL;
1270 /* Find service home and base service home directories, create it if it doesn't exist */
1271 GNUNET_assert (GNUNET_OK ==
1272 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1276 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1277 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1278 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1279 GNUNET_free (temp_file_name);
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1282 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1284 if (NULL == ret->cfgfile)
1286 GNUNET_free_non_null (ret->ssh_port_str);
1287 GNUNET_free_non_null (ret->hostname);
1291 ret->hostkey_callback = hostkey_callback;
1292 ret->hostkey_cls = hostkey_cls;
1294 ret->cb_cls = cb_cls;
1295 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1296 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1297 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1300 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1302 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1303 GNUNET_assert (private_key != NULL);
1304 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1305 GNUNET_CRYPTO_hash (&public_key,
1306 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1307 &ret->id.hashPubKey);
1308 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1309 ret->have_hostkey = GNUNET_YES;
1310 GNUNET_CRYPTO_rsa_key_free (private_key);
1313 /* Write hostkey to file, if we were given one */
1315 if (hostkey != NULL)
1317 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1318 fn = GNUNET_DISK_file_open (hostkeyfile,
1319 GNUNET_DISK_OPEN_READWRITE |
1320 GNUNET_DISK_OPEN_CREATE,
1321 GNUNET_DISK_PERM_USER_READ |
1322 GNUNET_DISK_PERM_USER_WRITE);
1323 GNUNET_assert (fn != NULL);
1324 GNUNET_assert (HOSTKEYFILESIZE ==
1325 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1326 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1329 /* write configuration to temporary file */
1330 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1332 if (0 != UNLINK (ret->cfgfile))
1333 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1335 GNUNET_CONFIGURATION_destroy (ret->cfg);
1336 GNUNET_free_non_null (ret->hostname);
1337 GNUNET_free (ret->cfgfile);
1341 if (ssh_username != NULL)
1342 username = GNUNET_strdup (ssh_username);
1343 if ((ssh_username == NULL) &&
1345 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1348 if (NULL != getenv ("USER"))
1349 username = GNUNET_strdup (getenv ("USER"));
1353 ret->username = username;
1355 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1357 /* copy directory to remote host */
1358 if (NULL != hostname)
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 "Copying configuration directory to host `%s'.\n", hostname);
1364 baseservicehome = GNUNET_strdup (servicehome);
1365 /* Remove trailing /'s */
1366 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1367 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1368 /* Find next directory /, jump one ahead */
1369 slash = strrchr (baseservicehome, '/');
1373 ret->phase = SP_COPYING;
1374 if (NULL != username)
1375 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1377 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1378 GNUNET_free (baseservicehome);
1379 if (ret->ssh_port_str == NULL)
1381 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1385 servicehome, arg, NULL);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1388 "copying directory with command scp -r %s %s\n",
1395 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r", "-P",
1400 servicehome, arg, NULL);
1403 if (NULL == ret->proc)
1405 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1407 ("Could not start `%s' process to copy configuration directory.\n"),
1409 if (0 != UNLINK (ret->cfgfile))
1410 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1412 GNUNET_CONFIGURATION_destroy (ret->cfg);
1413 GNUNET_free_non_null (ret->hostname);
1414 GNUNET_free_non_null (ret->username);
1415 GNUNET_free (ret->cfgfile);
1417 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1418 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1420 GNUNET_free_non_null (hostkeyfile);
1421 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1422 GNUNET_free (servicehome);
1427 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1429 GNUNET_free_non_null (hostkeyfile);
1430 GNUNET_free (servicehome);
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1435 "No need to copy configuration file since we are running locally.\n");
1437 ret->phase = SP_COPIED;
1438 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1439 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1441 GNUNET_free_non_null (hostkeyfile);
1442 GNUNET_free (servicehome);
1448 * Restart (stop and start) a GNUnet daemon.
1450 * @param d the daemon that should be restarted
1451 * @param cb function called once the daemon is (re)started
1452 * @param cb_cls closure for cb
1455 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1456 GNUNET_TESTING_NotifyDaemonRunning cb,
1465 d->dead = GNUNET_YES;
1472 if (d->phase == SP_CONFIG_UPDATE)
1474 GNUNET_SCHEDULER_cancel (d->task);
1475 d->phase = SP_START_DONE;
1477 if (d->server != NULL)
1479 GNUNET_CORE_disconnect (d->server);
1485 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1486 GNUNET_TRANSPORT_disconnect (d->th);
1489 /* state clean up and notifications */
1490 GNUNET_free_non_null (d->hello);
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1494 GNUNET_i2s (&d->id));
1497 d->phase = SP_START_ARMING;
1499 /* Check if this is a local or remote process */
1500 if (NULL != d->hostname)
1503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1504 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1505 d->cfgfile, d->hostname);
1508 if (d->username != NULL)
1509 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1511 arg = GNUNET_strdup (d->hostname);
1513 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1521 "-c", d->cfgfile, "-e", "-r", NULL);
1522 /* Use -r to restart arm and all services */
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1532 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1536 "-c", d->cfgfile, "-e", "-r", NULL);
1539 GNUNET_free_non_null (del_arg);
1541 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1547 * Stops a GNUnet daemon.
1549 * @param d the daemon that should be stopped
1550 * @param service the name of the service to stop
1551 * @param timeout how long to wait for process for shutdown to complete
1552 * @param cb function called once the daemon was stopped
1553 * @param cb_cls closure for cb
1554 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1556 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1557 * GNUNET_NO to kill off and clean up for good
1560 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1562 struct GNUNET_TIME_Relative timeout,
1563 GNUNET_TESTING_NotifyCompletion cb,
1569 d->dead_cb_cls = cb_cls;
1571 GNUNET_assert (d->running == GNUNET_YES);
1573 if (d->phase == SP_CONFIG_UPDATE)
1575 GNUNET_SCHEDULER_cancel (d->task);
1576 d->phase = SP_START_DONE;
1580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1581 GNUNET_i2s (&d->id));
1583 if (d->churned_services != NULL)
1585 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1588 d->phase = SP_SERVICE_SHUTDOWN_START;
1589 d->churned_services = GNUNET_strdup (service);
1590 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1591 /* Check if this is a local or remote process */
1592 if (NULL != d->hostname)
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1597 d->cfgfile, d->hostname);
1600 if (d->username != NULL)
1601 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1603 arg = GNUNET_strdup (d->hostname);
1605 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1613 "-c", d->cfgfile, "-k", service, "-q",
1615 GNUNET_TIME_relative_to_string (timeout),
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1619 arg, "gnunet-arm", d->cfgfile, service);
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1628 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1632 "-c", d->cfgfile, "-k", service, "-q",
1634 GNUNET_TIME_relative_to_string (timeout),
1638 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1639 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1644 * Stops a GNUnet daemon.
1646 * @param d the daemon that should be stopped
1647 * @param timeout how long to wait for process for shutdown to complete
1648 * @param cb function called once the daemon was stopped
1649 * @param cb_cls closure for cb
1650 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1652 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1653 * GNUNET_NO to kill off and clean up for good
1656 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1657 struct GNUNET_TIME_Relative timeout,
1658 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1659 int delete_files, int allow_restart)
1665 d->dead_cb_cls = cb_cls;
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1671 GNUNET_i2s (&d->id));
1673 d->dead = GNUNET_YES;
1677 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1679 /* Free what was left from churning! */
1680 GNUNET_assert (d->cfg != NULL);
1681 GNUNET_CONFIGURATION_destroy (d->cfg);
1682 if (delete_files == GNUNET_YES)
1684 if (0 != UNLINK (d->cfgfile))
1686 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1689 GNUNET_free (d->cfgfile);
1690 GNUNET_free_non_null (d->hostname);
1691 GNUNET_free_non_null (d->username);
1692 if (NULL != d->dead_cb)
1693 d->dead_cb (d->dead_cb_cls, NULL);
1699 if (delete_files == GNUNET_YES)
1701 GNUNET_asprintf (&del_arg, "-d");
1704 if (d->phase == SP_CONFIG_UPDATE)
1706 GNUNET_SCHEDULER_cancel (d->task);
1707 d->phase = SP_START_DONE;
1709 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1710 if (d->server != NULL)
1712 GNUNET_CORE_disconnect (d->server);
1716 /* shutdown ARM process (will terminate others) */
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1719 GNUNET_i2s (&d->id));
1721 d->phase = SP_SHUTDOWN_START;
1722 d->running = GNUNET_NO;
1723 if (allow_restart == GNUNET_YES)
1724 d->churn = GNUNET_YES;
1727 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1728 GNUNET_TRANSPORT_disconnect (d->th);
1731 /* Check if this is a local or remote process */
1732 if (NULL != d->hostname)
1735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1736 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1737 d->cfgfile, d->hostname);
1740 if (d->username != NULL)
1741 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1743 arg = GNUNET_strdup (d->hostname);
1745 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1753 "-c", d->cfgfile, "-e", "-q", "-T",
1754 GNUNET_TIME_relative_to_string (timeout),
1756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1757 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1758 arg, "gnunet-arm", d->cfgfile, del_arg);
1759 /* Use -e to end arm, and -d to remove temp files */
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1768 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1772 "-c", d->cfgfile, "-e", "-q", "-T",
1773 GNUNET_TIME_relative_to_string (timeout),
1777 GNUNET_free_non_null (del_arg);
1778 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1779 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1784 * Changes the configuration of a GNUnet daemon.
1786 * @param d the daemon that should be modified
1787 * @param cfg the new configuration for the daemon
1788 * @param cb function called once the configuration was changed
1789 * @param cb_cls closure for cb
1792 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1793 struct GNUNET_CONFIGURATION_Handle *cfg,
1794 GNUNET_TESTING_NotifyCompletion cb,
1799 if (d->phase != SP_START_DONE)
1804 ("Peer not yet running, can not change configuration at this point."));
1808 /* 1) write configuration to temporary file */
1809 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1812 cb (cb_cls, _("Failed to write new configuration to disk."));
1816 /* 2) copy file to remote host (if necessary) */
1817 if (NULL == d->hostname)
1819 /* signal success */
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "Copying updated configuration file to remote host `%s'.\n",
1829 d->phase = SP_CONFIG_UPDATE;
1830 if (NULL != d->username)
1831 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1833 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1834 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1838 d->cfgfile, arg, NULL);
1840 if (NULL == d->proc)
1842 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1843 _("Could not start `%s' process to copy configuration file.\n"),
1846 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1847 d->phase = SP_START_DONE;
1851 d->update_cb_cls = cb_cls;
1853 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1858 * Data kept for each pair of peers that we try
1861 struct ConnectContext
1864 * Testing handle to the first daemon.
1866 struct GNUNET_TESTING_Daemon *d1;
1869 * Handle to core of first daemon (to check connect)
1871 struct GNUNET_CORE_Handle *d1core;
1874 * Have we actually connected to the core of the first daemon yet?
1879 * Testing handle to the second daemon.
1881 struct GNUNET_TESTING_Daemon *d2;
1884 * Handler for the request to core to connect to this peer.
1886 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1889 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1891 struct GNUNET_TRANSPORT_Handle *d1th;
1894 * Function to call once we are done (or have timed out).
1896 GNUNET_TESTING_NotifyConnection cb;
1904 * The relative timeout from whence this connect attempt was
1905 * started. Allows for reconnect attempts.
1907 struct GNUNET_TIME_Relative relative_timeout;
1910 * Maximum number of connect attempts, will retry connection
1911 * this number of times on failures.
1913 unsigned int connect_attempts;
1916 * Hello timeout task
1918 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1921 * Connect timeout task
1923 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1926 * When should this operation be complete (or we must trigger
1929 struct GNUNET_TIME_Relative timeout_hello;
1932 * Was the connection attempt successful?
1937 * When connecting, do we need to send the HELLO?
1942 * The distance between the two connected peers
1948 /** Forward declaration **/
1949 static void reattempt_daemons_connect (void *cls,
1950 const struct GNUNET_SCHEDULER_TaskContext
1955 * Notify callback about success or failure of the attempt
1956 * to connect the two peers
1958 * @param cls our "struct ConnectContext" (freed)
1959 * @param tc reason tells us if we succeeded or failed
1962 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1964 struct ConnectContext *ctx = cls;
1966 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1967 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1969 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1970 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1973 if (ctx->connect_request_handle != NULL)
1975 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1976 ctx->connect_request_handle = NULL;
1979 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1981 if (ctx->d1th != NULL)
1982 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1984 if (ctx->d1core != NULL)
1985 GNUNET_CORE_disconnect (ctx->d1core);
1987 if (ctx->d2core != NULL)
1988 GNUNET_CORE_disconnect (ctx->d2core);
1996 if (ctx->d1th != NULL)
1997 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1999 if (ctx->d1core != NULL)
2000 GNUNET_CORE_disconnect (ctx->d1core);
2003 if (ctx->connected == GNUNET_YES)
2005 if (ctx->cb != NULL)
2007 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
2008 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
2011 else if (ctx->connect_attempts > 0)
2013 ctx->d1core_ready = GNUNET_NO;
2015 if (ctx->d2core != NULL)
2017 GNUNET_CORE_disconnect (ctx->d2core);
2021 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
2026 if (ctx->cb != NULL)
2028 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2029 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
2038 * Success, connection is up. Signal client our success.
2040 * @param cls our "struct ConnectContext"
2041 * @param peer identity of the peer that has connected
2042 * @param atsi performance information
2046 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
2047 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2049 struct ConnectContext *ctx = cls;
2052 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Connected peer %s to peer %s\n",
2053 ctx->d1->shortname, GNUNET_i2s (peer));
2056 if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
2059 ctx->connected = GNUNET_YES;
2060 ctx->distance = 0; /* FIXME: distance */
2061 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
2063 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
2064 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2066 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
2067 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2073 * Success, connection is up. Signal client our success.
2075 * @param cls our "struct ConnectContext"
2076 * @param peer identity of the peer that has connected
2077 * @param atsi performance information
2081 connect_notify_core2 (void *cls, const struct GNUNET_PeerIdentity *peer,
2082 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2084 struct ConnectContext *ctx = cls;
2086 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
2088 ctx->connected = GNUNET_YES;
2089 ctx->distance = 0; /* FIXME: distance */
2090 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
2091 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2098 * Task called once a core connect request has been transmitted.
2100 * @param cls struct ConnectContext
2101 * @param success was the request successful?
2104 core_connect_request_cont (void *cls, int success)
2106 struct ConnectContext *ctx = cls;
2108 ctx->connect_request_handle = NULL;
2112 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
2114 struct ConnectContext *ctx = cls;
2115 struct GNUNET_MessageHeader *hello;
2117 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2118 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2120 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
2121 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
2122 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
2124 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
2125 GNUNET_assert (hello != NULL);
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
2128 ctx->d2->shortname, ctx->d1->shortname);
2130 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
2131 GNUNET_assert (ctx->d1core != NULL);
2132 ctx->connect_request_handle =
2133 GNUNET_CORE_peer_request_connect (ctx->d1core, &ctx->d2->id,
2134 &core_connect_request_cont, ctx);
2137 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2138 "Sending connect request to CORE of %s for peer %s\n",
2139 GNUNET_i2s (&ctx->d1->id),
2140 GNUNET_h2s (&ctx->d2->id.hashPubKey));
2142 ctx->timeout_hello =
2143 GNUNET_TIME_relative_add (ctx->timeout_hello,
2144 GNUNET_TIME_relative_multiply
2145 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
2147 ctx->hello_send_task =
2148 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
2152 * Notify of a successful connection to the core service.
2154 * @param cls a ConnectContext
2155 * @param server handle to the core service
2156 * @param my_identity the peer identity of this peer
2157 * @param publicKey the public key of the peer
2160 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
2161 const struct GNUNET_PeerIdentity *my_identity,
2162 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded
2165 struct ConnectContext *connect_ctx = cls;
2167 connect_ctx->d1core_ready = GNUNET_YES;
2169 if (connect_ctx->send_hello == GNUNET_NO)
2171 connect_ctx->connect_request_handle =
2172 GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
2173 &connect_ctx->d2->id,
2174 &core_connect_request_cont,
2176 GNUNET_assert (connect_ctx->connect_request_handle != NULL);
2178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2179 "Sending connect request to CORE of %s for peer %s\n",
2180 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
2188 reattempt_daemons_connect (void *cls,
2189 const struct GNUNET_SCHEDULER_TaskContext *tc)
2191 struct ConnectContext *ctx = cls;
2193 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2198 #if DEBUG_TESTING_RECONNECT
2199 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2200 "re-attempting connect of peer %s to peer %s\n",
2201 ctx->d1->shortname, ctx->d2->shortname);
2203 ctx->connect_attempts--;
2204 GNUNET_assert (ctx->d1core == NULL);
2205 ctx->d1core_ready = GNUNET_NO;
2207 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2208 &connect_notify, NULL, NULL, NULL, GNUNET_NO, NULL,
2209 GNUNET_NO, no_handlers);
2210 if (ctx->d1core == NULL)
2212 if (NULL != ctx->cb)
2213 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2214 ctx->d2->cfg, ctx->d1, ctx->d2,
2215 _("Failed to connect to core service of first peer!\n"));
2220 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
2221 if (NULL != ctx->d2->hello)
2223 GNUNET_free (ctx->d2->hello);
2224 ctx->d2->hello = NULL;
2225 if (NULL != ctx->d2->th)
2227 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->th, &process_hello, ctx->d2);
2228 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
2231 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2233 GNUNET_assert (ctx->d2->th != NULL);
2234 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2237 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
2240 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2242 if (ctx->d2->th == NULL)
2244 GNUNET_CORE_disconnect (ctx->d1core);
2246 if (NULL != ctx->cb)
2247 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2248 ctx->d2->cfg, ctx->d1, ctx->d2,
2249 _("Failed to connect to transport service!\n"));
2252 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2255 if (ctx->send_hello == GNUNET_YES)
2258 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2260 if (ctx->d1th == NULL)
2262 GNUNET_CORE_disconnect (ctx->d1core);
2264 if (NULL != ctx->cb)
2265 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2266 ctx->d2->cfg, ctx->d1, ctx->d2,
2267 _("Failed to connect to transport service!\n"));
2270 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2274 ctx->connect_request_handle =
2275 GNUNET_CORE_peer_request_connect (ctx->d1core, &ctx->d2->id,
2276 &core_connect_request_cont, ctx);
2279 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2280 ¬ify_connect_result, ctx);
2284 * Iterator for currently known peers, to ensure
2285 * that we don't try to send duplicate connect
2288 * @param cls our "struct ConnectContext"
2289 * @param peer identity of the peer that has connected,
2290 * NULL when iteration has finished
2291 * @param atsi performance information
2295 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2296 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
2298 struct ConnectContext *ctx = cls;
2300 if ((peer != NULL) &&
2301 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2303 ctx->connected = GNUNET_YES;
2304 ctx->distance = 0; /* FIXME: distance */
2307 else if (peer == NULL) /* End of iteration over peers */
2309 if (ctx->connected == GNUNET_YES)
2312 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2316 /* Peer not already connected, need to schedule connect request! */
2317 if (ctx->d1core == NULL)
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2321 "Peers are NOT connected, connecting to core!\n");
2324 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2325 &connect_notify, NULL, NULL, NULL, GNUNET_NO,
2326 NULL, GNUNET_NO, no_handlers);
2329 if (ctx->d1core == NULL)
2332 if (NULL != ctx->cb)
2333 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2334 ctx->d2->cfg, ctx->d1, ctx->d2,
2335 _("Failed to connect to core service of first peer!\n"));
2339 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 */
2342 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL,
2344 if (ctx->d2->th == NULL)
2346 GNUNET_CORE_disconnect (ctx->d1core);
2348 if (NULL != ctx->cb)
2349 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2350 ctx->d2->cfg, ctx->d1, ctx->d2,
2351 _("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 */