fix div by zero
[oweals/gnunet.git] / src / transport / transport_api_monitor_peers.c
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-2014, 2016 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
19 /**
20  * @file transport/transport_api_monitor_peers.c
21  * @brief montoring api for transport peer status
22  *
23  * This api provides the ability to query the transport service about
24  * the connection status of a specific or all peers.
25  *
26  * Calls back with information about peer(s) including address used, state and
27  * state timeout for peer requests.
28  */
29 #include "platform.h"
30 #include "gnunet_util_lib.h"
31 #include "gnunet_arm_service.h"
32 #include "gnunet_hello_lib.h"
33 #include "gnunet_protocols.h"
34 #include "gnunet_transport_service.h"
35 #include "transport.h"
36
37 /**
38  * Context for iterating validation entries.
39  */
40 struct GNUNET_TRANSPORT_PeerMonitoringContext
41 {
42   /**
43    * Function to call with the binary address.
44    */
45   GNUNET_TRANSPORT_PeerIterateCallback cb;
46
47   /**
48    * Closure for @e cb.
49    */
50   void *cb_cls;
51
52   /**
53    * Connection to the service.
54    */
55   struct GNUNET_MQ_Handle *mq;
56
57   /**
58    * Configuration we use.
59    */
60   const struct GNUNET_CONFIGURATION_Handle *cfg;
61
62   /**
63    * Backoff for reconnect.
64    */
65   struct GNUNET_TIME_Relative backoff;
66
67   /**
68    * Task ID for reconnect.
69    */
70   struct GNUNET_SCHEDULER_Task *reconnect_task;
71
72   /**
73    * Identity of the peer to monitor.
74    */
75   struct GNUNET_PeerIdentity peer;
76
77   /**
78    * Was this a one-shot request?
79    */
80   int one_shot;
81 };
82
83
84 /**
85  * Check if a state is defined as connected
86  *
87  * @param state the state value
88  * @return #GNUNET_YES or #GNUNET_NO
89  */
90 int
91 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state)
92 {
93   switch (state)
94   {
95   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
96   case GNUNET_TRANSPORT_PS_INIT_ATS:
97   case GNUNET_TRANSPORT_PS_SYN_SENT:
98   case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
99   case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
100     return GNUNET_NO;
101   case GNUNET_TRANSPORT_PS_CONNECTED:
102   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
103   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
104   case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
105     return GNUNET_YES;
106   case GNUNET_TRANSPORT_PS_DISCONNECT:
107   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
108     return GNUNET_NO;
109   default:
110     GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
111                 "Unhandled state `%s'\n",
112                 GNUNET_TRANSPORT_ps2s (state));
113     GNUNET_break (0);
114     break;
115   }
116   return GNUNET_SYSERR;
117 }
118
119
120 /**
121  * Convert peer state to human-readable string.
122  *
123  * @param state the state value
124  * @return corresponding string
125  */
126 const char *
127 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state)
128 {
129   switch (state)
130   {
131   case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
132     return "S_NOT_CONNECTED";
133   case GNUNET_TRANSPORT_PS_INIT_ATS:
134     return "S_INIT_ATS";
135   case GNUNET_TRANSPORT_PS_SYN_SENT:
136     return "S_SYN_SENT";
137   case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
138     return "S_SYN_RECV_ATS";
139   case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
140     return "S_SYN_RECV_ACK";
141   case GNUNET_TRANSPORT_PS_CONNECTED:
142     return "S_CONNECTED";
143   case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
144     return "S_RECONNECT_ATS";
145   case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
146     return "S_RECONNECT_SENT";
147   case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
148     return "S_SWITCH_SYN_SENT";
149   case GNUNET_TRANSPORT_PS_DISCONNECT:
150     return "S_DISCONNECT";
151   case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
152     return "S_DISCONNECT_FINISHED";
153   default:
154     GNUNET_break (0);
155     return "UNDEFINED";
156   }
157 }
158
159
160 /**
161  * Task run to re-establish the connection.
162  *
163  * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
164  */
165 static void
166 do_peer_connect (void *cls);
167
168
169 /**
170  * Cut the existing connection and reconnect.
171  *
172  * @param pal_ctx our context
173  */
174 static void
175 reconnect_peer_ctx (struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx)
176 {
177   GNUNET_assert (GNUNET_NO == pal_ctx->one_shot);
178   GNUNET_MQ_destroy (pal_ctx->mq);
179   pal_ctx->mq = NULL;
180   pal_ctx->cb (pal_ctx->cb_cls,
181                NULL,
182                NULL,
183                GNUNET_TRANSPORT_PS_NOT_CONNECTED,
184                GNUNET_TIME_UNIT_ZERO_ABS);
185   pal_ctx->backoff = GNUNET_TIME_STD_BACKOFF (pal_ctx->backoff);
186   pal_ctx->reconnect_task = GNUNET_SCHEDULER_add_delayed (pal_ctx->backoff,
187                                                           &do_peer_connect,
188                                                           pal_ctx);
189 }
190
191
192 /**
193  * Function called with responses from the service.
194  *
195  * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
196  * @param msg message from service
197  */
198 static void
199 handle_response_end (void *cls,
200                      const struct GNUNET_MessageHeader *msg)
201 {
202   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
203
204   if (pal_ctx->one_shot)
205   {
206     /* iteration finished */
207     pal_ctx->cb (pal_ctx->cb_cls,
208                  NULL,
209                  NULL,
210                  GNUNET_TRANSPORT_PS_NOT_CONNECTED,
211                  GNUNET_TIME_UNIT_ZERO_ABS);
212     GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
213     return;
214   }
215   /* not quite what we expected, reconnect */
216   GNUNET_break (0);
217   reconnect_peer_ctx (pal_ctx);
218 }
219
220
221 /**
222  * Function called to check responses from the service.
223  *
224  * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
225  * @param pir_msg  message with the human-readable address
226  * @return #GNUNET_OK if @a pir_msg is well-formed
227  */
228 static int
229 check_response (void *cls,
230                 const struct PeerIterateResponseMessage *pir_msg)
231 {
232   uint16_t size = ntohs (pir_msg->header.size) - sizeof (*pir_msg);
233   size_t alen = ntohl (pir_msg->addrlen);
234   size_t tlen = ntohl (pir_msg->pluginlen);
235   const char *addr;
236   const char *transport_name;
237
238   if (size != tlen + alen)
239   {
240     GNUNET_break (0);
241     return GNUNET_SYSERR;
242   }
243   if ( (0 == tlen) && (0 == alen) )
244     return GNUNET_OK;
245   if (0 == tlen)
246   {
247     GNUNET_break (0); /* This must not happen: address without plugin */
248     return GNUNET_SYSERR;
249   }
250   addr = (const char *) &pir_msg[1];
251   transport_name = &addr[alen];
252   if (transport_name[tlen - 1] != '\0')
253   {
254     GNUNET_break (0);
255     return GNUNET_SYSERR;
256   }
257   return GNUNET_OK;
258 }
259
260
261 /**
262  * Function called with responses from the service.
263  *
264  * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
265  * @param msg  message with the human-readable address
266  */
267 static void
268 handle_response (void *cls,
269                  const struct PeerIterateResponseMessage *pir_msg)
270 {
271   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
272   struct GNUNET_HELLO_Address *address;
273   size_t alen = ntohl (pir_msg->addrlen);
274   size_t tlen = ntohl (pir_msg->pluginlen);
275   const char *addr;
276   const char *transport_name;
277
278   if ( (0 == tlen) &&
279        (0 == alen) )
280   {
281     /* No address available */
282     pal_ctx->cb (pal_ctx->cb_cls,
283                  &pir_msg->peer,
284                  NULL,
285                  ntohl(pir_msg->state),
286                  GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
287     return;
288   }
289   addr = (const char *) &pir_msg[1];
290   transport_name = &addr[alen];
291
292   /* notify client */
293   address = GNUNET_HELLO_address_allocate (&pir_msg->peer,
294                                            transport_name,
295                                            addr,
296                                            alen,
297                                            ntohl (pir_msg->local_address_info));
298   pal_ctx->cb (pal_ctx->cb_cls,
299                &pir_msg->peer,
300                address,
301                ntohl (pir_msg->state),
302                GNUNET_TIME_absolute_ntoh (pir_msg->state_timeout));
303   GNUNET_HELLO_address_free (address);
304 }
305
306
307
308 /**
309  * Generic error handler, called with the appropriate error code and
310  * the same closure specified at the creation of the message queue.
311  * Not every message queue implementation supports an error handler.
312  *
313  * @param cls closure with the `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
314  * @param error error code
315  */
316 static void
317 mq_error_handler (void *cls,
318                   enum GNUNET_MQ_Error error)
319 {
320   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
321
322   if (pal_ctx->one_shot)
323   {
324     /* Disconnect */
325     pal_ctx->cb (pal_ctx->cb_cls,
326                  NULL,
327                  NULL,
328                  GNUNET_TRANSPORT_PS_NOT_CONNECTED,
329                  GNUNET_TIME_UNIT_ZERO_ABS);
330     GNUNET_TRANSPORT_monitor_peers_cancel (pal_ctx);
331     return;
332   }
333   reconnect_peer_ctx (pal_ctx);
334 }
335
336
337 /**
338  * Task run to re-establish the connection.
339  *
340  * @param cls our `struct GNUNET_TRANSPORT_PeerMonitoringContext *`
341  */
342 static void
343 do_peer_connect (void *cls)
344 {
345   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx = cls;
346   struct GNUNET_MQ_MessageHandler handlers[] = {
347     GNUNET_MQ_hd_var_size (response,
348                            GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE,
349                            struct PeerIterateResponseMessage,
350                            pal_ctx),
351     GNUNET_MQ_hd_fixed_size (response_end,
352                              GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_RESPONSE_END,
353                              struct GNUNET_MessageHeader,
354                              pal_ctx),
355     GNUNET_MQ_handler_end ()
356   };
357   struct PeerMonitorMessage *msg;
358   struct GNUNET_MQ_Envelope *env;
359
360   pal_ctx->reconnect_task = NULL;
361   pal_ctx->mq = GNUNET_CLIENT_connect (pal_ctx->cfg,
362                                        "transport",
363                                        handlers,
364                                        &mq_error_handler,
365                                        pal_ctx);
366   if (NULL == pal_ctx->mq)
367     return;
368   env = GNUNET_MQ_msg (msg,
369                        GNUNET_MESSAGE_TYPE_TRANSPORT_MONITOR_PEER_REQUEST);
370   msg->one_shot = htonl (pal_ctx->one_shot);
371   msg->peer = pal_ctx->peer;
372   GNUNET_MQ_send (pal_ctx->mq,
373                   env);
374 }
375
376
377 /**
378  * Return information about a specific peer or all peers currently known to
379  * transport service once or in monitoring mode. To obtain information about
380  * a specific peer, a peer identity can be passed. To obtain information about
381  * all peers currently known to transport service, NULL can be passed as peer
382  * identity.
383  *
384  * For each peer, the callback is called with information about the address used
385  * to communicate with this peer, the state this peer is currently in and the
386  * the current timeout for this state.
387  *
388  * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
389  * more time with 'NULL'. After this, the operation must no longer be
390  * explicitly canceled.
391  *
392  * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
393  * the peer_callback!
394  *
395  * @param cfg configuration to use
396  * @param peer a specific peer identity to obtain information for,
397  *      NULL for all peers
398  * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
399  *                 #GNUNET_NO to monitor peers continuously
400  * @param peer_callback function to call with the results
401  * @param peer_callback_cls closure for @a peer_address_callback
402  */
403 struct GNUNET_TRANSPORT_PeerMonitoringContext *
404 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
405                                 const struct GNUNET_PeerIdentity *peer,
406                                 int one_shot,
407                                 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
408                                 void *peer_callback_cls)
409 {
410   struct GNUNET_TRANSPORT_PeerMonitoringContext *pal_ctx
411     = GNUNET_new (struct GNUNET_TRANSPORT_PeerMonitoringContext);
412
413   pal_ctx->cb = peer_callback;
414   pal_ctx->cb_cls = peer_callback_cls;
415   pal_ctx->cfg = cfg;
416   if (NULL != peer)
417     pal_ctx->peer = *peer;
418   pal_ctx->one_shot = one_shot;
419   do_peer_connect (pal_ctx);
420   if (NULL == pal_ctx->mq)
421   {
422     GNUNET_free (pal_ctx);
423     return NULL;
424   }
425   return pal_ctx;
426 }
427
428
429 /**
430  * Cancel request to monitor peers
431  *
432  * @param pic handle for the request to cancel
433  */
434 void
435 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic)
436 {
437   if (NULL != pic->mq)
438   {
439     GNUNET_MQ_destroy (pic->mq);
440     pic->mq = NULL;
441   }
442   if (NULL != pic->reconnect_task)
443   {
444     GNUNET_SCHEDULER_cancel (pic->reconnect_task);
445     pic->reconnect_task = NULL;
446   }
447   GNUNET_free (pic);
448 }
449
450
451 /* end of transport_api_monitor_peers.c */