2 This file is part of GNUnet.
3 Copyright (C) 2009-2014, 2016 GNUnet e.V.
5 GNUnet is free software: you can redistribute it and/or modify it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file transport/transport_api_monitor_peers.c
21 * @brief montoring api for transport peer status
23 * This api provides the ability to query the transport service about
24 * the connection status of a specific or all peers.
26 * Calls back with information about peer(s) including address used, state and
27 * state timeout for peer requests.
30 #include "gnunet_util_lib.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_transport_service.h"
35 #include "transport.h"
38 * Context for iterating validation entries.
40 struct GNUNET_TRANSPORT_PeerMonitoringContext
43 * Function to call with the binary address.
45 GNUNET_TRANSPORT_PeerIterateCallback cb;
53 * Connection to the service.
55 struct GNUNET_MQ_Handle *mq;
58 * Configuration we use.
60 const struct GNUNET_CONFIGURATION_Handle *cfg;
63 * Backoff for reconnect.
65 struct GNUNET_TIME_Relative backoff;
68 * Task ID for reconnect.
70 struct GNUNET_SCHEDULER_Task *reconnect_task;
73 * Identity of the peer to monitor.
75 struct GNUNET_PeerIdentity peer;
78 * Was this a one-shot request?
85 * Check if a state is defined as connected
87 * @param state the state value
88 * @return #GNUNET_YES or #GNUNET_NO
91 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state)
95 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
96 case GNUNET_TRANSPORT_PS_INIT_ATS:
97 case GNUNET_TRANSPORT_PS_SYN_SENT:
98 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
99 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
101 case GNUNET_TRANSPORT_PS_CONNECTED:
102 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
103 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
104 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
106 case GNUNET_TRANSPORT_PS_DISCONNECT:
107 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
110 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111 "Unhandled state `%s'\n",
112 GNUNET_TRANSPORT_ps2s (state));
116 return GNUNET_SYSERR;
121 * Convert peer state to human-readable string.
123 * @param state the state value
124 * @return corresponding string
127 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state)
131 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
132 return "S_NOT_CONNECTED";
133 case GNUNET_TRANSPORT_PS_INIT_ATS:
135 case GNUNET_TRANSPORT_PS_SYN_SENT:
137 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
138 return "S_SYN_RECV_ATS";
139 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
140 return "S_SYN_RECV_ACK";
141 case GNUNET_TRANSPORT_PS_CONNECTED:
142 return "S_CONNECTED";
143 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
144 return "S_RECONNECT_ATS";
145 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
146 return "S_RECONNECT_SENT";
147 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
148 return "S_SWITCH_SYN_SENT";
149 case GNUNET_TRANSPORT_PS_DISCONNECT:
150 return "S_DISCONNECT";
151 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
152 return "S_DISCONNECT_FINISHED";
161 * Task run to re-establish the connection.
163 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
166 do_peer_connect (void *cls);
170 * Cut the existing connection and reconnect.
172 * @param pal_ctx our context
175 reconnect_peer_ctx (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
177 GNUNET_assert (GNUNET_NO == pal_ctx->one_shot);
178 GNUNET_MQ_destroy (pal_ctx->mq);
180 pal_ctx->cb (pal_ctx->cb_cls,
183 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
184 GNUNET_TIME_UNIT_ZERO_ABS);
185 pal_ctx->backoff = GNUNET_TIME_STD_BACKOFF (pal_ctx->backoff);
186 pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (pal_ctx->backoff,
193 * Function called with responses from the service.
195 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
196 * @param msg message from service
199 handle_response_end (void *cls,
200 const struct GNUNET_MessageHeader *msg)
202 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
204 if (pal_ctx->one_shot)
206 /* iteration finished */
207 pal_ctx->cb (pal_ctx->cb_cls,
210 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
211 GNUNET_TIME_UNIT_ZERO_ABS);
212 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
215 /* not quite what we expected, reconnect */
217 reconnect_peer_ctx (pal_ctx);
222 * Function called to check responses from the service.
224 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
225 * @param pir_msg message with the human-readable address
226 * @return #GNUNET_OK if @a pir_msg is well-formed
229 check_response (void *cls,
230 const struct PeerIterateResponseMessage *pir_msg)
232 uint16_t size = ntohs (pir_msg->header.size) - sizeof (*pir_msg);
233 size_t alen = ntohl (pir_msg->addrlen);
234 size_t tlen = ntohl (pir_msg->pluginlen);
236 const char *transport_name;
238 if (size != tlen + alen)
241 return GNUNET_SYSERR;
243 if ( (0 == tlen) && (0 == alen) )
247 GNUNET_break (0); /* This must not happen: address without plugin */
248 return GNUNET_SYSERR;
250 addr = (const char *) &pir_msg[1];
251 transport_name = &addr[alen];
252 if (transport_name[tlen - 1] != '\0')
255 return GNUNET_SYSERR;
262 * Function called with responses from the service.
264 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
265 * @param msg message with the human-readable address
268 handle_response (void *cls,
269 const struct PeerIterateResponseMessage *pir_msg)
271 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
272 struct GNUNET_HELLO_Address *address;
273 size_t alen = ntohl (pir_msg->addrlen);
274 size_t tlen = ntohl (pir_msg->pluginlen);
276 const char *transport_name;
281 /* No address available */
282 pal_ctx->cb (pal_ctx->cb_cls,
285 ntohl(pir_msg->state),
286 GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
289 addr = (const char *) &pir_msg[1];
290 transport_name = &addr[alen];
293 address = GNUNET_HELLO_address_allocate (&pir_msg->peer,
297 ntohl (pir_msg->local_address_info));
298 pal_ctx->cb (pal_ctx->cb_cls,
301 ntohl (pir_msg->state),
302 GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
303 GNUNET_HELLO_address_free (address);
309 * Generic error handler, called with the appropriate error code and
310 * the same closure specified at the creation of the message queue.
311 * Not every message queue implementation supports an error handler.
313 * @param cls closure with the `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
314 * @param error error code
317 mq_error_handler (void *cls,
318 enum GNUNET_MQ_Error error)
320 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
322 if (pal_ctx->one_shot)
325 pal_ctx->cb (pal_ctx->cb_cls,
328 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
329 GNUNET_TIME_UNIT_ZERO_ABS);
330 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
333 reconnect_peer_ctx (pal_ctx);
338 * Task run to re-establish the connection.
340 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
343 do_peer_connect (void *cls)
345 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
346 struct GNUNET_MQ_MessageHandler handlers[] = {
347 GNUNET_MQ_hd_var_size (response,
348 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE,
349 struct PeerIterateResponseMessage,
351 GNUNET_MQ_hd_fixed_size (response_end,
352 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE_END,
353 struct GNUNET_MessageHeader,
355 GNUNET_MQ_handler_end ()
357 struct PeerMonitorMessage *msg;
358 struct GNUNET_MQ_Envelope *env;
360 pal_ctx->reconnect_task = NULL;
361 pal_ctx->mq = GNUNET_CLIENT_connect (pal_ctx->cfg,
366 if (NULL == pal_ctx->mq)
368 env = GNUNET_MQ_msg (msg,
369 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST);
370 msg->one_shot = htonl (pal_ctx->one_shot);
371 msg->peer = pal_ctx->peer;
372 GNUNET_MQ_send (pal_ctx->mq,
378 * Return information about a specific peer or all peers currently known to
379 * transport service once or in monitoring mode. To obtain information about
380 * a specific peer, a peer identity can be passed. To obtain information about
381 * all peers currently known to transport service, NULL can be passed as peer
384 * For each peer, the callback is called with information about the address used
385 * to communicate with this peer, the state this peer is currently in and the
386 * the current timeout for this state.
388 * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
389 * more time with 'NULL'. After this, the operation must no longer be
390 * explicitly canceled.
392 * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
395 * @param cfg configuration to use
396 * @param peer a specific peer identity to obtain information for,
398 * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
399 * #GNUNET_NO to monitor peers continuously
400 * @param peer_callback function to call with the results
401 * @param peer_callback_cls closure for @a peer_address_callback
403 struct GNUNET_TRANSPORT_PeerMonitoringContext *
404 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
405 const struct GNUNET_PeerIdentity *peer,
407 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
408 void *peer_callback_cls)
410 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx
411 = GNUNET_new (struct GNUNET_TRANSPORT_PeerMonitoringContext);
413 pal_ctx->cb = peer_callback;
414 pal_ctx->cb_cls = peer_callback_cls;
417 pal_ctx->peer = *peer;
418 pal_ctx->one_shot = one_shot;
419 do_peer_connect (pal_ctx);
420 if (NULL == pal_ctx->mq)
422 GNUNET_free (pal_ctx);
430 * Cancel request to monitor peers
432 * @param pic handle for the request to cancel
435 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic)
439 GNUNET_MQ_destroy (pic->mq);
442 if (NULL != pic->reconnect_task)
444 GNUNET_SCHEDULER_cancel (pic->reconnect_task);
445 pic->reconnect_task = NULL;
451 /* end of transport_api_monitor_peers.c */