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);
776 GNUNET_free (d->cfgfile);
777 GNUNET_free_non_null (d->hello);
778 GNUNET_free_non_null (d->hostname);
779 GNUNET_free_non_null (d->username);
780 GNUNET_free_non_null (d->shortname);
781 GNUNET_free_non_null (d->proc);
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
789 if (d->server != NULL)
791 GNUNET_CORE_disconnect (d->server);
797 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
799 GNUNET_TRANSPORT_disconnect (d->th);
803 if (NULL != d->dead_cb)
804 d->dead_cb (d->dead_cb_cls, NULL);
806 /* state clean up and notifications */
807 if (d->churn == GNUNET_NO)
809 GNUNET_CONFIGURATION_destroy (d->cfg);
810 GNUNET_free (d->cfgfile);
811 GNUNET_free_non_null (d->hostname);
812 GNUNET_free_non_null (d->username);
815 GNUNET_free_non_null (d->hello);
817 GNUNET_free_non_null (d->shortname);
818 GNUNET_free_non_null (d->proc);
821 if (d->churn == GNUNET_NO)
825 case SP_CONFIG_UPDATE:
826 /* confirm copying complete */
827 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
829 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
834 cb (d->cb_cls, NULL, d->cfg, d,
835 _("`scp' does not seem to terminate.\n"));
840 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
844 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
846 if (NULL != d->update_cb)
847 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Successfully copied configuration file.\n");
854 if (NULL != d->update_cb)
855 d->update_cb (d->update_cb_cls, NULL);
856 d->phase = SP_START_DONE;
862 * Continues GNUnet daemon startup when user wanted to be notified
863 * once a hostkey was generated (for creating friends files, blacklists,
866 * @param daemon the daemon to finish starting
869 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
871 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
872 daemon->phase = SP_TOPOLOGY_SETUP;
876 * Check whether the given daemon is running.
878 * @param daemon the daemon to check
880 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
881 * daemon is down, GNUNET_SYSERR on error.
884 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
887 return GNUNET_SYSERR;
889 if (daemon->running == GNUNET_YES)
896 * Starts a GNUnet daemon service which has been previously stopped.
898 * @param d the daemon for which the service should be started
899 * @param service the name of the service to start
900 * @param timeout how long to wait for process for shutdown to complete
901 * @param cb function called once the service starts
902 * @param cb_cls closure for cb
905 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
907 struct GNUNET_TIME_Relative
909 GNUNET_TESTING_NotifyDaemonRunning
917 GNUNET_assert (d->running == GNUNET_YES);
919 if (d->phase == SP_CONFIG_UPDATE)
921 GNUNET_SCHEDULER_cancel (d->task);
922 d->phase = SP_START_DONE;
925 if (d->churned_services == NULL)
927 d->cb (d->cb_cls, &d->id, d->cfg, d,
928 "No service has been churned off yet!!");
931 d->phase = SP_SERVICE_START;
932 GNUNET_free (d->churned_services);
933 d->churned_services = NULL;
934 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
935 /* Check if this is a local or remote process */
936 if (NULL != d->hostname)
939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
940 "Starting gnunet-arm with config `%s' on host `%s'.\n",
941 d->cfgfile, d->hostname);
944 if (d->username != NULL)
945 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
947 arg = GNUNET_strdup (d->hostname);
949 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
957 "-c", d->cfgfile, "-i", service, "-q",
959 GNUNET_TIME_relative_to_string (timeout),
961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
962 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
963 arg, "gnunet-arm", d->cfgfile, service);
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
972 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
976 "-c", d->cfgfile, "-i", service, "-q",
978 GNUNET_TIME_relative_to_string (timeout),
982 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
983 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
987 * Starts a GNUnet daemon's service.
989 * @param d the daemon for which the service should be started
990 * @param service the name of the service to start
991 * @param timeout how long to wait for process for startup
992 * @param cb function called once gnunet-arm returns
993 * @param cb_cls closure for cb
996 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
998 struct GNUNET_TIME_Relative timeout,
999 GNUNET_TESTING_NotifyDaemonRunning cb,
1007 GNUNET_assert (service != NULL);
1008 GNUNET_assert (d->running == GNUNET_YES);
1009 GNUNET_assert (d->phase == SP_START_DONE);
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 _("Starting service %s for peer `%4s'\n"), service,
1014 GNUNET_i2s (&d->id));
1017 d->phase = SP_SERVICE_START;
1018 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1019 /* Check if this is a local or remote process */
1020 if (NULL != d->hostname)
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1025 d->cfgfile, d->hostname);
1028 if (d->username != NULL)
1029 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1031 arg = GNUNET_strdup (d->hostname);
1033 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1041 "-c", d->cfgfile, "-i", service, "-q",
1043 GNUNET_TIME_relative_to_string (timeout),
1045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
1047 arg, "gnunet-arm", d->cfgfile, service,
1048 GNUNET_TIME_relative_to_string (timeout));
1054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1055 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1057 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1061 "-c", d->cfgfile, "-i", service, "-q",
1063 GNUNET_TIME_relative_to_string (timeout),
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1067 "gnunet-arm", d->cfgfile, service,
1068 GNUNET_TIME_relative_to_string (timeout));
1071 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1072 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1076 * Start a peer that has previously been stopped using the daemon_stop
1077 * call (and files weren't deleted and the allow restart flag)
1079 * @param daemon the daemon to start (has been previously stopped)
1080 * @param timeout how long to wait for restart
1081 * @param cb the callback for notification when the peer is running
1082 * @param cb_cls closure for the callback
1085 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1086 struct GNUNET_TIME_Relative timeout,
1087 GNUNET_TESTING_NotifyDaemonRunning cb,
1090 if (daemon->running == GNUNET_YES)
1092 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1093 "Daemon already running, can't restart!");
1098 daemon->cb_cls = cb_cls;
1099 daemon->phase = SP_TOPOLOGY_SETUP;
1100 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1101 /* FIXME: why add_continuation? */
1102 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1103 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1107 * Starts a GNUnet daemon. GNUnet must be installed on the target
1108 * system and available in the PATH. The machine must furthermore be
1109 * reachable via "ssh" (unless the hostname is "NULL") without the
1110 * need to enter a password.
1112 * @param cfg configuration to use
1113 * @param timeout how long to wait starting up peers
1114 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1115 * to really start the peer (default)
1116 * @param hostname name of the machine where to run GNUnet
1117 * (use NULL for localhost).
1118 * @param ssh_username ssh username to use when connecting to hostname
1119 * @param sshport port to pass to ssh process when connecting to hostname
1120 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1121 * @param hostkey_callback function to call once the hostkey has been
1122 * generated for this peer, but it hasn't yet been started
1123 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1124 * @param hostkey_cls closure for hostkey callback
1125 * @param cb function to call once peer is up, or failed to start
1126 * @param cb_cls closure for cb
1127 * @return handle to the daemon (actual start will be completed asynchronously)
1129 struct GNUNET_TESTING_Daemon *
1130 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1131 struct GNUNET_TIME_Relative timeout, int pretend,
1132 const char *hostname, const char *ssh_username,
1133 uint16_t sshport, const char *hostkey,
1134 GNUNET_TESTING_NotifyHostkeyCreated
1135 hostkey_callback, void *hostkey_cls,
1136 GNUNET_TESTING_NotifyDaemonRunning cb,
1139 struct GNUNET_TESTING_Daemon *ret;
1143 char *baseservicehome;
1146 char *temp_file_name;
1147 struct GNUNET_DISK_FileHandle *fn;
1148 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1149 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1151 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1152 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1155 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1158 ret->ssh_port_str = NULL;
1160 /* Find service home and base service home directories, create it if it doesn't exist */
1161 GNUNET_assert (GNUNET_OK ==
1162 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1166 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1167 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1168 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1169 GNUNET_free (temp_file_name);
1171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1172 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1174 if (NULL == ret->cfgfile)
1176 GNUNET_free_non_null (ret->ssh_port_str);
1177 GNUNET_free_non_null (ret->hostname);
1181 ret->hostkey_callback = hostkey_callback;
1182 ret->hostkey_cls = hostkey_cls;
1184 ret->cb_cls = cb_cls;
1185 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1186 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1187 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1190 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1192 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1193 GNUNET_assert (private_key != NULL);
1194 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1195 GNUNET_CRYPTO_hash (&public_key,
1196 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1197 &ret->id.hashPubKey);
1198 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1199 ret->have_hostkey = GNUNET_YES;
1200 GNUNET_CRYPTO_rsa_key_free (private_key);
1203 /* Write hostkey to file, if we were given one */
1205 if (hostkey != NULL)
1207 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1208 fn = GNUNET_DISK_file_open (hostkeyfile,
1209 GNUNET_DISK_OPEN_READWRITE |
1210 GNUNET_DISK_OPEN_CREATE,
1211 GNUNET_DISK_PERM_USER_READ |
1212 GNUNET_DISK_PERM_USER_WRITE);
1213 GNUNET_assert (fn != NULL);
1214 GNUNET_assert (HOSTKEYFILESIZE ==
1215 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1216 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1219 /* write configuration to temporary file */
1220 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1222 if (0 != UNLINK (ret->cfgfile))
1223 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1225 GNUNET_CONFIGURATION_destroy (ret->cfg);
1226 GNUNET_free_non_null (ret->hostname);
1227 GNUNET_free (ret->cfgfile);
1231 if (ssh_username != NULL)
1232 username = GNUNET_strdup (ssh_username);
1233 if ((ssh_username == NULL) &&
1235 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1238 if (NULL != getenv ("USER"))
1239 username = GNUNET_strdup (getenv ("USER"));
1243 ret->username = username;
1245 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1247 /* copy directory to remote host */
1248 if (NULL != hostname)
1251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1252 "Copying configuration directory to host `%s'.\n", hostname);
1254 baseservicehome = GNUNET_strdup (servicehome);
1255 /* Remove trailing /'s */
1256 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1257 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1258 /* Find next directory /, jump one ahead */
1259 slash = strrchr (baseservicehome, '/');
1263 ret->phase = SP_COPYING;
1264 if (NULL != username)
1265 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1267 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1268 GNUNET_free (baseservicehome);
1269 if (ret->ssh_port_str == NULL)
1271 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1275 servicehome, arg, NULL);
1277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1278 "copying directory with command scp -r %s %s\n",
1285 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r", "-P",
1290 servicehome, arg, NULL);
1293 if (NULL == ret->proc)
1295 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1297 ("Could not start `%s' process to copy configuration directory.\n"),
1299 if (0 != UNLINK (ret->cfgfile))
1300 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1302 GNUNET_CONFIGURATION_destroy (ret->cfg);
1303 GNUNET_free_non_null (ret->hostname);
1304 GNUNET_free_non_null (ret->username);
1305 GNUNET_free (ret->cfgfile);
1307 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1308 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1310 GNUNET_free_non_null (hostkeyfile);
1311 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1312 GNUNET_free (servicehome);
1317 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1319 GNUNET_free_non_null (hostkeyfile);
1320 GNUNET_free (servicehome);
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "No need to copy configuration file since we are running locally.\n");
1327 ret->phase = SP_COPIED;
1328 /* FIXME: why add_cont? */
1329 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1330 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1332 GNUNET_free_non_null (hostkeyfile);
1333 GNUNET_free (servicehome);
1339 * Restart (stop and start) a GNUnet daemon.
1341 * @param d the daemon that should be restarted
1342 * @param cb function called once the daemon is (re)started
1343 * @param cb_cls closure for cb
1346 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1347 GNUNET_TESTING_NotifyDaemonRunning cb,
1356 d->dead = GNUNET_YES;
1363 if (d->phase == SP_CONFIG_UPDATE)
1365 GNUNET_SCHEDULER_cancel (d->task);
1366 d->phase = SP_START_DONE;
1368 if (d->server != NULL)
1370 GNUNET_CORE_disconnect (d->server);
1376 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1378 GNUNET_TRANSPORT_disconnect (d->th);
1381 /* state clean up and notifications */
1382 GNUNET_free_non_null (d->hello);
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1386 GNUNET_i2s (&d->id));
1389 d->phase = SP_START_ARMING;
1391 /* Check if this is a local or remote process */
1392 if (NULL != d->hostname)
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1397 d->cfgfile, d->hostname);
1400 if (d->username != NULL)
1401 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1403 arg = GNUNET_strdup (d->hostname);
1405 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1413 "-c", d->cfgfile, "-e", "-r", NULL);
1414 /* Use -r to restart arm and all services */
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1422 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1424 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1428 "-c", d->cfgfile, "-e", "-r", NULL);
1431 GNUNET_free_non_null (del_arg);
1433 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1439 * Stops a GNUnet daemon.
1441 * @param d the daemon that should be stopped
1442 * @param service the name of the service to stop
1443 * @param timeout how long to wait for process for shutdown to complete
1444 * @param cb function called once the daemon was stopped
1445 * @param cb_cls closure for cb
1448 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1449 const char *service,
1450 struct GNUNET_TIME_Relative timeout,
1451 GNUNET_TESTING_NotifyCompletion cb,
1457 d->dead_cb_cls = cb_cls;
1459 GNUNET_assert (d->running == GNUNET_YES);
1461 if (d->phase == SP_CONFIG_UPDATE)
1463 GNUNET_SCHEDULER_cancel (d->task);
1464 d->phase = SP_START_DONE;
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1469 GNUNET_i2s (&d->id));
1471 if (d->churned_services != NULL)
1473 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1476 d->phase = SP_SERVICE_SHUTDOWN_START;
1477 d->churned_services = GNUNET_strdup (service);
1478 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1479 /* Check if this is a local or remote process */
1480 if (NULL != d->hostname)
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1484 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1485 d->cfgfile, d->hostname);
1488 if (d->username != NULL)
1489 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1491 arg = GNUNET_strdup (d->hostname);
1493 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1501 "-c", d->cfgfile, "-k", service, "-q",
1503 GNUNET_TIME_relative_to_string (timeout),
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1506 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1507 arg, "gnunet-arm", d->cfgfile, service);
1513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1514 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1516 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1520 "-c", d->cfgfile, "-k", service, "-q",
1522 GNUNET_TIME_relative_to_string (timeout),
1526 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1527 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1532 * Stops a GNUnet daemon.
1534 * @param d the daemon that should be stopped
1535 * @param timeout how long to wait for process for shutdown to complete
1536 * @param cb function called once the daemon was stopped
1537 * @param cb_cls closure for cb
1538 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1540 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1541 * GNUNET_NO to kill off and clean up for good
1544 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1545 struct GNUNET_TIME_Relative timeout,
1546 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1547 int delete_files, int allow_restart)
1553 d->dead_cb_cls = cb_cls;
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1559 GNUNET_i2s (&d->id));
1561 d->dead = GNUNET_YES;
1565 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1567 /* Free what was left from churning! */
1568 GNUNET_assert (d->cfg != NULL);
1569 GNUNET_CONFIGURATION_destroy (d->cfg);
1570 if (delete_files == GNUNET_YES)
1572 if (0 != UNLINK (d->cfgfile))
1574 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1577 GNUNET_free (d->cfgfile);
1578 GNUNET_free_non_null (d->hostname);
1579 GNUNET_free_non_null (d->username);
1580 if (NULL != d->dead_cb)
1581 d->dead_cb (d->dead_cb_cls, NULL);
1587 if (delete_files == GNUNET_YES)
1589 GNUNET_asprintf (&del_arg, "-d");
1592 if (d->phase == SP_CONFIG_UPDATE)
1594 GNUNET_SCHEDULER_cancel (d->task);
1595 d->phase = SP_START_DONE;
1597 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1598 if (d->server != NULL)
1600 GNUNET_CORE_disconnect (d->server);
1604 /* shutdown ARM process (will terminate others) */
1606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1607 GNUNET_i2s (&d->id));
1609 d->phase = SP_SHUTDOWN_START;
1610 d->running = GNUNET_NO;
1611 if (allow_restart == GNUNET_YES)
1612 d->churn = GNUNET_YES;
1615 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1617 GNUNET_TRANSPORT_disconnect (d->th);
1620 /* Check if this is a local or remote process */
1621 if (NULL != d->hostname)
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1626 d->cfgfile, d->hostname);
1629 if (d->username != NULL)
1630 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1632 arg = GNUNET_strdup (d->hostname);
1634 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1642 "-c", d->cfgfile, "-e", "-q", "-T",
1643 GNUNET_TIME_relative_to_string (timeout),
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1646 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1647 arg, "gnunet-arm", d->cfgfile, del_arg);
1648 /* Use -e to end arm, and -d to remove temp files */
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1655 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1657 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1661 "-c", d->cfgfile, "-e", "-q", "-T",
1662 GNUNET_TIME_relative_to_string (timeout),
1666 GNUNET_free_non_null (del_arg);
1667 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1668 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1673 * Changes the configuration of a GNUnet daemon.
1675 * @param d the daemon that should be modified
1676 * @param cfg the new configuration for the daemon
1677 * @param cb function called once the configuration was changed
1678 * @param cb_cls closure for cb
1681 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1682 struct GNUNET_CONFIGURATION_Handle *cfg,
1683 GNUNET_TESTING_NotifyCompletion cb,
1688 if (d->phase != SP_START_DONE)
1693 ("Peer not yet running, can not change configuration at this point."));
1697 /* 1) write configuration to temporary file */
1698 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1701 cb (cb_cls, _("Failed to write new configuration to disk."));
1705 /* 2) copy file to remote host (if necessary) */
1706 if (NULL == d->hostname)
1708 /* signal success */
1714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1715 "Copying updated configuration file to remote host `%s'.\n",
1718 d->phase = SP_CONFIG_UPDATE;
1719 if (NULL != d->username)
1720 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1722 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1723 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1727 d->cfgfile, arg, NULL);
1729 if (NULL == d->proc)
1731 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1732 _("Could not start `%s' process to copy configuration file.\n"),
1735 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1736 d->phase = SP_START_DONE;
1740 d->update_cb_cls = cb_cls;
1742 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1747 * Data kept for each pair of peers that we try
1750 struct GNUNET_TESTING_ConnectContext
1753 * Testing handle to the first daemon.
1755 struct GNUNET_TESTING_Daemon *d1;
1758 * Handle to core of first daemon (to check connect)
1760 struct GNUNET_CORE_Handle *d1core;
1763 * Have we actually connected to the core of the first daemon yet?
1768 * Testing handle to the second daemon.
1770 struct GNUNET_TESTING_Daemon *d2;
1773 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1775 struct GNUNET_TRANSPORT_Handle *d1th;
1778 * Function to call once we are done (or have timed out).
1780 GNUNET_TESTING_NotifyConnection cb;
1788 * The relative timeout from whence this connect attempt was
1789 * started. Allows for reconnect attempts.
1791 struct GNUNET_TIME_Relative relative_timeout;
1794 * Maximum number of connect attempts, will retry connection
1795 * this number of times on failures.
1797 unsigned int connect_attempts;
1800 * Hello timeout task
1802 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1805 * Connect timeout task
1807 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1810 * When should this operation be complete (or we must trigger
1813 struct GNUNET_TIME_Relative timeout_hello;
1816 * Was the connection attempt successful?
1821 * When connecting, do we need to send the HELLO?
1826 * The distance between the two connected peers
1832 /** Forward declaration **/
1834 reattempt_daemons_connect (void *cls,
1835 const struct GNUNET_SCHEDULER_TaskContext *tc);
1839 * Notify callback about success or failure of the attempt
1840 * to connect the two peers
1842 * @param cls our "struct GNUNET_TESTING_ConnectContext" (freed)
1843 * @param tc reason tells us if we succeeded or failed
1846 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1848 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1850 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1851 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1853 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1854 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1857 if (ctx->d1th != NULL)
1858 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1860 if (ctx->d1core != NULL)
1861 GNUNET_CORE_disconnect (ctx->d1core);
1864 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1870 if (ctx->connected == GNUNET_YES)
1872 if (ctx->cb != NULL)
1874 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
1875 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1878 else if (ctx->connect_attempts > 0)
1880 ctx->d1core_ready = GNUNET_NO;
1882 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1887 if (ctx->cb != NULL)
1889 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1890 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
1898 * Success, connection is up. Signal client our success.
1900 * @param cls our "struct GNUNET_TESTING_ConnectContext"
1901 * @param peer identity of the peer that has connected
1902 * @param atsi performance information
1903 * @param atsi_count number of records in 'atsi'
1907 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
1908 const struct GNUNET_ATS_Information *atsi,
1909 unsigned int atsi_count)
1911 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peer %s to peer %s\n",
1915 ctx->d1->shortname, GNUNET_i2s (peer));
1918 if (0 != memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1920 ctx->connected = GNUNET_YES;
1921 ctx->distance = 0; /* FIXME: distance */
1922 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1924 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1925 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1927 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1928 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
1933 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1935 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1936 struct GNUNET_MessageHeader *hello;
1938 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1939 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1941 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
1942 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
1943 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
1945 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1946 GNUNET_assert (hello != NULL);
1948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
1949 ctx->d2->shortname, ctx->d1->shortname);
1951 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1952 GNUNET_assert (ctx->d1core != NULL);
1954 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1955 "Sending connect request to TRANSPORT of %s for peer %s\n",
1956 GNUNET_i2s (&ctx->d1->id),
1957 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1959 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
1960 ctx->timeout_hello =
1961 GNUNET_TIME_relative_add (ctx->timeout_hello,
1962 GNUNET_TIME_relative_multiply
1963 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1965 ctx->hello_send_task =
1966 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
1970 * Notify of a successful connection to the core service.
1972 * @param cls a ConnectContext
1973 * @param server handle to the core service
1974 * @param my_identity the peer identity of this peer
1977 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
1978 const struct GNUNET_PeerIdentity *my_identity)
1980 struct GNUNET_TESTING_ConnectContext *connect_ctx = cls;
1982 connect_ctx->d1core_ready = GNUNET_YES;
1984 if (connect_ctx->send_hello == GNUNET_NO)
1986 GNUNET_TRANSPORT_try_connect (connect_ctx->d1th, &connect_ctx->d2->id);
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1989 "Sending connect request to TRANSPORT of %s for peer %s\n",
1990 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
1998 * Try to connect again some peers that failed in an earlier attempt. This will
1999 * be tried as many times as connection_attempts in the configuration file.
2001 * @param cls Closure (connection context between the two peers).
2002 * @param tc TaskContext.
2005 reattempt_daemons_connect (void *cls,
2006 const struct GNUNET_SCHEDULER_TaskContext *tc)
2008 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2010 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2011 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
2013 #if DEBUG_TESTING_RECONNECT
2014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2015 "re-attempting connect of peer %s to peer %s\n",
2016 ctx->d1->shortname, ctx->d2->shortname);
2018 ctx->connect_attempts--;
2019 GNUNET_assert (ctx->d1core == NULL);
2020 ctx->d1core_ready = GNUNET_NO;
2022 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2023 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
2024 GNUNET_NO, no_handlers);
2025 if (ctx->d1core == NULL)
2027 if (NULL != ctx->cb)
2028 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2029 ctx->d2->cfg, ctx->d1, ctx->d2,
2030 _("Failed to connect to core service of first peer!\n"));
2035 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
2036 if (NULL != ctx->d2->hello)
2038 #if DEBUG_TESTING_RECONNECT
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "updating %s's HELLO\n",
2040 ctx->d2->shortname);
2042 GNUNET_free (ctx->d2->hello);
2043 ctx->d2->hello = NULL;
2044 if (NULL != ctx->d2->th)
2046 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->ghh);
2047 ctx->d2->ghh = NULL;
2048 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
2051 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2053 GNUNET_assert (ctx->d2->th != NULL);
2055 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2057 #if DEBUG_TESTING_RECONNECT
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "didn't have %s's HELLO\n",
2061 ctx->d2->shortname);
2065 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
2067 #if DEBUG_TESTING_RECONNECT
2068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2069 "didn't have %s's HELLO, trying to get it now\n",
2070 ctx->d2->shortname);
2073 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2075 if (NULL == ctx->d2->th)
2077 GNUNET_CORE_disconnect (ctx->d1core);
2079 if (NULL != ctx->cb)
2080 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2081 ctx->d2->cfg, ctx->d1, ctx->d2,
2082 _("Failed to connect to transport service!\n"));
2086 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2088 #if DEBUG_TESTING_RECONNECT
2091 if (NULL == ctx->d2->hello)
2093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2094 "didn't have %s's HELLO but th wasn't NULL, not trying!!\n",
2095 ctx->d2->shortname);
2100 if (ctx->send_hello == GNUNET_YES)
2102 #if DEBUG_TESTING_RECONNECT
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending %s's HELLO to %s\n",
2104 ctx->d1->shortname, ctx->d2->shortname);
2107 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2109 if (ctx->d1th == NULL)
2111 GNUNET_CORE_disconnect (ctx->d1core);
2113 if (NULL != ctx->cb)
2114 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2115 ctx->d2->cfg, ctx->d1, ctx->d2,
2116 _("Failed to connect to transport service!\n"));
2119 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2120 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2124 #if DEBUG_TESTING_RECONNECT
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to reconnect %s to %s\n",
2126 ctx->d1->shortname, ctx->d2->shortname);
2128 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
2131 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2132 ¬ify_connect_result, ctx);
2136 * Iterator for currently known peers, to ensure
2137 * that we don't try to send duplicate connect
2140 * @param cls our "struct GNUNET_TESTING_ConnectContext"
2141 * @param peer identity of the peer that has connected,
2142 * NULL when iteration has finished
2143 * @param atsi performance information
2144 * @param atsi_count number of records in 'atsi'
2148 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2149 const struct GNUNET_ATS_Information *atsi,
2150 unsigned int atsi_count)
2152 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2154 if ((peer != NULL) &&
2155 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2157 ctx->connected = GNUNET_YES;
2158 ctx->distance = 0; /* FIXME: distance */
2162 return; /* ignore other peers */
2163 /* peer == NULL: End of iteration over peers */
2165 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->timeout_task);
2166 if (ctx->connected == GNUNET_YES)
2168 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2172 /* Peer not already connected, need to schedule connect request! */
2173 if (ctx->d1core == NULL)
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 "Peers are NOT connected, connecting to core!\n");
2180 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2181 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
2182 GNUNET_NO, no_handlers);
2185 if (ctx->d1core == NULL)
2187 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2191 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 */
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 "Don't have d2's HELLO, trying to get it!\n");
2198 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2200 if (ctx->d2->th == NULL)
2202 GNUNET_CORE_disconnect (ctx->d1core);
2205 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2209 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2212 if (ctx->send_hello == GNUNET_YES)
2215 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2217 if (ctx->d1th == NULL)
2219 GNUNET_CORE_disconnect (ctx->d1core);
2222 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2225 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2226 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2230 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2231 ¬ify_connect_result, ctx);
2237 * Establish a connection between two GNUnet daemons. The daemons
2238 * must both be running and not be stopped until either the
2239 * 'cb' callback is called OR the connection request has been
2240 * explicitly cancelled.
2242 * @param d1 handle for the first daemon
2243 * @param d2 handle for the second daemon
2244 * @param timeout how long is the connection attempt
2246 * @param max_connect_attempts how many times should we try to reconnect
2248 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2249 * the HELLO has already been exchanged
2250 * @param cb function to call at the end
2251 * @param cb_cls closure for cb
2252 * @return handle to cancel the request
2254 struct GNUNET_TESTING_ConnectContext *
2255 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2256 struct GNUNET_TESTING_Daemon *d2,
2257 struct GNUNET_TIME_Relative timeout,
2258 unsigned int max_connect_attempts,
2260 GNUNET_TESTING_NotifyConnection cb,
2263 struct GNUNET_TESTING_ConnectContext *ctx;
2265 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2268 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2269 _("Peers are not fully running yet, can not connect!\n"));
2270 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2274 ctx = GNUNET_malloc (sizeof (struct GNUNET_TESTING_ConnectContext));
2277 ctx->timeout_hello =
2278 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2279 ctx->relative_timeout =
2280 GNUNET_TIME_relative_divide (timeout, max_connect_attempts);
2282 ctx->cb_cls = cb_cls;
2283 ctx->connect_attempts = max_connect_attempts;
2284 ctx->connected = GNUNET_NO;
2285 ctx->send_hello = send_hello;
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to connect peer %s to peer %s\n",
2288 d1->shortname, d2->shortname);
2291 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2292 GNUNET_assert (GNUNET_OK ==
2293 GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id,
2294 &core_initial_iteration, ctx));
2300 * Cancel an attempt to connect two daemons.
2302 * @param cc connect context
2305 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext *cc)
2307 if (GNUNET_SCHEDULER_NO_TASK != cc->timeout_task)
2309 GNUNET_SCHEDULER_cancel (cc->timeout_task);
2310 cc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2312 if (GNUNET_SCHEDULER_NO_TASK != cc->hello_send_task)
2314 GNUNET_SCHEDULER_cancel (cc->hello_send_task);
2315 cc->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2317 if (NULL != cc->d1core)
2319 GNUNET_CORE_disconnect (cc->d1core);
2322 if (NULL != cc->d1th)
2324 GNUNET_TRANSPORT_disconnect (cc->d1th);
2331 /* end of testing.c */