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 */
278 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
279 if (d->pipe_stdout == NULL)
288 (NULL == d->hostname)
289 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
290 : _("Failed to create pipe for `ssh' process.\n"));
293 if (NULL == d->hostname)
296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
297 "Starting `%s', with command `%s %s %s %s'.\n",
298 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
302 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
303 "gnunet-peerinfo", "-c", d->cfgfile,
305 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
306 GNUNET_DISK_PIPE_END_WRITE);
310 if (d->username != NULL)
311 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
313 dst = GNUNET_strdup (d->hostname);
316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
317 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
318 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
321 if (d->ssh_port_str == NULL)
323 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
330 "-c", d->cfgfile, "-sq",
335 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
336 "ssh", "-p", d->ssh_port_str,
342 "-c", d->cfgfile, "-sq",
345 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
346 GNUNET_DISK_PIPE_END_WRITE);
351 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
352 _("Could not start `%s' process to create hostkey.\n"),
353 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
361 (NULL == d->hostname)
362 ? _("Failed to start `gnunet-peerinfo' process.\n")
363 : _("Failed to start `ssh' process.\n"));
364 GNUNET_DISK_pipe_close (d->pipe_stdout);
368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
369 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
371 d->phase = SP_HOSTKEY_CREATE;
374 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
376 GNUNET_DISK_pipe_handle
378 GNUNET_DISK_PIPE_END_READ),
381 case SP_HOSTKEY_CREATE:
383 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
384 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
385 &d->hostkeybuf[d->hostkeybufpos],
386 sizeof (d->hostkeybuf) - d->hostkeybufpos);
388 d->hostkeybufpos += bytes_read;
390 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
395 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
397 GNUNET_DISK_pipe_handle
399 GNUNET_DISK_PIPE_END_READ),
403 d->hostkeybuf[103] = '\0';
405 if ((bytes_read < 0) ||
406 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
411 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
412 _("Error reading from gnunet-peerinfo: %s\n"),
415 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
416 _("Malformed output from gnunet-peerinfo!\n"));
419 GNUNET_DISK_pipe_close (d->pipe_stdout);
420 d->pipe_stdout = NULL;
421 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
422 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
423 GNUNET_OS_process_close (d->proc);
426 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
429 GNUNET_DISK_pipe_close (d->pipe_stdout);
430 d->pipe_stdout = NULL;
431 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
432 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
433 GNUNET_OS_process_close (d->proc);
436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
438 if (d->hostkey_callback != NULL)
440 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
441 d->phase = SP_HOSTKEY_CREATED;
445 d->phase = SP_TOPOLOGY_SETUP;
448 case SP_HOSTKEY_CREATED:
449 /* wait for topology finished */
450 if ((GNUNET_YES == d->dead)
451 || (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
459 d->cfg, d, _("`Failed while waiting for topology setup!\n"));
464 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
467 case SP_TOPOLOGY_SETUP:
468 /* start GNUnet on remote host */
469 if (NULL == d->hostname)
472 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
473 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
474 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
475 "-L", "DEBUG", "-s");
477 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
478 "gnunet-arm", "-c", d->cfgfile,
486 if (d->username != NULL)
487 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
489 dst = GNUNET_strdup (d->hostname);
492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
493 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
494 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
495 d->cfgfile, "-L", "DEBUG", "-s", "-q");
497 if (d->ssh_port_str == NULL)
499 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
507 "-c", d->cfgfile, "-s", "-q",
513 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
514 "ssh", "-p", d->ssh_port_str,
522 "-c", d->cfgfile, "-s", "-q",
529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
530 _("Could not start `%s' process to start GNUnet.\n"),
531 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
539 (NULL == d->hostname)
540 ? _("Failed to start `gnunet-arm' process.\n")
541 : _("Failed to start `ssh' process.\n"));
545 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
546 "Started `%s', waiting for `%s' to be up.\n",
547 "gnunet-arm", "gnunet-service-core");
549 d->phase = SP_START_ARMING;
551 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
554 case SP_START_ARMING:
555 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
557 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
567 (NULL == d->hostname)
568 ? _("`gnunet-arm' does not seem to terminate.\n")
569 : _("`ssh' does not seem to terminate.\n"));
570 GNUNET_CONFIGURATION_destroy (d->cfg);
571 GNUNET_free (d->cfgfile);
572 GNUNET_free_non_null (d->hostname);
573 GNUNET_free_non_null (d->username);
574 GNUNET_free(d->proc);
580 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
585 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
586 "Successfully started `%s'.\n", "gnunet-arm");
588 GNUNET_free(d->proc);
589 d->phase = SP_START_CORE;
591 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
592 "Calling CORE_connect\n");
596 if (d->server != NULL)
597 GNUNET_CORE_disconnect(d->server);
599 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
609 _("Unable to connect to CORE service for peer!\n"));
610 GNUNET_CONFIGURATION_destroy (d->cfg);
611 GNUNET_free (d->cfgfile);
612 GNUNET_free_non_null (d->hostname);
613 GNUNET_free_non_null (d->username);
617 d->server = GNUNET_CORE_connect (d->cfg, 1,
622 NULL, GNUNET_NO, no_handlers);
624 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
628 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
631 if (d->server != NULL)
632 GNUNET_CORE_disconnect(d->server);
634 GNUNET_TRANSPORT_disconnect(d->th);
642 _("Unable to get HELLO for peer!\n"));
643 GNUNET_CONFIGURATION_destroy (d->cfg);
644 GNUNET_free (d->cfgfile);
645 GNUNET_free_non_null (d->hostname);
646 GNUNET_free_non_null (d->username);
650 if (d->hello != NULL)
652 GNUNET_assert(d->task == GNUNET_SCHEDULER_NO_TASK);
654 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
660 case SP_SHUTDOWN_START:
661 /* confirm copying complete */
662 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
664 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
667 if (NULL != d->dead_cb)
668 d->dead_cb (d->dead_cb_cls,
670 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
673 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello,
675 GNUNET_TRANSPORT_disconnect (d->th);
678 GNUNET_CONFIGURATION_destroy (d->cfg);
679 GNUNET_free (d->cfgfile);
680 GNUNET_free_non_null (d->hello);
681 GNUNET_free_non_null (d->hostname);
682 GNUNET_free_non_null (d->username);
683 GNUNET_free_non_null (d->shortname);
684 GNUNET_free_non_null (d->proc);
691 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
695 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
697 if (NULL != d->dead_cb)
698 d->dead_cb (d->dead_cb_cls,
700 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
703 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
704 GNUNET_TRANSPORT_disconnect (d->th);
707 if (d->server != NULL)
709 GNUNET_CORE_disconnect (d->server);
712 GNUNET_CONFIGURATION_destroy (d->cfg);
713 GNUNET_free (d->cfgfile);
714 GNUNET_free_non_null (d->hello);
715 GNUNET_free_non_null (d->hostname);
716 GNUNET_free_non_null (d->username);
717 GNUNET_free_non_null (d->shortname);
718 GNUNET_free_non_null (d->proc);
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
726 if (d->server != NULL)
728 GNUNET_CORE_disconnect (d->server);
734 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
735 GNUNET_TRANSPORT_disconnect (d->th);
738 /* state clean up and notifications */
739 if (d->churn == GNUNET_NO)
741 GNUNET_CONFIGURATION_destroy (d->cfg);
742 GNUNET_free (d->cfgfile);
743 GNUNET_free_non_null (d->hostname);
744 GNUNET_free_non_null (d->username);
747 GNUNET_free_non_null (d->hello);
749 GNUNET_free_non_null (d->shortname);
750 GNUNET_free_non_null (d->proc);
753 if (NULL != d->dead_cb)
754 d->dead_cb (d->dead_cb_cls, NULL);
756 if (d->churn == GNUNET_NO)
760 case SP_CONFIG_UPDATE:
761 /* confirm copying complete */
762 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
764 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
771 d->cfg, d, _("`scp' does not seem to terminate.\n"));
776 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
780 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
782 if (NULL != d->update_cb)
783 d->update_cb (d->update_cb_cls,
784 _("`scp' did not complete cleanly.\n"));
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Successfully copied configuration file.\n");
791 if (NULL != d->update_cb)
792 d->update_cb (d->update_cb_cls, NULL);
793 d->phase = SP_START_DONE;
799 * Continues GNUnet daemon startup when user wanted to be notified
800 * once a hostkey was generated (for creating friends files, blacklists,
803 * @param daemon the daemon to finish starting
806 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
808 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
809 daemon->phase = SP_TOPOLOGY_SETUP;
813 * Check whether the given daemon is running.
815 * @param daemon the daemon to check
817 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
818 * daemon is down, GNUNET_SYSERR on error.
821 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
824 return GNUNET_SYSERR;
826 if (daemon->running == GNUNET_YES)
833 * Start a peer that has previously been stopped using the daemon_stop
834 * call (and files weren't deleted and the allow restart flag)
836 * @param daemon the daemon to start (has been previously stopped)
837 * @param timeout how long to wait for restart
838 * @param cb the callback for notification when the peer is running
839 * @param cb_cls closure for the callback
842 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
843 struct GNUNET_TIME_Relative timeout,
844 GNUNET_TESTING_NotifyDaemonRunning cb,
847 if (daemon->running == GNUNET_YES)
849 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
850 "Daemon already running, can't restart!");
855 daemon->cb_cls = cb_cls;
856 daemon->phase = SP_TOPOLOGY_SETUP;
857 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
859 GNUNET_SCHEDULER_add_continuation (&start_fsm,
861 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
865 * Starts a GNUnet daemon. GNUnet must be installed on the target
866 * system and available in the PATH. The machine must furthermore be
867 * reachable via "ssh" (unless the hostname is "NULL") without the
868 * need to enter a password.
870 * @param cfg configuration to use
871 * @param timeout how long to wait starting up peers
872 * @param hostname name of the machine where to run GNUnet
873 * (use NULL for localhost).
874 * @param ssh_username ssh username to use when connecting to hostname
875 * @param sshport port to pass to ssh process when connecting to hostname
876 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
877 * @param hostkey_callback function to call once the hostkey has been
878 * generated for this peer, but it hasn't yet been started
879 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
880 * @param hostkey_cls closure for hostkey callback
881 * @param cb function to call once peer is up, or failed to start
882 * @param cb_cls closure for cb
883 * @return handle to the daemon (actual start will be completed asynchronously)
885 struct GNUNET_TESTING_Daemon *
886 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
887 struct GNUNET_TIME_Relative timeout,
888 const char *hostname,
889 const char *ssh_username,
892 GNUNET_TESTING_NotifyHostkeyCreated
893 hostkey_callback, void *hostkey_cls,
894 GNUNET_TESTING_NotifyDaemonRunning cb,
897 struct GNUNET_TESTING_Daemon *ret;
901 char *baseservicehome;
904 char *temp_file_name;
905 struct GNUNET_DISK_FileHandle *fn;
907 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
908 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
911 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
914 ret->ssh_port_str = NULL;
916 /* Find service home and base service home directories, create it if it doesn't exist */
917 GNUNET_assert(GNUNET_OK ==
918 GNUNET_CONFIGURATION_get_value_string (cfg,
923 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
924 GNUNET_asprintf(&temp_file_name, "%s/gnunet-testing-config", servicehome);
925 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
926 GNUNET_free(temp_file_name);
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "Setting up peer with configuration file `%s'.\n",
932 if (NULL == ret->cfgfile)
934 GNUNET_free_non_null (ret->ssh_port_str);
935 GNUNET_free_non_null (ret->hostname);
939 ret->hostkey_callback = hostkey_callback;
940 ret->hostkey_cls = hostkey_cls;
942 ret->cb_cls = cb_cls;
943 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
944 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
945 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
947 "DEFAULTCONFIG", ret->cfgfile);
949 /* Write hostkey to file, if we were given one */
953 GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome);
955 GNUNET_DISK_file_open (hostkeyfile,
956 GNUNET_DISK_OPEN_READWRITE
957 | GNUNET_DISK_OPEN_CREATE,
958 GNUNET_DISK_PERM_USER_READ |
959 GNUNET_DISK_PERM_USER_WRITE);
960 GNUNET_assert(fn != NULL);
961 GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE));
962 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn));
965 /* write configuration to temporary file */
966 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
968 if (0 != UNLINK (ret->cfgfile))
969 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
970 "unlink", ret->cfgfile);
971 GNUNET_CONFIGURATION_destroy (ret->cfg);
972 GNUNET_free_non_null (ret->hostname);
973 GNUNET_free (ret->cfgfile);
977 if (ssh_username != NULL)
978 username = GNUNET_strdup (ssh_username);
979 if ((ssh_username == NULL) && (GNUNET_OK !=
980 GNUNET_CONFIGURATION_get_value_string (cfg,
985 if (NULL != getenv ("USER"))
986 username = GNUNET_strdup (getenv ("USER"));
990 ret->username = username;
992 /* copy directory to remote host */
993 if (NULL != hostname)
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "Copying configuration directory to host `%s'.\n", hostname);
999 baseservicehome = GNUNET_strdup(servicehome);
1000 /* Remove trailing /'s */
1001 while (baseservicehome[strlen(baseservicehome) - 1] == '/')
1002 baseservicehome[strlen(baseservicehome) - 1] = '\0';
1003 /* Find next directory /, jump one ahead */
1004 slash = strrchr(baseservicehome, '/');
1008 ret->phase = SP_COPYING;
1009 if (NULL != username)
1010 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1012 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1014 if (ret->ssh_port_str == NULL)
1016 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1020 servicehome, arg, NULL);
1022 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", servicehome, arg);
1027 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
1028 "scp", "-r", "-P", ret->ssh_port_str,
1032 servicehome, arg, NULL);
1035 if (NULL == ret->proc)
1037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1039 ("Could not start `%s' process to copy configuration directory.\n"),
1041 if (0 != UNLINK (ret->cfgfile))
1042 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1043 "unlink", ret->cfgfile);
1044 GNUNET_CONFIGURATION_destroy (ret->cfg);
1045 GNUNET_free_non_null (ret->hostname);
1046 GNUNET_free_non_null (ret->username);
1047 GNUNET_free (ret->cfgfile);
1049 if ((hostkey != NULL) && (0 != UNLINK(hostkeyfile)))
1050 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1051 "unlink", hostkeyfile);
1052 GNUNET_free_non_null(hostkeyfile);
1053 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1054 GNUNET_free(servicehome);
1058 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1060 GNUNET_free_non_null(hostkeyfile);
1061 GNUNET_free(servicehome);
1065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1066 "No need to copy configuration file since we are running locally.\n");
1068 ret->phase = SP_COPIED;
1069 GNUNET_SCHEDULER_add_continuation (&start_fsm,
1071 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1072 GNUNET_free_non_null(hostkeyfile);
1073 GNUNET_free(servicehome);
1079 * Restart (stop and start) a GNUnet daemon.
1081 * @param d the daemon that should be restarted
1082 * @param cb function called once the daemon is (re)started
1083 * @param cb_cls closure for cb
1086 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1087 GNUNET_TESTING_NotifyDaemonRunning cb,
1096 d->dead = GNUNET_YES;
1103 if (d->phase == SP_CONFIG_UPDATE)
1105 GNUNET_SCHEDULER_cancel (d->task);
1106 d->phase = SP_START_DONE;
1108 if (d->server != NULL)
1110 GNUNET_CORE_disconnect (d->server);
1116 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1117 GNUNET_TRANSPORT_disconnect (d->th);
1120 /* state clean up and notifications */
1121 GNUNET_free_non_null (d->hello);
1124 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1125 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1128 d->phase = SP_START_ARMING;
1130 /* Check if this is a local or remote process */
1131 if (NULL != d->hostname)
1134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1135 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1136 d->cfgfile, d->hostname);
1139 if (d->username != NULL)
1140 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1142 arg = GNUNET_strdup (d->hostname);
1144 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1152 "-c", d->cfgfile, "-e", "-r", NULL);
1153 /* Use -r to restart arm and all services */
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161 "Stopping gnunet-arm with config `%s' locally.\n",
1164 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1169 "-c", d->cfgfile, "-e", "-r", NULL);
1172 GNUNET_free_non_null (del_arg);
1174 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1181 * Stops a GNUnet daemon.
1183 * @param d the daemon that should be stopped
1184 * @param timeout how long to wait for process for shutdown to complete
1185 * @param cb function called once the daemon was stopped
1186 * @param cb_cls closure for cb
1187 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1189 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1190 * GNUNET_NO to kill off and clean up for good
1193 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1194 struct GNUNET_TIME_Relative timeout,
1195 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1196 int delete_files, int allow_restart)
1201 d->dead_cb_cls = cb_cls;
1206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1207 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1209 d->dead = GNUNET_YES;
1213 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1215 /* Free what was left from churning! */
1216 GNUNET_assert (d->cfg != NULL);
1217 GNUNET_CONFIGURATION_destroy (d->cfg);
1218 if (delete_files == GNUNET_YES)
1220 if (0 != UNLINK (d->cfgfile))
1222 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1225 GNUNET_free (d->cfgfile);
1226 GNUNET_free_non_null (d->hostname);
1227 GNUNET_free_non_null (d->username);
1228 if (NULL != d->dead_cb)
1229 d->dead_cb (d->dead_cb_cls, NULL);
1235 if (delete_files == GNUNET_YES)
1237 GNUNET_asprintf (&del_arg, "-d");
1240 if (d->phase == SP_CONFIG_UPDATE)
1242 GNUNET_SCHEDULER_cancel (d->task);
1243 d->phase = SP_START_DONE;
1245 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1246 if (d->server != NULL)
1248 GNUNET_CORE_disconnect (d->server);
1252 /* shutdown ARM process (will terminate others) */
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1257 d->phase = SP_SHUTDOWN_START;
1258 d->running = GNUNET_NO;
1259 if (allow_restart == GNUNET_YES)
1260 d->churn = GNUNET_YES;
1263 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1264 GNUNET_TRANSPORT_disconnect (d->th);
1267 /* Check if this is a local or remote process */
1268 if (NULL != d->hostname)
1271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1272 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1273 d->cfgfile, d->hostname);
1276 if (d->username != NULL)
1277 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1279 arg = GNUNET_strdup (d->hostname);
1281 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1289 "-c", d->cfgfile, "-e", "-q",
1291 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1292 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1293 arg, "gnunet-arm", d->cfgfile, del_arg);
1294 /* Use -e to end arm, and -d to remove temp files */
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "Stopping gnunet-arm with config `%s' locally.\n",
1304 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1309 "-c", d->cfgfile, "-e", "-q",
1313 GNUNET_free_non_null (del_arg);
1314 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1315 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1320 * Changes the configuration of a GNUnet daemon.
1322 * @param d the daemon that should be modified
1323 * @param cfg the new configuration for the daemon
1324 * @param cb function called once the configuration was changed
1325 * @param cb_cls closure for cb
1328 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1329 struct GNUNET_CONFIGURATION_Handle *cfg,
1330 GNUNET_TESTING_NotifyCompletion cb,
1335 if (d->phase != SP_START_DONE)
1340 ("Peer not yet running, can not change configuration at this point."));
1344 /* 1) write configuration to temporary file */
1345 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1348 cb (cb_cls, _("Failed to write new configuration to disk."));
1352 /* 2) copy file to remote host (if necessary) */
1353 if (NULL == d->hostname)
1355 /* signal success */
1361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1362 "Copying updated configuration file to remote host `%s'.\n",
1365 d->phase = SP_CONFIG_UPDATE;
1366 if (NULL != d->username)
1367 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1369 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1370 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1374 d->cfgfile, arg, NULL);
1376 if (NULL == d->proc)
1378 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1380 ("Could not start `%s' process to copy configuration file.\n"),
1383 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1384 d->phase = SP_START_DONE;
1388 d->update_cb_cls = cb_cls;
1390 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1396 * Data kept for each pair of peers that we try
1399 struct ConnectContext
1402 * Testing handle to the first daemon.
1404 struct GNUNET_TESTING_Daemon *d1;
1407 * Handle to core of first daemon (to check connect)
1409 struct GNUNET_CORE_Handle *d1core;
1412 * Have we actually connected to the core of the first daemon yet?
1417 * Testing handle to the second daemon.
1419 struct GNUNET_TESTING_Daemon *d2;
1422 * Handler for the request to core to connect to this peer.
1424 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1427 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1429 struct GNUNET_TRANSPORT_Handle *d1th;
1432 * Function to call once we are done (or have timed out).
1434 GNUNET_TESTING_NotifyConnection cb;
1442 * The relative timeout from whence this connect attempt was
1443 * started. Allows for reconnect attempts.
1445 struct GNUNET_TIME_Relative relative_timeout;
1448 * Maximum number of connect attempts, will retry connection
1449 * this number of times on failures.
1451 unsigned int connect_attempts;
1454 * Hello timeout task
1456 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1459 * Connect timeout task
1461 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1464 * When should this operation be complete (or we must trigger
1467 struct GNUNET_TIME_Relative timeout_hello;
1470 * Was the connection attempt successful?
1475 * When connecting, do we need to send the HELLO?
1480 * The distance between the two connected peers
1486 /** Forward declaration **/
1488 reattempt_daemons_connect (void *cls,
1489 const struct GNUNET_SCHEDULER_TaskContext *tc);
1493 * Notify callback about success or failure of the attempt
1494 * to connect the two peers
1496 * @param cls our "struct ConnectContext" (freed)
1497 * @param tc reason tells us if we succeeded or failed
1500 notify_connect_result (void *cls,
1501 const struct GNUNET_SCHEDULER_TaskContext *tc)
1503 struct ConnectContext *ctx = cls;
1504 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1505 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1507 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1508 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1511 if (ctx->connect_request_handle != NULL)
1513 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1514 ctx->connect_request_handle = NULL;
1517 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1519 if (ctx->d1th != NULL)
1520 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1522 if (ctx->d1core != NULL)
1523 GNUNET_CORE_disconnect (ctx->d1core);
1525 if (ctx->d2core != NULL)
1526 GNUNET_CORE_disconnect (ctx->d2core);
1534 if (ctx->d1th != NULL)
1535 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1537 if (ctx->d1core != NULL)
1538 GNUNET_CORE_disconnect (ctx->d1core);
1541 if (ctx->connected == GNUNET_YES)
1543 if (ctx->cb != NULL)
1545 ctx->cb (ctx->cb_cls,
1549 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1552 else if (ctx->connect_attempts > 0)
1554 ctx->d1core_ready = GNUNET_NO;
1556 if (ctx->d2core != NULL)
1558 GNUNET_CORE_disconnect (ctx->d2core);
1562 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1567 if (ctx->cb != NULL)
1569 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1570 ctx->d2->cfg, ctx->d1, ctx->d2,
1571 _("Peers failed to connect"));
1580 * Success, connection is up. Signal client our success.
1582 * @param cls our "struct ConnectContext"
1583 * @param peer identity of the peer that has connected
1584 * @param atsi performance information
1588 connect_notify (void *cls,
1589 const struct GNUNET_PeerIdentity *peer,
1590 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1592 struct ConnectContext *ctx = cls;
1595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1596 "Connected peer %s to peer %s\n",
1597 ctx->d1->shortname, GNUNET_i2s(peer));
1600 if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1603 ctx->connected = GNUNET_YES;
1604 ctx->distance = 0; /* FIXME: distance */
1605 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1607 GNUNET_SCHEDULER_cancel(ctx->hello_send_task);
1608 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1610 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1611 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1618 * Success, connection is up. Signal client our success.
1620 * @param cls our "struct ConnectContext"
1621 * @param peer identity of the peer that has connected
1622 * @param atsi performance information
1626 connect_notify_core2 (void *cls,
1627 const struct GNUNET_PeerIdentity *peer,
1628 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1630 struct ConnectContext *ctx = cls;
1632 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
1634 ctx->connected = GNUNET_YES;
1635 ctx->distance = 0; /* FIXME: distance */
1636 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1637 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1645 * Task called once a core connect request has been transmitted.
1647 * @param cls struct ConnectContext
1648 * @param success was the request successful?
1651 core_connect_request_cont (void *cls,
1654 struct ConnectContext *ctx = cls;
1656 ctx->connect_request_handle = NULL;
1660 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1662 struct ConnectContext *ctx = cls;
1663 struct GNUNET_MessageHeader *hello;
1664 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1665 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1667 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
1668 && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
1670 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1671 GNUNET_assert (hello != NULL);
1672 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1673 GNUNET_assert (ctx->d1core != NULL);
1674 ctx->connect_request_handle =
1675 GNUNET_CORE_peer_request_connect (ctx->d1core,
1676 ctx->relative_timeout,
1678 &core_connect_request_cont, ctx);
1681 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1682 "Sending connect request to CORE of %s for peer %s\n",
1683 GNUNET_i2s (&ctx->d1->id),
1684 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1686 ctx->timeout_hello =
1687 GNUNET_TIME_relative_add (ctx->timeout_hello,
1688 GNUNET_TIME_relative_multiply
1689 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1691 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello,
1696 * Notify of a successful connection to the core service.
1698 * @param cls a ConnectContext
1699 * @param server handle to the core service
1700 * @param my_identity the peer identity of this peer
1701 * @param publicKey the public key of the peer
1704 core_init_notify (void *cls,
1705 struct GNUNET_CORE_Handle * server,
1706 const struct GNUNET_PeerIdentity *
1709 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1712 struct ConnectContext *connect_ctx = cls;
1713 connect_ctx->d1core_ready = GNUNET_YES;
1715 if (connect_ctx->send_hello == GNUNET_NO)
1717 connect_ctx->connect_request_handle =
1718 GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
1719 connect_ctx->relative_timeout,
1720 &connect_ctx->d2->id,
1721 &core_connect_request_cont, connect_ctx);
1722 GNUNET_assert(connect_ctx->connect_request_handle != NULL);
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1725 "Sending connect request to CORE of %s for peer %s\n",
1726 connect_ctx->d1->shortname,
1727 connect_ctx->d2->shortname);
1735 reattempt_daemons_connect (void *cls,
1736 const struct GNUNET_SCHEDULER_TaskContext *tc)
1738 struct ConnectContext *ctx = cls;
1739 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1744 #if DEBUG_TESTING_RECONNECT
1745 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1746 "re-attempting connect of peer %s to peer %s\n",
1747 ctx->d1->shortname, ctx->d2->shortname);
1749 ctx->connect_attempts--;
1750 GNUNET_assert (ctx->d1core == NULL);
1751 ctx->d1core_ready = GNUNET_NO;
1752 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1755 &connect_notify, NULL, NULL,
1757 NULL, GNUNET_NO, no_handlers);
1758 if (ctx->d1core == NULL)
1760 if (NULL != ctx->cb)
1761 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1762 ctx->d2->cfg, ctx->d1, ctx->d2,
1763 _("Failed to connect to core service of first peer!\n"));
1768 if (ctx->send_hello == GNUNET_YES)
1770 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1772 ctx->d1, NULL, NULL, NULL);
1773 if (ctx->d1th == NULL)
1775 GNUNET_CORE_disconnect (ctx->d1core);
1777 if (NULL != ctx->cb)
1778 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1779 ctx->d2->cfg, ctx->d1, ctx->d2,
1780 _("Failed to connect to transport service!\n"));
1783 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1787 ctx->connect_request_handle =
1788 GNUNET_CORE_peer_request_connect (ctx->d1core,
1789 ctx->relative_timeout,
1791 &core_connect_request_cont, ctx);
1794 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1795 ¬ify_connect_result, ctx);
1799 * Iterator for currently known peers, to ensure
1800 * that we don't try to send duplicate connect
1803 * @param cls our "struct ConnectContext"
1804 * @param peer identity of the peer that has connected,
1805 * NULL when iteration has finished
1806 * @param atsi performance information
1810 core_initial_iteration (void *cls,
1811 const struct GNUNET_PeerIdentity *peer,
1812 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1814 struct ConnectContext *ctx = cls;
1816 if ((peer != NULL) &&
1817 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
1819 ctx->connected = GNUNET_YES;
1820 ctx->distance = 0; /* FIXME: distance */
1823 else if (peer == NULL) /* End of iteration over peers */
1825 if (ctx->connected == GNUNET_YES)
1827 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1832 /* Peer not already connected, need to schedule connect request! */
1833 if (ctx->d1core == NULL)
1836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1837 "Peers are NOT connected, connecting to core!\n");
1839 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1842 &connect_notify, NULL, NULL,
1844 NULL, GNUNET_NO, no_handlers);
1847 if (ctx->d1core == NULL)
1850 if (NULL != ctx->cb)
1851 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1852 _("Failed to connect to core service of first peer!\n"));
1856 if (ctx->send_hello == GNUNET_YES)
1858 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1859 &ctx->d1->id, ctx->d1, NULL, NULL, NULL);
1860 if (ctx->d1th == NULL)
1862 GNUNET_CORE_disconnect (ctx->d1core);
1864 if (NULL != ctx->cb)
1865 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1866 _("Failed to connect to transport service!\n"));
1869 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1873 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1874 ¬ify_connect_result, ctx);
1880 * Establish a connection between two GNUnet daemons.
1882 * @param d1 handle for the first daemon
1883 * @param d2 handle for the second daemon
1884 * @param timeout how long is the connection attempt
1886 * @param max_connect_attempts how many times should we try to reconnect
1888 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
1889 * the HELLO has already been exchanged
1890 * @param cb function to call at the end
1891 * @param cb_cls closure for cb
1894 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1895 struct GNUNET_TESTING_Daemon *d2,
1896 struct GNUNET_TIME_Relative timeout,
1897 unsigned int max_connect_attempts,
1899 GNUNET_TESTING_NotifyConnection cb,
1902 struct ConnectContext *ctx;
1904 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1907 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1908 _("Peers are not fully running yet, can not connect!\n"));
1912 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1915 ctx->timeout_hello =
1916 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
1917 ctx->relative_timeout = GNUNET_TIME_relative_divide(timeout, max_connect_attempts);
1919 ctx->cb_cls = cb_cls;
1920 ctx->connect_attempts = max_connect_attempts;
1921 ctx->connected = GNUNET_NO;
1922 ctx->send_hello = send_hello;
1924 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1925 "Asked to connect peer %s to peer %s\n",
1926 d1->shortname, d2->shortname);
1929 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
1930 GNUNET_assert(GNUNET_OK == GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id, &core_initial_iteration, ctx));
1931 /*GNUNET_assert(GNUNET_OK == GNUNET_CORE_iterate_peers (ctx->d1->cfg, &core_initial_iteration, ctx));*/
1934 /* end of testing.c */