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
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 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 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
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
41 #if 0 /* keep Emacsens' auto-indent happy */
46 #include "gnunet_util_lib.h"
47 #include "gnunet_ats_service.h"
50 * Version number of the transport API.
52 #define GNUNET_TRANSPORT_VERSION 0x00000003
55 /* *************************** HELLO *************************** */
58 * Handle for a #GNUNET_TRANSPORT_offer_hello operation
60 struct GNUNET_TRANSPORT_OfferHelloHandle;
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.
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
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,
86 * Cancel the request to transport to offer the HELLO message
88 * @param ohh the `struct GNUNET_TRANSPORT_OfferHelloHandle` to cancel
91 GNUNET_TRANSPORT_offer_hello_cancel (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)
119 (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
125 * Convert a binary address into a human readable address.
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
136 struct GNUNET_TRANSPORT_AddressToStringContext *
137 GNUNET_TRANSPORT_address_to_string (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 (struct GNUNET_TRANSPORT_AddressToStringContext *alc);
154 /* *********************** Monitoring ************************** */
158 * Possible state of a neighbour. Initially, we are #GNUNET_TRANSPORT_PS_NOT_CONNECTED.
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.
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.
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).
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").
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.
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'
211 enum GNUNET_TRANSPORT_PeerState
214 * Fresh peer or completely disconnected
216 GNUNET_TRANSPORT_PS_NOT_CONNECTED = 0,
219 * Asked to initiate connection, trying to get address from ATS
221 GNUNET_TRANSPORT_PS_INIT_ATS,
224 * Sent SYN message to other peer, waiting for SYN_ACK
226 GNUNET_TRANSPORT_PS_SYN_SENT,
229 * Received a SYN, asking ATS about address suggestions.
231 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
234 * SYN request from other peer was SYN_ACK'ed, waiting for ACK.
236 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
239 * Got our SYN_ACK/ACK, connection is up.
241 GNUNET_TRANSPORT_PS_CONNECTED,
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.
247 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
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.
254 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
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.
262 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
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).
268 GNUNET_TRANSPORT_PS_DISCONNECT,
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()).
279 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED
284 * Convert a transport state to a human readable string.
286 * @param state the state
289 GNUNET_TRANSPORT_ps2s (enum GNUNET_TRANSPORT_PeerState state);
293 * Check if a state is defined as connected
295 * @param state the state value
296 * @return #GNUNET_YES or #GNUNET_NO
299 GNUNET_TRANSPORT_is_connected (enum GNUNET_TRANSPORT_PeerState state);
303 * Handle for a #GNUNET_TRANSPORT_monitor_peers operation.
305 struct GNUNET_TRANSPORT_PeerMonitoringContext;
309 * Function to call with information about a peer
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.
315 * The #GNUNET_TRANSPORT_monitor_peers_cancel() call MUST not be called from
316 * within this function!
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
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);
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
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.
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.
349 * The #GNUNET_TRANSPORT_monitor_peers_cancel call MUST not be called in the
352 * @param cfg configuration to use
353 * @param peer a specific peer identity to obtain information for,
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
360 struct GNUNET_TRANSPORT_PeerMonitoringContext *
361 GNUNET_TRANSPORT_monitor_peers (const struct GNUNET_CONFIGURATION_Handle *cfg,
362 const struct GNUNET_PeerIdentity *peer,
364 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
365 void *peer_callback_cls);
369 * Cancel request to monitor peers
371 * @param pic handle for the request to cancel
374 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
377 /* *********************** Blacklisting ************************ */
380 * Handle for blacklisting peers.
382 struct GNUNET_TRANSPORT_Blacklist;
386 * Function that decides if a connection is acceptable or not.
389 * @param pid peer to approve or disapproave
390 * @return #GNUNET_OK if the connection is allowed, #GNUNET_SYSERR if not
393 (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
394 const struct GNUNET_PeerIdentity *pid);
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
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
411 struct GNUNET_TRANSPORT_Blacklist *
412 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
413 GNUNET_TRANSPORT_BlacklistCallback cb,
418 * Abort the blacklist. Note that this function is the only way for
419 * removing a peer from the blacklist.
421 * @param br handle of the request that is to be cancelled
424 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
428 * Handle for a plugin session state monitor.
430 struct GNUNET_TRANSPORT_PluginMonitor;
433 * Abstract representation of a plugin's session.
434 * Corresponds to the `struct GNUNET_ATS_Session` within the TRANSPORT service.
436 struct GNUNET_TRANSPORT_PluginSession;
440 * Possible states of a session in a plugin.
442 enum GNUNET_TRANSPORT_SessionState
446 * The session was created (first call for each session object).
448 GNUNET_TRANSPORT_SS_INIT,
451 * Initial session handshake is in progress.
453 GNUNET_TRANSPORT_SS_HANDSHAKE,
456 * Session is fully UP.
458 GNUNET_TRANSPORT_SS_UP,
461 * This is just an update about the session,
462 * the state did not change.
464 GNUNET_TRANSPORT_SS_UPDATE,
467 * Session is being torn down and about to disappear.
468 * Last call for each session object.
470 GNUNET_TRANSPORT_SS_DONE
476 * Information about a plugin's session.
478 struct GNUNET_TRANSPORT_SessionInfo
482 * New state of the session.
484 enum GNUNET_TRANSPORT_SessionState state;
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'
496 * Number of messages pending transmission for this session.
498 uint32_t num_msg_pending;
501 * Number of bytes pending transmission for this session.
503 uint32_t num_bytes_pending;
506 * Until when does this plugin refuse to receive to manage
507 * staying within the inbound quota? ZERO if receive is
510 struct GNUNET_TIME_Absolute receive_delay;
513 * At what time will this session timeout (unless activity
516 struct GNUNET_TIME_Absolute session_timeout;
519 * Address used by the session. Can be NULL if none is available.
521 const struct GNUNET_HELLO_Address *address;
526 * Function called by the plugin with information about the
527 * current sessions managed by the plugin (for monitoring).
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
543 (*GNUNET_TRANSPORT_SessionMonitorCallback) (void *cls,
544 struct GNUNET_TRANSPORT_PluginSession *session,
546 const struct GNUNET_TRANSPORT_SessionInfo *info);
550 * Install a plugin session state monitor callback. The callback
551 * will be notified whenever the session changes.
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
558 struct GNUNET_TRANSPORT_PluginMonitor *
559 GNUNET_TRANSPORT_monitor_plugins (const struct GNUNET_CONFIGURATION_Handle *cfg,
560 GNUNET_TRANSPORT_SessionMonitorCallback cb,
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).
569 * @param pm handle of the request that is to be cancelled
572 GNUNET_TRANSPORT_monitor_plugins_cancel (struct GNUNET_TRANSPORT_PluginMonitor *pm);
576 #if 0 /* keep Emacsens' auto-indent happy */
583 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
586 /** @} */ /* end of group */
588 /* end of gnunet_transport_service.h */