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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
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_MQ_Handle *mq;
60 * Configuration we use.
62 const struct GNUNET_CONFIGURATION_Handle *cfg;
65 * Backoff for reconnect.
67 struct GNUNET_TIME_Relative backoff;
70 * Task ID for reconnect.
72 struct GNUNET_SCHEDULER_Task *reconnect_task;
75 * Identity of the peer to monitor.
77 struct GNUNET_PeerIdentity peer;
80 * Was this a one-shot request?
87 * Check if a state is defined as connected
89 * @param state the state value
90 * @return #GNUNET_YES or #GNUNET_NO
93 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state)
97 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
98 case GNUNET_TRANSPORT_PS_INIT_ATS:
99 case GNUNET_TRANSPORT_PS_SYN_SENT:
100 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
101 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
104 case GNUNET_TRANSPORT_PS_CONNECTED:
105 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
106 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
107 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
110 case GNUNET_TRANSPORT_PS_DISCONNECT:
111 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
116 "Unhandled state `%s'\n",
117 GNUNET_TRANSPORT_ps2s (state));
121 return GNUNET_SYSERR;
126 * Convert peer state to human-readable string.
128 * @param state the state value
129 * @return corresponding string
132 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state)
136 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
137 return "S_NOT_CONNECTED";
139 case GNUNET_TRANSPORT_PS_INIT_ATS:
142 case GNUNET_TRANSPORT_PS_SYN_SENT:
145 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
146 return "S_SYN_RECV_ATS";
148 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
149 return "S_SYN_RECV_ACK";
151 case GNUNET_TRANSPORT_PS_CONNECTED:
152 return "S_CONNECTED";
154 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
155 return "S_RECONNECT_ATS";
157 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
158 return "S_RECONNECT_SENT";
160 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
161 return "S_SWITCH_SYN_SENT";
163 case GNUNET_TRANSPORT_PS_DISCONNECT:
164 return "S_DISCONNECT";
166 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
167 return "S_DISCONNECT_FINISHED";
177 * Task run to re-establish the connection.
179 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
182 do_peer_connect (void *cls);
186 * Cut the existing connection and reconnect.
188 * @param pal_ctx our context
191 reconnect_peer_ctx (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
193 GNUNET_assert (GNUNET_NO == pal_ctx->one_shot);
194 GNUNET_MQ_destroy (pal_ctx->mq);
196 pal_ctx->cb (pal_ctx->cb_cls,
199 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
200 GNUNET_TIME_UNIT_ZERO_ABS);
201 pal_ctx->backoff = GNUNET_TIME_STD_BACKOFF (pal_ctx->backoff);
202 pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (pal_ctx->backoff,
209 * Function called with responses from the service.
211 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
212 * @param msg message from service
215 handle_response_end (void *cls,
216 const struct GNUNET_MessageHeader *msg)
218 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
220 if (pal_ctx->one_shot)
222 /* iteration finished */
223 pal_ctx->cb (pal_ctx->cb_cls,
226 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
227 GNUNET_TIME_UNIT_ZERO_ABS);
228 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
231 /* not quite what we expected, reconnect */
233 reconnect_peer_ctx (pal_ctx);
238 * Function called to check responses from the service.
240 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
241 * @param pir_msg message with the human-readable address
242 * @return #GNUNET_OK if @a pir_msg is well-formed
245 check_response (void *cls,
246 const struct PeerIterateResponseMessage *pir_msg)
248 uint16_t size = ntohs (pir_msg->header.size) - sizeof(*pir_msg);
249 size_t alen = ntohl (pir_msg->addrlen);
250 size_t tlen = ntohl (pir_msg->pluginlen);
252 const char *transport_name;
254 if (size != tlen + alen)
257 return GNUNET_SYSERR;
259 if ((0 == tlen) && (0 == alen))
263 GNUNET_break (0); /* This must not happen: address without plugin */
264 return GNUNET_SYSERR;
266 addr = (const char *) &pir_msg[1];
267 transport_name = &addr[alen];
268 if (transport_name[tlen - 1] != '\0')
271 return GNUNET_SYSERR;
278 * Function called with responses from the service.
280 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
281 * @param msg message with the human-readable address
284 handle_response (void *cls,
285 const struct PeerIterateResponseMessage *pir_msg)
287 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
288 struct GNUNET_HELLO_Address *address;
289 size_t alen = ntohl (pir_msg->addrlen);
290 size_t tlen = ntohl (pir_msg->pluginlen);
292 const char *transport_name;
297 /* No address available */
298 pal_ctx->cb (pal_ctx->cb_cls,
301 ntohl (pir_msg->state),
302 GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
305 addr = (const char *) &pir_msg[1];
306 transport_name = &addr[alen];
309 address = GNUNET_HELLO_address_allocate (&pir_msg->peer,
313 ntohl (pir_msg->local_address_info));
314 pal_ctx->cb (pal_ctx->cb_cls,
317 ntohl (pir_msg->state),
318 GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
319 GNUNET_HELLO_address_free (address);
325 * Generic error handler, called with the appropriate error code and
326 * the same closure specified at the creation of the message queue.
327 * Not every message queue implementation supports an error handler.
329 * @param cls closure with the `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
330 * @param error error code
333 mq_error_handler (void *cls,
334 enum GNUNET_MQ_Error error)
336 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
338 if (pal_ctx->one_shot)
341 pal_ctx->cb (pal_ctx->cb_cls,
344 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
345 GNUNET_TIME_UNIT_ZERO_ABS);
346 GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
349 reconnect_peer_ctx (pal_ctx);
354 * Task run to re-establish the connection.
356 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
359 do_peer_connect (void *cls)
361 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
362 struct GNUNET_MQ_MessageHandler handlers[] = {
363 GNUNET_MQ_hd_var_size (response,
364 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE,
365 struct PeerIterateResponseMessage,
367 GNUNET_MQ_hd_fixed_size (response_end,
368 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE_END,
369 struct GNUNET_MessageHeader,
371 GNUNET_MQ_handler_end ()
373 struct PeerMonitorMessage *msg;
374 struct GNUNET_MQ_Envelope *env;
376 pal_ctx->reconnect_task = NULL;
377 pal_ctx->mq = GNUNET_CLIENT_connect (pal_ctx->cfg,
382 if (NULL == pal_ctx->mq)
384 env = GNUNET_MQ_msg (msg,
385 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST);
386 msg->one_shot = htonl (pal_ctx->one_shot);
387 msg->peer = pal_ctx->peer;
388 GNUNET_MQ_send (pal_ctx->mq,
394 * Return information about a specific peer or all peers currently known to
395 * transport service once or in monitoring mode. To obtain information about
396 * a specific peer, a peer identity can be passed. To obtain information about
397 * all peers currently known to transport service, NULL can be passed as peer
400 * For each peer, the callback is called with information about the address used
401 * to communicate with this peer, the state this peer is currently in and the
402 * the current timeout for this state.
404 * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
405 * more time with 'NULL'. After this, the operation must no longer be
406 * explicitly canceled.
408 * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
411 * @param cfg configuration to use
412 * @param peer a specific peer identity to obtain information for,
414 * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
415 * #GNUNET_NO to monitor peers continuously
416 * @param peer_callback function to call with the results
417 * @param peer_callback_cls closure for @a peer_address_callback
419 struct GNUNET_TRANSPORT_PeerMonitoringContext *
420 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
421 const struct GNUNET_PeerIdentity *peer,
423 GNUNET_TRANSPORT_PeerIterateCallback
425 void *peer_callback_cls)
427 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx
428 = GNUNET_new (struct GNUNET_TRANSPORT_PeerMonitoringContext);
430 pal_ctx->cb = peer_callback;
431 pal_ctx->cb_cls = peer_callback_cls;
434 pal_ctx->peer = *peer;
435 pal_ctx->one_shot = one_shot;
436 do_peer_connect (pal_ctx);
437 if (NULL == pal_ctx->mq)
439 GNUNET_free (pal_ctx);
447 * Cancel request to monitor peers
449 * @param pic handle for the request to cancel
452 GNUNET_TRANSPORT_monitor_peers_cancel (struct
453 GNUNET_TRANSPORT_PeerMonitoringContext *
458 GNUNET_MQ_destroy (pic->mq);
461 if (NULL != pic->reconnect_task)
463 GNUNET_SCHEDULER_cancel (pic->reconnect_task);
464 pic->reconnect_task = NULL;
470 /* end of transport_api_monitor_peers.c */