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
944 check_dummy (void *cls,
945 const struct GNUNET_MessageHeader *message)
947 return GNUNET_OK; /* all messages are fine */
952 * Function called by the transport for each received message.
955 * @param message the message
958 handle_dummy (void *cls,
959 const struct GNUNET_MessageHeader *message)
961 if (! benchmark_receive)
965 _("Received %u bytes\n"),
966 (unsigned int) ntohs (message->size));
967 if (0 == traffic_received)
968 start_time = GNUNET_TIME_absolute_get ();
969 traffic_received += ntohs (message->size);
974 * Convert address to a printable format.
976 * @param address the address
977 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
978 * to try to use reverse DNS
979 * @param state state the peer is in
980 * @param state_timeout when will the peer's state expire
983 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
985 enum GNUNET_TRANSPORT_PeerState state,
986 struct GNUNET_TIME_Absolute state_timeout);
990 print_info (const struct GNUNET_PeerIdentity *id,
991 const char *transport,
993 enum GNUNET_TRANSPORT_PeerState state,
994 struct GNUNET_TIME_Absolute state_timeout)
997 if ( ((GNUNET_YES == iterate_connections) &&
998 (GNUNET_YES == iterate_all)) ||
999 (GNUNET_YES == monitor_connections))
1002 _("Peer `%s': %s %s in state `%s' until %s\n"),
1004 (NULL == transport) ? "<none>" : transport,
1005 (NULL == transport) ? "<none>" : addr,
1006 GNUNET_TRANSPORT_ps2s (state),
1007 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1009 else if ( (GNUNET_YES == iterate_connections) &&
1010 (GNUNET_TRANSPORT_is_connected(state)) )
1012 /* Only connected peers, skip state */
1014 _("Peer `%s': %s %s\n"),
1023 * Function called with a textual representation of an address. This
1024 * function will be called several times with different possible
1025 * textual representations, and a last time with @a address being NULL
1026 * to signal the end of the iteration. Note that @a address NULL
1027 * always is the last call, regardless of the value in @a res.
1029 * @param cls closure
1030 * @param address NULL on end of iteration,
1031 * otherwise 0-terminated printable UTF-8 string,
1032 * in particular an empty string if @a res is #GNUNET_NO
1033 * @param res result of the address to string conversion:
1034 * if #GNUNET_OK: conversion successful
1035 * if #GNUNET_NO: address was invalid (or not supported)
1036 * if #GNUNET_SYSERR: communication error (IPC error)
1039 process_peer_string (void *cls,
1040 const char *address,
1043 struct PeerResolutionContext *rc = cls;
1045 if (NULL != address)
1047 if (GNUNET_SYSERR == res)
1050 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
1051 GNUNET_i2s (&rc->addrcp->peer),
1052 rc->addrcp->transport_name,
1053 (unsigned int) rc->addrcp->address_length);
1054 print_info (&rc->addrcp->peer,
1059 rc->printed = GNUNET_YES;
1062 if (GNUNET_OK == res)
1064 print_info (&rc->addrcp->peer,
1069 rc->printed = GNUNET_YES;
1070 return; /* Wait for done call */
1072 /* GNUNET_NO == res: ignore, was simply not supported */
1075 /* NULL == address, last call, we are done */
1078 GNUNET_assert (address_resolutions > 0);
1079 address_resolutions--;
1080 if (GNUNET_NO == rc->printed)
1082 if (numeric == GNUNET_NO)
1084 /* Failed to resolve address, try numeric lookup
1085 (note: this should not be needed, as transport
1086 should fallback to numeric conversion if DNS takes
1088 resolve_peer_address (rc->addrcp,
1095 print_info (&rc->addrcp->peer,
1102 GNUNET_free (rc->transport);
1103 GNUNET_free (rc->addrcp);
1104 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1106 if ((0 == address_resolutions) && (iterate_connections))
1108 if (NULL != op_timeout)
1110 GNUNET_SCHEDULER_cancel (op_timeout);
1114 GNUNET_SCHEDULER_shutdown ();
1120 * Convert address to a printable format and print it
1121 * together with the given state data.
1123 * @param address the address
1124 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1125 * to try to use reverse DNS
1126 * @param state state the peer is in
1127 * @param state_timeout when will the peer's state expire
1130 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1132 enum GNUNET_TRANSPORT_PeerState state,
1133 struct GNUNET_TIME_Absolute state_timeout)
1135 struct PeerResolutionContext *rc;
1137 rc = GNUNET_new (struct PeerResolutionContext);
1138 GNUNET_CONTAINER_DLL_insert (rc_head,
1141 address_resolutions++;
1142 rc->transport = GNUNET_strdup (address->transport_name);
1143 rc->addrcp = GNUNET_HELLO_address_copy (address);
1144 rc->printed = GNUNET_NO;
1146 rc->state_timeout = state_timeout;
1147 /* Resolve address to string */
1148 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1152 &process_peer_string,
1158 * Function called with information about a peers during a one shot iteration
1160 * @param cls closure
1161 * @param peer identity of the peer, NULL for final callback when operation done
1162 * @param address binary address used to communicate with this peer,
1163 * NULL on disconnect or when done
1164 * @param state current state this peer is in
1165 * @param state_timeout time out for the current state
1168 process_peer_iteration_cb (void *cls,
1169 const struct GNUNET_PeerIdentity *peer,
1170 const struct GNUNET_HELLO_Address *address,
1171 enum GNUNET_TRANSPORT_PeerState state,
1172 struct GNUNET_TIME_Absolute state_timeout)
1181 if ( (GNUNET_NO == iterate_all) &&
1182 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1183 return; /* Display only connected peers */
1185 if (NULL != op_timeout)
1186 GNUNET_SCHEDULER_cancel (op_timeout);
1187 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1192 "Received address for peer `%s': %s\n",
1194 address ? address->transport_name : "");
1196 if (NULL != address)
1197 resolve_peer_address (address,
1211 * Context for address resolution by #plugin_monitoring_cb().
1213 struct PluginMonitorAddress
1217 * Ongoing resolution request.
1219 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
1222 * Resolved address as string.
1227 * Last event we got and did not yet print because
1228 * @e str was NULL (address not yet resolved).
1230 struct GNUNET_TRANSPORT_SessionInfo si;
1235 * Print information about a plugin monitoring event.
1237 * @param addr out internal context
1238 * @param info the monitoring information
1241 print_plugin_event_info (struct PluginMonitorAddress *addr,
1242 const struct GNUNET_TRANSPORT_SessionInfo *info)
1246 switch (info->state)
1248 case GNUNET_TRANSPORT_SS_INIT:
1251 case GNUNET_TRANSPORT_SS_HANDSHAKE:
1252 state = "HANDSHAKE";
1254 case GNUNET_TRANSPORT_SS_UP:
1257 case GNUNET_TRANSPORT_SS_UPDATE:
1260 case GNUNET_TRANSPORT_SS_DONE:
1268 "%s: state %s timeout in %s @ %s%s\n",
1269 GNUNET_i2s (&info->address->peer),
1271 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
1274 (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
1276 "%s: queue has %3u messages and %6u bytes\n",
1277 GNUNET_i2s (&info->address->peer),
1278 info->num_msg_pending,
1279 info->num_bytes_pending);
1280 if (0 != GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
1282 "%s: receiving blocked until %s\n",
1283 GNUNET_i2s (&info->address->peer),
1284 GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
1289 * Function called with a textual representation of an address. This
1290 * function will be called several times with different possible
1291 * textual representations, and a last time with @a address being NULL
1292 * to signal the end of the iteration. Note that @a address NULL
1293 * always is the last call, regardless of the value in @a res.
1295 * @param cls closure
1296 * @param address NULL on end of iteration,
1297 * otherwise 0-terminated printable UTF-8 string,
1298 * in particular an empty string if @a res is #GNUNET_NO
1299 * @param res result of the address to string conversion:
1300 * if #GNUNET_OK: conversion successful
1301 * if #GNUNET_NO: address was invalid (or not supported)
1302 * if #GNUNET_SYSERR: communication error (IPC error)
1305 address_cb (void *cls,
1306 const char *address,
1309 struct PluginMonitorAddress *addr = cls;
1311 if (NULL == address)
1316 if (NULL != addr->str)
1318 addr->str = GNUNET_strdup (address);
1319 print_plugin_event_info (addr,
1325 * Function called by the plugin with information about the
1326 * current sessions managed by the plugin (for monitoring).
1328 * @param cls closure (NULL)
1329 * @param session session handle this information is about,
1330 * NULL to indicate that we are "in sync" (initial
1331 * iteration complete)
1332 * @param session_ctx storage location where the application
1333 * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1334 * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1335 * @param info information about the state of the session,
1336 * NULL if @a session is also NULL and we are
1337 * merely signalling that the initial iteration is over;
1338 * NULL with @a session being non-NULL if the monitor
1339 * was being cancelled while sessions were active
1342 plugin_monitoring_cb (void *cls,
1343 struct GNUNET_TRANSPORT_PluginSession *session,
1345 const struct GNUNET_TRANSPORT_SessionInfo *info)
1347 struct PluginMonitorAddress *addr;
1349 if ( (NULL == info) &&
1351 return; /* in sync with transport service */
1352 addr = *session_ctx;
1357 if (NULL != addr->asc)
1359 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1362 GNUNET_free_non_null (addr->str);
1364 *session_ctx = NULL;
1366 return; /* shutdown */
1368 if ( (NULL != cpid) &&
1369 (0 != memcmp (&info->address->peer,
1371 sizeof (struct GNUNET_PeerIdentity))) )
1372 return; /* filtered */
1375 addr = GNUNET_new (struct PluginMonitorAddress);
1376 addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1379 GNUNET_TIME_UNIT_FOREVER_REL,
1382 *session_ctx = addr;
1384 if (NULL == addr->str)
1387 print_plugin_event_info (addr,
1389 if (GNUNET_TRANSPORT_SS_DONE == info->state)
1391 if (NULL != addr->asc)
1393 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1396 GNUNET_free_non_null (addr->str);
1398 *session_ctx = NULL;
1404 * Function called with information about a peers
1406 * @param cls closure, NULL
1407 * @param peer identity of the peer, NULL for final callback when operation done
1408 * @param address binary address used to communicate with this peer,
1409 * NULL on disconnect or when done
1410 * @param state current state this peer is in
1411 * @param state_timeout time out for the current state
1414 process_peer_monitoring_cb (void *cls,
1415 const struct GNUNET_PeerIdentity *peer,
1416 const struct GNUNET_HELLO_Address *address,
1417 enum GNUNET_TRANSPORT_PeerState state,
1418 struct GNUNET_TIME_Absolute state_timeout)
1420 struct MonitoredPeer *m;
1426 _("Monitor disconnected from transport service. Reconnecting.\n"));
1430 if (NULL != op_timeout)
1431 GNUNET_SCHEDULER_cancel (op_timeout);
1432 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1436 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1439 m = GNUNET_new (struct MonitoredPeer);
1440 GNUNET_CONTAINER_multipeermap_put (monitored_peers,
1443 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1447 if ( (m->state == state) &&
1448 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1449 (NULL == address) &&
1450 (NULL == m->address) )
1452 return; /* No real change */
1454 if ( (m->state == state) &&
1455 (NULL != address) &&
1456 (NULL != m->address) &&
1457 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1458 return; /* No real change */
1461 if (NULL != m->address)
1463 GNUNET_free (m->address);
1466 if (NULL != address)
1467 m->address = GNUNET_HELLO_address_copy (address);
1469 m->state_timeout = state_timeout;
1471 if (NULL != address)
1472 resolve_peer_address (m->address,
1486 * Function called with the transport service checking if we
1487 * want to blacklist a peer. Return #GNUNET_SYSERR for the
1488 * peer that we should disconnect from.
1491 * @param cpid peer to check blacklisting for
1492 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1495 blacklist_cb (void *cls,
1496 const struct GNUNET_PeerIdentity *cpid)
1498 if (0 == memcmp (cpid,
1500 sizeof (struct GNUNET_PeerIdentity)))
1501 return GNUNET_SYSERR;
1507 * Main function that will be run by the scheduler.
1509 * @param cls closure
1510 * @param args remaining command-line arguments
1511 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1512 * @param mycfg configuration
1517 const char *cfgfile,
1518 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1520 GNUNET_MQ_hd_var_size (dummy,
1521 GNUNET_MESSAGE_TYPE_DUMMY,
1522 struct GNUNET_MessageHeader);
1526 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1527 if (test_configuration)
1529 do_test_configuration (cfg);
1532 if ( (NULL != cpid) &&
1534 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
1539 _("Failed to parse peer identity `%s'\n"),
1544 counter = benchmark_send + benchmark_receive + iterate_connections
1545 + monitor_connections + monitor_connects + do_disconnect +
1551 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1554 "benchmark receive",
1564 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1567 "benchmark receive",
1575 if (do_disconnect) /* -D: Disconnect from peer */
1580 _("Option `%s' makes no sense without option `%s'.\n"),
1585 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1588 if (NULL == blacklist)
1592 _("Failed to connect to transport service for disconnection\n"));
1598 _("Blacklisting request in place, stop with CTRL-C\n"));
1600 else if (benchmark_send) /* -s: Benchmark sending */
1605 _("Option `%s' makes no sense without option `%s'.\n"),
1610 handle = GNUNET_TRANSPORT_core_connect (cfg,
1621 _("Failed to connect to transport service\n"));
1625 start_time = GNUNET_TIME_absolute_get ();
1626 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1630 else if (benchmark_receive) /* -b: Benchmark receiving */
1632 struct GNUNET_MQ_MessageHandler handlers[] = {
1633 make_dummy_handler (NULL),
1634 GNUNET_MQ_handler_end ()
1637 handle = GNUNET_TRANSPORT_core_connect (cfg,
1648 _("Failed to connect to transport service\n"));
1655 _("Starting to receive benchmark data\n"));
1656 start_time = GNUNET_TIME_absolute_get ();
1659 else if (iterate_connections) /* -i: List information about peers once */
1661 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1662 (NULL == cpid) ? NULL : &pid,
1664 &process_peer_iteration_cb,
1666 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1670 else if (monitor_connections) /* -m: List information about peers continuously */
1672 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
1674 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1675 (NULL == cpid) ? NULL : &pid,
1677 &process_peer_monitoring_cb,
1680 else if (monitor_plugins) /* -P: List information about plugins continuously */
1682 monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1683 pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
1684 &plugin_monitoring_cb,
1687 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1689 monitor_connect_counter = 0;
1690 handle = GNUNET_TRANSPORT_core_connect (cfg,
1694 &monitor_notify_connect,
1695 &monitor_notify_disconnect,
1701 _("Failed to connect to transport service\n"));
1713 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1723 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1725 gettext_noop ("print information for all peers (instead of only connected peers)"),
1726 0, &GNUNET_GETOPT_set_one, &iterate_all },
1727 { 'b', "benchmark", NULL,
1728 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1729 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1730 { 'D', "disconnect",
1731 NULL, gettext_noop ("disconnect from a peer"), 0,
1732 &GNUNET_GETOPT_set_one, &do_disconnect },
1733 { 'i', "information", NULL,
1734 gettext_noop ("provide information about all current connections (once)"),
1735 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1736 { 'm', "monitor", NULL,
1737 gettext_noop ("provide information about all current connections (continuously)"),
1738 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1739 { 'e', "events", NULL,
1740 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1741 0, &GNUNET_GETOPT_set_one, &monitor_connects },
1743 NULL, gettext_noop ("do not resolve hostnames"), 0,
1744 &GNUNET_GETOPT_set_one, &numeric },
1745 { 'p', "peer", "PEER",
1746 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1748 { 'P', "plugins", NULL,
1749 gettext_noop ("monitor plugin sessions"), 0, &GNUNET_GETOPT_set_one,
1751 { 's', "send", NULL, gettext_noop
1752 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1753 &GNUNET_GETOPT_set_one, &benchmark_send },
1754 { 't', "test", NULL,
1755 gettext_noop ("test transport configuration (involves external server)"),
1756 0, &GNUNET_GETOPT_set_one, &test_configuration },
1757 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1758 GNUNET_GETOPT_OPTION_END
1761 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1764 res = GNUNET_PROGRAM_run (argc, argv,
1766 gettext_noop ("Direct access to transport service."),
1769 GNUNET_free((void *) argv);
1770 if (GNUNET_OK == res)
1775 /* end of gnunet-transport.c */