2 This file is part of GNUnet.
3 (C) 2011 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 src/transport/gnunet-transport.c
23 * @brief Tool to help configure, measure and control the transport subsystem.
24 * @author Christian Grothoff
25 * @author Nathan Evans
27 * This utility can be used to test if a transport mechanism for
28 * GNUnet is properly configured.
32 #include "gnunet_util_lib.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_nat_lib.h"
39 * How long do we wait for the NAT test to report success?
40 * Should match NAT_SERVER_TIMEOUT in 'nat_test.c'.
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10)
44 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
47 * Benchmarking block size in KB
53 * Which peer should we connect to?
58 * Handle to transport service.
60 static struct GNUNET_TRANSPORT_Handle *handle;
66 struct GNUNET_TRANSPORT_TryConnectHandle * tc_handle;
71 static int benchmark_send;
76 static int benchmark_receive;
81 static int benchmark_receive;
86 static int iterate_connections;
91 static int test_configuration;
96 static int monitor_connects;
101 static int monitor_connections;
106 static int try_connect;
114 * Global return value (0 success).
119 * Current number of connections in monitor mode
121 static int monitor_connect_counter;
124 * Number of bytes of traffic we received so far.
126 static unsigned long long traffic_received;
129 * Number of bytes of traffic we sent so far.
131 static unsigned long long traffic_sent;
134 * Starting time of transmitting/receiving data.
136 static struct GNUNET_TIME_Absolute start_time;
139 * Handle for current transmission request.
141 static struct GNUNET_TRANSPORT_TransmitHandle *th;
146 struct GNUNET_TRANSPORT_PeerIterateContext *pic;
149 * Identity of the peer we transmit to / connect to.
150 * (equivalent to 'cpid' string).
152 static struct GNUNET_PeerIdentity pid;
155 * Task scheduled for cleanup / termination of the process.
157 static GNUNET_SCHEDULER_TaskIdentifier end;
160 * Task for operation timeout
162 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
165 static struct GNUNET_CONTAINER_MultiHashMap *peers;
168 * Selected level of verbosity.
170 static int verbosity;
173 * Resolver process handle.
175 struct GNUNET_OS_Process *resolver;
178 * Number of tasks running that still need the resolver.
180 static unsigned int resolver_users;
183 * Number of address resolutions pending
185 static unsigned int address_resolutions;
188 * Address resolutions pending in progress
190 static unsigned int address_resolution_in_progress;
193 * Context for a plugin test.
199 * Handle to the active NAT test.
201 struct GNUNET_NAT_Test *tst;
204 * Task identifier for the timeout.
206 GNUNET_SCHEDULER_TaskIdentifier tsk;
209 * Name of plugin under test.
217 * Task run in monitor mode when the user presses CTRL-C to abort.
218 * Stops monitoring activity.
220 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
221 * @param tc scheduler context
224 shutdown_task (void *cls,
225 const struct GNUNET_SCHEDULER_TaskContext *tc)
227 struct GNUNET_TIME_Relative duration;
228 end = GNUNET_SCHEDULER_NO_TASK;
229 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
231 GNUNET_SCHEDULER_cancel (op_timeout);
232 op_timeout = GNUNET_SCHEDULER_NO_TASK;
234 if (NULL != tc_handle)
236 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
241 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pic);
246 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th);
251 GNUNET_TRANSPORT_disconnect(handle);
256 GNUNET_CONTAINER_multihashmap_destroy (peers);
261 duration = GNUNET_TIME_absolute_get_duration (start_time);
262 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
263 1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
264 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
266 if (benchmark_receive)
268 duration = GNUNET_TIME_absolute_get_duration (start_time);
269 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
270 1000 * traffic_received / (1 + duration.rel_value),
272 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
279 operation_timeout (void *cls,
280 const struct GNUNET_SCHEDULER_TaskContext *tc)
282 op_timeout = GNUNET_SCHEDULER_NO_TASK;
283 if ((try_connect) || (benchmark_send) || (benchmark_receive))
285 FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_h2s_full (&pid.hashPubKey));
286 if (GNUNET_SCHEDULER_NO_TASK != end)
287 GNUNET_SCHEDULER_cancel (end);
288 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
297 * Display the result of the test.
299 * @param tc test context
300 * @param result GNUNET_YES on success
303 display_test_result (struct TestContext *tc, int result)
305 if (GNUNET_YES != result)
307 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
311 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
313 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
315 GNUNET_SCHEDULER_cancel (tc->tsk);
316 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
320 GNUNET_NAT_test_stop (tc->tst);
325 if ((0 == resolver_users) && (NULL != resolver))
327 GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
328 GNUNET_OS_process_destroy (resolver);
335 * Function called by NAT on success.
336 * Clean up and update GUI (with success).
338 * @param cls test context
339 * @param success currently always GNUNET_OK
342 result_callback (void *cls, int success)
344 struct TestContext *tc = cls;
346 display_test_result (tc, success);
351 * Function called if NAT failed to confirm success.
352 * Clean up and update GUI (with failure).
354 * @param cls test context
355 * @param tc scheduler callback
358 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
360 struct TestContext *tstc = cls;
362 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
363 display_test_result (tstc, GNUNET_NO);
368 * Test our plugin's configuration (NAT traversal, etc.).
370 * @param cfg configuration to test
373 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
377 unsigned long long bnd_port;
378 unsigned long long adv_port;
379 struct TestContext *tc;
382 GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
388 ("No transport plugins configured, peer will never communicate\n"));
392 for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
394 char section[12 + strlen (tok)];
396 GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
398 GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
401 _("No port configured for plugin `%s', cannot test it\n"), tok);
405 GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
408 if (NULL == resolver)
410 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-service-resolver",
411 "gnunet-service-resolver", NULL);
413 GNUNET_RESOLVER_connect (cfg);
414 tc = GNUNET_malloc (sizeof (struct TestContext));
415 tc->name = GNUNET_strdup (tok);
417 GNUNET_NAT_test_start (cfg,
420 "udp")) ? GNUNET_NO : GNUNET_YES,
421 (uint16_t) bnd_port, (uint16_t) adv_port,
422 &result_callback, tc);
425 display_test_result (tc, GNUNET_SYSERR);
428 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
430 GNUNET_free (plugins);
434 * Function called to notify a client about the socket
435 * begin ready to queue more data. "buf" will be
436 * NULL and "size" zero if the socket was closed for
437 * writing in the meantime.
440 * @param size number of bytes available in buf
441 * @param buf where the callee should write the message
442 * @return number of bytes written to buf
445 transmit_data (void *cls, size_t size, void *buf)
447 struct GNUNET_MessageHeader *m = buf;
449 if ((NULL == buf) && (0 == size))
455 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
456 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
457 m->size = ntohs (size);
458 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
459 memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
460 traffic_sent += size;
461 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, BLOCKSIZE * 1024, 0,
462 GNUNET_TIME_UNIT_FOREVER_REL,
463 &transmit_data, NULL);
465 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
472 * Function called to notify transport users that another
473 * peer connected to us.
476 * @param peer the peer that connected
477 * @param ats performance data
478 * @param ats_count number of entries in ats (excluding 0-termination)
481 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
482 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
484 if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
489 /* all done, terminate instantly */
490 FPRINTF (stdout, _("Successfully connected to `%s'\n"), GNUNET_h2s_full (&peer->hashPubKey));
493 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
495 GNUNET_SCHEDULER_cancel (op_timeout);
496 op_timeout = GNUNET_SCHEDULER_NO_TASK;
499 if (GNUNET_SCHEDULER_NO_TASK != end)
500 GNUNET_SCHEDULER_cancel (end);
501 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
506 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
508 GNUNET_SCHEDULER_cancel (op_timeout);
509 op_timeout = GNUNET_SCHEDULER_NO_TASK;
512 FPRINTF (stdout, _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
513 GNUNET_i2s (&pid), BLOCKSIZE);
514 start_time = GNUNET_TIME_absolute_get ();
516 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
518 GNUNET_TIME_UNIT_FOREVER_REL,
519 &transmit_data, NULL);
528 * Function called to notify transport users that another
529 * peer disconnected from us.
532 * @param peer the peer that disconnected
535 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
537 if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
542 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
547 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"), GNUNET_i2s (&pid));
548 if (GNUNET_SCHEDULER_NO_TASK != end)
549 GNUNET_SCHEDULER_cancel (end);
555 * Function called to notify transport users that another
556 * peer connected to us.
559 * @param peer the peer that connected
560 * @param ats performance data
561 * @param ats_count number of entries in ats (excluding 0-termination)
564 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
565 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
567 monitor_connect_counter ++;
568 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
569 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
571 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
575 monitor_connect_counter);
580 * Function called to notify transport users that another
581 * peer disconnected from us.
584 * @param peer the peer that disconnected
587 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
589 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
590 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
592 GNUNET_assert (monitor_connect_counter > 0);
593 monitor_connect_counter --;
595 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
597 _("Disconnected from"),
599 monitor_connect_counter);
605 * Function called by the transport for each received message.
608 * @param peer (claimed) identity of the other peer
609 * @param message the message
610 * @param ats performance data
611 * @param ats_count number of entries in ats
614 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
615 const struct GNUNET_MessageHeader *message,
616 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
618 if (benchmark_receive)
620 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
623 FPRINTF (stdout, _("Received %u bytes from %s\n"),
624 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
626 if (traffic_received == 0)
627 start_time = GNUNET_TIME_absolute_get ();
628 traffic_received += ntohs (message->size);
633 struct ResolutionContext
635 struct GNUNET_HELLO_Address *addrcp;
642 process_string (void *cls, const char *address)
644 struct ResolutionContext *rc = cls;
645 struct GNUNET_HELLO_Address *addrcp = rc->addrcp;
649 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
650 rc->printed = GNUNET_YES;
655 GNUNET_assert (address_resolutions > 0);
656 address_resolutions --;
657 if (GNUNET_NO == rc->printed)
658 FPRINTF (stdout, _("Peer `%s': %s <unable to resolve address>\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name);
659 GNUNET_free (rc->addrcp);
661 if ((0 == address_resolutions) && (iterate_connections))
663 if (GNUNET_SCHEDULER_NO_TASK != end)
664 GNUNET_SCHEDULER_cancel (end);
666 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
672 * Function to call with a binary address
675 * @param peer identity of the peer
676 * @param address binary address (NULL on disconnect)
679 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
680 const struct GNUNET_HELLO_Address *address)
682 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
683 struct ResolutionContext *rc;
688 address_resolution_in_progress = GNUNET_NO;
694 FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
698 rc = GNUNET_malloc(sizeof (struct ResolutionContext));
699 rc->addrcp = GNUNET_HELLO_address_copy(address);
700 rc->printed = GNUNET_NO;
702 GNUNET_assert (NULL != rc);
703 address_resolutions ++;
704 /* Resolve address to string */
705 GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
706 RESOLUTION_TIMEOUT, &process_string,
710 void try_connect_cb (void *cls,
713 static int retries = 0;
714 if (GNUNET_OK == result)
721 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL);
724 FPRINTF (stderr, "%s", _("Failed to send connect request to transport service\n"));
725 if (GNUNET_SCHEDULER_NO_TASK != end)
726 GNUNET_SCHEDULER_cancel (end);
728 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
735 testservice_task (void *cls,
736 const struct GNUNET_SCHEDULER_TaskContext *tc)
738 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
742 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
744 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
748 if ((NULL != cpid) && (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey)))
750 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
754 counter = benchmark_send + benchmark_receive + iterate_connections +
755 monitor_connections + monitor_connects + try_connect;
759 FPRINTF (stderr, _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
760 "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
765 FPRINTF (stderr, _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
766 "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
770 if (try_connect) /* -C: Connect to peer */
774 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
779 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
785 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
789 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL);
790 if (NULL == tc_handle)
792 FPRINTF (stderr, "%s", _("Failed to send request to transport service\n"));
796 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
797 &operation_timeout, NULL);
800 else if (benchmark_send) /* -s: Benchmark sending */
804 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
809 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
815 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
819 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb, NULL);
820 if (NULL == tc_handle)
822 FPRINTF (stderr, "%s", _("Failed to send request to transport service\n"));
826 start_time = GNUNET_TIME_absolute_get ();
827 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
828 &operation_timeout, NULL);
830 else if (benchmark_receive) /* -b: Benchmark receiving */
833 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
837 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
842 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
843 start_time = GNUNET_TIME_absolute_get ();
846 else if (iterate_connections) /* -i: List all active addresses once */
848 peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
849 address_resolution_in_progress = GNUNET_YES;
850 pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg,
851 (NULL == cpid) ? NULL : &pid,
854 &process_address, (void *) cfg);
856 else if (monitor_connections) /* -m: List all active addresses continously */
858 peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
859 address_resolution_in_progress = GNUNET_YES;
860 pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg,
861 (NULL == cpid) ? NULL : &pid,
864 &process_address, (void *) cfg);
866 else if (monitor_connects) /* -e : Monitor (dis)connect events continously */
868 monitor_connect_counter = 0;
869 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
870 &monitor_notify_connect,
871 &monitor_notify_disconnect);
874 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
886 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
894 * Main function that will be run by the scheduler.
897 * @param args remaining command-line arguments
898 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
899 * @param cfg configuration
902 run (void *cls, char *const *args, const char *cfgfile,
903 const struct GNUNET_CONFIGURATION_Handle *cfg)
905 if (test_configuration)
907 do_test_configuration (cfg);
911 GNUNET_CLIENT_service_test ("transport", cfg,
912 GNUNET_TIME_UNIT_SECONDS,
919 main (int argc, char *const *argv)
922 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
923 {'b', "benchmark", NULL,
924 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
925 0, &GNUNET_GETOPT_set_one, &benchmark_receive},
926 {'C', "connect", NULL,
927 gettext_noop ("connect to a peer"),
928 0, &GNUNET_GETOPT_set_one, &try_connect},
929 {'i', "information", NULL,
930 gettext_noop ("provide information about all current connections (once)"),
931 0, &GNUNET_GETOPT_set_one, &iterate_connections},
932 {'m', "monitor", NULL,
933 gettext_noop ("provide information about all current connections (continuously)"),
934 0, &GNUNET_GETOPT_set_one, &monitor_connections},
935 {'e', "events", NULL,
936 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
937 0, &GNUNET_GETOPT_set_one, &monitor_connects},
938 {'n', "numeric", NULL,
939 gettext_noop ("do not resolve hostnames"),
940 0, &GNUNET_GETOPT_set_one, &numeric},
941 {'p', "peer", "PEER",
942 gettext_noop ("peer identity"),
943 1, &GNUNET_GETOPT_set_string, &cpid},
946 ("send data for benchmarking to the other peer (until CTRL-C)"),
947 0, &GNUNET_GETOPT_set_one, &benchmark_send},
949 gettext_noop ("test transport configuration (involves external server)"),
950 0, &GNUNET_GETOPT_set_one, &test_configuration},
951 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
952 GNUNET_GETOPT_OPTION_END
955 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
958 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
960 ("Direct access to transport service."), options,
962 GNUNET_free ((void *) argv);
963 if (GNUNET_OK == res)
969 /* end of gnunet-transport.c */