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_connections;
91 * Global return value (0 success).
96 * Current number of connections in monitor mode
98 static int monitor_connections_counter;
101 * Number of bytes of traffic we received so far.
103 static unsigned long long traffic_received;
106 * Number of bytes of traffic we sent so far.
108 static unsigned long long traffic_sent;
111 * Starting time of transmitting/receiving data.
113 static struct GNUNET_TIME_Absolute start_time;
116 * Handle for current transmission request.
118 static struct GNUNET_TRANSPORT_TransmitHandle *th;
121 * Identity of the peer we transmit to / connect to.
122 * (equivalent to 'cpid' string).
124 static struct GNUNET_PeerIdentity pid;
127 * Task scheduled for cleanup / termination of the process.
129 static GNUNET_SCHEDULER_TaskIdentifier end;
131 static struct GNUNET_CONTAINER_MultiHashMap *peers;
134 * Selected level of verbosity.
136 static int verbosity;
139 * Resolver process handle.
141 struct GNUNET_OS_Process *resolver;
144 * Number of tasks running that still need the resolver.
146 static unsigned int resolver_users;
150 * Context for a plugin test.
156 * Handle to the active NAT test.
158 struct GNUNET_NAT_Test *tst;
161 * Task identifier for the timeout.
163 GNUNET_SCHEDULER_TaskIdentifier tsk;
166 * Name of plugin under test.
174 * Display the result of the test.
176 * @param tc test context
177 * @param result GNUNET_YES on success
180 display_test_result (struct TestContext *tc, int result)
182 if (GNUNET_YES != result)
184 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
188 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
190 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
192 GNUNET_SCHEDULER_cancel (tc->tsk);
193 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
197 GNUNET_NAT_test_stop (tc->tst);
202 if ((0 == resolver_users) && (NULL != resolver))
204 GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
205 GNUNET_OS_process_destroy (resolver);
212 * Function called by NAT on success.
213 * Clean up and update GUI (with success).
215 * @param cls test context
216 * @param success currently always GNUNET_OK
219 result_callback (void *cls, int success)
221 struct TestContext *tc = cls;
223 display_test_result (tc, success);
228 * Function called if NAT failed to confirm success.
229 * Clean up and update GUI (with failure).
231 * @param cls test context
232 * @param tc scheduler callback
235 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
237 struct TestContext *tstc = cls;
239 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
240 display_test_result (tstc, GNUNET_NO);
245 * Test our plugin's configuration (NAT traversal, etc.).
247 * @param cfg configuration to test
250 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
254 unsigned long long bnd_port;
255 unsigned long long adv_port;
256 struct TestContext *tc;
259 GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
265 ("No transport plugins configured, peer will never communicate\n"));
269 for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
271 char section[12 + strlen (tok)];
273 GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
275 GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
278 _("No port configured for plugin `%s', cannot test it\n"), tok);
282 GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
285 if (NULL == resolver)
287 GNUNET_OS_start_process (GNUNET_YES, GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, "gnunet-service-resolver",
288 "gnunet-service-resolver", NULL);
290 GNUNET_RESOLVER_connect (cfg);
291 tc = GNUNET_malloc (sizeof (struct TestContext));
292 tc->name = GNUNET_strdup (tok);
294 GNUNET_NAT_test_start (cfg,
297 "udp")) ? GNUNET_NO : GNUNET_YES,
298 (uint16_t) bnd_port, (uint16_t) adv_port,
299 &result_callback, tc);
302 display_test_result (tc, GNUNET_SYSERR);
305 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
307 GNUNET_free (plugins);
312 * Shutdown, print statistics.
315 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
317 struct GNUNET_TIME_Relative duration;
321 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
324 GNUNET_TRANSPORT_disconnect (handle);
325 if (benchmark_receive)
327 duration = GNUNET_TIME_absolute_get_duration (start_time);
328 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
329 1000 * traffic_received / (1 + duration.rel_value),
331 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
335 duration = GNUNET_TIME_absolute_get_duration (start_time);
336 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
337 1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
338 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
344 * Function called to notify a client about the socket
345 * begin ready to queue more data. "buf" will be
346 * NULL and "size" zero if the socket was closed for
347 * writing in the meantime.
350 * @param size number of bytes available in buf
351 * @param buf where the callee should write the message
352 * @return number of bytes written to buf
355 transmit_data (void *cls, size_t size, void *buf)
357 struct GNUNET_MessageHeader *m = buf;
359 if ((NULL == buf) && (0 == size))
365 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
366 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
367 m->size = ntohs (size);
368 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
369 memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
370 traffic_sent += size;
371 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, 32 * 1024, 0,
372 GNUNET_TIME_UNIT_FOREVER_REL,
373 &transmit_data, NULL);
375 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
382 * Function called to notify transport users that another
383 * peer connected to us.
386 * @param peer the peer that connected
387 * @param ats performance data
388 * @param ats_count number of entries in ats (excluding 0-termination)
391 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
392 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
395 FPRINTF (stdout, _("Connected to %s\n"), GNUNET_i2s (peer));
396 if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
401 start_time = GNUNET_TIME_absolute_get ();
403 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer, 32 * 1024, 0,
404 GNUNET_TIME_UNIT_FOREVER_REL,
405 &transmit_data, NULL);
409 /* all done, terminate instantly */
410 GNUNET_SCHEDULER_cancel (end);
411 end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
417 * Function called to notify transport users that another
418 * peer disconnected from us.
421 * @param peer the peer that disconnected
424 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
427 FPRINTF (stdout, _("Disconnected from %s\n"), GNUNET_i2s (peer));
430 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
437 * Function called to notify transport users that another
438 * peer connected to us.
441 * @param peer the peer that connected
442 * @param ats performance data
443 * @param ats_count number of entries in ats (excluding 0-termination)
446 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
447 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
449 monitor_connections_counter ++;
450 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
451 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
453 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
457 monitor_connections_counter);
462 * Function called to notify transport users that another
463 * peer disconnected from us.
466 * @param peer the peer that disconnected
469 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
471 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get();
472 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
474 GNUNET_assert (monitor_connections_counter > 0);
475 monitor_connections_counter --;
477 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"),
479 _("Disconnected from"),
481 monitor_connections_counter);
487 * Function called by the transport for each received message.
490 * @param peer (claimed) identity of the other peer
491 * @param message the message
492 * @param ats performance data
493 * @param ats_count number of entries in ats
496 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
497 const struct GNUNET_MessageHeader *message,
498 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
500 if (!benchmark_receive)
502 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
505 FPRINTF (stdout, _("Received %u bytes from %s\n"),
506 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
507 if (traffic_received == 0)
508 start_time = GNUNET_TIME_absolute_get ();
509 traffic_received += ntohs (message->size);
512 struct ResolutionContext
514 struct GNUNET_HELLO_Address *addrcp;
521 process_string (void *cls, const char *address)
523 struct ResolutionContext *rc = cls;
524 struct GNUNET_HELLO_Address *addrcp = rc->addrcp;
528 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
529 rc->printed = GNUNET_YES;
534 if (GNUNET_NO == rc->printed)
535 FPRINTF (stdout, _("Peer `%s': %s <unable to resolve address>\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name);
536 GNUNET_free (rc->addrcp);
542 * Function to call with a binary address
545 * @param peer identity of the peer
546 * @param address binary address (NULL on disconnect)
549 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
550 const struct GNUNET_HELLO_Address *address)
552 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
553 struct ResolutionContext *rc;
563 FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
567 rc = GNUNET_malloc(sizeof (struct ResolutionContext));
568 rc->addrcp = GNUNET_HELLO_address_copy(address);
569 rc->printed = GNUNET_NO;
571 GNUNET_assert (NULL != rc);
573 /* Resolve address to string */
574 GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
575 RESOLUTION_TIMEOUT, &process_string,
581 * Task run in monitor mode when the user presses CTRL-C to abort.
582 * Stops monitoring activity.
584 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
585 * @param tc scheduler context
588 shutdown_task (void *cls,
589 const struct GNUNET_SCHEDULER_TaskContext *tc)
593 GNUNET_TRANSPORT_notify_transmit_ready_cancel(th);
598 GNUNET_TRANSPORT_disconnect(handle);
604 GNUNET_CONTAINER_multihashmap_destroy (peers);
610 testservice_task (void *cls,
611 const struct GNUNET_SCHEDULER_TaskContext *tc)
613 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
615 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
617 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
621 if (benchmark_send && (NULL == cpid))
623 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
630 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
632 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
636 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
637 ¬ify_connect, ¬ify_disconnect);
638 GNUNET_TRANSPORT_try_connect (handle, &pid);
640 GNUNET_SCHEDULER_add_delayed (benchmark_send ?
641 GNUNET_TIME_UNIT_FOREVER_REL :
642 GNUNET_TIME_UNIT_SECONDS, &do_disconnect,
645 else if (benchmark_receive)
648 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
649 ¬ify_connect, ¬ify_disconnect);
650 GNUNET_TRANSPORT_try_connect (handle, &pid);
651 start_time = GNUNET_TIME_absolute_get ();
653 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
654 &do_disconnect, NULL);
656 if (iterate_connections)
658 peers = GNUNET_CONTAINER_multihashmap_create (20, GNUNET_NO);
659 GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL, GNUNET_YES,
661 &process_address, (void *) cfg);
663 if (monitor_connections)
665 monitor_connections_counter = 0;
666 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
667 &monitor_notify_connect,
668 &monitor_notify_disconnect);
671 GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
675 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
686 * Main function that will be run by the scheduler.
689 * @param args remaining command-line arguments
690 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
691 * @param cfg configuration
694 run (void *cls, char *const *args, const char *cfgfile,
695 const struct GNUNET_CONFIGURATION_Handle *cfg)
697 if (test_configuration)
699 do_test_configuration (cfg);
702 GNUNET_CLIENT_service_test ("transport", cfg,
703 GNUNET_TIME_UNIT_SECONDS,
710 main (int argc, char *const *argv)
713 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
714 {'b', "benchmark", NULL,
715 gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
716 0, &GNUNET_GETOPT_set_one, &benchmark_receive},
717 {'C', "connect", "PEER",
718 gettext_noop ("try to connect to the given peer"),
719 1, &GNUNET_GETOPT_set_string, &cpid},
720 {'i', "information", NULL,
721 gettext_noop ("provide information about all current connections (once)"),
722 0, &GNUNET_GETOPT_set_one, &iterate_connections},
723 {'m', "monitor", NULL,
724 gettext_noop ("provide information about all current connections (continuously)"),
725 0, &GNUNET_GETOPT_set_one, &monitor_connections},
726 {'n', "numeric", NULL,
727 gettext_noop ("do not resolve hostnames"),
728 0, &GNUNET_GETOPT_set_one, &numeric},
731 ("send data for benchmarking to the other peer (until CTRL-C)"),
732 0, &GNUNET_GETOPT_set_one, &benchmark_send},
734 gettext_noop ("test transport configuration (involves external server)"),
735 0, &GNUNET_GETOPT_set_one, &test_configuration},
736 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
737 GNUNET_GETOPT_OPTION_END
740 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
743 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
745 ("Direct access to transport service."), options,
747 GNUNET_free ((void *) argv);
749 if (GNUNET_OK == res)
758 /* end of gnunet-transport.c */