new operation queue for limiting overlay connects
[oweals/gnunet.git] / src / transport / transport_api_address_lookup.c
1 /*
2      This file is part of GNUnet.
3      (C) 2009, 2010 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 transport/transport_api_address_lookup.c
23  * @brief given a peer id, get all known addresses from transport service
24  *
25  * This api provides the ability to query the transport service about
26  * the status of connections to a specific peer.  Calls back with a
27  * pretty printed string of the address, as formatted by the appropriate
28  * transport plugin, and whether or not the address given is currently
29  * in the 'connected' state (according to the transport service).
30  */
31
32 #include "platform.h"
33 #include "gnunet_client_lib.h"
34 #include "gnunet_arm_service.h"
35 #include "gnunet_hello_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_server_lib.h"
38 #include "gnunet_time_lib.h"
39 #include "gnunet_transport_service.h"
40 #include "transport.h"
41
42 /**
43  * Context for the address lookup.
44  */
45 struct GNUNET_TRANSPORT_PeerIterateContext
46 {
47   /**
48    * Function to call with the binary address.
49    */
50   GNUNET_TRANSPORT_PeerIterateCallback cb;
51
52   /**
53    * Closure for cb.
54    */
55   void *cb_cls;
56
57   /**
58    * Connection to the service.
59    */
60   struct GNUNET_CLIENT_Connection *client;
61
62   /**
63    * Configuration we use.
64    */
65   const struct GNUNET_CONFIGURATION_Handle *cfg;
66
67   /**
68    * When should this operation time out?
69    */
70   struct GNUNET_TIME_Absolute timeout;
71
72   /**
73    * Backoff for reconnect.
74    */
75   struct GNUNET_TIME_Relative backoff;
76   
77   /**
78    * Task ID for reconnect.
79    */
80   GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
81
82   /**
83    * Identity of the peer to monitor.
84    */
85   struct GNUNET_PeerIdentity peer;
86
87   /**
88    * Was this a one-shot request?
89    */
90   int one_shot;
91 };
92
93
94 /**
95  * Function called with responses from the service.
96  *
97  * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
98  * @param msg NULL on timeout or error, otherwise presumably a
99  *        message with the human-readable address
100  */
101 static void
102 peer_address_response_processor (void *cls,
103                                  const struct GNUNET_MessageHeader *msg);
104
105
106 /**
107  * Send our subscription request to the service.
108  *
109  * @param pal_ctx our context
110  */
111 static void
112 send_request (struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx)
113 {
114   struct AddressIterateMessage msg;
115
116   msg.header.size = htons (sizeof (struct AddressIterateMessage));
117   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
118   msg.one_shot = htonl (pal_ctx->one_shot);
119   msg.timeout = GNUNET_TIME_absolute_hton (pal_ctx->timeout);
120   msg.peer = pal_ctx->peer;
121   GNUNET_assert (GNUNET_OK ==
122                  GNUNET_CLIENT_transmit_and_get_response (pal_ctx->client, 
123                                                           &msg.header,
124                                                           GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout),
125                                                           GNUNET_YES,
126                                                           &peer_address_response_processor,
127                                                           pal_ctx));
128 }
129
130 /**
131  * Task run to re-establish the connection.
132  * 
133  * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
134  * @param tc scheduler context, unused
135  */
136 static void
137 do_connect (void *cls,
138             const struct GNUNET_SCHEDULER_TaskContext *tc)
139 {
140   struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
141
142   pal_ctx->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
143   pal_ctx->client = GNUNET_CLIENT_connect ("transport", pal_ctx->cfg);
144   GNUNET_assert (NULL != pal_ctx->client);
145   send_request (pal_ctx);
146 }
147
148
149 /**
150  * Cut the existing connection and reconnect.
151  *
152  * @param pal_ctx our context
153  */
154 static void
155 reconnect (struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx)
156 {
157   GNUNET_assert (GNUNET_NO == pal_ctx->one_shot);
158   GNUNET_CLIENT_disconnect (pal_ctx->client);
159   pal_ctx->client = NULL;
160   pal_ctx->backoff = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS,
161                                                GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (pal_ctx->backoff, 2),
162                                                                          GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)));
163   pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (pal_ctx->backoff,
164                                                           &do_connect,
165                                                           pal_ctx);
166 }
167
168
169 /**
170  * Function called with responses from the service.
171  *
172  * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
173  * @param msg NULL on timeout or error, otherwise presumably a
174  *        message with the human-readable address
175  */
176 static void
177 peer_address_response_processor (void *cls,
178                                  const struct GNUNET_MessageHeader *msg)
179 {
180   struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
181   struct AddressIterateResponseMessage *air_msg;
182   struct GNUNET_HELLO_Address *address;
183   const char *addr;
184   const char *transport_name;
185   uint16_t size;
186   size_t alen;
187   size_t tlen;
188
189   if (msg == NULL)
190   {
191     if (pal_ctx->one_shot)
192     {
193       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
194       GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
195     }
196     else
197     {
198       reconnect (pal_ctx);
199     }
200     return;
201   }
202   size = ntohs (msg->size);
203   GNUNET_break (ntohs (msg->type) ==
204                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
205   if (size == sizeof (struct GNUNET_MessageHeader))
206   {
207     /* done! */
208     if (pal_ctx->one_shot)
209     {
210       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
211       GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
212     }
213     else
214     {
215       reconnect (pal_ctx);
216     }
217     return;
218   }
219
220   if ((size < sizeof (struct AddressIterateResponseMessage)) ||
221       (ntohs (msg->type) !=
222        GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE))
223   {
224     GNUNET_break (0);
225     if (pal_ctx->one_shot)
226     {
227       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
228       GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
229     }
230     else
231     {
232       reconnect (pal_ctx);
233     }
234     return;
235   }
236
237   air_msg = (struct AddressIterateResponseMessage *) msg;
238   tlen = ntohl (air_msg->pluginlen);
239   alen = ntohl (air_msg->addrlen);
240
241   if (size != sizeof (struct AddressIterateResponseMessage) + tlen + alen)
242   {
243     GNUNET_break (0);
244     if (pal_ctx->one_shot)
245     {
246       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
247       GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
248     }
249     else
250     {
251       reconnect (pal_ctx);
252     }
253     return;
254   }
255
256   if (alen == 0 && tlen == 0)
257   {
258     pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, NULL);
259   }
260   else
261   {
262     addr = (const char *) &air_msg[1];
263     transport_name = &addr[alen];
264
265     if (transport_name[tlen - 1] != '\0')
266     {
267       GNUNET_break (0);
268       if (pal_ctx->one_shot)    
269       {
270         pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
271         GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
272       }
273       else
274       {
275         reconnect (pal_ctx);
276       }
277       return;
278     }
279
280     /* notify client */
281     address =
282         GNUNET_HELLO_address_allocate (&air_msg->peer, transport_name, addr,
283                                        alen);
284     pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, address);
285     GNUNET_HELLO_address_free (address);
286   }
287
288   /* expect more replies */
289   GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor,
290                          pal_ctx,
291                          GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
292 }
293
294
295 /**
296  * Return all the known addresses for a specific peer or all peers.
297  * Returns continuously all address if one_shot is set to GNUNET_NO
298  *
299  * CHANGE: Returns the address(es) that we are currently using for this
300  * peer.  Upon completion, the 'AddressLookUpCallback' is called one more
301  * time with 'NULL' for the address and the peer.  After this, the operation must no
302  * longer be explicitly canceled.
303  *
304  * @param cfg configuration to use
305  * @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers
306  * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
307  *                 GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly canceled)
308  * @param timeout how long is the lookup allowed to take at most (irrelevant if one_shot is set to GNUNET_NO)
309  * @param peer_address_callback function to call with the results
310  * @param peer_address_callback_cls closure for peer_address_callback
311  */
312 struct GNUNET_TRANSPORT_PeerIterateContext *
313 GNUNET_TRANSPORT_peer_get_active_addresses (const struct
314                                             GNUNET_CONFIGURATION_Handle *cfg,
315                                             const struct GNUNET_PeerIdentity
316                                             *peer, int one_shot,
317                                             struct GNUNET_TIME_Relative timeout,
318                                             GNUNET_TRANSPORT_PeerIterateCallback
319                                             peer_address_callback,
320                                             void *peer_address_callback_cls)
321 {
322   struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx;
323   struct GNUNET_CLIENT_Connection *client;
324
325   client = GNUNET_CLIENT_connect ("transport", cfg);
326   if (client == NULL)
327     return NULL;
328   if (GNUNET_YES != one_shot)
329     timeout = GNUNET_TIME_UNIT_FOREVER_REL;
330   pal_ctx = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerIterateContext));
331   pal_ctx->cb = peer_address_callback;
332   pal_ctx->cb_cls = peer_address_callback_cls;
333   pal_ctx->cfg = cfg;
334   pal_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
335   if (NULL != peer)
336     pal_ctx->peer = *peer;
337   pal_ctx->one_shot = one_shot;  
338   pal_ctx->client = client;
339   send_request (pal_ctx);
340
341   return pal_ctx;
342 }
343
344
345 /**
346  * Cancel request for address conversion.
347  *
348  * @param alc handle for the request to cancel
349  */
350 void
351 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
352                                                    GNUNET_TRANSPORT_PeerIterateContext
353                                                    *alc)
354 {
355   if (NULL != alc->client)
356   {
357     GNUNET_CLIENT_disconnect (alc->client);
358     alc->client = NULL;
359   }
360   if (GNUNET_SCHEDULER_NO_TASK != alc->reconnect_task)
361   {
362     GNUNET_SCHEDULER_cancel (alc->reconnect_task);
363     alc->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
364   }
365   GNUNET_free (alc);
366 }
367
368
369 /* end of transport_api_peer_address_lookup.c */