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_monitoring.c
23 * @brief montoring api for transport peer status and validation entries
25 * This api provides the ability to query the transport service about
26 * the status of a specific or all peers as well as address validation entries.
28 * Calls back with information about peer(s) including address used, state and
29 * state timeout for peer requests and address, address lifetime and next revalidation
30 * for validation entries.
33 #include "gnunet_util_lib.h"
34 #include "gnunet_arm_service.h"
35 #include "gnunet_hello_lib.h"
36 #include "gnunet_protocols.h"
37 #include "gnunet_transport_service.h"
38 #include "transport.h"
41 * Context for iterating validation entries.
43 struct GNUNET_TRANSPORT_PeerMonitoringContext
46 * Function to call with the binary address.
48 GNUNET_TRANSPORT_PeerIterateCallback cb;
56 * Connection to the service.
58 struct GNUNET_CLIENT_Connection *client;
61 * Configuration we use.
63 const struct GNUNET_CONFIGURATION_Handle *cfg;
66 * When should this operation time out?
68 struct GNUNET_TIME_Absolute timeout;
71 * Backoff for reconnect.
73 struct GNUNET_TIME_Relative backoff;
76 * Task ID for reconnect.
78 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
81 * Identity of the peer to monitor.
83 struct GNUNET_PeerIdentity peer;
86 * Was this a one-shot request?
93 * Context for the address lookup.
95 struct GNUNET_TRANSPORT_ValidationMonitoringContext
98 * Function to call with the binary address.
100 GNUNET_TRANSPORT_ValidationIterateCallback cb;
108 * Connection to the service.
110 struct GNUNET_CLIENT_Connection *client;
113 * Configuration we use.
115 const struct GNUNET_CONFIGURATION_Handle *cfg;
118 * When should this operation time out?
120 struct GNUNET_TIME_Absolute timeout;
123 * Backoff for reconnect.
125 struct GNUNET_TIME_Relative backoff;
128 * Task ID for reconnect.
130 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
133 * Identity of the peer to monitor.
135 struct GNUNET_PeerIdentity peer;
138 * Was this a one-shot request?
144 * Check if a state is defined as connected
146 * @param state the state value
147 * @return GNUNET_YES or GNUNET_NO
150 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state)
154 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
155 case GNUNET_TRANSPORT_PS_INIT_ATS:
156 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
157 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
158 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
159 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
160 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
161 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
163 case GNUNET_TRANSPORT_PS_CONNECTED:
164 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
165 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
166 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
167 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
168 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
170 case GNUNET_TRANSPORT_PS_DISCONNECT:
171 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
174 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
175 "Unhandled state `%s' \n",
176 GNUNET_TRANSPORT_p2s (state));
180 return GNUNET_SYSERR;
184 * Convert state to human-readable string.
186 * @param state the state value
187 * @return corresponding string
190 GNUNET_TRANSPORT_p2s (enum GNUNET_TRANSPORT_PeerState state)
194 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
195 return "S_NOT_CONNECTED";
196 case GNUNET_TRANSPORT_PS_INIT_ATS:
198 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
199 return "S_INIT_BLACKLIST";
200 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
201 return "S_CONNECT_SENT";
202 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
203 return "S_CONNECT_RECV_BLACKLIST_INBOUND";
204 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
205 return "S_CONNECT_RECV_ATS";
206 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
207 return "S_CONNECT_RECV_BLACKLIST";
208 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
209 return "S_CONNECT_RECV_ACK";
210 case GNUNET_TRANSPORT_PS_CONNECTED:
211 return "S_CONNECTED";
212 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
213 return "S_RECONNECT_ATS";
214 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
215 return "S_RECONNECT_BLACKLIST";
216 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
217 return "S_RECONNECT_SENT";
218 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
219 return "S_CONNECTED_SWITCHING_BLACKLIST";
220 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
221 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
222 case GNUNET_TRANSPORT_PS_DISCONNECT:
223 return "S_DISCONNECT";
224 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
225 return "S_DISCONNECT_FINISHED";
234 * Function called with responses from the service.
236 * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
237 * @param msg NULL on timeout or error, otherwise presumably a
238 * message with the human-readable address
241 peer_response_processor (void *cls,
242 const struct GNUNET_MessageHeader *msg);
246 * Send our subscription request to the service.
248 * @param pal_ctx our context
251 send_request (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
253 struct PeerMonitorMessage msg;
255 msg.header.size = htons (sizeof (struct PeerMonitorMessage));
256 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST);
257 msg.one_shot = htonl (pal_ctx->one_shot);
258 msg.timeout = GNUNET_TIME_absolute_hton (pal_ctx->timeout);
259 msg.peer = pal_ctx->peer;
260 GNUNET_assert (GNUNET_OK ==
261 GNUNET_CLIENT_transmit_and_get_response (pal_ctx->client,
263 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout),
265 &peer_response_processor,
270 * Task run to re-establish the connection.
272 * @param cls our 'struct GNUNET_TRANSPORT_PeerAddressLookupContext*'
273 * @param tc scheduler context, unused
276 do_connect (void *cls,
277 const struct GNUNET_SCHEDULER_TaskContext *tc)
279 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
281 pal_ctx->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
282 pal_ctx->client = GNUNET_CLIENT_connect ("transport", pal_ctx->cfg);
283 GNUNET_assert (NULL != pal_ctx->client);
284 send_request (pal_ctx);
289 * Cut the existing connection and reconnect.
291 * @param pal_ctx our context
294 reconnect (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
296 GNUNET_assert (GNUNET_NO == pal_ctx->one_shot);
297 GNUNET_CLIENT_disconnect (pal_ctx->client);
298 pal_ctx->client = NULL;
299 pal_ctx->backoff = GNUNET_TIME_STD_BACKOFF (pal_ctx->backoff);
300 pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (pal_ctx->backoff,
307 * Function called with responses from the service.
309 * @param cls our 'struct GNUNET_TRANSPORT_PeerMonitoringContext*'
310 * @param msg NULL on timeout or error, otherwise presumably a
311 * message with the human-readable address
314 peer_response_processor (void *cls,
315 const struct GNUNET_MessageHeader *msg)
317 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
318 struct PeerIterateResponseMessage *pir_msg;
319 struct GNUNET_HELLO_Address *address;
321 const char *transport_name;
328 if (pal_ctx->one_shot)
331 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
332 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
333 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
341 size = ntohs (msg->size);
342 GNUNET_break (ntohs (msg->type) ==
343 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
344 if (size == sizeof (struct GNUNET_MessageHeader))
347 if (pal_ctx->one_shot)
349 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
350 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
351 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
360 if ((size < sizeof (struct PeerIterateResponseMessage)) ||
361 (ntohs (msg->type) !=
362 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE))
365 if (pal_ctx->one_shot)
367 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
368 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
369 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
378 pir_msg = (struct PeerIterateResponseMessage *) msg;
379 tlen = ntohl (pir_msg->pluginlen);
380 alen = ntohl (pir_msg->addrlen);
382 if (size != sizeof (struct PeerIterateResponseMessage) + tlen + alen)
385 if (pal_ctx->one_shot)
387 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
388 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
389 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
399 if ( (0 == tlen) && (0 == alen) )
401 /* No address available */
402 pal_ctx->cb (pal_ctx->cb_cls, &pir_msg->peer, NULL,
403 ntohl(pir_msg->state),
404 GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
410 GNUNET_break (0); /* This must not happen: address without plugin */
413 addr = (const char *) &pir_msg[1];
414 transport_name = &addr[alen];
416 if (transport_name[tlen - 1] != '\0')
418 /* Corrupt plugin name */
420 if (pal_ctx->one_shot)
422 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
423 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
424 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
434 address = GNUNET_HELLO_address_allocate (&pir_msg->peer,
435 transport_name, addr, alen);
436 pal_ctx->cb (pal_ctx->cb_cls, &pir_msg->peer, address,
437 ntohl(pir_msg->state),
438 GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
439 GNUNET_HELLO_address_free (address);
443 /* expect more replies */
444 GNUNET_CLIENT_receive (pal_ctx->client, &peer_response_processor,
446 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
451 * Return information about a specific peer or all peers currently known to
452 * transport service once or in monitoring mode. To obtain information about
453 * a specific peer, a peer identity can be passed. To obtain information about
454 * all peers currently known to transport service, NULL can be passed as peer
457 * For each peer, the callback is called with information about the address used
458 * to communicate with this peer, the state this peer is currently in and the
459 * the current timeout for this state.
461 * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
462 * more time with 'NULL'. After this, the operation must no longer be
463 * explicitly canceled.
465 * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
468 * @param cfg configuration to use
469 * @param peer a specific peer identity to obtain information for,
471 * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
472 * GNUNET_NO to monitor peers continuously
473 * @param timeout how long is the lookup allowed to take at most
474 * @param peer_callback function to call with the results
475 * @param peer_callback_cls closure for peer_address_callback
477 struct GNUNET_TRANSPORT_PeerMonitoringContext *
478 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
479 const struct GNUNET_PeerIdentity *peer,
481 struct GNUNET_TIME_Relative timeout,
482 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
483 void *peer_callback_cls)
485 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx;
486 struct GNUNET_CLIENT_Connection *client;
488 client = GNUNET_CLIENT_connect ("transport", cfg);
491 if (GNUNET_YES != one_shot)
492 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
493 pal_ctx = GNUNET_new (struct GNUNET_TRANSPORT_PeerMonitoringContext);
494 pal_ctx->cb = peer_callback;
495 pal_ctx->cb_cls = peer_callback_cls;
497 pal_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
499 pal_ctx->peer = *peer;
500 pal_ctx->one_shot = one_shot;
501 pal_ctx->client = client;
502 send_request (pal_ctx);
509 * Cancel request to monitor peers
511 * @param pic handle for the request to cancel
514 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic)
516 if (NULL != pic->client)
518 GNUNET_CLIENT_disconnect (pic->client);
521 if (GNUNET_SCHEDULER_NO_TASK != pic->reconnect_task)
523 GNUNET_SCHEDULER_cancel (pic->reconnect_task);
524 pic->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
531 * Return information about pending address validation operations for a specific
534 * @param cfg configuration to use
535 * @param peer a specific peer identity to obtain validation entries for,
537 * @param one_shot GNUNET_YES to return all entries and then end (with NULL+NULL),
538 * GNUNET_NO to monitor validation entries continuously
539 * @param timeout how long is the lookup allowed to take at most
540 * @param validation_callback function to call with the results
541 * @param validation_callback_cls closure for peer_address_callback
543 struct GNUNET_TRANSPORT_ValidationMonitoringContext *
544 GNUNET_TRANSPORT_monitor_validation_entries (const struct
545 GNUNET_CONFIGURATION_Handle *cfg,
546 const struct GNUNET_PeerIdentity *peer,
548 struct GNUNET_TIME_Relative timeout,
549 GNUNET_TRANSPORT_ValidationIterateCallback validation_callback,
550 void *validation_callback_cls)
552 /* Not implemented */
558 * Return information about all current pending validation operations
560 * @param vic handle for the request to cancel
563 GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic)
565 /* Not implemented */
569 /* end of transport_api_monitoring.c */