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/>.
20 * @file transport/transport_api_monitor_plugins.c
21 * @brief montoring api for transport plugin session status
22 * @author Christian Grothoff
25 #include "gnunet_util_lib.h"
26 #include "gnunet_arm_service.h"
27 #include "gnunet_hello_lib.h"
28 #include "gnunet_protocols.h"
29 #include "gnunet_transport_service.h"
30 #include "transport.h"
34 * Handle for a plugin session state monitor.
36 struct GNUNET_TRANSPORT_PluginMonitor
40 * Connection to the service.
42 struct GNUNET_MQ_Handle *mq;
47 const struct GNUNET_CONFIGURATION_Handle *cfg;
52 GNUNET_TRANSPORT_SessionMonitorCallback cb;
60 * Map of session_ids (reduced to 32-bits) to
61 * `struct GNUNET_TRANSPORT_PluginSession` objects.
63 struct GNUNET_CONTAINER_MultiHashMap32 *sessions;
66 * Backoff for reconnect.
68 struct GNUNET_TIME_Relative backoff;
71 * Task ID for reconnect.
73 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".
98 * Task run to re-establish the connection.
100 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
103 do_plugin_connect (void *cls);
107 * Free the session entry and notify the callback about its demise.
109 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor`
110 * @param key key of the session in the map
111 * @param value the session to free
112 * @return #GNUNET_OK (continue to iterate)
115 free_entry (void *cls,
119 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
120 struct GNUNET_TRANSPORT_PluginSession *ps = value;
126 GNUNET_break (GNUNET_YES ==
127 GNUNET_CONTAINER_multihashmap32_remove (pm->sessions,
130 GNUNET_break (NULL == ps->client_ctx);
137 * Cut the existing connection and reconnect.
139 * @param pm our context
142 reconnect_plugin_ctx (struct GNUNET_TRANSPORT_PluginMonitor *pm)
144 GNUNET_MQ_destroy (pm->mq);
146 GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
149 pm->backoff = GNUNET_TIME_STD_BACKOFF (pm->backoff);
150 pm->reconnect_task = GNUNET_SCHEDULER_add_delayed (pm->backoff,
157 * Convert 64-bit session ID to 32-bit index for hash map.
159 * @param id 64-bit session ID
160 * @return 32-bit hash map index
163 wrap_id (uint64_t id)
165 return ((uint32_t) id) ^ ((uint32_t) (id >> 32));
170 * Context for #locate_by_id().
178 struct GNUNET_TRANSPORT_PluginSession *ps;
189 * Locate a session entry.
191 * @param cls our `struct SearchContext`
192 * @param key key of the session in the map
193 * @param value a session
194 * @return #GNUNET_OK (continue to iterate), or #GNUNET_SYSERR (match found)
197 locate_by_id (void *cls,
201 struct SearchContext *sc = cls;
202 struct GNUNET_TRANSPORT_PluginSession *ps = value;
204 if (sc->session_id == ps->session_id)
207 return GNUNET_SYSERR;
214 * Function called with responses from the service.
216 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
217 * @paramm tpmm message with event data
218 * @return #GNUNET_Ok if message is well-formed
221 check_event (void *cls,
222 const struct TransportPluginMonitorMessage *tpmm)
228 pname = (const char *) &tpmm[1];
229 pname_len = ntohs (tpmm->plugin_name_len);
230 paddr_len = ntohs (tpmm->plugin_address_len);
233 sizeof (struct TransportPluginMonitorMessage) != ntohs (tpmm->header.size)) ||
234 ( (0 != pname_len) &&
235 ('\0' != pname[pname_len - 1]) ) )
238 return GNUNET_SYSERR;
245 * Function called with responses from the service.
247 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
248 * @paramm tpmm message with event data
251 handle_event (void *cls,
252 const struct TransportPluginMonitorMessage *tpmm)
254 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
255 struct GNUNET_TRANSPORT_PluginSession *ps;
258 enum GNUNET_TRANSPORT_SessionState ss;
261 struct GNUNET_TRANSPORT_SessionInfo info;
262 struct GNUNET_HELLO_Address addr;
263 struct SearchContext rv;
265 pname = (const char *) &tpmm[1];
266 pname_len = ntohs (tpmm->plugin_name_len);
267 paddr_len = ntohs (tpmm->plugin_address_len);
268 paddr = &pname[pname_len];
270 ss = (enum GNUNET_TRANSPORT_SessionState) ntohs (tpmm->session_state);
271 if (GNUNET_TRANSPORT_SS_INIT == ss)
273 ps = GNUNET_new (struct GNUNET_TRANSPORT_PluginSession);
274 ps->session_id = tpmm->session_id;
275 (void) GNUNET_CONTAINER_multihashmap32_put (pm->sessions,
276 wrap_id (tpmm->session_id),
278 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
283 rv.session_id = tpmm->session_id;
285 (void) GNUNET_CONTAINER_multihashmap32_get_multiple (pm->sessions,
286 wrap_id (tpmm->session_id),
293 reconnect_plugin_ctx (pm);
298 info.is_inbound = (int16_t) ntohs (tpmm->is_inbound);
299 info.num_msg_pending = ntohl (tpmm->msgs_pending);
300 info.num_bytes_pending = ntohl (tpmm->bytes_pending);
301 info.receive_delay = GNUNET_TIME_absolute_ntoh (tpmm->delay);
302 info.session_timeout = GNUNET_TIME_absolute_ntoh (tpmm->timeout);
303 info.address = &addr;
304 addr.peer = tpmm->peer;
305 addr.address = (0 == paddr_len) ? NULL : paddr;
306 addr.address_length = paddr_len;
307 addr.transport_name = (0 == pname_len) ? NULL : pname;
308 addr.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
314 if (GNUNET_TRANSPORT_SS_DONE == ss)
316 GNUNET_break (NULL == ps->client_ctx);
317 GNUNET_assert (GNUNET_YES ==
318 GNUNET_CONTAINER_multihashmap32_remove (pm->sessions,
319 wrap_id (tpmm->session_id),
327 * Function called with sync responses from the service.
329 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
330 * @param msg message from the service
333 handle_sync (void *cls,
334 const struct GNUNET_MessageHeader *msg)
336 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
338 /* we are in sync, notify callback */
347 * Generic error handler, called with the appropriate
348 * error code and the same closure specified at the creation of
350 * Not every message queue implementation supports an error handler.
352 * @param cls closure with the `struct GNUNET_NSE_Handle *`
353 * @param error error code
356 mq_error_handler (void *cls,
357 enum GNUNET_MQ_Error error)
359 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
361 reconnect_plugin_ctx (pm);
366 * Task run to re-establish the connection.
368 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
371 do_plugin_connect (void *cls)
373 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
374 struct GNUNET_MQ_MessageHandler handlers[] = {
375 GNUNET_MQ_hd_var_size (event,
376 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT,
377 struct TransportPluginMonitorMessage,
379 GNUNET_MQ_hd_fixed_size (sync,
380 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC,
381 struct GNUNET_MessageHeader,
383 GNUNET_MQ_handler_end ()
385 struct GNUNET_MessageHeader *msg;
386 struct GNUNET_MQ_Envelope *env;
388 pm->reconnect_task = NULL;
389 pm->mq = GNUNET_CLIENT_connect (pm->cfg,
396 env = GNUNET_MQ_msg (msg,
397 GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_START);
398 GNUNET_MQ_send (pm->mq,
404 * Install a plugin session state monitor callback. The callback
405 * will be notified whenever the session changes.
407 * @param cfg configuration to use
408 * @param cb callback to invoke on events
409 * @param cb_cls closure for @a cb
410 * @return NULL on error, otherwise handle for cancellation
412 struct GNUNET_TRANSPORT_PluginMonitor *
413 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
414 GNUNET_TRANSPORT_SessionMonitorCallback cb,
417 struct GNUNET_TRANSPORT_PluginMonitor *pm;
419 pm = GNUNET_new (struct GNUNET_TRANSPORT_PluginMonitor);
423 do_plugin_connect (pm);
429 pm->sessions = GNUNET_CONTAINER_multihashmap32_create (128);
435 * Cancel monitoring the plugin session state. The callback will
436 * be called once for each session that is up with the information
437 * #GNUNET_TRANSPORT_SS_FINI (even though the session may stay up;
438 * this is just to enable client-side cleanup).
440 * @param pm handle of the request that is to be cancelled
443 GNUNET_TRANSPORT_monitor_plugins_cancel (struct 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 */