move GNUNET_TRANSPORT_ATS_ to GNUNET_ATS_
[oweals/gnunet.git] / src / transport / transport_api_peer_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 char *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 char *) &msg[1];
103   if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0')
104   {
105     /* invalid reply */
106     GNUNET_break (0);
107     alucb->cb (alucb->cb_cls, NULL);
108     GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO);
109     GNUNET_free (alucb);
110     return;
111   }
112   /* expect more replies */
113   GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb,
114                          GNUNET_TIME_absolute_get_remaining (alucb->timeout));
115   alucb->cb (alucb->cb_cls, address);
116 }
117
118
119 /**
120  * Return all the known addresses for a peer.
121  *
122  * @param cfg configuration to use
123  * @param peer peer identity to look up the addresses of
124  * @param timeout how long is the lookup allowed to take at most
125  * @param peer_address_callback function to call with the results
126  * @param peer_address_callback_cls closure for peer_address_callback
127  * @return handle to cancel the operation, NULL on error
128  */
129 struct GNUNET_TRANSPORT_PeerAddressLookupContext *
130 GNUNET_TRANSPORT_peer_address_lookup (const struct GNUNET_CONFIGURATION_Handle
131                                       *cfg,
132                                       const struct GNUNET_PeerIdentity *peer,
133                                       struct GNUNET_TIME_Relative timeout,
134                                       GNUNET_TRANSPORT_AddressLookUpCallback
135                                       peer_address_callback,
136                                       void *peer_address_callback_cls)
137 {
138   struct PeerAddressLookupMessage msg;
139   struct GNUNET_TRANSPORT_PeerAddressLookupContext *alc;
140   struct GNUNET_CLIENT_Connection *client;
141
142   client = GNUNET_CLIENT_connect ("transport", cfg);
143   if (client == NULL)
144     return NULL;
145   msg.header.size = htons (sizeof (struct PeerAddressLookupMessage));
146   msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP);
147   msg.timeout = GNUNET_TIME_relative_hton (timeout);
148   memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity));
149   alc =
150       GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext));
151   alc->cb = peer_address_callback;
152   alc->cb_cls = peer_address_callback_cls;
153   alc->timeout = GNUNET_TIME_relative_to_absolute (timeout);
154   alc->client = client;
155   GNUNET_assert (GNUNET_OK ==
156                  GNUNET_CLIENT_transmit_and_get_response (client, &msg.header,
157                                                           timeout, GNUNET_YES,
158                                                           &peer_address_response_processor,
159                                                           alc));
160   return alc;
161 }
162
163
164 /**
165  * Cancel request for address conversion.
166  *
167  * @param alc handle for the request to cancel
168  */
169 void
170 GNUNET_TRANSPORT_peer_address_lookup_cancel (struct
171                                              GNUNET_TRANSPORT_PeerAddressLookupContext
172                                              *alc)
173 {
174   GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO);
175   GNUNET_free (alc);
176 }
177
178
179 /* end of transport_api_peer_address_lookup.c */