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)
45 * Which peer should we connect to?
50 * Handle to transport service.
52 static struct GNUNET_TRANSPORT_Handle *handle;
57 static int benchmark_send;
62 static int benchmark_receive;
67 static int benchmark_receive;
72 static int iterate_connections;
77 static int test_configuration;
82 static int monitor_connections;
90 * Global return value (0 success).
95 * Number of bytes of traffic we received so far.
97 static unsigned long long traffic_received;
100 * Number of bytes of traffic we sent so far.
102 static unsigned long long traffic_sent;
105 * Starting time of transmitting/receiving data.
107 static struct GNUNET_TIME_Absolute start_time;
110 * Handle for current transmission request.
112 static struct GNUNET_TRANSPORT_TransmitHandle *th;
115 * Identity of the peer we transmit to / connect to.
116 * (equivalent to 'cpid' string).
118 static struct GNUNET_PeerIdentity pid;
121 * Task scheduled for cleanup / termination of the process.
123 static GNUNET_SCHEDULER_TaskIdentifier end;
126 * Selected level of verbosity.
128 static int verbosity;
131 * Resolver process handle.
133 struct GNUNET_OS_Process *resolver;
136 * Number of tasks running that still need the resolver.
138 static unsigned int resolver_users;
142 * Context for a plugin test.
148 * Handle to the active NAT test.
150 struct GNUNET_NAT_Test *tst;
153 * Task identifier for the timeout.
155 GNUNET_SCHEDULER_TaskIdentifier tsk;
158 * Name of plugin under test.
166 * Display the result of the test.
168 * @param tc test context
169 * @param result GNUNET_YES on success
172 display_test_result (struct TestContext *tc, int result)
174 if (GNUNET_YES != result)
176 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
180 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
182 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
184 GNUNET_SCHEDULER_cancel (tc->tsk);
185 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
189 GNUNET_NAT_test_stop (tc->tst);
194 if ((0 == resolver_users) && (NULL != resolver))
196 GNUNET_break (0 == GNUNET_OS_process_kill (resolver, SIGTERM));
197 GNUNET_OS_process_close (resolver);
204 * Function called by NAT on success.
205 * Clean up and update GUI (with success).
207 * @param cls test context
208 * @param success currently always GNUNET_OK
211 result_callback (void *cls, int success)
213 struct TestContext *tc = cls;
215 display_test_result (tc, success);
220 * Function called if NAT failed to confirm success.
221 * Clean up and update GUI (with failure).
223 * @param cls test context
224 * @param tc scheduler callback
227 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
229 struct TestContext *tstc = cls;
231 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
232 display_test_result (tstc, GNUNET_NO);
237 * Test our plugin's configuration (NAT traversal, etc.).
239 * @param cfg configuration to test
242 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
246 unsigned long long bnd_port;
247 unsigned long long adv_port;
248 struct TestContext *tc;
251 GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
257 ("No transport plugins configured, peer will never communicate\n"));
261 for (tok = strtok (plugins, " "); tok != NULL; tok = strtok (NULL, " "))
263 char section[12 + strlen (tok)];
265 GNUNET_snprintf (section, sizeof (section), "transport-%s", tok);
267 GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT", &bnd_port))
270 _("No port configured for plugin `%s', cannot test it\n"), tok);
274 GNUNET_CONFIGURATION_get_value_number (cfg, section, "ADVERTISED_PORT",
277 if (NULL == resolver)
279 GNUNET_OS_start_process (GNUNET_YES, NULL, NULL, "gnunet-service-resolver",
280 "gnunet-service-resolver", NULL);
282 GNUNET_RESOLVER_connect (cfg);
283 tc = GNUNET_malloc (sizeof (struct TestContext));
284 tc->name = GNUNET_strdup (tok);
286 GNUNET_NAT_test_start (cfg,
289 "udp")) ? GNUNET_NO : GNUNET_YES,
290 (uint16_t) bnd_port, (uint16_t) adv_port,
291 &result_callback, tc);
294 display_test_result (tc, GNUNET_SYSERR);
297 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
299 GNUNET_free (plugins);
304 * Shutdown, print statistics.
307 do_disconnect (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
309 struct GNUNET_TIME_Relative duration;
313 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
316 GNUNET_TRANSPORT_disconnect (handle);
317 if (benchmark_receive)
319 duration = GNUNET_TIME_absolute_get_duration (start_time);
320 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %llu ms)\n"),
321 1000 * traffic_received / (1 + duration.rel_value),
322 traffic_received, (unsigned long long) duration.rel_value);
326 duration = GNUNET_TIME_absolute_get_duration (start_time);
327 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %llu ms)\n"),
328 1000 * traffic_sent / (1 + duration.rel_value), traffic_sent,
329 (unsigned long long) duration.rel_value);
335 * Function called to notify a client about the socket
336 * begin ready to queue more data. "buf" will be
337 * NULL and "size" zero if the socket was closed for
338 * writing in the meantime.
341 * @param size number of bytes available in buf
342 * @param buf where the callee should write the message
343 * @return number of bytes written to buf
346 transmit_data (void *cls, size_t size, void *buf)
348 struct GNUNET_MessageHeader *m = buf;
350 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
351 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
352 m->size = ntohs (size);
353 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
354 memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
355 traffic_sent += size;
356 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, 32 * 1024, 0,
357 GNUNET_TIME_UNIT_FOREVER_REL,
358 &transmit_data, NULL);
360 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
367 * Function called to notify transport users that another
368 * peer connected to us.
371 * @param peer the peer that connected
372 * @param ats performance data
373 * @param ats_count number of entries in ats (excluding 0-termination)
376 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer,
377 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
380 FPRINTF (stdout, _("Connected to %s\n"), GNUNET_i2s (peer));
381 if (0 != memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity)))
386 start_time = GNUNET_TIME_absolute_get ();
387 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer, 32 * 1024, 0,
388 GNUNET_TIME_UNIT_FOREVER_REL,
389 &transmit_data, NULL);
393 /* all done, terminate instantly */
394 GNUNET_SCHEDULER_cancel (end);
395 end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
401 * Function called to notify transport users that another
402 * peer disconnected from us.
405 * @param peer the peer that disconnected
408 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
411 FPRINTF (stdout, _("Disconnected from %s\n"), GNUNET_i2s (peer));
412 if ((0 == memcmp (&pid, peer, sizeof (struct GNUNET_PeerIdentity))) &&
415 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
417 GNUNET_SCHEDULER_cancel (end);
418 end = GNUNET_SCHEDULER_add_now (&do_disconnect, NULL);
424 * Function called by the transport for each received message.
427 * @param peer (claimed) identity of the other peer
428 * @param message the message
429 * @param ats performance data
430 * @param ats_count number of entries in ats
433 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
434 const struct GNUNET_MessageHeader *message,
435 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
437 if (!benchmark_receive)
440 FPRINTF (stdout, _("Received %u bytes from %s\n"),
441 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
442 if (traffic_received == 0)
443 start_time = GNUNET_TIME_absolute_get ();
444 traffic_received += ntohs (message->size);
448 process_string (void *cls, const char *address)
450 struct GNUNET_HELLO_Address *addrcp = cls;
452 if ((address != NULL))
454 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (&addrcp->peer), addrcp->transport_name, address);
459 GNUNET_free (addrcp);
464 * Function to call with a binary address
467 * @param peer identity of the peer
468 * @param address binary address (NULL on disconnect)
471 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
472 const struct GNUNET_HELLO_Address *address)
474 const struct GNUNET_CONFIGURATION_Handle *cfg = cls;
484 FPRINTF (stdout, _("Peer `%s' disconnected\n"), GNUNET_i2s (peer));
488 /* Resolve address to string */
489 GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
490 GNUNET_TIME_UNIT_MINUTES, &process_string,
491 GNUNET_HELLO_address_copy(address));
496 * Task run in monitor mode when the user presses CTRL-C to abort.
497 * Stops monitoring activity.
499 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
500 * @param tc scheduler context
503 shutdown_task (void *cls,
504 const struct GNUNET_SCHEDULER_TaskContext *tc)
506 struct GNUNET_TRANSPORT_PeerIterateContext *pic = cls;
508 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pic);
514 * Main function that will be run by the scheduler.
517 * @param args remaining command-line arguments
518 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
519 * @param cfg configuration
522 run (void *cls, char *const *args, const char *cfgfile,
523 const struct GNUNET_CONFIGURATION_Handle *cfg)
525 if (test_configuration)
527 do_test_configuration (cfg);
529 if (benchmark_send && (NULL == cpid))
531 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
538 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
540 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
544 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
545 ¬ify_connect, ¬ify_disconnect);
546 GNUNET_TRANSPORT_try_connect (handle, &pid);
548 GNUNET_SCHEDULER_add_delayed (benchmark_send ?
549 GNUNET_TIME_UNIT_FOREVER_REL :
550 GNUNET_TIME_UNIT_SECONDS, &do_disconnect,
553 else if (benchmark_receive)
556 GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
557 ¬ify_connect, ¬ify_disconnect);
558 GNUNET_TRANSPORT_try_connect (handle, &pid);
560 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
561 &do_disconnect, NULL);
563 if (iterate_connections)
565 GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL, GNUNET_YES,
566 GNUNET_TIME_UNIT_MINUTES,
567 &process_address, (void *) cfg);
569 if (monitor_connections)
571 struct GNUNET_TRANSPORT_PeerIterateContext *pic;
573 pic = GNUNET_TRANSPORT_peer_get_active_addresses (cfg, NULL, GNUNET_NO,
574 GNUNET_TIME_UNIT_FOREVER_REL,
575 &process_address, (void *) cfg);
576 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
584 main (int argc, char *const *argv)
586 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
587 {'b', "benchmark", NULL,
588 gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
589 0, &GNUNET_GETOPT_set_one, &benchmark_receive},
590 {'C', "connect", "PEER",
591 gettext_noop ("try to connect to the given peer"),
592 1, &GNUNET_GETOPT_set_string, &cpid},
593 {'i', "information", NULL,
594 gettext_noop ("provide information about all current connections (once)"),
595 0, &GNUNET_GETOPT_set_one, &iterate_connections},
596 {'m', "monitor", NULL,
597 gettext_noop ("provide information about all current connections (continuously)"),
598 0, &GNUNET_GETOPT_set_one, &monitor_connections},
599 {'n', "numeric", NULL,
600 gettext_noop ("do not resolve hostnames"),
601 0, &GNUNET_GETOPT_set_one, &numeric},
604 ("send data for benchmarking to the other peer (until CTRL-C)"),
605 0, &GNUNET_GETOPT_set_one, &benchmark_send},
607 gettext_noop ("test transport configuration (involves external server)"),
608 0, &GNUNET_GETOPT_set_one, &test_configuration},
609 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
610 GNUNET_GETOPT_OPTION_END
613 GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
615 ("Direct access to transport service."), options,
616 &run, NULL)) ? ret : 1;
620 /* end of gnunet-transport.c */