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 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.
17 * @author Christian Grothoff
22 * @defgroup transport Transport service
25 * @see [Documentation](https://gnunet.org/transport-service)
30 #ifndef GNUNET_TRANSPORT_SERVICE_H
31 #define GNUNET_TRANSPORT_SERVICE_H
36 #if 0 /* keep Emacsens' auto-indent happy */
41 #include "gnunet_util_lib.h"
42 #include "gnunet_ats_service.h"
45 * Version number of the transport API.
47 #define GNUNET_TRANSPORT_VERSION 0x00000003
50 /* *************************** HELLO *************************** */
53 * Handle for a #GNUNET_TRANSPORT_offer_hello operation
55 struct GNUNET_TRANSPORT_OfferHelloHandle;
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.
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
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,
81 * Cancel the request to transport to offer the HELLO message
83 * @param ohh the `struct GNUNET_TRANSPORT_OfferHelloHandle` to cancel
86 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh);
89 /* *********************** Address to String ******************* */
92 * Handle to cancel a pending address lookup.
94 struct GNUNET_TRANSPORT_AddressToStringContext;
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.
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)
114 (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
120 * Convert a binary address into a human readable address.
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
131 struct GNUNET_TRANSPORT_AddressToStringContext *
132 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle *cfg,
133 const struct GNUNET_HELLO_Address *address,
135 struct GNUNET_TIME_Relative timeout,
136 GNUNET_TRANSPORT_AddressToStringCallback aluc,
141 * Cancel request for address conversion.
143 * @param alc the context handle
146 GNUNET_TRANSPORT_address_to_string_cancel (struct GNUNET_TRANSPORT_AddressToStringContext *alc);
149 /* *********************** Monitoring ************************** */
153 * Possible state of a neighbour. Initially, we are #GNUNET_TRANSPORT_PS_NOT_CONNECTED.
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.
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.
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).
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").
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.
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'
206 enum GNUNET_TRANSPORT_PeerState
209 * Fresh peer or completely disconnected
211 GNUNET_TRANSPORT_PS_NOT_CONNECTED = 0,
214 * Asked to initiate connection, trying to get address from ATS
216 GNUNET_TRANSPORT_PS_INIT_ATS,
219 * Sent SYN message to other peer, waiting for SYN_ACK
221 GNUNET_TRANSPORT_PS_SYN_SENT,
224 * Received a SYN, asking ATS about address suggestions.
226 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
229 * SYN request from other peer was SYN_ACK'ed, waiting for ACK.
231 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
234 * Got our SYN_ACK/ACK, connection is up.
236 GNUNET_TRANSPORT_PS_CONNECTED,
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.
242 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
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.
249 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
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.
257 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
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).
263 GNUNET_TRANSPORT_PS_DISCONNECT,
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()).
274 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED
279 * Convert a transport state to a human readable string.
281 * @param state the state
284 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state);
288 * Check if a state is defined as connected
290 * @param state the state value
291 * @return #GNUNET_YES or #GNUNET_NO
294 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state);
298 * Handle for a #GNUNET_TRANSPORT_monitor_peers operation.
300 struct GNUNET_TRANSPORT_PeerMonitoringContext;
304 * Function to call with information about a peer
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.
310 * The #GNUNET_TRANSPORT_monitor_peers_cancel() call MUST not be called from
311 * within this function!
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
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);
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
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.
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.
344 * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
347 * @param cfg configuration to use
348 * @param peer a specific peer identity to obtain information for,
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
355 struct GNUNET_TRANSPORT_PeerMonitoringContext *
356 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
357 const struct GNUNET_PeerIdentity *peer,
359 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
360 void *peer_callback_cls);
364 * Cancel request to monitor peers
366 * @param pic handle for the request to cancel
369 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
372 /* *********************** Blacklisting ************************ */
375 * Handle for blacklisting peers.
377 struct GNUNET_TRANSPORT_Blacklist;
381 * Function that decides if a connection is acceptable or not.
384 * @param pid peer to approve or disapproave
385 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
388 (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
389 const struct GNUNET_PeerIdentity *pid);
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
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
406 struct GNUNET_TRANSPORT_Blacklist *
407 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
408 GNUNET_TRANSPORT_BlacklistCallback cb,
413 * Abort the blacklist. Note that this function is the only way for
414 * removing a peer from the blacklist.
416 * @param br handle of the request that is to be cancelled
419 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
423 * Handle for a plugin session state monitor.
425 struct GNUNET_TRANSPORT_PluginMonitor;
428 * Abstract representation of a plugin's session.
429 * Corresponds to the `struct GNUNET_ATS_Session` within the TRANSPORT service.
431 struct GNUNET_TRANSPORT_PluginSession;
435 * Possible states of a session in a plugin.
437 enum GNUNET_TRANSPORT_SessionState
441 * The session was created (first call for each session object).
443 GNUNET_TRANSPORT_SS_INIT,
446 * Initial session handshake is in progress.
448 GNUNET_TRANSPORT_SS_HANDSHAKE,
451 * Session is fully UP.
453 GNUNET_TRANSPORT_SS_UP,
456 * This is just an update about the session,
457 * the state did not change.
459 GNUNET_TRANSPORT_SS_UPDATE,
462 * Session is being torn down and about to disappear.
463 * Last call for each session object.
465 GNUNET_TRANSPORT_SS_DONE
471 * Information about a plugin's session.
473 struct GNUNET_TRANSPORT_SessionInfo
477 * New state of the session.
479 enum GNUNET_TRANSPORT_SessionState state;
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'
491 * Number of messages pending transmission for this session.
493 uint32_t num_msg_pending;
496 * Number of bytes pending transmission for this session.
498 uint32_t num_bytes_pending;
501 * Until when does this plugin refuse to receive to manage
502 * staying within the inbound quota? ZERO if receive is
505 struct GNUNET_TIME_Absolute receive_delay;
508 * At what time will this session timeout (unless activity
511 struct GNUNET_TIME_Absolute session_timeout;
514 * Address used by the session. Can be NULL if none is available.
516 const struct GNUNET_HELLO_Address *address;
521 * Function called by the plugin with information about the
522 * current sessions managed by the plugin (for monitoring).
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
538 (*GNUNET_TRANSPORT_SessionMonitorCallback) (void *cls,
539 struct GNUNET_TRANSPORT_PluginSession *session,
541 const struct GNUNET_TRANSPORT_SessionInfo *info);
545 * Install a plugin session state monitor callback. The callback
546 * will be notified whenever the session changes.
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
553 struct GNUNET_TRANSPORT_PluginMonitor *
554 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
555 GNUNET_TRANSPORT_SessionMonitorCallback cb,
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).
564 * @param pm handle of the request that is to be cancelled
567 GNUNET_TRANSPORT_monitor_plugins_cancel (struct GNUNET_TRANSPORT_PluginMonitor *pm);
571 #if 0 /* keep Emacsens' auto-indent happy */
578 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
581 /** @} */ /* end of group */
583 /* end of gnunet_transport_service.h */