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