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_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,
170 * Function called with responses from the service.
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
177 peer_address_response_processor (void *cls,
178 const struct GNUNET_MessageHeader *msg)
180 struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
181 struct AddressIterateResponseMessage *air_msg;
182 struct GNUNET_HELLO_Address *address;
184 const char *transport_name;
191 if (pal_ctx->one_shot)
193 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
194 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
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))
208 if (pal_ctx->one_shot)
210 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
211 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
220 if ((size < sizeof (struct AddressIterateResponseMessage)) ||
221 (ntohs (msg->type) !=
222 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE))
225 if (pal_ctx->one_shot)
227 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
228 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
237 air_msg = (struct AddressIterateResponseMessage *) msg;
238 tlen = ntohl (air_msg->pluginlen);
239 alen = ntohl (air_msg->addrlen);
241 if (size != sizeof (struct AddressIterateResponseMessage) + tlen + alen)
244 if (pal_ctx->one_shot)
246 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
247 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
256 if (alen == 0 && tlen == 0)
258 pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, NULL);
262 addr = (const char *) &air_msg[1];
263 transport_name = &addr[alen];
265 if (transport_name[tlen - 1] != '\0')
268 if (pal_ctx->one_shot)
270 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
271 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
282 GNUNET_HELLO_address_allocate (&air_msg->peer, transport_name, addr,
284 pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, address);
285 GNUNET_HELLO_address_free (address);
288 /* expect more replies */
289 GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor,
291 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
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
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.
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
312 struct GNUNET_TRANSPORT_PeerIterateContext *
313 GNUNET_TRANSPORT_peer_get_active_addresses (const struct
314 GNUNET_CONFIGURATION_Handle *cfg,
315 const struct GNUNET_PeerIdentity
317 struct GNUNET_TIME_Relative timeout,
318 GNUNET_TRANSPORT_PeerIterateCallback
319 peer_address_callback,
320 void *peer_address_callback_cls)
322 struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx;
323 struct GNUNET_CLIENT_Connection *client;
325 client = GNUNET_CLIENT_connect ("transport", cfg);
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;
334 pal_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
336 pal_ctx->peer = *peer;
337 pal_ctx->one_shot = one_shot;
338 pal_ctx->client = client;
339 send_request (pal_ctx);
346 * Cancel request for address conversion.
348 * @param alc handle for the request to cancel
351 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
352 GNUNET_TRANSPORT_PeerIterateContext
355 if (NULL != alc->client)
357 GNUNET_CLIENT_disconnect (alc->client);
360 if (GNUNET_SCHEDULER_NO_TASK != alc->reconnect_task)
362 GNUNET_SCHEDULER_cancel (alc->reconnect_task);
363 alc->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
369 /* end of transport_api_peer_address_lookup.c */