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, 60)
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 try_connect;
127 * Global return value (0 success).
132 * Current number of connections in monitor mode
134 static int monitor_connect_counter;
137 * Number of bytes of traffic we received so far.
139 static unsigned long long traffic_received;
142 * Number of bytes of traffic we sent so far.
144 static unsigned long long traffic_sent;
147 * Starting time of transmitting/receiving data.
149 static struct GNUNET_TIME_Absolute start_time;
152 * Handle for current transmission request.
154 static struct GNUNET_TRANSPORT_TransmitHandle *th;
157 * Map storing information about monitored peers
159 static struct GNUNET_CONTAINER_MultiPeerMap *monitored_peers;
164 static struct GNUNET_TRANSPORT_PeerMonitoringContext *pic;
166 static struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic;
169 * Identity of the peer we transmit to / connect to.
170 * (equivalent to 'cpid' string).
172 static struct GNUNET_PeerIdentity pid;
175 * Task scheduled for cleanup / termination of the process.
177 static GNUNET_SCHEDULER_TaskIdentifier end;
180 * Task for operation timeout
182 static GNUNET_SCHEDULER_TaskIdentifier op_timeout;
185 * Selected level of verbosity.
187 static int verbosity;
190 * Resolver process handle.
192 struct GNUNET_OS_Process *resolver;
195 * Number of tasks running that still need the resolver.
197 static unsigned int resolver_users;
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;
211 * Context for a plugin test.
217 * Handle to the active NAT test.
219 struct GNUNET_NAT_Test *tst;
222 * Task identifier for the timeout.
224 GNUNET_SCHEDULER_TaskIdentifier tsk;
227 * Name of plugin under test.
235 enum GNUNET_TRANSPORT_PeerState state;
236 struct GNUNET_TIME_Absolute state_timeout;
237 struct GNUNET_HELLO_Address *address;
241 int destroy_it (void *cls,
242 const struct GNUNET_PeerIdentity *key,
245 struct MonitoredPeer *m = value;
246 GNUNET_CONTAINER_multipeermap_remove (monitored_peers, key, value);
247 GNUNET_free_non_null (m->address);
253 * Task run in monitor mode when the user presses CTRL-C to abort.
254 * Stops monitoring activity.
256 * @param cls the 'struct GNUNET_TRANSPORT_PeerIterateContext *'
257 * @param tc scheduler context
260 shutdown_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
262 struct GNUNET_TIME_Relative duration;
263 end = GNUNET_SCHEDULER_NO_TASK;
264 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
266 GNUNET_SCHEDULER_cancel (op_timeout);
267 op_timeout = GNUNET_SCHEDULER_NO_TASK;
269 if (NULL != tc_handle)
271 GNUNET_TRANSPORT_try_connect_cancel (tc_handle);
276 GNUNET_TRANSPORT_monitor_peers_cancel (pic);
281 GNUNET_TRANSPORT_monitor_validation_entries_cancel (vic);
286 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
291 GNUNET_TRANSPORT_disconnect (handle);
296 duration = GNUNET_TIME_absolute_get_duration (start_time);
297 FPRINTF (stdout, _("Transmitted %llu bytes/s (%llu bytes in %s)\n"),
298 1000LL * 1000LL * traffic_sent / (1 + duration.rel_value_us),
300 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
302 if (benchmark_receive)
304 duration = GNUNET_TIME_absolute_get_duration (start_time);
305 FPRINTF (stdout, _("Received %llu bytes/s (%llu bytes in %s)\n"),
306 1000LL * 1000LL * traffic_received / (1 + duration.rel_value_us),
308 GNUNET_STRINGS_relative_time_to_string (duration, GNUNET_YES));
311 if (NULL != monitored_peers)
313 GNUNET_CONTAINER_multipeermap_iterate (monitored_peers, &destroy_it, NULL);
314 GNUNET_CONTAINER_multipeermap_destroy (monitored_peers);
315 monitored_peers = NULL;
319 static struct PeerResolutionContext *rc_head;
320 static struct PeerResolutionContext *rc_tail;
322 struct PeerResolutionContext
324 struct PeerResolutionContext *next;
325 struct PeerResolutionContext *prev;
326 struct GNUNET_PeerIdentity id;
327 struct GNUNET_HELLO_Address *addrcp;
328 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
329 enum GNUNET_TRANSPORT_PeerState state;
330 struct GNUNET_TIME_Absolute state_timeout;
335 static struct ValidationResolutionContext *vc_head;
336 static struct ValidationResolutionContext *vc_tail;
338 struct ValidationResolutionContext
340 struct ValidationResolutionContext *next;
341 struct ValidationResolutionContext *prev;
343 struct GNUNET_PeerIdentity id;
344 struct GNUNET_HELLO_Address *addrcp;
345 struct GNUNET_TIME_Absolute last_validation;
346 struct GNUNET_TIME_Absolute valid_until;
347 struct GNUNET_TIME_Absolute next_validation;
348 enum GNUNET_TRANSPORT_ValidationState state;
350 struct GNUNET_TRANSPORT_AddressToStringContext *asc;
357 operation_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
359 struct PeerResolutionContext *cur;
360 struct PeerResolutionContext *next;
361 op_timeout = GNUNET_SCHEDULER_NO_TASK;
362 if ((try_connect) || (benchmark_send) || (benchmark_receive))
364 FPRINTF (stdout, _("Failed to connect to `%s'\n"), GNUNET_i2s_full (&pid));
365 if (GNUNET_SCHEDULER_NO_TASK != end)
366 GNUNET_SCHEDULER_cancel (end);
367 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
371 if (iterate_connections)
374 while (NULL != (cur = next))
377 FPRINTF (stdout, _("Failed to resolve address for peer `%s'\n"),
378 GNUNET_i2s (&cur->addrcp->peer));
380 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, cur);
381 GNUNET_TRANSPORT_address_to_string_cancel (cur->asc);
382 GNUNET_free(cur->transport);
383 GNUNET_free(cur->addrcp);
387 FPRINTF (stdout, "%s", _("Failed to list connections, timeout occured\n") );
388 if (GNUNET_SCHEDULER_NO_TASK != end)
389 GNUNET_SCHEDULER_cancel (end);
390 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
398 * Display the result of the test.
400 * @param tc test context
401 * @param result #GNUNET_YES on success
404 display_test_result (struct TestContext *tc, int result)
406 if (GNUNET_YES != result)
408 FPRINTF (stderr, "Configuration for plugin `%s' did not work!\n", tc->name);
412 FPRINTF (stderr, "Configuration for plugin `%s' is working!\n", tc->name);
414 if (GNUNET_SCHEDULER_NO_TASK != tc->tsk)
416 GNUNET_SCHEDULER_cancel (tc->tsk);
417 tc->tsk = GNUNET_SCHEDULER_NO_TASK;
421 GNUNET_NAT_test_stop (tc->tst);
426 if ((0 == resolver_users) && (NULL != resolver))
428 GNUNET_break(0 == GNUNET_OS_process_kill (resolver, GNUNET_TERM_SIG));
429 GNUNET_OS_process_destroy (resolver);
435 * Function called by NAT on success.
436 * Clean up and update GUI (with success).
438 * @param cls test context
439 * @param success currently always #GNUNET_OK
440 * @param emsg error message, NULL on success
443 result_callback (void *cls, int success, const char *emsg)
445 struct TestContext *tc = cls;
447 display_test_result (tc, success);
451 * Function called if NAT failed to confirm success.
452 * Clean up and update GUI (with failure).
454 * @param cls test context
455 * @param tc scheduler callback
458 fail_timeout (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
460 struct TestContext *tstc = cls;
462 tstc->tsk = GNUNET_SCHEDULER_NO_TASK;
463 display_test_result (tstc, GNUNET_NO);
468 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
469 const struct GNUNET_HELLO_Address *address, int numeric,
470 struct GNUNET_TIME_Absolute last_validation,
471 struct GNUNET_TIME_Absolute valid_until,
472 struct GNUNET_TIME_Absolute next_validation,
473 enum GNUNET_TRANSPORT_ValidationState state);
476 process_validation_string (void *cls, const char *address)
478 struct ValidationResolutionContext *vc = cls;
483 if (address != NULL )
485 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->valid_until.abs_value_us)
486 s_valid = GNUNET_strdup("never");
488 s_valid = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->valid_until));
490 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->last_validation.abs_value_us)
491 s_last = GNUNET_strdup("never");
493 s_last = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->last_validation));
495 if (GNUNET_TIME_UNIT_ZERO_ABS.abs_value_us == vc->next_validation.abs_value_us)
496 s_next = GNUNET_strdup("never");
498 s_next = GNUNET_strdup(GNUNET_STRINGS_absolute_time_to_string (vc->next_validation));
501 _("Peer `%s' %s `%s'\n\t%s%s\n\t%s%s\n\t%s%s\n"),
502 GNUNET_i2s (&vc->id), address, GNUNET_TRANSPORT_vs2s (vc->state),
503 "Valid until : ", s_valid,
504 "Last validation: ",s_last,
505 "Next validation: ", s_next);
506 GNUNET_free (s_valid);
507 GNUNET_free (s_last);
508 GNUNET_free (s_next);
509 vc->printed = GNUNET_YES;
514 GNUNET_assert(address_resolutions > 0);
515 address_resolutions--;
516 if (GNUNET_NO == vc->printed)
518 if (numeric == GNUNET_NO)
520 /* Failed to resolve address, try numeric lookup */
521 resolve_validation_address (&vc->id, vc->addrcp, GNUNET_NO,
522 vc->last_validation, vc->valid_until, vc->next_validation,
527 FPRINTF (stdout, _("Peer `%s' %s `%s' \n"),
528 GNUNET_i2s (&vc->id), "<unable to resolve address>",
529 GNUNET_TRANSPORT_vs2s (vc->state));
532 GNUNET_free (vc->transport);
533 GNUNET_free (vc->addrcp);
534 GNUNET_CONTAINER_DLL_remove(vc_head, vc_tail, vc);
536 if ((0 == address_resolutions) && (iterate_validation))
538 if (GNUNET_SCHEDULER_NO_TASK != end)
540 GNUNET_SCHEDULER_cancel (end);
541 end = GNUNET_SCHEDULER_NO_TASK;
543 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
545 GNUNET_SCHEDULER_cancel (op_timeout);
546 op_timeout = GNUNET_SCHEDULER_NO_TASK;
549 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
557 resolve_validation_address (const struct GNUNET_PeerIdentity *id,
558 const struct GNUNET_HELLO_Address *address, int numeric,
559 struct GNUNET_TIME_Absolute last_validation,
560 struct GNUNET_TIME_Absolute valid_until,
561 struct GNUNET_TIME_Absolute next_validation,
562 enum GNUNET_TRANSPORT_ValidationState state)
564 struct ValidationResolutionContext *vc;
566 vc = GNUNET_new (struct ValidationResolutionContext);
567 GNUNET_assert(NULL != vc);
568 GNUNET_CONTAINER_DLL_insert(vc_head, vc_tail, vc);
569 address_resolutions++;
572 vc->transport = GNUNET_strdup(address->transport_name);
573 vc->addrcp = GNUNET_HELLO_address_copy (address);
574 vc->printed = GNUNET_NO;
576 vc->last_validation = last_validation;
577 vc->valid_until = valid_until;
578 vc->next_validation = next_validation;
580 /* Resolve address to string */
581 vc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
582 RESOLUTION_TIMEOUT, &process_validation_string, vc);
586 void process_validation_cb (void *cls,
587 const struct GNUNET_PeerIdentity *peer,
588 const struct GNUNET_HELLO_Address *address,
589 struct GNUNET_TIME_Absolute last_validation,
590 struct GNUNET_TIME_Absolute valid_until,
591 struct GNUNET_TIME_Absolute next_validation,
592 enum GNUNET_TRANSPORT_ValidationState state)
594 if ((NULL == peer) && (NULL == address))
598 if (GNUNET_SCHEDULER_NO_TASK != end)
599 GNUNET_SCHEDULER_cancel (end);
600 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
603 if ((NULL == peer) || (NULL == address))
605 /* invalid response */
607 if (GNUNET_SCHEDULER_NO_TASK != end)
608 GNUNET_SCHEDULER_cancel (end);
609 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
612 resolve_validation_address (peer, address,
613 numeric, last_validation,
614 valid_until, next_validation, state);
618 * Test our plugin's configuration (NAT traversal, etc.).
620 * @param cfg configuration to test
623 do_test_configuration (const struct GNUNET_CONFIGURATION_Handle *cfg)
627 unsigned long long bnd_port;
628 unsigned long long adv_port;
629 struct TestContext *tc;
633 != GNUNET_CONFIGURATION_get_value_string (cfg, "transport", "plugins",
636 FPRINTF (stderr, "%s", _
637 ("No transport plugins configured, peer will never communicate\n") );
641 for (tok = strtok (plugins, " "); tok != NULL ; tok = strtok (NULL, " "))
643 char section[12 + strlen (tok)];
645 GNUNET_snprintf (section, sizeof(section), "transport-%s", tok);
647 != GNUNET_CONFIGURATION_get_value_number (cfg, section, "PORT",
651 _("No port configured for plugin `%s', cannot test it\n"), tok);
655 != GNUNET_CONFIGURATION_get_value_number (cfg, section,
656 "ADVERTISED_PORT", &adv_port))
658 if (NULL == resolver)
660 binary = GNUNET_OS_get_libexec_binary_path ("gnunet-service-resolver");
661 resolver = GNUNET_OS_start_process (GNUNET_YES,
662 GNUNET_OS_INHERIT_STD_OUT_AND_ERR, NULL, NULL, binary,
663 "gnunet-service-resolver", NULL );
667 GNUNET_RESOLVER_connect (cfg);
668 tc = GNUNET_new (struct TestContext);
669 tc->name = GNUNET_strdup (tok);
670 tc->tst = GNUNET_NAT_test_start (cfg,
671 (0 == strcasecmp (tok, "udp")) ? GNUNET_NO : GNUNET_YES,
672 (uint16_t) bnd_port, (uint16_t) adv_port, &result_callback, tc);
675 display_test_result (tc, GNUNET_SYSERR);
678 tc->tsk = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &fail_timeout, tc);
680 GNUNET_free(plugins);
684 * Function called to notify a client about the socket
685 * begin ready to queue more data. @a buf will be
686 * NULL and @a size zero if the socket was closed for
687 * writing in the meantime.
690 * @param size number of bytes available in @a buf
691 * @param buf where the callee should write the message
692 * @return number of bytes written to @a buf
695 transmit_data (void *cls, size_t size, void *buf)
697 struct GNUNET_MessageHeader *m = buf;
699 if ((NULL == buf) || (0 == size))
705 GNUNET_assert(size >= sizeof(struct GNUNET_MessageHeader));
706 GNUNET_assert(size < GNUNET_SERVER_MAX_MESSAGE_SIZE);
707 m->size = ntohs (size);
708 m->type = ntohs (GNUNET_MESSAGE_TYPE_DUMMY);
709 memset (&m[1], 52, size - sizeof(struct GNUNET_MessageHeader));
710 traffic_sent += size;
711 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, &pid, BLOCKSIZE * 1024,
712 0, GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data, NULL );
714 FPRINTF (stdout, _("Transmitting %u bytes to %s\n"), (unsigned int) size,
720 * Function called to notify transport users that another
721 * peer connected to us.
724 * @param peer the peer that connected
727 notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
729 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
734 /* all done, terminate instantly */
735 FPRINTF (stdout, _("Successfully connected to `%s'\n"),
736 GNUNET_i2s_full (peer));
739 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
741 GNUNET_SCHEDULER_cancel (op_timeout);
742 op_timeout = GNUNET_SCHEDULER_NO_TASK;
745 if (GNUNET_SCHEDULER_NO_TASK != end)
746 GNUNET_SCHEDULER_cancel (end);
747 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
752 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
754 GNUNET_SCHEDULER_cancel (op_timeout);
755 op_timeout = GNUNET_SCHEDULER_NO_TASK;
759 _("Successfully connected to `%s', starting to send benchmark data in %u Kb blocks\n"),
760 GNUNET_i2s (&pid), BLOCKSIZE);
761 start_time = GNUNET_TIME_absolute_get ();
763 th = GNUNET_TRANSPORT_notify_transmit_ready (handle, peer,
764 BLOCKSIZE * 1024, 0, GNUNET_TIME_UNIT_FOREVER_REL, &transmit_data,
773 * Function called to notify transport users that another
774 * peer disconnected from us.
777 * @param peer the peer that disconnected
780 notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
782 if (0 != memcmp (&pid, peer, sizeof(struct GNUNET_PeerIdentity)))
787 GNUNET_TRANSPORT_notify_transmit_ready_cancel (th);
792 FPRINTF (stdout, _("Disconnected from peer `%s' while benchmarking\n"),
794 if (GNUNET_SCHEDULER_NO_TASK != end)
795 GNUNET_SCHEDULER_cancel (end);
801 * Function called to notify transport users that another
802 * peer connected to us.
805 * @param peer the peer that connected
808 monitor_notify_connect (void *cls, const struct GNUNET_PeerIdentity *peer)
810 monitor_connect_counter++;
811 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
812 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
814 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str,
815 _("Connected to"), GNUNET_i2s (peer), monitor_connect_counter);
819 * Function called to notify transport users that another
820 * peer disconnected from us.
823 * @param peer the peer that disconnected
826 monitor_notify_disconnect (void *cls, const struct GNUNET_PeerIdentity *peer)
828 struct GNUNET_TIME_Absolute now = GNUNET_TIME_absolute_get ();
829 const char *now_str = GNUNET_STRINGS_absolute_time_to_string (now);
831 GNUNET_assert(monitor_connect_counter > 0);
832 monitor_connect_counter--;
834 FPRINTF (stdout, _("%24s: %-17s %4s (%u connections in total)\n"), now_str,
835 _("Disconnected from"), GNUNET_i2s (peer), monitor_connect_counter);
839 * Function called by the transport for each received message.
842 * @param peer (claimed) identity of the other peer
843 * @param message the message
846 notify_receive (void *cls, const struct GNUNET_PeerIdentity *peer,
847 const struct GNUNET_MessageHeader *message)
849 if (benchmark_receive)
851 if (GNUNET_MESSAGE_TYPE_DUMMY != ntohs (message->type))
854 FPRINTF (stdout, _("Received %u bytes from %s\n"),
855 (unsigned int) ntohs (message->size), GNUNET_i2s (peer));
857 if (traffic_received == 0)
858 start_time = GNUNET_TIME_absolute_get ();
859 traffic_received += ntohs (message->size);
865 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
866 const struct GNUNET_HELLO_Address *address, int numeric,
867 enum GNUNET_TRANSPORT_PeerState state,
868 struct GNUNET_TIME_Absolute state_timeout);
871 print_info (const struct GNUNET_PeerIdentity *id, const char *transport,
872 const char *addr, enum GNUNET_TRANSPORT_PeerState state,
873 struct GNUNET_TIME_Absolute state_timeout)
875 if ((GNUNET_YES == iterate_all) || (GNUNET_YES == monitor_connections) )
877 FPRINTF (stdout, _("Peer `%s': %s %s in state `%s' until %s\n"),
879 (NULL == transport) ? "<none>" : transport,
880 (NULL == transport) ? "<none>" : addr,
881 GNUNET_TRANSPORT_ps2s (state),
882 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
886 /* Only connected peers, skip state */
887 FPRINTF (stdout, _("Peer `%s': %s %s\n"), GNUNET_i2s (id), transport, addr);
893 process_peer_string (void *cls, const char *address)
895 struct PeerResolutionContext *rc = cls;
897 if (address != NULL )
899 print_info (&rc->id, rc->transport, address, rc->state, rc->state_timeout);
900 rc->printed = GNUNET_YES;
905 GNUNET_assert(address_resolutions > 0);
906 address_resolutions--;
907 if (GNUNET_NO == rc->printed)
909 if (numeric == GNUNET_NO)
911 /* Failed to resolve address, try numeric lookup */
912 resolve_peer_address (&rc->id, rc->addrcp, GNUNET_YES,
913 rc->state, rc->state_timeout);
917 print_info (&rc->id, rc->transport, "<unable to resolve address>",
918 rc->state, rc->state_timeout);
921 GNUNET_free (rc->transport);
922 GNUNET_free (rc->addrcp);
923 GNUNET_CONTAINER_DLL_remove(rc_head, rc_tail, rc);
925 if ((0 == address_resolutions) && (iterate_connections))
927 if (GNUNET_SCHEDULER_NO_TASK != end)
929 GNUNET_SCHEDULER_cancel (end);
930 end = GNUNET_SCHEDULER_NO_TASK;
932 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
934 GNUNET_SCHEDULER_cancel (op_timeout);
935 op_timeout = GNUNET_SCHEDULER_NO_TASK;
938 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
944 resolve_peer_address (const struct GNUNET_PeerIdentity *id,
945 const struct GNUNET_HELLO_Address *address, int numeric,
946 enum GNUNET_TRANSPORT_PeerState state,
947 struct GNUNET_TIME_Absolute state_timeout)
949 struct PeerResolutionContext *rc;
951 rc = GNUNET_new (struct PeerResolutionContext);
952 GNUNET_assert(NULL != rc);
953 GNUNET_CONTAINER_DLL_insert(rc_head, rc_tail, rc);
954 address_resolutions++;
957 rc->transport = GNUNET_strdup(address->transport_name);
958 rc->addrcp = GNUNET_HELLO_address_copy (address);
959 rc->printed = GNUNET_NO;
961 rc->state_timeout = state_timeout;
962 /* Resolve address to string */
963 rc->asc = GNUNET_TRANSPORT_address_to_string (cfg, address, numeric,
964 RESOLUTION_TIMEOUT, &process_peer_string, rc);
968 * Function called with information about a peers during a one shot iteration
971 * @param peer identity of the peer, NULL for final callback when operation done
972 * @param address binary address used to communicate with this peer,
973 * NULL on disconnect or when done
974 * @param state current state this peer is in
975 * @param state_timeout time out for the current state
979 process_peer_iteration_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
980 const struct GNUNET_HELLO_Address *address,
981 enum GNUNET_TRANSPORT_PeerState state,
982 struct GNUNET_TIME_Absolute state_timeout)
987 address_resolution_in_progress = GNUNET_NO;
989 if (GNUNET_SCHEDULER_NO_TASK != end)
990 GNUNET_SCHEDULER_cancel (end);
991 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
995 if ((GNUNET_NO == iterate_all) && (GNUNET_NO == GNUNET_TRANSPORT_is_connected(state)) )
996 return; /* Display only connected peers */
998 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
999 GNUNET_SCHEDULER_cancel (op_timeout);
1000 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1003 GNUNET_log(GNUNET_ERROR_TYPE_DEBUG, "Received address for peer `%s': %s\n",
1004 GNUNET_i2s (peer), address->transport_name);
1006 if (NULL != address)
1007 resolve_peer_address (peer, address, numeric, state, state_timeout);
1009 print_info (peer, NULL, NULL, state, state_timeout);
1014 * Function called with information about a peers
1016 * @param cls closure
1017 * @param peer identity of the peer, NULL for final callback when operation done
1018 * @param address binary address used to communicate with this peer,
1019 * NULL on disconnect or when done
1020 * @param state current state this peer is in
1021 * @param state_timeout time out for the current state
1025 process_peer_monitoring_cb (void *cls, const struct GNUNET_PeerIdentity *peer,
1026 const struct GNUNET_HELLO_Address *address,
1027 enum GNUNET_TRANSPORT_PeerState state,
1028 struct GNUNET_TIME_Absolute state_timeout)
1030 struct MonitoredPeer *m;
1035 address_resolution_in_progress = GNUNET_NO;
1037 if (GNUNET_SCHEDULER_NO_TASK != end)
1038 GNUNET_SCHEDULER_cancel (end);
1039 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1043 if (GNUNET_SCHEDULER_NO_TASK != op_timeout)
1044 GNUNET_SCHEDULER_cancel (op_timeout);
1045 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1048 if (NULL == (m = GNUNET_CONTAINER_multipeermap_get (monitored_peers, peer)))
1050 m = GNUNET_new (struct MonitoredPeer);
1051 GNUNET_CONTAINER_multipeermap_put (monitored_peers, peer,
1052 m, GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_FAST);
1056 if ( (m->state == state) &&
1057 (m->state_timeout.abs_value_us == state_timeout.abs_value_us) &&
1058 ((NULL == address) && (NULL == m->address)) )
1060 return; /* No real change */
1062 if ( ((NULL != address) && (NULL != m->address)) &&
1063 (0 == GNUNET_HELLO_address_cmp(m->address, address)) )
1064 return; /* No real change */
1068 if (NULL != m->address)
1070 GNUNET_free (m->address);
1073 if (NULL != address)
1074 m->address = GNUNET_HELLO_address_copy (address);
1076 m->state_timeout = state_timeout;
1078 if (NULL != address)
1079 resolve_peer_address (peer, m->address, numeric, m->state, m->state_timeout);
1081 print_info (peer, NULL, NULL, m->state, m->state_timeout);
1085 try_connect_cb (void *cls, const int result)
1087 static int retries = 0;
1088 if (GNUNET_OK == result)
1095 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1099 FPRINTF (stderr, "%s",
1100 _("Failed to send connect request to transport service\n") );
1101 if (GNUNET_SCHEDULER_NO_TASK != end)
1102 GNUNET_SCHEDULER_cancel (end);
1104 end = GNUNET_SCHEDULER_add_now (&shutdown_task, NULL );
1110 * Function called with the result of the check if the 'transport'
1111 * service is running.
1113 * @param cls closure with our configuration
1114 * @param result #GNUNET_YES if transport is running
1117 testservice_task (void *cls, int result)
1122 if (GNUNET_YES != result)
1124 FPRINTF (stderr, _("Service `%s' is not running\n"), "transport");
1130 != GNUNET_CRYPTO_eddsa_public_key_from_string (cpid, strlen (cpid),
1133 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), cpid);
1137 counter = benchmark_send + benchmark_receive + iterate_connections
1138 + monitor_connections + monitor_connects + try_connect + iterate_validation;
1143 _("Multiple operations given. Please choose only one operation: %s, %s, %s, %s, %s, %s\n"),
1144 "connect", "benchmark send", "benchmark receive", "information",
1145 "monitor", "events");
1151 _("No operation given. Please choose one operation: %s, %s, %s, %s, %s, %s\n"),
1152 "connect", "benchmark send", "benchmark receive", "information",
1153 "monitor", "events");
1157 if (try_connect) /* -C: Connect to peer */
1161 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1166 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1167 ¬ify_connect, ¬ify_disconnect);
1170 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1174 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1176 if (NULL == tc_handle)
1178 FPRINTF (stderr, "%s",
1179 _("Failed to send request to transport service\n") );
1183 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1187 else if (benchmark_send) /* -s: Benchmark sending */
1191 FPRINTF (stderr, _("Option `%s' makes no sense without option `%s'.\n"),
1196 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive,
1197 ¬ify_connect, ¬ify_disconnect);
1200 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1204 tc_handle = GNUNET_TRANSPORT_try_connect (handle, &pid, try_connect_cb,
1206 if (NULL == tc_handle)
1208 FPRINTF (stderr, "%s",
1209 _("Failed to send request to transport service\n") );
1213 start_time = GNUNET_TIME_absolute_get ();
1214 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1217 else if (benchmark_receive) /* -b: Benchmark receiving */
1219 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, ¬ify_receive, NULL,
1223 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1228 FPRINTF (stdout, "%s", _("Starting to receive benchmark data\n") );
1229 start_time = GNUNET_TIME_absolute_get ();
1232 else if (iterate_connections) /* -i: List information about peers once */
1234 address_resolution_in_progress = GNUNET_YES;
1235 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1236 GNUNET_YES, TIMEOUT, &process_peer_iteration_cb, (void *) cfg);
1237 op_timeout = GNUNET_SCHEDULER_add_delayed (OP_TIMEOUT, &operation_timeout,
1240 else if (monitor_connections) /* -m: List information about peers continuously */
1242 monitored_peers = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
1243 address_resolution_in_progress = GNUNET_YES;
1244 pic = GNUNET_TRANSPORT_monitor_peers (cfg, (NULL == cpid) ? NULL : &pid,
1245 GNUNET_NO, TIMEOUT, &process_peer_monitoring_cb, (void *) cfg);
1247 else if (iterate_validation) /* -d: Print information about validations */
1249 vic = GNUNET_TRANSPORT_monitor_validation_entries (cfg, (NULL == cpid) ? NULL : &pid,
1250 GNUNET_YES, TIMEOUT, &process_validation_cb, (void *) cfg);
1252 else if (monitor_connects) /* -e : Monitor (dis)connect events continuously */
1254 monitor_connect_counter = 0;
1255 handle = GNUNET_TRANSPORT_connect (cfg, NULL, NULL, NULL,
1256 &monitor_notify_connect, &monitor_notify_disconnect);
1259 FPRINTF (stderr, "%s", _("Failed to connect to transport service\n") );
1271 end = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
1272 &shutdown_task, NULL );
1277 * Main function that will be run by the scheduler.
1279 * @param cls closure
1280 * @param args remaining command-line arguments
1281 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
1282 * @param mycfg configuration
1285 run (void *cls, char * const *args, const char *cfgfile,
1286 const struct GNUNET_CONFIGURATION_Handle *mycfg)
1288 cfg = (struct GNUNET_CONFIGURATION_Handle *) mycfg;
1289 if (test_configuration)
1291 do_test_configuration (cfg);
1294 GNUNET_CLIENT_service_test ("transport", cfg, GNUNET_TIME_UNIT_SECONDS,
1295 &testservice_task, (void *) cfg);
1299 main (int argc, char * const *argv)
1302 static const struct GNUNET_GETOPT_CommandLineOption options[] =
1305 gettext_noop ("print information for all peers (instead of only connected peers )"),
1306 0, &GNUNET_GETOPT_set_one, &iterate_all },
1307 { 'b', "benchmark", NULL,
1308 gettext_noop ("measure how fast we are receiving data from all peers (until CTRL-C)"),
1309 0, &GNUNET_GETOPT_set_one, &benchmark_receive }, { 'C', "connect",
1310 NULL, gettext_noop ("connect to a peer"), 0,
1311 &GNUNET_GETOPT_set_one, &try_connect },
1312 { 'd', "validation", NULL,
1313 gettext_noop ("print information for all pending validations "),
1314 0, &GNUNET_GETOPT_set_one, &iterate_validation },
1315 { 'i', "information", NULL,
1316 gettext_noop ("provide information about all current connections (once)"),
1317 0, &GNUNET_GETOPT_set_one, &iterate_connections },
1318 { 'm', "monitor", NULL,
1319 gettext_noop ("provide information about all current connections (continuously)"),
1320 0, &GNUNET_GETOPT_set_one, &monitor_connections },
1321 { 'e', "events", NULL,
1322 gettext_noop ("provide information about all connects and disconnect events (continuously)"),
1323 0, &GNUNET_GETOPT_set_one, &monitor_connects }, { 'n', "numeric",
1324 NULL, gettext_noop ("do not resolve hostnames"), 0,
1325 &GNUNET_GETOPT_set_one, &numeric }, { 'p', "peer", "PEER",
1326 gettext_noop ("peer identity"), 1, &GNUNET_GETOPT_set_string,
1327 &cpid }, { 's', "send", NULL, gettext_noop
1328 ("send data for benchmarking to the other peer (until CTRL-C)"), 0,
1329 &GNUNET_GETOPT_set_one, &benchmark_send },
1330 { 't', "test", NULL,
1331 gettext_noop ("test transport configuration (involves external server)"),
1332 0, &GNUNET_GETOPT_set_one, &test_configuration },
1333 GNUNET_GETOPT_OPTION_VERBOSE (&verbosity),
1334 GNUNET_GETOPT_OPTION_END };
1336 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1339 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-transport", gettext_noop
1340 ("Direct access to transport service."), options, &run, NULL );
1341 GNUNET_free((void * ) argv);
1342 if (GNUNET_OK == res)
1347 /* end of gnunet-transport.c */