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_EXTRA_LOGGING
42 #define DEBUG_TESTING_RECONNECT GNUNET_EXTRA_LOGGING
45 * Hack to deal with initial HELLO's being often devoid of addresses.
46 * This hack causes 'process_hello' to ignore HELLOs without addresses.
47 * The correct implementation would continue with 'process_hello' until
48 * the connection could be established...
50 #define EMPTY_HACK GNUNET_YES
53 * How long do we wait after starting gnunet-service-arm
54 * for the core service to be alive?
56 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
59 * How many times are we willing to try to wait for "scp" or
60 * "gnunet-service-arm" to complete (waitpid) before giving up?
62 #define MAX_EXEC_WAIT_RUNS 250
64 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
68 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
69 struct GNUNET_TIME_Absolute expiration)
79 * Receive the HELLO from one peer, give it to the other
80 * and ask them to connect.
82 * @param cls Closure (daemon whose hello is this).
83 * @param message HELLO message of peer
86 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
88 struct GNUNET_TESTING_Daemon *daemon = cls;
95 GNUNET_assert (message != NULL);
96 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) message,
97 GNUNET_NO, &test_address, &empty);
98 if (GNUNET_YES == empty)
100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
101 "Skipping empty HELLO address of peer %s\n",
102 GNUNET_i2s (&daemon->id));
106 GNUNET_assert (daemon->phase == SP_GET_HELLO ||
107 daemon->phase == SP_START_DONE);
108 daemon->cb = NULL; // FIXME: why??? (see fsm:SP_START_CORE, notify_daemon_started)
109 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
110 GNUNET_SCHEDULER_cancel (daemon->task);
112 if (daemon->server != NULL)
114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
115 "Received `%s' from transport service of `%4s', disconnecting core!\n",
116 "HELLO", GNUNET_i2s (&daemon->id));
117 GNUNET_CORE_disconnect (daemon->server);
118 daemon->server = NULL;
121 msize = ntohs (message->size);
124 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
125 "HELLO message of peer %s is of size 0\n",
126 GNUNET_i2s (&daemon->id));
129 if (daemon->ghh != NULL)
131 GNUNET_TRANSPORT_get_hello_cancel (daemon->ghh);
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Received `%s' from transport service of `%4s'\n", "HELLO",
136 GNUNET_i2s (&daemon->id));
137 GNUNET_free_non_null (daemon->hello);
138 daemon->hello = GNUNET_malloc (msize);
139 memcpy (daemon->hello, message, msize);
141 if (daemon->th != NULL)
143 GNUNET_TRANSPORT_disconnect (daemon->th);
146 daemon->phase = SP_START_DONE;
151 * Notify of a peer being up and running. Scheduled as a task
152 * so that variables which may need to be set are set before
153 * the connect callback can set up new operations.
154 * FIXME: what variables?????? where from????
156 * @param cls the testing daemon
157 * @param tc task scheduler context
160 notify_daemon_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
162 struct GNUNET_TESTING_Daemon *d = cls;
163 GNUNET_TESTING_NotifyDaemonRunning cb;
168 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
173 * Finite-state machine for starting GNUnet.
175 * @param cls our "struct GNUNET_TESTING_Daemon"
179 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
181 struct GNUNET_TESTING_Daemon *d = cls;
182 GNUNET_TESTING_NotifyDaemonRunning cb;
183 enum GNUNET_OS_ProcessStatusType type;
188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %s FSM is in phase %u.\n",
189 GNUNET_i2s (&d->id), d->phase);
190 d->task = GNUNET_SCHEDULER_NO_TASK;
194 /* confirm copying complete */
195 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_copying, &type, &code))
197 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
202 cb (d->cb_cls, NULL, d->cfg, d,
204 ("`scp' does not seem to terminate (timeout copying config).\n"));
209 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
213 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
218 cb (d->cb_cls, NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
221 GNUNET_OS_process_close (d->proc_arm_copying);
222 d->proc_arm_copying = NULL;
223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
224 "Successfully copied configuration file.\n");
225 d->phase = SP_COPIED;
228 /* Start create hostkey process if we don't already know the peer identity! */
229 if (GNUNET_NO == d->have_hostkey)
231 GNUNET_assert (NULL == d->proc_arm_peerinfo);
232 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_YES);
233 if (d->pipe_stdout == NULL)
238 cb (d->cb_cls, NULL, d->cfg, d,
241 _("Failed to create pipe for `gnunet-peerinfo' process.\n") :
242 _("Failed to create pipe for `ssh' process.\n"));
245 if (NULL == d->hostname)
247 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
248 "Starting `%s', with command `%s %s %s %s'.\n",
249 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
251 d->proc_arm_peerinfo =
252 GNUNET_OS_start_process (GNUNET_YES, NULL, d->pipe_stdout, "gnunet-peerinfo",
253 "gnunet-peerinfo", "-c", d->cfgfile, "-sq",
255 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
259 if (d->username != NULL)
260 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
262 dst = GNUNET_strdup (d->hostname);
264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
265 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
266 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
268 if (d->ssh_port_str == NULL)
270 d->proc_arm_peerinfo = GNUNET_OS_start_process (GNUNET_NO, NULL, d->pipe_stdout, "ssh", "ssh",
274 dst, "gnunet-peerinfo", "-c",
275 d->cfgfile, "-sq", NULL);
279 d->proc_arm_peerinfo =
280 GNUNET_OS_start_process (GNUNET_NO, NULL, d->pipe_stdout, "ssh", "ssh", "-p",
285 dst, "gnunet-peerinfo", "-c", d->cfgfile,
288 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
291 if (NULL == d->proc_arm_peerinfo)
293 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
294 _("Could not start `%s' process to create hostkey.\n"),
295 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
299 cb (d->cb_cls, NULL, d->cfg, d,
301 d->hostname) ? _("Failed to start `gnunet-peerinfo' process.\n")
302 : _("Failed to start `ssh' process.\n"));
303 GNUNET_DISK_pipe_close (d->pipe_stdout);
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
308 d->phase = SP_HOSTKEY_CREATE;
310 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
312 GNUNET_DISK_pipe_handle
314 GNUNET_DISK_PIPE_END_READ),
317 else /* Already have a hostkey! */
319 if (d->hostkey_callback != NULL)
321 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
322 d->hostkey_callback = NULL;
323 d->phase = SP_HOSTKEY_CREATED;
326 d->phase = SP_TOPOLOGY_SETUP;
329 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
332 case SP_HOSTKEY_CREATE:
334 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
335 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
336 &d->hostkeybuf[d->hostkeybufpos],
337 sizeof (d->hostkeybuf) - d->hostkeybufpos);
339 d->hostkeybufpos += bytes_read;
341 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
345 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
347 GNUNET_DISK_pipe_handle
349 GNUNET_DISK_PIPE_END_READ),
353 d->hostkeybuf[103] = '\0';
355 if ((bytes_read < 0) ||
357 GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey)))
361 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
362 _("Error reading from gnunet-peerinfo: %s\n"),
365 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
366 _("Malformed output from gnunet-peerinfo!\n"));
369 GNUNET_DISK_pipe_close (d->pipe_stdout);
370 d->pipe_stdout = NULL;
371 (void) GNUNET_OS_process_kill (d->proc_arm_peerinfo, SIGKILL);
372 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc_arm_peerinfo));
373 GNUNET_OS_process_close (d->proc_arm_peerinfo);
374 d->proc_arm_peerinfo = NULL;
376 cb (d->cb_cls, NULL, d->cfg, d, _("Failed to get hostkey!\n"));
379 d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
380 GNUNET_DISK_pipe_close (d->pipe_stdout);
381 d->pipe_stdout = NULL;
382 (void) GNUNET_OS_process_kill (d->proc_arm_peerinfo, SIGKILL);
383 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc_arm_peerinfo));
384 GNUNET_OS_process_close (d->proc_arm_peerinfo);
385 d->proc_arm_peerinfo = NULL;
386 d->have_hostkey = GNUNET_YES;
387 if (d->hostkey_callback != NULL)
389 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
390 d->hostkey_callback = NULL;
391 d->phase = SP_HOSTKEY_CREATED;
395 d->phase = SP_TOPOLOGY_SETUP;
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
399 case SP_HOSTKEY_CREATED:
400 /* wait for topology finished */
401 if ((GNUNET_YES == d->dead) ||
402 (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
407 cb (d->cb_cls, NULL, d->cfg, d,
408 _("`Failed while waiting for topology setup!\n"));
413 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
416 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
417 /* start GNUnet on remote host */
418 if (NULL == d->hostname)
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
421 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
422 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-L", "DEBUG",
425 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm", "-c",
429 GNUNET_TIME_relative_to_string
430 (GNUNET_TIME_absolute_get_remaining
431 (d->max_timeout)), NULL);
435 if (d->username != NULL)
436 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
438 dst = GNUNET_strdup (d->hostname);
440 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
441 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
442 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
443 "-L", "DEBUG", "-s", "-q");
444 if (d->ssh_port_str == NULL)
446 d->proc_arm_start = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
454 "-c", d->cfgfile, "-s", "-q", "-T",
455 GNUNET_TIME_relative_to_string
456 (GNUNET_TIME_absolute_get_remaining
457 (d->max_timeout)), NULL);
463 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh", "-p",
472 "-c", d->cfgfile, "-s", "-q", "-T",
473 GNUNET_TIME_relative_to_string
474 (GNUNET_TIME_absolute_get_remaining
475 (d->max_timeout)), NULL);
479 if (NULL == d->proc_arm_start)
481 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
482 _("Could not start `%s' process to start GNUnet.\n"),
483 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
487 cb (d->cb_cls, NULL, d->cfg, d,
489 d->hostname) ? _("Failed to start `gnunet-arm' process.\n") :
490 _("Failed to start `ssh' process.\n"));
493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
494 "Started `%s', waiting for `%s' to be up.\n", "gnunet-arm",
495 "gnunet-service-core");
496 d->phase = SP_START_ARMING;
498 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
501 case SP_START_ARMING:
502 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_start, &type, &code))
504 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
509 cb (d->cb_cls, NULL, d->cfg, d,
511 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
512 _("`ssh' does not seem to terminate.\n"));
515 GNUNET_CONFIGURATION_destroy (d->cfg);
518 if (d->cfgfile != NULL)
520 GNUNET_free (d->cfgfile);
523 GNUNET_free_non_null (d->hostname);
524 GNUNET_free_non_null (d->username);
525 GNUNET_free (d->proc_arm_start);
526 d->proc_arm_start = NULL;
527 d->hostname = NULL; // Quick hack to avoid crashing (testing need to be
528 d->cfg = NULL; // overhauled anyway, and the error managing is
529 // GNUNET_free (d); // FIXME (could this leak)
530 // pretty broken anyway.
535 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully started `%s'.\n",
541 GNUNET_free (d->proc_arm_start);
542 d->proc_arm_start = NULL;
543 d->phase = SP_START_CORE;
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling CORE_connect\n");
547 if (d->server != NULL)
548 GNUNET_CORE_disconnect (d->server);
550 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
553 if (GNUNET_YES == d->dead)
554 GNUNET_TESTING_daemon_stop (d,
555 GNUNET_TIME_absolute_get_remaining
556 (d->max_timeout), d->dead_cb,
557 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
558 else if (NULL != d->cb)
559 d->cb (d->cb_cls, &d->id, d->cfg, d,
560 _("Failed to connect to transport service!\n"));
563 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
564 "Connected to transport service `%s', getting HELLO\n",
565 GNUNET_i2s (&d->id));
566 d->ghh = GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
567 /* FIXME: store task ID somewhere! */
568 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
572 * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
573 d->running = GNUNET_YES;
574 d->phase = SP_GET_HELLO;
577 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
579 if (d->server != NULL)
580 GNUNET_CORE_disconnect (d->server);
582 GNUNET_TRANSPORT_disconnect (d->th);
586 cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
587 GNUNET_CONFIGURATION_destroy (d->cfg);
588 GNUNET_free (d->cfgfile);
589 GNUNET_free_non_null (d->hostname);
590 GNUNET_free_non_null (d->username);
594 if (d->hello != NULL)
596 GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
598 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
599 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
605 case SP_SERVICE_START:
606 /* confirm gnunet-arm exited */
607 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_srv_start, &type, &code))
609 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
614 cb (d->cb_cls, NULL, d->cfg, d,
616 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
617 _("`ssh' does not seem to terminate.\n"));
622 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
627 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
632 cb (d->cb_cls, NULL, d->cfg, d,
636 ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
637 : _("`ssh' does not seem to terminate.\n"));
641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
644 d->phase = SP_START_DONE;
646 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
648 case SP_SERVICE_SHUTDOWN_START:
649 /* confirm copying complete */
650 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_srv_stop, &type, &code))
652 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
654 if (NULL != d->dead_cb)
655 d->dead_cb (d->dead_cb_cls,
657 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
662 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
667 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
669 if (NULL != d->dead_cb)
670 d->dead_cb (d->dead_cb_cls,
672 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
677 if (NULL != d->dead_cb)
678 d->dead_cb (d->dead_cb_cls, NULL);
680 case SP_SHUTDOWN_START:
681 /* confirm copying complete !??? */
682 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_stop, &type, &code))
684 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
686 if (NULL != d->dead_cb)
687 d->dead_cb (d->dead_cb_cls,
689 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
692 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
694 GNUNET_TRANSPORT_disconnect (d->th);
699 GNUNET_CONFIGURATION_destroy (d->cfg);
702 if (d->cfgfile != NULL)
704 GNUNET_free (d->cfgfile);
707 GNUNET_free_non_null (d->hello);
708 GNUNET_free_non_null (d->hostname);
709 GNUNET_free_non_null (d->username);
710 GNUNET_free_non_null (d->shortname);
711 // GNUNET_free_non_null (d->proc); // !? FIXME
712 // d->proc_arm_stop = NULL;
718 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
722 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
724 if (NULL != d->dead_cb)
725 d->dead_cb (d->dead_cb_cls,
727 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
730 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
732 GNUNET_TRANSPORT_disconnect (d->th);
735 if (d->server != NULL)
737 GNUNET_CORE_disconnect (d->server);
740 GNUNET_CONFIGURATION_destroy (d->cfg);
742 GNUNET_free (d->cfgfile);
743 GNUNET_free_non_null (d->hello);
744 GNUNET_free_non_null (d->hostname);
745 GNUNET_free_non_null (d->username);
746 GNUNET_free_non_null (d->shortname);
747 // GNUNET_free_non_null (d->proc); // !? FIXME!
748 // d->proc = NULL; // !? FIXME!
752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
753 if (d->server != NULL)
755 GNUNET_CORE_disconnect (d->server);
761 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
763 GNUNET_TRANSPORT_disconnect (d->th);
767 if (NULL != d->dead_cb)
768 d->dead_cb (d->dead_cb_cls, NULL);
770 /* state clean up and notifications */
771 if (d->churn == GNUNET_NO)
773 GNUNET_CONFIGURATION_destroy (d->cfg);
775 GNUNET_free (d->cfgfile);
776 GNUNET_free_non_null (d->hostname);
777 GNUNET_free_non_null (d->username);
780 GNUNET_free_non_null (d->hello);
782 GNUNET_free_non_null (d->shortname);
783 // GNUNET_free_non_null (d->proc); // !? FIXME
784 // d->proc = NULL; // !? FIXME!
786 if (d->churn == GNUNET_NO)
790 case SP_CONFIG_UPDATE:
791 /* confirm copying complete */
792 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_copying, &type, &code))
794 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
799 cb (d->cb_cls, NULL, d->cfg, d,
800 _("`scp' does not seem to terminate.\n"));
805 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
809 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
811 if (NULL != d->update_cb)
812 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
816 "Successfully copied configuration file.\n");
817 if (NULL != d->update_cb)
818 d->update_cb (d->update_cb_cls, NULL);
819 d->phase = SP_START_DONE;
825 * Continues GNUnet daemon startup when user wanted to be notified
826 * once a hostkey was generated (for creating friends files, blacklists,
829 * @param daemon the daemon to finish starting
832 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
834 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
835 daemon->phase = SP_TOPOLOGY_SETUP;
839 * Check whether the given daemon is running.
841 * @param daemon the daemon to check
843 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
844 * daemon is down, GNUNET_SYSERR on error.
847 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
850 return GNUNET_SYSERR;
852 if (daemon->running == GNUNET_YES)
859 * Starts a GNUnet daemon service which has been previously stopped.
861 * @param d the daemon for which the service should be started
862 * @param service the name of the service to start
863 * @param timeout how long to wait for process for shutdown to complete
864 * @param cb function called once the service starts
865 * @param cb_cls closure for cb
868 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
870 struct GNUNET_TIME_Relative
872 GNUNET_TESTING_NotifyDaemonRunning
880 GNUNET_assert (d->running == GNUNET_YES);
882 if (d->phase == SP_CONFIG_UPDATE)
884 GNUNET_SCHEDULER_cancel (d->task);
885 d->phase = SP_START_DONE;
888 if (d->churned_services == NULL)
890 d->cb (d->cb_cls, &d->id, d->cfg, d,
891 "No service has been churned off yet!!");
894 d->phase = SP_SERVICE_START;
895 GNUNET_free (d->churned_services);
896 d->churned_services = NULL;
897 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
898 /* Check if this is a local or remote process */
899 if (NULL != d->hostname)
901 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
902 "Starting gnunet-arm with config `%s' on host `%s'.\n",
903 d->cfgfile, d->hostname);
904 if (d->username != NULL)
905 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
907 arg = GNUNET_strdup (d->hostname);
909 d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
917 "-c", d->cfgfile, "-i", service, "-q",
919 GNUNET_TIME_relative_to_string (timeout),
921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
922 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
923 arg, "gnunet-arm", d->cfgfile, service);
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
930 d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
934 "-c", d->cfgfile, "-i", service, "-q",
936 GNUNET_TIME_relative_to_string (timeout),
940 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
941 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
945 * Starts a GNUnet daemon's service.
947 * @param d the daemon for which the service should be started
948 * @param service the name of the service to start
949 * @param timeout how long to wait for process for startup
950 * @param cb function called once gnunet-arm returns
951 * @param cb_cls closure for cb
954 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
956 struct GNUNET_TIME_Relative timeout,
957 GNUNET_TESTING_NotifyDaemonRunning cb,
965 GNUNET_assert (service != NULL);
966 GNUNET_assert (d->running == GNUNET_YES);
967 GNUNET_assert (d->phase == SP_START_DONE);
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 _("Starting service %s for peer `%4s'\n"), service,
971 GNUNET_i2s (&d->id));
972 d->phase = SP_SERVICE_START;
973 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
974 /* Check if this is a local or remote process */
975 if (NULL != d->hostname)
977 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
978 "Starting gnunet-arm with config `%s' on host `%s'.\n",
979 d->cfgfile, d->hostname);
980 if (d->username != NULL)
981 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
983 arg = GNUNET_strdup (d->hostname);
985 d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
993 "-c", d->cfgfile, "-i", service, "-q",
995 GNUNET_TIME_relative_to_string (timeout),
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
999 arg, "gnunet-arm", d->cfgfile, service,
1000 GNUNET_TIME_relative_to_string (timeout));
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1007 d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
1011 "-c", d->cfgfile, "-i", service, "-q",
1013 GNUNET_TIME_relative_to_string (timeout),
1015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1016 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1017 "gnunet-arm", d->cfgfile, service,
1018 GNUNET_TIME_relative_to_string (timeout));
1021 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1022 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1026 * Start a peer that has previously been stopped using the daemon_stop
1027 * call (and files weren't deleted and the allow restart flag)
1029 * @param daemon the daemon to start (has been previously stopped)
1030 * @param timeout how long to wait for restart
1031 * @param cb the callback for notification when the peer is running
1032 * @param cb_cls closure for the callback
1035 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1036 struct GNUNET_TIME_Relative timeout,
1037 GNUNET_TESTING_NotifyDaemonRunning cb,
1040 if (daemon->running == GNUNET_YES)
1042 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1043 "Daemon already running, can't restart!");
1048 daemon->cb_cls = cb_cls;
1049 daemon->phase = SP_TOPOLOGY_SETUP;
1050 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1051 /* FIXME: why add_continuation? */
1052 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1053 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1057 * Starts a GNUnet daemon. GNUnet must be installed on the target
1058 * system and available in the PATH. The machine must furthermore be
1059 * reachable via "ssh" (unless the hostname is "NULL") without the
1060 * need to enter a password.
1062 * @param cfg configuration to use
1063 * @param timeout how long to wait starting up peers
1064 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1065 * to really start the peer (default)
1066 * @param hostname name of the machine where to run GNUnet
1067 * (use NULL for localhost).
1068 * @param ssh_username ssh username to use when connecting to hostname
1069 * @param sshport port to pass to ssh process when connecting to hostname
1070 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1071 * @param hostkey_callback function to call once the hostkey has been
1072 * generated for this peer, but it hasn't yet been started
1073 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1074 * @param hostkey_cls closure for hostkey callback
1075 * @param cb function to call once peer is up, or failed to start
1076 * @param cb_cls closure for cb
1077 * @return handle to the daemon (actual start will be completed asynchronously)
1079 struct GNUNET_TESTING_Daemon *
1080 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1081 struct GNUNET_TIME_Relative timeout, int pretend,
1082 const char *hostname, const char *ssh_username,
1083 uint16_t sshport, const char *hostkey,
1084 GNUNET_TESTING_NotifyHostkeyCreated
1085 hostkey_callback, void *hostkey_cls,
1086 GNUNET_TESTING_NotifyDaemonRunning cb,
1089 struct GNUNET_TESTING_Daemon *ret;
1093 char *baseservicehome;
1096 char *temp_file_name;
1097 struct GNUNET_DISK_FileHandle *fn;
1098 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1099 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1101 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1102 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1105 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1108 ret->ssh_port_str = NULL;
1110 /* Find service home and base service home directories, create it if it doesn't exist */
1111 GNUNET_assert (GNUNET_OK ==
1112 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1116 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1117 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1118 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1119 GNUNET_free (temp_file_name);
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1121 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1122 if (NULL == ret->cfgfile)
1124 GNUNET_free_non_null (ret->ssh_port_str);
1125 GNUNET_free_non_null (ret->hostname);
1129 ret->hostkey_callback = hostkey_callback;
1130 ret->hostkey_cls = hostkey_cls;
1132 ret->cb_cls = cb_cls;
1133 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1134 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1135 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1138 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1140 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1141 GNUNET_assert (private_key != NULL);
1142 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1143 GNUNET_CRYPTO_hash (&public_key,
1144 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1145 &ret->id.hashPubKey);
1146 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1147 ret->have_hostkey = GNUNET_YES;
1148 GNUNET_CRYPTO_rsa_key_free (private_key);
1151 /* Write hostkey to file, if we were given one */
1153 if (hostkey != NULL)
1155 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1156 fn = GNUNET_DISK_file_open (hostkeyfile,
1157 GNUNET_DISK_OPEN_READWRITE |
1158 GNUNET_DISK_OPEN_CREATE,
1159 GNUNET_DISK_PERM_USER_READ |
1160 GNUNET_DISK_PERM_USER_WRITE);
1161 GNUNET_assert (fn != NULL);
1162 GNUNET_assert (HOSTKEYFILESIZE ==
1163 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1164 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1167 /* write configuration to temporary file */
1168 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1170 if (0 != UNLINK (ret->cfgfile))
1171 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1173 GNUNET_CONFIGURATION_destroy (ret->cfg);
1174 GNUNET_free_non_null (ret->hostname);
1175 GNUNET_free (ret->cfgfile);
1179 if (ssh_username != NULL)
1180 username = GNUNET_strdup (ssh_username);
1181 if ((ssh_username == NULL) &&
1183 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1186 if (NULL != getenv ("USER"))
1187 username = GNUNET_strdup (getenv ("USER"));
1191 ret->username = username;
1193 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1195 /* copy directory to remote host */
1196 if (NULL != hostname)
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "Copying configuration directory to host `%s'.\n", hostname);
1200 baseservicehome = GNUNET_strdup (servicehome);
1201 /* Remove trailing /'s */
1202 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1203 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1204 /* Find next directory /, jump one ahead */
1205 slash = strrchr (baseservicehome, '/');
1209 ret->phase = SP_COPYING;
1210 if (NULL != username)
1211 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1213 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1214 GNUNET_free (baseservicehome);
1215 if (ret->ssh_port_str == NULL)
1217 ret->proc_arm_copying = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp", "-r",
1221 servicehome, arg, NULL);
1222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1223 "copying directory with command scp -r %s %s\n",
1228 ret->proc_arm_copying =
1229 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp", "-r", "-P",
1234 servicehome, arg, NULL);
1237 if (NULL == ret->proc_arm_copying)
1239 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1241 ("Could not start `%s' process to copy configuration directory.\n"),
1243 if (0 != UNLINK (ret->cfgfile))
1244 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1246 GNUNET_CONFIGURATION_destroy (ret->cfg);
1247 GNUNET_free_non_null (ret->hostname);
1248 GNUNET_free_non_null (ret->username);
1249 GNUNET_free (ret->cfgfile);
1251 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1252 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1254 GNUNET_free_non_null (hostkeyfile);
1255 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1256 GNUNET_free (servicehome);
1261 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1263 GNUNET_free_non_null (hostkeyfile);
1264 GNUNET_free (servicehome);
1267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1268 "No need to copy configuration file since we are running locally.\n");
1269 ret->phase = SP_COPIED;
1270 /* FIXME: why add_cont? */
1271 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1272 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1274 GNUNET_free_non_null (hostkeyfile);
1275 GNUNET_free (servicehome);
1281 * Restart (stop and start) a GNUnet daemon.
1283 * @param d the daemon that should be restarted
1284 * @param cb function called once the daemon is (re)started
1285 * @param cb_cls closure for cb
1288 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1289 GNUNET_TESTING_NotifyDaemonRunning cb,
1298 d->dead = GNUNET_YES;
1305 if (d->phase == SP_CONFIG_UPDATE)
1307 GNUNET_SCHEDULER_cancel (d->task);
1308 d->phase = SP_START_DONE;
1310 if (d->server != NULL)
1312 GNUNET_CORE_disconnect (d->server);
1318 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1320 GNUNET_TRANSPORT_disconnect (d->th);
1323 /* state clean up and notifications */
1324 GNUNET_free_non_null (d->hello);
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1327 GNUNET_i2s (&d->id));
1328 d->phase = SP_START_ARMING;
1330 /* Check if this is a local or remote process */
1331 if (NULL != d->hostname)
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1335 d->cfgfile, d->hostname);
1336 if (d->username != NULL)
1337 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1339 arg = GNUNET_strdup (d->hostname);
1341 d->proc_arm_stop = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
1349 "-c", d->cfgfile, "-e", "-r", NULL);
1350 /* Use -r to restart arm and all services */
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1357 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1358 d->proc_arm_stop = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
1362 "-c", d->cfgfile, "-e", "-r", NULL);
1365 GNUNET_free_non_null (del_arg);
1367 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1373 * Stops a GNUnet daemon.
1375 * @param d the daemon that should be stopped
1376 * @param service the name of the service to stop
1377 * @param timeout how long to wait for process for shutdown to complete
1378 * @param cb function called once the daemon was stopped
1379 * @param cb_cls closure for cb
1382 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1383 const char *service,
1384 struct GNUNET_TIME_Relative timeout,
1385 GNUNET_TESTING_NotifyCompletion cb,
1391 d->dead_cb_cls = cb_cls;
1393 GNUNET_assert (d->running == GNUNET_YES);
1395 if (d->phase == SP_CONFIG_UPDATE)
1397 GNUNET_SCHEDULER_cancel (d->task);
1398 d->phase = SP_START_DONE;
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1402 GNUNET_i2s (&d->id));
1403 if (d->churned_services != NULL)
1405 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1408 d->phase = SP_SERVICE_SHUTDOWN_START;
1409 d->churned_services = GNUNET_strdup (service);
1410 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1411 /* Check if this is a local or remote process */
1412 if (NULL != d->hostname)
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1415 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1416 d->cfgfile, d->hostname);
1417 if (d->username != NULL)
1418 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1420 arg = GNUNET_strdup (d->hostname);
1422 d->proc_arm_srv_stop = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
1430 "-c", d->cfgfile, "-k", service, "-q",
1432 GNUNET_TIME_relative_to_string (timeout),
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1435 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1436 arg, "gnunet-arm", d->cfgfile, service);
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1443 d->proc_arm_srv_stop = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
1447 "-c", d->cfgfile, "-k", service, "-q",
1449 GNUNET_TIME_relative_to_string (timeout),
1453 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1454 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1459 * Forcefully terminate a process and clean up the child.
1461 * @param proc handle to process to kill
1464 kill_and_close_process (struct GNUNET_OS_Process *proc)
1466 (void) GNUNET_OS_process_kill (proc, SIGKILL);
1467 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc));
1468 GNUNET_OS_process_close (proc);
1473 * Stops a GNUnet daemon.
1475 * @param d the daemon that should be stopped
1476 * @param timeout how long to wait for process for shutdown to complete
1477 * @param cb function called once the daemon was stopped
1478 * @param cb_cls closure for cb
1479 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1481 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1482 * GNUNET_NO to kill off and clean up for good
1485 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1486 struct GNUNET_TIME_Relative timeout,
1487 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1488 int delete_files, int allow_restart)
1494 d->dead_cb_cls = cb_cls;
1498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1499 GNUNET_i2s (&d->id));
1500 d->dead = GNUNET_YES;
1503 if (NULL != d->proc_arm_start)
1505 kill_and_close_process (d->proc_arm_start);
1506 d->proc_arm_start = NULL;
1508 if (NULL != d->proc_arm_srv_start)
1510 kill_and_close_process (d->proc_arm_srv_start);
1511 d->proc_arm_srv_start = NULL;
1513 if (NULL != d->proc_arm_srv_stop)
1515 kill_and_close_process (d->proc_arm_srv_stop);
1516 d->proc_arm_srv_stop = NULL;
1518 if (NULL != d->proc_arm_copying)
1520 kill_and_close_process (d->proc_arm_copying);
1521 d->proc_arm_copying = NULL;
1523 if (NULL != d->proc_arm_peerinfo)
1525 kill_and_close_process (d->proc_arm_peerinfo);
1526 d->proc_arm_peerinfo = NULL;
1528 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1530 /* Free what was left from churning! */
1531 GNUNET_assert (d->cfg != NULL);
1532 GNUNET_CONFIGURATION_destroy (d->cfg);
1533 if (delete_files == GNUNET_YES)
1535 if (0 != UNLINK (d->cfgfile))
1537 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1540 GNUNET_free (d->cfgfile);
1541 GNUNET_free_non_null (d->hostname);
1542 GNUNET_free_non_null (d->username);
1543 if (NULL != d->dead_cb)
1544 d->dead_cb (d->dead_cb_cls, NULL);
1545 GNUNET_assert (NULL == d->proc_arm_stop);
1551 if (delete_files == GNUNET_YES)
1553 GNUNET_asprintf (&del_arg, "-d");
1556 if (d->phase == SP_CONFIG_UPDATE)
1558 GNUNET_SCHEDULER_cancel (d->task);
1559 d->phase = SP_START_DONE;
1561 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1562 if (d->server != NULL)
1564 GNUNET_CORE_disconnect (d->server);
1568 /* shutdown ARM process (will terminate others) */
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Terminating peer `%4s'\n" ,
1570 GNUNET_i2s (&d->id));
1571 d->phase = SP_SHUTDOWN_START;
1572 d->running = GNUNET_NO;
1573 if (allow_restart == GNUNET_YES)
1574 d->churn = GNUNET_YES;
1577 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1579 GNUNET_TRANSPORT_disconnect (d->th);
1582 /* Check if this is a local or remote process */
1585 if (NULL != d->hostname)
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1588 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1589 d->cfgfile, d->hostname);
1590 if (d->username != NULL)
1591 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1593 arg = GNUNET_strdup (d->hostname);
1595 d->proc_arm_stop = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
1603 "-c", d->cfgfile, "-e", "-q", "-T",
1604 GNUNET_TIME_relative_to_string (timeout),
1606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1607 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1608 arg, "gnunet-arm", d->cfgfile, del_arg);
1609 /* Use -e to end arm, and -d to remove temp files */
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1616 d->proc_arm_stop = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "gnunet-arm", "gnunet-arm",
1620 "-c", d->cfgfile, "-e", "-q", "-T",
1621 GNUNET_TIME_relative_to_string (timeout),
1623 GNUNET_assert (NULL != d->proc_arm_stop);
1626 GNUNET_free_non_null (del_arg);
1627 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1628 if (GNUNET_SCHEDULER_NO_TASK != d->task)
1629 GNUNET_SCHEDULER_cancel(d->task);
1630 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1635 * Changes the configuration of a GNUnet daemon.
1637 * @param d the daemon that should be modified
1638 * @param cfg the new configuration for the daemon
1639 * @param cb function called once the configuration was changed
1640 * @param cb_cls closure for cb
1643 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1644 struct GNUNET_CONFIGURATION_Handle *cfg,
1645 GNUNET_TESTING_NotifyCompletion cb,
1650 if (d->phase != SP_START_DONE)
1655 ("Peer not yet running, can not change configuration at this point."));
1659 /* 1) write configuration to temporary file */
1660 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1663 cb (cb_cls, _("Failed to write new configuration to disk."));
1667 /* 2) copy file to remote host (if necessary) */
1668 if (NULL == d->hostname)
1670 /* signal success */
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "Copying updated configuration file to remote host `%s'.\n",
1678 d->phase = SP_CONFIG_UPDATE;
1679 if (NULL != d->username)
1680 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1682 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1683 d->proc_arm_copying = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp",
1687 d->cfgfile, arg, NULL);
1689 if (NULL == d->proc_arm_copying)
1691 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1692 _("Could not start `%s' process to copy configuration file.\n"),
1695 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1696 d->phase = SP_START_DONE;
1700 d->update_cb_cls = cb_cls;
1702 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1707 * Data kept for each pair of peers that we try
1710 struct GNUNET_TESTING_ConnectContext
1713 * Testing handle to the first daemon.
1715 struct GNUNET_TESTING_Daemon *d1;
1718 * Handle to core of first daemon (to check connect)
1720 struct GNUNET_CORE_Handle *d1core;
1723 * Have we actually connected to the core of the first daemon yet?
1728 * Testing handle to the second daemon.
1730 struct GNUNET_TESTING_Daemon *d2;
1733 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1735 struct GNUNET_TRANSPORT_Handle *d1th;
1738 * Function to call once we are done (or have timed out).
1740 GNUNET_TESTING_NotifyConnection cb;
1748 * The relative timeout from whence this connect attempt was
1749 * started. Allows for reconnect attempts.
1751 struct GNUNET_TIME_Relative relative_timeout;
1754 * Maximum number of connect attempts, will retry connection
1755 * this number of times on failures.
1757 unsigned int connect_attempts;
1760 * Hello timeout task
1762 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1765 * Connect timeout task
1767 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1770 * When should this operation be complete (or we must trigger
1773 struct GNUNET_TIME_Relative timeout_hello;
1776 * Was the connection attempt successful?
1781 * When connecting, do we need to send the HELLO?
1786 * The distance between the two connected peers
1792 /** Forward declaration **/
1794 reattempt_daemons_connect (void *cls,
1795 const struct GNUNET_SCHEDULER_TaskContext *tc);
1799 * Notify callback about success or failure of the attempt
1800 * to connect the two peers
1802 * @param cls our "struct GNUNET_TESTING_ConnectContext" (freed)
1803 * @param tc reason tells us if we succeeded or failed
1806 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1808 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1810 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1811 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1813 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1814 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1817 if (ctx->d1th != NULL)
1818 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1820 if (ctx->d1core != NULL)
1821 GNUNET_CORE_disconnect (ctx->d1core);
1824 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1830 if (ctx->connected == GNUNET_YES)
1832 if (ctx->cb != NULL)
1834 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
1835 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1838 else if (ctx->connect_attempts > 0)
1840 ctx->d1core_ready = GNUNET_NO;
1842 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1847 if (ctx->cb != NULL)
1849 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1850 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
1858 * Success, connection is up. Signal client our success.
1860 * @param cls our "struct GNUNET_TESTING_ConnectContext"
1861 * @param peer identity of the peer that has connected
1862 * @param atsi performance information
1863 * @param atsi_count number of records in 'atsi'
1867 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
1868 const struct GNUNET_ATS_Information *atsi,
1869 unsigned int atsi_count)
1871 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peer %s to peer %s\n",
1874 ctx->d1->shortname, GNUNET_i2s (peer));
1875 if (0 != memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1877 ctx->connected = GNUNET_YES;
1878 ctx->distance = 0; /* FIXME: distance */
1879 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1881 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1882 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1884 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1885 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
1890 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1892 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1893 struct GNUNET_MessageHeader *hello;
1895 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1896 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1898 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
1899 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
1900 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
1902 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1903 GNUNET_assert (hello != NULL);
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
1905 ctx->d2->shortname, ctx->d1->shortname);
1906 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1907 GNUNET_assert (ctx->d1core != NULL);
1908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1909 "Sending connect request to TRANSPORT of %s for peer %s\n",
1910 GNUNET_i2s (&ctx->d1->id),
1911 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1912 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
1913 ctx->timeout_hello =
1914 GNUNET_TIME_relative_add (ctx->timeout_hello,
1915 GNUNET_TIME_relative_multiply
1916 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1918 ctx->hello_send_task =
1919 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
1923 * Notify of a successful connection to the core service.
1925 * @param cls a ConnectContext
1926 * @param server handle to the core service
1927 * @param my_identity the peer identity of this peer
1930 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
1931 const struct GNUNET_PeerIdentity *my_identity)
1933 struct GNUNET_TESTING_ConnectContext *connect_ctx = cls;
1935 connect_ctx->d1core_ready = GNUNET_YES;
1937 if (connect_ctx->send_hello == GNUNET_NO)
1939 GNUNET_TRANSPORT_try_connect (connect_ctx->d1th, &connect_ctx->d2->id);
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 "Sending connect request to TRANSPORT of %s for peer %s\n",
1942 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
1948 * Try to connect again some peers that failed in an earlier attempt. This will
1949 * be tried as many times as connection_attempts in the configuration file.
1951 * @param cls Closure (connection context between the two peers).
1952 * @param tc TaskContext.
1955 reattempt_daemons_connect (void *cls,
1956 const struct GNUNET_SCHEDULER_TaskContext *tc)
1958 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1960 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1961 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1964 "re-attempting connect of peer %s to peer %s\n",
1965 ctx->d1->shortname, ctx->d2->shortname);
1966 ctx->connect_attempts--;
1967 GNUNET_assert (ctx->d1core == NULL);
1968 ctx->d1core_ready = GNUNET_NO;
1970 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
1971 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
1972 GNUNET_NO, no_handlers);
1973 if (ctx->d1core == NULL)
1975 if (NULL != ctx->cb)
1976 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1977 ctx->d2->cfg, ctx->d1, ctx->d2,
1978 _("Failed to connect to core service of first peer!\n"));
1983 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
1984 if (NULL != ctx->d2->hello)
1986 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "updating %s's HELLO\n",
1987 ctx->d2->shortname);
1988 GNUNET_free (ctx->d2->hello);
1989 ctx->d2->hello = NULL;
1990 if (NULL != ctx->d2->th)
1992 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->ghh);
1993 ctx->d2->ghh = NULL;
1994 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
1997 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
1999 GNUNET_assert (ctx->d2->th != NULL);
2001 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "didn't have %s's HELLO\n",
2006 ctx->d2->shortname);
2009 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
2011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2012 "didn't have %s's HELLO, trying to get it now\n",
2013 ctx->d2->shortname);
2015 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2017 if (NULL == ctx->d2->th)
2019 GNUNET_CORE_disconnect (ctx->d1core);
2021 if (NULL != ctx->cb)
2022 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2023 ctx->d2->cfg, ctx->d1, ctx->d2,
2024 _("Failed to connect to transport service!\n"));
2028 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2032 if (NULL == ctx->d2->hello)
2034 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2035 "didn't have %s's HELLO but th wasn't NULL, not trying!!\n",
2036 ctx->d2->shortname);
2040 if (ctx->send_hello == GNUNET_YES)
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending %s's HELLO to %s\n",
2043 ctx->d1->shortname, ctx->d2->shortname);
2045 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2047 if (ctx->d1th == NULL)
2049 GNUNET_CORE_disconnect (ctx->d1core);
2051 if (NULL != ctx->cb)
2052 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2053 ctx->d2->cfg, ctx->d1, ctx->d2,
2054 _("Failed to connect to transport service!\n"));
2057 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2058 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to reconnect %s to %s\n",
2063 ctx->d1->shortname, ctx->d2->shortname);
2064 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
2067 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2068 ¬ify_connect_result, ctx);
2072 * Iterator for currently known peers, to ensure
2073 * that we don't try to send duplicate connect
2076 * @param cls our "struct GNUNET_TESTING_ConnectContext"
2077 * @param peer identity of the peer that has connected,
2078 * NULL when iteration has finished
2079 * @param atsi performance information
2080 * @param atsi_count number of records in 'atsi'
2084 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2085 const struct GNUNET_ATS_Information *atsi,
2086 unsigned int atsi_count)
2088 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2090 if ((peer != NULL) &&
2091 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2093 ctx->connected = GNUNET_YES;
2094 ctx->distance = 0; /* FIXME: distance */
2098 return; /* ignore other peers */
2099 /* peer == NULL: End of iteration over peers */
2101 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->timeout_task);
2102 if (ctx->connected == GNUNET_YES)
2104 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2108 /* Peer not already connected, need to schedule connect request! */
2109 if (ctx->d1core == NULL)
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "Peers are NOT connected, connecting to core!\n");
2114 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2115 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
2116 GNUNET_NO, no_handlers);
2119 if (ctx->d1core == NULL)
2121 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2125 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 */
2127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2128 "Don't have d2's HELLO, trying to get it!\n");
2130 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2132 if (ctx->d2->th == NULL)
2134 GNUNET_CORE_disconnect (ctx->d1core);
2137 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2141 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2144 if (ctx->send_hello == GNUNET_YES)
2147 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2149 if (ctx->d1th == NULL)
2151 GNUNET_CORE_disconnect (ctx->d1core);
2154 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2157 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2158 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2162 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2163 ¬ify_connect_result, ctx);
2169 * Establish a connection between two GNUnet daemons. The daemons
2170 * must both be running and not be stopped until either the
2171 * 'cb' callback is called OR the connection request has been
2172 * explicitly cancelled.
2174 * @param d1 handle for the first daemon
2175 * @param d2 handle for the second daemon
2176 * @param timeout how long is the connection attempt
2178 * @param max_connect_attempts how many times should we try to reconnect
2180 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2181 * the HELLO has already been exchanged
2182 * @param cb function to call at the end
2183 * @param cb_cls closure for cb
2184 * @return handle to cancel the request
2186 struct GNUNET_TESTING_ConnectContext *
2187 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2188 struct GNUNET_TESTING_Daemon *d2,
2189 struct GNUNET_TIME_Relative timeout,
2190 unsigned int max_connect_attempts,
2192 GNUNET_TESTING_NotifyConnection cb,
2195 struct GNUNET_TESTING_ConnectContext *ctx;
2197 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2200 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2201 _("Peers are not fully running yet, can not connect!\n"));
2202 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2206 ctx = GNUNET_malloc (sizeof (struct GNUNET_TESTING_ConnectContext));
2209 ctx->timeout_hello =
2210 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2211 ctx->relative_timeout =
2212 GNUNET_TIME_relative_divide (timeout, max_connect_attempts);
2214 ctx->cb_cls = cb_cls;
2215 ctx->connect_attempts = max_connect_attempts;
2216 ctx->connected = GNUNET_NO;
2217 ctx->send_hello = send_hello;
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to connect peer %s to peer %s\n",
2219 d1->shortname, d2->shortname);
2220 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2221 GNUNET_assert (GNUNET_OK ==
2222 GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id,
2223 &core_initial_iteration, ctx));
2229 * Cancel an attempt to connect two daemons.
2231 * @param cc connect context
2234 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext *cc)
2236 if (GNUNET_SCHEDULER_NO_TASK != cc->timeout_task)
2238 GNUNET_SCHEDULER_cancel (cc->timeout_task);
2239 cc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2241 if (GNUNET_SCHEDULER_NO_TASK != cc->hello_send_task)
2243 GNUNET_SCHEDULER_cancel (cc->hello_send_task);
2244 cc->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2246 if (NULL != cc->d1core)
2248 GNUNET_CORE_disconnect (cc->d1core);
2251 if (NULL != cc->d1th)
2253 GNUNET_TRANSPORT_disconnect (cc->d1th);
2260 /* end of testing.c */