diff options
author | Matthias Wachs <wachs@net.in.tum.de> | 2011-11-29 09:52:10 +0000 |
---|---|---|
committer | Matthias Wachs <wachs@net.in.tum.de> | 2011-11-29 09:52:10 +0000 |
commit | ba232c9d203303cd2beb2994fc49c5d5b700d85c (patch) | |
tree | 9cfb172b64e2870aa244fc9a3eb546486a887342 /src/transport/transport_api_address_lookup.c | |
parent | 623df97886da9ee06724b7860a1a2463737b750e (diff) | |
download | gnunet-ba232c9d203303cd2beb2994fc49c5d5b700d85c.tar.gz gnunet-ba232c9d203303cd2beb2994fc49c5d5b700d85c.zip |
api clean up next steps
Diffstat (limited to 'src/transport/transport_api_address_lookup.c')
-rw-r--r-- | src/transport/transport_api_address_lookup.c | 209 |
1 files changed, 153 insertions, 56 deletions
diff --git a/src/transport/transport_api_address_lookup.c b/src/transport/transport_api_address_lookup.c index 259c77152..385abd423 100644 --- a/src/transport/transport_api_address_lookup.c +++ b/src/transport/transport_api_address_lookup.c | |||
@@ -17,6 +17,18 @@ | |||
17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, | 17 | Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
18 | Boston, MA 02111-1307, USA. | 18 | Boston, MA 02111-1307, USA. |
19 | */ | 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 | |||
20 | #include "platform.h" | 32 | #include "platform.h" |
21 | #include "gnunet_client_lib.h" | 33 | #include "gnunet_client_lib.h" |
22 | #include "gnunet_arm_service.h" | 34 | #include "gnunet_arm_service.h" |
@@ -30,12 +42,12 @@ | |||
30 | /** | 42 | /** |
31 | * Context for the address lookup. | 43 | * Context for the address lookup. |
32 | */ | 44 | */ |
33 | struct GNUNET_TRANSPORT_AddressToStringContext | 45 | struct GNUNET_TRANSPORT_PeerAddressLookupContext |
34 | { | 46 | { |
35 | /** | 47 | /** |
36 | * Function to call with the human-readable address. | 48 | * Function to call with the human-readable address. |
37 | */ | 49 | */ |
38 | GNUNET_TRANSPORT_AddressToStringCallback cb; | 50 | GNUNET_TRANSPORT_AddressLookUpCallback cb; |
39 | 51 | ||
40 | /** | 52 | /** |
41 | * Closure for cb. | 53 | * Closure for cb. |
@@ -57,15 +69,16 @@ struct GNUNET_TRANSPORT_AddressToStringContext | |||
57 | /** | 69 | /** |
58 | * Function called with responses from the service. | 70 | * Function called with responses from the service. |
59 | * | 71 | * |
60 | * @param cls our 'struct GNUNET_TRANSPORT_AddressLookupContext*' | 72 | * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*' |
61 | * @param msg NULL on timeout or error, otherwise presumably a | 73 | * @param msg NULL on timeout or error, otherwise presumably a |
62 | * message with the human-readable address | 74 | * message with the human-readable address |
63 | */ | 75 | */ |
64 | static void | 76 | static void |
65 | address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) | 77 | peer_address_response_processor (void *cls, |
78 | const struct GNUNET_MessageHeader *msg) | ||
66 | { | 79 | { |
67 | struct GNUNET_TRANSPORT_AddressToStringContext *alucb = cls; | 80 | struct GNUNET_TRANSPORT_PeerAddressLookupContext *alucb = cls; |
68 | const char *address; | 81 | const struct GNUNET_HELLO_Address *address; |
69 | uint16_t size; | 82 | uint16_t size; |
70 | 83 | ||
71 | if (msg == NULL) | 84 | if (msg == NULL) |
@@ -81,86 +94,72 @@ address_response_processor (void *cls, const struct GNUNET_MessageHeader *msg) | |||
81 | if (size == sizeof (struct GNUNET_MessageHeader)) | 94 | if (size == sizeof (struct GNUNET_MessageHeader)) |
82 | { | 95 | { |
83 | /* done! */ | 96 | /* done! */ |
84 | alucb->cb (alucb->cb_cls, NULL); | 97 | alucb->cb (alucb->cb_cls, NULL ); |
85 | GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); | 98 | GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); |
86 | GNUNET_free (alucb); | 99 | GNUNET_free (alucb); |
87 | return; | 100 | return; |
88 | } | 101 | } |
89 | address = (const char *) &msg[1]; | 102 | address = (const struct GNUNET_HELLO_Address *) &msg[1]; |
103 | #if 0 | ||
90 | if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0') | 104 | if (address[size - sizeof (struct GNUNET_MessageHeader) - 1] != '\0') |
91 | { | 105 | { |
92 | /* invalid reply */ | 106 | /* invalid reply */ |
93 | GNUNET_break (0); | 107 | GNUNET_break (0); |
94 | alucb->cb (alucb->cb_cls, NULL); | 108 | alucb->cb (alucb->cb_cls, NULL ); |
95 | GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); | 109 | GNUNET_CLIENT_disconnect (alucb->client, GNUNET_NO); |
96 | GNUNET_free (alucb); | 110 | GNUNET_free (alucb); |
97 | return; | 111 | return; |
98 | } | 112 | } |
113 | #endif | ||
99 | /* expect more replies */ | 114 | /* expect more replies */ |
100 | GNUNET_CLIENT_receive (alucb->client, &address_response_processor, alucb, | 115 | GNUNET_CLIENT_receive (alucb->client, &peer_address_response_processor, alucb, |
101 | GNUNET_TIME_absolute_get_remaining (alucb->timeout)); | 116 | GNUNET_TIME_absolute_get_remaining (alucb->timeout)); |
102 | alucb->cb (alucb->cb_cls, address); | 117 | |
118 | /* REFACTOR FIX THIS */ | ||
119 | alucb->cb (alucb->cb_cls, address ); | ||
103 | } | 120 | } |
104 | 121 | ||
105 | 122 | ||
106 | /** | 123 | /** |
107 | * Convert a binary address into a human readable address. | 124 | * Return all the known addresses for a peer. |
108 | * | 125 | * |
109 | * @param cfg configuration to use | 126 | * @param cfg configuration to use |
110 | * @param address address to convert (binary format) | 127 | * @param peer peer identity to look up the addresses of |
111 | * @param addressLen number of bytes in address | ||
112 | * @param numeric should (IP) addresses be displayed in numeric form | ||
113 | * (otherwise do reverse DNS lookup) | ||
114 | * @param nameTrans name of the transport to which the address belongs | ||
115 | * @param timeout how long is the lookup allowed to take at most | 128 | * @param timeout how long is the lookup allowed to take at most |
116 | * @param aluc function to call with the results | 129 | * @param peer_address_callback function to call with the results |
117 | * @param aluc_cls closure for aluc | 130 | * @param peer_address_callback_cls closure for peer_address_callback |
118 | * @return handle to cancel the operation, NULL on error | 131 | * @return handle to cancel the operation, NULL on error |
119 | */ | 132 | */ |
120 | struct GNUNET_TRANSPORT_AddressToStringContext * | 133 | struct GNUNET_TRANSPORT_PeerAddressLookupContext * |
121 | GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg, | 134 | GNUNET_TRANSPORT_peer_get_active_addresses (const struct GNUNET_CONFIGURATION_Handle *cfg, |
122 | const struct GNUNET_HELLO_Address *address, | 135 | const struct GNUNET_PeerIdentity *peer, |
123 | int numeric, | 136 | int one_shot, |
124 | struct GNUNET_TIME_Relative timeout, | 137 | struct GNUNET_TIME_Relative timeout, |
125 | GNUNET_TRANSPORT_AddressToStringCallback aluc, | 138 | GNUNET_TRANSPORT_AddressLookUpCallback peer_address_callback, |
126 | void *aluc_cls) | 139 | void *peer_address_callback_cls) |
127 | { | 140 | { |
128 | size_t len; | 141 | struct PeerAddressLookupMessage msg; |
129 | size_t alen; | 142 | struct GNUNET_TRANSPORT_PeerAddressLookupContext *alc; |
130 | struct AddressLookupMessage *msg; | ||
131 | struct GNUNET_TRANSPORT_AddressToStringContext *alc; | ||
132 | struct GNUNET_CLIENT_Connection *client; | 143 | struct GNUNET_CLIENT_Connection *client; |
133 | char *addrbuf; | ||
134 | 144 | ||
135 | alen = GNUNET_HELLO_address_get_size (address); | ||
136 | len = sizeof (struct AddressLookupMessage) + alen; | ||
137 | if (len >= GNUNET_SERVER_MAX_MESSAGE_SIZE) | ||
138 | { | ||
139 | GNUNET_break (0); | ||
140 | return NULL; | ||
141 | } | ||
142 | client = GNUNET_CLIENT_connect ("transport", cfg); | 145 | client = GNUNET_CLIENT_connect ("transport", cfg); |
143 | if (client == NULL) | 146 | if (client == NULL) |
144 | return NULL; | 147 | return NULL; |
145 | msg = GNUNET_malloc (len); | 148 | msg.header.size = htons (sizeof (struct PeerAddressLookupMessage)); |
146 | msg->header.size = htons (len); | 149 | msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_PEER_ADDRESS_LOOKUP); |
147 | msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_LOOKUP); | 150 | msg.reserved = htonl (0); |
148 | msg->numeric_only = htonl (numeric); | 151 | msg.timeout = GNUNET_TIME_relative_hton (timeout); |
149 | msg->timeout = GNUNET_TIME_relative_hton (timeout); | 152 | memcpy (&msg.peer, peer, sizeof (struct GNUNET_PeerIdentity)); |
150 | msg->addrlen = htonl (alen); | 153 | alc = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerAddressLookupContext)); |
151 | addrbuf = (char *) &msg[1]; | 154 | alc->cb = peer_address_callback; |
152 | memcpy (addrbuf, address, alen); | 155 | alc->cb_cls = peer_address_callback_cls; |
153 | alc = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_AddressToStringContext)); | ||
154 | alc->cb = aluc; | ||
155 | alc->cb_cls = aluc_cls; | ||
156 | alc->timeout = GNUNET_TIME_relative_to_absolute (timeout); | 156 | alc->timeout = GNUNET_TIME_relative_to_absolute (timeout); |
157 | alc->client = client; | 157 | alc->client = client; |
158 | GNUNET_assert (GNUNET_OK == | 158 | GNUNET_assert (GNUNET_OK == |
159 | GNUNET_CLIENT_transmit_and_get_response (client, &msg->header, | 159 | GNUNET_CLIENT_transmit_and_get_response (client, &msg.header, |
160 | timeout, GNUNET_YES, | 160 | timeout, GNUNET_YES, |
161 | &address_response_processor, | 161 | &peer_address_response_processor, |
162 | alc)); | 162 | alc)); |
163 | GNUNET_free (msg); | ||
164 | return alc; | 163 | return alc; |
165 | } | 164 | } |
166 | 165 | ||
@@ -171,14 +170,112 @@ GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cf | |||
171 | * @param alc handle for the request to cancel | 170 | * @param alc handle for the request to cancel |
172 | */ | 171 | */ |
173 | void | 172 | void |
174 | GNUNET_TRANSPORT_address_to_string_cancel (struct | 173 | GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct |
175 | GNUNET_TRANSPORT_AddressToStringContext | 174 | GNUNET_TRANSPORT_PeerAddressLookupContext |
176 | *alc) | 175 | *alc) |
177 | { | 176 | { |
178 | GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO); | 177 | GNUNET_CLIENT_disconnect (alc->client, GNUNET_NO); |
179 | GNUNET_free (alc); | 178 | GNUNET_free (alc); |
180 | } | 179 | } |
181 | 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 | |||
182 | 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 | } | ||
183 | 280 | ||
184 | /* end of transport_api_address_lookup.c */ | 281 | /* end of transport_api_peer_address_lookup.c */ |