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));
611 GNUNET_SCHEDULER_cancel (end);
612 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
616 if (iterate_connections)
619 while (NULL != (cur = next))
623 _("Failed to resolve address for peer `%s'\n"),
624 GNUNET_i2s (&cur->addrcp->peer));
626 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
627 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
628 GNUNET_free(cur->transport);
629 GNUNET_free(cur->addrcp);
635 _("Failed to list connections, timeout occured\n"));
637 GNUNET_SCHEDULER_cancel (end);
638 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
650 * Display the result of the test.
652 * @param tc test context
653 * @param result #GNUNET_YES on success
656 display_test_result (struct TestContext *tc,
657 enum TestResult result)
662 _("Configuration for plugin `%s' did not work!\n"),
665 case NAT_TEST_SUCCESS:
667 _("Configuration for plugin `%s' did work!\n"),
670 case NAT_TEST_INTERNAL_FAIL:
672 _("Internal NAT error while running test for plugin `%s'\n"),
675 case NAT_TEST_FAILED_TO_START:
677 _("Failed to start NAT test for plugin `%s'\n"),
680 case NAT_TEST_TIMEOUT:
682 _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
687 _("Configuration for plugin `%s' did not work!\n"),
693 GNUNET_SCHEDULER_cancel (tc->tsk);
698 GNUNET_NAT_test_stop (tc->tst);
702 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
703 GNUNET_free (tc->name);
706 if ((NULL == head) && (NULL != resolver))
708 GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
710 GNUNET_OS_process_destroy (resolver);
719 * Function called by NAT to report the outcome of the nat-test.
720 * Clean up and update GUI.
722 * @param cls test context
723 * @param result status code
726 result_callback (void *cls,
727 enum GNUNET_NAT_StatusCode result)
729 struct TestContext *tc = cls;
731 display_test_result (tc,
737 * Resolve address we got a validation state for to a string.
739 * @param address the address itself
740 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
741 * @param last_validation when was the address validated last
742 * @param valid_until until when is the address valid
743 * @param next_validation when will we try to revalidate the address next
744 * @param state where are we in the validation state machine
747 resolve_validation_address (const struct GNUNET_HELLO_Address *address,
749 struct GNUNET_TIME_Absolute last_validation,
750 struct GNUNET_TIME_Absolute valid_until,
751 struct GNUNET_TIME_Absolute next_validation,
752 enum GNUNET_TRANSPORT_ValidationState state);
756 * Function to call with a textual representation of an address. This
757 * function will be called several times with different possible
758 * textual representations, and a last time with @a address being NULL
759 * to signal the end of the iteration. Note that @a address NULL
760 * always is the last call, regardless of the value in @a res.
763 * @param address NULL on end of iteration,
764 * otherwise 0-terminated printable UTF-8 string,
765 * in particular an empty string if @a res is #GNUNET_NO
766 * @param res result of the address to string conversion:
767 * if #GNUNET_OK: conversion successful
768 * if #GNUNET_NO: address was invalid (or not supported)
769 * if #GNUNET_SYSERR: communication error (IPC error)
772 process_validation_string (void *cls,
776 struct ValidationResolutionContext *vc = cls;
783 if (GNUNET_SYSERR == res)
786 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
787 GNUNET_i2s (&vc->addrcp->peer),
788 vc->addrcp->transport_name,
789 (unsigned int) vc->addrcp->address_length);
791 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
792 s_valid = GNUNET_strdup ("never");
794 s_valid = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
796 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
797 s_last = GNUNET_strdup ("never");
799 s_last = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
801 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
802 s_next = GNUNET_strdup ("never");
804 s_next = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
807 _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
808 GNUNET_i2s (&vc->addrcp->peer),
809 (GNUNET_OK == res) ? address : "<invalid address>",
810 (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
811 "Valid until : ", s_valid,
812 "Last validation: ",s_last,
813 "Next validation: ", s_next);
814 GNUNET_free (s_valid);
815 GNUNET_free (s_last);
816 GNUNET_free (s_next);
817 vc->printed = GNUNET_YES;
820 /* last call, we are done */
821 GNUNET_assert (address_resolutions > 0);
822 address_resolutions--;
823 if ( (GNUNET_SYSERR == res) &&
824 (GNUNET_NO == vc->printed))
826 if (numeric == GNUNET_NO)
828 /* Failed to resolve address, try numeric lookup
829 (note: this should be unnecessary, as
830 transport should fallback to numeric lookup
831 internally if DNS takes too long anyway) */
832 resolve_validation_address (vc->addrcp,
842 _("Peer `%s' %s `%s' \n"),
843 GNUNET_i2s (&vc->addrcp->peer),
844 "<unable to resolve address>",
845 GNUNET_TRANSPORT_vs2s (vc->state));
848 GNUNET_free (vc->transport);
849 GNUNET_free (vc->addrcp);
850 GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, vc);
852 if ((0 == address_resolutions) && (iterate_validation))
856 GNUNET_SCHEDULER_cancel (end);
859 if (NULL != op_timeout)
861 GNUNET_SCHEDULER_cancel (op_timeout);
865 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
871 * Resolve address we got a validation state for to a string.
873 * @param address the address itself
874 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
875 * @param last_validation when was the address validated last
876 * @param valid_until until when is the address valid
877 * @param next_validation when will we try to revalidate the address next
878 * @param state where are we in the validation state machine
881 resolve_validation_address (const struct GNUNET_HELLO_Address *address,
883 struct GNUNET_TIME_Absolute last_validation,
884 struct GNUNET_TIME_Absolute valid_until,
885 struct GNUNET_TIME_Absolute next_validation,
886 enum GNUNET_TRANSPORT_ValidationState state)
888 struct ValidationResolutionContext *vc;
890 vc = GNUNET_new (struct ValidationResolutionContext);
891 GNUNET_assert(NULL != vc);
892 GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
893 address_resolutions++;
895 vc->transport = GNUNET_strdup(address->transport_name);
896 vc->addrcp = GNUNET_HELLO_address_copy (address);
897 vc->printed = GNUNET_NO;
899 vc->last_validation = last_validation;
900 vc->valid_until = valid_until;
901 vc->next_validation = next_validation;
903 /* Resolve address to string */
904 vc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
908 &process_validation_string, vc);
913 * Resolve address we got a validation state for to a string.
916 * @param address the address itself
917 * @param last_validation when was the address validated last
918 * @param valid_until until when is the address valid
919 * @param next_validation when will we try to revalidate the address next
920 * @param state where are we in the validation state machine
923 process_validation_cb (void *cls,
924 const struct GNUNET_HELLO_Address *address,
925 struct GNUNET_TIME_Absolute last_validation,
926 struct GNUNET_TIME_Absolute valid_until,
927 struct GNUNET_TIME_Absolute next_validation,
928 enum GNUNET_TRANSPORT_ValidationState state)
932 if (monitor_validation)
936 _("Monitor disconnected from transport service. Reconnecting.\n"));
941 GNUNET_SCHEDULER_cancel (end);
942 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
945 resolve_validation_address (address,
957 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
958 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
960 (uint16_t) head->bnd_port,
961 (uint16_t) head->adv_port);
963 head->tst = GNUNET_NAT_test_start (cfg,
964 (0 == strcasecmp (head->name, "udp"))
965 ? GNUNET_NO : GNUNET_YES,
966 (uint16_t) head->bnd_port,
967 (uint16_t) head->adv_port,
969 &result_callback, head);
974 * Test our plugin's configuration (NAT traversal, etc.).
976 * @param cfg configuration to test
979 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
983 unsigned long long bnd_port;
984 unsigned long long adv_port;
985 struct TestContext *tc;
989 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
992 FPRINTF (stderr, "%s", _
993 ("No transport plugins configured, peer will never communicate\n"));
998 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
1000 char section[12 + strlen (tok)];
1001 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
1003 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
1007 _("No port configured for plugin `%s', cannot test it\n"), tok);
1010 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
1011 "ADVERTISED_PORT", &adv_port))
1012 adv_port = bnd_port;
1014 tc = GNUNET_new (struct TestContext);
1015 tc->name = GNUNET_strdup (tok);
1016 tc->adv_port = adv_port;
1017 tc->bnd_port = bnd_port;
1018 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
1020 GNUNET_free(plugins);
1022 if ((NULL != head) && (NULL == resolver))
1024 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
1025 resolver = GNUNET_OS_start_process (GNUNET_YES,
1026 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1029 "gnunet-service-resolver", NULL);
1030 if (NULL == resolver)
1032 FPRINTF (stderr, _("Failed to start resolver!\n"));
1036 GNUNET_free(binary);
1037 GNUNET_RESOLVER_connect (cfg);
1044 * Function called to notify a client about the socket
1045 * begin ready to queue more data. @a buf will be
1046 * NULL and @a size zero if the socket was closed for
1047 * writing in the meantime.
1049 * @param cls closure
1050 * @param size number of bytes available in @a buf
1051 * @param buf where the callee should write the message
1052 * @return number of bytes written to @a buf
1055 transmit_data (void *cls,
1059 struct GNUNET_MessageHeader *m = buf;
1061 if ((NULL == buf) || (0 == size))
1067 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
1068 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1069 m->size = ntohs (size);
1070 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
1071 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
1072 traffic_sent += size;
1073 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
1075 GNUNET_TIME_UNIT_FOREVER_REL,
1076 &transmit_data, NULL);
1078 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
1085 * Function called to notify transport users that another
1086 * peer connected to us.
1088 * @param cls closure
1089 * @param peer the peer that connected
1092 notify_connect (void *cls,
1093 const struct GNUNET_PeerIdentity *peer)
1095 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1100 if (NULL != op_timeout)
1102 GNUNET_SCHEDULER_cancel (op_timeout);
1107 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
1108 GNUNET_i2s (&pid), BLOCKSIZE);
1109 start_time = GNUNET_TIME_absolute_get ();
1111 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
1113 GNUNET_TIME_UNIT_FOREVER_REL,
1124 * Function called to notify transport users that another
1125 * peer disconnected from us.
1127 * @param cls closure
1128 * @param peer the peer that disconnected
1131 notify_disconnect (void *cls,
1132 const struct GNUNET_PeerIdentity *peer)
1134 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1139 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
1144 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
1147 GNUNET_SCHEDULER_cancel (end);
1154 * Function called to notify transport users that another
1155 * peer connected to us.
1157 * @param cls closure
1158 * @param peer the peer that connected
1161 monitor_notify_connect (void *cls,
1162 const struct GNUNET_PeerIdentity *peer)
1164 monitor_connect_counter++;
1165 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1166 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1169 _("%24s: %-17s %4s (%u connections in total)\n"),
1173 monitor_connect_counter);
1178 * Function called to notify transport users that another
1179 * peer disconnected from us.
1181 * @param cls closure
1182 * @param peer the peer that disconnected
1185 monitor_notify_disconnect (void *cls,
1186 const struct GNUNET_PeerIdentity *peer)
1188 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1189 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1191 GNUNET_assert(monitor_connect_counter > 0);
1192 monitor_connect_counter--;
1195 _("%24s: %-17s %4s (%u connections in total)\n"),
1197 _("Disconnected from"),
1199 monitor_connect_counter);
1204 * Function called by the transport for each received message.
1206 * @param cls closure
1207 * @param peer (claimed) identity of the other peer
1208 * @param message the message
1211 notify_receive (void *cls,
1212 const struct GNUNET_PeerIdentity *peer,
1213 const struct GNUNET_MessageHeader *message)
1215 if (benchmark_receive)
1217 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
1221 _("Received %u bytes from %s\n"),
1222 (unsigned int) ntohs (message->size),
1225 if (traffic_received == 0)
1226 start_time = GNUNET_TIME_absolute_get ();
1227 traffic_received += ntohs (message->size);
1234 * Convert address to a printable format.
1236 * @param address the address
1237 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1238 * to try to use reverse DNS
1239 * @param state state the peer is in
1240 * @param state_timeout when will the peer's state expire
1243 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1245 enum GNUNET_TRANSPORT_PeerState state,
1246 struct GNUNET_TIME_Absolute state_timeout);
1250 print_info (const struct GNUNET_PeerIdentity *id,
1251 const char *transport,
1253 enum GNUNET_TRANSPORT_PeerState state,
1254 struct GNUNET_TIME_Absolute state_timeout)
1257 if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1258 (GNUNET_YES == monitor_connections))
1261 _("Peer `%s': %s %s in state `%s' until %s\n"),
1263 (NULL == transport) ? "<none>" : transport,
1264 (NULL == transport) ? "<none>" : addr,
1265 GNUNET_TRANSPORT_ps2s (state),
1266 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1268 else if ( (GNUNET_YES == iterate_connections) &&
1269 (GNUNET_TRANSPORT_is_connected(state)))
1271 /* Only connected peers, skip state */
1273 _("Peer `%s': %s %s\n"),
1282 * Function called with a textual representation of an address. This
1283 * function will be called several times with different possible
1284 * textual representations, and a last time with @a address being NULL
1285 * to signal the end of the iteration. Note that @a address NULL
1286 * always is the last call, regardless of the value in @a res.
1288 * @param cls closure
1289 * @param address NULL on end of iteration,
1290 * otherwise 0-terminated printable UTF-8 string,
1291 * in particular an empty string if @a res is #GNUNET_NO
1292 * @param res result of the address to string conversion:
1293 * if #GNUNET_OK: conversion successful
1294 * if #GNUNET_NO: address was invalid (or not supported)
1295 * if #GNUNET_SYSERR: communication error (IPC error)
1298 process_peer_string (void *cls,
1299 const char *address,
1302 struct PeerResolutionContext *rc = cls;
1304 if (NULL != address)
1306 if (GNUNET_SYSERR == res)
1309 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
1310 GNUNET_i2s (&rc->addrcp->peer),
1311 rc->addrcp->transport_name,
1312 (unsigned int) rc->addrcp->address_length);
1313 print_info (&rc->addrcp->peer,
1318 rc->printed = GNUNET_YES;
1321 if (GNUNET_OK == res)
1323 print_info (&rc->addrcp->peer,
1328 rc->printed = GNUNET_YES;
1329 return; /* Wait for done call */
1331 /* GNUNET_NO == res: ignore, was simply not supported */
1334 /* NULL == address, last call, we are done */
1337 GNUNET_assert (address_resolutions > 0);
1338 address_resolutions--;
1339 if (GNUNET_NO == rc->printed)
1341 if (numeric == GNUNET_NO)
1343 /* Failed to resolve address, try numeric lookup
1344 (note: this should not be needed, as transport
1345 should fallback to numeric conversion if DNS takes
1347 resolve_peer_address (rc->addrcp,
1354 print_info (&rc->addrcp->peer,
1361 GNUNET_free (rc->transport);
1362 GNUNET_free (rc->addrcp);
1363 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1365 if ((0 == address_resolutions) && (iterate_connections))
1369 GNUNET_SCHEDULER_cancel (end);
1372 if (NULL != op_timeout)
1374 GNUNET_SCHEDULER_cancel (op_timeout);
1378 end = GNUNET_SCHEDULER_add_now (&shutdown_task,
1385 * Convert address to a printable format and print it
1386 * together with the given state data.
1388 * @param address the address
1389 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1390 * to try to use reverse DNS
1391 * @param state state the peer is in
1392 * @param state_timeout when will the peer's state expire
1395 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1397 enum GNUNET_TRANSPORT_PeerState state,
1398 struct GNUNET_TIME_Absolute state_timeout)
1400 struct PeerResolutionContext *rc;
1402 rc = GNUNET_new (struct PeerResolutionContext);
1403 GNUNET_CONTAINER_DLL_insert (rc_head,
1406 address_resolutions++;
1407 rc->transport = GNUNET_strdup (address->transport_name);
1408 rc->addrcp = GNUNET_HELLO_address_copy (address);
1409 rc->printed = GNUNET_NO;
1411 rc->state_timeout = state_timeout;
1412 /* Resolve address to string */
1413 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1417 &process_peer_string, rc);
1422 * Function called with information about a peers during a one shot iteration
1424 * @param cls closure
1425 * @param peer identity of the peer, NULL for final callback when operation done
1426 * @param address binary address used to communicate with this peer,
1427 * NULL on disconnect or when done
1428 * @param state current state this peer is in
1429 * @param state_timeout time out for the current state
1432 process_peer_iteration_cb (void *cls,
1433 const struct GNUNET_PeerIdentity *peer,
1434 const struct GNUNET_HELLO_Address *address,
1435 enum GNUNET_TRANSPORT_PeerState state,
1436 struct GNUNET_TIME_Absolute state_timeout)
1445 if ( (GNUNET_NO == iterate_all) &&
1446 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1447 return; /* Display only connected peers */
1449 if (NULL != op_timeout)
1450 GNUNET_SCHEDULER_cancel (op_timeout);
1451 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Received address for peer `%s': %s\n",
1458 address ? address->transport_name : "");
1460 if (NULL != address)
1461 resolve_peer_address (address,
1475 * Context for address resolution by #plugin_monitoring_cb().
1477 struct PluginMonitorAddress
1481 * Ongoing resolution request.
1483 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
1486 * Resolved address as string.
1491 * Last event we got and did not yet print because
1492 * @e str was NULL (address not yet resolved).
1494 struct GNUNET_TRANSPORT_SessionInfo si;
1499 * Print information about a plugin monitoring event.
1501 * @param addr out internal context
1502 * @param info the monitoring information
1505 print_plugin_event_info (struct PluginMonitorAddress *addr,
1506 const struct GNUNET_TRANSPORT_SessionInfo *info)
1510 switch (info->state)
1512 case GNUNET_TRANSPORT_SS_INIT:
1515 case GNUNET_TRANSPORT_SS_HANDSHAKE:
1516 state = "HANDSHAKE";
1518 case GNUNET_TRANSPORT_SS_UP:
1521 case GNUNET_TRANSPORT_SS_UPDATE:
1524 case GNUNET_TRANSPORT_SS_DONE:
1532 "%s: state %s timeout in %s @ %s%s\n",
1533 GNUNET_i2s (&info->address->peer),
1535 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
1538 (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
1540 "%s: queue has %3u messages and %6u bytes\n",
1541 GNUNET_i2s (&info->address->peer),
1542 info->num_msg_pending,
1543 info->num_bytes_pending);
1544 if (0 != GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
1546 "%s: receiving blocked until %s\n",
1547 GNUNET_i2s (&info->address->peer),
1548 GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
1553 * Function called with a textual representation of an address. This
1554 * function will be called several times with different possible
1555 * textual representations, and a last time with @a address being NULL
1556 * to signal the end of the iteration. Note that @a address NULL
1557 * always is the last call, regardless of the value in @a res.
1559 * @param cls closure
1560 * @param address NULL on end of iteration,
1561 * otherwise 0-terminated printable UTF-8 string,
1562 * in particular an empty string if @a res is #GNUNET_NO
1563 * @param res result of the address to string conversion:
1564 * if #GNUNET_OK: conversion successful
1565 * if #GNUNET_NO: address was invalid (or not supported)
1566 * if #GNUNET_SYSERR: communication error (IPC error)
1569 address_cb (void *cls,
1570 const char *address,
1573 struct PluginMonitorAddress *addr = cls;
1575 if (NULL == address)
1580 if (NULL != addr->str)
1582 addr->str = GNUNET_strdup (address);
1583 print_plugin_event_info (addr,
1589 * Function called by the plugin with information about the
1590 * current sessions managed by the plugin (for monitoring).
1592 * @param cls closure (NULL)
1593 * @param session session handle this information is about,
1594 * NULL to indicate that we are "in sync" (initial
1595 * iteration complete)
1596 * @param session_ctx storage location where the application
1597 * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1598 * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1599 * @param info information about the state of the session,
1600 * NULL if @a session is also NULL and we are
1601 * merely signalling that the initial iteration is over;
1602 * NULL with @a session being non-NULL if the monitor
1603 * was being cancelled while sessions were active
1606 plugin_monitoring_cb (void *cls,
1607 struct GNUNET_TRANSPORT_PluginSession *session,
1609 const struct GNUNET_TRANSPORT_SessionInfo *info)
1611 struct PluginMonitorAddress *addr;
1613 if ( (NULL == info) &&
1615 return; /* in sync with transport service */
1616 addr = *session_ctx;
1621 if (NULL != addr->asc)
1623 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1626 GNUNET_free_non_null (addr->str);
1628 *session_ctx = NULL;
1630 return; /* shutdown */
1632 if ( (NULL != cpid) &&
1633 (0 != memcmp (&info->address->peer,
1635 sizeof (struct GNUNET_PeerIdentity))) )
1636 return; /* filtered */
1639 addr = GNUNET_new (struct PluginMonitorAddress);
1640 addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1643 GNUNET_TIME_UNIT_FOREVER_REL,
1646 *session_ctx = addr;
1648 if (NULL == addr->str)
1651 print_plugin_event_info (addr,
1653 if (GNUNET_TRANSPORT_SS_DONE == info->state)
1655 if (NULL != addr->asc)
1657 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1660 GNUNET_free_non_null (addr->str);
1662 *session_ctx = NULL;
1668 * Function called with information about a peers
1670 * @param cls closure, NULL
1671 * @param peer identity of the peer, NULL for final callback when operation done
1672 * @param address binary address used to communicate with this peer,
1673 * NULL on disconnect or when done
1674 * @param state current state this peer is in
1675 * @param state_timeout time out for the current state
1678 process_peer_monitoring_cb (void *cls,
1679 const struct GNUNET_PeerIdentity *peer,
1680 const struct GNUNET_HELLO_Address *address,
1681 enum GNUNET_TRANSPORT_PeerState state,
1682 struct GNUNET_TIME_Absolute state_timeout)
1684 struct MonitoredPeer *m;
1690 _("Monitor disconnected from transport service. Reconnecting.\n"));
1694 if (NULL != op_timeout)
1695 GNUNET_SCHEDULER_cancel (op_timeout);
1696 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1700 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1703 m = GNUNET_new (struct MonitoredPeer);
1704 GNUNET_CONTAINER_multipeermap_put (monitored_peers,
1707 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1711 if ( (m->state == state) &&
1712 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1713 (NULL == address) &&
1714 (NULL == m->address) )
1716 return; /* No real change */
1718 if ( (m->state == state) &&
1719 (NULL != address) &&
1720 (NULL != m->address) &&
1721 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1722 return; /* No real change */
1725 if (NULL != m->address)
1727 GNUNET_free (m->address);
1730 if (NULL != address)
1731 m->address = GNUNET_HELLO_address_copy (address);
1733 m->state_timeout = state_timeout;
1735 if (NULL != address)
1736 resolve_peer_address (m->address,
1750 * Function called with the transport service checking if we
1751 * want to blacklist a peer. Return #GNUNET_SYSERR for the
1752 * peer that we should disconnect from.
1755 * @param cpid peer to check blacklisting for
1756 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1759 blacklist_cb (void *cls,
1760 const struct GNUNET_PeerIdentity *cpid)
1762 if (0 == memcmp (cpid,
1764 sizeof (struct GNUNET_PeerIdentity)))
1765 return GNUNET_SYSERR;
1771 * Function called with the result of the check if the 'transport'
1772 * service is running.
1774 * @param cls closure with our configuration
1775 * @param result #GNUNET_YES if transport is running
1778 testservice_task (void *cls,
1784 if (GNUNET_YES != result)
1786 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1790 if ( (NULL != cpid) &&
1792 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
1797 _("Failed to parse peer identity `%s'\n"),
1802 counter = benchmark_send + benchmark_receive + iterate_connections
1803 + monitor_connections + monitor_connects + do_disconnect +
1804 + iterate_validation + monitor_validation + monitor_plugins;
1809 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1810 "disconnect", "benchmark send", "benchmark receive", "information",
1811 "monitor", "events", "plugins");
1817 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1818 "disconnect", "benchmark send", "benchmark receive", "information",
1819 "monitor", "events", "plugins");
1823 if (do_disconnect) /* -D: Disconnect from peer */
1828 _("Option `%s' makes no sense without option `%s'.\n"),
1833 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1836 if (NULL == blacklist)
1840 _("Failed to connect to transport service for disconnection\n"));
1846 _("Blacklisting request in place, stop with CTRL-C\n"));
1848 else if (benchmark_send) /* -s: Benchmark sending */
1852 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1857 handle = GNUNET_TRANSPORT_connect (cfg,
1862 ¬ify_disconnect);
1865 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1869 start_time = GNUNET_TIME_absolute_get ();
1870 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1874 else if (benchmark_receive) /* -b: Benchmark receiving */
1876 handle = GNUNET_TRANSPORT_connect (cfg,
1884 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1889 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
1890 start_time = GNUNET_TIME_absolute_get ();
1893 else if (iterate_connections) /* -i: List information about peers once */
1895 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1896 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1897 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1901 else if (monitor_connections) /* -m: List information about peers continuously */
1903 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
1905 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1906 (NULL == cpid) ? NULL : &pid,
1909 &process_peer_monitoring_cb, NULL);
1911 else if (monitor_plugins) /* -P: List information about plugins continuously */
1913 monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1914 pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
1915 &plugin_monitoring_cb,
1918 else if (iterate_validation) /* -d: Print information about validations */
1920 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1921 (NULL == cpid) ? NULL : &pid,
1922 GNUNET_YES, TIMEOUT,
1923 &process_validation_cb, (void *) cfg);
1925 else if (monitor_validation) /* -f: Print information about validations continuously */
1927 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1928 (NULL == cpid) ? NULL : &pid,
1930 &process_validation_cb, (void *) cfg);
1932 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1934 monitor_connect_counter = 0;
1935 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1936 &monitor_notify_connect,
1937 &monitor_notify_disconnect);
1942 _("Failed to connect to transport service\n"));
1954 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1961 * Main function that will be run by the scheduler.
1963 * @param cls closure
1964 * @param args remaining command-line arguments
1965 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1966 * @param mycfg configuration
1971 const char *cfgfile,
1972 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1974 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1975 if (test_configuration)
1977 do_test_configuration (cfg);
1980 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1981 &testservice_task, (void *) cfg);
1985 main (int argc, char * const *argv)
1988 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1990 gettext_noop ("print information for all peers (instead of only connected peers)"),
1991 0, &GNUNET_GETOPT_set_one, &iterate_all },
1992 { 'b', "benchmark", NULL,
1993 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1994 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1995 { 'D', "disconnect",
1996 NULL, gettext_noop ("disconnect to a peer"), 0,
1997 &GNUNET_GETOPT_set_one, &do_disconnect },
1998 { 'd', "validation", NULL,
1999 gettext_noop ("print information for all pending validations "),
2000 0, &GNUNET_GETOPT_set_one, &iterate_validation },
2001 { 'f', "monitorvalidation", NULL,
2002 gettext_noop ("print information for all pending validations continuously"),
2003 0, &GNUNET_GETOPT_set_one, &monitor_validation },
2004 { 'i', "information", NULL,
2005 gettext_noop ("provide information about all current connections (once)"),
2006 0, &GNUNET_GETOPT_set_one, &iterate_connections },
2007 { 'm', "monitor", NULL,
2008 gettext_noop ("provide information about all current connections (continuously)"),
2009 0, &GNUNET_GETOPT_set_one, &monitor_connections },
2010 { 'e', "events", NULL,
2011 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
2012 0, &GNUNET_GETOPT_set_one, &monitor_connects },
2014 NULL, gettext_noop ("do not resolve hostnames"), 0,
2015 &GNUNET_GETOPT_set_one, &numeric },
2016 { 'p', "peer", "PEER",
2017 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
2019 { 'P', "plugins", NULL,
2020 gettext_noop ("monitor plugin sessions"), 0, &GNUNET_GETOPT_set_one,
2022 { 's', "send", NULL, gettext_noop
2023 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
2024 &GNUNET_GETOPT_set_one, &benchmark_send },
2025 { 't', "test", NULL,
2026 gettext_noop ("test transport configuration (involves external server)"),
2027 0, &GNUNET_GETOPT_set_one, &test_configuration },
2028 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
2029 GNUNET_GETOPT_OPTION_END
2032 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2035 res = GNUNET_PROGRAM_run (argc, argv,
2037 gettext_noop ("Direct access to transport service."),
2040 GNUNET_free((void *) argv);
2041 if (GNUNET_OK == res)
2046 /* end of gnunet-transport.c */