2 This file is part of GNUnet.
3 (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file transport/transport_api_address_lookup.c
23 * @brief given a peer id, get all known addresses from transport service
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).
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"
43 * Context for the address lookup.
45 struct GNUNET_TRANSPORT_PeerIterateContext
48 * Function to call with the binary address.
50 GNUNET_TRANSPORT_PeerIterateCallback cb;
58 * Connection to the service.
60 struct GNUNET_CLIENT_Connection *client;
63 * Configuration we use.
65 const struct GNUNET_CONFIGURATION_Handle *cfg;
68 * When should this operation time out?
70 struct GNUNET_TIME_Absolute timeout;
73 * Backoff for reconnect.
75 struct GNUNET_TIME_Relative backoff;
78 * Task ID for reconnect.
80 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
83 * Identity of the peer to monitor.
85 struct GNUNET_PeerIdentity peer;
88 * Was this a one-shot request?
95 * Function called with responses from the service.
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
102 peer_address_response_processor (void *cls,
103 const struct GNUNET_MessageHeader *msg);
107 * Send our subscription request to the service.
109 * @param pal_ctx our context
112 send_request (struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx)
114 struct AddressIterateMessage msg;
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,
124 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout),
126 &peer_address_response_processor,
131 * Task run to re-establish the connection.
133 * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
134 * @param tc scheduler context, unused
137 do_connect (void *cls,
138 const struct GNUNET_SCHEDULER_TaskContext *tc)
140 struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
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);
150 * Cut the existing connection and reconnect.
152 * @param pal_ctx our context
155 reconnect (struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx)
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,
168 * Function called with responses from the service.
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
175 peer_address_response_processor (void *cls,
176 const struct GNUNET_MessageHeader *msg)
178 struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
179 struct AddressIterateResponseMessage *air_msg;
180 struct GNUNET_HELLO_Address *address;
182 const char *transport_name;
189 if (pal_ctx->one_shot)
191 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
192 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
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))
206 if (pal_ctx->one_shot)
208 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
209 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
218 if ((size < sizeof (struct AddressIterateResponseMessage)) ||
219 (ntohs (msg->type) !=
220 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE))
223 if (pal_ctx->one_shot)
225 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
226 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
235 air_msg = (struct AddressIterateResponseMessage *) msg;
236 tlen = ntohl (air_msg->pluginlen);
237 alen = ntohl (air_msg->addrlen);
239 if (size != sizeof (struct AddressIterateResponseMessage) + tlen + alen)
242 if (pal_ctx->one_shot)
244 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
245 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
254 if (alen == 0 && tlen == 0)
256 pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, NULL);
260 addr = (const char *) &air_msg[1];
261 transport_name = &addr[alen];
263 if (transport_name[tlen - 1] != '\0')
266 if (pal_ctx->one_shot)
268 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
269 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
280 GNUNET_HELLO_address_allocate (&air_msg->peer, transport_name, addr,
282 pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, address);
283 GNUNET_HELLO_address_free (address);
286 /* expect more replies */
287 GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor,
289 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
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
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.
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
310 struct GNUNET_TRANSPORT_PeerIterateContext *
311 GNUNET_TRANSPORT_peer_get_active_addresses (const struct
312 GNUNET_CONFIGURATION_Handle *cfg,
313 const struct GNUNET_PeerIdentity
315 struct GNUNET_TIME_Relative timeout,
316 GNUNET_TRANSPORT_PeerIterateCallback
317 peer_address_callback,
318 void *peer_address_callback_cls)
320 struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx;
321 struct GNUNET_CLIENT_Connection *client;
323 client = GNUNET_CLIENT_connect ("transport", cfg);
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;
332 pal_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
334 pal_ctx->peer = *peer;
335 pal_ctx->one_shot = one_shot;
336 pal_ctx->client = client;
337 send_request (pal_ctx);
344 * Cancel request for address conversion.
346 * @param alc handle for the request to cancel
349 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
350 GNUNET_TRANSPORT_PeerIterateContext
353 if (NULL != alc->client)
355 GNUNET_CLIENT_disconnect (alc->client);
358 if (GNUNET_SCHEDULER_NO_TASK != alc->reconnect_task)
360 GNUNET_SCHEDULER_cancel (alc->reconnect_task);
361 alc->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
367 /* end of transport_api_peer_address_lookup.c */