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)
42 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
43 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
46 struct ValidationResolutionContext
48 struct ValidationResolutionContext *next;
50 struct ValidationResolutionContext *prev;
52 struct GNUNET_PeerIdentity id;
54 struct GNUNET_HELLO_Address *addrcp;
56 struct GNUNET_TIME_Absolute last_validation;
58 struct GNUNET_TIME_Absolute valid_until;
60 struct GNUNET_TIME_Absolute next_validation;
62 enum GNUNET_TRANSPORT_ValidationState state;
64 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
74 enum GNUNET_TRANSPORT_PeerState state;
76 struct GNUNET_TIME_Absolute state_timeout;
78 struct GNUNET_HELLO_Address *address;
82 struct PeerResolutionContext
84 struct PeerResolutionContext *next;
85 struct PeerResolutionContext *prev;
86 struct GNUNET_PeerIdentity id;
87 struct GNUNET_HELLO_Address *addrcp;
88 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
89 enum GNUNET_TRANSPORT_PeerState state;
90 struct GNUNET_TIME_Absolute state_timeout;
97 * Context for a plugin test.
104 struct TestContext *prev;
109 struct TestContext *next;
112 * Handle to the active NAT test.
114 struct GNUNET_NAT_Test *tst;
117 * Task identifier for the timeout.
119 GNUNET_SCHEDULER_TaskIdentifier tsk;
122 * Name of plugin under test.
129 unsigned long long bnd_port;
134 unsigned long long adv_port;
145 * NAT returned success
147 NAT_TEST_SUCCESS = GNUNET_OK,
150 * NAT returned failure
152 NAT_TEST_FAIL = GNUNET_NO,
155 * NAT returned failure while running test
157 NAT_TEST_INTERNAL_FAIL = GNUNET_SYSERR,
160 * We could not start the test
162 NAT_TEST_FAILED_TO_START = 2,
165 * We had a timeout while running the test
172 * Benchmarking block size in KB
177 * Which peer should we connect to?
182 * Handle to transport service.
184 static struct GNUNET_TRANSPORT_Handle *handle;
187 * Configuration handle
189 static struct GNUNET_CONFIGURATION_Handle *cfg;
194 struct GNUNET_TRANSPORT_TryConnectHandle *tc_handle;
199 static int benchmark_send;
204 static int benchmark_receive;
209 static int benchmark_receive;
214 static int iterate_connections;
219 static int iterate_validation;
224 static int iterate_all;
229 static int test_configuration;
234 static int monitor_connects;
239 static int monitor_connections;
244 static int monitor_validation;
249 static int try_connect;
254 static int try_disconnect;
262 * Global return value (0 success).
267 * Current number of connections in monitor mode
269 static int monitor_connect_counter;
272 * Number of bytes of traffic we received so far.
274 static unsigned long long traffic_received;
277 * Number of bytes of traffic we sent so far.
279 static unsigned long long traffic_sent;
282 * Starting time of transmitting/receiving data.
284 static struct GNUNET_TIME_Absolute start_time;
287 * Handle for current transmission request.
289 static struct GNUNET_TRANSPORT_TransmitHandle *th;
292 * Map storing information about monitored peers
294 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
299 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
304 static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic;
307 * Identity of the peer we transmit to / connect to.
308 * (equivalent to 'cpid' string).
310 static struct GNUNET_PeerIdentity pid;
313 * Task scheduled for cleanup / termination of the process.
315 static GNUNET_SCHEDULER_TaskIdentifier end;
318 * Task for operation timeout
320 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
323 * Selected level of verbosity.
325 static int verbosity;
328 * Resolver process handle.
330 struct GNUNET_OS_Process *resolver;
333 * Number of address resolutions pending
335 static unsigned int address_resolutions;
338 * Address resolutions pending in progress
340 static unsigned int address_resolution_in_progress;
343 * DLL for NAT Test Contexts: head
345 struct TestContext *head;
348 * DLL for NAT Test Contexts: tail
350 struct TestContext *tail;
352 static struct ValidationResolutionContext *vc_head;
354 static struct ValidationResolutionContext *vc_tail;
356 static struct PeerResolutionContext *rc_head;
358 static struct PeerResolutionContext *rc_tail;
362 destroy_it (void *cls,
363 const struct GNUNET_PeerIdentity *key,
366 struct MonitoredPeer *m = value;
368 GNUNET_assert (GNUNET_OK ==
369 GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
372 GNUNET_free_non_null (m->address);
379 * Task run in monitor mode when the user presses CTRL-C to abort.
380 * Stops monitoring activity.
383 * @param tc scheduler context
386 shutdown_task (void *cls,
387 const struct GNUNET_SCHEDULER_TaskContext *tc)
389 struct GNUNET_TIME_Relative duration;
390 struct ValidationResolutionContext *cur;
391 struct ValidationResolutionContext *next;
393 end = GNUNET_SCHEDULER_NO_TASK;
394 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
396 GNUNET_SCHEDULER_cancel (op_timeout);
397 op_timeout = GNUNET_SCHEDULER_NO_TASK;
399 if (NULL != tc_handle)
401 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
406 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
411 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
416 for (cur = next; NULL != cur; cur = next)
420 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
421 GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, cur);
422 GNUNET_free (cur->transport);
423 GNUNET_HELLO_address_free (cur->addrcp);
429 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
434 GNUNET_TRANSPORT_disconnect (handle);
439 duration = GNUNET_TIME_absolute_get_duration (start_time);
441 _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
442 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
444 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
446 if (benchmark_receive)
448 duration = GNUNET_TIME_absolute_get_duration (start_time);
450 _("Received %llu bytes/s (%llu bytes in %s)\n"),
451 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
453 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
456 if (NULL != monitored_peers)
458 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
459 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
460 monitored_peers = NULL;
466 operation_timeout (void *cls,
467 const struct GNUNET_SCHEDULER_TaskContext *tc)
469 struct PeerResolutionContext *cur;
470 struct PeerResolutionContext *next;
471 op_timeout = GNUNET_SCHEDULER_NO_TASK;
472 if ((try_connect) || (benchmark_send) || (benchmark_receive))
475 _("Failed to connect to `%s'\n"),
476 GNUNET_i2s_full (&pid));
477 if (GNUNET_SCHEDULER_NO_TASK != end)
478 GNUNET_SCHEDULER_cancel (end);
479 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
483 if (iterate_connections)
486 while (NULL != (cur = next))
490 _("Failed to resolve address for peer `%s'\n"),
491 GNUNET_i2s (&cur->addrcp->peer));
493 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
494 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
495 GNUNET_free(cur->transport);
496 GNUNET_free(cur->addrcp);
502 _("Failed to list connections, timeout occured\n"));
503 if (GNUNET_SCHEDULER_NO_TASK != end)
504 GNUNET_SCHEDULER_cancel (end);
505 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
517 * Display the result of the test.
519 * @param tc test context
520 * @param result #GNUNET_YES on success
523 display_test_result (struct TestContext *tc,
524 enum TestResult result)
529 _("Configuration for plugin `%s' did not work!\n"),
532 case NAT_TEST_SUCCESS:
534 _("Configuration for plugin `%s' did work!\n"),
537 case NAT_TEST_INTERNAL_FAIL:
539 _("Internal NAT error while running test for plugin `%s'\n"),
542 case NAT_TEST_FAILED_TO_START:
544 _("Failed to start NAT test for plugin `%s'\n"),
547 case NAT_TEST_TIMEOUT:
549 _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
556 if (GNUNET_YES != result)
559 _("Configuration for plugin `%s' did not work!\n"),
565 _("Configuration for plugin `%s' is working!\n"),
568 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
570 GNUNET_SCHEDULER_cancel (tc->tsk);
571 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
575 GNUNET_NAT_test_stop (tc->tst);
579 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
580 GNUNET_free (tc->name);
583 if ((NULL == head) && (NULL != resolver))
585 GNUNET_break (0 == GNUNET_OS_process_kill (resolver,
587 GNUNET_OS_process_destroy (resolver);
595 * Function called by NAT to report the outcome of the nat-test.
596 * Clean up and update GUI.
598 * @param cls test context
599 * @param success currently always #GNUNET_OK
600 * @param emsg error message, NULL on success
603 result_callback (void *cls,
604 enum GNUNET_NAT_StatusCode result)
606 struct TestContext *tc = cls;
608 display_test_result (tc, result);
613 * Resolve address we got a validation state for to a string.
615 * @param id peer identity the address is for
616 * @param address the address itself
617 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
618 * @param last_validation when was the address validated last
619 * @param valid_until until when is the address valid
620 * @param next_validation when will we try to revalidate the address next
621 * @param state where are we in the validation state machine
624 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
625 const struct GNUNET_HELLO_Address *address,
627 struct GNUNET_TIME_Absolute last_validation,
628 struct GNUNET_TIME_Absolute valid_until,
629 struct GNUNET_TIME_Absolute next_validation,
630 enum GNUNET_TRANSPORT_ValidationState state);
634 * Function to call with a textual representation of an address. This
635 * function will be called several times with different possible
636 * textual representations, and a last time with @address being NULL
637 * to signal the end of the iteration. Note that @address NULL
638 * always is the last call, regardless of the value in @a res.
641 * @param address NULL on end of iteration,
642 * otherwise 0-terminated printable UTF-8 string,
643 * in particular an empty string if @a res is #GNUNET_NO
644 * @param res result of the address to string conversion:
645 * if #GNUNET_OK: conversion successful
646 * if #GNUNET_NO: address was invalid (or not supported)
647 * if #GNUNET_SYSERR: communication error (IPC error)
650 process_validation_string (void *cls,
654 struct ValidationResolutionContext *vc = cls;
661 if (GNUNET_SYSERR == res)
664 "Failed to convert address for peer `%s' plugin `%s' length %lu to string \n",
665 GNUNET_i2s (&vc->id),
666 vc->addrcp->transport_name,
667 vc->addrcp->address_length);
669 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
670 s_valid = GNUNET_strdup ("never");
672 s_valid = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
674 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
675 s_last = GNUNET_strdup ("never");
677 s_last = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
679 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
680 s_next = GNUNET_strdup ("never");
682 s_next = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
685 _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
686 GNUNET_i2s (&vc->id),
687 (GNUNET_OK == res) ? address : "<invalid address>",
688 (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
689 "Valid until : ", s_valid,
690 "Last validation: ",s_last,
691 "Next validation: ", s_next);
692 GNUNET_free (s_valid);
693 GNUNET_free (s_last);
694 GNUNET_free (s_next);
695 vc->printed = GNUNET_YES;
698 /* last call, we are done */
699 GNUNET_assert (address_resolutions > 0);
700 address_resolutions--;
701 if ( (GNUNET_SYSERR == res) &&
702 (GNUNET_NO == vc->printed))
704 if (numeric == GNUNET_NO)
706 /* Failed to resolve address, try numeric lookup
707 (note: this should be unnecessary, as
708 transport should fallback to numeric lookup
709 internally if DNS takes too long anyway) */
710 resolve_validation_address (&vc->id,
721 _("Peer `%s' %s `%s' \n"),
722 GNUNET_i2s (&vc->id),
723 "<unable to resolve address>",
724 GNUNET_TRANSPORT_vs2s (vc->state));
727 GNUNET_free (vc->transport);
728 GNUNET_free (vc->addrcp);
729 GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, vc);
731 if ((0 == address_resolutions) && (iterate_validation))
733 if (GNUNET_SCHEDULER_NO_TASK != end)
735 GNUNET_SCHEDULER_cancel (end);
736 end = GNUNET_SCHEDULER_NO_TASK;
738 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
740 GNUNET_SCHEDULER_cancel (op_timeout);
741 op_timeout = GNUNET_SCHEDULER_NO_TASK;
744 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
750 * Resolve address we got a validation state for to a string.
752 * @param id peer identity the address is for
753 * @param address the address itself
754 * @param numeric #GNUNET_YES to disable DNS, #GNUNET_NO to try reverse lookup
755 * @param last_validation when was the address validated last
756 * @param valid_until until when is the address valid
757 * @param next_validation when will we try to revalidate the address next
758 * @param state where are we in the validation state machine
761 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
762 const struct GNUNET_HELLO_Address *address,
764 struct GNUNET_TIME_Absolute last_validation,
765 struct GNUNET_TIME_Absolute valid_until,
766 struct GNUNET_TIME_Absolute next_validation,
767 enum GNUNET_TRANSPORT_ValidationState state)
769 struct ValidationResolutionContext *vc;
771 vc = GNUNET_new (struct ValidationResolutionContext);
772 GNUNET_assert(NULL != vc);
773 GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
774 address_resolutions++;
777 vc->transport = GNUNET_strdup(address->transport_name);
778 vc->addrcp = GNUNET_HELLO_address_copy (address);
779 vc->printed = GNUNET_NO;
781 vc->last_validation = last_validation;
782 vc->valid_until = valid_until;
783 vc->next_validation = next_validation;
785 /* Resolve address to string */
786 vc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
790 &process_validation_string, vc);
795 * Resolve address we got a validation state for to a string.
798 * @param peer peer identity the address is for
799 * @param address the address itself
800 * @param last_validation when was the address validated last
801 * @param valid_until until when is the address valid
802 * @param next_validation when will we try to revalidate the address next
803 * @param state where are we in the validation state machine
806 process_validation_cb (void *cls,
807 const struct GNUNET_PeerIdentity *peer,
808 const struct GNUNET_HELLO_Address *address,
809 struct GNUNET_TIME_Absolute last_validation,
810 struct GNUNET_TIME_Absolute valid_until,
811 struct GNUNET_TIME_Absolute next_validation,
812 enum GNUNET_TRANSPORT_ValidationState state)
814 if ((NULL == peer) && (NULL == address))
816 if (monitor_validation)
820 _("Monitor disconnected from transport service. Reconnecting.\n"));
826 if (GNUNET_SCHEDULER_NO_TASK != end)
827 GNUNET_SCHEDULER_cancel (end);
828 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
831 if ((NULL == peer) || (NULL == address))
833 /* invalid response */
835 if (GNUNET_SCHEDULER_NO_TASK != end)
836 GNUNET_SCHEDULER_cancel (end);
837 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
840 resolve_validation_address (peer,
853 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
854 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
856 (uint16_t) head->bnd_port,
857 (uint16_t) head->adv_port);
859 head->tst = GNUNET_NAT_test_start (cfg,
860 (0 == strcasecmp (head->name, "udp")) ? GNUNET_NO : GNUNET_YES,
861 (uint16_t) head->bnd_port,
862 (uint16_t) head->adv_port,
864 &result_callback, head);
869 * Test our plugin's configuration (NAT traversal, etc.).
871 * @param cfg configuration to test
874 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
878 unsigned long long bnd_port;
879 unsigned long long adv_port;
880 struct TestContext *tc;
884 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
887 FPRINTF (stderr, "%s", _
888 ("No transport plugins configured, peer will never communicate\n"));
893 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
895 char section[12 + strlen (tok)];
896 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
898 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
902 _("No port configured for plugin `%s', cannot test it\n"), tok);
905 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
906 "ADVERTISED_PORT", &adv_port))
909 tc = GNUNET_new (struct TestContext);
910 tc->name = GNUNET_strdup (tok);
911 tc->adv_port = adv_port;
912 tc->bnd_port = bnd_port;
913 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
915 GNUNET_free(plugins);
917 if ((NULL != head) && (NULL == resolver))
919 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
920 resolver = GNUNET_OS_start_process (GNUNET_YES,
921 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
924 "gnunet-service-resolver", NULL);
925 if (NULL == resolver)
927 FPRINTF (stderr, _("Failed to start resolver!\n"));
932 GNUNET_RESOLVER_connect (cfg);
939 * Function called to notify a client about the socket
940 * begin ready to queue more data. @a buf will be
941 * NULL and @a size zero if the socket was closed for
942 * writing in the meantime.
945 * @param size number of bytes available in @a buf
946 * @param buf where the callee should write the message
947 * @return number of bytes written to @a buf
950 transmit_data (void *cls,
954 struct GNUNET_MessageHeader *m = buf;
956 if ((NULL == buf) || (0 == size))
962 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
963 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
964 m->size = ntohs (size);
965 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
966 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
967 traffic_sent += size;
968 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
970 GNUNET_TIME_UNIT_FOREVER_REL,
971 &transmit_data, NULL);
973 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
980 * Function called to notify transport users that another
981 * peer connected to us.
984 * @param peer the peer that connected
987 notify_connect (void *cls,
988 const struct GNUNET_PeerIdentity *peer)
990 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
995 /* all done, terminate instantly */
996 FPRINTF (stdout, _("Successfully connected to `%s'\n"),
997 GNUNET_i2s_full (peer));
1000 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1002 GNUNET_SCHEDULER_cancel (op_timeout);
1003 op_timeout = GNUNET_SCHEDULER_NO_TASK;
1006 if (GNUNET_SCHEDULER_NO_TASK != end)
1007 GNUNET_SCHEDULER_cancel (end);
1008 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1013 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1015 GNUNET_SCHEDULER_cancel (op_timeout);
1016 op_timeout = GNUNET_SCHEDULER_NO_TASK;
1020 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
1021 GNUNET_i2s (&pid), BLOCKSIZE);
1022 start_time = GNUNET_TIME_absolute_get ();
1024 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
1026 GNUNET_TIME_UNIT_FOREVER_REL,
1037 * Function called to notify transport users that another
1038 * peer disconnected from us.
1040 * @param cls closure
1041 * @param peer the peer that disconnected
1044 notify_disconnect (void *cls,
1045 const struct GNUNET_PeerIdentity *peer)
1047 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
1052 /* all done, terminate instantly */
1053 FPRINTF (stdout, _("Successfully disconnected from `%s'\n"),
1054 GNUNET_i2s_full (peer));
1057 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1059 GNUNET_SCHEDULER_cancel (op_timeout);
1060 op_timeout = GNUNET_SCHEDULER_NO_TASK;
1063 if (GNUNET_SCHEDULER_NO_TASK != end)
1064 GNUNET_SCHEDULER_cancel (end);
1065 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1071 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
1076 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
1078 if (GNUNET_SCHEDULER_NO_TASK != end)
1079 GNUNET_SCHEDULER_cancel (end);
1086 * Function called to notify transport users that another
1087 * peer connected to us.
1089 * @param cls closure
1090 * @param peer the peer that connected
1093 monitor_notify_connect (void *cls,
1094 const struct GNUNET_PeerIdentity *peer)
1096 monitor_connect_counter++;
1097 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1098 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1101 _("%24s: %-17s %4s (%u connections in total)\n"),
1105 monitor_connect_counter);
1110 * Function called to notify transport users that another
1111 * peer disconnected from us.
1113 * @param cls closure
1114 * @param peer the peer that disconnected
1117 monitor_notify_disconnect (void *cls,
1118 const struct GNUNET_PeerIdentity *peer)
1120 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1121 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1123 GNUNET_assert(monitor_connect_counter > 0);
1124 monitor_connect_counter--;
1127 _("%24s: %-17s %4s (%u connections in total)\n"),
1129 _("Disconnected from"),
1131 monitor_connect_counter);
1136 * Function called by the transport for each received message.
1138 * @param cls closure
1139 * @param peer (claimed) identity of the other peer
1140 * @param message the message
1143 notify_receive (void *cls,
1144 const struct GNUNET_PeerIdentity *peer,
1145 const struct GNUNET_MessageHeader *message)
1147 if (benchmark_receive)
1149 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
1153 _("Received %u bytes from %s\n"),
1154 (unsigned int) ntohs (message->size),
1157 if (traffic_received == 0)
1158 start_time = GNUNET_TIME_absolute_get ();
1159 traffic_received += ntohs (message->size);
1166 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1167 const struct GNUNET_HELLO_Address *address,
1169 enum GNUNET_TRANSPORT_PeerState state,
1170 struct GNUNET_TIME_Absolute state_timeout);
1174 print_info (const struct GNUNET_PeerIdentity *id,
1175 const char *transport,
1177 enum GNUNET_TRANSPORT_PeerState state,
1178 struct GNUNET_TIME_Absolute state_timeout)
1181 if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1182 (GNUNET_YES == monitor_connections))
1185 _("Peer `%s': %s %s in state `%s' until %s\n"),
1187 (NULL == transport) ? "<none>" : transport,
1188 (NULL == transport) ? "<none>" : addr,
1189 GNUNET_TRANSPORT_ps2s (state),
1190 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1192 else if ( (GNUNET_YES == iterate_connections) &&
1193 (GNUNET_TRANSPORT_is_connected(state)))
1195 /* Only connected peers, skip state */
1197 _("Peer `%s': %s %s\n"),
1206 * Function called with a textual representation of an address. This
1207 * function will be called several times with different possible
1208 * textual representations, and a last time with @address being NULL
1209 * to signal the end of the iteration. Note that @address NULL
1210 * always is the last call, regardless of the value in @a res.
1212 * @param cls closure
1213 * @param address NULL on end of iteration,
1214 * otherwise 0-terminated printable UTF-8 string,
1215 * in particular an empty string if @a res is #GNUNET_NO
1216 * @param res result of the address to string conversion:
1217 * if #GNUNET_OK: conversion successful
1218 * if #GNUNET_NO: address was invalid (or not supported)
1219 * if #GNUNET_SYSERR: communication error (IPC error)
1222 process_peer_string (void *cls,
1223 const char *address,
1226 struct PeerResolutionContext *rc = cls;
1228 if (NULL != address)
1230 if (GNUNET_SYSERR == res)
1233 "Failed to convert address for peer `%s' plugin `%s' length %lu to string \n",
1234 GNUNET_i2s (&rc->id),
1235 rc->addrcp->transport_name,
1236 rc->addrcp->address_length);
1237 print_info (&rc->id,
1242 rc->printed = GNUNET_YES;
1245 if (GNUNET_OK == res)
1247 print_info (&rc->id,
1252 rc->printed = GNUNET_YES;
1253 return; /* Wait for done call */
1255 /* GNUNET_NO == res: ignore, was simply not supported */
1258 /* NULL == address, last call, we are done */
1260 GNUNET_assert (address_resolutions > 0);
1261 address_resolutions--;
1262 if (GNUNET_NO == rc->printed)
1264 if (numeric == GNUNET_NO)
1266 /* Failed to resolve address, try numeric lookup
1267 (note: this should not be needed, as transport
1268 should fallback to numeric conversion if DNS takes
1270 resolve_peer_address (&rc->id,
1278 print_info (&rc->id,
1285 GNUNET_free (rc->transport);
1286 GNUNET_free (rc->addrcp);
1287 GNUNET_CONTAINER_DLL_remove (rc_head, rc_tail, rc);
1289 if ((0 == address_resolutions) && (iterate_connections))
1291 if (GNUNET_SCHEDULER_NO_TASK != end)
1293 GNUNET_SCHEDULER_cancel (end);
1294 end = GNUNET_SCHEDULER_NO_TASK;
1296 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1298 GNUNET_SCHEDULER_cancel (op_timeout);
1299 op_timeout = GNUNET_SCHEDULER_NO_TASK;
1302 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1308 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1309 const struct GNUNET_HELLO_Address *address,
1311 enum GNUNET_TRANSPORT_PeerState state,
1312 struct GNUNET_TIME_Absolute state_timeout)
1314 struct PeerResolutionContext *rc;
1316 rc = GNUNET_new (struct PeerResolutionContext);
1317 GNUNET_assert(NULL != rc);
1318 GNUNET_CONTAINER_DLL_insert(rc_head, rc_tail, rc);
1319 address_resolutions++;
1322 rc->transport = GNUNET_strdup(address->transport_name);
1323 rc->addrcp = GNUNET_HELLO_address_copy (address);
1324 rc->printed = GNUNET_NO;
1326 rc->state_timeout = state_timeout;
1327 /* Resolve address to string */
1328 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg,
1332 &process_peer_string, rc);
1337 * Function called with information about a peers during a one shot iteration
1339 * @param cls closure
1340 * @param peer identity of the peer, NULL for final callback when operation done
1341 * @param address binary address used to communicate with this peer,
1342 * NULL on disconnect or when done
1343 * @param state current state this peer is in
1344 * @param state_timeout time out for the current state
1347 process_peer_iteration_cb (void *cls,
1348 const struct GNUNET_PeerIdentity *peer,
1349 const struct GNUNET_HELLO_Address *address,
1350 enum GNUNET_TRANSPORT_PeerState state,
1351 struct GNUNET_TIME_Absolute state_timeout)
1356 address_resolution_in_progress = GNUNET_NO;
1358 if (GNUNET_SCHEDULER_NO_TASK != end)
1359 GNUNET_SCHEDULER_cancel (end);
1360 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1364 if ( (GNUNET_NO == iterate_all) &&
1365 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)))
1366 return; /* Display only connected peers */
1368 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1369 GNUNET_SCHEDULER_cancel (op_timeout);
1370 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 "Received address for peer `%s': %s\n",
1377 address->transport_name);
1379 if (NULL != address)
1380 resolve_peer_address (peer, address, numeric, state, state_timeout);
1382 print_info (peer, NULL, NULL, state, state_timeout);
1387 * Function called with information about a peers
1389 * @param cls closure
1390 * @param peer identity of the peer, NULL for final callback when operation done
1391 * @param address binary address used to communicate with this peer,
1392 * NULL on disconnect or when done
1393 * @param state current state this peer is in
1394 * @param state_timeout time out for the current state
1398 process_peer_monitoring_cb (void *cls,
1399 const struct GNUNET_PeerIdentity *peer,
1400 const struct GNUNET_HELLO_Address *address,
1401 enum GNUNET_TRANSPORT_PeerState state,
1402 struct GNUNET_TIME_Absolute state_timeout)
1404 struct MonitoredPeer *m;
1410 _("Monitor disconnected from transport service. Reconnecting.\n"));
1414 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1415 GNUNET_SCHEDULER_cancel (op_timeout);
1416 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT,
1420 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
1422 m = GNUNET_new (struct MonitoredPeer);
1423 GNUNET_CONTAINER_multipeermap_put (monitored_peers, peer,
1424 m, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1428 if ( (m->state == state) &&
1429 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1430 ((NULL == address) && (NULL == m->address)))
1432 return; /* No real change */
1434 if ( (m->state == state) && ((NULL != address) && (NULL != m->address)) &&
1435 (0 == GNUNET_HELLO_address_cmp(m->address, address)))
1436 return; /* No real change */
1439 if (NULL != m->address)
1441 GNUNET_free (m->address);
1444 if (NULL != address)
1445 m->address = GNUNET_HELLO_address_copy (address);
1447 m->state_timeout = state_timeout;
1449 if (NULL != address)
1450 resolve_peer_address (peer,
1465 try_connect_cb (void *cls,
1468 static int retries = 0;
1470 if (GNUNET_OK == result)
1477 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1483 _("Failed to send connect request to transport service\n"));
1484 if (GNUNET_SCHEDULER_NO_TASK != end)
1485 GNUNET_SCHEDULER_cancel (end);
1487 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1494 try_disconnect_cb (void *cls,
1497 static int retries = 0;
1498 if (GNUNET_OK == result)
1505 tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1509 FPRINTF (stderr, "%s",
1510 _("Failed to send connect request to transport service\n"));
1511 if (GNUNET_SCHEDULER_NO_TASK != end)
1512 GNUNET_SCHEDULER_cancel (end);
1514 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL);
1521 * Function called with the result of the check if the 'transport'
1522 * service is running.
1524 * @param cls closure with our configuration
1525 * @param result #GNUNET_YES if transport is running
1528 testservice_task (void *cls, int result)
1533 if (GNUNET_YES != result)
1535 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1541 != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
1544 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
1548 counter = benchmark_send + benchmark_receive + iterate_connections
1549 + monitor_connections + monitor_connects + try_connect + try_disconnect +
1550 + iterate_validation + monitor_validation;
1555 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
1556 "connect", "benchmark send", "benchmark receive", "information",
1557 "monitor", "events");
1563 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
1564 "connect", "benchmark send", "benchmark receive", "information",
1565 "monitor", "events");
1569 if (try_connect) /* -C: Connect to peer */
1573 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1578 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1579 ¬ify_connect, ¬ify_disconnect);
1582 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1586 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1588 if (NULL == tc_handle)
1590 FPRINTF (stderr, "%s",
1591 _("Failed to send request to transport service\n"));
1595 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1599 else if (try_disconnect) /* -D: Disconnect from peer */
1603 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1608 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1609 ¬ify_connect, ¬ify_disconnect);
1612 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1616 tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1618 if (NULL == tc_handle)
1620 FPRINTF (stderr, "%s",
1621 _("Failed to send request to transport service\n"));
1625 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1629 else if (benchmark_send) /* -s: Benchmark sending */
1633 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1638 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1639 ¬ify_connect, ¬ify_disconnect);
1642 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1646 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1648 if (NULL == tc_handle)
1650 FPRINTF (stderr, "%s",
1651 _("Failed to send request to transport service\n"));
1655 start_time = GNUNET_TIME_absolute_get ();
1656 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1659 else if (benchmark_receive) /* -b: Benchmark receiving */
1661 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive, NULL,
1665 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1670 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n"));
1671 start_time = GNUNET_TIME_absolute_get ();
1674 else if (iterate_connections) /* -i: List information about peers once */
1676 address_resolution_in_progress = GNUNET_YES;
1677 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1678 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1679 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1682 else if (monitor_connections) /* -m: List information about peers continuously */
1684 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1685 address_resolution_in_progress = GNUNET_YES;
1686 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1687 GNUNET_NO, TIMEOUT, &process_peer_monitoring_cb, (void *) cfg);
1689 else if (iterate_validation) /* -d: Print information about validations */
1691 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1692 (NULL == cpid) ? NULL : &pid,
1693 GNUNET_YES, TIMEOUT, &process_validation_cb, (void *) cfg);
1695 else if (monitor_validation) /* -f: Print information about validations continuously */
1697 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1698 (NULL == cpid) ? NULL : &pid,
1699 GNUNET_NO, TIMEOUT, &process_validation_cb, (void *) cfg);
1701 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1703 monitor_connect_counter = 0;
1704 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1705 &monitor_notify_connect,
1706 &monitor_notify_disconnect);
1709 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n"));
1721 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1729 * Main function that will be run by the scheduler.
1731 * @param cls closure
1732 * @param args remaining command-line arguments
1733 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1734 * @param mycfg configuration
1739 const char *cfgfile,
1740 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1742 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1743 if (test_configuration)
1745 do_test_configuration (cfg);
1748 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1749 &testservice_task, (void *) cfg);
1753 main (int argc, char * const *argv)
1756 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
1758 gettext_noop ("print information for all peers (instead of only connected peers)"),
1759 0, &GNUNET_GETOPT_set_one, &iterate_all },
1760 { 'b', "benchmark", NULL,
1761 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1762 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1764 NULL, gettext_noop ("connect to a peer"), 0,
1765 &GNUNET_GETOPT_set_one, &try_connect },
1766 { 'D', "disconnect",
1767 NULL, gettext_noop ("disconnect to a peer"), 0,
1768 &GNUNET_GETOPT_set_one, &try_disconnect },
1769 { 'd', "validation", NULL,
1770 gettext_noop ("print information for all pending validations "),
1771 0, &GNUNET_GETOPT_set_one, &iterate_validation },
1772 { 'f', "monitorvalidation", NULL,
1773 gettext_noop ("print information for all pending validations continously"),
1774 0, &GNUNET_GETOPT_set_one, &monitor_validation },
1775 { 'i', "information", NULL,
1776 gettext_noop ("provide information about all current connections (once)"),
1777 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1778 { 'm', "monitor", NULL,
1779 gettext_noop ("provide information about all current connections (continuously)"),
1780 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1781 { 'e', "events", NULL,
1782 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1783 0, &GNUNET_GETOPT_set_one, &monitor_connects },
1785 NULL, gettext_noop ("do not resolve hostnames"), 0,
1786 &GNUNET_GETOPT_set_one, &numeric },
1787 { 'p', "peer", "PEER",
1788 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1790 { 's', "send", NULL, gettext_noop
1791 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1792 &GNUNET_GETOPT_set_one, &benchmark_send },
1793 { 't', "test", NULL,
1794 gettext_noop ("test transport configuration (involves external server)"),
1795 0, &GNUNET_GETOPT_set_one, &test_configuration },
1796 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1797 GNUNET_GETOPT_OPTION_END
1800 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1803 res = GNUNET_PROGRAM_run (argc, argv,
1805 gettext_noop ("Direct access to transport service."),
1808 GNUNET_free((void *) argv);
1809 if (GNUNET_OK == res)
1814 /* end of gnunet-transport.c */