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_NO
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;
72 if (daemon->server != NULL)
75 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
76 "Received `%s' from transport service of `%4s', disconnecting core!\n",
77 "HELLO", GNUNET_i2s (&daemon->id));
79 GNUNET_CORE_disconnect (daemon->server);
80 daemon->server = NULL;
83 GNUNET_assert (message != NULL);
84 msize = ntohs (message->size);
89 if (daemon->th != NULL)
91 GNUNET_TRANSPORT_get_hello_cancel (daemon->th, &process_hello, daemon);
94 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
95 "Received `%s' from transport service of `%4s'\n",
96 "HELLO", GNUNET_i2s (&daemon->id));
99 GNUNET_free_non_null (daemon->hello);
100 daemon->hello = GNUNET_malloc (msize);
101 memcpy (daemon->hello, message, msize);
103 if (daemon->th != NULL)
105 GNUNET_TRANSPORT_disconnect (daemon->th);
112 * Function called after GNUNET_CORE_connect has succeeded
113 * (or failed for good). Note that the private key of the
114 * peer is intentionally not exposed here; if you need it,
115 * your process should try to read the private key file
116 * directly (which should work if you are authorized...).
119 * @param server handle to the server, NULL if we failed
120 * @param my_identity ID of this peer, NULL if we failed
121 * @param publicKey public key of this peer, NULL if we failed
124 testing_init (void *cls,
125 struct GNUNET_CORE_Handle *server,
126 const struct GNUNET_PeerIdentity *my_identity,
127 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
129 struct GNUNET_TESTING_Daemon *d = cls;
130 GNUNET_TESTING_NotifyDaemonRunning cb;
132 GNUNET_assert (d->phase == SP_START_CORE);
133 d->phase = SP_START_DONE;
139 if (GNUNET_YES == d->dead)
140 GNUNET_TESTING_daemon_stop (d,
141 GNUNET_TIME_absolute_get_remaining
142 (d->max_timeout), d->dead_cb,
143 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
145 cb (d->cb_cls, NULL, d->cfg, d,
146 _("Failed to connect to core service\n"));
150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
151 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
153 d->id = *my_identity;
154 d->shortname = strdup (GNUNET_i2s (my_identity));
156 d->running = GNUNET_YES;
158 if (NULL != cb) /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
159 cb (d->cb_cls, my_identity, d->cfg, d, NULL);
161 if (GNUNET_NO == d->running)
164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
165 "Peer is dead (d->running == GNUNET_NO)\n");
170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
171 "Successfully started peer `%4s', connecting to transport service.\n",
172 GNUNET_i2s (my_identity));
175 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
178 if (GNUNET_YES == d->dead)
179 GNUNET_TESTING_daemon_stop (d,
180 GNUNET_TIME_absolute_get_remaining
181 (d->max_timeout), d->dead_cb,
182 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
183 else if (NULL != d->cb)
184 d->cb (d->cb_cls, &d->id, d->cfg, d,
185 _("Failed to connect to transport service!\n"));
189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
190 "Connected to transport service `%s', getting HELLO\n",
191 GNUNET_i2s (my_identity));
194 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
199 * Finite-state machine for starting GNUnet.
201 * @param cls our "struct GNUNET_TESTING_Daemon"
205 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
207 struct GNUNET_TESTING_Daemon *d = cls;
208 GNUNET_TESTING_NotifyDaemonRunning cb;
209 enum GNUNET_OS_ProcessStatusType type;
215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
216 "Peer FSM is in phase %u.\n", d->phase);
219 d->task = GNUNET_SCHEDULER_NO_TASK;
223 /* confirm copying complete */
224 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
226 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
236 ("`scp' does not seem to terminate (timeout copying config).\n"));
241 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
245 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
251 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
256 "Successfully copied configuration file.\n");
258 d->phase = SP_COPIED;
261 /* Start create hostkey process */
262 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
263 if (d->pipe_stdout == NULL)
272 (NULL == d->hostname)
273 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
274 : _("Failed to create pipe for `ssh' process.\n"));
277 if (NULL == d->hostname)
280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
281 "Starting `%s', with command `%s %s %s %s'.\n",
282 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
286 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
287 "gnunet-peerinfo", "-c", d->cfgfile,
289 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
290 GNUNET_DISK_PIPE_END_WRITE);
294 if (d->username != NULL)
295 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
297 dst = GNUNET_strdup (d->hostname);
300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
301 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
302 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
305 if (d->ssh_port_str == NULL)
307 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
314 "-c", d->cfgfile, "-sq",
319 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
320 "ssh", "-p", d->ssh_port_str,
326 "-c", d->cfgfile, "-sq",
329 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
330 GNUNET_DISK_PIPE_END_WRITE);
335 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
336 _("Could not start `%s' process to create hostkey.\n"),
337 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
345 (NULL == d->hostname)
346 ? _("Failed to start `gnunet-peerinfo' process.\n")
347 : _("Failed to start `ssh' process.\n"));
348 GNUNET_DISK_pipe_close (d->pipe_stdout);
352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
353 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
355 d->phase = SP_HOSTKEY_CREATE;
358 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
360 GNUNET_DISK_pipe_handle
362 GNUNET_DISK_PIPE_END_READ),
365 case SP_HOSTKEY_CREATE:
367 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
368 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
369 &d->hostkeybuf[d->hostkeybufpos],
370 sizeof (d->hostkeybuf) - d->hostkeybufpos);
372 d->hostkeybufpos += bytes_read;
374 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
379 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
381 GNUNET_DISK_pipe_handle
383 GNUNET_DISK_PIPE_END_READ),
387 d->hostkeybuf[103] = '\0';
389 if ((bytes_read < 0) ||
390 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
395 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
396 _("Error reading from gnunet-peerinfo: %s\n"),
399 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
400 _("Malformed output from gnunet-peerinfo!\n"));
403 GNUNET_DISK_pipe_close (d->pipe_stdout);
404 d->pipe_stdout = NULL;
405 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
406 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
407 GNUNET_OS_process_close (d->proc);
410 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
413 GNUNET_DISK_pipe_close (d->pipe_stdout);
414 d->pipe_stdout = NULL;
415 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
416 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
417 GNUNET_OS_process_close (d->proc);
420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
422 if (d->hostkey_callback != NULL)
424 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
425 d->phase = SP_HOSTKEY_CREATED;
429 d->phase = SP_TOPOLOGY_SETUP;
432 case SP_HOSTKEY_CREATED:
433 /* wait for topology finished */
434 if ((GNUNET_YES == d->dead)
435 || (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
443 d->cfg, d, _("`Failed while waiting for topology setup!\n"));
448 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
451 case SP_TOPOLOGY_SETUP:
452 /* start GNUnet on remote host */
453 if (NULL == d->hostname)
456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
457 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
458 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
459 "-L", "DEBUG", "-s");
461 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
462 "gnunet-arm", "-c", d->cfgfile,
470 if (d->username != NULL)
471 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
473 dst = GNUNET_strdup (d->hostname);
476 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
477 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
478 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
479 d->cfgfile, "-L", "DEBUG", "-s", "-q");
481 if (d->ssh_port_str == NULL)
483 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
491 "-c", d->cfgfile, "-s", "-q",
497 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
498 "ssh", "-p", d->ssh_port_str,
506 "-c", d->cfgfile, "-s", "-q",
513 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
514 _("Could not start `%s' process to start GNUnet.\n"),
515 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
523 (NULL == d->hostname)
524 ? _("Failed to start `gnunet-arm' process.\n")
525 : _("Failed to start `ssh' process.\n"));
529 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
530 "Started `%s', waiting for `%s' to be up.\n",
531 "gnunet-arm", "gnunet-service-core");
533 d->phase = SP_START_ARMING;
535 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
538 case SP_START_ARMING:
539 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
541 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
551 (NULL == d->hostname)
552 ? _("`gnunet-arm' does not seem to terminate.\n")
553 : _("`ssh' does not seem to terminate.\n"));
554 GNUNET_free(d->proc);
559 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
564 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
565 "Successfully started `%s'.\n", "gnunet-arm");
567 GNUNET_free(d->proc);
568 d->phase = SP_START_CORE;
570 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
571 "Calling CORE_connect\n");
573 d->server = GNUNET_CORE_connect (d->cfg, 1,
574 #if NO_MORE_TIMEOUT_FIXME
581 NULL, GNUNET_NO, no_handlers);
589 case SP_SHUTDOWN_START:
590 /* confirm copying complete */
591 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
593 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
596 if (NULL != d->dead_cb)
597 d->dead_cb (d->dead_cb_cls,
599 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
602 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello,
604 GNUNET_TRANSPORT_disconnect (d->th);
607 GNUNET_CONFIGURATION_destroy (d->cfg);
608 GNUNET_free (d->cfgfile);
609 GNUNET_free_non_null (d->hello);
610 GNUNET_free_non_null (d->hostname);
611 GNUNET_free_non_null (d->username);
612 GNUNET_free_non_null (d->shortname);
613 GNUNET_free_non_null (d->proc);
620 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
624 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
626 if (NULL != d->dead_cb)
627 d->dead_cb (d->dead_cb_cls,
629 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
632 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
633 GNUNET_TRANSPORT_disconnect (d->th);
636 if (d->server != NULL)
638 GNUNET_CORE_disconnect (d->server);
641 GNUNET_CONFIGURATION_destroy (d->cfg);
642 GNUNET_free (d->cfgfile);
643 GNUNET_free_non_null (d->hello);
644 GNUNET_free_non_null (d->hostname);
645 GNUNET_free_non_null (d->username);
646 GNUNET_free_non_null (d->shortname);
647 GNUNET_free_non_null (d->proc);
653 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
655 if (d->server != NULL)
657 GNUNET_CORE_disconnect (d->server);
663 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
664 GNUNET_TRANSPORT_disconnect (d->th);
667 /* state clean up and notifications */
668 if (d->churn == GNUNET_NO)
670 GNUNET_CONFIGURATION_destroy (d->cfg);
671 GNUNET_free (d->cfgfile);
672 GNUNET_free_non_null (d->hostname);
673 GNUNET_free_non_null (d->username);
676 GNUNET_free_non_null (d->hello);
678 GNUNET_free_non_null (d->shortname);
679 GNUNET_free_non_null (d->proc);
682 if (NULL != d->dead_cb)
683 d->dead_cb (d->dead_cb_cls, NULL);
685 if (d->churn == GNUNET_NO)
689 case SP_CONFIG_UPDATE:
690 /* confirm copying complete */
691 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
693 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
700 d->cfg, d, _("`scp' does not seem to terminate.\n"));
705 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
709 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
711 if (NULL != d->update_cb)
712 d->update_cb (d->update_cb_cls,
713 _("`scp' did not complete cleanly.\n"));
717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
718 "Successfully copied configuration file.\n");
720 if (NULL != d->update_cb)
721 d->update_cb (d->update_cb_cls, NULL);
722 d->phase = SP_START_DONE;
728 * Continues GNUnet daemon startup when user wanted to be notified
729 * once a hostkey was generated (for creating friends files, blacklists,
732 * @param daemon the daemon to finish starting
735 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
737 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
738 daemon->phase = SP_TOPOLOGY_SETUP;
742 * Check whether the given daemon is running.
744 * @param daemon the daemon to check
746 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
747 * daemon is down, GNUNET_SYSERR on error.
750 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
753 return GNUNET_SYSERR;
755 if (daemon->running == GNUNET_YES)
762 * Start a peer that has previously been stopped using the daemon_stop
763 * call (and files weren't deleted and the allow restart flag)
765 * @param daemon the daemon to start (has been previously stopped)
766 * @param timeout how long to wait for restart
767 * @param cb the callback for notification when the peer is running
768 * @param cb_cls closure for the callback
771 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
772 struct GNUNET_TIME_Relative timeout,
773 GNUNET_TESTING_NotifyDaemonRunning cb,
776 if (daemon->running == GNUNET_YES)
778 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
779 "Daemon already running, can't restart!");
784 daemon->cb_cls = cb_cls;
785 daemon->phase = SP_TOPOLOGY_SETUP;
786 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
788 GNUNET_SCHEDULER_add_continuation (&start_fsm,
790 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
794 * Starts a GNUnet daemon. GNUnet must be installed on the target
795 * system and available in the PATH. The machine must furthermore be
796 * reachable via "ssh" (unless the hostname is "NULL") without the
797 * need to enter a password.
799 * @param cfg configuration to use
800 * @param timeout how long to wait starting up peers
801 * @param hostname name of the machine where to run GNUnet
802 * (use NULL for localhost).
803 * @param ssh_username ssh username to use when connecting to hostname
804 * @param sshport port to pass to ssh process when connecting to hostname
805 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
806 * @param hostkey_callback function to call once the hostkey has been
807 * generated for this peer, but it hasn't yet been started
808 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
809 * @param hostkey_cls closure for hostkey callback
810 * @param cb function to call once peer is up, or failed to start
811 * @param cb_cls closure for cb
812 * @return handle to the daemon (actual start will be completed asynchronously)
814 struct GNUNET_TESTING_Daemon *
815 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
816 struct GNUNET_TIME_Relative timeout,
817 const char *hostname,
818 const char *ssh_username,
821 GNUNET_TESTING_NotifyHostkeyCreated
822 hostkey_callback, void *hostkey_cls,
823 GNUNET_TESTING_NotifyDaemonRunning cb,
826 struct GNUNET_TESTING_Daemon *ret;
830 char *baseservicehome;
833 char *temp_file_name;
834 struct GNUNET_DISK_FileHandle *fn;
836 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
837 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
840 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
843 ret->ssh_port_str = NULL;
845 /* Find service home and base service home directories, create it if it doesn't exist */
846 GNUNET_assert(GNUNET_OK ==
847 GNUNET_CONFIGURATION_get_value_string (cfg,
852 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
853 GNUNET_asprintf(&temp_file_name, "%s/gnunet-testing-config", servicehome);
854 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
855 GNUNET_free(temp_file_name);
857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
858 "Setting up peer with configuration file `%s'.\n",
861 if (NULL == ret->cfgfile)
863 GNUNET_free_non_null (ret->ssh_port_str);
864 GNUNET_free_non_null (ret->hostname);
868 ret->hostkey_callback = hostkey_callback;
869 ret->hostkey_cls = hostkey_cls;
871 ret->cb_cls = cb_cls;
872 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
873 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
874 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
876 "DEFAULTCONFIG", ret->cfgfile);
878 /* Write hostkey to file, if we were given one */
882 GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome);
884 GNUNET_DISK_file_open (hostkeyfile,
885 GNUNET_DISK_OPEN_READWRITE
886 | GNUNET_DISK_OPEN_CREATE,
887 GNUNET_DISK_PERM_USER_READ |
888 GNUNET_DISK_PERM_USER_WRITE);
889 GNUNET_assert(fn != NULL);
890 GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE));
891 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn));
894 /* write configuration to temporary file */
895 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
897 if (0 != UNLINK (ret->cfgfile))
898 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
899 "unlink", ret->cfgfile);
900 GNUNET_CONFIGURATION_destroy (ret->cfg);
901 GNUNET_free_non_null (ret->hostname);
902 GNUNET_free (ret->cfgfile);
906 if (ssh_username != NULL)
907 username = GNUNET_strdup (ssh_username);
908 if ((ssh_username == NULL) && (GNUNET_OK !=
909 GNUNET_CONFIGURATION_get_value_string (cfg,
914 if (NULL != getenv ("USER"))
915 username = GNUNET_strdup (getenv ("USER"));
919 ret->username = username;
921 /* copy directory to remote host */
922 if (NULL != hostname)
925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
926 "Copying configuration directory to host `%s'.\n", hostname);
928 baseservicehome = GNUNET_strdup(servicehome);
929 /* Remove trailing /'s */
930 while (baseservicehome[strlen(baseservicehome) - 1] == '/')
931 baseservicehome[strlen(baseservicehome) - 1] = '\0';
932 /* Find next directory /, jump one ahead */
933 slash = strrchr(baseservicehome, '/');
937 ret->phase = SP_COPYING;
938 if (NULL != username)
939 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
941 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
943 if (ret->ssh_port_str == NULL)
945 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
949 servicehome, arg, NULL);
951 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", servicehome, arg);
956 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
957 "scp", "-r", "-P", ret->ssh_port_str,
961 servicehome, arg, NULL);
964 if (NULL == ret->proc)
966 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
968 ("Could not start `%s' process to copy configuration directory.\n"),
970 if (0 != UNLINK (ret->cfgfile))
971 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
972 "unlink", ret->cfgfile);
973 GNUNET_CONFIGURATION_destroy (ret->cfg);
974 GNUNET_free_non_null (ret->hostname);
975 GNUNET_free_non_null (ret->username);
976 GNUNET_free (ret->cfgfile);
978 if ((hostkey != NULL) && (0 != UNLINK(hostkeyfile)))
979 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
980 "unlink", hostkeyfile);
981 GNUNET_free_non_null(hostkeyfile);
982 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
983 GNUNET_free(servicehome);
987 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
989 GNUNET_free_non_null(hostkeyfile);
990 GNUNET_free(servicehome);
994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
995 "No need to copy configuration file since we are running locally.\n");
997 ret->phase = SP_COPIED;
998 GNUNET_SCHEDULER_add_continuation (&start_fsm,
1000 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1001 GNUNET_free_non_null(hostkeyfile);
1002 GNUNET_free(servicehome);
1008 * Restart (stop and start) a GNUnet daemon.
1010 * @param d the daemon that should be restarted
1011 * @param cb function called once the daemon is (re)started
1012 * @param cb_cls closure for cb
1015 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1016 GNUNET_TESTING_NotifyDaemonRunning cb,
1025 d->dead = GNUNET_YES;
1032 if (d->phase == SP_CONFIG_UPDATE)
1034 GNUNET_SCHEDULER_cancel (d->task);
1035 d->phase = SP_START_DONE;
1037 if (d->server != NULL)
1039 GNUNET_CORE_disconnect (d->server);
1045 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1046 GNUNET_TRANSPORT_disconnect (d->th);
1049 /* state clean up and notifications */
1050 GNUNET_free_non_null (d->hello);
1053 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1054 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1057 d->phase = SP_START_ARMING;
1059 /* Check if this is a local or remote process */
1060 if (NULL != d->hostname)
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1065 d->cfgfile, d->hostname);
1068 if (d->username != NULL)
1069 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1071 arg = GNUNET_strdup (d->hostname);
1073 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1081 "-c", d->cfgfile, "-e", "-r", NULL);
1082 /* Use -r to restart arm and all services */
1089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1090 "Stopping gnunet-arm with config `%s' locally.\n",
1093 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1098 "-c", d->cfgfile, "-e", "-r", NULL);
1101 GNUNET_free_non_null (del_arg);
1103 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1110 * Stops a GNUnet daemon.
1112 * @param d the daemon that should be stopped
1113 * @param timeout how long to wait for process for shutdown to complete
1114 * @param cb function called once the daemon was stopped
1115 * @param cb_cls closure for cb
1116 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1118 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1119 * GNUNET_NO to kill off and clean up for good
1122 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1123 struct GNUNET_TIME_Relative timeout,
1124 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1125 int delete_files, int allow_restart)
1130 d->dead_cb_cls = cb_cls;
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1138 d->dead = GNUNET_YES;
1142 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1144 /* Free what was left from churning! */
1145 GNUNET_assert (d->cfg != NULL);
1146 GNUNET_CONFIGURATION_destroy (d->cfg);
1147 if (delete_files == GNUNET_YES)
1149 if (0 != UNLINK (d->cfgfile))
1151 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1154 GNUNET_free (d->cfgfile);
1155 GNUNET_free_non_null (d->hostname);
1156 GNUNET_free_non_null (d->username);
1157 if (NULL != d->dead_cb)
1158 d->dead_cb (d->dead_cb_cls, NULL);
1164 if (delete_files == GNUNET_YES)
1166 GNUNET_asprintf (&del_arg, "-d");
1169 if (d->phase == SP_CONFIG_UPDATE)
1171 GNUNET_SCHEDULER_cancel (d->task);
1172 d->phase = SP_START_DONE;
1174 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1175 if (d->server != NULL)
1177 GNUNET_CORE_disconnect (d->server);
1181 /* shutdown ARM process (will terminate others) */
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1186 d->phase = SP_SHUTDOWN_START;
1187 d->running = GNUNET_NO;
1188 if (allow_restart == GNUNET_YES)
1189 d->churn = GNUNET_YES;
1192 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1193 GNUNET_TRANSPORT_disconnect (d->th);
1196 /* Check if this is a local or remote process */
1197 if (NULL != d->hostname)
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1202 d->cfgfile, d->hostname);
1205 if (d->username != NULL)
1206 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1208 arg = GNUNET_strdup (d->hostname);
1210 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1218 "-c", d->cfgfile, "-e", "-q",
1220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1221 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1222 arg, "gnunet-arm", d->cfgfile, del_arg);
1223 /* Use -e to end arm, and -d to remove temp files */
1229 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1230 "Stopping gnunet-arm with config `%s' locally.\n",
1233 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1238 "-c", d->cfgfile, "-e", "-q",
1242 GNUNET_free_non_null (del_arg);
1243 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1244 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1249 * Changes the configuration of a GNUnet daemon.
1251 * @param d the daemon that should be modified
1252 * @param cfg the new configuration for the daemon
1253 * @param cb function called once the configuration was changed
1254 * @param cb_cls closure for cb
1257 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1258 struct GNUNET_CONFIGURATION_Handle *cfg,
1259 GNUNET_TESTING_NotifyCompletion cb,
1264 if (d->phase != SP_START_DONE)
1269 ("Peer not yet running, can not change configuration at this point."));
1273 /* 1) write configuration to temporary file */
1274 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1277 cb (cb_cls, _("Failed to write new configuration to disk."));
1281 /* 2) copy file to remote host (if necessary) */
1282 if (NULL == d->hostname)
1284 /* signal success */
1290 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1291 "Copying updated configuration file to remote host `%s'.\n",
1294 d->phase = SP_CONFIG_UPDATE;
1295 if (NULL != d->username)
1296 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1298 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1299 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1303 d->cfgfile, arg, NULL);
1305 if (NULL == d->proc)
1307 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1309 ("Could not start `%s' process to copy configuration file.\n"),
1312 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1313 d->phase = SP_START_DONE;
1317 d->update_cb_cls = cb_cls;
1319 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1325 * Data kept for each pair of peers that we try
1328 struct ConnectContext
1331 * Testing handle to the first daemon.
1333 struct GNUNET_TESTING_Daemon *d1;
1336 * Handle to core of first daemon (to check connect)
1338 struct GNUNET_CORE_Handle *d1core;
1341 * Have we actually connected to the core of the first daemon yet?
1346 * Testing handle to the second daemon.
1348 struct GNUNET_TESTING_Daemon *d2;
1351 * Handler for the request to core to connect to this peer.
1353 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1356 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1358 struct GNUNET_TRANSPORT_Handle *d1th;
1361 * Function to call once we are done (or have timed out).
1363 GNUNET_TESTING_NotifyConnection cb;
1371 * When should this operation be complete (or we must trigger
1374 struct GNUNET_TIME_Absolute timeout;
1377 * The relative timeout from whence this connect attempt was
1378 * started. Allows for reconnect attempts.
1380 struct GNUNET_TIME_Relative relative_timeout;
1383 * Maximum number of connect attempts, will retry connection
1384 * this number of times on failures.
1386 unsigned int max_connect_attempts;
1389 * Hello timeout task
1391 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1394 * Connect timeout task
1396 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1399 * When should this operation be complete (or we must trigger
1402 struct GNUNET_TIME_Relative timeout_hello;
1405 * Was the connection attempt successful?
1410 * The distance between the two connected peers
1416 /** Forward declaration **/
1418 reattempt_daemons_connect (void *cls,
1419 const struct GNUNET_SCHEDULER_TaskContext *tc);
1423 * Notify callback about success or failure of the attempt
1424 * to connect the two peers
1426 * @param cls our "struct ConnectContext" (freed)
1427 * @param tc reason tells us if we succeeded or failed
1430 notify_connect_result (void *cls,
1431 const struct GNUNET_SCHEDULER_TaskContext *tc)
1433 struct ConnectContext *ctx = cls;
1434 struct GNUNET_TIME_Relative remaining;
1436 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1437 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1439 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1440 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1443 if (ctx->connect_request_handle != NULL)
1445 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1446 ctx->connect_request_handle = NULL;
1448 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1450 if (ctx->d1th != NULL)
1451 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1453 if (ctx->d1core != NULL)
1454 GNUNET_CORE_disconnect (ctx->d1core);
1456 if (ctx->d2core != NULL)
1457 GNUNET_CORE_disconnect (ctx->d2core);
1466 remaining = GNUNET_TIME_absolute_get_remaining (ctx->timeout);
1468 if (ctx->connected == GNUNET_YES)
1470 if (ctx->cb != NULL)
1472 ctx->cb (ctx->cb_cls,
1476 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1479 else if (remaining.rel_value > 0)
1481 if (ctx->d1core != NULL)
1483 GNUNET_CORE_disconnect (ctx->d1core);
1486 ctx->d1core_ready = GNUNET_NO;
1488 if (ctx->d2core != NULL)
1490 GNUNET_CORE_disconnect (ctx->d2core);
1495 if (ctx->d1th != NULL)
1497 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1500 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1505 if (ctx->cb != NULL)
1507 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1508 ctx->d2->cfg, ctx->d1, ctx->d2,
1509 _("Peers failed to connect"));
1513 if (ctx->d1th != NULL)
1514 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1516 if (ctx->d1core != NULL)
1517 GNUNET_CORE_disconnect (ctx->d1core);
1524 * Success, connection is up. Signal client our success.
1526 * @param cls our "struct ConnectContext"
1527 * @param peer identity of the peer that has connected
1528 * @param atsi performance information
1532 connect_notify (void *cls,
1533 const struct GNUNET_PeerIdentity *peer,
1534 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1536 struct ConnectContext *ctx = cls;
1538 if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1540 ctx->connected = GNUNET_YES;
1541 ctx->distance = 0; /* FIXME: distance */
1542 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1544 GNUNET_SCHEDULER_cancel(ctx->hello_send_task);
1545 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1547 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1548 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1555 * Success, connection is up. Signal client our success.
1557 * @param cls our "struct ConnectContext"
1558 * @param peer identity of the peer that has connected
1559 * @param atsi performance information
1563 connect_notify_core2 (void *cls,
1564 const struct GNUNET_PeerIdentity *peer,
1565 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1567 struct ConnectContext *ctx = cls;
1569 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
1571 ctx->connected = GNUNET_YES;
1572 ctx->distance = 0; /* FIXME: distance */
1573 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1574 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1582 * Task called once a core connect request has been transmitted.
1584 * @param cls struct ConnectContext
1585 * @param success was the request successful?
1588 core_connect_request_cont (void *cls,
1591 struct ConnectContext *ctx = cls;
1593 ctx->connect_request_handle = NULL;
1597 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1599 struct ConnectContext *ctx = cls;
1600 struct GNUNET_MessageHeader *hello;
1601 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1602 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1604 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
1605 && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
1607 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1608 GNUNET_assert (hello != NULL);
1609 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello);
1610 GNUNET_assert (ctx->d1core != NULL);
1611 ctx->connect_request_handle =
1612 GNUNET_CORE_peer_request_connect (ctx->d1core,
1613 GNUNET_TIME_relative_divide
1614 (ctx->relative_timeout,
1615 ctx->max_connect_attempts + 1),
1617 &core_connect_request_cont, ctx);
1619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1620 "Sending connect request to CORE of %s for peer %s\n",
1621 GNUNET_i2s (&ctx->d1->id),
1622 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1624 ctx->timeout_hello =
1625 GNUNET_TIME_relative_add (ctx->timeout_hello,
1626 GNUNET_TIME_relative_multiply
1627 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1629 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello,
1634 * Notify of a successful connection to the core service.
1636 * @param cls a ConnectContext
1637 * @param server handle to the core service
1638 * @param my_identity the peer identity of this peer
1639 * @param publicKey the public key of the peer
1642 core_init_notify (void *cls,
1643 struct GNUNET_CORE_Handle * server,
1644 const struct GNUNET_PeerIdentity *
1647 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1650 struct ConnectContext *connect_ctx = cls;
1651 connect_ctx->d1core_ready = GNUNET_YES;
1655 * Iterator for currently known peers, to ensure
1656 * that we don't try to send duplicate connect
1659 * @param cls our "struct ConnectContext"
1660 * @param peer identity of the peer that has connected,
1661 * NULL when iteration has finished
1662 * @param atsi performance information
1666 core_initial_iteration (void *cls,
1667 const struct GNUNET_PeerIdentity *peer,
1668 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1670 struct ConnectContext *ctx = cls;
1672 if ((peer != NULL) &&
1673 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
1675 ctx->connected = GNUNET_YES;
1676 ctx->distance = 0; /* FIXME: distance */
1679 else if (peer == NULL) /* Peer not already connected, need to schedule connect request! */
1681 if (ctx->connected == GNUNET_YES)
1683 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1688 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1691 &connect_notify, NULL, NULL,
1693 NULL, GNUNET_NO, no_handlers);
1694 if (ctx->d1core == NULL)
1697 if (NULL != ctx->cb)
1698 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1699 _("Failed to connect to core service of first peer!\n"));
1703 #if DEBUG_TESTING > 2
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "Asked to connect peer %s to peer %s\n",
1706 ctx->d1->shortname, ctx->d2->shortname);
1707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1708 "Connecting to transport service of peer %s\n", ctx->d2->shortname);
1712 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1713 &ctx->d1->id, ctx->d1, NULL, NULL, NULL);
1714 if (ctx->d1th == NULL)
1716 GNUNET_CORE_disconnect (ctx->d1core);
1718 if (NULL != ctx->cb)
1719 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1720 _("Failed to connect to transport service!\n"));
1725 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
1726 (ctx->relative_timeout,
1727 ctx->max_connect_attempts),
1728 ¬ify_connect_result, ctx);
1730 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1736 * Establish a connection between two GNUnet daemons.
1738 * @param d1 handle for the first daemon
1739 * @param d2 handle for the second daemon
1740 * @param timeout how long is the connection attempt
1742 * @param max_connect_attempts how many times should we try to reconnect
1744 * @param cb function to call at the end
1745 * @param cb_cls closure for cb
1748 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1749 struct GNUNET_TESTING_Daemon *d2,
1750 struct GNUNET_TIME_Relative timeout,
1751 unsigned int max_connect_attempts,
1752 GNUNET_TESTING_NotifyConnection cb,
1755 struct ConnectContext *ctx;
1757 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1760 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1761 _("Peers are not fully running yet, can not connect!\n"));
1764 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1767 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1768 ctx->timeout_hello =
1769 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
1770 ctx->relative_timeout = timeout;
1772 ctx->cb_cls = cb_cls;
1773 ctx->max_connect_attempts = max_connect_attempts;
1774 ctx->connected = GNUNET_NO;
1776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1777 "Asked to connect peer %s to peer %s\n",
1778 d1->shortname, d2->shortname);
1781 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
1782 GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id, &core_initial_iteration, ctx);
1783 /* GNUNET_CORE_iterate_peers(ctx->d1->cfg, &core_initial_iteration, ctx); */
1788 reattempt_daemons_connect (void *cls,
1789 const struct GNUNET_SCHEDULER_TaskContext *tc)
1792 struct ConnectContext *ctx = cls;
1793 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1797 #if DEBUG_TESTING_RECONNECT
1798 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1799 "re-attempting connect of peer %s to peer %s\n",
1800 ctx->d1->shortname, ctx->d2->shortname);
1803 GNUNET_assert (ctx->d1core == NULL);
1804 ctx->d1core_ready = GNUNET_NO;
1805 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1808 &connect_notify, NULL, NULL,
1810 NULL, GNUNET_NO, no_handlers);
1811 if (ctx->d1core == NULL)
1813 if (NULL != ctx->cb)
1814 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1815 ctx->d2->cfg, ctx->d1, ctx->d2,
1816 _("Failed to connect to core service of first peer!\n"));
1821 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1823 ctx->d1, NULL, NULL, NULL);
1824 if (ctx->d1th == NULL)
1826 GNUNET_CORE_disconnect (ctx->d1core);
1828 if (NULL != ctx->cb)
1829 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1830 ctx->d2->cfg, ctx->d1, ctx->d2,
1831 _("Failed to connect to transport service!\n"));
1836 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_divide
1837 (ctx->relative_timeout,
1838 ctx->max_connect_attempts),
1839 ¬ify_connect_result, ctx);
1841 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1844 /* end of testing.c */