2 This file is part of GNUnet.
3 Copyright (C) 2014 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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
42 * Connection to the service.
44 struct GNUNET_CLIENT_Connection *client;
49 const struct GNUNET_CONFIGURATION_Handle *cfg;
54 GNUNET_TRANSPORT_SessionMonitorCallback cb;
62 * Map of session_ids (reduced to 32-bits) to
63 * `struct GNUNET_TRANSPORT_PluginSession` objects.
65 struct GNUNET_CONTAINER_MultiHashMap32 *sessions;
68 * Backoff for reconnect.
70 struct GNUNET_TIME_Relative backoff;
73 * Task ID for reconnect.
75 struct GNUNET_SCHEDULER_Task * reconnect_task;
81 * Abstract representation of a plugin's session.
82 * Corresponds to the `struct Session` within the TRANSPORT service.
84 struct GNUNET_TRANSPORT_PluginSession
87 * Unique session identifier.
92 * Location for the client to store "data".
99 * Function called with responses from the service.
101 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
102 * @param msg NULL on timeout or error, otherwise presumably a
103 * message with the human-readable address
106 response_processor (void *cls,
107 const struct GNUNET_MessageHeader *msg);
111 * Send our subscription request to the service.
113 * @param pal_ctx our context
116 send_plugin_mon_request (struct GNUNET_TRANSPORT_PluginMonitor *pm)
118 struct GNUNET_MessageHeader msg;
120 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
121 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_START);
122 GNUNET_assert (GNUNET_OK ==
123 GNUNET_CLIENT_transmit_and_get_response (pm->client,
125 GNUNET_TIME_UNIT_FOREVER_REL,
133 * Task run to re-establish the connection.
135 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
136 * @param tc scheduler context, unused
139 do_plugin_connect (void *cls,
140 const struct GNUNET_SCHEDULER_TaskContext *tc)
142 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
144 pm->reconnect_task = NULL;
145 pm->client = GNUNET_CLIENT_connect ("transport", pm->cfg);
146 GNUNET_assert (NULL != pm->client);
147 send_plugin_mon_request (pm);
152 * Free the session entry and notify the callback about its demise.
154 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor`
155 * @param key key of the session in the map
156 * @param value the session to free
157 * @return #GNUNET_OK (continue to iterate)
160 free_entry (void *cls,
164 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
165 struct GNUNET_TRANSPORT_PluginSession *ps = value;
171 GNUNET_break (GNUNET_YES ==
172 GNUNET_CONTAINER_multihashmap32_remove (pm->sessions,
175 GNUNET_break (NULL == ps->client_ctx);
182 * Cut the existing connection and reconnect.
184 * @param pm our context
187 reconnect_plugin_ctx (struct GNUNET_TRANSPORT_PluginMonitor *pm)
189 GNUNET_CLIENT_disconnect (pm->client);
191 GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
194 pm->backoff = GNUNET_TIME_STD_BACKOFF (pm->backoff);
195 pm->reconnect_task = GNUNET_SCHEDULER_add_delayed (pm->backoff,
202 * Convert 64-bit session ID to 32-bit index for hash map.
204 * @param id 64-bit session ID
205 * @return 32-bit hash map index
208 wrap_id (uint64_t id)
210 return ((uint32_t) id) ^ ((uint32_t) (id >> 32));
215 * Context for #locate_by_id().
223 struct GNUNET_TRANSPORT_PluginSession *ps;
234 * Locate a session entry.
236 * @param cls our `struct SearchContext`
237 * @param key key of the session in the map
238 * @param value a session
239 * @return #GNUNET_OK (continue to iterate), or #GNUNET_SYSERR (match found)
242 locate_by_id (void *cls,
246 struct SearchContext *sc = cls;
247 struct GNUNET_TRANSPORT_PluginSession *ps = value;
249 if (sc->session_id == ps->session_id)
252 return GNUNET_SYSERR;
259 * Function called with responses from the service.
261 * @param cls our `struct GNUNET_TRANSPORT_PluginMonitor *`
262 * @param msg NULL on timeout or error, otherwise presumably a
263 * message with the human-readable address
266 response_processor (void *cls,
267 const struct GNUNET_MessageHeader *msg)
269 struct GNUNET_TRANSPORT_PluginMonitor *pm = cls;
270 const struct TransportPluginMonitorMessage *tpmm;
271 struct GNUNET_TRANSPORT_PluginSession *ps;
274 enum GNUNET_TRANSPORT_SessionState ss;
277 struct GNUNET_TRANSPORT_SessionInfo info;
278 struct GNUNET_HELLO_Address addr;
279 struct SearchContext rv;
283 reconnect_plugin_ctx (pm);
286 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_SYNC == ntohs (msg->type)) &&
287 (sizeof (struct GNUNET_MessageHeader) == ntohs (msg->size)) )
294 GNUNET_CLIENT_receive (pm->client,
297 GNUNET_TIME_UNIT_FOREVER_REL);
301 if ( (GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PLUGIN_EVENT != ntohs (msg->type)) ||
302 (sizeof (struct TransportPluginMonitorMessage) > ntohs (msg->size)) )
305 reconnect_plugin_ctx (pm);
308 tpmm = (const struct TransportPluginMonitorMessage *) msg;
309 pname = (const char *) &tpmm[1];
310 pname_len = ntohs (tpmm->plugin_name_len);
311 paddr_len = ntohs (tpmm->plugin_address_len);
314 sizeof (struct TransportPluginMonitorMessage) != ntohs (msg->size)) ||
315 ( (0 != pname_len) &&
316 ('\0' != pname[pname_len - 1]) ) )
319 reconnect_plugin_ctx (pm);
322 paddr = &pname[pname_len];
324 ss = (enum GNUNET_TRANSPORT_SessionState) ntohs (tpmm->session_state);
325 if (GNUNET_TRANSPORT_SS_INIT == ss)
327 ps = GNUNET_new (struct GNUNET_TRANSPORT_PluginSession);
328 ps->session_id = tpmm->session_id;
329 (void) GNUNET_CONTAINER_multihashmap32_put (pm->sessions,
330 wrap_id (tpmm->session_id),
332 GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
337 rv.session_id = tpmm->session_id;
339 (void) GNUNET_CONTAINER_multihashmap32_get_multiple (pm->sessions,
340 wrap_id (tpmm->session_id),
347 reconnect_plugin_ctx (pm);
352 info.is_inbound = (int16_t) ntohs (tpmm->is_inbound);
353 info.num_msg_pending = ntohl (tpmm->msgs_pending);
354 info.num_bytes_pending = ntohl (tpmm->bytes_pending);
355 info.receive_delay = GNUNET_TIME_absolute_ntoh (tpmm->delay);
356 info.session_timeout = GNUNET_TIME_absolute_ntoh (tpmm->timeout);
357 info.address = &addr;
358 addr.peer = tpmm->peer;
359 addr.address = (0 == paddr_len) ? NULL : paddr;
360 addr.address_length = paddr_len;
361 addr.transport_name = (0 == pname_len) ? NULL : pname;
362 addr.local_info = GNUNET_HELLO_ADDRESS_INFO_NONE;
368 if (GNUNET_TRANSPORT_SS_DONE == ss)
370 GNUNET_break (NULL == ps->client_ctx);
371 GNUNET_assert (GNUNET_YES ==
372 GNUNET_CONTAINER_multihashmap32_remove (pm->sessions,
373 wrap_id (tpmm->session_id),
377 GNUNET_CLIENT_receive (pm->client,
380 GNUNET_TIME_UNIT_FOREVER_REL);
385 * Install a plugin session state monitor callback. The callback
386 * will be notified whenever the session changes.
388 * @param cfg configuration to use
389 * @param cb callback to invoke on events
390 * @param cb_cls closure for @a cb
391 * @return NULL on error, otherwise handle for cancellation
393 struct GNUNET_TRANSPORT_PluginMonitor *
394 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
395 GNUNET_TRANSPORT_SessionMonitorCallback cb,
398 struct GNUNET_TRANSPORT_PluginMonitor *pm;
399 struct GNUNET_CLIENT_Connection *client;
401 client = GNUNET_CLIENT_connect ("transport",
405 pm = GNUNET_new (struct GNUNET_TRANSPORT_PluginMonitor);
410 pm->sessions = GNUNET_CONTAINER_multihashmap32_create (128);
411 send_plugin_mon_request (pm);
417 * Cancel monitoring the plugin session state. The callback will
418 * be called once for each session that is up with the information
419 * #GNUNET_TRANSPORT_SS_FINI (even though the session may stay up;
420 * this is just to enable client-side cleanup).
422 * @param pm handle of the request that is to be cancelled
425 GNUNET_TRANSPORT_monitor_plugins_cancel (struct GNUNET_TRANSPORT_PluginMonitor *pm)
427 if (NULL != pm->client)
429 GNUNET_CLIENT_disconnect (pm->client);
432 if (NULL != pm->reconnect_task)
434 GNUNET_SCHEDULER_cancel (pm->reconnect_task);
435 pm->reconnect_task = NULL;
437 GNUNET_CONTAINER_multihashmap32_iterate (pm->sessions,
440 GNUNET_CONTAINER_multihashmap32_destroy (pm->sessions);
445 /* end of transport_api_monitor_plugins.c */