2 This file is part of GNUnet.
3 Copyright (C) 2011-2014 Christian Grothoff (and other contributing authors)
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 * Address resolutions pending in progress
432 static unsigned int address_resolution_in_progress;
435 * DLL for NAT Test Contexts: head
437 struct TestContext *head;
440 * DLL for NAT Test Contexts: tail
442 struct TestContext *tail;
445 * DLL: head of validation resolution entries
447 static struct ValidationResolutionContext *vc_head;
450 * DLL: tail of validation resolution entries
452 static struct ValidationResolutionContext *vc_tail;
455 * DLL: head of resolution entries
457 static struct PeerResolutionContext *rc_head;
460 * DLL: head of resolution entries
462 static struct PeerResolutionContext *rc_tail;
466 * Function called to release data stored in the #monitored_peers map.
469 * @param key the peer identity
470 * @param value a `struct MonitoredPeer` to release
471 * @return #GNUNET_OK (continue to iterate)
474 destroy_it (void *cls,
475 const struct GNUNET_PeerIdentity *key,
478 struct MonitoredPeer *m = value;
480 GNUNET_assert (GNUNET_OK ==
481 GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
484 GNUNET_free_non_null (m->address);
491 * Task run in monitor mode when the user presses CTRL-C to abort.
492 * Stops monitoring activity.
495 * @param tc scheduler context
498 shutdown_task (void *cls,
499 const struct GNUNET_SCHEDULER_TaskContext *tc)
501 struct GNUNET_TIME_Relative duration;
502 struct ValidationResolutionContext *cur;
503 struct ValidationResolutionContext *next;
504 struct PeerResolutionContext *rc;
507 if (NULL != op_timeout)
509 GNUNET_SCHEDULER_cancel (op_timeout);
514 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
519 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
524 GNUNET_TRANSPORT_monitor_plugins_cancel (pm);
529 for (cur = next; NULL != cur; cur = next)
533 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
534 GNUNET_CONTAINER_DLL_remove (vc_head,
537 GNUNET_free (cur->transport);
538 GNUNET_HELLO_address_free (cur->addrcp);
541 while (NULL != (rc = rc_head))
543 GNUNET_CONTAINER_DLL_remove (rc_head,
546 GNUNET_TRANSPORT_address_to_string_cancel (rc->asc);
547 GNUNET_free (rc->transport);
548 GNUNET_free (rc->addrcp);
553 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
558 GNUNET_TRANSPORT_disconnect (handle);
563 duration = GNUNET_TIME_absolute_get_duration (start_time);
565 _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
566 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
568 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
570 if (benchmark_receive)
572 duration = GNUNET_TIME_absolute_get_duration (start_time);
574 _("Received %llu bytes/s (%llu bytes in %s)\n"),
575 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
577 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
580 if (NULL != monitored_peers)
582 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
583 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
584 monitored_peers = NULL;
586 if (NULL != monitored_plugins)
589 GNUNET_CONTAINER_multipeermap_size (monitored_plugins));
590 GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
591 monitored_plugins = NULL;
593 if (NULL != blacklist)
595 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
603 * We are done, shut down.
606 operation_timeout (void *cls,
607 const struct GNUNET_SCHEDULER_TaskContext *tc)
609 struct PeerResolutionContext *cur;
610 struct PeerResolutionContext *next;
613 if ((benchmark_send) || (benchmark_receive))
616 _("Failed to connect to `%s'\n"),
617 GNUNET_i2s_full (&pid));
619 GNUNET_SCHEDULER_cancel (end);
620 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
624 if (iterate_connections)
627 while (NULL != (cur = next))
631 _("Failed to resolve address for peer `%s'\n"),
632 GNUNET_i2s (&cur->addrcp->peer));
634 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
635 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
636 GNUNET_free(cur->transport);
637 GNUNET_free(cur->addrcp);
643 _("Failed to list connections, timeout occured\n"));
645 GNUNET_SCHEDULER_cancel (end);
646 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
658 * Display the result of the test.
660 * @param tc test context
661 * @param result #GNUNET_YES on success
664 display_test_result (struct TestContext *tc,
665 enum TestResult result)
670 _("Configuration for plugin `%s' did not work!\n"),
673 case NAT_TEST_SUCCESS:
675 _("Configuration for plugin `%s' did work!\n"),
678 case NAT_TEST_INTERNAL_FAIL:
680 _("Internal NAT error while running test for plugin `%s'\n"),
683 case NAT_TEST_FAILED_TO_START:
685 _("Failed to start NAT test for plugin `%s'\n"),
688 case NAT_TEST_TIMEOUT:
690 _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
695 _("Configuration for plugin `%s' did not work!\n"),
701 GNUNET_SCHEDULER_cancel (tc->tsk);
706 GNUNET_NAT_test_stop (tc->tst);
710 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
711 GNUNET_free (tc->name);
714 if ((NULL == head) && (NULL != resolver))
716 GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
718 GNUNET_OS_process_destroy (resolver);
727 * Function called by NAT to report the outcome of the nat-test.
728 * Clean up and update GUI.
730 * @param cls test context
731 * @param result status code
734 result_callback (void *cls,
735 enum GNUNET_NAT_StatusCode result)
737 struct TestContext *tc = cls;
739 display_test_result (tc,
745 * Resolve address we got a validation state for to a string.
747 * @param address the address itself
748 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
749 * @param last_validation when was the address validated last
750 * @param valid_until until when is the address valid
751 * @param next_validation when will we try to revalidate the address next
752 * @param state where are we in the validation state machine
755 resolve_validation_address (const struct GNUNET_HELLO_Address *address,
757 struct GNUNET_TIME_Absolute last_validation,
758 struct GNUNET_TIME_Absolute valid_until,
759 struct GNUNET_TIME_Absolute next_validation,
760 enum GNUNET_TRANSPORT_ValidationState state);
764 * Function to call with a textual representation of an address. This
765 * function will be called several times with different possible
766 * textual representations, and a last time with @a address being NULL
767 * to signal the end of the iteration. Note that @a address NULL
768 * always is the last call, regardless of the value in @a res.
771 * @param address NULL on end of iteration,
772 * otherwise 0-terminated printable UTF-8 string,
773 * in particular an empty string if @a res is #GNUNET_NO
774 * @param res result of the address to string conversion:
775 * if #GNUNET_OK: conversion successful
776 * if #GNUNET_NO: address was invalid (or not supported)
777 * if #GNUNET_SYSERR: communication error (IPC error)
780 process_validation_string (void *cls,
784 struct ValidationResolutionContext *vc = cls;
791 if (GNUNET_SYSERR == res)
794 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
795 GNUNET_i2s (&vc->addrcp->peer),
796 vc->addrcp->transport_name,
797 (unsigned int) vc->addrcp->address_length);
799 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
800 s_valid = GNUNET_strdup ("never");
802 s_valid = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
804 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
805 s_last = GNUNET_strdup ("never");
807 s_last = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
809 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
810 s_next = GNUNET_strdup ("never");
812 s_next = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
815 _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
816 GNUNET_i2s (&vc->addrcp->peer),
817 (GNUNET_OK == res) ? address : "<invalid address>",
818 (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
819 "Valid until : ", s_valid,
820 "Last validation: ",s_last,
821 "Next validation: ", s_next);
822 GNUNET_free (s_valid);
823 GNUNET_free (s_last);
824 GNUNET_free (s_next);
825 vc->printed = GNUNET_YES;
828 /* last call, we are done */
829 GNUNET_assert (address_resolutions > 0);
830 address_resolutions--;
831 if ( (GNUNET_SYSERR == res) &&
832 (GNUNET_NO == vc->printed))
834 if (numeric == GNUNET_NO)
836 /* Failed to resolve address, try numeric lookup
837 (note: this should be unnecessary, as
838 transport should fallback to numeric lookup
839 internally if DNS takes too long anyway) */
840 resolve_validation_address (vc->addrcp,
850 _("Peer `%s' %s `%s' \n"),
851 GNUNET_i2s (&vc->addrcp->peer),
852 "<unable to resolve address>",
853 GNUNET_TRANSPORT_vs2s (vc->state));
856 GNUNET_free (vc->transport);
857 GNUNET_free (vc->addrcp);
858 GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, vc);
860 if ((0 == address_resolutions) && (iterate_validation))
864 GNUNET_SCHEDULER_cancel (end);
867 if (NULL != op_timeout)
869 GNUNET_SCHEDULER_cancel (op_timeout);
873 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
879 * Resolve address we got a validation state for to a string.
881 * @param address the address itself
882 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
883 * @param last_validation when was the address validated last
884 * @param valid_until until when is the address valid
885 * @param next_validation when will we try to revalidate the address next
886 * @param state where are we in the validation state machine
889 resolve_validation_address (const struct GNUNET_HELLO_Address *address,
891 struct GNUNET_TIME_Absolute last_validation,
892 struct GNUNET_TIME_Absolute valid_until,
893 struct GNUNET_TIME_Absolute next_validation,
894 enum GNUNET_TRANSPORT_ValidationState state)
896 struct ValidationResolutionContext *vc;
898 vc = GNUNET_new (struct ValidationResolutionContext);
899 GNUNET_assert(NULL != vc);
900 GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
901 address_resolutions++;
903 vc->transport = GNUNET_strdup(address->transport_name);
904 vc->addrcp = GNUNET_HELLO_address_copy (address);
905 vc->printed = GNUNET_NO;
907 vc->last_validation = last_validation;
908 vc->valid_until = valid_until;
909 vc->next_validation = next_validation;
911 /* Resolve address to string */
912 vc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
916 &process_validation_string, vc);
921 * Resolve address we got a validation state for to a string.
924 * @param address the address itself
925 * @param last_validation when was the address validated last
926 * @param valid_until until when is the address valid
927 * @param next_validation when will we try to revalidate the address next
928 * @param state where are we in the validation state machine
931 process_validation_cb (void *cls,
932 const struct GNUNET_HELLO_Address *address,
933 struct GNUNET_TIME_Absolute last_validation,
934 struct GNUNET_TIME_Absolute valid_until,
935 struct GNUNET_TIME_Absolute next_validation,
936 enum GNUNET_TRANSPORT_ValidationState state)
940 if (monitor_validation)
944 _("Monitor disconnected from transport service. Reconnecting.\n"));
949 GNUNET_SCHEDULER_cancel (end);
950 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
953 resolve_validation_address (address,
965 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
966 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
968 (uint16_t) head->bnd_port,
969 (uint16_t) head->adv_port);
971 head->tst = GNUNET_NAT_test_start (cfg,
972 (0 == strcasecmp (head->name, "udp"))
973 ? GNUNET_NO : GNUNET_YES,
974 (uint16_t) head->bnd_port,
975 (uint16_t) head->adv_port,
977 &result_callback, head);
982 * Test our plugin's configuration (NAT traversal, etc.).
984 * @param cfg configuration to test
987 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
991 unsigned long long bnd_port;
992 unsigned long long adv_port;
993 struct TestContext *tc;
997 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
1000 FPRINTF (stderr, "%s", _
1001 ("No transport plugins configured, peer will never communicate\n"));
1006 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
1008 char section[12 + strlen (tok)];
1009 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
1011 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
1015 _("No port configured for plugin `%s', cannot test it\n"), tok);
1018 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
1019 "ADVERTISED_PORT", &adv_port))
1020 adv_port = bnd_port;
1022 tc = GNUNET_new (struct TestContext);
1023 tc->name = GNUNET_strdup (tok);
1024 tc->adv_port = adv_port;
1025 tc->bnd_port = bnd_port;
1026 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
1028 GNUNET_free(plugins);
1030 if ((NULL != head) && (NULL == resolver))
1032 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
1033 resolver = GNUNET_OS_start_process (GNUNET_YES,
1034 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1037 "gnunet-service-resolver", NULL);
1038 if (NULL == resolver)
1040 FPRINTF (stderr, _("Failed to start resolver!\n"));
1044 GNUNET_free(binary);
1045 GNUNET_RESOLVER_connect (cfg);
1052 * Function called to notify a client about the socket
1053 * begin ready to queue more data. @a buf will be
1054 * NULL and @a size zero if the socket was closed for
1055 * writing in the meantime.
1057 * @param cls closure
1058 * @param size number of bytes available in @a buf
1059 * @param buf where the callee should write the message
1060 * @return number of bytes written to @a buf
1063 transmit_data (void *cls,
1067 struct GNUNET_MessageHeader *m = buf;
1069 if ((NULL == buf) || (0 == size))
1075 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
1076 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1077 m->size = ntohs (size);
1078 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
1079 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
1080 traffic_sent += size;
1081 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
1083 GNUNET_TIME_UNIT_FOREVER_REL,
1084 &transmit_data, NULL);
1086 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
1093 * Function called to notify transport users that another
1094 * peer connected to us.
1096 * @param cls closure
1097 * @param peer the peer that connected
1100 notify_connect (void *cls,
1101 const struct GNUNET_PeerIdentity *peer)
1103 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1108 if (NULL != op_timeout)
1110 GNUNET_SCHEDULER_cancel (op_timeout);
1115 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
1116 GNUNET_i2s (&pid), BLOCKSIZE);
1117 start_time = GNUNET_TIME_absolute_get ();
1119 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
1121 GNUNET_TIME_UNIT_FOREVER_REL,
1132 * Function called to notify transport users that another
1133 * peer disconnected from us.
1135 * @param cls closure
1136 * @param peer the peer that disconnected
1139 notify_disconnect (void *cls,
1140 const struct GNUNET_PeerIdentity *peer)
1142 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1147 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
1152 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
1155 GNUNET_SCHEDULER_cancel (end);
1162 * Function called to notify transport users that another
1163 * peer connected to us.
1165 * @param cls closure
1166 * @param peer the peer that connected
1169 monitor_notify_connect (void *cls,
1170 const struct GNUNET_PeerIdentity *peer)
1172 monitor_connect_counter++;
1173 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1174 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1177 _("%24s: %-17s %4s (%u connections in total)\n"),
1181 monitor_connect_counter);
1186 * Function called to notify transport users that another
1187 * peer disconnected from us.
1189 * @param cls closure
1190 * @param peer the peer that disconnected
1193 monitor_notify_disconnect (void *cls,
1194 const struct GNUNET_PeerIdentity *peer)
1196 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1197 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1199 GNUNET_assert(monitor_connect_counter > 0);
1200 monitor_connect_counter--;
1203 _("%24s: %-17s %4s (%u connections in total)\n"),
1205 _("Disconnected from"),
1207 monitor_connect_counter);
1212 * Function called by the transport for each received message.
1214 * @param cls closure
1215 * @param peer (claimed) identity of the other peer
1216 * @param message the message
1219 notify_receive (void *cls,
1220 const struct GNUNET_PeerIdentity *peer,
1221 const struct GNUNET_MessageHeader *message)
1223 if (benchmark_receive)
1225 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
1229 _("Received %u bytes from %s\n"),
1230 (unsigned int) ntohs (message->size),
1233 if (traffic_received == 0)
1234 start_time = GNUNET_TIME_absolute_get ();
1235 traffic_received += ntohs (message->size);
1242 * Convert address to a printable format.
1244 * @param address the address
1245 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1246 * to try to use reverse DNS
1247 * @param state state the peer is in
1248 * @param state_timeout when will the peer's state expire
1251 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1253 enum GNUNET_TRANSPORT_PeerState state,
1254 struct GNUNET_TIME_Absolute state_timeout);
1258 print_info (const struct GNUNET_PeerIdentity *id,
1259 const char *transport,
1261 enum GNUNET_TRANSPORT_PeerState state,
1262 struct GNUNET_TIME_Absolute state_timeout)
1265 if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1266 (GNUNET_YES == monitor_connections))
1269 _("Peer `%s': %s %s in state `%s' until %s\n"),
1271 (NULL == transport) ? "<none>" : transport,
1272 (NULL == transport) ? "<none>" : addr,
1273 GNUNET_TRANSPORT_ps2s (state),
1274 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1276 else if ( (GNUNET_YES == iterate_connections) &&
1277 (GNUNET_TRANSPORT_is_connected(state)))
1279 /* Only connected peers, skip state */
1281 _("Peer `%s': %s %s\n"),
1290 * Function called with a textual representation of an address. This
1291 * function will be called several times with different possible
1292 * textual representations, and a last time with @a address being NULL
1293 * to signal the end of the iteration. Note that @a address NULL
1294 * always is the last call, regardless of the value in @a res.
1296 * @param cls closure
1297 * @param address NULL on end of iteration,
1298 * otherwise 0-terminated printable UTF-8 string,
1299 * in particular an empty string if @a res is #GNUNET_NO
1300 * @param res result of the address to string conversion:
1301 * if #GNUNET_OK: conversion successful
1302 * if #GNUNET_NO: address was invalid (or not supported)
1303 * if #GNUNET_SYSERR: communication error (IPC error)
1306 process_peer_string (void *cls,
1307 const char *address,
1310 struct PeerResolutionContext *rc = cls;
1312 if (NULL != address)
1314 if (GNUNET_SYSERR == res)
1317 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
1318 GNUNET_i2s (&rc->addrcp->peer),
1319 rc->addrcp->transport_name,
1320 (unsigned int) rc->addrcp->address_length);
1321 print_info (&rc->addrcp->peer,
1326 rc->printed = GNUNET_YES;
1329 if (GNUNET_OK == res)
1331 print_info (&rc->addrcp->peer,
1336 rc->printed = GNUNET_YES;
1337 return; /* Wait for done call */
1339 /* GNUNET_NO == res: ignore, was simply not supported */
1342 /* NULL == address, last call, we are done */
1345 GNUNET_assert (address_resolutions > 0);
1346 address_resolutions--;
1347 if (GNUNET_NO == rc->printed)
1349 if (numeric == GNUNET_NO)
1351 /* Failed to resolve address, try numeric lookup
1352 (note: this should not be needed, as transport
1353 should fallback to numeric conversion if DNS takes
1355 resolve_peer_address (rc->addrcp,
1362 print_info (&rc->addrcp->peer,
1369 GNUNET_free (rc->transport);
1370 GNUNET_free (rc->addrcp);
1371 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1373 if ((0 == address_resolutions) && (iterate_connections))
1377 GNUNET_SCHEDULER_cancel (end);
1380 if (NULL != op_timeout)
1382 GNUNET_SCHEDULER_cancel (op_timeout);
1386 end = GNUNET_SCHEDULER_add_now (&shutdown_task,
1393 * Convert address to a printable format and print it
1394 * together with the given state data.
1396 * @param address the address
1397 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1398 * to try to use reverse DNS
1399 * @param state state the peer is in
1400 * @param state_timeout when will the peer's state expire
1403 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1405 enum GNUNET_TRANSPORT_PeerState state,
1406 struct GNUNET_TIME_Absolute state_timeout)
1408 struct PeerResolutionContext *rc;
1410 rc = GNUNET_new (struct PeerResolutionContext);
1411 GNUNET_CONTAINER_DLL_insert (rc_head,
1414 address_resolutions++;
1415 rc->transport = GNUNET_strdup (address->transport_name);
1416 rc->addrcp = GNUNET_HELLO_address_copy (address);
1417 rc->printed = GNUNET_NO;
1419 rc->state_timeout = state_timeout;
1420 /* Resolve address to string */
1421 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1425 &process_peer_string, rc);
1430 * Function called with information about a peers during a one shot iteration
1432 * @param cls closure
1433 * @param peer identity of the peer, NULL for final callback when operation done
1434 * @param address binary address used to communicate with this peer,
1435 * NULL on disconnect or when done
1436 * @param state current state this peer is in
1437 * @param state_timeout time out for the current state
1440 process_peer_iteration_cb (void *cls,
1441 const struct GNUNET_PeerIdentity *peer,
1442 const struct GNUNET_HELLO_Address *address,
1443 enum GNUNET_TRANSPORT_PeerState state,
1444 struct GNUNET_TIME_Absolute state_timeout)
1449 address_resolution_in_progress = GNUNET_NO;
1452 GNUNET_SCHEDULER_cancel (end);
1453 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1457 if ( (GNUNET_NO == iterate_all) &&
1458 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1459 return; /* Display only connected peers */
1461 if (NULL != op_timeout)
1462 GNUNET_SCHEDULER_cancel (op_timeout);
1463 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1468 "Received address for peer `%s': %s\n",
1470 address->transport_name);
1472 if (NULL != address)
1473 resolve_peer_address (address,
1487 * Context for address resolution by #plugin_monitoring_cb().
1489 struct PluginMonitorAddress
1493 * Ongoing resolution request.
1495 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
1498 * Resolved address as string.
1503 * Last event we got and did not yet print because
1504 * @e str was NULL (address not yet resolved).
1506 struct GNUNET_TRANSPORT_SessionInfo si;
1511 * Print information about a plugin monitoring event.
1513 * @param addr out internal context
1514 * @param info the monitoring information
1517 print_plugin_event_info (struct PluginMonitorAddress *addr,
1518 const struct GNUNET_TRANSPORT_SessionInfo *info)
1522 switch (info->state)
1524 case GNUNET_TRANSPORT_SS_INIT:
1527 case GNUNET_TRANSPORT_SS_HANDSHAKE:
1528 state = "HANDSHAKE";
1530 case GNUNET_TRANSPORT_SS_UP:
1533 case GNUNET_TRANSPORT_SS_UPDATE:
1536 case GNUNET_TRANSPORT_SS_DONE:
1544 "%s: state %s timeout in %s @ %s%s\n",
1545 GNUNET_i2s (&info->address->peer),
1547 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
1550 (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
1552 "%s: queue has %3u messages and %6u bytes\n",
1553 GNUNET_i2s (&info->address->peer),
1554 info->num_msg_pending,
1555 info->num_bytes_pending);
1556 if (0 != GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
1558 "%s: receiving blocked until %s\n",
1559 GNUNET_i2s (&info->address->peer),
1560 GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
1565 * Function called with a textual representation of an address. This
1566 * function will be called several times with different possible
1567 * textual representations, and a last time with @a address being NULL
1568 * to signal the end of the iteration. Note that @a address NULL
1569 * always is the last call, regardless of the value in @a res.
1571 * @param cls closure
1572 * @param address NULL on end of iteration,
1573 * otherwise 0-terminated printable UTF-8 string,
1574 * in particular an empty string if @a res is #GNUNET_NO
1575 * @param res result of the address to string conversion:
1576 * if #GNUNET_OK: conversion successful
1577 * if #GNUNET_NO: address was invalid (or not supported)
1578 * if #GNUNET_SYSERR: communication error (IPC error)
1581 address_cb (void *cls,
1582 const char *address,
1585 struct PluginMonitorAddress *addr = cls;
1587 if (NULL == address)
1592 if (NULL != addr->str)
1594 addr->str = GNUNET_strdup (address);
1595 print_plugin_event_info (addr,
1601 * Function called by the plugin with information about the
1602 * current sessions managed by the plugin (for monitoring).
1604 * @param cls closure (NULL)
1605 * @param session session handle this information is about,
1606 * NULL to indicate that we are "in sync" (initial
1607 * iteration complete)
1608 * @param session_ctx storage location where the application
1609 * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1610 * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1611 * @param info information about the state of the session,
1612 * NULL if @a session is also NULL and we are
1613 * merely signalling that the initial iteration is over;
1614 * NULL with @a session being non-NULL if the monitor
1615 * was being cancelled while sessions were active
1618 plugin_monitoring_cb (void *cls,
1619 struct GNUNET_TRANSPORT_PluginSession *session,
1621 const struct GNUNET_TRANSPORT_SessionInfo *info)
1623 struct PluginMonitorAddress *addr;
1625 if ( (NULL == info) &&
1627 return; /* in sync with transport service */
1628 addr = *session_ctx;
1633 if (NULL != addr->asc)
1635 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1638 GNUNET_free_non_null (addr->str);
1640 *session_ctx = NULL;
1642 return; /* shutdown */
1644 if ( (NULL != cpid) &&
1645 (0 != memcmp (&info->address->peer,
1647 sizeof (struct GNUNET_PeerIdentity))) )
1648 return; /* filtered */
1651 addr = GNUNET_new (struct PluginMonitorAddress);
1652 addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1655 GNUNET_TIME_UNIT_FOREVER_REL,
1658 *session_ctx = addr;
1660 if (NULL == addr->str)
1663 print_plugin_event_info (addr,
1665 if (GNUNET_TRANSPORT_SS_DONE == info->state)
1667 if (NULL != addr->asc)
1669 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1672 GNUNET_free_non_null (addr->str);
1674 *session_ctx = NULL;
1680 * Function called with information about a peers
1682 * @param cls closure, NULL
1683 * @param peer identity of the peer, NULL for final callback when operation done
1684 * @param address binary address used to communicate with this peer,
1685 * NULL on disconnect or when done
1686 * @param state current state this peer is in
1687 * @param state_timeout time out for the current state
1690 process_peer_monitoring_cb (void *cls,
1691 const struct GNUNET_PeerIdentity *peer,
1692 const struct GNUNET_HELLO_Address *address,
1693 enum GNUNET_TRANSPORT_PeerState state,
1694 struct GNUNET_TIME_Absolute state_timeout)
1696 struct MonitoredPeer *m;
1702 _("Monitor disconnected from transport service. Reconnecting.\n"));
1706 if (NULL != op_timeout)
1707 GNUNET_SCHEDULER_cancel (op_timeout);
1708 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1712 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1715 m = GNUNET_new (struct MonitoredPeer);
1716 GNUNET_CONTAINER_multipeermap_put (monitored_peers,
1719 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1723 if ( (m->state == state) &&
1724 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1725 (NULL == address) &&
1726 (NULL == m->address) )
1728 return; /* No real change */
1730 if ( (m->state == state) &&
1731 (NULL != address) &&
1732 (NULL != m->address) &&
1733 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1734 return; /* No real change */
1737 if (NULL != m->address)
1739 GNUNET_free (m->address);
1742 if (NULL != address)
1743 m->address = GNUNET_HELLO_address_copy (address);
1745 m->state_timeout = state_timeout;
1747 if (NULL != address)
1748 resolve_peer_address (m->address,
1762 * Function called with the transport service checking if we
1763 * want to blacklist a peer. Return #GNUNET_SYSERR for the
1764 * peer that we should disconnect from.
1767 * @param cpid peer to check blacklisting for
1768 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1771 blacklist_cb (void *cls,
1772 const struct GNUNET_PeerIdentity *cpid)
1774 if (0 == memcmp (cpid,
1776 sizeof (struct GNUNET_PeerIdentity)))
1777 return GNUNET_SYSERR;
1783 * Function called with the result of the check if the 'transport'
1784 * service is running.
1786 * @param cls closure with our configuration
1787 * @param result #GNUNET_YES if transport is running
1790 testservice_task (void *cls,
1796 if (GNUNET_YES != result)
1798 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1802 if ( (NULL != cpid) &&
1804 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
1809 _("Failed to parse peer identity `%s'\n"),
1814 counter = benchmark_send + benchmark_receive + iterate_connections
1815 + monitor_connections + monitor_connects + do_disconnect +
1816 + iterate_validation + monitor_validation + monitor_plugins;
1821 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1822 "disconnect", "benchmark send", "benchmark receive", "information",
1823 "monitor", "events", "plugins");
1829 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1830 "disconnect", "benchmark send", "benchmark receive", "information",
1831 "monitor", "events", "plugins");
1835 if (do_disconnect) /* -D: Disconnect from peer */
1840 _("Option `%s' makes no sense without option `%s'.\n"),
1845 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1848 if (NULL == blacklist)
1852 _("Failed to connect to transport service for disconnection\n"));
1858 _("Blacklisting request in place, stop with CTRL-C\n"));
1860 else if (benchmark_send) /* -s: Benchmark sending */
1864 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1869 handle = GNUNET_TRANSPORT_connect (cfg,
1874 ¬ify_disconnect);
1877 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1881 start_time = GNUNET_TIME_absolute_get ();
1882 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1886 else if (benchmark_receive) /* -b: Benchmark receiving */
1888 handle = GNUNET_TRANSPORT_connect (cfg,
1896 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1901 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
1902 start_time = GNUNET_TIME_absolute_get ();
1905 else if (iterate_connections) /* -i: List information about peers once */
1907 address_resolution_in_progress = GNUNET_YES;
1908 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1909 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1910 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1914 else if (monitor_connections) /* -m: List information about peers continuously */
1916 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
1918 address_resolution_in_progress = GNUNET_YES;
1919 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1920 (NULL == cpid) ? NULL : &pid,
1923 &process_peer_monitoring_cb, NULL);
1925 else if (monitor_plugins) /* -P: List information about plugins continuously */
1927 monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1928 pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
1929 &plugin_monitoring_cb,
1932 else if (iterate_validation) /* -d: Print information about validations */
1934 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1935 (NULL == cpid) ? NULL : &pid,
1936 GNUNET_YES, TIMEOUT,
1937 &process_validation_cb, (void *) cfg);
1939 else if (monitor_validation) /* -f: Print information about validations continuously */
1941 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1942 (NULL == cpid) ? NULL : &pid,
1944 &process_validation_cb, (void *) cfg);
1946 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1948 monitor_connect_counter = 0;
1949 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1950 &monitor_notify_connect,
1951 &monitor_notify_disconnect);
1956 _("Failed to connect to transport service\n"));
1968 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1975 * Main function that will be run by the scheduler.
1977 * @param cls closure
1978 * @param args remaining command-line arguments
1979 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1980 * @param mycfg configuration
1985 const char *cfgfile,
1986 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1988 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1989 if (test_configuration)
1991 do_test_configuration (cfg);
1994 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1995 &testservice_task, (void *) cfg);
1999 main (int argc, char * const *argv)
2002 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
2004 gettext_noop ("print information for all peers (instead of only connected peers)"),
2005 0, &GNUNET_GETOPT_set_one, &iterate_all },
2006 { 'b', "benchmark", NULL,
2007 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
2008 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
2009 { 'D', "disconnect",
2010 NULL, gettext_noop ("disconnect to a peer"), 0,
2011 &GNUNET_GETOPT_set_one, &do_disconnect },
2012 { 'd', "validation", NULL,
2013 gettext_noop ("print information for all pending validations "),
2014 0, &GNUNET_GETOPT_set_one, &iterate_validation },
2015 { 'f', "monitorvalidation", NULL,
2016 gettext_noop ("print information for all pending validations continuously"),
2017 0, &GNUNET_GETOPT_set_one, &monitor_validation },
2018 { 'i', "information", NULL,
2019 gettext_noop ("provide information about all current connections (once)"),
2020 0, &GNUNET_GETOPT_set_one, &iterate_connections },
2021 { 'm', "monitor", NULL,
2022 gettext_noop ("provide information about all current connections (continuously)"),
2023 0, &GNUNET_GETOPT_set_one, &monitor_connections },
2024 { 'e', "events", NULL,
2025 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
2026 0, &GNUNET_GETOPT_set_one, &monitor_connects },
2028 NULL, gettext_noop ("do not resolve hostnames"), 0,
2029 &GNUNET_GETOPT_set_one, &numeric },
2030 { 'p', "peer", "PEER",
2031 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
2033 { 'P', "plugins", NULL,
2034 gettext_noop ("monitor plugin sessions"), 0, &GNUNET_GETOPT_set_one,
2036 { 's', "send", NULL, gettext_noop
2037 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
2038 &GNUNET_GETOPT_set_one, &benchmark_send },
2039 { 't', "test", NULL,
2040 gettext_noop ("test transport configuration (involves external server)"),
2041 0, &GNUNET_GETOPT_set_one, &test_configuration },
2042 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
2043 GNUNET_GETOPT_OPTION_END
2046 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2049 res = GNUNET_PROGRAM_run (argc, argv,
2051 gettext_noop ("Direct access to transport service."),
2054 GNUNET_free((void *) argv);
2055 if (GNUNET_OK == res)
2060 /* end of gnunet-transport.c */