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)
101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
102 "Skipping empty HELLO address of peer %s\n",
103 GNUNET_i2s (&daemon->id));
108 GNUNET_assert (daemon->phase == SP_GET_HELLO ||
109 daemon->phase == SP_START_DONE);
110 daemon->cb = NULL; // FIXME: why??? (see fsm:SP_START_CORE, notify_daemon_started)
111 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
112 GNUNET_SCHEDULER_cancel (daemon->task);
114 if (daemon->server != NULL)
117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
118 "Received `%s' from transport service of `%4s', disconnecting core!\n",
119 "HELLO", GNUNET_i2s (&daemon->id));
121 GNUNET_CORE_disconnect (daemon->server);
122 daemon->server = NULL;
125 msize = ntohs (message->size);
128 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
129 "HELLO message of peer %s is of size 0\n",
130 GNUNET_i2s (&daemon->id));
133 if (daemon->ghh != NULL)
135 GNUNET_TRANSPORT_get_hello_cancel (daemon->ghh);
139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
140 "Received `%s' from transport service of `%4s'\n", "HELLO",
141 GNUNET_i2s (&daemon->id));
144 GNUNET_free_non_null (daemon->hello);
145 daemon->hello = GNUNET_malloc (msize);
146 memcpy (daemon->hello, message, msize);
148 if (daemon->th != NULL)
150 GNUNET_TRANSPORT_disconnect (daemon->th);
153 daemon->phase = SP_START_DONE;
158 * Notify of a peer being up and running. Scheduled as a task
159 * so that variables which may need to be set are set before
160 * the connect callback can set up new operations.
161 * FIXME: what variables?????? where from????
163 * @param cls the testing daemon
164 * @param tc task scheduler context
167 notify_daemon_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
169 struct GNUNET_TESTING_Daemon *d = cls;
170 GNUNET_TESTING_NotifyDaemonRunning cb;
175 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
180 * Finite-state machine for starting GNUnet.
182 * @param cls our "struct GNUNET_TESTING_Daemon"
186 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
188 struct GNUNET_TESTING_Daemon *d = cls;
189 GNUNET_TESTING_NotifyDaemonRunning cb;
190 enum GNUNET_OS_ProcessStatusType type;
196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %s FSM is in phase %u.\n",
197 GNUNET_i2s (&d->id), d->phase);
200 d->task = GNUNET_SCHEDULER_NO_TASK;
204 /* confirm copying complete */
205 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
207 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
212 cb (d->cb_cls, NULL, d->cfg, d,
214 ("`scp' does not seem to terminate (timeout copying config).\n"));
219 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
223 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
228 cb (d->cb_cls, NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
231 GNUNET_OS_process_close (d->proc);
233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
234 "Successfully copied configuration file.\n");
236 d->phase = SP_COPIED;
239 /* Start create hostkey process if we don't already know the peer identity! */
240 if (GNUNET_NO == d->have_hostkey)
242 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_NO, GNUNET_YES);
243 if (d->pipe_stdout == NULL)
248 cb (d->cb_cls, NULL, d->cfg, d,
251 _("Failed to create pipe for `gnunet-peerinfo' process.\n") :
252 _("Failed to create pipe for `ssh' process.\n"));
255 if (NULL == d->hostname)
258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
259 "Starting `%s', with command `%s %s %s %s'.\n",
260 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
264 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
265 "gnunet-peerinfo", "-c", d->cfgfile, "-sq",
267 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
271 if (d->username != NULL)
272 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
274 dst = GNUNET_strdup (d->hostname);
277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
278 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
279 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
282 if (d->ssh_port_str == NULL)
284 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh",
288 dst, "gnunet-peerinfo", "-c",
289 d->cfgfile, "-sq", NULL);
294 GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh", "-p",
299 dst, "gnunet-peerinfo", "-c", d->cfgfile,
302 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
308 _("Could not start `%s' process to create hostkey.\n"),
309 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
313 cb (d->cb_cls, NULL, d->cfg, d,
315 d->hostname) ? _("Failed to start `gnunet-peerinfo' process.\n")
316 : _("Failed to start `ssh' process.\n"));
317 GNUNET_DISK_pipe_close (d->pipe_stdout);
321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
322 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
324 d->phase = SP_HOSTKEY_CREATE;
326 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
328 GNUNET_DISK_pipe_handle
330 GNUNET_DISK_PIPE_END_READ),
333 else /* Already have a hostkey! */
335 if (d->hostkey_callback != NULL)
337 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
338 d->hostkey_callback = NULL;
339 d->phase = SP_HOSTKEY_CREATED;
342 d->phase = SP_TOPOLOGY_SETUP;
345 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
348 case SP_HOSTKEY_CREATE:
350 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
351 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
352 &d->hostkeybuf[d->hostkeybufpos],
353 sizeof (d->hostkeybuf) - d->hostkeybufpos);
355 d->hostkeybufpos += bytes_read;
357 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
361 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
363 GNUNET_DISK_pipe_handle
365 GNUNET_DISK_PIPE_END_READ),
369 d->hostkeybuf[103] = '\0';
371 if ((bytes_read < 0) ||
373 GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey)))
377 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
378 _("Error reading from gnunet-peerinfo: %s\n"),
381 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
382 _("Malformed output from gnunet-peerinfo!\n"));
385 GNUNET_DISK_pipe_close (d->pipe_stdout);
386 d->pipe_stdout = NULL;
387 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
388 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
389 GNUNET_OS_process_close (d->proc);
392 cb (d->cb_cls, NULL, d->cfg, d, _("Failed to get hostkey!\n"));
395 d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
396 GNUNET_DISK_pipe_close (d->pipe_stdout);
397 d->pipe_stdout = NULL;
398 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
399 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
400 GNUNET_OS_process_close (d->proc);
402 d->have_hostkey = GNUNET_YES;
403 if (d->hostkey_callback != NULL)
405 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
406 d->hostkey_callback = NULL;
407 d->phase = SP_HOSTKEY_CREATED;
411 d->phase = SP_TOPOLOGY_SETUP;
414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
417 case SP_HOSTKEY_CREATED:
418 /* wait for topology finished */
419 if ((GNUNET_YES == d->dead) ||
420 (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
425 cb (d->cb_cls, NULL, d->cfg, d,
426 _("`Failed while waiting for topology setup!\n"));
431 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
434 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
435 /* start GNUnet on remote host */
436 if (NULL == d->hostname)
439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
440 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
441 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-L", "DEBUG",
445 GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm", "-c",
451 GNUNET_TIME_relative_to_string
452 (GNUNET_TIME_absolute_get_remaining
453 (d->max_timeout)), NULL);
457 if (d->username != NULL)
458 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
460 dst = GNUNET_strdup (d->hostname);
463 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
464 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
465 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
466 "-L", "DEBUG", "-s", "-q");
468 if (d->ssh_port_str == NULL)
470 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
478 "-c", d->cfgfile, "-s", "-q", "-T",
479 GNUNET_TIME_relative_to_string
480 (GNUNET_TIME_absolute_get_remaining
481 (d->max_timeout)), NULL);
487 GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-p",
496 "-c", d->cfgfile, "-s", "-q", "-T",
497 GNUNET_TIME_relative_to_string
498 (GNUNET_TIME_absolute_get_remaining
499 (d->max_timeout)), NULL);
505 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
506 _("Could not start `%s' process to start GNUnet.\n"),
507 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
511 cb (d->cb_cls, NULL, d->cfg, d,
513 d->hostname) ? _("Failed to start `gnunet-arm' process.\n") :
514 _("Failed to start `ssh' process.\n"));
518 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
519 "Started `%s', waiting for `%s' to be up.\n", "gnunet-arm",
520 "gnunet-service-core");
522 d->phase = SP_START_ARMING;
524 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
527 case SP_START_ARMING:
528 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
530 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
535 cb (d->cb_cls, NULL, d->cfg, d,
537 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
538 _("`ssh' does not seem to terminate.\n"));
541 GNUNET_CONFIGURATION_destroy (d->cfg);
544 if (d->cfgfile != NULL)
546 GNUNET_free (d->cfgfile);
549 GNUNET_free_non_null (d->hostname);
550 GNUNET_free_non_null (d->username);
551 GNUNET_free (d->proc);
552 // GNUNET_free (d); // FIXME (could this leak)
553 d->hostname = NULL; // Quick hack to avoid crashing (testing need to be
554 d->cfg = NULL; // overhauled anyway, and the error managing is
555 // pretty broken anyway.
560 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully started `%s'.\n",
568 GNUNET_free (d->proc);
569 d->phase = SP_START_CORE;
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling CORE_connect\n");
575 if (d->server != NULL)
576 GNUNET_CORE_disconnect (d->server);
578 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
581 if (GNUNET_YES == d->dead)
582 GNUNET_TESTING_daemon_stop (d,
583 GNUNET_TIME_absolute_get_remaining
584 (d->max_timeout), d->dead_cb,
585 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
586 else if (NULL != d->cb)
587 d->cb (d->cb_cls, &d->id, d->cfg, d,
588 _("Failed to connect to transport service!\n"));
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "Connected to transport service `%s', getting HELLO\n",
594 GNUNET_i2s (&d->id));
597 d->ghh = GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
598 /* FIXME: store task ID somewhere! */
599 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
603 * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
604 d->running = GNUNET_YES;
605 d->phase = SP_GET_HELLO;
608 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
610 if (d->server != NULL)
611 GNUNET_CORE_disconnect (d->server);
613 GNUNET_TRANSPORT_disconnect (d->th);
617 cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
618 GNUNET_CONFIGURATION_destroy (d->cfg);
619 GNUNET_free (d->cfgfile);
620 GNUNET_free_non_null (d->hostname);
621 GNUNET_free_non_null (d->username);
625 if (d->hello != NULL)
627 GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
629 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
630 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
636 case SP_SERVICE_START:
637 /* confirm gnunet-arm exited */
638 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
640 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
645 cb (d->cb_cls, NULL, d->cfg, d,
647 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
648 _("`ssh' does not seem to terminate.\n"));
653 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
658 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
663 cb (d->cb_cls, NULL, d->cfg, d,
667 ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
668 : _("`ssh' does not seem to terminate.\n"));
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
677 d->phase = SP_START_DONE;
679 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
681 case SP_SERVICE_SHUTDOWN_START:
682 /* confirm copying complete */
683 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
685 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
687 if (NULL != d->dead_cb)
688 d->dead_cb (d->dead_cb_cls,
690 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
695 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
700 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
702 if (NULL != d->dead_cb)
703 d->dead_cb (d->dead_cb_cls,
705 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
712 if (NULL != d->dead_cb)
713 d->dead_cb (d->dead_cb_cls, NULL);
715 case SP_SHUTDOWN_START:
716 /* confirm copying complete */
717 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
719 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
721 if (NULL != d->dead_cb)
722 d->dead_cb (d->dead_cb_cls,
724 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
727 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
729 GNUNET_TRANSPORT_disconnect (d->th);
734 GNUNET_CONFIGURATION_destroy (d->cfg);
737 if (d->cfgfile != NULL)
739 GNUNET_free (d->cfgfile);
742 GNUNET_free_non_null (d->hello);
743 GNUNET_free_non_null (d->hostname);
744 GNUNET_free_non_null (d->username);
745 GNUNET_free_non_null (d->shortname);
746 GNUNET_free_non_null (d->proc);
753 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
757 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
759 if (NULL != d->dead_cb)
760 d->dead_cb (d->dead_cb_cls,
762 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
765 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
767 GNUNET_TRANSPORT_disconnect (d->th);
770 if (d->server != NULL)
772 GNUNET_CORE_disconnect (d->server);
775 GNUNET_CONFIGURATION_destroy (d->cfg);
777 GNUNET_free (d->cfgfile);
778 GNUNET_free_non_null (d->hello);
779 GNUNET_free_non_null (d->hostname);
780 GNUNET_free_non_null (d->username);
781 GNUNET_free_non_null (d->shortname);
782 GNUNET_free_non_null (d->proc);
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
790 if (d->server != NULL)
792 GNUNET_CORE_disconnect (d->server);
798 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
800 GNUNET_TRANSPORT_disconnect (d->th);
804 if (NULL != d->dead_cb)
805 d->dead_cb (d->dead_cb_cls, NULL);
807 /* state clean up and notifications */
808 if (d->churn == GNUNET_NO)
810 GNUNET_CONFIGURATION_destroy (d->cfg);
812 GNUNET_free (d->cfgfile);
813 GNUNET_free_non_null (d->hostname);
814 GNUNET_free_non_null (d->username);
817 GNUNET_free_non_null (d->hello);
819 GNUNET_free_non_null (d->shortname);
820 GNUNET_free_non_null (d->proc);
823 if (d->churn == GNUNET_NO)
827 case SP_CONFIG_UPDATE:
828 /* confirm copying complete */
829 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
831 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
836 cb (d->cb_cls, NULL, d->cfg, d,
837 _("`scp' does not seem to terminate.\n"));
842 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
846 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
848 if (NULL != d->update_cb)
849 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
854 "Successfully copied configuration file.\n");
856 if (NULL != d->update_cb)
857 d->update_cb (d->update_cb_cls, NULL);
858 d->phase = SP_START_DONE;
864 * Continues GNUnet daemon startup when user wanted to be notified
865 * once a hostkey was generated (for creating friends files, blacklists,
868 * @param daemon the daemon to finish starting
871 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
873 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
874 daemon->phase = SP_TOPOLOGY_SETUP;
878 * Check whether the given daemon is running.
880 * @param daemon the daemon to check
882 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
883 * daemon is down, GNUNET_SYSERR on error.
886 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
889 return GNUNET_SYSERR;
891 if (daemon->running == GNUNET_YES)
898 * Starts a GNUnet daemon service which has been previously stopped.
900 * @param d the daemon for which the service should be started
901 * @param service the name of the service to start
902 * @param timeout how long to wait for process for shutdown to complete
903 * @param cb function called once the service starts
904 * @param cb_cls closure for cb
907 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
909 struct GNUNET_TIME_Relative
911 GNUNET_TESTING_NotifyDaemonRunning
919 GNUNET_assert (d->running == GNUNET_YES);
921 if (d->phase == SP_CONFIG_UPDATE)
923 GNUNET_SCHEDULER_cancel (d->task);
924 d->phase = SP_START_DONE;
927 if (d->churned_services == NULL)
929 d->cb (d->cb_cls, &d->id, d->cfg, d,
930 "No service has been churned off yet!!");
933 d->phase = SP_SERVICE_START;
934 GNUNET_free (d->churned_services);
935 d->churned_services = NULL;
936 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
937 /* Check if this is a local or remote process */
938 if (NULL != d->hostname)
941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
942 "Starting gnunet-arm with config `%s' on host `%s'.\n",
943 d->cfgfile, d->hostname);
946 if (d->username != NULL)
947 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
949 arg = GNUNET_strdup (d->hostname);
951 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
959 "-c", d->cfgfile, "-i", service, "-q",
961 GNUNET_TIME_relative_to_string (timeout),
963 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
964 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
965 arg, "gnunet-arm", d->cfgfile, service);
971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
972 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
974 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
978 "-c", d->cfgfile, "-i", service, "-q",
980 GNUNET_TIME_relative_to_string (timeout),
984 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
985 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
989 * Starts a GNUnet daemon's service.
991 * @param d the daemon for which the service should be started
992 * @param service the name of the service to start
993 * @param timeout how long to wait for process for startup
994 * @param cb function called once gnunet-arm returns
995 * @param cb_cls closure for cb
998 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
1000 struct GNUNET_TIME_Relative timeout,
1001 GNUNET_TESTING_NotifyDaemonRunning cb,
1009 GNUNET_assert (service != NULL);
1010 GNUNET_assert (d->running == GNUNET_YES);
1011 GNUNET_assert (d->phase == SP_START_DONE);
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 _("Starting service %s for peer `%4s'\n"), service,
1016 GNUNET_i2s (&d->id));
1019 d->phase = SP_SERVICE_START;
1020 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1021 /* Check if this is a local or remote process */
1022 if (NULL != d->hostname)
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1027 d->cfgfile, d->hostname);
1030 if (d->username != NULL)
1031 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1033 arg = GNUNET_strdup (d->hostname);
1035 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1043 "-c", d->cfgfile, "-i", service, "-q",
1045 GNUNET_TIME_relative_to_string (timeout),
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
1049 arg, "gnunet-arm", d->cfgfile, service,
1050 GNUNET_TIME_relative_to_string (timeout));
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1057 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1059 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1063 "-c", d->cfgfile, "-i", service, "-q",
1065 GNUNET_TIME_relative_to_string (timeout),
1067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1068 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1069 "gnunet-arm", d->cfgfile, service,
1070 GNUNET_TIME_relative_to_string (timeout));
1073 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1074 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1078 * Start a peer that has previously been stopped using the daemon_stop
1079 * call (and files weren't deleted and the allow restart flag)
1081 * @param daemon the daemon to start (has been previously stopped)
1082 * @param timeout how long to wait for restart
1083 * @param cb the callback for notification when the peer is running
1084 * @param cb_cls closure for the callback
1087 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1088 struct GNUNET_TIME_Relative timeout,
1089 GNUNET_TESTING_NotifyDaemonRunning cb,
1092 if (daemon->running == GNUNET_YES)
1094 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1095 "Daemon already running, can't restart!");
1100 daemon->cb_cls = cb_cls;
1101 daemon->phase = SP_TOPOLOGY_SETUP;
1102 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1103 /* FIXME: why add_continuation? */
1104 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1105 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1109 * Starts a GNUnet daemon. GNUnet must be installed on the target
1110 * system and available in the PATH. The machine must furthermore be
1111 * reachable via "ssh" (unless the hostname is "NULL") without the
1112 * need to enter a password.
1114 * @param cfg configuration to use
1115 * @param timeout how long to wait starting up peers
1116 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1117 * to really start the peer (default)
1118 * @param hostname name of the machine where to run GNUnet
1119 * (use NULL for localhost).
1120 * @param ssh_username ssh username to use when connecting to hostname
1121 * @param sshport port to pass to ssh process when connecting to hostname
1122 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1123 * @param hostkey_callback function to call once the hostkey has been
1124 * generated for this peer, but it hasn't yet been started
1125 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1126 * @param hostkey_cls closure for hostkey callback
1127 * @param cb function to call once peer is up, or failed to start
1128 * @param cb_cls closure for cb
1129 * @return handle to the daemon (actual start will be completed asynchronously)
1131 struct GNUNET_TESTING_Daemon *
1132 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1133 struct GNUNET_TIME_Relative timeout, int pretend,
1134 const char *hostname, const char *ssh_username,
1135 uint16_t sshport, const char *hostkey,
1136 GNUNET_TESTING_NotifyHostkeyCreated
1137 hostkey_callback, void *hostkey_cls,
1138 GNUNET_TESTING_NotifyDaemonRunning cb,
1141 struct GNUNET_TESTING_Daemon *ret;
1145 char *baseservicehome;
1148 char *temp_file_name;
1149 struct GNUNET_DISK_FileHandle *fn;
1150 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1151 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1153 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1154 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1157 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1160 ret->ssh_port_str = NULL;
1162 /* Find service home and base service home directories, create it if it doesn't exist */
1163 GNUNET_assert (GNUNET_OK ==
1164 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1168 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1169 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1170 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1171 GNUNET_free (temp_file_name);
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1176 if (NULL == ret->cfgfile)
1178 GNUNET_free_non_null (ret->ssh_port_str);
1179 GNUNET_free_non_null (ret->hostname);
1183 ret->hostkey_callback = hostkey_callback;
1184 ret->hostkey_cls = hostkey_cls;
1186 ret->cb_cls = cb_cls;
1187 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1188 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1189 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1192 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1194 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1195 GNUNET_assert (private_key != NULL);
1196 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1197 GNUNET_CRYPTO_hash (&public_key,
1198 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1199 &ret->id.hashPubKey);
1200 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1201 ret->have_hostkey = GNUNET_YES;
1202 GNUNET_CRYPTO_rsa_key_free (private_key);
1205 /* Write hostkey to file, if we were given one */
1207 if (hostkey != NULL)
1209 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1210 fn = GNUNET_DISK_file_open (hostkeyfile,
1211 GNUNET_DISK_OPEN_READWRITE |
1212 GNUNET_DISK_OPEN_CREATE,
1213 GNUNET_DISK_PERM_USER_READ |
1214 GNUNET_DISK_PERM_USER_WRITE);
1215 GNUNET_assert (fn != NULL);
1216 GNUNET_assert (HOSTKEYFILESIZE ==
1217 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1218 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1221 /* write configuration to temporary file */
1222 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1224 if (0 != UNLINK (ret->cfgfile))
1225 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1227 GNUNET_CONFIGURATION_destroy (ret->cfg);
1228 GNUNET_free_non_null (ret->hostname);
1229 GNUNET_free (ret->cfgfile);
1233 if (ssh_username != NULL)
1234 username = GNUNET_strdup (ssh_username);
1235 if ((ssh_username == NULL) &&
1237 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1240 if (NULL != getenv ("USER"))
1241 username = GNUNET_strdup (getenv ("USER"));
1245 ret->username = username;
1247 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1249 /* copy directory to remote host */
1250 if (NULL != hostname)
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Copying configuration directory to host `%s'.\n", hostname);
1256 baseservicehome = GNUNET_strdup (servicehome);
1257 /* Remove trailing /'s */
1258 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1259 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1260 /* Find next directory /, jump one ahead */
1261 slash = strrchr (baseservicehome, '/');
1265 ret->phase = SP_COPYING;
1266 if (NULL != username)
1267 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1269 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1270 GNUNET_free (baseservicehome);
1271 if (ret->ssh_port_str == NULL)
1273 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1277 servicehome, arg, NULL);
1279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1280 "copying directory with command scp -r %s %s\n",
1287 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r", "-P",
1292 servicehome, arg, NULL);
1295 if (NULL == ret->proc)
1297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1299 ("Could not start `%s' process to copy configuration directory.\n"),
1301 if (0 != UNLINK (ret->cfgfile))
1302 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1304 GNUNET_CONFIGURATION_destroy (ret->cfg);
1305 GNUNET_free_non_null (ret->hostname);
1306 GNUNET_free_non_null (ret->username);
1307 GNUNET_free (ret->cfgfile);
1309 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1310 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1312 GNUNET_free_non_null (hostkeyfile);
1313 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1314 GNUNET_free (servicehome);
1319 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1321 GNUNET_free_non_null (hostkeyfile);
1322 GNUNET_free (servicehome);
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "No need to copy configuration file since we are running locally.\n");
1329 ret->phase = SP_COPIED;
1330 /* FIXME: why add_cont? */
1331 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1332 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1334 GNUNET_free_non_null (hostkeyfile);
1335 GNUNET_free (servicehome);
1341 * Restart (stop and start) a GNUnet daemon.
1343 * @param d the daemon that should be restarted
1344 * @param cb function called once the daemon is (re)started
1345 * @param cb_cls closure for cb
1348 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1349 GNUNET_TESTING_NotifyDaemonRunning cb,
1358 d->dead = GNUNET_YES;
1365 if (d->phase == SP_CONFIG_UPDATE)
1367 GNUNET_SCHEDULER_cancel (d->task);
1368 d->phase = SP_START_DONE;
1370 if (d->server != NULL)
1372 GNUNET_CORE_disconnect (d->server);
1378 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1380 GNUNET_TRANSPORT_disconnect (d->th);
1383 /* state clean up and notifications */
1384 GNUNET_free_non_null (d->hello);
1387 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1388 GNUNET_i2s (&d->id));
1391 d->phase = SP_START_ARMING;
1393 /* Check if this is a local or remote process */
1394 if (NULL != d->hostname)
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1399 d->cfgfile, d->hostname);
1402 if (d->username != NULL)
1403 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1405 arg = GNUNET_strdup (d->hostname);
1407 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1415 "-c", d->cfgfile, "-e", "-r", NULL);
1416 /* Use -r to restart arm and all services */
1423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1424 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1426 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1430 "-c", d->cfgfile, "-e", "-r", NULL);
1433 GNUNET_free_non_null (del_arg);
1435 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1441 * Stops a GNUnet daemon.
1443 * @param d the daemon that should be stopped
1444 * @param service the name of the service to stop
1445 * @param timeout how long to wait for process for shutdown to complete
1446 * @param cb function called once the daemon was stopped
1447 * @param cb_cls closure for cb
1450 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1451 const char *service,
1452 struct GNUNET_TIME_Relative timeout,
1453 GNUNET_TESTING_NotifyCompletion cb,
1459 d->dead_cb_cls = cb_cls;
1461 GNUNET_assert (d->running == GNUNET_YES);
1463 if (d->phase == SP_CONFIG_UPDATE)
1465 GNUNET_SCHEDULER_cancel (d->task);
1466 d->phase = SP_START_DONE;
1470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1471 GNUNET_i2s (&d->id));
1473 if (d->churned_services != NULL)
1475 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1478 d->phase = SP_SERVICE_SHUTDOWN_START;
1479 d->churned_services = GNUNET_strdup (service);
1480 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1481 /* Check if this is a local or remote process */
1482 if (NULL != d->hostname)
1485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1486 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1487 d->cfgfile, d->hostname);
1490 if (d->username != NULL)
1491 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1493 arg = GNUNET_strdup (d->hostname);
1495 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1503 "-c", d->cfgfile, "-k", service, "-q",
1505 GNUNET_TIME_relative_to_string (timeout),
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1509 arg, "gnunet-arm", d->cfgfile, service);
1515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1516 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1518 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1522 "-c", d->cfgfile, "-k", service, "-q",
1524 GNUNET_TIME_relative_to_string (timeout),
1528 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1529 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1534 * Stops a GNUnet daemon.
1536 * @param d the daemon that should be stopped
1537 * @param timeout how long to wait for process for shutdown to complete
1538 * @param cb function called once the daemon was stopped
1539 * @param cb_cls closure for cb
1540 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1542 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1543 * GNUNET_NO to kill off and clean up for good
1546 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1547 struct GNUNET_TIME_Relative timeout,
1548 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1549 int delete_files, int allow_restart)
1555 d->dead_cb_cls = cb_cls;
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1561 GNUNET_i2s (&d->id));
1563 d->dead = GNUNET_YES;
1567 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1569 /* Free what was left from churning! */
1570 GNUNET_assert (d->cfg != NULL);
1571 GNUNET_CONFIGURATION_destroy (d->cfg);
1572 if (delete_files == GNUNET_YES)
1574 if (0 != UNLINK (d->cfgfile))
1576 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1579 GNUNET_free (d->cfgfile);
1580 GNUNET_free_non_null (d->hostname);
1581 GNUNET_free_non_null (d->username);
1582 if (NULL != d->dead_cb)
1583 d->dead_cb (d->dead_cb_cls, NULL);
1589 if (delete_files == GNUNET_YES)
1591 GNUNET_asprintf (&del_arg, "-d");
1594 if (d->phase == SP_CONFIG_UPDATE)
1596 GNUNET_SCHEDULER_cancel (d->task);
1597 d->phase = SP_START_DONE;
1599 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1600 if (d->server != NULL)
1602 GNUNET_CORE_disconnect (d->server);
1606 /* shutdown ARM process (will terminate others) */
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1609 GNUNET_i2s (&d->id));
1611 d->phase = SP_SHUTDOWN_START;
1612 d->running = GNUNET_NO;
1613 if (allow_restart == GNUNET_YES)
1614 d->churn = GNUNET_YES;
1617 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1619 GNUNET_TRANSPORT_disconnect (d->th);
1622 /* Check if this is a local or remote process */
1623 if (NULL != d->hostname)
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1628 d->cfgfile, d->hostname);
1631 if (d->username != NULL)
1632 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1634 arg = GNUNET_strdup (d->hostname);
1636 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1644 "-c", d->cfgfile, "-e", "-q", "-T",
1645 GNUNET_TIME_relative_to_string (timeout),
1647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1648 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1649 arg, "gnunet-arm", d->cfgfile, del_arg);
1650 /* Use -e to end arm, and -d to remove temp files */
1656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1657 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1659 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1663 "-c", d->cfgfile, "-e", "-q", "-T",
1664 GNUNET_TIME_relative_to_string (timeout),
1666 GNUNET_assert (NULL != d->proc);
1669 GNUNET_free_non_null (del_arg);
1670 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1671 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1676 * Changes the configuration of a GNUnet daemon.
1678 * @param d the daemon that should be modified
1679 * @param cfg the new configuration for the daemon
1680 * @param cb function called once the configuration was changed
1681 * @param cb_cls closure for cb
1684 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1685 struct GNUNET_CONFIGURATION_Handle *cfg,
1686 GNUNET_TESTING_NotifyCompletion cb,
1691 if (d->phase != SP_START_DONE)
1696 ("Peer not yet running, can not change configuration at this point."));
1700 /* 1) write configuration to temporary file */
1701 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1704 cb (cb_cls, _("Failed to write new configuration to disk."));
1708 /* 2) copy file to remote host (if necessary) */
1709 if (NULL == d->hostname)
1711 /* signal success */
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1718 "Copying updated configuration file to remote host `%s'.\n",
1721 d->phase = SP_CONFIG_UPDATE;
1722 if (NULL != d->username)
1723 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1725 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1726 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1730 d->cfgfile, arg, NULL);
1732 if (NULL == d->proc)
1734 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1735 _("Could not start `%s' process to copy configuration file.\n"),
1738 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1739 d->phase = SP_START_DONE;
1743 d->update_cb_cls = cb_cls;
1745 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1750 * Data kept for each pair of peers that we try
1753 struct GNUNET_TESTING_ConnectContext
1756 * Testing handle to the first daemon.
1758 struct GNUNET_TESTING_Daemon *d1;
1761 * Handle to core of first daemon (to check connect)
1763 struct GNUNET_CORE_Handle *d1core;
1766 * Have we actually connected to the core of the first daemon yet?
1771 * Testing handle to the second daemon.
1773 struct GNUNET_TESTING_Daemon *d2;
1776 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1778 struct GNUNET_TRANSPORT_Handle *d1th;
1781 * Function to call once we are done (or have timed out).
1783 GNUNET_TESTING_NotifyConnection cb;
1791 * The relative timeout from whence this connect attempt was
1792 * started. Allows for reconnect attempts.
1794 struct GNUNET_TIME_Relative relative_timeout;
1797 * Maximum number of connect attempts, will retry connection
1798 * this number of times on failures.
1800 unsigned int connect_attempts;
1803 * Hello timeout task
1805 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1808 * Connect timeout task
1810 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1813 * When should this operation be complete (or we must trigger
1816 struct GNUNET_TIME_Relative timeout_hello;
1819 * Was the connection attempt successful?
1824 * When connecting, do we need to send the HELLO?
1829 * The distance between the two connected peers
1835 /** Forward declaration **/
1837 reattempt_daemons_connect (void *cls,
1838 const struct GNUNET_SCHEDULER_TaskContext *tc);
1842 * Notify callback about success or failure of the attempt
1843 * to connect the two peers
1845 * @param cls our "struct GNUNET_TESTING_ConnectContext" (freed)
1846 * @param tc reason tells us if we succeeded or failed
1849 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1851 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1853 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1854 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1856 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1857 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1860 if (ctx->d1th != NULL)
1861 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1863 if (ctx->d1core != NULL)
1864 GNUNET_CORE_disconnect (ctx->d1core);
1867 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1873 if (ctx->connected == GNUNET_YES)
1875 if (ctx->cb != NULL)
1877 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
1878 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1881 else if (ctx->connect_attempts > 0)
1883 ctx->d1core_ready = GNUNET_NO;
1885 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1890 if (ctx->cb != NULL)
1892 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1893 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
1901 * Success, connection is up. Signal client our success.
1903 * @param cls our "struct GNUNET_TESTING_ConnectContext"
1904 * @param peer identity of the peer that has connected
1905 * @param atsi performance information
1906 * @param atsi_count number of records in 'atsi'
1910 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
1911 const struct GNUNET_ATS_Information *atsi,
1912 unsigned int atsi_count)
1914 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peer %s to peer %s\n",
1918 ctx->d1->shortname, GNUNET_i2s (peer));
1921 if (0 != memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1923 ctx->connected = GNUNET_YES;
1924 ctx->distance = 0; /* FIXME: distance */
1925 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1927 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1928 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1930 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1931 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
1936 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1938 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1939 struct GNUNET_MessageHeader *hello;
1941 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1942 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1944 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
1945 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
1946 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
1948 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1949 GNUNET_assert (hello != NULL);
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
1952 ctx->d2->shortname, ctx->d1->shortname);
1954 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1955 GNUNET_assert (ctx->d1core != NULL);
1957 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1958 "Sending connect request to TRANSPORT of %s for peer %s\n",
1959 GNUNET_i2s (&ctx->d1->id),
1960 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1962 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
1963 ctx->timeout_hello =
1964 GNUNET_TIME_relative_add (ctx->timeout_hello,
1965 GNUNET_TIME_relative_multiply
1966 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1968 ctx->hello_send_task =
1969 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
1973 * Notify of a successful connection to the core service.
1975 * @param cls a ConnectContext
1976 * @param server handle to the core service
1977 * @param my_identity the peer identity of this peer
1980 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
1981 const struct GNUNET_PeerIdentity *my_identity)
1983 struct GNUNET_TESTING_ConnectContext *connect_ctx = cls;
1985 connect_ctx->d1core_ready = GNUNET_YES;
1987 if (connect_ctx->send_hello == GNUNET_NO)
1989 GNUNET_TRANSPORT_try_connect (connect_ctx->d1th, &connect_ctx->d2->id);
1991 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1992 "Sending connect request to TRANSPORT of %s for peer %s\n",
1993 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
2001 * Try to connect again some peers that failed in an earlier attempt. This will
2002 * be tried as many times as connection_attempts in the configuration file.
2004 * @param cls Closure (connection context between the two peers).
2005 * @param tc TaskContext.
2008 reattempt_daemons_connect (void *cls,
2009 const struct GNUNET_SCHEDULER_TaskContext *tc)
2011 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2013 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2014 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2016 #if DEBUG_TESTING_RECONNECT
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2018 "re-attempting connect of peer %s to peer %s\n",
2019 ctx->d1->shortname, ctx->d2->shortname);
2021 ctx->connect_attempts--;
2022 GNUNET_assert (ctx->d1core == NULL);
2023 ctx->d1core_ready = GNUNET_NO;
2025 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2026 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
2027 GNUNET_NO, no_handlers);
2028 if (ctx->d1core == NULL)
2030 if (NULL != ctx->cb)
2031 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2032 ctx->d2->cfg, ctx->d1, ctx->d2,
2033 _("Failed to connect to core service of first peer!\n"));
2038 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
2039 if (NULL != ctx->d2->hello)
2041 #if DEBUG_TESTING_RECONNECT
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "updating %s's HELLO\n",
2043 ctx->d2->shortname);
2045 GNUNET_free (ctx->d2->hello);
2046 ctx->d2->hello = NULL;
2047 if (NULL != ctx->d2->th)
2049 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->ghh);
2050 ctx->d2->ghh = NULL;
2051 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
2054 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2056 GNUNET_assert (ctx->d2->th != NULL);
2058 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2060 #if DEBUG_TESTING_RECONNECT
2063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "didn't have %s's HELLO\n",
2064 ctx->d2->shortname);
2068 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
2070 #if DEBUG_TESTING_RECONNECT
2071 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2072 "didn't have %s's HELLO, trying to get it now\n",
2073 ctx->d2->shortname);
2076 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2078 if (NULL == ctx->d2->th)
2080 GNUNET_CORE_disconnect (ctx->d1core);
2082 if (NULL != ctx->cb)
2083 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2084 ctx->d2->cfg, ctx->d1, ctx->d2,
2085 _("Failed to connect to transport service!\n"));
2089 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2091 #if DEBUG_TESTING_RECONNECT
2094 if (NULL == ctx->d2->hello)
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2097 "didn't have %s's HELLO but th wasn't NULL, not trying!!\n",
2098 ctx->d2->shortname);
2103 if (ctx->send_hello == GNUNET_YES)
2105 #if DEBUG_TESTING_RECONNECT
2106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending %s's HELLO to %s\n",
2107 ctx->d1->shortname, ctx->d2->shortname);
2110 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2112 if (ctx->d1th == NULL)
2114 GNUNET_CORE_disconnect (ctx->d1core);
2116 if (NULL != ctx->cb)
2117 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2118 ctx->d2->cfg, ctx->d1, ctx->d2,
2119 _("Failed to connect to transport service!\n"));
2122 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2123 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2127 #if DEBUG_TESTING_RECONNECT
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to reconnect %s to %s\n",
2129 ctx->d1->shortname, ctx->d2->shortname);
2131 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
2134 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2135 ¬ify_connect_result, ctx);
2139 * Iterator for currently known peers, to ensure
2140 * that we don't try to send duplicate connect
2143 * @param cls our "struct GNUNET_TESTING_ConnectContext"
2144 * @param peer identity of the peer that has connected,
2145 * NULL when iteration has finished
2146 * @param atsi performance information
2147 * @param atsi_count number of records in 'atsi'
2151 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2152 const struct GNUNET_ATS_Information *atsi,
2153 unsigned int atsi_count)
2155 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2157 if ((peer != NULL) &&
2158 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2160 ctx->connected = GNUNET_YES;
2161 ctx->distance = 0; /* FIXME: distance */
2165 return; /* ignore other peers */
2166 /* peer == NULL: End of iteration over peers */
2168 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->timeout_task);
2169 if (ctx->connected == GNUNET_YES)
2171 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2175 /* Peer not already connected, need to schedule connect request! */
2176 if (ctx->d1core == NULL)
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2180 "Peers are NOT connected, connecting to core!\n");
2183 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2184 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
2185 GNUNET_NO, no_handlers);
2188 if (ctx->d1core == NULL)
2190 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2194 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 */
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "Don't have d2's HELLO, trying to get it!\n");
2201 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2203 if (ctx->d2->th == NULL)
2205 GNUNET_CORE_disconnect (ctx->d1core);
2208 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2212 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2215 if (ctx->send_hello == GNUNET_YES)
2218 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2220 if (ctx->d1th == NULL)
2222 GNUNET_CORE_disconnect (ctx->d1core);
2225 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2228 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2229 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2233 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2234 ¬ify_connect_result, ctx);
2240 * Establish a connection between two GNUnet daemons. The daemons
2241 * must both be running and not be stopped until either the
2242 * 'cb' callback is called OR the connection request has been
2243 * explicitly cancelled.
2245 * @param d1 handle for the first daemon
2246 * @param d2 handle for the second daemon
2247 * @param timeout how long is the connection attempt
2249 * @param max_connect_attempts how many times should we try to reconnect
2251 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2252 * the HELLO has already been exchanged
2253 * @param cb function to call at the end
2254 * @param cb_cls closure for cb
2255 * @return handle to cancel the request
2257 struct GNUNET_TESTING_ConnectContext *
2258 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2259 struct GNUNET_TESTING_Daemon *d2,
2260 struct GNUNET_TIME_Relative timeout,
2261 unsigned int max_connect_attempts,
2263 GNUNET_TESTING_NotifyConnection cb,
2266 struct GNUNET_TESTING_ConnectContext *ctx;
2268 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2271 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2272 _("Peers are not fully running yet, can not connect!\n"));
2273 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2277 ctx = GNUNET_malloc (sizeof (struct GNUNET_TESTING_ConnectContext));
2280 ctx->timeout_hello =
2281 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2282 ctx->relative_timeout =
2283 GNUNET_TIME_relative_divide (timeout, max_connect_attempts);
2285 ctx->cb_cls = cb_cls;
2286 ctx->connect_attempts = max_connect_attempts;
2287 ctx->connected = GNUNET_NO;
2288 ctx->send_hello = send_hello;
2290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to connect peer %s to peer %s\n",
2291 d1->shortname, d2->shortname);
2294 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2295 GNUNET_assert (GNUNET_OK ==
2296 GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id,
2297 &core_initial_iteration, ctx));
2303 * Cancel an attempt to connect two daemons.
2305 * @param cc connect context
2308 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext *cc)
2310 if (GNUNET_SCHEDULER_NO_TASK != cc->timeout_task)
2312 GNUNET_SCHEDULER_cancel (cc->timeout_task);
2313 cc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2315 if (GNUNET_SCHEDULER_NO_TASK != cc->hello_send_task)
2317 GNUNET_SCHEDULER_cancel (cc->hello_send_task);
2318 cc->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2320 if (NULL != cc->d1core)
2322 GNUNET_CORE_disconnect (cc->d1core);
2325 if (NULL != cc->d1th)
2327 GNUNET_TRANSPORT_disconnect (cc->d1th);
2334 /* end of testing.c */