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
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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?
229 * Head of list of pending resolution requests.
231 static struct PendingResolutions *head;
234 * Tail of list of pending resolution requests.
236 static struct PendingResolutions *tail;
240 * Free address corresponding to a given peer.
243 * @param key peer identity
244 * @param value the `struct ATSAddress *` to be freed
245 * @return #GNUNET_YES (always)
248 free_addr_it (void *cls,
249 const struct GNUNET_PeerIdentity *key,
252 struct ATSAddress *a = value;
254 GNUNET_assert (GNUNET_OK ==
255 GNUNET_CONTAINER_multipeermap_remove (addresses,
258 GNUNET_HELLO_address_free (a->address);
265 * Task run on shutdown.
272 struct PendingResolutions * pr;
273 struct PendingResolutions * next;
274 unsigned int pending;
278 GNUNET_ATS_performance_list_addresses_cancel (alh);
284 GNUNET_ATS_performance_done (ph);
290 while (NULL != (pr = next))
293 GNUNET_CONTAINER_DLL_remove(head, tail, pr);
294 GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
295 GNUNET_free(pr->address);
299 GNUNET_CONTAINER_multipeermap_iterate (addresses,
302 GNUNET_CONTAINER_multipeermap_destroy (addresses);
307 _("%u address resolutions had a timeout\n"),
309 if (opt_list_used || opt_list_all)
311 _("ATS returned stat_results for %u addresses\n"),
316 GNUNET_ATS_connectivity_suggest_cancel (ats_sh);
321 GNUNET_ATS_connectivity_done (ats_ch);
329 * Function to call with a textual representation of an address. This
330 * function will be called several times with different possible
331 * textual representations, and a last time with @a address being NULL
332 * to signal the end of the iteration. Note that @a address NULL
333 * always is the last call, regardless of the value in @a res.
335 * @param cls closure, a `struct PendingResolutions *`
336 * @param address NULL on end of iteration,
337 * otherwise 0-terminated printable UTF-8 string,
338 * in particular an empty string if @a res is #GNUNET_NO
339 * @param res result of the address to string conversion:
340 * if #GNUNET_OK: conversion successful
341 * if #GNUNET_NO: address was invalid (or not supported)
342 * if #GNUNET_SYSERR: communication error (IPC error)
345 transport_addr_to_str_cb (void *cls,
349 struct PendingResolutions *pr = cls;
354 GNUNET_CONTAINER_DLL_remove (head,
357 GNUNET_free (pr->address);
361 if ((GNUNET_YES == stat_receive_done) && (0 == stat_pending))
363 /* All messages received and no resolutions pending*/
364 if (shutdown_task != NULL)
365 GNUNET_SCHEDULER_cancel (shutdown_task);
366 shutdown_task = GNUNET_SCHEDULER_add_now (end, NULL);
374 "Failed to convert address for peer `%s' plugin `%s' length %u to string (communication error)\n",
375 GNUNET_i2s (&pr->address->peer),
376 pr->address->transport_name,
377 (unsigned int) pr->address->address_length);
381 "Failed to convert address for peer `%s' plugin `%s' length %u to string (address invalid or not supported)\n",
382 GNUNET_i2s (&pr->address->peer),
383 pr->address->transport_name,
384 (unsigned int) pr->address->address_length);
387 /* continues below */
395 _("Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
396 GNUNET_i2s (&pr->address->peer),
397 pr->address->transport_name,
399 GNUNET_ATS_print_network_type (pr->properties.scope),
400 ntohl (pr->bandwidth_out.value__),
401 ntohl (pr->bandwidth_in.value__),
402 pr->active ? _("active ") : _("inactive "));
407 * Closure for #find_address_it().
409 struct AddressFindCtx
412 * Address we are looking for.
414 const struct GNUNET_HELLO_Address *src;
417 * Where to write the `struct ATSAddress` if we found one that matches.
419 struct ATSAddress *res;
424 * Find address corresponding to a given peer.
426 * @param cls the `struct AddressFindCtx *`
427 * @param key peer identity
428 * @param value the `struct ATSAddress *` for an existing address
429 * @return #GNUNET_NO if we found a match, #GNUNET_YES if not
432 find_address_it (void *cls,
433 const struct GNUNET_PeerIdentity *key,
436 struct AddressFindCtx *actx = cls;
437 struct ATSAddress *exist = value;
439 if (0 == GNUNET_HELLO_address_cmp (actx->src, exist->address))
450 * Signature of a function that is called with QoS information about an address.
452 * @param cls closure (NULL)
453 * @param address the address, NULL if ATS service was disconnected
454 * @param active #GNUNET_YES if this address is actively used
455 * to maintain a connection to a peer;
456 * #GNUNET_NO if the address is not actively used;
457 * #GNUNET_SYSERR if this address is no longer available for ATS
458 * @param bandwidth_out assigned outbound bandwidth for the connection
459 * @param bandwidth_in assigned inbound bandwidth for the connection
460 * @param prop performance data for the address (as far as known)
463 ats_perf_mon_cb (void *cls,
464 const struct GNUNET_HELLO_Address *address,
466 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
467 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
468 const struct GNUNET_ATS_Properties *prop)
470 struct PendingResolutions *pr;
471 struct PendingResolutions *cur;
472 struct PendingResolutions *next;
476 /* ATS service temporarily disconnected, remove current state */
478 for (cur = next; NULL != cur; cur = next)
481 GNUNET_CONTAINER_DLL_remove (head, tail, cur);
482 GNUNET_TRANSPORT_address_to_string_cancel (cur->tats_ctx);
483 GNUNET_HELLO_address_free (cur->address);
486 GNUNET_CONTAINER_multipeermap_iterate (addresses,
491 if (GNUNET_SYSERR == active)
494 struct AddressFindCtx actx;
498 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
502 if (NULL == actx.res)
507 GNUNET_break(GNUNET_OK ==
508 GNUNET_CONTAINER_multipeermap_remove (addresses,
512 _("Removed address of peer `%s' with plugin `%s'\n"),
513 GNUNET_i2s (&address->peer),
514 actx.res->address->transport_name);
515 GNUNET_HELLO_address_free (actx.res);
519 if (GNUNET_NO == opt_verbose)
521 struct AddressFindCtx actx;
522 struct ATSAddress *a;
526 GNUNET_CONTAINER_multipeermap_get_multiple (addresses,
530 if ((NULL != actx.res))
532 if ((bandwidth_in.value__ == actx.res->bandwidth_in.value__) &&
533 (bandwidth_out.value__ == actx.res->bandwidth_out.value__) &&
534 (active == actx.res->active))
536 return; /* Nothing to do here */
540 actx.res->bandwidth_in = bandwidth_in;
541 actx.res->bandwidth_out = bandwidth_out;
546 a = GNUNET_new (struct ATSAddress);
548 a->address = GNUNET_HELLO_address_copy(address);
549 a->bandwidth_in = bandwidth_in;
550 a->bandwidth_out = bandwidth_out;
552 GNUNET_CONTAINER_multipeermap_put (addresses,
555 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
559 pr = GNUNET_new (struct PendingResolutions);
560 pr->properties = *prop;
561 pr->address = GNUNET_HELLO_address_copy (address);
562 pr->bandwidth_in = bandwidth_in;
563 pr->bandwidth_out = bandwidth_out;
565 pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (cfg, address,
566 opt_resolve_addresses_numeric,
567 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
568 &transport_addr_to_str_cb,
570 GNUNET_CONTAINER_DLL_insert (head, tail, pr);
577 * Signature of a function that is called with QoS information about an address.
579 * @param cls closure (NULL)
580 * @param address the address, NULL if ATS service was disconnected
581 * @param active is this address actively used to maintain a connection
583 * @param bandwidth_out assigned outbound bandwidth for the connection
584 * @param bandwidth_in assigned inbound bandwidth for the connection
585 * @param prop performance data for the address (as far as known)
588 ats_perf_cb (void *cls,
589 const struct GNUNET_HELLO_Address *address,
591 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
592 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
593 const struct GNUNET_ATS_Properties *prop)
595 struct PendingResolutions *pr;
599 /* All messages received */
600 stat_receive_done = GNUNET_YES;
602 if (0 == stat_pending)
604 /* All messages received and no resolutions pending*/
605 if (shutdown_task != NULL)
606 GNUNET_SCHEDULER_cancel (shutdown_task);
607 shutdown_task = GNUNET_SCHEDULER_add_now (end, NULL);
612 pr = GNUNET_new (struct PendingResolutions);
613 pr->properties = *prop;
614 pr->address = GNUNET_HELLO_address_copy (address);
615 pr->bandwidth_in = bandwidth_in;
616 pr->bandwidth_out = bandwidth_out;
618 pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (cfg, address,
619 opt_resolve_addresses_numeric,
620 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
621 &transport_addr_to_str_cb, pr);
622 GNUNET_CONTAINER_DLL_insert (head, tail, pr);
629 * Print information about the quotas configured for the various
632 * @param cfg configuration to obtain quota information from
633 * @return total number of ATS network types known
636 print_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg)
638 char * entry_in = NULL;
639 char * entry_out = NULL;
640 char * quota_out_str;
642 unsigned long long int quota_out;
643 unsigned long long int quota_in;
646 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount); c++)
649 GNUNET_asprintf (&entry_out,
651 GNUNET_ATS_print_network_type (c));
652 GNUNET_asprintf (&entry_in,
654 GNUNET_ATS_print_network_type (c));
658 GNUNET_CONFIGURATION_get_value_string (cfg,
663 if (0 == strcmp (quota_out_str, UNLIMITED_STRING)
665 GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
667 quota_out = UINT32_MAX;
669 GNUNET_free(quota_out_str);
670 GNUNET_asprintf ("a_out_str, "%llu", quota_out);
675 "Outbound quota for network `%11s' not configured!\n",
676 GNUNET_ATS_print_network_type (c));
677 GNUNET_asprintf ("a_out_str, "-");
679 GNUNET_free (entry_out);
683 GNUNET_CONFIGURATION_get_value_string (cfg,
688 if (0 == strcmp (quota_in_str, UNLIMITED_STRING)
690 GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, "a_in)))
691 quota_in = UINT32_MAX;
692 GNUNET_free (quota_in_str);
693 GNUNET_asprintf ("a_in_str, "%llu", quota_in);
698 "Inbound quota for network `%11s' not configured!\n",
699 GNUNET_ATS_print_network_type (c));
700 GNUNET_asprintf ("a_in_str, "-");
702 GNUNET_free(entry_in);
705 _("Quota for network `%11s' (in/out): %10s / %10s\n"),
706 GNUNET_ATS_print_network_type (c),
709 GNUNET_free(quota_out_str);
710 GNUNET_free(quota_in_str);
712 return GNUNET_ATS_NetworkTypeCount;
717 * Main function that will be run by the scheduler.
720 * @param args remaining command-line arguments
721 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
722 * @param my_cfg configuration
728 const struct GNUNET_CONFIGURATION_Handle *my_cfg)
730 struct GNUNET_PeerIdentity pid;
731 struct GNUNET_PeerIdentity cpid;
735 cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
736 addresses = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
740 if (NULL != opt_pid_str)
743 GNUNET_CRYPTO_eddsa_public_key_from_string (opt_pid_str,
744 strlen (opt_pid_str),
748 _("Failed to parse peer identity `%s'\n"),
753 if (NULL != cpid_str)
756 GNUNET_CRYPTO_eddsa_public_key_from_string (cpid_str,
761 _("Failed to parse peer identity `%s'\n"),
768 c += opt_list_all + opt_list_used + opt_monitor + opt_set_pref;
773 _("Please select one operation: %s or %s or %s or %s or %s\n"),
782 opt_list_used = GNUNET_YES; /* set default */
783 if (opt_print_quotas)
785 ret = print_quotas (cfg);
790 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
795 _("Cannot connect to ATS service, exiting...\n"));
798 alh = GNUNET_ATS_performance_list_addresses (ph,
799 (NULL == opt_pid_str) ? NULL : &pid,
806 _("Cannot issue request to ATS service, exiting...\n"));
807 shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
810 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
817 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
821 _("Cannot connect to ATS service, exiting...\n"));
823 alh = GNUNET_ATS_performance_list_addresses (ph,
824 (NULL == opt_pid_str)
833 _("Cannot issue request to ATS service, exiting...\n"));
834 shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
837 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
844 ph = GNUNET_ATS_performance_init (cfg,
850 _("Cannot connect to ATS service, exiting...\n"));
851 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
858 if (NULL == opt_type_str)
862 _("No preference type given!\n"));
865 if (NULL == opt_pid_str)
869 _("No peer given!\n"));
873 for (c = 0; c < strlen (opt_type_str); c++)
875 if (isupper (opt_type_str[c]))
876 opt_type_str[c] = tolower (opt_type_str[c]);
879 if (0 == strcasecmp ("latency", opt_type_str))
880 type = GNUNET_ATS_PREFERENCE_LATENCY;
881 else if (0 == strcasecmp ("bandwidth", opt_type_str))
882 type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
887 _("Valid type required\n"));
892 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
896 _("Cannot connect to ATS service, exiting...\n"));
898 GNUNET_ATS_performance_change_preference (ph,
901 (double) opt_pref_value,
902 GNUNET_ATS_PREFERENCE_END);
904 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
909 if (NULL != cpid_str)
911 ats_ch = GNUNET_ATS_connectivity_init (cfg);
912 ats_sh = GNUNET_ATS_connectivity_suggest (ats_ch,
916 = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
928 * @param argc number of arguments from the command line
929 * @param argv command line arguments
930 * @return 0 ok, 1 on error
938 opt_resolve_addresses_numeric = GNUNET_NO;
939 opt_monitor = GNUNET_NO;
940 opt_list_all = GNUNET_NO;
941 opt_list_used = GNUNET_NO;
942 opt_set_pref = GNUNET_NO;
944 stat_receive_done = GNUNET_NO;
947 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
949 gettext_noop ("get list of active addresses currently used"), 0,
950 &GNUNET_GETOPT_set_one, &opt_list_used },
951 { 'a', "all", NULL, gettext_noop ("get list of all active addresses"), 0,
952 &GNUNET_GETOPT_set_one, &opt_list_all },
953 { 'C', "connect", "PEER",
954 gettext_noop ("connect to PEER"), 1,
955 &GNUNET_GETOPT_set_string, &cpid_str },
956 { 'n', "numeric", NULL,
957 gettext_noop ("do not resolve IP addresses to hostnames"), 0,
958 &GNUNET_GETOPT_set_one, &opt_resolve_addresses_numeric },
959 { 'm', "monitor", NULL, gettext_noop ("monitor mode"), 0,
960 &GNUNET_GETOPT_set_one, &opt_monitor },
961 { 'p', "preference", NULL, gettext_noop ("set preference for the given peer"),
962 0, &GNUNET_GETOPT_set_one, &opt_set_pref },
963 { 'q', "quotas", NULL, gettext_noop ("print all configured quotas"), 0,
964 &GNUNET_GETOPT_set_one, &opt_print_quotas },
965 { 'i', "id", "TYPE", gettext_noop ("peer id"), 1, &GNUNET_GETOPT_set_string,
967 { 't', "type", "TYPE",
968 gettext_noop ("preference type to set: latency | bandwidth"), 1,
969 &GNUNET_GETOPT_set_string, &opt_type_str },
970 { 'k', "value", "VALUE", gettext_noop ("preference value"), 1,
971 &GNUNET_GETOPT_set_uint, &opt_pref_value },
972 { 'V', "verbose", NULL,
973 gettext_noop ("verbose output (include ATS address properties)"), 0,
974 &GNUNET_GETOPT_set_one, &opt_verbose },
975 GNUNET_GETOPT_OPTION_END
978 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
981 res = GNUNET_PROGRAM_run (argc, argv,
983 gettext_noop ("Print information about ATS state"),
986 GNUNET_free_non_null(opt_pid_str);
987 GNUNET_free_non_null(opt_type_str);
988 GNUNET_free((void *) argv);
990 if (GNUNET_OK == res)
996 /* end of gnunet-ats.c */