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 * state of validation process
92 enum GNUNET_TRANSPORT_ValidationState state;
95 * Tranport conversion handle
97 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
105 * was the entry printed
111 * Struct to store information about peers in monitor mode
118 enum GNUNET_TRANSPORT_PeerState state;
123 struct GNUNET_TIME_Absolute state_timeout;
126 * The address to convert
128 struct GNUNET_HELLO_Address *address;
132 * Context to store name resolutions for valiation
134 struct PeerResolutionContext
139 struct PeerResolutionContext *next;
144 struct PeerResolutionContext *prev;
149 struct GNUNET_HELLO_Address *addrcp;
152 * transport conversiion context
154 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
159 enum GNUNET_TRANSPORT_PeerState state;
164 struct GNUNET_TIME_Absolute state_timeout;
172 * was the entry printed
179 * Context for a plugin test.
186 struct TestContext *prev;
191 struct TestContext *next;
194 * Handle to the active NAT test.
196 struct GNUNET_NAT_Test *tst;
199 * Task identifier for the timeout.
201 struct GNUNET_SCHEDULER_Task * tsk;
204 * Name of plugin under test.
211 unsigned long long bnd_port;
216 unsigned long long adv_port;
227 * NAT returned success
229 NAT_TEST_SUCCESS = GNUNET_OK,
232 * NAT returned failure
234 NAT_TEST_FAIL = GNUNET_NO,
237 * NAT returned failure while running test
239 NAT_TEST_INTERNAL_FAIL = GNUNET_SYSERR,
242 * We could not start the test
244 NAT_TEST_FAILED_TO_START = 2,
247 * We had a timeout while running the test
254 * Benchmarking block size in KB
259 * Which peer should we connect to?
264 * Handle to transport service.
266 static struct GNUNET_TRANSPORT_Handle *handle;
269 * Configuration handle
271 static struct GNUNET_CONFIGURATION_Handle *cfg;
274 * Blacklisting handle
276 struct GNUNET_TRANSPORT_Blacklist *blacklist;
281 static int benchmark_send;
286 static int benchmark_receive;
291 static int benchmark_receive;
296 static int iterate_connections;
301 static int iterate_validation;
306 static int iterate_all;
311 static int test_configuration;
316 static int monitor_connects;
321 static int monitor_connections;
326 static int monitor_validation;
331 static int monitor_plugins;
336 static int do_disconnect;
344 * Global return value (0 success).
349 * Current number of connections in monitor mode
351 static int monitor_connect_counter;
354 * Number of bytes of traffic we received so far.
356 static unsigned long long traffic_received;
359 * Number of bytes of traffic we sent so far.
361 static unsigned long long traffic_sent;
364 * Starting time of transmitting/receiving data.
366 static struct GNUNET_TIME_Absolute start_time;
369 * Handle for current transmission request.
371 static struct GNUNET_TRANSPORT_TransmitHandle *th;
374 * Map storing information about monitored peers
376 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
379 * Map storing information about monitored plugins's sessions.
381 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_plugins;
384 * Handle if we are monitoring peers at the transport level.
386 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
389 * Handle if we are monitoring transport validation activity.
391 static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic;
394 * Handle if we are monitoring plugin session activity.
396 static struct GNUNET_TRANSPORT_PluginMonitor *pm;
399 * Identity of the peer we transmit to / connect to.
400 * (equivalent to 'cpid' string).
402 static struct GNUNET_PeerIdentity pid;
405 * Task scheduled for cleanup / termination of the process.
407 static struct GNUNET_SCHEDULER_Task * end;
410 * Task for operation timeout
412 static struct GNUNET_SCHEDULER_Task * op_timeout;
415 * Selected level of verbosity.
417 static int verbosity;
420 * Resolver process handle.
422 struct GNUNET_OS_Process *resolver;
425 * Number of address resolutions pending
427 static unsigned int address_resolutions;
430 * DLL for NAT Test Contexts: head
432 struct TestContext *head;
435 * DLL for NAT Test Contexts: tail
437 struct TestContext *tail;
440 * DLL: head of validation resolution entries
442 static struct ValidationResolutionContext *vc_head;
445 * DLL: tail of validation resolution entries
447 static struct ValidationResolutionContext *vc_tail;
450 * DLL: head of resolution entries
452 static struct PeerResolutionContext *rc_head;
455 * DLL: head of resolution entries
457 static struct PeerResolutionContext *rc_tail;
461 * Function called to release data stored in the #monitored_peers map.
464 * @param key the peer identity
465 * @param value a `struct MonitoredPeer` to release
466 * @return #GNUNET_OK (continue to iterate)
469 destroy_it (void *cls,
470 const struct GNUNET_PeerIdentity *key,
473 struct MonitoredPeer *m = value;
475 GNUNET_assert (GNUNET_OK ==
476 GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
479 GNUNET_free_non_null (m->address);
486 * Task run in monitor mode when the user presses CTRL-C to abort.
487 * Stops monitoring activity.
492 shutdown_task (void *cls)
494 struct GNUNET_TIME_Relative duration;
495 struct ValidationResolutionContext *cur;
496 struct ValidationResolutionContext *next;
497 struct PeerResolutionContext *rc;
500 if (NULL != op_timeout)
502 GNUNET_SCHEDULER_cancel (op_timeout);
507 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
512 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
517 GNUNET_TRANSPORT_monitor_plugins_cancel (pm);
522 for (cur = next; NULL != cur; cur = next)
526 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
527 GNUNET_CONTAINER_DLL_remove (vc_head,
530 GNUNET_free (cur->transport);
531 GNUNET_HELLO_address_free (cur->addrcp);
534 while (NULL != (rc = rc_head))
536 GNUNET_CONTAINER_DLL_remove (rc_head,
539 GNUNET_TRANSPORT_address_to_string_cancel (rc->asc);
540 GNUNET_free (rc->transport);
541 GNUNET_free (rc->addrcp);
546 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
551 GNUNET_TRANSPORT_disconnect (handle);
556 duration = GNUNET_TIME_absolute_get_duration (start_time);
558 _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
559 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
561 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
563 if (benchmark_receive)
565 duration = GNUNET_TIME_absolute_get_duration (start_time);
567 _("Received %llu bytes/s (%llu bytes in %s)\n"),
568 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
570 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
573 if (NULL != monitored_peers)
575 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
576 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
577 monitored_peers = NULL;
579 if (NULL != monitored_plugins)
582 GNUNET_CONTAINER_multipeermap_size (monitored_plugins));
583 GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
584 monitored_plugins = NULL;
586 if (NULL != blacklist)
588 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
596 * We are done, shut down.
599 operation_timeout (void *cls)
601 struct PeerResolutionContext *cur;
602 struct PeerResolutionContext *next;
605 if ((benchmark_send) || (benchmark_receive))
608 _("Failed to connect to `%s'\n"),
609 GNUNET_i2s_full (&pid));
610 GNUNET_SCHEDULER_shutdown ();
614 if (iterate_connections)
617 while (NULL != (cur = next))
621 _("Failed to resolve address for peer `%s'\n"),
622 GNUNET_i2s (&cur->addrcp->peer));
624 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
625 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
626 GNUNET_free(cur->transport);
627 GNUNET_free(cur->addrcp);
633 _("Failed to list connections, timeout occured\n"));
634 GNUNET_SCHEDULER_shutdown ();
646 * Display the result of the test.
648 * @param tc test context
649 * @param result #GNUNET_YES on success
652 display_test_result (struct TestContext *tc,
653 enum TestResult result)
658 _("Configuration for plugin `%s' did not work!\n"),
661 case NAT_TEST_SUCCESS:
663 _("Configuration for plugin `%s' did work!\n"),
666 case NAT_TEST_INTERNAL_FAIL:
668 _("Internal NAT error while running test for plugin `%s'\n"),
671 case NAT_TEST_FAILED_TO_START:
673 _("Failed to start NAT test for plugin `%s'\n"),
676 case NAT_TEST_TIMEOUT:
678 _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
683 _("Configuration for plugin `%s' did not work!\n"),
689 GNUNET_SCHEDULER_cancel (tc->tsk);
694 GNUNET_NAT_test_stop (tc->tst);
698 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
699 GNUNET_free (tc->name);
702 if ((NULL == head) && (NULL != resolver))
704 GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
706 GNUNET_OS_process_destroy (resolver);
715 * Function called by NAT to report the outcome of the nat-test.
716 * Clean up and update GUI.
718 * @param cls test context
719 * @param result status code
722 result_callback (void *cls,
723 enum GNUNET_NAT_StatusCode result)
725 struct TestContext *tc = cls;
727 display_test_result (tc,
733 * Resolve address we got a validation state for to a string.
735 * @param address the address itself
736 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
737 * @param last_validation when was the address validated last
738 * @param valid_until until when is the address valid
739 * @param next_validation when will we try to revalidate the address next
740 * @param state where are we in the validation state machine
743 resolve_validation_address (const struct GNUNET_HELLO_Address *address,
745 struct GNUNET_TIME_Absolute last_validation,
746 struct GNUNET_TIME_Absolute valid_until,
747 struct GNUNET_TIME_Absolute next_validation,
748 enum GNUNET_TRANSPORT_ValidationState state);
752 * Function to call with a textual representation of an address. This
753 * function will be called several times with different possible
754 * textual representations, and a last time with @a address being NULL
755 * to signal the end of the iteration. Note that @a address NULL
756 * always is the last call, regardless of the value in @a res.
759 * @param address NULL on end of iteration,
760 * otherwise 0-terminated printable UTF-8 string,
761 * in particular an empty string if @a res is #GNUNET_NO
762 * @param res result of the address to string conversion:
763 * if #GNUNET_OK: conversion successful
764 * if #GNUNET_NO: address was invalid (or not supported)
765 * if #GNUNET_SYSERR: communication error (IPC error)
768 process_validation_string (void *cls,
772 struct ValidationResolutionContext *vc = cls;
779 if (GNUNET_SYSERR == res)
782 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
783 GNUNET_i2s (&vc->addrcp->peer),
784 vc->addrcp->transport_name,
785 (unsigned int) vc->addrcp->address_length);
787 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
788 s_valid = GNUNET_strdup ("never");
790 s_valid = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
792 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
793 s_last = GNUNET_strdup ("never");
795 s_last = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
797 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
798 s_next = GNUNET_strdup ("never");
800 s_next = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
803 _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
804 GNUNET_i2s (&vc->addrcp->peer),
805 (GNUNET_OK == res) ? address : "<invalid address>",
806 (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
807 "Valid until : ", s_valid,
808 "Last validation: ",s_last,
809 "Next validation: ", s_next);
810 GNUNET_free (s_valid);
811 GNUNET_free (s_last);
812 GNUNET_free (s_next);
813 vc->printed = GNUNET_YES;
816 /* last call, we are done */
817 GNUNET_assert (address_resolutions > 0);
818 address_resolutions--;
819 if ( (GNUNET_SYSERR == res) &&
820 (GNUNET_NO == vc->printed))
822 if (numeric == GNUNET_NO)
824 /* Failed to resolve address, try numeric lookup
825 (note: this should be unnecessary, as
826 transport should fallback to numeric lookup
827 internally if DNS takes too long anyway) */
828 resolve_validation_address (vc->addrcp,
838 _("Peer `%s' %s `%s' \n"),
839 GNUNET_i2s (&vc->addrcp->peer),
840 "<unable to resolve address>",
841 GNUNET_TRANSPORT_vs2s (vc->state));
844 GNUNET_free (vc->transport);
845 GNUNET_free (vc->addrcp);
846 GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, vc);
848 if ((0 == address_resolutions) && (iterate_validation))
850 if (NULL != op_timeout)
852 GNUNET_SCHEDULER_cancel (op_timeout);
856 GNUNET_SCHEDULER_shutdown ();
862 * Resolve address we got a validation state for to a string.
864 * @param address the address itself
865 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
866 * @param last_validation when was the address validated last
867 * @param valid_until until when is the address valid
868 * @param next_validation when will we try to revalidate the address next
869 * @param state where are we in the validation state machine
872 resolve_validation_address (const struct GNUNET_HELLO_Address *address,
874 struct GNUNET_TIME_Absolute last_validation,
875 struct GNUNET_TIME_Absolute valid_until,
876 struct GNUNET_TIME_Absolute next_validation,
877 enum GNUNET_TRANSPORT_ValidationState state)
879 struct ValidationResolutionContext *vc;
881 vc = GNUNET_new (struct ValidationResolutionContext);
882 GNUNET_assert(NULL != vc);
883 GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
884 address_resolutions++;
886 vc->transport = GNUNET_strdup(address->transport_name);
887 vc->addrcp = GNUNET_HELLO_address_copy (address);
888 vc->printed = GNUNET_NO;
890 vc->last_validation = last_validation;
891 vc->valid_until = valid_until;
892 vc->next_validation = next_validation;
894 /* Resolve address to string */
895 vc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
899 &process_validation_string, vc);
904 * Resolve address we got a validation state for to a string.
907 * @param address the address itself
908 * @param last_validation when was the address validated last
909 * @param valid_until until when is the address valid
910 * @param next_validation when will we try to revalidate the address next
911 * @param state where are we in the validation state machine
914 process_validation_cb (void *cls,
915 const struct GNUNET_HELLO_Address *address,
916 struct GNUNET_TIME_Absolute last_validation,
917 struct GNUNET_TIME_Absolute valid_until,
918 struct GNUNET_TIME_Absolute next_validation,
919 enum GNUNET_TRANSPORT_ValidationState state)
923 if (monitor_validation)
927 _("Monitor disconnected from transport service. Reconnecting.\n"));
931 GNUNET_SCHEDULER_shutdown ();
934 resolve_validation_address (address,
946 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
947 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
949 (uint16_t) head->bnd_port,
950 (uint16_t) head->adv_port);
952 head->tst = GNUNET_NAT_test_start (cfg,
953 (0 == strcasecmp (head->name, "udp"))
954 ? GNUNET_NO : GNUNET_YES,
955 (uint16_t) head->bnd_port,
956 (uint16_t) head->adv_port,
958 &result_callback, head);
963 * Test our plugin's configuration (NAT traversal, etc.).
965 * @param cfg configuration to test
968 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
972 unsigned long long bnd_port;
973 unsigned long long adv_port;
974 struct TestContext *tc;
978 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
981 FPRINTF (stderr, "%s", _
982 ("No transport plugins configured, peer will never communicate\n"));
987 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
989 char section[12 + strlen (tok)];
990 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
992 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
996 _("No port configured for plugin `%s', cannot test it\n"), tok);
999 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
1000 "ADVERTISED_PORT", &adv_port))
1001 adv_port = bnd_port;
1003 tc = GNUNET_new (struct TestContext);
1004 tc->name = GNUNET_strdup (tok);
1005 tc->adv_port = adv_port;
1006 tc->bnd_port = bnd_port;
1007 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
1009 GNUNET_free(plugins);
1011 if ((NULL != head) && (NULL == resolver))
1013 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
1014 resolver = GNUNET_OS_start_process (GNUNET_YES,
1015 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1018 "gnunet-service-resolver", NULL);
1019 if (NULL == resolver)
1021 FPRINTF (stderr, _("Failed to start resolver!\n"));
1025 GNUNET_free(binary);
1026 GNUNET_RESOLVER_connect (cfg);
1033 * Function called to notify a client about the socket
1034 * begin ready to queue more data. @a buf will be
1035 * NULL and @a size zero if the socket was closed for
1036 * writing in the meantime.
1038 * @param cls closure
1039 * @param size number of bytes available in @a buf
1040 * @param buf where the callee should write the message
1041 * @return number of bytes written to @a buf
1044 transmit_data (void *cls,
1048 struct GNUNET_MessageHeader *m = buf;
1050 if ((NULL == buf) || (0 == size))
1056 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
1057 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1058 m->size = ntohs (size);
1059 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
1060 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
1061 traffic_sent += size;
1062 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
1064 GNUNET_TIME_UNIT_FOREVER_REL,
1065 &transmit_data, NULL);
1067 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
1074 * Function called to notify transport users that another
1075 * peer connected to us.
1077 * @param cls closure
1078 * @param peer the peer that connected
1081 notify_connect (void *cls,
1082 const struct GNUNET_PeerIdentity *peer)
1084 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1089 if (NULL != op_timeout)
1091 GNUNET_SCHEDULER_cancel (op_timeout);
1096 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
1097 GNUNET_i2s (&pid), BLOCKSIZE);
1098 start_time = GNUNET_TIME_absolute_get ();
1100 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
1102 GNUNET_TIME_UNIT_FOREVER_REL,
1113 * Function called to notify transport users that another
1114 * peer disconnected from us.
1116 * @param cls closure
1117 * @param peer the peer that disconnected
1120 notify_disconnect (void *cls,
1121 const struct GNUNET_PeerIdentity *peer)
1123 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1128 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
1133 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
1136 GNUNET_SCHEDULER_cancel (end);
1143 * Function called to notify transport users that another
1144 * peer connected to us.
1146 * @param cls closure
1147 * @param peer the peer that connected
1150 monitor_notify_connect (void *cls,
1151 const struct GNUNET_PeerIdentity *peer)
1153 monitor_connect_counter++;
1154 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1155 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1158 _("%24s: %-17s %4s (%u connections in total)\n"),
1162 monitor_connect_counter);
1167 * Function called to notify transport users that another
1168 * peer disconnected from us.
1170 * @param cls closure
1171 * @param peer the peer that disconnected
1174 monitor_notify_disconnect (void *cls,
1175 const struct GNUNET_PeerIdentity *peer)
1177 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1178 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1180 GNUNET_assert(monitor_connect_counter > 0);
1181 monitor_connect_counter--;
1184 _("%24s: %-17s %4s (%u connections in total)\n"),
1186 _("Disconnected from"),
1188 monitor_connect_counter);
1193 * Function called by the transport for each received message.
1195 * @param cls closure
1196 * @param peer (claimed) identity of the other peer
1197 * @param message the message
1200 notify_receive (void *cls,
1201 const struct GNUNET_PeerIdentity *peer,
1202 const struct GNUNET_MessageHeader *message)
1204 if (benchmark_receive)
1206 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
1210 _("Received %u bytes from %s\n"),
1211 (unsigned int) ntohs (message->size),
1214 if (traffic_received == 0)
1215 start_time = GNUNET_TIME_absolute_get ();
1216 traffic_received += ntohs (message->size);
1223 * Convert address to a printable format.
1225 * @param address the address
1226 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1227 * to try to use reverse DNS
1228 * @param state state the peer is in
1229 * @param state_timeout when will the peer's state expire
1232 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1234 enum GNUNET_TRANSPORT_PeerState state,
1235 struct GNUNET_TIME_Absolute state_timeout);
1239 print_info (const struct GNUNET_PeerIdentity *id,
1240 const char *transport,
1242 enum GNUNET_TRANSPORT_PeerState state,
1243 struct GNUNET_TIME_Absolute state_timeout)
1246 if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1247 (GNUNET_YES == monitor_connections))
1250 _("Peer `%s': %s %s in state `%s' until %s\n"),
1252 (NULL == transport) ? "<none>" : transport,
1253 (NULL == transport) ? "<none>" : addr,
1254 GNUNET_TRANSPORT_ps2s (state),
1255 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1257 else if ( (GNUNET_YES == iterate_connections) &&
1258 (GNUNET_TRANSPORT_is_connected(state)))
1260 /* Only connected peers, skip state */
1262 _("Peer `%s': %s %s\n"),
1271 * Function called with a textual representation of an address. This
1272 * function will be called several times with different possible
1273 * textual representations, and a last time with @a address being NULL
1274 * to signal the end of the iteration. Note that @a address NULL
1275 * always is the last call, regardless of the value in @a res.
1277 * @param cls closure
1278 * @param address NULL on end of iteration,
1279 * otherwise 0-terminated printable UTF-8 string,
1280 * in particular an empty string if @a res is #GNUNET_NO
1281 * @param res result of the address to string conversion:
1282 * if #GNUNET_OK: conversion successful
1283 * if #GNUNET_NO: address was invalid (or not supported)
1284 * if #GNUNET_SYSERR: communication error (IPC error)
1287 process_peer_string (void *cls,
1288 const char *address,
1291 struct PeerResolutionContext *rc = cls;
1293 if (NULL != address)
1295 if (GNUNET_SYSERR == res)
1298 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
1299 GNUNET_i2s (&rc->addrcp->peer),
1300 rc->addrcp->transport_name,
1301 (unsigned int) rc->addrcp->address_length);
1302 print_info (&rc->addrcp->peer,
1307 rc->printed = GNUNET_YES;
1310 if (GNUNET_OK == res)
1312 print_info (&rc->addrcp->peer,
1317 rc->printed = GNUNET_YES;
1318 return; /* Wait for done call */
1320 /* GNUNET_NO == res: ignore, was simply not supported */
1323 /* NULL == address, last call, we are done */
1326 GNUNET_assert (address_resolutions > 0);
1327 address_resolutions--;
1328 if (GNUNET_NO == rc->printed)
1330 if (numeric == GNUNET_NO)
1332 /* Failed to resolve address, try numeric lookup
1333 (note: this should not be needed, as transport
1334 should fallback to numeric conversion if DNS takes
1336 resolve_peer_address (rc->addrcp,
1343 print_info (&rc->addrcp->peer,
1350 GNUNET_free (rc->transport);
1351 GNUNET_free (rc->addrcp);
1352 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1354 if ((0 == address_resolutions) && (iterate_connections))
1356 if (NULL != op_timeout)
1358 GNUNET_SCHEDULER_cancel (op_timeout);
1362 GNUNET_SCHEDULER_shutdown ();
1368 * Convert address to a printable format and print it
1369 * together with the given state data.
1371 * @param address the address
1372 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1373 * to try to use reverse DNS
1374 * @param state state the peer is in
1375 * @param state_timeout when will the peer's state expire
1378 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1380 enum GNUNET_TRANSPORT_PeerState state,
1381 struct GNUNET_TIME_Absolute state_timeout)
1383 struct PeerResolutionContext *rc;
1385 rc = GNUNET_new (struct PeerResolutionContext);
1386 GNUNET_CONTAINER_DLL_insert (rc_head,
1389 address_resolutions++;
1390 rc->transport = GNUNET_strdup (address->transport_name);
1391 rc->addrcp = GNUNET_HELLO_address_copy (address);
1392 rc->printed = GNUNET_NO;
1394 rc->state_timeout = state_timeout;
1395 /* Resolve address to string */
1396 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1400 &process_peer_string, rc);
1405 * Function called with information about a peers during a one shot iteration
1407 * @param cls closure
1408 * @param peer identity of the peer, NULL for final callback when operation done
1409 * @param address binary address used to communicate with this peer,
1410 * NULL on disconnect or when done
1411 * @param state current state this peer is in
1412 * @param state_timeout time out for the current state
1415 process_peer_iteration_cb (void *cls,
1416 const struct GNUNET_PeerIdentity *peer,
1417 const struct GNUNET_HELLO_Address *address,
1418 enum GNUNET_TRANSPORT_PeerState state,
1419 struct GNUNET_TIME_Absolute state_timeout)
1428 if ( (GNUNET_NO == iterate_all) &&
1429 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1430 return; /* Display only connected peers */
1432 if (NULL != op_timeout)
1433 GNUNET_SCHEDULER_cancel (op_timeout);
1434 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1438 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1439 "Received address for peer `%s': %s\n",
1441 address ? address->transport_name : "");
1443 if (NULL != address)
1444 resolve_peer_address (address,
1458 * Context for address resolution by #plugin_monitoring_cb().
1460 struct PluginMonitorAddress
1464 * Ongoing resolution request.
1466 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
1469 * Resolved address as string.
1474 * Last event we got and did not yet print because
1475 * @e str was NULL (address not yet resolved).
1477 struct GNUNET_TRANSPORT_SessionInfo si;
1482 * Print information about a plugin monitoring event.
1484 * @param addr out internal context
1485 * @param info the monitoring information
1488 print_plugin_event_info (struct PluginMonitorAddress *addr,
1489 const struct GNUNET_TRANSPORT_SessionInfo *info)
1493 switch (info->state)
1495 case GNUNET_TRANSPORT_SS_INIT:
1498 case GNUNET_TRANSPORT_SS_HANDSHAKE:
1499 state = "HANDSHAKE";
1501 case GNUNET_TRANSPORT_SS_UP:
1504 case GNUNET_TRANSPORT_SS_UPDATE:
1507 case GNUNET_TRANSPORT_SS_DONE:
1515 "%s: state %s timeout in %s @ %s%s\n",
1516 GNUNET_i2s (&info->address->peer),
1518 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
1521 (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
1523 "%s: queue has %3u messages and %6u bytes\n",
1524 GNUNET_i2s (&info->address->peer),
1525 info->num_msg_pending,
1526 info->num_bytes_pending);
1527 if (0 != GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
1529 "%s: receiving blocked until %s\n",
1530 GNUNET_i2s (&info->address->peer),
1531 GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
1536 * Function called with a textual representation of an address. This
1537 * function will be called several times with different possible
1538 * textual representations, and a last time with @a address being NULL
1539 * to signal the end of the iteration. Note that @a address NULL
1540 * always is the last call, regardless of the value in @a res.
1542 * @param cls closure
1543 * @param address NULL on end of iteration,
1544 * otherwise 0-terminated printable UTF-8 string,
1545 * in particular an empty string if @a res is #GNUNET_NO
1546 * @param res result of the address to string conversion:
1547 * if #GNUNET_OK: conversion successful
1548 * if #GNUNET_NO: address was invalid (or not supported)
1549 * if #GNUNET_SYSERR: communication error (IPC error)
1552 address_cb (void *cls,
1553 const char *address,
1556 struct PluginMonitorAddress *addr = cls;
1558 if (NULL == address)
1563 if (NULL != addr->str)
1565 addr->str = GNUNET_strdup (address);
1566 print_plugin_event_info (addr,
1572 * Function called by the plugin with information about the
1573 * current sessions managed by the plugin (for monitoring).
1575 * @param cls closure (NULL)
1576 * @param session session handle this information is about,
1577 * NULL to indicate that we are "in sync" (initial
1578 * iteration complete)
1579 * @param session_ctx storage location where the application
1580 * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1581 * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1582 * @param info information about the state of the session,
1583 * NULL if @a session is also NULL and we are
1584 * merely signalling that the initial iteration is over;
1585 * NULL with @a session being non-NULL if the monitor
1586 * was being cancelled while sessions were active
1589 plugin_monitoring_cb (void *cls,
1590 struct GNUNET_TRANSPORT_PluginSession *session,
1592 const struct GNUNET_TRANSPORT_SessionInfo *info)
1594 struct PluginMonitorAddress *addr;
1596 if ( (NULL == info) &&
1598 return; /* in sync with transport service */
1599 addr = *session_ctx;
1604 if (NULL != addr->asc)
1606 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1609 GNUNET_free_non_null (addr->str);
1611 *session_ctx = NULL;
1613 return; /* shutdown */
1615 if ( (NULL != cpid) &&
1616 (0 != memcmp (&info->address->peer,
1618 sizeof (struct GNUNET_PeerIdentity))) )
1619 return; /* filtered */
1622 addr = GNUNET_new (struct PluginMonitorAddress);
1623 addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1626 GNUNET_TIME_UNIT_FOREVER_REL,
1629 *session_ctx = addr;
1631 if (NULL == addr->str)
1634 print_plugin_event_info (addr,
1636 if (GNUNET_TRANSPORT_SS_DONE == info->state)
1638 if (NULL != addr->asc)
1640 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1643 GNUNET_free_non_null (addr->str);
1645 *session_ctx = NULL;
1651 * Function called with information about a peers
1653 * @param cls closure, NULL
1654 * @param peer identity of the peer, NULL for final callback when operation done
1655 * @param address binary address used to communicate with this peer,
1656 * NULL on disconnect or when done
1657 * @param state current state this peer is in
1658 * @param state_timeout time out for the current state
1661 process_peer_monitoring_cb (void *cls,
1662 const struct GNUNET_PeerIdentity *peer,
1663 const struct GNUNET_HELLO_Address *address,
1664 enum GNUNET_TRANSPORT_PeerState state,
1665 struct GNUNET_TIME_Absolute state_timeout)
1667 struct MonitoredPeer *m;
1673 _("Monitor disconnected from transport service. Reconnecting.\n"));
1677 if (NULL != op_timeout)
1678 GNUNET_SCHEDULER_cancel (op_timeout);
1679 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1683 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1686 m = GNUNET_new (struct MonitoredPeer);
1687 GNUNET_CONTAINER_multipeermap_put (monitored_peers,
1690 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1694 if ( (m->state == state) &&
1695 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1696 (NULL == address) &&
1697 (NULL == m->address) )
1699 return; /* No real change */
1701 if ( (m->state == state) &&
1702 (NULL != address) &&
1703 (NULL != m->address) &&
1704 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1705 return; /* No real change */
1708 if (NULL != m->address)
1710 GNUNET_free (m->address);
1713 if (NULL != address)
1714 m->address = GNUNET_HELLO_address_copy (address);
1716 m->state_timeout = state_timeout;
1718 if (NULL != address)
1719 resolve_peer_address (m->address,
1733 * Function called with the transport service checking if we
1734 * want to blacklist a peer. Return #GNUNET_SYSERR for the
1735 * peer that we should disconnect from.
1738 * @param cpid peer to check blacklisting for
1739 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1742 blacklist_cb (void *cls,
1743 const struct GNUNET_PeerIdentity *cpid)
1745 if (0 == memcmp (cpid,
1747 sizeof (struct GNUNET_PeerIdentity)))
1748 return GNUNET_SYSERR;
1754 * Function called with the result of the check if the 'transport'
1755 * service is running.
1757 * @param cls closure with our configuration
1758 * @param result #GNUNET_YES if transport is running
1761 testservice_task (void *cls,
1767 if (GNUNET_YES != result)
1769 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1773 if ( (NULL != cpid) &&
1775 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
1780 _("Failed to parse peer identity `%s'\n"),
1785 counter = benchmark_send + benchmark_receive + iterate_connections
1786 + monitor_connections + monitor_connects + do_disconnect +
1787 + iterate_validation + monitor_validation + monitor_plugins;
1792 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1793 "disconnect", "benchmark send", "benchmark receive", "information",
1794 "monitor", "events", "plugins");
1800 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1801 "disconnect", "benchmark send", "benchmark receive", "information",
1802 "monitor", "events", "plugins");
1806 if (do_disconnect) /* -D: Disconnect from peer */
1811 _("Option `%s' makes no sense without option `%s'.\n"),
1816 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1819 if (NULL == blacklist)
1823 _("Failed to connect to transport service for disconnection\n"));
1829 _("Blacklisting request in place, stop with CTRL-C\n"));
1831 else if (benchmark_send) /* -s: Benchmark sending */
1835 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1840 handle = GNUNET_TRANSPORT_connect (cfg,
1845 ¬ify_disconnect);
1848 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1852 start_time = GNUNET_TIME_absolute_get ();
1853 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1857 else if (benchmark_receive) /* -b: Benchmark receiving */
1859 handle = GNUNET_TRANSPORT_connect (cfg,
1867 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1872 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
1873 start_time = GNUNET_TIME_absolute_get ();
1876 else if (iterate_connections) /* -i: List information about peers once */
1878 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1879 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1880 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1884 else if (monitor_connections) /* -m: List information about peers continuously */
1886 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
1888 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1889 (NULL == cpid) ? NULL : &pid,
1892 &process_peer_monitoring_cb, NULL);
1894 else if (monitor_plugins) /* -P: List information about plugins continuously */
1896 monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1897 pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
1898 &plugin_monitoring_cb,
1901 else if (iterate_validation) /* -d: Print information about validations */
1903 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1904 (NULL == cpid) ? NULL : &pid,
1905 GNUNET_YES, TIMEOUT,
1906 &process_validation_cb, (void *) cfg);
1908 else if (monitor_validation) /* -f: Print information about validations continuously */
1910 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1911 (NULL == cpid) ? NULL : &pid,
1913 &process_validation_cb, (void *) cfg);
1915 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1917 monitor_connect_counter = 0;
1918 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1919 &monitor_notify_connect,
1920 &monitor_notify_disconnect);
1925 _("Failed to connect to transport service\n"));
1937 GNUNET_SCHEDULER_add_shutdown (&shutdown_task,
1943 * Main function that will be run by the scheduler.
1945 * @param cls closure
1946 * @param args remaining command-line arguments
1947 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1948 * @param mycfg configuration
1953 const char *cfgfile,
1954 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1956 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1957 if (test_configuration)
1959 do_test_configuration (cfg);
1962 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1963 &testservice_task, (void *) cfg);
1967 main (int argc, char * const *argv)
1970 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1972 gettext_noop ("print information for all peers (instead of only connected peers)"),
1973 0, &GNUNET_GETOPT_set_one, &iterate_all },
1974 { 'b', "benchmark", NULL,
1975 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1976 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1977 { 'D', "disconnect",
1978 NULL, gettext_noop ("disconnect to a peer"), 0,
1979 &GNUNET_GETOPT_set_one, &do_disconnect },
1980 { 'd', "validation", NULL,
1981 gettext_noop ("print information for all pending validations "),
1982 0, &GNUNET_GETOPT_set_one, &iterate_validation },
1983 { 'f', "monitorvalidation", NULL,
1984 gettext_noop ("print information for all pending validations continuously"),
1985 0, &GNUNET_GETOPT_set_one, &monitor_validation },
1986 { 'i', "information", NULL,
1987 gettext_noop ("provide information about all current connections (once)"),
1988 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1989 { 'm', "monitor", NULL,
1990 gettext_noop ("provide information about all current connections (continuously)"),
1991 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1992 { 'e', "events", NULL,
1993 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1994 0, &GNUNET_GETOPT_set_one, &monitor_connects },
1996 NULL, gettext_noop ("do not resolve hostnames"), 0,
1997 &GNUNET_GETOPT_set_one, &numeric },
1998 { 'p', "peer", "PEER",
1999 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
2001 { 'P', "plugins", NULL,
2002 gettext_noop ("monitor plugin sessions"), 0, &GNUNET_GETOPT_set_one,
2004 { 's', "send", NULL, gettext_noop
2005 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
2006 &GNUNET_GETOPT_set_one, &benchmark_send },
2007 { 't', "test", NULL,
2008 gettext_noop ("test transport configuration (involves external server)"),
2009 0, &GNUNET_GETOPT_set_one, &test_configuration },
2010 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
2011 GNUNET_GETOPT_OPTION_END
2014 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2017 res = GNUNET_PROGRAM_run (argc, argv,
2019 gettext_noop ("Direct access to transport service."),
2022 GNUNET_free((void *) argv);
2023 if (GNUNET_OK == res)
2028 /* end of gnunet-transport.c */