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 d->phase = SP_HOSTKEY_CREATED;
388 = GNUNET_SCHEDULER_add_now (&start_fsm, d);
391 case SP_HOSTKEY_CREATE:
393 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
394 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
395 &d->hostkeybuf[d->hostkeybufpos],
396 sizeof (d->hostkeybuf) - d->hostkeybufpos);
398 d->hostkeybufpos += bytes_read;
400 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
405 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
407 GNUNET_DISK_pipe_handle
409 GNUNET_DISK_PIPE_END_READ),
413 d->hostkeybuf[103] = '\0';
415 if ((bytes_read < 0) ||
416 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
421 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
422 _("Error reading from gnunet-peerinfo: %s\n"),
425 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
426 _("Malformed output from gnunet-peerinfo!\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 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
439 GNUNET_DISK_pipe_close (d->pipe_stdout);
440 d->pipe_stdout = NULL;
441 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
442 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
443 GNUNET_OS_process_close (d->proc);
445 d->have_hostkey = GNUNET_YES;
447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
450 case SP_HOSTKEY_CREATED:
451 GNUNET_assert(d->have_hostkey == GNUNET_YES);
452 if (d->hostkey_callback != NULL)
454 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
455 d->hostkey_callback = NULL;
456 d->phase = SP_HOSTKEY_CREATED;
460 d->phase = SP_TOPOLOGY_SETUP;
462 /* wait for topology finished */
463 if ((GNUNET_YES == d->dead)
464 || (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
472 d->cfg, d, _("`Failed while waiting for topology setup!\n"));
477 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
480 case SP_TOPOLOGY_SETUP:
481 /* start GNUnet on remote host */
482 if (NULL == d->hostname)
485 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
486 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
487 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
488 "-L", "DEBUG", "-s");
490 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
491 "gnunet-arm", "-c", d->cfgfile,
499 if (d->username != NULL)
500 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
502 dst = GNUNET_strdup (d->hostname);
505 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
506 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
507 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
508 d->cfgfile, "-L", "DEBUG", "-s", "-q");
510 if (d->ssh_port_str == NULL)
512 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
520 "-c", d->cfgfile, "-s", "-q",
526 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
527 "ssh", "-p", d->ssh_port_str,
535 "-c", d->cfgfile, "-s", "-q",
542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
543 _("Could not start `%s' process to start GNUnet.\n"),
544 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
552 (NULL == d->hostname)
553 ? _("Failed to start `gnunet-arm' process.\n")
554 : _("Failed to start `ssh' process.\n"));
558 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
559 "Started `%s', waiting for `%s' to be up.\n",
560 "gnunet-arm", "gnunet-service-core");
562 d->phase = SP_START_ARMING;
564 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
567 case SP_START_ARMING:
568 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
570 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
580 (NULL == d->hostname)
581 ? _("`gnunet-arm' does not seem to terminate.\n")
582 : _("`ssh' does not seem to terminate.\n"));
583 GNUNET_CONFIGURATION_destroy (d->cfg);
584 GNUNET_free (d->cfgfile);
585 GNUNET_free_non_null (d->hostname);
586 GNUNET_free_non_null (d->username);
587 GNUNET_free(d->proc);
593 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
599 "Successfully started `%s'.\n", "gnunet-arm");
601 GNUNET_free(d->proc);
602 d->phase = SP_START_CORE;
604 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
605 "Calling CORE_connect\n");
609 if (d->server != NULL)
610 GNUNET_CORE_disconnect(d->server);
612 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
622 _("Unable to connect to CORE service for peer!\n"));
623 GNUNET_CONFIGURATION_destroy (d->cfg);
624 GNUNET_free (d->cfgfile);
625 GNUNET_free_non_null (d->hostname);
626 GNUNET_free_non_null (d->username);
630 d->server = GNUNET_CORE_connect (d->cfg, 1,
635 NULL, GNUNET_NO, no_handlers);
637 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
641 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
644 if (d->server != NULL)
645 GNUNET_CORE_disconnect(d->server);
647 GNUNET_TRANSPORT_disconnect(d->th);
655 _("Unable to get HELLO for peer!\n"));
656 GNUNET_CONFIGURATION_destroy (d->cfg);
657 GNUNET_free (d->cfgfile);
658 GNUNET_free_non_null (d->hostname);
659 GNUNET_free_non_null (d->username);
663 if (d->hello != NULL)
665 GNUNET_assert(d->task == GNUNET_SCHEDULER_NO_TASK);
667 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
673 case SP_SHUTDOWN_START:
674 /* confirm copying complete */
675 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
677 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
680 if (NULL != d->dead_cb)
681 d->dead_cb (d->dead_cb_cls,
683 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
686 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello,
688 GNUNET_TRANSPORT_disconnect (d->th);
691 GNUNET_CONFIGURATION_destroy (d->cfg);
692 GNUNET_free (d->cfgfile);
693 GNUNET_free_non_null (d->hello);
694 GNUNET_free_non_null (d->hostname);
695 GNUNET_free_non_null (d->username);
696 GNUNET_free_non_null (d->shortname);
697 GNUNET_free_non_null (d->proc);
704 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
708 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
710 if (NULL != d->dead_cb)
711 d->dead_cb (d->dead_cb_cls,
713 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
716 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
717 GNUNET_TRANSPORT_disconnect (d->th);
720 if (d->server != NULL)
722 GNUNET_CORE_disconnect (d->server);
725 GNUNET_CONFIGURATION_destroy (d->cfg);
726 GNUNET_free (d->cfgfile);
727 GNUNET_free_non_null (d->hello);
728 GNUNET_free_non_null (d->hostname);
729 GNUNET_free_non_null (d->username);
730 GNUNET_free_non_null (d->shortname);
731 GNUNET_free_non_null (d->proc);
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
739 if (d->server != NULL)
741 GNUNET_CORE_disconnect (d->server);
747 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
748 GNUNET_TRANSPORT_disconnect (d->th);
751 /* state clean up and notifications */
752 if (d->churn == GNUNET_NO)
754 GNUNET_CONFIGURATION_destroy (d->cfg);
755 GNUNET_free (d->cfgfile);
756 GNUNET_free_non_null (d->hostname);
757 GNUNET_free_non_null (d->username);
760 GNUNET_free_non_null (d->hello);
762 GNUNET_free_non_null (d->shortname);
763 GNUNET_free_non_null (d->proc);
766 if (NULL != d->dead_cb)
767 d->dead_cb (d->dead_cb_cls, NULL);
769 if (d->churn == GNUNET_NO)
773 case SP_CONFIG_UPDATE:
774 /* confirm copying complete */
775 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
777 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
784 d->cfg, d, _("`scp' does not seem to terminate.\n"));
789 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
793 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
795 if (NULL != d->update_cb)
796 d->update_cb (d->update_cb_cls,
797 _("`scp' did not complete cleanly.\n"));
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Successfully copied configuration file.\n");
804 if (NULL != d->update_cb)
805 d->update_cb (d->update_cb_cls, NULL);
806 d->phase = SP_START_DONE;
812 * Continues GNUnet daemon startup when user wanted to be notified
813 * once a hostkey was generated (for creating friends files, blacklists,
816 * @param daemon the daemon to finish starting
819 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
821 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
822 daemon->phase = SP_TOPOLOGY_SETUP;
826 * Check whether the given daemon is running.
828 * @param daemon the daemon to check
830 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
831 * daemon is down, GNUNET_SYSERR on error.
834 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
837 return GNUNET_SYSERR;
839 if (daemon->running == GNUNET_YES)
846 * Start a peer that has previously been stopped using the daemon_stop
847 * call (and files weren't deleted and the allow restart flag)
849 * @param daemon the daemon to start (has been previously stopped)
850 * @param timeout how long to wait for restart
851 * @param cb the callback for notification when the peer is running
852 * @param cb_cls closure for the callback
855 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
856 struct GNUNET_TIME_Relative timeout,
857 GNUNET_TESTING_NotifyDaemonRunning cb,
860 if (daemon->running == GNUNET_YES)
862 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
863 "Daemon already running, can't restart!");
868 daemon->cb_cls = cb_cls;
869 daemon->phase = SP_TOPOLOGY_SETUP;
870 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
872 GNUNET_SCHEDULER_add_continuation (&start_fsm,
874 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
878 * Starts a GNUnet daemon. GNUnet must be installed on the target
879 * system and available in the PATH. The machine must furthermore be
880 * reachable via "ssh" (unless the hostname is "NULL") without the
881 * need to enter a password.
883 * @param cfg configuration to use
884 * @param timeout how long to wait starting up peers
885 * @param hostname name of the machine where to run GNUnet
886 * (use NULL for localhost).
887 * @param ssh_username ssh username to use when connecting to hostname
888 * @param sshport port to pass to ssh process when connecting to hostname
889 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
890 * @param hostkey_callback function to call once the hostkey has been
891 * generated for this peer, but it hasn't yet been started
892 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
893 * @param hostkey_cls closure for hostkey callback
894 * @param cb function to call once peer is up, or failed to start
895 * @param cb_cls closure for cb
896 * @return handle to the daemon (actual start will be completed asynchronously)
898 struct GNUNET_TESTING_Daemon *
899 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
900 struct GNUNET_TIME_Relative timeout,
901 const char *hostname,
902 const char *ssh_username,
905 GNUNET_TESTING_NotifyHostkeyCreated
906 hostkey_callback, void *hostkey_cls,
907 GNUNET_TESTING_NotifyDaemonRunning cb,
910 struct GNUNET_TESTING_Daemon *ret;
914 char *baseservicehome;
917 char *temp_file_name;
918 struct GNUNET_DISK_FileHandle *fn;
919 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
920 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
922 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
923 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
926 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
929 ret->ssh_port_str = NULL;
931 /* Find service home and base service home directories, create it if it doesn't exist */
932 GNUNET_assert(GNUNET_OK ==
933 GNUNET_CONFIGURATION_get_value_string (cfg,
938 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
939 GNUNET_asprintf(&temp_file_name, "%s/gnunet-testing-config", servicehome);
940 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
941 GNUNET_free(temp_file_name);
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Setting up peer with configuration file `%s'.\n",
947 if (NULL == ret->cfgfile)
949 GNUNET_free_non_null (ret->ssh_port_str);
950 GNUNET_free_non_null (ret->hostname);
954 ret->hostkey_callback = hostkey_callback;
955 ret->hostkey_cls = hostkey_cls;
957 ret->cb_cls = cb_cls;
958 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
959 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
960 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
962 "DEFAULTCONFIG", ret->cfgfile);
964 if (hostkey != NULL) /* Get the peer identity from the hostkey */
966 private_key = GNUNET_CRYPTO_rsa_decode_key(hostkey, HOSTKEYFILESIZE);
967 GNUNET_assert(private_key != NULL);
968 GNUNET_CRYPTO_rsa_key_get_public (private_key,
970 GNUNET_CRYPTO_hash(&public_key, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &ret->id.hashPubKey);
971 ret->shortname = GNUNET_strdup(GNUNET_i2s(&ret->id));
972 ret->have_hostkey = GNUNET_YES;
973 GNUNET_free(private_key);
976 /* Write hostkey to file, if we were given one */
980 GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome);
982 GNUNET_DISK_file_open (hostkeyfile,
983 GNUNET_DISK_OPEN_READWRITE
984 | GNUNET_DISK_OPEN_CREATE,
985 GNUNET_DISK_PERM_USER_READ |
986 GNUNET_DISK_PERM_USER_WRITE);
987 GNUNET_assert(fn != NULL);
988 GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE));
989 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn));
992 /* write configuration to temporary file */
993 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
995 if (0 != UNLINK (ret->cfgfile))
996 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
997 "unlink", ret->cfgfile);
998 GNUNET_CONFIGURATION_destroy (ret->cfg);
999 GNUNET_free_non_null (ret->hostname);
1000 GNUNET_free (ret->cfgfile);
1004 if (ssh_username != NULL)
1005 username = GNUNET_strdup (ssh_username);
1006 if ((ssh_username == NULL) && (GNUNET_OK !=
1007 GNUNET_CONFIGURATION_get_value_string (cfg,
1012 if (NULL != getenv ("USER"))
1013 username = GNUNET_strdup (getenv ("USER"));
1017 ret->username = username;
1019 /* copy directory to remote host */
1020 if (NULL != hostname)
1023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1024 "Copying configuration directory to host `%s'.\n", hostname);
1026 baseservicehome = GNUNET_strdup(servicehome);
1027 /* Remove trailing /'s */
1028 while (baseservicehome[strlen(baseservicehome) - 1] == '/')
1029 baseservicehome[strlen(baseservicehome) - 1] = '\0';
1030 /* Find next directory /, jump one ahead */
1031 slash = strrchr(baseservicehome, '/');
1035 ret->phase = SP_COPYING;
1036 if (NULL != username)
1037 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1039 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1041 if (ret->ssh_port_str == NULL)
1043 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1047 servicehome, arg, NULL);
1049 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", servicehome, arg);
1054 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
1055 "scp", "-r", "-P", ret->ssh_port_str,
1059 servicehome, arg, NULL);
1062 if (NULL == ret->proc)
1064 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1066 ("Could not start `%s' process to copy configuration directory.\n"),
1068 if (0 != UNLINK (ret->cfgfile))
1069 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1070 "unlink", ret->cfgfile);
1071 GNUNET_CONFIGURATION_destroy (ret->cfg);
1072 GNUNET_free_non_null (ret->hostname);
1073 GNUNET_free_non_null (ret->username);
1074 GNUNET_free (ret->cfgfile);
1076 if ((hostkey != NULL) && (0 != UNLINK(hostkeyfile)))
1077 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1078 "unlink", hostkeyfile);
1079 GNUNET_free_non_null(hostkeyfile);
1080 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1081 GNUNET_free(servicehome);
1086 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1088 GNUNET_free_non_null(hostkeyfile);
1089 GNUNET_free(servicehome);
1093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1094 "No need to copy configuration file since we are running locally.\n");
1096 ret->phase = SP_COPIED;
1097 GNUNET_SCHEDULER_add_continuation (&start_fsm,
1099 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1100 GNUNET_free_non_null(hostkeyfile);
1101 GNUNET_free(servicehome);
1107 * Restart (stop and start) a GNUnet daemon.
1109 * @param d the daemon that should be restarted
1110 * @param cb function called once the daemon is (re)started
1111 * @param cb_cls closure for cb
1114 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1115 GNUNET_TESTING_NotifyDaemonRunning cb,
1124 d->dead = GNUNET_YES;
1131 if (d->phase == SP_CONFIG_UPDATE)
1133 GNUNET_SCHEDULER_cancel (d->task);
1134 d->phase = SP_START_DONE;
1136 if (d->server != NULL)
1138 GNUNET_CORE_disconnect (d->server);
1144 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1145 GNUNET_TRANSPORT_disconnect (d->th);
1148 /* state clean up and notifications */
1149 GNUNET_free_non_null (d->hello);
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1156 d->phase = SP_START_ARMING;
1158 /* Check if this is a local or remote process */
1159 if (NULL != d->hostname)
1162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1163 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1164 d->cfgfile, d->hostname);
1167 if (d->username != NULL)
1168 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1170 arg = GNUNET_strdup (d->hostname);
1172 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1180 "-c", d->cfgfile, "-e", "-r", NULL);
1181 /* Use -r to restart arm and all services */
1188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1189 "Stopping gnunet-arm with config `%s' locally.\n",
1192 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1197 "-c", d->cfgfile, "-e", "-r", NULL);
1200 GNUNET_free_non_null (del_arg);
1202 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1209 * Stops a GNUnet daemon.
1211 * @param d the daemon that should be stopped
1212 * @param timeout how long to wait for process for shutdown to complete
1213 * @param cb function called once the daemon was stopped
1214 * @param cb_cls closure for cb
1215 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1217 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1218 * GNUNET_NO to kill off and clean up for good
1221 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1222 struct GNUNET_TIME_Relative timeout,
1223 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1224 int delete_files, int allow_restart)
1229 d->dead_cb_cls = cb_cls;
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1237 d->dead = GNUNET_YES;
1241 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1243 /* Free what was left from churning! */
1244 GNUNET_assert (d->cfg != NULL);
1245 GNUNET_CONFIGURATION_destroy (d->cfg);
1246 if (delete_files == GNUNET_YES)
1248 if (0 != UNLINK (d->cfgfile))
1250 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1253 GNUNET_free (d->cfgfile);
1254 GNUNET_free_non_null (d->hostname);
1255 GNUNET_free_non_null (d->username);
1256 if (NULL != d->dead_cb)
1257 d->dead_cb (d->dead_cb_cls, NULL);
1263 if (delete_files == GNUNET_YES)
1265 GNUNET_asprintf (&del_arg, "-d");
1268 if (d->phase == SP_CONFIG_UPDATE)
1270 GNUNET_SCHEDULER_cancel (d->task);
1271 d->phase = SP_START_DONE;
1273 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1274 if (d->server != NULL)
1276 GNUNET_CORE_disconnect (d->server);
1280 /* shutdown ARM process (will terminate others) */
1282 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1283 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1285 d->phase = SP_SHUTDOWN_START;
1286 d->running = GNUNET_NO;
1287 if (allow_restart == GNUNET_YES)
1288 d->churn = GNUNET_YES;
1291 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1292 GNUNET_TRANSPORT_disconnect (d->th);
1295 /* Check if this is a local or remote process */
1296 if (NULL != d->hostname)
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1301 d->cfgfile, d->hostname);
1304 if (d->username != NULL)
1305 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1307 arg = GNUNET_strdup (d->hostname);
1309 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1317 "-c", d->cfgfile, "-e", "-q",
1319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1320 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1321 arg, "gnunet-arm", d->cfgfile, del_arg);
1322 /* Use -e to end arm, and -d to remove temp files */
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329 "Stopping gnunet-arm with config `%s' locally.\n",
1332 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1337 "-c", d->cfgfile, "-e", "-q",
1341 GNUNET_free_non_null (del_arg);
1342 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1343 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1348 * Changes the configuration of a GNUnet daemon.
1350 * @param d the daemon that should be modified
1351 * @param cfg the new configuration for the daemon
1352 * @param cb function called once the configuration was changed
1353 * @param cb_cls closure for cb
1356 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1357 struct GNUNET_CONFIGURATION_Handle *cfg,
1358 GNUNET_TESTING_NotifyCompletion cb,
1363 if (d->phase != SP_START_DONE)
1368 ("Peer not yet running, can not change configuration at this point."));
1372 /* 1) write configuration to temporary file */
1373 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1376 cb (cb_cls, _("Failed to write new configuration to disk."));
1380 /* 2) copy file to remote host (if necessary) */
1381 if (NULL == d->hostname)
1383 /* signal success */
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390 "Copying updated configuration file to remote host `%s'.\n",
1393 d->phase = SP_CONFIG_UPDATE;
1394 if (NULL != d->username)
1395 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1397 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1398 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1402 d->cfgfile, arg, NULL);
1404 if (NULL == d->proc)
1406 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1408 ("Could not start `%s' process to copy configuration file.\n"),
1411 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1412 d->phase = SP_START_DONE;
1416 d->update_cb_cls = cb_cls;
1418 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1424 * Data kept for each pair of peers that we try
1427 struct ConnectContext
1430 * Testing handle to the first daemon.
1432 struct GNUNET_TESTING_Daemon *d1;
1435 * Handle to core of first daemon (to check connect)
1437 struct GNUNET_CORE_Handle *d1core;
1440 * Have we actually connected to the core of the first daemon yet?
1445 * Testing handle to the second daemon.
1447 struct GNUNET_TESTING_Daemon *d2;
1450 * Handler for the request to core to connect to this peer.
1452 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1455 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1457 struct GNUNET_TRANSPORT_Handle *d1th;
1460 * Function to call once we are done (or have timed out).
1462 GNUNET_TESTING_NotifyConnection cb;
1470 * The relative timeout from whence this connect attempt was
1471 * started. Allows for reconnect attempts.
1473 struct GNUNET_TIME_Relative relative_timeout;
1476 * Maximum number of connect attempts, will retry connection
1477 * this number of times on failures.
1479 unsigned int connect_attempts;
1482 * Hello timeout task
1484 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1487 * Connect timeout task
1489 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1492 * When should this operation be complete (or we must trigger
1495 struct GNUNET_TIME_Relative timeout_hello;
1498 * Was the connection attempt successful?
1503 * When connecting, do we need to send the HELLO?
1508 * The distance between the two connected peers
1514 /** Forward declaration **/
1516 reattempt_daemons_connect (void *cls,
1517 const struct GNUNET_SCHEDULER_TaskContext *tc);
1521 * Notify callback about success or failure of the attempt
1522 * to connect the two peers
1524 * @param cls our "struct ConnectContext" (freed)
1525 * @param tc reason tells us if we succeeded or failed
1528 notify_connect_result (void *cls,
1529 const struct GNUNET_SCHEDULER_TaskContext *tc)
1531 struct ConnectContext *ctx = cls;
1532 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1533 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1535 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1536 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1539 if (ctx->connect_request_handle != NULL)
1541 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1542 ctx->connect_request_handle = NULL;
1545 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1547 if (ctx->d1th != NULL)
1548 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1550 if (ctx->d1core != NULL)
1551 GNUNET_CORE_disconnect (ctx->d1core);
1553 if (ctx->d2core != NULL)
1554 GNUNET_CORE_disconnect (ctx->d2core);
1562 if (ctx->d1th != NULL)
1563 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1565 if (ctx->d1core != NULL)
1566 GNUNET_CORE_disconnect (ctx->d1core);
1569 if (ctx->connected == GNUNET_YES)
1571 if (ctx->cb != NULL)
1573 ctx->cb (ctx->cb_cls,
1577 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1580 else if (ctx->connect_attempts > 0)
1582 ctx->d1core_ready = GNUNET_NO;
1584 if (ctx->d2core != NULL)
1586 GNUNET_CORE_disconnect (ctx->d2core);
1590 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1595 if (ctx->cb != NULL)
1597 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1598 ctx->d2->cfg, ctx->d1, ctx->d2,
1599 _("Peers failed to connect"));
1608 * Success, connection is up. Signal client our success.
1610 * @param cls our "struct ConnectContext"
1611 * @param peer identity of the peer that has connected
1612 * @param atsi performance information
1616 connect_notify (void *cls,
1617 const struct GNUNET_PeerIdentity *peer,
1618 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1620 struct ConnectContext *ctx = cls;
1623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1624 "Connected peer %s to peer %s\n",
1625 ctx->d1->shortname, GNUNET_i2s(peer));
1628 if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1631 ctx->connected = GNUNET_YES;
1632 ctx->distance = 0; /* FIXME: distance */
1633 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1635 GNUNET_SCHEDULER_cancel(ctx->hello_send_task);
1636 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1638 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1639 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1646 * Success, connection is up. Signal client our success.
1648 * @param cls our "struct ConnectContext"
1649 * @param peer identity of the peer that has connected
1650 * @param atsi performance information
1654 connect_notify_core2 (void *cls,
1655 const struct GNUNET_PeerIdentity *peer,
1656 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1658 struct ConnectContext *ctx = cls;
1660 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
1662 ctx->connected = GNUNET_YES;
1663 ctx->distance = 0; /* FIXME: distance */
1664 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1665 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1673 * Task called once a core connect request has been transmitted.
1675 * @param cls struct ConnectContext
1676 * @param success was the request successful?
1679 core_connect_request_cont (void *cls,
1682 struct ConnectContext *ctx = cls;
1684 ctx->connect_request_handle = NULL;
1688 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1690 struct ConnectContext *ctx = cls;
1691 struct GNUNET_MessageHeader *hello;
1692 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1693 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1695 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
1696 && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
1698 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1699 GNUNET_assert (hello != NULL);
1700 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1701 GNUNET_assert (ctx->d1core != NULL);
1702 ctx->connect_request_handle =
1703 GNUNET_CORE_peer_request_connect (ctx->d1core,
1704 ctx->relative_timeout,
1706 &core_connect_request_cont, ctx);
1709 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1710 "Sending connect request to CORE of %s for peer %s\n",
1711 GNUNET_i2s (&ctx->d1->id),
1712 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1714 ctx->timeout_hello =
1715 GNUNET_TIME_relative_add (ctx->timeout_hello,
1716 GNUNET_TIME_relative_multiply
1717 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1719 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello,
1724 * Notify of a successful connection to the core service.
1726 * @param cls a ConnectContext
1727 * @param server handle to the core service
1728 * @param my_identity the peer identity of this peer
1729 * @param publicKey the public key of the peer
1732 core_init_notify (void *cls,
1733 struct GNUNET_CORE_Handle * server,
1734 const struct GNUNET_PeerIdentity *
1737 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1740 struct ConnectContext *connect_ctx = cls;
1741 connect_ctx->d1core_ready = GNUNET_YES;
1743 if (connect_ctx->send_hello == GNUNET_NO)
1745 connect_ctx->connect_request_handle =
1746 GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
1747 connect_ctx->relative_timeout,
1748 &connect_ctx->d2->id,
1749 &core_connect_request_cont, connect_ctx);
1750 GNUNET_assert(connect_ctx->connect_request_handle != NULL);
1752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1753 "Sending connect request to CORE of %s for peer %s\n",
1754 connect_ctx->d1->shortname,
1755 connect_ctx->d2->shortname);
1763 reattempt_daemons_connect (void *cls,
1764 const struct GNUNET_SCHEDULER_TaskContext *tc)
1766 struct ConnectContext *ctx = cls;
1767 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1772 #if DEBUG_TESTING_RECONNECT
1773 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1774 "re-attempting connect of peer %s to peer %s\n",
1775 ctx->d1->shortname, ctx->d2->shortname);
1777 ctx->connect_attempts--;
1778 GNUNET_assert (ctx->d1core == NULL);
1779 ctx->d1core_ready = GNUNET_NO;
1780 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1783 &connect_notify, NULL, NULL,
1785 NULL, GNUNET_NO, no_handlers);
1786 if (ctx->d1core == NULL)
1788 if (NULL != ctx->cb)
1789 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1790 ctx->d2->cfg, ctx->d1, ctx->d2,
1791 _("Failed to connect to core service of first peer!\n"));
1796 if (ctx->send_hello == GNUNET_YES)
1798 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1800 ctx->d1, NULL, NULL, NULL);
1801 if (ctx->d1th == NULL)
1803 GNUNET_CORE_disconnect (ctx->d1core);
1805 if (NULL != ctx->cb)
1806 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1807 ctx->d2->cfg, ctx->d1, ctx->d2,
1808 _("Failed to connect to transport service!\n"));
1811 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1815 ctx->connect_request_handle =
1816 GNUNET_CORE_peer_request_connect (ctx->d1core,
1817 ctx->relative_timeout,
1819 &core_connect_request_cont, ctx);
1822 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1823 ¬ify_connect_result, ctx);
1827 * Iterator for currently known peers, to ensure
1828 * that we don't try to send duplicate connect
1831 * @param cls our "struct ConnectContext"
1832 * @param peer identity of the peer that has connected,
1833 * NULL when iteration has finished
1834 * @param atsi performance information
1838 core_initial_iteration (void *cls,
1839 const struct GNUNET_PeerIdentity *peer,
1840 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1842 struct ConnectContext *ctx = cls;
1844 if ((peer != NULL) &&
1845 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
1847 ctx->connected = GNUNET_YES;
1848 ctx->distance = 0; /* FIXME: distance */
1851 else if (peer == NULL) /* End of iteration over peers */
1853 if (ctx->connected == GNUNET_YES)
1855 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1860 /* Peer not already connected, need to schedule connect request! */
1861 if (ctx->d1core == NULL)
1864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1865 "Peers are NOT connected, connecting to core!\n");
1867 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1870 &connect_notify, NULL, NULL,
1872 NULL, GNUNET_NO, no_handlers);
1875 if (ctx->d1core == NULL)
1878 if (NULL != ctx->cb)
1879 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1880 _("Failed to connect to core service of first peer!\n"));
1884 if (ctx->send_hello == GNUNET_YES)
1886 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1887 &ctx->d1->id, ctx->d1, NULL, NULL, NULL);
1888 if (ctx->d1th == NULL)
1890 GNUNET_CORE_disconnect (ctx->d1core);
1892 if (NULL != ctx->cb)
1893 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1894 _("Failed to connect to transport service!\n"));
1897 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1901 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1902 ¬ify_connect_result, ctx);
1908 * Establish a connection between two GNUnet daemons.
1910 * @param d1 handle for the first daemon
1911 * @param d2 handle for the second daemon
1912 * @param timeout how long is the connection attempt
1914 * @param max_connect_attempts how many times should we try to reconnect
1916 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
1917 * the HELLO has already been exchanged
1918 * @param cb function to call at the end
1919 * @param cb_cls closure for cb
1922 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1923 struct GNUNET_TESTING_Daemon *d2,
1924 struct GNUNET_TIME_Relative timeout,
1925 unsigned int max_connect_attempts,
1927 GNUNET_TESTING_NotifyConnection cb,
1930 struct ConnectContext *ctx;
1932 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
1935 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
1936 _("Peers are not fully running yet, can not connect!\n"));
1940 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
1943 ctx->timeout_hello =
1944 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
1945 ctx->relative_timeout = GNUNET_TIME_relative_divide(timeout, max_connect_attempts);
1947 ctx->cb_cls = cb_cls;
1948 ctx->connect_attempts = max_connect_attempts;
1949 ctx->connected = GNUNET_NO;
1950 ctx->send_hello = send_hello;
1952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1953 "Asked to connect peer %s to peer %s\n",
1954 d1->shortname, d2->shortname);
1957 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
1958 GNUNET_assert(GNUNET_OK == GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id, &core_initial_iteration, ctx));
1959 /*GNUNET_assert(GNUNET_OK == GNUNET_CORE_iterate_peers (ctx->d1->cfg, &core_initial_iteration, ctx));*/
1962 /* end of testing.c */