2 This file is part of GNUnet.
3 Copyright (C) 2009-2016 GNUnet e.V.
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.
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.
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/>.
20 * @author Christian Grothoff
25 * @defgroup transport Transport service
28 * @see [Documentation](https://gnunet.org/transport-service)
33 #ifndef GNUNET_TRANSPORT_SERVICE_H
34 #define GNUNET_TRANSPORT_SERVICE_H
39 #if 0 /* keep Emacsens' auto-indent happy */
44 #include "gnunet_util_lib.h"
45 #include "gnunet_ats_service.h"
48 * Version number of the transport API.
50 #define GNUNET_TRANSPORT_VERSION 0x00000003
53 /* *************************** HELLO *************************** */
56 * Handle for a #GNUNET_TRANSPORT_offer_hello operation
58 struct GNUNET_TRANSPORT_OfferHelloHandle;
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.
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
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,
84 * Cancel the request to transport to offer the HELLO message
86 * @param ohh the `struct GNUNET_TRANSPORT_OfferHelloHandle` to cancel
89 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh);
92 /* *********************** Address to String ******************* */
95 * Handle to cancel a pending address lookup.
97 struct GNUNET_TRANSPORT_AddressToStringContext;
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.
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)
117 (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
123 * Convert a binary address into a human readable address.
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
134 struct GNUNET_TRANSPORT_AddressToStringContext *
135 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
136 const struct GNUNET_HELLO_Address *address,
138 struct GNUNET_TIME_Relative timeout,
139 GNUNET_TRANSPORT_AddressToStringCallback aluc,
144 * Cancel request for address conversion.
146 * @param alc the context handle
149 GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *alc);
152 /* *********************** Monitoring ************************** */
156 * Possible state of a neighbour. Initially, we are #GNUNET_TRANSPORT_PS_NOT_CONNECTED.
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.
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.
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).
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").
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.
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'
209 enum GNUNET_TRANSPORT_PeerState
212 * Fresh peer or completely disconnected
214 GNUNET_TRANSPORT_PS_NOT_CONNECTED = 0,
217 * Asked to initiate connection, trying to get address from ATS
219 GNUNET_TRANSPORT_PS_INIT_ATS,
222 * Sent SYN message to other peer, waiting for SYN_ACK
224 GNUNET_TRANSPORT_PS_SYN_SENT,
227 * Received a SYN, asking ATS about address suggestions.
229 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
232 * SYN request from other peer was SYN_ACK'ed, waiting for ACK.
234 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
237 * Got our SYN_ACK/ACK, connection is up.
239 GNUNET_TRANSPORT_PS_CONNECTED,
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.
245 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
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.
252 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
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.
260 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
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).
266 GNUNET_TRANSPORT_PS_DISCONNECT,
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()).
277 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED
282 * Convert a transport state to a human readable string.
284 * @param state the state
287 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state);
291 * Check if a state is defined as connected
293 * @param state the state value
294 * @return #GNUNET_YES or #GNUNET_NO
297 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state);
301 * Handle for a #GNUNET_TRANSPORT_monitor_peers operation.
303 struct GNUNET_TRANSPORT_PeerMonitoringContext;
307 * Function to call with information about a peer
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.
313 * The #GNUNET_TRANSPORT_monitor_peers_cancel() call MUST not be called from
314 * within this function!
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
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);
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
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.
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.
347 * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
350 * @param cfg configuration to use
351 * @param peer a specific peer identity to obtain information for,
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
358 struct GNUNET_TRANSPORT_PeerMonitoringContext *
359 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
360 const struct GNUNET_PeerIdentity *peer,
362 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
363 void *peer_callback_cls);
367 * Cancel request to monitor peers
369 * @param pic handle for the request to cancel
372 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
375 /* *********************** Blacklisting ************************ */
378 * Handle for blacklisting peers.
380 struct GNUNET_TRANSPORT_Blacklist;
384 * Function that decides if a connection is acceptable or not.
387 * @param pid peer to approve or disapproave
388 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
391 (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
392 const struct GNUNET_PeerIdentity *pid);
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
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
409 struct GNUNET_TRANSPORT_Blacklist *
410 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
411 GNUNET_TRANSPORT_BlacklistCallback cb,
416 * Abort the blacklist. Note that this function is the only way for
417 * removing a peer from the blacklist.
419 * @param br handle of the request that is to be cancelled
422 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
426 * Handle for a plugin session state monitor.
428 struct GNUNET_TRANSPORT_PluginMonitor;
431 * Abstract representation of a plugin's session.
432 * Corresponds to the `struct GNUNET_ATS_Session` within the TRANSPORT service.
434 struct GNUNET_TRANSPORT_PluginSession;
438 * Possible states of a session in a plugin.
440 enum GNUNET_TRANSPORT_SessionState
444 * The session was created (first call for each session object).
446 GNUNET_TRANSPORT_SS_INIT,
449 * Initial session handshake is in progress.
451 GNUNET_TRANSPORT_SS_HANDSHAKE,
454 * Session is fully UP.
456 GNUNET_TRANSPORT_SS_UP,
459 * This is just an update about the session,
460 * the state did not change.
462 GNUNET_TRANSPORT_SS_UPDATE,
465 * Session is being torn down and about to disappear.
466 * Last call for each session object.
468 GNUNET_TRANSPORT_SS_DONE
474 * Information about a plugin's session.
476 struct GNUNET_TRANSPORT_SessionInfo
480 * New state of the session.
482 enum GNUNET_TRANSPORT_SessionState state;
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'
494 * Number of messages pending transmission for this session.
496 uint32_t num_msg_pending;
499 * Number of bytes pending transmission for this session.
501 uint32_t num_bytes_pending;
504 * Until when does this plugin refuse to receive to manage
505 * staying within the inbound quota? ZERO if receive is
508 struct GNUNET_TIME_Absolute receive_delay;
511 * At what time will this session timeout (unless activity
514 struct GNUNET_TIME_Absolute session_timeout;
517 * Address used by the session. Can be NULL if none is available.
519 const struct GNUNET_HELLO_Address *address;
524 * Function called by the plugin with information about the
525 * current sessions managed by the plugin (for monitoring).
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
541 (*GNUNET_TRANSPORT_SessionMonitorCallback) (void *cls,
542 struct GNUNET_TRANSPORT_PluginSession *session,
544 const struct GNUNET_TRANSPORT_SessionInfo *info);
548 * Install a plugin session state monitor callback. The callback
549 * will be notified whenever the session changes.
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
556 struct GNUNET_TRANSPORT_PluginMonitor *
557 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
558 GNUNET_TRANSPORT_SessionMonitorCallback cb,
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).
567 * @param pm handle of the request that is to be cancelled
570 GNUNET_TRANSPORT_monitor_plugins_cancel (struct GNUNET_TRANSPORT_PluginMonitor *pm);
574 #if 0 /* keep Emacsens' auto-indent happy */
581 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
584 /** @} */ /* end of group */
586 /* end of gnunet_transport_service.h */