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