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 * Which peer should we connect to?
52 * Handle to transport service.
54 static struct GNUNET_TRANSPORT_Handle *handle;
59 static int benchmark_send;
64 static int benchmark_receive;
69 static int benchmark_receive;
74 static int iterate_connections;
79 static int test_configuration;
84 static int monitor_connects;
89 static int monitor_connections;
94 static int try_connect;
102 * Global return value (0 success).
107 * Current number of connections in monitor mode
109 static int monitor_connect_counter;
112 * Number of bytes of traffic we received so far.
114 static unsigned long long traffic_received;
117 * Number of bytes of traffic we sent so far.
119 static unsigned long long traffic_sent;
122 * Starting time of transmitting/receiving data.
124 static struct GNUNET_TIME_Absolute start_time;
127 * Handle for current transmission request.
129 static struct GNUNET_TRANSPORT_TransmitHandle *th;
134 struct GNUNET_TRANSPORT_PeerIterateContext *pic;
137 * Identity of the peer we transmit to / connect to.
138 * (equivalent to 'cpid' string).
140 static struct GNUNET_PeerIdentity pid;
143 * Task scheduled for cleanup / termination of the process.
145 static GNUNET_SCHEDULER_TaskIdentifier end;
148 * Task for operation timeout
150 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
153 static struct GNUNET_CONTAINER_MultiHashMap *peers;
156 * Selected level of verbosity.
158 static int verbosity;
161 * Resolver process handle.
163 struct GNUNET_OS_Process *resolver;
166 * Number of tasks running that still need the resolver.
168 static unsigned int resolver_users;
171 * Number of address resolutions pending
173 static unsigned int address_resolutions;
176 * Address resolutions pending in progress
178 static unsigned int address_resolution_in_progress;
181 * Context for a plugin test.
187 * Handle to the active NAT test.
189 struct GNUNET_NAT_Test *tst;
192 * Task identifier for the timeout.
194 GNUNET_SCHEDULER_TaskIdentifier tsk;
197 * Name of plugin under test.
205 * Task run in monitor mode when the user presses CTRL-C to abort.
206 * Stops monitoring activity.
208 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
209 * @param tc scheduler context
212 shutdown_task (void *cls,
213 const struct GNUNET_SCHEDULER_TaskContext *tc)
215 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
217 GNUNET_SCHEDULER_cancel (op_timeout);
218 op_timeout = GNUNET_SCHEDULER_NO_TASK;
222 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pic);
227 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th);
232 GNUNET_TRANSPORT_disconnect(handle);
237 GNUNET_CONTAINER_multihashmap_destroy (peers);
245 operation_timeout (void *cls,
246 const struct GNUNET_SCHEDULER_TaskContext *tc)
248 op_timeout = GNUNET_SCHEDULER_NO_TASK;
251 FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_h2s_full (&pid.hashPubKey));
252 if (GNUNET_SCHEDULER_NO_TASK != end)
253 GNUNET_SCHEDULER_cancel (end);
254 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
263 * Display the result of the test.
265 * @param tc test context
266 * @param result GNUNET_YES on success
269 display_test_result (struct TestContext *tc, int result)
271 if (GNUNET_YES != result)
273 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
277 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
279 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
281 GNUNET_SCHEDULER_cancel (tc->tsk);
282 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
286 GNUNET_NAT_test_stop (tc->tst);
291 if ((0 == resolver_users) && (NULL != resolver))
293 GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
294 GNUNET_OS_process_destroy (resolver);
301 * Function called by NAT on success.
302 * Clean up and update GUI (with success).
304 * @param cls test context
305 * @param success currently always GNUNET_OK
308 result_callback (void *cls, int success)
310 struct TestContext *tc = cls;
312 display_test_result (tc, success);
317 * Function called if NAT failed to confirm success.
318 * Clean up and update GUI (with failure).
320 * @param cls test context
321 * @param tc scheduler callback
324 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 struct TestContext *tstc = cls;
328 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
329 display_test_result (tstc, GNUNET_NO);
334 * Test our plugin's configuration (NAT traversal, etc.).
336 * @param cfg configuration to test
339 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
343 unsigned long long bnd_port;
344 unsigned long long adv_port;
345 struct TestContext *tc;
348 GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
354 ("No transport plugins configured, peer will never communicate\n"));
358 for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
360 char section[12 + strlen (tok)];
362 GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
364 GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
367 _("No port configured for plugin `%s', cannot test it\n"), tok);
371 GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
374 if (NULL == resolver)
376 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-service-resolver",
377 "gnunet-service-resolver", NULL);
379 GNUNET_RESOLVER_connect (cfg);
380 tc = GNUNET_malloc (sizeof (struct TestContext));
381 tc->name = GNUNET_strdup (tok);
383 GNUNET_NAT_test_start (cfg,
386 "udp")) ? GNUNET_NO : GNUNET_YES,
387 (uint16_t) bnd_port, (uint16_t) adv_port,
388 &result_callback, tc);
391 display_test_result (tc, GNUNET_SYSERR);
394 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
396 GNUNET_free (plugins);
401 * Shutdown, print statistics.
404 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
406 struct GNUNET_TIME_Relative duration;
410 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
413 GNUNET_TRANSPORT_disconnect (handle);
414 if (benchmark_receive)
416 duration = GNUNET_TIME_absolute_get_duration (start_time);
417 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
418 1000 * traffic_received / (1 + duration.rel_value),
420 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
424 duration = GNUNET_TIME_absolute_get_duration (start_time);
425 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
426 1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
427 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
433 * Function called to notify a client about the socket
434 * begin ready to queue more data. "buf" will be
435 * NULL and "size" zero if the socket was closed for
436 * writing in the meantime.
439 * @param size number of bytes available in buf
440 * @param buf where the callee should write the message
441 * @return number of bytes written to buf
444 transmit_data (void *cls, size_t size, void *buf)
446 struct GNUNET_MessageHeader *m = buf;
448 if ((NULL == buf) && (0 == size))
454 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
455 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
456 m->size = ntohs (size);
457 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
458 memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
459 traffic_sent += size;
460 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, 32 * 1024, 0,
461 GNUNET_TIME_UNIT_FOREVER_REL,
462 &transmit_data, NULL);
464 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
471 * Function called to notify transport users that another
472 * peer connected to us.
475 * @param peer the peer that connected
476 * @param ats performance data
477 * @param ats_count number of entries in ats (excluding 0-termination)
480 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
481 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
484 FPRINTF (stdout, _("Connected to %s\n"), GNUNET_i2s (peer));
485 if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
490 /* all done, terminate instantly */
491 FPRINTF (stdout, _("Successfully connected to %s\n"), GNUNET_h2s_full (&peer->hashPubKey));
494 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
495 GNUNET_SCHEDULER_cancel (op_timeout);
497 if (GNUNET_SCHEDULER_NO_TASK != end)
498 GNUNET_SCHEDULER_cancel (end);
499 end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
504 start_time = GNUNET_TIME_absolute_get ();
506 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer, 32 * 1024, 0,
507 GNUNET_TIME_UNIT_FOREVER_REL,
508 &transmit_data, NULL);
515 * Function called to notify transport users that another
516 * peer disconnected from us.
519 * @param peer the peer that disconnected
522 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
525 FPRINTF (stdout, _("Disconnected from %s\n"), GNUNET_i2s (peer));
528 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
535 * Function called to notify transport users that another
536 * peer connected to us.
539 * @param peer the peer that connected
540 * @param ats performance data
541 * @param ats_count number of entries in ats (excluding 0-termination)
544 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
545 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
547 monitor_connect_counter ++;
548 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
549 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
551 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
555 monitor_connect_counter);
560 * Function called to notify transport users that another
561 * peer disconnected from us.
564 * @param peer the peer that disconnected
567 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
569 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
570 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
572 GNUNET_assert (monitor_connect_counter > 0);
573 monitor_connect_counter --;
575 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
577 _("Disconnected from"),
579 monitor_connect_counter);
585 * Function called by the transport for each received message.
588 * @param peer (claimed) identity of the other peer
589 * @param message the message
590 * @param ats performance data
591 * @param ats_count number of entries in ats
594 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
595 const struct GNUNET_MessageHeader *message,
596 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
598 if (!benchmark_receive)
600 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
603 FPRINTF (stdout, _("Received %u bytes from %s\n"),
604 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
605 if (traffic_received == 0)
606 start_time = GNUNET_TIME_absolute_get ();
607 traffic_received += ntohs (message->size);
610 struct ResolutionContext
612 struct GNUNET_HELLO_Address *addrcp;
619 process_string (void *cls, const char *address)
621 struct ResolutionContext *rc = cls;
622 struct GNUNET_HELLO_Address *addrcp = rc->addrcp;
626 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
627 rc->printed = GNUNET_YES;
632 GNUNET_assert (address_resolutions > 0);
633 address_resolutions --;
634 if (GNUNET_NO == rc->printed)
635 FPRINTF (stdout, _("Peer `%s': %s <unable to resolve address>\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name);
636 GNUNET_free (rc->addrcp);
638 if (0 == address_resolutions)
640 if (GNUNET_SCHEDULER_NO_TASK != end)
641 GNUNET_SCHEDULER_cancel (end);
643 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
649 * Function to call with a binary address
652 * @param peer identity of the peer
653 * @param address binary address (NULL on disconnect)
656 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
657 const struct GNUNET_HELLO_Address *address)
659 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
660 struct ResolutionContext *rc;
665 address_resolution_in_progress = GNUNET_NO;
671 FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
675 rc = GNUNET_malloc(sizeof (struct ResolutionContext));
676 rc->addrcp = GNUNET_HELLO_address_copy(address);
677 rc->printed = GNUNET_NO;
679 GNUNET_assert (NULL != rc);
680 address_resolutions ++;
681 /* Resolve address to string */
682 GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
683 RESOLUTION_TIMEOUT, &process_string,
689 testservice_task (void *cls,
690 const struct GNUNET_SCHEDULER_TaskContext *tc)
692 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
696 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
698 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
702 if ((NULL != cpid) && (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey)))
704 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
708 counter = benchmark_send + benchmark_receive + iterate_connections +
709 monitor_connections + monitor_connects + try_connect;
713 FPRINTF (stderr, _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
714 "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
719 FPRINTF (stderr, _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
720 "connect", "benchmark send", "benchmark receive", "information", "monitor", "events");
724 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
732 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
736 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
742 FPRINTF (stderr, _("Failed to connect to transport service\n"));
746 GNUNET_TRANSPORT_try_connect (handle, &pid);
747 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
748 &operation_timeout, NULL);
751 else if (benchmark_send) /* Benchmark sending */
755 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
760 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
766 FPRINTF (stderr, _("Failed to connect to transport service\n"));
770 GNUNET_TRANSPORT_try_connect (handle, &pid);
772 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
776 else if (benchmark_receive) /* Benchmark receiving */
779 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
780 ¬ify_connect, ¬ify_disconnect);
783 FPRINTF (stderr, _("Failed to connect to transport service\n"));
787 GNUNET_TRANSPORT_try_connect (handle, &pid);
788 start_time = GNUNET_TIME_absolute_get ();
790 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
791 &do_disconnect, NULL);*/
793 else if (iterate_connections) /* -i: List all active addresses once */
795 peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
796 address_resolution_in_progress = GNUNET_YES;
797 pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
800 &process_address, (void *) cfg);
802 else if (monitor_connections) /* -m: List all active addresses continously */
804 peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
805 address_resolution_in_progress = GNUNET_YES;
806 pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL,
809 &process_address, (void *) cfg);
811 else if (monitor_connects) /* -e : Monitor (dis)connect events continously */
813 monitor_connect_counter = 0;
814 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
815 &monitor_notify_connect,
816 &monitor_notify_disconnect);
819 FPRINTF (stderr, _("Failed to connect to transport service\n"));
834 * Main function that will be run by the scheduler.
837 * @param args remaining command-line arguments
838 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
839 * @param cfg configuration
842 run (void *cls, char *const *args, const char *cfgfile,
843 const struct GNUNET_CONFIGURATION_Handle *cfg)
845 if (test_configuration)
847 do_test_configuration (cfg);
850 GNUNET_CLIENT_service_test ("transport", cfg,
851 GNUNET_TIME_UNIT_SECONDS,
858 main (int argc, char *const *argv)
861 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
862 {'b', "benchmark", NULL,
863 gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
864 0, &GNUNET_GETOPT_set_one, &benchmark_receive},
865 {'C', "connect", NULL,
866 gettext_noop ("connect to a peer"),
867 0, &GNUNET_GETOPT_set_one, &try_connect},
868 {'i', "information", NULL,
869 gettext_noop ("provide information about all current connections (once)"),
870 0, &GNUNET_GETOPT_set_one, &iterate_connections},
871 {'m', "monitor", NULL,
872 gettext_noop ("provide information about all current connections (continuously)"),
873 0, &GNUNET_GETOPT_set_one, &monitor_connections},
874 {'e', "events", NULL,
875 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
876 0, &GNUNET_GETOPT_set_one, &monitor_connects},
877 {'n', "numeric", NULL,
878 gettext_noop ("do not resolve hostnames"),
879 0, &GNUNET_GETOPT_set_one, &numeric},
880 {'p', "peer", "PEER",
881 gettext_noop ("peer identity"),
882 1, &GNUNET_GETOPT_set_string, &cpid},
885 ("send data for benchmarking to the other peer (until CTRL-C)"),
886 0, &GNUNET_GETOPT_set_one, &benchmark_send},
888 gettext_noop ("test transport configuration (involves external server)"),
889 0, &GNUNET_GETOPT_set_one, &test_configuration},
890 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
891 GNUNET_GETOPT_OPTION_END
894 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
897 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
899 ("Direct access to transport service."), options,
901 GNUNET_free ((void *) argv);
902 if (GNUNET_OK == res)
908 /* end of gnunet-transport.c */