1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*
This file is part of GNUnet.
(C) 2009, 2010 Christian Grothoff (and other contributing authors)
GNUnet is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published
by the Free Software Foundation; either version 3, or (at your
option) any later version.
GNUnet is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
You should have received a copy of the GNU General Public License
along with GNUnet; see the file COPYING. If not, write to the
Free Software Foundation, Inc., 59 Temple Place - Suite 330,
Boston, MA 02111-1307, USA.
*/
/**
* @file transport/transport_api_address_lookup.c
* @brief given a peer id, get all known addresses from transport service
*
* This api provides the ability to query the transport service about
* the status of connections to a specific peer. Calls back with a
* pretty printed string of the address, as formatted by the appropriate
* transport plugin, and whether or not the address given is currently
* in the 'connected' state (according to the transport service).
*/
#include "platform.h"
#include "gnunet_client_lib.h"
#include "gnunet_arm_service.h"
#include "gnunet_hello_lib.h"
#include "gnunet_protocols.h"
#include "gnunet_server_lib.h"
#include "gnunet_time_lib.h"
#include "gnunet_transport_service.h"
#include "transport.h"
/**
* Context for the address lookup.
*/
struct GNUNET_TRANSPORT_PeerIterateContext
{
/**
* Function to call with the binary address.
*/
GNUNET_TRANSPORT_PeerIterateCallback cb;
/**
* Closure for cb.
*/
void *cb_cls;
/**
* Connection to the service.
*/
struct GNUNET_CLIENT_Connection *client;
/**
* Configuration we use.
*/
const struct GNUNET_CONFIGURATION_Handle *cfg;
/**
* When should this operation time out?
*/
struct GNUNET_TIME_Absolute timeout;
/**
* Backoff for reconnect.
*/
struct GNUNET_TIME_Relative backoff;
/**
* Task ID for reconnect.
*/
GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
/**
* Identity of the peer to monitor.
*/
struct GNUNET_PeerIdentity peer;
/**
* Was this a one-shot request?
*/
int one_shot;
};
/**
* Function called with responses from the service.
*
* @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
* @param msg NULL on timeout or error, otherwise presumably a
* message with the human-readable address
*/
static void
peer_address_response_processor (void *cls,
const struct GNUNET_MessageHeader *msg);
/**
* Send our subscription request to the service.
*
* @param pal_ctx our context
*/
static void
send_request (struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx)
{
struct AddressIterateMessage msg;
msg.header.size = htons (sizeof (struct AddressIterateMessage));
msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE);
msg.one_shot = htonl (pal_ctx->one_shot);
msg.timeout = GNUNET_TIME_absolute_hton (pal_ctx->timeout);
msg.peer = pal_ctx->peer;
GNUNET_assert (GNUNET_OK ==
GNUNET_CLIENT_transmit_and_get_response (pal_ctx->client,
&msg.header,
GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout),
GNUNET_YES,
&peer_address_response_processor,
pal_ctx));
}
/**
* Task run to re-establish the connection.
*
* @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
* @param tc scheduler context, unused
*/
static void
do_connect (void *cls,
const struct GNUNET_SCHEDULER_TaskContext *tc)
{
struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
pal_ctx->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
pal_ctx->client = GNUNET_CLIENT_connect ("transport", pal_ctx->cfg);
GNUNET_assert (NULL != pal_ctx->client);
send_request (pal_ctx);
}
/**
* Cut the existing connection and reconnect.
*
* @param pal_ctx our context
*/
static void
reconnect (struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx)
{
GNUNET_assert (GNUNET_NO == pal_ctx->one_shot);
GNUNET_CLIENT_disconnect (pal_ctx->client);
pal_ctx->client = NULL;
pal_ctx->backoff = GNUNET_TIME_relative_max (GNUNET_TIME_UNIT_MILLISECONDS,
GNUNET_TIME_relative_min (GNUNET_TIME_relative_multiply (pal_ctx->backoff, 2),
GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)));
pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (pal_ctx->backoff,
&do_connect,
pal_ctx);
}
/**
* Function called with responses from the service.
*
* @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
* @param msg NULL on timeout or error, otherwise presumably a
* message with the human-readable address
*/
static void
peer_address_response_processor (void *cls,
const struct GNUNET_MessageHeader *msg)
{
struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx = cls;
struct AddressIterateResponseMessage *air_msg;
struct GNUNET_HELLO_Address *address;
const char *addr;
const char *transport_name;
uint16_t size;
size_t alen;
size_t tlen;
if (msg == NULL)
{
if (pal_ctx->one_shot)
{
pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
}
else
{
reconnect (pal_ctx);
}
return;
}
size = ntohs (msg->size);
GNUNET_break (ntohs (msg->type) ==
GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE);
if (size == sizeof (struct GNUNET_MessageHeader))
{
/* done! */
if (pal_ctx->one_shot)
{
pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
}
else
{
reconnect (pal_ctx);
}
return;
}
if ((size < sizeof (struct AddressIterateResponseMessage)) ||
(ntohs (msg->type) !=
GNUNET_MESSAGE_TYPE_TRANSPORT_ADDRESS_ITERATE_RESPONSE))
{
GNUNET_break (0);
if (pal_ctx->one_shot)
{
pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
}
else
{
reconnect (pal_ctx);
}
return;
}
air_msg = (struct AddressIterateResponseMessage *) msg;
tlen = ntohl (air_msg->pluginlen);
alen = ntohl (air_msg->addrlen);
if (size != sizeof (struct AddressIterateResponseMessage) + tlen + alen)
{
GNUNET_break (0);
if (pal_ctx->one_shot)
{
pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
}
else
{
reconnect (pal_ctx);
}
return;
}
if (alen == 0 && tlen == 0)
{
pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, NULL);
}
else
{
addr = (const char *) &air_msg[1];
transport_name = &addr[alen];
if (transport_name[tlen - 1] != '\0')
{
GNUNET_break (0);
if (pal_ctx->one_shot)
{
pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL);
GNUNET_TRANSPORT_peer_get_active_addresses_cancel (pal_ctx);
}
else
{
reconnect (pal_ctx);
}
return;
}
/* notify client */
address =
GNUNET_HELLO_address_allocate (&air_msg->peer, transport_name, addr,
alen);
pal_ctx->cb (pal_ctx->cb_cls, &air_msg->peer, address);
GNUNET_HELLO_address_free (address);
}
/* expect more replies */
GNUNET_CLIENT_receive (pal_ctx->client, &peer_address_response_processor,
pal_ctx,
GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
}
/**
* Return all the known addresses for a specific peer or all peers.
* Returns continuously all address if one_shot is set to GNUNET_NO
*
* CHANGE: Returns the address(es) that we are currently using for this
* peer. Upon completion, the 'AddressLookUpCallback' is called one more
* time with 'NULL' for the address and the peer. After this, the operation must no
* longer be explicitly canceled.
*
* @param cfg configuration to use
* @param peer peer identity to look up the addresses of, CHANGE: allow NULL for all (connected) peers
* @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
* GNUNET_NO to monitor the set of addresses used (continuously, must be explicitly canceled)
* @param timeout how long is the lookup allowed to take at most (irrelevant if one_shot is set to GNUNET_NO)
* @param peer_address_callback function to call with the results
* @param peer_address_callback_cls closure for peer_address_callback
*/
struct GNUNET_TRANSPORT_PeerIterateContext *
GNUNET_TRANSPORT_peer_get_active_addresses (const struct
GNUNET_CONFIGURATION_Handle *cfg,
const struct GNUNET_PeerIdentity
*peer, int one_shot,
struct GNUNET_TIME_Relative timeout,
GNUNET_TRANSPORT_PeerIterateCallback
peer_address_callback,
void *peer_address_callback_cls)
{
struct GNUNET_TRANSPORT_PeerIterateContext *pal_ctx;
struct GNUNET_CLIENT_Connection *client;
client = GNUNET_CLIENT_connect ("transport", cfg);
if (client == NULL)
return NULL;
if (GNUNET_YES != one_shot)
timeout = GNUNET_TIME_UNIT_FOREVER_REL;
pal_ctx = GNUNET_malloc (sizeof (struct GNUNET_TRANSPORT_PeerIterateContext));
pal_ctx->cb = peer_address_callback;
pal_ctx->cb_cls = peer_address_callback_cls;
pal_ctx->cfg = cfg;
pal_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
if (NULL != peer)
pal_ctx->peer = *peer;
pal_ctx->one_shot = one_shot;
pal_ctx->client = client;
send_request (pal_ctx);
return pal_ctx;
}
/**
* Cancel request for address conversion.
*
* @param alc handle for the request to cancel
*/
void
GNUNET_TRANSPORT_peer_get_active_addresses_cancel (struct
GNUNET_TRANSPORT_PeerIterateContext
*alc)
{
if (NULL != alc->client)
{
GNUNET_CLIENT_disconnect (alc->client);
alc->client = NULL;
}
if (GNUNET_SCHEDULER_NO_TASK != alc->reconnect_task)
{
GNUNET_SCHEDULER_cancel (alc->reconnect_task);
alc->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
}
GNUNET_free (alc);
}
/* end of transport_api_peer_address_lookup.c */
|