advanced logging
[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 static struct GNUNET_ATS_PerformanceHandle *ph;
83
84 static struct GNUNET_ATS_AddressListHandle *alh;
85
86 static struct GNUNET_CONFIGURATION_Handle *cfg;
87
88 static GNUNET_SCHEDULER_TaskIdentifier end_task;
89
90 static struct GNUNET_CONTAINER_MultiPeerMap *addresses;
91
92
93 struct PendingResolutions
94 {
95   struct PendingResolutions *next;
96   struct PendingResolutions *prev;
97
98   struct GNUNET_HELLO_Address *address;
99   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
100   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
101
102   struct GNUNET_ATS_Information *ats;
103   uint32_t ats_count;
104
105   struct GNUNET_TRANSPORT_AddressToStringContext * tats_ctx;
106 };
107
108 struct ATSAddress
109 {
110   struct GNUNET_HELLO_Address *address;
111   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
112   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
113 };
114
115
116
117 static struct PendingResolutions *head;
118
119 static struct PendingResolutions *tail;
120
121 static int
122 free_addr_it (void *cls,
123     const struct GNUNET_PeerIdentity *key,
124     void *value)
125 {
126   struct ATSAddress *a = value;
127   GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (addresses,
128       key, a));
129   GNUNET_HELLO_address_free (a->address);
130   GNUNET_free (a);
131   return GNUNET_OK;
132 }
133
134 static void
135 end(void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
136 {
137   struct PendingResolutions * pr;
138   struct PendingResolutions * next;
139   unsigned int pending;
140
141   if (NULL != alh)
142   {
143     GNUNET_ATS_performance_list_addresses_cancel (alh);
144     alh = NULL;
145   }
146
147   if (NULL != ph)
148   {
149     GNUNET_ATS_performance_done (ph);
150     ph = NULL;
151   }
152
153   pending = 0;
154   next = head;
155   while (NULL != (pr = next))
156   {
157     next = pr->next;
158     GNUNET_CONTAINER_DLL_remove(head, tail, pr);
159     GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
160     GNUNET_free(pr->address);
161     GNUNET_free(pr);
162     pending++;
163   }
164
165   GNUNET_CONTAINER_multipeermap_iterate(addresses, &free_addr_it, NULL);
166   GNUNET_CONTAINER_multipeermap_destroy(addresses);
167
168   if (0 < pending)
169     fprintf (stderr, _("%u address resolutions had a timeout\n"), pending);
170   if (op_list_used || op_list_all)
171     fprintf (stderr, _("ATS returned results for %u addresses\n"), results);
172   ret = 0;
173 }
174
175 static void
176 transport_addr_to_str_cb(void *cls, const char *address)
177 {
178   struct PendingResolutions * pr = cls;
179   char *ats_str;
180   char *ats_tmp;
181   char *ats_prop_arr[GNUNET_ATS_PropertyCount] = GNUNET_ATS_PropertyStrings;
182   char *ats_prop_value;
183   unsigned int c;
184   uint32_t ats_type;
185   uint32_t ats_value;
186   uint32_t network;
187
188   if (NULL != address)
189   {
190     ats_str = GNUNET_strdup("");
191     network = GNUNET_ATS_NET_UNSPECIFIED;
192     for (c = 0; c < pr->ats_count; c++)
193     {
194       ats_tmp = ats_str;
195
196       ats_type = ntohl (pr->ats[c].type);
197       ats_value = ntohl (pr->ats[c].value);
198
199       if (ats_type > GNUNET_ATS_PropertyCount)
200       {
201         fprintf (stderr, "Invalid ATS property type %u %u for address %s\n", ats_type, pr->ats[c].type,
202             address);
203         continue;
204       }
205
206       switch (ats_type)
207       {
208       case GNUNET_ATS_NETWORK_TYPE:
209         if (ats_value > GNUNET_ATS_NetworkTypeCount)
210         {
211           GNUNET_break(0);
212           continue;
213         }
214         network = ats_value;
215         GNUNET_asprintf (&ats_prop_value, "%s",
216             GNUNET_ATS_print_network_type (ats_value));
217         break;
218       default:
219         GNUNET_asprintf (&ats_prop_value, "%u", ats_value);
220         break;
221       }
222       if ((verbose) && (ats_type < GNUNET_ATS_PropertyCount))
223       {
224         GNUNET_asprintf (&ats_str, "%s%s=%s, ", ats_tmp, ats_prop_arr[ats_type],
225             ats_prop_value);
226         GNUNET_free(ats_tmp);
227       }
228       GNUNET_free(ats_prop_value);
229     }
230
231     fprintf (stderr,
232         _("Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
233         GNUNET_i2s (&pr->address->peer), pr->address->transport_name, address,
234         GNUNET_ATS_print_network_type (network),
235         ntohl (pr->bandwidth_out.value__), ntohl (pr->bandwidth_in.value__),
236         ats_str);
237     GNUNET_free(ats_str);
238   }
239   else
240   {
241     /* We're done */
242     GNUNET_CONTAINER_DLL_remove(head, tail, pr);
243     GNUNET_free(pr->address);
244     GNUNET_free(pr);
245     pending--;
246
247     if ((GNUNET_YES == receive_done) && (0 == pending))
248     {
249       /* All messages received and no resolutions pending*/
250       if (end_task != GNUNET_SCHEDULER_NO_TASK )
251         GNUNET_SCHEDULER_cancel (end_task);
252       end_task = GNUNET_SCHEDULER_add_now (end, NULL );
253     }
254   }
255 }
256
257
258 struct AddressFindCtx
259 {
260   const struct GNUNET_HELLO_Address *src;
261   struct ATSAddress *res;
262 };
263
264 static int
265 find_address_it (void *cls,
266     const struct GNUNET_PeerIdentity *key,
267     void *value)
268 {
269   struct AddressFindCtx *actx = cls;
270   struct ATSAddress *exist = value;
271
272   if (0 == GNUNET_HELLO_address_cmp (actx->src, exist->address))
273   {
274     actx->res = exist;
275     return GNUNET_NO;
276   }
277
278   return GNUNET_YES;
279 }
280
281 static void
282 ats_perf_cb(void *cls, const struct GNUNET_HELLO_Address *address, int active,
283     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
284     struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
285     const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
286 {
287   struct PendingResolutions * pr;
288
289   if ((GNUNET_YES == op_monitor) && (GNUNET_NO == verbose))
290   {
291     struct AddressFindCtx actx;
292
293     actx.src = address;
294     actx.res = NULL;
295
296     GNUNET_CONTAINER_multipeermap_iterate (addresses, find_address_it, &actx);
297     if ((actx.res != NULL))
298     {
299       if ((bandwidth_in.value__ == actx.res->bandwidth_in.value__) &&
300           (bandwidth_out.value__ == actx.res->bandwidth_out.value__) )
301       {
302         return; /* Nothing to do here */
303       }
304       else
305       {
306         actx.res->bandwidth_in = bandwidth_in;
307         actx.res->bandwidth_out = bandwidth_out;
308       }
309     }
310
311     struct ATSAddress *a = GNUNET_new (struct ATSAddress);
312     a->address = GNUNET_HELLO_address_copy(address);
313     a->bandwidth_in = bandwidth_in;
314     a->bandwidth_out = bandwidth_out;
315     GNUNET_CONTAINER_multipeermap_put (addresses, &address->peer, a,
316         GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
317   }
318
319   if (NULL != address)
320   {
321     pr = GNUNET_malloc (sizeof (struct PendingResolutions) +
322         ats_count * sizeof (struct GNUNET_ATS_Information));
323
324     pr->ats_count = ats_count;
325     pr->ats = (struct GNUNET_ATS_Information *) &pr[1];
326     if (ats_count > 0)
327       memcpy (pr->ats, ats, ats_count * sizeof(struct GNUNET_ATS_Information));
328     pr->address = GNUNET_HELLO_address_copy (address);
329     pr->bandwidth_in = bandwidth_in;
330     pr->bandwidth_out = bandwidth_out;
331     pr->tats_ctx = GNUNET_TRANSPORT_address_to_string (cfg, address,
332         resolve_addresses_numeric,
333         GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 10),
334         transport_addr_to_str_cb, pr);
335     GNUNET_CONTAINER_DLL_insert(head, tail, pr);
336     results++;
337     pending++;
338   }
339   else
340   {
341     /* All messages received */
342     receive_done = GNUNET_YES;
343     alh = NULL;
344     if (0 == pending)
345     {
346       /* All messages received and no resolutions pending*/
347       if (end_task != GNUNET_SCHEDULER_NO_TASK )
348         GNUNET_SCHEDULER_cancel (end_task);
349       end_task = GNUNET_SCHEDULER_add_now (end, NULL );
350     }
351   }
352 }
353
354 static unsigned int
355 print_quotas(const struct GNUNET_CONFIGURATION_Handle *cfg)
356 {
357   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
358   char * entry_in = NULL;
359   char * entry_out = NULL;
360   char * quota_out_str;
361   char * quota_in_str;
362   unsigned long long int quota_out;
363   unsigned long long int quota_in;
364   int c;
365
366   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount); c++)
367   {
368
369     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
370     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
371
372     /* quota out */
373     if (GNUNET_OK
374         == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_out,
375             &quota_out_str))
376     {
377       if (0 == strcmp (quota_out_str, BIG_M_STRING)
378           || (GNUNET_SYSERR
379               == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &quota_out)))
380         quota_out = UINT32_MAX;
381
382       GNUNET_free(quota_out_str);
383       GNUNET_asprintf (&quota_out_str, "%llu", quota_out);
384     }
385     else
386     {
387       fprintf (stderr, "Outbound quota for network `%11s' not configured!\n",
388           network_str[c]);
389       GNUNET_asprintf (&quota_out_str, "-");
390     }
391     GNUNET_free(entry_out);
392
393     /* quota in */
394     if (GNUNET_OK
395         == GNUNET_CONFIGURATION_get_value_string (cfg, "ats", entry_in,
396             &quota_in_str))
397     {
398       if (0 == strcmp (quota_in_str, BIG_M_STRING)
399           || (GNUNET_SYSERR
400               == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &quota_in)))
401         quota_in = UINT32_MAX;
402       GNUNET_free(quota_in_str);
403       GNUNET_asprintf (&quota_in_str, "%llu", quota_in);
404     }
405     else
406     {
407       fprintf (stderr, "Inbound quota for network `%11s' not configured!\n",
408           network_str[c]);
409       GNUNET_asprintf (&quota_in_str, "-");
410     }
411     GNUNET_free(entry_in);
412
413     fprintf (stderr, _("Quota for network `%11s' (in/out): %10s / %10s\n"),
414         network_str[c], quota_in_str, quota_out_str);
415     GNUNET_free(quota_out_str);
416     GNUNET_free(quota_in_str);
417   }
418   return GNUNET_ATS_NetworkTypeCount;
419 }
420
421 static void
422 testservice_ats(void *cls, int result)
423 {
424   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
425   struct GNUNET_PeerIdentity pid;
426   unsigned int c;
427   unsigned int type;
428
429   addresses = GNUNET_CONTAINER_multipeermap_create (10, GNUNET_YES);
430
431   if (GNUNET_YES != result)
432   {
433     FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
434     return;
435   }
436
437   results = 0;
438
439   if (NULL != pid_str)
440   {
441     if (GNUNET_OK
442         != GNUNET_CRYPTO_eddsa_public_key_from_string (pid_str,
443             strlen (pid_str), &pid.public_key))
444     {
445       FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), pid_str);
446       return;
447     }
448   }
449
450   c = op_list_all + op_list_used + op_monitor + op_set_pref;
451   if ((1 < c))
452   {
453     FPRINTF (stderr,
454         _("Please select one operation : %s or %s or %s or %s or %s\n"),
455         "--used", "--all", "--monitor", "--preference", "--quotas");
456     return;
457   }
458   if ((0 == c))
459     op_list_used = GNUNET_YES; /* set default */
460   if (op_print_quotas)
461   {
462     ret = print_quotas (cfg);
463     return;
464   }
465   if (op_list_all)
466   {
467     ph = GNUNET_ATS_performance_init (cfg, NULL, NULL );
468     if (NULL == ph)
469     {
470       fprintf (stderr, _("Cannot connect to ATS service, exiting...\n") );
471       return;
472     }
473
474     alh = GNUNET_ATS_performance_list_addresses (ph,
475         (NULL == pid_str) ? NULL : &pid, GNUNET_YES, ats_perf_cb, NULL );
476     if (NULL == alh)
477     {
478       fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n") );
479       end_task = GNUNET_SCHEDULER_add_now (&end, NULL );
480       return;
481     }
482     end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end,
483         NULL );
484   }
485   else if (op_list_used)
486   {
487     ph = GNUNET_ATS_performance_init (cfg, NULL, NULL );
488     if (NULL == ph)
489       fprintf (stderr, _("Cannot connect to ATS service, exiting...\n") );
490
491     alh = GNUNET_ATS_performance_list_addresses (ph,
492         (NULL == pid_str) ? NULL : &pid, GNUNET_NO, ats_perf_cb, NULL );
493     if (NULL == alh)
494     {
495       fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n") );
496       end_task = GNUNET_SCHEDULER_add_now (&end, NULL );
497       return;
498     }
499     end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end,
500         NULL );
501   }
502   else if (op_monitor)
503   {
504     ph = GNUNET_ATS_performance_init (cfg, &ats_perf_cb, NULL );
505     if (NULL == ph)
506       fprintf (stderr, _("Cannot connect to ATS service, exiting...\n") );
507     end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end,
508         NULL );
509
510   }
511   else if (op_set_pref)
512   {
513     if (NULL == type_str)
514     {
515       fprintf (stderr, _("No preference type given!\n") );
516       return;
517     }
518     if (NULL == pid_str)
519     {
520       fprintf (stderr, _("No peer given!\n") );
521       return;
522     }
523
524     for (c = 0; c < strlen (type_str); c++)
525     {
526       if (isupper (type_str[c]))
527         type_str[c] = tolower (type_str[c]);
528     }
529
530     if (0 == strcasecmp ("latency", type_str))
531       type = GNUNET_ATS_PREFERENCE_LATENCY;
532     else if (0 == strcasecmp ("bandwidth", type_str))
533       type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
534     else
535     {
536       FPRINTF (stderr, "%s", _("Valid type required\n") );
537       return;
538     }
539
540     /* set */
541     ph = GNUNET_ATS_performance_init (cfg, NULL, NULL );
542     if (NULL == ph)
543       fprintf (stderr, _("Cannot connect to ATS service, exiting...\n") );
544
545     GNUNET_ATS_performance_change_preference (ph, &pid, type, (double) value,
546         GNUNET_ATS_PREFERENCE_END);
547
548     end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end,
549         NULL );
550   }
551   ret = 1;
552 }
553
554 /**
555  * Main function that will be run by the scheduler.
556  *
557  * @param cls closure
558  * @param args remaining command-line arguments
559  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
560  * @param my_cfg configuration
561  */
562 static void
563 run(void *cls, char * const *args, const char *cfgfile,
564     const struct GNUNET_CONFIGURATION_Handle *my_cfg)
565 {
566   cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
567   GNUNET_CLIENT_service_test ("ats", cfg, TIMEOUT, &testservice_ats,
568       (void *) cfg);
569 }
570
571 /**
572  * The main function.
573  *
574  * @param argc number of arguments from the command line
575  * @param argv command line arguments
576  * @return 0 ok, 1 on error
577  */
578 int
579 main(int argc, char * const *argv)
580 {
581   int res;
582   resolve_addresses_numeric = GNUNET_NO;
583   op_monitor = GNUNET_NO;
584   op_list_all = GNUNET_NO;
585   op_list_used = GNUNET_NO;
586   op_set_pref = GNUNET_NO;
587   pending = 0;
588   receive_done = GNUNET_NO;
589   type_str = NULL;
590
591   static const struct GNUNET_GETOPT_CommandLineOption options[] =
592   {
593   { 'u', "used", NULL,
594       gettext_noop ("get list of active addresses currently used"), 0,
595       &GNUNET_GETOPT_set_one, &op_list_used },
596   { 'a', "all", NULL, gettext_noop ("get list of all active addresses"), 0,
597       &GNUNET_GETOPT_set_one, &op_list_all },
598   { 'n', "numeric", NULL,
599       gettext_noop ("do not resolve IP addresses to hostnames"), 0,
600       &GNUNET_GETOPT_set_one, &resolve_addresses_numeric },
601   { 'm', "monitor", NULL, gettext_noop ("monitor mode"), 0,
602       &GNUNET_GETOPT_set_one, &op_monitor },
603   { 'p', "preference", NULL, gettext_noop ("set preference for the given peer"),
604       0, &GNUNET_GETOPT_set_one, &op_set_pref },
605   { 'q', "quotas", NULL, gettext_noop ("print all configured quotas"), 0,
606       &GNUNET_GETOPT_set_one, &op_print_quotas },
607   { 'i', "id", "TYPE", gettext_noop ("peer id"), 1, &GNUNET_GETOPT_set_string,
608       &pid_str },
609   { 't', "type", "TYPE",
610       gettext_noop ("preference type to set: latency | bandwidth"), 1,
611       &GNUNET_GETOPT_set_string, &type_str },
612   { 'k', "value", "VALUE", gettext_noop ("preference value"), 1,
613       &GNUNET_GETOPT_set_uint, &value },
614   { 'V', "verbose", NULL,
615       gettext_noop ("verbose output (include ATS address properties)"), 0,
616       &GNUNET_GETOPT_set_one, &verbose }, GNUNET_GETOPT_OPTION_END };
617
618   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
619     return 2;
620
621   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
622       gettext_noop ("Print information about ATS state"), options, &run, NULL );
623   GNUNET_free_non_null(pid_str);
624   GNUNET_free_non_null(type_str);
625   GNUNET_free((void * ) argv);
626
627   if (GNUNET_OK == res)
628     return ret;
629   else
630     return 1;
631
632 }
633
634 /* end of gnunet-ats.c */