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