- finale commit for the api change
[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_peer_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    * When should this operation time out?
64    */
65   struct GNUNET_TIME_Absolute timeout;
66 };
67
68 /**
69  * Function called with responses from the service.
70  *
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
74  */
75 static void
76 peer_address_response_processor (void *cls,
77                                  const struct GNUNET_MessageHeader *msg)
78 {
79   struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
80   struct AddressIterateResponseMessage *air_msg;
81   struct GNUNET_HELLO_Address *address;
82   uint16_t size;
83
84   if (msg == NULL)
85   {
86     pal_ctx->cb (pal_ctx->cb_cls, NULL);
87     GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
88     GNUNET_free (pal_ctx);
89     return;
90   }
91   size = ntohs (msg->size);
92   GNUNET_break (ntohs (msg->type) == GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
93   if (size == sizeof (struct GNUNET_MessageHeader))
94   {
95     /* done! */
96     pal_ctx->cb (pal_ctx->cb_cls, NULL );
97     GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
98     GNUNET_free (pal_ctx);
99     return;
100   }
101
102   if (size < sizeof (struct GNUNET_MessageHeader) + sizeof (struct AddressIterateResponseMessage))
103   {
104     GNUNET_break_op (0);
105     pal_ctx->cb (pal_ctx->cb_cls, NULL );
106     GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
107     GNUNET_free (pal_ctx);
108     return;
109   }
110
111   air_msg = (struct AddressIterateResponseMessage *) &msg[1];
112   size_t tlen = ntohl(air_msg->pluginlen);
113   size_t alen = ntohl(air_msg->addrlen);
114
115   if (size != sizeof (struct GNUNET_MessageHeader) + sizeof (struct AddressIterateResponseMessage) + tlen + alen)
116   {
117     GNUNET_break_op (0);
118     pal_ctx->cb (pal_ctx->cb_cls, NULL );
119     GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
120     GNUNET_free (pal_ctx);
121     return;
122   }
123
124   char * addr = (char *) &air_msg[1];
125   char * transport_name = &addr[alen];
126
127   if (transport_name[tlen-1] != '\0')
128   {
129     GNUNET_break_op (0);
130     pal_ctx->cb (pal_ctx->cb_cls, NULL );
131     GNUNET_CLIENT_disconnect (pal_ctx->client, GNUNET_NO);
132     GNUNET_free (pal_ctx);
133     return;
134   }
135
136   address = GNUNET_HELLO_address_allocate(&air_msg->peer, transport_name, addr, alen);
137
138   /* expect more replies */
139   GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor, pal_ctx,
140                          GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
141
142   pal_ctx->cb (pal_ctx->cb_cls, address);
143   GNUNET_free (address);
144 }
145
146
147 /**
148  * Return all the known addresses for a specific peer or all peers.
149  * Returns continously all address if one_shot is set to GNUNET_NO
150  *
151  * CHANGE: Returns the address(es) that we are currently using for this
152  * peer.  Upon completion, the 'AddressLookUpCallback' is called one more
153  * time with 'NULL' for the address and the peer.  After this, the operation must no
154  * longer be explicitly cancelled.
155  *
156  * @param cfg configuration to use
157  * @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers
158  * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
159  *                 GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly cancelled)
160  * @param timeout how long is the lookup allowed to take at most
161  * @param peer_address_callback function to call with the results
162  * @param peer_address_callback_cls closure for peer_address_callback
163  */
164 struct GNUNET_TRANSPORT_PeerIterateContext *
165 GNUNET_TRANSPORT_peer_get_active_addresses (const struct GNUNET_CONFIGURATION_Handle *cfg,
166                                             const struct GNUNET_PeerIdentity *peer,
167                                             int one_shot,
168                                             struct GNUNET_TIME_Relative timeout,
169                                             GNUNET_TRANSPORT_PeerIterateCallback peer_address_callback,
170                                             void *peer_address_callback_cls)
171 {
172   struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx;
173   struct AddressIterateMessage msg;
174   struct GNUNET_CLIENT_Connection *client;
175   struct GNUNET_TIME_Absolute abs_timeout;
176
177   client = GNUNET_CLIENT_connect ("transport", cfg);
178   if (client == NULL)
179   {
180     peer_address_callback (peer_address_callback_cls, NULL);
181     return NULL;
182   }
183
184   abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
185
186   msg.header.size = htons (sizeof (struct AddressIterateMessage));
187   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
188   msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
189   if (peer == NULL)
190    memset (&msg.peer, 0 , sizeof (struct GNUNET_PeerIdentity));
191   else
192    memcpy (&msg.peer, peer , sizeof (struct GNUNET_PeerIdentity));
193
194   pal_ctx = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerIterateContext));
195   pal_ctx->cb = peer_address_callback;
196   pal_ctx->cb_cls = peer_address_callback_cls;
197   pal_ctx->timeout = abs_timeout;
198   pal_ctx->client = client;
199   GNUNET_assert (GNUNET_OK ==
200                  GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
201                                                           timeout, GNUNET_YES,
202                                                           &peer_address_response_processor,
203                                                           pal_ctx));
204   return pal_ctx;
205 }
206
207
208 /**
209  * Cancel request for address conversion.
210  *
211  * @param alc handle for the request to cancel
212  */
213 void
214 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
215                                              GNUNET_TRANSPORT_PeerIterateContext
216                                              *alc)
217 {
218   GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO);
219   GNUNET_free (alc);
220 }
221
222 /**
223  * Return all addresses for all peers.
224  *
225  * @param cfg configuration to use
226  * @param timeout how long is the lookup allowed to take at most
227  * @param peer_address_callback function to call with the results
228  * @param peer_address_callback_cls closure for peer_address_callback
229  */
230 void
231 GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
232                                   struct GNUNET_TIME_Relative timeout,
233                                   GNUNET_TRANSPORT_PeerIterateCallback
234                                   peer_address_callback,
235                                   void *peer_address_callback_cls)
236 {
237   GNUNET_TRANSPORT_peer_get_active_addresses (cfg,
238       NULL,
239       GNUNET_YES,
240       timeout,
241       peer_address_callback,
242       peer_address_callback_cls);
243 }
244
245 /* end of transport_api_peer_address_lookup.c */