error msg
[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_STD_BACKOFF (pal_ctx->backoff);
161   pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (pal_ctx->backoff,
162                                                           &do_connect,
163                                                           pal_ctx);
164 }
165
166
167 /**
168  * Function called with responses from the service.
169  *
170  * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
171  * @param msg NULL on timeout or error, otherwise presumably a
172  *        message with the human-readable address
173  */
174 static void
175 peer_address_response_processor (void *cls,
176                                  const struct GNUNET_MessageHeader *msg)
177 {
178   struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
179   struct AddressIterateResponseMessage *air_msg;
180   struct GNUNET_HELLO_Address *address;
181   const char *addr;
182   const char *transport_name;
183   uint16_t size;
184   size_t alen;
185   size_t tlen;
186
187   if (msg == NULL)
188   {
189     if (pal_ctx->one_shot)
190     {
191       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
192       GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
193     }
194     else
195     {
196       reconnect (pal_ctx);
197     }
198     return;
199   }
200   size = ntohs (msg->size);
201   GNUNET_break (ntohs (msg->type) ==
202                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
203   if (size == sizeof (struct GNUNET_MessageHeader))
204   {
205     /* done! */
206     if (pal_ctx->one_shot)
207     {
208       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
209       GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
210     }
211     else
212     {
213       reconnect (pal_ctx);
214     }
215     return;
216   }
217
218   if ((size < sizeof (struct AddressIterateResponseMessage)) ||
219       (ntohs (msg->type) !=
220        GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE))
221   {
222     GNUNET_break (0);
223     if (pal_ctx->one_shot)
224     {
225       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
226       GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
227     }
228     else
229     {
230       reconnect (pal_ctx);
231     }
232     return;
233   }
234
235   air_msg = (struct AddressIterateResponseMessage *) msg;
236   tlen = ntohl (air_msg->pluginlen);
237   alen = ntohl (air_msg->addrlen);
238
239   if (size != sizeof (struct AddressIterateResponseMessage) + tlen + alen)
240   {
241     GNUNET_break (0);
242     if (pal_ctx->one_shot)
243     {
244       pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
245       GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
246     }
247     else
248     {
249       reconnect (pal_ctx);
250     }
251     return;
252   }
253
254   if (alen == 0 && tlen == 0)
255   {
256     pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, NULL);
257   }
258   else
259   {
260     addr = (const char *) &air_msg[1];
261     transport_name = &addr[alen];
262
263     if (transport_name[tlen - 1] != '\0')
264     {
265       GNUNET_break (0);
266       if (pal_ctx->one_shot)    
267       {
268         pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
269         GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
270       }
271       else
272       {
273         reconnect (pal_ctx);
274       }
275       return;
276     }
277
278     /* notify client */
279     address =
280         GNUNET_HELLO_address_allocate (&air_msg->peer, transport_name, addr,
281                                        alen);
282     pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, address);
283     GNUNET_HELLO_address_free (address);
284   }
285
286   /* expect more replies */
287   GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor,
288                          pal_ctx,
289                          GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
290 }
291
292
293 /**
294  * Return all the known addresses for a specific peer or all peers.
295  * Returns continuously all address if one_shot is set to GNUNET_NO
296  *
297  * CHANGE: Returns the address(es) that we are currently using for this
298  * peer.  Upon completion, the 'AddressLookUpCallback' is called one more
299  * time with 'NULL' for the address and the peer.  After this, the operation must no
300  * longer be explicitly canceled.
301  *
302  * @param cfg configuration to use
303  * @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers
304  * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
305  *                 GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly canceled)
306  * @param timeout how long is the lookup allowed to take at most (irrelevant if one_shot is set to GNUNET_NO)
307  * @param peer_address_callback function to call with the results
308  * @param peer_address_callback_cls closure for peer_address_callback
309  */
310 struct GNUNET_TRANSPORT_PeerIterateContext *
311 GNUNET_TRANSPORT_peer_get_active_addresses (const struct
312                                             GNUNET_CONFIGURATION_Handle *cfg,
313                                             const struct GNUNET_PeerIdentity
314                                             *peer, int one_shot,
315                                             struct GNUNET_TIME_Relative timeout,
316                                             GNUNET_TRANSPORT_PeerIterateCallback
317                                             peer_address_callback,
318                                             void *peer_address_callback_cls)
319 {
320   struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx;
321   struct GNUNET_CLIENT_Connection *client;
322
323   client = GNUNET_CLIENT_connect ("transport", cfg);
324   if (client == NULL)
325     return NULL;
326   if (GNUNET_YES != one_shot)
327     timeout = GNUNET_TIME_UNIT_FOREVER_REL;
328   pal_ctx = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerIterateContext));
329   pal_ctx->cb = peer_address_callback;
330   pal_ctx->cb_cls = peer_address_callback_cls;
331   pal_ctx->cfg = cfg;
332   pal_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
333   if (NULL != peer)
334     pal_ctx->peer = *peer;
335   pal_ctx->one_shot = one_shot;  
336   pal_ctx->client = client;
337   send_request (pal_ctx);
338
339   return pal_ctx;
340 }
341
342
343 /**
344  * Cancel request for address conversion.
345  *
346  * @param alc handle for the request to cancel
347  */
348 void
349 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
350                                                    GNUNET_TRANSPORT_PeerIterateContext
351                                                    *alc)
352 {
353   if (NULL != alc->client)
354   {
355     GNUNET_CLIENT_disconnect (alc->client);
356     alc->client = NULL;
357   }
358   if (GNUNET_SCHEDULER_NO_TASK != alc->reconnect_task)
359   {
360     GNUNET_SCHEDULER_cancel (alc->reconnect_task);
361     alc->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
362   }
363   GNUNET_free (alc);
364 }
365
366
367 /* end of transport_api_peer_address_lookup.c */