385abd423f3754f923bee5d39bc25e85bfca3251
[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_PeerAddressLookupContext
46 {
47   /**
48    * Function to call with the human-readable address.
49    */
50   GNUNET_TRANSPORT_AddressLookUpCallback 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 /**
70  * Function called with responses from the service.
71  *
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
75  */
76 static void
77 peer_address_response_processor (void *cls,
78                                  const struct GNUNET_MessageHeader *msg)
79 {
80   struct GNUNET_TRANSPORT_PeerAddressLookupContext *alucb = cls;
81   const struct GNUNET_HELLO_Address *address;
82   uint16_t size;
83
84   if (msg == NULL)
85   {
86     alucb->cb (alucb->cb_cls, NULL);
87     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
88     GNUNET_free (alucb);
89     return;
90   }
91   GNUNET_break (ntohs (msg->type) ==
92                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
93   size = ntohs (msg->size);
94   if (size == sizeof (struct GNUNET_MessageHeader))
95   {
96     /* done! */
97     alucb->cb (alucb->cb_cls, NULL );
98     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
99     GNUNET_free (alucb);
100     return;
101   }
102   address = (const struct GNUNET_HELLO_Address *) &msg[1];
103 #if 0
104   if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
105   {
106     /* invalid reply */
107     GNUNET_break (0);
108     alucb->cb (alucb->cb_cls, NULL );
109     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
110     GNUNET_free (alucb);
111     return;
112   }
113 #endif
114   /* expect more replies */
115   GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb,
116                          GNUNET_TIME_absolute_get_remaining (alucb->timeout));
117
118   /* REFACTOR FIX THIS */
119   alucb->cb (alucb->cb_cls, address );
120 }
121
122
123 /**
124  * Return all the known addresses for a peer.
125  *
126  * @param cfg configuration to use
127  * @param peer peer identity to look up the addresses of
128  * @param timeout how long is the lookup allowed to take at most
129  * @param peer_address_callback function to call with the results
130  * @param peer_address_callback_cls closure for peer_address_callback
131  * @return handle to cancel the operation, NULL on error
132  */
133 struct GNUNET_TRANSPORT_PeerAddressLookupContext *
134 GNUNET_TRANSPORT_peer_get_active_addresses (const struct GNUNET_CONFIGURATION_Handle *cfg,
135                                       const struct GNUNET_PeerIdentity *peer,
136                                       int one_shot,
137                                       struct GNUNET_TIME_Relative timeout,
138                                       GNUNET_TRANSPORT_AddressLookUpCallback peer_address_callback,
139                                       void *peer_address_callback_cls)
140 {
141   struct PeerAddressLookupMessage msg;
142   struct GNUNET_TRANSPORT_PeerAddressLookupContext *alc;
143   struct GNUNET_CLIENT_Connection *client;
144
145   client = GNUNET_CLIENT_connect ("transport", cfg);
146   if (client == NULL)
147     return NULL;
148   msg.header.size = htons (sizeof (struct PeerAddressLookupMessage));
149   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP);
150   msg.reserved = htonl (0);
151   msg.timeout = GNUNET_TIME_relative_hton (timeout);
152   memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
153   alc = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext));
154   alc->cb = peer_address_callback;
155   alc->cb_cls = peer_address_callback_cls;
156   alc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
157   alc->client = client;
158   GNUNET_assert (GNUNET_OK ==
159                  GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
160                                                           timeout, GNUNET_YES,
161                                                           &peer_address_response_processor,
162                                                           alc));
163   return alc;
164 }
165
166
167 /**
168  * Cancel request for address conversion.
169  *
170  * @param alc handle for the request to cancel
171  */
172 void
173 GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
174                                              GNUNET_TRANSPORT_PeerAddressLookupContext
175                                              *alc)
176 {
177   GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO);
178   GNUNET_free (alc);
179 }
180
181 /**
182  * Function called with responses from the service.
183  *
184  * @param cls our 'struct AddressLookupCtx*'
185  * @param msg NULL on timeout or error, otherwise presumably a
186  *        message with the human-readable peer and address
187  */
188 static void
189 peer_address_iteration_response_processor (void *cls,
190                                  const struct GNUNET_MessageHeader *msg)
191 {
192   struct GNUNET_TRANSPORT_PeerAddressLookupContext *alucb = cls;
193   struct AddressIterateResponseMessage *arm;
194   struct GNUNET_HELLO_Address * address;
195   uint16_t size;
196
197   if (msg == NULL)
198   {
199     alucb->cb (alucb->cb_cls, NULL);
200     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
201     GNUNET_free (alucb);
202     return;
203   }
204
205   GNUNET_break (ntohs (msg->type) ==
206                 GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_REPLY);
207   size = ntohs (msg->size);
208   GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received message type %u size %u\n",
209               ntohs (msg->type), size);
210   if (size == sizeof (struct GNUNET_MessageHeader))
211   {
212     /* done! */
213     alucb->cb (alucb->cb_cls, NULL);
214     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
215     GNUNET_free (alucb);
216     return;
217   }
218   if (size < sizeof (struct AddressIterateResponseMessage))
219   {
220     /* invalid reply */
221     GNUNET_break (0);
222     alucb->cb (alucb->cb_cls, NULL);
223     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
224     GNUNET_free (alucb);
225     return;
226   }
227
228   arm = (struct AddressIterateResponseMessage *) &msg[1];
229   address = (struct GNUNET_HELLO_Address *) &arm[1];
230
231   /* expect more replies */
232   GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb,
233                          GNUNET_TIME_absolute_get_remaining (alucb->timeout));
234   alucb->cb (alucb->cb_cls, address);
235 }
236
237
238 /**
239  * Return all the known addresses for a peer.
240  *
241  * @param cfg configuration to use
242  * @param timeout how long is the lookup allowed to take at most
243  * @param peer_address_callback function to call with the results
244  * @param peer_address_callback_cls closure for peer_address_callback
245  */
246 void
247 GNUNET_TRANSPORT_address_iterate (const struct GNUNET_CONFIGURATION_Handle *cfg,
248                                   struct GNUNET_TIME_Relative timeout,
249                                   GNUNET_TRANSPORT_AddressLookUpCallback
250                                   peer_address_callback,
251                                   void *peer_address_callback_cls)
252 {
253   struct AddressIterateMessage msg;
254   struct GNUNET_TIME_Absolute abs_timeout;
255   struct GNUNET_TRANSPORT_PeerAddressLookupContext *peer_address_lookup_cb;
256   struct GNUNET_CLIENT_Connection *client;
257
258   client = GNUNET_CLIENT_connect ("transport", cfg);
259   if (client == NULL)
260   {
261     peer_address_callback (peer_address_callback_cls, NULL);
262     return;
263   }
264   abs_timeout = GNUNET_TIME_relative_to_absolute (timeout);
265
266   msg.header.size = htons (sizeof (struct AddressIterateMessage));
267   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
268   msg.timeout = GNUNET_TIME_absolute_hton (abs_timeout);
269   peer_address_lookup_cb = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext));
270   peer_address_lookup_cb->cb = peer_address_callback;
271   peer_address_lookup_cb->cb_cls = peer_address_callback_cls;
272   peer_address_lookup_cb->timeout = abs_timeout;
273   peer_address_lookup_cb->client = client;
274   GNUNET_assert (GNUNET_OK ==
275                  GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
276                                                           timeout, GNUNET_YES,
277                                                           &peer_address_iteration_response_processor,
278                                                           peer_address_lookup_cb));
279 }
280
281 /* end of transport_api_peer_address_lookup.c */