2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, 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, 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, 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, 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, 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, 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);
289 if ( (0 == tlen) && (0 == alen) )
292 if (val_ctx->one_shot)
294 val_ctx->cb (val_ctx->cb_cls, NULL, NULL,
295 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TIME_UNIT_ZERO_ABS,
296 GNUNET_TIME_UNIT_ZERO_ABS, GNUNET_TRANSPORT_VS_NONE);
297 GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
301 reconnect_val_ctx (val_ctx);
309 GNUNET_break (0); /* This must not happen: address without plugin */
312 addr = (const char *) &vr_msg[1];
313 transport_name = &addr[alen];
315 if (transport_name[tlen - 1] != '\0')
317 /* Corrupt plugin name */
319 if (val_ctx->one_shot)
321 val_ctx->cb (val_ctx->cb_cls,
323 GNUNET_TIME_UNIT_ZERO_ABS,
324 GNUNET_TIME_UNIT_ZERO_ABS,
325 GNUNET_TIME_UNIT_ZERO_ABS,
326 GNUNET_TRANSPORT_VS_NONE);
327 GNUNET_TRANSPORT_monitor_validation_entries_cancel (val_ctx);
331 reconnect_val_ctx (val_ctx);
337 address = GNUNET_HELLO_address_allocate (&vr_msg->peer,
340 ntohl (vr_msg->local_address_info));
341 val_ctx->cb (val_ctx->cb_cls,
344 GNUNET_TIME_absolute_ntoh (vr_msg->last_validation),
345 GNUNET_TIME_absolute_ntoh (vr_msg->valid_until),
346 GNUNET_TIME_absolute_ntoh (vr_msg->next_validation),
347 ntohl(vr_msg->state));
348 GNUNET_HELLO_address_free (address);
350 /* expect more replies */
351 GNUNET_CLIENT_receive (val_ctx->client,
352 &val_response_processor,
354 GNUNET_TIME_absolute_get_remaining (val_ctx->timeout));
359 * Return information about pending address validation operations for a specific
362 * @param cfg configuration to use
363 * @param peer a specific peer identity to obtain validation entries for,
365 * @param one_shot #GNUNET_YES to return all entries and then end (with NULL+NULL),
366 * #GNUNET_NO to monitor validation entries continuously
367 * @param timeout how long is the lookup allowed to take at most
368 * @param validation_callback function to call with the results
369 * @param validation_callback_cls closure for peer_address_callback
371 struct GNUNET_TRANSPORT_ValidationMonitoringContext *
372 GNUNET_TRANSPORT_monitor_validation_entries (const struct GNUNET_CONFIGURATION_Handle *cfg,
373 const struct GNUNET_PeerIdentity *peer,
375 struct GNUNET_TIME_Relative timeout,
376 GNUNET_TRANSPORT_ValidationIterateCallback validation_callback,
377 void *validation_callback_cls)
379 struct GNUNET_TRANSPORT_ValidationMonitoringContext *val_ctx;
380 struct GNUNET_CLIENT_Connection *client;
382 client = GNUNET_CLIENT_connect ("transport", cfg);
385 if (GNUNET_YES != one_shot)
386 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
387 val_ctx = GNUNET_new (struct GNUNET_TRANSPORT_ValidationMonitoringContext);
388 val_ctx->cb = validation_callback;
389 val_ctx->cb_cls = validation_callback_cls;
391 val_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
393 val_ctx->peer = *peer;
394 val_ctx->one_shot = one_shot;
395 val_ctx->client = client;
396 send_val_mon_request (val_ctx);
403 * Return information about all current pending validation operations
405 * @param vic handle for the request to cancel
408 GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic)
410 if (NULL != vic->client)
412 GNUNET_CLIENT_disconnect (vic->client);
415 if (NULL != vic->reconnect_task)
417 GNUNET_SCHEDULER_cancel (vic->reconnect_task);
418 vic->reconnect_task = NULL;
424 /* end of transport_api_monitor_validation.c */