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