2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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 operations
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_PeerIdentity id;
77 struct GNUNET_HELLO_Address *addrcp;
80 * Time of last validation
82 struct GNUNET_TIME_Absolute last_validation;
85 * Address is valid until
87 struct GNUNET_TIME_Absolute valid_until;
90 * Time of next validation
92 struct GNUNET_TIME_Absolute next_validation;
95 * state of validation process
97 enum GNUNET_TRANSPORT_ValidationState state;
100 * Tranport conversion handle
102 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
110 * was the entry printed
116 * Struct to store information about peers in monitor mode
123 enum GNUNET_TRANSPORT_PeerState state;
128 struct GNUNET_TIME_Absolute state_timeout;
131 * The address to convert
133 struct GNUNET_HELLO_Address *address;
137 * Context to store name resolutions for valiation
139 struct PeerResolutionContext
144 struct PeerResolutionContext *next;
149 struct PeerResolutionContext *prev;
154 struct GNUNET_PeerIdentity id;
159 struct GNUNET_HELLO_Address *addrcp;
162 * transport conversiion context
164 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
169 enum GNUNET_TRANSPORT_PeerState state;
174 struct GNUNET_TIME_Absolute state_timeout;
182 * was the entry printed
189 * Context for a plugin test.
196 struct TestContext *prev;
201 struct TestContext *next;
204 * Handle to the active NAT test.
206 struct GNUNET_NAT_Test *tst;
209 * Task identifier for the timeout.
211 GNUNET_SCHEDULER_TaskIdentifier tsk;
214 * Name of plugin under test.
221 unsigned long long bnd_port;
226 unsigned long long adv_port;
237 * NAT returned success
239 NAT_TEST_SUCCESS = GNUNET_OK,
242 * NAT returned failure
244 NAT_TEST_FAIL = GNUNET_NO,
247 * NAT returned failure while running test
249 NAT_TEST_INTERNAL_FAIL = GNUNET_SYSERR,
252 * We could not start the test
254 NAT_TEST_FAILED_TO_START = 2,
257 * We had a timeout while running the test
264 * Benchmarking block size in KB
269 * Which peer should we connect to?
274 * Handle to transport service.
276 static struct GNUNET_TRANSPORT_Handle *handle;
279 * Configuration handle
281 static struct GNUNET_CONFIGURATION_Handle *cfg;
286 struct GNUNET_TRANSPORT_TryConnectHandle *tc_handle;
291 static int benchmark_send;
296 static int benchmark_receive;
301 static int benchmark_receive;
306 static int iterate_connections;
311 static int iterate_validation;
316 static int iterate_all;
321 static int test_configuration;
326 static int monitor_connects;
331 static int monitor_connections;
336 static int monitor_validation;
341 static int try_connect;
346 static int try_disconnect;
354 * Global return value (0 success).
359 * Current number of connections in monitor mode
361 static int monitor_connect_counter;
364 * Number of bytes of traffic we received so far.
366 static unsigned long long traffic_received;
369 * Number of bytes of traffic we sent so far.
371 static unsigned long long traffic_sent;
374 * Starting time of transmitting/receiving data.
376 static struct GNUNET_TIME_Absolute start_time;
379 * Handle for current transmission request.
381 static struct GNUNET_TRANSPORT_TransmitHandle *th;
384 * Map storing information about monitored peers
386 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
391 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
396 static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic;
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 GNUNET_SCHEDULER_TaskIdentifier end;
410 * Task for operation timeout
412 static GNUNET_SCHEDULER_TaskIdentifier 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 destroy_it (void *cls,
467 const struct GNUNET_PeerIdentity *key,
470 struct MonitoredPeer *m = value;
472 GNUNET_assert (GNUNET_OK ==
473 GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
476 GNUNET_free_non_null (m->address);
483 * Task run in monitor mode when the user presses CTRL-C to abort.
484 * Stops monitoring activity.
487 * @param tc scheduler context
490 shutdown_task (void *cls,
491 const struct GNUNET_SCHEDULER_TaskContext *tc)
493 struct GNUNET_TIME_Relative duration;
494 struct ValidationResolutionContext *cur;
495 struct ValidationResolutionContext *next;
497 end = GNUNET_SCHEDULER_NO_TASK;
498 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
500 GNUNET_SCHEDULER_cancel (op_timeout);
501 op_timeout = GNUNET_SCHEDULER_NO_TASK;
503 if (NULL != tc_handle)
505 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
510 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
515 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
520 for (cur = next; NULL != cur; cur = next)
524 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
525 GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, cur);
526 GNUNET_free (cur->transport);
527 GNUNET_HELLO_address_free (cur->addrcp);
533 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
538 GNUNET_TRANSPORT_disconnect (handle);
543 duration = GNUNET_TIME_absolute_get_duration (start_time);
545 _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
546 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
548 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
550 if (benchmark_receive)
552 duration = GNUNET_TIME_absolute_get_duration (start_time);
554 _("Received %llu bytes/s (%llu bytes in %s)\n"),
555 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
557 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
560 if (NULL != monitored_peers)
562 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
563 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
564 monitored_peers = NULL;
570 operation_timeout (void *cls,
571 const struct GNUNET_SCHEDULER_TaskContext *tc)
573 struct PeerResolutionContext *cur;
574 struct PeerResolutionContext *next;
575 op_timeout = GNUNET_SCHEDULER_NO_TASK;
576 if ((try_connect) || (benchmark_send) || (benchmark_receive))
579 _("Failed to connect to `%s'\n"),
580 GNUNET_i2s_full (&pid));
581 if (GNUNET_SCHEDULER_NO_TASK != end)
582 GNUNET_SCHEDULER_cancel (end);
583 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
587 if (iterate_connections)
590 while (NULL != (cur = next))
594 _("Failed to resolve address for peer `%s'\n"),
595 GNUNET_i2s (&cur->addrcp->peer));
597 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
598 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
599 GNUNET_free(cur->transport);
600 GNUNET_free(cur->addrcp);
606 _("Failed to list connections, timeout occured\n"));
607 if (GNUNET_SCHEDULER_NO_TASK != end)
608 GNUNET_SCHEDULER_cancel (end);
609 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
621 * Display the result of the test.
623 * @param tc test context
624 * @param result #GNUNET_YES on success
627 display_test_result (struct TestContext *tc,
628 enum TestResult result)
633 _("Configuration for plugin `%s' did not work!\n"),
636 case NAT_TEST_SUCCESS:
638 _("Configuration for plugin `%s' did work!\n"),
641 case NAT_TEST_INTERNAL_FAIL:
643 _("Internal NAT error while running test for plugin `%s'\n"),
646 case NAT_TEST_FAILED_TO_START:
648 _("Failed to start NAT test for plugin `%s'\n"),
651 case NAT_TEST_TIMEOUT:
653 _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
660 if (GNUNET_YES != result)
663 _("Configuration for plugin `%s' did not work!\n"),
669 _("Configuration for plugin `%s' is working!\n"),
672 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
674 GNUNET_SCHEDULER_cancel (tc->tsk);
675 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
679 GNUNET_NAT_test_stop (tc->tst);
683 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
684 GNUNET_free (tc->name);
687 if ((NULL == head) && (NULL != resolver))
689 GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
691 GNUNET_OS_process_destroy (resolver);
699 * Function called by NAT to report the outcome of the nat-test.
700 * Clean up and update GUI.
702 * @param cls test context
703 * @param result status code
706 result_callback (void *cls,
707 enum GNUNET_NAT_StatusCode result)
709 struct TestContext *tc = cls;
711 display_test_result (tc, result);
716 * Resolve address we got a validation state for to a string.
718 * @param id peer identity the address is for
719 * @param address the address itself
720 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
721 * @param last_validation when was the address validated last
722 * @param valid_until until when is the address valid
723 * @param next_validation when will we try to revalidate the address next
724 * @param state where are we in the validation state machine
727 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
728 const struct GNUNET_HELLO_Address *address,
730 struct GNUNET_TIME_Absolute last_validation,
731 struct GNUNET_TIME_Absolute valid_until,
732 struct GNUNET_TIME_Absolute next_validation,
733 enum GNUNET_TRANSPORT_ValidationState state);
737 * Function to call with a textual representation of an address. This
738 * function will be called several times with different possible
739 * textual representations, and a last time with @a address being NULL
740 * to signal the end of the iteration. Note that @a address NULL
741 * always is the last call, regardless of the value in @a res.
744 * @param address NULL on end of iteration,
745 * otherwise 0-terminated printable UTF-8 string,
746 * in particular an empty string if @a res is #GNUNET_NO
747 * @param res result of the address to string conversion:
748 * if #GNUNET_OK: conversion successful
749 * if #GNUNET_NO: address was invalid (or not supported)
750 * if #GNUNET_SYSERR: communication error (IPC error)
753 process_validation_string (void *cls,
757 struct ValidationResolutionContext *vc = cls;
764 if (GNUNET_SYSERR == res)
767 "Failed to convert address for peer `%s' plugin `%s' length %lu to string \n",
768 GNUNET_i2s (&vc->id),
769 vc->addrcp->transport_name,
770 vc->addrcp->address_length);
772 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
773 s_valid = GNUNET_strdup ("never");
775 s_valid = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
777 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
778 s_last = GNUNET_strdup ("never");
780 s_last = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
782 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
783 s_next = GNUNET_strdup ("never");
785 s_next = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
788 _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
789 GNUNET_i2s (&vc->id),
790 (GNUNET_OK == res) ? address : "<invalid address>",
791 (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
792 "Valid until : ", s_valid,
793 "Last validation: ",s_last,
794 "Next validation: ", s_next);
795 GNUNET_free (s_valid);
796 GNUNET_free (s_last);
797 GNUNET_free (s_next);
798 vc->printed = GNUNET_YES;
801 /* last call, we are done */
802 GNUNET_assert (address_resolutions > 0);
803 address_resolutions--;
804 if ( (GNUNET_SYSERR == res) &&
805 (GNUNET_NO == vc->printed))
807 if (numeric == GNUNET_NO)
809 /* Failed to resolve address, try numeric lookup
810 (note: this should be unnecessary, as
811 transport should fallback to numeric lookup
812 internally if DNS takes too long anyway) */
813 resolve_validation_address (&vc->id,
824 _("Peer `%s' %s `%s' \n"),
825 GNUNET_i2s (&vc->id),
826 "<unable to resolve address>",
827 GNUNET_TRANSPORT_vs2s (vc->state));
830 GNUNET_free (vc->transport);
831 GNUNET_free (vc->addrcp);
832 GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, vc);
834 if ((0 == address_resolutions) && (iterate_validation))
836 if (GNUNET_SCHEDULER_NO_TASK != end)
838 GNUNET_SCHEDULER_cancel (end);
839 end = GNUNET_SCHEDULER_NO_TASK;
841 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
843 GNUNET_SCHEDULER_cancel (op_timeout);
844 op_timeout = GNUNET_SCHEDULER_NO_TASK;
847 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
853 * Resolve address we got a validation state for to a string.
855 * @param id peer identity the address is for
856 * @param address the address itself
857 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
858 * @param last_validation when was the address validated last
859 * @param valid_until until when is the address valid
860 * @param next_validation when will we try to revalidate the address next
861 * @param state where are we in the validation state machine
864 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
865 const struct GNUNET_HELLO_Address *address,
867 struct GNUNET_TIME_Absolute last_validation,
868 struct GNUNET_TIME_Absolute valid_until,
869 struct GNUNET_TIME_Absolute next_validation,
870 enum GNUNET_TRANSPORT_ValidationState state)
872 struct ValidationResolutionContext *vc;
874 vc = GNUNET_new (struct ValidationResolutionContext);
875 GNUNET_assert(NULL != vc);
876 GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
877 address_resolutions++;
880 vc->transport = GNUNET_strdup(address->transport_name);
881 vc->addrcp = GNUNET_HELLO_address_copy (address);
882 vc->printed = GNUNET_NO;
884 vc->last_validation = last_validation;
885 vc->valid_until = valid_until;
886 vc->next_validation = next_validation;
888 /* Resolve address to string */
889 vc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
893 &process_validation_string, vc);
898 * Resolve address we got a validation state for to a string.
901 * @param peer peer identity the address is for
902 * @param address the address itself
903 * @param last_validation when was the address validated last
904 * @param valid_until until when is the address valid
905 * @param next_validation when will we try to revalidate the address next
906 * @param state where are we in the validation state machine
909 process_validation_cb (void *cls,
910 const struct GNUNET_PeerIdentity *peer,
911 const struct GNUNET_HELLO_Address *address,
912 struct GNUNET_TIME_Absolute last_validation,
913 struct GNUNET_TIME_Absolute valid_until,
914 struct GNUNET_TIME_Absolute next_validation,
915 enum GNUNET_TRANSPORT_ValidationState state)
917 if ((NULL == peer) && (NULL == address))
919 if (monitor_validation)
923 _("Monitor disconnected from transport service. Reconnecting.\n"));
929 if (GNUNET_SCHEDULER_NO_TASK != end)
930 GNUNET_SCHEDULER_cancel (end);
931 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
934 if ((NULL == peer) || (NULL == address))
936 /* invalid response */
938 if (GNUNET_SCHEDULER_NO_TASK != end)
939 GNUNET_SCHEDULER_cancel (end);
940 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
943 resolve_validation_address (peer,
956 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
957 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
959 (uint16_t) head->bnd_port,
960 (uint16_t) head->adv_port);
962 head->tst = GNUNET_NAT_test_start (cfg,
963 (0 == strcasecmp (head->name, "udp")) ? GNUNET_NO : GNUNET_YES,
964 (uint16_t) head->bnd_port,
965 (uint16_t) head->adv_port,
967 &result_callback, head);
972 * Test our plugin's configuration (NAT traversal, etc.).
974 * @param cfg configuration to test
977 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
981 unsigned long long bnd_port;
982 unsigned long long adv_port;
983 struct TestContext *tc;
987 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
990 FPRINTF (stderr, "%s", _
991 ("No transport plugins configured, peer will never communicate\n"));
996 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
998 char section[12 + strlen (tok)];
999 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
1001 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
1005 _("No port configured for plugin `%s', cannot test it\n"), tok);
1008 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
1009 "ADVERTISED_PORT", &adv_port))
1010 adv_port = bnd_port;
1012 tc = GNUNET_new (struct TestContext);
1013 tc->name = GNUNET_strdup (tok);
1014 tc->adv_port = adv_port;
1015 tc->bnd_port = bnd_port;
1016 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
1018 GNUNET_free(plugins);
1020 if ((NULL != head) && (NULL == resolver))
1022 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
1023 resolver = GNUNET_OS_start_process (GNUNET_YES,
1024 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
1027 "gnunet-service-resolver", NULL);
1028 if (NULL == resolver)
1030 FPRINTF (stderr, _("Failed to start resolver!\n"));
1034 GNUNET_free(binary);
1035 GNUNET_RESOLVER_connect (cfg);
1042 * Function called to notify a client about the socket
1043 * begin ready to queue more data. @a buf will be
1044 * NULL and @a size zero if the socket was closed for
1045 * writing in the meantime.
1047 * @param cls closure
1048 * @param size number of bytes available in @a buf
1049 * @param buf where the callee should write the message
1050 * @return number of bytes written to @a buf
1053 transmit_data (void *cls,
1057 struct GNUNET_MessageHeader *m = buf;
1059 if ((NULL == buf) || (0 == size))
1065 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
1066 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
1067 m->size = ntohs (size);
1068 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
1069 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
1070 traffic_sent += size;
1071 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
1073 GNUNET_TIME_UNIT_FOREVER_REL,
1074 &transmit_data, NULL);
1076 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
1083 * Function called to notify transport users that another
1084 * peer connected to us.
1086 * @param cls closure
1087 * @param peer the peer that connected
1090 notify_connect (void *cls,
1091 const struct GNUNET_PeerIdentity *peer)
1093 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1098 /* all done, terminate instantly */
1099 FPRINTF (stdout, _("Successfully connected to `%s'\n"),
1100 GNUNET_i2s_full (peer));
1103 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1105 GNUNET_SCHEDULER_cancel (op_timeout);
1106 op_timeout = GNUNET_SCHEDULER_NO_TASK;
1109 if (GNUNET_SCHEDULER_NO_TASK != end)
1110 GNUNET_SCHEDULER_cancel (end);
1111 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1116 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1118 GNUNET_SCHEDULER_cancel (op_timeout);
1119 op_timeout = GNUNET_SCHEDULER_NO_TASK;
1123 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
1124 GNUNET_i2s (&pid), BLOCKSIZE);
1125 start_time = GNUNET_TIME_absolute_get ();
1127 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
1129 GNUNET_TIME_UNIT_FOREVER_REL,
1140 * Function called to notify transport users that another
1141 * peer disconnected from us.
1143 * @param cls closure
1144 * @param peer the peer that disconnected
1147 notify_disconnect (void *cls,
1148 const struct GNUNET_PeerIdentity *peer)
1150 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1155 /* all done, terminate instantly */
1156 FPRINTF (stdout, _("Successfully disconnected from `%s'\n"),
1157 GNUNET_i2s_full (peer));
1160 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1162 GNUNET_SCHEDULER_cancel (op_timeout);
1163 op_timeout = GNUNET_SCHEDULER_NO_TASK;
1166 if (GNUNET_SCHEDULER_NO_TASK != end)
1167 GNUNET_SCHEDULER_cancel (end);
1168 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1174 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
1179 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
1181 if (GNUNET_SCHEDULER_NO_TASK != end)
1182 GNUNET_SCHEDULER_cancel (end);
1189 * Function called to notify transport users that another
1190 * peer connected to us.
1192 * @param cls closure
1193 * @param peer the peer that connected
1196 monitor_notify_connect (void *cls,
1197 const struct GNUNET_PeerIdentity *peer)
1199 monitor_connect_counter++;
1200 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1201 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1204 _("%24s: %-17s %4s (%u connections in total)\n"),
1208 monitor_connect_counter);
1213 * Function called to notify transport users that another
1214 * peer disconnected from us.
1216 * @param cls closure
1217 * @param peer the peer that disconnected
1220 monitor_notify_disconnect (void *cls,
1221 const struct GNUNET_PeerIdentity *peer)
1223 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1224 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1226 GNUNET_assert(monitor_connect_counter > 0);
1227 monitor_connect_counter--;
1230 _("%24s: %-17s %4s (%u connections in total)\n"),
1232 _("Disconnected from"),
1234 monitor_connect_counter);
1239 * Function called by the transport for each received message.
1241 * @param cls closure
1242 * @param peer (claimed) identity of the other peer
1243 * @param message the message
1246 notify_receive (void *cls,
1247 const struct GNUNET_PeerIdentity *peer,
1248 const struct GNUNET_MessageHeader *message)
1250 if (benchmark_receive)
1252 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
1256 _("Received %u bytes from %s\n"),
1257 (unsigned int) ntohs (message->size),
1260 if (traffic_received == 0)
1261 start_time = GNUNET_TIME_absolute_get ();
1262 traffic_received += ntohs (message->size);
1269 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1270 const struct GNUNET_HELLO_Address *address,
1272 enum GNUNET_TRANSPORT_PeerState state,
1273 struct GNUNET_TIME_Absolute state_timeout);
1277 print_info (const struct GNUNET_PeerIdentity *id,
1278 const char *transport,
1280 enum GNUNET_TRANSPORT_PeerState state,
1281 struct GNUNET_TIME_Absolute state_timeout)
1284 if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1285 (GNUNET_YES == monitor_connections))
1288 _("Peer `%s': %s %s in state `%s' until %s\n"),
1290 (NULL == transport) ? "<none>" : transport,
1291 (NULL == transport) ? "<none>" : addr,
1292 GNUNET_TRANSPORT_ps2s (state),
1293 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1295 else if ( (GNUNET_YES == iterate_connections) &&
1296 (GNUNET_TRANSPORT_is_connected(state)))
1298 /* Only connected peers, skip state */
1300 _("Peer `%s': %s %s\n"),
1309 * Function called with a textual representation of an address. This
1310 * function will be called several times with different possible
1311 * textual representations, and a last time with @a address being NULL
1312 * to signal the end of the iteration. Note that @a address NULL
1313 * always is the last call, regardless of the value in @a res.
1315 * @param cls closure
1316 * @param address NULL on end of iteration,
1317 * otherwise 0-terminated printable UTF-8 string,
1318 * in particular an empty string if @a res is #GNUNET_NO
1319 * @param res result of the address to string conversion:
1320 * if #GNUNET_OK: conversion successful
1321 * if #GNUNET_NO: address was invalid (or not supported)
1322 * if #GNUNET_SYSERR: communication error (IPC error)
1325 process_peer_string (void *cls,
1326 const char *address,
1329 struct PeerResolutionContext *rc = cls;
1331 if (NULL != address)
1333 if (GNUNET_SYSERR == res)
1336 "Failed to convert address for peer `%s' plugin `%s' length %lu to string \n",
1337 GNUNET_i2s (&rc->id),
1338 rc->addrcp->transport_name,
1339 rc->addrcp->address_length);
1340 print_info (&rc->id,
1345 rc->printed = GNUNET_YES;
1348 if (GNUNET_OK == res)
1350 print_info (&rc->id,
1355 rc->printed = GNUNET_YES;
1356 return; /* Wait for done call */
1358 /* GNUNET_NO == res: ignore, was simply not supported */
1361 /* NULL == address, last call, we are done */
1363 GNUNET_assert (address_resolutions > 0);
1364 address_resolutions--;
1365 if (GNUNET_NO == rc->printed)
1367 if (numeric == GNUNET_NO)
1369 /* Failed to resolve address, try numeric lookup
1370 (note: this should not be needed, as transport
1371 should fallback to numeric conversion if DNS takes
1373 resolve_peer_address (&rc->id,
1381 print_info (&rc->id,
1388 GNUNET_free (rc->transport);
1389 GNUNET_free (rc->addrcp);
1390 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1392 if ((0 == address_resolutions) && (iterate_connections))
1394 if (GNUNET_SCHEDULER_NO_TASK != end)
1396 GNUNET_SCHEDULER_cancel (end);
1397 end = GNUNET_SCHEDULER_NO_TASK;
1399 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1401 GNUNET_SCHEDULER_cancel (op_timeout);
1402 op_timeout = GNUNET_SCHEDULER_NO_TASK;
1405 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1411 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1412 const struct GNUNET_HELLO_Address *address,
1414 enum GNUNET_TRANSPORT_PeerState state,
1415 struct GNUNET_TIME_Absolute state_timeout)
1417 struct PeerResolutionContext *rc;
1419 rc = GNUNET_new (struct PeerResolutionContext);
1420 GNUNET_assert(NULL != rc);
1421 GNUNET_CONTAINER_DLL_insert(rc_head, rc_tail, rc);
1422 address_resolutions++;
1425 rc->transport = GNUNET_strdup(address->transport_name);
1426 rc->addrcp = GNUNET_HELLO_address_copy (address);
1427 rc->printed = GNUNET_NO;
1429 rc->state_timeout = state_timeout;
1430 /* Resolve address to string */
1431 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1435 &process_peer_string, rc);
1440 * Function called with information about a peers during a one shot iteration
1442 * @param cls closure
1443 * @param peer identity of the peer, NULL for final callback when operation done
1444 * @param address binary address used to communicate with this peer,
1445 * NULL on disconnect or when done
1446 * @param state current state this peer is in
1447 * @param state_timeout time out for the current state
1450 process_peer_iteration_cb (void *cls,
1451 const struct GNUNET_PeerIdentity *peer,
1452 const struct GNUNET_HELLO_Address *address,
1453 enum GNUNET_TRANSPORT_PeerState state,
1454 struct GNUNET_TIME_Absolute state_timeout)
1459 address_resolution_in_progress = GNUNET_NO;
1461 if (GNUNET_SCHEDULER_NO_TASK != end)
1462 GNUNET_SCHEDULER_cancel (end);
1463 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1467 if ( (GNUNET_NO == iterate_all) &&
1468 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1469 return; /* Display only connected peers */
1471 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1472 GNUNET_SCHEDULER_cancel (op_timeout);
1473 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 "Received address for peer `%s': %s\n",
1480 address->transport_name);
1482 if (NULL != address)
1483 resolve_peer_address (peer, address, numeric, state, state_timeout);
1485 print_info (peer, NULL, NULL, state, state_timeout);
1490 * Function called with information about a peers
1492 * @param cls closure
1493 * @param peer identity of the peer, NULL for final callback when operation done
1494 * @param address binary address used to communicate with this peer,
1495 * NULL on disconnect or when done
1496 * @param state current state this peer is in
1497 * @param state_timeout time out for the current state
1501 process_peer_monitoring_cb (void *cls,
1502 const struct GNUNET_PeerIdentity *peer,
1503 const struct GNUNET_HELLO_Address *address,
1504 enum GNUNET_TRANSPORT_PeerState state,
1505 struct GNUNET_TIME_Absolute state_timeout)
1507 struct MonitoredPeer *m;
1513 _("Monitor disconnected from transport service. Reconnecting.\n"));
1517 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1518 GNUNET_SCHEDULER_cancel (op_timeout);
1519 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1523 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
1525 m = GNUNET_new (struct MonitoredPeer);
1526 GNUNET_CONTAINER_multipeermap_put (monitored_peers, peer,
1527 m, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1531 if ( (m->state == state) &&
1532 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1533 ((NULL == address) && (NULL == m->address)))
1535 return; /* No real change */
1537 if ( (m->state == state) && ((NULL != address) && (NULL != m->address)) &&
1538 (0 == GNUNET_HELLO_address_cmp(m->address, address)))
1539 return; /* No real change */
1542 if (NULL != m->address)
1544 GNUNET_free (m->address);
1547 if (NULL != address)
1548 m->address = GNUNET_HELLO_address_copy (address);
1550 m->state_timeout = state_timeout;
1552 if (NULL != address)
1553 resolve_peer_address (peer,
1568 try_connect_cb (void *cls,
1571 static int retries = 0;
1573 if (GNUNET_OK == result)
1580 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1586 _("Failed to send connect request to transport service\n"));
1587 if (GNUNET_SCHEDULER_NO_TASK != end)
1588 GNUNET_SCHEDULER_cancel (end);
1590 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1597 try_disconnect_cb (void *cls,
1600 static int retries = 0;
1601 if (GNUNET_OK == result)
1608 tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1612 FPRINTF (stderr, "%s",
1613 _("Failed to send connect request to transport service\n"));
1614 if (GNUNET_SCHEDULER_NO_TASK != end)
1615 GNUNET_SCHEDULER_cancel (end);
1617 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1624 * Function called with the result of the check if the 'transport'
1625 * service is running.
1627 * @param cls closure with our configuration
1628 * @param result #GNUNET_YES if transport is running
1631 testservice_task (void *cls, int result)
1636 if (GNUNET_YES != result)
1638 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1644 != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
1647 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
1651 counter = benchmark_send + benchmark_receive + iterate_connections
1652 + monitor_connections + monitor_connects + try_connect + try_disconnect +
1653 + iterate_validation + monitor_validation;
1658 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
1659 "connect", "benchmark send", "benchmark receive", "information",
1660 "monitor", "events");
1666 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
1667 "connect", "benchmark send", "benchmark receive", "information",
1668 "monitor", "events");
1672 if (try_connect) /* -C: Connect to peer */
1676 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1681 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1682 ¬ify_connect, ¬ify_disconnect);
1685 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1689 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1691 if (NULL == tc_handle)
1693 FPRINTF (stderr, "%s",
1694 _("Failed to send request to transport service\n"));
1698 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1702 else if (try_disconnect) /* -D: Disconnect from peer */
1706 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1711 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1712 ¬ify_connect, ¬ify_disconnect);
1715 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1719 tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1721 if (NULL == tc_handle)
1723 FPRINTF (stderr, "%s",
1724 _("Failed to send request to transport service\n"));
1728 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1732 else if (benchmark_send) /* -s: Benchmark sending */
1736 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1741 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1742 ¬ify_connect, ¬ify_disconnect);
1745 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1749 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1751 if (NULL == tc_handle)
1753 FPRINTF (stderr, "%s",
1754 _("Failed to send request to transport service\n"));
1758 start_time = GNUNET_TIME_absolute_get ();
1759 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1762 else if (benchmark_receive) /* -b: Benchmark receiving */
1764 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive, NULL,
1768 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1773 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
1774 start_time = GNUNET_TIME_absolute_get ();
1777 else if (iterate_connections) /* -i: List information about peers once */
1779 address_resolution_in_progress = GNUNET_YES;
1780 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1781 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1782 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1785 else if (monitor_connections) /* -m: List information about peers continuously */
1787 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1788 address_resolution_in_progress = GNUNET_YES;
1789 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1791 &process_peer_monitoring_cb, (void *) cfg);
1793 else if (iterate_validation) /* -d: Print information about validations */
1795 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1796 (NULL == cpid) ? NULL : &pid,
1797 GNUNET_YES, TIMEOUT,
1798 &process_validation_cb, (void *) cfg);
1800 else if (monitor_validation) /* -f: Print information about validations continuously */
1802 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1803 (NULL == cpid) ? NULL : &pid,
1805 &process_validation_cb, (void *) cfg);
1807 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1809 monitor_connect_counter = 0;
1810 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1811 &monitor_notify_connect,
1812 &monitor_notify_disconnect);
1817 _("Failed to connect to transport service\n"));
1829 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1836 * Main function that will be run by the scheduler.
1838 * @param cls closure
1839 * @param args remaining command-line arguments
1840 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1841 * @param mycfg configuration
1846 const char *cfgfile,
1847 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1849 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1850 if (test_configuration)
1852 do_test_configuration (cfg);
1855 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1856 &testservice_task, (void *) cfg);
1860 main (int argc, char * const *argv)
1863 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1865 gettext_noop ("print information for all peers (instead of only connected peers)"),
1866 0, &GNUNET_GETOPT_set_one, &iterate_all },
1867 { 'b', "benchmark", NULL,
1868 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1869 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1871 NULL, gettext_noop ("connect to a peer"), 0,
1872 &GNUNET_GETOPT_set_one, &try_connect },
1873 { 'D', "disconnect",
1874 NULL, gettext_noop ("disconnect to a peer"), 0,
1875 &GNUNET_GETOPT_set_one, &try_disconnect },
1876 { 'd', "validation", NULL,
1877 gettext_noop ("print information for all pending validations "),
1878 0, &GNUNET_GETOPT_set_one, &iterate_validation },
1879 { 'f', "monitorvalidation", NULL,
1880 gettext_noop ("print information for all pending validations continuously"),
1881 0, &GNUNET_GETOPT_set_one, &monitor_validation },
1882 { 'i', "information", NULL,
1883 gettext_noop ("provide information about all current connections (once)"),
1884 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1885 { 'm', "monitor", NULL,
1886 gettext_noop ("provide information about all current connections (continuously)"),
1887 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1888 { 'e', "events", NULL,
1889 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1890 0, &GNUNET_GETOPT_set_one, &monitor_connects },
1892 NULL, gettext_noop ("do not resolve hostnames"), 0,
1893 &GNUNET_GETOPT_set_one, &numeric },
1894 { 'p', "peer", "PEER",
1895 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1897 { 's', "send", NULL, gettext_noop
1898 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1899 &GNUNET_GETOPT_set_one, &benchmark_send },
1900 { 't', "test", NULL,
1901 gettext_noop ("test transport configuration (involves external server)"),
1902 0, &GNUNET_GETOPT_set_one, &test_configuration },
1903 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1904 GNUNET_GETOPT_OPTION_END
1907 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1910 res = GNUNET_PROGRAM_run (argc, argv,
1912 gettext_noop ("Direct access to transport service."),
1915 GNUNET_free((void *) argv);
1916 if (GNUNET_OK == res)
1921 /* end of gnunet-transport.c */