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"
41 * Hack to deal with initial HELLO's being often devoid of addresses.
42 * This hack causes 'process_hello' to ignore HELLOs without addresses.
43 * The correct implementation would continue with 'process_hello' until
44 * the connection could be established...
46 #define EMPTY_HACK GNUNET_YES
49 * How long do we wait after starting gnunet-service-arm
50 * for the core service to be alive?
52 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
55 * How many times are we willing to try to wait for "scp" or
56 * "gnunet-service-arm" to complete (waitpid) before giving up?
58 #define MAX_EXEC_WAIT_RUNS 250
60 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
64 test_address (void *cls, const struct GNUNET_HELLO_Address *address,
65 struct GNUNET_TIME_Absolute expiration)
75 * Receive the HELLO from one peer, give it to the other
76 * and ask them to connect.
78 * @param cls Closure (daemon whose hello is this).
79 * @param message HELLO message of peer
82 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
84 struct GNUNET_TESTING_Daemon *daemon = cls;
91 GNUNET_assert (message != NULL);
92 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) message,
93 GNUNET_NO, &test_address, &empty);
94 if (GNUNET_YES == empty)
96 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
97 "Skipping empty HELLO address of peer %s\n",
98 GNUNET_i2s (&daemon->id));
102 GNUNET_assert (daemon->phase == SP_GET_HELLO ||
103 daemon->phase == SP_START_DONE);
104 daemon->cb = NULL; // FIXME: why??? (see fsm:SP_START_CORE, notify_daemon_started)
105 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
106 GNUNET_SCHEDULER_cancel (daemon->task);
108 if (daemon->server != NULL)
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111 "Received `%s' from transport service of `%4s', disconnecting core!\n",
112 "HELLO", GNUNET_i2s (&daemon->id));
113 GNUNET_CORE_disconnect (daemon->server);
114 daemon->server = NULL;
117 msize = ntohs (message->size);
120 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
121 "HELLO message of peer %s is of size 0\n",
122 GNUNET_i2s (&daemon->id));
125 if (daemon->ghh != NULL)
127 GNUNET_TRANSPORT_get_hello_cancel (daemon->ghh);
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Received `%s' from transport service of `%4s'\n", "HELLO",
132 GNUNET_i2s (&daemon->id));
133 GNUNET_free_non_null (daemon->hello);
134 daemon->hello = GNUNET_malloc (msize);
135 memcpy (daemon->hello, message, msize);
137 if (daemon->th != NULL)
139 GNUNET_TRANSPORT_disconnect (daemon->th);
142 daemon->phase = SP_START_DONE;
147 * Notify of a peer being up and running. Scheduled as a task
148 * so that variables which may need to be set are set before
149 * the connect callback can set up new operations.
150 * FIXME: what variables?????? where from????
152 * @param cls the testing daemon
153 * @param tc task scheduler context
156 notify_daemon_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
158 struct GNUNET_TESTING_Daemon *d = cls;
159 GNUNET_TESTING_NotifyDaemonRunning cb;
164 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
169 * Finite-state machine for starting GNUnet.
171 * @param cls our "struct GNUNET_TESTING_Daemon"
175 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
177 struct GNUNET_TESTING_Daemon *d = cls;
178 GNUNET_TESTING_NotifyDaemonRunning cb;
179 enum GNUNET_OS_ProcessStatusType type;
184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %s FSM is in phase %u.\n",
185 GNUNET_i2s (&d->id), d->phase);
186 d->task = GNUNET_SCHEDULER_NO_TASK;
190 /* confirm copying complete */
191 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_copying, &type, &code))
193 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
198 cb (d->cb_cls, NULL, d->cfg, d,
200 ("`scp' does not seem to terminate (timeout copying config).\n"));
205 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
209 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
214 cb (d->cb_cls, NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
217 GNUNET_OS_process_close (d->proc_arm_copying);
218 d->proc_arm_copying = NULL;
219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
220 "Successfully copied configuration file.\n");
221 d->phase = SP_COPIED;
224 /* Start create hostkey process if we don't already know the peer identity! */
225 if (GNUNET_NO == d->have_hostkey)
227 GNUNET_assert (NULL == d->proc_arm_peerinfo);
228 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_YES);
229 if (d->pipe_stdout == NULL)
234 cb (d->cb_cls, NULL, d->cfg, d,
237 _("Failed to create pipe for `gnunet-peerinfo' process.\n") :
238 _("Failed to create pipe for `ssh' process.\n"));
241 if (NULL == d->hostname)
243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
244 "Starting `%s', with command `%s %s %s %s'.\n",
245 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
247 d->proc_arm_peerinfo =
248 GNUNET_OS_start_process (GNUNET_YES, NULL, d->pipe_stdout, "gnunet-peerinfo",
249 "gnunet-peerinfo", "-c", d->cfgfile, "-sq",
251 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
255 if (d->username != NULL)
256 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
258 dst = GNUNET_strdup (d->hostname);
260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
261 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
262 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
264 if (d->ssh_port_str == NULL)
266 d->proc_arm_peerinfo = GNUNET_OS_start_process (GNUNET_NO, NULL, d->pipe_stdout, "ssh", "ssh",
268 dst, "gnunet-peerinfo", "-c",
269 d->cfgfile, "-sq", NULL);
273 d->proc_arm_peerinfo =
274 GNUNET_OS_start_process (GNUNET_NO, NULL, d->pipe_stdout, "ssh", "ssh", "-p",
277 dst, "gnunet-peerinfo", "-c", d->cfgfile,
280 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
283 if (NULL == d->proc_arm_peerinfo)
285 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
286 _("Could not start `%s' process to create hostkey.\n"),
287 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
291 cb (d->cb_cls, NULL, d->cfg, d,
293 d->hostname) ? _("Failed to start `gnunet-peerinfo' process.\n")
294 : _("Failed to start `ssh' process.\n"));
295 GNUNET_DISK_pipe_close (d->pipe_stdout);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
300 d->phase = SP_HOSTKEY_CREATE;
302 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
304 GNUNET_DISK_pipe_handle
306 GNUNET_DISK_PIPE_END_READ),
309 else /* Already have a hostkey! */
311 if (d->hostkey_callback != NULL)
313 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
314 d->hostkey_callback = NULL;
315 d->phase = SP_HOSTKEY_CREATED;
318 d->phase = SP_TOPOLOGY_SETUP;
321 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
324 case SP_HOSTKEY_CREATE:
326 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
327 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
328 &d->hostkeybuf[d->hostkeybufpos],
329 sizeof (d->hostkeybuf) - d->hostkeybufpos);
331 d->hostkeybufpos += bytes_read;
333 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
337 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
339 GNUNET_DISK_pipe_handle
341 GNUNET_DISK_PIPE_END_READ),
345 d->hostkeybuf[103] = '\0';
347 if ((bytes_read < 0) ||
349 GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey)))
353 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
354 _("Error reading from gnunet-peerinfo: %s\n"),
357 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
358 _("Malformed output from gnunet-peerinfo!\n"));
361 GNUNET_DISK_pipe_close (d->pipe_stdout);
362 d->pipe_stdout = NULL;
363 (void) GNUNET_OS_process_kill (d->proc_arm_peerinfo, SIGKILL);
364 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc_arm_peerinfo));
365 GNUNET_OS_process_close (d->proc_arm_peerinfo);
366 d->proc_arm_peerinfo = NULL;
368 cb (d->cb_cls, NULL, d->cfg, d, _("Failed to get hostkey!\n"));
371 d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
372 GNUNET_DISK_pipe_close (d->pipe_stdout);
373 d->pipe_stdout = NULL;
374 (void) GNUNET_OS_process_kill (d->proc_arm_peerinfo, SIGKILL);
375 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc_arm_peerinfo));
376 GNUNET_OS_process_close (d->proc_arm_peerinfo);
377 d->proc_arm_peerinfo = NULL;
378 d->have_hostkey = GNUNET_YES;
379 if (d->hostkey_callback != NULL)
381 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
382 d->hostkey_callback = NULL;
383 d->phase = SP_HOSTKEY_CREATED;
387 d->phase = SP_TOPOLOGY_SETUP;
389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
391 case SP_HOSTKEY_CREATED:
392 /* wait for topology finished */
393 if ((GNUNET_YES == d->dead) ||
394 (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
399 cb (d->cb_cls, NULL, d->cfg, d,
400 _("`Failed while waiting for topology setup!\n"));
405 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
408 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
409 /* start GNUnet on remote host */
410 if (NULL == d->hostname)
412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
413 "Starting `%s', with command `%s %s %s %s'.\n",
414 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
417 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm", "-c",
420 GNUNET_TIME_relative_to_string
421 (GNUNET_TIME_absolute_get_remaining
422 (d->max_timeout)), NULL);
426 if (d->username != NULL)
427 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
429 dst = GNUNET_strdup (d->hostname);
431 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
432 "Starting `%s', with command `%s %s %s %s %s %s %s'.\n",
433 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
435 if (d->ssh_port_str == NULL)
437 d->proc_arm_start = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
440 "-c", d->cfgfile, "-s", "-q", "-T",
441 GNUNET_TIME_relative_to_string
442 (GNUNET_TIME_absolute_get_remaining
443 (d->max_timeout)), NULL);
449 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh", "-p",
453 "-c", d->cfgfile, "-s", "-q", "-T",
454 GNUNET_TIME_relative_to_string
455 (GNUNET_TIME_absolute_get_remaining
456 (d->max_timeout)), NULL);
460 if (NULL == d->proc_arm_start)
462 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
463 _("Could not start `%s' process to start GNUnet.\n"),
464 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
468 cb (d->cb_cls, NULL, d->cfg, d,
470 d->hostname) ? _("Failed to start `gnunet-arm' process.\n") :
471 _("Failed to start `ssh' process.\n"));
474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
475 "Started `%s', waiting for `%s' to be up.\n", "gnunet-arm",
476 "gnunet-service-core");
477 d->phase = SP_START_ARMING;
479 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
482 case SP_START_ARMING:
483 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_start, &type, &code))
485 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
490 cb (d->cb_cls, NULL, d->cfg, d,
492 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
493 _("`ssh' does not seem to terminate.\n"));
496 GNUNET_CONFIGURATION_destroy (d->cfg);
499 if (d->cfgfile != NULL)
501 GNUNET_free (d->cfgfile);
504 GNUNET_free_non_null (d->hostname);
505 GNUNET_free_non_null (d->username);
506 GNUNET_free (d->proc_arm_start);
507 d->proc_arm_start = NULL;
508 d->hostname = NULL; // Quick hack to avoid crashing (testing need to be
509 d->cfg = NULL; // overhauled anyway, and the error managing is
510 // GNUNET_free (d); // FIXME (could this leak)
511 // pretty broken anyway.
516 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
520 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully started `%s'.\n",
522 GNUNET_free (d->proc_arm_start);
523 d->proc_arm_start = NULL;
524 d->phase = SP_START_CORE;
525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling CORE_connect\n");
528 if (d->server != NULL)
529 GNUNET_CORE_disconnect (d->server);
531 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
534 if (GNUNET_YES == d->dead)
535 GNUNET_TESTING_daemon_stop (d,
536 GNUNET_TIME_absolute_get_remaining
537 (d->max_timeout), d->dead_cb,
538 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
539 else if (NULL != d->cb)
540 d->cb (d->cb_cls, &d->id, d->cfg, d,
541 _("Failed to connect to transport service!\n"));
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Connected to transport service `%s', getting HELLO\n",
546 GNUNET_i2s (&d->id));
547 d->ghh = GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
548 /* FIXME: store task ID somewhere! */
549 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
553 * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
554 d->running = GNUNET_YES;
555 d->phase = SP_GET_HELLO;
558 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
560 if (d->server != NULL)
561 GNUNET_CORE_disconnect (d->server);
563 GNUNET_TRANSPORT_disconnect (d->th);
567 cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
568 GNUNET_CONFIGURATION_destroy (d->cfg);
569 GNUNET_free (d->cfgfile);
570 GNUNET_free_non_null (d->hostname);
571 GNUNET_free_non_null (d->username);
575 if (d->hello != NULL)
577 GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
579 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
580 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
586 case SP_SERVICE_START:
587 /* confirm gnunet-arm exited */
588 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_srv_start, &type, &code))
590 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
595 cb (d->cb_cls, NULL, d->cfg, d,
597 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
598 _("`ssh' does not seem to terminate.\n"));
603 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
608 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
613 cb (d->cb_cls, NULL, d->cfg, d,
617 ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
618 : _("`ssh' does not seem to terminate.\n"));
622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
625 d->phase = SP_START_DONE;
627 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
628 GNUNET_free (d->proc_arm_srv_start);
629 d->proc_arm_srv_start = NULL;
631 case SP_SERVICE_SHUTDOWN_START:
632 /* confirm copying complete */
633 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_srv_stop, &type, &code))
635 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
637 if (NULL != d->dead_cb)
638 d->dead_cb (d->dead_cb_cls,
640 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
645 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
650 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
652 if (NULL != d->dead_cb)
653 d->dead_cb (d->dead_cb_cls,
655 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
660 if (NULL != d->dead_cb)
661 d->dead_cb (d->dead_cb_cls, NULL);
663 case SP_SHUTDOWN_START:
664 /* confirm copying complete !??? */
665 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_stop, &type, &code))
667 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
669 if (NULL != d->dead_cb)
670 d->dead_cb (d->dead_cb_cls,
672 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
675 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
677 GNUNET_TRANSPORT_disconnect (d->th);
682 GNUNET_CONFIGURATION_destroy (d->cfg);
685 if (d->cfgfile != NULL)
687 GNUNET_free (d->cfgfile);
690 GNUNET_free_non_null (d->hello);
691 GNUNET_free_non_null (d->hostname);
692 GNUNET_free_non_null (d->username);
693 GNUNET_free_non_null (d->shortname);
694 // GNUNET_free_non_null (d->proc); // !? FIXME
695 // d->proc_arm_stop = NULL;
701 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
705 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
707 if (NULL != d->dead_cb)
708 d->dead_cb (d->dead_cb_cls,
710 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
713 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
715 GNUNET_TRANSPORT_disconnect (d->th);
718 if (d->server != NULL)
720 GNUNET_CORE_disconnect (d->server);
723 GNUNET_CONFIGURATION_destroy (d->cfg);
725 GNUNET_free (d->cfgfile);
726 GNUNET_free_non_null (d->hello);
727 GNUNET_free_non_null (d->hostname);
728 GNUNET_free_non_null (d->username);
729 GNUNET_free_non_null (d->shortname);
730 // GNUNET_free_non_null (d->proc); // !? FIXME!
731 // d->proc = NULL; // !? FIXME!
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
736 if (d->server != NULL)
738 GNUNET_CORE_disconnect (d->server);
744 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
746 GNUNET_TRANSPORT_disconnect (d->th);
750 if (NULL != d->dead_cb)
751 d->dead_cb (d->dead_cb_cls, NULL);
753 /* state clean up and notifications */
754 if (d->churn == GNUNET_NO)
756 GNUNET_CONFIGURATION_destroy (d->cfg);
758 GNUNET_free (d->cfgfile);
759 GNUNET_free_non_null (d->hostname);
760 GNUNET_free_non_null (d->username);
763 GNUNET_free_non_null (d->hello);
765 GNUNET_free_non_null (d->shortname);
766 // GNUNET_free_non_null (d->proc); // !? FIXME
767 // d->proc = NULL; // !? FIXME!
769 if (d->churn == GNUNET_NO)
773 case SP_CONFIG_UPDATE:
774 /* confirm copying complete */
775 if (GNUNET_OK != GNUNET_OS_process_status (d->proc_arm_copying, &type, &code))
777 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
782 cb (d->cb_cls, NULL, d->cfg, d,
783 _("`scp' does not seem to terminate.\n"));
788 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
792 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
794 if (NULL != d->update_cb)
795 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
799 "Successfully copied configuration file.\n");
800 if (NULL != d->update_cb)
801 d->update_cb (d->update_cb_cls, NULL);
802 d->phase = SP_START_DONE;
808 * Continues GNUnet daemon startup when user wanted to be notified
809 * once a hostkey was generated (for creating friends files, blacklists,
812 * @param daemon the daemon to finish starting
815 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
817 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
818 daemon->phase = SP_TOPOLOGY_SETUP;
822 * Check whether the given daemon is running.
824 * @param daemon the daemon to check
826 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
827 * daemon is down, GNUNET_SYSERR on error.
830 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
833 return GNUNET_SYSERR;
835 if (daemon->running == GNUNET_YES)
842 * Starts a GNUnet daemon service which has been previously stopped.
844 * @param d the daemon for which the service should be started
845 * @param service the name of the service to start
846 * @param timeout how long to wait for process for shutdown to complete
847 * @param cb function called once the service starts
848 * @param cb_cls closure for cb
851 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
853 struct GNUNET_TIME_Relative
855 GNUNET_TESTING_NotifyDaemonRunning
863 GNUNET_assert (d->running == GNUNET_YES);
865 if (d->phase == SP_CONFIG_UPDATE)
867 GNUNET_SCHEDULER_cancel (d->task);
868 d->phase = SP_START_DONE;
871 if (d->churned_services == NULL)
873 d->cb (d->cb_cls, &d->id, d->cfg, d,
874 "No service has been churned off yet!!");
877 d->phase = SP_SERVICE_START;
878 GNUNET_free (d->churned_services);
879 d->churned_services = NULL;
880 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
881 /* Check if this is a local or remote process */
882 if (NULL != d->hostname)
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 "Starting gnunet-arm with config `%s' on host `%s'.\n",
886 d->cfgfile, d->hostname);
887 if (d->username != NULL)
888 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
890 arg = GNUNET_strdup (d->hostname);
892 d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
895 "-c", d->cfgfile, "-i", service, "-q",
897 GNUNET_TIME_relative_to_string (timeout),
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
901 arg, "gnunet-arm", d->cfgfile, service);
906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
907 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
908 d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
909 "-c", d->cfgfile, "-i", service, "-q",
911 GNUNET_TIME_relative_to_string (timeout),
915 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
916 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
920 * Starts a GNUnet daemon's service.
922 * @param d the daemon for which the service should be started
923 * @param service the name of the service to start
924 * @param timeout how long to wait for process for startup
925 * @param cb function called once gnunet-arm returns
926 * @param cb_cls closure for cb
929 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
931 struct GNUNET_TIME_Relative timeout,
932 GNUNET_TESTING_NotifyDaemonRunning cb,
940 GNUNET_assert (service != NULL);
941 GNUNET_assert (d->running == GNUNET_YES);
942 GNUNET_assert (d->phase == SP_START_DONE);
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 _("Starting service %s for peer `%4s'\n"), service,
946 GNUNET_i2s (&d->id));
947 d->phase = SP_SERVICE_START;
948 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
949 /* Check if this is a local or remote process */
950 if (NULL != d->hostname)
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Starting gnunet-arm with config `%s' on host `%s'.\n",
954 d->cfgfile, d->hostname);
955 if (d->username != NULL)
956 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
958 arg = GNUNET_strdup (d->hostname);
960 d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
963 "-c", d->cfgfile, "-i", service, "-q",
965 GNUNET_TIME_relative_to_string (timeout),
967 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
968 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
969 arg, "gnunet-arm", d->cfgfile, service,
970 GNUNET_TIME_relative_to_string (timeout));
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
977 d->proc_arm_srv_start = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
978 "-c", d->cfgfile, "-i", service, "-q",
980 GNUNET_TIME_relative_to_string (timeout),
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
984 "gnunet-arm", d->cfgfile, service,
985 GNUNET_TIME_relative_to_string (timeout));
988 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
989 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
993 * Start a peer that has previously been stopped using the daemon_stop
994 * call (and files weren't deleted and the allow restart flag)
996 * @param daemon the daemon to start (has been previously stopped)
997 * @param timeout how long to wait for restart
998 * @param cb the callback for notification when the peer is running
999 * @param cb_cls closure for the callback
1002 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1003 struct GNUNET_TIME_Relative timeout,
1004 GNUNET_TESTING_NotifyDaemonRunning cb,
1007 if (daemon->running == GNUNET_YES)
1009 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1010 "Daemon already running, can't restart!");
1015 daemon->cb_cls = cb_cls;
1016 daemon->phase = SP_TOPOLOGY_SETUP;
1017 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1018 /* FIXME: why add_continuation? */
1019 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1020 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1024 * Starts a GNUnet daemon. GNUnet must be installed on the target
1025 * system and available in the PATH. The machine must furthermore be
1026 * reachable via "ssh" (unless the hostname is "NULL") without the
1027 * need to enter a password.
1029 * @param cfg configuration to use
1030 * @param timeout how long to wait starting up peers
1031 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1032 * to really start the peer (default)
1033 * @param hostname name of the machine where to run GNUnet
1034 * (use NULL for localhost).
1035 * @param ssh_username ssh username to use when connecting to hostname
1036 * @param sshport port to pass to ssh process when connecting to hostname
1037 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1038 * @param hostkey_callback function to call once the hostkey has been
1039 * generated for this peer, but it hasn't yet been started
1040 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1041 * @param hostkey_cls closure for hostkey callback
1042 * @param cb function to call once peer is up, or failed to start
1043 * @param cb_cls closure for cb
1044 * @return handle to the daemon (actual start will be completed asynchronously)
1046 struct GNUNET_TESTING_Daemon *
1047 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1048 struct GNUNET_TIME_Relative timeout, int pretend,
1049 const char *hostname, const char *ssh_username,
1050 uint16_t sshport, const char *hostkey,
1051 GNUNET_TESTING_NotifyHostkeyCreated
1052 hostkey_callback, void *hostkey_cls,
1053 GNUNET_TESTING_NotifyDaemonRunning cb,
1056 struct GNUNET_TESTING_Daemon *ret;
1060 char *baseservicehome;
1063 char *temp_file_name;
1064 struct GNUNET_DISK_FileHandle *fn;
1065 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1066 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1068 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1069 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1072 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1075 ret->ssh_port_str = NULL;
1077 /* Find service home and base service home directories, create it if it doesn't exist */
1078 GNUNET_assert (GNUNET_OK ==
1079 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1083 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1084 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1085 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1086 GNUNET_free (temp_file_name);
1087 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1088 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1089 if (NULL == ret->cfgfile)
1091 GNUNET_free_non_null (ret->ssh_port_str);
1092 GNUNET_free_non_null (ret->hostname);
1096 ret->hostkey_callback = hostkey_callback;
1097 ret->hostkey_cls = hostkey_cls;
1099 ret->cb_cls = cb_cls;
1100 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1101 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1102 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1105 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1107 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1108 GNUNET_assert (private_key != NULL);
1109 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1110 GNUNET_CRYPTO_hash (&public_key,
1111 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1112 &ret->id.hashPubKey);
1113 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1114 ret->have_hostkey = GNUNET_YES;
1115 GNUNET_CRYPTO_rsa_key_free (private_key);
1118 /* Write hostkey to file, if we were given one */
1120 if (hostkey != NULL)
1122 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1123 fn = GNUNET_DISK_file_open (hostkeyfile,
1124 GNUNET_DISK_OPEN_READWRITE |
1125 GNUNET_DISK_OPEN_CREATE,
1126 GNUNET_DISK_PERM_USER_READ |
1127 GNUNET_DISK_PERM_USER_WRITE);
1128 GNUNET_assert (fn != NULL);
1129 GNUNET_assert (HOSTKEYFILESIZE ==
1130 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1131 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1134 /* write configuration to temporary file */
1135 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1137 if (0 != UNLINK (ret->cfgfile))
1138 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1140 GNUNET_CONFIGURATION_destroy (ret->cfg);
1141 GNUNET_free_non_null (ret->hostname);
1142 GNUNET_free (ret->cfgfile);
1146 if (ssh_username != NULL)
1147 username = GNUNET_strdup (ssh_username);
1148 if ((ssh_username == NULL) &&
1150 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1153 if (NULL != getenv ("USER"))
1154 username = GNUNET_strdup (getenv ("USER"));
1158 ret->username = username;
1160 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1162 /* copy directory to remote host */
1163 if (NULL != hostname)
1165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1166 "Copying configuration directory to host `%s'.\n", hostname);
1167 baseservicehome = GNUNET_strdup (servicehome);
1168 /* Remove trailing /'s */
1169 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1170 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1171 /* Find next directory /, jump one ahead */
1172 slash = strrchr (baseservicehome, '/');
1176 ret->phase = SP_COPYING;
1177 if (NULL != username)
1178 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1180 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1181 GNUNET_free (baseservicehome);
1182 if (ret->ssh_port_str == NULL)
1184 ret->proc_arm_copying = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp", "-r",
1186 servicehome, arg, NULL);
1187 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1188 "copying directory with command scp -r %s %s\n",
1193 ret->proc_arm_copying =
1194 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp", "-r", "-P",
1197 servicehome, arg, NULL);
1200 if (NULL == ret->proc_arm_copying)
1202 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1204 ("Could not start `%s' process to copy configuration directory.\n"),
1206 if (0 != UNLINK (ret->cfgfile))
1207 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1209 GNUNET_CONFIGURATION_destroy (ret->cfg);
1210 GNUNET_free_non_null (ret->hostname);
1211 GNUNET_free_non_null (ret->username);
1212 GNUNET_free (ret->cfgfile);
1214 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1215 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1217 GNUNET_free_non_null (hostkeyfile);
1218 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1219 GNUNET_free (servicehome);
1224 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1226 GNUNET_free_non_null (hostkeyfile);
1227 GNUNET_free (servicehome);
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "No need to copy configuration file since we are running locally.\n");
1232 ret->phase = SP_COPIED;
1233 /* FIXME: why add_cont? */
1234 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1235 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1237 GNUNET_free_non_null (hostkeyfile);
1238 GNUNET_free (servicehome);
1244 * Restart (stop and start) a GNUnet daemon.
1246 * @param d the daemon that should be restarted
1247 * @param cb function called once the daemon is (re)started
1248 * @param cb_cls closure for cb
1251 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1252 GNUNET_TESTING_NotifyDaemonRunning cb,
1261 d->dead = GNUNET_YES;
1268 if (d->phase == SP_CONFIG_UPDATE)
1270 GNUNET_SCHEDULER_cancel (d->task);
1271 d->phase = SP_START_DONE;
1273 if (d->server != NULL)
1275 GNUNET_CORE_disconnect (d->server);
1281 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1283 GNUNET_TRANSPORT_disconnect (d->th);
1286 /* state clean up and notifications */
1287 GNUNET_free_non_null (d->hello);
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1290 GNUNET_i2s (&d->id));
1291 d->phase = SP_START_ARMING;
1293 /* Check if this is a local or remote process */
1294 if (NULL != d->hostname)
1296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1297 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1298 d->cfgfile, d->hostname);
1299 if (d->username != NULL)
1300 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1302 arg = GNUNET_strdup (d->hostname);
1304 d->proc_arm_stop = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
1307 "-c", d->cfgfile, "-e", "-r", NULL);
1308 /* Use -r to restart arm and all services */
1314 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1315 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1316 d->proc_arm_stop = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
1317 "-c", d->cfgfile, "-e", "-r", NULL);
1320 GNUNET_free_non_null (del_arg);
1322 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1328 * Stops a GNUnet daemon.
1330 * @param d the daemon that should be stopped
1331 * @param service the name of the service to stop
1332 * @param timeout how long to wait for process for shutdown to complete
1333 * @param cb function called once the daemon was stopped
1334 * @param cb_cls closure for cb
1337 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1338 const char *service,
1339 struct GNUNET_TIME_Relative timeout,
1340 GNUNET_TESTING_NotifyCompletion cb,
1346 d->dead_cb_cls = cb_cls;
1348 GNUNET_assert (d->running == GNUNET_YES);
1350 if (d->phase == SP_CONFIG_UPDATE)
1352 GNUNET_SCHEDULER_cancel (d->task);
1353 d->phase = SP_START_DONE;
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1357 GNUNET_i2s (&d->id));
1358 if (d->churned_services != NULL)
1360 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1363 d->phase = SP_SERVICE_SHUTDOWN_START;
1364 d->churned_services = GNUNET_strdup (service);
1365 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1366 /* Check if this is a local or remote process */
1367 if (NULL != d->hostname)
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1371 d->cfgfile, d->hostname);
1372 if (d->username != NULL)
1373 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1375 arg = GNUNET_strdup (d->hostname);
1377 d->proc_arm_srv_stop = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
1380 "-c", d->cfgfile, "-k", service, "-q",
1382 GNUNET_TIME_relative_to_string (timeout),
1384 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1385 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1386 arg, "gnunet-arm", d->cfgfile, service);
1391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1392 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1393 d->proc_arm_srv_stop = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
1394 "-c", d->cfgfile, "-k", service, "-q",
1396 GNUNET_TIME_relative_to_string (timeout),
1400 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1401 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1406 * Forcefully terminate a process and clean up the child.
1408 * @param proc handle to process to kill
1411 kill_and_close_process (struct GNUNET_OS_Process *proc)
1413 (void) GNUNET_OS_process_kill (proc, SIGKILL);
1414 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (proc));
1415 GNUNET_OS_process_close (proc);
1420 * Stops a GNUnet daemon.
1422 * @param d the daemon that should be stopped
1423 * @param timeout how long to wait for process for shutdown to complete
1424 * @param cb function called once the daemon was stopped
1425 * @param cb_cls closure for cb
1426 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1428 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1429 * GNUNET_NO to kill off and clean up for good
1432 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1433 struct GNUNET_TIME_Relative timeout,
1434 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1435 int delete_files, int allow_restart)
1441 d->dead_cb_cls = cb_cls;
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1446 GNUNET_i2s (&d->id));
1447 d->dead = GNUNET_YES;
1450 if (NULL != d->proc_arm_start)
1452 kill_and_close_process (d->proc_arm_start);
1453 d->proc_arm_start = NULL;
1455 if (NULL != d->proc_arm_srv_start)
1457 kill_and_close_process (d->proc_arm_srv_start);
1458 d->proc_arm_srv_start = NULL;
1460 if (NULL != d->proc_arm_srv_stop)
1462 kill_and_close_process (d->proc_arm_srv_stop);
1463 d->proc_arm_srv_stop = NULL;
1465 if (NULL != d->proc_arm_copying)
1467 kill_and_close_process (d->proc_arm_copying);
1468 d->proc_arm_copying = NULL;
1470 if (NULL != d->proc_arm_peerinfo)
1472 kill_and_close_process (d->proc_arm_peerinfo);
1473 d->proc_arm_peerinfo = NULL;
1475 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1477 /* Free what was left from churning! */
1478 GNUNET_assert (d->cfg != NULL);
1479 GNUNET_CONFIGURATION_destroy (d->cfg);
1480 if (delete_files == GNUNET_YES)
1482 if (0 != UNLINK (d->cfgfile))
1484 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1487 GNUNET_free (d->cfgfile);
1488 GNUNET_free_non_null (d->hostname);
1489 GNUNET_free_non_null (d->username);
1490 if (NULL != d->dead_cb)
1491 d->dead_cb (d->dead_cb_cls, NULL);
1492 /* FIXME: this should be an assert and the test below
1493 should not be required, but testing is broken... */
1494 GNUNET_break (NULL == d->proc_arm_stop);
1495 if (NULL == d->proc_arm_stop)
1501 if (delete_files == GNUNET_YES)
1503 GNUNET_asprintf (&del_arg, "-d");
1506 if (d->phase == SP_CONFIG_UPDATE)
1508 GNUNET_SCHEDULER_cancel (d->task);
1509 d->phase = SP_START_DONE;
1511 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1512 if (d->server != NULL)
1514 GNUNET_CORE_disconnect (d->server);
1518 /* shutdown ARM process (will terminate others) */
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Terminating peer `%4s'\n" ,
1520 GNUNET_i2s (&d->id));
1521 d->phase = SP_SHUTDOWN_START;
1522 d->running = GNUNET_NO;
1523 if (allow_restart == GNUNET_YES)
1524 d->churn = GNUNET_YES;
1527 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1529 GNUNET_TRANSPORT_disconnect (d->th);
1532 /* Check if this is a local or remote process */
1535 if (NULL != d->hostname)
1537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1538 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1539 d->cfgfile, d->hostname);
1540 if (d->username != NULL)
1541 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1543 arg = GNUNET_strdup (d->hostname);
1545 d->proc_arm_stop = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
1548 "-c", d->cfgfile, "-e", "-q", "-T",
1549 GNUNET_TIME_relative_to_string (timeout),
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1553 arg, "gnunet-arm", d->cfgfile, del_arg);
1554 /* Use -e to end arm, and -d to remove temp files */
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1561 d->proc_arm_stop = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "gnunet-arm", "gnunet-arm",
1562 "-c", d->cfgfile, "-e", "-q", "-T",
1563 GNUNET_TIME_relative_to_string (timeout),
1565 GNUNET_assert (NULL != d->proc_arm_stop);
1568 GNUNET_free_non_null (del_arg);
1569 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1570 if (GNUNET_SCHEDULER_NO_TASK != d->task)
1571 GNUNET_SCHEDULER_cancel(d->task);
1572 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1577 * Changes the configuration of a GNUnet daemon.
1579 * @param d the daemon that should be modified
1580 * @param cfg the new configuration for the daemon
1581 * @param cb function called once the configuration was changed
1582 * @param cb_cls closure for cb
1585 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1586 struct GNUNET_CONFIGURATION_Handle *cfg,
1587 GNUNET_TESTING_NotifyCompletion cb,
1592 if (d->phase != SP_START_DONE)
1597 ("Peer not yet running, can not change configuration at this point."));
1601 /* 1) write configuration to temporary file */
1602 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1605 cb (cb_cls, _("Failed to write new configuration to disk."));
1609 /* 2) copy file to remote host (if necessary) */
1610 if (NULL == d->hostname)
1612 /* signal success */
1617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1618 "Copying updated configuration file to remote host `%s'.\n",
1620 d->phase = SP_CONFIG_UPDATE;
1621 if (NULL != d->username)
1622 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1624 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1625 d->proc_arm_copying = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp",
1627 d->cfgfile, arg, NULL);
1629 if (NULL == d->proc_arm_copying)
1631 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1632 _("Could not start `%s' process to copy configuration file.\n"),
1635 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1636 d->phase = SP_START_DONE;
1640 d->update_cb_cls = cb_cls;
1642 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1647 * Data kept for each pair of peers that we try
1650 struct GNUNET_TESTING_ConnectContext
1653 * Testing handle to the first daemon.
1655 struct GNUNET_TESTING_Daemon *d1;
1658 * Handle to core of first daemon (to check connect)
1660 struct GNUNET_CORE_Handle *d1core;
1663 * Have we actually connected to the core of the first daemon yet?
1668 * Testing handle to the second daemon.
1670 struct GNUNET_TESTING_Daemon *d2;
1673 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1675 struct GNUNET_TRANSPORT_Handle *d1th;
1678 * Function to call once we are done (or have timed out).
1680 GNUNET_TESTING_NotifyConnection cb;
1688 * The relative timeout from whence this connect attempt was
1689 * started. Allows for reconnect attempts.
1691 struct GNUNET_TIME_Relative relative_timeout;
1694 * Maximum number of connect attempts, will retry connection
1695 * this number of times on failures.
1697 unsigned int connect_attempts;
1700 * Hello timeout task
1702 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1705 * Connect timeout task
1707 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1710 * When should this operation be complete (or we must trigger
1713 struct GNUNET_TIME_Relative timeout_hello;
1716 * Was the connection attempt successful?
1721 * When connecting, do we need to send the HELLO?
1726 * The distance between the two connected peers
1732 /** Forward declaration **/
1734 reattempt_daemons_connect (void *cls,
1735 const struct GNUNET_SCHEDULER_TaskContext *tc);
1739 * Notify callback about success or failure of the attempt
1740 * to connect the two peers
1742 * @param cls our "struct GNUNET_TESTING_ConnectContext" (freed)
1743 * @param tc reason tells us if we succeeded or failed
1746 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1748 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1750 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1751 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1753 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1754 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1757 if (ctx->d1th != NULL)
1758 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1760 if (ctx->d1core != NULL)
1761 GNUNET_CORE_disconnect (ctx->d1core);
1764 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1770 if (ctx->connected == GNUNET_YES)
1772 if (ctx->cb != NULL)
1774 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
1775 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1778 else if (ctx->connect_attempts > 0)
1780 ctx->d1core_ready = GNUNET_NO;
1782 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1787 if (ctx->cb != NULL)
1789 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1790 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
1798 * Success, connection is up. Signal client our success.
1800 * @param cls our "struct GNUNET_TESTING_ConnectContext"
1801 * @param peer identity of the peer that has connected
1802 * @param atsi performance information
1803 * @param atsi_count number of records in 'atsi'
1807 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
1808 const struct GNUNET_ATS_Information *atsi,
1809 unsigned int atsi_count)
1811 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peer %s to peer %s\n",
1814 ctx->d1->shortname, GNUNET_i2s (peer));
1815 if (0 != memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1817 ctx->connected = GNUNET_YES;
1818 ctx->distance = 0; /* FIXME: distance */
1819 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1821 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1822 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1824 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1825 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
1830 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1832 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1833 struct GNUNET_MessageHeader *hello;
1835 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1836 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1838 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
1839 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
1840 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
1842 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1843 GNUNET_assert (hello != NULL);
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
1845 ctx->d2->shortname, ctx->d1->shortname);
1846 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1847 GNUNET_assert (ctx->d1core != NULL);
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1849 "Sending connect request to TRANSPORT of %s for peer %s\n",
1850 GNUNET_i2s (&ctx->d1->id),
1851 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1852 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
1853 ctx->timeout_hello =
1854 GNUNET_TIME_relative_add (ctx->timeout_hello,
1855 GNUNET_TIME_relative_multiply
1856 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1858 ctx->hello_send_task =
1859 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
1863 * Notify of a successful connection to the core service.
1865 * @param cls a ConnectContext
1866 * @param server handle to the core service
1867 * @param my_identity the peer identity of this peer
1870 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
1871 const struct GNUNET_PeerIdentity *my_identity)
1873 struct GNUNET_TESTING_ConnectContext *connect_ctx = cls;
1875 connect_ctx->d1core_ready = GNUNET_YES;
1877 if (connect_ctx->send_hello == GNUNET_NO)
1879 GNUNET_TRANSPORT_try_connect (connect_ctx->d1th, &connect_ctx->d2->id);
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1881 "Sending connect request to TRANSPORT of %s for peer %s\n",
1882 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
1888 * Try to connect again some peers that failed in an earlier attempt. This will
1889 * be tried as many times as connection_attempts in the configuration file.
1891 * @param cls Closure (connection context between the two peers).
1892 * @param tc TaskContext.
1895 reattempt_daemons_connect (void *cls,
1896 const struct GNUNET_SCHEDULER_TaskContext *tc)
1898 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1900 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1901 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1904 "re-attempting connect of peer %s to peer %s\n",
1905 ctx->d1->shortname, ctx->d2->shortname);
1906 ctx->connect_attempts--;
1907 GNUNET_assert (ctx->d1core == NULL);
1908 ctx->d1core_ready = GNUNET_NO;
1910 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
1911 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
1912 GNUNET_NO, no_handlers);
1913 if (ctx->d1core == NULL)
1915 if (NULL != ctx->cb)
1916 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1917 ctx->d2->cfg, ctx->d1, ctx->d2,
1918 _("Failed to connect to core service of first peer!\n"));
1923 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
1924 if (NULL != ctx->d2->hello)
1926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "updating %s's HELLO\n",
1927 ctx->d2->shortname);
1928 GNUNET_free (ctx->d2->hello);
1929 ctx->d2->hello = NULL;
1930 if (NULL != ctx->d2->th)
1932 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->ghh);
1933 ctx->d2->ghh = NULL;
1934 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
1937 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
1939 GNUNET_assert (ctx->d2->th != NULL);
1941 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "didn't have %s's HELLO\n",
1946 ctx->d2->shortname);
1949 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1952 "didn't have %s's HELLO, trying to get it now\n",
1953 ctx->d2->shortname);
1955 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
1957 if (NULL == ctx->d2->th)
1959 GNUNET_CORE_disconnect (ctx->d1core);
1961 if (NULL != ctx->cb)
1962 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1963 ctx->d2->cfg, ctx->d1, ctx->d2,
1964 _("Failed to connect to transport service!\n"));
1968 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
1972 if (NULL == ctx->d2->hello)
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1975 "didn't have %s's HELLO but th wasn't NULL, not trying!!\n",
1976 ctx->d2->shortname);
1980 if (ctx->send_hello == GNUNET_YES)
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending %s's HELLO to %s\n",
1983 ctx->d1->shortname, ctx->d2->shortname);
1985 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
1987 if (ctx->d1th == NULL)
1989 GNUNET_CORE_disconnect (ctx->d1core);
1991 if (NULL != ctx->cb)
1992 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1993 ctx->d2->cfg, ctx->d1, ctx->d2,
1994 _("Failed to connect to transport service!\n"));
1997 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
1998 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to reconnect %s to %s\n",
2003 ctx->d1->shortname, ctx->d2->shortname);
2004 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
2007 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2008 ¬ify_connect_result, ctx);
2012 * Iterator for currently known peers, to ensure
2013 * that we don't try to send duplicate connect
2016 * @param cls our "struct GNUNET_TESTING_ConnectContext"
2017 * @param peer identity of the peer that has connected,
2018 * NULL when iteration has finished
2019 * @param atsi performance information
2020 * @param atsi_count number of records in 'atsi'
2024 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2025 const struct GNUNET_ATS_Information *atsi,
2026 unsigned int atsi_count)
2028 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2030 if ((peer != NULL) &&
2031 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2033 ctx->connected = GNUNET_YES;
2034 ctx->distance = 0; /* FIXME: distance */
2038 return; /* ignore other peers */
2039 /* peer == NULL: End of iteration over peers */
2041 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->timeout_task);
2042 if (ctx->connected == GNUNET_YES)
2044 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2048 /* Peer not already connected, need to schedule connect request! */
2049 if (ctx->d1core == NULL)
2051 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2052 "Peers are NOT connected, connecting to core!\n");
2054 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2055 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
2056 GNUNET_NO, no_handlers);
2059 if (ctx->d1core == NULL)
2061 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2065 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 */
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 "Don't have d2's HELLO, trying to get it!\n");
2070 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2072 if (ctx->d2->th == NULL)
2074 GNUNET_CORE_disconnect (ctx->d1core);
2077 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2081 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2084 if (ctx->send_hello == GNUNET_YES)
2087 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2089 if (ctx->d1th == NULL)
2091 GNUNET_CORE_disconnect (ctx->d1core);
2094 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2097 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2098 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2102 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2103 ¬ify_connect_result, ctx);
2109 * Establish a connection between two GNUnet daemons. The daemons
2110 * must both be running and not be stopped until either the
2111 * 'cb' callback is called OR the connection request has been
2112 * explicitly cancelled.
2114 * @param d1 handle for the first daemon
2115 * @param d2 handle for the second daemon
2116 * @param timeout how long is the connection attempt
2118 * @param max_connect_attempts how many times should we try to reconnect
2120 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2121 * the HELLO has already been exchanged
2122 * @param cb function to call at the end
2123 * @param cb_cls closure for cb
2124 * @return handle to cancel the request
2126 struct GNUNET_TESTING_ConnectContext *
2127 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2128 struct GNUNET_TESTING_Daemon *d2,
2129 struct GNUNET_TIME_Relative timeout,
2130 unsigned int max_connect_attempts,
2132 GNUNET_TESTING_NotifyConnection cb,
2135 struct GNUNET_TESTING_ConnectContext *ctx;
2137 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2140 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2141 _("Peers are not fully running yet, can not connect!\n"));
2142 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2146 ctx = GNUNET_malloc (sizeof (struct GNUNET_TESTING_ConnectContext));
2149 ctx->timeout_hello =
2150 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2151 ctx->relative_timeout =
2152 GNUNET_TIME_relative_divide (timeout, max_connect_attempts);
2154 ctx->cb_cls = cb_cls;
2155 ctx->connect_attempts = max_connect_attempts;
2156 ctx->connected = GNUNET_NO;
2157 ctx->send_hello = send_hello;
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to connect peer %s to peer %s\n",
2159 d1->shortname, d2->shortname);
2160 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2161 GNUNET_assert (NULL !=
2162 GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id,
2163 &core_initial_iteration, ctx));
2169 * Cancel an attempt to connect two daemons.
2171 * @param cc connect context
2174 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext *cc)
2176 if (GNUNET_SCHEDULER_NO_TASK != cc->timeout_task)
2178 GNUNET_SCHEDULER_cancel (cc->timeout_task);
2179 cc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2181 if (GNUNET_SCHEDULER_NO_TASK != cc->hello_send_task)
2183 GNUNET_SCHEDULER_cancel (cc->hello_send_task);
2184 cc->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2186 if (NULL != cc->d1core)
2188 GNUNET_CORE_disconnect (cc->d1core);
2191 if (NULL != cc->d1th)
2193 GNUNET_TRANSPORT_disconnect (cc->d1th);
2200 /* end of testing.c */