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 * 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.
490 * @param tc scheduler context
493 shutdown_task (void *cls,
494 const struct GNUNET_SCHEDULER_TaskContext *tc)
496 struct GNUNET_TIME_Relative duration;
497 struct ValidationResolutionContext *cur;
498 struct ValidationResolutionContext *next;
499 struct PeerResolutionContext *rc;
502 if (NULL != op_timeout)
504 GNUNET_SCHEDULER_cancel (op_timeout);
509 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
514 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
519 GNUNET_TRANSPORT_monitor_plugins_cancel (pm);
524 for (cur = next; NULL != cur; cur = next)
528 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
529 GNUNET_CONTAINER_DLL_remove (vc_head,
532 GNUNET_free (cur->transport);
533 GNUNET_HELLO_address_free (cur->addrcp);
536 while (NULL != (rc = rc_head))
538 GNUNET_CONTAINER_DLL_remove (rc_head,
541 GNUNET_TRANSPORT_address_to_string_cancel (rc->asc);
542 GNUNET_free (rc->transport);
543 GNUNET_free (rc->addrcp);
548 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
553 GNUNET_TRANSPORT_disconnect (handle);
558 duration = GNUNET_TIME_absolute_get_duration (start_time);
560 _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
561 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
563 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
565 if (benchmark_receive)
567 duration = GNUNET_TIME_absolute_get_duration (start_time);
569 _("Received %llu bytes/s (%llu bytes in %s)\n"),
570 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
572 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
575 if (NULL != monitored_peers)
577 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
578 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
579 monitored_peers = NULL;
581 if (NULL != monitored_plugins)
584 GNUNET_CONTAINER_multipeermap_size (monitored_plugins));
585 GNUNET_CONTAINER_multipeermap_destroy (monitored_plugins);
586 monitored_plugins = NULL;
588 if (NULL != blacklist)
590 GNUNET_TRANSPORT_blacklist_cancel (blacklist);
598 * We are done, shut down.
601 operation_timeout (void *cls,
602 const struct GNUNET_SCHEDULER_TaskContext *tc)
604 struct PeerResolutionContext *cur;
605 struct PeerResolutionContext *next;
608 if ((benchmark_send) || (benchmark_receive))
611 _("Failed to connect to `%s'\n"),
612 GNUNET_i2s_full (&pid));
614 GNUNET_SCHEDULER_cancel (end);
615 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
619 if (iterate_connections)
622 while (NULL != (cur = next))
626 _("Failed to resolve address for peer `%s'\n"),
627 GNUNET_i2s (&cur->addrcp->peer));
629 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
630 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
631 GNUNET_free(cur->transport);
632 GNUNET_free(cur->addrcp);
638 _("Failed to list connections, timeout occured\n"));
640 GNUNET_SCHEDULER_cancel (end);
641 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
653 * Display the result of the test.
655 * @param tc test context
656 * @param result #GNUNET_YES on success
659 display_test_result (struct TestContext *tc,
660 enum TestResult result)
665 _("Configuration for plugin `%s' did not work!\n"),
668 case NAT_TEST_SUCCESS:
670 _("Configuration for plugin `%s' did work!\n"),
673 case NAT_TEST_INTERNAL_FAIL:
675 _("Internal NAT error while running test for plugin `%s'\n"),
678 case NAT_TEST_FAILED_TO_START:
680 _("Failed to start NAT test for plugin `%s'\n"),
683 case NAT_TEST_TIMEOUT:
685 _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
690 _("Configuration for plugin `%s' did not work!\n"),
696 GNUNET_SCHEDULER_cancel (tc->tsk);
701 GNUNET_NAT_test_stop (tc->tst);
705 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
706 GNUNET_free (tc->name);
709 if ((NULL == head) && (NULL != resolver))
711 GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
713 GNUNET_OS_process_destroy (resolver);
722 * Function called by NAT to report the outcome of the nat-test.
723 * Clean up and update GUI.
725 * @param cls test context
726 * @param result status code
729 result_callback (void *cls,
730 enum GNUNET_NAT_StatusCode result)
732 struct TestContext *tc = cls;
734 display_test_result (tc,
740 * Resolve address we got a validation state for to a string.
742 * @param address the address itself
743 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
744 * @param last_validation when was the address validated last
745 * @param valid_until until when is the address valid
746 * @param next_validation when will we try to revalidate the address next
747 * @param state where are we in the validation state machine
750 resolve_validation_address (const struct GNUNET_HELLO_Address *address,
752 struct GNUNET_TIME_Absolute last_validation,
753 struct GNUNET_TIME_Absolute valid_until,
754 struct GNUNET_TIME_Absolute next_validation,
755 enum GNUNET_TRANSPORT_ValidationState state);
759 * Function to call with a textual representation of an address. This
760 * function will be called several times with different possible
761 * textual representations, and a last time with @a address being NULL
762 * to signal the end of the iteration. Note that @a address NULL
763 * always is the last call, regardless of the value in @a res.
766 * @param address NULL on end of iteration,
767 * otherwise 0-terminated printable UTF-8 string,
768 * in particular an empty string if @a res is #GNUNET_NO
769 * @param res result of the address to string conversion:
770 * if #GNUNET_OK: conversion successful
771 * if #GNUNET_NO: address was invalid (or not supported)
772 * if #GNUNET_SYSERR: communication error (IPC error)
775 process_validation_string (void *cls,
779 struct ValidationResolutionContext *vc = cls;
786 if (GNUNET_SYSERR == res)
789 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
790 GNUNET_i2s (&vc->addrcp->peer),
791 vc->addrcp->transport_name,
792 (unsigned int) vc->addrcp->address_length);
794 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
795 s_valid = GNUNET_strdup ("never");
797 s_valid = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
799 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
800 s_last = GNUNET_strdup ("never");
802 s_last = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
804 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
805 s_next = GNUNET_strdup ("never");
807 s_next = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
810 _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
811 GNUNET_i2s (&vc->addrcp->peer),
812 (GNUNET_OK == res) ? address : "<invalid address>",
813 (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
814 "Valid until : ", s_valid,
815 "Last validation: ",s_last,
816 "Next validation: ", s_next);
817 GNUNET_free (s_valid);
818 GNUNET_free (s_last);
819 GNUNET_free (s_next);
820 vc->printed = GNUNET_YES;
823 /* last call, we are done */
824 GNUNET_assert (address_resolutions > 0);
825 address_resolutions--;
826 if ( (GNUNET_SYSERR == res) &&
827 (GNUNET_NO == vc->printed))
829 if (numeric == GNUNET_NO)
831 /* Failed to resolve address, try numeric lookup
832 (note: this should be unnecessary, as
833 transport should fallback to numeric lookup
834 internally if DNS takes too long anyway) */
835 resolve_validation_address (vc->addrcp,
845 _("Peer `%s' %s `%s' \n"),
846 GNUNET_i2s (&vc->addrcp->peer),
847 "<unable to resolve address>",
848 GNUNET_TRANSPORT_vs2s (vc->state));
851 GNUNET_free (vc->transport);
852 GNUNET_free (vc->addrcp);
853 GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, vc);
855 if ((0 == address_resolutions) && (iterate_validation))
859 GNUNET_SCHEDULER_cancel (end);
862 if (NULL != op_timeout)
864 GNUNET_SCHEDULER_cancel (op_timeout);
868 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
874 * Resolve address we got a validation state for to a string.
876 * @param address the address itself
877 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
878 * @param last_validation when was the address validated last
879 * @param valid_until until when is the address valid
880 * @param next_validation when will we try to revalidate the address next
881 * @param state where are we in the validation state machine
884 resolve_validation_address (const struct GNUNET_HELLO_Address *address,
886 struct GNUNET_TIME_Absolute last_validation,
887 struct GNUNET_TIME_Absolute valid_until,
888 struct GNUNET_TIME_Absolute next_validation,
889 enum GNUNET_TRANSPORT_ValidationState state)
891 struct ValidationResolutionContext *vc;
893 vc = GNUNET_new (struct ValidationResolutionContext);
894 GNUNET_assert(NULL != vc);
895 GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
896 address_resolutions++;
898 vc->transport = GNUNET_strdup(address->transport_name);
899 vc->addrcp = GNUNET_HELLO_address_copy (address);
900 vc->printed = GNUNET_NO;
902 vc->last_validation = last_validation;
903 vc->valid_until = valid_until;
904 vc->next_validation = next_validation;
906 /* Resolve address to string */
907 vc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
911 &process_validation_string, vc);
916 * Resolve address we got a validation state for to a string.
919 * @param address the address itself
920 * @param last_validation when was the address validated last
921 * @param valid_until until when is the address valid
922 * @param next_validation when will we try to revalidate the address next
923 * @param state where are we in the validation state machine
926 process_validation_cb (void *cls,
927 const struct GNUNET_HELLO_Address *address,
928 struct GNUNET_TIME_Absolute last_validation,
929 struct GNUNET_TIME_Absolute valid_until,
930 struct GNUNET_TIME_Absolute next_validation,
931 enum GNUNET_TRANSPORT_ValidationState state)
935 if (monitor_validation)
939 _("Monitor disconnected from transport service. Reconnecting.\n"));
944 GNUNET_SCHEDULER_cancel (end);
945 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
948 resolve_validation_address (address,
960 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
961 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
963 (uint16_t) head->bnd_port,
964 (uint16_t) head->adv_port);
966 head->tst = GNUNET_NAT_test_start (cfg,
967 (0 == strcasecmp (head->name, "udp"))
968 ? GNUNET_NO : GNUNET_YES,
969 (uint16_t) head->bnd_port,
970 (uint16_t) head->adv_port,
972 &result_callback, head);
977 * Test our plugin's configuration (NAT traversal, etc.).
979 * @param cfg configuration to test
982 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
986 unsigned long long bnd_port;
987 unsigned long long adv_port;
988 struct TestContext *tc;
992 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
995 FPRINTF (stderr, "%s", _
996 ("No transport plugins configured, peer will never communicate\n"));
1001 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
1003 char section[12 + strlen (tok)];
1004 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
1006 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
1010 _("No port configured for plugin `%s', cannot test it\n"), tok);
1013 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
1014 "ADVERTISED_PORT", &adv_port))
1015 adv_port = bnd_port;
1017 tc = GNUNET_new (struct TestContext);
1018 tc->name = GNUNET_strdup (tok);
1019 tc->adv_port = adv_port;
1020 tc->bnd_port = bnd_port;
1021 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
1023 GNUNET_free(plugins);
1025 if ((NULL != head) && (NULL == resolver))
1027 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
1028 resolver = GNUNET_OS_start_process (GNUNET_YES,
1029 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1032 "gnunet-service-resolver", NULL);
1033 if (NULL == resolver)
1035 FPRINTF (stderr, _("Failed to start resolver!\n"));
1039 GNUNET_free(binary);
1040 GNUNET_RESOLVER_connect (cfg);
1047 * Function called to notify a client about the socket
1048 * begin ready to queue more data. @a buf will be
1049 * NULL and @a size zero if the socket was closed for
1050 * writing in the meantime.
1052 * @param cls closure
1053 * @param size number of bytes available in @a buf
1054 * @param buf where the callee should write the message
1055 * @return number of bytes written to @a buf
1058 transmit_data (void *cls,
1062 struct GNUNET_MessageHeader *m = buf;
1064 if ((NULL == buf) || (0 == size))
1070 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
1071 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1072 m->size = ntohs (size);
1073 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
1074 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
1075 traffic_sent += size;
1076 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
1078 GNUNET_TIME_UNIT_FOREVER_REL,
1079 &transmit_data, NULL);
1081 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
1088 * Function called to notify transport users that another
1089 * peer connected to us.
1091 * @param cls closure
1092 * @param peer the peer that connected
1095 notify_connect (void *cls,
1096 const struct GNUNET_PeerIdentity *peer)
1098 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1103 if (NULL != op_timeout)
1105 GNUNET_SCHEDULER_cancel (op_timeout);
1110 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
1111 GNUNET_i2s (&pid), BLOCKSIZE);
1112 start_time = GNUNET_TIME_absolute_get ();
1114 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
1116 GNUNET_TIME_UNIT_FOREVER_REL,
1127 * Function called to notify transport users that another
1128 * peer disconnected from us.
1130 * @param cls closure
1131 * @param peer the peer that disconnected
1134 notify_disconnect (void *cls,
1135 const struct GNUNET_PeerIdentity *peer)
1137 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1142 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
1147 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
1150 GNUNET_SCHEDULER_cancel (end);
1157 * Function called to notify transport users that another
1158 * peer connected to us.
1160 * @param cls closure
1161 * @param peer the peer that connected
1164 monitor_notify_connect (void *cls,
1165 const struct GNUNET_PeerIdentity *peer)
1167 monitor_connect_counter++;
1168 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1169 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1172 _("%24s: %-17s %4s (%u connections in total)\n"),
1176 monitor_connect_counter);
1181 * Function called to notify transport users that another
1182 * peer disconnected from us.
1184 * @param cls closure
1185 * @param peer the peer that disconnected
1188 monitor_notify_disconnect (void *cls,
1189 const struct GNUNET_PeerIdentity *peer)
1191 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1192 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1194 GNUNET_assert(monitor_connect_counter > 0);
1195 monitor_connect_counter--;
1198 _("%24s: %-17s %4s (%u connections in total)\n"),
1200 _("Disconnected from"),
1202 monitor_connect_counter);
1207 * Function called by the transport for each received message.
1209 * @param cls closure
1210 * @param peer (claimed) identity of the other peer
1211 * @param message the message
1214 notify_receive (void *cls,
1215 const struct GNUNET_PeerIdentity *peer,
1216 const struct GNUNET_MessageHeader *message)
1218 if (benchmark_receive)
1220 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
1224 _("Received %u bytes from %s\n"),
1225 (unsigned int) ntohs (message->size),
1228 if (traffic_received == 0)
1229 start_time = GNUNET_TIME_absolute_get ();
1230 traffic_received += ntohs (message->size);
1237 * Convert address to a printable format.
1239 * @param address the address
1240 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1241 * to try to use reverse DNS
1242 * @param state state the peer is in
1243 * @param state_timeout when will the peer's state expire
1246 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1248 enum GNUNET_TRANSPORT_PeerState state,
1249 struct GNUNET_TIME_Absolute state_timeout);
1253 print_info (const struct GNUNET_PeerIdentity *id,
1254 const char *transport,
1256 enum GNUNET_TRANSPORT_PeerState state,
1257 struct GNUNET_TIME_Absolute state_timeout)
1260 if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1261 (GNUNET_YES == monitor_connections))
1264 _("Peer `%s': %s %s in state `%s' until %s\n"),
1266 (NULL == transport) ? "<none>" : transport,
1267 (NULL == transport) ? "<none>" : addr,
1268 GNUNET_TRANSPORT_ps2s (state),
1269 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1271 else if ( (GNUNET_YES == iterate_connections) &&
1272 (GNUNET_TRANSPORT_is_connected(state)))
1274 /* Only connected peers, skip state */
1276 _("Peer `%s': %s %s\n"),
1285 * Function called with a textual representation of an address. This
1286 * function will be called several times with different possible
1287 * textual representations, and a last time with @a address being NULL
1288 * to signal the end of the iteration. Note that @a address NULL
1289 * always is the last call, regardless of the value in @a res.
1291 * @param cls closure
1292 * @param address NULL on end of iteration,
1293 * otherwise 0-terminated printable UTF-8 string,
1294 * in particular an empty string if @a res is #GNUNET_NO
1295 * @param res result of the address to string conversion:
1296 * if #GNUNET_OK: conversion successful
1297 * if #GNUNET_NO: address was invalid (or not supported)
1298 * if #GNUNET_SYSERR: communication error (IPC error)
1301 process_peer_string (void *cls,
1302 const char *address,
1305 struct PeerResolutionContext *rc = cls;
1307 if (NULL != address)
1309 if (GNUNET_SYSERR == res)
1312 "Failed to convert address for peer `%s' plugin `%s' length %u to string \n",
1313 GNUNET_i2s (&rc->addrcp->peer),
1314 rc->addrcp->transport_name,
1315 (unsigned int) rc->addrcp->address_length);
1316 print_info (&rc->addrcp->peer,
1321 rc->printed = GNUNET_YES;
1324 if (GNUNET_OK == res)
1326 print_info (&rc->addrcp->peer,
1331 rc->printed = GNUNET_YES;
1332 return; /* Wait for done call */
1334 /* GNUNET_NO == res: ignore, was simply not supported */
1337 /* NULL == address, last call, we are done */
1340 GNUNET_assert (address_resolutions > 0);
1341 address_resolutions--;
1342 if (GNUNET_NO == rc->printed)
1344 if (numeric == GNUNET_NO)
1346 /* Failed to resolve address, try numeric lookup
1347 (note: this should not be needed, as transport
1348 should fallback to numeric conversion if DNS takes
1350 resolve_peer_address (rc->addrcp,
1357 print_info (&rc->addrcp->peer,
1364 GNUNET_free (rc->transport);
1365 GNUNET_free (rc->addrcp);
1366 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1368 if ((0 == address_resolutions) && (iterate_connections))
1372 GNUNET_SCHEDULER_cancel (end);
1375 if (NULL != op_timeout)
1377 GNUNET_SCHEDULER_cancel (op_timeout);
1381 end = GNUNET_SCHEDULER_add_now (&shutdown_task,
1388 * Convert address to a printable format and print it
1389 * together with the given state data.
1391 * @param address the address
1392 * @param numeric #GNUNET_YES to convert to numeric format, #GNUNET_NO
1393 * to try to use reverse DNS
1394 * @param state state the peer is in
1395 * @param state_timeout when will the peer's state expire
1398 resolve_peer_address (const struct GNUNET_HELLO_Address *address,
1400 enum GNUNET_TRANSPORT_PeerState state,
1401 struct GNUNET_TIME_Absolute state_timeout)
1403 struct PeerResolutionContext *rc;
1405 rc = GNUNET_new (struct PeerResolutionContext);
1406 GNUNET_CONTAINER_DLL_insert (rc_head,
1409 address_resolutions++;
1410 rc->transport = GNUNET_strdup (address->transport_name);
1411 rc->addrcp = GNUNET_HELLO_address_copy (address);
1412 rc->printed = GNUNET_NO;
1414 rc->state_timeout = state_timeout;
1415 /* Resolve address to string */
1416 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1420 &process_peer_string, rc);
1425 * Function called with information about a peers during a one shot iteration
1427 * @param cls closure
1428 * @param peer identity of the peer, NULL for final callback when operation done
1429 * @param address binary address used to communicate with this peer,
1430 * NULL on disconnect or when done
1431 * @param state current state this peer is in
1432 * @param state_timeout time out for the current state
1435 process_peer_iteration_cb (void *cls,
1436 const struct GNUNET_PeerIdentity *peer,
1437 const struct GNUNET_HELLO_Address *address,
1438 enum GNUNET_TRANSPORT_PeerState state,
1439 struct GNUNET_TIME_Absolute state_timeout)
1448 if ( (GNUNET_NO == iterate_all) &&
1449 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1450 return; /* Display only connected peers */
1452 if (NULL != op_timeout)
1453 GNUNET_SCHEDULER_cancel (op_timeout);
1454 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "Received address for peer `%s': %s\n",
1461 address ? address->transport_name : "");
1463 if (NULL != address)
1464 resolve_peer_address (address,
1478 * Context for address resolution by #plugin_monitoring_cb().
1480 struct PluginMonitorAddress
1484 * Ongoing resolution request.
1486 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
1489 * Resolved address as string.
1494 * Last event we got and did not yet print because
1495 * @e str was NULL (address not yet resolved).
1497 struct GNUNET_TRANSPORT_SessionInfo si;
1502 * Print information about a plugin monitoring event.
1504 * @param addr out internal context
1505 * @param info the monitoring information
1508 print_plugin_event_info (struct PluginMonitorAddress *addr,
1509 const struct GNUNET_TRANSPORT_SessionInfo *info)
1513 switch (info->state)
1515 case GNUNET_TRANSPORT_SS_INIT:
1518 case GNUNET_TRANSPORT_SS_HANDSHAKE:
1519 state = "HANDSHAKE";
1521 case GNUNET_TRANSPORT_SS_UP:
1524 case GNUNET_TRANSPORT_SS_UPDATE:
1527 case GNUNET_TRANSPORT_SS_DONE:
1535 "%s: state %s timeout in %s @ %s%s\n",
1536 GNUNET_i2s (&info->address->peer),
1538 GNUNET_STRINGS_relative_time_to_string (GNUNET_TIME_absolute_get_remaining (info->session_timeout),
1541 (info->is_inbound == GNUNET_YES) ? " (INBOUND)" : "");
1543 "%s: queue has %3u messages and %6u bytes\n",
1544 GNUNET_i2s (&info->address->peer),
1545 info->num_msg_pending,
1546 info->num_bytes_pending);
1547 if (0 != GNUNET_TIME_absolute_get_remaining (info->receive_delay).rel_value_us)
1549 "%s: receiving blocked until %s\n",
1550 GNUNET_i2s (&info->address->peer),
1551 GNUNET_STRINGS_absolute_time_to_string (info->receive_delay));
1556 * Function called with a textual representation of an address. This
1557 * function will be called several times with different possible
1558 * textual representations, and a last time with @a address being NULL
1559 * to signal the end of the iteration. Note that @a address NULL
1560 * always is the last call, regardless of the value in @a res.
1562 * @param cls closure
1563 * @param address NULL on end of iteration,
1564 * otherwise 0-terminated printable UTF-8 string,
1565 * in particular an empty string if @a res is #GNUNET_NO
1566 * @param res result of the address to string conversion:
1567 * if #GNUNET_OK: conversion successful
1568 * if #GNUNET_NO: address was invalid (or not supported)
1569 * if #GNUNET_SYSERR: communication error (IPC error)
1572 address_cb (void *cls,
1573 const char *address,
1576 struct PluginMonitorAddress *addr = cls;
1578 if (NULL == address)
1583 if (NULL != addr->str)
1585 addr->str = GNUNET_strdup (address);
1586 print_plugin_event_info (addr,
1592 * Function called by the plugin with information about the
1593 * current sessions managed by the plugin (for monitoring).
1595 * @param cls closure (NULL)
1596 * @param session session handle this information is about,
1597 * NULL to indicate that we are "in sync" (initial
1598 * iteration complete)
1599 * @param session_ctx storage location where the application
1600 * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
1601 * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
1602 * @param info information about the state of the session,
1603 * NULL if @a session is also NULL and we are
1604 * merely signalling that the initial iteration is over;
1605 * NULL with @a session being non-NULL if the monitor
1606 * was being cancelled while sessions were active
1609 plugin_monitoring_cb (void *cls,
1610 struct GNUNET_TRANSPORT_PluginSession *session,
1612 const struct GNUNET_TRANSPORT_SessionInfo *info)
1614 struct PluginMonitorAddress *addr;
1616 if ( (NULL == info) &&
1618 return; /* in sync with transport service */
1619 addr = *session_ctx;
1624 if (NULL != addr->asc)
1626 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1629 GNUNET_free_non_null (addr->str);
1631 *session_ctx = NULL;
1633 return; /* shutdown */
1635 if ( (NULL != cpid) &&
1636 (0 != memcmp (&info->address->peer,
1638 sizeof (struct GNUNET_PeerIdentity))) )
1639 return; /* filtered */
1642 addr = GNUNET_new (struct PluginMonitorAddress);
1643 addr->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1646 GNUNET_TIME_UNIT_FOREVER_REL,
1649 *session_ctx = addr;
1651 if (NULL == addr->str)
1654 print_plugin_event_info (addr,
1656 if (GNUNET_TRANSPORT_SS_DONE == info->state)
1658 if (NULL != addr->asc)
1660 GNUNET_TRANSPORT_address_to_string_cancel (addr->asc);
1663 GNUNET_free_non_null (addr->str);
1665 *session_ctx = NULL;
1671 * Function called with information about a peers
1673 * @param cls closure, NULL
1674 * @param peer identity of the peer, NULL for final callback when operation done
1675 * @param address binary address used to communicate with this peer,
1676 * NULL on disconnect or when done
1677 * @param state current state this peer is in
1678 * @param state_timeout time out for the current state
1681 process_peer_monitoring_cb (void *cls,
1682 const struct GNUNET_PeerIdentity *peer,
1683 const struct GNUNET_HELLO_Address *address,
1684 enum GNUNET_TRANSPORT_PeerState state,
1685 struct GNUNET_TIME_Absolute state_timeout)
1687 struct MonitoredPeer *m;
1693 _("Monitor disconnected from transport service. Reconnecting.\n"));
1697 if (NULL != op_timeout)
1698 GNUNET_SCHEDULER_cancel (op_timeout);
1699 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1703 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers,
1706 m = GNUNET_new (struct MonitoredPeer);
1707 GNUNET_CONTAINER_multipeermap_put (monitored_peers,
1710 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1714 if ( (m->state == state) &&
1715 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1716 (NULL == address) &&
1717 (NULL == m->address) )
1719 return; /* No real change */
1721 if ( (m->state == state) &&
1722 (NULL != address) &&
1723 (NULL != m->address) &&
1724 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1725 return; /* No real change */
1728 if (NULL != m->address)
1730 GNUNET_free (m->address);
1733 if (NULL != address)
1734 m->address = GNUNET_HELLO_address_copy (address);
1736 m->state_timeout = state_timeout;
1738 if (NULL != address)
1739 resolve_peer_address (m->address,
1753 * Function called with the transport service checking if we
1754 * want to blacklist a peer. Return #GNUNET_SYSERR for the
1755 * peer that we should disconnect from.
1758 * @param cpid peer to check blacklisting for
1759 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
1762 blacklist_cb (void *cls,
1763 const struct GNUNET_PeerIdentity *cpid)
1765 if (0 == memcmp (cpid,
1767 sizeof (struct GNUNET_PeerIdentity)))
1768 return GNUNET_SYSERR;
1774 * Function called with the result of the check if the 'transport'
1775 * service is running.
1777 * @param cls closure with our configuration
1778 * @param result #GNUNET_YES if transport is running
1781 testservice_task (void *cls,
1787 if (GNUNET_YES != result)
1789 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1793 if ( (NULL != cpid) &&
1795 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid,
1800 _("Failed to parse peer identity `%s'\n"),
1805 counter = benchmark_send + benchmark_receive + iterate_connections
1806 + monitor_connections + monitor_connects + do_disconnect +
1807 + iterate_validation + monitor_validation + monitor_plugins;
1812 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s %s\n"),
1813 "disconnect", "benchmark send", "benchmark receive", "information",
1814 "monitor", "events", "plugins");
1820 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s, %s\n"),
1821 "disconnect", "benchmark send", "benchmark receive", "information",
1822 "monitor", "events", "plugins");
1826 if (do_disconnect) /* -D: Disconnect from peer */
1831 _("Option `%s' makes no sense without option `%s'.\n"),
1836 blacklist = GNUNET_TRANSPORT_blacklist (cfg,
1839 if (NULL == blacklist)
1843 _("Failed to connect to transport service for disconnection\n"));
1849 _("Blacklisting request in place, stop with CTRL-C\n"));
1851 else if (benchmark_send) /* -s: Benchmark sending */
1855 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1860 handle = GNUNET_TRANSPORT_connect (cfg,
1865 ¬ify_disconnect);
1868 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1872 start_time = GNUNET_TIME_absolute_get ();
1873 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1877 else if (benchmark_receive) /* -b: Benchmark receiving */
1879 handle = GNUNET_TRANSPORT_connect (cfg,
1887 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1892 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
1893 start_time = GNUNET_TIME_absolute_get ();
1896 else if (iterate_connections) /* -i: List information about peers once */
1898 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1899 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1900 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1904 else if (monitor_connections) /* -m: List information about peers continuously */
1906 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10,
1908 pic = GNUNET_TRANSPORT_monitor_peers (cfg,
1909 (NULL == cpid) ? NULL : &pid,
1912 &process_peer_monitoring_cb, NULL);
1914 else if (monitor_plugins) /* -P: List information about plugins continuously */
1916 monitored_plugins = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1917 pm = GNUNET_TRANSPORT_monitor_plugins (cfg,
1918 &plugin_monitoring_cb,
1921 else if (iterate_validation) /* -d: Print information about validations */
1923 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1924 (NULL == cpid) ? NULL : &pid,
1925 GNUNET_YES, TIMEOUT,
1926 &process_validation_cb, (void *) cfg);
1928 else if (monitor_validation) /* -f: Print information about validations continuously */
1930 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1931 (NULL == cpid) ? NULL : &pid,
1933 &process_validation_cb, (void *) cfg);
1935 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1937 monitor_connect_counter = 0;
1938 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1939 &monitor_notify_connect,
1940 &monitor_notify_disconnect);
1945 _("Failed to connect to transport service\n"));
1957 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1964 * Main function that will be run by the scheduler.
1966 * @param cls closure
1967 * @param args remaining command-line arguments
1968 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1969 * @param mycfg configuration
1974 const char *cfgfile,
1975 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1977 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1978 if (test_configuration)
1980 do_test_configuration (cfg);
1983 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1984 &testservice_task, (void *) cfg);
1988 main (int argc, char * const *argv)
1991 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1993 gettext_noop ("print information for all peers (instead of only connected peers)"),
1994 0, &GNUNET_GETOPT_set_one, &iterate_all },
1995 { 'b', "benchmark", NULL,
1996 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1997 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1998 { 'D', "disconnect",
1999 NULL, gettext_noop ("disconnect to a peer"), 0,
2000 &GNUNET_GETOPT_set_one, &do_disconnect },
2001 { 'd', "validation", NULL,
2002 gettext_noop ("print information for all pending validations "),
2003 0, &GNUNET_GETOPT_set_one, &iterate_validation },
2004 { 'f', "monitorvalidation", NULL,
2005 gettext_noop ("print information for all pending validations continuously"),
2006 0, &GNUNET_GETOPT_set_one, &monitor_validation },
2007 { 'i', "information", NULL,
2008 gettext_noop ("provide information about all current connections (once)"),
2009 0, &GNUNET_GETOPT_set_one, &iterate_connections },
2010 { 'm', "monitor", NULL,
2011 gettext_noop ("provide information about all current connections (continuously)"),
2012 0, &GNUNET_GETOPT_set_one, &monitor_connections },
2013 { 'e', "events", NULL,
2014 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
2015 0, &GNUNET_GETOPT_set_one, &monitor_connects },
2017 NULL, gettext_noop ("do not resolve hostnames"), 0,
2018 &GNUNET_GETOPT_set_one, &numeric },
2019 { 'p', "peer", "PEER",
2020 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
2022 { 'P', "plugins", NULL,
2023 gettext_noop ("monitor plugin sessions"), 0, &GNUNET_GETOPT_set_one,
2025 { 's', "send", NULL, gettext_noop
2026 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
2027 &GNUNET_GETOPT_set_one, &benchmark_send },
2028 { 't', "test", NULL,
2029 gettext_noop ("test transport configuration (involves external server)"),
2030 0, &GNUNET_GETOPT_set_one, &test_configuration },
2031 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
2032 GNUNET_GETOPT_OPTION_END
2035 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
2038 res = GNUNET_PROGRAM_run (argc, argv,
2040 gettext_noop ("Direct access to transport service."),
2043 GNUNET_free((void *) argv);
2044 if (GNUNET_OK == res)
2049 /* end of gnunet-transport.c */