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,
69 const struct GNUNET_HELLO_Address *address,
70 struct GNUNET_TIME_Absolute expiration)
80 * Receive the HELLO from one peer, give it to the other
81 * and ask them to connect.
83 * @param cls Closure (daemon whose hello is this).
84 * @param message HELLO message of peer
87 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
89 struct GNUNET_TESTING_Daemon *daemon = cls;
96 GNUNET_assert (message != NULL);
97 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message *) message,
98 GNUNET_NO, &test_address, &empty);
99 if (GNUNET_YES == empty)
101 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
102 "Skipping empty HELLO address of peer %s\n",
103 GNUNET_i2s (&daemon->id));
107 GNUNET_assert (daemon->phase == SP_GET_HELLO ||
108 daemon->phase == SP_START_DONE);
109 daemon->cb = NULL; // FIXME: why??? (see fsm:SP_START_CORE, notify_daemon_started)
110 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
111 GNUNET_SCHEDULER_cancel (daemon->task);
113 if (daemon->server != NULL)
116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
117 "Received `%s' from transport service of `%4s', disconnecting core!\n",
118 "HELLO", GNUNET_i2s (&daemon->id));
120 GNUNET_CORE_disconnect (daemon->server);
121 daemon->server = NULL;
124 msize = ntohs (message->size);
127 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
128 "HELLO message of peer %s is of size 0\n",
129 GNUNET_i2s (&daemon->id));
132 if (daemon->ghh != NULL)
134 GNUNET_TRANSPORT_get_hello_cancel (daemon->ghh);
138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
139 "Received `%s' from transport service of `%4s'\n", "HELLO",
140 GNUNET_i2s (&daemon->id));
143 GNUNET_free_non_null (daemon->hello);
144 daemon->hello = GNUNET_malloc (msize);
145 memcpy (daemon->hello, message, msize);
147 if (daemon->th != NULL)
149 GNUNET_TRANSPORT_disconnect (daemon->th);
152 daemon->phase = SP_START_DONE;
157 * Notify of a peer being up and running. Scheduled as a task
158 * so that variables which may need to be set are set before
159 * the connect callback can set up new operations.
160 * FIXME: what variables?????? where from????
162 * @param cls the testing daemon
163 * @param tc task scheduler context
166 notify_daemon_started (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
168 struct GNUNET_TESTING_Daemon *d = cls;
169 GNUNET_TESTING_NotifyDaemonRunning cb;
174 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
179 * Finite-state machine for starting GNUnet.
181 * @param cls our "struct GNUNET_TESTING_Daemon"
185 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
187 struct GNUNET_TESTING_Daemon *d = cls;
188 GNUNET_TESTING_NotifyDaemonRunning cb;
189 enum GNUNET_OS_ProcessStatusType type;
195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer %s FSM is in phase %u.\n",
196 GNUNET_i2s (&d->id), d->phase);
199 d->task = GNUNET_SCHEDULER_NO_TASK;
203 /* confirm copying complete */
204 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
206 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
211 cb (d->cb_cls, NULL, d->cfg, d,
213 ("`scp' does not seem to terminate (timeout copying config).\n"));
218 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
222 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
227 cb (d->cb_cls, NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
230 GNUNET_OS_process_close (d->proc);
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Successfully copied configuration file.\n");
235 d->phase = SP_COPIED;
238 /* Start create hostkey process if we don't already know the peer identity! */
239 if (GNUNET_NO == d->have_hostkey)
241 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
242 if (d->pipe_stdout == NULL)
247 cb (d->cb_cls, NULL, d->cfg, d,
250 _("Failed to create pipe for `gnunet-peerinfo' process.\n") :
251 _("Failed to create pipe for `ssh' process.\n"));
254 if (NULL == d->hostname)
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258 "Starting `%s', with command `%s %s %s %s'.\n",
259 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
263 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
264 "gnunet-peerinfo", "-c", d->cfgfile, "-sq",
266 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
270 if (d->username != NULL)
271 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
273 dst = GNUNET_strdup (d->hostname);
276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
277 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
278 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
281 if (d->ssh_port_str == NULL)
283 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh",
287 dst, "gnunet-peerinfo", "-c",
288 d->cfgfile, "-sq", NULL);
293 GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh", "ssh", "-p",
298 dst, "gnunet-peerinfo", "-c", d->cfgfile,
301 GNUNET_DISK_pipe_close_end (d->pipe_stdout, GNUNET_DISK_PIPE_END_WRITE);
306 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
307 _("Could not start `%s' process to create hostkey.\n"),
308 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
312 cb (d->cb_cls, NULL, d->cfg, d,
314 d->hostname) ? _("Failed to start `gnunet-peerinfo' process.\n")
315 : _("Failed to start `ssh' process.\n"));
316 GNUNET_DISK_pipe_close (d->pipe_stdout);
320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
321 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
323 d->phase = SP_HOSTKEY_CREATE;
325 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
327 GNUNET_DISK_pipe_handle
329 GNUNET_DISK_PIPE_END_READ),
332 else /* Already have a hostkey! */
334 if (d->hostkey_callback != NULL)
336 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
337 d->hostkey_callback = NULL;
338 d->phase = SP_HOSTKEY_CREATED;
341 d->phase = SP_TOPOLOGY_SETUP;
344 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
347 case SP_HOSTKEY_CREATE:
349 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
350 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
351 &d->hostkeybuf[d->hostkeybufpos],
352 sizeof (d->hostkeybuf) - d->hostkeybufpos);
354 d->hostkeybufpos += bytes_read;
356 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
360 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
362 GNUNET_DISK_pipe_handle
364 GNUNET_DISK_PIPE_END_READ),
368 d->hostkeybuf[103] = '\0';
370 if ((bytes_read < 0) ||
372 GNUNET_CRYPTO_hash_from_string (d->hostkeybuf, &d->id.hashPubKey)))
376 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
377 _("Error reading from gnunet-peerinfo: %s\n"),
380 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
381 _("Malformed output from gnunet-peerinfo!\n"));
384 GNUNET_DISK_pipe_close (d->pipe_stdout);
385 d->pipe_stdout = NULL;
386 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
387 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
388 GNUNET_OS_process_close (d->proc);
391 cb (d->cb_cls, NULL, d->cfg, d, _("Failed to get hostkey!\n"));
394 d->shortname = GNUNET_strdup (GNUNET_i2s (&d->id));
395 GNUNET_DISK_pipe_close (d->pipe_stdout);
396 d->pipe_stdout = NULL;
397 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
398 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
399 GNUNET_OS_process_close (d->proc);
401 d->have_hostkey = GNUNET_YES;
402 if (d->hostkey_callback != NULL)
404 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
405 d->hostkey_callback = NULL;
406 d->phase = SP_HOSTKEY_CREATED;
410 d->phase = SP_TOPOLOGY_SETUP;
413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
416 case SP_HOSTKEY_CREATED:
417 /* wait for topology finished */
418 if ((GNUNET_YES == d->dead) ||
419 (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0))
424 cb (d->cb_cls, NULL, d->cfg, d,
425 _("`Failed while waiting for topology setup!\n"));
430 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
433 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
434 /* start GNUnet on remote host */
435 if (NULL == d->hostname)
438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
439 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
440 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile, "-L", "DEBUG",
444 GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm", "-c",
450 GNUNET_TIME_relative_to_string
451 (GNUNET_TIME_absolute_get_remaining
452 (d->max_timeout)), NULL);
456 if (d->username != NULL)
457 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
459 dst = GNUNET_strdup (d->hostname);
462 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
463 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
464 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
465 "-L", "DEBUG", "-s", "-q");
467 if (d->ssh_port_str == NULL)
469 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
477 "-c", d->cfgfile, "-s", "-q", "-T",
478 GNUNET_TIME_relative_to_string
479 (GNUNET_TIME_absolute_get_remaining
480 (d->max_timeout)), NULL);
486 GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh", "-p",
495 "-c", d->cfgfile, "-s", "-q", "-T",
496 GNUNET_TIME_relative_to_string
497 (GNUNET_TIME_absolute_get_remaining
498 (d->max_timeout)), NULL);
504 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
505 _("Could not start `%s' process to start GNUnet.\n"),
506 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
510 cb (d->cb_cls, NULL, d->cfg, d,
512 d->hostname) ? _("Failed to start `gnunet-arm' process.\n") :
513 _("Failed to start `ssh' process.\n"));
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "Started `%s', waiting for `%s' to be up.\n", "gnunet-arm",
519 "gnunet-service-core");
521 d->phase = SP_START_ARMING;
523 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
526 case SP_START_ARMING:
527 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
529 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
534 cb (d->cb_cls, NULL, d->cfg, d,
536 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
537 _("`ssh' does not seem to terminate.\n"));
538 GNUNET_CONFIGURATION_destroy (d->cfg);
539 GNUNET_free (d->cfgfile);
540 GNUNET_free_non_null (d->hostname);
541 GNUNET_free_non_null (d->username);
542 GNUNET_free (d->proc);
543 // GNUNET_free (d); // FIXME (could this leak)
544 d->hostname = NULL; // Quick hack to avoid crashing (testing need to be
545 d->cfg = NULL; // overhauled anyway, and the error managing is
546 // pretty broken anyway.
551 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully started `%s'.\n",
559 GNUNET_free (d->proc);
560 d->phase = SP_START_CORE;
562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Calling CORE_connect\n");
566 if (d->server != NULL)
567 GNUNET_CORE_disconnect (d->server);
569 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
572 if (GNUNET_YES == d->dead)
573 GNUNET_TESTING_daemon_stop (d,
574 GNUNET_TIME_absolute_get_remaining
575 (d->max_timeout), d->dead_cb,
576 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
577 else if (NULL != d->cb)
578 d->cb (d->cb_cls, &d->id, d->cfg, d,
579 _("Failed to connect to transport service!\n"));
583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
584 "Connected to transport service `%s', getting HELLO\n",
585 GNUNET_i2s (&d->id));
588 d->ghh = GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
589 /* FIXME: store task ID somewhere! */
590 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
594 * cb (d->cb_cls, &d->id, d->cfg, d, NULL); */
595 d->running = GNUNET_YES;
596 d->phase = SP_GET_HELLO;
599 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
601 if (d->server != NULL)
602 GNUNET_CORE_disconnect (d->server);
604 GNUNET_TRANSPORT_disconnect (d->th);
608 cb (d->cb_cls, NULL, d->cfg, d, _("Unable to get HELLO for peer!\n"));
609 GNUNET_CONFIGURATION_destroy (d->cfg);
610 GNUNET_free (d->cfgfile);
611 GNUNET_free_non_null (d->hostname);
612 GNUNET_free_non_null (d->username);
616 if (d->hello != NULL)
618 GNUNET_assert (d->task == GNUNET_SCHEDULER_NO_TASK);
620 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply
621 (GNUNET_CONSTANTS_SERVICE_RETRY, 2),
627 case SP_SERVICE_START:
628 /* confirm gnunet-arm exited */
629 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
631 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
636 cb (d->cb_cls, NULL, d->cfg, d,
638 d->hostname) ? _("`gnunet-arm' does not seem to terminate.\n") :
639 _("`ssh' does not seem to terminate.\n"));
644 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
649 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
654 cb (d->cb_cls, NULL, d->cfg, d,
658 ("`gnunet-arm' terminated with non-zero exit status (or timed out)!\n")
659 : _("`ssh' does not seem to terminate.\n"));
664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service startup complete!\n");
668 d->phase = SP_START_DONE;
670 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
672 case SP_SERVICE_SHUTDOWN_START:
673 /* confirm copying complete */
674 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
676 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
678 if (NULL != d->dead_cb)
679 d->dead_cb (d->dead_cb_cls,
681 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
686 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
691 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
693 if (NULL != d->dead_cb)
694 d->dead_cb (d->dead_cb_cls,
696 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Service shutdown complete.\n");
703 if (NULL != d->dead_cb)
704 d->dead_cb (d->dead_cb_cls, NULL);
706 case SP_SHUTDOWN_START:
707 /* confirm copying complete */
708 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
710 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0)
712 if (NULL != d->dead_cb)
713 d->dead_cb (d->dead_cb_cls,
715 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
718 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
720 GNUNET_TRANSPORT_disconnect (d->th);
723 GNUNET_CONFIGURATION_destroy (d->cfg);
724 GNUNET_free (d->cfgfile);
725 GNUNET_free_non_null (d->hello);
726 GNUNET_free_non_null (d->hostname);
727 GNUNET_free_non_null (d->username);
728 GNUNET_free_non_null (d->shortname);
729 GNUNET_free_non_null (d->proc);
736 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
740 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
742 if (NULL != d->dead_cb)
743 d->dead_cb (d->dead_cb_cls,
745 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
748 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
750 GNUNET_TRANSPORT_disconnect (d->th);
753 if (d->server != NULL)
755 GNUNET_CORE_disconnect (d->server);
758 GNUNET_CONFIGURATION_destroy (d->cfg);
759 GNUNET_free (d->cfgfile);
760 GNUNET_free_non_null (d->hello);
761 GNUNET_free_non_null (d->hostname);
762 GNUNET_free_non_null (d->username);
763 GNUNET_free_non_null (d->shortname);
764 GNUNET_free_non_null (d->proc);
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
772 if (d->server != NULL)
774 GNUNET_CORE_disconnect (d->server);
780 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
782 GNUNET_TRANSPORT_disconnect (d->th);
786 if (NULL != d->dead_cb)
787 d->dead_cb (d->dead_cb_cls, NULL);
789 /* state clean up and notifications */
790 if (d->churn == GNUNET_NO)
792 GNUNET_CONFIGURATION_destroy (d->cfg);
793 GNUNET_free (d->cfgfile);
794 GNUNET_free_non_null (d->hostname);
795 GNUNET_free_non_null (d->username);
798 GNUNET_free_non_null (d->hello);
800 GNUNET_free_non_null (d->shortname);
801 GNUNET_free_non_null (d->proc);
804 if (d->churn == GNUNET_NO)
808 case SP_CONFIG_UPDATE:
809 /* confirm copying complete */
810 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
812 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
817 cb (d->cb_cls, NULL, d->cfg, d,
818 _("`scp' does not seem to terminate.\n"));
823 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
827 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
829 if (NULL != d->update_cb)
830 d->update_cb (d->update_cb_cls, _("`scp' did not complete cleanly.\n"));
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "Successfully copied configuration file.\n");
837 if (NULL != d->update_cb)
838 d->update_cb (d->update_cb_cls, NULL);
839 d->phase = SP_START_DONE;
845 * Continues GNUnet daemon startup when user wanted to be notified
846 * once a hostkey was generated (for creating friends files, blacklists,
849 * @param daemon the daemon to finish starting
852 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
854 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
855 daemon->phase = SP_TOPOLOGY_SETUP;
859 * Check whether the given daemon is running.
861 * @param daemon the daemon to check
863 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
864 * daemon is down, GNUNET_SYSERR on error.
867 GNUNET_TESTING_test_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
870 return GNUNET_SYSERR;
872 if (daemon->running == GNUNET_YES)
879 * Starts a GNUnet daemon service which has been previously stopped.
881 * @param d the daemon for which the service should be started
882 * @param service the name of the service to start
883 * @param timeout how long to wait for process for shutdown to complete
884 * @param cb function called once the service starts
885 * @param cb_cls closure for cb
888 GNUNET_TESTING_daemon_start_stopped_service (struct GNUNET_TESTING_Daemon *d,
890 struct GNUNET_TIME_Relative
892 GNUNET_TESTING_NotifyDaemonRunning
900 GNUNET_assert (d->running == GNUNET_YES);
902 if (d->phase == SP_CONFIG_UPDATE)
904 GNUNET_SCHEDULER_cancel (d->task);
905 d->phase = SP_START_DONE;
908 if (d->churned_services == NULL)
910 d->cb (d->cb_cls, &d->id, d->cfg, d,
911 "No service has been churned off yet!!");
914 d->phase = SP_SERVICE_START;
915 GNUNET_free (d->churned_services);
916 d->churned_services = NULL;
917 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
918 /* Check if this is a local or remote process */
919 if (NULL != d->hostname)
922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
923 "Starting gnunet-arm with config `%s' on host `%s'.\n",
924 d->cfgfile, d->hostname);
927 if (d->username != NULL)
928 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
930 arg = GNUNET_strdup (d->hostname);
932 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
940 "-c", d->cfgfile, "-i", service, "-q",
942 GNUNET_TIME_relative_to_string (timeout),
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q\n",
946 arg, "gnunet-arm", d->cfgfile, service);
952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
953 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
955 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
959 "-c", d->cfgfile, "-i", service, "-q",
961 GNUNET_TIME_relative_to_string (timeout),
965 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
966 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
970 * Starts a GNUnet daemon's service.
972 * @param d the daemon for which the service should be started
973 * @param service the name of the service to start
974 * @param timeout how long to wait for process for startup
975 * @param cb function called once gnunet-arm returns
976 * @param cb_cls closure for cb
979 GNUNET_TESTING_daemon_start_service (struct GNUNET_TESTING_Daemon *d,
981 struct GNUNET_TIME_Relative timeout,
982 GNUNET_TESTING_NotifyDaemonRunning cb,
990 GNUNET_assert (service != NULL);
991 GNUNET_assert (d->running == GNUNET_YES);
992 GNUNET_assert (d->phase == SP_START_DONE);
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 _("Starting service %s for peer `%4s'\n"), service,
997 GNUNET_i2s (&d->id));
1000 d->phase = SP_SERVICE_START;
1001 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1002 /* Check if this is a local or remote process */
1003 if (NULL != d->hostname)
1006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1007 "Starting gnunet-arm with config `%s' on host `%s'.\n",
1008 d->cfgfile, d->hostname);
1011 if (d->username != NULL)
1012 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1014 arg = GNUNET_strdup (d->hostname);
1016 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1024 "-c", d->cfgfile, "-i", service, "-q",
1026 GNUNET_TIME_relative_to_string (timeout),
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1029 "Starting gnunet-arm with command ssh %s gnunet-arm -c %s -i %s -q -T %s\n",
1030 arg, "gnunet-arm", d->cfgfile, service,
1031 GNUNET_TIME_relative_to_string (timeout));
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1038 "Starting gnunet-arm with config `%s' locally.\n", d->cfgfile);
1040 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1044 "-c", d->cfgfile, "-i", service, "-q",
1046 GNUNET_TIME_relative_to_string (timeout),
1048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1049 "Starting gnunet-arm with command %s -c %s -i %s -q -T %s\n",
1050 "gnunet-arm", d->cfgfile, service,
1051 GNUNET_TIME_relative_to_string (timeout));
1054 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1055 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1059 * Start a peer that has previously been stopped using the daemon_stop
1060 * call (and files weren't deleted and the allow restart flag)
1062 * @param daemon the daemon to start (has been previously stopped)
1063 * @param timeout how long to wait for restart
1064 * @param cb the callback for notification when the peer is running
1065 * @param cb_cls closure for the callback
1068 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
1069 struct GNUNET_TIME_Relative timeout,
1070 GNUNET_TESTING_NotifyDaemonRunning cb,
1073 if (daemon->running == GNUNET_YES)
1075 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
1076 "Daemon already running, can't restart!");
1081 daemon->cb_cls = cb_cls;
1082 daemon->phase = SP_TOPOLOGY_SETUP;
1083 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1084 /* FIXME: why add_continuation? */
1085 GNUNET_SCHEDULER_add_continuation (&start_fsm, daemon,
1086 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1090 * Starts a GNUnet daemon. GNUnet must be installed on the target
1091 * system and available in the PATH. The machine must furthermore be
1092 * reachable via "ssh" (unless the hostname is "NULL") without the
1093 * need to enter a password.
1095 * @param cfg configuration to use
1096 * @param timeout how long to wait starting up peers
1097 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1098 * to really start the peer (default)
1099 * @param hostname name of the machine where to run GNUnet
1100 * (use NULL for localhost).
1101 * @param ssh_username ssh username to use when connecting to hostname
1102 * @param sshport port to pass to ssh process when connecting to hostname
1103 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1104 * @param hostkey_callback function to call once the hostkey has been
1105 * generated for this peer, but it hasn't yet been started
1106 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1107 * @param hostkey_cls closure for hostkey callback
1108 * @param cb function to call once peer is up, or failed to start
1109 * @param cb_cls closure for cb
1110 * @return handle to the daemon (actual start will be completed asynchronously)
1112 struct GNUNET_TESTING_Daemon *
1113 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1114 struct GNUNET_TIME_Relative timeout, int pretend,
1115 const char *hostname, const char *ssh_username,
1116 uint16_t sshport, const char *hostkey,
1117 GNUNET_TESTING_NotifyHostkeyCreated
1118 hostkey_callback, void *hostkey_cls,
1119 GNUNET_TESTING_NotifyDaemonRunning cb,
1122 struct GNUNET_TESTING_Daemon *ret;
1126 char *baseservicehome;
1129 char *temp_file_name;
1130 struct GNUNET_DISK_FileHandle *fn;
1131 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1132 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1134 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1135 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1138 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1141 ret->ssh_port_str = NULL;
1143 /* Find service home and base service home directories, create it if it doesn't exist */
1144 GNUNET_assert (GNUNET_OK ==
1145 GNUNET_CONFIGURATION_get_value_string (cfg, "PATHS",
1149 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1150 GNUNET_asprintf (&temp_file_name, "%s/gnunet-testing-config", servicehome);
1151 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1152 GNUNET_free (temp_file_name);
1154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1155 "Setting up peer with configuration file `%s'.\n", ret->cfgfile);
1157 if (NULL == ret->cfgfile)
1159 GNUNET_free_non_null (ret->ssh_port_str);
1160 GNUNET_free_non_null (ret->hostname);
1164 ret->hostkey_callback = hostkey_callback;
1165 ret->hostkey_cls = hostkey_cls;
1167 ret->cb_cls = cb_cls;
1168 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1169 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1170 GNUNET_CONFIGURATION_set_value_string (ret->cfg, "PATHS", "DEFAULTCONFIG",
1173 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1175 private_key = GNUNET_CRYPTO_rsa_decode_key (hostkey, HOSTKEYFILESIZE);
1176 GNUNET_assert (private_key != NULL);
1177 GNUNET_CRYPTO_rsa_key_get_public (private_key, &public_key);
1178 GNUNET_CRYPTO_hash (&public_key,
1179 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1180 &ret->id.hashPubKey);
1181 ret->shortname = GNUNET_strdup (GNUNET_i2s (&ret->id));
1182 ret->have_hostkey = GNUNET_YES;
1183 GNUNET_CRYPTO_rsa_key_free (private_key);
1186 /* Write hostkey to file, if we were given one */
1188 if (hostkey != NULL)
1190 GNUNET_asprintf (&hostkeyfile, "%s/.hostkey", servicehome);
1191 fn = GNUNET_DISK_file_open (hostkeyfile,
1192 GNUNET_DISK_OPEN_READWRITE |
1193 GNUNET_DISK_OPEN_CREATE,
1194 GNUNET_DISK_PERM_USER_READ |
1195 GNUNET_DISK_PERM_USER_WRITE);
1196 GNUNET_assert (fn != NULL);
1197 GNUNET_assert (HOSTKEYFILESIZE ==
1198 GNUNET_DISK_file_write (fn, hostkey, HOSTKEYFILESIZE));
1199 GNUNET_assert (GNUNET_OK == GNUNET_DISK_file_close (fn));
1202 /* write configuration to temporary file */
1203 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1205 if (0 != UNLINK (ret->cfgfile))
1206 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1208 GNUNET_CONFIGURATION_destroy (ret->cfg);
1209 GNUNET_free_non_null (ret->hostname);
1210 GNUNET_free (ret->cfgfile);
1214 if (ssh_username != NULL)
1215 username = GNUNET_strdup (ssh_username);
1216 if ((ssh_username == NULL) &&
1218 GNUNET_CONFIGURATION_get_value_string (cfg, "TESTING", "USERNAME",
1221 if (NULL != getenv ("USER"))
1222 username = GNUNET_strdup (getenv ("USER"));
1226 ret->username = username;
1228 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1230 /* copy directory to remote host */
1231 if (NULL != hostname)
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Copying configuration directory to host `%s'.\n", hostname);
1237 baseservicehome = GNUNET_strdup (servicehome);
1238 /* Remove trailing /'s */
1239 while (baseservicehome[strlen (baseservicehome) - 1] == '/')
1240 baseservicehome[strlen (baseservicehome) - 1] = '\0';
1241 /* Find next directory /, jump one ahead */
1242 slash = strrchr (baseservicehome, '/');
1246 ret->phase = SP_COPYING;
1247 if (NULL != username)
1248 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1250 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1251 GNUNET_free (baseservicehome);
1252 if (ret->ssh_port_str == NULL)
1254 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1258 servicehome, arg, NULL);
1260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1261 "copying directory with command scp -r %s %s\n",
1268 GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r", "-P",
1273 servicehome, arg, NULL);
1276 if (NULL == ret->proc)
1278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1280 ("Could not start `%s' process to copy configuration directory.\n"),
1282 if (0 != UNLINK (ret->cfgfile))
1283 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1285 GNUNET_CONFIGURATION_destroy (ret->cfg);
1286 GNUNET_free_non_null (ret->hostname);
1287 GNUNET_free_non_null (ret->username);
1288 GNUNET_free (ret->cfgfile);
1290 if ((hostkey != NULL) && (0 != UNLINK (hostkeyfile)))
1291 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING, "unlink",
1293 GNUNET_free_non_null (hostkeyfile);
1294 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1295 GNUNET_free (servicehome);
1300 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm,
1302 GNUNET_free_non_null (hostkeyfile);
1303 GNUNET_free (servicehome);
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "No need to copy configuration file since we are running locally.\n");
1310 ret->phase = SP_COPIED;
1311 /* FIXME: why add_cont? */
1312 GNUNET_SCHEDULER_add_continuation (&start_fsm, ret,
1313 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1315 GNUNET_free_non_null (hostkeyfile);
1316 GNUNET_free (servicehome);
1322 * Restart (stop and start) a GNUnet daemon.
1324 * @param d the daemon that should be restarted
1325 * @param cb function called once the daemon is (re)started
1326 * @param cb_cls closure for cb
1329 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1330 GNUNET_TESTING_NotifyDaemonRunning cb,
1339 d->dead = GNUNET_YES;
1346 if (d->phase == SP_CONFIG_UPDATE)
1348 GNUNET_SCHEDULER_cancel (d->task);
1349 d->phase = SP_START_DONE;
1351 if (d->server != NULL)
1353 GNUNET_CORE_disconnect (d->server);
1359 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1361 GNUNET_TRANSPORT_disconnect (d->th);
1364 /* state clean up and notifications */
1365 GNUNET_free_non_null (d->hello);
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1369 GNUNET_i2s (&d->id));
1372 d->phase = SP_START_ARMING;
1374 /* Check if this is a local or remote process */
1375 if (NULL != d->hostname)
1378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1379 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1380 d->cfgfile, d->hostname);
1383 if (d->username != NULL)
1384 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1386 arg = GNUNET_strdup (d->hostname);
1388 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1396 "-c", d->cfgfile, "-e", "-r", NULL);
1397 /* Use -r to restart arm and all services */
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1407 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1411 "-c", d->cfgfile, "-e", "-r", NULL);
1414 GNUNET_free_non_null (del_arg);
1416 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1422 * Stops a GNUnet daemon.
1424 * @param d the daemon that should be stopped
1425 * @param service the name of the service to stop
1426 * @param timeout how long to wait for process for shutdown to complete
1427 * @param cb function called once the daemon was stopped
1428 * @param cb_cls closure for cb
1431 GNUNET_TESTING_daemon_stop_service (struct GNUNET_TESTING_Daemon *d,
1432 const char *service,
1433 struct GNUNET_TIME_Relative timeout,
1434 GNUNET_TESTING_NotifyCompletion cb,
1440 d->dead_cb_cls = cb_cls;
1442 GNUNET_assert (d->running == GNUNET_YES);
1444 if (d->phase == SP_CONFIG_UPDATE)
1446 GNUNET_SCHEDULER_cancel (d->task);
1447 d->phase = SP_START_DONE;
1451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1452 GNUNET_i2s (&d->id));
1454 if (d->churned_services != NULL)
1456 d->dead_cb (d->dead_cb_cls, "A service has already been turned off!!");
1459 d->phase = SP_SERVICE_SHUTDOWN_START;
1460 d->churned_services = GNUNET_strdup (service);
1461 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1462 /* Check if this is a local or remote process */
1463 if (NULL != d->hostname)
1466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1467 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1468 d->cfgfile, d->hostname);
1471 if (d->username != NULL)
1472 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1474 arg = GNUNET_strdup (d->hostname);
1476 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1484 "-c", d->cfgfile, "-k", service, "-q",
1486 GNUNET_TIME_relative_to_string (timeout),
1488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1489 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -k %s -q\n",
1490 arg, "gnunet-arm", d->cfgfile, service);
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1499 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1503 "-c", d->cfgfile, "-k", service, "-q",
1505 GNUNET_TIME_relative_to_string (timeout),
1509 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1510 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1515 * Stops a GNUnet daemon.
1517 * @param d the daemon that should be stopped
1518 * @param timeout how long to wait for process for shutdown to complete
1519 * @param cb function called once the daemon was stopped
1520 * @param cb_cls closure for cb
1521 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1523 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1524 * GNUNET_NO to kill off and clean up for good
1527 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1528 struct GNUNET_TIME_Relative timeout,
1529 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1530 int delete_files, int allow_restart)
1536 d->dead_cb_cls = cb_cls;
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Setting d->dead on peer `%4s'\n"),
1542 GNUNET_i2s (&d->id));
1544 d->dead = GNUNET_YES;
1548 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1550 /* Free what was left from churning! */
1551 GNUNET_assert (d->cfg != NULL);
1552 GNUNET_CONFIGURATION_destroy (d->cfg);
1553 if (delete_files == GNUNET_YES)
1555 if (0 != UNLINK (d->cfgfile))
1557 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1560 GNUNET_free (d->cfgfile);
1561 GNUNET_free_non_null (d->hostname);
1562 GNUNET_free_non_null (d->username);
1563 if (NULL != d->dead_cb)
1564 d->dead_cb (d->dead_cb_cls, NULL);
1570 if (delete_files == GNUNET_YES)
1572 GNUNET_asprintf (&del_arg, "-d");
1575 if (d->phase == SP_CONFIG_UPDATE)
1577 GNUNET_SCHEDULER_cancel (d->task);
1578 d->phase = SP_START_DONE;
1580 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1581 if (d->server != NULL)
1583 GNUNET_CORE_disconnect (d->server);
1587 /* shutdown ARM process (will terminate others) */
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, _("Terminating peer `%4s'\n"),
1590 GNUNET_i2s (&d->id));
1592 d->phase = SP_SHUTDOWN_START;
1593 d->running = GNUNET_NO;
1594 if (allow_restart == GNUNET_YES)
1595 d->churn = GNUNET_YES;
1598 GNUNET_TRANSPORT_get_hello_cancel (d->ghh);
1600 GNUNET_TRANSPORT_disconnect (d->th);
1603 /* Check if this is a local or remote process */
1604 if (NULL != d->hostname)
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1609 d->cfgfile, d->hostname);
1612 if (d->username != NULL)
1613 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1615 arg = GNUNET_strdup (d->hostname);
1617 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1625 "-c", d->cfgfile, "-e", "-q", "-T",
1626 GNUNET_TIME_relative_to_string (timeout),
1628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1629 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1630 arg, "gnunet-arm", d->cfgfile, del_arg);
1631 /* Use -e to end arm, and -d to remove temp files */
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1638 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
1640 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm", "gnunet-arm",
1644 "-c", d->cfgfile, "-e", "-q", "-T",
1645 GNUNET_TIME_relative_to_string (timeout),
1649 GNUNET_free_non_null (del_arg);
1650 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1651 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1656 * Changes the configuration of a GNUnet daemon.
1658 * @param d the daemon that should be modified
1659 * @param cfg the new configuration for the daemon
1660 * @param cb function called once the configuration was changed
1661 * @param cb_cls closure for cb
1664 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1665 struct GNUNET_CONFIGURATION_Handle *cfg,
1666 GNUNET_TESTING_NotifyCompletion cb,
1671 if (d->phase != SP_START_DONE)
1676 ("Peer not yet running, can not change configuration at this point."));
1680 /* 1) write configuration to temporary file */
1681 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1684 cb (cb_cls, _("Failed to write new configuration to disk."));
1688 /* 2) copy file to remote host (if necessary) */
1689 if (NULL == d->hostname)
1691 /* signal success */
1697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1698 "Copying updated configuration file to remote host `%s'.\n",
1701 d->phase = SP_CONFIG_UPDATE;
1702 if (NULL != d->username)
1703 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1705 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1706 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1710 d->cfgfile, arg, NULL);
1712 if (NULL == d->proc)
1714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1715 _("Could not start `%s' process to copy configuration file.\n"),
1718 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1719 d->phase = SP_START_DONE;
1723 d->update_cb_cls = cb_cls;
1725 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT, &start_fsm, d);
1730 * Data kept for each pair of peers that we try
1733 struct GNUNET_TESTING_ConnectContext
1736 * Testing handle to the first daemon.
1738 struct GNUNET_TESTING_Daemon *d1;
1741 * Handle to core of first daemon (to check connect)
1743 struct GNUNET_CORE_Handle *d1core;
1746 * Have we actually connected to the core of the first daemon yet?
1751 * Testing handle to the second daemon.
1753 struct GNUNET_TESTING_Daemon *d2;
1756 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1758 struct GNUNET_TRANSPORT_Handle *d1th;
1761 * Function to call once we are done (or have timed out).
1763 GNUNET_TESTING_NotifyConnection cb;
1771 * The relative timeout from whence this connect attempt was
1772 * started. Allows for reconnect attempts.
1774 struct GNUNET_TIME_Relative relative_timeout;
1777 * Maximum number of connect attempts, will retry connection
1778 * this number of times on failures.
1780 unsigned int connect_attempts;
1783 * Hello timeout task
1785 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1788 * Connect timeout task
1790 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1793 * When should this operation be complete (or we must trigger
1796 struct GNUNET_TIME_Relative timeout_hello;
1799 * Was the connection attempt successful?
1804 * When connecting, do we need to send the HELLO?
1809 * The distance between the two connected peers
1815 /** Forward declaration **/
1817 reattempt_daemons_connect (void *cls,
1818 const struct GNUNET_SCHEDULER_TaskContext *tc);
1822 * Notify callback about success or failure of the attempt
1823 * to connect the two peers
1825 * @param cls our "struct GNUNET_TESTING_ConnectContext" (freed)
1826 * @param tc reason tells us if we succeeded or failed
1829 notify_connect_result (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1831 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1833 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1834 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1836 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1837 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1840 if (ctx->d1th != NULL)
1841 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1843 if (ctx->d1core != NULL)
1844 GNUNET_CORE_disconnect (ctx->d1core);
1847 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1853 if (ctx->connected == GNUNET_YES)
1855 if (ctx->cb != NULL)
1857 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->distance,
1858 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1861 else if (ctx->connect_attempts > 0)
1863 ctx->d1core_ready = GNUNET_NO;
1865 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1870 if (ctx->cb != NULL)
1872 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1873 ctx->d2->cfg, ctx->d1, ctx->d2, _("Peers failed to connect"));
1881 * Success, connection is up. Signal client our success.
1883 * @param cls our "struct GNUNET_TESTING_ConnectContext"
1884 * @param peer identity of the peer that has connected
1885 * @param atsi performance information
1886 * @param atsi_count number of records in 'atsi'
1890 connect_notify (void *cls, const struct GNUNET_PeerIdentity *peer,
1891 const struct GNUNET_ATS_Information *atsi,
1892 unsigned int atsi_count)
1894 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Connected peer %s to peer %s\n",
1898 ctx->d1->shortname, GNUNET_i2s (peer));
1901 if (0 != memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1903 ctx->connected = GNUNET_YES;
1904 ctx->distance = 0; /* FIXME: distance */
1905 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1907 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1908 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1910 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1911 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
1916 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1918 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1919 struct GNUNET_MessageHeader *hello;
1921 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1922 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1924 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL) &&
1925 (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)) &&
1926 (ctx->d1->phase == SP_START_DONE) && (ctx->d2->phase == SP_START_DONE))
1928 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1929 GNUNET_assert (hello != NULL);
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n",
1932 ctx->d2->shortname, ctx->d1->shortname);
1934 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1935 GNUNET_assert (ctx->d1core != NULL);
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1938 "Sending connect request to TRANSPORT of %s for peer %s\n",
1939 GNUNET_i2s (&ctx->d1->id),
1940 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1942 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
1943 ctx->timeout_hello =
1944 GNUNET_TIME_relative_add (ctx->timeout_hello,
1945 GNUNET_TIME_relative_multiply
1946 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1948 ctx->hello_send_task =
1949 GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello, &send_hello, ctx);
1953 * Notify of a successful connection to the core service.
1955 * @param cls a ConnectContext
1956 * @param server handle to the core service
1957 * @param my_identity the peer identity of this peer
1960 core_init_notify (void *cls, struct GNUNET_CORE_Handle *server,
1961 const struct GNUNET_PeerIdentity *my_identity)
1963 struct GNUNET_TESTING_ConnectContext *connect_ctx = cls;
1965 connect_ctx->d1core_ready = GNUNET_YES;
1967 if (connect_ctx->send_hello == GNUNET_NO)
1969 GNUNET_TRANSPORT_try_connect (connect_ctx->d1th, &connect_ctx->d2->id);
1971 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1972 "Sending connect request to TRANSPORT of %s for peer %s\n",
1973 connect_ctx->d1->shortname, connect_ctx->d2->shortname);
1981 * Try to connect again some peers that failed in an earlier attempt. This will
1982 * be tried as many times as connection_attempts in the configuration file.
1984 * @param cls Closure (connection context between the two peers).
1985 * @param tc TaskContext.
1988 reattempt_daemons_connect (void *cls,
1989 const struct GNUNET_SCHEDULER_TaskContext *tc)
1991 struct GNUNET_TESTING_ConnectContext *ctx = cls;
1993 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1994 if ((tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1996 #if DEBUG_TESTING_RECONNECT
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1998 "re-attempting connect of peer %s to peer %s\n",
1999 ctx->d1->shortname, ctx->d2->shortname);
2001 ctx->connect_attempts--;
2002 GNUNET_assert (ctx->d1core == NULL);
2003 ctx->d1core_ready = GNUNET_NO;
2005 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2006 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
2007 GNUNET_NO, no_handlers);
2008 if (ctx->d1core == NULL)
2010 if (NULL != ctx->cb)
2011 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2012 ctx->d2->cfg, ctx->d1, ctx->d2,
2013 _("Failed to connect to core service of first peer!\n"));
2018 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
2019 if (NULL != ctx->d2->hello)
2021 #if DEBUG_TESTING_RECONNECT
2022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2023 "updating %s's HELLO\n",
2024 ctx->d2->shortname);
2026 GNUNET_free (ctx->d2->hello);
2027 ctx->d2->hello = NULL;
2028 if (NULL != ctx->d2->th)
2030 GNUNET_TRANSPORT_get_hello_cancel (ctx->d2->ghh);
2031 ctx->d2->ghh = NULL;
2032 GNUNET_TRANSPORT_disconnect (ctx->d2->th);
2035 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2037 GNUNET_assert (ctx->d2->th != NULL);
2039 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2041 #if DEBUG_TESTING_RECONNECT
2044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2045 "didn't have %s's HELLO\n",
2046 ctx->d2->shortname);
2050 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
2052 #if DEBUG_TESTING_RECONNECT
2053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2054 "didn't have %s's HELLO, trying to get it now\n",
2055 ctx->d2->shortname);
2058 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2060 if (NULL == ctx->d2->th)
2062 GNUNET_CORE_disconnect (ctx->d1core);
2064 if (NULL != ctx->cb)
2065 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2066 ctx->d2->cfg, ctx->d1, ctx->d2,
2067 _("Failed to connect to transport service!\n"));
2071 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2073 #if DEBUG_TESTING_RECONNECT
2076 if (NULL == ctx->d2->hello)
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 "didn't have %s's HELLO but th wasn't NULL, not trying!!\n",
2080 ctx->d2->shortname);
2085 if (ctx->send_hello == GNUNET_YES)
2087 #if DEBUG_TESTING_RECONNECT
2088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2089 "Sending %s's HELLO to %s\n",
2090 ctx->d1->shortname, ctx->d2->shortname);
2093 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2095 if (ctx->d1th == NULL)
2097 GNUNET_CORE_disconnect (ctx->d1core);
2099 if (NULL != ctx->cb)
2100 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
2101 ctx->d2->cfg, ctx->d1, ctx->d2,
2102 _("Failed to connect to transport service!\n"));
2105 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2106 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2110 #if DEBUG_TESTING_RECONNECT
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "Trying to reconnect %s to %s\n",
2113 ctx->d1->shortname, ctx->d2->shortname);
2115 GNUNET_TRANSPORT_try_connect (ctx->d1th, &ctx->d2->id);
2118 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2119 ¬ify_connect_result, ctx);
2123 * Iterator for currently known peers, to ensure
2124 * that we don't try to send duplicate connect
2127 * @param cls our "struct GNUNET_TESTING_ConnectContext"
2128 * @param peer identity of the peer that has connected,
2129 * NULL when iteration has finished
2130 * @param atsi performance information
2131 * @param atsi_count number of records in 'atsi'
2135 core_initial_iteration (void *cls, const struct GNUNET_PeerIdentity *peer,
2136 const struct GNUNET_ATS_Information *atsi,
2137 unsigned int atsi_count)
2139 struct GNUNET_TESTING_ConnectContext *ctx = cls;
2141 if ((peer != NULL) &&
2142 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2144 ctx->connected = GNUNET_YES;
2145 ctx->distance = 0; /* FIXME: distance */
2149 return; /* ignore other peers */
2150 /* peer == NULL: End of iteration over peers */
2152 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->timeout_task);
2153 if (ctx->connected == GNUNET_YES)
2155 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2159 /* Peer not already connected, need to schedule connect request! */
2160 if (ctx->d1core == NULL)
2163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2164 "Peers are NOT connected, connecting to core!\n");
2167 GNUNET_CORE_connect (ctx->d1->cfg, 1, ctx, &core_init_notify,
2168 &connect_notify, NULL, NULL, GNUNET_NO, NULL,
2169 GNUNET_NO, no_handlers);
2172 if (ctx->d1core == NULL)
2174 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2178 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 */
2181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2182 "Don't have d2's HELLO, trying to get it!\n");
2185 GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL,
2187 if (ctx->d2->th == NULL)
2189 GNUNET_CORE_disconnect (ctx->d1core);
2192 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2196 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2199 if (ctx->send_hello == GNUNET_YES)
2202 GNUNET_TRANSPORT_connect (ctx->d1->cfg, &ctx->d1->id, ctx->d1, NULL,
2204 if (ctx->d1th == NULL)
2206 GNUNET_CORE_disconnect (ctx->d1core);
2209 GNUNET_SCHEDULER_add_now (¬ify_connect_result, ctx);
2212 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == ctx->hello_send_task);
2213 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2217 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2218 ¬ify_connect_result, ctx);
2224 * Establish a connection between two GNUnet daemons. The daemons
2225 * must both be running and not be stopped until either the
2226 * 'cb' callback is called OR the connection request has been
2227 * explicitly cancelled.
2229 * @param d1 handle for the first daemon
2230 * @param d2 handle for the second daemon
2231 * @param timeout how long is the connection attempt
2233 * @param max_connect_attempts how many times should we try to reconnect
2235 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2236 * the HELLO has already been exchanged
2237 * @param cb function to call at the end
2238 * @param cb_cls closure for cb
2239 * @return handle to cancel the request
2241 struct GNUNET_TESTING_ConnectContext *
2242 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2243 struct GNUNET_TESTING_Daemon *d2,
2244 struct GNUNET_TIME_Relative timeout,
2245 unsigned int max_connect_attempts,
2247 GNUNET_TESTING_NotifyConnection cb,
2250 struct GNUNET_TESTING_ConnectContext *ctx;
2252 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2255 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2256 _("Peers are not fully running yet, can not connect!\n"));
2257 GNUNET_log (GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2261 ctx = GNUNET_malloc (sizeof (struct GNUNET_TESTING_ConnectContext));
2264 ctx->timeout_hello =
2265 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2266 ctx->relative_timeout =
2267 GNUNET_TIME_relative_divide (timeout, max_connect_attempts);
2269 ctx->cb_cls = cb_cls;
2270 ctx->connect_attempts = max_connect_attempts;
2271 ctx->connected = GNUNET_NO;
2272 ctx->send_hello = send_hello;
2274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Asked to connect peer %s to peer %s\n",
2275 d1->shortname, d2->shortname);
2278 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2279 GNUNET_assert (GNUNET_OK ==
2280 GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id,
2281 &core_initial_iteration, ctx));
2287 * Cancel an attempt to connect two daemons.
2289 * @param cc connect context
2292 GNUNET_TESTING_daemons_connect_cancel (struct GNUNET_TESTING_ConnectContext *cc)
2294 if (GNUNET_SCHEDULER_NO_TASK != cc->timeout_task)
2296 GNUNET_SCHEDULER_cancel (cc->timeout_task);
2297 cc->timeout_task = GNUNET_SCHEDULER_NO_TASK;
2299 if (GNUNET_SCHEDULER_NO_TASK != cc->hello_send_task)
2301 GNUNET_SCHEDULER_cancel (cc->hello_send_task);
2302 cc->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
2304 if (NULL != cc->d1core)
2306 GNUNET_CORE_disconnect (cc->d1core);
2309 if (NULL != cc->d1th)
2311 GNUNET_TRANSPORT_disconnect (cc->d1th);
2318 /* end of testing.c */