From 2f3c9c69f7b6cea83930e8c927d35b519a5655a0 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Wed, 14 Nov 2018 14:35:49 +0100 Subject: add first sketch of libgnunettransportmonitor.so implementation --- src/include/gnunet_protocols.h | 27 ++- src/include/gnunet_transport_monitor_service.h | 18 +- src/transport/Makefile.am | 12 + src/transport/gnunet-service-tng.c | 5 - src/transport/transport.h | 83 +++++++ src/transport/transport_api2_monitor.c | 313 +++++++++++++++++++++++++ 6 files changed, 440 insertions(+), 18 deletions(-) create mode 100644 src/transport/transport_api2_monitor.c diff --git a/src/include/gnunet_protocols.h b/src/include/gnunet_protocols.h index fbdee5415..6f3b886d8 100644 --- a/src/include/gnunet_protocols.h +++ b/src/include/gnunet_protocols.h @@ -11,7 +11,7 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ @@ -3041,7 +3041,7 @@ extern "C" #define GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_SETUP 1204 /** - * @brief inform transport that a queue was torn down + * @brief inform transport that a queue was torn down */ #define GNUNET_MESSAGE_TYPE_TRANSPORT_QUEUE_TEARDOWN 1205 @@ -3072,12 +3072,31 @@ extern "C" /** * Message sent to indicate to the transport which address - * prefix is supported by a communicator. + * prefix is supported by a communicator. */ #define GNUNET_MESSAGE_TYPE_TRANSPORT_NEW_COMMUNICATOR 1211 + +/** + * Message sent to indicate to the transport that a monitor + * wants to observe certain events. + */ +#define GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_START 1250 + +/** + * Message sent to indicate to a monitor about events. + */ +#define GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_DATA 1251 + +/** + * Message sent to indicate to a monitor that a one-shot + * iteration over events is done. + */ +#define GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_END 1252 + + /** - * Next available: 1300 + * Next available: 1400 */ diff --git a/src/include/gnunet_transport_monitor_service.h b/src/include/gnunet_transport_monitor_service.h index b9d024d59..76fec0af4 100644 --- a/src/include/gnunet_transport_monitor_service.h +++ b/src/include/gnunet_transport_monitor_service.h @@ -11,7 +11,7 @@ WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. - + You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */ @@ -29,7 +29,6 @@ * * @{ */ - #ifndef GNUNET_TRANSPORT_MONITOR_SERVICE_H #define GNUNET_TRANSPORT_MONITOR_SERVICE_H @@ -42,6 +41,7 @@ extern "C" #endif #include "gnunet_util_lib.h" +#include "gnunet_ats_service.h" /** * Version number of the transport API. @@ -121,7 +121,7 @@ struct GNUNET_TRANSPORT_MonitorInformation * @param mi monitoring data on the peer */ typedef void -(*GNUNET_TRANSPORT_MontiorCallback) (void *cls, +(*GNUNET_TRANSPORT_MonitorCallback) (void *cls, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_TRANSPORT_MonitorInformation *mi); @@ -155,24 +155,24 @@ struct GNUNET_TRANSPORT_MonitorContext; * NULL for all peers * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL), * #GNUNET_NO to monitor peers continuously - * @param mc function to call with the results - * @param mc_cls closure for @a mc + * @param cb function to call with the results + * @param cb_cls closure for @a mc */ struct GNUNET_TRANSPORT_MonitorContext * GNUNET_TRANSPORT_monitor (const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_PeerIdentity *peer, int one_shot, - GNUNET_TRANSPORT_MonitorCallback mc, - void *mc_cls); + GNUNET_TRANSPORT_MonitorCallback cb, + void *cb_cls); /** * Cancel request to monitor peers * - * @param pmc handle for the request to cancel + * @param mc handle for the request to cancel */ void -GNUNET_TRANSPORT_monitor_cancel (struct GNUNET_TRANSPORT_MonitorContext *pmc); +GNUNET_TRANSPORT_monitor_cancel (struct GNUNET_TRANSPORT_MonitorContext *mc); #if 0 /* keep Emacsens' auto-indent happy */ diff --git a/src/transport/Makefile.am b/src/transport/Makefile.am index 92b53137f..2a549d413 100644 --- a/src/transport/Makefile.am +++ b/src/transport/Makefile.am @@ -151,6 +151,7 @@ endif lib_LTLIBRARIES = \ libgnunettransport.la \ libgnunettransportcommunicator.la \ + libgnunettransportmonitor.la \ $(TESTING_LIBS) libgnunettransporttesting_la_SOURCES = \ @@ -200,6 +201,17 @@ libgnunettransportcommunicator_la_LDFLAGS = \ $(GN_LIB_LDFLAGS) $(WINFLAGS) \ -version-info 0:0:0 + +libgnunettransportmonitor_la_SOURCES = \ + transport_api2_monitor.c +libgnunettransportmonitor_la_LIBADD = \ + $(top_builddir)/src/util/libgnunetutil.la \ + $(GN_LIBINTL) +libgnunettransportmonitor_la_LDFLAGS = \ + $(GN_LIB_LDFLAGS) $(WINFLAGS) \ + -version-info 0:0:0 + + libexec_PROGRAMS = \ $(WLAN_BIN) \ $(WLAN_BIN_DUMMY) \ diff --git a/src/transport/gnunet-service-tng.c b/src/transport/gnunet-service-tng.c index 8cbca3188..447e9fa5d 100644 --- a/src/transport/gnunet-service-tng.c +++ b/src/transport/gnunet-service-tng.c @@ -149,11 +149,6 @@ const struct GNUNET_CONFIGURATION_Handle *GST_cfg; */ struct GNUNET_PeerIdentity GST_my_identity; -/** - * Handle to peerinfo service. - */ -struct GNUNET_PEERINFO_Handle *GST_peerinfo; - /** * Our private key. */ diff --git a/src/transport/transport.h b/src/transport/transport.h index 1b46213cf..c5191a5ce 100644 --- a/src/transport/transport.h +++ b/src/transport/transport.h @@ -936,6 +936,89 @@ struct GNUNET_TRANSPORT_SendMessageToAck + +/** + * Request to start monitoring. + */ +struct GNUNET_TRANSPORT_MonitorStart +{ + + /** + * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_START. + */ + struct GNUNET_MessageHeader header; + + /** + * #GNUNET_YES for one-shot montoring, #GNUNET_NO for continuous monitoring. + */ + uint32_t one_shot; + + /** + * Target identifier to monitor, all zeros for "all peers". + */ + struct GNUNET_PeerIdentity peer; + +}; + + +/** + * Monitoring data. + */ +struct GNUNET_TRANSPORT_MonitorData +{ + + /** + * Type will be #GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_DATA. + */ + struct GNUNET_MessageHeader header; + + /** + * Network type (an `enum GNUNET_ATS_Network_Type` in NBO). + */ + uint32_t nt GNUNET_PACKED; + + /** + * Target identifier. + */ + struct GNUNET_PeerIdentity peer; + + /** + * @deprecated To be discussed if we keep these... + */ + struct GNUNET_TIME_AbsoluteNBO last_validation; + struct GNUNET_TIME_AbsoluteNBO valid_until; + struct GNUNET_TIME_AbsoluteNBO next_validation; + + /** + * Current round-trip time estimate. + */ + struct GNUNET_TIME_RelativeNBO rtt; + + /** + * Is inbound (in NBO). + */ + uint32_t is_inbound GNUNET_PACKED; + + /** + * Messages pending (in NBO). + */ + uint32_t num_msg_pending GNUNET_PACKED; + + /** + * Bytes pending (in NBO). + */ + uint32_t num_bytes_pending GNUNET_PACKED; + + /* Followed by 0-terminated address of the peer + (TODO: do we allow no address? If so, + adjust transport_api2_monitor!) */ + +}; + + + + + GNUNET_NETWORK_STRUCT_END /* end of transport.h */ diff --git a/src/transport/transport_api2_monitor.c b/src/transport/transport_api2_monitor.c new file mode 100644 index 000000000..d7b13ec74 --- /dev/null +++ b/src/transport/transport_api2_monitor.c @@ -0,0 +1,313 @@ +/* + This file is part of GNUnet. + Copyright (C) 2018 GNUnet e.V. + + GNUnet is free software: you can redistribute it and/or modify it + under the terms of the GNU Affero General Public License as published + by the Free Software Foundation, either version 3 of the License, + or (at your option) any later version. + + GNUnet is distributed in the hope that it will be useful, but + WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +/** + * @file transport/transport_api2_monitor.c + * @brief implementation of the gnunet_transport_monitor_service.h API + * @author Christian Grothoff + */ +#include "platform.h" +#include "gnunet_util_lib.h" +#include "gnunet_protocols.h" +#include "gnunet_transport_monitor_service.h" +#include "transport.h" + + +/** + * Opaque handle to the transport service for monitors. + */ +struct GNUNET_TRANSPORT_MonitorContext +{ + /** + * Our configuration. + */ + const struct GNUNET_CONFIGURATION_Handle *cfg; + + /** + * Queue to talk to the transport service. + */ + struct GNUNET_MQ_Handle *mq; + + /** + * Peer we monitor, all zeros for "all" + */ + struct GNUNET_PeerIdentity peer; + + /** + * #GNUNET_YES to return the current state and then end. + */ + int one_shot; + + /** + * Function to call with monitor data. + */ + GNUNET_TRANSPORT_MonitorCallback cb; + + /** + * Closure for @e cb. + */ + void *cb_cls; + +}; + + +/** + * (re)connect our monitor to the transport service + * + * @param mc handle to reconnect + */ +static void +reconnect (struct GNUNET_TRANSPORT_MonitorContext *mc); + + +/** + * Send message to the transport service about our montoring + * desire. + * + * @param ai address to delete + */ +static void +send_start_monitor (struct GNUNET_TRANSPORT_MonitorContext *mc) +{ + struct GNUNET_MQ_Envelope *env; + struct GNUNET_TRANSPORT_MonitorStart *smm; + + if (NULL == mc->mq) + return; + env = GNUNET_MQ_msg (smm, + GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_START); + smm->one_shot = htonl ((uint32_t) mc->one_shot); + smm->peer = mc->peer; + GNUNET_MQ_send (mc->mq, + env); +} + + +/** + * Disconnect from the transport service. + * + * @param mc service to disconnect from + */ +static void +disconnect (struct GNUNET_TRANSPORT_MonitorContext *mc) +{ + if (NULL == mc->mq) + return; + GNUNET_MQ_destroy (mc->mq); + mc->mq = NULL; +} + + +/** + * Function called on MQ errors. Reconnects to the service. + * + * @param cls our `struct GNUNET_TRANSPORT_MonitorContext *` + * @param error what error happened? + */ +static void +error_handler (void *cls, + enum GNUNET_MQ_Error error) +{ + struct GNUNET_TRANSPORT_MonitorContext *mc = cls; + + GNUNET_log (GNUNET_ERROR_TYPE_INFO, + "MQ failure %d, reconnecting to transport service.\n", + error); + disconnect (mc); + /* TODO: maybe do this with exponential backoff/delay */ + reconnect (mc); +} + + +/** + * Transport service sends us information about what is going on. + * Check if @a md is well-formed. + * + * @param cls our `struct GNUNET_TRANSPORT_MonitorContext *` + * @param md the monitor data we got + * @return #GNUNET_OK if @a smt is well-formed + */ +static int +check_monitor_data (void *cls, + const struct GNUNET_TRANSPORT_MonitorData *md) +{ + uint16_t len = ntohs (md->header.size) - sizeof (*md); + const char *addr = (const char *) &md[1]; + + (void) cls; + if ( (0 == len) || + ('\0' != addr[len-1]) ) + { + GNUNET_break (0); + return GNUNET_SYSERR; + } + return GNUNET_OK; +} + + +/** + * Transport service sends us information about what is going on. + * + * @param cls our `struct GNUNET_TRANSPORT_MonitorContext *` + * @param md monitor data + */ +static void +handle_monitor_data (void *cls, + const struct GNUNET_TRANSPORT_MonitorData *md) +{ + struct GNUNET_TRANSPORT_MonitorContext *mc = cls; + struct GNUNET_TRANSPORT_MonitorInformation mi; + + mi.address = (const char *) &md[1]; + mi.nt = (enum GNUNET_ATS_Network_Type) ntohl (md->nt); + mi.is_inbound = (int) ntohl (md->is_inbound); + mi.num_msg_pending = ntohl (md->num_msg_pending); + mi.num_bytes_pending = ntohl (md->num_bytes_pending); + mi.last_validation = GNUNET_TIME_absolute_ntoh (md->last_validation); + mi.valid_until = GNUNET_TIME_absolute_ntoh (md->valid_until); + mi.next_validation = GNUNET_TIME_absolute_ntoh (md->next_validation); + mi.rtt = GNUNET_TIME_relative_ntoh (md->rtt); + mc->cb (mc->cb_cls, + &md->peer, + &mi); +} + + +/** + * One shot was requested, and transport service is done. + * + * @param cls our `struct GNUNET_TRANSPORT_MonitorContext *` + * @param me end message + */ +static void +handle_monitor_end (void *cls, + const struct GNUNET_MessageHeader *me) +{ + struct GNUNET_TRANSPORT_MonitorContext *mc = cls; + + if (GNUNET_YES != mc->one_shot) + { + GNUNET_break (0); + disconnect (mc); + reconnect (mc); + return; + } + mc->cb (mc->cb_cls, + NULL, + NULL); + GNUNET_TRANSPORT_monitor_cancel (mc); +} + + +/** + * (re)connect our monitor to the transport service + * + * @param mc handle to reconnect + */ +static void +reconnect (struct GNUNET_TRANSPORT_MonitorContext *mc) +{ + struct GNUNET_MQ_MessageHandler handlers[] = { + GNUNET_MQ_hd_var_size (monitor_data, + GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_DATA, + struct GNUNET_TRANSPORT_MonitorData, + mc), + GNUNET_MQ_hd_fixed_size (monitor_end, + GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_END, + struct GNUNET_MessageHeader, + mc), + GNUNET_MQ_handler_end() + }; + + mc->mq = GNUNET_CLIENT_connect (mc->cfg, + "transport", + handlers, + &error_handler, + mc); + if (NULL == mc->mq) + return; + send_start_monitor (mc); +} + + +/** + * Return information about a specific peer or all peers currently known to + * transport service once or in monitoring mode. To obtain information about + * a specific peer, a peer identity can be passed. To obtain information about + * all peers currently known to transport service, NULL can be passed as peer + * identity. + * + * For each peer, the callback is called with information about the address used + * to communicate with this peer, the state this peer is currently in and the + * the current timeout for this state. + * + * Upon completion, the #GNUNET_TRANSPORT_PeerIterateCallback is called one + * more time with `NULL`. After this, the operation must no longer be + * explicitly canceled. + * + * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the + * the peer_callback! + * + * @param cfg configuration to use + * @param peer a specific peer identity to obtain information for, + * NULL for all peers + * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL), + * #GNUNET_NO to monitor peers continuously + * @param cb function to call with the results + * @param cb_cls closure for @a mc + */ +struct GNUNET_TRANSPORT_MonitorContext * +GNUNET_TRANSPORT_monitor (const struct GNUNET_CONFIGURATION_Handle *cfg, + const struct GNUNET_PeerIdentity *peer, + int one_shot, + GNUNET_TRANSPORT_MonitorCallback cb, + void *cb_cls) +{ + struct GNUNET_TRANSPORT_MonitorContext *mc; + + mc = GNUNET_new (struct GNUNET_TRANSPORT_MonitorContext); + mc->cfg = cfg; + if (NULL != peer) + mc->peer = *peer; + mc->one_shot = one_shot; + mc->cb = cb; + mc->cb_cls = cb_cls; + reconnect (mc); + if (NULL == mc->mq) + { + GNUNET_free (mc); + return NULL; + } + return mc; +} + + + +/** + * Cancel request to monitor peers + * + * @param pmc handle for the request to cancel + */ +void +GNUNET_TRANSPORT_monitor_cancel (struct GNUNET_TRANSPORT_MonitorContext *mc) +{ + disconnect (mc); + GNUNET_free (mc); +} + +/* end of transport_api2_monitor.c */ -- cgit v1.2.3