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_PeerAddressLookupContext
48 * Function to call with the human-readable address.
50 GNUNET_TRANSPORT_AddressLookUpCallback 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;
69 * Function called with responses from the service.
71 * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
72 * @param msg NULL on timeout or error, otherwise presumably a
73 * message with the human-readable address
76 peer_address_response_processor (void *cls,
77 const struct GNUNET_MessageHeader *msg)
79 struct GNUNET_TRANSPORT_PeerAddressLookupContext *pal_ctx = cls;
80 struct AddressIterateResponseMessage *air_msg = (struct AddressIterateResponseMessage *) &msg[1];
81 const struct GNUNET_HELLO_Address *address;
85 pal_ctx->cb (pal_ctx->cb_cls, NULL);
86 GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
87 GNUNET_free (pal_ctx);
90 size = ntohs (msg->size);
91 GNUNET_break (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
92 if (size == sizeof (struct GNUNET_MessageHeader))
95 pal_ctx->cb (pal_ctx->cb_cls, NULL );
96 GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
97 GNUNET_free (pal_ctx);
101 size_t tlen = ntohl(air_msg->pluginlen);
102 size_t alen = ntohl(air_msg->addrlen);
104 if (size != sizeof (struct GNUNET_MessageHeader) + sizeof (struct AddressIterateResponseMessage) + tlen + alen)
107 pal_ctx->cb (pal_ctx->cb_cls, NULL );
108 GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
109 GNUNET_free (pal_ctx);
113 char * addr = (char *) &air_msg[1];
114 char * transport_name = &addr[alen];
116 if (transport_name[tlen] != '\0')
119 pal_ctx->cb (pal_ctx->cb_cls, NULL );
120 GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
121 GNUNET_free (pal_ctx);
125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "peer %s transport_name: %s\n",GNUNET_i2s(&air_msg->peer), transport_name);
127 address = GNUNET_HELLO_address_allocate(&air_msg->peer, transport_name, addr, alen);
129 /* expect more replies */
130 GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor, pal_ctx,
131 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
133 pal_ctx->cb (pal_ctx->cb_cls, address);
138 * Return all the known addresses for a peer.
140 * @param cfg configuration to use
141 * @param peer peer identity to look up the addresses of
142 * @param timeout how long is the lookup allowed to take at most
143 * @param peer_address_callback function to call with the results
144 * @param peer_address_callback_cls closure for peer_address_callback
145 * @return handle to cancel the operation, NULL on error
147 struct GNUNET_TRANSPORT_PeerAddressLookupContext *
148 GNUNET_TRANSPORT_peer_get_active_addresses (const struct GNUNET_CONFIGURATION_Handle *cfg,
149 const struct GNUNET_PeerIdentity *peer,
151 struct GNUNET_TIME_Relative timeout,
152 GNUNET_TRANSPORT_AddressLookUpCallback peer_address_callback,
153 void *peer_address_callback_cls)
155 struct GNUNET_TRANSPORT_PeerAddressLookupContext *pal_ctx;
156 struct AddressIterateMessage msg;
157 struct GNUNET_CLIENT_Connection *client;
158 struct GNUNET_TIME_Absolute abs_timeout;
160 client = GNUNET_CLIENT_connect ("transport", cfg);
163 peer_address_callback (peer_address_callback_cls, NULL);
167 abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
169 msg.header.size = htons (sizeof (struct AddressIterateMessage));
170 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
171 msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
173 memset (&msg.peer, 0 , sizeof (struct GNUNET_PeerIdentity));
175 memcpy (&msg.peer, peer , sizeof (struct GNUNET_PeerIdentity));
177 pal_ctx = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext));
178 pal_ctx->cb = peer_address_callback;
179 pal_ctx->cb_cls = peer_address_callback_cls;
180 pal_ctx->timeout = abs_timeout;
181 pal_ctx->client = client;
182 GNUNET_assert (GNUNET_OK ==
183 GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
185 &peer_address_response_processor,
192 * Cancel request for address conversion.
194 * @param alc handle for the request to cancel
197 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
198 GNUNET_TRANSPORT_PeerAddressLookupContext
201 GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO);
206 * Return all the known addresses for all peers.
208 * @param cfg configuration to use
209 * @param timeout how long is the lookup allowed to take at most
210 * @param peer_address_callback function to call with the results
211 * @param peer_address_callback_cls closure for peer_address_callback
214 GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
215 struct GNUNET_TIME_Relative timeout,
216 GNUNET_TRANSPORT_AddressLookUpCallback
217 peer_address_callback,
218 void *peer_address_callback_cls)
220 GNUNET_TRANSPORT_peer_get_active_addresses (cfg,
224 peer_address_callback,
225 peer_address_callback_cls);
228 /* end of transport_api_peer_address_lookup.c */