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