Merge branch 'master' of ssh://gnunet.org/gnunet
[oweals/gnunet.git] / src / include / gnunet_transport_service.h
1 /*
2      This file is part of GNUnet.
3      Copyright (C) 2009-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  * @author Christian Grothoff
21  *
22  * @file
23  * Low-level P2P IO
24  *
25  * @defgroup transport  Transport service
26  * Low-level P2P IO
27  *
28  * @see [Documentation](https://gnunet.org/transport-service)
29  *
30  * @{
31  */
32
33 #ifndef GNUNET_TRANSPORT_SERVICE_H
34 #define GNUNET_TRANSPORT_SERVICE_H
35
36 #ifdef __cplusplus
37 extern "C"
38 {
39 #if 0                           /* keep Emacsens' auto-indent happy */
40 }
41 #endif
42 #endif
43
44 #include "gnunet_util_lib.h"
45 #include "gnunet_ats_service.h"
46
47 /**
48  * Version number of the transport API.
49  */
50 #define GNUNET_TRANSPORT_VERSION 0x00000003
51
52
53 /* *************************** HELLO *************************** */
54
55 /**
56  * Handle for a #GNUNET_TRANSPORT_offer_hello operation
57  */
58 struct GNUNET_TRANSPORT_OfferHelloHandle;
59
60
61 /**
62  * Offer the transport service the HELLO of another peer.  Note that
63  * the transport service may just ignore this message if the HELLO is
64  * malformed or useless due to our local configuration.
65  *
66  * @param cfg configuration
67  * @param hello the hello message
68  * @param cont continuation to call when HELLO has been sent,
69  *      tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
70  *      tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
71  * @param cont_cls closure for @a cont
72  * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on failure,
73  *      in case of failure @a cont will not be called
74  *
75  */
76 struct GNUNET_TRANSPORT_OfferHelloHandle *
77 GNUNET_TRANSPORT_offer_hello (const struct GNUNET_CONFIGURATION_Handle *cfg,
78                               const struct GNUNET_MessageHeader *hello,
79                               GNUNET_SCHEDULER_TaskCallback cont,
80                               void *cont_cls);
81
82
83 /**
84  * Cancel the request to transport to offer the HELLO message
85  *
86  * @param ohh the `struct GNUNET_TRANSPORT_OfferHelloHandle` to cancel
87  */
88 void
89 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh);
90
91
92 /* *********************** Address to String ******************* */
93
94 /**
95  * Handle to cancel a pending address lookup.
96  */
97 struct GNUNET_TRANSPORT_AddressToStringContext;
98
99
100 /**
101  * Function to call with a textual representation of an address.  This
102  * function will be called several times with different possible
103  * textual representations, and a last time with @a address being NULL
104  * to signal the end of the iteration.  Note that @a address NULL
105  * always is the last call, regardless of the value in @a res.
106  *
107  * @param cls closure
108  * @param address NULL on end of iteration,
109  *        otherwise 0-terminated printable UTF-8 string,
110  *        in particular an empty string if @a res is #GNUNET_NO
111  * @param res result of the address to string conversion:
112  *        if #GNUNET_OK: conversion successful
113  *        if #GNUNET_NO: address was invalid (or not supported)
114  *        if #GNUNET_SYSERR: communication error (IPC error)
115  */
116 typedef void
117 (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
118                                              const char *address,
119                                              int res);
120
121
122 /**
123  * Convert a binary address into a human readable address.
124  *
125  * @param cfg configuration to use
126  * @param address address to convert (binary format)
127  * @param numeric should (IP) addresses be displayed in numeric form
128  *                (otherwise do reverse DNS lookup)
129  * @param timeout how long is the lookup allowed to take at most
130  * @param aluc function to call with the results
131  * @param aluc_cls closure for @a aluc
132  * @return handle to cancel the operation, NULL on error
133  */
134 struct GNUNET_TRANSPORT_AddressToStringContext *
135 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
136                                     const struct GNUNET_HELLO_Address *address,
137                                     int numeric,
138                                     struct GNUNET_TIME_Relative timeout,
139                                     GNUNET_TRANSPORT_AddressToStringCallback aluc,
140                                     void *aluc_cls);
141
142
143 /**
144  * Cancel request for address conversion.
145  *
146  * @param alc the context handle
147  */
148 void
149 GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *alc);
150
151
152 /* *********************** Monitoring ************************** */
153
154
155 /**
156  * Possible state of a neighbour.  Initially, we are #GNUNET_TRANSPORT_PS_NOT_CONNECTED.
157  *
158  * Then, there are two main paths. If we receive a SYN message, we give
159  * the inbound address to ATS. After the check we ask ATS for a suggestion
160  * (#GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS). If ATS makes a suggestion, we
161  * send our SYN_ACK and go to #GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK.
162  * If we receive a ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
163  * (and notify everyone about the new connection). If the operation times out,
164  * we go to #GNUNET_TRANSPORT_PS_DISCONNECT.
165  *
166  * The other case is where we transmit a SYN message first.  We
167  * start with #GNUNET_TRANSPORT_PS_INIT_ATS.  If we get an address, we send
168  * the SYN message and go to state #GNUNET_TRANSPORT_PS_CONNECT_SENT.
169  * Once we receive a SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
170  * (and notify everyone about the new connection and send
171  * back a ACK).  If the operation times out, we go to
172  * #GNUNET_TRANSPORT_PS_DISCONNECT.
173  *
174  * If the session is in trouble (i.e. transport-level disconnect or
175  * timeout), we go to #GNUNET_TRANSPORT_PS_RECONNECT_ATS where we ask ATS for a new
176  * address (we don't notify anyone about the disconnect yet).  Once we
177  * have a new address, we enter #GNUNET_TRANSPORT_PS_RECONNECT_SENT and send a
178  * SYN message.  If we receive a
179  * SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED and nobody noticed that we had
180  * trouble; we also send a ACK at this time just in case.  If
181  * the operation times out, we go to #GNUNET_TRANSPORT_PS_DISCONNECT (and notify everyone
182  * about the lost connection).
183  *
184  * If ATS decides to switch addresses while we have a normal
185  * connection, we go to #GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT
186  * and send a SESSION_CONNECT.  If we get a ACK back, we switch the
187  * primary connection to the suggested alternative from ATS, go back
188  * to #GNUNET_TRANSPORT_PS_CONNECTED and send a ACK to the other peer just to be
189  * sure.  If the operation times out
190  * we go to #GNUNET_TRANSPORT_PS_CONNECTED (and notify ATS that the given alternative
191  * address is "invalid").
192  *
193  * Once a session is in #GNUNET_TRANSPORT_PS_DISCONNECT, it is cleaned up and then goes
194  * to (#GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED).  If we receive an explicit disconnect
195  * request, we can go from any state to #GNUNET_TRANSPORT_PS_DISCONNECT, possibly after
196  * generating disconnect notifications.
197  *
198  * Note that it is quite possible that while we are in any of these
199  * states, we could receive a 'SYN' request from the other peer.
200  * We then enter a 'weird' state where we pursue our own primary state
201  * machine (as described above), but with the 'send_connect_ack' flag
202  * set to 1.  If our state machine allows us to send a 'SYN_ACK'
203  * (because we have an acceptable address), we send the 'SYN_ACK'
204  * and set the 'send_connect_ack' to 2.  If we then receive a
205  * 'ACK', we go to #GNUNET_TRANSPORT_PS_CONNECTED (and reset 'send_connect_ack'
206  * to 0).
207  *
208  */
209 enum GNUNET_TRANSPORT_PeerState
210 {
211   /**
212    * Fresh peer or completely disconnected
213    */
214   GNUNET_TRANSPORT_PS_NOT_CONNECTED = 0,
215
216   /**
217    * Asked to initiate connection, trying to get address from ATS
218    */
219   GNUNET_TRANSPORT_PS_INIT_ATS,
220
221   /**
222    * Sent SYN message to other peer, waiting for SYN_ACK
223    */
224   GNUNET_TRANSPORT_PS_SYN_SENT,
225
226   /**
227    * Received a SYN, asking ATS about address suggestions.
228    */
229   GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
230
231   /**
232    * SYN request from other peer was SYN_ACK'ed, waiting for ACK.
233    */
234   GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
235
236   /**
237    * Got our SYN_ACK/ACK, connection is up.
238    */
239   GNUNET_TRANSPORT_PS_CONNECTED,
240
241   /**
242    * Connection got into trouble, rest of the system still believes
243    * it to be up, but we're getting a new address from ATS.
244    */
245   GNUNET_TRANSPORT_PS_RECONNECT_ATS,
246
247   /**
248    * Sent SYN over new address (either by ATS telling us to switch
249    * addresses or from RECONNECT_ATS); if this fails, we need to tell
250    * the rest of the system about a disconnect.
251    */
252   GNUNET_TRANSPORT_PS_RECONNECT_SENT,
253
254   /**
255    * We have some primary connection, but ATS suggested we switch
256    * to some alternative; we now sent a SYN message for the
257    * alternative session to the other peer and waiting for a
258    * SYN_ACK to make this our primary connection.
259    */
260   GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
261
262   /**
263    * Disconnect in progress (we're sending the DISCONNECT message to the
264    * other peer; after that is finished, the state will be cleaned up).
265    */
266   GNUNET_TRANSPORT_PS_DISCONNECT,
267
268   /**
269    * We're finished with the disconnect; and are cleaning up the state
270    * now!  We put the struct into this state when we are really in the
271    * task that calls 'free' on it and are about to remove the record
272    * from the map.  We should never find a 'struct NeighbourMapEntry'
273    * in this state in the map.  Accessing a 'struct NeighbourMapEntry'
274    * in this state virtually always means using memory that has been
275    * freed (the exception being the cleanup code in #free_neighbour()).
276    */
277   GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED
278 };
279
280
281 /**
282  * Convert a transport state to a human readable string.
283  *
284  * @param state the state
285  */
286 const char *
287 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state);
288
289
290 /**
291  * Check if a state is defined as connected
292  *
293  * @param state the state value
294  * @return #GNUNET_YES or #GNUNET_NO
295  */
296 int
297 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state);
298
299
300 /**
301  * Handle for a #GNUNET_TRANSPORT_monitor_peers operation.
302  */
303 struct GNUNET_TRANSPORT_PeerMonitoringContext;
304
305
306 /**
307  * Function to call with information about a peer
308  *
309  * If one_shot was set to #GNUNET_YES to iterate over all peers once,
310  * a final call with NULL for peer and address will follow when done.
311  * In this case state and timeout do not contain valid values.
312  *
313  * The #GNUNET_TRANSPORT_monitor_peers_cancel() call MUST not be called from
314  * within this function!
315  *
316  *
317  * @param cls closure
318  * @param peer peer this update is about,
319  *      NULL if this is the final last callback for a iteration operation
320  * @param address address, NULL if this is the final callback for iteration op
321  * @param state current state this peer is in
322  * @param state_timeout timeout for the current state of the peer
323  */
324 typedef void
325 (*GNUNET_TRANSPORT_PeerIterateCallback) (void *cls,
326                                          const struct GNUNET_PeerIdentity *peer,
327                                          const struct GNUNET_HELLO_Address *address,
328                                          enum GNUNET_TRANSPORT_PeerState state,
329                                          struct GNUNET_TIME_Absolute state_timeout);
330
331
332 /**
333  * Return information about a specific peer or all peers currently known to
334  * transport service once or in monitoring mode. To obtain information about
335  * a specific peer, a peer identity can be passed. To obtain information about
336  * all peers currently known to transport service, NULL can be passed as peer
337  * identity.
338  *
339  * For each peer, the callback is called with information about the address used
340  * to communicate with this peer, the state this peer is currently in and the
341  * the current timeout for this state.
342  *
343  * Upon completion, the #GNUNET_TRANSPORT_PeerIterateCallback is called one
344  * more time with `NULL`. After this, the operation must no longer be
345  * explicitly canceled.
346  *
347  * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
348  * the peer_callback!
349  *
350  * @param cfg configuration to use
351  * @param peer a specific peer identity to obtain information for,
352  *      NULL for all peers
353  * @param one_shot #GNUNET_YES to return the current state and then end (with NULL+NULL),
354  *                 #GNUNET_NO to monitor peers continuously
355  * @param peer_callback function to call with the results
356  * @param peer_callback_cls closure for @a peer_callback
357  */
358 struct GNUNET_TRANSPORT_PeerMonitoringContext *
359 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
360                                 const struct GNUNET_PeerIdentity *peer,
361                                 int one_shot,
362                                 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
363                                 void *peer_callback_cls);
364
365
366 /**
367  * Cancel request to monitor peers
368  *
369  * @param pic handle for the request to cancel
370  */
371 void
372 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
373
374
375 /* *********************** Blacklisting ************************ */
376
377 /**
378  * Handle for blacklisting peers.
379  */
380 struct GNUNET_TRANSPORT_Blacklist;
381
382
383 /**
384  * Function that decides if a connection is acceptable or not.
385  *
386  * @param cls closure
387  * @param pid peer to approve or disapproave
388  * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
389  */
390 typedef int
391 (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
392                                        const struct GNUNET_PeerIdentity *pid);
393
394
395 /**
396  * Install a blacklist callback.  The service will be queried for all
397  * existing connections as well as any fresh connections to check if
398  * they are permitted.  If the blacklisting callback is unregistered,
399  * all hosts that were denied in the past will automatically be
400  * whitelisted again.  Cancelling the blacklist handle is also the
401  * only way to re-enable connections from peers that were previously
402  * blacklisted.
403  *
404  * @param cfg configuration to use
405  * @param cb callback to invoke to check if connections are allowed
406  * @param cb_cls closure for @a cb
407  * @return NULL on error, otherwise handle for cancellation
408  */
409 struct GNUNET_TRANSPORT_Blacklist *
410 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
411                             GNUNET_TRANSPORT_BlacklistCallback cb,
412                             void *cb_cls);
413
414
415 /**
416  * Abort the blacklist.  Note that this function is the only way for
417  * removing a peer from the blacklist.
418  *
419  * @param br handle of the request that is to be cancelled
420  */
421 void
422 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
423
424
425 /**
426  * Handle for a plugin session state monitor.
427  */
428 struct GNUNET_TRANSPORT_PluginMonitor;
429
430 /**
431  * Abstract representation of a plugin's session.
432  * Corresponds to the `struct GNUNET_ATS_Session` within the TRANSPORT service.
433  */
434 struct GNUNET_TRANSPORT_PluginSession;
435
436
437 /**
438  * Possible states of a session in a plugin.
439  */
440 enum GNUNET_TRANSPORT_SessionState
441 {
442
443   /**
444    * The session was created (first call for each session object).
445    */
446   GNUNET_TRANSPORT_SS_INIT,
447
448   /**
449    * Initial session handshake is in progress.
450    */
451   GNUNET_TRANSPORT_SS_HANDSHAKE,
452
453   /**
454    * Session is fully UP.
455    */
456   GNUNET_TRANSPORT_SS_UP,
457
458   /**
459    * This is just an update about the session,
460    * the state did not change.
461    */
462   GNUNET_TRANSPORT_SS_UPDATE,
463
464   /**
465    * Session is being torn down and about to disappear.
466    * Last call for each session object.
467    */
468   GNUNET_TRANSPORT_SS_DONE
469
470 };
471
472
473 /**
474  * Information about a plugin's session.
475  */
476 struct GNUNET_TRANSPORT_SessionInfo
477 {
478
479   /**
480    * New state of the session.
481    */
482   enum GNUNET_TRANSPORT_SessionState state;
483
484   /**
485    * #GNUNET_YES if this is an inbound connection,
486    * #GNUNET_NO if this is an outbound connection,
487    * #GNUNET_SYSERR if connections of this plugin
488    *             are so fundamentally bidirectional
489    *             that they have no 'initiator'
490    */
491   int is_inbound;
492
493   /**
494    * Number of messages pending transmission for this session.
495    */
496   uint32_t num_msg_pending;
497
498   /**
499    * Number of bytes pending transmission for this session.
500    */
501   uint32_t num_bytes_pending;
502
503   /**
504    * Until when does this plugin refuse to receive to manage
505    * staying within the inbound quota?  ZERO if receive is
506    * active.
507    */
508   struct GNUNET_TIME_Absolute receive_delay;
509
510   /**
511    * At what time will this session timeout (unless activity
512    * happens)?
513    */
514   struct GNUNET_TIME_Absolute session_timeout;
515
516   /**
517    * Address used by the session.  Can be NULL if none is available.
518    */
519   const struct GNUNET_HELLO_Address *address;
520 };
521
522
523 /**
524  * Function called by the plugin with information about the
525  * current sessions managed by the plugin (for monitoring).
526  *
527  * @param cls closure
528  * @param session session handle this information is about,
529  *        NULL to indicate that we are "in sync" (initial
530  *        iteration complete)
531  * @param session_ctx storage location where the application
532  *        can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
533  *        and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
534  * @param info information about the state of the session,
535  *        NULL if @a session is also NULL and we are
536  *        merely signalling that the initial iteration is over;
537  *        NULL with @a session being non-NULL if the monitor
538  *        was being cancelled while sessions were active
539  */
540 typedef void
541 (*GNUNET_TRANSPORT_SessionMonitorCallback) (void *cls,
542                                             struct GNUNET_TRANSPORT_PluginSession *session,
543                                             void **session_ctx,
544                                             const struct GNUNET_TRANSPORT_SessionInfo *info);
545
546
547 /**
548  * Install a plugin session state monitor callback.  The callback
549  * will be notified whenever the session changes.
550  *
551  * @param cfg configuration to use
552  * @param cb callback to invoke on events
553  * @param cb_cls closure for @a cb
554  * @return NULL on error, otherwise handle for cancellation
555  */
556 struct GNUNET_TRANSPORT_PluginMonitor *
557 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
558                                   GNUNET_TRANSPORT_SessionMonitorCallback cb,
559                                   void *cb_cls);
560
561
562 /**
563  * Cancel monitoring the plugin session state.  The callback will be
564  * called once for each session that is up with the "info" argument
565  * being NULL (this is just to enable client-side cleanup).
566  *
567  * @param pm handle of the request that is to be cancelled
568  */
569 void
570 GNUNET_TRANSPORT_monitor_plugins_cancel (struct GNUNET_TRANSPORT_PluginMonitor *pm);
571
572
573
574 #if 0                           /* keep Emacsens' auto-indent happy */
575 {
576 #endif
577 #ifdef __cplusplus
578 }
579 #endif
580
581 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
582 #endif
583
584 /** @} */  /* end of group */
585
586 /* end of gnunet_transport_service.h */