2 This file is part of GNUnet.
3 Copyright (C) 2009, 2010 Christian Grothoff (and other contributing authors)
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.
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.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file transport/transport_api_monitor_validation.c
23 * @brief montoring api for validation status
25 * This api provides the ability to query the transport service about
26 * the status of address validation.
29 #include "gnunet_util_lib.h"
30 #include "gnunet_arm_service.h"
31 #include "gnunet_hello_lib.h"
32 #include "gnunet_protocols.h"
33 #include "gnunet_transport_service.h"
34 #include "transport.h"
38 * Context for the address lookup.
40 struct GNUNET_TRANSPORT_ValidationMonitoringContext
43 * Function to call with the binary address.
45 GNUNET_TRANSPORT_ValidationIterateCallback cb;
53 * Connection to the service.
55 struct GNUNET_CLIENT_Connection *client;
58 * Configuration we use.
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
63 * When should this operation time out?
65 struct GNUNET_TIME_Absolute timeout;
68 * Backoff for reconnect.
70 struct GNUNET_TIME_Relative backoff;
73 * Task ID for reconnect.
75 struct GNUNET_SCHEDULER_Task * reconnect_task;
78 * Identity of the peer to monitor.
80 struct GNUNET_PeerIdentity peer;
83 * Was this a one-shot request?
90 * Convert validation state to human-readable string.
92 * @param state the state value
93 * @return corresponding string
96 GNUNET_TRANSPORT_vs2s (enum GNUNET_TRANSPORT_ValidationState state)
100 case GNUNET_TRANSPORT_VS_NONE:
102 case GNUNET_TRANSPORT_VS_NEW:
104 case GNUNET_TRANSPORT_VS_REMOVE:
106 case GNUNET_TRANSPORT_VS_TIMEOUT:
108 case GNUNET_TRANSPORT_VS_UPDATE:
118 * Function called with responses from the service.
120 * @param cls our `struct GNUNET_TRANSPORT_ValidationMonitoringContext *`
121 * @param msg NULL on timeout or error, otherwise presumably a
122 * message with the human-readable address
125 val_response_processor (void *cls,
126 const struct GNUNET_MessageHeader *msg);
130 * Send our subscription request to the service.
132 * @param val_ctx our context
135 send_val_mon_request (struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx)
137 struct ValidationMonitorMessage msg;
139 msg.header.size = htons (sizeof (struct ValidationMonitorMessage));
140 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_REQUEST);
141 msg.one_shot = htonl (val_ctx->one_shot);
142 msg.peer = val_ctx->peer;
143 GNUNET_assert (GNUNET_OK ==
144 GNUNET_CLIENT_transmit_and_get_response (val_ctx->client,
146 GNUNET_TIME_absolute_get_remaining (val_ctx->timeout),
148 &val_response_processor,
154 * Task run to re-establish the connection.
156 * @param cls our `struct GNUNET_TRANSPORT_ValidationMonitoringContext *`
157 * @param tc scheduler context, unused
160 do_val_connect (void *cls,
161 const struct GNUNET_SCHEDULER_TaskContext *tc)
163 struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx = cls;
165 val_ctx->reconnect_task = NULL;
166 val_ctx->client = GNUNET_CLIENT_connect ("transport", val_ctx->cfg);
167 GNUNET_assert (NULL != val_ctx->client);
168 send_val_mon_request (val_ctx);
173 * Cut the existing connection and reconnect.
175 * @param val_ctx our context
178 reconnect_val_ctx (struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx)
180 GNUNET_assert (GNUNET_NO == val_ctx->one_shot);
181 GNUNET_CLIENT_disconnect (val_ctx->client);
182 val_ctx->client = NULL;
183 /* notify clients about (re)connect */
184 val_ctx->cb (val_ctx->cb_cls, NULL,
185 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
186 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_TIMEOUT);
187 val_ctx->backoff = GNUNET_TIME_STD_BACKOFF (val_ctx->backoff);
188 val_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (val_ctx->backoff,
195 * Function called with responses from the service.
197 * @param cls our `struct GNUNET_TRANSPORT_ValidationMonitoringContext *`
198 * @param msg NULL on timeout or error, otherwise presumably a
199 * message with the human-readable address
202 val_response_processor (void *cls,
203 const struct GNUNET_MessageHeader *msg)
205 struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx = cls;
206 struct ValidationIterateResponseMessage *vr_msg;
207 struct GNUNET_HELLO_Address *address;
209 const char *transport_name;
216 if (val_ctx->one_shot)
219 val_ctx->cb (val_ctx->cb_cls, NULL,
220 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
221 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_TIMEOUT);
222 GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
226 reconnect_val_ctx (val_ctx);
230 size = ntohs (msg->size);
231 GNUNET_break (ntohs (msg->type) ==
232 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE);
234 if (size == sizeof (struct GNUNET_MessageHeader))
237 if (val_ctx->one_shot)
239 val_ctx->cb (val_ctx->cb_cls, NULL,
240 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
241 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_NONE);
242 GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
246 reconnect_val_ctx (val_ctx);
251 if ((size < sizeof (struct ValidationIterateResponseMessage)) ||
252 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_VALIDATION_RESPONSE))
255 if (val_ctx->one_shot)
257 val_ctx->cb (val_ctx->cb_cls, NULL,
258 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
259 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_NONE);
260 GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
264 reconnect_val_ctx (val_ctx);
269 vr_msg = (struct ValidationIterateResponseMessage *) msg;
270 tlen = ntohl (vr_msg->pluginlen);
271 alen = ntohl (vr_msg->addrlen);
273 if (size != sizeof (struct ValidationIterateResponseMessage) + tlen + alen)
276 if (val_ctx->one_shot)
278 val_ctx->cb (val_ctx->cb_cls, NULL,
279 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
280 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_NONE);
281 GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
285 reconnect_val_ctx (val_ctx);
291 GNUNET_break (0); /* This must not happen: address without plugin */
294 addr = (const char *) &vr_msg[1];
295 transport_name = &addr[alen];
297 if (transport_name[tlen - 1] != '\0')
299 /* Corrupt plugin name */
301 if (val_ctx->one_shot)
303 val_ctx->cb (val_ctx->cb_cls,
305 GNUNET_TIME_UNIT_ZERO_ABS,
306 GNUNET_TIME_UNIT_ZERO_ABS,
307 GNUNET_TIME_UNIT_ZERO_ABS,
308 GNUNET_TRANSPORT_VS_NONE);
309 GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
313 reconnect_val_ctx (val_ctx);
319 address = GNUNET_HELLO_address_allocate (&vr_msg->peer,
322 ntohl (vr_msg->local_address_info));
323 val_ctx->cb (val_ctx->cb_cls,
325 GNUNET_TIME_absolute_ntoh (vr_msg->last_validation),
326 GNUNET_TIME_absolute_ntoh (vr_msg->valid_until),
327 GNUNET_TIME_absolute_ntoh (vr_msg->next_validation),
328 ntohl(vr_msg->state));
329 GNUNET_HELLO_address_free (address);
330 /* expect more replies */
331 GNUNET_CLIENT_receive (val_ctx->client,
332 &val_response_processor,
334 GNUNET_TIME_absolute_get_remaining (val_ctx->timeout));
339 * Return information about pending address validation operations for a specific
342 * @param cfg configuration to use
343 * @param peer a specific peer identity to obtain validation entries for,
345 * @param one_shot #GNUNET_YES to return all entries and then end (with NULL+NULL),
346 * #GNUNET_NO to monitor validation entries continuously
347 * @param timeout how long is the lookup allowed to take at most
348 * @param validation_callback function to call with the results
349 * @param validation_callback_cls closure for peer_address_callback
351 struct GNUNET_TRANSPORT_ValidationMonitoringContext *
352 GNUNET_TRANSPORT_monitor_validation_entries (const struct GNUNET_CONFIGURATION_Handle *cfg,
353 const struct GNUNET_PeerIdentity *peer,
355 struct GNUNET_TIME_Relative timeout,
356 GNUNET_TRANSPORT_ValidationIterateCallback validation_callback,
357 void *validation_callback_cls)
359 struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx;
360 struct GNUNET_CLIENT_Connection *client;
362 client = GNUNET_CLIENT_connect ("transport", cfg);
365 if (GNUNET_YES != one_shot)
366 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
367 val_ctx = GNUNET_new (struct GNUNET_TRANSPORT_ValidationMonitoringContext);
368 val_ctx->cb = validation_callback;
369 val_ctx->cb_cls = validation_callback_cls;
371 val_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
373 val_ctx->peer = *peer;
374 val_ctx->one_shot = one_shot;
375 val_ctx->client = client;
376 send_val_mon_request (val_ctx);
383 * Return information about all current pending validation operations
385 * @param vic handle for the request to cancel
388 GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic)
390 if (NULL != vic->client)
392 GNUNET_CLIENT_disconnect (vic->client);
395 if (NULL != vic->reconnect_task)
397 GNUNET_SCHEDULER_cancel (vic->reconnect_task);
398 vic->reconnect_task = NULL;
404 /* end of transport_api_monitor_validation.c */