2 This file is part of GNUnet.
3 (C) 2009 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;
79 * Global return value (0 success).
84 * Number of bytes of traffic we received so far.
86 static unsigned long long traffic_received;
89 * Number of bytes of traffic we sent so far.
91 static unsigned long long traffic_sent;
94 * Starting time of transmitting/receiving data.
96 static struct GNUNET_TIME_Absolute start_time;
99 * Handle for current transmission request.
101 static struct GNUNET_TRANSPORT_TransmitHandle *th;
104 * Identity of the peer we transmit to / connect to.
105 * (equivalent to 'cpid' string).
107 static struct GNUNET_PeerIdentity pid;
110 * Task scheduled for cleanup / termination of the process.
112 static GNUNET_SCHEDULER_TaskIdentifier end;
115 * Selected level of verbosity.
117 static int verbosity;
120 * Resolver process handle.
122 struct GNUNET_OS_Process *resolver;
125 * Number of tasks running that still need the resolver.
127 static unsigned int resolver_users;
131 * Context for a plugin test.
137 * Handle to the active NAT test.
139 struct GNUNET_NAT_Test *tst;
142 * Task identifier for the timeout.
144 GNUNET_SCHEDULER_TaskIdentifier tsk;
147 * Name of plugin under test.
155 * Display the result of the test.
157 * @param tc test context
158 * @param result GNUNET_YES on success
161 display_test_result (struct TestContext *tc, int result)
163 if (GNUNET_YES != result)
166 "Configuration for plugin `%s' did not work!\n",
172 "Configuration for plugin `%s' is working!\n",
175 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
177 GNUNET_SCHEDULER_cancel (tc->tsk);
178 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
182 GNUNET_NAT_test_stop (tc->tst);
187 if ( (0 == resolver_users) &&
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,
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,
264 GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
268 _("No port configured for plugin `%s', cannot test it\n"),
273 GNUNET_CONFIGURATION_get_value_number (cfg, section,
274 "ADVERTISED_PORT", &adv_port))
276 if (NULL == resolver)
278 GNUNET_OS_start_process (NULL, NULL, "gnunet-service-resolver",
279 "gnunet-service-resolver", NULL);
281 GNUNET_RESOLVER_connect (cfg);
282 tc = GNUNET_malloc (sizeof (struct TestContext));
283 tc->name = GNUNET_strdup (tok);
285 GNUNET_NAT_test_start (cfg,
286 (0 == strcasecmp (tok, "udp"))
290 (uint16_t) adv_port, &result_callback, tc);
293 display_test_result (tc, GNUNET_SYSERR);
296 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
298 GNUNET_free (plugins);
303 * Shutdown, print statistics.
306 do_disconnect (void *cls,
307 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);
321 _("Received %llu bytes/s (%llu bytes in %llu ms)\n"),
322 1000 * traffic_received / (1+duration.rel_value),
324 (unsigned long long) duration.rel_value);
328 duration = GNUNET_TIME_absolute_get_duration (start_time);
330 _("Transmitted %llu bytes/s (%llu bytes in %llu ms)\n"),
331 1000 * traffic_sent / (1+duration.rel_value),
333 (unsigned long long) duration.rel_value);
339 * Function called to notify a client about the socket
340 * begin ready to queue more data. "buf" will be
341 * NULL and "size" zero if the socket was closed for
342 * writing in the meantime.
345 * @param size number of bytes available in buf
346 * @param buf where the callee should write the message
347 * @return number of bytes written to buf
350 transmit_data (void *cls, size_t size,
353 struct GNUNET_MessageHeader *m = buf;
355 GNUNET_assert (size >= sizeof (struct GNUNET_MessageHeader));
356 GNUNET_assert (size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
357 m->size = ntohs (size);
358 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
359 memset (&m[1], 52, size - sizeof (struct GNUNET_MessageHeader));
360 traffic_sent += size;
361 th = GNUNET_TRANSPORT_notify_transmit_ready (handle,
365 GNUNET_TIME_UNIT_FOREVER_REL,
366 &transmit_data, NULL);
369 _("Transmitting %u bytes to %s\n"),
377 * Function called to notify transport users that another
378 * peer connected to us.
381 * @param peer the peer that connected
382 * @param ats performance data
383 * @param ats_count number of entries in ats (excluding 0-termination)
386 notify_connect (void *cls,
387 const struct GNUNET_PeerIdentity
390 GNUNET_ATS_Information
391 * ats, uint32_t ats_count)
395 _("Connected to %s\n"),
397 if (0 != memcmp (&pid,
399 sizeof (struct GNUNET_PeerIdentity)))
404 start_time = GNUNET_TIME_absolute_get ();
405 th = GNUNET_TRANSPORT_notify_transmit_ready (handle,
409 GNUNET_TIME_UNIT_FOREVER_REL,
410 &transmit_data, NULL);
414 /* all done, terminate instantly */
415 GNUNET_SCHEDULER_cancel (end);
416 end = GNUNET_SCHEDULER_add_now (&do_disconnect,
423 * Function called to notify transport users that another
424 * peer disconnected from us.
427 * @param peer the peer that disconnected
430 notify_disconnect (void *cls,
432 GNUNET_PeerIdentity * peer)
436 _("Disconnected from %s\n"),
438 if ( (0 == memcmp (&pid,
440 sizeof (struct GNUNET_PeerIdentity))) &&
443 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
445 GNUNET_SCHEDULER_cancel (end);
446 end = GNUNET_SCHEDULER_add_now (&do_disconnect,
453 * Function called by the transport for each received message.
456 * @param peer (claimed) identity of the other peer
457 * @param message the message
458 * @param ats performance data
459 * @param ats_count number of entries in ats
462 notify_receive (void *cls,
464 GNUNET_PeerIdentity * peer,
466 GNUNET_MessageHeader *
469 GNUNET_ATS_Information
470 * ats, uint32_t ats_count)
472 if (! benchmark_receive)
476 _("Received %u bytes from %s\n"),
477 (unsigned int) ntohs (message->size),
479 if (traffic_received == 0)
480 start_time = GNUNET_TIME_absolute_get ();
481 traffic_received += ntohs (message->size);
486 * Function to call with a human-readable format of an address
489 * @param address NULL on error, otherwise 0-terminated printable UTF-8 string
492 process_address (void *cls, const struct GNUNET_PeerIdentity *peer,
493 const char *transport, const void *addr, size_t addrlen)
495 if ((peer != NULL) || (transport != NULL) ||
496 ((addr != NULL) && (addrlen > 0)))
498 _("Peer `%s' plugin: `%s' address `%s'\n"),
499 (peer != NULL) ? GNUNET_i2s (peer) : "<unknown>",
500 (transport != NULL) ? transport : "<unknown>",
501 ((addr != NULL) && (addrlen > 0) && (transport != NULL)) ?
502 "how do i resolve the name without transport service?" :
508 * Main function that will be run by the scheduler.
511 * @param args remaining command-line arguments
512 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
513 * @param cfg configuration
516 run (void *cls, char *const *args, const char *cfgfile,
517 const struct GNUNET_CONFIGURATION_Handle *cfg)
519 if (test_configuration)
521 do_test_configuration (cfg);
523 if (benchmark_send && (NULL == cpid))
525 fprintf (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
533 GNUNET_CRYPTO_hash_from_string (cpid, &pid.hashPubKey))
536 _("Failed to parse peer identity `%s'\n"),
540 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
544 GNUNET_TRANSPORT_try_connect (handle, &pid);
545 end = GNUNET_SCHEDULER_add_delayed (benchmark_send
546 ? GNUNET_TIME_UNIT_FOREVER_REL
547 : GNUNET_TIME_UNIT_SECONDS,
550 } else if (benchmark_receive)
552 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL,
556 GNUNET_TRANSPORT_try_connect (handle, &pid);
557 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
561 if (iterate_connections)
563 GNUNET_TRANSPORT_address_iterate (cfg, GNUNET_TIME_UNIT_MINUTES,
564 &process_address, NULL);
570 main (int argc, char *const *argv)
572 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
573 {'b', "benchmark", NULL,
574 gettext_noop ("measure how fast we are receiving data (until CTRL-C)"),
575 0, &GNUNET_GETOPT_set_one, &benchmark_receive},
576 {'C', "connect", "PEER",
577 gettext_noop ("try to connect to the given peer"),
578 1, &GNUNET_GETOPT_set_string, &cpid},
579 {'i', "information", NULL,
580 gettext_noop ("provide information about all current connections (once)"),
581 0, &GNUNET_GETOPT_set_one, &iterate_connections},
583 gettext_noop ("send data for benchmarking to the other peer (until CTRL-C)"),
584 0, &GNUNET_GETOPT_set_one, &benchmark_send},
586 gettext_noop ("test transport configuration (involves external server)"),
587 0, &GNUNET_GETOPT_set_one, &test_configuration},
588 GNUNET_GETOPT_OPTION_VERBOSE(&verbosity),
589 GNUNET_GETOPT_OPTION_END
592 GNUNET_PROGRAM_run (argc, argv, "gnunet-transport",
593 gettext_noop ("Direct access to transport service."),
594 options, &run, NULL)) ? ret : 1;
598 /* end of gnunet-transport.c */