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