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;
65 static int benchmark_send;
70 static int benchmark_receive;
75 static int benchmark_receive;
80 static int iterate_connections;
85 static int test_configuration;
90 static int monitor_connects;
95 static int monitor_connections;
100 static int try_connect;
108 * Global return value (0 success).
113 * Current number of connections in monitor mode
115 static int monitor_connect_counter;
118 * Number of bytes of traffic we received so far.
120 static unsigned long long traffic_received;
123 * Number of bytes of traffic we sent so far.
125 static unsigned long long traffic_sent;
128 * Starting time of transmitting/receiving data.
130 static struct GNUNET_TIME_Absolute start_time;
133 * Handle for current transmission request.
135 static struct GNUNET_TRANSPORT_TransmitHandle *th;
140 struct GNUNET_TRANSPORT_PeerIterateContext *pic;
143 * Identity of the peer we transmit to / connect to.
144 * (equivalent to 'cpid' string).
146 static struct GNUNET_PeerIdentity pid;
149 * Task scheduled for cleanup / termination of the process.
151 static GNUNET_SCHEDULER_TaskIdentifier end;
154 * Task for operation timeout
156 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
159 static struct GNUNET_CONTAINER_MultiHashMap *peers;
162 * Selected level of verbosity.
164 static int verbosity;
167 * Resolver process handle.
169 struct GNUNET_OS_Process *resolver;
172 * Number of tasks running that still need the resolver.
174 static unsigned int resolver_users;
177 * Number of address resolutions pending
179 static unsigned int address_resolutions;
182 * Address resolutions pending in progress
184 static unsigned int address_resolution_in_progress;
187 * Context for a plugin test.
193 * Handle to the active NAT test.
195 struct GNUNET_NAT_Test *tst;
198 * Task identifier for the timeout.
200 GNUNET_SCHEDULER_TaskIdentifier tsk;
203 * Name of plugin under test.
211 * Task run in monitor mode when the user presses CTRL-C to abort.
212 * Stops monitoring activity.
214 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
215 * @param tc scheduler context
218 shutdown_task (void *cls,
219 const struct GNUNET_SCHEDULER_TaskContext *tc)
221 struct GNUNET_TIME_Relative duration;
222 end = GNUNET_SCHEDULER_NO_TASK;
223 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
225 GNUNET_SCHEDULER_cancel (op_timeout);
226 op_timeout = GNUNET_SCHEDULER_NO_TASK;
230 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pic);
235 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th);
240 GNUNET_TRANSPORT_disconnect(handle);
245 GNUNET_CONTAINER_multihashmap_destroy (peers);
250 duration = GNUNET_TIME_absolute_get_duration (start_time);
251 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
252 1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
253 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
255 if (benchmark_receive)
257 duration = GNUNET_TIME_absolute_get_duration (start_time);
258 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
259 1000 * traffic_received / (1 + duration.rel_value),
261 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
268 operation_timeout (void *cls,
269 const struct GNUNET_SCHEDULER_TaskContext *tc)
271 op_timeout = GNUNET_SCHEDULER_NO_TASK;
272 if ((try_connect) || (benchmark_send) || (benchmark_receive))
274 FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_h2s_full (&pid.hashPubKey));
275 if (GNUNET_SCHEDULER_NO_TASK != end)
276 GNUNET_SCHEDULER_cancel (end);
277 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
286 * Display the result of the test.
288 * @param tc test context
289 * @param result GNUNET_YES on success
292 display_test_result (struct TestContext *tc, int result)
294 if (GNUNET_YES != result)
296 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
300 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
302 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
304 GNUNET_SCHEDULER_cancel (tc->tsk);
305 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
309 GNUNET_NAT_test_stop (tc->tst);
314 if ((0 == resolver_users) && (NULL != resolver))
316 GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
317 GNUNET_OS_process_destroy (resolver);
324 * Function called by NAT on success.
325 * Clean up and update GUI (with success).
327 * @param cls test context
328 * @param success currently always GNUNET_OK
331 result_callback (void *cls, int success)
333 struct TestContext *tc = cls;
335 display_test_result (tc, success);
340 * Function called if NAT failed to confirm success.
341 * Clean up and update GUI (with failure).
343 * @param cls test context
344 * @param tc scheduler callback
347 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
349 struct TestContext *tstc = cls;
351 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
352 display_test_result (tstc, GNUNET_NO);
357 * Test our plugin's configuration (NAT traversal, etc.).
359 * @param cfg configuration to test
362 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
366 unsigned long long bnd_port;
367 unsigned long long adv_port;
368 struct TestContext *tc;
371 GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
377 ("No transport plugins configured, peer will never communicate\n"));
381 for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
383 char section[12 + strlen (tok)];
385 GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
387 GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
390 _("No port configured for plugin `%s', cannot test it\n"), tok);
394 GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
397 if (NULL == resolver)
399 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-service-resolver",
400 "gnunet-service-resolver", NULL);
402 GNUNET_RESOLVER_connect (cfg);
403 tc = GNUNET_malloc (sizeof (struct TestContext));
404 tc->name = GNUNET_strdup (tok);
406 GNUNET_NAT_test_start (cfg,
409 "udp")) ? GNUNET_NO : GNUNET_YES,
410 (uint16_t) bnd_port, (uint16_t) adv_port,
411 &result_callback, tc);
414 display_test_result (tc, GNUNET_SYSERR);
417 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
419 GNUNET_free (plugins);
423 * Function called to notify a client about the socket
424 * begin ready to queue more data. "buf" will be
425 * NULL and "size" zero if the socket was closed for
426 * writing in the meantime.
429 * @param size number of bytes available in buf
430 * @param buf where the callee should write the message
431 * @return number of bytes written to buf
434 transmit_data (void *cls, size_t size, void *buf)
436 struct GNUNET_MessageHeader *m = buf;
438 if ((NULL == buf) && (0 == size))
444 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
445 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
446 m->size = ntohs (size);
447 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
448 memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
449 traffic_sent += size;
450 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, BLOCKSIZE * 1024, 0,
451 GNUNET_TIME_UNIT_FOREVER_REL,
452 &transmit_data, NULL);
454 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
461 * Function called to notify transport users that another
462 * peer connected to us.
465 * @param peer the peer that connected
466 * @param ats performance data
467 * @param ats_count number of entries in ats (excluding 0-termination)
470 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
471 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
473 if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
478 /* all done, terminate instantly */
479 FPRINTF (stdout, _("Successfully connected to `%s'\n"), GNUNET_h2s_full (&peer->hashPubKey));
482 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
484 GNUNET_SCHEDULER_cancel (op_timeout);
485 op_timeout = GNUNET_SCHEDULER_NO_TASK;
488 if (GNUNET_SCHEDULER_NO_TASK != end)
489 GNUNET_SCHEDULER_cancel (end);
490 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
495 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
497 GNUNET_SCHEDULER_cancel (op_timeout);
498 op_timeout = GNUNET_SCHEDULER_NO_TASK;
501 FPRINTF (stdout, _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
502 GNUNET_i2s (&pid), BLOCKSIZE);
503 start_time = GNUNET_TIME_absolute_get ();
505 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
507 GNUNET_TIME_UNIT_FOREVER_REL,
508 &transmit_data, NULL);
517 * Function called to notify transport users that another
518 * peer disconnected from us.
521 * @param peer the peer that disconnected
524 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
526 if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
531 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
536 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"), GNUNET_i2s (&pid));
537 if (GNUNET_SCHEDULER_NO_TASK != end)
538 GNUNET_SCHEDULER_cancel (end);
544 * Function called to notify transport users that another
545 * peer connected to us.
548 * @param peer the peer that connected
549 * @param ats performance data
550 * @param ats_count number of entries in ats (excluding 0-termination)
553 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
554 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
556 monitor_connect_counter ++;
557 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
558 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
560 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
564 monitor_connect_counter);
569 * Function called to notify transport users that another
570 * peer disconnected from us.
573 * @param peer the peer that disconnected
576 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
578 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
579 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
581 GNUNET_assert (monitor_connect_counter > 0);
582 monitor_connect_counter --;
584 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
586 _("Disconnected from"),
588 monitor_connect_counter);
594 * Function called by the transport for each received message.
597 * @param peer (claimed) identity of the other peer
598 * @param message the message
599 * @param ats performance data
600 * @param ats_count number of entries in ats
603 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
604 const struct GNUNET_MessageHeader *message,
605 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
607 if (benchmark_receive)
609 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
612 FPRINTF (stdout, _("Received %u bytes from %s\n"),
613 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
615 if (traffic_received == 0)
616 start_time = GNUNET_TIME_absolute_get ();
617 traffic_received += ntohs (message->size);
622 struct ResolutionContext
624 struct GNUNET_HELLO_Address *addrcp;
631 process_string (void *cls, const char *address)
633 struct ResolutionContext *rc = cls;
634 struct GNUNET_HELLO_Address *addrcp = rc->addrcp;
638 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
639 rc->printed = GNUNET_YES;
644 GNUNET_assert (address_resolutions > 0);
645 address_resolutions --;
646 if (GNUNET_NO == rc->printed)
647 FPRINTF (stdout, _("Peer `%s': %s <unable to resolve address>\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name);
648 GNUNET_free (rc->addrcp);
650 if ((0 == address_resolutions) && (iterate_connections))
652 if (GNUNET_SCHEDULER_NO_TASK != end)
653 GNUNET_SCHEDULER_cancel (end);
655 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
661 * Function to call with a binary address
664 * @param peer identity of the peer
665 * @param address binary address (NULL on disconnect)
668 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
669 const struct GNUNET_HELLO_Address *address)
671 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
672 struct ResolutionContext *rc;
677 address_resolution_in_progress = GNUNET_NO;
683 FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
687 rc = GNUNET_malloc(sizeof (struct ResolutionContext));
688 rc->addrcp = GNUNET_HELLO_address_copy(address);
689 rc->printed = GNUNET_NO;
691 GNUNET_assert (NULL != rc);
692 address_resolutions ++;
693 /* Resolve address to string */
694 GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
695 RESOLUTION_TIMEOUT, &process_string,
701 testservice_task (void *cls,
702 const struct GNUNET_SCHEDULER_TaskContext *tc)
704 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
708 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
710 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
714 if ((NULL != cpid) && (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey)))
716 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
720 counter = benchmark_send + benchmark_receive + iterate_connections +
721 monitor_connections + monitor_connects + try_connect;
725 FPRINTF (stderr, _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
726 "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
731 FPRINTF (stderr, _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
732 "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
736 if (try_connect) /* -C: Connect to peer */
740 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
745 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
751 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
755 GNUNET_TRANSPORT_try_connect (handle, &pid);
756 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
757 &operation_timeout, NULL);
760 else if (benchmark_send) /* -s: Benchmark sending */
764 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
769 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
775 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
779 GNUNET_TRANSPORT_try_connect (handle, &pid);
780 start_time = GNUNET_TIME_absolute_get ();
781 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
782 &operation_timeout, NULL);
784 else if (benchmark_receive) /* -b: Benchmark receiving */
787 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
791 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
796 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
797 start_time = GNUNET_TIME_absolute_get ();
800 else if (iterate_connections) /* -i: List all active addresses once */
802 peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
803 address_resolution_in_progress = GNUNET_YES;
804 pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
807 &process_address, (void *) cfg);
809 else if (monitor_connections) /* -m: List all active addresses continously */
811 peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
812 address_resolution_in_progress = GNUNET_YES;
813 pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
816 &process_address, (void *) cfg);
818 else if (monitor_connects) /* -e : Monitor (dis)connect events continously */
820 monitor_connect_counter = 0;
821 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
822 &monitor_notify_connect,
823 &monitor_notify_disconnect);
826 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
838 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
846 * Main function that will be run by the scheduler.
849 * @param args remaining command-line arguments
850 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
851 * @param cfg configuration
854 run (void *cls, char *const *args, const char *cfgfile,
855 const struct GNUNET_CONFIGURATION_Handle *cfg)
857 if (test_configuration)
859 do_test_configuration (cfg);
863 GNUNET_CLIENT_service_test ("transport", cfg,
864 GNUNET_TIME_UNIT_SECONDS,
871 main (int argc, char *const *argv)
874 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
875 {'b', "benchmark", NULL,
876 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
877 0, &GNUNET_GETOPT_set_one, &benchmark_receive},
878 {'C', "connect", NULL,
879 gettext_noop ("connect to a peer"),
880 0, &GNUNET_GETOPT_set_one, &try_connect},
881 {'i', "information", NULL,
882 gettext_noop ("provide information about all current connections (once)"),
883 0, &GNUNET_GETOPT_set_one, &iterate_connections},
884 {'m', "monitor", NULL,
885 gettext_noop ("provide information about all current connections (continuously)"),
886 0, &GNUNET_GETOPT_set_one, &monitor_connections},
887 {'e', "events", NULL,
888 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
889 0, &GNUNET_GETOPT_set_one, &monitor_connects},
890 {'n', "numeric", NULL,
891 gettext_noop ("do not resolve hostnames"),
892 0, &GNUNET_GETOPT_set_one, &numeric},
893 {'p', "peer", "PEER",
894 gettext_noop ("peer identity"),
895 1, &GNUNET_GETOPT_set_string, &cpid},
898 ("send data for benchmarking to the other peer (until CTRL-C)"),
899 0, &GNUNET_GETOPT_set_one, &benchmark_send},
901 gettext_noop ("test transport configuration (involves external server)"),
902 0, &GNUNET_GETOPT_set_one, &test_configuration},
903 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
904 GNUNET_GETOPT_OPTION_END
907 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
910 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
912 ("Direct access to transport service."), options,
914 GNUNET_free ((void *) argv);
915 if (GNUNET_OK == res)
921 /* end of gnunet-transport.c */