GNUnet  0.10.x
transport_api2_monitor.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet.
3  Copyright (C) 2018 GNUnet e.V.
4 
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.
9 
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.
14 
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/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
20 
26 #include "platform.h"
27 #include "gnunet_util_lib.h"
28 #include "gnunet_protocols.h"
30 #include "transport.h"
31 
32 
41 
46 
51 
55  int one_shot;
56 
61 
65  void *cb_cls;
66 };
67 
68 
74 static void
76 
77 
84 static void
86 {
87  struct GNUNET_MQ_Envelope *env;
88  struct GNUNET_TRANSPORT_MonitorStart *smm;
89 
90  if (NULL == mc->mq)
91  return;
93  smm->one_shot = htonl((uint32_t)mc->one_shot);
94  smm->peer = mc->peer;
95  GNUNET_MQ_send(mc->mq, env);
96 }
97 
98 
104 static void
106 {
107  if (NULL == mc->mq)
108  return;
109  GNUNET_MQ_destroy(mc->mq);
110  mc->mq = NULL;
111 }
112 
113 
120 static void
121 error_handler(void *cls, enum GNUNET_MQ_Error error)
122 {
123  struct GNUNET_TRANSPORT_MonitorContext *mc = cls;
124 
126  "MQ failure %d, reconnecting to transport service.\n",
127  error);
128  disconnect(mc);
129  /* TODO: maybe do this with exponential backoff/delay */
130  reconnect(mc);
131 }
132 
133 
142 static int
143 check_monitor_data(void *cls, const struct GNUNET_TRANSPORT_MonitorData *md)
144 {
145  (void)cls;
147  return GNUNET_OK;
148 }
149 
150 
157 static void
158 handle_monitor_data(void *cls, const struct GNUNET_TRANSPORT_MonitorData *md)
159 {
160  struct GNUNET_TRANSPORT_MonitorContext *mc = cls;
162 
163  mi.address = (const char *)&md[1];
164  mi.nt = (enum GNUNET_NetworkType)ntohl(md->nt);
165  mi.cs = (enum GNUNET_TRANSPORT_ConnectionStatus)ntohl(md->cs);
166  mi.num_msg_pending = ntohl(md->num_msg_pending);
167  mi.num_bytes_pending = ntohl(md->num_bytes_pending);
168  mi.last_validation = GNUNET_TIME_absolute_ntoh(md->last_validation);
169  mi.valid_until = GNUNET_TIME_absolute_ntoh(md->valid_until);
170  mi.next_validation = GNUNET_TIME_absolute_ntoh(md->next_validation);
171  mi.rtt = GNUNET_TIME_relative_ntoh(md->rtt);
172  mc->cb(mc->cb_cls, &md->peer, &mi);
173 }
174 
175 
182 static void
183 handle_monitor_end(void *cls, const struct GNUNET_MessageHeader *me)
184 {
185  struct GNUNET_TRANSPORT_MonitorContext *mc = cls;
186 
187  if (GNUNET_YES != mc->one_shot)
188  {
189  GNUNET_break(0);
190  disconnect(mc);
191  reconnect(mc);
192  return;
193  }
194  mc->cb(mc->cb_cls, NULL, NULL);
196 }
197 
198 
204 static void
206 {
207  struct GNUNET_MQ_MessageHandler handlers[] =
208  { GNUNET_MQ_hd_var_size(monitor_data,
210  struct GNUNET_TRANSPORT_MonitorData,
211  mc),
212  GNUNET_MQ_hd_fixed_size(monitor_end,
214  struct GNUNET_MessageHeader,
215  mc),
217 
218  mc->mq =
219  GNUNET_CLIENT_connect(mc->cfg, "transport", handlers, &error_handler, mc);
220  if (NULL == mc->mq)
221  return;
222  send_start_monitor(mc);
223 }
224 
225 
254  const struct GNUNET_PeerIdentity *peer,
255  int one_shot,
257  void *cb_cls)
258 {
260 
262  mc->cfg = cfg;
263  if (NULL != peer)
264  mc->peer = *peer;
265  mc->one_shot = one_shot;
266  mc->cb = cb;
267  mc->cb_cls = cb_cls;
268  reconnect(mc);
269  if (NULL == mc->mq)
270  {
271  GNUNET_free(mc);
272  return NULL;
273  }
274  return mc;
275 }
276 
277 
283 void
285 {
286  disconnect(mc);
287  GNUNET_free(mc);
288 }
289 
290 /* end of transport_api2_monitor.c */
Information about another peer&#39;s address.
uint32_t num_bytes_pending
Number of bytes pending transmission for this address.
static GNUNET_NETWORK_STRUCT_END struct GNUNET_PeerIdentity me
Our own peer identity.
uint32_t num_msg_pending
Number of messages pending transmission for this address.
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:671
common internal definitions for transport service
struct GNUNET_MQ_Handle * GNUNET_CLIENT_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *service_name, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *error_handler_cls)
Create a message queue to connect to a GNUnet service.
Definition: client.c:900
GNUNET_MQ_Error
Error codes for the queue.
struct GNUNET_TIME_Absolute valid_until
When does this address expire.
#define GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_START
Message sent to indicate to the transport that a monitor wants to observe certain events...
struct GNUNET_TIME_Absolute last_validation
When was this address last validated.
#define GNUNET_MQ_hd_fixed_size(name, code, str, ctx)
#define GNUNET_MQ_msg(mvar, type)
Allocate a GNUNET_MQ_Envelope.
Definition: gnunet_mq_lib.h:67
enum GNUNET_TRANSPORT_ConnectionStatus cs
Connection status.
static void error_handler(void *cls, enum GNUNET_MQ_Error error)
Function called on MQ errors.
#define GNUNET_OK
Named constants for return values.
Definition: gnunet_common.h:75
static void handle_monitor_data(void *cls, const struct GNUNET_TRANSPORT_MonitorData *md)
Transport service sends us information about what is going on.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
GNUNET_NetworkType
Types of networks (with separate quotas) we support.
Definition: gnunet_nt_lib.h:35
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
static int check_monitor_data(void *cls, const struct GNUNET_TRANSPORT_MonitorData *md)
Transport service sends us information about what is going on.
const char * address
Address we have for the peer, human-readable, 0-terminated, in UTF-8.
struct GNUNET_PeerIdentity peer
Peer we monitor, all zeros for "all".
#define GNUNET_MQ_check_zero_termination(m)
Insert code for a "check_" function that verifies that a given variable-length message received over ...
#define GNUNET_MQ_hd_var_size(name, code, str, ctx)
#define GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_DATA
Message sent to indicate to a monitor about events.
Opaque handle to the transport service for monitors.
int one_shot
GNUNET_YES to return the current state and then end.
void(* GNUNET_TRANSPORT_MonitorCallback)(void *cls, const struct GNUNET_PeerIdentity *peer, const struct GNUNET_TRANSPORT_MonitorInformation *mi)
Function to call with information about a peer.
static void handle_monitor_end(void *cls, const struct GNUNET_MessageHeader *me)
One shot was requested, and transport service is done.
struct GNUNET_TIME_Absolute next_validation
Time of the next validation operation.
static void reconnect(struct GNUNET_TRANSPORT_MonitorContext *mc)
(re)connect our monitor to the transport service
static void send_start_monitor(struct GNUNET_TRANSPORT_MonitorContext *mc)
Send message to the transport service about our montoring desire.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
Definition: time.c:639
struct GNUNET_MQ_Handle * mq
Queue to talk to the transport service.
Message handler for a specific message type.
static struct GNUNET_TESTBED_Controller * mc
Handle to the master controller.
struct GNUNET_TIME_Relative rtt
Current estimate of the RTT.
GNUNET_TRANSPORT_MonitorCallback cb
Function to call with monitor data.
struct GNUNET_MQ_Envelope * env
Definition: 005.c:1
enum GNUNET_NetworkType nt
Network type of the address.
Handle to a message queue.
Definition: mq.c:84
The identity of the host (wraps the signing key of the peer).
configuration data
Definition: configuration.c:83
void GNUNET_TRANSPORT_monitor_cancel(struct GNUNET_TRANSPORT_MonitorContext *mc)
Cancel request to monitor peers.
#define GNUNET_log(kind,...)
Header for all communications.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
Definition: mq.c:821
#define GNUNET_YES
Definition: gnunet_common.h:77
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
Definition: mq.c:351
GNUNET_TRANSPORT_ConnectionStatus
Possible states of a connection.
#define GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_END
Message sent to indicate to a monitor that a one-shot iteration over events is done.
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)
Return information about a specific peer or all peers currently known to transport service once or in...
const struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static void disconnect(struct GNUNET_TRANSPORT_MonitorContext *mc)
Disconnect from the transport service.
#define GNUNET_MQ_handler_end()
End-marker for the handlers array.
#define GNUNET_free(ptr)
Wrapper around free.