- change peer preferences via cmd line
[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 static struct GNUNET_ATS_PerformanceHandle *ph;
50
51 static struct GNUNET_CONFIGURATION_Handle *cfg;
52
53 GNUNET_SCHEDULER_TaskIdentifier end_task;
54
55 struct PendingResolutions
56 {
57   struct PendingResolutions *next;
58   struct PendingResolutions *prev;
59
60   struct GNUNET_HELLO_Address *address;
61   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
62   struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
63
64   struct GNUNET_TRANSPORT_AddressToStringContext * tats_ctx;
65 };
66
67 struct PendingResolutions *head;
68 struct PendingResolutions *tail;
69
70 void transport_addr_to_str_cb (void *cls, const char *address)
71 {
72   struct PendingResolutions * pr = cls;
73   if (NULL != address)
74   {
75       fprintf (stderr, _("Peer `%s' plugin `%s', address `%s', bandwidth out: %u Bytes/s, bandwidth in %u Bytes/s\n"),
76         GNUNET_i2s (&pr->address->peer), pr->address->transport_name, address,
77         ntohl (pr->bandwidth_out.value__), ntohl (pr->bandwidth_in.value__));
78   }
79   else if (NULL != pr)
80   {
81       /* We're done */
82       GNUNET_CONTAINER_DLL_remove (head, tail, pr);
83       GNUNET_free (pr->address);
84       GNUNET_free (pr);
85   }
86
87 }
88
89 void ats_perf_cb (void *cls,
90                   const struct
91                   GNUNET_HELLO_Address *
92                   address,
93                   struct
94                   GNUNET_BANDWIDTH_Value32NBO
95                   bandwidth_out,
96                   struct
97                   GNUNET_BANDWIDTH_Value32NBO
98                   bandwidth_in,
99                   const struct
100                   GNUNET_ATS_Information *
101                   ats, uint32_t ats_count)
102 {
103   struct PendingResolutions * pr;
104
105   pr = GNUNET_malloc (sizeof (struct PendingResolutions));
106   pr->address = GNUNET_HELLO_address_copy (address);
107   pr->bandwidth_in = bandwidth_in;
108   pr->bandwidth_out = bandwidth_out;
109   pr->tats_ctx = GNUNET_TRANSPORT_address_to_string(cfg, address,
110                     resolve_addresses_numeric, GNUNET_TIME_UNIT_FOREVER_REL, transport_addr_to_str_cb, pr);
111   GNUNET_CONTAINER_DLL_insert (head, tail, pr);
112   results++;
113 }
114
115 void end (void *cls,
116           const struct GNUNET_SCHEDULER_TaskContext *tc)
117 {
118   struct PendingResolutions * pr;
119   struct PendingResolutions * next;
120   unsigned int pending;
121
122   GNUNET_ATS_performance_done (ph);
123   ph = NULL;
124
125   pending = 0;
126   next = head;
127   while (NULL != (pr = next))
128   {
129       next = pr->next;
130       GNUNET_CONTAINER_DLL_remove (head, tail, pr);
131       GNUNET_TRANSPORT_address_to_string_cancel (pr->tats_ctx);
132       GNUNET_free (pr->address);
133       GNUNET_free (pr);
134       pending ++;
135   }
136   if (0 < pending)
137     fprintf (stderr, _("%u address resolutions had a timeout\n"), pending);
138
139   fprintf (stderr, _("ATS returned results for %u addresses\n"), results);
140   ret = 0;
141 }
142
143 void testservice_ats (void *cls,
144                const struct GNUNET_SCHEDULER_TaskContext *tc)
145 {
146   struct GNUNET_PeerIdentity pid;
147   struct GNUNET_CONFIGURATION_Handle *cfg = cls;
148   unsigned int c;
149   unsigned int type;
150
151   if (0 != (tc->reason & GNUNET_SCHEDULER_REASON_TIMEOUT))
152   {
153       FPRINTF (stderr, _("Service `%s' is not running\n"), "ats");
154       return;
155   }
156
157   results = 0;
158   if (NULL != pid_str)
159   {
160       if (GNUNET_OK != GNUNET_CRYPTO_hash_from_string (pid_str, &pid.hashPubKey))
161       {
162         FPRINTF (stderr, _("Failed to parse peer identity `%s'\n"), pid_str);
163         return;
164       }
165       if (NULL == type_str)
166       {
167         FPRINTF (stderr, _("Type required\n"));
168         return;
169       }
170
171       for (c = 0; c<strlen(type_str); c++)
172         if (isupper (type_str[c]))
173           type_str[c] = tolower (type_str[c]);
174
175
176       if (0 == strcasecmp("latency", type_str))
177         type = GNUNET_ATS_PREFERENCE_LATENCY;
178       else if (0 == strcasecmp("bandwidth", type_str))
179         type = GNUNET_ATS_PREFERENCE_BANDWIDTH;
180       else
181       {
182         FPRINTF (stderr, _("Type required\n"));
183         return;
184       }
185
186       /* set */
187       ph = GNUNET_ATS_performance_init (cfg, NULL, NULL);
188       if (NULL == ph)
189         fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
190
191       GNUNET_ATS_change_preference (ph, &pid, type, (double) value, GNUNET_ATS_PREFERENCE_END);
192
193       end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_SECONDS, &end, NULL);
194   }
195   else
196   {
197       ph = GNUNET_ATS_performance_init (cfg, ats_perf_cb, NULL);
198       if (NULL == ph)
199         fprintf (stderr, _("Cannot connect to ATS service, exiting...\n"));
200
201       if (GNUNET_NO == monitor)
202         end_task = GNUNET_SCHEDULER_add_delayed (TIMEOUT, &end, NULL);
203       else
204         end_task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_UNIT_FOREVER_REL, &end, NULL);
205   }
206
207
208   ret = 1;
209 }
210
211 /**
212  * Main function that will be run by the scheduler.
213  *
214  * @param cls closure
215  * @param args remaining command-line arguments
216  * @param cfgfile name of the configuration file used (for saving, can be NULL!)
217  * @param my_cfg configuration
218  */
219 static void
220 run (void *cls, char *const *args, const char *cfgfile,
221      const struct GNUNET_CONFIGURATION_Handle *my_cfg)
222 {
223   cfg = (struct GNUNET_CONFIGURATION_Handle *) my_cfg;
224   GNUNET_CLIENT_service_test ("ats", cfg,
225                               TIMEOUT,
226                               &testservice_ats,
227                               (void *) cfg);
228 }
229
230
231 /**
232  * The main function.
233  *
234  * @param argc number of arguments from the command line
235  * @param argv command line arguments
236  * @return 0 ok, 1 on error
237  */
238 int
239 main (int argc, char *const *argv)
240 {
241   int res;
242   resolve_addresses_numeric = GNUNET_NO;
243   monitor = GNUNET_NO;
244
245   static const struct GNUNET_GETOPT_CommandLineOption options[] = {
246       {'n', "numeric", NULL,
247        gettext_noop ("do not resolve IP addresses to hostnames"),
248        0, &GNUNET_GETOPT_set_one, &resolve_addresses_numeric},
249        {'m', "monitor", NULL,
250         gettext_noop ("monitor mode"),
251         0, &GNUNET_GETOPT_set_one, &monitor},
252        {'p', "preference", "PEER",
253          gettext_noop ("set preference for the given peer"),
254          1, &GNUNET_GETOPT_set_string, &pid_str},
255        {'t', "type", "TYPE",
256          gettext_noop ("preference type to set: latency | bandwidth"),
257          1, &GNUNET_GETOPT_set_string, &type_str},
258          {'k', "value", "VALUE",
259            gettext_noop ("preference value"),
260            1, &GNUNET_GETOPT_set_uint, &value},
261     GNUNET_GETOPT_OPTION_END
262   };
263
264   if (GNUNET_OK != GNUNET_STRINGS_get_utf8_args (argc, argv, &argc, &argv))
265     return 2;
266
267   res = GNUNET_PROGRAM_run (argc, argv, "gnunet-ats",
268                               gettext_noop ("Print information about ATS state"), options, &run,
269                               NULL);
270   GNUNET_free_non_null (pid_str);
271   GNUNET_free_non_null (type_str);
272   GNUNET_free ((void *) argv);
273
274   if (GNUNET_OK == res)
275     return ret;
276   else
277     return 1;
278
279 }
280
281 /* end of gnunet-ats.c */