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