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
42 #define DEBUG_TESTING_RECONNECT GNUNET_NO
44 #define WAIT_FOR_HELLO GNUNET_NO
47 * Hack to deal with initial HELLO's being often devoid of addresses.
48 * This hack causes 'process_hello' to ignore HELLOs without addresses.
49 * The correct implementation would continue with 'process_hello' until
50 * the connection could be established...
52 #define EMPTY_HACK GNUNET_YES
55 * How long do we wait after starting gnunet-service-arm
56 * for the core service to be alive?
58 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
61 * How many times are we willing to try to wait for "scp" or
62 * "gnunet-service-arm" to complete (waitpid) before giving up?
64 #define MAX_EXEC_WAIT_RUNS 250
66 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
70 test_address (void *cls,
72 struct GNUNET_TIME_Absolute expiration,
83 * Receive the HELLO from one peer, give it to the other
84 * and ask them to connect.
86 * @param cls "struct ConnectContext"
87 * @param message HELLO message of peer
90 process_hello (void *cls,
91 const struct GNUNET_MessageHeader *message)
93 struct GNUNET_TESTING_Daemon *daemon = cls;
96 GNUNET_TESTING_NotifyDaemonRunning cb;
102 GNUNET_assert (message != NULL);
103 GNUNET_HELLO_iterate_addresses ((const struct GNUNET_HELLO_Message*) message,
107 if (GNUNET_YES == empty)
110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
111 "Skipping empty HELLO address\n");
119 GNUNET_assert (daemon->phase == SP_GET_HELLO || daemon->phase == SP_START_DONE);
124 if (daemon->task != GNUNET_SCHEDULER_NO_TASK) /* Assertion here instead? */
125 GNUNET_SCHEDULER_cancel(daemon->task);
127 if (daemon->server != NULL)
130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
131 "Received `%s' from transport service of `%4s', disconnecting core!\n",
132 "HELLO", GNUNET_i2s (&daemon->id));
134 GNUNET_CORE_disconnect (daemon->server);
135 daemon->server = NULL;
138 msize = ntohs (message->size);
143 if (daemon->th != NULL)
145 GNUNET_TRANSPORT_get_hello_cancel (daemon->th, &process_hello, daemon);
148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
149 "Received `%s' from transport service of `%4s'\n",
150 "HELLO", GNUNET_i2s (&daemon->id));
153 GNUNET_free_non_null (daemon->hello);
154 daemon->hello = GNUNET_malloc (msize);
155 memcpy (daemon->hello, message, msize);
157 if (daemon->th != NULL)
159 GNUNET_TRANSPORT_disconnect (daemon->th);
162 daemon->phase = SP_START_DONE;
165 if (NULL != cb) /* FIXME: what happens when this callback calls GNUNET_TESTING_daemon_stop? */
174 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
178 * Function called after GNUNET_CORE_connect has succeeded
179 * (or failed for good). Note that the private key of the
180 * peer is intentionally not exposed here; if you need it,
181 * your process should try to read the private key file
182 * directly (which should work if you are authorized...).
185 * @param server handle to the server, NULL if we failed
186 * @param my_identity ID of this peer, NULL if we failed
187 * @param publicKey public key of this peer, NULL if we failed
190 testing_init (void *cls,
191 struct GNUNET_CORE_Handle *server,
192 const struct GNUNET_PeerIdentity *my_identity,
193 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
195 struct GNUNET_TESTING_Daemon *d = cls;
197 GNUNET_assert (d->phase == SP_START_CORE);
198 d->phase = SP_GET_HELLO;
203 if (GNUNET_YES == d->dead)
204 GNUNET_TESTING_daemon_stop (d,
205 GNUNET_TIME_absolute_get_remaining
206 (d->max_timeout), d->dead_cb,
207 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
208 else if (NULL != d->cb)
209 d->cb (d->cb_cls, NULL, d->cfg, d,
210 _("Failed to connect to core service\n"));
214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
215 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
217 d->id = *my_identity; /* FIXME: shouldn't we already have this from reading the hostkey file? */
218 if (d->shortname == NULL)
219 d->shortname = strdup (GNUNET_i2s (my_identity));
221 d->running = GNUNET_YES;
223 if (GNUNET_NO == d->running)
226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
227 "Peer is dead (d->running == GNUNET_NO)\n");
232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
233 "Successfully started peer `%4s', connecting to transport service.\n",
234 GNUNET_i2s (my_identity));
237 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
240 if (GNUNET_YES == d->dead)
241 GNUNET_TESTING_daemon_stop (d,
242 GNUNET_TIME_absolute_get_remaining
243 (d->max_timeout), d->dead_cb,
244 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
245 else if (NULL != d->cb)
246 d->cb (d->cb_cls, &d->id, d->cfg, d,
247 _("Failed to connect to transport service!\n"));
251 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
252 "Connected to transport service `%s', getting HELLO\n",
253 GNUNET_i2s (my_identity));
256 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
258 if (d->task != GNUNET_SCHEDULER_NO_TASK)
259 GNUNET_SCHEDULER_cancel(d->task);
261 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
268 * Notify of a peer being up and running. Scheduled as a task
269 * so that variables which may need to be set are set before
270 * the connect callback can set up new operations.
272 * @param cls the testing daemon
273 * @param tc task scheduler context
276 notify_daemon_started (void *cls,
277 const struct GNUNET_SCHEDULER_TaskContext *tc)
279 struct GNUNET_TESTING_Daemon *d = cls;
280 GNUNET_TESTING_NotifyDaemonRunning cb;
285 cb (d->cb_cls, &d->id, d->cfg, d, NULL);
290 * Finite-state machine for starting GNUnet.
292 * @param cls our "struct GNUNET_TESTING_Daemon"
296 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
298 struct GNUNET_TESTING_Daemon *d = cls;
299 GNUNET_TESTING_NotifyDaemonRunning cb;
300 enum GNUNET_OS_ProcessStatusType type;
306 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
307 "Peer FSM is in phase %u.\n", d->phase);
310 d->task = GNUNET_SCHEDULER_NO_TASK;
314 /* confirm copying complete */
315 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
317 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
327 ("`scp' does not seem to terminate (timeout copying config).\n"));
332 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
336 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
342 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
345 GNUNET_OS_process_close(d->proc);
347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
348 "Successfully copied configuration file.\n");
350 d->phase = SP_COPIED;
353 /* Start create hostkey process if we don't already know the peer identity!*/
354 if (GNUNET_NO == d->have_hostkey)
356 d->pipe_stdout = GNUNET_DISK_pipe (GNUNET_NO, GNUNET_NO, GNUNET_YES);
357 if (d->pipe_stdout == NULL)
366 (NULL == d->hostname)
367 ? _("Failed to create pipe for `gnunet-peerinfo' process.\n")
368 : _("Failed to create pipe for `ssh' process.\n"));
371 if (NULL == d->hostname)
374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
375 "Starting `%s', with command `%s %s %s %s'.\n",
376 "gnunet-peerinfo", "gnunet-peerinfo", "-c", d->cfgfile,
380 GNUNET_OS_start_process (NULL, d->pipe_stdout, "gnunet-peerinfo",
381 "gnunet-peerinfo", "-c", d->cfgfile,
383 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
384 GNUNET_DISK_PIPE_END_WRITE);
388 if (d->username != NULL)
389 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
391 dst = GNUNET_strdup (d->hostname);
394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
395 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
396 "gnunet-peerinfo", "ssh", dst, "gnunet-peerinfo", "-c",
399 if (d->ssh_port_str == NULL)
401 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
408 "-c", d->cfgfile, "-sq",
413 d->proc = GNUNET_OS_start_process (NULL, d->pipe_stdout, "ssh",
414 "ssh", "-p", d->ssh_port_str,
420 "-c", d->cfgfile, "-sq",
423 GNUNET_DISK_pipe_close_end (d->pipe_stdout,
424 GNUNET_DISK_PIPE_END_WRITE);
429 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
430 _("Could not start `%s' process to create hostkey.\n"),
431 (NULL == d->hostname) ? "gnunet-peerinfo" : "ssh");
439 (NULL == d->hostname)
440 ? _("Failed to start `gnunet-peerinfo' process.\n")
441 : _("Failed to start `ssh' process.\n"));
442 GNUNET_DISK_pipe_close (d->pipe_stdout);
446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
447 "Started `%s', waiting for hostkey.\n", "gnunet-peerinfo");
449 d->phase = SP_HOSTKEY_CREATE;
452 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
454 GNUNET_DISK_pipe_handle
456 GNUNET_DISK_PIPE_END_READ),
459 else /* Already have a hostkey! */
461 if (d->hostkey_callback != NULL)
463 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
464 d->hostkey_callback = NULL;
465 d->phase = SP_HOSTKEY_CREATED;
468 d->phase = SP_TOPOLOGY_SETUP;
472 = GNUNET_SCHEDULER_add_now (&start_fsm, d);
475 case SP_HOSTKEY_CREATE:
477 GNUNET_DISK_file_read (GNUNET_DISK_pipe_handle
478 (d->pipe_stdout, GNUNET_DISK_PIPE_END_READ),
479 &d->hostkeybuf[d->hostkeybufpos],
480 sizeof (d->hostkeybuf) - d->hostkeybufpos);
482 d->hostkeybufpos += bytes_read;
484 if ((d->hostkeybufpos < 104) && (bytes_read > 0))
489 GNUNET_SCHEDULER_add_read_file (GNUNET_TIME_absolute_get_remaining
491 GNUNET_DISK_pipe_handle
493 GNUNET_DISK_PIPE_END_READ),
497 d->hostkeybuf[103] = '\0';
499 if ((bytes_read < 0) ||
500 (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (d->hostkeybuf,
505 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
506 _("Error reading from gnunet-peerinfo: %s\n"),
509 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
510 _("Malformed output from gnunet-peerinfo!\n"));
513 GNUNET_DISK_pipe_close (d->pipe_stdout);
514 d->pipe_stdout = NULL;
515 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
516 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
517 GNUNET_OS_process_close (d->proc);
520 cb (d->cb_cls, NULL, d->cfg, d, _("`Failed to get hostkey!\n"));
523 d->shortname = GNUNET_strdup(GNUNET_i2s(&d->id));
524 GNUNET_DISK_pipe_close (d->pipe_stdout);
525 d->pipe_stdout = NULL;
526 (void) GNUNET_OS_process_kill (d->proc, SIGKILL);
527 GNUNET_break (GNUNET_OK == GNUNET_OS_process_wait (d->proc));
528 GNUNET_OS_process_close (d->proc);
530 d->have_hostkey = GNUNET_YES;
531 if (d->hostkey_callback != NULL)
533 d->hostkey_callback (d->hostkey_cls, &d->id, d, NULL);
534 d->hostkey_callback = NULL;
535 d->phase = SP_HOSTKEY_CREATED;
539 d->phase = SP_TOPOLOGY_SETUP;
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Successfully got hostkey!\n");
545 case SP_HOSTKEY_CREATED:
546 /* wait for topology finished */
547 if ((GNUNET_YES == d->dead)
548 || (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
556 d->cfg, d, _("`Failed while waiting for topology setup!\n"));
561 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
564 case SP_TOPOLOGY_SETUP: /* Indicates topology setup has completed! */
565 /* start GNUnet on remote host */
566 if (NULL == d->hostname)
569 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
570 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
571 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
572 "-L", "DEBUG", "-s");
574 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
575 "gnunet-arm", "-c", d->cfgfile,
583 if (d->username != NULL)
584 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
586 dst = GNUNET_strdup (d->hostname);
589 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
590 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
591 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c",
592 d->cfgfile, "-L", "DEBUG", "-s", "-q");
594 if (d->ssh_port_str == NULL)
596 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
604 "-c", d->cfgfile, "-s", "-q",
610 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh",
611 "ssh", "-p", d->ssh_port_str,
619 "-c", d->cfgfile, "-s", "-q",
626 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
627 _("Could not start `%s' process to start GNUnet.\n"),
628 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
636 (NULL == d->hostname)
637 ? _("Failed to start `gnunet-arm' process.\n")
638 : _("Failed to start `ssh' process.\n"));
642 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
643 "Started `%s', waiting for `%s' to be up.\n",
644 "gnunet-arm", "gnunet-service-core");
646 d->phase = SP_START_ARMING;
648 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
651 case SP_START_ARMING:
652 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
654 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
664 (NULL == d->hostname)
665 ? _("`gnunet-arm' does not seem to terminate.\n")
666 : _("`ssh' does not seem to terminate.\n"));
667 GNUNET_CONFIGURATION_destroy (d->cfg);
668 GNUNET_free (d->cfgfile);
669 GNUNET_free_non_null (d->hostname);
670 GNUNET_free_non_null (d->username);
671 GNUNET_free(d->proc);
677 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
682 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
683 "Successfully started `%s'.\n", "gnunet-arm");
685 GNUNET_free(d->proc);
686 d->phase = SP_START_CORE;
688 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
689 "Calling CORE_connect\n");
693 if (d->server != NULL)
694 GNUNET_CORE_disconnect(d->server);
697 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
707 _("Unable to connect to CORE service for peer!\n"));
708 GNUNET_CONFIGURATION_destroy (d->cfg);
709 GNUNET_free (d->cfgfile);
710 GNUNET_free_non_null (d->hostname);
711 GNUNET_free_non_null (d->username);
715 d->server = GNUNET_CORE_connect (d->cfg, 1,
720 NULL, GNUNET_NO, no_handlers);
722 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
725 d->th = GNUNET_TRANSPORT_connect (d->cfg, &d->id, d, NULL, NULL, NULL);
728 if (GNUNET_YES == d->dead)
729 GNUNET_TESTING_daemon_stop (d,
730 GNUNET_TIME_absolute_get_remaining
731 (d->max_timeout), d->dead_cb,
732 d->dead_cb_cls, GNUNET_YES, GNUNET_NO);
733 else if (NULL != d->cb)
734 d->cb (d->cb_cls, &d->id, d->cfg, d,
735 _("Failed to connect to transport service!\n"));
739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
740 "Connected to transport service `%s', getting HELLO\n",
741 GNUNET_i2s (&d->id));
744 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
745 GNUNET_SCHEDULER_add_now (¬ify_daemon_started, d);
749 cb (d->cb_cls, &d->id, d->cfg, d, NULL);*/
750 d->running = GNUNET_YES;
751 d->phase = SP_GET_HELLO;
755 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
758 if (d->server != NULL)
759 GNUNET_CORE_disconnect(d->server);
761 GNUNET_TRANSPORT_disconnect(d->th);
769 _("Unable to get HELLO for peer!\n"));
770 GNUNET_CONFIGURATION_destroy (d->cfg);
771 GNUNET_free (d->cfgfile);
772 GNUNET_free_non_null (d->hostname);
773 GNUNET_free_non_null (d->username);
777 if (d->hello != NULL)
779 GNUNET_assert(d->task == GNUNET_SCHEDULER_NO_TASK);
781 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_relative_multiply(GNUNET_CONSTANTS_SERVICE_RETRY, 2),
787 case SP_SHUTDOWN_START:
788 /* confirm copying complete */
789 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
791 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value ==
794 if (NULL != d->dead_cb)
795 d->dead_cb (d->dead_cb_cls,
797 ("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
800 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello,
802 GNUNET_TRANSPORT_disconnect (d->th);
805 GNUNET_CONFIGURATION_destroy (d->cfg);
806 GNUNET_free (d->cfgfile);
807 GNUNET_free_non_null (d->hello);
808 GNUNET_free_non_null (d->hostname);
809 GNUNET_free_non_null (d->username);
810 GNUNET_free_non_null (d->shortname);
811 GNUNET_free_non_null (d->proc);
818 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
822 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
824 if (NULL != d->dead_cb)
825 d->dead_cb (d->dead_cb_cls,
827 ("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
830 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
831 GNUNET_TRANSPORT_disconnect (d->th);
834 if (d->server != NULL)
836 GNUNET_CORE_disconnect (d->server);
839 GNUNET_CONFIGURATION_destroy (d->cfg);
840 GNUNET_free (d->cfgfile);
841 GNUNET_free_non_null (d->hello);
842 GNUNET_free_non_null (d->hostname);
843 GNUNET_free_non_null (d->username);
844 GNUNET_free_non_null (d->shortname);
845 GNUNET_free_non_null (d->proc);
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
853 if (d->server != NULL)
855 GNUNET_CORE_disconnect (d->server);
861 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
862 GNUNET_TRANSPORT_disconnect (d->th);
866 if (NULL != d->dead_cb)
867 d->dead_cb (d->dead_cb_cls, NULL);
869 /* state clean up and notifications */
870 if (d->churn == GNUNET_NO)
872 GNUNET_CONFIGURATION_destroy (d->cfg);
873 GNUNET_free (d->cfgfile);
874 GNUNET_free_non_null (d->hostname);
875 GNUNET_free_non_null (d->username);
878 GNUNET_free_non_null (d->hello);
880 GNUNET_free_non_null (d->shortname);
881 GNUNET_free_non_null (d->proc);
884 if (d->churn == GNUNET_NO)
888 case SP_CONFIG_UPDATE:
889 /* confirm copying complete */
890 if (GNUNET_OK != GNUNET_OS_process_status (d->proc, &type, &code))
892 if (GNUNET_TIME_absolute_get_remaining (d->max_timeout).rel_value == 0) /* FIXME: config update should take timeout parameter! */
899 d->cfg, d, _("`scp' does not seem to terminate.\n"));
904 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
908 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
910 if (NULL != d->update_cb)
911 d->update_cb (d->update_cb_cls,
912 _("`scp' did not complete cleanly.\n"));
916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
917 "Successfully copied configuration file.\n");
919 if (NULL != d->update_cb)
920 d->update_cb (d->update_cb_cls, NULL);
921 d->phase = SP_START_DONE;
927 * Continues GNUnet daemon startup when user wanted to be notified
928 * once a hostkey was generated (for creating friends files, blacklists,
931 * @param daemon the daemon to finish starting
934 GNUNET_TESTING_daemon_continue_startup (struct GNUNET_TESTING_Daemon *daemon)
936 GNUNET_assert (daemon->phase == SP_HOSTKEY_CREATED);
937 daemon->phase = SP_TOPOLOGY_SETUP;
941 * Check whether the given daemon is running.
943 * @param daemon the daemon to check
945 * @return GNUNET_YES if the daemon is up, GNUNET_NO if the
946 * daemon is down, GNUNET_SYSERR on error.
949 GNUNET_TESTING_daemon_running (struct GNUNET_TESTING_Daemon *daemon)
952 return GNUNET_SYSERR;
954 if (daemon->running == GNUNET_YES)
961 * Start a peer that has previously been stopped using the daemon_stop
962 * call (and files weren't deleted and the allow restart flag)
964 * @param daemon the daemon to start (has been previously stopped)
965 * @param timeout how long to wait for restart
966 * @param cb the callback for notification when the peer is running
967 * @param cb_cls closure for the callback
970 GNUNET_TESTING_daemon_start_stopped (struct GNUNET_TESTING_Daemon *daemon,
971 struct GNUNET_TIME_Relative timeout,
972 GNUNET_TESTING_NotifyDaemonRunning cb,
975 if (daemon->running == GNUNET_YES)
977 cb (cb_cls, &daemon->id, daemon->cfg, daemon,
978 "Daemon already running, can't restart!");
983 daemon->cb_cls = cb_cls;
984 daemon->phase = SP_TOPOLOGY_SETUP;
985 daemon->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
987 GNUNET_SCHEDULER_add_continuation (&start_fsm,
989 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
993 * Starts a GNUnet daemon. GNUnet must be installed on the target
994 * system and available in the PATH. The machine must furthermore be
995 * reachable via "ssh" (unless the hostname is "NULL") without the
996 * need to enter a password.
998 * @param cfg configuration to use
999 * @param timeout how long to wait starting up peers
1000 * @param pretend GNUNET_YES to set up files but not start peer GNUNET_NO
1001 * to really start the peer (default)
1002 * @param hostname name of the machine where to run GNUnet
1003 * (use NULL for localhost).
1004 * @param ssh_username ssh username to use when connecting to hostname
1005 * @param sshport port to pass to ssh process when connecting to hostname
1006 * @param hostkey pointer to a hostkey to be written to disk (instead of being generated)
1007 * @param hostkey_callback function to call once the hostkey has been
1008 * generated for this peer, but it hasn't yet been started
1009 * (NULL to start immediately, otherwise waits on GNUNET_TESTING_daemon_continue_start)
1010 * @param hostkey_cls closure for hostkey callback
1011 * @param cb function to call once peer is up, or failed to start
1012 * @param cb_cls closure for cb
1013 * @return handle to the daemon (actual start will be completed asynchronously)
1015 struct GNUNET_TESTING_Daemon *
1016 GNUNET_TESTING_daemon_start (const struct GNUNET_CONFIGURATION_Handle *cfg,
1017 struct GNUNET_TIME_Relative timeout,
1019 const char *hostname,
1020 const char *ssh_username,
1022 const char *hostkey,
1023 GNUNET_TESTING_NotifyHostkeyCreated
1024 hostkey_callback, void *hostkey_cls,
1025 GNUNET_TESTING_NotifyDaemonRunning cb,
1028 struct GNUNET_TESTING_Daemon *ret;
1032 char *baseservicehome;
1035 char *temp_file_name;
1036 struct GNUNET_DISK_FileHandle *fn;
1037 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
1038 struct GNUNET_CRYPTO_RsaPrivateKey *private_key;
1040 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
1041 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
1044 GNUNET_asprintf (&ret->ssh_port_str, "%d", sshport);
1047 ret->ssh_port_str = NULL;
1049 /* Find service home and base service home directories, create it if it doesn't exist */
1050 GNUNET_assert(GNUNET_OK ==
1051 GNUNET_CONFIGURATION_get_value_string (cfg,
1056 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_create (servicehome));
1057 GNUNET_asprintf(&temp_file_name, "%s/gnunet-testing-config", servicehome);
1058 ret->cfgfile = GNUNET_DISK_mktemp (temp_file_name);
1059 GNUNET_free(temp_file_name);
1061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1062 "Setting up peer with configuration file `%s'.\n",
1065 if (NULL == ret->cfgfile)
1067 GNUNET_free_non_null (ret->ssh_port_str);
1068 GNUNET_free_non_null (ret->hostname);
1072 ret->hostkey_callback = hostkey_callback;
1073 ret->hostkey_cls = hostkey_cls;
1075 ret->cb_cls = cb_cls;
1076 ret->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1077 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
1078 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
1080 "DEFAULTCONFIG", ret->cfgfile);
1082 if (hostkey != NULL) /* Get the peer identity from the hostkey */
1084 private_key = GNUNET_CRYPTO_rsa_decode_key(hostkey, HOSTKEYFILESIZE);
1085 GNUNET_assert(private_key != NULL);
1086 GNUNET_CRYPTO_rsa_key_get_public (private_key,
1088 GNUNET_CRYPTO_hash(&public_key, sizeof(struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded), &ret->id.hashPubKey);
1089 ret->shortname = GNUNET_strdup(GNUNET_i2s(&ret->id));
1090 ret->have_hostkey = GNUNET_YES;
1091 GNUNET_CRYPTO_rsa_key_free(private_key);
1094 /* Write hostkey to file, if we were given one */
1096 if (hostkey != NULL)
1098 GNUNET_asprintf(&hostkeyfile, "%s/.hostkey", servicehome);
1100 GNUNET_DISK_file_open (hostkeyfile,
1101 GNUNET_DISK_OPEN_READWRITE
1102 | GNUNET_DISK_OPEN_CREATE,
1103 GNUNET_DISK_PERM_USER_READ |
1104 GNUNET_DISK_PERM_USER_WRITE);
1105 GNUNET_assert(fn != NULL);
1106 GNUNET_assert(HOSTKEYFILESIZE == GNUNET_DISK_file_write(fn, hostkey, HOSTKEYFILESIZE));
1107 GNUNET_assert(GNUNET_OK == GNUNET_DISK_file_close(fn));
1110 /* write configuration to temporary file */
1111 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
1113 if (0 != UNLINK (ret->cfgfile))
1114 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1115 "unlink", ret->cfgfile);
1116 GNUNET_CONFIGURATION_destroy (ret->cfg);
1117 GNUNET_free_non_null (ret->hostname);
1118 GNUNET_free (ret->cfgfile);
1122 if (ssh_username != NULL)
1123 username = GNUNET_strdup (ssh_username);
1124 if ((ssh_username == NULL) && (GNUNET_OK !=
1125 GNUNET_CONFIGURATION_get_value_string (cfg,
1130 if (NULL != getenv ("USER"))
1131 username = GNUNET_strdup (getenv ("USER"));
1135 ret->username = username;
1137 if (GNUNET_NO == pretend) /* Copy files, enter finite state machine */
1139 /* copy directory to remote host */
1140 if (NULL != hostname)
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144 "Copying configuration directory to host `%s'.\n", hostname);
1146 baseservicehome = GNUNET_strdup(servicehome);
1147 /* Remove trailing /'s */
1148 while (baseservicehome[strlen(baseservicehome) - 1] == '/')
1149 baseservicehome[strlen(baseservicehome) - 1] = '\0';
1150 /* Find next directory /, jump one ahead */
1151 slash = strrchr(baseservicehome, '/');
1155 ret->phase = SP_COPYING;
1156 if (NULL != username)
1157 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, baseservicehome);
1159 GNUNET_asprintf (&arg, "%s:%s", hostname, baseservicehome);
1161 if (ret->ssh_port_str == NULL)
1163 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", "-r",
1167 servicehome, arg, NULL);
1169 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "copying directory with command scp -r %s %s\n", servicehome, arg);
1174 ret->proc = GNUNET_OS_start_process (NULL, NULL, "scp",
1175 "scp", "-r", "-P", ret->ssh_port_str,
1179 servicehome, arg, NULL);
1182 if (NULL == ret->proc)
1184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1186 ("Could not start `%s' process to copy configuration directory.\n"),
1188 if (0 != UNLINK (ret->cfgfile))
1189 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1190 "unlink", ret->cfgfile);
1191 GNUNET_CONFIGURATION_destroy (ret->cfg);
1192 GNUNET_free_non_null (ret->hostname);
1193 GNUNET_free_non_null (ret->username);
1194 GNUNET_free (ret->cfgfile);
1196 if ((hostkey != NULL) && (0 != UNLINK(hostkeyfile)))
1197 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
1198 "unlink", hostkeyfile);
1199 GNUNET_free_non_null(hostkeyfile);
1200 GNUNET_assert (GNUNET_OK == GNUNET_DISK_directory_remove (servicehome));
1201 GNUNET_free(servicehome);
1206 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1208 GNUNET_free_non_null(hostkeyfile);
1209 GNUNET_free(baseservicehome);
1210 GNUNET_free(servicehome);
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "No need to copy configuration file since we are running locally.\n");
1217 ret->phase = SP_COPIED;
1218 GNUNET_SCHEDULER_add_continuation (&start_fsm,
1220 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
1222 GNUNET_free_non_null(hostkeyfile);
1223 GNUNET_free(servicehome);
1229 * Restart (stop and start) a GNUnet daemon.
1231 * @param d the daemon that should be restarted
1232 * @param cb function called once the daemon is (re)started
1233 * @param cb_cls closure for cb
1236 GNUNET_TESTING_daemon_restart (struct GNUNET_TESTING_Daemon *d,
1237 GNUNET_TESTING_NotifyDaemonRunning cb,
1246 d->dead = GNUNET_YES;
1253 if (d->phase == SP_CONFIG_UPDATE)
1255 GNUNET_SCHEDULER_cancel (d->task);
1256 d->phase = SP_START_DONE;
1258 if (d->server != NULL)
1260 GNUNET_CORE_disconnect (d->server);
1266 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1267 GNUNET_TRANSPORT_disconnect (d->th);
1270 /* state clean up and notifications */
1271 GNUNET_free_non_null (d->hello);
1274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1275 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1278 d->phase = SP_START_ARMING;
1280 /* Check if this is a local or remote process */
1281 if (NULL != d->hostname)
1284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1285 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1286 d->cfgfile, d->hostname);
1289 if (d->username != NULL)
1290 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1292 arg = GNUNET_strdup (d->hostname);
1294 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1302 "-c", d->cfgfile, "-e", "-r", NULL);
1303 /* Use -r to restart arm and all services */
1310 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1311 "Stopping gnunet-arm with config `%s' locally.\n",
1314 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1319 "-c", d->cfgfile, "-e", "-r", NULL);
1322 GNUNET_free_non_null (del_arg);
1324 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1331 * Stops a GNUnet daemon.
1333 * @param d the daemon that should be stopped
1334 * @param timeout how long to wait for process for shutdown to complete
1335 * @param cb function called once the daemon was stopped
1336 * @param cb_cls closure for cb
1337 * @param delete_files GNUNET_YES to remove files, GNUNET_NO
1339 * @param allow_restart GNUNET_YES to restart peer later (using this API)
1340 * GNUNET_NO to kill off and clean up for good
1343 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
1344 struct GNUNET_TIME_Relative timeout,
1345 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls,
1346 int delete_files, int allow_restart)
1351 d->dead_cb_cls = cb_cls;
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1357 _("Setting d->dead on peer `%4s'\n"), GNUNET_i2s (&d->id));
1359 d->dead = GNUNET_YES;
1363 if ((d->running == GNUNET_NO) && (d->churn == GNUNET_YES)) /* Peer has already been stopped in churn context! */
1365 /* Free what was left from churning! */
1366 GNUNET_assert (d->cfg != NULL);
1367 GNUNET_CONFIGURATION_destroy (d->cfg);
1368 if (delete_files == GNUNET_YES)
1370 if (0 != UNLINK (d->cfgfile))
1372 GNUNET_log_strerror (GNUNET_ERROR_TYPE_WARNING, "unlink");
1375 GNUNET_free (d->cfgfile);
1376 GNUNET_free_non_null (d->hostname);
1377 GNUNET_free_non_null (d->username);
1378 if (NULL != d->dead_cb)
1379 d->dead_cb (d->dead_cb_cls, NULL);
1385 if (delete_files == GNUNET_YES)
1387 GNUNET_asprintf (&del_arg, "-d");
1390 if (d->phase == SP_CONFIG_UPDATE)
1392 GNUNET_SCHEDULER_cancel (d->task);
1393 d->phase = SP_START_DONE;
1395 /** Move this call to scheduled shutdown as fix for CORE_connect calling daemon_stop?
1396 if (d->server != NULL)
1398 GNUNET_CORE_disconnect (d->server);
1402 /* shutdown ARM process (will terminate others) */
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
1407 d->phase = SP_SHUTDOWN_START;
1408 d->running = GNUNET_NO;
1409 if (allow_restart == GNUNET_YES)
1410 d->churn = GNUNET_YES;
1413 GNUNET_TRANSPORT_get_hello_cancel (d->th, &process_hello, d);
1414 GNUNET_TRANSPORT_disconnect (d->th);
1417 /* Check if this is a local or remote process */
1418 if (NULL != d->hostname)
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1422 "Stopping gnunet-arm with config `%s' on host `%s'.\n",
1423 d->cfgfile, d->hostname);
1426 if (d->username != NULL)
1427 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
1429 arg = GNUNET_strdup (d->hostname);
1431 d->proc = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
1439 "-c", d->cfgfile, "-e", "-q",
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Stopping gnunet-arm with command ssh %s gnunet-arm -c %s -e -q %s\n",
1443 arg, "gnunet-arm", d->cfgfile, del_arg);
1444 /* Use -e to end arm, and -d to remove temp files */
1450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1451 "Stopping gnunet-arm with config `%s' locally.\n",
1454 d->proc = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
1459 "-c", d->cfgfile, "-e", "-q",
1463 GNUNET_free_non_null (del_arg);
1464 d->max_timeout = GNUNET_TIME_relative_to_absolute (timeout);
1465 d->task = GNUNET_SCHEDULER_add_now (&start_fsm, d);
1470 * Changes the configuration of a GNUnet daemon.
1472 * @param d the daemon that should be modified
1473 * @param cfg the new configuration for the daemon
1474 * @param cb function called once the configuration was changed
1475 * @param cb_cls closure for cb
1478 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
1479 struct GNUNET_CONFIGURATION_Handle *cfg,
1480 GNUNET_TESTING_NotifyCompletion cb,
1485 if (d->phase != SP_START_DONE)
1490 ("Peer not yet running, can not change configuration at this point."));
1494 /* 1) write configuration to temporary file */
1495 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
1498 cb (cb_cls, _("Failed to write new configuration to disk."));
1502 /* 2) copy file to remote host (if necessary) */
1503 if (NULL == d->hostname)
1505 /* signal success */
1511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1512 "Copying updated configuration file to remote host `%s'.\n",
1515 d->phase = SP_CONFIG_UPDATE;
1516 if (NULL != d->username)
1517 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
1519 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
1520 d->proc = GNUNET_OS_start_process (NULL, NULL, "scp", "scp",
1524 d->cfgfile, arg, NULL);
1526 if (NULL == d->proc)
1528 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1530 ("Could not start `%s' process to copy configuration file.\n"),
1533 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
1534 d->phase = SP_START_DONE;
1538 d->update_cb_cls = cb_cls;
1540 = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_EXEC_WAIT,
1546 * Data kept for each pair of peers that we try
1549 struct ConnectContext
1552 * Testing handle to the first daemon.
1554 struct GNUNET_TESTING_Daemon *d1;
1557 * Handle to core of first daemon (to check connect)
1559 struct GNUNET_CORE_Handle *d1core;
1562 * Have we actually connected to the core of the first daemon yet?
1567 * Testing handle to the second daemon.
1569 struct GNUNET_TESTING_Daemon *d2;
1572 * Handler for the request to core to connect to this peer.
1574 struct GNUNET_CORE_PeerRequestHandle *connect_request_handle;
1577 * Transport handle to the first daemon (to offer the HELLO of the second daemon to).
1579 struct GNUNET_TRANSPORT_Handle *d1th;
1582 * Function to call once we are done (or have timed out).
1584 GNUNET_TESTING_NotifyConnection cb;
1592 * The relative timeout from whence this connect attempt was
1593 * started. Allows for reconnect attempts.
1595 struct GNUNET_TIME_Relative relative_timeout;
1598 * Maximum number of connect attempts, will retry connection
1599 * this number of times on failures.
1601 unsigned int connect_attempts;
1604 * Hello timeout task
1606 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
1609 * Connect timeout task
1611 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
1614 * When should this operation be complete (or we must trigger
1617 struct GNUNET_TIME_Relative timeout_hello;
1620 * Was the connection attempt successful?
1625 * When connecting, do we need to send the HELLO?
1630 * The distance between the two connected peers
1636 /** Forward declaration **/
1638 reattempt_daemons_connect (void *cls,
1639 const struct GNUNET_SCHEDULER_TaskContext *tc);
1643 * Notify callback about success or failure of the attempt
1644 * to connect the two peers
1646 * @param cls our "struct ConnectContext" (freed)
1647 * @param tc reason tells us if we succeeded or failed
1650 notify_connect_result (void *cls,
1651 const struct GNUNET_SCHEDULER_TaskContext *tc)
1653 struct ConnectContext *ctx = cls;
1654 ctx->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1655 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1657 GNUNET_SCHEDULER_cancel (ctx->hello_send_task);
1658 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1661 if (ctx->connect_request_handle != NULL)
1663 GNUNET_CORE_peer_request_connect_cancel (ctx->connect_request_handle);
1664 ctx->connect_request_handle = NULL;
1667 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1669 if (ctx->d1th != NULL)
1670 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1672 if (ctx->d1core != NULL)
1673 GNUNET_CORE_disconnect (ctx->d1core);
1675 if (ctx->d2core != NULL)
1676 GNUNET_CORE_disconnect (ctx->d2core);
1684 if (ctx->d1th != NULL)
1685 GNUNET_TRANSPORT_disconnect (ctx->d1th);
1687 if (ctx->d1core != NULL)
1688 GNUNET_CORE_disconnect (ctx->d1core);
1691 if (ctx->connected == GNUNET_YES)
1693 if (ctx->cb != NULL)
1695 ctx->cb (ctx->cb_cls,
1699 ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
1702 else if (ctx->connect_attempts > 0)
1704 ctx->d1core_ready = GNUNET_NO;
1706 if (ctx->d2core != NULL)
1708 GNUNET_CORE_disconnect (ctx->d2core);
1712 GNUNET_SCHEDULER_add_now (&reattempt_daemons_connect, ctx);
1717 if (ctx->cb != NULL)
1719 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1720 ctx->d2->cfg, ctx->d1, ctx->d2,
1721 _("Peers failed to connect"));
1730 * Success, connection is up. Signal client our success.
1732 * @param cls our "struct ConnectContext"
1733 * @param peer identity of the peer that has connected
1734 * @param atsi performance information
1738 connect_notify (void *cls,
1739 const struct GNUNET_PeerIdentity *peer,
1740 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1742 struct ConnectContext *ctx = cls;
1745 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1746 "Connected peer %s to peer %s\n",
1747 ctx->d1->shortname, GNUNET_i2s(peer));
1750 if (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)))
1753 ctx->connected = GNUNET_YES;
1754 ctx->distance = 0; /* FIXME: distance */
1755 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
1757 GNUNET_SCHEDULER_cancel(ctx->hello_send_task);
1758 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1760 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1761 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1768 * Success, connection is up. Signal client our success.
1770 * @param cls our "struct ConnectContext"
1771 * @param peer identity of the peer that has connected
1772 * @param atsi performance information
1776 connect_notify_core2 (void *cls,
1777 const struct GNUNET_PeerIdentity *peer,
1778 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1780 struct ConnectContext *ctx = cls;
1782 if (memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity)) == 0)
1784 ctx->connected = GNUNET_YES;
1785 ctx->distance = 0; /* FIXME: distance */
1786 GNUNET_SCHEDULER_cancel (ctx->timeout_task);
1787 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
1795 * Task called once a core connect request has been transmitted.
1797 * @param cls struct ConnectContext
1798 * @param success was the request successful?
1801 core_connect_request_cont (void *cls,
1804 struct ConnectContext *ctx = cls;
1806 ctx->connect_request_handle = NULL;
1810 send_hello (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1812 struct ConnectContext *ctx = cls;
1813 struct GNUNET_MessageHeader *hello;
1814 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
1815 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1817 if ((ctx->d1core_ready == GNUNET_YES) && (ctx->d2->hello != NULL)
1818 && (NULL != GNUNET_HELLO_get_header (ctx->d2->hello))
1819 && (ctx->d1->phase == SP_START_DONE)
1820 && (ctx->d2->phase == SP_START_DONE))
1822 hello = GNUNET_HELLO_get_header (ctx->d2->hello);
1823 GNUNET_assert (hello != NULL);
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Offering hello of %s to %s\n", ctx->d2->shortname, ctx->d1->shortname);
1827 GNUNET_TRANSPORT_offer_hello (ctx->d1th, hello, NULL, NULL);
1828 GNUNET_assert (ctx->d1core != NULL);
1829 ctx->connect_request_handle =
1830 GNUNET_CORE_peer_request_connect (ctx->d1core,
1832 &core_connect_request_cont, ctx);
1835 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1836 "Sending connect request to CORE of %s for peer %s\n",
1837 GNUNET_i2s (&ctx->d1->id),
1838 GNUNET_h2s (&ctx->d2->id.hashPubKey));
1840 ctx->timeout_hello =
1841 GNUNET_TIME_relative_add (ctx->timeout_hello,
1842 GNUNET_TIME_relative_multiply
1843 (GNUNET_TIME_UNIT_MILLISECONDS, 500));
1845 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed (ctx->timeout_hello,
1850 * Notify of a successful connection to the core service.
1852 * @param cls a ConnectContext
1853 * @param server handle to the core service
1854 * @param my_identity the peer identity of this peer
1855 * @param publicKey the public key of the peer
1858 core_init_notify (void *cls,
1859 struct GNUNET_CORE_Handle * server,
1860 const struct GNUNET_PeerIdentity *
1863 GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *
1866 struct ConnectContext *connect_ctx = cls;
1867 connect_ctx->d1core_ready = GNUNET_YES;
1869 if (connect_ctx->send_hello == GNUNET_NO)
1871 connect_ctx->connect_request_handle =
1872 GNUNET_CORE_peer_request_connect (connect_ctx->d1core,
1873 &connect_ctx->d2->id,
1874 &core_connect_request_cont, connect_ctx);
1875 GNUNET_assert(connect_ctx->connect_request_handle != NULL);
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Sending connect request to CORE of %s for peer %s\n",
1879 connect_ctx->d1->shortname,
1880 connect_ctx->d2->shortname);
1888 reattempt_daemons_connect (void *cls,
1889 const struct GNUNET_SCHEDULER_TaskContext *tc)
1891 struct ConnectContext *ctx = cls;
1892 if ( (tc->reason & GNUNET_SCHEDULER_REASON_SHUTDOWN) != 0)
1897 #if DEBUG_TESTING_RECONNECT
1898 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1899 "re-attempting connect of peer %s to peer %s\n",
1900 ctx->d1->shortname, ctx->d2->shortname);
1902 ctx->connect_attempts--;
1903 GNUNET_assert (ctx->d1core == NULL);
1904 ctx->d1core_ready = GNUNET_NO;
1905 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
1908 &connect_notify, NULL, NULL,
1910 NULL, GNUNET_NO, no_handlers);
1911 if (ctx->d1core == NULL)
1913 if (NULL != ctx->cb)
1914 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1915 ctx->d2->cfg, ctx->d1, ctx->d2,
1916 _("Failed to connect to core service of first peer!\n"));
1921 /* Don't know reason for initial connect failure, update the HELLO for the second peer */
1922 if (NULL != ctx->d2->hello)
1924 GNUNET_free(ctx->d2->hello);
1925 ctx->d2->hello = NULL;
1926 if (NULL != ctx->d2->th)
1928 GNUNET_TRANSPORT_get_hello_cancel(ctx->d2->th, &process_hello, ctx->d2);
1929 GNUNET_TRANSPORT_disconnect(ctx->d2->th);
1931 ctx->d2->th = GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL, NULL);
1932 GNUNET_assert(ctx->d2->th != NULL);
1933 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
1936 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL))
1938 ctx->d2->th = GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL, NULL);
1939 if (ctx->d2->th == NULL)
1941 GNUNET_CORE_disconnect (ctx->d1core);
1943 if (NULL != ctx->cb)
1944 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1945 _("Failed to connect to transport service!\n"));
1948 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
1951 if (ctx->send_hello == GNUNET_YES)
1953 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
1955 ctx->d1, NULL, NULL, NULL);
1956 if (ctx->d1th == NULL)
1958 GNUNET_CORE_disconnect (ctx->d1core);
1960 if (NULL != ctx->cb)
1961 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg,
1962 ctx->d2->cfg, ctx->d1, ctx->d2,
1963 _("Failed to connect to transport service!\n"));
1966 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
1970 ctx->connect_request_handle =
1971 GNUNET_CORE_peer_request_connect (ctx->d1core,
1973 &core_connect_request_cont, ctx);
1976 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
1977 ¬ify_connect_result, ctx);
1981 * Iterator for currently known peers, to ensure
1982 * that we don't try to send duplicate connect
1985 * @param cls our "struct ConnectContext"
1986 * @param peer identity of the peer that has connected,
1987 * NULL when iteration has finished
1988 * @param atsi performance information
1992 core_initial_iteration (void *cls,
1993 const struct GNUNET_PeerIdentity *peer,
1994 const struct GNUNET_TRANSPORT_ATS_Information *atsi)
1996 struct ConnectContext *ctx = cls;
1998 if ((peer != NULL) &&
1999 (0 == memcmp (&ctx->d2->id, peer, sizeof (struct GNUNET_PeerIdentity))))
2001 ctx->connected = GNUNET_YES;
2002 ctx->distance = 0; /* FIXME: distance */
2005 else if (peer == NULL) /* End of iteration over peers */
2007 if (ctx->connected == GNUNET_YES)
2009 ctx->timeout_task = GNUNET_SCHEDULER_add_now (¬ify_connect_result,
2014 /* Peer not already connected, need to schedule connect request! */
2015 if (ctx->d1core == NULL)
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 "Peers are NOT connected, connecting to core!\n");
2021 ctx->d1core = GNUNET_CORE_connect (ctx->d1->cfg, 1,
2024 &connect_notify, NULL, NULL,
2026 NULL, GNUNET_NO, no_handlers);
2029 if (ctx->d1core == NULL)
2032 if (NULL != ctx->cb)
2033 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
2034 _("Failed to connect to core service of first peer!\n"));
2038 if ((NULL == ctx->d2->hello) && (ctx->d2->th == NULL)) /* Do not yet have the second peer's hello, set up a task to get it */
2040 ctx->d2->th = GNUNET_TRANSPORT_connect (ctx->d2->cfg, &ctx->d2->id, NULL, NULL, NULL, NULL);
2041 if (ctx->d2->th == NULL)
2043 GNUNET_CORE_disconnect (ctx->d1core);
2045 if (NULL != ctx->cb)
2046 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
2047 _("Failed to connect to transport service!\n"));
2050 GNUNET_TRANSPORT_get_hello (ctx->d2->th, &process_hello, ctx->d2);
2053 if (ctx->send_hello == GNUNET_YES)
2055 ctx->d1th = GNUNET_TRANSPORT_connect (ctx->d1->cfg,
2056 &ctx->d1->id, ctx->d1, NULL, NULL, NULL);
2057 if (ctx->d1th == NULL)
2059 GNUNET_CORE_disconnect (ctx->d1core);
2061 if (NULL != ctx->cb)
2062 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, 0, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
2063 _("Failed to connect to transport service!\n"));
2066 ctx->hello_send_task = GNUNET_SCHEDULER_add_now (&send_hello, ctx);
2070 GNUNET_SCHEDULER_add_delayed (ctx->relative_timeout,
2071 ¬ify_connect_result, ctx);
2077 * Establish a connection between two GNUnet daemons.
2079 * @param d1 handle for the first daemon
2080 * @param d2 handle for the second daemon
2081 * @param timeout how long is the connection attempt
2083 * @param max_connect_attempts how many times should we try to reconnect
2085 * @param send_hello GNUNET_YES to send the HELLO, GNUNET_NO to assume
2086 * the HELLO has already been exchanged
2087 * @param cb function to call at the end
2088 * @param cb_cls closure for cb
2091 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
2092 struct GNUNET_TESTING_Daemon *d2,
2093 struct GNUNET_TIME_Relative timeout,
2094 unsigned int max_connect_attempts,
2096 GNUNET_TESTING_NotifyConnection cb,
2099 struct ConnectContext *ctx;
2101 if ((d1->running == GNUNET_NO) || (d2->running == GNUNET_NO))
2104 cb (cb_cls, &d1->id, &d2->id, 0, d1->cfg, d2->cfg, d1, d2,
2105 _("Peers are not fully running yet, can not connect!\n"));
2106 GNUNET_log(GNUNET_ERROR_TYPE_WARNING, "Peers are not up!\n");
2110 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
2113 ctx->timeout_hello =
2114 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500);
2115 ctx->relative_timeout = GNUNET_TIME_relative_divide(timeout, max_connect_attempts);
2117 ctx->cb_cls = cb_cls;
2118 ctx->connect_attempts = max_connect_attempts;
2119 ctx->connected = GNUNET_NO;
2120 ctx->send_hello = send_hello;
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 "Asked to connect peer %s to peer %s\n",
2124 d1->shortname, d2->shortname);
2127 /* Core is up! Iterate over all _known_ peers first to check if we are already connected to the peer! */
2128 GNUNET_assert(GNUNET_OK == GNUNET_CORE_is_peer_connected (ctx->d1->cfg, &ctx->d2->id, &core_initial_iteration, ctx));
2129 /*GNUNET_assert(GNUNET_OK == GNUNET_CORE_iterate_peers (ctx->d1->cfg, &core_initial_iteration, ctx));*/
2132 /* end of testing.c */