- fix state selection
[oweals/gnunet.git] / src / ats-tool / gnunet-ats.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009--2013 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19 */
20
21 /**
22  * @file ats-tool/gnunet-ats.c
23  * @brief ATS command line tool
24  * @author Matthias Wachs
25  */
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_ats_service.h"
29 #include "gnunet_transport_service.h"
30
31 #define TIMEOUT GNUNET_TIME_relative_multiply(GNUNET_TIME_UNIT_SECONDS, 5)
32
33 #define BIG_M_STRING "unlimited"
34
35 /**
36  * Final status code.
37  */
38 static int ret;
39 static int results;
40 static int resolve_addresses_numeric;
41 static int receive_done;
42
43 /**
44  * For which peer should we change preference values?
45  */
46 static char *pid_str;
47
48 static char *type_str;
49 static unsigned int value;
50 static int pending;
51
52 /**
53  * Print verbose ATS information
54  */
55 static int verbose;
56
57 /**
58  * List only addresses currently used (active)
59  */
60 static int op_list_used;
61
62 /**
63  * List all addresses
64  */
65 static int op_list_all;
66
67 /**
68  * List all addresses
69  */
70 static int op_set_pref;
71
72 /**
73  * Print quotas configured
74  */
75 static int op_print_quotas;
76
77 /**
78  * Monitor addresses used
79  */
80 static int op_monitor;
81
82
83 static struct GNUNET_ATS_PerformanceHandle *ph;
84
85 static struct GNUNET_ATS_AddressListHandle *alh;
86
87 static struct GNUNET_CONFIGURATION_Handle *cfg;
88
89 static GNUNET_SCHEDULER_TaskIdentifier end_task;
90
91
92 struct PendingResolutions
93 {
94   struct PendingResolutions *next;
95   struct PendingResolutions *prev;
96
97   struct GNUNET_HELLO_Address *address;
98   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
99   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
100
101   struct GNUNET_ATS_Information *ats;
102   uint32_t ats_count;
103
104   struct GNUNET_TRANSPORT_AddressToStringContext * tats_ctx;
105 };
106
107
108 static struct PendingResolutions *head;
109
110 static struct PendingResolutions *tail;
111
112
113 static void
114 end (void *cls,
115      const struct GNUNET_SCHEDULER_TaskContext *tc)
116 {
117   struct PendingResolutions * pr;
118   struct PendingResolutions * next;
119   unsigned int pending;
120
121   if (NULL != alh)
122   {
123       GNUNET_ATS_performance_list_addresses_cancel (alh);
124       alh = NULL;
125   }
126
127   if (NULL != ph)
128   {
129     GNUNET_ATS_performance_done (ph);
130     ph = NULL;
131   }
132
133   pending = 0;
134   next = head;
135   while (NULL != (pr = next))
136   {
137       next = pr->next;
138       GNUNET_CONTAINER_DLL_remove (head, tail, pr);
139       GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
140       GNUNET_free (pr->address);
141       GNUNET_free (pr);
142       pending ++;
143   }
144   if (0 < pending)
145     fprintf (stderr, _("%u address resolutions had a timeout\n"), pending);
146   if (op_list_used || op_list_all)
147         fprintf (stderr, _("ATS returned results for %u addresses\n"), results);
148   ret = 0;
149 }
150
151
152 static void
153 transport_addr_to_str_cb (void *cls, const char *address)
154 {
155   struct PendingResolutions * pr = cls;
156   char *ats_str;
157   char *ats_tmp;
158   char *ats_prop_arr[GNUNET_ATS_PropertyCount] = GNUNET_ATS_PropertyStrings;
159   char *ats_prop_value;
160   unsigned int c;
161   uint32_t ats_type;
162   uint32_t ats_value;
163   uint32_t network;
164
165   if (NULL != address)
166   {
167     ats_str = GNUNET_strdup("");
168     network = GNUNET_ATS_NET_UNSPECIFIED;
169     for (c = 0; c < pr->ats_count; c++)
170     {
171         ats_tmp = ats_str;
172
173         ats_type = ntohl(pr->ats[c].type);
174         ats_value = ntohl(pr->ats[c].value);
175
176         if (ats_type > GNUNET_ATS_PropertyCount)
177         {
178             GNUNET_break (0);
179             continue;
180         }
181
182         switch (ats_type) {
183           case GNUNET_ATS_NETWORK_TYPE:
184             if (ats_value > GNUNET_ATS_NetworkTypeCount)
185             {
186                 GNUNET_break (0);
187                 continue;
188             }
189             network = ats_value;
190             GNUNET_asprintf (&ats_prop_value, "%s", GNUNET_ATS_print_network_type(ats_value));
191             break;
192           default:
193             GNUNET_asprintf (&ats_prop_value, "%u", ats_value);
194             break;
195         }
196         if ((verbose) && (ats_type < GNUNET_ATS_PropertyCount))
197         {
198           GNUNET_asprintf (&ats_str, "%s%s=%s, ", ats_tmp, ats_prop_arr[ats_type] , ats_prop_value);
199           GNUNET_free (ats_tmp);
200         }
201         GNUNET_free (ats_prop_value);
202     }
203
204     fprintf (stderr,
205              _("Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
206              GNUNET_i2s (&pr->address->peer),
207              pr->address->transport_name,
208              address,
209              GNUNET_ATS_print_network_type(network),
210              ntohl (pr->bandwidth_out.value__),
211              ntohl (pr->bandwidth_in.value__),
212              ats_str);
213     GNUNET_free (ats_str);
214   }
215   else
216   {
217     /* We're done */
218     GNUNET_CONTAINER_DLL_remove (head, tail, pr);
219     GNUNET_free (pr->address);
220     GNUNET_free (pr);
221     pending--;
222
223     if ((GNUNET_YES == receive_done) && (0 == pending))
224     {
225         /* All messages received and no resolutions pending*/
226         if (end_task != GNUNET_SCHEDULER_NO_TASK)
227           GNUNET_SCHEDULER_cancel (end_task);
228         end_task = GNUNET_SCHEDULER_add_now (end, NULL);
229     }
230   }
231 }
232
233
234 static void
235 ats_perf_cb (void *cls,
236              const struct GNUNET_HELLO_Address *address,
237              int active,
238              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
239              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
240              const struct GNUNET_ATS_Information *ats,
241              uint32_t ats_count)
242 {
243   struct PendingResolutions * pr;
244
245   if (NULL != address)
246   {
247     pr = GNUNET_malloc (sizeof (struct PendingResolutions) +
248                         ats_count * sizeof (struct GNUNET_ATS_Information));
249
250     pr->ats_count = ats_count;
251     pr->ats = (struct GNUNET_ATS_Information *) &pr[1];
252     if (ats_count > 0)
253       memcpy (pr->ats, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
254     pr->address = GNUNET_HELLO_address_copy (address);
255     pr->bandwidth_in = bandwidth_in;
256     pr->bandwidth_out = bandwidth_out;
257     pr->tats_ctx = GNUNET_TRANSPORT_address_to_string(cfg, address,
258                       resolve_addresses_numeric,
259                                                           GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
260                       transport_addr_to_str_cb, pr);
261     GNUNET_CONTAINER_DLL_insert (head, tail, pr);
262     results++;
263     pending++;
264   }
265   else
266   {
267     /* All messages received */
268     receive_done = GNUNET_YES;
269     alh = NULL;
270     if (0 == pending)
271     {
272       /* All messages received and no resolutions pending*/
273       if (end_task != GNUNET_SCHEDULER_NO_TASK)
274         GNUNET_SCHEDULER_cancel (end_task);
275       end_task = GNUNET_SCHEDULER_add_now (end, NULL);
276     }
277   }
278 }
279
280 static unsigned int
281 print_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg)
282 {
283   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
284   char * entry_in = NULL;
285   char * entry_out = NULL;
286   char * quota_out_str;
287   char * quota_in_str;
288   unsigned long long int quota_out;
289   unsigned long long int quota_in;
290   int c;
291
292   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount); c++)
293   {
294
295     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
296     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
297
298     /* quota out */
299     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
300     {
301       if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
302           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &quota_out)))
303         quota_out = UINT32_MAX;
304
305       GNUNET_free (quota_out_str);
306       GNUNET_asprintf (&quota_out_str, "%llu", quota_out);
307     }
308     else
309     {
310         fprintf (stderr, "Outbound quota for network `%11s' not configured!\n",
311             network_str[c]);
312         GNUNET_asprintf (&quota_out_str, "-");
313     }
314     GNUNET_free (entry_out);
315
316     /* quota in */
317     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
318     {
319       if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
320           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &quota_in)))
321           quota_in = UINT32_MAX;
322       GNUNET_free (quota_in_str);
323       GNUNET_asprintf (&quota_in_str, "%llu", quota_in);
324     }
325     else
326     {
327         fprintf (stderr, "Inbound quota for network `%11s' not configured!\n",
328             network_str[c]);
329         GNUNET_asprintf (&quota_in_str, "-");
330     }
331     GNUNET_free (entry_in);
332
333     fprintf (stderr, _("Quota for network `%11s' (in/out): %10s / %10s\n"), network_str[c], quota_in_str, quota_out_str );
334     GNUNET_free (quota_out_str);
335     GNUNET_free (quota_in_str);
336   }
337   return GNUNET_ATS_NetworkTypeCount;
338 }
339
340
341 static void
342 testservice_ats (void *cls,
343                  int result)
344 {
345   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
346   struct GNUNET_PeerIdentity pid;
347   unsigned int c;
348   unsigned int type;
349
350   if (GNUNET_YES != result)
351   {
352     FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
353     return;
354   }
355
356   results = 0;
357
358   if (NULL != pid_str)
359   {
360     if (GNUNET_OK != GNUNET_CRYPTO_eddsa_public_key_from_string (pid_str,
361                                                                     strlen (pid_str),
362                                                                     &pid.public_key))
363     {
364       FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), pid_str);
365       return;
366     }
367   }
368
369   c = op_list_all + op_list_used + op_monitor + op_set_pref;
370   if ((1 < c))
371   {
372       FPRINTF (stderr, _("Please select one operation : %s or %s or %s or %s or %s\n"),
373                "--used", "--all", "--monitor", "--preference", "--quotas");
374       return;
375   }
376   if ((0 == c))
377     op_list_used = GNUNET_YES; /* set default */
378     if (op_print_quotas)
379     {
380         ret = print_quotas (cfg);
381         return;
382     }
383     if (op_list_all)
384     {
385         ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
386         if (NULL == ph)
387         {
388           fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
389           return;
390         }
391
392         alh = GNUNET_ATS_performance_list_addresses (ph,
393                                                (NULL == pid_str) ? NULL : &pid,
394                                                GNUNET_YES, ats_perf_cb, NULL);
395         if (NULL == alh)
396         {
397           fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n"));
398           end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
399           return;
400         }
401         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
402     }
403     else if (op_list_used)
404     {
405         ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
406         if (NULL == ph)
407           fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
408
409         alh = GNUNET_ATS_performance_list_addresses (ph,
410                                                (NULL == pid_str) ? NULL : &pid,
411                                                GNUNET_NO, ats_perf_cb, NULL);
412         if (NULL == alh)
413         {
414           fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n"));
415           end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
416           return;
417         }
418         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
419     }
420     else if (op_monitor)
421     {
422         ph = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL);
423         if (NULL == ph)
424           fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
425         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
426
427     }
428     else if (op_set_pref)
429     {
430                 if (NULL == type_str)
431                 {
432                         fprintf (stderr, _("No preference type given!\n"));
433                         return;
434                 }
435                 if (NULL == pid_str)
436                 {
437                         fprintf (stderr, _("No peer given!\n"));
438                         return;
439                 }
440
441
442
443         for (c = 0; c<strlen(type_str); c++)
444         {
445           if (isupper (type_str[c]))
446             type_str[c] = tolower (type_str[c]);
447         }
448
449         if (0 == strcasecmp("latency", type_str))
450           type = GNUNET_ATS_PREFERENCE_LATENCY;
451         else if (0 == strcasecmp("bandwidth", type_str))
452           type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
453         else
454         {
455           FPRINTF (stderr, "%s", _("Valid type required\n"));
456           return;
457         }
458
459             /* set */
460             ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
461             if (NULL == ph)
462               fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
463
464             GNUNET_ATS_performance_change_preference (ph, &pid, type, (double) value, GNUNET_ATS_PREFERENCE_END);
465
466             end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
467     }
468   ret = 1;
469 }
470
471 /**
472  * Main function that will be run by the scheduler.
473  *
474  * @param cls closure
475  * @param args remaining command-line arguments
476  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
477  * @param my_cfg configuration
478  */
479 static void
480 run (void *cls, char *const *args, const char *cfgfile,
481      const struct GNUNET_CONFIGURATION_Handle *my_cfg)
482 {
483   cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
484   GNUNET_CLIENT_service_test ("ats", cfg,
485                               TIMEOUT,
486                               &testservice_ats,
487                               (void *) cfg);
488 }
489
490
491 /**
492  * The main function.
493  *
494  * @param argc number of arguments from the command line
495  * @param argv command line arguments
496  * @return 0 ok, 1 on error
497  */
498 int
499 main (int argc, char *const *argv)
500 {
501   int res;
502   resolve_addresses_numeric = GNUNET_NO;
503   op_monitor = GNUNET_NO;
504   op_list_all = GNUNET_NO;
505   op_list_used = GNUNET_NO;
506   op_set_pref = GNUNET_NO;
507   pending = 0;
508   receive_done = GNUNET_NO;
509   type_str = NULL;
510
511   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
512       {'u', "used", NULL,
513        gettext_noop ("get list of active addresses currently used"),
514        0, &GNUNET_GETOPT_set_one, &op_list_used},
515        {'a', "all", NULL,
516         gettext_noop ("get list of all active addresses"),
517         0, &GNUNET_GETOPT_set_one, &op_list_all},
518       {'n', "numeric", NULL,
519        gettext_noop ("do not resolve IP addresses to hostnames"),
520        0, &GNUNET_GETOPT_set_one, &resolve_addresses_numeric},
521        {'m', "monitor", NULL,
522         gettext_noop ("monitor mode"),
523         0, &GNUNET_GETOPT_set_one, &op_monitor},
524        {'p', "preference", NULL,
525          gettext_noop ("set preference for the given peer"),
526          0, &GNUNET_GETOPT_set_one, &op_set_pref},
527        {'q', "quotas", NULL,
528          gettext_noop ("print all configured quotas"),
529          0, &GNUNET_GETOPT_set_one, &op_print_quotas},
530        {'i', "id", "TYPE",
531          gettext_noop ("peer id"),
532          1, &GNUNET_GETOPT_set_string, &pid_str},
533        {'t', "type", "TYPE",
534          gettext_noop ("preference type to set: latency | bandwidth"),
535          1, &GNUNET_GETOPT_set_string, &type_str},
536        {'k', "value", "VALUE",
537          gettext_noop ("preference value"),
538          1, &GNUNET_GETOPT_set_uint, &value},
539        {'V', "verbose", NULL,
540         gettext_noop ("verbose output (include ATS address properties)"),
541         0, &GNUNET_GETOPT_set_one, &verbose},
542     GNUNET_GETOPT_OPTION_END
543   };
544
545   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
546     return 2;
547
548   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
549                               gettext_noop ("Print information about ATS state"), options, &run,
550                               NULL);
551   GNUNET_free_non_null (pid_str);
552   GNUNET_free_non_null (type_str);
553   GNUNET_free ((void *) argv);
554
555   if (GNUNET_OK == res)
556     return ret;
557   else
558     return 1;
559
560 }
561
562 /* end of gnunet-ats.c */