changes
[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
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     for (c = 0; c < pr->ats_count; c++)
169     {
170         ats_tmp = ats_str;
171
172         ats_type = ntohl(pr->ats[c].type);
173         ats_value = ntohl(pr->ats[c].value);
174
175         if (ats_type > GNUNET_ATS_PropertyCount)
176         {
177             GNUNET_break (0);
178             continue;
179         }
180
181         switch (ats_type) {
182           case GNUNET_ATS_NETWORK_TYPE:
183             if (ats_value > GNUNET_ATS_NetworkTypeCount)
184             {
185                 GNUNET_break (0);
186                 continue;
187             }
188             network = ats_value;
189             GNUNET_asprintf (&ats_prop_value, "%s", GNUNET_ATS_print_network_type(ats_value));
190             break;
191           default:
192             GNUNET_asprintf (&ats_prop_value, "%u", ats_value);
193             break;
194         }
195         if ((verbose) && (ats_type < GNUNET_ATS_PropertyCount))
196         {
197           GNUNET_asprintf (&ats_str, "%s%s=%s, ", ats_tmp, ats_prop_arr[ats_type] , ats_prop_value);
198           GNUNET_free (ats_tmp);
199         }
200         GNUNET_free (ats_prop_value);
201                 }
202
203     fprintf (stderr, _("Peer `%s' plugin `%s', address `%s', `%s' bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
204       GNUNET_i2s (&pr->address->peer), pr->address->transport_name, address,
205       GNUNET_ATS_print_network_type(network),
206       ntohl (pr->bandwidth_out.value__), ntohl (pr->bandwidth_in.value__),ats_str);
207     GNUNET_free (ats_str);
208   }
209   else
210   {
211     /* We're done */
212     GNUNET_CONTAINER_DLL_remove (head, tail, pr);
213     GNUNET_free (pr->address);
214     GNUNET_free (pr);
215     pending--;
216
217     if ((GNUNET_YES == receive_done) && (0 == pending))
218     {
219         /* All messages received and no resolutions pending*/
220         if (end_task != GNUNET_SCHEDULER_NO_TASK)
221           GNUNET_SCHEDULER_cancel (end_task);
222         end_task = GNUNET_SCHEDULER_add_now (end, NULL);
223     }
224   }
225 }
226
227
228 static void 
229 ats_perf_cb (void *cls,
230              const struct GNUNET_HELLO_Address *address,
231              int active,
232              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
233              struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
234              const struct GNUNET_ATS_Information *ats,
235              uint32_t ats_count)
236 {
237   struct PendingResolutions * pr;
238
239   if (NULL != address)
240   {
241     pr = GNUNET_malloc (sizeof (struct PendingResolutions) +
242                         ats_count * sizeof (struct GNUNET_ATS_Information));
243
244     pr->ats_count = ats_count;
245     pr->ats = (struct GNUNET_ATS_Information *) &pr[1];
246     if (ats_count > 0)
247       memcpy (pr->ats, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
248     pr->address = GNUNET_HELLO_address_copy (address);
249     pr->bandwidth_in = bandwidth_in;
250     pr->bandwidth_out = bandwidth_out;
251     pr->tats_ctx = GNUNET_TRANSPORT_address_to_string(cfg, address,
252                       resolve_addresses_numeric, GNUNET_TIME_UNIT_FOREVER_REL, transport_addr_to_str_cb, pr);
253     GNUNET_CONTAINER_DLL_insert (head, tail, pr);
254     results++;
255     pending++;
256   }
257   else
258   {
259     /* All messages received */
260     receive_done = GNUNET_YES;
261     alh = NULL;
262     if (0 == pending)
263     {
264       /* All messages received and no resolutions pending*/
265       if (end_task != GNUNET_SCHEDULER_NO_TASK)
266         GNUNET_SCHEDULER_cancel (end_task);
267       end_task = GNUNET_SCHEDULER_add_now (end, NULL);
268     }
269   }
270 }
271
272 static unsigned int
273 print_quotas (const struct GNUNET_CONFIGURATION_Handle *cfg)
274 {
275   char *network_str[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
276   char * entry_in = NULL;
277   char * entry_out = NULL;
278   char * quota_out_str;
279   char * quota_in_str;
280   unsigned long long int quota_out;
281   unsigned long long int quota_in;
282   int c;
283
284   for (c = 0; (c < GNUNET_ATS_NetworkTypeCount); c++)
285   {
286
287     GNUNET_asprintf (&entry_out, "%s_QUOTA_OUT", network_str[c]);
288     GNUNET_asprintf (&entry_in, "%s_QUOTA_IN", network_str[c]);
289
290     /* quota out */
291     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_out, &quota_out_str))
292     {
293       if (0 == strcmp(quota_out_str, BIG_M_STRING) ||
294           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_out_str, &quota_out)))
295         quota_out = UINT32_MAX;
296
297       GNUNET_free (quota_out_str);
298       GNUNET_asprintf (&quota_out_str, "%llu", quota_out);
299     }
300     else
301     {
302         fprintf (stderr, "Outbound quota for network `%11s' not configured!\n",
303             network_str[c]);
304         GNUNET_asprintf (&quota_out_str, "-");
305     }
306     GNUNET_free (entry_out);
307
308     /* quota in */
309     if (GNUNET_OK == GNUNET_CONFIGURATION_get_value_string(cfg, "ats", entry_in, &quota_in_str))
310     {
311       if (0 == strcmp(quota_in_str, BIG_M_STRING) ||
312           (GNUNET_SYSERR == GNUNET_STRINGS_fancy_size_to_bytes (quota_in_str, &quota_in)))
313           quota_in = UINT32_MAX;
314       GNUNET_free (quota_in_str);
315       GNUNET_asprintf (&quota_in_str, "%llu", quota_in);
316     }
317     else
318     {
319         fprintf (stderr, "Inbound quota for network `%11s' not configured!\n",
320             network_str[c]);
321         GNUNET_asprintf (&quota_in_str, "-");
322     }
323     GNUNET_free (entry_in);
324
325     fprintf (stderr, _("Quota for network `%11s' (in/out): %10s / %10s\n"), network_str[c], quota_in_str, quota_out_str );
326     GNUNET_free (quota_out_str);
327     GNUNET_free (quota_in_str);
328   }
329   return GNUNET_ATS_NetworkTypeCount;
330 }
331
332
333 static void
334 testservice_ats (void *cls,
335                  int result)
336 {
337   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
338   struct GNUNET_PeerIdentity pid;
339   unsigned int c;
340   unsigned int type;
341
342   if (GNUNET_YES != result)
343   {
344     FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
345     return;
346   }
347
348   results = 0;
349
350   if (NULL != pid_str)
351   {
352     if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (pid_str, &pid.hashPubKey))
353     {
354       FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), pid_str);
355       return;
356     }
357   }
358
359   c = op_list_all + op_list_used + op_monitor + op_set_pref;
360   if ((1 < c))
361   {
362       FPRINTF (stderr, _("Please select one operation : %s or %s or %s or %s or %s\n"),
363                "--used", "--all", "--monitor", "--preference", "--quotas");
364       return;
365   }
366   if ((0 == c))
367     op_list_used = GNUNET_YES; /* set default */
368     if (op_print_quotas)
369     {
370         ret = print_quotas (cfg);
371         return;
372     }
373     if (op_list_all)
374     {
375         ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
376         if (NULL == ph)
377         {
378           fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
379           return;
380         }
381
382         alh = GNUNET_ATS_performance_list_addresses (ph,
383                                                (NULL == pid_str) ? NULL : &pid,
384                                                GNUNET_YES, ats_perf_cb, NULL);
385         if (NULL == alh)
386         {
387           fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n"));
388           end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
389           return;
390         }
391         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
392     }
393     else if (op_list_used)
394     {
395         ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
396         if (NULL == ph)
397           fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
398
399         alh = GNUNET_ATS_performance_list_addresses (ph,
400                                                (NULL == pid_str) ? NULL : &pid,
401                                                GNUNET_NO, ats_perf_cb, NULL);
402         if (NULL == alh)
403         {
404           fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n"));
405           end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
406           return;
407         }
408         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
409     }
410     else if (op_monitor)
411     {
412         ph = GNUNET_ATS_performance_init (cfg, ats_perf_cb, NULL);
413         if (NULL == ph)
414           fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
415         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
416
417     }
418     else if (op_set_pref)
419     {
420         for (c = 0; c<strlen(type_str); c++)
421         {
422           if (isupper (type_str[c]))
423             type_str[c] = tolower (type_str[c]);
424         }
425
426         if (0 == strcasecmp("latency", type_str))
427           type = GNUNET_ATS_PREFERENCE_LATENCY;
428         else if (0 == strcasecmp("bandwidth", type_str))
429           type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
430         else
431         {
432           FPRINTF (stderr, "%s", _("Type required\n"));
433           return;
434         }
435
436             /* set */
437             ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
438             if (NULL == ph)
439               fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
440
441             GNUNET_ATS_change_preference (ph, &pid, type, (double) value, GNUNET_ATS_PREFERENCE_END);
442
443             end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
444     }
445   ret = 1;
446 }
447
448 /**
449  * Main function that will be run by the scheduler.
450  *
451  * @param cls closure
452  * @param args remaining command-line arguments
453  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
454  * @param my_cfg configuration
455  */
456 static void
457 run (void *cls, char *const *args, const char *cfgfile,
458      const struct GNUNET_CONFIGURATION_Handle *my_cfg)
459 {
460   cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
461   GNUNET_CLIENT_service_test ("ats", cfg,
462                               TIMEOUT,
463                               &testservice_ats,
464                               (void *) cfg);
465 }
466
467
468 /**
469  * The main function.
470  *
471  * @param argc number of arguments from the command line
472  * @param argv command line arguments
473  * @return 0 ok, 1 on error
474  */
475 int
476 main (int argc, char *const *argv)
477 {
478   int res;
479   resolve_addresses_numeric = GNUNET_NO;
480   op_monitor = GNUNET_NO;
481   op_list_all = GNUNET_NO;
482   op_list_used = GNUNET_NO;
483   op_set_pref = GNUNET_NO;
484   pending = 0;
485   receive_done = GNUNET_NO;
486
487   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
488       {'u', "used", NULL,
489        gettext_noop ("get list of active addresses currently used"),
490        0, &GNUNET_GETOPT_set_one, &op_list_used},
491        {'a', "all", NULL,
492         gettext_noop ("get list of all active addresses"),
493         0, &GNUNET_GETOPT_set_one, &op_list_all},
494       {'n', "numeric", NULL,
495        gettext_noop ("do not resolve IP addresses to hostnames"),
496        0, &GNUNET_GETOPT_set_one, &resolve_addresses_numeric},
497        {'m', "monitor", NULL,
498         gettext_noop ("monitor mode"),
499         0, &GNUNET_GETOPT_set_one, &op_monitor},
500        {'p', "preference", NULL,
501          gettext_noop ("set preference for the given peer"),
502          0, &GNUNET_GETOPT_set_one, &op_set_pref},
503        {'q', "quotas", NULL,
504          gettext_noop ("print all configured quotas"),
505          0, &GNUNET_GETOPT_set_one, &op_print_quotas},
506        {'i', "id", "TYPE",
507          gettext_noop ("peer id"),
508          1, &GNUNET_GETOPT_set_string, &pid_str},
509        {'t', "type", "TYPE",
510          gettext_noop ("preference type to set: latency | bandwidth"),
511          1, &GNUNET_GETOPT_set_string, &type_str},
512        {'k', "value", "VALUE",
513          gettext_noop ("preference value"),
514          1, &GNUNET_GETOPT_set_uint, &value},
515        {'V', "verbose", NULL,
516         gettext_noop ("verbose output (include ATS address properties)"),
517         0, &GNUNET_GETOPT_set_one, &verbose},
518     GNUNET_GETOPT_OPTION_END
519   };
520
521   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
522     return 2;
523
524   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
525                               gettext_noop ("Print information about ATS state"), options, &run,
526                               NULL);
527   GNUNET_free_non_null (pid_str);
528   GNUNET_free_non_null (type_str);
529   GNUNET_free ((void *) argv);
530
531   if (GNUNET_OK == res)
532     return ret;
533   else
534     return 1;
535
536 }
537
538 /* end of gnunet-ats.c */