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 2, 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
43 * How long do we wait after starting gnunet-service-arm
44 * for the core service to be alive?
46 #define ARM_START_WAIT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 120)
49 * How many times are we willing to try to wait for "scp" or
50 * "gnunet-service-arm" to complete (waitpid) before giving up?
52 #define MAX_EXEC_WAIT_RUNS 50
54 static struct GNUNET_CORE_MessageHandler no_handlers[] = { {NULL, 0, 0} };
57 * Receive the HELLO from one peer, give it to the other
58 * and ask them to connect.
60 * @param cls "struct ConnectContext"
61 * @param message HELLO message of peer
64 process_hello (void *cls, const struct GNUNET_MessageHeader *message)
66 struct GNUNET_TESTING_Daemon *daemon = cls;
67 GNUNET_TRANSPORT_get_hello_cancel(daemon->th, &process_hello, daemon);
69 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
70 "Received `%s' from transport service of `%4s'\n",
71 "HELLO", GNUNET_i2s (&daemon->id));
74 GNUNET_assert (message != NULL);
75 GNUNET_free_non_null(daemon->hello);
76 daemon->hello = GNUNET_malloc(ntohs(message->size));
77 memcpy(daemon->hello, message, ntohs(message->size));
79 if (daemon->th != NULL)
81 GNUNET_TRANSPORT_disconnect(daemon->th);
88 * Function called after GNUNET_CORE_connect has succeeded
89 * (or failed for good). Note that the private key of the
90 * peer is intentionally not exposed here; if you need it,
91 * your process should try to read the private key file
92 * directly (which should work if you are authorized...).
95 * @param server handle to the server, NULL if we failed
96 * @param my_identity ID of this peer, NULL if we failed
97 * @param publicKey public key of this peer, NULL if we failed
100 testing_init (void *cls,
101 struct GNUNET_CORE_Handle *server,
102 const struct GNUNET_PeerIdentity *my_identity,
103 const struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded *publicKey)
105 struct GNUNET_TESTING_Daemon *d = cls;
106 GNUNET_TESTING_NotifyDaemonRunning cb;
108 GNUNET_assert (d->phase == SP_START_CORE);
109 d->phase = SP_START_DONE;
115 if (GNUNET_YES == d->dead)
116 GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
118 cb (d->cb_cls, NULL, d->cfg, d,
119 _("Failed to connect to core service\n"));
123 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
124 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
126 d->id = *my_identity;
127 d->shortname = strdup (GNUNET_i2s (my_identity));
129 if (GNUNET_YES == d->dead)
130 GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
132 cb (d->cb_cls, my_identity, d->cfg, d, NULL);
134 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
135 "Successfully started peer `%4s'.\n", GNUNET_i2s (my_identity));
139 d->th = GNUNET_TRANSPORT_connect (d->sched,
140 d->cfg, d, NULL, NULL, NULL);
143 if (GNUNET_YES == d->dead)
144 GNUNET_TESTING_daemon_stop (d, d->dead_cb, d->dead_cb_cls);
145 else if (NULL != d->cb)
146 d->cb (d->cb_cls, &d->id, d->cfg, d,
147 _("Failed to connect to transport service!\n"));
151 GNUNET_TRANSPORT_get_hello (d->th, &process_hello, d);
156 * Finite-state machine for starting GNUnet.
158 * @param cls our "struct GNUNET_TESTING_Daemon"
162 start_fsm (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
164 struct GNUNET_TESTING_Daemon *d = cls;
165 GNUNET_TESTING_NotifyDaemonRunning cb;
166 enum GNUNET_OS_ProcessStatusType type;
171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
172 "Peer FSM is in phase %u.\n", d->phase);
175 d->task = GNUNET_SCHEDULER_NO_TASK;
179 /* confirm copying complete */
180 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
183 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
190 d->cfg, d, _("`scp' does not seem to terminate.\n"));
195 = GNUNET_SCHEDULER_add_delayed (d->sched,
196 GNUNET_CONSTANTS_EXEC_WAIT,
200 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
206 NULL, d->cfg, d, _("`scp' did not complete cleanly.\n"));
210 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
211 "Successfully copied configuration file.\n");
213 d->phase = SP_COPIED;
216 /* start GNUnet on remote host */
217 if (NULL == d->hostname)
220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
221 "Starting `%s', with command `%s %s %s %s %s %s'.\n",
222 "gnunet-arm", "gnunet-arm", "-c", d->cfgfile,
226 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
236 if (d->username != NULL)
237 GNUNET_asprintf (&dst, "%s@%s", d->username, d->hostname);
239 dst = GNUNET_strdup (d->hostname);
242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
243 "Starting `%s', with command `%s %s %s %s %s %s %s %s'.\n",
244 "gnunet-arm", "ssh", dst, "gnunet-arm", "-c", d->cfgfile,
245 "-L", "DEBUG", "-s", "-q");
247 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh",
254 "-c", d->cfgfile, "-s", "-q", NULL);
259 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
260 _("Could not start `%s' process to start GNUnet.\n"),
261 (NULL == d->hostname) ? "gnunet-arm" : "ssh");
269 (NULL == d->hostname)
270 ? _("Failed to start `gnunet-arm' process.\n")
271 : _("Failed to start `ssh' process.\n"));
274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
275 "Started `%s', waiting for `%s' to be up.\n",
276 "gnunet-arm", "gnunet-service-core");
278 d->phase = SP_START_ARMING;
281 = GNUNET_SCHEDULER_add_delayed (d->sched,
282 GNUNET_CONSTANTS_EXEC_WAIT,
285 case SP_START_ARMING:
286 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
289 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
298 (NULL == d->hostname)
299 ? _("`gnunet-arm' does not seem to terminate.\n")
300 : _("`ssh' does not seem to terminate.\n"));
305 = GNUNET_SCHEDULER_add_delayed (d->sched,
306 GNUNET_CONSTANTS_EXEC_WAIT,
311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
312 "Successfully started `%s'.\n", "gnunet-arm");
314 d->phase = SP_START_CORE;
315 d->server = GNUNET_CORE_connect (d->sched,
322 NULL, GNUNET_NO, no_handlers);
330 case SP_SHUTDOWN_START:
331 /* confirm copying complete */
332 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
335 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
337 d->dead_cb (d->dead_cb_cls,
338 _("either `gnunet-arm' or `ssh' does not seem to terminate.\n"));
341 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
342 GNUNET_TRANSPORT_disconnect(d->th);
345 GNUNET_CONFIGURATION_destroy (d->cfg);
346 GNUNET_free (d->cfgfile);
347 GNUNET_free_non_null (d->hostname);
348 GNUNET_free_non_null (d->username);
349 GNUNET_free_non_null (d->shortname);
355 = GNUNET_SCHEDULER_add_delayed (d->sched,
356 GNUNET_CONSTANTS_EXEC_WAIT,
360 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
362 if (NULL != d->dead_cb)
363 d->dead_cb (d->dead_cb_cls,
364 _("shutdown (either `gnunet-arm' or `ssh') did not complete cleanly.\n"));
367 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
368 GNUNET_TRANSPORT_disconnect(d->th);
371 GNUNET_CONFIGURATION_destroy (d->cfg);
372 GNUNET_free (d->cfgfile);
373 GNUNET_free_non_null (d->hostname);
374 GNUNET_free_non_null (d->username);
375 GNUNET_free_non_null (d->shortname);
380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer shutdown complete.\n");
384 GNUNET_TRANSPORT_get_hello_cancel(d->th, &process_hello, d);
385 GNUNET_TRANSPORT_disconnect(d->th);
388 /* state clean up and notifications */
389 GNUNET_CONFIGURATION_destroy (d->cfg);
390 GNUNET_free (d->cfgfile);
391 GNUNET_free_non_null (d->hostname);
392 GNUNET_free_non_null (d->username);
393 GNUNET_free_non_null (d->shortname);
394 if (NULL != d->dead_cb)
395 d->dead_cb (d->dead_cb_cls, NULL);
398 case SP_CONFIG_UPDATE:
399 /* confirm copying complete */
400 if (GNUNET_OK != GNUNET_OS_process_status (d->pid, &type, &code))
403 if (d->wait_runs > MAX_EXEC_WAIT_RUNS)
410 d->cfg, d, _("`scp' does not seem to terminate.\n"));
415 = GNUNET_SCHEDULER_add_delayed (d->sched,
416 GNUNET_CONSTANTS_EXEC_WAIT,
420 if ((type != GNUNET_OS_PROCESS_EXITED) || (code != 0))
422 if (NULL != d->update_cb)
423 d->update_cb (d->update_cb_cls,
424 _("`scp' did not complete cleanly.\n"));
428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
429 "Successfully copied configuration file.\n");
431 if (NULL != d->update_cb)
432 d->update_cb (d->update_cb_cls, NULL);
433 d->phase = SP_START_DONE;
440 * Starts a GNUnet daemon. GNUnet must be installed on the target
441 * system and available in the PATH. The machine must furthermore be
442 * reachable via "ssh" (unless the hostname is "NULL") without the
443 * need to enter a password.
445 * @param sched scheduler to use
446 * @param cfg configuration to use
447 * @param hostname name of the machine where to run GNUnet
448 * (use NULL for localhost).
449 * @param cb function to call with the result
450 * @param cb_cls closure for cb
451 * @return handle to the daemon (actual start will be completed asynchronously)
453 struct GNUNET_TESTING_Daemon *
454 GNUNET_TESTING_daemon_start (struct GNUNET_SCHEDULER_Handle *sched,
455 const struct GNUNET_CONFIGURATION_Handle *cfg,
456 const char *hostname,
457 GNUNET_TESTING_NotifyDaemonRunning cb,
460 struct GNUNET_TESTING_Daemon *ret;
464 ret = GNUNET_malloc (sizeof (struct GNUNET_TESTING_Daemon));
466 ret->hostname = (hostname == NULL) ? NULL : GNUNET_strdup (hostname);
467 ret->cfgfile = GNUNET_DISK_mktemp ("gnunet-testing-config");
469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
470 "Setting up peer with configuration file `%s'.\n",
473 if (NULL == ret->cfgfile)
475 GNUNET_free_non_null (ret->hostname);
480 ret->cb_cls = cb_cls;
481 ret->cfg = GNUNET_CONFIGURATION_dup (cfg);
482 GNUNET_CONFIGURATION_set_value_string (ret->cfg,
484 "DEFAULTCONFIG", ret->cfgfile);
485 /* 1) write configuration to temporary file */
486 if (GNUNET_OK != GNUNET_CONFIGURATION_write (ret->cfg, ret->cfgfile))
488 if (0 != UNLINK (ret->cfgfile))
489 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
490 "unlink", ret->cfgfile);
491 GNUNET_CONFIGURATION_destroy (ret->cfg);
492 GNUNET_free_non_null (ret->hostname);
493 GNUNET_free (ret->cfgfile);
498 GNUNET_CONFIGURATION_get_value_string (cfg,
500 "USERNAME", &username))
502 if (NULL != getenv ("USER"))
503 username = GNUNET_strdup (getenv ("USER"));
507 ret->username = username;
509 /* 2) copy file to remote host */
510 if (NULL != hostname)
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "Copying configuration file to host `%s'.\n", hostname);
516 ret->phase = SP_COPYING;
517 if (NULL != username)
518 GNUNET_asprintf (&arg, "%s@%s:%s", username, hostname, ret->cfgfile);
520 GNUNET_asprintf (&arg, "%s:%s", hostname, ret->cfgfile);
521 ret->pid = GNUNET_OS_start_process (NULL, NULL, "scp",
522 "scp", ret->cfgfile, arg, NULL);
526 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
528 ("Could not start `%s' process to copy configuration file.\n"),
530 if (0 != UNLINK (ret->cfgfile))
531 GNUNET_log_strerror_file (GNUNET_ERROR_TYPE_WARNING,
532 "unlink", ret->cfgfile);
533 GNUNET_CONFIGURATION_destroy (ret->cfg);
534 GNUNET_free_non_null (ret->hostname);
535 GNUNET_free_non_null (ret->username);
536 GNUNET_free (ret->cfgfile);
541 = GNUNET_SCHEDULER_add_delayed (sched,
542 GNUNET_CONSTANTS_EXEC_WAIT,
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
548 "No need to copy configuration file since we are running locally.\n");
550 ret->phase = SP_COPIED;
551 GNUNET_SCHEDULER_add_continuation (sched,
554 GNUNET_SCHEDULER_REASON_PREREQ_DONE);
560 * Stops a GNUnet daemon.
562 * @param d the daemon that should be stopped
563 * @param cb function called once the daemon was stopped
564 * @param cb_cls closure for cb
567 GNUNET_TESTING_daemon_stop (struct GNUNET_TESTING_Daemon *d,
568 GNUNET_TESTING_NotifyCompletion cb, void *cb_cls)
573 d->dead_cb_cls = cb_cls;
577 d->dead = GNUNET_YES;
580 if (d->phase == SP_CONFIG_UPDATE)
582 GNUNET_SCHEDULER_cancel (d->sched, d->task);
583 d->phase = SP_START_DONE;
585 if (d->server != NULL)
587 GNUNET_CORE_disconnect (d->server);
590 /* shutdown ARM process (will terminate others) */
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 _("Terminating peer `%4s'\n"), GNUNET_i2s (&d->id));
594 /* sleep(15); Manual check for running */
597 d->phase = SP_SHUTDOWN_START;
599 /* Check if this is a local or remote process */
600 if (NULL != d->hostname)
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "Stopping gnunet-arm with config `%s' on host `%s'.\n", d->cfgfile, d->hostname);
607 if (d->username != NULL)
608 GNUNET_asprintf (&arg, "%s@%s", d->username, d->hostname);
610 arg = GNUNET_strdup (d->hostname);
612 d->pid = GNUNET_OS_start_process (NULL, NULL, "ssh", "ssh",
617 "-c", d->cfgfile, "-e", "-d", "-q", NULL);
618 /* Use -e to end arm, and -d to remove temp files */
625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
626 "Stopping gnunet-arm with config `%s' locally.\n", d->cfgfile);
628 d->pid = GNUNET_OS_start_process (NULL, NULL, "gnunet-arm",
633 "-c", d->cfgfile, "-e", "-d", "-q", NULL);
636 GNUNET_free_non_null(d->hello);
640 = GNUNET_SCHEDULER_add_delayed (d->sched,
641 GNUNET_CONSTANTS_EXEC_WAIT,
648 * Changes the configuration of a GNUnet daemon.
650 * @param d the daemon that should be modified
651 * @param cfg the new configuration for the daemon
652 * @param cb function called once the configuration was changed
653 * @param cb_cls closure for cb
656 GNUNET_TESTING_daemon_reconfigure (struct GNUNET_TESTING_Daemon *d,
657 struct GNUNET_CONFIGURATION_Handle *cfg,
658 GNUNET_TESTING_NotifyCompletion cb,
663 if (d->phase != SP_START_DONE)
668 ("Peer not yet running, can not change configuration at this point."));
672 /* 1) write configuration to temporary file */
673 if (GNUNET_OK != GNUNET_CONFIGURATION_write (cfg, d->cfgfile))
676 cb (cb_cls, _("Failed to write new configuration to disk."));
680 /* 2) copy file to remote host (if necessary) */
681 if (NULL == d->hostname)
689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
690 "Copying updated configuration file to remote host `%s'.\n",
693 d->phase = SP_CONFIG_UPDATE;
694 if (NULL != d->username)
695 GNUNET_asprintf (&arg, "%s@%s:%s", d->username, d->hostname, d->cfgfile);
697 GNUNET_asprintf (&arg, "%s:%s", d->hostname, d->cfgfile);
698 d->pid = GNUNET_OS_start_process (NULL, NULL, "scp", "scp", d->cfgfile, arg, NULL);
702 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
704 ("Could not start `%s' process to copy configuration file.\n"),
707 cb (cb_cls, _("Failed to copy new configuration to remote machine."));
708 d->phase = SP_START_DONE;
712 d->update_cb_cls = cb_cls;
714 = GNUNET_SCHEDULER_add_delayed (d->sched,
715 GNUNET_CONSTANTS_EXEC_WAIT,
721 * Data kept for each pair of peers that we try
724 struct ConnectContext
727 * Testing handle to the first daemon.
729 struct GNUNET_TESTING_Daemon *d1;
732 * Handle to core of first daemon (to check connect)
734 struct GNUNET_CORE_Handle * d1core;
737 * Testing handle to the second daemon.
739 struct GNUNET_TESTING_Daemon *d2;
742 * Transport handle to the second daemon.
744 struct GNUNET_TRANSPORT_Handle *d2th;
747 * Function to call once we are done (or have timed out).
749 GNUNET_TESTING_NotifyConnection cb;
757 * When should this operation be complete (or we must trigger
760 struct GNUNET_TIME_Absolute timeout;
763 * The relative timeout from whence this connect attempt was
764 * started. Allows for reconnect attempts.
766 struct GNUNET_TIME_Relative relative_timeout;
769 * Maximum number of connect attempts, will retry connection
770 * this number of times on failures.
772 unsigned int max_connect_attempts;
777 GNUNET_SCHEDULER_TaskIdentifier hello_send_task;
780 * Connect timeout task
782 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
785 * When should this operation be complete (or we must trigger
788 struct GNUNET_TIME_Relative timeout_hello;
792 * Was the connection attempt successful?
798 /** Forward declaration **/
800 reattempt_daemons_connect(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
804 * Notify callback about success or failure of the attempt
805 * to connect the two peers
807 * @param cls our "struct ConnectContext" (freed)
808 * @param tc reason tells us if we succeeded or failed
811 notify_connect_result (void *cls,
812 const struct GNUNET_SCHEDULER_TaskContext *tc)
814 struct ConnectContext *ctx = cls;
815 struct GNUNET_TIME_Relative remaining;
817 if (ctx->hello_send_task != GNUNET_SCHEDULER_NO_TASK)
819 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->hello_send_task);
820 ctx->hello_send_task = GNUNET_SCHEDULER_NO_TASK;
823 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
825 if (ctx->d2th != NULL)
826 GNUNET_TRANSPORT_disconnect (ctx->d2th);
828 if (ctx->d1core != NULL)
829 GNUNET_CORE_disconnect (ctx->d1core);
836 remaining = GNUNET_TIME_absolute_get_remaining(ctx->timeout);
838 if (ctx->connected == GNUNET_YES)
842 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
843 ctx->d2->cfg, ctx->d1, ctx->d2, NULL);
845 GNUNET_SCHEDULER_cancel(ctx->d1->sched, ctx->timeout_task);
847 else if (remaining.value > 0)
849 GNUNET_SCHEDULER_add_now(ctx->d1->sched, &reattempt_daemons_connect, ctx);
856 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg,
857 ctx->d2->cfg, ctx->d1, ctx->d2,
858 _("Peers failed to connect"));
863 GNUNET_TRANSPORT_disconnect (ctx->d2th);
865 GNUNET_CORE_disconnect (ctx->d1core);
872 * Success, connection is up. Signal client our success.
874 * @param cls our "struct ConnectContext"
875 * @param peer identity of the peer that has connected
876 * @param latency the round trip latency of the connection to this peer
877 * @param distance distance the transport level distance to this peer
881 connect_notify (void *cls, const struct GNUNET_PeerIdentity * peer, struct GNUNET_TIME_Relative latency,
884 struct ConnectContext *ctx = cls;
886 if (memcmp(&ctx->d2->id, peer, sizeof(struct GNUNET_PeerIdentity)) == 0)
888 ctx->connected = GNUNET_YES;
889 GNUNET_SCHEDULER_add_now (ctx->d1->sched,
890 ¬ify_connect_result,
897 send_hello(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
899 struct ConnectContext *ctx = cls;
901 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
904 if (ctx->d1->hello != NULL)
906 GNUNET_TRANSPORT_offer_hello (ctx->d2th, GNUNET_HELLO_get_header(ctx->d1->hello));
907 ctx->timeout_hello = GNUNET_TIME_relative_add(ctx->timeout_hello,
908 GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS,
911 ctx->hello_send_task = GNUNET_SCHEDULER_add_delayed(ctx->d1->sched,
917 * Establish a connection between two GNUnet daemons.
919 * @param d1 handle for the first daemon
920 * @param d2 handle for the second daemon
921 * @param timeout how long is the connection attempt
923 * @param max_connect_attempts how many times should we try to reconnect
925 * @param cb function to call at the end
926 * @param cb_cls closure for cb
929 GNUNET_TESTING_daemons_connect (struct GNUNET_TESTING_Daemon *d1,
930 struct GNUNET_TESTING_Daemon *d2,
931 struct GNUNET_TIME_Relative timeout,
932 unsigned int max_connect_attempts,
933 GNUNET_TESTING_NotifyConnection cb,
936 struct ConnectContext *ctx;
938 if ((d1->server == NULL) || (d2->server == NULL))
941 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
942 _("Peers are not fully running yet, can not connect!\n"));
945 ctx = GNUNET_malloc (sizeof (struct ConnectContext));
948 ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
949 ctx->timeout_hello = GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_MILLISECONDS, 500);
950 ctx->relative_timeout = timeout;
952 ctx->cb_cls = cb_cls;
953 ctx->max_connect_attempts = max_connect_attempts;
954 ctx->connected = GNUNET_NO;
956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
957 "Asked to connect peer %s to peer %s\n",
958 d1->shortname, d2->shortname);
961 ctx->d1core = GNUNET_CORE_connect (d1->sched,
966 &connect_notify, NULL,
968 NULL, GNUNET_NO, no_handlers);
969 if (ctx->d1core == NULL)
973 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
974 _("Failed to connect to core service of first peer!\n"));
978 #if DEBUG_TESTING > 2
979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
980 "Asked to connect peer %s to peer %s\n",
981 d1->shortname, d2->shortname);
982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
983 "Connecting to transport service of peer %s\n", d2->shortname);
987 ctx->d2th = GNUNET_TRANSPORT_connect (d2->sched,
988 d2->cfg, d2, NULL, NULL, NULL);
989 if (ctx->d2th == NULL)
993 cb (cb_cls, &d1->id, &d2->id, d1->cfg, d2->cfg, d1, d2,
994 _("Failed to connect to transport service!\n"));
998 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (d1->sched,
999 GNUNET_TIME_relative_divide(ctx->relative_timeout, max_connect_attempts), /* Allow up to 8 reconnect attempts */
1000 ¬ify_connect_result, ctx);
1002 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1006 reattempt_daemons_connect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1009 struct ConnectContext *ctx = cls;
1010 if (tc->reason == GNUNET_SCHEDULER_REASON_SHUTDOWN)
1015 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "re-attempting connect of peer %s to peer %s\n",
1016 ctx->d1->shortname, ctx->d2->shortname);
1019 if (ctx->d1core != NULL)
1021 GNUNET_CORE_disconnect(ctx->d1core);
1025 if (ctx->d2th != NULL)
1027 GNUNET_TRANSPORT_disconnect(ctx->d2th);
1031 ctx->d1core = GNUNET_CORE_connect (ctx->d1->sched,
1033 GNUNET_TIME_absolute_get_remaining(ctx->timeout),
1036 &connect_notify, NULL,
1038 NULL, GNUNET_NO, no_handlers);
1039 if (ctx->d1core == NULL)
1041 if (NULL != ctx->cb)
1042 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1043 _("Failed to connect to core service of first peer!\n"));
1048 ctx->d2th = GNUNET_TRANSPORT_connect (ctx->d2->sched,
1049 ctx->d2->cfg, ctx->d2, NULL, NULL, NULL);
1050 if (ctx->d2th == NULL)
1053 if (NULL != ctx->cb)
1054 ctx->cb (ctx->cb_cls, &ctx->d1->id, &ctx->d2->id, ctx->d1->cfg, ctx->d2->cfg, ctx->d1, ctx->d2,
1055 _("Failed to connect to transport service!\n"));
1059 ctx->timeout_task = GNUNET_SCHEDULER_add_delayed (ctx->d1->sched,
1060 GNUNET_TIME_relative_divide(ctx->relative_timeout, ctx->max_connect_attempts),
1061 ¬ify_connect_result, ctx);
1063 ctx->hello_send_task = GNUNET_SCHEDULER_add_now(ctx->d1->sched, &send_hello, ctx);
1066 /* end of testing.c */