2 This file is part of GNUnet.
3 Copyright (C) 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_plugins.c
23 * @brief montoring api for transport plugin session status
24 * @author Christian Grothoff
27 #include "gnunet_util_lib.h"
28 #include "gnunet_arm_service.h"
29 #include "gnunet_hello_lib.h"
30 #include "gnunet_protocols.h"
31 #include "gnunet_transport_service.h"
32 #include "transport.h"
36 * Handle for a plugin session state monitor.
38 struct GNUNET_TRANSPORT_PluginMonitor
41 * Connection to the service.
43 struct GNUNET_MQ_Handle *mq;
48 const struct GNUNET_CONFIGURATION_Handle *cfg;
53 GNUNET_TRANSPORT_SessionMonitorCallback cb;
61 * Map of session_ids (reduced to 32-bits) to
62 * `struct GNUNET_TRANSPORT_PluginSession` objects.
64 struct GNUNET_CONTAINER_MultiHashMap32 *sessions;
67 * Backoff for reconnect.
69 struct GNUNET_TIME_Relative backoff;
72 * Task ID for reconnect.
74 struct GNUNET_SCHEDULER_Task *reconnect_task;
79 * Abstract representation of a plugin's session.
80 * Corresponds to the `struct GNUNET_ATS_Session` within the TRANSPORT service.
82 struct GNUNET_TRANSPORT_PluginSession
85 * Unique session identifier.
90 * Location for the client to store "data".
97 * Task run to re-establish the connection.
99 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
102 do_plugin_connect (void *cls);
106 * Free the session entry and notify the callback about its demise.
108 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor`
109 * @param key key of the session in the map
110 * @param value the session to free
111 * @return #GNUNET_OK (continue to iterate)
114 free_entry (void *cls,
118 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
119 struct GNUNET_TRANSPORT_PluginSession *ps = value;
125 GNUNET_break (GNUNET_YES ==
126 GNUNET_CONTAINER_multihashmap32_remove (pm->sessions,
129 GNUNET_break (NULL == ps->client_ctx);
136 * Cut the existing connection and reconnect.
138 * @param pm our context
141 reconnect_plugin_ctx (struct GNUNET_TRANSPORT_PluginMonitor *pm)
143 GNUNET_MQ_destroy (pm->mq);
145 GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
148 pm->backoff = GNUNET_TIME_STD_BACKOFF (pm->backoff);
149 pm->reconnect_task = GNUNET_SCHEDULER_add_delayed (pm->backoff,
156 * Convert 64-bit session ID to 32-bit index for hash map.
158 * @param id 64-bit session ID
159 * @return 32-bit hash map index
162 wrap_id (uint64_t id)
164 return ((uint32_t) id) ^ ((uint32_t) (id >> 32));
169 * Context for #locate_by_id().
176 struct GNUNET_TRANSPORT_PluginSession *ps;
186 * Locate a session entry.
188 * @param cls our `struct SearchContext`
189 * @param key key of the session in the map
190 * @param value a session
191 * @return #GNUNET_OK (continue to iterate), or #GNUNET_SYSERR (match found)
194 locate_by_id (void *cls,
198 struct SearchContext *sc = cls;
199 struct GNUNET_TRANSPORT_PluginSession *ps = value;
201 if (sc->session_id == ps->session_id)
204 return GNUNET_SYSERR;
211 * Function called with responses from the service.
213 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
214 * @paramm tpmm message with event data
215 * @return #GNUNET_Ok if message is well-formed
218 check_event (void *cls,
219 const struct TransportPluginMonitorMessage *tpmm)
225 pname = (const char *) &tpmm[1];
226 pname_len = ntohs (tpmm->plugin_name_len);
227 paddr_len = ntohs (tpmm->plugin_address_len);
230 + sizeof(struct TransportPluginMonitorMessage) != ntohs (
231 tpmm->header.size)) ||
233 ('\0' != pname[pname_len - 1])))
236 return GNUNET_SYSERR;
243 * Function called with responses from the service.
245 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
246 * @paramm tpmm message with event data
249 handle_event (void *cls,
250 const struct TransportPluginMonitorMessage *tpmm)
252 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
253 struct GNUNET_TRANSPORT_PluginSession *ps;
256 enum GNUNET_TRANSPORT_SessionState ss;
259 struct GNUNET_TRANSPORT_SessionInfo info;
260 struct GNUNET_HELLO_Address addr;
261 struct SearchContext rv;
263 pname = (const char *) &tpmm[1];
264 pname_len = ntohs (tpmm->plugin_name_len);
265 paddr_len = ntohs (tpmm->plugin_address_len);
266 paddr = &pname[pname_len];
268 ss = (enum GNUNET_TRANSPORT_SessionState) ntohs (tpmm->session_state);
269 if (GNUNET_TRANSPORT_SS_INIT == ss)
271 ps = GNUNET_new (struct GNUNET_TRANSPORT_PluginSession);
272 ps->session_id = tpmm->session_id;
273 (void) GNUNET_CONTAINER_multihashmap32_put (pm->sessions,
274 wrap_id (tpmm->session_id),
276 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
280 rv.session_id = tpmm->session_id;
282 (void) GNUNET_CONTAINER_multihashmap32_get_multiple (pm->sessions,
291 reconnect_plugin_ctx (pm);
296 info.is_inbound = (int16_t) ntohs (tpmm->is_inbound);
297 info.num_msg_pending = ntohl (tpmm->msgs_pending);
298 info.num_bytes_pending = ntohl (tpmm->bytes_pending);
299 info.receive_delay = GNUNET_TIME_absolute_ntoh (tpmm->delay);
300 info.session_timeout = GNUNET_TIME_absolute_ntoh (tpmm->timeout);
301 info.address = &addr;
302 addr.peer = tpmm->peer;
303 addr.address = (0 == paddr_len) ? NULL : paddr;
304 addr.address_length = paddr_len;
305 addr.transport_name = (0 == pname_len) ? NULL : pname;
306 addr.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
312 if (GNUNET_TRANSPORT_SS_DONE == ss)
314 GNUNET_break (NULL == ps->client_ctx);
315 GNUNET_assert (GNUNET_YES ==
316 GNUNET_CONTAINER_multihashmap32_remove (pm->sessions,
326 * Function called with sync responses from the service.
328 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
329 * @param msg message from the service
332 handle_sync (void *cls,
333 const struct GNUNET_MessageHeader *msg)
335 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
337 /* we are in sync, notify callback */
346 * Generic error handler, called with the appropriate
347 * error code and the same closure specified at the creation of
349 * Not every message queue implementation supports an error handler.
351 * @param cls closure with the `struct GNUNET_NSE_Handle *`
352 * @param error error code
355 mq_error_handler (void *cls,
356 enum GNUNET_MQ_Error error)
358 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
360 reconnect_plugin_ctx (pm);
365 * Task run to re-establish the connection.
367 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
370 do_plugin_connect (void *cls)
372 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
373 struct GNUNET_MQ_MessageHandler handlers[] = {
374 GNUNET_MQ_hd_var_size (event,
375 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT,
376 struct TransportPluginMonitorMessage,
378 GNUNET_MQ_hd_fixed_size (sync,
379 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC,
380 struct GNUNET_MessageHeader,
382 GNUNET_MQ_handler_end ()
384 struct GNUNET_MessageHeader *msg;
385 struct GNUNET_MQ_Envelope *env;
387 pm->reconnect_task = NULL;
388 pm->mq = GNUNET_CLIENT_connect (pm->cfg,
395 env = GNUNET_MQ_msg (msg,
396 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_START);
397 GNUNET_MQ_send (pm->mq,
403 * Install a plugin session state monitor callback. The callback
404 * will be notified whenever the session changes.
406 * @param cfg configuration to use
407 * @param cb callback to invoke on events
408 * @param cb_cls closure for @a cb
409 * @return NULL on error, otherwise handle for cancellation
411 struct GNUNET_TRANSPORT_PluginMonitor *
412 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
413 GNUNET_TRANSPORT_SessionMonitorCallback cb,
416 struct GNUNET_TRANSPORT_PluginMonitor *pm;
418 pm = GNUNET_new (struct GNUNET_TRANSPORT_PluginMonitor);
422 do_plugin_connect (pm);
428 pm->sessions = GNUNET_CONTAINER_multihashmap32_create (128);
434 * Cancel monitoring the plugin session state. The callback will
435 * be called once for each session that is up with the information
436 * #GNUNET_TRANSPORT_SS_FINI (even though the session may stay up;
437 * this is just to enable client-side cleanup).
439 * @param pm handle of the request that is to be cancelled
442 GNUNET_TRANSPORT_monitor_plugins_cancel (struct
443 GNUNET_TRANSPORT_PluginMonitor *pm)
447 GNUNET_MQ_destroy (pm->mq);
450 if (NULL != pm->reconnect_task)
452 GNUNET_SCHEDULER_cancel (pm->reconnect_task);
453 pm->reconnect_task = NULL;
455 GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
458 GNUNET_CONTAINER_multihashmap32_destroy (pm->sessions);
463 /* end of transport_api_monitor_plugins.c */