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, &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);
222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
223 "Successfully copied configuration file.\n");
224 d->phase = SP_COPIED;
227 /* Start create hostkey process if we don't already know the peer identity! */
228 if (GNUNET_NO == d->have_hostkey)
230 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_YES);
231 if (d->pipe_stdout == NULL)
236 cb (d->cb_cls, NULL, d->cfg, d,
239 _("Failed to create pipe for `gnunet-peerinfo' process.\n") :
240 _("Failed to create pipe for `ssh' process.\n"));
243 if (NULL == d->hostname)
245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
246 "Starting `%s', with command `%s %s %s %s'.\n",
247 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
250 GNUNET_OS_start_process (GNUNET_YES, NULL, d->pipe_stdout, "gnunet-peerinfo",
251 "gnunet-peerinfo", "-c", d->cfgfile, "-sq",
253 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
257 if (d->username != NULL)
258 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
260 dst = GNUNET_strdup (d->hostname);
262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
263 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
264 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
266 if (d->ssh_port_str == NULL)
268 d->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, d->pipe_stdout, "ssh", "ssh",
272 dst, "gnunet-peerinfo", "-c",
273 d->cfgfile, "-sq", NULL);
278 GNUNET_OS_start_process (GNUNET_NO, NULL, d->pipe_stdout, "ssh", "ssh", "-p",
283 dst, "gnunet-peerinfo", "-c", d->cfgfile,
286 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
292 _("Could not start `%s' process to create hostkey.\n"),
293 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
297 cb (d->cb_cls, NULL, d->cfg, d,
299 d->hostname) ? _("Failed to start `gnunet-peerinfo' process.\n")
300 : _("Failed to start `ssh' process.\n"));
301 GNUNET_DISK_pipe_close (d->pipe_stdout);
304 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
305 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
306 d->phase = SP_HOSTKEY_CREATE;
308 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
310 GNUNET_DISK_pipe_handle
312 GNUNET_DISK_PIPE_END_READ),
315 else /* Already have a hostkey! */
317 if (d->hostkey_callback != NULL)
319 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
320 d->hostkey_callback = NULL;
321 d->phase = SP_HOSTKEY_CREATED;
324 d->phase = SP_TOPOLOGY_SETUP;
327 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
330 case SP_HOSTKEY_CREATE:
332 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
333 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
334 &d->hostkeybuf[d->hostkeybufpos],
335 sizeof (d->hostkeybuf) - d->hostkeybufpos);
337 d->hostkeybufpos += bytes_read;
339 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
343 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
345 GNUNET_DISK_pipe_handle
347 GNUNET_DISK_PIPE_END_READ),
351 d->hostkeybuf[103] = '\0';
353 if ((bytes_read < 0) ||
355 GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey)))
359 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
360 _("Error reading from gnunet-peerinfo: %s\n"),
363 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
364 _("Malformed output from gnunet-peerinfo!\n"));
367 GNUNET_DISK_pipe_close (d->pipe_stdout);
368 d->pipe_stdout = NULL;
369 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
370 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
371 GNUNET_OS_process_close (d->proc);
374 cb (d->cb_cls, NULL, d->cfg, d, _("Failed to get hostkey!\n"));
377 d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
378 GNUNET_DISK_pipe_close (d->pipe_stdout);
379 d->pipe_stdout = NULL;
380 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
381 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
382 GNUNET_OS_process_close (d->proc);
384 d->have_hostkey = GNUNET_YES;
385 if (d->hostkey_callback != NULL)
387 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
388 d->hostkey_callback = NULL;
389 d->phase = SP_HOSTKEY_CREATED;
393 d->phase = SP_TOPOLOGY_SETUP;
395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
397 case SP_HOSTKEY_CREATED:
398 /* wait for topology finished */
399 if ((GNUNET_YES == d->dead) ||
400 (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
405 cb (d->cb_cls, NULL, d->cfg, d,
406 _("`Failed while waiting for topology setup!\n"));
411 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
414 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
415 /* start GNUnet on remote host */
416 if (NULL == d->hostname)
418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
419 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
420 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-L", "DEBUG",
423 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm", "-c",
427 GNUNET_TIME_relative_to_string
428 (GNUNET_TIME_absolute_get_remaining
429 (d->max_timeout)), NULL);
433 if (d->username != NULL)
434 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
436 dst = GNUNET_strdup (d->hostname);
438 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
439 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
440 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
441 "-L", "DEBUG", "-s", "-q");
442 if (d->ssh_port_str == NULL)
444 d->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
452 "-c", d->cfgfile, "-s", "-q", "-T",
453 GNUNET_TIME_relative_to_string
454 (GNUNET_TIME_absolute_get_remaining
455 (d->max_timeout)), NULL);
461 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh", "-p",
470 "-c", d->cfgfile, "-s", "-q", "-T",
471 GNUNET_TIME_relative_to_string
472 (GNUNET_TIME_absolute_get_remaining
473 (d->max_timeout)), NULL);
479 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
480 _("Could not start `%s' process to start GNUnet.\n"),
481 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
485 cb (d->cb_cls, NULL, d->cfg, d,
487 d->hostname) ? _("Failed to start `gnunet-arm' process.\n") :
488 _("Failed to start `ssh' process.\n"));
491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
492 "Started `%s', waiting for `%s' to be up.\n", "gnunet-arm",
493 "gnunet-service-core");
494 d->phase = SP_START_ARMING;
496 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
499 case SP_START_ARMING:
500 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
502 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
507 cb (d->cb_cls, NULL, d->cfg, d,
509 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
510 _("`ssh' does not seem to terminate.\n"));
513 GNUNET_CONFIGURATION_destroy (d->cfg);
516 if (d->cfgfile != NULL)
518 GNUNET_free (d->cfgfile);
521 GNUNET_free_non_null (d->hostname);
522 GNUNET_free_non_null (d->username);
523 GNUNET_free (d->proc);
524 // GNUNET_free (d); // FIXME (could this leak)
525 d->hostname = NULL; // Quick hack to avoid crashing (testing need to be
526 d->cfg = NULL; // overhauled anyway, and the error managing is
527 // pretty broken anyway.
532 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully started `%s'.\n",
538 GNUNET_free (d->proc);
539 d->phase = SP_START_CORE;
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling CORE_connect\n");
543 if (d->server != NULL)
544 GNUNET_CORE_disconnect (d->server);
546 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
549 if (GNUNET_YES == d->dead)
550 GNUNET_TESTING_daemon_stop (d,
551 GNUNET_TIME_absolute_get_remaining
552 (d->max_timeout), d->dead_cb,
553 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
554 else if (NULL != d->cb)
555 d->cb (d->cb_cls, &d->id, d->cfg, d,
556 _("Failed to connect to transport service!\n"));
559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
560 "Connected to transport service `%s', getting HELLO\n",
561 GNUNET_i2s (&d->id));
562 d->ghh = GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
563 /* FIXME: store task ID somewhere! */
564 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
568 * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
569 d->running = GNUNET_YES;
570 d->phase = SP_GET_HELLO;
573 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
575 if (d->server != NULL)
576 GNUNET_CORE_disconnect (d->server);
578 GNUNET_TRANSPORT_disconnect (d->th);
582 cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
583 GNUNET_CONFIGURATION_destroy (d->cfg);
584 GNUNET_free (d->cfgfile);
585 GNUNET_free_non_null (d->hostname);
586 GNUNET_free_non_null (d->username);
590 if (d->hello != NULL)
592 GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
594 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
595 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
601 case SP_SERVICE_START:
602 /* confirm gnunet-arm exited */
603 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
605 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
610 cb (d->cb_cls, NULL, d->cfg, d,
612 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
613 _("`ssh' does not seem to terminate.\n"));
618 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
623 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
628 cb (d->cb_cls, NULL, d->cfg, d,
632 ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
633 : _("`ssh' does not seem to terminate.\n"));
637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
640 d->phase = SP_START_DONE;
642 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
644 case SP_SERVICE_SHUTDOWN_START:
645 /* confirm copying complete */
646 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
648 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
650 if (NULL != d->dead_cb)
651 d->dead_cb (d->dead_cb_cls,
653 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
658 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
663 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
665 if (NULL != d->dead_cb)
666 d->dead_cb (d->dead_cb_cls,
668 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
673 if (NULL != d->dead_cb)
674 d->dead_cb (d->dead_cb_cls, NULL);
676 case SP_SHUTDOWN_START:
677 /* confirm copying complete */
678 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
680 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
682 if (NULL != d->dead_cb)
683 d->dead_cb (d->dead_cb_cls,
685 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
688 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
690 GNUNET_TRANSPORT_disconnect (d->th);
695 GNUNET_CONFIGURATION_destroy (d->cfg);
698 if (d->cfgfile != NULL)
700 GNUNET_free (d->cfgfile);
703 GNUNET_free_non_null (d->hello);
704 GNUNET_free_non_null (d->hostname);
705 GNUNET_free_non_null (d->username);
706 GNUNET_free_non_null (d->shortname);
707 GNUNET_free_non_null (d->proc);
714 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
718 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
720 if (NULL != d->dead_cb)
721 d->dead_cb (d->dead_cb_cls,
723 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
726 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
728 GNUNET_TRANSPORT_disconnect (d->th);
731 if (d->server != NULL)
733 GNUNET_CORE_disconnect (d->server);
736 GNUNET_CONFIGURATION_destroy (d->cfg);
738 GNUNET_free (d->cfgfile);
739 GNUNET_free_non_null (d->hello);
740 GNUNET_free_non_null (d->hostname);
741 GNUNET_free_non_null (d->username);
742 GNUNET_free_non_null (d->shortname);
743 GNUNET_free_non_null (d->proc);
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
749 if (d->server != NULL)
751 GNUNET_CORE_disconnect (d->server);
757 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
759 GNUNET_TRANSPORT_disconnect (d->th);
763 if (NULL != d->dead_cb)
764 d->dead_cb (d->dead_cb_cls, NULL);
766 /* state clean up and notifications */
767 if (d->churn == GNUNET_NO)
769 GNUNET_CONFIGURATION_destroy (d->cfg);
771 GNUNET_free (d->cfgfile);
772 GNUNET_free_non_null (d->hostname);
773 GNUNET_free_non_null (d->username);
776 GNUNET_free_non_null (d->hello);
778 GNUNET_free_non_null (d->shortname);
779 GNUNET_free_non_null (d->proc);
782 if (d->churn == GNUNET_NO)
786 case SP_CONFIG_UPDATE:
787 /* confirm copying complete */
788 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
790 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
795 cb (d->cb_cls, NULL, d->cfg, d,
796 _("`scp' does not seem to terminate.\n"));
801 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
805 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
807 if (NULL != d->update_cb)
808 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Successfully copied configuration file.\n");
813 if (NULL != d->update_cb)
814 d->update_cb (d->update_cb_cls, NULL);
815 d->phase = SP_START_DONE;
821 * Continues GNUnet daemon startup when user wanted to be notified
822 * once a hostkey was generated (for creating friends files, blacklists,
825 * @param daemon the daemon to finish starting
828 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
830 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
831 daemon->phase = SP_TOPOLOGY_SETUP;
835 * Check whether the given daemon is running.
837 * @param daemon the daemon to check
839 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
840 * daemon is down, GNUNET_SYSERR on error.
843 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
846 return GNUNET_SYSERR;
848 if (daemon->running == GNUNET_YES)
855 * Starts a GNUnet daemon service which has been previously stopped.
857 * @param d the daemon for which the service should be started
858 * @param service the name of the service to start
859 * @param timeout how long to wait for process for shutdown to complete
860 * @param cb function called once the service starts
861 * @param cb_cls closure for cb
864 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
866 struct GNUNET_TIME_Relative
868 GNUNET_TESTING_NotifyDaemonRunning
876 GNUNET_assert (d->running == GNUNET_YES);
878 if (d->phase == SP_CONFIG_UPDATE)
880 GNUNET_SCHEDULER_cancel (d->task);
881 d->phase = SP_START_DONE;
884 if (d->churned_services == NULL)
886 d->cb (d->cb_cls, &d->id, d->cfg, d,
887 "No service has been churned off yet!!");
890 d->phase = SP_SERVICE_START;
891 GNUNET_free (d->churned_services);
892 d->churned_services = NULL;
893 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
894 /* Check if this is a local or remote process */
895 if (NULL != d->hostname)
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Starting gnunet-arm with config `%s' on host `%s'.\n",
899 d->cfgfile, d->hostname);
900 if (d->username != NULL)
901 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
903 arg = GNUNET_strdup (d->hostname);
905 d->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
913 "-c", d->cfgfile, "-i", service, "-q",
915 GNUNET_TIME_relative_to_string (timeout),
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
919 arg, "gnunet-arm", d->cfgfile, service);
924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
925 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
926 d->proc = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
930 "-c", d->cfgfile, "-i", service, "-q",
932 GNUNET_TIME_relative_to_string (timeout),
936 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
937 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
941 * Starts a GNUnet daemon's service.
943 * @param d the daemon for which the service should be started
944 * @param service the name of the service to start
945 * @param timeout how long to wait for process for startup
946 * @param cb function called once gnunet-arm returns
947 * @param cb_cls closure for cb
950 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
952 struct GNUNET_TIME_Relative timeout,
953 GNUNET_TESTING_NotifyDaemonRunning cb,
961 GNUNET_assert (service != NULL);
962 GNUNET_assert (d->running == GNUNET_YES);
963 GNUNET_assert (d->phase == SP_START_DONE);
965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
966 _("Starting service %s for peer `%4s'\n"), service,
967 GNUNET_i2s (&d->id));
968 d->phase = SP_SERVICE_START;
969 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
970 /* Check if this is a local or remote process */
971 if (NULL != d->hostname)
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
974 "Starting gnunet-arm with config `%s' on host `%s'.\n",
975 d->cfgfile, d->hostname);
976 if (d->username != NULL)
977 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
979 arg = GNUNET_strdup (d->hostname);
981 d->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
989 "-c", d->cfgfile, "-i", service, "-q",
991 GNUNET_TIME_relative_to_string (timeout),
993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
994 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
995 arg, "gnunet-arm", d->cfgfile, service,
996 GNUNET_TIME_relative_to_string (timeout));
1001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1002 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1003 d->proc = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
1007 "-c", d->cfgfile, "-i", service, "-q",
1009 GNUNET_TIME_relative_to_string (timeout),
1011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1012 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1013 "gnunet-arm", d->cfgfile, service,
1014 GNUNET_TIME_relative_to_string (timeout));
1017 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1018 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1022 * Start a peer that has previously been stopped using the daemon_stop
1023 * call (and files weren't deleted and the allow restart flag)
1025 * @param daemon the daemon to start (has been previously stopped)
1026 * @param timeout how long to wait for restart
1027 * @param cb the callback for notification when the peer is running
1028 * @param cb_cls closure for the callback
1031 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1032 struct GNUNET_TIME_Relative timeout,
1033 GNUNET_TESTING_NotifyDaemonRunning cb,
1036 if (daemon->running == GNUNET_YES)
1038 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1039 "Daemon already running, can't restart!");
1044 daemon->cb_cls = cb_cls;
1045 daemon->phase = SP_TOPOLOGY_SETUP;
1046 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1047 /* FIXME: why add_continuation? */
1048 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1049 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1053 * Starts a GNUnet daemon. GNUnet must be installed on the target
1054 * system and available in the PATH. The machine must furthermore be
1055 * reachable via "ssh" (unless the hostname is "NULL") without the
1056 * need to enter a password.
1058 * @param cfg configuration to use
1059 * @param timeout how long to wait starting up peers
1060 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1061 * to really start the peer (default)
1062 * @param hostname name of the machine where to run GNUnet
1063 * (use NULL for localhost).
1064 * @param ssh_username ssh username to use when connecting to hostname
1065 * @param sshport port to pass to ssh process when connecting to hostname
1066 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1067 * @param hostkey_callback function to call once the hostkey has been
1068 * generated for this peer, but it hasn't yet been started
1069 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1070 * @param hostkey_cls closure for hostkey callback
1071 * @param cb function to call once peer is up, or failed to start
1072 * @param cb_cls closure for cb
1073 * @return handle to the daemon (actual start will be completed asynchronously)
1075 struct GNUNET_TESTING_Daemon *
1076 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1077 struct GNUNET_TIME_Relative timeout, int pretend,
1078 const char *hostname, const char *ssh_username,
1079 uint16_t sshport, const char *hostkey,
1080 GNUNET_TESTING_NotifyHostkeyCreated
1081 hostkey_callback, void *hostkey_cls,
1082 GNUNET_TESTING_NotifyDaemonRunning cb,
1085 struct GNUNET_TESTING_Daemon *ret;
1089 char *baseservicehome;
1092 char *temp_file_name;
1093 struct GNUNET_DISK_FileHandle *fn;
1094 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1095 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1097 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1098 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1101 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1104 ret->ssh_port_str = NULL;
1106 /* Find service home and base service home directories, create it if it doesn't exist */
1107 GNUNET_assert (GNUNET_OK ==
1108 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1112 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1113 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1114 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1115 GNUNET_free (temp_file_name);
1116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1117 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1118 if (NULL == ret->cfgfile)
1120 GNUNET_free_non_null (ret->ssh_port_str);
1121 GNUNET_free_non_null (ret->hostname);
1125 ret->hostkey_callback = hostkey_callback;
1126 ret->hostkey_cls = hostkey_cls;
1128 ret->cb_cls = cb_cls;
1129 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1130 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1131 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1134 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1136 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1137 GNUNET_assert (private_key != NULL);
1138 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1139 GNUNET_CRYPTO_hash (&public_key,
1140 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1141 &ret->id.hashPubKey);
1142 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1143 ret->have_hostkey = GNUNET_YES;
1144 GNUNET_CRYPTO_rsa_key_free (private_key);
1147 /* Write hostkey to file, if we were given one */
1149 if (hostkey != NULL)
1151 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1152 fn = GNUNET_DISK_file_open (hostkeyfile,
1153 GNUNET_DISK_OPEN_READWRITE |
1154 GNUNET_DISK_OPEN_CREATE,
1155 GNUNET_DISK_PERM_USER_READ |
1156 GNUNET_DISK_PERM_USER_WRITE);
1157 GNUNET_assert (fn != NULL);
1158 GNUNET_assert (HOSTKEYFILESIZE ==
1159 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1160 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1163 /* write configuration to temporary file */
1164 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1166 if (0 != UNLINK (ret->cfgfile))
1167 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1169 GNUNET_CONFIGURATION_destroy (ret->cfg);
1170 GNUNET_free_non_null (ret->hostname);
1171 GNUNET_free (ret->cfgfile);
1175 if (ssh_username != NULL)
1176 username = GNUNET_strdup (ssh_username);
1177 if ((ssh_username == NULL) &&
1179 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1182 if (NULL != getenv ("USER"))
1183 username = GNUNET_strdup (getenv ("USER"));
1187 ret->username = username;
1189 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1191 /* copy directory to remote host */
1192 if (NULL != hostname)
1194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1195 "Copying configuration directory to host `%s'.\n", hostname);
1196 baseservicehome = GNUNET_strdup (servicehome);
1197 /* Remove trailing /'s */
1198 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1199 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1200 /* Find next directory /, jump one ahead */
1201 slash = strrchr (baseservicehome, '/');
1205 ret->phase = SP_COPYING;
1206 if (NULL != username)
1207 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1209 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1210 GNUNET_free (baseservicehome);
1211 if (ret->ssh_port_str == NULL)
1213 ret->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp", "-r",
1217 servicehome, arg, NULL);
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "copying directory with command scp -r %s %s\n",
1225 GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp", "-r", "-P",
1230 servicehome, arg, NULL);
1233 if (NULL == ret->proc)
1235 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1237 ("Could not start `%s' process to copy configuration directory.\n"),
1239 if (0 != UNLINK (ret->cfgfile))
1240 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1242 GNUNET_CONFIGURATION_destroy (ret->cfg);
1243 GNUNET_free_non_null (ret->hostname);
1244 GNUNET_free_non_null (ret->username);
1245 GNUNET_free (ret->cfgfile);
1247 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1248 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1250 GNUNET_free_non_null (hostkeyfile);
1251 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1252 GNUNET_free (servicehome);
1257 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1259 GNUNET_free_non_null (hostkeyfile);
1260 GNUNET_free (servicehome);
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "No need to copy configuration file since we are running locally.\n");
1265 ret->phase = SP_COPIED;
1266 /* FIXME: why add_cont? */
1267 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1268 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1270 GNUNET_free_non_null (hostkeyfile);
1271 GNUNET_free (servicehome);
1277 * Restart (stop and start) a GNUnet daemon.
1279 * @param d the daemon that should be restarted
1280 * @param cb function called once the daemon is (re)started
1281 * @param cb_cls closure for cb
1284 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1285 GNUNET_TESTING_NotifyDaemonRunning cb,
1294 d->dead = GNUNET_YES;
1301 if (d->phase == SP_CONFIG_UPDATE)
1303 GNUNET_SCHEDULER_cancel (d->task);
1304 d->phase = SP_START_DONE;
1306 if (d->server != NULL)
1308 GNUNET_CORE_disconnect (d->server);
1314 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1316 GNUNET_TRANSPORT_disconnect (d->th);
1319 /* state clean up and notifications */
1320 GNUNET_free_non_null (d->hello);
1322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1323 GNUNET_i2s (&d->id));
1324 d->phase = SP_START_ARMING;
1326 /* Check if this is a local or remote process */
1327 if (NULL != d->hostname)
1329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1330 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1331 d->cfgfile, d->hostname);
1332 if (d->username != NULL)
1333 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1335 arg = GNUNET_strdup (d->hostname);
1337 d->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
1345 "-c", d->cfgfile, "-e", "-r", NULL);
1346 /* Use -r to restart arm and all services */
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1354 d->proc = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
1358 "-c", d->cfgfile, "-e", "-r", NULL);
1361 GNUNET_free_non_null (del_arg);
1363 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1369 * Stops a GNUnet daemon.
1371 * @param d the daemon that should be stopped
1372 * @param service the name of the service to stop
1373 * @param timeout how long to wait for process for shutdown to complete
1374 * @param cb function called once the daemon was stopped
1375 * @param cb_cls closure for cb
1378 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1379 const char *service,
1380 struct GNUNET_TIME_Relative timeout,
1381 GNUNET_TESTING_NotifyCompletion cb,
1387 d->dead_cb_cls = cb_cls;
1389 GNUNET_assert (d->running == GNUNET_YES);
1391 if (d->phase == SP_CONFIG_UPDATE)
1393 GNUNET_SCHEDULER_cancel (d->task);
1394 d->phase = SP_START_DONE;
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1398 GNUNET_i2s (&d->id));
1399 if (d->churned_services != NULL)
1401 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1404 d->phase = SP_SERVICE_SHUTDOWN_START;
1405 d->churned_services = GNUNET_strdup (service);
1406 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1407 /* Check if this is a local or remote process */
1408 if (NULL != d->hostname)
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1412 d->cfgfile, d->hostname);
1413 if (d->username != NULL)
1414 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1416 arg = GNUNET_strdup (d->hostname);
1418 d->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
1426 "-c", d->cfgfile, "-k", service, "-q",
1428 GNUNET_TIME_relative_to_string (timeout),
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1432 arg, "gnunet-arm", d->cfgfile, service);
1437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1438 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1439 d->proc = GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-arm", "gnunet-arm",
1443 "-c", d->cfgfile, "-k", service, "-q",
1445 GNUNET_TIME_relative_to_string (timeout),
1449 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1450 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1455 * Stops a GNUnet daemon.
1457 * @param d the daemon that should be stopped
1458 * @param timeout how long to wait for process for shutdown to complete
1459 * @param cb function called once the daemon was stopped
1460 * @param cb_cls closure for cb
1461 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1463 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1464 * GNUNET_NO to kill off and clean up for good
1467 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1468 struct GNUNET_TIME_Relative timeout,
1469 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1470 int delete_files, int allow_restart)
1476 d->dead_cb_cls = cb_cls;
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1481 GNUNET_i2s (&d->id));
1482 d->dead = GNUNET_YES;
1486 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1488 /* Free what was left from churning! */
1489 GNUNET_assert (d->cfg != NULL);
1490 GNUNET_CONFIGURATION_destroy (d->cfg);
1491 if (delete_files == GNUNET_YES)
1493 if (0 != UNLINK (d->cfgfile))
1495 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1498 GNUNET_free (d->cfgfile);
1499 GNUNET_free_non_null (d->hostname);
1500 GNUNET_free_non_null (d->username);
1501 if (NULL != d->dead_cb)
1502 d->dead_cb (d->dead_cb_cls, NULL);
1508 if (delete_files == GNUNET_YES)
1510 GNUNET_asprintf (&del_arg, "-d");
1513 if (d->phase == SP_CONFIG_UPDATE)
1515 GNUNET_SCHEDULER_cancel (d->task);
1516 d->phase = SP_START_DONE;
1518 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1519 if (d->server != NULL)
1521 GNUNET_CORE_disconnect (d->server);
1525 /* shutdown ARM process (will terminate others) */
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Terminating peer `%4s'\n" ,
1527 GNUNET_i2s (&d->id));
1528 d->phase = SP_SHUTDOWN_START;
1529 d->running = GNUNET_NO;
1530 if (allow_restart == GNUNET_YES)
1531 d->churn = GNUNET_YES;
1534 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1536 GNUNET_TRANSPORT_disconnect (d->th);
1539 /* Check if this is a local or remote process */
1540 if (NULL != d->hostname)
1542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1543 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1544 d->cfgfile, d->hostname);
1545 if (d->username != NULL)
1546 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1548 arg = GNUNET_strdup (d->hostname);
1550 d->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "ssh", "ssh",
1558 "-c", d->cfgfile, "-e", "-q", "-T",
1559 GNUNET_TIME_relative_to_string (timeout),
1561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1562 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1563 arg, "gnunet-arm", d->cfgfile, del_arg);
1564 /* Use -e to end arm, and -d to remove temp files */
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1571 d->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "gnunet-arm", "gnunet-arm",
1575 "-c", d->cfgfile, "-e", "-q", "-T",
1576 GNUNET_TIME_relative_to_string (timeout),
1578 GNUNET_assert (NULL != d->proc);
1581 GNUNET_free_non_null (del_arg);
1582 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1583 if (GNUNET_SCHEDULER_NO_TASK != d->task)
1584 GNUNET_SCHEDULER_cancel(d->task);
1585 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1590 * Changes the configuration of a GNUnet daemon.
1592 * @param d the daemon that should be modified
1593 * @param cfg the new configuration for the daemon
1594 * @param cb function called once the configuration was changed
1595 * @param cb_cls closure for cb
1598 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1599 struct GNUNET_CONFIGURATION_Handle *cfg,
1600 GNUNET_TESTING_NotifyCompletion cb,
1605 if (d->phase != SP_START_DONE)
1610 ("Peer not yet running, can not change configuration at this point."));
1614 /* 1) write configuration to temporary file */
1615 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1618 cb (cb_cls, _("Failed to write new configuration to disk."));
1622 /* 2) copy file to remote host (if necessary) */
1623 if (NULL == d->hostname)
1625 /* signal success */
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Copying updated configuration file to remote host `%s'.\n",
1633 d->phase = SP_CONFIG_UPDATE;
1634 if (NULL != d->username)
1635 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1637 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1638 d->proc = GNUNET_OS_start_process (GNUNET_NO, NULL, NULL, "scp", "scp",
1642 d->cfgfile, arg, NULL);
1644 if (NULL == d->proc)
1646 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1647 _("Could not start `%s' process to copy configuration file.\n"),
1650 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1651 d->phase = SP_START_DONE;
1655 d->update_cb_cls = cb_cls;
1657 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1662 * Data kept for each pair of peers that we try
1665 struct GNUNET_TESTING_ConnectContext
1668 * Testing handle to the first daemon.
1670 struct GNUNET_TESTING_Daemon *d1;
1673 * Handle to core of first daemon (to check connect)
1675 struct GNUNET_CORE_Handle *d1core;
1678 * Have we actually connected to the core of the first daemon yet?
1683 * Testing handle to the second daemon.
1685 struct GNUNET_TESTING_Daemon *d2;
1688 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1690 struct GNUNET_TRANSPORT_Handle *d1th;
1693 * Function to call once we are done (or have timed out).
1695 GNUNET_TESTING_NotifyConnection cb;
1703 * The relative timeout from whence this connect attempt was
1704 * started. Allows for reconnect attempts.
1706 struct GNUNET_TIME_Relative relative_timeout;
1709 * Maximum number of connect attempts, will retry connection
1710 * this number of times on failures.
1712 unsigned int connect_attempts;
1715 * Hello timeout task
1717 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1720 * Connect timeout task
1722 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1725 * When should this operation be complete (or we must trigger
1728 struct GNUNET_TIME_Relative timeout_hello;
1731 * Was the connection attempt successful?
1736 * When connecting, do we need to send the HELLO?
1741 * The distance between the two connected peers
1747 /** Forward declaration **/
1749 reattempt_daemons_connect (void *cls,
1750 const struct GNUNET_SCHEDULER_TaskContext *tc);
1754 * Notify callback about success or failure of the attempt
1755 * to connect the two peers
1757 * @param cls our "struct GNUNET_TESTING_ConnectContext" (freed)
1758 * @param tc reason tells us if we succeeded or failed
1761 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1763 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1765 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1766 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1768 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1769 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1772 if (ctx->d1th != NULL)
1773 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1775 if (ctx->d1core != NULL)
1776 GNUNET_CORE_disconnect (ctx->d1core);
1779 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1785 if (ctx->connected == GNUNET_YES)
1787 if (ctx->cb != NULL)
1789 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
1790 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1793 else if (ctx->connect_attempts > 0)
1795 ctx->d1core_ready = GNUNET_NO;
1797 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1802 if (ctx->cb != NULL)
1804 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1805 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
1813 * Success, connection is up. Signal client our success.
1815 * @param cls our "struct GNUNET_TESTING_ConnectContext"
1816 * @param peer identity of the peer that has connected
1817 * @param atsi performance information
1818 * @param atsi_count number of records in 'atsi'
1822 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
1823 const struct GNUNET_ATS_Information *atsi,
1824 unsigned int atsi_count)
1826 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peer %s to peer %s\n",
1829 ctx->d1->shortname, GNUNET_i2s (peer));
1830 if (0 != memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1832 ctx->connected = GNUNET_YES;
1833 ctx->distance = 0; /* FIXME: distance */
1834 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1836 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1837 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1839 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1840 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
1845 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1847 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1848 struct GNUNET_MessageHeader *hello;
1850 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1851 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1853 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
1854 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
1855 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
1857 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1858 GNUNET_assert (hello != NULL);
1859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
1860 ctx->d2->shortname, ctx->d1->shortname);
1861 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1862 GNUNET_assert (ctx->d1core != NULL);
1863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1864 "Sending connect request to TRANSPORT of %s for peer %s\n",
1865 GNUNET_i2s (&ctx->d1->id),
1866 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1867 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
1868 ctx->timeout_hello =
1869 GNUNET_TIME_relative_add (ctx->timeout_hello,
1870 GNUNET_TIME_relative_multiply
1871 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1873 ctx->hello_send_task =
1874 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
1878 * Notify of a successful connection to the core service.
1880 * @param cls a ConnectContext
1881 * @param server handle to the core service
1882 * @param my_identity the peer identity of this peer
1885 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
1886 const struct GNUNET_PeerIdentity *my_identity)
1888 struct GNUNET_TESTING_ConnectContext *connect_ctx = cls;
1890 connect_ctx->d1core_ready = GNUNET_YES;
1892 if (connect_ctx->send_hello == GNUNET_NO)
1894 GNUNET_TRANSPORT_try_connect (connect_ctx->d1th, &connect_ctx->d2->id);
1895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1896 "Sending connect request to TRANSPORT of %s for peer %s\n",
1897 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
1903 * Try to connect again some peers that failed in an earlier attempt. This will
1904 * be tried as many times as connection_attempts in the configuration file.
1906 * @param cls Closure (connection context between the two peers).
1907 * @param tc TaskContext.
1910 reattempt_daemons_connect (void *cls,
1911 const struct GNUNET_SCHEDULER_TaskContext *tc)
1913 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1915 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1916 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1919 "re-attempting connect of peer %s to peer %s\n",
1920 ctx->d1->shortname, ctx->d2->shortname);
1921 ctx->connect_attempts--;
1922 GNUNET_assert (ctx->d1core == NULL);
1923 ctx->d1core_ready = GNUNET_NO;
1925 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
1926 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
1927 GNUNET_NO, no_handlers);
1928 if (ctx->d1core == NULL)
1930 if (NULL != ctx->cb)
1931 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1932 ctx->d2->cfg, ctx->d1, ctx->d2,
1933 _("Failed to connect to core service of first peer!\n"));
1938 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
1939 if (NULL != ctx->d2->hello)
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "updating %s's HELLO\n",
1942 ctx->d2->shortname);
1943 GNUNET_free (ctx->d2->hello);
1944 ctx->d2->hello = NULL;
1945 if (NULL != ctx->d2->th)
1947 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->ghh);
1948 ctx->d2->ghh = NULL;
1949 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
1952 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
1954 GNUNET_assert (ctx->d2->th != NULL);
1956 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
1960 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "didn't have %s's HELLO\n",
1961 ctx->d2->shortname);
1964 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "didn't have %s's HELLO, trying to get it now\n",
1968 ctx->d2->shortname);
1970 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
1972 if (NULL == ctx->d2->th)
1974 GNUNET_CORE_disconnect (ctx->d1core);
1976 if (NULL != ctx->cb)
1977 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1978 ctx->d2->cfg, ctx->d1, ctx->d2,
1979 _("Failed to connect to transport service!\n"));
1983 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
1987 if (NULL == ctx->d2->hello)
1989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1990 "didn't have %s's HELLO but th wasn't NULL, not trying!!\n",
1991 ctx->d2->shortname);
1995 if (ctx->send_hello == GNUNET_YES)
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending %s's HELLO to %s\n",
1998 ctx->d1->shortname, ctx->d2->shortname);
2000 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2002 if (ctx->d1th == NULL)
2004 GNUNET_CORE_disconnect (ctx->d1core);
2006 if (NULL != ctx->cb)
2007 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2008 ctx->d2->cfg, ctx->d1, ctx->d2,
2009 _("Failed to connect to transport service!\n"));
2012 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2013 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to reconnect %s to %s\n",
2018 ctx->d1->shortname, ctx->d2->shortname);
2019 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
2022 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2023 ¬ify_connect_result, ctx);
2027 * Iterator for currently known peers, to ensure
2028 * that we don't try to send duplicate connect
2031 * @param cls our "struct GNUNET_TESTING_ConnectContext"
2032 * @param peer identity of the peer that has connected,
2033 * NULL when iteration has finished
2034 * @param atsi performance information
2035 * @param atsi_count number of records in 'atsi'
2039 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2040 const struct GNUNET_ATS_Information *atsi,
2041 unsigned int atsi_count)
2043 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2045 if ((peer != NULL) &&
2046 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2048 ctx->connected = GNUNET_YES;
2049 ctx->distance = 0; /* FIXME: distance */
2053 return; /* ignore other peers */
2054 /* peer == NULL: End of iteration over peers */
2056 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->timeout_task);
2057 if (ctx->connected == GNUNET_YES)
2059 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2063 /* Peer not already connected, need to schedule connect request! */
2064 if (ctx->d1core == NULL)
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2067 "Peers are NOT connected, connecting to core!\n");
2069 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2070 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
2071 GNUNET_NO, no_handlers);
2074 if (ctx->d1core == NULL)
2076 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2080 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 */
2082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2083 "Don't have d2's HELLO, trying to get it!\n");
2085 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2087 if (ctx->d2->th == NULL)
2089 GNUNET_CORE_disconnect (ctx->d1core);
2092 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2096 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2099 if (ctx->send_hello == GNUNET_YES)
2102 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2104 if (ctx->d1th == NULL)
2106 GNUNET_CORE_disconnect (ctx->d1core);
2109 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2112 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2113 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2117 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2118 ¬ify_connect_result, ctx);
2124 * Establish a connection between two GNUnet daemons. The daemons
2125 * must both be running and not be stopped until either the
2126 * 'cb' callback is called OR the connection request has been
2127 * explicitly cancelled.
2129 * @param d1 handle for the first daemon
2130 * @param d2 handle for the second daemon
2131 * @param timeout how long is the connection attempt
2133 * @param max_connect_attempts how many times should we try to reconnect
2135 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2136 * the HELLO has already been exchanged
2137 * @param cb function to call at the end
2138 * @param cb_cls closure for cb
2139 * @return handle to cancel the request
2141 struct GNUNET_TESTING_ConnectContext *
2142 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2143 struct GNUNET_TESTING_Daemon *d2,
2144 struct GNUNET_TIME_Relative timeout,
2145 unsigned int max_connect_attempts,
2147 GNUNET_TESTING_NotifyConnection cb,
2150 struct GNUNET_TESTING_ConnectContext *ctx;
2152 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2155 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2156 _("Peers are not fully running yet, can not connect!\n"));
2157 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2161 ctx = GNUNET_malloc (sizeof (struct GNUNET_TESTING_ConnectContext));
2164 ctx->timeout_hello =
2165 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2166 ctx->relative_timeout =
2167 GNUNET_TIME_relative_divide (timeout, max_connect_attempts);
2169 ctx->cb_cls = cb_cls;
2170 ctx->connect_attempts = max_connect_attempts;
2171 ctx->connected = GNUNET_NO;
2172 ctx->send_hello = send_hello;
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to connect peer %s to peer %s\n",
2174 d1->shortname, d2->shortname);
2175 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2176 GNUNET_assert (GNUNET_OK ==
2177 GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id,
2178 &core_initial_iteration, ctx));
2184 * Cancel an attempt to connect two daemons.
2186 * @param cc connect context
2189 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext *cc)
2191 if (GNUNET_SCHEDULER_NO_TASK != cc->timeout_task)
2193 GNUNET_SCHEDULER_cancel (cc->timeout_task);
2194 cc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2196 if (GNUNET_SCHEDULER_NO_TASK != cc->hello_send_task)
2198 GNUNET_SCHEDULER_cancel (cc->hello_send_task);
2199 cc->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2201 if (NULL != cc->d1core)
2203 GNUNET_CORE_disconnect (cc->d1core);
2206 if (NULL != cc->d1th)
2208 GNUNET_TRANSPORT_disconnect (cc->d1th);
2215 /* end of testing.c */