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"
31 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
33 #define BIG_M_STRING "unlimited"
40 static int resolve_addresses_numeric;
41 static int receive_done;
44 * For which peer should we change preference values?
48 static char *type_str;
49 static unsigned int value;
53 * Print verbose ATS information
58 * List only addresses currently used (active)
60 static int op_list_used;
65 static int op_list_all;
70 static int op_set_pref;
73 * Print quotas configured
75 static int op_print_quotas;
78 * Monitor addresses used
80 static int op_monitor;
82 static struct GNUNET_ATS_PerformanceHandle *ph;
84 static struct GNUNET_ATS_AddressListHandle *alh;
86 static struct GNUNET_CONFIGURATION_Handle *cfg;
88 static GNUNET_SCHEDULER_TaskIdentifier end_task;
90 static struct GNUNET_CONTAINER_MultiPeerMap *addresses;
93 struct PendingResolutions
95 struct PendingResolutions *next;
96 struct PendingResolutions *prev;
98 struct GNUNET_HELLO_Address *address;
99 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
100 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
102 struct GNUNET_ATS_Information *ats;
105 struct GNUNET_TRANSPORT_AddressToStringContext * tats_ctx;
110 struct GNUNET_HELLO_Address *address;
111 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
112 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
117 static struct PendingResolutions *head;
119 static struct PendingResolutions *tail;
122 free_addr_it (void *cls,
123 const struct GNUNET_PeerIdentity *key,
126 struct ATSAddress *a = value;
127 GNUNET_CONTAINER_multipeermap_remove (addresses, key, a);
128 GNUNET_HELLO_address_free (a->address);
134 end(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 struct PendingResolutions * pr;
137 struct PendingResolutions * next;
138 unsigned int pending;
142 GNUNET_ATS_performance_list_addresses_cancel (alh);
148 GNUNET_ATS_performance_done (ph);
154 while (NULL != (pr = next))
157 GNUNET_CONTAINER_DLL_remove(head, tail, pr);
158 GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
159 GNUNET_free(pr->address);
164 GNUNET_CONTAINER_multipeermap_iterate(addresses, &free_addr_it, NULL);
165 GNUNET_CONTAINER_multipeermap_destroy(addresses);
168 fprintf (stderr, _("%u address resolutions had a timeout\n"), pending);
169 if (op_list_used || op_list_all)
170 fprintf (stderr, _("ATS returned results for %u addresses\n"), results);
175 transport_addr_to_str_cb(void *cls, const char *address)
177 struct PendingResolutions * pr = cls;
180 char *ats_prop_arr[GNUNET_ATS_PropertyCount] = GNUNET_ATS_PropertyStrings;
181 char *ats_prop_value;
189 ats_str = GNUNET_strdup("");
190 network = GNUNET_ATS_NET_UNSPECIFIED;
191 for (c = 0; c < pr->ats_count; c++)
195 ats_type = ntohl (pr->ats[c].type);
196 ats_value = ntohl (pr->ats[c].value);
198 if (ats_type > GNUNET_ATS_PropertyCount)
200 fprintf (stderr, "Invalid ATS property type %u %u for address %s\n", ats_type, pr->ats[c].type,
207 case GNUNET_ATS_NETWORK_TYPE:
208 if (ats_value > GNUNET_ATS_NetworkTypeCount)
214 GNUNET_asprintf (&ats_prop_value, "%s",
215 GNUNET_ATS_print_network_type (ats_value));
218 GNUNET_asprintf (&ats_prop_value, "%u", ats_value);
221 if ((verbose) && (ats_type < GNUNET_ATS_PropertyCount))
223 GNUNET_asprintf (&ats_str, "%s%s=%s, ", ats_tmp, ats_prop_arr[ats_type],
225 GNUNET_free(ats_tmp);
227 GNUNET_free(ats_prop_value);
231 _("Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
232 GNUNET_i2s (&pr->address->peer), pr->address->transport_name, address,
233 GNUNET_ATS_print_network_type (network),
234 ntohl (pr->bandwidth_out.value__), ntohl (pr->bandwidth_in.value__),
236 GNUNET_free(ats_str);
241 GNUNET_CONTAINER_DLL_remove(head, tail, pr);
242 GNUNET_free(pr->address);
246 if ((GNUNET_YES == receive_done) && (0 == pending))
248 /* All messages received and no resolutions pending*/
249 if (end_task != GNUNET_SCHEDULER_NO_TASK )
250 GNUNET_SCHEDULER_cancel (end_task);
251 end_task = GNUNET_SCHEDULER_add_now (end, NULL );
257 struct AddressFindCtx
259 const struct GNUNET_HELLO_Address *src;
260 struct ATSAddress *res;
264 find_address_it (void *cls,
265 const struct GNUNET_PeerIdentity *key,
268 struct AddressFindCtx *actx = cls;
269 struct ATSAddress *exist = value;
271 if (0 == GNUNET_HELLO_address_cmp (actx->src, exist->address))
281 ats_perf_cb(void *cls, const struct GNUNET_HELLO_Address *address, int active,
282 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
283 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
284 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
286 struct PendingResolutions * pr;
288 if ((GNUNET_YES == op_monitor) && (GNUNET_NO == verbose))
290 struct AddressFindCtx actx;
295 GNUNET_CONTAINER_multipeermap_iterate (addresses, find_address_it, &actx);
296 if ((actx.res != NULL))
298 if ((bandwidth_in.value__ == actx.res->bandwidth_in.value__) &&
299 (bandwidth_out.value__ == actx.res->bandwidth_out.value__) )
301 return; /* Nothing to do here */
305 actx.res->bandwidth_in = bandwidth_in;
306 actx.res->bandwidth_out = bandwidth_out;
310 struct ATSAddress *a = GNUNET_new (struct ATSAddress);
311 a->address = GNUNET_HELLO_address_copy(address);
312 a->bandwidth_in = bandwidth_in;
313 a->bandwidth_out = bandwidth_out;
314 GNUNET_CONTAINER_multipeermap_put (addresses, &address->peer, a,
315 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
320 pr = GNUNET_malloc (sizeof (struct PendingResolutions) +
321 ats_count * sizeof (struct GNUNET_ATS_Information));
323 pr->ats_count = ats_count;
324 pr->ats = (struct GNUNET_ATS_Information *) &pr[1];
326 memcpy (pr->ats, ats, ats_count * sizeof(struct GNUNET_ATS_Information));
327 pr->address = GNUNET_HELLO_address_copy (address);
328 pr->bandwidth_in = bandwidth_in;
329 pr->bandwidth_out = bandwidth_out;
330 pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (cfg, address,
331 resolve_addresses_numeric,
332 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
333 transport_addr_to_str_cb, pr);
334 GNUNET_CONTAINER_DLL_insert(head, tail, pr);
340 /* All messages received */
341 receive_done = GNUNET_YES;
345 /* All messages received and no resolutions pending*/
346 if (end_task != GNUNET_SCHEDULER_NO_TASK )
347 GNUNET_SCHEDULER_cancel (end_task);
348 end_task = GNUNET_SCHEDULER_add_now (end, NULL );
354 print_quotas(const struct GNUNET_CONFIGURATION_Handle *cfg)
356 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
357 char * entry_in = NULL;
358 char * entry_out = NULL;
359 char * quota_out_str;
361 unsigned long long int quota_out;
362 unsigned long long int quota_in;
365 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount); c++)
368 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
369 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
373 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
376 if (0 == strcmp (quota_out_str, BIG_M_STRING)
378 == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, "a_out)))
379 quota_out = UINT32_MAX;
381 GNUNET_free(quota_out_str);
382 GNUNET_asprintf ("a_out_str, "%llu", quota_out);
386 fprintf (stderr, "Outbound quota for network `%11s' not configured!\n",
388 GNUNET_asprintf ("a_out_str, "-");
390 GNUNET_free(entry_out);
394 == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
397 if (0 == strcmp (quota_in_str, BIG_M_STRING)
399 == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, "a_in)))
400 quota_in = UINT32_MAX;
401 GNUNET_free(quota_in_str);
402 GNUNET_asprintf ("a_in_str, "%llu", quota_in);
406 fprintf (stderr, "Inbound quota for network `%11s' not configured!\n",
408 GNUNET_asprintf ("a_in_str, "-");
410 GNUNET_free(entry_in);
412 fprintf (stderr, _("Quota for network `%11s' (in/out): %10s / %10s\n"),
413 network_str[c], quota_in_str, quota_out_str);
414 GNUNET_free(quota_out_str);
415 GNUNET_free(quota_in_str);
417 return GNUNET_ATS_NetworkTypeCount;
421 testservice_ats(void *cls, int result)
423 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
424 struct GNUNET_PeerIdentity pid;
428 addresses = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_YES);
430 if (GNUNET_YES != result)
432 FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
441 != GNUNET_CRYPTO_eddsa_public_key_from_string (pid_str,
442 strlen (pid_str), &pid.public_key))
444 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), pid_str);
449 c = op_list_all + op_list_used + op_monitor + op_set_pref;
453 _("Please select one operation : %s or %s or %s or %s or %s\n"),
454 "--used", "--all", "--monitor", "--preference", "--quotas");
458 op_list_used = GNUNET_YES; /* set default */
461 ret = print_quotas (cfg);
466 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL );
469 fprintf (stderr, _("Cannot connect to ATS service, exiting...\n") );
473 alh = GNUNET_ATS_performance_list_addresses (ph,
474 (NULL == pid_str) ? NULL : &pid, GNUNET_YES, ats_perf_cb, NULL );
477 fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n") );
478 end_task = GNUNET_SCHEDULER_add_now (&end, NULL );
481 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end,
484 else if (op_list_used)
486 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL );
488 fprintf (stderr, _("Cannot connect to ATS service, exiting...\n") );
490 alh = GNUNET_ATS_performance_list_addresses (ph,
491 (NULL == pid_str) ? NULL : &pid, GNUNET_NO, ats_perf_cb, NULL );
494 fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n") );
495 end_task = GNUNET_SCHEDULER_add_now (&end, NULL );
498 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end,
503 ph = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL );
505 fprintf (stderr, _("Cannot connect to ATS service, exiting...\n") );
506 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end,
510 else if (op_set_pref)
512 if (NULL == type_str)
514 fprintf (stderr, _("No preference type given!\n") );
519 fprintf (stderr, _("No peer given!\n") );
523 for (c = 0; c < strlen (type_str); c++)
525 if (isupper (type_str[c]))
526 type_str[c] = tolower (type_str[c]);
529 if (0 == strcasecmp ("latency", type_str))
530 type = GNUNET_ATS_PREFERENCE_LATENCY;
531 else if (0 == strcasecmp ("bandwidth", type_str))
532 type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
535 FPRINTF (stderr, "%s", _("Valid type required\n") );
540 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL );
542 fprintf (stderr, _("Cannot connect to ATS service, exiting...\n") );
544 GNUNET_ATS_performance_change_preference (ph, &pid, type, (double) value,
545 GNUNET_ATS_PREFERENCE_END);
547 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end,
554 * Main function that will be run by the scheduler.
557 * @param args remaining command-line arguments
558 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
559 * @param my_cfg configuration
562 run(void *cls, char * const *args, const char *cfgfile,
563 const struct GNUNET_CONFIGURATION_Handle *my_cfg)
565 cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
566 GNUNET_CLIENT_service_test ("ats", cfg, TIMEOUT, &testservice_ats,
573 * @param argc number of arguments from the command line
574 * @param argv command line arguments
575 * @return 0 ok, 1 on error
578 main(int argc, char * const *argv)
581 resolve_addresses_numeric = GNUNET_NO;
582 op_monitor = GNUNET_NO;
583 op_list_all = GNUNET_NO;
584 op_list_used = GNUNET_NO;
585 op_set_pref = GNUNET_NO;
587 receive_done = GNUNET_NO;
590 static const struct GNUNET_GETOPT_CommandLineOption options[] =
593 gettext_noop ("get list of active addresses currently used"), 0,
594 &GNUNET_GETOPT_set_one, &op_list_used },
595 { 'a', "all", NULL, gettext_noop ("get list of all active addresses"), 0,
596 &GNUNET_GETOPT_set_one, &op_list_all },
597 { 'n', "numeric", NULL,
598 gettext_noop ("do not resolve IP addresses to hostnames"), 0,
599 &GNUNET_GETOPT_set_one, &resolve_addresses_numeric },
600 { 'm', "monitor", NULL, gettext_noop ("monitor mode"), 0,
601 &GNUNET_GETOPT_set_one, &op_monitor },
602 { 'p', "preference", NULL, gettext_noop ("set preference for the given peer"),
603 0, &GNUNET_GETOPT_set_one, &op_set_pref },
604 { 'q', "quotas", NULL, gettext_noop ("print all configured quotas"), 0,
605 &GNUNET_GETOPT_set_one, &op_print_quotas },
606 { 'i', "id", "TYPE", gettext_noop ("peer id"), 1, &GNUNET_GETOPT_set_string,
608 { 't', "type", "TYPE",
609 gettext_noop ("preference type to set: latency | bandwidth"), 1,
610 &GNUNET_GETOPT_set_string, &type_str },
611 { 'k', "value", "VALUE", gettext_noop ("preference value"), 1,
612 &GNUNET_GETOPT_set_uint, &value },
613 { 'V', "verbose", NULL,
614 gettext_noop ("verbose output (include ATS address properties)"), 0,
615 &GNUNET_GETOPT_set_one, &verbose }, GNUNET_GETOPT_OPTION_END };
617 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
620 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
621 gettext_noop ("Print information about ATS state"), options, &run, NULL );
622 GNUNET_free_non_null(pid_str);
623 GNUNET_free_non_null(type_str);
624 GNUNET_free((void * ) argv);
626 if (GNUNET_OK == res)
633 /* end of gnunet-ats.c */