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, 60)
43 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
44 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 * Benchmarking block size in KB
52 * Which peer should we connect to?
57 * Handle to transport service.
59 static struct GNUNET_TRANSPORT_Handle *handle;
62 * Configuration handle
64 static struct GNUNET_CONFIGURATION_Handle *cfg;
69 struct GNUNET_TRANSPORT_TryConnectHandle * tc_handle;
74 static int benchmark_send;
79 static int benchmark_receive;
84 static int benchmark_receive;
89 static int iterate_connections;
94 static int iterate_all;
99 static int test_configuration;
104 static int monitor_connects;
109 static int monitor_connections;
114 static int try_connect;
122 * Global return value (0 success).
127 * Current number of connections in monitor mode
129 static int monitor_connect_counter;
132 * Number of bytes of traffic we received so far.
134 static unsigned long long traffic_received;
137 * Number of bytes of traffic we sent so far.
139 static unsigned long long traffic_sent;
142 * Starting time of transmitting/receiving data.
144 static struct GNUNET_TIME_Absolute start_time;
147 * Handle for current transmission request.
149 static struct GNUNET_TRANSPORT_TransmitHandle *th;
152 * Map storing information about monitored peers
154 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
159 struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
162 * Identity of the peer we transmit to / connect to.
163 * (equivalent to 'cpid' string).
165 static struct GNUNET_PeerIdentity pid;
168 * Task scheduled for cleanup / termination of the process.
170 static GNUNET_SCHEDULER_TaskIdentifier end;
173 * Task for operation timeout
175 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
178 * Selected level of verbosity.
180 static int verbosity;
183 * Resolver process handle.
185 struct GNUNET_OS_Process *resolver;
188 * Number of tasks running that still need the resolver.
190 static unsigned int resolver_users;
193 * Number of address resolutions pending
195 static unsigned int address_resolutions;
198 * Address resolutions pending in progress
200 static unsigned int address_resolution_in_progress;
204 * Context for a plugin test.
210 * Handle to the active NAT test.
212 struct GNUNET_NAT_Test *tst;
215 * Task identifier for the timeout.
217 GNUNET_SCHEDULER_TaskIdentifier tsk;
220 * Name of plugin under test.
228 enum GNUNET_TRANSPORT_PeerState state;
229 struct GNUNET_TIME_Absolute state_timeout;
230 struct GNUNET_HELLO_Address *address;
234 int destroy_it (void *cls,
235 const struct GNUNET_PeerIdentity *key,
238 struct MonitoredPeer *m = value;
239 GNUNET_CONTAINER_multipeermap_remove (monitored_peers, key, value);
240 GNUNET_free_non_null (m->address);
246 * Task run in monitor mode when the user presses CTRL-C to abort.
247 * Stops monitoring activity.
249 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
250 * @param tc scheduler context
253 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
255 struct GNUNET_TIME_Relative duration;
256 end = GNUNET_SCHEDULER_NO_TASK;
257 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
259 GNUNET_SCHEDULER_cancel (op_timeout);
260 op_timeout = GNUNET_SCHEDULER_NO_TASK;
262 if (NULL != tc_handle)
264 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
269 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
274 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
279 GNUNET_TRANSPORT_disconnect (handle);
284 duration = GNUNET_TIME_absolute_get_duration (start_time);
285 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
286 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
288 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
290 if (benchmark_receive)
292 duration = GNUNET_TIME_absolute_get_duration (start_time);
293 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
294 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
296 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
299 if (NULL != monitored_peers)
301 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
302 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
303 monitored_peers = NULL;
307 static struct ResolutionContext *rc_head;
308 static struct ResolutionContext *rc_tail;
310 struct ResolutionContext
312 struct ResolutionContext *next;
313 struct ResolutionContext *prev;
314 struct GNUNET_PeerIdentity id;
315 struct GNUNET_HELLO_Address *addrcp;
316 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
317 enum GNUNET_TRANSPORT_PeerState state;
318 struct GNUNET_TIME_Absolute state_timeout;
324 operation_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
326 struct ResolutionContext *cur;
327 struct ResolutionContext *next;
328 op_timeout = GNUNET_SCHEDULER_NO_TASK;
329 if ((try_connect) || (benchmark_send) || (benchmark_receive))
331 FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_i2s_full (&pid));
332 if (GNUNET_SCHEDULER_NO_TASK != end)
333 GNUNET_SCHEDULER_cancel (end);
334 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
338 if (iterate_connections)
341 while (NULL != (cur = next))
344 FPRINTF (stdout, _("Failed to resolve address for peer `%s'\n"),
345 GNUNET_i2s (&cur->addrcp->peer));
347 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
348 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
349 GNUNET_free(cur->transport);
350 GNUNET_free(cur->addrcp);
354 FPRINTF (stdout, "%s", _("Failed to list connections, timeout occured\n") );
355 if (GNUNET_SCHEDULER_NO_TASK != end)
356 GNUNET_SCHEDULER_cancel (end);
357 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
365 * Display the result of the test.
367 * @param tc test context
368 * @param result #GNUNET_YES on success
371 display_test_result (struct TestContext *tc, int result)
373 if (GNUNET_YES != result)
375 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
379 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
381 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
383 GNUNET_SCHEDULER_cancel (tc->tsk);
384 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
388 GNUNET_NAT_test_stop (tc->tst);
393 if ((0 == resolver_users) && (NULL != resolver))
395 GNUNET_break(0 == GNUNET_OS_process_kill (resolver, GNUNET_TERM_SIG));
396 GNUNET_OS_process_destroy (resolver);
402 * Function called by NAT on success.
403 * Clean up and update GUI (with success).
405 * @param cls test context
406 * @param success currently always #GNUNET_OK
407 * @param emsg error message, NULL on success
410 result_callback (void *cls, int success, const char *emsg)
412 struct TestContext *tc = cls;
414 display_test_result (tc, success);
418 * Function called if NAT failed to confirm success.
419 * Clean up and update GUI (with failure).
421 * @param cls test context
422 * @param tc scheduler callback
425 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
427 struct TestContext *tstc = cls;
429 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
430 display_test_result (tstc, GNUNET_NO);
434 * Test our plugin's configuration (NAT traversal, etc.).
436 * @param cfg configuration to test
439 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
443 unsigned long long bnd_port;
444 unsigned long long adv_port;
445 struct TestContext *tc;
449 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
452 FPRINTF (stderr, "%s", _
453 ("No transport plugins configured, peer will never communicate\n") );
457 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
459 char section[12 + strlen (tok)];
461 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
463 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
467 _("No port configured for plugin `%s', cannot test it\n"), tok);
471 != GNUNET_CONFIGURATION_get_value_number (cfg, section,
472 "ADVERTISED_PORT", &adv_port))
474 if (NULL == resolver)
476 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
477 resolver = GNUNET_OS_start_process (GNUNET_YES,
478 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, binary,
479 "gnunet-service-resolver", NULL );
483 GNUNET_RESOLVER_connect (cfg);
484 tc = GNUNET_new (struct TestContext);
485 tc->name = GNUNET_strdup (tok);
486 tc->tst = GNUNET_NAT_test_start (cfg,
487 (0 == strcasecmp (tok, "udp")) ? GNUNET_NO : GNUNET_YES,
488 (uint16_t) bnd_port, (uint16_t) adv_port, &result_callback, tc);
491 display_test_result (tc, GNUNET_SYSERR);
494 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
496 GNUNET_free(plugins);
500 * Function called to notify a client about the socket
501 * begin ready to queue more data. @a buf will be
502 * NULL and @a size zero if the socket was closed for
503 * writing in the meantime.
506 * @param size number of bytes available in @a buf
507 * @param buf where the callee should write the message
508 * @return number of bytes written to @a buf
511 transmit_data (void *cls, size_t size, void *buf)
513 struct GNUNET_MessageHeader *m = buf;
515 if ((NULL == buf) || (0 == size))
521 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
522 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
523 m->size = ntohs (size);
524 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
525 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
526 traffic_sent += size;
527 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, BLOCKSIZE * 1024,
528 0, GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
530 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
536 * Function called to notify transport users that another
537 * peer connected to us.
540 * @param peer the peer that connected
543 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
545 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
550 /* all done, terminate instantly */
551 FPRINTF (stdout, _("Successfully connected to `%s'\n"),
552 GNUNET_i2s_full (peer));
555 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
557 GNUNET_SCHEDULER_cancel (op_timeout);
558 op_timeout = GNUNET_SCHEDULER_NO_TASK;
561 if (GNUNET_SCHEDULER_NO_TASK != end)
562 GNUNET_SCHEDULER_cancel (end);
563 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
568 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
570 GNUNET_SCHEDULER_cancel (op_timeout);
571 op_timeout = GNUNET_SCHEDULER_NO_TASK;
575 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
576 GNUNET_i2s (&pid), BLOCKSIZE);
577 start_time = GNUNET_TIME_absolute_get ();
579 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
580 BLOCKSIZE * 1024, 0, GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data,
589 * Function called to notify transport users that another
590 * peer disconnected from us.
593 * @param peer the peer that disconnected
596 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
598 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
603 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
608 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
610 if (GNUNET_SCHEDULER_NO_TASK != end)
611 GNUNET_SCHEDULER_cancel (end);
617 * Function called to notify transport users that another
618 * peer connected to us.
621 * @param peer the peer that connected
624 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
626 monitor_connect_counter++;
627 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
628 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
630 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str,
631 _("Connected to"), GNUNET_i2s (peer), monitor_connect_counter);
635 * Function called to notify transport users that another
636 * peer disconnected from us.
639 * @param peer the peer that disconnected
642 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
644 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
645 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
647 GNUNET_assert(monitor_connect_counter > 0);
648 monitor_connect_counter--;
650 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str,
651 _("Disconnected from"), GNUNET_i2s (peer), monitor_connect_counter);
655 * Function called by the transport for each received message.
658 * @param peer (claimed) identity of the other peer
659 * @param message the message
662 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
663 const struct GNUNET_MessageHeader *message)
665 if (benchmark_receive)
667 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
670 FPRINTF (stdout, _("Received %u bytes from %s\n"),
671 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
673 if (traffic_received == 0)
674 start_time = GNUNET_TIME_absolute_get ();
675 traffic_received += ntohs (message->size);
681 resolve_address (const struct GNUNET_PeerIdentity *id,
682 const struct GNUNET_HELLO_Address *address, int numeric,
683 enum GNUNET_TRANSPORT_PeerState state,
684 struct GNUNET_TIME_Absolute state_timeout);
687 print_info (const struct GNUNET_PeerIdentity *id, const char *transport,
688 const char *addr, enum GNUNET_TRANSPORT_PeerState state,
689 struct GNUNET_TIME_Absolute state_timeout)
691 if ((GNUNET_YES == iterate_all) || (GNUNET_YES == monitor_connections) )
693 FPRINTF (stdout, _("Peer `%s': %s %s in state `%s' until %s\n"),
695 (NULL == transport) ? "<none>" : transport,
696 (NULL == transport) ? "<none>" : addr,
697 GNUNET_TRANSPORT_p2s (state),
698 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
702 /* Only connected peers, skip state */
703 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (id), transport, addr);
709 process_string (void *cls, const char *address)
711 struct ResolutionContext *rc = cls;
713 if (address != NULL )
715 print_info (&rc->id, rc->transport, address, rc->state, rc->state_timeout);
716 rc->printed = GNUNET_YES;
721 GNUNET_assert(address_resolutions > 0);
722 address_resolutions--;
723 if (GNUNET_NO == rc->printed)
725 if (numeric == GNUNET_NO)
727 /* Failed to resolve address, try numeric lookup */
728 resolve_address (&rc->id, rc->addrcp, GNUNET_YES,
729 rc->state, rc->state_timeout);
733 print_info (&rc->id, rc->transport, "<unable to resolve address>",
734 rc->state, rc->state_timeout);
737 GNUNET_free (rc->transport);
738 GNUNET_free (rc->addrcp);
739 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, rc);
741 if ((0 == address_resolutions) && (iterate_connections))
743 if (GNUNET_SCHEDULER_NO_TASK != end)
745 GNUNET_SCHEDULER_cancel (end);
746 end = GNUNET_SCHEDULER_NO_TASK;
748 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
750 GNUNET_SCHEDULER_cancel (op_timeout);
751 op_timeout = GNUNET_SCHEDULER_NO_TASK;
754 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
760 resolve_address (const struct GNUNET_PeerIdentity *id,
761 const struct GNUNET_HELLO_Address *address, int numeric,
762 enum GNUNET_TRANSPORT_PeerState state,
763 struct GNUNET_TIME_Absolute state_timeout)
765 struct ResolutionContext *rc;
767 rc = GNUNET_new (struct ResolutionContext);
768 GNUNET_assert(NULL != rc);
769 GNUNET_CONTAINER_DLL_insert(rc_head, rc_tail, rc);
770 address_resolutions++;
773 rc->transport = GNUNET_strdup(address->transport_name);
774 rc->addrcp = GNUNET_HELLO_address_copy (address);
775 rc->printed = GNUNET_NO;
777 rc->state_timeout = state_timeout;
778 /* Resolve address to string */
779 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
780 RESOLUTION_TIMEOUT, &process_string, rc);
784 * Function called with information about a peers during a one shot iteration
787 * @param peer identity of the peer, NULL for final callback when operation done
788 * @param address binary address used to communicate with this peer,
789 * NULL on disconnect or when done
790 * @param state current state this peer is in
791 * @param state_timeout time out for the current state
795 process_peer_iteration_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
796 const struct GNUNET_HELLO_Address *address,
797 enum GNUNET_TRANSPORT_PeerState state,
798 struct GNUNET_TIME_Absolute state_timeout)
803 address_resolution_in_progress = GNUNET_NO;
805 if (GNUNET_SCHEDULER_NO_TASK != end)
806 GNUNET_SCHEDULER_cancel (end);
807 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
811 if ((GNUNET_NO == iterate_all) && (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)) )
812 return; /* Display only connected peers */
814 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
815 GNUNET_SCHEDULER_cancel (op_timeout);
816 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
819 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received address for peer `%s': %s\n",
820 GNUNET_i2s (peer), address->transport_name);
823 resolve_address (peer, address, numeric, state, state_timeout);
825 print_info (peer, NULL, NULL, state, state_timeout);
830 * Function called with information about a peers
833 * @param peer identity of the peer, NULL for final callback when operation done
834 * @param address binary address used to communicate with this peer,
835 * NULL on disconnect or when done
836 * @param state current state this peer is in
837 * @param state_timeout time out for the current state
841 process_peer_monitoring_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
842 const struct GNUNET_HELLO_Address *address,
843 enum GNUNET_TRANSPORT_PeerState state,
844 struct GNUNET_TIME_Absolute state_timeout)
846 struct MonitoredPeer *m;
851 address_resolution_in_progress = GNUNET_NO;
853 if (GNUNET_SCHEDULER_NO_TASK != end)
854 GNUNET_SCHEDULER_cancel (end);
855 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
859 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
860 GNUNET_SCHEDULER_cancel (op_timeout);
861 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
864 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
866 m = GNUNET_new (struct MonitoredPeer);
867 GNUNET_CONTAINER_multipeermap_put (monitored_peers, peer,
868 m, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
872 if ( (m->state == state) &&
873 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
874 ((NULL == address) && (NULL == m->address)) )
876 return; /* No real change */
878 if ( ((NULL != address) && (NULL != m->address)) &&
879 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
880 return; /* No real change */
884 if (NULL != m->address)
886 GNUNET_free (m->address);
890 m->address = GNUNET_HELLO_address_copy (address);
892 m->state_timeout = state_timeout;
895 resolve_address (peer, m->address, numeric, m->state, m->state_timeout);
897 print_info (peer, NULL, NULL, m->state, m->state_timeout);
901 try_connect_cb (void *cls, const int result)
903 static int retries = 0;
904 if (GNUNET_OK == result)
911 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
915 FPRINTF (stderr, "%s",
916 _("Failed to send connect request to transport service\n") );
917 if (GNUNET_SCHEDULER_NO_TASK != end)
918 GNUNET_SCHEDULER_cancel (end);
920 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
926 * Function called with the result of the check if the 'transport'
927 * service is running.
929 * @param cls closure with our configuration
930 * @param result #GNUNET_YES if transport is running
933 testservice_task (void *cls, int result)
938 if (GNUNET_YES != result)
940 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
946 != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
949 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
953 counter = benchmark_send + benchmark_receive + iterate_connections
954 + monitor_connections + monitor_connects + try_connect;
959 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
960 "connect", "benchmark send", "benchmark receive", "information",
961 "monitor", "events");
967 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
968 "connect", "benchmark send", "benchmark receive", "information",
969 "monitor", "events");
973 if (try_connect) /* -C: Connect to peer */
977 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
982 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
983 ¬ify_connect, ¬ify_disconnect);
986 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
990 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
992 if (NULL == tc_handle)
994 FPRINTF (stderr, "%s",
995 _("Failed to send request to transport service\n") );
999 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1003 else if (benchmark_send) /* -s: Benchmark sending */
1007 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1012 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1013 ¬ify_connect, ¬ify_disconnect);
1016 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1020 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1022 if (NULL == tc_handle)
1024 FPRINTF (stderr, "%s",
1025 _("Failed to send request to transport service\n") );
1029 start_time = GNUNET_TIME_absolute_get ();
1030 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1033 else if (benchmark_receive) /* -b: Benchmark receiving */
1035 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive, NULL,
1039 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1044 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n") );
1045 start_time = GNUNET_TIME_absolute_get ();
1048 else if (iterate_connections) /* -i: List information about peers once */
1050 address_resolution_in_progress = GNUNET_YES;
1051 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1052 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1053 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1056 else if (monitor_connections) /* -m: List information about peers continuously */
1058 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1059 address_resolution_in_progress = GNUNET_YES;
1060 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1061 GNUNET_NO, TIMEOUT, &process_peer_monitoring_cb, (void *) cfg);
1063 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1065 monitor_connect_counter = 0;
1066 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1067 &monitor_notify_connect, &monitor_notify_disconnect);
1070 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1082 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1083 &shutdown_task, NULL );
1088 * Main function that will be run by the scheduler.
1090 * @param cls closure
1091 * @param args remaining command-line arguments
1092 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1093 * @param mycfg configuration
1096 run (void *cls, char * const *args, const char *cfgfile,
1097 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1099 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1100 if (test_configuration)
1102 do_test_configuration (cfg);
1105 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1106 &testservice_task, (void *) cfg);
1110 main (int argc, char * const *argv)
1113 static const struct GNUNET_GETOPT_CommandLineOption options[] =
1116 gettext_noop ("print information for all peers (instead of only connected peers )"),
1117 0, &GNUNET_GETOPT_set_one, &iterate_all },
1118 { 'b', "benchmark", NULL,
1119 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1120 0, &GNUNET_GETOPT_set_one, &benchmark_receive }, { 'C', "connect",
1121 NULL, gettext_noop ("connect to a peer"), 0,
1122 &GNUNET_GETOPT_set_one, &try_connect },
1123 { 'i', "information", NULL,
1124 gettext_noop ("provide information about all current connections (once)"),
1125 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1126 { 'm', "monitor", NULL,
1127 gettext_noop ("provide information about all current connections (continuously)"),
1128 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1129 { 'e', "events", NULL,
1130 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1131 0, &GNUNET_GETOPT_set_one, &monitor_connects }, { 'n', "numeric",
1132 NULL, gettext_noop ("do not resolve hostnames"), 0,
1133 &GNUNET_GETOPT_set_one, &numeric }, { 'p', "peer", "PEER",
1134 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1135 &cpid }, { 's', "send", NULL, gettext_noop
1136 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1137 &GNUNET_GETOPT_set_one, &benchmark_send },
1138 { 't', "test", NULL,
1139 gettext_noop ("test transport configuration (involves external server)"),
1140 0, &GNUNET_GETOPT_set_one, &test_configuration },
1141 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1142 GNUNET_GETOPT_OPTION_END };
1144 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1147 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport", gettext_noop
1148 ("Direct access to transport service."), options, &run, NULL );
1149 GNUNET_free((void * ) argv);
1150 if (GNUNET_OK == res)
1155 /* end of gnunet-transport.c */