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_NO
41 #define DEBUG_TESTING_RECONNECT GNUNET_YES
44 * How long do we wait after starting gnunet-service-arm
45 * for the core service to be alive?
47 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
50 * How many times are we willing to try to wait for "scp" or
51 * "gnunet-service-arm" to complete (waitpid) before giving up?
53 #define MAX_EXEC_WAIT_RUNS 250
55 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
58 * Receive the HELLO from one peer, give it to the other
59 * and ask them to connect.
61 * @param cls "struct ConnectContext"
62 * @param message HELLO message of peer
65 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
67 struct GNUNET_TESTING_Daemon *daemon = cls;
68 GNUNET_TESTING_NotifyDaemonRunning cb;
74 GNUNET_assert (daemon->phase == SP_GET_HELLO);
78 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
79 GNUNET_SCHEDULER_cancel(daemon->task);
81 if (daemon->server != NULL)
84 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
85 "Received `%s' from transport service of `%4s', disconnecting core!\n",
86 "HELLO", GNUNET_i2s (&daemon->id));
88 GNUNET_CORE_disconnect (daemon->server);
89 daemon->server = NULL;
92 GNUNET_assert (message != NULL);
93 msize = ntohs (message->size);
98 if (daemon->th != NULL)
100 GNUNET_TRANSPORT_get_hello_cancel (daemon->th, &process_hello, daemon);
103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
104 "Received `%s' from transport service of `%4s'\n",
105 "HELLO", GNUNET_i2s (&daemon->id));
108 GNUNET_free_non_null (daemon->hello);
109 daemon->hello = GNUNET_malloc (msize);
110 memcpy (daemon->hello, message, msize);
112 if (daemon->th != NULL)
114 GNUNET_TRANSPORT_disconnect (daemon->th);
117 daemon->phase = SP_START_DONE;
119 if (NULL != cb) /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
120 cb (daemon->cb_cls, &daemon->id, daemon->cfg, daemon, NULL);
124 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
127 * Function called after GNUNET_CORE_connect has succeeded
128 * (or failed for good). Note that the private key of the
129 * peer is intentionally not exposed here; if you need it,
130 * your process should try to read the private key file
131 * directly (which should work if you are authorized...).
134 * @param server handle to the server, NULL if we failed
135 * @param my_identity ID of this peer, NULL if we failed
136 * @param publicKey public key of this peer, NULL if we failed
139 testing_init (void *cls,
140 struct GNUNET_CORE_Handle *server,
141 const struct GNUNET_PeerIdentity *my_identity,
142 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
144 struct GNUNET_TESTING_Daemon *d = cls;
146 GNUNET_assert (d->phase == SP_START_CORE);
147 d->phase = SP_GET_HELLO;
152 if (GNUNET_YES == d->dead)
153 GNUNET_TESTING_daemon_stop (d,
154 GNUNET_TIME_absolute_get_remaining
155 (d->max_timeout), d->dead_cb,
156 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
157 else if (NULL != d->cb)
158 d->cb (d->cb_cls, NULL, d->cfg, d,
159 _("Failed to connect to core service\n"));
163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
164 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
166 d->id = *my_identity;
167 d->shortname = strdup (GNUNET_i2s (my_identity));
169 d->running = GNUNET_YES;
171 if (GNUNET_NO == d->running)
174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
175 "Peer is dead (d->running == GNUNET_NO)\n");
180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
181 "Successfully started peer `%4s', connecting to transport service.\n",
182 GNUNET_i2s (my_identity));
185 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
188 if (GNUNET_YES == d->dead)
189 GNUNET_TESTING_daemon_stop (d,
190 GNUNET_TIME_absolute_get_remaining
191 (d->max_timeout), d->dead_cb,
192 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
193 else if (NULL != d->cb)
194 d->cb (d->cb_cls, &d->id, d->cfg, d,
195 _("Failed to connect to transport service!\n"));
199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
200 "Connected to transport service `%s', getting HELLO\n",
201 GNUNET_i2s (my_identity));
204 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
206 if (d->task != GNUNET_SCHEDULER_NO_TASK)
207 GNUNET_SCHEDULER_cancel(d->task);
209 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
215 * Finite-state machine for starting GNUnet.
217 * @param cls our "struct GNUNET_TESTING_Daemon"
221 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 struct GNUNET_TESTING_Daemon *d = cls;
224 GNUNET_TESTING_NotifyDaemonRunning cb;
225 enum GNUNET_OS_ProcessStatusType type;
231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
232 "Peer FSM is in phase %u.\n", d->phase);
235 d->task = GNUNET_SCHEDULER_NO_TASK;
239 /* confirm copying complete */
240 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
242 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
252 ("`scp' does not seem to terminate (timeout copying config).\n"));
257 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
261 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
267 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
272 "Successfully copied configuration file.\n");
274 d->phase = SP_COPIED;
277 /* Start create hostkey process if we don't already know the peer identity!*/
278 if (GNUNET_NO == d->have_hostkey)
280 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
281 if (d->pipe_stdout == NULL)
290 (NULL == d->hostname)
291 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
292 : _("Failed to create pipe for `ssh' process.\n"));
295 if (NULL == d->hostname)
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Starting `%s', with command `%s %s %s %s'.\n",
300 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
304 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
305 "gnunet-peerinfo", "-c", d->cfgfile,
307 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
308 GNUNET_DISK_PIPE_END_WRITE);
312 if (d->username != NULL)
313 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
315 dst = GNUNET_strdup (d->hostname);
318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
319 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
320 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
323 if (d->ssh_port_str == NULL)
325 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
332 "-c", d->cfgfile, "-sq",
337 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
338 "ssh", "-p", d->ssh_port_str,
344 "-c", d->cfgfile, "-sq",
347 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
348 GNUNET_DISK_PIPE_END_WRITE);
353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
354 _("Could not start `%s' process to create hostkey.\n"),
355 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
363 (NULL == d->hostname)
364 ? _("Failed to start `gnunet-peerinfo' process.\n")
365 : _("Failed to start `ssh' process.\n"));
366 GNUNET_DISK_pipe_close (d->pipe_stdout);
370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
371 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
373 d->phase = SP_HOSTKEY_CREATE;
376 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
378 GNUNET_DISK_pipe_handle
380 GNUNET_DISK_PIPE_END_READ),
383 else /* Already have a hostkey! */
385 if (d->hostkey_callback != NULL)
387 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
388 d->hostkey_callback = NULL;
389 d->phase = SP_HOSTKEY_CREATED;
392 d->phase = SP_TOPOLOGY_SETUP;
396 = GNUNET_SCHEDULER_add_now (&start_fsm, d);
399 case SP_HOSTKEY_CREATE:
401 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
402 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
403 &d->hostkeybuf[d->hostkeybufpos],
404 sizeof (d->hostkeybuf) - d->hostkeybufpos);
406 d->hostkeybufpos += bytes_read;
408 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
413 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
415 GNUNET_DISK_pipe_handle
417 GNUNET_DISK_PIPE_END_READ),
421 d->hostkeybuf[103] = '\0';
423 if ((bytes_read < 0) ||
424 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
429 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
430 _("Error reading from gnunet-peerinfo: %s\n"),
433 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
434 _("Malformed output from gnunet-peerinfo!\n"));
437 GNUNET_DISK_pipe_close (d->pipe_stdout);
438 d->pipe_stdout = NULL;
439 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
440 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
441 GNUNET_OS_process_close (d->proc);
444 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
447 GNUNET_DISK_pipe_close (d->pipe_stdout);
448 d->pipe_stdout = NULL;
449 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
450 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
451 GNUNET_OS_process_close (d->proc);
453 d->have_hostkey = GNUNET_YES;
454 if (d->hostkey_callback != NULL)
456 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
457 d->hostkey_callback = NULL;
458 d->phase = SP_HOSTKEY_CREATED;
462 d->phase = SP_TOPOLOGY_SETUP;
465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
468 case SP_HOSTKEY_CREATED:
469 /* wait for topology finished */
470 if ((GNUNET_YES == d->dead)
471 || (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
479 d->cfg, d, _("`Failed while waiting for topology setup!\n"));
484 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
487 case SP_TOPOLOGY_SETUP:
488 /* start GNUnet on remote host */
489 if (NULL == d->hostname)
492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
493 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
494 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
495 "-L", "DEBUG", "-s");
497 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
498 "gnunet-arm", "-c", d->cfgfile,
506 if (d->username != NULL)
507 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
509 dst = GNUNET_strdup (d->hostname);
512 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
513 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
514 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
515 d->cfgfile, "-L", "DEBUG", "-s", "-q");
517 if (d->ssh_port_str == NULL)
519 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
527 "-c", d->cfgfile, "-s", "-q",
533 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
534 "ssh", "-p", d->ssh_port_str,
542 "-c", d->cfgfile, "-s", "-q",
549 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
550 _("Could not start `%s' process to start GNUnet.\n"),
551 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
559 (NULL == d->hostname)
560 ? _("Failed to start `gnunet-arm' process.\n")
561 : _("Failed to start `ssh' process.\n"));
565 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
566 "Started `%s', waiting for `%s' to be up.\n",
567 "gnunet-arm", "gnunet-service-core");
569 d->phase = SP_START_ARMING;
571 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
574 case SP_START_ARMING:
575 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
577 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
587 (NULL == d->hostname)
588 ? _("`gnunet-arm' does not seem to terminate.\n")
589 : _("`ssh' does not seem to terminate.\n"));
590 GNUNET_CONFIGURATION_destroy (d->cfg);
591 GNUNET_free (d->cfgfile);
592 GNUNET_free_non_null (d->hostname);
593 GNUNET_free_non_null (d->username);
594 GNUNET_free(d->proc);
600 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
605 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
606 "Successfully started `%s'.\n", "gnunet-arm");
608 GNUNET_free(d->proc);
609 d->phase = SP_START_CORE;
611 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
612 "Calling CORE_connect\n");
616 if (d->server != NULL)
617 GNUNET_CORE_disconnect(d->server);
619 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
629 _("Unable to connect to CORE service for peer!\n"));
630 GNUNET_CONFIGURATION_destroy (d->cfg);
631 GNUNET_free (d->cfgfile);
632 GNUNET_free_non_null (d->hostname);
633 GNUNET_free_non_null (d->username);
637 d->server = GNUNET_CORE_connect (d->cfg, 1,
642 NULL, GNUNET_NO, no_handlers);
644 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
648 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
651 if (d->server != NULL)
652 GNUNET_CORE_disconnect(d->server);
654 GNUNET_TRANSPORT_disconnect(d->th);
662 _("Unable to get HELLO for peer!\n"));
663 GNUNET_CONFIGURATION_destroy (d->cfg);
664 GNUNET_free (d->cfgfile);
665 GNUNET_free_non_null (d->hostname);
666 GNUNET_free_non_null (d->username);
670 if (d->hello != NULL)
672 GNUNET_assert(d->task == GNUNET_SCHEDULER_NO_TASK);
674 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
680 case SP_SHUTDOWN_START:
681 /* confirm copying complete */
682 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
684 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
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"));
693 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello,
695 GNUNET_TRANSPORT_disconnect (d->th);
698 GNUNET_CONFIGURATION_destroy (d->cfg);
699 GNUNET_free (d->cfgfile);
700 GNUNET_free_non_null (d->hello);
701 GNUNET_free_non_null (d->hostname);
702 GNUNET_free_non_null (d->username);
703 GNUNET_free_non_null (d->shortname);
704 GNUNET_free_non_null (d->proc);
711 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
715 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
717 if (NULL != d->dead_cb)
718 d->dead_cb (d->dead_cb_cls,
720 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
723 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
724 GNUNET_TRANSPORT_disconnect (d->th);
727 if (d->server != NULL)
729 GNUNET_CORE_disconnect (d->server);
732 GNUNET_CONFIGURATION_destroy (d->cfg);
733 GNUNET_free (d->cfgfile);
734 GNUNET_free_non_null (d->hello);
735 GNUNET_free_non_null (d->hostname);
736 GNUNET_free_non_null (d->username);
737 GNUNET_free_non_null (d->shortname);
738 GNUNET_free_non_null (d->proc);
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
746 if (d->server != NULL)
748 GNUNET_CORE_disconnect (d->server);
754 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
755 GNUNET_TRANSPORT_disconnect (d->th);
758 /* state clean up and notifications */
759 if (d->churn == GNUNET_NO)
761 GNUNET_CONFIGURATION_destroy (d->cfg);
762 GNUNET_free (d->cfgfile);
763 GNUNET_free_non_null (d->hostname);
764 GNUNET_free_non_null (d->username);
767 GNUNET_free_non_null (d->hello);
769 GNUNET_free_non_null (d->shortname);
770 GNUNET_free_non_null (d->proc);
773 if (NULL != d->dead_cb)
774 d->dead_cb (d->dead_cb_cls, NULL);
776 if (d->churn == GNUNET_NO)
780 case SP_CONFIG_UPDATE:
781 /* confirm copying complete */
782 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
784 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
791 d->cfg, d, _("`scp' does not seem to terminate.\n"));
796 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
800 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
802 if (NULL != d->update_cb)
803 d->update_cb (d->update_cb_cls,
804 _("`scp' did not complete cleanly.\n"));
808 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
809 "Successfully copied configuration file.\n");
811 if (NULL != d->update_cb)
812 d->update_cb (d->update_cb_cls, NULL);
813 d->phase = SP_START_DONE;
819 * Continues GNUnet daemon startup when user wanted to be notified
820 * once a hostkey was generated (for creating friends files, blacklists,
823 * @param daemon the daemon to finish starting
826 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
828 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
829 daemon->phase = SP_TOPOLOGY_SETUP;
833 * Check whether the given daemon is running.
835 * @param daemon the daemon to check
837 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
838 * daemon is down, GNUNET_SYSERR on error.
841 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
844 return GNUNET_SYSERR;
846 if (daemon->running == GNUNET_YES)
853 * Start a peer that has previously been stopped using the daemon_stop
854 * call (and files weren't deleted and the allow restart flag)
856 * @param daemon the daemon to start (has been previously stopped)
857 * @param timeout how long to wait for restart
858 * @param cb the callback for notification when the peer is running
859 * @param cb_cls closure for the callback
862 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
863 struct GNUNET_TIME_Relative timeout,
864 GNUNET_TESTING_NotifyDaemonRunning cb,
867 if (daemon->running == GNUNET_YES)
869 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
870 "Daemon already running, can't restart!");
875 daemon->cb_cls = cb_cls;
876 daemon->phase = SP_TOPOLOGY_SETUP;
877 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
879 GNUNET_SCHEDULER_add_continuation (&start_fsm,
881 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
885 * Starts a GNUnet daemon. GNUnet must be installed on the target
886 * system and available in the PATH. The machine must furthermore be
887 * reachable via "ssh" (unless the hostname is "NULL") without the
888 * need to enter a password.
890 * @param cfg configuration to use
891 * @param timeout how long to wait starting up peers
892 * @param hostname name of the machine where to run GNUnet
893 * (use NULL for localhost).
894 * @param ssh_username ssh username to use when connecting to hostname
895 * @param sshport port to pass to ssh process when connecting to hostname
896 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
897 * @param hostkey_callback function to call once the hostkey has been
898 * generated for this peer, but it hasn't yet been started
899 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
900 * @param hostkey_cls closure for hostkey callback
901 * @param cb function to call once peer is up, or failed to start
902 * @param cb_cls closure for cb
903 * @return handle to the daemon (actual start will be completed asynchronously)
905 struct GNUNET_TESTING_Daemon *
906 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
907 struct GNUNET_TIME_Relative timeout,
908 const char *hostname,
909 const char *ssh_username,
912 GNUNET_TESTING_NotifyHostkeyCreated
913 hostkey_callback, void *hostkey_cls,
914 GNUNET_TESTING_NotifyDaemonRunning cb,
917 struct GNUNET_TESTING_Daemon *ret;
921 char *baseservicehome;
924 char *temp_file_name;
925 struct GNUNET_DISK_FileHandle *fn;
926 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
927 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
929 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
930 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
933 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
936 ret->ssh_port_str = NULL;
938 /* Find service home and base service home directories, create it if it doesn't exist */
939 GNUNET_assert(GNUNET_OK ==
940 GNUNET_CONFIGURATION_get_value_string (cfg,
945 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
946 GNUNET_asprintf(&temp_file_name, "%s/gnunet-testing-config", servicehome);
947 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
948 GNUNET_free(temp_file_name);
950 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
951 "Setting up peer with configuration file `%s'.\n",
954 if (NULL == ret->cfgfile)
956 GNUNET_free_non_null (ret->ssh_port_str);
957 GNUNET_free_non_null (ret->hostname);
961 ret->hostkey_callback = hostkey_callback;
962 ret->hostkey_cls = hostkey_cls;
964 ret->cb_cls = cb_cls;
965 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
966 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
967 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
969 "DEFAULTCONFIG", ret->cfgfile);
971 if (hostkey != NULL) /* Get the peer identity from the hostkey */
973 private_key = GNUNET_CRYPTO_rsa_decode_key(hostkey, HOSTKEYFILESIZE);
974 GNUNET_assert(private_key != NULL);
975 GNUNET_CRYPTO_rsa_key_get_public (private_key,
977 GNUNET_CRYPTO_hash(&public_key, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &ret->id.hashPubKey);
978 ret->shortname = GNUNET_strdup(GNUNET_i2s(&ret->id));
979 ret->have_hostkey = GNUNET_YES;
980 GNUNET_free(private_key);
983 /* Write hostkey to file, if we were given one */
987 GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome);
989 GNUNET_DISK_file_open (hostkeyfile,
990 GNUNET_DISK_OPEN_READWRITE
991 | GNUNET_DISK_OPEN_CREATE,
992 GNUNET_DISK_PERM_USER_READ |
993 GNUNET_DISK_PERM_USER_WRITE);
994 GNUNET_assert(fn != NULL);
995 GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE));
996 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn));
999 /* write configuration to temporary file */
1000 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1002 if (0 != UNLINK (ret->cfgfile))
1003 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1004 "unlink", ret->cfgfile);
1005 GNUNET_CONFIGURATION_destroy (ret->cfg);
1006 GNUNET_free_non_null (ret->hostname);
1007 GNUNET_free (ret->cfgfile);
1011 if (ssh_username != NULL)
1012 username = GNUNET_strdup (ssh_username);
1013 if ((ssh_username == NULL) && (GNUNET_OK !=
1014 GNUNET_CONFIGURATION_get_value_string (cfg,
1019 if (NULL != getenv ("USER"))
1020 username = GNUNET_strdup (getenv ("USER"));
1024 ret->username = username;
1026 /* copy directory to remote host */
1027 if (NULL != hostname)
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1031 "Copying configuration directory to host `%s'.\n", hostname);
1033 baseservicehome = GNUNET_strdup(servicehome);
1034 /* Remove trailing /'s */
1035 while (baseservicehome[strlen(baseservicehome) - 1] == '/')
1036 baseservicehome[strlen(baseservicehome) - 1] = '\0';
1037 /* Find next directory /, jump one ahead */
1038 slash = strrchr(baseservicehome, '/');
1042 ret->phase = SP_COPYING;
1043 if (NULL != username)
1044 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1046 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1048 if (ret->ssh_port_str == NULL)
1050 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1054 servicehome, arg, NULL);
1056 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", servicehome, arg);
1061 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
1062 "scp", "-r", "-P", ret->ssh_port_str,
1066 servicehome, arg, NULL);
1069 if (NULL == ret->proc)
1071 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1073 ("Could not start `%s' process to copy configuration directory.\n"),
1075 if (0 != UNLINK (ret->cfgfile))
1076 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1077 "unlink", ret->cfgfile);
1078 GNUNET_CONFIGURATION_destroy (ret->cfg);
1079 GNUNET_free_non_null (ret->hostname);
1080 GNUNET_free_non_null (ret->username);
1081 GNUNET_free (ret->cfgfile);
1083 if ((hostkey != NULL) && (0 != UNLINK(hostkeyfile)))
1084 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1085 "unlink", hostkeyfile);
1086 GNUNET_free_non_null(hostkeyfile);
1087 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1088 GNUNET_free(servicehome);
1093 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1095 GNUNET_free_non_null(hostkeyfile);
1096 GNUNET_free(servicehome);
1100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1101 "No need to copy configuration file since we are running locally.\n");
1103 ret->phase = SP_COPIED;
1104 GNUNET_SCHEDULER_add_continuation (&start_fsm,
1106 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1107 GNUNET_free_non_null(hostkeyfile);
1108 GNUNET_free(servicehome);
1114 * Restart (stop and start) a GNUnet daemon.
1116 * @param d the daemon that should be restarted
1117 * @param cb function called once the daemon is (re)started
1118 * @param cb_cls closure for cb
1121 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1122 GNUNET_TESTING_NotifyDaemonRunning cb,
1131 d->dead = GNUNET_YES;
1138 if (d->phase == SP_CONFIG_UPDATE)
1140 GNUNET_SCHEDULER_cancel (d->task);
1141 d->phase = SP_START_DONE;
1143 if (d->server != NULL)
1145 GNUNET_CORE_disconnect (d->server);
1151 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1152 GNUNET_TRANSPORT_disconnect (d->th);
1155 /* state clean up and notifications */
1156 GNUNET_free_non_null (d->hello);
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1163 d->phase = SP_START_ARMING;
1165 /* Check if this is a local or remote process */
1166 if (NULL != d->hostname)
1169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1170 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1171 d->cfgfile, d->hostname);
1174 if (d->username != NULL)
1175 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1177 arg = GNUNET_strdup (d->hostname);
1179 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1187 "-c", d->cfgfile, "-e", "-r", NULL);
1188 /* Use -r to restart arm and all services */
1195 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1196 "Stopping gnunet-arm with config `%s' locally.\n",
1199 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1204 "-c", d->cfgfile, "-e", "-r", NULL);
1207 GNUNET_free_non_null (del_arg);
1209 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1216 * Stops a GNUnet daemon.
1218 * @param d the daemon that should be stopped
1219 * @param timeout how long to wait for process for shutdown to complete
1220 * @param cb function called once the daemon was stopped
1221 * @param cb_cls closure for cb
1222 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1224 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1225 * GNUNET_NO to kill off and clean up for good
1228 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1229 struct GNUNET_TIME_Relative timeout,
1230 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1231 int delete_files, int allow_restart)
1236 d->dead_cb_cls = cb_cls;
1241 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1242 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1244 d->dead = GNUNET_YES;
1248 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1250 /* Free what was left from churning! */
1251 GNUNET_assert (d->cfg != NULL);
1252 GNUNET_CONFIGURATION_destroy (d->cfg);
1253 if (delete_files == GNUNET_YES)
1255 if (0 != UNLINK (d->cfgfile))
1257 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1260 GNUNET_free (d->cfgfile);
1261 GNUNET_free_non_null (d->hostname);
1262 GNUNET_free_non_null (d->username);
1263 if (NULL != d->dead_cb)
1264 d->dead_cb (d->dead_cb_cls, NULL);
1270 if (delete_files == GNUNET_YES)
1272 GNUNET_asprintf (&del_arg, "-d");
1275 if (d->phase == SP_CONFIG_UPDATE)
1277 GNUNET_SCHEDULER_cancel (d->task);
1278 d->phase = SP_START_DONE;
1280 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1281 if (d->server != NULL)
1283 GNUNET_CORE_disconnect (d->server);
1287 /* shutdown ARM process (will terminate others) */
1289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1290 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1292 d->phase = SP_SHUTDOWN_START;
1293 d->running = GNUNET_NO;
1294 if (allow_restart == GNUNET_YES)
1295 d->churn = GNUNET_YES;
1298 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1299 GNUNET_TRANSPORT_disconnect (d->th);
1302 /* Check if this is a local or remote process */
1303 if (NULL != d->hostname)
1306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1307 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1308 d->cfgfile, d->hostname);
1311 if (d->username != NULL)
1312 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1314 arg = GNUNET_strdup (d->hostname);
1316 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1324 "-c", d->cfgfile, "-e", "-q",
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1328 arg, "gnunet-arm", d->cfgfile, del_arg);
1329 /* Use -e to end arm, and -d to remove temp files */
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 "Stopping gnunet-arm with config `%s' locally.\n",
1339 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1344 "-c", d->cfgfile, "-e", "-q",
1348 GNUNET_free_non_null (del_arg);
1349 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1350 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1355 * Changes the configuration of a GNUnet daemon.
1357 * @param d the daemon that should be modified
1358 * @param cfg the new configuration for the daemon
1359 * @param cb function called once the configuration was changed
1360 * @param cb_cls closure for cb
1363 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1364 struct GNUNET_CONFIGURATION_Handle *cfg,
1365 GNUNET_TESTING_NotifyCompletion cb,
1370 if (d->phase != SP_START_DONE)
1375 ("Peer not yet running, can not change configuration at this point."));
1379 /* 1) write configuration to temporary file */
1380 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1383 cb (cb_cls, _("Failed to write new configuration to disk."));
1387 /* 2) copy file to remote host (if necessary) */
1388 if (NULL == d->hostname)
1390 /* signal success */
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Copying updated configuration file to remote host `%s'.\n",
1400 d->phase = SP_CONFIG_UPDATE;
1401 if (NULL != d->username)
1402 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1404 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1405 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1409 d->cfgfile, arg, NULL);
1411 if (NULL == d->proc)
1413 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1415 ("Could not start `%s' process to copy configuration file.\n"),
1418 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1419 d->phase = SP_START_DONE;
1423 d->update_cb_cls = cb_cls;
1425 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1431 * Data kept for each pair of peers that we try
1434 struct ConnectContext
1437 * Testing handle to the first daemon.
1439 struct GNUNET_TESTING_Daemon *d1;
1442 * Handle to core of first daemon (to check connect)
1444 struct GNUNET_CORE_Handle *d1core;
1447 * Have we actually connected to the core of the first daemon yet?
1452 * Testing handle to the second daemon.
1454 struct GNUNET_TESTING_Daemon *d2;
1457 * Handler for the request to core to connect to this peer.
1459 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1462 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1464 struct GNUNET_TRANSPORT_Handle *d1th;
1467 * Function to call once we are done (or have timed out).
1469 GNUNET_TESTING_NotifyConnection cb;
1477 * The relative timeout from whence this connect attempt was
1478 * started. Allows for reconnect attempts.
1480 struct GNUNET_TIME_Relative relative_timeout;
1483 * Maximum number of connect attempts, will retry connection
1484 * this number of times on failures.
1486 unsigned int connect_attempts;
1489 * Hello timeout task
1491 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1494 * Connect timeout task
1496 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1499 * When should this operation be complete (or we must trigger
1502 struct GNUNET_TIME_Relative timeout_hello;
1505 * Was the connection attempt successful?
1510 * When connecting, do we need to send the HELLO?
1515 * The distance between the two connected peers
1521 /** Forward declaration **/
1523 reattempt_daemons_connect (void *cls,
1524 const struct GNUNET_SCHEDULER_TaskContext *tc);
1528 * Notify callback about success or failure of the attempt
1529 * to connect the two peers
1531 * @param cls our "struct ConnectContext" (freed)
1532 * @param tc reason tells us if we succeeded or failed
1535 notify_connect_result (void *cls,
1536 const struct GNUNET_SCHEDULER_TaskContext *tc)
1538 struct ConnectContext *ctx = cls;
1539 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1540 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1542 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1543 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1546 if (ctx->connect_request_handle != NULL)
1548 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1549 ctx->connect_request_handle = NULL;
1552 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1554 if (ctx->d1th != NULL)
1555 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1557 if (ctx->d1core != NULL)
1558 GNUNET_CORE_disconnect (ctx->d1core);
1560 if (ctx->d2core != NULL)
1561 GNUNET_CORE_disconnect (ctx->d2core);
1569 if (ctx->d1th != NULL)
1570 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1572 if (ctx->d1core != NULL)
1573 GNUNET_CORE_disconnect (ctx->d1core);
1576 if (ctx->connected == GNUNET_YES)
1578 if (ctx->cb != NULL)
1580 ctx->cb (ctx->cb_cls,
1584 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1587 else if (ctx->connect_attempts > 0)
1589 ctx->d1core_ready = GNUNET_NO;
1591 if (ctx->d2core != NULL)
1593 GNUNET_CORE_disconnect (ctx->d2core);
1597 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1602 if (ctx->cb != NULL)
1604 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1605 ctx->d2->cfg, ctx->d1, ctx->d2,
1606 _("Peers failed to connect"));
1615 * Success, connection is up. Signal client our success.
1617 * @param cls our "struct ConnectContext"
1618 * @param peer identity of the peer that has connected
1619 * @param atsi performance information
1623 connect_notify (void *cls,
1624 const struct GNUNET_PeerIdentity *peer,
1625 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1627 struct ConnectContext *ctx = cls;
1630 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1631 "Connected peer %s to peer %s\n",
1632 ctx->d1->shortname, GNUNET_i2s(peer));
1635 if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1638 ctx->connected = GNUNET_YES;
1639 ctx->distance = 0; /* FIXME: distance */
1640 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1642 GNUNET_SCHEDULER_cancel(ctx->hello_send_task);
1643 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1645 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1646 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1653 * Success, connection is up. Signal client our success.
1655 * @param cls our "struct ConnectContext"
1656 * @param peer identity of the peer that has connected
1657 * @param atsi performance information
1661 connect_notify_core2 (void *cls,
1662 const struct GNUNET_PeerIdentity *peer,
1663 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1665 struct ConnectContext *ctx = cls;
1667 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
1669 ctx->connected = GNUNET_YES;
1670 ctx->distance = 0; /* FIXME: distance */
1671 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1672 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1680 * Task called once a core connect request has been transmitted.
1682 * @param cls struct ConnectContext
1683 * @param success was the request successful?
1686 core_connect_request_cont (void *cls,
1689 struct ConnectContext *ctx = cls;
1691 ctx->connect_request_handle = NULL;
1695 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1697 struct ConnectContext *ctx = cls;
1698 struct GNUNET_MessageHeader *hello;
1699 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1700 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1702 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
1703 && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
1705 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1706 GNUNET_assert (hello != NULL);
1707 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1708 GNUNET_assert (ctx->d1core != NULL);
1709 ctx->connect_request_handle =
1710 GNUNET_CORE_peer_request_connect (ctx->d1core,
1711 ctx->relative_timeout,
1713 &core_connect_request_cont, ctx);
1716 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1717 "Sending connect request to CORE of %s for peer %s\n",
1718 GNUNET_i2s (&ctx->d1->id),
1719 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1721 ctx->timeout_hello =
1722 GNUNET_TIME_relative_add (ctx->timeout_hello,
1723 GNUNET_TIME_relative_multiply
1724 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1726 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello,
1731 * Notify of a successful connection to the core service.
1733 * @param cls a ConnectContext
1734 * @param server handle to the core service
1735 * @param my_identity the peer identity of this peer
1736 * @param publicKey the public key of the peer
1739 core_init_notify (void *cls,
1740 struct GNUNET_CORE_Handle * server,
1741 const struct GNUNET_PeerIdentity *
1744 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1747 struct ConnectContext *connect_ctx = cls;
1748 connect_ctx->d1core_ready = GNUNET_YES;
1750 if (connect_ctx->send_hello == GNUNET_NO)
1752 connect_ctx->connect_request_handle =
1753 GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
1754 connect_ctx->relative_timeout,
1755 &connect_ctx->d2->id,
1756 &core_connect_request_cont, connect_ctx);
1757 GNUNET_assert(connect_ctx->connect_request_handle != NULL);
1759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1760 "Sending connect request to CORE of %s for peer %s\n",
1761 connect_ctx->d1->shortname,
1762 connect_ctx->d2->shortname);
1770 reattempt_daemons_connect (void *cls,
1771 const struct GNUNET_SCHEDULER_TaskContext *tc)
1773 struct ConnectContext *ctx = cls;
1774 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1779 #if DEBUG_TESTING_RECONNECT
1780 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1781 "re-attempting connect of peer %s to peer %s\n",
1782 ctx->d1->shortname, ctx->d2->shortname);
1784 ctx->connect_attempts--;
1785 GNUNET_assert (ctx->d1core == NULL);
1786 ctx->d1core_ready = GNUNET_NO;
1787 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1790 &connect_notify, NULL, NULL,
1792 NULL, GNUNET_NO, no_handlers);
1793 if (ctx->d1core == NULL)
1795 if (NULL != ctx->cb)
1796 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1797 ctx->d2->cfg, ctx->d1, ctx->d2,
1798 _("Failed to connect to core service of first peer!\n"));
1803 if (ctx->send_hello == GNUNET_YES)
1805 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1807 ctx->d1, NULL, NULL, NULL);
1808 if (ctx->d1th == NULL)
1810 GNUNET_CORE_disconnect (ctx->d1core);
1812 if (NULL != ctx->cb)
1813 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1814 ctx->d2->cfg, ctx->d1, ctx->d2,
1815 _("Failed to connect to transport service!\n"));
1818 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1822 ctx->connect_request_handle =
1823 GNUNET_CORE_peer_request_connect (ctx->d1core,
1824 ctx->relative_timeout,
1826 &core_connect_request_cont, ctx);
1829 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1830 ¬ify_connect_result, ctx);
1834 * Iterator for currently known peers, to ensure
1835 * that we don't try to send duplicate connect
1838 * @param cls our "struct ConnectContext"
1839 * @param peer identity of the peer that has connected,
1840 * NULL when iteration has finished
1841 * @param atsi performance information
1845 core_initial_iteration (void *cls,
1846 const struct GNUNET_PeerIdentity *peer,
1847 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1849 struct ConnectContext *ctx = cls;
1851 if ((peer != NULL) &&
1852 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
1854 ctx->connected = GNUNET_YES;
1855 ctx->distance = 0; /* FIXME: distance */
1858 else if (peer == NULL) /* End of iteration over peers */
1860 if (ctx->connected == GNUNET_YES)
1862 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1867 /* Peer not already connected, need to schedule connect request! */
1868 if (ctx->d1core == NULL)
1871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1872 "Peers are NOT connected, connecting to core!\n");
1874 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1877 &connect_notify, NULL, NULL,
1879 NULL, GNUNET_NO, no_handlers);
1882 if (ctx->d1core == NULL)
1885 if (NULL != ctx->cb)
1886 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1887 _("Failed to connect to core service of first peer!\n"));
1891 if (ctx->send_hello == GNUNET_YES)
1893 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1894 &ctx->d1->id, ctx->d1, NULL, NULL, NULL);
1895 if (ctx->d1th == NULL)
1897 GNUNET_CORE_disconnect (ctx->d1core);
1899 if (NULL != ctx->cb)
1900 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1901 _("Failed to connect to transport service!\n"));
1904 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1908 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1909 ¬ify_connect_result, ctx);
1915 * Establish a connection between two GNUnet daemons.
1917 * @param d1 handle for the first daemon
1918 * @param d2 handle for the second daemon
1919 * @param timeout how long is the connection attempt
1921 * @param max_connect_attempts how many times should we try to reconnect
1923 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
1924 * the HELLO has already been exchanged
1925 * @param cb function to call at the end
1926 * @param cb_cls closure for cb
1929 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1930 struct GNUNET_TESTING_Daemon *d2,
1931 struct GNUNET_TIME_Relative timeout,
1932 unsigned int max_connect_attempts,
1934 GNUNET_TESTING_NotifyConnection cb,
1937 struct ConnectContext *ctx;
1939 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1942 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1943 _("Peers are not fully running yet, can not connect!\n"));
1947 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1950 ctx->timeout_hello =
1951 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
1952 ctx->relative_timeout = GNUNET_TIME_relative_divide(timeout, max_connect_attempts);
1954 ctx->cb_cls = cb_cls;
1955 ctx->connect_attempts = max_connect_attempts;
1956 ctx->connected = GNUNET_NO;
1957 ctx->send_hello = send_hello;
1959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1960 "Asked to connect peer %s to peer %s\n",
1961 d1->shortname, d2->shortname);
1964 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
1965 GNUNET_assert(GNUNET_OK == GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id, &core_initial_iteration, ctx));
1966 /*GNUNET_assert(GNUNET_OK == GNUNET_CORE_iterate_peers (ctx->d1->cfg, &core_initial_iteration, ctx));*/
1969 /* end of testing.c */