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;
218 * Benchmarking block size in KB
223 * Which peer should we connect to?
228 * Handle to transport service.
230 static struct GNUNET_TRANSPORT_CoreHandle *handle;
233 * Configuration handle
235 static struct GNUNET_CONFIGURATION_Handle *cfg;
238 * Blacklisting handle
240 struct GNUNET_TRANSPORT_Blacklist *blacklist;
245 static int benchmark_send;
250 static int benchmark_receive;
255 static int benchmark_receive;
260 static int iterate_connections;
265 static int iterate_all;
270 static int test_configuration;
275 static int monitor_connects;
280 static int monitor_connections;
285 static int monitor_plugins;
290 static int do_disconnect;
298 * Global return value (0 success).
303 * Current number of connections in monitor mode
305 static int monitor_connect_counter;
308 * Number of bytes of traffic we received so far.
310 static unsigned long long traffic_received;
313 * Number of bytes of traffic we sent so far.
315 static unsigned long long traffic_sent;
318 * Starting time of transmitting/receiving data.
320 static struct GNUNET_TIME_Absolute start_time;
323 * Map storing information about monitored peers
325 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
328 * Map storing information about monitored plugins's sessions.
330 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_plugins;
333 * Handle if we are monitoring peers at the transport level.
335 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
338 * Handle if we are monitoring plugin session activity.
340 static struct GNUNET_TRANSPORT_PluginMonitor *pm;
343 * Identity of the peer we transmit to / connect to.
344 * (equivalent to 'cpid' string).
346 static struct GNUNET_PeerIdentity pid;
349 * Task for operation timeout
351 static struct GNUNET_SCHEDULER_Task *op_timeout;
354 * Selected level of verbosity.
356 static int verbosity;
359 * Resolver process handle.
361 struct GNUNET_OS_Process *resolver;
364 * Number of address resolutions pending
366 static unsigned int address_resolutions;
369 * DLL for NAT Test Contexts: head
371 static struct TestContext *head;
374 * DLL for NAT Test Contexts: tail
376 static struct TestContext *tail;
379 * DLL: head of validation resolution entries
381 static struct ValidationResolutionContext *vc_head;
384 * DLL: tail of validation resolution entries
386 static struct ValidationResolutionContext *vc_tail;
389 * DLL: head of resolution entries
391 static struct PeerResolutionContext *rc_head;
394 * DLL: head of resolution entries
396 static struct PeerResolutionContext *rc_tail;
400 * Function called to release data stored in the #monitored_peers map.
403 * @param key the peer identity
404 * @param value a `struct MonitoredPeer` to release
405 * @return #GNUNET_OK (continue to iterate)
408 destroy_it (void *cls,
409 const struct GNUNET_PeerIdentity *key,
412 struct MonitoredPeer *m = value;
414 GNUNET_assert (GNUNET_OK ==
415 GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
418 GNUNET_free_non_null (m->address);
425 * Task run in monitor mode when the user presses CTRL-C to abort.
426 * Stops monitoring activity.
431 shutdown_task (void *cls)
433 struct GNUNET_TIME_Relative duration;
434 struct ValidationResolutionContext *cur;
435 struct ValidationResolutionContext *next;
436 struct PeerResolutionContext *rc;
438 if (NULL != op_timeout)
440 GNUNET_SCHEDULER_cancel (op_timeout);
445 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
450 GNUNET_TRANSPORT_monitor_plugins_cancel (pm);
455 for (cur = next; NULL != cur; cur = next)
459 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
460 GNUNET_CONTAINER_DLL_remove (vc_head,
463 GNUNET_free (cur->transport);
464 GNUNET_HELLO_address_free (cur->addrcp);
467 while (NULL != (rc = rc_head))
469 GNUNET_CONTAINER_DLL_remove (rc_head,
472 GNUNET_TRANSPORT_address_to_string_cancel (rc->asc);
473 GNUNET_free (rc->transport);
474 GNUNET_free (rc->addrcp);
479 GNUNET_TRANSPORT_core_disconnect (handle);
484 duration = GNUNET_TIME_absolute_get_duration (start_time);
486 _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
487 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
489 GNUNET_STRINGS_relative_time_to_string (duration,
492 if (benchmark_receive)
494 duration = GNUNET_TIME_absolute_get_duration (start_time);
496 _("Received %llu bytes/s (%llu bytes in %s)\n"),
497 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
499 GNUNET_STRINGS_relative_time_to_string (duration,
503 if (NULL != monitored_peers)
505 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers,
508 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
509 monitored_peers = NULL;
511 if (NULL != monitored_plugins)
514 GNUNET_CONTAINER_multipeermap_size (monitored_plugins));
515 GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
516 monitored_plugins = NULL;
518 if (NULL != blacklist)
520 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
528 * We are done, shut down.
531 operation_timeout (void *cls)
533 struct PeerResolutionContext *cur;
534 struct PeerResolutionContext *next;
537 if ((benchmark_send) || (benchmark_receive))
540 _("Failed to connect to `%s'\n"),
541 GNUNET_i2s_full (&pid));
542 GNUNET_SCHEDULER_shutdown ();
546 if (iterate_connections)
549 while (NULL != (cur = next))
553 _("Failed to resolve address for peer `%s'\n"),
554 GNUNET_i2s (&cur->addrcp->peer));
556 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
557 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
558 GNUNET_free(cur->transport);
559 GNUNET_free(cur->addrcp);
565 _("Failed to list connections, timeout occured\n"));
566 GNUNET_SCHEDULER_shutdown ();
578 * Display the result of the test.
580 * @param tc test context
581 * @param result #GNUNET_YES on success
584 display_test_result (struct TestContext *tc,
585 enum GNUNET_NAT_StatusCode result)
588 _("NAT plugin `%s' reports: %s\n"),
590 GNUNET_NAT_status2string (result));
593 GNUNET_SCHEDULER_cancel (tc->tsk);
598 GNUNET_NAT_test_stop (tc->tst);
602 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
603 GNUNET_free (tc->name);
606 if ((NULL == head) && (NULL != resolver))
608 GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
610 GNUNET_OS_process_destroy (resolver);
619 * Function called by NAT to report the outcome of the nat-test.
620 * Clean up and update GUI.
622 * @param cls test context
623 * @param result status code
626 result_callback (void *cls,
627 enum GNUNET_NAT_StatusCode result)
629 struct TestContext *tc = cls;
631 display_test_result (tc,
639 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
640 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
642 (uint16_t) head->bnd_port,
643 (uint16_t) head->adv_port);
645 head->tst = GNUNET_NAT_test_start (cfg,
646 (0 == strcasecmp (head->name, "udp"))
647 ? GNUNET_NO : GNUNET_YES,
648 (uint16_t) head->bnd_port,
649 (uint16_t) head->adv_port,
651 &result_callback, head);
656 * Test our plugin's configuration (NAT traversal, etc.).
658 * @param cfg configuration to test
661 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
665 unsigned long long bnd_port;
666 unsigned long long adv_port;
667 struct TestContext *tc;
671 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
674 FPRINTF (stderr, "%s", _
675 ("No transport plugins configured, peer will never communicate\n"));
680 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
682 char section[12 + strlen (tok)];
683 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
685 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
689 _("No port configured for plugin `%s', cannot test it\n"), tok);
692 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
693 "ADVERTISED_PORT", &adv_port))
696 tc = GNUNET_new (struct TestContext);
697 tc->name = GNUNET_strdup (tok);
698 tc->adv_port = adv_port;
699 tc->bnd_port = bnd_port;
700 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
702 GNUNET_free(plugins);
704 if ((NULL != head) && (NULL == resolver))
706 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
707 resolver = GNUNET_OS_start_process (GNUNET_YES,
708 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
711 "gnunet-service-resolver", NULL);
712 if (NULL == resolver)
714 FPRINTF (stderr, _("Failed to start resolver!\n"));
719 GNUNET_RESOLVER_connect (cfg);
726 * Function called to notify a client about the socket
727 * begin ready to queue more data. Sends another message.
729 * @param cls closure with the message queue
734 struct GNUNET_MQ_Handle *mq = cls;
735 struct GNUNET_MessageHeader *m;
736 struct GNUNET_MQ_Envelope *env;
738 env = GNUNET_MQ_msg_extra (m,
740 GNUNET_MESSAGE_TYPE_DUMMY);
743 BLOCKSIZE * 1024 - sizeof(struct GNUNET_MessageHeader));
744 traffic_sent += BLOCKSIZE * 1024;
745 GNUNET_MQ_notify_sent (env,
750 _("Transmitting %u bytes\n"),
751 (unsigned int) BLOCKSIZE * 1024);
758 * Function called to notify transport users that another
759 * peer connected to us.
762 * @param peer the peer that connected
763 * @param mq message queue for sending to @a peer
766 notify_connect (void *cls,
767 const struct GNUNET_PeerIdentity *peer,
768 struct GNUNET_MQ_Handle *mq)
770 if (0 != memcmp (&pid,
772 sizeof(struct GNUNET_PeerIdentity)))
775 if (! benchmark_send)
777 if (NULL != op_timeout)
779 GNUNET_SCHEDULER_cancel (op_timeout);
784 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
787 start_time = GNUNET_TIME_absolute_get ();
794 * Function called to notify transport users that another
795 * peer disconnected from us.
798 * @param peer the peer that disconnected
799 * @param internal_cls what we returned from #notify_connect()
802 notify_disconnect (void *cls,
803 const struct GNUNET_PeerIdentity *peer,
806 if (0 != memcmp (&pid,
808 sizeof(struct GNUNET_PeerIdentity)))
810 if (NULL == internal_cls)
811 return; /* not about target peer */
812 if (! benchmark_send)
813 return; /* not transmitting */
815 _("Disconnected from peer `%s' while benchmarking\n"),
821 * Function called to notify transport users that another
822 * peer connected to us.
825 * @param peer the peer that connected
826 * @param mq for sending messages to @a peer
830 monitor_notify_connect (void *cls,
831 const struct GNUNET_PeerIdentity *peer,
832 struct GNUNET_MQ_Handle *mq)
834 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
835 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
837 monitor_connect_counter++;
839 _("%24s: %-17s %4s (%u connections in total)\n"),
843 monitor_connect_counter);
849 * Function called to notify transport users that another
850 * peer disconnected from us.
853 * @param peer the peer that disconnected
854 * @param internal_cls what we returned from #monitor_notify_connect()
857 monitor_notify_disconnect (void *cls,
858 const struct GNUNET_PeerIdentity *peer,
861 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
862 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
864 GNUNET_assert(monitor_connect_counter > 0);
865 monitor_connect_counter--;
868 _("%24s: %-17s %4s (%u connections in total)\n"),
870 _("Disconnected from"),
872 monitor_connect_counter);
877 * Function called by the transport for each received message.
880 * @param message the message
884 check_dummy (void *cls,
885 const struct GNUNET_MessageHeader *message)
887 return GNUNET_OK; /* all messages are fine */
892 * Function called by the transport for each received message.
895 * @param message the message
898 handle_dummy (void *cls,
899 const struct GNUNET_MessageHeader *message)
901 if (! benchmark_receive)
905 _("Received %u bytes\n"),
906 (unsigned int) ntohs (message->size));
907 if (0 == traffic_received)
908 start_time = GNUNET_TIME_absolute_get ();
909 traffic_received += ntohs (message->size);
914 * Convert address to a printable format.
916 * @param address the address
917 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
918 * to try to use reverse DNS
919 * @param state state the peer is in
920 * @param state_timeout when will the peer's state expire
923 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
925 enum GNUNET_TRANSPORT_PeerState state,
926 struct GNUNET_TIME_Absolute state_timeout);
930 print_info (const struct GNUNET_PeerIdentity *id,
931 const char *transport,
933 enum GNUNET_TRANSPORT_PeerState state,
934 struct GNUNET_TIME_Absolute state_timeout)
937 if ( ((GNUNET_YES == iterate_connections) &&
938 (GNUNET_YES == iterate_all)) ||
939 (GNUNET_YES == monitor_connections))
942 _("Peer `%s': %s %s in state `%s' until %s\n"),
944 (NULL == transport) ? "<none>" : transport,
945 (NULL == transport) ? "<none>" : addr,
946 GNUNET_TRANSPORT_ps2s (state),
947 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
949 else if ( (GNUNET_YES == iterate_connections) &&
950 (GNUNET_TRANSPORT_is_connected(state)) )
952 /* Only connected peers, skip state */
954 _("Peer `%s': %s %s\n"),
963 * Function called with a textual representation of an address. This
964 * function will be called several times with different possible
965 * textual representations, and a last time with @a address being NULL
966 * to signal the end of the iteration. Note that @a address NULL
967 * always is the last call, regardless of the value in @a res.
970 * @param address NULL on end of iteration,
971 * otherwise 0-terminated printable UTF-8 string,
972 * in particular an empty string if @a res is #GNUNET_NO
973 * @param res result of the address to string conversion:
974 * if #GNUNET_OK: conversion successful
975 * if #GNUNET_NO: address was invalid (or not supported)
976 * if #GNUNET_SYSERR: communication error (IPC error)
979 process_peer_string (void *cls,
983 struct PeerResolutionContext *rc = cls;
987 if (GNUNET_SYSERR == res)
990 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
991 GNUNET_i2s (&rc->addrcp->peer),
992 rc->addrcp->transport_name,
993 (unsigned int) rc->addrcp->address_length);
994 print_info (&rc->addrcp->peer,
999 rc->printed = GNUNET_YES;
1002 if (GNUNET_OK == res)
1004 print_info (&rc->addrcp->peer,
1009 rc->printed = GNUNET_YES;
1010 return; /* Wait for done call */
1012 /* GNUNET_NO == res: ignore, was simply not supported */
1015 /* NULL == address, last call, we are done */
1018 GNUNET_assert (address_resolutions > 0);
1019 address_resolutions--;
1020 if (GNUNET_NO == rc->printed)
1022 if (numeric == GNUNET_NO)
1024 /* Failed to resolve address, try numeric lookup
1025 (note: this should not be needed, as transport
1026 should fallback to numeric conversion if DNS takes
1028 resolve_peer_address (rc->addrcp,
1035 print_info (&rc->addrcp->peer,
1042 GNUNET_free (rc->transport);
1043 GNUNET_free (rc->addrcp);
1044 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1046 if ((0 == address_resolutions) && (iterate_connections))
1048 if (NULL != op_timeout)
1050 GNUNET_SCHEDULER_cancel (op_timeout);
1054 GNUNET_SCHEDULER_shutdown ();
1060 * Convert address to a printable format and print it
1061 * together with the given state data.
1063 * @param address the address
1064 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1065 * to try to use reverse DNS
1066 * @param state state the peer is in
1067 * @param state_timeout when will the peer's state expire
1070 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1072 enum GNUNET_TRANSPORT_PeerState state,
1073 struct GNUNET_TIME_Absolute state_timeout)
1075 struct PeerResolutionContext *rc;
1077 rc = GNUNET_new (struct PeerResolutionContext);
1078 GNUNET_CONTAINER_DLL_insert (rc_head,
1081 address_resolutions++;
1082 rc->transport = GNUNET_strdup (address->transport_name);
1083 rc->addrcp = GNUNET_HELLO_address_copy (address);
1084 rc->printed = GNUNET_NO;
1086 rc->state_timeout = state_timeout;
1087 /* Resolve address to string */
1088 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1092 &process_peer_string,
1098 * Function called with information about a peers during a one shot iteration
1100 * @param cls closure
1101 * @param peer identity of the peer, NULL for final callback when operation done
1102 * @param address binary address used to communicate with this peer,
1103 * NULL on disconnect or when done
1104 * @param state current state this peer is in
1105 * @param state_timeout time out for the current state
1108 process_peer_iteration_cb (void *cls,
1109 const struct GNUNET_PeerIdentity *peer,
1110 const struct GNUNET_HELLO_Address *address,
1111 enum GNUNET_TRANSPORT_PeerState state,
1112 struct GNUNET_TIME_Absolute state_timeout)
1121 if ( (GNUNET_NO == iterate_all) &&
1122 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1123 return; /* Display only connected peers */
1125 if (NULL != op_timeout)
1126 GNUNET_SCHEDULER_cancel (op_timeout);
1127 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1132 "Received address for peer `%s': %s\n",
1134 address ? address->transport_name : "");
1136 if (NULL != address)
1137 resolve_peer_address (address,
1151 * Context for address resolution by #plugin_monitoring_cb().
1153 struct PluginMonitorAddress
1157 * Ongoing resolution request.
1159 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
1162 * Resolved address as string.
1167 * Last event we got and did not yet print because
1168 * @e str was NULL (address not yet resolved).
1170 struct GNUNET_TRANSPORT_SessionInfo si;
1175 * Print information about a plugin monitoring event.
1177 * @param addr out internal context
1178 * @param info the monitoring information
1181 print_plugin_event_info (struct PluginMonitorAddress *addr,
1182 const struct GNUNET_TRANSPORT_SessionInfo *info)
1186 switch (info->state)
1188 case GNUNET_TRANSPORT_SS_INIT:
1191 case GNUNET_TRANSPORT_SS_HANDSHAKE:
1192 state = "HANDSHAKE";
1194 case GNUNET_TRANSPORT_SS_UP:
1197 case GNUNET_TRANSPORT_SS_UPDATE:
1200 case GNUNET_TRANSPORT_SS_DONE:
1208 "%s: state %s timeout in %s @ %s%s\n",
1209 GNUNET_i2s (&info->address->peer),
1211 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
1214 (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
1216 "%s: queue has %3u messages and %6u bytes\n",
1217 GNUNET_i2s (&info->address->peer),
1218 info->num_msg_pending,
1219 info->num_bytes_pending);
1220 if (0 != GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
1222 "%s: receiving blocked until %s\n",
1223 GNUNET_i2s (&info->address->peer),
1224 GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
1229 * Function called with a textual representation of an address. This
1230 * function will be called several times with different possible
1231 * textual representations, and a last time with @a address being NULL
1232 * to signal the end of the iteration. Note that @a address NULL
1233 * always is the last call, regardless of the value in @a res.
1235 * @param cls closure
1236 * @param address NULL on end of iteration,
1237 * otherwise 0-terminated printable UTF-8 string,
1238 * in particular an empty string if @a res is #GNUNET_NO
1239 * @param res result of the address to string conversion:
1240 * if #GNUNET_OK: conversion successful
1241 * if #GNUNET_NO: address was invalid (or not supported)
1242 * if #GNUNET_SYSERR: communication error (IPC error)
1245 address_cb (void *cls,
1246 const char *address,
1249 struct PluginMonitorAddress *addr = cls;
1251 if (NULL == address)
1256 if (NULL != addr->str)
1258 addr->str = GNUNET_strdup (address);
1259 print_plugin_event_info (addr,
1265 * Function called by the plugin with information about the
1266 * current sessions managed by the plugin (for monitoring).
1268 * @param cls closure (NULL)
1269 * @param session session handle this information is about,
1270 * NULL to indicate that we are "in sync" (initial
1271 * iteration complete)
1272 * @param session_ctx storage location where the application
1273 * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1274 * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1275 * @param info information about the state of the session,
1276 * NULL if @a session is also NULL and we are
1277 * merely signalling that the initial iteration is over;
1278 * NULL with @a session being non-NULL if the monitor
1279 * was being cancelled while sessions were active
1282 plugin_monitoring_cb (void *cls,
1283 struct GNUNET_TRANSPORT_PluginSession *session,
1285 const struct GNUNET_TRANSPORT_SessionInfo *info)
1287 struct PluginMonitorAddress *addr;
1289 if ( (NULL == info) &&
1291 return; /* in sync with transport service */
1292 addr = *session_ctx;
1297 if (NULL != addr->asc)
1299 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1302 GNUNET_free_non_null (addr->str);
1304 *session_ctx = NULL;
1306 return; /* shutdown */
1308 if ( (NULL != cpid) &&
1309 (0 != memcmp (&info->address->peer,
1311 sizeof (struct GNUNET_PeerIdentity))) )
1312 return; /* filtered */
1315 addr = GNUNET_new (struct PluginMonitorAddress);
1316 addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1319 GNUNET_TIME_UNIT_FOREVER_REL,
1322 *session_ctx = addr;
1324 if (NULL == addr->str)
1327 print_plugin_event_info (addr,
1329 if (GNUNET_TRANSPORT_SS_DONE == info->state)
1331 if (NULL != addr->asc)
1333 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1336 GNUNET_free_non_null (addr->str);
1338 *session_ctx = NULL;
1344 * Function called with information about a peers
1346 * @param cls closure, NULL
1347 * @param peer identity of the peer, NULL for final callback when operation done
1348 * @param address binary address used to communicate with this peer,
1349 * NULL on disconnect or when done
1350 * @param state current state this peer is in
1351 * @param state_timeout time out for the current state
1354 process_peer_monitoring_cb (void *cls,
1355 const struct GNUNET_PeerIdentity *peer,
1356 const struct GNUNET_HELLO_Address *address,
1357 enum GNUNET_TRANSPORT_PeerState state,
1358 struct GNUNET_TIME_Absolute state_timeout)
1360 struct MonitoredPeer *m;
1366 _("Monitor disconnected from transport service. Reconnecting.\n"));
1370 if (NULL != op_timeout)
1371 GNUNET_SCHEDULER_cancel (op_timeout);
1372 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1376 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1379 m = GNUNET_new (struct MonitoredPeer);
1380 GNUNET_CONTAINER_multipeermap_put (monitored_peers,
1383 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1387 if ( (m->state == state) &&
1388 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1389 (NULL == address) &&
1390 (NULL == m->address) )
1392 return; /* No real change */
1394 if ( (m->state == state) &&
1395 (NULL != address) &&
1396 (NULL != m->address) &&
1397 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1398 return; /* No real change */
1401 if (NULL != m->address)
1403 GNUNET_free (m->address);
1406 if (NULL != address)
1407 m->address = GNUNET_HELLO_address_copy (address);
1409 m->state_timeout = state_timeout;
1411 if (NULL != address)
1412 resolve_peer_address (m->address,
1426 * Function called with the transport service checking if we
1427 * want to blacklist a peer. Return #GNUNET_SYSERR for the
1428 * peer that we should disconnect from.
1431 * @param cpid peer to check blacklisting for
1432 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1435 blacklist_cb (void *cls,
1436 const struct GNUNET_PeerIdentity *cpid)
1438 if (0 == memcmp (cpid,
1440 sizeof (struct GNUNET_PeerIdentity)))
1441 return GNUNET_SYSERR;
1447 * Main function that will be run by the scheduler.
1449 * @param cls closure
1450 * @param args remaining command-line arguments
1451 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1452 * @param mycfg configuration
1457 const char *cfgfile,
1458 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1463 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1464 if (test_configuration)
1466 do_test_configuration (cfg);
1469 if ( (NULL != cpid) &&
1471 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
1476 _("Failed to parse peer identity `%s'\n"),
1481 counter = benchmark_send + benchmark_receive + iterate_connections
1482 + monitor_connections + monitor_connects + do_disconnect +
1488 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1491 "benchmark receive",
1501 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1504 "benchmark receive",
1512 if (do_disconnect) /* -D: Disconnect from peer */
1517 _("Option `%s' makes no sense without option `%s'.\n"),
1522 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1525 if (NULL == blacklist)
1529 _("Failed to connect to transport service for disconnection\n"));
1535 _("Blacklisting request in place, stop with CTRL-C\n"));
1537 else if (benchmark_send) /* -s: Benchmark sending */
1542 _("Option `%s' makes no sense without option `%s'.\n"),
1547 handle = GNUNET_TRANSPORT_core_connect (cfg,
1558 _("Failed to connect to transport service\n"));
1562 start_time = GNUNET_TIME_absolute_get ();
1563 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1567 else if (benchmark_receive) /* -b: Benchmark receiving */
1569 struct GNUNET_MQ_MessageHandler handlers[] = {
1570 GNUNET_MQ_hd_var_size (dummy,
1571 GNUNET_MESSAGE_TYPE_DUMMY,
1572 struct GNUNET_MessageHeader,
1574 GNUNET_MQ_handler_end ()
1577 handle = GNUNET_TRANSPORT_core_connect (cfg,
1588 _("Failed to connect to transport service\n"));
1595 _("Starting to receive benchmark data\n"));
1596 start_time = GNUNET_TIME_absolute_get ();
1599 else if (iterate_connections) /* -i: List information about peers once */
1601 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1602 (NULL == cpid) ? NULL : &pid,
1604 &process_peer_iteration_cb,
1606 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1610 else if (monitor_connections) /* -m: List information about peers continuously */
1612 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
1614 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1615 (NULL == cpid) ? NULL : &pid,
1617 &process_peer_monitoring_cb,
1620 else if (monitor_plugins) /* -P: List information about plugins continuously */
1622 monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1623 pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
1624 &plugin_monitoring_cb,
1627 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1629 monitor_connect_counter = 0;
1630 handle = GNUNET_TRANSPORT_core_connect (cfg,
1634 &monitor_notify_connect,
1635 &monitor_notify_disconnect,
1641 _("Failed to connect to transport service\n"));
1653 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1663 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1665 gettext_noop ("print information for all peers (instead of only connected peers)"),
1666 0, &GNUNET_GETOPT_set_one, &iterate_all },
1667 { 'b', "benchmark", NULL,
1668 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1669 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1670 { 'D', "disconnect",
1671 NULL, gettext_noop ("disconnect from a peer"), 0,
1672 &GNUNET_GETOPT_set_one, &do_disconnect },
1673 { 'i', "information", NULL,
1674 gettext_noop ("provide information about all current connections (once)"),
1675 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1676 { 'm', "monitor", NULL,
1677 gettext_noop ("provide information about all current connections (continuously)"),
1678 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1679 { 'e', "events", NULL,
1680 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1681 0, &GNUNET_GETOPT_set_one, &monitor_connects },
1683 NULL, gettext_noop ("do not resolve hostnames"), 0,
1684 &GNUNET_GETOPT_set_one, &numeric },
1685 { 'p', "peer", "PEER",
1686 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1688 { 'P', "plugins", NULL,
1689 gettext_noop ("monitor plugin sessions"), 0, &GNUNET_GETOPT_set_one,
1691 { 's', "send", NULL, gettext_noop
1692 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1693 &GNUNET_GETOPT_set_one, &benchmark_send },
1694 { 't', "test", NULL,
1695 gettext_noop ("test transport configuration (involves external server)"),
1696 0, &GNUNET_GETOPT_set_one, &test_configuration },
1697 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1698 GNUNET_GETOPT_OPTION_END
1701 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1704 res = GNUNET_PROGRAM_run (argc, argv,
1706 gettext_noop ("Direct access to transport service."),
1709 GNUNET_free((void *) argv);
1710 if (GNUNET_OK == res)
1715 /* end of gnunet-transport.c */