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_peers.c
23 * @brief montoring api for transport peer status
25 * This api provides the ability to query the transport service about
26 * the connection status of a specific or all peers.
28 * Calls back with information about peer(s) including address used, state and
29 * state timeout for peer requests.
32 #include "gnunet_util_lib.h"
33 #include "gnunet_arm_service.h"
34 #include "gnunet_hello_lib.h"
35 #include "gnunet_protocols.h"
36 #include "gnunet_transport_service.h"
37 #include "transport.h"
40 * Context for iterating validation entries.
42 struct GNUNET_TRANSPORT_PeerMonitoringContext
45 * Function to call with the binary address.
47 GNUNET_TRANSPORT_PeerIterateCallback cb;
55 * Connection to the service.
57 struct GNUNET_CLIENT_Connection *client;
60 * Configuration we use.
62 const struct GNUNET_CONFIGURATION_Handle *cfg;
65 * When should this operation time out?
67 struct GNUNET_TIME_Absolute timeout;
70 * Backoff for reconnect.
72 struct GNUNET_TIME_Relative backoff;
75 * Task ID for reconnect.
77 GNUNET_SCHEDULER_TaskIdentifier reconnect_task;
80 * Identity of the peer to monitor.
82 struct GNUNET_PeerIdentity peer;
85 * Was this a one-shot request?
92 * Check if a state is defined as connected
94 * @param state the state value
95 * @return #GNUNET_YES or #GNUNET_NO
98 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state)
102 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
103 case GNUNET_TRANSPORT_PS_INIT_ATS:
104 case GNUNET_TRANSPORT_PS_SYN_SENT:
105 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
106 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
108 case GNUNET_TRANSPORT_PS_CONNECTED:
109 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
110 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
111 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
113 case GNUNET_TRANSPORT_PS_DISCONNECT:
114 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
118 "Unhandled state `%s' \n",
119 GNUNET_TRANSPORT_ps2s (state));
123 return GNUNET_SYSERR;
128 * Convert peer state to human-readable string.
130 * @param state the state value
131 * @return corresponding string
134 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state)
138 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
139 return "S_NOT_CONNECTED";
140 case GNUNET_TRANSPORT_PS_INIT_ATS:
142 case GNUNET_TRANSPORT_PS_SYN_SENT:
144 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
145 return "S_SYN_RECV_ATS";
146 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
147 return "S_SYN_RECV_ACK";
148 case GNUNET_TRANSPORT_PS_CONNECTED:
149 return "S_CONNECTED";
150 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
151 return "S_RECONNECT_ATS";
152 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
153 return "S_RECONNECT_SENT";
154 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
155 return "S_CONNECTED_SWITCHING_SYN_SENT";
156 case GNUNET_TRANSPORT_PS_DISCONNECT:
157 return "S_DISCONNECT";
158 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
159 return "S_DISCONNECT_FINISHED";
168 * Function called with responses from the service.
170 * @param cls our `struct GNUNET_TRANSPORT_PeerAddressLookupContext *`
171 * @param msg NULL on timeout or error, otherwise presumably a
172 * message with the human-readable address
175 peer_response_processor (void *cls,
176 const struct GNUNET_MessageHeader *msg);
180 * Send our subscription request to the service.
182 * @param pal_ctx our context
185 send_peer_mon_request (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
187 struct PeerMonitorMessage msg;
189 msg.header.size = htons (sizeof (struct PeerMonitorMessage));
190 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST);
191 msg.one_shot = htonl (pal_ctx->one_shot);
192 msg.peer = pal_ctx->peer;
193 GNUNET_assert (GNUNET_OK ==
194 GNUNET_CLIENT_transmit_and_get_response (pal_ctx->client,
196 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout),
198 &peer_response_processor,
204 * Task run to re-establish the connection.
206 * @param cls our `struct GNUNET_TRANSPORT_PeerAddressLookupContext *`
207 * @param tc scheduler context, unused
210 do_peer_connect (void *cls,
211 const struct GNUNET_SCHEDULER_TaskContext *tc)
213 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
215 pal_ctx->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
216 pal_ctx->client = GNUNET_CLIENT_connect ("transport", pal_ctx->cfg);
217 GNUNET_assert (NULL != pal_ctx->client);
218 send_peer_mon_request (pal_ctx);
223 * Cut the existing connection and reconnect.
225 * @param pal_ctx our context
228 reconnect_peer_ctx (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
230 GNUNET_assert (GNUNET_NO == pal_ctx->one_shot);
231 GNUNET_CLIENT_disconnect (pal_ctx->client);
232 pal_ctx->client = NULL;
233 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
234 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
235 GNUNET_TIME_UNIT_ZERO_ABS);
236 pal_ctx->backoff = GNUNET_TIME_STD_BACKOFF (pal_ctx->backoff);
237 pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (pal_ctx->backoff,
244 * Function called with responses from the service.
246 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
247 * @param msg NULL on timeout or error, otherwise presumably a
248 * message with the human-readable address
251 peer_response_processor (void *cls,
252 const struct GNUNET_MessageHeader *msg)
254 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
255 struct PeerIterateResponseMessage *pir_msg;
256 struct GNUNET_HELLO_Address *address;
258 const char *transport_name;
265 if (pal_ctx->one_shot)
268 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
269 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
270 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
274 reconnect_peer_ctx (pal_ctx);
278 size = ntohs (msg->size);
279 GNUNET_break (ntohs (msg->type) ==
280 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE);
281 if (size == sizeof (struct GNUNET_MessageHeader))
284 if (pal_ctx->one_shot)
286 /* iteration finished */
287 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
288 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
289 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
293 reconnect_peer_ctx (pal_ctx);
298 if ((size < sizeof (struct PeerIterateResponseMessage)) ||
299 (ntohs (msg->type) != GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE))
302 if (pal_ctx->one_shot)
304 /* iteration finished (with error) */
305 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
306 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
307 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
311 reconnect_peer_ctx (pal_ctx);
316 pir_msg = (struct PeerIterateResponseMessage *) msg;
317 tlen = ntohl (pir_msg->pluginlen);
318 alen = ntohl (pir_msg->addrlen);
320 if (size != sizeof (struct PeerIterateResponseMessage) + tlen + alen)
323 if (pal_ctx->one_shot)
325 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
326 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
327 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
331 reconnect_peer_ctx (pal_ctx);
336 if ( (0 == tlen) && (0 == alen) )
338 /* No address available */
339 pal_ctx->cb (pal_ctx->cb_cls, &pir_msg->peer, NULL,
340 ntohl(pir_msg->state),
341 GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
347 GNUNET_break (0); /* This must not happen: address without plugin */
350 addr = (const char *) &pir_msg[1];
351 transport_name = &addr[alen];
353 if (transport_name[tlen - 1] != '\0')
355 /* Corrupt plugin name */
357 if (pal_ctx->one_shot)
359 pal_ctx->cb (pal_ctx->cb_cls, NULL, NULL,
360 GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_ZERO_ABS);
361 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
365 reconnect_peer_ctx (pal_ctx);
371 address = GNUNET_HELLO_address_allocate (&pir_msg->peer,
372 transport_name, addr, alen, ntohl(pir_msg->local_address_info));
373 pal_ctx->cb (pal_ctx->cb_cls, &pir_msg->peer, address,
374 ntohl(pir_msg->state),
375 GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
376 GNUNET_HELLO_address_free (address);
380 /* expect more replies */
381 GNUNET_CLIENT_receive (pal_ctx->client, &peer_response_processor,
383 GNUNET_TIME_absolute_get_remaining (pal_ctx->timeout));
388 * Return information about a specific peer or all peers currently known to
389 * transport service once or in monitoring mode. To obtain information about
390 * a specific peer, a peer identity can be passed. To obtain information about
391 * all peers currently known to transport service, NULL can be passed as peer
394 * For each peer, the callback is called with information about the address used
395 * to communicate with this peer, the state this peer is currently in and the
396 * the current timeout for this state.
398 * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
399 * more time with 'NULL'. After this, the operation must no longer be
400 * explicitly canceled.
402 * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
405 * @param cfg configuration to use
406 * @param peer a specific peer identity to obtain information for,
408 * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
409 * #GNUNET_NO to monitor peers continuously
410 * @param timeout how long is the lookup allowed to take at most
411 * @param peer_callback function to call with the results
412 * @param peer_callback_cls closure for @a peer_address_callback
414 struct GNUNET_TRANSPORT_PeerMonitoringContext *
415 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
416 const struct GNUNET_PeerIdentity *peer,
418 struct GNUNET_TIME_Relative timeout,
419 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
420 void *peer_callback_cls)
422 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx;
423 struct GNUNET_CLIENT_Connection *client;
425 client = GNUNET_CLIENT_connect ("transport", cfg);
428 if (GNUNET_YES != one_shot)
429 timeout = GNUNET_TIME_UNIT_FOREVER_REL;
430 pal_ctx = GNUNET_new (struct GNUNET_TRANSPORT_PeerMonitoringContext);
431 pal_ctx->cb = peer_callback;
432 pal_ctx->cb_cls = peer_callback_cls;
434 pal_ctx->timeout = GNUNET_TIME_relative_to_absolute (timeout);
436 pal_ctx->peer = *peer;
437 pal_ctx->one_shot = one_shot;
438 pal_ctx->client = client;
439 send_peer_mon_request (pal_ctx);
446 * Cancel request to monitor peers
448 * @param pic handle for the request to cancel
451 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic)
453 if (NULL != pic->client)
455 GNUNET_CLIENT_disconnect (pic->client);
458 if (GNUNET_SCHEDULER_NO_TASK != pic->reconnect_task)
460 GNUNET_SCHEDULER_cancel (pic->reconnect_task);
461 pic->reconnect_task = GNUNET_SCHEDULER_NO_TASK;
467 /* end of transport_api_monitor_peers.c */