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.
17 * @file transport/transport_api_monitor_plugins.c
18 * @brief montoring api for transport plugin session status
19 * @author Christian Grothoff
22 #include "gnunet_util_lib.h"
23 #include "gnunet_arm_service.h"
24 #include "gnunet_hello_lib.h"
25 #include "gnunet_protocols.h"
26 #include "gnunet_transport_service.h"
27 #include "transport.h"
31 * Handle for a plugin session state monitor.
33 struct GNUNET_TRANSPORT_PluginMonitor
37 * Connection to the service.
39 struct GNUNET_MQ_Handle *mq;
44 const struct GNUNET_CONFIGURATION_Handle *cfg;
49 GNUNET_TRANSPORT_SessionMonitorCallback cb;
57 * Map of session_ids (reduced to 32-bits) to
58 * `struct GNUNET_TRANSPORT_PluginSession` objects.
60 struct GNUNET_CONTAINER_MultiHashMap32 *sessions;
63 * Backoff for reconnect.
65 struct GNUNET_TIME_Relative backoff;
68 * Task ID for reconnect.
70 struct GNUNET_SCHEDULER_Task *reconnect_task;
76 * Abstract representation of a plugin's session.
77 * Corresponds to the `struct GNUNET_ATS_Session` within the TRANSPORT service.
79 struct GNUNET_TRANSPORT_PluginSession
82 * Unique session identifier.
87 * Location for the client to store "data".
95 * Task run to re-establish the connection.
97 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
100 do_plugin_connect (void *cls);
104 * Free the session entry and notify the callback about its demise.
106 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor`
107 * @param key key of the session in the map
108 * @param value the session to free
109 * @return #GNUNET_OK (continue to iterate)
112 free_entry (void *cls,
116 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
117 struct GNUNET_TRANSPORT_PluginSession *ps = value;
123 GNUNET_break (GNUNET_YES ==
124 GNUNET_CONTAINER_multihashmap32_remove (pm->sessions,
127 GNUNET_break (NULL == ps->client_ctx);
134 * Cut the existing connection and reconnect.
136 * @param pm our context
139 reconnect_plugin_ctx (struct GNUNET_TRANSPORT_PluginMonitor *pm)
141 GNUNET_MQ_destroy (pm->mq);
143 GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
146 pm->backoff = GNUNET_TIME_STD_BACKOFF (pm->backoff);
147 pm->reconnect_task = GNUNET_SCHEDULER_add_delayed (pm->backoff,
154 * Convert 64-bit session ID to 32-bit index for hash map.
156 * @param id 64-bit session ID
157 * @return 32-bit hash map index
160 wrap_id (uint64_t id)
162 return ((uint32_t) id) ^ ((uint32_t) (id >> 32));
167 * Context for #locate_by_id().
175 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 (tpmm->header.size)) ||
231 ( (0 != pname_len) &&
232 ('\0' != pname[pname_len - 1]) ) )
235 return GNUNET_SYSERR;
242 * Function called with responses from the service.
244 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
245 * @paramm tpmm message with event data
248 handle_event (void *cls,
249 const struct TransportPluginMonitorMessage *tpmm)
251 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
252 struct GNUNET_TRANSPORT_PluginSession *ps;
255 enum GNUNET_TRANSPORT_SessionState ss;
258 struct GNUNET_TRANSPORT_SessionInfo info;
259 struct GNUNET_HELLO_Address addr;
260 struct SearchContext rv;
262 pname = (const char *) &tpmm[1];
263 pname_len = ntohs (tpmm->plugin_name_len);
264 paddr_len = ntohs (tpmm->plugin_address_len);
265 paddr = &pname[pname_len];
267 ss = (enum GNUNET_TRANSPORT_SessionState) ntohs (tpmm->session_state);
268 if (GNUNET_TRANSPORT_SS_INIT == ss)
270 ps = GNUNET_new (struct GNUNET_TRANSPORT_PluginSession);
271 ps->session_id = tpmm->session_id;
272 (void) GNUNET_CONTAINER_multihashmap32_put (pm->sessions,
273 wrap_id (tpmm->session_id),
275 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
280 rv.session_id = tpmm->session_id;
282 (void) GNUNET_CONTAINER_multihashmap32_get_multiple (pm->sessions,
283 wrap_id (tpmm->session_id),
290 reconnect_plugin_ctx (pm);
295 info.is_inbound = (int16_t) ntohs (tpmm->is_inbound);
296 info.num_msg_pending = ntohl (tpmm->msgs_pending);
297 info.num_bytes_pending = ntohl (tpmm->bytes_pending);
298 info.receive_delay = GNUNET_TIME_absolute_ntoh (tpmm->delay);
299 info.session_timeout = GNUNET_TIME_absolute_ntoh (tpmm->timeout);
300 info.address = &addr;
301 addr.peer = tpmm->peer;
302 addr.address = (0 == paddr_len) ? NULL : paddr;
303 addr.address_length = paddr_len;
304 addr.transport_name = (0 == pname_len) ? NULL : pname;
305 addr.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
311 if (GNUNET_TRANSPORT_SS_DONE == ss)
313 GNUNET_break (NULL == ps->client_ctx);
314 GNUNET_assert (GNUNET_YES ==
315 GNUNET_CONTAINER_multihashmap32_remove (pm->sessions,
316 wrap_id (tpmm->session_id),
324 * Function called with sync responses from the service.
326 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
327 * @param msg message from the service
330 handle_sync (void *cls,
331 const struct GNUNET_MessageHeader *msg)
333 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
335 /* we are in sync, notify callback */
344 * Generic error handler, called with the appropriate
345 * error code and the same closure specified at the creation of
347 * Not every message queue implementation supports an error handler.
349 * @param cls closure with the `struct GNUNET_NSE_Handle *`
350 * @param error error code
353 mq_error_handler (void *cls,
354 enum GNUNET_MQ_Error error)
356 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
358 reconnect_plugin_ctx (pm);
363 * Task run to re-establish the connection.
365 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
368 do_plugin_connect (void *cls)
370 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
371 struct GNUNET_MQ_MessageHandler handlers[] = {
372 GNUNET_MQ_hd_var_size (event,
373 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT,
374 struct TransportPluginMonitorMessage,
376 GNUNET_MQ_hd_fixed_size (sync,
377 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC,
378 struct GNUNET_MessageHeader,
380 GNUNET_MQ_handler_end ()
382 struct GNUNET_MessageHeader *msg;
383 struct GNUNET_MQ_Envelope *env;
385 pm->reconnect_task = NULL;
386 pm->mq = GNUNET_CLIENT_connect (pm->cfg,
393 env = GNUNET_MQ_msg (msg,
394 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_START);
395 GNUNET_MQ_send (pm->mq,
401 * Install a plugin session state monitor callback. The callback
402 * will be notified whenever the session changes.
404 * @param cfg configuration to use
405 * @param cb callback to invoke on events
406 * @param cb_cls closure for @a cb
407 * @return NULL on error, otherwise handle for cancellation
409 struct GNUNET_TRANSPORT_PluginMonitor *
410 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
411 GNUNET_TRANSPORT_SessionMonitorCallback cb,
414 struct GNUNET_TRANSPORT_PluginMonitor *pm;
416 pm = GNUNET_new (struct GNUNET_TRANSPORT_PluginMonitor);
420 do_plugin_connect (pm);
426 pm->sessions = GNUNET_CONTAINER_multihashmap32_create (128);
432 * Cancel monitoring the plugin session state. The callback will
433 * be called once for each session that is up with the information
434 * #GNUNET_TRANSPORT_SS_FINI (even though the session may stay up;
435 * this is just to enable client-side cleanup).
437 * @param pm handle of the request that is to be cancelled
440 GNUNET_TRANSPORT_monitor_plugins_cancel (struct GNUNET_TRANSPORT_PluginMonitor *pm)
444 GNUNET_MQ_destroy (pm->mq);
447 if (NULL != pm->reconnect_task)
449 GNUNET_SCHEDULER_cancel (pm->reconnect_task);
450 pm->reconnect_task = NULL;
452 GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
455 GNUNET_CONTAINER_multihashmap32_destroy (pm->sessions);
460 /* end of transport_api_monitor_plugins.c */