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/>.
18 SPDX-License-Identifier: AGPL3.0-or-later
22 * @author Christian Grothoff
27 * @defgroup transport Transport service
30 * @see [Documentation](https://gnunet.org/transport-service)
35 #ifndef GNUNET_TRANSPORT_SERVICE_H
36 #define GNUNET_TRANSPORT_SERVICE_H
40 #if 0 /* keep Emacsens' auto-indent happy */
45 #include "gnunet_util_lib.h"
46 #include "gnunet_ats_service.h"
49 * Version number of the transport API.
51 #define GNUNET_TRANSPORT_VERSION 0x00000003
54 /* *************************** HELLO *************************** */
57 * Handle for a #GNUNET_TRANSPORT_offer_hello operation
59 struct GNUNET_TRANSPORT_OfferHelloHandle;
63 * Offer the transport service the HELLO of another peer. Note that
64 * the transport service may just ignore this message if the HELLO is
65 * malformed or useless due to our local configuration.
67 * @param cfg configuration
68 * @param hello the hello message
69 * @param cont continuation to call when HELLO has been sent,
70 * tc reason #GNUNET_SCHEDULER_REASON_TIMEOUT for fail
71 * tc reasong #GNUNET_SCHEDULER_REASON_READ_READY for success
72 * @param cont_cls closure for @a cont
73 * @return a `struct GNUNET_TRANSPORT_OfferHelloHandle` handle or NULL on
74 * failure, in case of failure @a cont will not be called
77 struct GNUNET_TRANSPORT_OfferHelloHandle *
78 GNUNET_TRANSPORT_offer_hello (const struct GNUNET_CONFIGURATION_Handle *cfg,
79 const struct GNUNET_MessageHeader *hello,
80 GNUNET_SCHEDULER_TaskCallback cont,
85 * Cancel the request to transport to offer the HELLO message
87 * @param ohh the `struct GNUNET_TRANSPORT_OfferHelloHandle` to cancel
90 GNUNET_TRANSPORT_offer_hello_cancel (
91 struct GNUNET_TRANSPORT_OfferHelloHandle *ohh);
94 /* *********************** Address to String ******************* */
97 * Handle to cancel a pending address lookup.
99 struct GNUNET_TRANSPORT_AddressToStringContext;
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.
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)
118 typedef void (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
124 * Convert a binary address into a human readable address.
126 * @param cfg configuration to use
127 * @param address address to convert (binary format)
128 * @param numeric should (IP) addresses be displayed in numeric form
129 * (otherwise do reverse DNS lookup)
130 * @param timeout how long is the lookup allowed to take at most
131 * @param aluc function to call with the results
132 * @param aluc_cls closure for @a aluc
133 * @return handle to cancel the operation, NULL on error
135 struct GNUNET_TRANSPORT_AddressToStringContext *
136 GNUNET_TRANSPORT_address_to_string (
137 const struct GNUNET_CONFIGURATION_Handle *cfg,
138 const struct GNUNET_HELLO_Address *address,
140 struct GNUNET_TIME_Relative timeout,
141 GNUNET_TRANSPORT_AddressToStringCallback aluc,
146 * Cancel request for address conversion.
148 * @param alc the context handle
151 GNUNET_TRANSPORT_address_to_string_cancel (
152 struct GNUNET_TRANSPORT_AddressToStringContext *alc);
155 /* *********************** Monitoring ************************** */
159 * Possible state of a neighbour. Initially, we are
160 * #GNUNET_TRANSPORT_PS_NOT_CONNECTED.
162 * Then, there are two main paths. If we receive a SYN message, we give
163 * the inbound address to ATS. After the check we ask ATS for a suggestion
164 * (#GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS). If ATS makes a suggestion, we
165 * send our SYN_ACK and go to #GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK.
166 * If we receive a ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
167 * (and notify everyone about the new connection). If the operation times out,
168 * we go to #GNUNET_TRANSPORT_PS_DISCONNECT.
170 * The other case is where we transmit a SYN message first. We
171 * start with #GNUNET_TRANSPORT_PS_INIT_ATS. If we get an address, we send
172 * the SYN message and go to state #GNUNET_TRANSPORT_PS_CONNECT_SENT.
173 * Once we receive a SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
174 * (and notify everyone about the new connection and send
175 * back a ACK). If the operation times out, we go to
176 * #GNUNET_TRANSPORT_PS_DISCONNECT.
178 * If the session is in trouble (i.e. transport-level disconnect or
179 * timeout), we go to #GNUNET_TRANSPORT_PS_RECONNECT_ATS where we ask ATS for a
180 * new address (we don't notify anyone about the disconnect yet). Once we have
181 * a new address, we enter #GNUNET_TRANSPORT_PS_RECONNECT_SENT and send a SYN
182 * message. If we receive a SYN_ACK, we go to #GNUNET_TRANSPORT_PS_CONNECTED
183 * and nobody noticed that we had trouble; we also send a ACK at this time just
184 * in case. If the operation times out, we go to
185 * #GNUNET_TRANSPORT_PS_DISCONNECT (and notify everyone about the lost
188 * If ATS decides to switch addresses while we have a normal
189 * connection, we go to #GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT
190 * and send a SESSION_CONNECT. If we get a ACK back, we switch the
191 * primary connection to the suggested alternative from ATS, go back
192 * to #GNUNET_TRANSPORT_PS_CONNECTED and send a ACK to the other peer just to be
193 * sure. If the operation times out
194 * we go to #GNUNET_TRANSPORT_PS_CONNECTED (and notify ATS that the given
195 * alternative address is "invalid").
197 * Once a session is in #GNUNET_TRANSPORT_PS_DISCONNECT, it is cleaned up and
198 * then goes to (#GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED). If we receive an
199 * explicit disconnect request, we can go from any state to
200 * #GNUNET_TRANSPORT_PS_DISCONNECT, possibly after generating disconnect
203 * Note that it is quite possible that while we are in any of these
204 * states, we could receive a 'SYN' request from the other peer.
205 * We then enter a 'weird' state where we pursue our own primary state
206 * machine (as described above), but with the 'send_connect_ack' flag
207 * set to 1. If our state machine allows us to send a 'SYN_ACK'
208 * (because we have an acceptable address), we send the 'SYN_ACK'
209 * and set the 'send_connect_ack' to 2. If we then receive a
210 * 'ACK', we go to #GNUNET_TRANSPORT_PS_CONNECTED (and reset 'send_connect_ack'
214 enum GNUNET_TRANSPORT_PeerState
217 * Fresh peer or completely disconnected
219 GNUNET_TRANSPORT_PS_NOT_CONNECTED = 0,
222 * Asked to initiate connection, trying to get address from ATS
224 GNUNET_TRANSPORT_PS_INIT_ATS,
227 * Sent SYN message to other peer, waiting for SYN_ACK
229 GNUNET_TRANSPORT_PS_SYN_SENT,
232 * Received a SYN, asking ATS about address suggestions.
234 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
237 * SYN request from other peer was SYN_ACK'ed, waiting for ACK.
239 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
242 * Got our SYN_ACK/ACK, connection is up.
244 GNUNET_TRANSPORT_PS_CONNECTED,
247 * Connection got into trouble, rest of the system still believes
248 * it to be up, but we're getting a new address from ATS.
250 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
253 * Sent SYN over new address (either by ATS telling us to switch
254 * addresses or from RECONNECT_ATS); if this fails, we need to tell
255 * the rest of the system about a disconnect.
257 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
260 * We have some primary connection, but ATS suggested we switch
261 * to some alternative; we now sent a SYN message for the
262 * alternative session to the other peer and waiting for a
263 * SYN_ACK to make this our primary connection.
265 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
268 * Disconnect in progress (we're sending the DISCONNECT message to the
269 * other peer; after that is finished, the state will be cleaned up).
271 GNUNET_TRANSPORT_PS_DISCONNECT,
274 * We're finished with the disconnect; and are cleaning up the state
275 * now! We put the struct into this state when we are really in the
276 * task that calls 'free' on it and are about to remove the record
277 * from the map. We should never find a 'struct NeighbourMapEntry'
278 * in this state in the map. Accessing a 'struct NeighbourMapEntry'
279 * in this state virtually always means using memory that has been
280 * freed (the exception being the cleanup code in #free_neighbour()).
282 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED
287 * Convert a transport state to a human readable string.
289 * @param state the state
292 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state);
296 * Check if a state is defined as connected
298 * @param state the state value
299 * @return #GNUNET_YES or #GNUNET_NO
302 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state);
306 * Handle for a #GNUNET_TRANSPORT_monitor_peers operation.
308 struct GNUNET_TRANSPORT_PeerMonitoringContext;
312 * Function to call with information about a peer
314 * If one_shot was set to #GNUNET_YES to iterate over all peers once,
315 * a final call with NULL for peer and address will follow when done.
316 * In this case state and timeout do not contain valid values.
318 * The #GNUNET_TRANSPORT_monitor_peers_cancel() call MUST not be called from
319 * within this function!
323 * @param peer peer this update is about,
324 * NULL if this is the final last callback for a iteration operation
325 * @param address address, NULL if this is the final callback for iteration op
326 * @param state current state this peer is in
327 * @param state_timeout timeout for the current state of the peer
329 typedef void (*GNUNET_TRANSPORT_PeerIterateCallback) (
331 const struct GNUNET_PeerIdentity *peer,
332 const struct GNUNET_HELLO_Address *address,
333 enum GNUNET_TRANSPORT_PeerState state,
334 struct GNUNET_TIME_Absolute state_timeout);
338 * Return information about a specific peer or all peers currently known to
339 * transport service once or in monitoring mode. To obtain information about
340 * a specific peer, a peer identity can be passed. To obtain information about
341 * all peers currently known to transport service, NULL can be passed as peer
344 * For each peer, the callback is called with information about the address used
345 * to communicate with this peer, the state this peer is currently in and the
346 * the current timeout for this state.
348 * Upon completion, the #GNUNET_TRANSPORT_PeerIterateCallback is called one
349 * more time with `NULL`. After this, the operation must no longer be
350 * explicitly canceled.
352 * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
355 * @param cfg configuration to use
356 * @param peer a specific peer identity to obtain information for,
358 * @param one_shot #GNUNET_YES to return the current state and then end (with
359 * NULL+NULL), #GNUNET_NO to monitor peers continuously
360 * @param peer_callback function to call with the results
361 * @param peer_callback_cls closure for @a peer_callback
363 struct GNUNET_TRANSPORT_PeerMonitoringContext *
364 GNUNET_TRANSPORT_monitor_peers (
365 const struct GNUNET_CONFIGURATION_Handle *cfg,
366 const struct GNUNET_PeerIdentity *peer,
368 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
369 void *peer_callback_cls);
373 * Cancel request to monitor peers
375 * @param pic handle for the request to cancel
378 GNUNET_TRANSPORT_monitor_peers_cancel (
379 struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
382 /* *********************** Blacklisting ************************ */
385 * Handle for blacklisting peers.
387 struct GNUNET_TRANSPORT_Blacklist;
391 * Function that decides if a connection is acceptable or not.
394 * @param pid peer to approve or disapproave
395 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
397 typedef int (*GNUNET_TRANSPORT_BlacklistCallback) (
399 const struct GNUNET_PeerIdentity *pid);
403 * Install a blacklist callback. The service will be queried for all
404 * existing connections as well as any fresh connections to check if
405 * they are permitted. If the blacklisting callback is unregistered,
406 * all hosts that were denied in the past will automatically be
407 * whitelisted again. Cancelling the blacklist handle is also the
408 * only way to re-enable connections from peers that were previously
411 * @param cfg configuration to use
412 * @param cb callback to invoke to check if connections are allowed
413 * @param cb_cls closure for @a cb
414 * @return NULL on error, otherwise handle for cancellation
416 struct GNUNET_TRANSPORT_Blacklist *
417 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
418 GNUNET_TRANSPORT_BlacklistCallback cb,
423 * Abort the blacklist. Note that this function is the only way for
424 * removing a peer from the blacklist.
426 * @param br handle of the request that is to be cancelled
429 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
433 * Handle for a plugin session state monitor.
435 struct GNUNET_TRANSPORT_PluginMonitor;
438 * Abstract representation of a plugin's session.
439 * Corresponds to the `struct GNUNET_ATS_Session` within the TRANSPORT service.
441 struct GNUNET_TRANSPORT_PluginSession;
445 * Possible states of a session in a plugin.
447 enum GNUNET_TRANSPORT_SessionState
450 * The session was created (first call for each session object).
452 GNUNET_TRANSPORT_SS_INIT,
455 * Initial session handshake is in progress.
457 GNUNET_TRANSPORT_SS_HANDSHAKE,
460 * Session is fully UP.
462 GNUNET_TRANSPORT_SS_UP,
465 * This is just an update about the session,
466 * the state did not change.
468 GNUNET_TRANSPORT_SS_UPDATE,
471 * Session is being torn down and about to disappear.
472 * Last call for each session object.
474 GNUNET_TRANSPORT_SS_DONE
479 * Information about a plugin's session.
481 struct GNUNET_TRANSPORT_SessionInfo
484 * New state of the session.
486 enum GNUNET_TRANSPORT_SessionState state;
489 * #GNUNET_YES if this is an inbound connection,
490 * #GNUNET_NO if this is an outbound connection,
491 * #GNUNET_SYSERR if connections of this plugin
492 * are so fundamentally bidirectional
493 * that they have no 'initiator'
498 * Number of messages pending transmission for this session.
500 uint32_t num_msg_pending;
503 * Number of bytes pending transmission for this session.
505 uint32_t num_bytes_pending;
508 * Until when does this plugin refuse to receive to manage
509 * staying within the inbound quota? ZERO if receive is
512 struct GNUNET_TIME_Absolute receive_delay;
515 * At what time will this session timeout (unless activity
518 struct GNUNET_TIME_Absolute session_timeout;
521 * Address used by the session. Can be NULL if none is available.
523 const struct GNUNET_HELLO_Address *address;
528 * Function called by the plugin with information about the
529 * current sessions managed by the plugin (for monitoring).
532 * @param session session handle this information is about,
533 * NULL to indicate that we are "in sync" (initial
534 * iteration complete)
535 * @param session_ctx storage location where the application
536 * can store data; will point to NULL on #GNUNET_TRANSPORT_SS_INIT,
537 * and must be reset to NULL on #GNUNET_TRANSPORT_SS_DONE
538 * @param info information about the state of the session,
539 * NULL if @a session is also NULL and we are
540 * merely signalling that the initial iteration is over;
541 * NULL with @a session being non-NULL if the monitor
542 * was being cancelled while sessions were active
544 typedef void (*GNUNET_TRANSPORT_SessionMonitorCallback) (
546 struct GNUNET_TRANSPORT_PluginSession *session,
548 const struct GNUNET_TRANSPORT_SessionInfo *info);
552 * Install a plugin session state monitor callback. The callback
553 * will be notified whenever the session changes.
555 * @param cfg configuration to use
556 * @param cb callback to invoke on events
557 * @param cb_cls closure for @a cb
558 * @return NULL on error, otherwise handle for cancellation
560 struct GNUNET_TRANSPORT_PluginMonitor *
561 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
562 GNUNET_TRANSPORT_SessionMonitorCallback cb,
567 * Cancel monitoring the plugin session state. The callback will be
568 * called once for each session that is up with the "info" argument
569 * being NULL (this is just to enable client-side cleanup).
571 * @param pm handle of the request that is to be cancelled
574 GNUNET_TRANSPORT_monitor_plugins_cancel (
575 struct GNUNET_TRANSPORT_PluginMonitor *pm);
579 * Opaque handle to the service.
581 struct GNUNET_TRANSPORT_CoreHandle;
585 * Function called to notify transport users that another
586 * peer connected to us.
589 * @param peer the identity of the peer that connected; this
590 * pointer will remain valid until the disconnect, hence
591 * applications do not necessarily have to make a copy
592 * of the value if they only need it until disconnect
593 * @param mq message queue to use to transmit to @a peer
594 * @return closure to use in MQ handlers
596 typedef void *(*GNUNET_TRANSPORT_NotifyConnect) (
598 const struct GNUNET_PeerIdentity *peer,
599 struct GNUNET_MQ_Handle *mq);
603 * Function called to notify transport users that another peer
604 * disconnected from us. The message queue that was given to the
605 * connect notification will be destroyed and must not be used
608 * @param cls closure from #GNUNET_TRANSPORT_core_connect
609 * @param peer the peer that disconnected
610 * @param handlers_cls closure of the handlers, was returned from the
611 * connect notification callback
613 typedef void (*GNUNET_TRANSPORT_NotifyDisconnect) (
615 const struct GNUNET_PeerIdentity *peer,
620 * Function called if we have "excess" bandwidth to a peer.
621 * The notification will happen the first time we have excess
622 * bandwidth, and then only again after the client has performed
623 * some transmission to the peer.
625 * Excess bandwidth is defined as being allowed (by ATS) to send
626 * more data, and us reaching the limit of the capacity build-up
627 * (which, if we go past it, means we don't use available bandwidth).
628 * See also the "max carry" in `struct GNUNET_BANDWIDTH_Tracker`.
630 * @param cls the closure
631 * @param neighbour peer that we have excess bandwidth to
632 * @param handlers_cls closure of the handlers, was returned from the
633 * connect notification callback
635 typedef void (*GNUNET_TRANSPORT_NotifyExcessBandwidth) (
637 const struct GNUNET_PeerIdentity *neighbour,
642 * Connect to the transport service. Note that the connection may
643 * complete (or fail) asynchronously.
645 * @param cfg configuration to use
646 * @param self our own identity (API should check that it matches
647 * the identity found by transport), or NULL (no check)
648 * @param handlers array of message handlers; note that the
649 * closures provided will be ignored and replaced
650 * with the respective return value from @a nc
651 * @param handlers array with handlers to call when we receive messages, or NULL
652 * @param cls closure for the @a nc, @a nd and @a neb callbacks
653 * @param nc function to call on connect events, or NULL
654 * @param nd function to call on disconnect events, or NULL
655 * @param neb function to call if we have excess bandwidth to a peer, or NULL
656 * @return NULL on error
658 struct GNUNET_TRANSPORT_CoreHandle *
659 GNUNET_TRANSPORT_core_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
660 const struct GNUNET_PeerIdentity *self,
661 const struct GNUNET_MQ_MessageHandler *handlers,
663 GNUNET_TRANSPORT_NotifyConnect nc,
664 GNUNET_TRANSPORT_NotifyDisconnect nd,
665 GNUNET_TRANSPORT_NotifyExcessBandwidth neb);
669 * Disconnect from the transport service.
671 * @param handle handle returned from connect
674 GNUNET_TRANSPORT_core_disconnect (struct GNUNET_TRANSPORT_CoreHandle *handle);
678 * Checks if a given peer is connected to us and get the message queue.
680 * @param handle connection to transport service
681 * @param peer the peer to check
682 * @return NULL if disconnected, otherwise message queue for @a peer
684 struct GNUNET_MQ_Handle *
685 GNUNET_TRANSPORT_core_get_mq (struct GNUNET_TRANSPORT_CoreHandle *handle,
686 const struct GNUNET_PeerIdentity *peer);
689 #if 0 /* keep Emacsens' auto-indent happy */
696 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
699 /** @} */ /* end of group */
701 /* end of gnunet_transport_service.h */