2 This file is part of GNUnet.
3 (C) 2001, 2002, 2004, 2005, 2006, 2007, 2009 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;
84 static struct GNUNET_ATS_PerformanceHandle *ph;
86 struct GNUNET_ATS_AddressListHandle *alh;
88 static struct GNUNET_CONFIGURATION_Handle *cfg;
90 GNUNET_SCHEDULER_TaskIdentifier end_task;
92 struct PendingResolutions
94 struct PendingResolutions *next;
95 struct PendingResolutions *prev;
97 struct GNUNET_HELLO_Address *address;
98 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
99 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
101 struct GNUNET_ATS_Information *ats;
104 struct GNUNET_TRANSPORT_AddressToStringContext * tats_ctx;
107 struct PendingResolutions *head;
108 struct PendingResolutions *tail;
111 const struct GNUNET_SCHEDULER_TaskContext *tc)
113 struct PendingResolutions * pr;
114 struct PendingResolutions * next;
115 unsigned int pending;
119 GNUNET_ATS_performance_list_addresses_cancel (alh);
125 GNUNET_ATS_performance_done (ph);
131 while (NULL != (pr = next))
134 GNUNET_CONTAINER_DLL_remove (head, tail, pr);
135 GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
136 GNUNET_free (pr->address);
141 fprintf (stderr, _("%u address resolutions had a timeout\n"), pending);
143 fprintf (stderr, _("ATS returned results for %u addresses\n"), results);
148 void transport_addr_to_str_cb (void *cls, const char *address)
150 struct PendingResolutions * pr = cls;
153 char *ats_prop_arr[GNUNET_ATS_PropertyCount] = GNUNET_ATS_PropertyStrings;
154 char *ats_prop_value;
161 ats_str = GNUNET_strdup("");
164 for (c = 0; c < pr->ats_count; c++)
168 ats_type = ntohl(pr->ats[c].type);
169 ats_value = ntohl(pr->ats[c].value);
171 if (ats_type > GNUNET_ATS_PropertyCount)
178 case GNUNET_ATS_NETWORK_TYPE:
179 if (ats_value > GNUNET_ATS_NetworkTypeCount)
185 GNUNET_asprintf (&ats_prop_value, "%s", GNUNET_ATS_print_network_type(ats_value));
188 GNUNET_asprintf (&ats_prop_value, "%u", ats_value);
191 if ((verbose) && (ats_type < GNUNET_ATS_PropertyCount))
193 GNUNET_asprintf (&ats_str, "%s%s=%s, ", ats_tmp, ats_prop_arr[ats_type] , ats_prop_value);
194 GNUNET_free (ats_tmp);
196 GNUNET_free (ats_prop_value);
199 fprintf (stderr, _("Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
200 GNUNET_i2s (&pr->address->peer), pr->address->transport_name, address,
201 GNUNET_ATS_print_network_type(network),
202 ntohl (pr->bandwidth_out.value__), ntohl (pr->bandwidth_in.value__),ats_str);
203 GNUNET_free (ats_str);
208 GNUNET_CONTAINER_DLL_remove (head, tail, pr);
209 GNUNET_free (pr->address);
213 if ((GNUNET_YES == receive_done) && (0 == pending))
215 /* All messages received and no resolutions pending*/
216 if (end_task != GNUNET_SCHEDULER_NO_TASK)
217 GNUNET_SCHEDULER_cancel (end_task);
218 end_task = GNUNET_SCHEDULER_add_now (end, NULL);
223 void ats_perf_cb (void *cls,
225 GNUNET_HELLO_Address *
228 GNUNET_BANDWIDTH_Value32NBO
231 GNUNET_BANDWIDTH_Value32NBO
234 GNUNET_ATS_Information *
235 ats, uint32_t ats_count)
237 struct PendingResolutions * pr;
242 pr = GNUNET_malloc (sizeof (struct PendingResolutions) +
243 ats_count * sizeof (struct GNUNET_ATS_Information));
245 pr->ats_count = ats_count;
246 pr->ats = (struct GNUNET_ATS_Information *) &pr[1];
248 memcpy (pr->ats, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
249 pr->address = GNUNET_HELLO_address_copy (address);
250 pr->bandwidth_in = bandwidth_in;
251 pr->bandwidth_out = bandwidth_out;
252 pr->tats_ctx = GNUNET_TRANSPORT_address_to_string(cfg, address,
253 resolve_addresses_numeric, GNUNET_TIME_UNIT_FOREVER_REL, transport_addr_to_str_cb, pr);
254 GNUNET_CONTAINER_DLL_insert (head, tail, pr);
260 /* All messages received */
261 receive_done = GNUNET_YES;
265 /* All messages received and no resolutions pending*/
266 if (end_task != GNUNET_SCHEDULER_NO_TASK)
267 GNUNET_SCHEDULER_cancel (end_task);
268 end_task = GNUNET_SCHEDULER_add_now (end, NULL);
274 print_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg)
276 char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
277 char * entry_in = NULL;
278 char * entry_out = NULL;
279 char * quota_out_str;
281 unsigned long long int quota_out;
282 unsigned long long int quota_in;
285 for (c = 0; (c < GNUNET_ATS_NetworkTypeCount); c++)
288 GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
289 GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
292 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, "a_out_str))
294 if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
295 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, "a_out)))
296 quota_out = UINT32_MAX;
298 GNUNET_free (quota_out_str);
299 GNUNET_asprintf ("a_out_str, "%llu", quota_out);
303 fprintf (stderr, "Outbound quota for network `%11s' not configured!\n",
305 GNUNET_asprintf ("a_out_str, "-");
307 GNUNET_free (entry_out);
310 if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, "a_in_str))
312 if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
313 (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, "a_in)))
314 quota_in = UINT32_MAX;
315 GNUNET_free (quota_in_str);
316 GNUNET_asprintf ("a_in_str, "%llu", quota_in);
320 fprintf (stderr, "Inbound quota for network `%11s' not configured!\n",
322 GNUNET_asprintf ("a_in_str, "-");
324 GNUNET_free (entry_in);
326 fprintf (stderr, _("Quota for network `%11s' (in/out): %10s / %10s\n"), network_str[c], quota_in_str, quota_out_str );
327 GNUNET_free (quota_out_str);
328 GNUNET_free (quota_in_str);
330 return GNUNET_ATS_NetworkTypeCount;
335 void testservice_ats (void *cls,
336 const struct GNUNET_SCHEDULER_TaskContext *tc)
338 struct GNUNET_PeerIdentity pid;
339 struct GNUNET_CONFIGURATION_Handle *cfg = cls;
343 if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
345 FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
353 if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (pid_str, &pid.hashPubKey))
355 FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), pid_str);
360 c = op_list_all + op_list_used + op_monitor + op_set_pref;
363 FPRINTF (stderr, _("Please select one operation : %s or %s or %s or %s or %s\n"),
364 "--used", "--all", "--monitor", "--preference", "--quotas");
368 op_list_used = GNUNET_YES; /* set default */
371 ret = print_quotas (cfg);
376 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
379 fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
383 alh = GNUNET_ATS_performance_list_addresses (ph,
384 (NULL == pid_str) ? NULL : &pid,
385 GNUNET_YES, ats_perf_cb, NULL);
388 fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n"));
389 end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
392 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
394 else if (op_list_used)
396 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
398 fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
400 alh = GNUNET_ATS_performance_list_addresses (ph,
401 (NULL == pid_str) ? NULL : &pid,
402 GNUNET_NO, ats_perf_cb, NULL);
405 fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n"));
406 end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
409 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
413 ph = GNUNET_ATS_performance_init (cfg, ats_perf_cb, NULL);
415 fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
416 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
419 else if (op_set_pref)
421 for (c = 0; c<strlen(type_str); c++)
423 if (isupper (type_str[c]))
424 type_str[c] = tolower (type_str[c]);
427 if (0 == strcasecmp("latency", type_str))
428 type = GNUNET_ATS_PREFERENCE_LATENCY;
429 else if (0 == strcasecmp("bandwidth", type_str))
430 type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
433 FPRINTF (stderr, "%s", _("Type required\n"));
438 ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
440 fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
442 GNUNET_ATS_change_preference (ph, &pid, type, (double) value, GNUNET_ATS_PREFERENCE_END);
444 end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
450 * Main function that will be run by the scheduler.
453 * @param args remaining command-line arguments
454 * @param cfgfile name of the configuration file used (for saving, can be NULL!)
455 * @param my_cfg configuration
458 run (void *cls, char *const *args, const char *cfgfile,
459 const struct GNUNET_CONFIGURATION_Handle *my_cfg)
461 cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
462 GNUNET_CLIENT_service_test ("ats", cfg,
472 * @param argc number of arguments from the command line
473 * @param argv command line arguments
474 * @return 0 ok, 1 on error
477 main (int argc, char *const *argv)
480 resolve_addresses_numeric = GNUNET_NO;
481 op_monitor = GNUNET_NO;
482 op_list_all = GNUNET_NO;
483 op_list_used = GNUNET_NO;
484 op_set_pref = GNUNET_NO;
486 receive_done = GNUNET_NO;
488 static const struct GNUNET_GETOPT_CommandLineOption options[] = {
490 gettext_noop ("get list of active addresses currently used"),
491 0, &GNUNET_GETOPT_set_one, &op_list_used},
493 gettext_noop ("get list of all active addresses"),
494 0, &GNUNET_GETOPT_set_one, &op_list_all},
495 {'n', "numeric", NULL,
496 gettext_noop ("do not resolve IP addresses to hostnames"),
497 0, &GNUNET_GETOPT_set_one, &resolve_addresses_numeric},
498 {'m', "monitor", NULL,
499 gettext_noop ("monitor mode"),
500 0, &GNUNET_GETOPT_set_one, &op_monitor},
501 {'p', "preference", NULL,
502 gettext_noop ("set preference for the given peer"),
503 0, &GNUNET_GETOPT_set_one, &op_set_pref},
504 {'q', "quotas", NULL,
505 gettext_noop ("print all configured quotas"),
506 0, &GNUNET_GETOPT_set_one, &op_print_quotas},
508 gettext_noop ("peer id"),
509 1, &GNUNET_GETOPT_set_string, &pid_str},
510 {'t', "type", "TYPE",
511 gettext_noop ("preference type to set: latency | bandwidth"),
512 1, &GNUNET_GETOPT_set_string, &type_str},
513 {'k', "value", "VALUE",
514 gettext_noop ("preference value"),
515 1, &GNUNET_GETOPT_set_uint, &value},
516 {'V', "verbose", NULL,
517 gettext_noop ("verbose output (include ATS address properties)"),
518 0, &GNUNET_GETOPT_set_one, &verbose},
519 GNUNET_GETOPT_OPTION_END
522 if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
525 res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
526 gettext_noop ("Print information about ATS state"), options, &run,
528 GNUNET_free_non_null (pid_str);
529 GNUNET_free_non_null (type_str);
530 GNUNET_free ((void *) argv);
532 if (GNUNET_OK == res)
539 /* end of gnunet-ats.c */