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 {
44 * Function to call with the binary address.
46 GNUNET_TRANSPORT_PeerIterateCallback cb;
54 * Connection to the service.
56 struct GNUNET_MQ_Handle *mq;
59 * Configuration we use.
61 const struct GNUNET_CONFIGURATION_Handle *cfg;
64 * Backoff for reconnect.
66 struct GNUNET_TIME_Relative backoff;
69 * Task ID for reconnect.
71 struct GNUNET_SCHEDULER_Task *reconnect_task;
74 * Identity of the peer to monitor.
76 struct GNUNET_PeerIdentity peer;
79 * Was this a one-shot request?
86 * Check if a state is defined as connected
88 * @param state the state value
89 * @return #GNUNET_YES or #GNUNET_NO
92 GNUNET_TRANSPORT_is_connected(enum GNUNET_TRANSPORT_PeerState state)
96 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
97 case GNUNET_TRANSPORT_PS_INIT_ATS:
98 case GNUNET_TRANSPORT_PS_SYN_SENT:
99 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
100 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
103 case GNUNET_TRANSPORT_PS_CONNECTED:
104 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
105 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
106 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
109 case GNUNET_TRANSPORT_PS_DISCONNECT:
110 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
114 GNUNET_log(GNUNET_ERROR_TYPE_ERROR,
115 "Unhandled state `%s'\n",
116 GNUNET_TRANSPORT_ps2s(state));
120 return GNUNET_SYSERR;
125 * Convert peer state to human-readable string.
127 * @param state the state value
128 * @return corresponding string
131 GNUNET_TRANSPORT_ps2s(enum GNUNET_TRANSPORT_PeerState state)
135 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
136 return "S_NOT_CONNECTED";
138 case GNUNET_TRANSPORT_PS_INIT_ATS:
141 case GNUNET_TRANSPORT_PS_SYN_SENT:
144 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
145 return "S_SYN_RECV_ATS";
147 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
148 return "S_SYN_RECV_ACK";
150 case GNUNET_TRANSPORT_PS_CONNECTED:
151 return "S_CONNECTED";
153 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
154 return "S_RECONNECT_ATS";
156 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
157 return "S_RECONNECT_SENT";
159 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
160 return "S_SWITCH_SYN_SENT";
162 case GNUNET_TRANSPORT_PS_DISCONNECT:
163 return "S_DISCONNECT";
165 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
166 return "S_DISCONNECT_FINISHED";
176 * Task run to re-establish the connection.
178 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
181 do_peer_connect(void *cls);
185 * Cut the existing connection and reconnect.
187 * @param pal_ctx our context
190 reconnect_peer_ctx(struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
192 GNUNET_assert(GNUNET_NO == pal_ctx->one_shot);
193 GNUNET_MQ_destroy(pal_ctx->mq);
195 pal_ctx->cb(pal_ctx->cb_cls,
198 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
199 GNUNET_TIME_UNIT_ZERO_ABS);
200 pal_ctx->backoff = GNUNET_TIME_STD_BACKOFF(pal_ctx->backoff);
201 pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed(pal_ctx->backoff,
208 * Function called with responses from the service.
210 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
211 * @param msg message from service
214 handle_response_end(void *cls,
215 const struct GNUNET_MessageHeader *msg)
217 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
219 if (pal_ctx->one_shot)
221 /* iteration finished */
222 pal_ctx->cb(pal_ctx->cb_cls,
225 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
226 GNUNET_TIME_UNIT_ZERO_ABS);
227 GNUNET_TRANSPORT_monitor_peers_cancel(pal_ctx);
230 /* not quite what we expected, reconnect */
232 reconnect_peer_ctx(pal_ctx);
237 * Function called to check responses from the service.
239 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
240 * @param pir_msg message with the human-readable address
241 * @return #GNUNET_OK if @a pir_msg is well-formed
244 check_response(void *cls,
245 const struct PeerIterateResponseMessage *pir_msg)
247 uint16_t size = ntohs(pir_msg->header.size) - sizeof(*pir_msg);
248 size_t alen = ntohl(pir_msg->addrlen);
249 size_t tlen = ntohl(pir_msg->pluginlen);
251 const char *transport_name;
253 if (size != tlen + alen)
256 return GNUNET_SYSERR;
258 if ((0 == tlen) && (0 == alen))
262 GNUNET_break(0); /* This must not happen: address without plugin */
263 return GNUNET_SYSERR;
265 addr = (const char *)&pir_msg[1];
266 transport_name = &addr[alen];
267 if (transport_name[tlen - 1] != '\0')
270 return GNUNET_SYSERR;
277 * Function called with responses from the service.
279 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
280 * @param msg message with the human-readable address
283 handle_response(void *cls,
284 const struct PeerIterateResponseMessage *pir_msg)
286 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
287 struct GNUNET_HELLO_Address *address;
288 size_t alen = ntohl(pir_msg->addrlen);
289 size_t tlen = ntohl(pir_msg->pluginlen);
291 const char *transport_name;
296 /* No address available */
297 pal_ctx->cb(pal_ctx->cb_cls,
300 ntohl(pir_msg->state),
301 GNUNET_TIME_absolute_ntoh(pir_msg->state_timeout));
304 addr = (const char *)&pir_msg[1];
305 transport_name = &addr[alen];
308 address = GNUNET_HELLO_address_allocate(&pir_msg->peer,
312 ntohl(pir_msg->local_address_info));
313 pal_ctx->cb(pal_ctx->cb_cls,
316 ntohl(pir_msg->state),
317 GNUNET_TIME_absolute_ntoh(pir_msg->state_timeout));
318 GNUNET_HELLO_address_free(address);
324 * Generic error handler, called with the appropriate error code and
325 * the same closure specified at the creation of the message queue.
326 * Not every message queue implementation supports an error handler.
328 * @param cls closure with the `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
329 * @param error error code
332 mq_error_handler(void *cls,
333 enum GNUNET_MQ_Error error)
335 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
337 if (pal_ctx->one_shot)
340 pal_ctx->cb(pal_ctx->cb_cls,
343 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
344 GNUNET_TIME_UNIT_ZERO_ABS);
345 GNUNET_TRANSPORT_monitor_peers_cancel(pal_ctx);
348 reconnect_peer_ctx(pal_ctx);
353 * Task run to re-establish the connection.
355 * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
358 do_peer_connect(void *cls)
360 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
361 struct GNUNET_MQ_MessageHandler handlers[] = {
362 GNUNET_MQ_hd_var_size(response,
363 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE,
364 struct PeerIterateResponseMessage,
366 GNUNET_MQ_hd_fixed_size(response_end,
367 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE_END,
368 struct GNUNET_MessageHeader,
370 GNUNET_MQ_handler_end()
372 struct PeerMonitorMessage *msg;
373 struct GNUNET_MQ_Envelope *env;
375 pal_ctx->reconnect_task = NULL;
376 pal_ctx->mq = GNUNET_CLIENT_connect(pal_ctx->cfg,
381 if (NULL == pal_ctx->mq)
383 env = GNUNET_MQ_msg(msg,
384 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST);
385 msg->one_shot = htonl(pal_ctx->one_shot);
386 msg->peer = pal_ctx->peer;
387 GNUNET_MQ_send(pal_ctx->mq,
393 * Return information about a specific peer or all peers currently known to
394 * transport service once or in monitoring mode. To obtain information about
395 * a specific peer, a peer identity can be passed. To obtain information about
396 * all peers currently known to transport service, NULL can be passed as peer
399 * For each peer, the callback is called with information about the address used
400 * to communicate with this peer, the state this peer is currently in and the
401 * the current timeout for this state.
403 * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
404 * more time with 'NULL'. After this, the operation must no longer be
405 * explicitly canceled.
407 * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
410 * @param cfg configuration to use
411 * @param peer a specific peer identity to obtain information for,
413 * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
414 * #GNUNET_NO to monitor peers continuously
415 * @param peer_callback function to call with the results
416 * @param peer_callback_cls closure for @a peer_address_callback
418 struct GNUNET_TRANSPORT_PeerMonitoringContext *
419 GNUNET_TRANSPORT_monitor_peers(const struct GNUNET_CONFIGURATION_Handle *cfg,
420 const struct GNUNET_PeerIdentity *peer,
422 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
423 void *peer_callback_cls)
425 struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx
426 = GNUNET_new(struct GNUNET_TRANSPORT_PeerMonitoringContext);
428 pal_ctx->cb = peer_callback;
429 pal_ctx->cb_cls = peer_callback_cls;
432 pal_ctx->peer = *peer;
433 pal_ctx->one_shot = one_shot;
434 do_peer_connect(pal_ctx);
435 if (NULL == pal_ctx->mq)
437 GNUNET_free(pal_ctx);
445 * Cancel request to monitor peers
447 * @param pic handle for the request to cancel
450 GNUNET_TRANSPORT_monitor_peers_cancel(struct GNUNET_TRANSPORT_PeerMonitoringContext *pic)
454 GNUNET_MQ_destroy(pic->mq);
457 if (NULL != pic->reconnect_task)
459 GNUNET_SCHEDULER_cancel(pic->reconnect_task);
460 pic->reconnect_task = NULL;
466 /* end of transport_api_monitor_peers.c */