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, 5)
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;
132 * Global return value (0 success).
137 * Current number of connections in monitor mode
139 static int monitor_connect_counter;
142 * Number of bytes of traffic we received so far.
144 static unsigned long long traffic_received;
147 * Number of bytes of traffic we sent so far.
149 static unsigned long long traffic_sent;
152 * Starting time of transmitting/receiving data.
154 static struct GNUNET_TIME_Absolute start_time;
157 * Handle for current transmission request.
159 static struct GNUNET_TRANSPORT_TransmitHandle *th;
162 * Map storing information about monitored peers
164 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
169 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
171 static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic;
174 * Identity of the peer we transmit to / connect to.
175 * (equivalent to 'cpid' string).
177 static struct GNUNET_PeerIdentity pid;
180 * Task scheduled for cleanup / termination of the process.
182 static GNUNET_SCHEDULER_TaskIdentifier end;
185 * Task for operation timeout
187 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
190 * Selected level of verbosity.
192 static int verbosity;
195 * Resolver process handle.
197 struct GNUNET_OS_Process *resolver;
200 * Number of address resolutions pending
202 static unsigned int address_resolutions;
205 * Address resolutions pending in progress
207 static unsigned int address_resolution_in_progress;
210 * DLL for NAT Test Contexts: head
212 struct TestContext *head;
215 * DLL for NAT Test Contexts: tail
217 struct TestContext *tail;
220 * Context for a plugin test.
227 struct TestContext *prev;
232 struct TestContext *next;
235 * Handle to the active NAT test.
237 struct GNUNET_NAT_Test *tst;
240 * Task identifier for the timeout.
242 GNUNET_SCHEDULER_TaskIdentifier tsk;
245 * Name of plugin under test.
252 unsigned long long bnd_port;
257 unsigned long long adv_port;
263 enum GNUNET_TRANSPORT_PeerState state;
264 struct GNUNET_TIME_Absolute state_timeout;
265 struct GNUNET_HELLO_Address *address;
269 int destroy_it (void *cls,
270 const struct GNUNET_PeerIdentity *key,
273 struct MonitoredPeer *m = value;
274 GNUNET_CONTAINER_multipeermap_remove (monitored_peers, key, value);
275 GNUNET_free_non_null (m->address);
281 * Task run in monitor mode when the user presses CTRL-C to abort.
282 * Stops monitoring activity.
284 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
285 * @param tc scheduler context
288 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
290 struct GNUNET_TIME_Relative duration;
291 end = GNUNET_SCHEDULER_NO_TASK;
292 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
294 GNUNET_SCHEDULER_cancel (op_timeout);
295 op_timeout = GNUNET_SCHEDULER_NO_TASK;
297 if (NULL != tc_handle)
299 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
304 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
309 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
314 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
319 GNUNET_TRANSPORT_disconnect (handle);
324 duration = GNUNET_TIME_absolute_get_duration (start_time);
325 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
326 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
328 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
330 if (benchmark_receive)
332 duration = GNUNET_TIME_absolute_get_duration (start_time);
333 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
334 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
336 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
339 if (NULL != monitored_peers)
341 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
342 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
343 monitored_peers = NULL;
347 static struct PeerResolutionContext *rc_head;
348 static struct PeerResolutionContext *rc_tail;
350 struct PeerResolutionContext
352 struct PeerResolutionContext *next;
353 struct PeerResolutionContext *prev;
354 struct GNUNET_PeerIdentity id;
355 struct GNUNET_HELLO_Address *addrcp;
356 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
357 enum GNUNET_TRANSPORT_PeerState state;
358 struct GNUNET_TIME_Absolute state_timeout;
363 static struct ValidationResolutionContext *vc_head;
364 static struct ValidationResolutionContext *vc_tail;
366 struct ValidationResolutionContext
368 struct ValidationResolutionContext *next;
369 struct ValidationResolutionContext *prev;
371 struct GNUNET_PeerIdentity id;
372 struct GNUNET_HELLO_Address *addrcp;
373 struct GNUNET_TIME_Absolute last_validation;
374 struct GNUNET_TIME_Absolute valid_until;
375 struct GNUNET_TIME_Absolute next_validation;
376 enum GNUNET_TRANSPORT_ValidationState state;
378 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
385 operation_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
387 struct PeerResolutionContext *cur;
388 struct PeerResolutionContext *next;
389 op_timeout = GNUNET_SCHEDULER_NO_TASK;
390 if ((try_connect) || (benchmark_send) || (benchmark_receive))
392 FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_i2s_full (&pid));
393 if (GNUNET_SCHEDULER_NO_TASK != end)
394 GNUNET_SCHEDULER_cancel (end);
395 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
399 if (iterate_connections)
402 while (NULL != (cur = next))
405 FPRINTF (stdout, _("Failed to resolve address for peer `%s'\n"),
406 GNUNET_i2s (&cur->addrcp->peer));
408 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
409 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
410 GNUNET_free(cur->transport);
411 GNUNET_free(cur->addrcp);
415 FPRINTF (stdout, "%s", _("Failed to list connections, timeout occured\n") );
416 if (GNUNET_SCHEDULER_NO_TASK != end)
417 GNUNET_SCHEDULER_cancel (end);
418 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
429 * Display the result of the test.
431 * @param tc test context
432 * @param result #GNUNET_YES on success
435 display_test_result (struct TestContext *tc, int result)
437 if (GNUNET_YES != result)
439 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
443 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
445 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
447 GNUNET_SCHEDULER_cancel (tc->tsk);
448 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
452 GNUNET_NAT_test_stop (tc->tst);
456 GNUNET_CONTAINER_DLL_remove (head, tail, tc);
457 GNUNET_free (tc->name);
460 if ((NULL == head) && (NULL != resolver))
462 GNUNET_break(0 == GNUNET_OS_process_kill (resolver, GNUNET_TERM_SIG));
463 GNUNET_OS_process_destroy (resolver);
471 * Function called by NAT on success.
472 * Clean up and update GUI (with success).
474 * @param cls test context
475 * @param success currently always #GNUNET_OK
476 * @param emsg error message, NULL on success
479 result_callback (void *cls, int success, const char *emsg)
481 struct TestContext *tc = cls;
482 display_test_result (tc, success);
486 * Function called if NAT failed to confirm success.
487 * Clean up and update GUI (with failure).
489 * @param cls test context
490 * @param tc scheduler callback
493 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
495 struct TestContext *tstc = cls;
497 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
498 display_test_result (tstc, GNUNET_NO);
503 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
504 const struct GNUNET_HELLO_Address *address, int numeric,
505 struct GNUNET_TIME_Absolute last_validation,
506 struct GNUNET_TIME_Absolute valid_until,
507 struct GNUNET_TIME_Absolute next_validation,
508 enum GNUNET_TRANSPORT_ValidationState state);
511 process_validation_string (void *cls, const char *address)
513 struct ValidationResolutionContext *vc = cls;
518 if (address != NULL )
520 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
521 s_valid = GNUNET_strdup("never");
523 s_valid = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
525 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
526 s_last = GNUNET_strdup("never");
528 s_last = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
530 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
531 s_next = GNUNET_strdup("never");
533 s_next = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
536 _("Peer `%s' %s %s\n\t%s%s\n\t%s%s\n\t%s%s\n"),
537 GNUNET_i2s (&vc->id), address,
538 (monitor_validation) ? GNUNET_TRANSPORT_vs2s (vc->state) : "",
539 "Valid until : ", s_valid,
540 "Last validation: ",s_last,
541 "Next validation: ", s_next);
542 GNUNET_free (s_valid);
543 GNUNET_free (s_last);
544 GNUNET_free (s_next);
545 vc->printed = GNUNET_YES;
550 GNUNET_assert(address_resolutions > 0);
551 address_resolutions--;
552 if (GNUNET_NO == vc->printed)
554 if (numeric == GNUNET_NO)
556 /* Failed to resolve address, try numeric lookup */
557 resolve_validation_address (&vc->id, vc->addrcp, GNUNET_NO,
558 vc->last_validation, vc->valid_until, vc->next_validation,
563 FPRINTF (stdout, _("Peer `%s' %s `%s' \n"),
564 GNUNET_i2s (&vc->id), "<unable to resolve address>",
565 GNUNET_TRANSPORT_vs2s (vc->state));
568 GNUNET_free (vc->transport);
569 GNUNET_free (vc->addrcp);
570 GNUNET_CONTAINER_DLL_remove(vc_head, vc_tail, vc);
572 if ((0 == address_resolutions) && (iterate_validation))
574 if (GNUNET_SCHEDULER_NO_TASK != end)
576 GNUNET_SCHEDULER_cancel (end);
577 end = GNUNET_SCHEDULER_NO_TASK;
579 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
581 GNUNET_SCHEDULER_cancel (op_timeout);
582 op_timeout = GNUNET_SCHEDULER_NO_TASK;
585 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
593 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
594 const struct GNUNET_HELLO_Address *address, int numeric,
595 struct GNUNET_TIME_Absolute last_validation,
596 struct GNUNET_TIME_Absolute valid_until,
597 struct GNUNET_TIME_Absolute next_validation,
598 enum GNUNET_TRANSPORT_ValidationState state)
600 struct ValidationResolutionContext *vc;
602 vc = GNUNET_new (struct ValidationResolutionContext);
603 GNUNET_assert(NULL != vc);
604 GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
605 address_resolutions++;
608 vc->transport = GNUNET_strdup(address->transport_name);
609 vc->addrcp = GNUNET_HELLO_address_copy (address);
610 vc->printed = GNUNET_NO;
612 vc->last_validation = last_validation;
613 vc->valid_until = valid_until;
614 vc->next_validation = next_validation;
616 /* Resolve address to string */
617 vc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
618 RESOLUTION_TIMEOUT, &process_validation_string, vc);
622 void process_validation_cb (void *cls,
623 const struct GNUNET_PeerIdentity *peer,
624 const struct GNUNET_HELLO_Address *address,
625 struct GNUNET_TIME_Absolute last_validation,
626 struct GNUNET_TIME_Absolute valid_until,
627 struct GNUNET_TIME_Absolute next_validation,
628 enum GNUNET_TRANSPORT_ValidationState state)
630 if ((NULL == peer) && (NULL == address))
634 if (GNUNET_SCHEDULER_NO_TASK != end)
635 GNUNET_SCHEDULER_cancel (end);
636 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
639 if ((NULL == peer) || (NULL == address))
641 /* invalid response */
643 if (GNUNET_SCHEDULER_NO_TASK != end)
644 GNUNET_SCHEDULER_cancel (end);
645 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
648 resolve_validation_address (peer, address,
649 numeric, last_validation,
650 valid_until, next_validation, state);
656 head->tst = GNUNET_NAT_test_start (cfg,
657 (0 == strcasecmp (head->name, "udp")) ? GNUNET_NO : GNUNET_YES,
658 (uint16_t) head->bnd_port,
659 (uint16_t) head->adv_port,
660 &result_callback, head);
661 if (NULL == head->tst)
663 display_test_result (head, GNUNET_SYSERR);
666 head->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, head);
670 * Test our plugin's configuration (NAT traversal, etc.).
672 * @param cfg configuration to test
675 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
679 unsigned long long bnd_port;
680 unsigned long long adv_port;
681 struct TestContext *tc;
685 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
688 FPRINTF (stderr, "%s", _
689 ("No transport plugins configured, peer will never communicate\n") );
694 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
696 char section[12 + strlen (tok)];
697 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
699 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
703 _("No port configured for plugin `%s', cannot test it\n"), tok);
707 != GNUNET_CONFIGURATION_get_value_number (cfg, section,
708 "ADVERTISED_PORT", &adv_port))
711 tc = GNUNET_new (struct TestContext);
712 tc->name = GNUNET_strdup (tok);
713 tc->adv_port = adv_port;
714 tc->bnd_port = bnd_port;
715 GNUNET_CONTAINER_DLL_insert_tail (head, tail, tc);
717 GNUNET_free(plugins);
719 if ((NULL != head) && (NULL == resolver))
721 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
722 resolver = GNUNET_OS_start_process (GNUNET_YES,
723 GNUNET_OS_INHERIT_STD_OUT_AND_ERR,
726 "gnunet-service-resolver", NULL );
728 GNUNET_RESOLVER_connect (cfg);
734 * Function called to notify a client about the socket
735 * begin ready to queue more data. @a buf will be
736 * NULL and @a size zero if the socket was closed for
737 * writing in the meantime.
740 * @param size number of bytes available in @a buf
741 * @param buf where the callee should write the message
742 * @return number of bytes written to @a buf
745 transmit_data (void *cls, size_t size, void *buf)
747 struct GNUNET_MessageHeader *m = buf;
749 if ((NULL == buf) || (0 == size))
755 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
756 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
757 m->size = ntohs (size);
758 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
759 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
760 traffic_sent += size;
761 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid,
763 GNUNET_TIME_UNIT_FOREVER_REL,
764 &transmit_data, NULL );
766 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
772 * Function called to notify transport users that another
773 * peer connected to us.
776 * @param peer the peer that connected
779 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
781 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
786 /* all done, terminate instantly */
787 FPRINTF (stdout, _("Successfully connected to `%s'\n"),
788 GNUNET_i2s_full (peer));
791 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
793 GNUNET_SCHEDULER_cancel (op_timeout);
794 op_timeout = GNUNET_SCHEDULER_NO_TASK;
797 if (GNUNET_SCHEDULER_NO_TASK != end)
798 GNUNET_SCHEDULER_cancel (end);
799 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
804 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
806 GNUNET_SCHEDULER_cancel (op_timeout);
807 op_timeout = GNUNET_SCHEDULER_NO_TASK;
811 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
812 GNUNET_i2s (&pid), BLOCKSIZE);
813 start_time = GNUNET_TIME_absolute_get ();
815 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
817 GNUNET_TIME_UNIT_FOREVER_REL,
827 * Function called to notify transport users that another
828 * peer disconnected from us.
831 * @param peer the peer that disconnected
834 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
836 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
841 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
846 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
848 if (GNUNET_SCHEDULER_NO_TASK != end)
849 GNUNET_SCHEDULER_cancel (end);
855 * Function called to notify transport users that another
856 * peer connected to us.
859 * @param peer the peer that connected
862 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
864 monitor_connect_counter++;
865 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
866 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
868 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str,
869 _("Connected to"), GNUNET_i2s (peer), monitor_connect_counter);
873 * Function called to notify transport users that another
874 * peer disconnected from us.
877 * @param peer the peer that disconnected
880 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
882 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
883 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
885 GNUNET_assert(monitor_connect_counter > 0);
886 monitor_connect_counter--;
888 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str,
889 _("Disconnected from"), GNUNET_i2s (peer), monitor_connect_counter);
893 * Function called by the transport for each received message.
896 * @param peer (claimed) identity of the other peer
897 * @param message the message
900 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
901 const struct GNUNET_MessageHeader *message)
903 if (benchmark_receive)
905 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
908 FPRINTF (stdout, _("Received %u bytes from %s\n"),
909 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
911 if (traffic_received == 0)
912 start_time = GNUNET_TIME_absolute_get ();
913 traffic_received += ntohs (message->size);
919 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
920 const struct GNUNET_HELLO_Address *address, int numeric,
921 enum GNUNET_TRANSPORT_PeerState state,
922 struct GNUNET_TIME_Absolute state_timeout);
925 print_info (const struct GNUNET_PeerIdentity *id, const char *transport,
926 const char *addr, enum GNUNET_TRANSPORT_PeerState state,
927 struct GNUNET_TIME_Absolute state_timeout)
929 if ((GNUNET_YES == iterate_all) || (GNUNET_YES == monitor_connections) )
931 FPRINTF (stdout, _("Peer `%s': %s %s in state `%s' until %s\n"),
933 (NULL == transport) ? "<none>" : transport,
934 (NULL == transport) ? "<none>" : addr,
935 GNUNET_TRANSPORT_ps2s (state),
936 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
940 /* Only connected peers, skip state */
941 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (id), transport, addr);
947 process_peer_string (void *cls, const char *address)
949 struct PeerResolutionContext *rc = cls;
951 if (address != NULL )
953 print_info (&rc->id, rc->transport, address, rc->state, rc->state_timeout);
954 rc->printed = GNUNET_YES;
959 GNUNET_assert(address_resolutions > 0);
960 address_resolutions--;
961 if (GNUNET_NO == rc->printed)
963 if (numeric == GNUNET_NO)
965 /* Failed to resolve address, try numeric lookup */
966 resolve_peer_address (&rc->id, rc->addrcp, GNUNET_YES,
967 rc->state, rc->state_timeout);
971 print_info (&rc->id, rc->transport, "<unable to resolve address>",
972 rc->state, rc->state_timeout);
975 GNUNET_free (rc->transport);
976 GNUNET_free (rc->addrcp);
977 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, rc);
979 if ((0 == address_resolutions) && (iterate_connections))
981 if (GNUNET_SCHEDULER_NO_TASK != end)
983 GNUNET_SCHEDULER_cancel (end);
984 end = GNUNET_SCHEDULER_NO_TASK;
986 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
988 GNUNET_SCHEDULER_cancel (op_timeout);
989 op_timeout = GNUNET_SCHEDULER_NO_TASK;
992 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
998 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
999 const struct GNUNET_HELLO_Address *address, int numeric,
1000 enum GNUNET_TRANSPORT_PeerState state,
1001 struct GNUNET_TIME_Absolute state_timeout)
1003 struct PeerResolutionContext *rc;
1005 rc = GNUNET_new (struct PeerResolutionContext);
1006 GNUNET_assert(NULL != rc);
1007 GNUNET_CONTAINER_DLL_insert(rc_head, rc_tail, rc);
1008 address_resolutions++;
1011 rc->transport = GNUNET_strdup(address->transport_name);
1012 rc->addrcp = GNUNET_HELLO_address_copy (address);
1013 rc->printed = GNUNET_NO;
1015 rc->state_timeout = state_timeout;
1016 /* Resolve address to string */
1017 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
1018 RESOLUTION_TIMEOUT, &process_peer_string, rc);
1022 * Function called with information about a peers during a one shot iteration
1024 * @param cls closure
1025 * @param peer identity of the peer, NULL for final callback when operation done
1026 * @param address binary address used to communicate with this peer,
1027 * NULL on disconnect or when done
1028 * @param state current state this peer is in
1029 * @param state_timeout time out for the current state
1033 process_peer_iteration_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
1034 const struct GNUNET_HELLO_Address *address,
1035 enum GNUNET_TRANSPORT_PeerState state,
1036 struct GNUNET_TIME_Absolute state_timeout)
1041 address_resolution_in_progress = GNUNET_NO;
1043 if (GNUNET_SCHEDULER_NO_TASK != end)
1044 GNUNET_SCHEDULER_cancel (end);
1045 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1049 if ((GNUNET_NO == iterate_all) && (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)) )
1050 return; /* Display only connected peers */
1052 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1053 GNUNET_SCHEDULER_cancel (op_timeout);
1054 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1057 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received address for peer `%s': %s\n",
1058 GNUNET_i2s (peer), address->transport_name);
1060 if (NULL != address)
1061 resolve_peer_address (peer, address, numeric, state, state_timeout);
1063 print_info (peer, NULL, NULL, state, state_timeout);
1068 * Function called with information about a peers
1070 * @param cls closure
1071 * @param peer identity of the peer, NULL for final callback when operation done
1072 * @param address binary address used to communicate with this peer,
1073 * NULL on disconnect or when done
1074 * @param state current state this peer is in
1075 * @param state_timeout time out for the current state
1079 process_peer_monitoring_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
1080 const struct GNUNET_HELLO_Address *address,
1081 enum GNUNET_TRANSPORT_PeerState state,
1082 struct GNUNET_TIME_Absolute state_timeout)
1084 struct MonitoredPeer *m;
1089 address_resolution_in_progress = GNUNET_NO;
1091 if (GNUNET_SCHEDULER_NO_TASK != end)
1092 GNUNET_SCHEDULER_cancel (end);
1093 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1097 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1098 GNUNET_SCHEDULER_cancel (op_timeout);
1099 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1102 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
1104 m = GNUNET_new (struct MonitoredPeer);
1105 GNUNET_CONTAINER_multipeermap_put (monitored_peers, peer,
1106 m, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1110 if ( (m->state == state) &&
1111 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1112 ((NULL == address) && (NULL == m->address)) )
1114 return; /* No real change */
1116 if ( (m->state == state) && ((NULL != address) && (NULL != m->address)) &&
1117 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1118 return; /* No real change */
1121 if (NULL != m->address)
1123 GNUNET_free (m->address);
1126 if (NULL != address)
1127 m->address = GNUNET_HELLO_address_copy (address);
1129 m->state_timeout = state_timeout;
1131 if (NULL != address)
1132 resolve_peer_address (peer, m->address, numeric, m->state, m->state_timeout);
1134 print_info (peer, NULL, NULL, m->state, m->state_timeout);
1138 try_connect_cb (void *cls, const int result)
1140 static int retries = 0;
1141 if (GNUNET_OK == result)
1148 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1152 FPRINTF (stderr, "%s",
1153 _("Failed to send connect request to transport service\n") );
1154 if (GNUNET_SCHEDULER_NO_TASK != end)
1155 GNUNET_SCHEDULER_cancel (end);
1157 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1163 * Function called with the result of the check if the 'transport'
1164 * service is running.
1166 * @param cls closure with our configuration
1167 * @param result #GNUNET_YES if transport is running
1170 testservice_task (void *cls, int result)
1175 if (GNUNET_YES != result)
1177 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1183 != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
1186 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
1190 counter = benchmark_send + benchmark_receive + iterate_connections
1191 + monitor_connections + monitor_connects + try_connect
1192 + iterate_validation + monitor_validation;
1197 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
1198 "connect", "benchmark send", "benchmark receive", "information",
1199 "monitor", "events");
1205 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
1206 "connect", "benchmark send", "benchmark receive", "information",
1207 "monitor", "events");
1211 if (try_connect) /* -C: Connect to peer */
1215 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1220 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1221 ¬ify_connect, ¬ify_disconnect);
1224 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1228 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1230 if (NULL == tc_handle)
1232 FPRINTF (stderr, "%s",
1233 _("Failed to send request to transport service\n") );
1237 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1241 else if (benchmark_send) /* -s: Benchmark sending */
1245 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1250 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1251 ¬ify_connect, ¬ify_disconnect);
1254 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1258 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1260 if (NULL == tc_handle)
1262 FPRINTF (stderr, "%s",
1263 _("Failed to send request to transport service\n") );
1267 start_time = GNUNET_TIME_absolute_get ();
1268 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1271 else if (benchmark_receive) /* -b: Benchmark receiving */
1273 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive, NULL,
1277 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1282 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n") );
1283 start_time = GNUNET_TIME_absolute_get ();
1286 else if (iterate_connections) /* -i: List information about peers once */
1288 address_resolution_in_progress = GNUNET_YES;
1289 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1290 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1291 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1294 else if (monitor_connections) /* -m: List information about peers continuously */
1296 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1297 address_resolution_in_progress = GNUNET_YES;
1298 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1299 GNUNET_NO, TIMEOUT, &process_peer_monitoring_cb, (void *) cfg);
1301 else if (iterate_validation) /* -d: Print information about validations */
1303 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1304 (NULL == cpid) ? NULL : &pid,
1305 GNUNET_YES, TIMEOUT, &process_validation_cb, (void *) cfg);
1307 else if (monitor_validation) /* -f: Print information about validations continuously */
1309 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg,
1310 (NULL == cpid) ? NULL : &pid,
1311 GNUNET_NO, TIMEOUT, &process_validation_cb, (void *) cfg);
1313 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1315 monitor_connect_counter = 0;
1316 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1317 &monitor_notify_connect, &monitor_notify_disconnect);
1320 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1332 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1333 &shutdown_task, NULL );
1338 * Main function that will be run by the scheduler.
1340 * @param cls closure
1341 * @param args remaining command-line arguments
1342 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1343 * @param mycfg configuration
1346 run (void *cls, char * const *args, const char *cfgfile,
1347 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1349 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1350 if (test_configuration)
1352 do_test_configuration (cfg);
1355 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1356 &testservice_task, (void *) cfg);
1360 main (int argc, char * const *argv)
1363 static const struct GNUNET_GETOPT_CommandLineOption options[] =
1366 gettext_noop ("print information for all peers (instead of only connected peers )"),
1367 0, &GNUNET_GETOPT_set_one, &iterate_all },
1368 { 'b', "benchmark", NULL,
1369 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1370 0, &GNUNET_GETOPT_set_one, &benchmark_receive }, { 'C', "connect",
1371 NULL, gettext_noop ("connect to a peer"), 0,
1372 &GNUNET_GETOPT_set_one, &try_connect },
1373 { 'd', "validation", NULL,
1374 gettext_noop ("print information for all pending validations "),
1375 0, &GNUNET_GETOPT_set_one, &iterate_validation },
1376 { 'f', "monitorvalidation", NULL,
1377 gettext_noop ("print information for all pending validations continously"),
1378 0, &GNUNET_GETOPT_set_one, &monitor_validation },
1379 { 'i', "information", NULL,
1380 gettext_noop ("provide information about all current connections (once)"),
1381 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1382 { 'm', "monitor", NULL,
1383 gettext_noop ("provide information about all current connections (continuously)"),
1384 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1385 { 'e', "events", NULL,
1386 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1387 0, &GNUNET_GETOPT_set_one, &monitor_connects }, { 'n', "numeric",
1388 NULL, gettext_noop ("do not resolve hostnames"), 0,
1389 &GNUNET_GETOPT_set_one, &numeric }, { 'p', "peer", "PEER",
1390 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1391 &cpid }, { 's', "send", NULL, gettext_noop
1392 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1393 &GNUNET_GETOPT_set_one, &benchmark_send },
1394 { 't', "test", NULL,
1395 gettext_noop ("test transport configuration (involves external server)"),
1396 0, &GNUNET_GETOPT_set_one, &test_configuration },
1397 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1398 GNUNET_GETOPT_OPTION_END };
1400 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1403 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport", gettext_noop
1404 ("Direct access to transport service."), options, &run, NULL );
1405 GNUNET_free((void * ) argv);
1406 if (GNUNET_OK == res)
1411 /* end of gnunet-transport.c */