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
42 #define WAIT_FOR_HELLO GNUNET_NO
45 * How long do we wait after starting gnunet-service-arm
46 * for the core service to be alive?
48 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
51 * How many times are we willing to try to wait for "scp" or
52 * "gnunet-service-arm" to complete (waitpid) before giving up?
54 #define MAX_EXEC_WAIT_RUNS 250
56 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
59 * Receive the HELLO from one peer, give it to the other
60 * and ask them to connect.
62 * @param cls "struct ConnectContext"
63 * @param message HELLO message of peer
66 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
68 struct GNUNET_TESTING_Daemon *daemon = cls;
69 GNUNET_TESTING_NotifyDaemonRunning cb;
75 GNUNET_assert (daemon->phase == SP_GET_HELLO);
79 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
80 GNUNET_SCHEDULER_cancel(daemon->task);
82 if (daemon->server != NULL)
85 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
86 "Received `%s' from transport service of `%4s', disconnecting core!\n",
87 "HELLO", GNUNET_i2s (&daemon->id));
89 GNUNET_CORE_disconnect (daemon->server);
90 daemon->server = NULL;
93 GNUNET_assert (message != NULL);
94 msize = ntohs (message->size);
99 if (daemon->th != NULL)
101 GNUNET_TRANSPORT_get_hello_cancel (daemon->th, &process_hello, daemon);
104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
105 "Received `%s' from transport service of `%4s'\n",
106 "HELLO", GNUNET_i2s (&daemon->id));
109 GNUNET_free_non_null (daemon->hello);
110 daemon->hello = GNUNET_malloc (msize);
111 memcpy (daemon->hello, message, msize);
113 if (daemon->th != NULL)
115 GNUNET_TRANSPORT_disconnect (daemon->th);
118 daemon->phase = SP_START_DONE;
121 if (NULL != cb) /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
122 cb (daemon->cb_cls, &daemon->id, daemon->cfg, daemon, NULL);
127 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
131 * Function called after GNUNET_CORE_connect has succeeded
132 * (or failed for good). Note that the private key of the
133 * peer is intentionally not exposed here; if you need it,
134 * your process should try to read the private key file
135 * directly (which should work if you are authorized...).
138 * @param server handle to the server, NULL if we failed
139 * @param my_identity ID of this peer, NULL if we failed
140 * @param publicKey public key of this peer, NULL if we failed
143 testing_init (void *cls,
144 struct GNUNET_CORE_Handle *server,
145 const struct GNUNET_PeerIdentity *my_identity,
146 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
148 struct GNUNET_TESTING_Daemon *d = cls;
150 GNUNET_assert (d->phase == SP_START_CORE);
151 d->phase = SP_GET_HELLO;
156 if (GNUNET_YES == d->dead)
157 GNUNET_TESTING_daemon_stop (d,
158 GNUNET_TIME_absolute_get_remaining
159 (d->max_timeout), d->dead_cb,
160 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
161 else if (NULL != d->cb)
162 d->cb (d->cb_cls, NULL, d->cfg, d,
163 _("Failed to connect to core service\n"));
167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
168 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
170 d->id = *my_identity; /* FIXME: shouldn't we already have this from reading the hostkey file? */
171 if (d->shortname == NULL)
172 d->shortname = strdup (GNUNET_i2s (my_identity));
174 d->running = GNUNET_YES;
176 if (GNUNET_NO == d->running)
179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
180 "Peer is dead (d->running == GNUNET_NO)\n");
185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
186 "Successfully started peer `%4s', connecting to transport service.\n",
187 GNUNET_i2s (my_identity));
190 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
193 if (GNUNET_YES == d->dead)
194 GNUNET_TESTING_daemon_stop (d,
195 GNUNET_TIME_absolute_get_remaining
196 (d->max_timeout), d->dead_cb,
197 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
198 else if (NULL != d->cb)
199 d->cb (d->cb_cls, &d->id, d->cfg, d,
200 _("Failed to connect to transport service!\n"));
204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
205 "Connected to transport service `%s', getting HELLO\n",
206 GNUNET_i2s (my_identity));
209 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
211 if (d->task != GNUNET_SCHEDULER_NO_TASK)
212 GNUNET_SCHEDULER_cancel(d->task);
214 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
220 * Notify of a peer being up and running. Scheduled as a task
221 * so that variables which may need to be set are set before
222 * the connect callback can set up new operations.
224 * @param cls the testing daemon
225 * @param tc task scheduler context
228 notify_daemon_started (void *cls,
229 const struct GNUNET_SCHEDULER_TaskContext *tc)
231 struct GNUNET_TESTING_Daemon *d = cls;
232 GNUNET_TESTING_NotifyDaemonRunning cb;
237 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
241 * Finite-state machine for starting GNUnet.
243 * @param cls our "struct GNUNET_TESTING_Daemon"
247 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
249 struct GNUNET_TESTING_Daemon *d = cls;
250 GNUNET_TESTING_NotifyDaemonRunning cb;
251 enum GNUNET_OS_ProcessStatusType type;
257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
258 "Peer FSM is in phase %u.\n", d->phase);
261 d->task = GNUNET_SCHEDULER_NO_TASK;
265 /* confirm copying complete */
266 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
268 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
278 ("`scp' does not seem to terminate (timeout copying config).\n"));
283 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
287 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
293 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
296 GNUNET_OS_process_close(d->proc);
298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
299 "Successfully copied configuration file.\n");
301 d->phase = SP_COPIED;
304 /* Start create hostkey process if we don't already know the peer identity!*/
305 if (GNUNET_NO == d->have_hostkey)
307 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
308 if (d->pipe_stdout == NULL)
317 (NULL == d->hostname)
318 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
319 : _("Failed to create pipe for `ssh' process.\n"));
322 if (NULL == d->hostname)
325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
326 "Starting `%s', with command `%s %s %s %s'.\n",
327 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
331 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
332 "gnunet-peerinfo", "-c", d->cfgfile,
334 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
335 GNUNET_DISK_PIPE_END_WRITE);
339 if (d->username != NULL)
340 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
342 dst = GNUNET_strdup (d->hostname);
345 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
346 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
347 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
350 if (d->ssh_port_str == NULL)
352 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
359 "-c", d->cfgfile, "-sq",
364 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
365 "ssh", "-p", d->ssh_port_str,
371 "-c", d->cfgfile, "-sq",
374 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
375 GNUNET_DISK_PIPE_END_WRITE);
380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
381 _("Could not start `%s' process to create hostkey.\n"),
382 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
390 (NULL == d->hostname)
391 ? _("Failed to start `gnunet-peerinfo' process.\n")
392 : _("Failed to start `ssh' process.\n"));
393 GNUNET_DISK_pipe_close (d->pipe_stdout);
397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
398 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
400 d->phase = SP_HOSTKEY_CREATE;
403 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
405 GNUNET_DISK_pipe_handle
407 GNUNET_DISK_PIPE_END_READ),
410 else /* Already have a hostkey! */
412 if (d->hostkey_callback != NULL)
414 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
415 d->hostkey_callback = NULL;
416 d->phase = SP_HOSTKEY_CREATED;
419 d->phase = SP_TOPOLOGY_SETUP;
423 = GNUNET_SCHEDULER_add_now (&start_fsm, d);
426 case SP_HOSTKEY_CREATE:
428 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
429 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
430 &d->hostkeybuf[d->hostkeybufpos],
431 sizeof (d->hostkeybuf) - d->hostkeybufpos);
433 d->hostkeybufpos += bytes_read;
435 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
440 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
442 GNUNET_DISK_pipe_handle
444 GNUNET_DISK_PIPE_END_READ),
448 d->hostkeybuf[103] = '\0';
450 if ((bytes_read < 0) ||
451 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
456 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
457 _("Error reading from gnunet-peerinfo: %s\n"),
460 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
461 _("Malformed output from gnunet-peerinfo!\n"));
464 GNUNET_DISK_pipe_close (d->pipe_stdout);
465 d->pipe_stdout = NULL;
466 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
467 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
468 GNUNET_OS_process_close (d->proc);
471 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
474 GNUNET_DISK_pipe_close (d->pipe_stdout);
475 d->pipe_stdout = NULL;
476 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
477 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
478 GNUNET_OS_process_close (d->proc);
480 d->have_hostkey = GNUNET_YES;
481 if (d->hostkey_callback != NULL)
483 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
484 d->hostkey_callback = NULL;
485 d->phase = SP_HOSTKEY_CREATED;
489 d->phase = SP_TOPOLOGY_SETUP;
492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
495 case SP_HOSTKEY_CREATED:
496 /* wait for topology finished */
497 if ((GNUNET_YES == d->dead)
498 || (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
506 d->cfg, d, _("`Failed while waiting for topology setup!\n"));
511 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
514 case SP_TOPOLOGY_SETUP:
515 /* start GNUnet on remote host */
516 if (NULL == d->hostname)
519 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
520 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
521 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
522 "-L", "DEBUG", "-s");
524 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
525 "gnunet-arm", "-c", d->cfgfile,
533 if (d->username != NULL)
534 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
536 dst = GNUNET_strdup (d->hostname);
539 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
540 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
541 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
542 d->cfgfile, "-L", "DEBUG", "-s", "-q");
544 if (d->ssh_port_str == NULL)
546 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
554 "-c", d->cfgfile, "-s", "-q",
560 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
561 "ssh", "-p", d->ssh_port_str,
569 "-c", d->cfgfile, "-s", "-q",
576 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
577 _("Could not start `%s' process to start GNUnet.\n"),
578 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
586 (NULL == d->hostname)
587 ? _("Failed to start `gnunet-arm' process.\n")
588 : _("Failed to start `ssh' process.\n"));
592 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
593 "Started `%s', waiting for `%s' to be up.\n",
594 "gnunet-arm", "gnunet-service-core");
596 d->phase = SP_START_ARMING;
598 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
601 case SP_START_ARMING:
602 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
604 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
614 (NULL == d->hostname)
615 ? _("`gnunet-arm' does not seem to terminate.\n")
616 : _("`ssh' does not seem to terminate.\n"));
617 GNUNET_CONFIGURATION_destroy (d->cfg);
618 GNUNET_free (d->cfgfile);
619 GNUNET_free_non_null (d->hostname);
620 GNUNET_free_non_null (d->username);
621 GNUNET_free(d->proc);
627 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
632 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
633 "Successfully started `%s'.\n", "gnunet-arm");
635 GNUNET_free(d->proc);
636 d->phase = SP_START_CORE;
638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
639 "Calling CORE_connect\n");
643 if (d->server != NULL)
644 GNUNET_CORE_disconnect(d->server);
647 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
657 _("Unable to connect to CORE service for peer!\n"));
658 GNUNET_CONFIGURATION_destroy (d->cfg);
659 GNUNET_free (d->cfgfile);
660 GNUNET_free_non_null (d->hostname);
661 GNUNET_free_non_null (d->username);
665 d->server = GNUNET_CORE_connect (d->cfg, 1,
670 NULL, GNUNET_NO, no_handlers);
672 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
675 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
678 if (GNUNET_YES == d->dead)
679 GNUNET_TESTING_daemon_stop (d,
680 GNUNET_TIME_absolute_get_remaining
681 (d->max_timeout), d->dead_cb,
682 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
683 else if (NULL != d->cb)
684 d->cb (d->cb_cls, &d->id, d->cfg, d,
685 _("Failed to connect to transport service!\n"));
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Connected to transport service `%s', getting HELLO\n",
691 GNUNET_i2s (my_identity));
694 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
695 GNUNET_SCHEDULER_add_now(¬ify_daemon_started, d);
699 cb (d->cb_cls, &d->id, d->cfg, d, NULL);*/
700 d->running = GNUNET_YES;
701 d->phase = SP_GET_HELLO;
705 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
708 if (d->server != NULL)
709 GNUNET_CORE_disconnect(d->server);
711 GNUNET_TRANSPORT_disconnect(d->th);
719 _("Unable to get HELLO for peer!\n"));
720 GNUNET_CONFIGURATION_destroy (d->cfg);
721 GNUNET_free (d->cfgfile);
722 GNUNET_free_non_null (d->hostname);
723 GNUNET_free_non_null (d->username);
727 if (d->hello != NULL)
729 GNUNET_assert(d->task == GNUNET_SCHEDULER_NO_TASK);
731 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
737 case SP_SHUTDOWN_START:
738 /* confirm copying complete */
739 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
741 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
744 if (NULL != d->dead_cb)
745 d->dead_cb (d->dead_cb_cls,
747 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
750 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello,
752 GNUNET_TRANSPORT_disconnect (d->th);
755 GNUNET_CONFIGURATION_destroy (d->cfg);
756 GNUNET_free (d->cfgfile);
757 GNUNET_free_non_null (d->hello);
758 GNUNET_free_non_null (d->hostname);
759 GNUNET_free_non_null (d->username);
760 GNUNET_free_non_null (d->shortname);
761 GNUNET_free_non_null (d->proc);
768 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
772 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
774 if (NULL != d->dead_cb)
775 d->dead_cb (d->dead_cb_cls,
777 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
780 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
781 GNUNET_TRANSPORT_disconnect (d->th);
784 if (d->server != NULL)
786 GNUNET_CORE_disconnect (d->server);
789 GNUNET_CONFIGURATION_destroy (d->cfg);
790 GNUNET_free (d->cfgfile);
791 GNUNET_free_non_null (d->hello);
792 GNUNET_free_non_null (d->hostname);
793 GNUNET_free_non_null (d->username);
794 GNUNET_free_non_null (d->shortname);
795 GNUNET_free_non_null (d->proc);
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
803 if (d->server != NULL)
805 GNUNET_CORE_disconnect (d->server);
811 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
812 GNUNET_TRANSPORT_disconnect (d->th);
816 if (NULL != d->dead_cb)
817 d->dead_cb (d->dead_cb_cls, NULL);
819 /* state clean up and notifications */
820 if (d->churn == GNUNET_NO)
822 GNUNET_CONFIGURATION_destroy (d->cfg);
823 GNUNET_free (d->cfgfile);
824 GNUNET_free_non_null (d->hostname);
825 GNUNET_free_non_null (d->username);
828 GNUNET_free_non_null (d->hello);
830 GNUNET_free_non_null (d->shortname);
831 GNUNET_free_non_null (d->proc);
834 if (d->churn == GNUNET_NO)
838 case SP_CONFIG_UPDATE:
839 /* confirm copying complete */
840 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
842 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
849 d->cfg, d, _("`scp' does not seem to terminate.\n"));
854 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
858 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
860 if (NULL != d->update_cb)
861 d->update_cb (d->update_cb_cls,
862 _("`scp' did not complete cleanly.\n"));
866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
867 "Successfully copied configuration file.\n");
869 if (NULL != d->update_cb)
870 d->update_cb (d->update_cb_cls, NULL);
871 d->phase = SP_START_DONE;
877 * Continues GNUnet daemon startup when user wanted to be notified
878 * once a hostkey was generated (for creating friends files, blacklists,
881 * @param daemon the daemon to finish starting
884 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
886 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
887 daemon->phase = SP_TOPOLOGY_SETUP;
891 * Check whether the given daemon is running.
893 * @param daemon the daemon to check
895 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
896 * daemon is down, GNUNET_SYSERR on error.
899 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
902 return GNUNET_SYSERR;
904 if (daemon->running == GNUNET_YES)
911 * Start a peer that has previously been stopped using the daemon_stop
912 * call (and files weren't deleted and the allow restart flag)
914 * @param daemon the daemon to start (has been previously stopped)
915 * @param timeout how long to wait for restart
916 * @param cb the callback for notification when the peer is running
917 * @param cb_cls closure for the callback
920 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
921 struct GNUNET_TIME_Relative timeout,
922 GNUNET_TESTING_NotifyDaemonRunning cb,
925 if (daemon->running == GNUNET_YES)
927 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
928 "Daemon already running, can't restart!");
933 daemon->cb_cls = cb_cls;
934 daemon->phase = SP_TOPOLOGY_SETUP;
935 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
937 GNUNET_SCHEDULER_add_continuation (&start_fsm,
939 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
943 * Starts a GNUnet daemon. GNUnet must be installed on the target
944 * system and available in the PATH. The machine must furthermore be
945 * reachable via "ssh" (unless the hostname is "NULL") without the
946 * need to enter a password.
948 * @param cfg configuration to use
949 * @param timeout how long to wait starting up peers
950 * @param hostname name of the machine where to run GNUnet
951 * (use NULL for localhost).
952 * @param ssh_username ssh username to use when connecting to hostname
953 * @param sshport port to pass to ssh process when connecting to hostname
954 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
955 * @param hostkey_callback function to call once the hostkey has been
956 * generated for this peer, but it hasn't yet been started
957 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
958 * @param hostkey_cls closure for hostkey callback
959 * @param cb function to call once peer is up, or failed to start
960 * @param cb_cls closure for cb
961 * @return handle to the daemon (actual start will be completed asynchronously)
963 struct GNUNET_TESTING_Daemon *
964 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
965 struct GNUNET_TIME_Relative timeout,
966 const char *hostname,
967 const char *ssh_username,
970 GNUNET_TESTING_NotifyHostkeyCreated
971 hostkey_callback, void *hostkey_cls,
972 GNUNET_TESTING_NotifyDaemonRunning cb,
975 struct GNUNET_TESTING_Daemon *ret;
979 char *baseservicehome;
982 char *temp_file_name;
983 struct GNUNET_DISK_FileHandle *fn;
984 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
985 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
987 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
988 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
991 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
994 ret->ssh_port_str = NULL;
996 /* Find service home and base service home directories, create it if it doesn't exist */
997 GNUNET_assert(GNUNET_OK ==
998 GNUNET_CONFIGURATION_get_value_string (cfg,
1003 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1004 GNUNET_asprintf(&temp_file_name, "%s/gnunet-testing-config", servicehome);
1005 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1006 GNUNET_free(temp_file_name);
1008 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1009 "Setting up peer with configuration file `%s'.\n",
1012 if (NULL == ret->cfgfile)
1014 GNUNET_free_non_null (ret->ssh_port_str);
1015 GNUNET_free_non_null (ret->hostname);
1019 ret->hostkey_callback = hostkey_callback;
1020 ret->hostkey_cls = hostkey_cls;
1022 ret->cb_cls = cb_cls;
1023 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1024 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1025 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
1027 "DEFAULTCONFIG", ret->cfgfile);
1029 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1031 private_key = GNUNET_CRYPTO_rsa_decode_key(hostkey, HOSTKEYFILESIZE);
1032 GNUNET_assert(private_key != NULL);
1033 GNUNET_CRYPTO_rsa_key_get_public (private_key,
1035 GNUNET_CRYPTO_hash(&public_key, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &ret->id.hashPubKey);
1036 ret->shortname = GNUNET_strdup(GNUNET_i2s(&ret->id));
1037 ret->have_hostkey = GNUNET_YES;
1038 GNUNET_CRYPTO_rsa_key_free(private_key);
1041 /* Write hostkey to file, if we were given one */
1043 if (hostkey != NULL)
1045 GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome);
1047 GNUNET_DISK_file_open (hostkeyfile,
1048 GNUNET_DISK_OPEN_READWRITE
1049 | GNUNET_DISK_OPEN_CREATE,
1050 GNUNET_DISK_PERM_USER_READ |
1051 GNUNET_DISK_PERM_USER_WRITE);
1052 GNUNET_assert(fn != NULL);
1053 GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE));
1054 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn));
1057 /* write configuration to temporary file */
1058 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1060 if (0 != UNLINK (ret->cfgfile))
1061 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1062 "unlink", ret->cfgfile);
1063 GNUNET_CONFIGURATION_destroy (ret->cfg);
1064 GNUNET_free_non_null (ret->hostname);
1065 GNUNET_free (ret->cfgfile);
1069 if (ssh_username != NULL)
1070 username = GNUNET_strdup (ssh_username);
1071 if ((ssh_username == NULL) && (GNUNET_OK !=
1072 GNUNET_CONFIGURATION_get_value_string (cfg,
1077 if (NULL != getenv ("USER"))
1078 username = GNUNET_strdup (getenv ("USER"));
1082 ret->username = username;
1084 /* copy directory to remote host */
1085 if (NULL != hostname)
1088 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1089 "Copying configuration directory to host `%s'.\n", hostname);
1091 baseservicehome = GNUNET_strdup(servicehome);
1092 /* Remove trailing /'s */
1093 while (baseservicehome[strlen(baseservicehome) - 1] == '/')
1094 baseservicehome[strlen(baseservicehome) - 1] = '\0';
1095 /* Find next directory /, jump one ahead */
1096 slash = strrchr(baseservicehome, '/');
1100 ret->phase = SP_COPYING;
1101 if (NULL != username)
1102 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1104 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1106 if (ret->ssh_port_str == NULL)
1108 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1112 servicehome, arg, NULL);
1114 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", servicehome, arg);
1119 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
1120 "scp", "-r", "-P", ret->ssh_port_str,
1124 servicehome, arg, NULL);
1127 if (NULL == ret->proc)
1129 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1131 ("Could not start `%s' process to copy configuration directory.\n"),
1133 if (0 != UNLINK (ret->cfgfile))
1134 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1135 "unlink", ret->cfgfile);
1136 GNUNET_CONFIGURATION_destroy (ret->cfg);
1137 GNUNET_free_non_null (ret->hostname);
1138 GNUNET_free_non_null (ret->username);
1139 GNUNET_free (ret->cfgfile);
1141 if ((hostkey != NULL) && (0 != UNLINK(hostkeyfile)))
1142 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1143 "unlink", hostkeyfile);
1144 GNUNET_free_non_null(hostkeyfile);
1145 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1146 GNUNET_free(servicehome);
1151 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1153 GNUNET_free_non_null(hostkeyfile);
1154 GNUNET_free(baseservicehome);
1155 GNUNET_free(servicehome);
1159 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1160 "No need to copy configuration file since we are running locally.\n");
1162 ret->phase = SP_COPIED;
1163 GNUNET_SCHEDULER_add_continuation (&start_fsm,
1165 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1166 GNUNET_free_non_null(hostkeyfile);
1167 GNUNET_free(servicehome);
1173 * Restart (stop and start) a GNUnet daemon.
1175 * @param d the daemon that should be restarted
1176 * @param cb function called once the daemon is (re)started
1177 * @param cb_cls closure for cb
1180 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1181 GNUNET_TESTING_NotifyDaemonRunning cb,
1190 d->dead = GNUNET_YES;
1197 if (d->phase == SP_CONFIG_UPDATE)
1199 GNUNET_SCHEDULER_cancel (d->task);
1200 d->phase = SP_START_DONE;
1202 if (d->server != NULL)
1204 GNUNET_CORE_disconnect (d->server);
1210 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1211 GNUNET_TRANSPORT_disconnect (d->th);
1214 /* state clean up and notifications */
1215 GNUNET_free_non_null (d->hello);
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1222 d->phase = SP_START_ARMING;
1224 /* Check if this is a local or remote process */
1225 if (NULL != d->hostname)
1228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1229 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1230 d->cfgfile, d->hostname);
1233 if (d->username != NULL)
1234 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1236 arg = GNUNET_strdup (d->hostname);
1238 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1246 "-c", d->cfgfile, "-e", "-r", NULL);
1247 /* Use -r to restart arm and all services */
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 "Stopping gnunet-arm with config `%s' locally.\n",
1258 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1263 "-c", d->cfgfile, "-e", "-r", NULL);
1266 GNUNET_free_non_null (del_arg);
1268 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1275 * Stops a GNUnet daemon.
1277 * @param d the daemon that should be stopped
1278 * @param timeout how long to wait for process for shutdown to complete
1279 * @param cb function called once the daemon was stopped
1280 * @param cb_cls closure for cb
1281 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1283 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1284 * GNUNET_NO to kill off and clean up for good
1287 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1288 struct GNUNET_TIME_Relative timeout,
1289 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1290 int delete_files, int allow_restart)
1295 d->dead_cb_cls = cb_cls;
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1303 d->dead = GNUNET_YES;
1307 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1309 /* Free what was left from churning! */
1310 GNUNET_assert (d->cfg != NULL);
1311 GNUNET_CONFIGURATION_destroy (d->cfg);
1312 if (delete_files == GNUNET_YES)
1314 if (0 != UNLINK (d->cfgfile))
1316 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1319 GNUNET_free (d->cfgfile);
1320 GNUNET_free_non_null (d->hostname);
1321 GNUNET_free_non_null (d->username);
1322 if (NULL != d->dead_cb)
1323 d->dead_cb (d->dead_cb_cls, NULL);
1329 if (delete_files == GNUNET_YES)
1331 GNUNET_asprintf (&del_arg, "-d");
1334 if (d->phase == SP_CONFIG_UPDATE)
1336 GNUNET_SCHEDULER_cancel (d->task);
1337 d->phase = SP_START_DONE;
1339 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1340 if (d->server != NULL)
1342 GNUNET_CORE_disconnect (d->server);
1346 /* shutdown ARM process (will terminate others) */
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1351 d->phase = SP_SHUTDOWN_START;
1352 d->running = GNUNET_NO;
1353 if (allow_restart == GNUNET_YES)
1354 d->churn = GNUNET_YES;
1357 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1358 GNUNET_TRANSPORT_disconnect (d->th);
1361 /* Check if this is a local or remote process */
1362 if (NULL != d->hostname)
1365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1366 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1367 d->cfgfile, d->hostname);
1370 if (d->username != NULL)
1371 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1373 arg = GNUNET_strdup (d->hostname);
1375 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1383 "-c", d->cfgfile, "-e", "-q",
1385 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1386 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1387 arg, "gnunet-arm", d->cfgfile, del_arg);
1388 /* Use -e to end arm, and -d to remove temp files */
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Stopping gnunet-arm with config `%s' locally.\n",
1398 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1403 "-c", d->cfgfile, "-e", "-q",
1407 GNUNET_free_non_null (del_arg);
1408 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1409 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1414 * Changes the configuration of a GNUnet daemon.
1416 * @param d the daemon that should be modified
1417 * @param cfg the new configuration for the daemon
1418 * @param cb function called once the configuration was changed
1419 * @param cb_cls closure for cb
1422 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1423 struct GNUNET_CONFIGURATION_Handle *cfg,
1424 GNUNET_TESTING_NotifyCompletion cb,
1429 if (d->phase != SP_START_DONE)
1434 ("Peer not yet running, can not change configuration at this point."));
1438 /* 1) write configuration to temporary file */
1439 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1442 cb (cb_cls, _("Failed to write new configuration to disk."));
1446 /* 2) copy file to remote host (if necessary) */
1447 if (NULL == d->hostname)
1449 /* signal success */
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Copying updated configuration file to remote host `%s'.\n",
1459 d->phase = SP_CONFIG_UPDATE;
1460 if (NULL != d->username)
1461 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1463 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1464 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1468 d->cfgfile, arg, NULL);
1470 if (NULL == d->proc)
1472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1474 ("Could not start `%s' process to copy configuration file.\n"),
1477 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1478 d->phase = SP_START_DONE;
1482 d->update_cb_cls = cb_cls;
1484 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1490 * Data kept for each pair of peers that we try
1493 struct ConnectContext
1496 * Testing handle to the first daemon.
1498 struct GNUNET_TESTING_Daemon *d1;
1501 * Handle to core of first daemon (to check connect)
1503 struct GNUNET_CORE_Handle *d1core;
1506 * Have we actually connected to the core of the first daemon yet?
1511 * Testing handle to the second daemon.
1513 struct GNUNET_TESTING_Daemon *d2;
1516 * Handler for the request to core to connect to this peer.
1518 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1521 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1523 struct GNUNET_TRANSPORT_Handle *d1th;
1526 * Function to call once we are done (or have timed out).
1528 GNUNET_TESTING_NotifyConnection cb;
1536 * The relative timeout from whence this connect attempt was
1537 * started. Allows for reconnect attempts.
1539 struct GNUNET_TIME_Relative relative_timeout;
1542 * Maximum number of connect attempts, will retry connection
1543 * this number of times on failures.
1545 unsigned int connect_attempts;
1548 * Hello timeout task
1550 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1553 * Connect timeout task
1555 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1558 * When should this operation be complete (or we must trigger
1561 struct GNUNET_TIME_Relative timeout_hello;
1564 * Was the connection attempt successful?
1569 * When connecting, do we need to send the HELLO?
1574 * The distance between the two connected peers
1580 /** Forward declaration **/
1582 reattempt_daemons_connect (void *cls,
1583 const struct GNUNET_SCHEDULER_TaskContext *tc);
1587 * Notify callback about success or failure of the attempt
1588 * to connect the two peers
1590 * @param cls our "struct ConnectContext" (freed)
1591 * @param tc reason tells us if we succeeded or failed
1594 notify_connect_result (void *cls,
1595 const struct GNUNET_SCHEDULER_TaskContext *tc)
1597 struct ConnectContext *ctx = cls;
1598 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1599 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1601 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1602 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1605 if (ctx->connect_request_handle != NULL)
1607 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1608 ctx->connect_request_handle = NULL;
1611 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1613 if (ctx->d1th != NULL)
1614 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1616 if (ctx->d1core != NULL)
1617 GNUNET_CORE_disconnect (ctx->d1core);
1619 if (ctx->d2core != NULL)
1620 GNUNET_CORE_disconnect (ctx->d2core);
1628 if (ctx->d1th != NULL)
1629 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1631 if (ctx->d1core != NULL)
1632 GNUNET_CORE_disconnect (ctx->d1core);
1635 if (ctx->connected == GNUNET_YES)
1637 if (ctx->cb != NULL)
1639 ctx->cb (ctx->cb_cls,
1643 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1646 else if (ctx->connect_attempts > 0)
1648 ctx->d1core_ready = GNUNET_NO;
1650 if (ctx->d2core != NULL)
1652 GNUNET_CORE_disconnect (ctx->d2core);
1656 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1661 if (ctx->cb != NULL)
1663 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1664 ctx->d2->cfg, ctx->d1, ctx->d2,
1665 _("Peers failed to connect"));
1674 * Success, connection is up. Signal client our success.
1676 * @param cls our "struct ConnectContext"
1677 * @param peer identity of the peer that has connected
1678 * @param atsi performance information
1682 connect_notify (void *cls,
1683 const struct GNUNET_PeerIdentity *peer,
1684 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1686 struct ConnectContext *ctx = cls;
1689 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1690 "Connected peer %s to peer %s\n",
1691 ctx->d1->shortname, GNUNET_i2s(peer));
1694 if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1697 ctx->connected = GNUNET_YES;
1698 ctx->distance = 0; /* FIXME: distance */
1699 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1701 GNUNET_SCHEDULER_cancel(ctx->hello_send_task);
1702 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1704 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1705 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1712 * Success, connection is up. Signal client our success.
1714 * @param cls our "struct ConnectContext"
1715 * @param peer identity of the peer that has connected
1716 * @param atsi performance information
1720 connect_notify_core2 (void *cls,
1721 const struct GNUNET_PeerIdentity *peer,
1722 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1724 struct ConnectContext *ctx = cls;
1726 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
1728 ctx->connected = GNUNET_YES;
1729 ctx->distance = 0; /* FIXME: distance */
1730 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1731 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1739 * Task called once a core connect request has been transmitted.
1741 * @param cls struct ConnectContext
1742 * @param success was the request successful?
1745 core_connect_request_cont (void *cls,
1748 struct ConnectContext *ctx = cls;
1750 ctx->connect_request_handle = NULL;
1754 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1756 struct ConnectContext *ctx = cls;
1757 struct GNUNET_MessageHeader *hello;
1758 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1759 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1761 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
1762 && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello)))
1764 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1765 GNUNET_assert (hello != NULL);
1766 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1767 GNUNET_assert (ctx->d1core != NULL);
1768 ctx->connect_request_handle =
1769 GNUNET_CORE_peer_request_connect (ctx->d1core,
1770 ctx->relative_timeout,
1772 &core_connect_request_cont, ctx);
1775 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1776 "Sending connect request to CORE of %s for peer %s\n",
1777 GNUNET_i2s (&ctx->d1->id),
1778 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1780 ctx->timeout_hello =
1781 GNUNET_TIME_relative_add (ctx->timeout_hello,
1782 GNUNET_TIME_relative_multiply
1783 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1785 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello,
1790 * Notify of a successful connection to the core service.
1792 * @param cls a ConnectContext
1793 * @param server handle to the core service
1794 * @param my_identity the peer identity of this peer
1795 * @param publicKey the public key of the peer
1798 core_init_notify (void *cls,
1799 struct GNUNET_CORE_Handle * server,
1800 const struct GNUNET_PeerIdentity *
1803 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1806 struct ConnectContext *connect_ctx = cls;
1807 connect_ctx->d1core_ready = GNUNET_YES;
1809 if (connect_ctx->send_hello == GNUNET_NO)
1811 connect_ctx->connect_request_handle =
1812 GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
1813 connect_ctx->relative_timeout,
1814 &connect_ctx->d2->id,
1815 &core_connect_request_cont, connect_ctx);
1816 GNUNET_assert(connect_ctx->connect_request_handle != NULL);
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1819 "Sending connect request to CORE of %s for peer %s\n",
1820 connect_ctx->d1->shortname,
1821 connect_ctx->d2->shortname);
1829 reattempt_daemons_connect (void *cls,
1830 const struct GNUNET_SCHEDULER_TaskContext *tc)
1832 struct ConnectContext *ctx = cls;
1833 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1838 #if DEBUG_TESTING_RECONNECT
1839 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1840 "re-attempting connect of peer %s to peer %s\n",
1841 ctx->d1->shortname, ctx->d2->shortname);
1843 ctx->connect_attempts--;
1844 GNUNET_assert (ctx->d1core == NULL);
1845 ctx->d1core_ready = GNUNET_NO;
1846 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1849 &connect_notify, NULL, NULL,
1851 NULL, GNUNET_NO, no_handlers);
1852 if (ctx->d1core == NULL)
1854 if (NULL != ctx->cb)
1855 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1856 ctx->d2->cfg, ctx->d1, ctx->d2,
1857 _("Failed to connect to core service of first peer!\n"));
1862 if (ctx->send_hello == GNUNET_YES)
1864 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1866 ctx->d1, NULL, NULL, NULL);
1867 if (ctx->d1th == NULL)
1869 GNUNET_CORE_disconnect (ctx->d1core);
1871 if (NULL != ctx->cb)
1872 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1873 ctx->d2->cfg, ctx->d1, ctx->d2,
1874 _("Failed to connect to transport service!\n"));
1877 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1881 ctx->connect_request_handle =
1882 GNUNET_CORE_peer_request_connect (ctx->d1core,
1883 ctx->relative_timeout,
1885 &core_connect_request_cont, ctx);
1888 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1889 ¬ify_connect_result, ctx);
1893 * Iterator for currently known peers, to ensure
1894 * that we don't try to send duplicate connect
1897 * @param cls our "struct ConnectContext"
1898 * @param peer identity of the peer that has connected,
1899 * NULL when iteration has finished
1900 * @param atsi performance information
1904 core_initial_iteration (void *cls,
1905 const struct GNUNET_PeerIdentity *peer,
1906 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1908 struct ConnectContext *ctx = cls;
1910 if ((peer != NULL) &&
1911 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
1913 ctx->connected = GNUNET_YES;
1914 ctx->distance = 0; /* FIXME: distance */
1917 else if (peer == NULL) /* End of iteration over peers */
1919 if (ctx->connected == GNUNET_YES)
1921 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1926 /* Peer not already connected, need to schedule connect request! */
1927 if (ctx->d1core == NULL)
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931 "Peers are NOT connected, connecting to core!\n");
1933 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1936 &connect_notify, NULL, NULL,
1938 NULL, GNUNET_NO, no_handlers);
1941 if (ctx->d1core == NULL)
1944 if (NULL != ctx->cb)
1945 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1946 _("Failed to connect to core service of first peer!\n"));
1950 if (ctx->send_hello == GNUNET_YES)
1952 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1953 &ctx->d1->id, ctx->d1, NULL, NULL, NULL);
1954 if (ctx->d1th == NULL)
1956 GNUNET_CORE_disconnect (ctx->d1core);
1958 if (NULL != ctx->cb)
1959 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1960 _("Failed to connect to transport service!\n"));
1963 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1967 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1968 ¬ify_connect_result, ctx);
1974 * Establish a connection between two GNUnet daemons.
1976 * @param d1 handle for the first daemon
1977 * @param d2 handle for the second daemon
1978 * @param timeout how long is the connection attempt
1980 * @param max_connect_attempts how many times should we try to reconnect
1982 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
1983 * the HELLO has already been exchanged
1984 * @param cb function to call at the end
1985 * @param cb_cls closure for cb
1988 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
1989 struct GNUNET_TESTING_Daemon *d2,
1990 struct GNUNET_TIME_Relative timeout,
1991 unsigned int max_connect_attempts,
1993 GNUNET_TESTING_NotifyConnection cb,
1996 struct ConnectContext *ctx;
1998 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2001 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2002 _("Peers are not fully running yet, can not connect!\n"));
2003 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2007 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
2010 ctx->timeout_hello =
2011 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2012 ctx->relative_timeout = GNUNET_TIME_relative_divide(timeout, max_connect_attempts);
2014 ctx->cb_cls = cb_cls;
2015 ctx->connect_attempts = max_connect_attempts;
2016 ctx->connected = GNUNET_NO;
2017 ctx->send_hello = send_hello;
2019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2020 "Asked to connect peer %s to peer %s\n",
2021 d1->shortname, d2->shortname);
2024 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2025 GNUNET_assert(GNUNET_OK == GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id, &core_initial_iteration, ctx));
2026 /*GNUNET_assert(GNUNET_OK == GNUNET_CORE_iterate_peers (ctx->d1->cfg, &core_initial_iteration, ctx));*/
2029 /* end of testing.c */