2 This file is part of GNUnet.
3 (C) 2011 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.
32 #include "gnunet_util_lib.h"
33 #include "gnunet_resolver_service.h"
34 #include "gnunet_protocols.h"
35 #include "gnunet_transport_service.h"
36 #include "gnunet_nat_lib.h"
39 * How long do we wait for the NAT test to report success?
40 * Should match NAT_SERVER_TIMEOUT in 'nat_test.c'.
42 #define TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 20)
43 #define RESOLUTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
44 #define OP_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
47 * Benchmarking block size in KB
52 * Which peer should we connect to?
57 * Handle to transport service.
59 static struct GNUNET_TRANSPORT_Handle *handle;
62 * Configuration handle
64 static struct GNUNET_CONFIGURATION_Handle *cfg;
69 struct GNUNET_TRANSPORT_TryConnectHandle * tc_handle;
74 static int benchmark_send;
79 static int benchmark_receive;
84 static int benchmark_receive;
89 static int iterate_connections;
94 static int iterate_validation;
99 static int iterate_all;
104 static int test_configuration;
109 static int monitor_connects;
114 static int monitor_connections;
119 static int monitor_validation;
124 static int try_connect;
129 static int try_disconnect;
137 * Global return value (0 success).
142 * Current number of connections in monitor mode
144 static int monitor_connect_counter;
147 * Number of bytes of traffic we received so far.
149 static unsigned long long traffic_received;
152 * Number of bytes of traffic we sent so far.
154 static unsigned long long traffic_sent;
157 * Starting time of transmitting/receiving data.
159 static struct GNUNET_TIME_Absolute start_time;
162 * Handle for current transmission request.
164 static struct GNUNET_TRANSPORT_TransmitHandle *th;
167 * Map storing information about monitored peers
169 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
174 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
176 static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic;
179 * Identity of the peer we transmit to / connect to.
180 * (equivalent to 'cpid' string).
182 static struct GNUNET_PeerIdentity pid;
185 * Task scheduled for cleanup / termination of the process.
187 static GNUNET_SCHEDULER_TaskIdentifier end;
190 * Task for operation timeout
192 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
195 * Selected level of verbosity.
197 static int verbosity;
200 * Resolver process handle.
202 struct GNUNET_OS_Process *resolver;
205 * Number of address resolutions pending
207 static unsigned int address_resolutions;
210 * Address resolutions pending in progress
212 static unsigned int address_resolution_in_progress;
215 * DLL for NAT Test Contexts: head
217 struct TestContext *head;
220 * DLL for NAT Test Contexts: tail
222 struct TestContext *tail;
225 * Context for a plugin test.
232 struct TestContext *prev;
237 struct TestContext *next;
240 * Handle to the active NAT test.
242 struct GNUNET_NAT_Test *tst;
245 * Task identifier for the timeout.
247 GNUNET_SCHEDULER_TaskIdentifier tsk;
250 * Name of plugin under test.
257 unsigned long long bnd_port;
262 unsigned long long adv_port;
268 /* NAT returned success */
269 NAT_TEST_SUCCESS = GNUNET_OK,
271 /* NAT returned failure */
272 NAT_TEST_FAIL = GNUNET_NO,
274 /* NAT returned failure while running test */
275 NAT_TEST_INTERNAL_FAIL = GNUNET_SYSERR,
277 /* We could not start the test */
278 NAT_TEST_FAILED_TO_START = 2,
280 /* We had a timeout while running the test */
281 NAT_TEST_TIMEOUT = 3,
284 static struct ValidationResolutionContext *vc_head;
285 static struct ValidationResolutionContext *vc_tail;
287 struct ValidationResolutionContext
289 struct ValidationResolutionContext *next;
290 struct ValidationResolutionContext *prev;
292 struct GNUNET_PeerIdentity id;
293 struct GNUNET_HELLO_Address *addrcp;
294 struct GNUNET_TIME_Absolute last_validation;
295 struct GNUNET_TIME_Absolute valid_until;
296 struct GNUNET_TIME_Absolute next_validation;
297 enum GNUNET_TRANSPORT_ValidationState state;
299 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
307 enum GNUNET_TRANSPORT_PeerState state;
308 struct GNUNET_TIME_Absolute state_timeout;
309 struct GNUNET_HELLO_Address *address;
314 destroy_it (void *cls,
315 const struct GNUNET_PeerIdentity *key,
318 struct MonitoredPeer *m = value;
320 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (monitored_peers,
322 GNUNET_free_non_null (m->address);
329 * Task run in monitor mode when the user presses CTRL-C to abort.
330 * Stops monitoring activity.
332 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
333 * @param tc scheduler context
336 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
338 struct GNUNET_TIME_Relative duration;
339 struct ValidationResolutionContext *cur;
340 struct ValidationResolutionContext *next;
341 end = GNUNET_SCHEDULER_NO_TASK;
342 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
344 GNUNET_SCHEDULER_cancel (op_timeout);
345 op_timeout = GNUNET_SCHEDULER_NO_TASK;
347 if (NULL != tc_handle)
349 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
354 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
359 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
364 for (cur = next; NULL != cur; cur = next)
368 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
369 GNUNET_CONTAINER_DLL_remove (vc_head, vc_tail, cur);
370 GNUNET_free (cur->transport);
371 GNUNET_HELLO_address_free (cur->addrcp);
377 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
382 GNUNET_TRANSPORT_disconnect (handle);
387 duration = GNUNET_TIME_absolute_get_duration (start_time);
388 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
389 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
391 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
393 if (benchmark_receive)
395 duration = GNUNET_TIME_absolute_get_duration (start_time);
396 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
397 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
399 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
402 if (NULL != monitored_peers)
404 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
405 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
406 monitored_peers = NULL;
410 static struct PeerResolutionContext *rc_head;
411 static struct PeerResolutionContext *rc_tail;
413 struct PeerResolutionContext
415 struct PeerResolutionContext *next;
416 struct PeerResolutionContext *prev;
417 struct GNUNET_PeerIdentity id;
418 struct GNUNET_HELLO_Address *addrcp;
419 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
420 enum GNUNET_TRANSPORT_PeerState state;
421 struct GNUNET_TIME_Absolute state_timeout;
429 operation_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
431 struct PeerResolutionContext *cur;
432 struct PeerResolutionContext *next;
433 op_timeout = GNUNET_SCHEDULER_NO_TASK;
434 if ((try_connect) || (benchmark_send) || (benchmark_receive))
436 FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_i2s_full (&pid));
437 if (GNUNET_SCHEDULER_NO_TASK != end)
438 GNUNET_SCHEDULER_cancel (end);
439 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
443 if (iterate_connections)
446 while (NULL != (cur = next))
449 FPRINTF (stdout, _("Failed to resolve address for peer `%s'\n"),
450 GNUNET_i2s (&cur->addrcp->peer));
452 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
453 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
454 GNUNET_free(cur->transport);
455 GNUNET_free(cur->addrcp);
459 FPRINTF (stdout, "%s", _("Failed to list connections, timeout occured\n") );
460 if (GNUNET_SCHEDULER_NO_TASK != end)
461 GNUNET_SCHEDULER_cancel (end);
462 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
473 * Display the result of the test.
475 * @param tc test context
476 * @param result #GNUNET_YES on success
479 display_test_result (struct TestContext *tc, enum TestResult result)
483 FPRINTF (stderr, _("Configuration for plugin `%s' did not work!\n"),
486 case NAT_TEST_SUCCESS:
487 FPRINTF (stderr, _("Configuration for plugin `%s' did work!\n"),
490 case NAT_TEST_INTERNAL_FAIL:
491 FPRINTF (stderr, _("Internal NAT error while running test for plugin `%s'\n"),
494 case NAT_TEST_FAILED_TO_START:
495 FPRINTF (stderr, _("Failed to start NAT test for plugin `%s'\n"),
498 case NAT_TEST_TIMEOUT:
499 FPRINTF (stderr, _("Timeout while waiting for result of NAT test for plugin `%s'\n"),
506 if (GNUNET_YES != result)
508 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
512 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
514 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
516 GNUNET_SCHEDULER_cancel (tc->tsk);
517 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
521 GNUNET_NAT_test_stop (tc->tst);
525 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
526 GNUNET_free (tc->name);
529 if ((NULL == head) && (NULL != resolver))
531 GNUNET_break(0 == GNUNET_OS_process_kill (resolver, GNUNET_TERM_SIG));
532 GNUNET_OS_process_destroy (resolver);
540 * Function called by NAT on success.
541 * Clean up and update GUI (with success).
543 * @param cls test context
544 * @param success currently always #GNUNET_OK
545 * @param emsg error message, NULL on success
548 result_callback (void *cls, enum GNUNET_NAT_FailureCode result)
550 struct TestContext *tc = cls;
551 display_test_result (tc, result);
555 * Function called if NAT failed to confirm success.
556 * Clean up and update GUI (with failure).
558 * @param cls test context
559 * @param tc scheduler callback
562 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
564 struct TestContext *tstc = cls;
566 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
567 display_test_result (tstc, NAT_TEST_TIMEOUT);
572 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
573 const struct GNUNET_HELLO_Address *address,
575 struct GNUNET_TIME_Absolute last_validation,
576 struct GNUNET_TIME_Absolute valid_until,
577 struct GNUNET_TIME_Absolute next_validation,
578 enum GNUNET_TRANSPORT_ValidationState state);
582 process_validation_string (void *cls, const char *address, int res)
584 struct ValidationResolutionContext *vc = cls;
589 if (address != NULL )
591 if (GNUNET_SYSERR == res)
593 FPRINTF (stderr, "Failed to convert address for peer `%s' plugin `%s' length %lu to string \n",
594 GNUNET_i2s (&vc->id),
595 vc->addrcp->transport_name,
596 vc->addrcp->address_length);
598 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
599 s_valid = GNUNET_strdup("never");
601 s_valid = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
603 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
604 s_last = GNUNET_strdup("never");
606 s_last = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
608 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
609 s_next = GNUNET_strdup("never");
611 s_next = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
614 _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
615 GNUNET_i2s (&vc->id),
616 (GNUNET_OK == res) ? address : "<invalid address>",
617 (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
618 "Valid until : ", s_valid,
619 "Last validation: ",s_last,
620 "Next validation: ", s_next);
621 GNUNET_free (s_valid);
622 GNUNET_free (s_last);
623 GNUNET_free (s_next);
624 vc->printed = GNUNET_YES;
630 GNUNET_assert(address_resolutions > 0);
631 address_resolutions--;
632 if ((GNUNET_SYSERR == res) && (GNUNET_NO == vc->printed))
634 if (numeric == GNUNET_NO)
636 /* Failed to resolve address, try numeric lookup */
637 resolve_validation_address (&vc->id, vc->addrcp, GNUNET_NO,
638 vc->last_validation, vc->valid_until, vc->next_validation,
643 FPRINTF (stdout, _("Peer `%s' %s `%s' \n"),
644 GNUNET_i2s (&vc->id), "<unable to resolve address>",
645 GNUNET_TRANSPORT_vs2s (vc->state));
648 GNUNET_free (vc->transport);
649 GNUNET_free (vc->addrcp);
650 GNUNET_CONTAINER_DLL_remove(vc_head, vc_tail, vc);
652 if ((0 == address_resolutions) && (iterate_validation))
654 if (GNUNET_SCHEDULER_NO_TASK != end)
656 GNUNET_SCHEDULER_cancel (end);
657 end = GNUNET_SCHEDULER_NO_TASK;
659 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
661 GNUNET_SCHEDULER_cancel (op_timeout);
662 op_timeout = GNUNET_SCHEDULER_NO_TASK;
665 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
673 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
674 const struct GNUNET_HELLO_Address *address, int numeric,
675 struct GNUNET_TIME_Absolute last_validation,
676 struct GNUNET_TIME_Absolute valid_until,
677 struct GNUNET_TIME_Absolute next_validation,
678 enum GNUNET_TRANSPORT_ValidationState state)
680 struct ValidationResolutionContext *vc;
682 vc = GNUNET_new (struct ValidationResolutionContext);
683 GNUNET_assert(NULL != vc);
684 GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
685 address_resolutions++;
688 vc->transport = GNUNET_strdup(address->transport_name);
689 vc->addrcp = GNUNET_HELLO_address_copy (address);
690 vc->printed = GNUNET_NO;
692 vc->last_validation = last_validation;
693 vc->valid_until = valid_until;
694 vc->next_validation = next_validation;
696 /* Resolve address to string */
697 vc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
698 RESOLUTION_TIMEOUT, &process_validation_string, vc);
703 process_validation_cb (void *cls,
704 const struct GNUNET_PeerIdentity *peer,
705 const struct GNUNET_HELLO_Address *address,
706 struct GNUNET_TIME_Absolute last_validation,
707 struct GNUNET_TIME_Absolute valid_until,
708 struct GNUNET_TIME_Absolute next_validation,
709 enum GNUNET_TRANSPORT_ValidationState state)
711 if ((NULL == peer) && (NULL == address))
713 if (monitor_validation)
717 _("Monitor disconnected from transport service. Reconnecting.\n"));
723 if (GNUNET_SCHEDULER_NO_TASK != end)
724 GNUNET_SCHEDULER_cancel (end);
725 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
728 if ((NULL == peer) || (NULL == address))
730 /* invalid response */
732 if (GNUNET_SCHEDULER_NO_TASK != end)
733 GNUNET_SCHEDULER_cancel (end);
734 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
737 resolve_validation_address (peer, address,
738 numeric, last_validation,
739 valid_until, next_validation, state);
746 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
747 "Running test for plugin `%s' using bind port %u and advertised port %u \n",
748 head->name, (uint16_t) head->bnd_port, (uint16_t) head->adv_port);
750 head->tst = GNUNET_NAT_test_start (cfg,
751 (0 == strcasecmp (head->name, "udp")) ? GNUNET_NO : GNUNET_YES,
752 (uint16_t) head->bnd_port,
753 (uint16_t) head->adv_port,
754 &result_callback, head);
755 if (NULL == head->tst)
757 display_test_result (head, NAT_TEST_FAILED_TO_START);
760 head->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, head);
765 * Test our plugin's configuration (NAT traversal, etc.).
767 * @param cfg configuration to test
770 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
774 unsigned long long bnd_port;
775 unsigned long long adv_port;
776 struct TestContext *tc;
780 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
783 FPRINTF (stderr, "%s", _
784 ("No transport plugins configured, peer will never communicate\n") );
789 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
791 char section[12 + strlen (tok)];
792 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
794 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
798 _("No port configured for plugin `%s', cannot test it\n"), tok);
801 if (GNUNET_OK != GNUNET_CONFIGURATION_get_value_number (cfg, section,
802 "ADVERTISED_PORT", &adv_port))
805 tc = GNUNET_new (struct TestContext);
806 tc->name = GNUNET_strdup (tok);
807 tc->adv_port = adv_port;
808 tc->bnd_port = bnd_port;
809 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
811 GNUNET_free(plugins);
813 if ((NULL != head) && (NULL == resolver))
815 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
816 resolver = GNUNET_OS_start_process (GNUNET_YES,
817 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
820 "gnunet-service-resolver", NULL );
821 if (NULL == resolver)
823 FPRINTF (stderr, _("Failed to start resolver!\n"));
828 GNUNET_RESOLVER_connect (cfg);
834 * Function called to notify a client about the socket
835 * begin ready to queue more data. @a buf will be
836 * NULL and @a size zero if the socket was closed for
837 * writing in the meantime.
840 * @param size number of bytes available in @a buf
841 * @param buf where the callee should write the message
842 * @return number of bytes written to @a buf
845 transmit_data (void *cls, size_t size, void *buf)
847 struct GNUNET_MessageHeader *m = buf;
849 if ((NULL == buf) || (0 == size))
855 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
856 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
857 m->size = ntohs (size);
858 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
859 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
860 traffic_sent += size;
861 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
863 GNUNET_TIME_UNIT_FOREVER_REL,
864 &transmit_data, NULL );
866 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
873 * Function called to notify transport users that another
874 * peer connected to us.
877 * @param peer the peer that connected
880 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
882 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
887 /* all done, terminate instantly */
888 FPRINTF (stdout, _("Successfully connected to `%s'\n"),
889 GNUNET_i2s_full (peer));
892 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
894 GNUNET_SCHEDULER_cancel (op_timeout);
895 op_timeout = GNUNET_SCHEDULER_NO_TASK;
898 if (GNUNET_SCHEDULER_NO_TASK != end)
899 GNUNET_SCHEDULER_cancel (end);
900 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
905 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
907 GNUNET_SCHEDULER_cancel (op_timeout);
908 op_timeout = GNUNET_SCHEDULER_NO_TASK;
912 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
913 GNUNET_i2s (&pid), BLOCKSIZE);
914 start_time = GNUNET_TIME_absolute_get ();
916 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
918 GNUNET_TIME_UNIT_FOREVER_REL,
928 * Function called to notify transport users that another
929 * peer disconnected from us.
932 * @param peer the peer that disconnected
935 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
937 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
942 /* all done, terminate instantly */
943 FPRINTF (stdout, _("Successfully disconnected from `%s'\n"),
944 GNUNET_i2s_full (peer));
947 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
949 GNUNET_SCHEDULER_cancel (op_timeout);
950 op_timeout = GNUNET_SCHEDULER_NO_TASK;
953 if (GNUNET_SCHEDULER_NO_TASK != end)
954 GNUNET_SCHEDULER_cancel (end);
955 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
961 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
966 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
968 if (GNUNET_SCHEDULER_NO_TASK != end)
969 GNUNET_SCHEDULER_cancel (end);
975 * Function called to notify transport users that another
976 * peer connected to us.
979 * @param peer the peer that connected
982 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
984 monitor_connect_counter++;
985 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
986 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
988 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str,
989 _("Connected to"), GNUNET_i2s (peer), monitor_connect_counter);
994 * Function called to notify transport users that another
995 * peer disconnected from us.
998 * @param peer the peer that disconnected
1001 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
1003 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
1004 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
1006 GNUNET_assert(monitor_connect_counter > 0);
1007 monitor_connect_counter--;
1009 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str,
1010 _("Disconnected from"), GNUNET_i2s (peer), monitor_connect_counter);
1015 * Function called by the transport for each received message.
1017 * @param cls closure
1018 * @param peer (claimed) identity of the other peer
1019 * @param message the message
1022 notify_receive (void *cls,
1023 const struct GNUNET_PeerIdentity *peer,
1024 const struct GNUNET_MessageHeader *message)
1026 if (benchmark_receive)
1028 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
1031 FPRINTF (stdout, _("Received %u bytes from %s\n"),
1032 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
1034 if (traffic_received == 0)
1035 start_time = GNUNET_TIME_absolute_get ();
1036 traffic_received += ntohs (message->size);
1043 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1044 const struct GNUNET_HELLO_Address *address,
1046 enum GNUNET_TRANSPORT_PeerState state,
1047 struct GNUNET_TIME_Absolute state_timeout);
1051 print_info (const struct GNUNET_PeerIdentity *id,
1052 const char *transport,
1054 enum GNUNET_TRANSPORT_PeerState state,
1055 struct GNUNET_TIME_Absolute state_timeout)
1058 if ( ((GNUNET_YES == iterate_connections) && (GNUNET_YES == iterate_all)) ||
1059 (GNUNET_YES == monitor_connections) )
1061 FPRINTF (stdout, _("Peer `%s': %s %s in state `%s' until %s\n"),
1063 (NULL == transport) ? "<none>" : transport,
1064 (NULL == transport) ? "<none>" : addr,
1065 GNUNET_TRANSPORT_ps2s (state),
1066 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
1068 else if ( (GNUNET_YES == iterate_connections) &&
1069 (GNUNET_TRANSPORT_is_connected(state)) )
1071 /* Only connected peers, skip state */
1072 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (id), transport, addr);
1078 process_peer_string (void *cls, const char *address, int res)
1080 struct PeerResolutionContext *rc = cls;
1082 if (GNUNET_SYSERR == res)
1084 FPRINTF (stderr, "Failed to convert address for peer `%s' plugin `%s' length %lu to string \n",
1085 GNUNET_i2s (&rc->id),
1086 rc->addrcp->transport_name,
1087 rc->addrcp->address_length);
1088 print_info (&rc->id, rc->transport, NULL, rc->state, rc->state_timeout);
1089 rc->printed = GNUNET_YES;
1093 if ((GNUNET_OK == res) && (address != NULL))
1095 print_info (&rc->id, rc->transport, address, rc->state, rc->state_timeout);
1096 rc->printed = GNUNET_YES;
1097 return; /* Wait for done call */
1100 if (NULL == address)
1103 GNUNET_assert(address_resolutions > 0);
1104 address_resolutions--;
1105 if (GNUNET_NO == rc->printed)
1107 if (numeric == GNUNET_NO)
1109 /* Failed to resolve address, try numeric lookup */
1110 resolve_peer_address (&rc->id, rc->addrcp, GNUNET_YES,
1111 rc->state, rc->state_timeout);
1115 print_info (&rc->id, rc->transport, NULL,
1116 rc->state, rc->state_timeout);
1119 GNUNET_free (rc->transport);
1120 GNUNET_free (rc->addrcp);
1121 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, rc);
1123 if ((0 == address_resolutions) && (iterate_connections))
1125 if (GNUNET_SCHEDULER_NO_TASK != end)
1127 GNUNET_SCHEDULER_cancel (end);
1128 end = GNUNET_SCHEDULER_NO_TASK;
1130 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1132 GNUNET_SCHEDULER_cancel (op_timeout);
1133 op_timeout = GNUNET_SCHEDULER_NO_TASK;
1136 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1143 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
1144 const struct GNUNET_HELLO_Address *address,
1146 enum GNUNET_TRANSPORT_PeerState state,
1147 struct GNUNET_TIME_Absolute state_timeout)
1149 struct PeerResolutionContext *rc;
1151 rc = GNUNET_new (struct PeerResolutionContext);
1152 GNUNET_assert(NULL != rc);
1153 GNUNET_CONTAINER_DLL_insert(rc_head, rc_tail, rc);
1154 address_resolutions++;
1157 rc->transport = GNUNET_strdup(address->transport_name);
1158 rc->addrcp = GNUNET_HELLO_address_copy (address);
1159 rc->printed = GNUNET_NO;
1161 rc->state_timeout = state_timeout;
1162 /* Resolve address to string */
1163 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
1164 RESOLUTION_TIMEOUT, &process_peer_string, rc);
1169 * Function called with information about a peers during a one shot iteration
1171 * @param cls closure
1172 * @param peer identity of the peer, NULL for final callback when operation done
1173 * @param address binary address used to communicate with this peer,
1174 * NULL on disconnect or when done
1175 * @param state current state this peer is in
1176 * @param state_timeout time out for the current state
1179 process_peer_iteration_cb (void *cls,
1180 const struct GNUNET_PeerIdentity *peer,
1181 const struct GNUNET_HELLO_Address *address,
1182 enum GNUNET_TRANSPORT_PeerState state,
1183 struct GNUNET_TIME_Absolute state_timeout)
1188 address_resolution_in_progress = GNUNET_NO;
1190 if (GNUNET_SCHEDULER_NO_TASK != end)
1191 GNUNET_SCHEDULER_cancel (end);
1192 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1196 if ( (GNUNET_NO == iterate_all) &&
1197 (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)) )
1198 return; /* Display only connected peers */
1200 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1201 GNUNET_SCHEDULER_cancel (op_timeout);
1202 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1205 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG,
1206 "Received address for peer `%s': %s\n",
1207 GNUNET_i2s (peer), address->transport_name);
1209 if (NULL != address)
1210 resolve_peer_address (peer, address, numeric, state, state_timeout);
1212 print_info (peer, NULL, NULL, state, state_timeout);
1217 * Function called with information about a peers
1219 * @param cls closure
1220 * @param peer identity of the peer, NULL for final callback when operation done
1221 * @param address binary address used to communicate with this peer,
1222 * NULL on disconnect or when done
1223 * @param state current state this peer is in
1224 * @param state_timeout time out for the current state
1228 process_peer_monitoring_cb (void *cls,
1229 const struct GNUNET_PeerIdentity *peer,
1230 const struct GNUNET_HELLO_Address *address,
1231 enum GNUNET_TRANSPORT_PeerState state,
1232 struct GNUNET_TIME_Absolute state_timeout)
1234 struct MonitoredPeer *m;
1240 _("Monitor disconnected from transport service. Reconnecting.\n"));
1244 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1245 GNUNET_SCHEDULER_cancel (op_timeout);
1246 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1249 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
1251 m = GNUNET_new (struct MonitoredPeer);
1252 GNUNET_CONTAINER_multipeermap_put (monitored_peers, peer,
1253 m, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1257 if ( (m->state == state) &&
1258 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1259 ((NULL == address) && (NULL == m->address)) )
1261 return; /* No real change */
1263 if ( (m->state == state) && ((NULL != address) && (NULL != m->address)) &&
1264 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1265 return; /* No real change */
1268 if (NULL != m->address)
1270 GNUNET_free (m->address);
1273 if (NULL != address)
1274 m->address = GNUNET_HELLO_address_copy (address);
1276 m->state_timeout = state_timeout;
1278 if (NULL != address)
1279 resolve_peer_address (peer, m->address, numeric, m->state, m->state_timeout);
1281 print_info (peer, NULL, NULL, m->state, m->state_timeout);
1285 try_connect_cb (void *cls, const int result)
1287 static int retries = 0;
1288 if (GNUNET_OK == result)
1295 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1299 FPRINTF (stderr, "%s",
1300 _("Failed to send connect request to transport service\n") );
1301 if (GNUNET_SCHEDULER_NO_TASK != end)
1302 GNUNET_SCHEDULER_cancel (end);
1304 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1310 try_disconnect_cb (void *cls, const int result)
1312 static int retries = 0;
1313 if (GNUNET_OK == result)
1320 tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1324 FPRINTF (stderr, "%s",
1325 _("Failed to send connect request to transport service\n") );
1326 if (GNUNET_SCHEDULER_NO_TASK != end)
1327 GNUNET_SCHEDULER_cancel (end);
1329 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1335 * Function called with the result of the check if the 'transport'
1336 * service is running.
1338 * @param cls closure with our configuration
1339 * @param result #GNUNET_YES if transport is running
1342 testservice_task (void *cls, int result)
1347 if (GNUNET_YES != result)
1349 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1355 != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
1358 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
1362 counter = benchmark_send + benchmark_receive + iterate_connections
1363 + monitor_connections + monitor_connects + try_connect + try_disconnect +
1364 + iterate_validation + monitor_validation;
1369 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
1370 "connect", "benchmark send", "benchmark receive", "information",
1371 "monitor", "events");
1377 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
1378 "connect", "benchmark send", "benchmark receive", "information",
1379 "monitor", "events");
1383 if (try_connect) /* -C: Connect to peer */
1387 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1392 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1393 ¬ify_connect, ¬ify_disconnect);
1396 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1400 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1402 if (NULL == tc_handle)
1404 FPRINTF (stderr, "%s",
1405 _("Failed to send request to transport service\n") );
1409 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1413 else if (try_disconnect) /* -D: Disconnect from peer */
1417 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1422 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1423 ¬ify_connect, ¬ify_disconnect);
1426 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1430 tc_handle = GNUNET_TRANSPORT_try_disconnect (handle, &pid, try_disconnect_cb,
1432 if (NULL == tc_handle)
1434 FPRINTF (stderr, "%s",
1435 _("Failed to send request to transport service\n") );
1439 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1443 else if (benchmark_send) /* -s: Benchmark sending */
1447 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1452 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1453 ¬ify_connect, ¬ify_disconnect);
1456 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1460 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1462 if (NULL == tc_handle)
1464 FPRINTF (stderr, "%s",
1465 _("Failed to send request to transport service\n") );
1469 start_time = GNUNET_TIME_absolute_get ();
1470 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1473 else if (benchmark_receive) /* -b: Benchmark receiving */
1475 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive, NULL,
1479 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1484 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n") );
1485 start_time = GNUNET_TIME_absolute_get ();
1488 else if (iterate_connections) /* -i: List information about peers once */
1490 address_resolution_in_progress = GNUNET_YES;
1491 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1492 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1493 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1496 else if (monitor_connections) /* -m: List information about peers continuously */
1498 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1499 address_resolution_in_progress = GNUNET_YES;
1500 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1501 GNUNET_NO, TIMEOUT, &process_peer_monitoring_cb, (void *) cfg);
1503 else if (iterate_validation) /* -d: Print information about validations */
1505 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1506 (NULL == cpid) ? NULL : &pid,
1507 GNUNET_YES, TIMEOUT, &process_validation_cb, (void *) cfg);
1509 else if (monitor_validation) /* -f: Print information about validations continuously */
1511 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1512 (NULL == cpid) ? NULL : &pid,
1513 GNUNET_NO, TIMEOUT, &process_validation_cb, (void *) cfg);
1515 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1517 monitor_connect_counter = 0;
1518 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1519 &monitor_notify_connect,
1520 &monitor_notify_disconnect);
1523 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1535 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1536 &shutdown_task, NULL );
1541 * Main function that will be run by the scheduler.
1543 * @param cls closure
1544 * @param args remaining command-line arguments
1545 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1546 * @param mycfg configuration
1549 run (void *cls, char * const *args, const char *cfgfile,
1550 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1552 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1553 if (test_configuration)
1555 do_test_configuration (cfg);
1558 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1559 &testservice_task, (void *) cfg);
1563 main (int argc, char * const *argv)
1566 static const struct GNUNET_GETOPT_CommandLineOption options[] =
1569 gettext_noop ("print information for all peers (instead of only connected peers )"),
1570 0, &GNUNET_GETOPT_set_one, &iterate_all },
1571 { 'b', "benchmark", NULL,
1572 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1573 0, &GNUNET_GETOPT_set_one, &benchmark_receive },
1575 NULL, gettext_noop ("connect to a peer"), 0,
1576 &GNUNET_GETOPT_set_one, &try_connect },
1577 { 'D', "disconnect",
1578 NULL, gettext_noop ("disconnect to a peer"), 0,
1579 &GNUNET_GETOPT_set_one, &try_disconnect },
1580 { 'd', "validation", NULL,
1581 gettext_noop ("print information for all pending validations "),
1582 0, &GNUNET_GETOPT_set_one, &iterate_validation },
1583 { 'f', "monitorvalidation", NULL,
1584 gettext_noop ("print information for all pending validations continously"),
1585 0, &GNUNET_GETOPT_set_one, &monitor_validation },
1586 { 'i', "information", NULL,
1587 gettext_noop ("provide information about all current connections (once)"),
1588 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1589 { 'm', "monitor", NULL,
1590 gettext_noop ("provide information about all current connections (continuously)"),
1591 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1592 { 'e', "events", NULL,
1593 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1594 0, &GNUNET_GETOPT_set_one, &monitor_connects }, { 'n', "numeric",
1595 NULL, gettext_noop ("do not resolve hostnames"), 0,
1596 &GNUNET_GETOPT_set_one, &numeric }, { 'p', "peer", "PEER",
1597 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1598 &cpid }, { 's', "send", NULL, gettext_noop
1599 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1600 &GNUNET_GETOPT_set_one, &benchmark_send },
1601 { 't', "test", NULL,
1602 gettext_noop ("test transport configuration (involves external server)"),
1603 0, &GNUNET_GETOPT_set_one, &test_configuration },
1604 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1605 GNUNET_GETOPT_OPTION_END };
1607 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1610 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport", gettext_noop
1611 ("Direct access to transport service."), options, &run, NULL );
1612 GNUNET_free((void * ) argv);
1613 if (GNUNET_OK == res)
1618 /* end of gnunet-transport.c */