- changes
[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 /**
34  * Final status code.
35  */
36 static int ret;
37 static int results;
38 static int resolve_addresses_numeric;
39 /**
40  * For which peer should we change preference values?
41  */
42 static char *pid_str;
43
44 static char *type_str;
45 static unsigned int value;
46
47 /**
48  * Print verbose ATS information
49  */
50 static int verbose;
51
52 /**
53  * List only addresses currently used (active)
54  */
55 static int op_list_used;
56
57 /**
58  * List all addresses
59  */
60 static int op_list_all;
61
62 /**
63  * List all addresses
64  */
65 static int op_set_pref;
66
67 /**
68  * Monitor addresses used
69  */
70 static int op_monitor;
71
72
73
74 static struct GNUNET_ATS_PerformanceHandle *ph;
75
76 struct GNUNET_ATS_AddressListHandle *alh;
77
78 static struct GNUNET_CONFIGURATION_Handle *cfg;
79
80 GNUNET_SCHEDULER_TaskIdentifier end_task;
81
82 struct PendingResolutions
83 {
84   struct PendingResolutions *next;
85   struct PendingResolutions *prev;
86
87   struct GNUNET_HELLO_Address *address;
88   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
89   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
90
91   struct GNUNET_ATS_Information *ats;
92   uint32_t ats_count;
93
94   struct GNUNET_TRANSPORT_AddressToStringContext * tats_ctx;
95 };
96
97 struct PendingResolutions *head;
98 struct PendingResolutions *tail;
99
100 void transport_addr_to_str_cb (void *cls, const char *address)
101 {
102   struct PendingResolutions * pr = cls;
103   char *ats_str;
104   char *ats_tmp;
105   char *ats_prop_arr[GNUNET_ATS_PropertyCount] = GNUNET_ATS_PropertyStrings;
106   char *ats_net_arr[GNUNET_ATS_NetworkTypeCount] = GNUNET_ATS_NetworkTypeString;
107   char *ats_prop_value;
108   unsigned int c;
109   uint32_t ats_type;
110   uint32_t ats_value;
111
112   if (NULL != address)
113   {
114       ats_str = GNUNET_strdup("");
115       if (verbose)
116       {
117       for (c = 0; c <  pr->ats_count; c++)
118       {
119           ats_tmp = ats_str;
120
121           ats_type = ntohl(pr->ats[c].type);
122           ats_value = ntohl(pr->ats[c].value);
123
124           if (ats_type > GNUNET_ATS_PropertyCount)
125           {
126               GNUNET_break (0);
127               continue;
128           }
129
130           switch (ats_type) {
131             case GNUNET_ATS_NETWORK_TYPE:
132               if (ats_value > GNUNET_ATS_NetworkTypeCount)
133               {
134                   GNUNET_break (0);
135                   continue;
136               }
137               GNUNET_asprintf (&ats_prop_value, "%s", ats_net_arr[ats_value]);
138               break;
139             default:
140               GNUNET_asprintf (&ats_prop_value, "%u", ats_value);
141               break;
142           }
143
144           GNUNET_asprintf (&ats_str, "%s%s=%s, ", ats_tmp, ats_prop_arr[ats_type] , ats_prop_value);
145           GNUNET_free (ats_tmp);
146           GNUNET_free (ats_prop_value);
147         }
148       }
149
150       fprintf (stderr, _("Peer `%s' plugin `%s', address `%s', bw out: %u Bytes/s, bw in %u Bytes/s, %s\n"),
151         GNUNET_i2s (&pr->address->peer), pr->address->transport_name, address,
152         ntohl (pr->bandwidth_out.value__), ntohl (pr->bandwidth_in.value__),ats_str);
153       GNUNET_free (ats_str);
154   }
155   else if (NULL != pr)
156   {
157       /* We're done */
158       GNUNET_CONTAINER_DLL_remove (head, tail, pr);
159       GNUNET_free (pr->address);
160       GNUNET_free (pr);
161   }
162
163 }
164
165 void ats_perf_cb (void *cls,
166                   const struct
167                   GNUNET_HELLO_Address *
168                   address,
169                   struct
170                   GNUNET_BANDWIDTH_Value32NBO
171                   bandwidth_out,
172                   struct
173                   GNUNET_BANDWIDTH_Value32NBO
174                   bandwidth_in,
175                   const struct
176                   GNUNET_ATS_Information *
177                   ats, uint32_t ats_count)
178 {
179   struct PendingResolutions * pr;
180
181   pr = GNUNET_malloc (sizeof (struct PendingResolutions) +
182                       ats_count * sizeof (struct GNUNET_ATS_Information));
183
184   pr->ats_count = ats_count;
185   pr->ats = (struct GNUNET_ATS_Information *) &pr[1];
186   if (ats_count > 0)
187     memcpy (pr->ats, ats, ats_count * sizeof (struct GNUNET_ATS_Information));
188   pr->address = GNUNET_HELLO_address_copy (address);
189   pr->bandwidth_in = bandwidth_in;
190   pr->bandwidth_out = bandwidth_out;
191   pr->tats_ctx = GNUNET_TRANSPORT_address_to_string(cfg, address,
192                     resolve_addresses_numeric, GNUNET_TIME_UNIT_FOREVER_REL, transport_addr_to_str_cb, pr);
193   GNUNET_CONTAINER_DLL_insert (head, tail, pr);
194   results++;
195 }
196
197 void la_cb (void *cls,
198             const struct GNUNET_HELLO_Address * address,
199             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
200             struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
201             const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
202 {
203
204 }
205
206 void end (void *cls,
207           const struct GNUNET_SCHEDULER_TaskContext *tc)
208 {
209   struct PendingResolutions * pr;
210   struct PendingResolutions * next;
211   unsigned int pending;
212
213   if (NULL != alh)
214   {
215       GNUNET_ATS_performance_list_addresses_cancel (alh);
216       alh = NULL;
217   }
218
219   GNUNET_ATS_performance_done (ph);
220   ph = NULL;
221
222   pending = 0;
223   next = head;
224   while (NULL != (pr = next))
225   {
226       next = pr->next;
227       GNUNET_CONTAINER_DLL_remove (head, tail, pr);
228       GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
229       GNUNET_free (pr->address);
230       GNUNET_free (pr);
231       pending ++;
232   }
233   if (0 < pending)
234     fprintf (stderr, _("%u address resolutions had a timeout\n"), pending);
235
236   fprintf (stderr, _("ATS returned results for %u addresses\n"), results);
237   ret = 0;
238 }
239
240 void testservice_ats (void *cls,
241                const struct GNUNET_SCHEDULER_TaskContext *tc)
242 {
243   struct GNUNET_PeerIdentity pid;
244   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
245   unsigned int c;
246   unsigned int type;
247
248   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
249   {
250       FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
251       return;
252   }
253
254   results = 0;
255
256   if (NULL != pid_str)
257   {
258       if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (pid_str, &pid.hashPubKey))
259       {
260         FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), pid_str);
261         return;
262       }
263       if (NULL == type_str)
264       {
265         FPRINTF (stderr, "%s", _("Type required\n"));
266         return;
267       }
268   }
269
270   c = op_list_all + op_list_used + op_monitor + op_set_pref;
271   if ((1 < c))
272   {
273       FPRINTF (stderr, _("Please select one operation : %s or %s or %s or %s\n"),
274                "--used", "--all", "--monitor", "--preference");
275       return;
276   }
277   if ((0 == c))
278     op_list_used = GNUNET_YES; /* set default */
279
280
281     if (op_list_all)
282     {
283         ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
284         if (NULL == ph)
285         {
286           fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
287           return;
288         }
289
290         alh = GNUNET_ATS_performance_list_addresses (ph,
291                                                (NULL == pid_str) ? NULL : &pid,
292                                                GNUNET_YES, la_cb, NULL);
293         if (NULL == alh)
294         {
295           fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n"));
296           end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
297           return;
298         }
299
300         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
301
302     }
303     else if (op_list_used)
304     {
305         ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
306         if (NULL == ph)
307           fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
308
309         alh = GNUNET_ATS_performance_list_addresses (ph,
310                                                (NULL == pid_str) ? NULL : &pid,
311                                                GNUNET_NO, la_cb, NULL);
312         if (NULL == alh)
313         {
314           fprintf (stderr, _("Cannot issue request to ATS service, exiting...\n"));
315           end_task = GNUNET_SCHEDULER_add_now (&end, NULL);
316           return;
317         }
318
319
320         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
321     }
322     else if (op_monitor)
323     {
324         ph = GNUNET_ATS_performance_init (cfg, ats_perf_cb, NULL);
325         if (NULL == ph)
326           fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
327         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
328
329     }
330     else if (op_set_pref)
331     {
332         for (c = 0; c<strlen(type_str); c++)
333         {
334           if (isupper (type_str[c]))
335             type_str[c] = tolower (type_str[c]);
336         }
337
338         if (0 == strcasecmp("latency", type_str))
339           type = GNUNET_ATS_PREFERENCE_LATENCY;
340         else if (0 == strcasecmp("bandwidth", type_str))
341           type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
342         else
343         {
344           FPRINTF (stderr, "%s", _("Type required\n"));
345           return;
346         }
347
348             /* set */
349             ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
350             if (NULL == ph)
351               fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
352
353             GNUNET_ATS_change_preference (ph, &pid, type, (double) value, GNUNET_ATS_PREFERENCE_END);
354
355             end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
356     }
357   ret = 1;
358 }
359
360 /**
361  * Main function that will be run by the scheduler.
362  *
363  * @param cls closure
364  * @param args remaining command-line arguments
365  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
366  * @param my_cfg configuration
367  */
368 static void
369 run (void *cls, char *const *args, const char *cfgfile,
370      const struct GNUNET_CONFIGURATION_Handle *my_cfg)
371 {
372   cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
373   GNUNET_CLIENT_service_test ("ats", cfg,
374                               TIMEOUT,
375                               &testservice_ats,
376                               (void *) cfg);
377 }
378
379
380 /**
381  * The main function.
382  *
383  * @param argc number of arguments from the command line
384  * @param argv command line arguments
385  * @return 0 ok, 1 on error
386  */
387 int
388 main (int argc, char *const *argv)
389 {
390   int res;
391   resolve_addresses_numeric = GNUNET_NO;
392   op_monitor = GNUNET_NO;
393   op_list_all = GNUNET_NO;
394   op_list_used = GNUNET_NO;
395
396   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
397       {'u', "used", NULL,
398        gettext_noop ("get list of active addresses currently used"),
399        0, &GNUNET_GETOPT_set_one, &op_list_used},
400        {'a', "all", NULL,
401         gettext_noop ("get list of all active addresses"),
402         0, &GNUNET_GETOPT_set_one, &op_list_all},
403       {'n', "numeric", NULL,
404        gettext_noop ("do not resolve IP addresses to hostnames"),
405        0, &GNUNET_GETOPT_set_one, &resolve_addresses_numeric},
406        {'m', "monitor", NULL,
407         gettext_noop ("monitor mode"),
408         0, &GNUNET_GETOPT_set_one, &op_monitor},
409        {'p', "preference", NULL,
410          gettext_noop ("set preference for the given peer"),
411          0, &GNUNET_GETOPT_set_one, &op_set_pref},
412        {'i', "id", "TYPE",
413          gettext_noop ("peer id"),
414          1, &GNUNET_GETOPT_set_string, &pid_str},
415        {'t', "type", "TYPE",
416          gettext_noop ("preference type to set: latency | bandwidth"),
417          1, &GNUNET_GETOPT_set_string, &type_str},
418        {'k', "value", "VALUE",
419          gettext_noop ("preference value"),
420          1, &GNUNET_GETOPT_set_uint, &value},
421        {'V', "verbose", NULL,
422         gettext_noop ("verbose output (include ATS address properties)"),
423         0, &GNUNET_GETOPT_set_one, &verbose},
424     GNUNET_GETOPT_OPTION_END
425   };
426
427   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
428     return 2;
429
430   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
431                               gettext_noop ("Print information about ATS state"), options, &run,
432                               NULL);
433   GNUNET_free_non_null (pid_str);
434   GNUNET_free_non_null (type_str);
435   GNUNET_free ((void *) argv);
436
437   if (GNUNET_OK == res)
438     return ret;
439   else
440     return 1;
441
442 }
443
444 /* end of gnunet-ats.c */