2 This file is part of GNUnet.
3 Copyright (C) 2009--2015 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @file ats-tool/gnunet-ats.c
23 * @brief ATS command line tool
24 * @author Matthias Wachs
25 * @author Christian Grothoff
28 #include "gnunet_util_lib.h"
29 #include "gnunet_ats_service.h"
30 #include "gnunet_transport_service.h"
33 * String to respresent unlimited
35 #define UNLIMITED_STRING "unlimited"
41 static int opt_resolve_addresses_numeric;
44 * CLI Opt: Print verbose ATS information
46 static int opt_verbose;
49 * CLI Option: List only addresses currently used (active)
51 static int opt_list_used;
54 * CLI Option: List all addresses
56 static int opt_list_all;
59 * CLI Option: set preference
61 static int opt_set_pref;
64 * CLI Option: print quotas configured
66 static int opt_print_quotas;
69 * CLI Option: Monitor addresses used
71 static int opt_monitor;
74 * CLI Option: use specific peer
76 static char *opt_pid_str;
79 * CLI Option: preference type to set
81 static char *opt_type_str;
84 * CLI Option: preference value to set
86 static unsigned int opt_pref_value;
94 * Number of results returned from service
96 static int stat_results;
99 * State: all pending receive operations done?
101 static int stat_receive_done;
104 * State: number of pending operations
106 static int stat_pending;
109 * Which peer should we connect to?
111 static char *cpid_str;
114 * ATS performance handle used
116 static struct GNUNET_ATS_PerformanceHandle *ph;
119 * Our connectivity handle.
121 static struct GNUNET_ATS_ConnectivityHandle *ats_ch;
124 * Handle for address suggestion request.
126 static struct GNUNET_ATS_ConnectivitySuggestHandle *ats_sh;
129 * ATS address list handle used
131 static struct GNUNET_ATS_AddressListHandle *alh;
134 * Configuration handle
136 static struct GNUNET_CONFIGURATION_Handle *cfg;
141 static struct GNUNET_SCHEDULER_Task *shutdown_task;
144 * Hashmap to store addresses
146 static struct GNUNET_CONTAINER_MultiPeerMap *addresses;
150 * Structure used to remember all pending address resolutions.
151 * We keep address information in here while we talk to transport
152 * to map the address to a string.
154 struct PendingResolutions
159 struct PendingResolutions *next;
164 struct PendingResolutions *prev;
167 * Copy of the address we are resolving.
169 struct GNUNET_HELLO_Address *address;
172 * Handle to the transport request to convert the address
175 struct GNUNET_TRANSPORT_AddressToStringContext *tats_ctx;
180 struct GNUNET_ATS_Properties properties;
183 * Amount of outbound bandwidth assigned by ATS.
185 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
188 * Amount of inbound bandwidth assigned by ATS.
190 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
193 * Is this an active address?
200 * Information we keep for an address. Used to avoid
201 * printing the same data multiple times.
206 * Address information.
208 struct GNUNET_HELLO_Address *address;
211 * Current outbound bandwidth.
213 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
216 * Current inbound bandwidth.
218 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
221 * Is this an active address?
228 * Head of list of pending resolution requests.
230 static struct PendingResolutions *head;
233 * Tail of list of pending resolution requests.
235 static struct PendingResolutions *tail;
239 * Free address corresponding to a given peer.
242 * @param key peer identity
243 * @param value the `struct ATSAddress *` to be freed
244 * @return #GNUNET_YES (always)
247 free_addr_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
249 struct ATSAddress *a = value;
251 GNUNET_assert (GNUNET_OK ==
252 GNUNET_CONTAINER_multipeermap_remove (addresses, key, value));
253 GNUNET_HELLO_address_free (a->address);
260 * Task run on shutdown.
267 struct PendingResolutions *pr;
268 struct PendingResolutions *next;
269 unsigned int pending;
273 GNUNET_ATS_performance_list_addresses_cancel (alh);
279 GNUNET_ATS_performance_done (ph);
285 while (NULL != (pr = next))
288 GNUNET_CONTAINER_DLL_remove (head, tail, pr);
289 GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
290 GNUNET_free (pr->address);
294 GNUNET_CONTAINER_multipeermap_iterate (addresses, &free_addr_it, NULL);
295 GNUNET_CONTAINER_multipeermap_destroy (addresses);
299 fprintf (stdout, _ ("%u address resolutions had a timeout\n"), pending);
300 if (opt_list_used || opt_list_all)
302 _ ("ATS returned stat_results for %u addresses\n"),
307 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
312 GNUNET_ATS_connectivity_done (ats_ch);
320 * Function to call with a textual representation of an address. This
321 * function will be called several times with different possible
322 * textual representations, and a last time with @a address being NULL
323 * to signal the end of the iteration. Note that @a address NULL
324 * always is the last call, regardless of the value in @a res.
326 * @param cls closure, a `struct PendingResolutions *`
327 * @param address NULL on end of iteration,
328 * otherwise 0-terminated printable UTF-8 string,
329 * in particular an empty string if @a res is #GNUNET_NO
330 * @param res result of the address to string conversion:
331 * if #GNUNET_OK: conversion successful
332 * if #GNUNET_NO: address was invalid (or not supported)
333 * if #GNUNET_SYSERR: communication error (IPC error)
336 transport_addr_to_str_cb (void *cls, const char *address, int res)
338 struct PendingResolutions *pr = cls;
343 GNUNET_CONTAINER_DLL_remove (head, tail, pr);
344 GNUNET_free (pr->address);
348 if ((GNUNET_YES == stat_receive_done) && (0 == stat_pending))
350 /* All messages received and no resolutions pending*/
351 if (shutdown_task != NULL)
352 GNUNET_SCHEDULER_cancel (shutdown_task);
353 shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
362 "Failed to convert address for peer `%s' plugin `%s' length %u to string (communication error)\n",
363 GNUNET_i2s (&pr->address->peer),
364 pr->address->transport_name,
365 (unsigned int) pr->address->address_length);
371 "Failed to convert address for peer `%s' plugin `%s' length %u to string (address invalid or not supported)\n",
372 GNUNET_i2s (&pr->address->peer),
373 pr->address->transport_name,
374 (unsigned int) pr->address->address_length);
378 /* continues below */
389 "Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
390 GNUNET_i2s (&pr->address->peer),
391 pr->address->transport_name,
393 GNUNET_NT_to_string (pr->properties.scope),
394 ntohl (pr->bandwidth_out.value__),
395 ntohl (pr->bandwidth_in.value__),
396 pr->active ? _ ("active ") : _ ("inactive "));
401 * Closure for #find_address_it().
403 struct AddressFindCtx
406 * Address we are looking for.
408 const struct GNUNET_HELLO_Address *src;
411 * Where to write the `struct ATSAddress` if we found one that matches.
413 struct ATSAddress *res;
418 * Find address corresponding to a given peer.
420 * @param cls the `struct AddressFindCtx *`
421 * @param key peer identity
422 * @param value the `struct ATSAddress *` for an existing address
423 * @return #GNUNET_NO if we found a match, #GNUNET_YES if not
426 find_address_it (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
428 struct AddressFindCtx *actx = cls;
429 struct ATSAddress *exist = value;
431 if (0 == GNUNET_HELLO_address_cmp (actx->src, exist->address))
441 * Signature of a function that is called with QoS information about an address.
443 * @param cls closure (NULL)
444 * @param address the address, NULL if ATS service was disconnected
445 * @param active #GNUNET_YES if this address is actively used
446 * to maintain a connection to a peer;
447 * #GNUNET_NO if the address is not actively used;
448 * #GNUNET_SYSERR if this address is no longer available for ATS
449 * @param bandwidth_out assigned outbound bandwidth for the connection
450 * @param bandwidth_in assigned inbound bandwidth for the connection
451 * @param prop performance data for the address (as far as known)
454 ats_perf_mon_cb (void *cls,
455 const struct GNUNET_HELLO_Address *address,
457 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
458 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
459 const struct GNUNET_ATS_Properties *prop)
461 struct PendingResolutions *pr;
462 struct PendingResolutions *cur;
463 struct PendingResolutions *next;
467 /* ATS service temporarily disconnected, remove current state */
469 for (cur = next; NULL != cur; cur = next)
472 GNUNET_CONTAINER_DLL_remove (head, tail, cur);
473 GNUNET_TRANSPORT_address_to_string_cancel (cur->tats_ctx);
474 GNUNET_HELLO_address_free (cur->address);
477 GNUNET_CONTAINER_multipeermap_iterate (addresses, &free_addr_it, NULL);
480 if (GNUNET_SYSERR == active)
483 struct AddressFindCtx actx;
487 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
491 if (NULL == actx.res)
496 GNUNET_break (GNUNET_OK ==
497 GNUNET_CONTAINER_multipeermap_remove (addresses,
501 _ ("Removed address of peer `%s' with plugin `%s'\n"),
502 GNUNET_i2s (&address->peer),
503 actx.res->address->transport_name);
504 GNUNET_HELLO_address_free (actx.res);
508 if (GNUNET_NO == opt_verbose)
510 struct AddressFindCtx actx;
511 struct ATSAddress *a;
515 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
519 if ((NULL != actx.res))
521 if ((bandwidth_in.value__ == actx.res->bandwidth_in.value__) &&
522 (bandwidth_out.value__ == actx.res->bandwidth_out.value__) &&
523 (active == actx.res->active))
525 return; /* Nothing to do here */
529 actx.res->bandwidth_in = bandwidth_in;
530 actx.res->bandwidth_out = bandwidth_out;
535 a = GNUNET_new (struct ATSAddress);
537 a->address = GNUNET_HELLO_address_copy (address);
538 a->bandwidth_in = bandwidth_in;
539 a->bandwidth_out = bandwidth_out;
541 GNUNET_CONTAINER_multipeermap_put (
545 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
549 pr = GNUNET_new (struct PendingResolutions);
550 pr->properties = *prop;
551 pr->address = GNUNET_HELLO_address_copy (address);
552 pr->bandwidth_in = bandwidth_in;
553 pr->bandwidth_out = bandwidth_out;
555 pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (
558 opt_resolve_addresses_numeric,
559 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
560 &transport_addr_to_str_cb,
562 GNUNET_CONTAINER_DLL_insert (head, tail, pr);
569 * Signature of a function that is called with QoS information about an address.
571 * @param cls closure (NULL)
572 * @param address the address, NULL if ATS service was disconnected
573 * @param active is this address actively used to maintain a connection
575 * @param bandwidth_out assigned outbound bandwidth for the connection
576 * @param bandwidth_in assigned inbound bandwidth for the connection
577 * @param prop performance data for the address (as far as known)
580 ats_perf_cb (void *cls,
581 const struct GNUNET_HELLO_Address *address,
583 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
584 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
585 const struct GNUNET_ATS_Properties *prop)
587 struct PendingResolutions *pr;
591 /* All messages received */
592 stat_receive_done = GNUNET_YES;
594 if (0 == stat_pending)
596 /* All messages received and no resolutions pending*/
597 if (shutdown_task != NULL)
598 GNUNET_SCHEDULER_cancel (shutdown_task);
599 shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
604 pr = GNUNET_new (struct PendingResolutions);
605 pr->properties = *prop;
606 pr->address = GNUNET_HELLO_address_copy (address);
607 pr->bandwidth_in = bandwidth_in;
608 pr->bandwidth_out = bandwidth_out;
610 pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (
613 opt_resolve_addresses_numeric,
614 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
615 &transport_addr_to_str_cb,
617 GNUNET_CONTAINER_DLL_insert (head, tail, pr);
624 * Print information about the quotas configured for the various
627 * @param cfg configuration to obtain quota information from
628 * @return total number of ATS network types known
631 print_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg)
633 char *entry_in = NULL;
634 char *entry_out = NULL;
637 unsigned long long int quota_out;
638 unsigned long long int quota_in;
641 for (c = 0; (c < GNUNET_NT_COUNT); c++)
643 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", GNUNET_NT_to_string (c));
644 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", GNUNET_NT_to_string (c));
647 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
652 if ((0 == strcmp (quota_out_str, UNLIMITED_STRING)) ||
654 GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, "a_out)))
655 quota_out = UINT32_MAX;
657 GNUNET_free (quota_out_str);
658 GNUNET_asprintf ("a_out_str, "%llu", quota_out);
663 "Outbound quota for network `%11s' not configured!\n",
664 GNUNET_NT_to_string (c));
665 GNUNET_asprintf ("a_out_str, "-");
667 GNUNET_free (entry_out);
670 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string (cfg,
675 if ((0 == strcmp (quota_in_str, UNLIMITED_STRING)) ||
677 GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, "a_in)))
678 quota_in = UINT32_MAX;
679 GNUNET_free (quota_in_str);
680 GNUNET_asprintf ("a_in_str, "%llu", quota_in);
685 "Inbound quota for network `%11s' not configured!\n",
686 GNUNET_NT_to_string (c));
687 GNUNET_asprintf ("a_in_str, "-");
689 GNUNET_free (entry_in);
692 _ ("Quota for network `%11s' (in/out): %10s / %10s\n"),
693 GNUNET_NT_to_string (c),
696 GNUNET_free (quota_out_str);
697 GNUNET_free (quota_in_str);
699 return GNUNET_NT_COUNT;
704 * Main function that will be run by the scheduler.
707 * @param args remaining command-line arguments
708 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
709 * @param my_cfg configuration
715 const struct GNUNET_CONFIGURATION_Handle *my_cfg)
717 struct GNUNET_PeerIdentity pid;
718 struct GNUNET_PeerIdentity cpid;
722 cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
723 addresses = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
727 if (NULL != opt_pid_str)
730 GNUNET_CRYPTO_eddsa_public_key_from_string (opt_pid_str,
731 strlen (opt_pid_str),
734 fprintf (stderr, _ ("Failed to parse peer identity `%s'\n"), opt_pid_str);
738 if (NULL != cpid_str)
741 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid_str,
745 fprintf (stderr, _ ("Failed to parse peer identity `%s'\n"), cpid_str);
751 c += opt_list_all + opt_list_used + opt_monitor + opt_set_pref;
756 _ ("Please select one operation: %s or %s or %s or %s or %s\n"),
765 opt_list_used = GNUNET_YES; /* set default */
766 if (opt_print_quotas)
768 ret = print_quotas (cfg);
773 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
776 fprintf (stderr, "%s", _ ("Cannot connect to ATS service, exiting...\n"));
779 alh = GNUNET_ATS_performance_list_addresses (ph,
780 (NULL == opt_pid_str) ? NULL
789 _ ("Cannot issue request to ATS service, exiting...\n"));
790 shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
793 shutdown_task = GNUNET_SCHEDULER_add_shutdown (&end, NULL);
798 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
800 fprintf (stderr, "%s", _ ("Cannot connect to ATS service, exiting...\n"));
802 alh = GNUNET_ATS_performance_list_addresses (ph,
803 (NULL == opt_pid_str) ? NULL
812 _ ("Cannot issue request to ATS service, exiting...\n"));
813 shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
816 shutdown_task = GNUNET_SCHEDULER_add_shutdown (&end, NULL);
821 ph = GNUNET_ATS_performance_init (cfg, &ats_perf_mon_cb, NULL);
822 shutdown_task = GNUNET_SCHEDULER_add_shutdown (&end, NULL);
825 fprintf (stderr, "%s", _ ("Cannot connect to ATS service, exiting...\n"));
826 GNUNET_SCHEDULER_shutdown ();
832 if (NULL == opt_type_str)
834 fprintf (stderr, "%s", _ ("No preference type given!\n"));
837 if (NULL == opt_pid_str)
839 fprintf (stderr, "%s", _ ("No peer given!\n"));
843 for (c = 0; c < strlen (opt_type_str); c++)
845 if (isupper ((unsigned char) opt_type_str[c]))
846 opt_type_str[c] = tolower ((unsigned char) opt_type_str[c]);
849 if (0 == strcasecmp ("latency", opt_type_str))
850 type = GNUNET_ATS_PREFERENCE_LATENCY;
851 else if (0 == strcasecmp ("bandwidth", opt_type_str))
852 type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
855 fprintf (stderr, "%s", _ ("Valid type required\n"));
860 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
862 fprintf (stderr, "%s", _ ("Cannot connect to ATS service, exiting...\n"));
864 GNUNET_ATS_performance_change_preference (ph,
867 (double) opt_pref_value,
868 GNUNET_ATS_PREFERENCE_END);
871 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
874 if (NULL != cpid_str)
876 ats_ch = GNUNET_ATS_connectivity_init (cfg);
877 ats_sh = GNUNET_ATS_connectivity_suggest (ats_ch, &cpid, 1000);
879 GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
889 * @param argc number of arguments from the command line
890 * @param argv command line arguments
891 * @return 0 ok, 1 on error
894 main (int argc, char *const *argv)
898 opt_resolve_addresses_numeric = GNUNET_NO;
899 opt_monitor = GNUNET_NO;
900 opt_list_all = GNUNET_NO;
901 opt_list_used = GNUNET_NO;
902 opt_set_pref = GNUNET_NO;
904 stat_receive_done = GNUNET_NO;
907 struct GNUNET_GETOPT_CommandLineOption options[] =
908 { GNUNET_GETOPT_option_flag ('u',
911 "get list of active addresses currently used"),
913 GNUNET_GETOPT_option_flag ('a',
916 "get list of all active addresses"),
919 GNUNET_GETOPT_option_string ('C',
922 gettext_noop ("connect to PEER"),
924 GNUNET_GETOPT_option_flag ('n',
927 "do not resolve IP addresses to hostnames"),
928 &opt_resolve_addresses_numeric),
930 GNUNET_GETOPT_option_flag ('m',
932 gettext_noop ("monitor mode"),
935 GNUNET_GETOPT_option_flag ('p',
938 "set preference for the given peer"),
941 GNUNET_GETOPT_option_flag ('q',
943 gettext_noop ("print all configured quotas"),
945 GNUNET_GETOPT_option_string ('i',
948 gettext_noop ("peer id"),
951 GNUNET_GETOPT_option_string ('t',
955 "preference type to set: latency | bandwidth"),
958 GNUNET_GETOPT_option_uint ('k',
961 gettext_noop ("preference value"),
964 GNUNET_GETOPT_option_flag (
967 gettext_noop ("verbose output (include ATS address properties)"),
969 GNUNET_GETOPT_OPTION_END };
971 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
974 res = GNUNET_PROGRAM_run (argc,
977 gettext_noop ("Print information about ATS state"),
981 GNUNET_free_non_null (opt_pid_str);
982 GNUNET_free_non_null (opt_type_str);
983 GNUNET_free_nz ((void *) argv);
985 if (GNUNET_OK == res)
992 /* end of gnunet-ats.c */