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_peer_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 * When should this operation time out?
65 struct GNUNET_TIME_Absolute timeout;
70 * Function called with responses from the service.
72 * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
73 * @param msg NULL on timeout or error, otherwise presumably a
74 * message with the human-readable address
77 peer_address_response_processor (void *cls,
78 const struct GNUNET_MessageHeader *msg)
80 struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
81 struct AddressIterateResponseMessage *air_msg;
82 struct GNUNET_HELLO_Address *address;
84 const char *transport_name;
91 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
92 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
95 size = ntohs (msg->size);
96 GNUNET_break (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
97 if (size == sizeof (struct GNUNET_MessageHeader))
100 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
101 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
105 if ( (size < sizeof (struct GNUNET_MessageHeader) + sizeof (struct AddressIterateResponseMessage)) ||
106 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE) )
109 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
110 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
114 air_msg = (struct AddressIterateResponseMessage *) msg;
115 tlen = ntohl(air_msg->pluginlen);
116 alen = ntohl(air_msg->addrlen);
118 if (size != sizeof (struct AddressIterateResponseMessage) + tlen + alen)
121 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
122 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
126 addr = (const char *) &air_msg[1];
127 transport_name = &addr[alen];
129 if (transport_name[tlen-1] != '\0')
132 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
133 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
137 /* expect more replies */
138 GNUNET_CLIENT_receive (pal_ctx->client,
139 &peer_address_response_processor, pal_ctx,
140 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
143 address = GNUNET_HELLO_address_allocate (&air_msg->peer,
144 transport_name, addr, alen);
145 pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, address);
146 GNUNET_HELLO_address_free (address);
151 * Return all the known addresses for a specific peer or all peers.
152 * Returns continously all address if one_shot is set to GNUNET_NO
154 * CHANGE: Returns the address(es) that we are currently using for this
155 * peer. Upon completion, the 'AddressLookUpCallback' is called one more
156 * time with 'NULL' for the address and the peer. After this, the operation must no
157 * longer be explicitly cancelled.
159 * @param cfg configuration to use
160 * @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers
161 * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
162 * GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly cancelled)
163 * @param timeout how long is the lookup allowed to take at most
164 * @param peer_address_callback function to call with the results
165 * @param peer_address_callback_cls closure for peer_address_callback
167 struct GNUNET_TRANSPORT_PeerIterateContext *
168 GNUNET_TRANSPORT_peer_get_active_addresses (const struct GNUNET_CONFIGURATION_Handle *cfg,
169 const struct GNUNET_PeerIdentity *peer,
171 struct GNUNET_TIME_Relative timeout,
172 GNUNET_TRANSPORT_PeerIterateCallback peer_address_callback,
173 void *peer_address_callback_cls)
175 struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx;
176 struct AddressIterateMessage msg;
177 struct GNUNET_CLIENT_Connection *client;
178 struct GNUNET_TIME_Absolute abs_timeout;
180 if (GNUNET_YES != one_shot)
182 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
183 "Address monitoring not implemented\n");
186 client = GNUNET_CLIENT_connect ("transport", cfg);
189 abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
190 msg.header.size = htons (sizeof (struct AddressIterateMessage));
191 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
192 msg.one_shot = htonl (one_shot);
193 msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
195 memset (&msg.peer, 0 , sizeof (struct GNUNET_PeerIdentity));
198 pal_ctx = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerIterateContext));
199 pal_ctx->cb = peer_address_callback;
200 pal_ctx->cb_cls = peer_address_callback_cls;
201 pal_ctx->timeout = abs_timeout;
202 pal_ctx->client = client;
203 GNUNET_assert (GNUNET_OK ==
204 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
206 &peer_address_response_processor,
213 * Cancel request for address conversion.
215 * @param alc handle for the request to cancel
218 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
219 GNUNET_TRANSPORT_PeerIterateContext
222 GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO);
227 /* end of transport_api_peer_address_lookup.c */