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)
46 * Which peer should we connect to?
51 * Handle to transport service.
53 static struct GNUNET_TRANSPORT_Handle *handle;
58 static int benchmark_send;
63 static int benchmark_receive;
68 static int benchmark_receive;
73 static int iterate_connections;
78 static int test_configuration;
83 static int monitor_connects;
88 static int monitor_connections;
96 * Global return value (0 success).
101 * Current number of connections in monitor mode
103 static int monitor_connect_counter;
106 * Number of bytes of traffic we received so far.
108 static unsigned long long traffic_received;
111 * Number of bytes of traffic we sent so far.
113 static unsigned long long traffic_sent;
116 * Starting time of transmitting/receiving data.
118 static struct GNUNET_TIME_Absolute start_time;
121 * Handle for current transmission request.
123 static struct GNUNET_TRANSPORT_TransmitHandle *th;
128 struct GNUNET_TRANSPORT_PeerIterateContext *pic;
131 * Identity of the peer we transmit to / connect to.
132 * (equivalent to 'cpid' string).
134 static struct GNUNET_PeerIdentity pid;
137 * Task scheduled for cleanup / termination of the process.
139 static GNUNET_SCHEDULER_TaskIdentifier end;
141 static struct GNUNET_CONTAINER_MultiHashMap *peers;
144 * Selected level of verbosity.
146 static int verbosity;
149 * Resolver process handle.
151 struct GNUNET_OS_Process *resolver;
154 * Number of tasks running that still need the resolver.
156 static unsigned int resolver_users;
159 * Number of address resolutions pending
161 static unsigned int address_resolutions;
164 * Address resolutions pending in progress
166 static unsigned int address_resolution_in_progress;
169 * Context for a plugin test.
175 * Handle to the active NAT test.
177 struct GNUNET_NAT_Test *tst;
180 * Task identifier for the timeout.
182 GNUNET_SCHEDULER_TaskIdentifier tsk;
185 * Name of plugin under test.
193 * Task run in monitor mode when the user presses CTRL-C to abort.
194 * Stops monitoring activity.
196 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
197 * @param tc scheduler context
200 shutdown_task (void *cls,
201 const struct GNUNET_SCHEDULER_TaskContext *tc)
205 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pic);
210 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th);
215 GNUNET_TRANSPORT_disconnect(handle);
221 GNUNET_CONTAINER_multihashmap_destroy (peers);
227 * Display the result of the test.
229 * @param tc test context
230 * @param result GNUNET_YES on success
233 display_test_result (struct TestContext *tc, int result)
235 if (GNUNET_YES != result)
237 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
241 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
243 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
245 GNUNET_SCHEDULER_cancel (tc->tsk);
246 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
250 GNUNET_NAT_test_stop (tc->tst);
255 if ((0 == resolver_users) && (NULL != resolver))
257 GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
258 GNUNET_OS_process_destroy (resolver);
265 * Function called by NAT on success.
266 * Clean up and update GUI (with success).
268 * @param cls test context
269 * @param success currently always GNUNET_OK
272 result_callback (void *cls, int success)
274 struct TestContext *tc = cls;
276 display_test_result (tc, success);
281 * Function called if NAT failed to confirm success.
282 * Clean up and update GUI (with failure).
284 * @param cls test context
285 * @param tc scheduler callback
288 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 struct TestContext *tstc = cls;
292 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
293 display_test_result (tstc, GNUNET_NO);
298 * Test our plugin's configuration (NAT traversal, etc.).
300 * @param cfg configuration to test
303 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
307 unsigned long long bnd_port;
308 unsigned long long adv_port;
309 struct TestContext *tc;
312 GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
318 ("No transport plugins configured, peer will never communicate\n"));
322 for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
324 char section[12 + strlen (tok)];
326 GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
328 GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
331 _("No port configured for plugin `%s', cannot test it\n"), tok);
335 GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
338 if (NULL == resolver)
340 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-service-resolver",
341 "gnunet-service-resolver", NULL);
343 GNUNET_RESOLVER_connect (cfg);
344 tc = GNUNET_malloc (sizeof (struct TestContext));
345 tc->name = GNUNET_strdup (tok);
347 GNUNET_NAT_test_start (cfg,
350 "udp")) ? GNUNET_NO : GNUNET_YES,
351 (uint16_t) bnd_port, (uint16_t) adv_port,
352 &result_callback, tc);
355 display_test_result (tc, GNUNET_SYSERR);
358 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
360 GNUNET_free (plugins);
365 * Shutdown, print statistics.
368 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
370 struct GNUNET_TIME_Relative duration;
374 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
377 GNUNET_TRANSPORT_disconnect (handle);
378 if (benchmark_receive)
380 duration = GNUNET_TIME_absolute_get_duration (start_time);
381 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
382 1000 * traffic_received / (1 + duration.rel_value),
384 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
388 duration = GNUNET_TIME_absolute_get_duration (start_time);
389 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
390 1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
391 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
397 * Function called to notify a client about the socket
398 * begin ready to queue more data. "buf" will be
399 * NULL and "size" zero if the socket was closed for
400 * writing in the meantime.
403 * @param size number of bytes available in buf
404 * @param buf where the callee should write the message
405 * @return number of bytes written to buf
408 transmit_data (void *cls, size_t size, void *buf)
410 struct GNUNET_MessageHeader *m = buf;
412 if ((NULL == buf) && (0 == size))
418 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
419 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
420 m->size = ntohs (size);
421 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
422 memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
423 traffic_sent += size;
424 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, 32 * 1024, 0,
425 GNUNET_TIME_UNIT_FOREVER_REL,
426 &transmit_data, NULL);
428 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
435 * Function called to notify transport users that another
436 * peer connected to us.
439 * @param peer the peer that connected
440 * @param ats performance data
441 * @param ats_count number of entries in ats (excluding 0-termination)
444 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
445 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
448 FPRINTF (stdout, _("Connected to %s\n"), GNUNET_i2s (peer));
449 if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
454 start_time = GNUNET_TIME_absolute_get ();
456 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer, 32 * 1024, 0,
457 GNUNET_TIME_UNIT_FOREVER_REL,
458 &transmit_data, NULL);
462 /* all done, terminate instantly */
463 GNUNET_SCHEDULER_cancel (end);
464 end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
470 * Function called to notify transport users that another
471 * peer disconnected from us.
474 * @param peer the peer that disconnected
477 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
480 FPRINTF (stdout, _("Disconnected from %s\n"), GNUNET_i2s (peer));
483 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
490 * Function called to notify transport users that another
491 * peer connected to us.
494 * @param peer the peer that connected
495 * @param ats performance data
496 * @param ats_count number of entries in ats (excluding 0-termination)
499 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
500 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
502 monitor_connect_counter ++;
503 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
504 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
506 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
510 monitor_connect_counter);
515 * Function called to notify transport users that another
516 * peer disconnected from us.
519 * @param peer the peer that disconnected
522 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
524 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
525 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
527 GNUNET_assert (monitor_connect_counter > 0);
528 monitor_connect_counter --;
530 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
532 _("Disconnected from"),
534 monitor_connect_counter);
540 * Function called by the transport for each received message.
543 * @param peer (claimed) identity of the other peer
544 * @param message the message
545 * @param ats performance data
546 * @param ats_count number of entries in ats
549 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
550 const struct GNUNET_MessageHeader *message,
551 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
553 if (!benchmark_receive)
555 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
558 FPRINTF (stdout, _("Received %u bytes from %s\n"),
559 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
560 if (traffic_received == 0)
561 start_time = GNUNET_TIME_absolute_get ();
562 traffic_received += ntohs (message->size);
565 struct ResolutionContext
567 struct GNUNET_HELLO_Address *addrcp;
574 process_string (void *cls, const char *address)
576 struct ResolutionContext *rc = cls;
577 struct GNUNET_HELLO_Address *addrcp = rc->addrcp;
581 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
582 rc->printed = GNUNET_YES;
587 GNUNET_assert (address_resolutions > 0);
588 address_resolutions --;
589 if (GNUNET_NO == rc->printed)
590 FPRINTF (stdout, _("Peer `%s': %s <unable to resolve address>\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name);
591 GNUNET_free (rc->addrcp);
593 FPRINTF (stdout, _("Peer --: %u\n"), address_resolutions);
594 if (0 == address_resolutions)
596 if (GNUNET_SCHEDULER_NO_TASK == end)
597 GNUNET_SCHEDULER_cancel (end);
598 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
605 * Function to call with a binary address
608 * @param peer identity of the peer
609 * @param address binary address (NULL on disconnect)
612 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
613 const struct GNUNET_HELLO_Address *address)
615 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
616 struct ResolutionContext *rc;
621 address_resolution_in_progress = GNUNET_NO;
627 FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
631 rc = GNUNET_malloc(sizeof (struct ResolutionContext));
632 rc->addrcp = GNUNET_HELLO_address_copy(address);
633 rc->printed = GNUNET_NO;
635 GNUNET_assert (NULL != rc);
636 address_resolutions ++;
637 FPRINTF (stdout, _("Peer `%s' ++: %u\n"), GNUNET_i2s (peer), address_resolutions);
638 /* Resolve address to string */
639 GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
640 RESOLUTION_TIMEOUT, &process_string,
646 testservice_task (void *cls,
647 const struct GNUNET_SCHEDULER_TaskContext *tc)
649 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
652 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
654 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
658 counter = benchmark_send + benchmark_receive + iterate_connections +
659 monitor_connections + monitor_connects;
663 FPRINTF (stderr, _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s\n"),
664 "benchmark send", "benchmark receive", "information", "monitor", "events");
669 FPRINTF (stderr, _("No operation given. Please choose one operation: %s, %s, %s, %s, %s\n"),
670 "benchmark send", "benchmark receive", "information", "monitor", "events");
675 if (benchmark_send) /* Benchmark sending */
679 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
684 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
686 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
689 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
693 GNUNET_TRANSPORT_try_connect (handle, &pid);
694 end = GNUNET_SCHEDULER_add_delayed (benchmark_send ?
695 GNUNET_TIME_UNIT_FOREVER_REL :
696 GNUNET_TIME_UNIT_SECONDS,
700 else if (benchmark_receive) /* Benchmark receiving */
703 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
704 ¬ify_connect, ¬ify_disconnect);
705 GNUNET_TRANSPORT_try_connect (handle, &pid);
706 start_time = GNUNET_TIME_absolute_get ();
708 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
709 &do_disconnect, NULL);
711 else if (iterate_connections) /* List all active addresses once */
713 peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
714 address_resolution_in_progress = GNUNET_YES;
715 pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
718 &process_address, (void *) cfg);
720 else if (monitor_connections) /* List all active addresses continously */
722 peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
723 address_resolution_in_progress = GNUNET_YES;
724 pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
727 &process_address, (void *) cfg);
729 else if (monitor_connects) /* Monitor (dis)connect events continously */
731 monitor_connect_counter = 0;
732 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
733 &monitor_notify_connect,
734 &monitor_notify_disconnect);
737 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
747 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
754 * Main function that will be run by the scheduler.
757 * @param args remaining command-line arguments
758 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
759 * @param cfg configuration
762 run (void *cls, char *const *args, const char *cfgfile,
763 const struct GNUNET_CONFIGURATION_Handle *cfg)
765 if (test_configuration)
767 do_test_configuration (cfg);
770 GNUNET_CLIENT_service_test ("transport", cfg,
771 GNUNET_TIME_UNIT_SECONDS,
778 main (int argc, char *const *argv)
781 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
782 {'b', "benchmark", NULL,
783 gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
784 0, &GNUNET_GETOPT_set_one, &benchmark_receive},
785 {'C', "connect", "PEER",
786 gettext_noop ("try to connect to the given peer"),
787 1, &GNUNET_GETOPT_set_string, &cpid},
788 {'i', "information", NULL,
789 gettext_noop ("provide information about all current connections (once)"),
790 0, &GNUNET_GETOPT_set_one, &iterate_connections},
791 {'m', "monitor", NULL,
792 gettext_noop ("provide information about all current connections (continuously)"),
793 0, &GNUNET_GETOPT_set_one, &monitor_connections},
794 {'e', "events", NULL,
795 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
796 0, &GNUNET_GETOPT_set_one, &monitor_connects},
797 {'n', "numeric", NULL,
798 gettext_noop ("do not resolve hostnames"),
799 0, &GNUNET_GETOPT_set_one, &numeric},
802 ("send data for benchmarking to the other peer (until CTRL-C)"),
803 0, &GNUNET_GETOPT_set_one, &benchmark_send},
805 gettext_noop ("test transport configuration (involves external server)"),
806 0, &GNUNET_GETOPT_set_one, &test_configuration},
807 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
808 GNUNET_GETOPT_OPTION_END
811 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
814 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
816 ("Direct access to transport service."), options,
818 GNUNET_free ((void *) argv);
819 if (GNUNET_OK == res)
825 /* end of gnunet-transport.c */