2 This file is part of GNUnet.
3 Copyright (C) 2011-2014, 2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_transport_core_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, 20)
45 * Timeout for a name resolution
47 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
50 * Timeout for an operation
52 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
56 * Context to store name resolutions for valiation
58 struct ValidationResolutionContext
63 struct ValidationResolutionContext *next;
68 struct ValidationResolutionContext *prev;
73 struct GNUNET_HELLO_Address *addrcp;
76 * Time of last validation
78 struct GNUNET_TIME_Absolute last_validation;
81 * Address is valid until
83 struct GNUNET_TIME_Absolute valid_until;
86 * Time of next validation
88 struct GNUNET_TIME_Absolute next_validation;
91 * Tranport conversion handle
93 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
101 * was the entry printed
107 * Struct to store information about peers in monitor mode
114 enum GNUNET_TRANSPORT_PeerState state;
119 struct GNUNET_TIME_Absolute state_timeout;
122 * The address to convert
124 struct GNUNET_HELLO_Address *address;
128 * Context to store name resolutions for valiation
130 struct PeerResolutionContext
135 struct PeerResolutionContext *next;
140 struct PeerResolutionContext *prev;
145 struct GNUNET_HELLO_Address *addrcp;
148 * transport conversiion context
150 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
155 enum GNUNET_TRANSPORT_PeerState state;
160 struct GNUNET_TIME_Absolute state_timeout;
168 * was the entry printed
175 * Context for a plugin test.
182 struct TestContext *prev;
187 struct TestContext *next;
190 * Handle to the active NAT test.
192 struct GNUNET_NAT_Test *tst;
195 * Task identifier for the timeout.
197 struct GNUNET_SCHEDULER_Task * tsk;
200 * Name of plugin under test.
207 unsigned long long bnd_port;
212 unsigned long long adv_port;
223 * NAT returned success
225 NAT_TEST_SUCCESS = GNUNET_OK,
228 * NAT returned failure
230 NAT_TEST_FAIL = GNUNET_NO,
233 * NAT returned failure while running test
235 NAT_TEST_INTERNAL_FAIL = GNUNET_SYSERR,
238 * We could not start the test
240 NAT_TEST_FAILED_TO_START = 2,
243 * We had a timeout while running the test
250 * Benchmarking block size in KB
255 * Which peer should we connect to?
260 * Handle to transport service.
262 static struct GNUNET_TRANSPORT_CoreHandle *handle;
265 * Configuration handle
267 static struct GNUNET_CONFIGURATION_Handle *cfg;
270 * Blacklisting handle
272 struct GNUNET_TRANSPORT_Blacklist *blacklist;
277 static int benchmark_send;
282 static int benchmark_receive;
287 static int benchmark_receive;
292 static int iterate_connections;
297 static int iterate_all;
302 static int test_configuration;
307 static int monitor_connects;
312 static int monitor_connections;
317 static int monitor_plugins;
322 static int do_disconnect;
330 * Global return value (0 success).
335 * Current number of connections in monitor mode
337 static int monitor_connect_counter;
340 * Number of bytes of traffic we received so far.
342 static unsigned long long traffic_received;
345 * Number of bytes of traffic we sent so far.
347 static unsigned long long traffic_sent;
350 * Starting time of transmitting/receiving data.
352 static struct GNUNET_TIME_Absolute start_time;
355 * Map storing information about monitored peers
357 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
360 * Map storing information about monitored plugins's sessions.
362 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_plugins;
365 * Handle if we are monitoring peers at the transport level.
367 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
370 * Handle if we are monitoring plugin session activity.
372 static struct GNUNET_TRANSPORT_PluginMonitor *pm;
375 * Identity of the peer we transmit to / connect to.
376 * (equivalent to 'cpid' string).
378 static struct GNUNET_PeerIdentity pid;
381 * Task for operation timeout
383 static struct GNUNET_SCHEDULER_Task *op_timeout;
386 * Selected level of verbosity.
388 static int verbosity;
391 * Resolver process handle.
393 struct GNUNET_OS_Process *resolver;
396 * Number of address resolutions pending
398 static unsigned int address_resolutions;
401 * DLL for NAT Test Contexts: head
403 static struct TestContext *head;
406 * DLL for NAT Test Contexts: tail
408 static struct TestContext *tail;
411 * DLL: head of validation resolution entries
413 static struct ValidationResolutionContext *vc_head;
416 * DLL: tail of validation resolution entries
418 static struct ValidationResolutionContext *vc_tail;
421 * DLL: head of resolution entries
423 static struct PeerResolutionContext *rc_head;
426 * DLL: head of resolution entries
428 static struct PeerResolutionContext *rc_tail;
432 * Function called to release data stored in the #monitored_peers map.
435 * @param key the peer identity
436 * @param value a `struct MonitoredPeer` to release
437 * @return #GNUNET_OK (continue to iterate)
440 destroy_it (void *cls,
441 const struct GNUNET_PeerIdentity *key,
444 struct MonitoredPeer *m = value;
446 GNUNET_assert (GNUNET_OK ==
447 GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
450 GNUNET_free_non_null (m->address);
457 * Task run in monitor mode when the user presses CTRL-C to abort.
458 * Stops monitoring activity.
463 shutdown_task (void *cls)
465 struct GNUNET_TIME_Relative duration;
466 struct ValidationResolutionContext *cur;
467 struct ValidationResolutionContext *next;
468 struct PeerResolutionContext *rc;
470 if (NULL != op_timeout)
472 GNUNET_SCHEDULER_cancel (op_timeout);
477 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
482 GNUNET_TRANSPORT_monitor_plugins_cancel (pm);
487 for (cur = next; NULL != cur; cur = next)
491 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
492 GNUNET_CONTAINER_DLL_remove (vc_head,
495 GNUNET_free (cur->transport);
496 GNUNET_HELLO_address_free (cur->addrcp);
499 while (NULL != (rc = rc_head))
501 GNUNET_CONTAINER_DLL_remove (rc_head,
504 GNUNET_TRANSPORT_address_to_string_cancel (rc->asc);
505 GNUNET_free (rc->transport);
506 GNUNET_free (rc->addrcp);
511 GNUNET_TRANSPORT_core_disconnect (handle);
516 duration = GNUNET_TIME_absolute_get_duration (start_time);
518 _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
519 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
521 GNUNET_STRINGS_relative_time_to_string (duration,
524 if (benchmark_receive)
526 duration = GNUNET_TIME_absolute_get_duration (start_time);
528 _("Received %llu bytes/s (%llu bytes in %s)\n"),
529 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
531 GNUNET_STRINGS_relative_time_to_string (duration,
535 if (NULL != monitored_peers)
537 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers,
540 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
541 monitored_peers = NULL;
543 if (NULL != monitored_plugins)
546 GNUNET_CONTAINER_multipeermap_size (monitored_plugins));
547 GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
548 monitored_plugins = NULL;
550 if (NULL != blacklist)
552 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
560 * We are done, shut down.
563 operation_timeout (void *cls)
565 struct PeerResolutionContext *cur;
566 struct PeerResolutionContext *next;
569 if ((benchmark_send) || (benchmark_receive))
572 _("Failed to connect to `%s'\n"),
573 GNUNET_i2s_full (&pid));
574 GNUNET_SCHEDULER_shutdown ();
578 if (iterate_connections)
581 while (NULL != (cur = next))
585 _("Failed to resolve address for peer `%s'\n"),
586 GNUNET_i2s (&cur->addrcp->peer));
588 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
589 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
590 GNUNET_free(cur->transport);
591 GNUNET_free(cur->addrcp);
597 _("Failed to list connections, timeout occured\n"));
598 GNUNET_SCHEDULER_shutdown ();
610 * Display the result of the test.
612 * @param tc test context
613 * @param result #GNUNET_YES on success
616 display_test_result (struct TestContext *tc,
617 enum TestResult result)
622 _("Configuration for plugin `%s' did not work!\n"),
625 case NAT_TEST_SUCCESS:
627 _("Configuration for plugin `%s' did work!\n"),
630 case NAT_TEST_INTERNAL_FAIL:
632 _("Internal NAT error while running test for plugin `%s'\n"),
635 case NAT_TEST_FAILED_TO_START:
637 _("Failed to start NAT test for plugin `%s'\n"),
640 case NAT_TEST_TIMEOUT:
642 _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
647 _("Configuration for plugin `%s' did not work!\n"),
653 GNUNET_SCHEDULER_cancel (tc->tsk);
658 GNUNET_NAT_test_stop (tc->tst);
662 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
663 GNUNET_free (tc->name);
666 if ((NULL == head) && (NULL != resolver))
668 GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
670 GNUNET_OS_process_destroy (resolver);
679 * Function called by NAT to report the outcome of the nat-test.
680 * Clean up and update GUI.
682 * @param cls test context
683 * @param result status code
686 result_callback (void *cls,
687 enum GNUNET_NAT_StatusCode result)
689 struct TestContext *tc = cls;
691 display_test_result (tc,
699 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
700 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
702 (uint16_t) head->bnd_port,
703 (uint16_t) head->adv_port);
705 head->tst = GNUNET_NAT_test_start (cfg,
706 (0 == strcasecmp (head->name, "udp"))
707 ? GNUNET_NO : GNUNET_YES,
708 (uint16_t) head->bnd_port,
709 (uint16_t) head->adv_port,
711 &result_callback, head);
716 * Test our plugin's configuration (NAT traversal, etc.).
718 * @param cfg configuration to test
721 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
725 unsigned long long bnd_port;
726 unsigned long long adv_port;
727 struct TestContext *tc;
731 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
734 FPRINTF (stderr, "%s", _
735 ("No transport plugins configured, peer will never communicate\n"));
740 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
742 char section[12 + strlen (tok)];
743 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
745 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
749 _("No port configured for plugin `%s', cannot test it\n"), tok);
752 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
753 "ADVERTISED_PORT", &adv_port))
756 tc = GNUNET_new (struct TestContext);
757 tc->name = GNUNET_strdup (tok);
758 tc->adv_port = adv_port;
759 tc->bnd_port = bnd_port;
760 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
762 GNUNET_free(plugins);
764 if ((NULL != head) && (NULL == resolver))
766 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
767 resolver = GNUNET_OS_start_process (GNUNET_YES,
768 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
771 "gnunet-service-resolver", NULL);
772 if (NULL == resolver)
774 FPRINTF (stderr, _("Failed to start resolver!\n"));
779 GNUNET_RESOLVER_connect (cfg);
786 * Function called to notify a client about the socket
787 * begin ready to queue more data. Sends another message.
789 * @param cls closure with the message queue
794 struct GNUNET_MQ_Handle *mq = cls;
795 struct GNUNET_MessageHeader *m;
796 struct GNUNET_MQ_Envelope *env;
798 env = GNUNET_MQ_msg_extra (m,
800 GNUNET_MESSAGE_TYPE_DUMMY);
803 BLOCKSIZE * 1024 - sizeof(struct GNUNET_MessageHeader));
804 traffic_sent += BLOCKSIZE * 1024;
805 GNUNET_MQ_notify_sent (env,
810 _("Transmitting %u bytes\n"),
811 (unsigned int) BLOCKSIZE * 1024);
818 * Function called to notify transport users that another
819 * peer connected to us.
822 * @param peer the peer that connected
823 * @param mq message queue for sending to @a peer
826 notify_connect (void *cls,
827 const struct GNUNET_PeerIdentity *peer,
828 struct GNUNET_MQ_Handle *mq)
830 if (0 != memcmp (&pid,
832 sizeof(struct GNUNET_PeerIdentity)))
835 if (! benchmark_send)
837 if (NULL != op_timeout)
839 GNUNET_SCHEDULER_cancel (op_timeout);
844 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
847 start_time = GNUNET_TIME_absolute_get ();
854 * Function called to notify transport users that another
855 * peer disconnected from us.
858 * @param peer the peer that disconnected
859 * @param internal_cls what we returned from #notify_connect()
862 notify_disconnect (void *cls,
863 const struct GNUNET_PeerIdentity *peer,
866 if (0 != memcmp (&pid,
868 sizeof(struct GNUNET_PeerIdentity)))
870 if (NULL == internal_cls)
871 return; /* not about target peer */
872 if (! benchmark_send)
873 return; /* not transmitting */
875 _("Disconnected from peer `%s' while benchmarking\n"),
881 * Function called to notify transport users that another
882 * peer connected to us.
885 * @param peer the peer that connected
886 * @param mq for sending messages to @a peer
890 monitor_notify_connect (void *cls,
891 const struct GNUNET_PeerIdentity *peer,
892 struct GNUNET_MQ_Handle *mq)
894 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
895 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
897 monitor_connect_counter++;
899 _("%24s: %-17s %4s (%u connections in total)\n"),
903 monitor_connect_counter);
909 * Function called to notify transport users that another
910 * peer disconnected from us.
913 * @param peer the peer that disconnected
914 * @param internal_cls what we returned from #monitor_notify_connect()
917 monitor_notify_disconnect (void *cls,
918 const struct GNUNET_PeerIdentity *peer,
921 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
922 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
924 GNUNET_assert(monitor_connect_counter > 0);
925 monitor_connect_counter--;
928 _("%24s: %-17s %4s (%u connections in total)\n"),
930 _("Disconnected from"),
932 monitor_connect_counter);
937 * Function called by the transport for each received message.
940 * @param message the message
943 check_dummy (void *cls,
944 const struct GNUNET_MessageHeader *message)
946 return GNUNET_OK; /* all messages are fine */
951 * Function called by the transport for each received message.
954 * @param message the message
957 handle_dummy (void *cls,
958 const struct GNUNET_MessageHeader *message)
960 if (! benchmark_receive)
964 _("Received %u bytes\n"),
965 (unsigned int) ntohs (message->size));
966 if (0 == traffic_received)
967 start_time = GNUNET_TIME_absolute_get ();
968 traffic_received += ntohs (message->size);
973 * Convert address to a printable format.
975 * @param address the address
976 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
977 * to try to use reverse DNS
978 * @param state state the peer is in
979 * @param state_timeout when will the peer's state expire
982 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
984 enum GNUNET_TRANSPORT_PeerState state,
985 struct GNUNET_TIME_Absolute state_timeout);
989 print_info (const struct GNUNET_PeerIdentity *id,
990 const char *transport,
992 enum GNUNET_TRANSPORT_PeerState state,
993 struct GNUNET_TIME_Absolute state_timeout)
996 if ( ((GNUNET_YES == iterate_connections) &&
997 (GNUNET_YES == iterate_all)) ||
998 (GNUNET_YES == monitor_connections))
1001 _("Peer `%s': %s %s in state `%s' until %s\n"),
1003 (NULL == transport) ? "<none>" : transport,
1004 (NULL == transport) ? "<none>" : addr,
1005 GNUNET_TRANSPORT_ps2s (state),
1006 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1008 else if ( (GNUNET_YES == iterate_connections) &&
1009 (GNUNET_TRANSPORT_is_connected(state)) )
1011 /* Only connected peers, skip state */
1013 _("Peer `%s': %s %s\n"),
1022 * Function called with a textual representation of an address. This
1023 * function will be called several times with different possible
1024 * textual representations, and a last time with @a address being NULL
1025 * to signal the end of the iteration. Note that @a address NULL
1026 * always is the last call, regardless of the value in @a res.
1028 * @param cls closure
1029 * @param address NULL on end of iteration,
1030 * otherwise 0-terminated printable UTF-8 string,
1031 * in particular an empty string if @a res is #GNUNET_NO
1032 * @param res result of the address to string conversion:
1033 * if #GNUNET_OK: conversion successful
1034 * if #GNUNET_NO: address was invalid (or not supported)
1035 * if #GNUNET_SYSERR: communication error (IPC error)
1038 process_peer_string (void *cls,
1039 const char *address,
1042 struct PeerResolutionContext *rc = cls;
1044 if (NULL != address)
1046 if (GNUNET_SYSERR == res)
1049 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
1050 GNUNET_i2s (&rc->addrcp->peer),
1051 rc->addrcp->transport_name,
1052 (unsigned int) rc->addrcp->address_length);
1053 print_info (&rc->addrcp->peer,
1058 rc->printed = GNUNET_YES;
1061 if (GNUNET_OK == res)
1063 print_info (&rc->addrcp->peer,
1068 rc->printed = GNUNET_YES;
1069 return; /* Wait for done call */
1071 /* GNUNET_NO == res: ignore, was simply not supported */
1074 /* NULL == address, last call, we are done */
1077 GNUNET_assert (address_resolutions > 0);
1078 address_resolutions--;
1079 if (GNUNET_NO == rc->printed)
1081 if (numeric == GNUNET_NO)
1083 /* Failed to resolve address, try numeric lookup
1084 (note: this should not be needed, as transport
1085 should fallback to numeric conversion if DNS takes
1087 resolve_peer_address (rc->addrcp,
1094 print_info (&rc->addrcp->peer,
1101 GNUNET_free (rc->transport);
1102 GNUNET_free (rc->addrcp);
1103 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1105 if ((0 == address_resolutions) && (iterate_connections))
1107 if (NULL != op_timeout)
1109 GNUNET_SCHEDULER_cancel (op_timeout);
1113 GNUNET_SCHEDULER_shutdown ();
1119 * Convert address to a printable format and print it
1120 * together with the given state data.
1122 * @param address the address
1123 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1124 * to try to use reverse DNS
1125 * @param state state the peer is in
1126 * @param state_timeout when will the peer's state expire
1129 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1131 enum GNUNET_TRANSPORT_PeerState state,
1132 struct GNUNET_TIME_Absolute state_timeout)
1134 struct PeerResolutionContext *rc;
1136 rc = GNUNET_new (struct PeerResolutionContext);
1137 GNUNET_CONTAINER_DLL_insert (rc_head,
1140 address_resolutions++;
1141 rc->transport = GNUNET_strdup (address->transport_name);
1142 rc->addrcp = GNUNET_HELLO_address_copy (address);
1143 rc->printed = GNUNET_NO;
1145 rc->state_timeout = state_timeout;
1146 /* Resolve address to string */
1147 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1151 &process_peer_string,
1157 * Function called with information about a peers during a one shot iteration
1159 * @param cls closure
1160 * @param peer identity of the peer, NULL for final callback when operation done
1161 * @param address binary address used to communicate with this peer,
1162 * NULL on disconnect or when done
1163 * @param state current state this peer is in
1164 * @param state_timeout time out for the current state
1167 process_peer_iteration_cb (void *cls,
1168 const struct GNUNET_PeerIdentity *peer,
1169 const struct GNUNET_HELLO_Address *address,
1170 enum GNUNET_TRANSPORT_PeerState state,
1171 struct GNUNET_TIME_Absolute state_timeout)
1180 if ( (GNUNET_NO == iterate_all) &&
1181 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1182 return; /* Display only connected peers */
1184 if (NULL != op_timeout)
1185 GNUNET_SCHEDULER_cancel (op_timeout);
1186 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1190 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1191 "Received address for peer `%s': %s\n",
1193 address ? address->transport_name : "");
1195 if (NULL != address)
1196 resolve_peer_address (address,
1210 * Context for address resolution by #plugin_monitoring_cb().
1212 struct PluginMonitorAddress
1216 * Ongoing resolution request.
1218 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
1221 * Resolved address as string.
1226 * Last event we got and did not yet print because
1227 * @e str was NULL (address not yet resolved).
1229 struct GNUNET_TRANSPORT_SessionInfo si;
1234 * Print information about a plugin monitoring event.
1236 * @param addr out internal context
1237 * @param info the monitoring information
1240 print_plugin_event_info (struct PluginMonitorAddress *addr,
1241 const struct GNUNET_TRANSPORT_SessionInfo *info)
1245 switch (info->state)
1247 case GNUNET_TRANSPORT_SS_INIT:
1250 case GNUNET_TRANSPORT_SS_HANDSHAKE:
1251 state = "HANDSHAKE";
1253 case GNUNET_TRANSPORT_SS_UP:
1256 case GNUNET_TRANSPORT_SS_UPDATE:
1259 case GNUNET_TRANSPORT_SS_DONE:
1267 "%s: state %s timeout in %s @ %s%s\n",
1268 GNUNET_i2s (&info->address->peer),
1270 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
1273 (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
1275 "%s: queue has %3u messages and %6u bytes\n",
1276 GNUNET_i2s (&info->address->peer),
1277 info->num_msg_pending,
1278 info->num_bytes_pending);
1279 if (0 != GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
1281 "%s: receiving blocked until %s\n",
1282 GNUNET_i2s (&info->address->peer),
1283 GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
1288 * Function called with a textual representation of an address. This
1289 * function will be called several times with different possible
1290 * textual representations, and a last time with @a address being NULL
1291 * to signal the end of the iteration. Note that @a address NULL
1292 * always is the last call, regardless of the value in @a res.
1294 * @param cls closure
1295 * @param address NULL on end of iteration,
1296 * otherwise 0-terminated printable UTF-8 string,
1297 * in particular an empty string if @a res is #GNUNET_NO
1298 * @param res result of the address to string conversion:
1299 * if #GNUNET_OK: conversion successful
1300 * if #GNUNET_NO: address was invalid (or not supported)
1301 * if #GNUNET_SYSERR: communication error (IPC error)
1304 address_cb (void *cls,
1305 const char *address,
1308 struct PluginMonitorAddress *addr = cls;
1310 if (NULL == address)
1315 if (NULL != addr->str)
1317 addr->str = GNUNET_strdup (address);
1318 print_plugin_event_info (addr,
1324 * Function called by the plugin with information about the
1325 * current sessions managed by the plugin (for monitoring).
1327 * @param cls closure (NULL)
1328 * @param session session handle this information is about,
1329 * NULL to indicate that we are "in sync" (initial
1330 * iteration complete)
1331 * @param session_ctx storage location where the application
1332 * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1333 * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1334 * @param info information about the state of the session,
1335 * NULL if @a session is also NULL and we are
1336 * merely signalling that the initial iteration is over;
1337 * NULL with @a session being non-NULL if the monitor
1338 * was being cancelled while sessions were active
1341 plugin_monitoring_cb (void *cls,
1342 struct GNUNET_TRANSPORT_PluginSession *session,
1344 const struct GNUNET_TRANSPORT_SessionInfo *info)
1346 struct PluginMonitorAddress *addr;
1348 if ( (NULL == info) &&
1350 return; /* in sync with transport service */
1351 addr = *session_ctx;
1356 if (NULL != addr->asc)
1358 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1361 GNUNET_free_non_null (addr->str);
1363 *session_ctx = NULL;
1365 return; /* shutdown */
1367 if ( (NULL != cpid) &&
1368 (0 != memcmp (&info->address->peer,
1370 sizeof (struct GNUNET_PeerIdentity))) )
1371 return; /* filtered */
1374 addr = GNUNET_new (struct PluginMonitorAddress);
1375 addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1378 GNUNET_TIME_UNIT_FOREVER_REL,
1381 *session_ctx = addr;
1383 if (NULL == addr->str)
1386 print_plugin_event_info (addr,
1388 if (GNUNET_TRANSPORT_SS_DONE == info->state)
1390 if (NULL != addr->asc)
1392 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1395 GNUNET_free_non_null (addr->str);
1397 *session_ctx = NULL;
1403 * Function called with information about a peers
1405 * @param cls closure, NULL
1406 * @param peer identity of the peer, NULL for final callback when operation done
1407 * @param address binary address used to communicate with this peer,
1408 * NULL on disconnect or when done
1409 * @param state current state this peer is in
1410 * @param state_timeout time out for the current state
1413 process_peer_monitoring_cb (void *cls,
1414 const struct GNUNET_PeerIdentity *peer,
1415 const struct GNUNET_HELLO_Address *address,
1416 enum GNUNET_TRANSPORT_PeerState state,
1417 struct GNUNET_TIME_Absolute state_timeout)
1419 struct MonitoredPeer *m;
1425 _("Monitor disconnected from transport service. Reconnecting.\n"));
1429 if (NULL != op_timeout)
1430 GNUNET_SCHEDULER_cancel (op_timeout);
1431 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1435 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1438 m = GNUNET_new (struct MonitoredPeer);
1439 GNUNET_CONTAINER_multipeermap_put (monitored_peers,
1442 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1446 if ( (m->state == state) &&
1447 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1448 (NULL == address) &&
1449 (NULL == m->address) )
1451 return; /* No real change */
1453 if ( (m->state == state) &&
1454 (NULL != address) &&
1455 (NULL != m->address) &&
1456 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1457 return; /* No real change */
1460 if (NULL != m->address)
1462 GNUNET_free (m->address);
1465 if (NULL != address)
1466 m->address = GNUNET_HELLO_address_copy (address);
1468 m->state_timeout = state_timeout;
1470 if (NULL != address)
1471 resolve_peer_address (m->address,
1485 * Function called with the transport service checking if we
1486 * want to blacklist a peer. Return #GNUNET_SYSERR for the
1487 * peer that we should disconnect from.
1490 * @param cpid peer to check blacklisting for
1491 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1494 blacklist_cb (void *cls,
1495 const struct GNUNET_PeerIdentity *cpid)
1497 if (0 == memcmp (cpid,
1499 sizeof (struct GNUNET_PeerIdentity)))
1500 return GNUNET_SYSERR;
1506 * Main function that will be run by the scheduler.
1508 * @param cls closure
1509 * @param args remaining command-line arguments
1510 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1511 * @param mycfg configuration
1516 const char *cfgfile,
1517 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1519 GNUNET_MQ_hd_var_size (dummy,
1520 GNUNET_MESSAGE_TYPE_DUMMY,
1521 struct GNUNET_MessageHeader);
1525 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1526 if (test_configuration)
1528 do_test_configuration (cfg);
1531 if ( (NULL != cpid) &&
1533 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
1538 _("Failed to parse peer identity `%s'\n"),
1543 counter = benchmark_send + benchmark_receive + iterate_connections
1544 + monitor_connections + monitor_connects + do_disconnect +
1550 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1553 "benchmark receive",
1563 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1566 "benchmark receive",
1574 if (do_disconnect) /* -D: Disconnect from peer */
1579 _("Option `%s' makes no sense without option `%s'.\n"),
1584 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1587 if (NULL == blacklist)
1591 _("Failed to connect to transport service for disconnection\n"));
1597 _("Blacklisting request in place, stop with CTRL-C\n"));
1599 else if (benchmark_send) /* -s: Benchmark sending */
1604 _("Option `%s' makes no sense without option `%s'.\n"),
1609 handle = GNUNET_TRANSPORT_core_connect (cfg,
1620 _("Failed to connect to transport service\n"));
1624 start_time = GNUNET_TIME_absolute_get ();
1625 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1629 else if (benchmark_receive) /* -b: Benchmark receiving */
1631 struct GNUNET_MQ_MessageHandler handlers[] = {
1632 make_dummy_handler (NULL),
1633 GNUNET_MQ_handler_end ()
1636 handle = GNUNET_TRANSPORT_core_connect (cfg,
1647 _("Failed to connect to transport service\n"));
1654 _("Starting to receive benchmark data\n"));
1655 start_time = GNUNET_TIME_absolute_get ();
1658 else if (iterate_connections) /* -i: List information about peers once */
1660 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1661 (NULL == cpid) ? NULL : &pid,
1663 &process_peer_iteration_cb,
1665 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1669 else if (monitor_connections) /* -m: List information about peers continuously */
1671 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
1673 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1674 (NULL == cpid) ? NULL : &pid,
1676 &process_peer_monitoring_cb,
1679 else if (monitor_plugins) /* -P: List information about plugins continuously */
1681 monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1682 pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
1683 &plugin_monitoring_cb,
1686 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1688 monitor_connect_counter = 0;
1689 handle = GNUNET_TRANSPORT_core_connect (cfg,
1693 &monitor_notify_connect,
1694 &monitor_notify_disconnect,
1700 _("Failed to connect to transport service\n"));
1712 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1722 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1724 gettext_noop ("print information for all peers (instead of only connected peers)"),
1725 0, &GNUNET_GETOPT_set_one, &iterate_all },
1726 { 'b', "benchmark", NULL,
1727 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1728 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1729 { 'D', "disconnect",
1730 NULL, gettext_noop ("disconnect from a peer"), 0,
1731 &GNUNET_GETOPT_set_one, &do_disconnect },
1732 { 'i', "information", NULL,
1733 gettext_noop ("provide information about all current connections (once)"),
1734 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1735 { 'm', "monitor", NULL,
1736 gettext_noop ("provide information about all current connections (continuously)"),
1737 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1738 { 'e', "events", NULL,
1739 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1740 0, &GNUNET_GETOPT_set_one, &monitor_connects },
1742 NULL, gettext_noop ("do not resolve hostnames"), 0,
1743 &GNUNET_GETOPT_set_one, &numeric },
1744 { 'p', "peer", "PEER",
1745 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1747 { 'P', "plugins", NULL,
1748 gettext_noop ("monitor plugin sessions"), 0, &GNUNET_GETOPT_set_one,
1750 { 's', "send", NULL, gettext_noop
1751 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1752 &GNUNET_GETOPT_set_one, &benchmark_send },
1753 { 't', "test", NULL,
1754 gettext_noop ("test transport configuration (involves external server)"),
1755 0, &GNUNET_GETOPT_set_one, &test_configuration },
1756 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1757 GNUNET_GETOPT_OPTION_END
1760 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1763 res = GNUNET_PROGRAM_run (argc, argv,
1765 gettext_noop ("Direct access to transport service."),
1768 GNUNET_free((void *) argv);
1769 if (GNUNET_OK == res)
1774 /* end of gnunet-transport.c */