2 This file is part of GNUnet.
3 (C) 2009--2013 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 ats-tool/gnunet-ats.c
23 * @brief ATS command line tool
24 * @author Matthias Wachs
27 #include "gnunet_util_lib.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_transport_service.h"
32 * String to respresent unlimited
34 #define UNLIMITED_STRING "unlimited"
40 static int opt_resolve_addresses_numeric;
43 * CLI Opt: Print verbose ATS information
45 static int opt_verbose;
48 * CLI Option: List only addresses currently used (active)
50 static int opt_list_used;
53 * CLI Option: List all addresses
55 static int opt_list_all;
58 * CLI Option: set preference
60 static int opt_set_pref;
63 * CLI Option: print quotas configured
65 static int opt_print_quotas;
68 * CLI Option: Monitor addresses used
70 static int opt_monitor;
73 * CLI Option: use specific peer
75 static char *opt_pid_str;
78 * CLI Option: preference type to set
80 static char *opt_type_str;
83 * CLI Option: preference value to set
85 static unsigned int opt_pref_value;
95 * Number of results returned from service
97 static int stat_results;
100 * State: all pending receive operations done?
102 static int stat_receive_done;
105 * State: number of pending operations
107 static int stat_pending;
110 * ATS performance handle used
112 static struct GNUNET_ATS_PerformanceHandle *ph;
115 * ATS address list handle used
117 static struct GNUNET_ATS_AddressListHandle *alh;
120 * Configuration handle
122 static struct GNUNET_CONFIGURATION_Handle *cfg;
127 static struct GNUNET_SCHEDULER_Task * shutdown_task;
130 * Hashmap to store addresses
132 static struct GNUNET_CONTAINER_MultiPeerMap *addresses;
136 * Structure used to remember all pending address resolutions.
137 * We keep address information in here while we talk to transport
138 * to map the address to a string.
140 struct PendingResolutions
145 struct PendingResolutions *next;
150 struct PendingResolutions *prev;
153 * Copy of the address we are resolving.
155 struct GNUNET_HELLO_Address *address;
158 * Handle to the transport request to convert the address
161 struct GNUNET_TRANSPORT_AddressToStringContext *tats_ctx;
164 * Array of performance data.
166 struct GNUNET_ATS_Information *ats;
169 * Length of the @e ats array.
174 * Amount of outbound bandwidth assigned by ATS.
176 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
179 * Amount of inbound bandwidth assigned by ATS.
181 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
184 * Is this an active address?
191 * Information we keep for an address. Used to avoid
192 * printing the same data multiple times.
197 * Address information.
199 struct GNUNET_HELLO_Address *address;
202 * Current outbound bandwidth.
204 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
207 * Current inbound bandwidth.
209 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
212 * Is this an active address?
220 * Head of list of pending resolution requests.
222 static struct PendingResolutions *head;
225 * Tail of list of pending resolution requests.
227 static struct PendingResolutions *tail;
231 * Free address corresponding to a given peer.
234 * @param key peer identity
235 * @param value the `struct ATSAddress *` to be freed
236 * @return #GNUNET_YES (always)
239 free_addr_it (void *cls,
240 const struct GNUNET_PeerIdentity *key,
243 struct ATSAddress *a = value;
244 GNUNET_assert (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (addresses, key, value));
245 GNUNET_HELLO_address_free (a->address);
252 * Task run on shutdown.
255 * @param tc scheduler context
259 const struct GNUNET_SCHEDULER_TaskContext *tc)
261 struct PendingResolutions * pr;
262 struct PendingResolutions * next;
263 unsigned int pending;
267 GNUNET_ATS_performance_list_addresses_cancel (alh);
273 GNUNET_ATS_performance_done (ph);
279 while (NULL != (pr = next))
282 GNUNET_CONTAINER_DLL_remove(head, tail, pr);
283 GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
284 GNUNET_free(pr->address);
288 GNUNET_CONTAINER_multipeermap_iterate (addresses,
291 GNUNET_CONTAINER_multipeermap_destroy (addresses);
296 _("%u address resolutions had a timeout\n"),
298 if (opt_list_used || opt_list_all)
300 _("ATS returned stat_results for %u addresses\n"),
307 * Function to call with a textual representation of an address. This
308 * function will be called several times with different possible
309 * textual representations, and a last time with @a address being NULL
310 * to signal the end of the iteration. Note that @a address NULL
311 * always is the last call, regardless of the value in @a res.
313 * @param cls closure, a `struct PendingResolutions *`
314 * @param address NULL on end of iteration,
315 * otherwise 0-terminated printable UTF-8 string,
316 * in particular an empty string if @a res is #GNUNET_NO
317 * @param res result of the address to string conversion:
318 * if #GNUNET_OK: conversion successful
319 * if #GNUNET_NO: address was invalid (or not supported)
320 * if #GNUNET_SYSERR: communication error (IPC error)
323 transport_addr_to_str_cb (void *cls,
327 struct PendingResolutions *pr = cls;
330 char *ats_prop_value;
339 GNUNET_CONTAINER_DLL_remove(head, tail, pr);
340 GNUNET_free(pr->address);
344 if ((GNUNET_YES == stat_receive_done) && (0 == stat_pending))
346 /* All messages received and no resolutions pending*/
347 if (shutdown_task != NULL)
348 GNUNET_SCHEDULER_cancel (shutdown_task);
349 shutdown_task = GNUNET_SCHEDULER_add_now (end, NULL);
357 "Failed to convert address for peer `%s' plugin `%s' length %u to string (communication error)\n",
358 GNUNET_i2s (&pr->address->peer),
359 pr->address->transport_name,
360 (unsigned int) pr->address->address_length);
364 "Failed to convert address for peer `%s' plugin `%s' length %u to string (address invalid or not supported)\n",
365 GNUNET_i2s (&pr->address->peer),
366 pr->address->transport_name,
367 (unsigned int) pr->address->address_length);
370 /* continues below */
377 ats_str = GNUNET_strdup (pr->active ? _("active ") : _("inactive "));
378 network = GNUNET_ATS_NET_UNSPECIFIED;
379 for (c = 0; c < pr->ats_count; c++)
383 ats_type = ntohl (pr->ats[c].type);
384 ats_value = ntohl (pr->ats[c].value);
386 if (ats_type > GNUNET_ATS_PropertyCount)
389 "Invalid ATS property type %u %u for address %s\n",
398 case GNUNET_ATS_NETWORK_TYPE:
399 if (ats_value > GNUNET_ATS_NetworkTypeCount)
405 GNUNET_asprintf (&ats_prop_value,
407 GNUNET_ATS_print_network_type (ats_value));
410 GNUNET_asprintf (&ats_prop_value, "%u", ats_value);
413 if ((opt_verbose) && (ats_type < GNUNET_ATS_PropertyCount))
415 GNUNET_asprintf (&ats_str,
418 GNUNET_ATS_print_property_type (ats_type),
420 GNUNET_free(ats_tmp);
422 GNUNET_free(ats_prop_value);
426 _("Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
427 GNUNET_i2s (&pr->address->peer),
428 pr->address->transport_name,
430 GNUNET_ATS_print_network_type (network),
431 ntohl (pr->bandwidth_out.value__),
432 ntohl (pr->bandwidth_in.value__),
434 GNUNET_free (ats_str);
439 * Closure for #find_address_it().
441 struct AddressFindCtx
444 * Address we are looking for.
446 const struct GNUNET_HELLO_Address *src;
449 * Where to write the `struct ATSAddress` if we found one that matches.
451 struct ATSAddress *res;
456 * Find address corresponding to a given peer.
458 * @param cls the `struct AddressFindCtx *`
459 * @param key peer identity
460 * @param value the `struct ATSAddress *` for an existing address
461 * @return #GNUNET_NO if we found a match, #GNUNET_YES if not
464 find_address_it (void *cls,
465 const struct GNUNET_PeerIdentity *key,
468 struct AddressFindCtx *actx = cls;
469 struct ATSAddress *exist = value;
471 if (0 == GNUNET_HELLO_address_cmp (actx->src, exist->address))
482 * Signature of a function that is called with QoS information about an address.
484 * @param cls closure (NULL)
485 * @param address the address, NULL if ATS service was disconnected
486 * @param active #GNUNET_YES if this address is actively used
487 * to maintain a connection to a peer;
488 * #GNUNET_NO if the address is not actively used;
489 * #GNUNET_SYSERR if this address is no longer available for ATS
490 * @param bandwidth_out assigned outbound bandwidth for the connection
491 * @param bandwidth_in assigned inbound bandwidth for the connection
492 * @param ats performance data for the address (as far as known)
493 * @param ats_count number of performance records in @a ats
496 ats_perf_mon_cb (void *cls,
497 const struct GNUNET_HELLO_Address *address,
499 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
500 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
501 const struct GNUNET_ATS_Information *ats,
504 struct PendingResolutions *pr;
505 struct PendingResolutions *cur;
506 struct PendingResolutions *next;
510 /* ATS service temporarily disconnected, remove current state */
512 for (cur = next; NULL != cur; cur = next)
515 GNUNET_CONTAINER_DLL_remove (head, tail, cur);
516 GNUNET_TRANSPORT_address_to_string_cancel (cur->tats_ctx);
517 GNUNET_HELLO_address_free (cur->address);
521 GNUNET_CONTAINER_multipeermap_iterate (addresses,
526 if (GNUNET_SYSERR == active)
529 struct AddressFindCtx actx;
533 GNUNET_CONTAINER_multipeermap_get_multiple (addresses, &address->peer,
534 &find_address_it, &actx);
535 if (NULL == actx.res)
541 GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (addresses, &address->peer, actx.res));
543 _("Removed address of peer `%s' with plugin `%s'\n"),
544 GNUNET_i2s (&address->peer),
545 actx.res->address->transport_name);
546 GNUNET_HELLO_address_free (actx.res);
550 if (GNUNET_NO == opt_verbose)
552 struct AddressFindCtx actx;
553 struct ATSAddress *a;
557 GNUNET_CONTAINER_multipeermap_get_multiple (addresses, &address->peer,
558 &find_address_it, &actx);
559 if ((NULL != actx.res))
561 if ((bandwidth_in.value__ == actx.res->bandwidth_in.value__) &&
562 (bandwidth_out.value__ == actx.res->bandwidth_out.value__) &&
563 (active == actx.res->active))
565 return; /* Nothing to do here */
569 actx.res->bandwidth_in = bandwidth_in;
570 actx.res->bandwidth_out = bandwidth_out;
575 a = GNUNET_new (struct ATSAddress);
577 a->address = GNUNET_HELLO_address_copy(address);
578 a->bandwidth_in = bandwidth_in;
579 a->bandwidth_out = bandwidth_out;
581 GNUNET_CONTAINER_multipeermap_put (addresses, &address->peer, a,
582 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
586 pr = GNUNET_malloc (sizeof (struct PendingResolutions) +
587 ats_count * sizeof (struct GNUNET_ATS_Information));
589 pr->ats_count = ats_count;
590 pr->ats = (struct GNUNET_ATS_Information *) &pr[1];
592 memcpy (pr->ats, ats, ats_count * sizeof(struct GNUNET_ATS_Information));
593 pr->address = GNUNET_HELLO_address_copy (address);
594 pr->bandwidth_in = bandwidth_in;
595 pr->bandwidth_out = bandwidth_out;
597 pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (cfg, address,
598 opt_resolve_addresses_numeric,
599 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
600 &transport_addr_to_str_cb,
602 GNUNET_CONTAINER_DLL_insert (head, tail, pr);
609 * Signature of a function that is called with QoS information about an address.
611 * @param cls closure (NULL)
612 * @param address the address, NULL if ATS service was disconnected
613 * @param active is this address actively used to maintain a connection
615 * @param bandwidth_out assigned outbound bandwidth for the connection
616 * @param bandwidth_in assigned inbound bandwidth for the connection
617 * @param ats performance data for the address (as far as known)
618 * @param ats_count number of performance records in @a ats
621 ats_perf_cb (void *cls,
622 const struct GNUNET_HELLO_Address *address,
624 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
625 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
626 const struct GNUNET_ATS_Information *ats,
629 struct PendingResolutions *pr;
633 /* All messages received */
634 stat_receive_done = GNUNET_YES;
636 if (0 == stat_pending)
638 /* All messages received and no resolutions pending*/
639 if (shutdown_task != NULL)
640 GNUNET_SCHEDULER_cancel (shutdown_task);
641 shutdown_task = GNUNET_SCHEDULER_add_now (end, NULL);
646 pr = GNUNET_malloc (sizeof (struct PendingResolutions) +
647 ats_count * sizeof (struct GNUNET_ATS_Information));
649 pr->ats_count = ats_count;
650 pr->ats = (struct GNUNET_ATS_Information *) &pr[1];
652 memcpy (pr->ats, ats, ats_count * sizeof(struct GNUNET_ATS_Information));
653 pr->address = GNUNET_HELLO_address_copy (address);
654 pr->bandwidth_in = bandwidth_in;
655 pr->bandwidth_out = bandwidth_out;
657 pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (cfg, address,
658 opt_resolve_addresses_numeric,
659 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
660 &transport_addr_to_str_cb, pr);
661 GNUNET_CONTAINER_DLL_insert (head, tail, pr);
668 * Print information about the quotas configured for the various
671 * @param cfg configuration to obtain quota information from
672 * @return total number of ATS network types known
675 print_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg)
677 char * entry_in = NULL;
678 char * entry_out = NULL;
679 char * quota_out_str;
681 unsigned long long int quota_out;
682 unsigned long long int quota_in;
685 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount); c++)
688 GNUNET_asprintf (&entry_out,
690 GNUNET_ATS_print_network_type (c));
691 GNUNET_asprintf (&entry_in,
693 GNUNET_ATS_print_network_type (c));
697 GNUNET_CONFIGURATION_get_value_string (cfg,
702 if (0 == strcmp (quota_out_str, UNLIMITED_STRING)
704 GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str,
706 quota_out = UINT32_MAX;
708 GNUNET_free(quota_out_str);
709 GNUNET_asprintf ("a_out_str, "%llu", quota_out);
714 "Outbound quota for network `%11s' not configured!\n",
715 GNUNET_ATS_print_network_type (c));
716 GNUNET_asprintf ("a_out_str, "-");
718 GNUNET_free(entry_out);
722 GNUNET_CONFIGURATION_get_value_string (cfg,
727 if (0 == strcmp (quota_in_str, UNLIMITED_STRING)
729 GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, "a_in)))
730 quota_in = UINT32_MAX;
731 GNUNET_free(quota_in_str);
732 GNUNET_asprintf ("a_in_str, "%llu", quota_in);
737 "Inbound quota for network `%11s' not configured!\n",
738 GNUNET_ATS_print_network_type (c));
739 GNUNET_asprintf ("a_in_str, "-");
741 GNUNET_free(entry_in);
744 _("Quota for network `%11s' (in/out): %10s / %10s\n"),
745 GNUNET_ATS_print_network_type (c),
748 GNUNET_free(quota_out_str);
749 GNUNET_free(quota_in_str);
751 return GNUNET_ATS_NetworkTypeCount;
756 * Function called with the result from the test if ATS is
757 * running. Runs the actual main logic.
759 * @param cls the `struct GNUNET_CONFIGURATION_Handle *`
760 * @param result result of the test, #GNUNET_YES if ATS is running
763 testservice_ats (void *cls,
766 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
767 struct GNUNET_PeerIdentity pid;
771 addresses = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_NO);
773 if (GNUNET_YES != result)
776 _("Service `%s' is not running\n"),
783 if (NULL != opt_pid_str)
786 != GNUNET_CRYPTO_eddsa_public_key_from_string (opt_pid_str,
787 strlen (opt_pid_str), &pid.public_key))
790 _("Failed to parse peer identity `%s'\n"),
796 c = opt_list_all + opt_list_used + opt_monitor + opt_set_pref;
800 _("Please select one operation : %s or %s or %s or %s or %s\n"),
809 opt_list_used = GNUNET_YES; /* set default */
810 if (opt_print_quotas)
812 ret = print_quotas (cfg);
817 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
822 _("Cannot connect to ATS service, exiting...\n"));
826 alh = GNUNET_ATS_performance_list_addresses (ph,
827 (NULL == opt_pid_str) ? NULL : &pid,
834 _("Cannot issue request to ATS service, exiting...\n"));
835 shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
838 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
842 else if (opt_list_used)
844 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
848 _("Cannot connect to ATS service, exiting...\n"));
850 alh = GNUNET_ATS_performance_list_addresses (ph,
851 (NULL == opt_pid_str)
860 _("Cannot issue request to ATS service, exiting...\n"));
861 shutdown_task = GNUNET_SCHEDULER_add_now (&end, NULL);
864 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
868 else if (opt_monitor)
870 ph = GNUNET_ATS_performance_init (cfg,
876 _("Cannot connect to ATS service, exiting...\n"));
877 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL,
882 else if (opt_set_pref)
884 if (NULL == opt_type_str)
888 _("No preference type given!\n"));
891 if (NULL == opt_pid_str)
895 _("No peer given!\n"));
899 for (c = 0; c < strlen (opt_type_str); c++)
901 if (isupper (opt_type_str[c]))
902 opt_type_str[c] = tolower (opt_type_str[c]);
905 if (0 == strcasecmp ("latency", opt_type_str))
906 type = GNUNET_ATS_PREFERENCE_LATENCY;
907 else if (0 == strcasecmp ("bandwidth", opt_type_str))
908 type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
913 _("Valid type required\n"));
918 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
922 _("Cannot connect to ATS service, exiting...\n"));
924 GNUNET_ATS_performance_change_preference (ph, &pid, type, (double) opt_pref_value,
925 GNUNET_ATS_PREFERENCE_END);
927 shutdown_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS,
936 * Main function that will be run by the scheduler.
939 * @param args remaining command-line arguments
940 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
941 * @param my_cfg configuration
947 const struct GNUNET_CONFIGURATION_Handle *my_cfg)
949 cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
950 GNUNET_CLIENT_service_test ("ats", cfg,
951 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5),
952 &testservice_ats, (void *) cfg);
959 * @param argc number of arguments from the command line
960 * @param argv command line arguments
961 * @return 0 ok, 1 on error
969 opt_resolve_addresses_numeric = GNUNET_NO;
970 opt_monitor = GNUNET_NO;
971 opt_list_all = GNUNET_NO;
972 opt_list_used = GNUNET_NO;
973 opt_set_pref = GNUNET_NO;
975 stat_receive_done = GNUNET_NO;
978 static const struct GNUNET_GETOPT_CommandLineOption options[] =
981 gettext_noop ("get list of active addresses currently used"), 0,
982 &GNUNET_GETOPT_set_one, &opt_list_used },
983 { 'a', "all", NULL, gettext_noop ("get list of all active addresses"), 0,
984 &GNUNET_GETOPT_set_one, &opt_list_all },
985 { 'n', "numeric", NULL,
986 gettext_noop ("do not resolve IP addresses to hostnames"), 0,
987 &GNUNET_GETOPT_set_one, &opt_resolve_addresses_numeric },
988 { 'm', "monitor", NULL, gettext_noop ("monitor mode"), 0,
989 &GNUNET_GETOPT_set_one, &opt_monitor },
990 { 'p', "preference", NULL, gettext_noop ("set preference for the given peer"),
991 0, &GNUNET_GETOPT_set_one, &opt_set_pref },
992 { 'q', "quotas", NULL, gettext_noop ("print all configured quotas"), 0,
993 &GNUNET_GETOPT_set_one, &opt_print_quotas },
994 { 'i', "id", "TYPE", gettext_noop ("peer id"), 1, &GNUNET_GETOPT_set_string,
996 { 't', "type", "TYPE",
997 gettext_noop ("preference type to set: latency | bandwidth"), 1,
998 &GNUNET_GETOPT_set_string, &opt_type_str },
999 { 'k', "value", "VALUE", gettext_noop ("preference value"), 1,
1000 &GNUNET_GETOPT_set_uint, &opt_pref_value },
1001 { 'V', "verbose", NULL,
1002 gettext_noop ("verbose output (include ATS address properties)"), 0,
1003 &GNUNET_GETOPT_set_one, &opt_verbose }, GNUNET_GETOPT_OPTION_END };
1005 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
1008 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
1009 gettext_noop ("Print information about ATS state"),
1012 GNUNET_free_non_null(opt_pid_str);
1013 GNUNET_free_non_null(opt_type_str);
1014 GNUNET_free((void *) argv);
1016 if (GNUNET_OK == res)
1023 /* end of gnunet-ats.c */