2 This file is part of GNUnet.
3 Copyright (C) 2011-2014 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_nat_lib.h"
38 * How long do we wait for the NAT test to report success?
39 * Should match NAT_SERVER_TIMEOUT in 'nat_test.c'.
41 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
44 * Timeout for a name resolution
46 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
49 * Timeout for an operation
51 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
55 * Context to store name resolutions for valiation
57 struct ValidationResolutionContext
62 struct ValidationResolutionContext *next;
67 struct ValidationResolutionContext *prev;
72 struct GNUNET_HELLO_Address *addrcp;
75 * Time of last validation
77 struct GNUNET_TIME_Absolute last_validation;
80 * Address is valid until
82 struct GNUNET_TIME_Absolute valid_until;
85 * Time of next validation
87 struct GNUNET_TIME_Absolute next_validation;
90 * Tranport conversion handle
92 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
100 * was the entry printed
106 * Struct to store information about peers in monitor mode
113 enum GNUNET_TRANSPORT_PeerState state;
118 struct GNUNET_TIME_Absolute state_timeout;
121 * The address to convert
123 struct GNUNET_HELLO_Address *address;
127 * Context to store name resolutions for valiation
129 struct PeerResolutionContext
134 struct PeerResolutionContext *next;
139 struct PeerResolutionContext *prev;
144 struct GNUNET_HELLO_Address *addrcp;
147 * transport conversiion context
149 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
154 enum GNUNET_TRANSPORT_PeerState state;
159 struct GNUNET_TIME_Absolute state_timeout;
167 * was the entry printed
174 * Context for a plugin test.
181 struct TestContext *prev;
186 struct TestContext *next;
189 * Handle to the active NAT test.
191 struct GNUNET_NAT_Test *tst;
194 * Task identifier for the timeout.
196 struct GNUNET_SCHEDULER_Task * tsk;
199 * Name of plugin under test.
206 unsigned long long bnd_port;
211 unsigned long long adv_port;
222 * NAT returned success
224 NAT_TEST_SUCCESS = GNUNET_OK,
227 * NAT returned failure
229 NAT_TEST_FAIL = GNUNET_NO,
232 * NAT returned failure while running test
234 NAT_TEST_INTERNAL_FAIL = GNUNET_SYSERR,
237 * We could not start the test
239 NAT_TEST_FAILED_TO_START = 2,
242 * We had a timeout while running the test
249 * Benchmarking block size in KB
254 * Which peer should we connect to?
259 * Handle to transport service.
261 static struct GNUNET_TRANSPORT_Handle *handle;
264 * Configuration handle
266 static struct GNUNET_CONFIGURATION_Handle *cfg;
269 * Blacklisting handle
271 struct GNUNET_TRANSPORT_Blacklist *blacklist;
276 static int benchmark_send;
281 static int benchmark_receive;
286 static int benchmark_receive;
291 static int iterate_connections;
296 static int iterate_all;
301 static int test_configuration;
306 static int monitor_connects;
311 static int monitor_connections;
316 static int monitor_plugins;
321 static int do_disconnect;
329 * Global return value (0 success).
334 * Current number of connections in monitor mode
336 static int monitor_connect_counter;
339 * Number of bytes of traffic we received so far.
341 static unsigned long long traffic_received;
344 * Number of bytes of traffic we sent so far.
346 static unsigned long long traffic_sent;
349 * Starting time of transmitting/receiving data.
351 static struct GNUNET_TIME_Absolute start_time;
354 * Handle for current transmission request.
356 static struct GNUNET_TRANSPORT_TransmitHandle *th;
359 * Map storing information about monitored peers
361 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
364 * Map storing information about monitored plugins's sessions.
366 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_plugins;
369 * Handle if we are monitoring peers at the transport level.
371 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
374 * Handle if we are monitoring plugin session activity.
376 static struct GNUNET_TRANSPORT_PluginMonitor *pm;
379 * Identity of the peer we transmit to / connect to.
380 * (equivalent to 'cpid' string).
382 static struct GNUNET_PeerIdentity pid;
385 * Task scheduled for cleanup / termination of the process.
387 static struct GNUNET_SCHEDULER_Task * end;
390 * Task for operation timeout
392 static struct GNUNET_SCHEDULER_Task * op_timeout;
395 * Selected level of verbosity.
397 static int verbosity;
400 * Resolver process handle.
402 struct GNUNET_OS_Process *resolver;
405 * Number of address resolutions pending
407 static unsigned int address_resolutions;
410 * DLL for NAT Test Contexts: head
412 struct TestContext *head;
415 * DLL for NAT Test Contexts: tail
417 struct TestContext *tail;
420 * DLL: head of validation resolution entries
422 static struct ValidationResolutionContext *vc_head;
425 * DLL: tail of validation resolution entries
427 static struct ValidationResolutionContext *vc_tail;
430 * DLL: head of resolution entries
432 static struct PeerResolutionContext *rc_head;
435 * DLL: head of resolution entries
437 static struct PeerResolutionContext *rc_tail;
441 * Function called to release data stored in the #monitored_peers map.
444 * @param key the peer identity
445 * @param value a `struct MonitoredPeer` to release
446 * @return #GNUNET_OK (continue to iterate)
449 destroy_it (void *cls,
450 const struct GNUNET_PeerIdentity *key,
453 struct MonitoredPeer *m = value;
455 GNUNET_assert (GNUNET_OK ==
456 GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
459 GNUNET_free_non_null (m->address);
466 * Task run in monitor mode when the user presses CTRL-C to abort.
467 * Stops monitoring activity.
472 shutdown_task (void *cls)
474 struct GNUNET_TIME_Relative duration;
475 struct ValidationResolutionContext *cur;
476 struct ValidationResolutionContext *next;
477 struct PeerResolutionContext *rc;
480 if (NULL != op_timeout)
482 GNUNET_SCHEDULER_cancel (op_timeout);
487 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
492 GNUNET_TRANSPORT_monitor_plugins_cancel (pm);
497 for (cur = next; NULL != cur; cur = next)
501 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
502 GNUNET_CONTAINER_DLL_remove (vc_head,
505 GNUNET_free (cur->transport);
506 GNUNET_HELLO_address_free (cur->addrcp);
509 while (NULL != (rc = rc_head))
511 GNUNET_CONTAINER_DLL_remove (rc_head,
514 GNUNET_TRANSPORT_address_to_string_cancel (rc->asc);
515 GNUNET_free (rc->transport);
516 GNUNET_free (rc->addrcp);
521 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
526 GNUNET_TRANSPORT_disconnect (handle);
531 duration = GNUNET_TIME_absolute_get_duration (start_time);
533 _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
534 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
536 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
538 if (benchmark_receive)
540 duration = GNUNET_TIME_absolute_get_duration (start_time);
542 _("Received %llu bytes/s (%llu bytes in %s)\n"),
543 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
545 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
548 if (NULL != monitored_peers)
550 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
551 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
552 monitored_peers = NULL;
554 if (NULL != monitored_plugins)
557 GNUNET_CONTAINER_multipeermap_size (monitored_plugins));
558 GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
559 monitored_plugins = NULL;
561 if (NULL != blacklist)
563 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
571 * We are done, shut down.
574 operation_timeout (void *cls)
576 struct PeerResolutionContext *cur;
577 struct PeerResolutionContext *next;
580 if ((benchmark_send) || (benchmark_receive))
583 _("Failed to connect to `%s'\n"),
584 GNUNET_i2s_full (&pid));
585 GNUNET_SCHEDULER_shutdown ();
589 if (iterate_connections)
592 while (NULL != (cur = next))
596 _("Failed to resolve address for peer `%s'\n"),
597 GNUNET_i2s (&cur->addrcp->peer));
599 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
600 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
601 GNUNET_free(cur->transport);
602 GNUNET_free(cur->addrcp);
608 _("Failed to list connections, timeout occured\n"));
609 GNUNET_SCHEDULER_shutdown ();
621 * Display the result of the test.
623 * @param tc test context
624 * @param result #GNUNET_YES on success
627 display_test_result (struct TestContext *tc,
628 enum TestResult result)
633 _("Configuration for plugin `%s' did not work!\n"),
636 case NAT_TEST_SUCCESS:
638 _("Configuration for plugin `%s' did work!\n"),
641 case NAT_TEST_INTERNAL_FAIL:
643 _("Internal NAT error while running test for plugin `%s'\n"),
646 case NAT_TEST_FAILED_TO_START:
648 _("Failed to start NAT test for plugin `%s'\n"),
651 case NAT_TEST_TIMEOUT:
653 _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
658 _("Configuration for plugin `%s' did not work!\n"),
664 GNUNET_SCHEDULER_cancel (tc->tsk);
669 GNUNET_NAT_test_stop (tc->tst);
673 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
674 GNUNET_free (tc->name);
677 if ((NULL == head) && (NULL != resolver))
679 GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
681 GNUNET_OS_process_destroy (resolver);
690 * Function called by NAT to report the outcome of the nat-test.
691 * Clean up and update GUI.
693 * @param cls test context
694 * @param result status code
697 result_callback (void *cls,
698 enum GNUNET_NAT_StatusCode result)
700 struct TestContext *tc = cls;
702 display_test_result (tc,
710 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
711 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
713 (uint16_t) head->bnd_port,
714 (uint16_t) head->adv_port);
716 head->tst = GNUNET_NAT_test_start (cfg,
717 (0 == strcasecmp (head->name, "udp"))
718 ? GNUNET_NO : GNUNET_YES,
719 (uint16_t) head->bnd_port,
720 (uint16_t) head->adv_port,
722 &result_callback, head);
727 * Test our plugin's configuration (NAT traversal, etc.).
729 * @param cfg configuration to test
732 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
736 unsigned long long bnd_port;
737 unsigned long long adv_port;
738 struct TestContext *tc;
742 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
745 FPRINTF (stderr, "%s", _
746 ("No transport plugins configured, peer will never communicate\n"));
751 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
753 char section[12 + strlen (tok)];
754 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
756 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
760 _("No port configured for plugin `%s', cannot test it\n"), tok);
763 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
764 "ADVERTISED_PORT", &adv_port))
767 tc = GNUNET_new (struct TestContext);
768 tc->name = GNUNET_strdup (tok);
769 tc->adv_port = adv_port;
770 tc->bnd_port = bnd_port;
771 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
773 GNUNET_free(plugins);
775 if ((NULL != head) && (NULL == resolver))
777 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
778 resolver = GNUNET_OS_start_process (GNUNET_YES,
779 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
782 "gnunet-service-resolver", NULL);
783 if (NULL == resolver)
785 FPRINTF (stderr, _("Failed to start resolver!\n"));
790 GNUNET_RESOLVER_connect (cfg);
797 * Function called to notify a client about the socket
798 * begin ready to queue more data. @a buf will be
799 * NULL and @a size zero if the socket was closed for
800 * writing in the meantime.
803 * @param size number of bytes available in @a buf
804 * @param buf where the callee should write the message
805 * @return number of bytes written to @a buf
808 transmit_data (void *cls,
812 struct GNUNET_MessageHeader *m = buf;
814 if ((NULL == buf) || (0 == size))
820 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
821 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
822 m->size = ntohs (size);
823 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
824 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
825 traffic_sent += size;
826 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
828 GNUNET_TIME_UNIT_FOREVER_REL,
829 &transmit_data, NULL);
831 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
838 * Function called to notify transport users that another
839 * peer connected to us.
842 * @param peer the peer that connected
845 notify_connect (void *cls,
846 const struct GNUNET_PeerIdentity *peer)
848 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
853 if (NULL != op_timeout)
855 GNUNET_SCHEDULER_cancel (op_timeout);
860 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
861 GNUNET_i2s (&pid), BLOCKSIZE);
862 start_time = GNUNET_TIME_absolute_get ();
864 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
866 GNUNET_TIME_UNIT_FOREVER_REL,
877 * Function called to notify transport users that another
878 * peer disconnected from us.
881 * @param peer the peer that disconnected
884 notify_disconnect (void *cls,
885 const struct GNUNET_PeerIdentity *peer)
887 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
892 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
897 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
900 GNUNET_SCHEDULER_cancel (end);
907 * Function called to notify transport users that another
908 * peer connected to us.
911 * @param peer the peer that connected
914 monitor_notify_connect (void *cls,
915 const struct GNUNET_PeerIdentity *peer)
917 monitor_connect_counter++;
918 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
919 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
922 _("%24s: %-17s %4s (%u connections in total)\n"),
926 monitor_connect_counter);
931 * Function called to notify transport users that another
932 * peer disconnected from us.
935 * @param peer the peer that disconnected
938 monitor_notify_disconnect (void *cls,
939 const struct GNUNET_PeerIdentity *peer)
941 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
942 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
944 GNUNET_assert(monitor_connect_counter > 0);
945 monitor_connect_counter--;
948 _("%24s: %-17s %4s (%u connections in total)\n"),
950 _("Disconnected from"),
952 monitor_connect_counter);
957 * Function called by the transport for each received message.
960 * @param peer (claimed) identity of the other peer
961 * @param message the message
964 notify_receive (void *cls,
965 const struct GNUNET_PeerIdentity *peer,
966 const struct GNUNET_MessageHeader *message)
968 if (benchmark_receive)
970 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
974 _("Received %u bytes from %s\n"),
975 (unsigned int) ntohs (message->size),
978 if (traffic_received == 0)
979 start_time = GNUNET_TIME_absolute_get ();
980 traffic_received += ntohs (message->size);
987 * Convert address to a printable format.
989 * @param address the address
990 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
991 * to try to use reverse DNS
992 * @param state state the peer is in
993 * @param state_timeout when will the peer's state expire
996 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
998 enum GNUNET_TRANSPORT_PeerState state,
999 struct GNUNET_TIME_Absolute state_timeout);
1003 print_info (const struct GNUNET_PeerIdentity *id,
1004 const char *transport,
1006 enum GNUNET_TRANSPORT_PeerState state,
1007 struct GNUNET_TIME_Absolute state_timeout)
1010 if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1011 (GNUNET_YES == monitor_connections))
1014 _("Peer `%s': %s %s in state `%s' until %s\n"),
1016 (NULL == transport) ? "<none>" : transport,
1017 (NULL == transport) ? "<none>" : addr,
1018 GNUNET_TRANSPORT_ps2s (state),
1019 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1021 else if ( (GNUNET_YES == iterate_connections) &&
1022 (GNUNET_TRANSPORT_is_connected(state)))
1024 /* Only connected peers, skip state */
1026 _("Peer `%s': %s %s\n"),
1035 * Function called with a textual representation of an address. This
1036 * function will be called several times with different possible
1037 * textual representations, and a last time with @a address being NULL
1038 * to signal the end of the iteration. Note that @a address NULL
1039 * always is the last call, regardless of the value in @a res.
1041 * @param cls closure
1042 * @param address NULL on end of iteration,
1043 * otherwise 0-terminated printable UTF-8 string,
1044 * in particular an empty string if @a res is #GNUNET_NO
1045 * @param res result of the address to string conversion:
1046 * if #GNUNET_OK: conversion successful
1047 * if #GNUNET_NO: address was invalid (or not supported)
1048 * if #GNUNET_SYSERR: communication error (IPC error)
1051 process_peer_string (void *cls,
1052 const char *address,
1055 struct PeerResolutionContext *rc = cls;
1057 if (NULL != address)
1059 if (GNUNET_SYSERR == res)
1062 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
1063 GNUNET_i2s (&rc->addrcp->peer),
1064 rc->addrcp->transport_name,
1065 (unsigned int) rc->addrcp->address_length);
1066 print_info (&rc->addrcp->peer,
1071 rc->printed = GNUNET_YES;
1074 if (GNUNET_OK == res)
1076 print_info (&rc->addrcp->peer,
1081 rc->printed = GNUNET_YES;
1082 return; /* Wait for done call */
1084 /* GNUNET_NO == res: ignore, was simply not supported */
1087 /* NULL == address, last call, we are done */
1090 GNUNET_assert (address_resolutions > 0);
1091 address_resolutions--;
1092 if (GNUNET_NO == rc->printed)
1094 if (numeric == GNUNET_NO)
1096 /* Failed to resolve address, try numeric lookup
1097 (note: this should not be needed, as transport
1098 should fallback to numeric conversion if DNS takes
1100 resolve_peer_address (rc->addrcp,
1107 print_info (&rc->addrcp->peer,
1114 GNUNET_free (rc->transport);
1115 GNUNET_free (rc->addrcp);
1116 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1118 if ((0 == address_resolutions) && (iterate_connections))
1120 if (NULL != op_timeout)
1122 GNUNET_SCHEDULER_cancel (op_timeout);
1126 GNUNET_SCHEDULER_shutdown ();
1132 * Convert address to a printable format and print it
1133 * together with the given state data.
1135 * @param address the address
1136 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1137 * to try to use reverse DNS
1138 * @param state state the peer is in
1139 * @param state_timeout when will the peer's state expire
1142 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1144 enum GNUNET_TRANSPORT_PeerState state,
1145 struct GNUNET_TIME_Absolute state_timeout)
1147 struct PeerResolutionContext *rc;
1149 rc = GNUNET_new (struct PeerResolutionContext);
1150 GNUNET_CONTAINER_DLL_insert (rc_head,
1153 address_resolutions++;
1154 rc->transport = GNUNET_strdup (address->transport_name);
1155 rc->addrcp = GNUNET_HELLO_address_copy (address);
1156 rc->printed = GNUNET_NO;
1158 rc->state_timeout = state_timeout;
1159 /* Resolve address to string */
1160 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1164 &process_peer_string, rc);
1169 * Function called with information about a peers during a one shot iteration
1171 * @param cls closure
1172 * @param peer identity of the peer, NULL for final callback when operation done
1173 * @param address binary address used to communicate with this peer,
1174 * NULL on disconnect or when done
1175 * @param state current state this peer is in
1176 * @param state_timeout time out for the current state
1179 process_peer_iteration_cb (void *cls,
1180 const struct GNUNET_PeerIdentity *peer,
1181 const struct GNUNET_HELLO_Address *address,
1182 enum GNUNET_TRANSPORT_PeerState state,
1183 struct GNUNET_TIME_Absolute state_timeout)
1192 if ( (GNUNET_NO == iterate_all) &&
1193 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1194 return; /* Display only connected peers */
1196 if (NULL != op_timeout)
1197 GNUNET_SCHEDULER_cancel (op_timeout);
1198 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1202 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1203 "Received address for peer `%s': %s\n",
1205 address ? address->transport_name : "");
1207 if (NULL != address)
1208 resolve_peer_address (address,
1222 * Context for address resolution by #plugin_monitoring_cb().
1224 struct PluginMonitorAddress
1228 * Ongoing resolution request.
1230 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
1233 * Resolved address as string.
1238 * Last event we got and did not yet print because
1239 * @e str was NULL (address not yet resolved).
1241 struct GNUNET_TRANSPORT_SessionInfo si;
1246 * Print information about a plugin monitoring event.
1248 * @param addr out internal context
1249 * @param info the monitoring information
1252 print_plugin_event_info (struct PluginMonitorAddress *addr,
1253 const struct GNUNET_TRANSPORT_SessionInfo *info)
1257 switch (info->state)
1259 case GNUNET_TRANSPORT_SS_INIT:
1262 case GNUNET_TRANSPORT_SS_HANDSHAKE:
1263 state = "HANDSHAKE";
1265 case GNUNET_TRANSPORT_SS_UP:
1268 case GNUNET_TRANSPORT_SS_UPDATE:
1271 case GNUNET_TRANSPORT_SS_DONE:
1279 "%s: state %s timeout in %s @ %s%s\n",
1280 GNUNET_i2s (&info->address->peer),
1282 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
1285 (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
1287 "%s: queue has %3u messages and %6u bytes\n",
1288 GNUNET_i2s (&info->address->peer),
1289 info->num_msg_pending,
1290 info->num_bytes_pending);
1291 if (0 != GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
1293 "%s: receiving blocked until %s\n",
1294 GNUNET_i2s (&info->address->peer),
1295 GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
1300 * Function called with a textual representation of an address. This
1301 * function will be called several times with different possible
1302 * textual representations, and a last time with @a address being NULL
1303 * to signal the end of the iteration. Note that @a address NULL
1304 * always is the last call, regardless of the value in @a res.
1306 * @param cls closure
1307 * @param address NULL on end of iteration,
1308 * otherwise 0-terminated printable UTF-8 string,
1309 * in particular an empty string if @a res is #GNUNET_NO
1310 * @param res result of the address to string conversion:
1311 * if #GNUNET_OK: conversion successful
1312 * if #GNUNET_NO: address was invalid (or not supported)
1313 * if #GNUNET_SYSERR: communication error (IPC error)
1316 address_cb (void *cls,
1317 const char *address,
1320 struct PluginMonitorAddress *addr = cls;
1322 if (NULL == address)
1327 if (NULL != addr->str)
1329 addr->str = GNUNET_strdup (address);
1330 print_plugin_event_info (addr,
1336 * Function called by the plugin with information about the
1337 * current sessions managed by the plugin (for monitoring).
1339 * @param cls closure (NULL)
1340 * @param session session handle this information is about,
1341 * NULL to indicate that we are "in sync" (initial
1342 * iteration complete)
1343 * @param session_ctx storage location where the application
1344 * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1345 * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1346 * @param info information about the state of the session,
1347 * NULL if @a session is also NULL and we are
1348 * merely signalling that the initial iteration is over;
1349 * NULL with @a session being non-NULL if the monitor
1350 * was being cancelled while sessions were active
1353 plugin_monitoring_cb (void *cls,
1354 struct GNUNET_TRANSPORT_PluginSession *session,
1356 const struct GNUNET_TRANSPORT_SessionInfo *info)
1358 struct PluginMonitorAddress *addr;
1360 if ( (NULL == info) &&
1362 return; /* in sync with transport service */
1363 addr = *session_ctx;
1368 if (NULL != addr->asc)
1370 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1373 GNUNET_free_non_null (addr->str);
1375 *session_ctx = NULL;
1377 return; /* shutdown */
1379 if ( (NULL != cpid) &&
1380 (0 != memcmp (&info->address->peer,
1382 sizeof (struct GNUNET_PeerIdentity))) )
1383 return; /* filtered */
1386 addr = GNUNET_new (struct PluginMonitorAddress);
1387 addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1390 GNUNET_TIME_UNIT_FOREVER_REL,
1393 *session_ctx = addr;
1395 if (NULL == addr->str)
1398 print_plugin_event_info (addr,
1400 if (GNUNET_TRANSPORT_SS_DONE == info->state)
1402 if (NULL != addr->asc)
1404 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1407 GNUNET_free_non_null (addr->str);
1409 *session_ctx = NULL;
1415 * Function called with information about a peers
1417 * @param cls closure, NULL
1418 * @param peer identity of the peer, NULL for final callback when operation done
1419 * @param address binary address used to communicate with this peer,
1420 * NULL on disconnect or when done
1421 * @param state current state this peer is in
1422 * @param state_timeout time out for the current state
1425 process_peer_monitoring_cb (void *cls,
1426 const struct GNUNET_PeerIdentity *peer,
1427 const struct GNUNET_HELLO_Address *address,
1428 enum GNUNET_TRANSPORT_PeerState state,
1429 struct GNUNET_TIME_Absolute state_timeout)
1431 struct MonitoredPeer *m;
1437 _("Monitor disconnected from transport service. Reconnecting.\n"));
1441 if (NULL != op_timeout)
1442 GNUNET_SCHEDULER_cancel (op_timeout);
1443 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1447 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1450 m = GNUNET_new (struct MonitoredPeer);
1451 GNUNET_CONTAINER_multipeermap_put (monitored_peers,
1454 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1458 if ( (m->state == state) &&
1459 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1460 (NULL == address) &&
1461 (NULL == m->address) )
1463 return; /* No real change */
1465 if ( (m->state == state) &&
1466 (NULL != address) &&
1467 (NULL != m->address) &&
1468 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1469 return; /* No real change */
1472 if (NULL != m->address)
1474 GNUNET_free (m->address);
1477 if (NULL != address)
1478 m->address = GNUNET_HELLO_address_copy (address);
1480 m->state_timeout = state_timeout;
1482 if (NULL != address)
1483 resolve_peer_address (m->address,
1497 * Function called with the transport service checking if we
1498 * want to blacklist a peer. Return #GNUNET_SYSERR for the
1499 * peer that we should disconnect from.
1502 * @param cpid peer to check blacklisting for
1503 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1506 blacklist_cb (void *cls,
1507 const struct GNUNET_PeerIdentity *cpid)
1509 if (0 == memcmp (cpid,
1511 sizeof (struct GNUNET_PeerIdentity)))
1512 return GNUNET_SYSERR;
1518 * Function called with the result of the check if the 'transport'
1519 * service is running.
1521 * @param cls closure with our configuration
1522 * @param result #GNUNET_YES if transport is running
1525 testservice_task (void *cls,
1531 if (GNUNET_YES != result)
1533 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1537 if ( (NULL != cpid) &&
1539 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
1544 _("Failed to parse peer identity `%s'\n"),
1549 counter = benchmark_send + benchmark_receive + iterate_connections
1550 + monitor_connections + monitor_connects + do_disconnect +
1556 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1557 "disconnect", "benchmark send", "benchmark receive", "information",
1558 "monitor", "events", "plugins");
1564 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1565 "disconnect", "benchmark send", "benchmark receive", "information",
1566 "monitor", "events", "plugins");
1570 if (do_disconnect) /* -D: Disconnect from peer */
1575 _("Option `%s' makes no sense without option `%s'.\n"),
1580 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1583 if (NULL == blacklist)
1587 _("Failed to connect to transport service for disconnection\n"));
1593 _("Blacklisting request in place, stop with CTRL-C\n"));
1595 else if (benchmark_send) /* -s: Benchmark sending */
1599 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1604 handle = GNUNET_TRANSPORT_connect (cfg,
1609 ¬ify_disconnect);
1612 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1616 start_time = GNUNET_TIME_absolute_get ();
1617 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1621 else if (benchmark_receive) /* -b: Benchmark receiving */
1623 handle = GNUNET_TRANSPORT_connect (cfg,
1631 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1636 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
1637 start_time = GNUNET_TIME_absolute_get ();
1640 else if (iterate_connections) /* -i: List information about peers once */
1642 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1643 (NULL == cpid) ? NULL : &pid,
1645 &process_peer_iteration_cb,
1647 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1651 else if (monitor_connections) /* -m: List information about peers continuously */
1653 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
1655 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1656 (NULL == cpid) ? NULL : &pid,
1658 &process_peer_monitoring_cb,
1661 else if (monitor_plugins) /* -P: List information about plugins continuously */
1663 monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1664 pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
1665 &plugin_monitoring_cb,
1668 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1670 monitor_connect_counter = 0;
1671 handle = GNUNET_TRANSPORT_connect (cfg,
1675 &monitor_notify_connect,
1676 &monitor_notify_disconnect);
1681 _("Failed to connect to transport service\n"));
1693 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1699 * Main function that will be run by the scheduler.
1701 * @param cls closure
1702 * @param args remaining command-line arguments
1703 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1704 * @param mycfg configuration
1709 const char *cfgfile,
1710 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1712 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1713 if (test_configuration)
1715 do_test_configuration (cfg);
1718 GNUNET_CLIENT_service_test ("transport",
1720 GNUNET_TIME_UNIT_SECONDS,
1731 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1733 gettext_noop ("print information for all peers (instead of only connected peers)"),
1734 0, &GNUNET_GETOPT_set_one, &iterate_all },
1735 { 'b', "benchmark", NULL,
1736 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1737 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1738 { 'D', "disconnect",
1739 NULL, gettext_noop ("disconnect from a peer"), 0,
1740 &GNUNET_GETOPT_set_one, &do_disconnect },
1741 { 'i', "information", NULL,
1742 gettext_noop ("provide information about all current connections (once)"),
1743 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1744 { 'm', "monitor", NULL,
1745 gettext_noop ("provide information about all current connections (continuously)"),
1746 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1747 { 'e', "events", NULL,
1748 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1749 0, &GNUNET_GETOPT_set_one, &monitor_connects },
1751 NULL, gettext_noop ("do not resolve hostnames"), 0,
1752 &GNUNET_GETOPT_set_one, &numeric },
1753 { 'p', "peer", "PEER",
1754 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1756 { 'P', "plugins", NULL,
1757 gettext_noop ("monitor plugin sessions"), 0, &GNUNET_GETOPT_set_one,
1759 { 's', "send", NULL, gettext_noop
1760 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1761 &GNUNET_GETOPT_set_one, &benchmark_send },
1762 { 't', "test", NULL,
1763 gettext_noop ("test transport configuration (involves external server)"),
1764 0, &GNUNET_GETOPT_set_one, &test_configuration },
1765 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1766 GNUNET_GETOPT_OPTION_END
1769 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1772 res = GNUNET_PROGRAM_run (argc, argv,
1774 gettext_noop ("Direct access to transport service."),
1777 GNUNET_free((void *) argv);
1778 if (GNUNET_OK == res)
1783 /* end of gnunet-transport.c */