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?
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
44 * Which peer should we connect to?
49 * Handle to transport service.
51 static struct GNUNET_TRANSPORT_Handle *handle;
56 static int benchmark_send;
61 static int benchmark_receive;
66 static int benchmark_receive;
71 static int iterate_connections;
76 static int test_configuration;
84 * Global return value (0 success).
89 * Number of bytes of traffic we received so far.
91 static unsigned long long traffic_received;
94 * Number of bytes of traffic we sent so far.
96 static unsigned long long traffic_sent;
99 * Starting time of transmitting/receiving data.
101 static struct GNUNET_TIME_Absolute start_time;
104 * Handle for current transmission request.
106 static struct GNUNET_TRANSPORT_TransmitHandle *th;
109 * Identity of the peer we transmit to / connect to.
110 * (equivalent to 'cpid' string).
112 static struct GNUNET_PeerIdentity pid;
115 * Task scheduled for cleanup / termination of the process.
117 static GNUNET_SCHEDULER_TaskIdentifier end;
120 * Selected level of verbosity.
122 static int verbosity;
125 * Resolver process handle.
127 struct GNUNET_OS_Process *resolver;
130 * Number of tasks running that still need the resolver.
132 static unsigned int resolver_users;
136 * Context for a plugin test.
142 * Handle to the active NAT test.
144 struct GNUNET_NAT_Test *tst;
147 * Task identifier for the timeout.
149 GNUNET_SCHEDULER_TaskIdentifier tsk;
152 * Name of plugin under test.
160 * Display the result of the test.
162 * @param tc test context
163 * @param result GNUNET_YES on success
166 display_test_result (struct TestContext *tc, int result)
168 if (GNUNET_YES != result)
170 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
174 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
176 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
178 GNUNET_SCHEDULER_cancel (tc->tsk);
179 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
183 GNUNET_NAT_test_stop (tc->tst);
188 if ((0 == resolver_users) && (NULL != resolver))
190 GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
191 GNUNET_OS_process_close (resolver);
198 * Function called by NAT on success.
199 * Clean up and update GUI (with success).
201 * @param cls test context
202 * @param success currently always GNUNET_OK
205 result_callback (void *cls, int success)
207 struct TestContext *tc = cls;
209 display_test_result (tc, success);
214 * Function called if NAT failed to confirm success.
215 * Clean up and update GUI (with failure).
217 * @param cls test context
218 * @param tc scheduler callback
221 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
223 struct TestContext *tstc = cls;
225 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
226 display_test_result (tstc, GNUNET_NO);
231 * Test our plugin's configuration (NAT traversal, etc.).
233 * @param cfg configuration to test
236 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
240 unsigned long long bnd_port;
241 unsigned long long adv_port;
242 struct TestContext *tc;
245 GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
251 ("No transport plugins configured, peer will never communicate\n"));
255 for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
257 char section[12 + strlen (tok)];
259 GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
261 GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
264 _("No port configured for plugin `%s', cannot test it\n"), tok);
268 GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
271 if (NULL == resolver)
273 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-resolver",
274 "gnunet-service-resolver", NULL);
276 GNUNET_RESOLVER_connect (cfg);
277 tc = GNUNET_malloc (sizeof (struct TestContext));
278 tc->name = GNUNET_strdup (tok);
280 GNUNET_NAT_test_start (cfg,
283 "udp")) ? GNUNET_NO : GNUNET_YES,
284 (uint16_t) bnd_port, (uint16_t) adv_port,
285 &result_callback, tc);
288 display_test_result (tc, GNUNET_SYSERR);
291 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
293 GNUNET_free (plugins);
298 * Shutdown, print statistics.
301 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
303 struct GNUNET_TIME_Relative duration;
307 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
310 GNUNET_TRANSPORT_disconnect (handle);
311 if (benchmark_receive)
313 duration = GNUNET_TIME_absolute_get_duration (start_time);
314 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %llu ms)\n"),
315 1000 * traffic_received / (1 + duration.rel_value),
316 traffic_received, (unsigned long long) duration.rel_value);
320 duration = GNUNET_TIME_absolute_get_duration (start_time);
321 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %llu ms)\n"),
322 1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
323 (unsigned long long) duration.rel_value);
329 * Function called to notify a client about the socket
330 * begin ready to queue more data. "buf" will be
331 * NULL and "size" zero if the socket was closed for
332 * writing in the meantime.
335 * @param size number of bytes available in buf
336 * @param buf where the callee should write the message
337 * @return number of bytes written to buf
340 transmit_data (void *cls, size_t size, void *buf)
342 struct GNUNET_MessageHeader *m = buf;
344 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
345 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
346 m->size = ntohs (size);
347 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
348 memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
349 traffic_sent += size;
350 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, 32 * 1024, 0,
351 GNUNET_TIME_UNIT_FOREVER_REL,
352 &transmit_data, NULL);
354 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
361 * Function called to notify transport users that another
362 * peer connected to us.
365 * @param peer the peer that connected
366 * @param ats performance data
367 * @param ats_count number of entries in ats (excluding 0-termination)
370 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
371 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
374 FPRINTF (stdout, _("Connected to %s\n"), GNUNET_i2s (peer));
375 if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
380 start_time = GNUNET_TIME_absolute_get ();
381 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer, 32 * 1024, 0,
382 GNUNET_TIME_UNIT_FOREVER_REL,
383 &transmit_data, NULL);
387 /* all done, terminate instantly */
388 GNUNET_SCHEDULER_cancel (end);
389 end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
395 * Function called to notify transport users that another
396 * peer disconnected from us.
399 * @param peer the peer that disconnected
402 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
405 FPRINTF (stdout, _("Disconnected from %s\n"), GNUNET_i2s (peer));
406 if ((0 == memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity))) &&
409 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
411 GNUNET_SCHEDULER_cancel (end);
412 end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
418 * Function called by the transport for each received message.
421 * @param peer (claimed) identity of the other peer
422 * @param message the message
423 * @param ats performance data
424 * @param ats_count number of entries in ats
427 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
428 const struct GNUNET_MessageHeader *message,
429 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
431 if (!benchmark_receive)
434 FPRINTF (stdout, _("Received %u bytes from %s\n"),
435 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
436 if (traffic_received == 0)
437 start_time = GNUNET_TIME_absolute_get ();
438 traffic_received += ntohs (message->size);
442 process_string (void *cls, const char *address)
444 struct GNUNET_HELLO_Address *addrcp = cls;
446 if ((address != NULL))
448 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
453 GNUNET_free (addrcp);
458 * Function to call with a human-readable format of an address
461 * @param peer identity of the peer
462 * @param transport name of the plugin
463 * @param addr binary address
464 * @param addrlen number of bytes in addr
467 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
468 const struct GNUNET_HELLO_Address *address)
470 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
472 if ((address == NULL) || (peer == NULL))
478 /* Resolve address to string */
479 GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
480 GNUNET_TIME_UNIT_MINUTES, &process_string,
481 GNUNET_HELLO_address_copy(address));
486 * Main function that will be run by the scheduler.
489 * @param args remaining command-line arguments
490 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
491 * @param cfg configuration
494 run (void *cls, char *const *args, const char *cfgfile,
495 const struct GNUNET_CONFIGURATION_Handle *cfg)
497 if (test_configuration)
499 do_test_configuration (cfg);
501 if (benchmark_send && (NULL == cpid))
503 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
510 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
512 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
516 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
517 ¬ify_connect, ¬ify_disconnect);
518 GNUNET_TRANSPORT_try_connect (handle, &pid);
520 GNUNET_SCHEDULER_add_delayed (benchmark_send ?
521 GNUNET_TIME_UNIT_FOREVER_REL :
522 GNUNET_TIME_UNIT_SECONDS, &do_disconnect,
525 else if (benchmark_receive)
528 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
529 ¬ify_connect, ¬ify_disconnect);
530 GNUNET_TRANSPORT_try_connect (handle, &pid);
532 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
533 &do_disconnect, NULL);
535 if (iterate_connections)
537 GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL, GNUNET_YES,
538 GNUNET_TIME_UNIT_MINUTES,
539 &process_address, (void *) cfg);
545 main (int argc, char *const *argv)
547 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
548 {'b', "benchmark", NULL,
549 gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
550 0, &GNUNET_GETOPT_set_one, &benchmark_receive},
551 {'C', "connect", "PEER",
552 gettext_noop ("try to connect to the given peer"),
553 1, &GNUNET_GETOPT_set_string, &cpid},
554 {'i', "information", NULL,
555 gettext_noop ("provide information about all current connections (once)"),
556 0, &GNUNET_GETOPT_set_one, &iterate_connections},
559 ("send data for benchmarking to the other peer (until CTRL-C)"),
560 0, &GNUNET_GETOPT_set_one, &benchmark_send},
562 gettext_noop ("test transport configuration (involves external server)"),
563 0, &GNUNET_GETOPT_set_one, &test_configuration},
564 {'n', "numeric", NULL,
565 gettext_noop ("do not resolve hostnames"),
566 0, &GNUNET_GETOPT_set_one, &numeric},
567 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
568 GNUNET_GETOPT_OPTION_END
571 GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
573 ("Direct access to transport service."), options,
574 &run, NULL)) ? ret : 1;
578 /* end of gnunet-transport.c */