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