2 This file is part of GNUnet.
3 (C) 2011-2014 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.
31 #include "gnunet_util_lib.h"
32 #include "gnunet_resolver_service.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_transport_service.h"
35 #include "gnunet_nat_lib.h"
39 * Timeout for a connections
41 #define CONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
44 * Benchmarking block size in bye
46 #define DEFAULT_MESSAGE_SIZE 1024
49 * Benchmarking message count
51 #define DEFAULT_MESSAGE_COUNT 1024
56 static int benchmark_send;
61 static int benchmark_receive;
66 static unsigned int benchmark_count;
71 static unsigned int benchmark_size;
74 * Which peer should we connect to?
79 * Handle to transport service.
81 static struct GNUNET_TRANSPORT_Handle *handle;
84 * Configuration handle
86 static struct GNUNET_CONFIGURATION_Handle *cfg;
91 struct GNUNET_TRANSPORT_TryConnectHandle *tc_handle;
96 * Global return value (0 success).
100 * Number of bytes of traffic we received so far.
102 static unsigned long long traffic_received;
105 * Number of bytes of traffic we sent so far.
107 static unsigned long long traffic_sent;
110 * Starting time of transmitting/receiving data.
112 static struct GNUNET_TIME_Absolute start_time;
115 * Handle for current transmission request.
117 static struct GNUNET_TRANSPORT_TransmitHandle *th;
119 struct GNUNET_TRANSPORT_Blacklist *bl_handle;
122 * Identity of the peer we transmit to / connect to.
123 * (equivalent to 'cpid' string).
125 static struct GNUNET_PeerIdentity pid;
128 * Task scheduled for cleanup / termination of the process.
130 static GNUNET_SCHEDULER_TaskIdentifier end;
133 * Task for operation timeout
135 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
138 * Selected level of verbosity.
140 static int verbosity;
143 * Task run in monitor mode when the user presses CTRL-C to abort.
144 * Stops monitoring activity.
147 * @param tc scheduler context
150 shutdown_task (void *cls,
151 const struct GNUNET_SCHEDULER_TaskContext *tc)
153 struct GNUNET_TIME_Relative duration;
155 end = GNUNET_SCHEDULER_NO_TASK;
156 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
158 GNUNET_SCHEDULER_cancel (op_timeout);
159 op_timeout = GNUNET_SCHEDULER_NO_TASK;
162 if (NULL != tc_handle)
164 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
169 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
174 GNUNET_TRANSPORT_disconnect (handle);
177 if (NULL != bl_handle )
179 GNUNET_TRANSPORT_blacklist_cancel (bl_handle);
185 duration = GNUNET_TIME_absolute_get_duration (start_time);
187 _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
188 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
190 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
192 if (benchmark_receive)
194 duration = GNUNET_TIME_absolute_get_duration (start_time);
196 _("Received %llu bytes/s (%llu bytes in %s)\n"),
197 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
199 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
206 operation_timeout (void *cls,
207 const struct GNUNET_SCHEDULER_TaskContext *tc)
210 op_timeout = GNUNET_SCHEDULER_NO_TASK;
211 if ((benchmark_send) || (benchmark_receive))
214 _("Failed to connect to `%s'\n"),
215 GNUNET_i2s_full (&pid));
216 if (GNUNET_SCHEDULER_NO_TASK != end)
217 GNUNET_SCHEDULER_cancel (end);
218 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
226 * Function called to notify a client about the socket
227 * begin ready to queue more data. @a buf will be
228 * NULL and @a size zero if the socket was closed for
229 * writing in the meantime.
232 * @param size number of bytes available in @a buf
233 * @param buf where the callee should write the message
234 * @return number of bytes written to @a buf
237 transmit_data (void *cls,
241 static msgs_sent = 0;
242 struct GNUNET_MessageHeader *m = buf;
245 if ((NULL == buf) || (0 == size))
253 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
254 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
255 m->size = ntohs (size);
256 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
257 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
258 traffic_sent += size;
260 if (msgs_sent <benchmark_count)
262 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
264 GNUNET_TIME_UNIT_FOREVER_REL,
265 &transmit_data, NULL);
270 FPRINTF (stdout, _("DONE!\n"));
274 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
285 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
287 GNUNET_SCHEDULER_cancel (op_timeout);
288 op_timeout = GNUNET_SCHEDULER_NO_TASK;
292 _("Successfully connected to `%s', starting to send %u messages in %u byte blocks\n"),
293 GNUNET_i2s (&pid), benchmark_count, benchmark_size);
294 start_time = GNUNET_TIME_absolute_get ();
296 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, benchmark_size,
297 GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
306 * Function called to notify transport users that another
307 * peer connected to us.
310 * @param peer the peer that connected
313 notify_connect (void *cls,
314 const struct GNUNET_PeerIdentity *peer)
316 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
319 _("Connected to different peer `%s'\n"), GNUNET_i2s (&pid));
325 _("Successfully connected to `%s'\n"),
328 if (NULL != tc_handle)
330 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
339 * Function called to notify transport users that another
340 * peer disconnected from us.
343 * @param peer the peer that disconnected
346 notify_disconnect (void *cls,
347 const struct GNUNET_PeerIdentity *peer)
349 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
353 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
355 if (GNUNET_SCHEDULER_NO_TASK != end)
356 GNUNET_SCHEDULER_cancel (end);
362 * Function called by the transport for each received message.
365 * @param peer (claimed) identity of the other peer
366 * @param message the message
369 notify_receive (void *cls,
370 const struct GNUNET_PeerIdentity *peer,
371 const struct GNUNET_MessageHeader *message)
373 if (benchmark_receive)
375 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
379 _("Received %u bytes from %s\n"),
380 (unsigned int) ntohs (message->size),
383 if (traffic_received == 0)
384 start_time = GNUNET_TIME_absolute_get ();
385 traffic_received += ntohs (message->size);
393 try_connect_cb (void *cls,
396 static int retries = 0;
398 if (GNUNET_OK == result)
408 FPRINTF (stdout, _("Retrying to connect to `%s'\n"), GNUNET_i2s (&pid));
410 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
417 _("Failed to send connect request to transport service\n"));
418 if (GNUNET_SCHEDULER_NO_TASK != end)
419 GNUNET_SCHEDULER_cancel (end);
421 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
427 blacklist_cb (void *cls, const struct GNUNET_PeerIdentity *peer)
429 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
433 _("Denying connection to `%s'\n"),
435 return GNUNET_SYSERR;
440 _("Permitting connection to `%s'\n"),
448 * Function called with the result of the check if the 'transport'
449 * service is running.
451 * @param cls closure with our configuration
452 * @param result #GNUNET_YES if transport is running
455 testservice_task (void *cls, int result)
459 if (GNUNET_YES != result)
461 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
468 FPRINTF (stderr, _("No peer identity given\n"));
471 if ((GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
474 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
479 if (1 == benchmark_send)
482 _("Trying to send %u messages with size %u to peer `%s'\n"),
483 benchmark_count, benchmark_size, GNUNET_i2s (&pid));
485 else if (1 == benchmark_receive)
488 _("Trying to receive messages from peer `%s'\n"),
493 FPRINTF (stderr, _("No operation given\n"));
497 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
498 ¬ify_connect, ¬ify_disconnect);
502 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
507 bl_handle = GNUNET_TRANSPORT_blacklist (cfg, blacklist_cb, NULL);
508 tc_handle = GNUNET_TRANSPORT_try_connect(handle, &pid, try_connect_cb, NULL);
510 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
517 * Main function that will be run by the scheduler.
520 * @param args remaining command-line arguments
521 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
522 * @param mycfg configuration
528 const struct GNUNET_CONFIGURATION_Handle *mycfg)
530 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
531 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
532 &testservice_task, (void *) cfg);
536 main (int argc, char * const *argv)
539 benchmark_count = DEFAULT_MESSAGE_COUNT;
540 benchmark_size = DEFAULT_MESSAGE_SIZE;
542 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
545 gettext_noop ("send data to peer"),
546 0, &GNUNET_GETOPT_set_one, &benchmark_send},
547 { 'r', "receive", NULL, gettext_noop
548 ("receive data from peer"), 0,
549 &GNUNET_GETOPT_set_one, &benchmark_receive},
550 { 'n', "number", NULL, gettext_noop
551 ("number of messages to send"), 1,
552 &GNUNET_GETOPT_set_uint, &benchmark_count},
553 { 'm', "messagesize", NULL, gettext_noop
554 ("message size to use"), 1,
555 &GNUNET_GETOPT_set_uint, &benchmark_size},
556 { 'p', "peer", "PEER",
557 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
559 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
560 GNUNET_GETOPT_OPTION_END
563 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
566 res = GNUNET_PROGRAM_run (argc, argv,
568 gettext_noop ("Direct access to transport service."),
571 GNUNET_free((void *) argv);
572 if (GNUNET_OK == res)
577 /* end of gnunet-transport.c */