2 This file is part of GNUnet
3 Copyright (C) 2009-2014 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
25 * Transport service plugin API
27 * @defgroup transport-plugin Transport Service plugin API
29 * Specifies the struct that is given to the plugin's entry method and the other
30 * struct that must be returned. Note that the destructors of transport plugins
31 * will be given the value returned by the constructor and is expected to return
36 #ifndef PLUGIN_TRANSPORT_H
37 #define PLUGIN_TRANSPORT_H
39 #include "gnunet_configuration_lib.h"
40 #include "gnunet_scheduler_lib.h"
41 #include "gnunet_statistics_service.h"
42 #include "gnunet_transport_service.h"
43 #include "gnunet_ats_service.h"
45 #define TRANSPORT_SESSION_INBOUND_STRING "<inbound>"
48 * Opaque pointer that plugins can use to distinguish specific
49 * connections to a given peer. Typically used by stateful plugins to
50 * allow the service to refer to specific streams instead of a more
51 * general notion of "some connection" to the given peer. This is
52 * useful since sometimes (i.e. for inbound TCP connections) a
53 * connection may not have an address that can be used for meaningful
54 * distinction between sessions to the same peer.
56 * Each 'struct GNUNET_ATS_Session' MUST start with the 'struct GNUNET_PeerIdentity'
57 * of the peer the session is for (which will be used for some error
58 * checking by the ATS code).
60 struct GNUNET_ATS_Session;
64 * Function that will be called whenever the plugin internally
65 * cleans up a session pointer and hence the service needs to
66 * discard all of those sessions as well. Plugins that do not
67 * use sessions can simply omit calling this function and always
68 * use NULL wherever a session pointer is needed. This function
69 * should be called BEFORE a potential "TransmitContinuation"
70 * from the "TransmitFunction".
73 * @param peer which peer was the session for
74 * @param session which session is being destroyed
77 (*GNUNET_TRANSPORT_SessionEnd) (void *cls,
78 const struct GNUNET_HELLO_Address *address,
79 struct GNUNET_ATS_Session *session);
83 * Plugin tells transport service about a new inbound session
86 * @param address the address
87 * @param session the new session
88 * @param net network information
91 (*GNUNET_TRANSPORT_SessionStart) (void *cls,
92 const struct GNUNET_HELLO_Address *address,
93 struct GNUNET_ATS_Session *session,
94 enum GNUNET_NetworkType net);
98 * Function called by the transport for each received message.
99 * This function should also be called with "NULL" for the
100 * message to signal that the other peer disconnected.
103 * @param peer (claimed) identity of the other peer
104 * @param message the message, NULL if we only care about
105 * learning about the delay until we should receive again
106 * @param session identifier used for this session (NULL for plugins
107 * that do not offer bi-directional communication to the sender
108 * using the same "connection")
109 * @param sender_address binary address of the sender (if we established the
110 * connection or are otherwise sure of it; should be NULL
111 * for inbound TCP/UDP connections since it it not clear
112 * that we could establish ourselves a connection to that
113 * IP address and get the same system)
114 * @param sender_address_len number of bytes in @a sender_address
115 * @return how long the plugin should wait until receiving more data;
116 * returning #GNUNET_TIME_UNIT_FOREVER_REL means that the
117 * connection should be closed
119 typedef struct GNUNET_TIME_Relative
120 (*GNUNET_TRANSPORT_PluginReceiveCallback) (void *cls,
121 const struct GNUNET_HELLO_Address *address,
122 struct GNUNET_ATS_Session *session,
123 const struct GNUNET_MessageHeader *message);
127 * Function that can be called by plugins to figure if an address is
128 * an loopback, LAN or WAN address. Ultimately invokes
129 * #GNUNET_ATS_address_get_type().
132 * @param addr binary address
133 * @param addrlen length of the @a addr
134 * @return type of the network the address belongs to
136 typedef enum GNUNET_NetworkType
137 (*GNUNET_TRANSPORT_AddressToType) (void *cls,
138 const struct sockaddr *addr,
143 * Function called when distance of an address changes.
147 * @param distance new distance
150 (*GNUNET_TRANSPORT_UpdateAddressDistance) (void *cls,
151 const struct GNUNET_HELLO_Address *address,
156 * Function that will be called for each address the transport
157 * is aware that it might be reachable under.
160 * @param add_remove should the address added (#GNUNET_YES) or removed (#GNUNET_NO) from the
161 * set of valid addresses?
162 * @param address the address to add or remove
165 (*GNUNET_TRANSPORT_AddressNotification) (void *cls,
167 const struct GNUNET_HELLO_Address *address);
171 * Function that will be called whenever the plugin receives data over
172 * the network and wants to determine how long it should wait until
173 * the next time it reads from the given peer. Note that some plugins
174 * (such as UDP) may not be able to wait (for a particular peer), so
175 * the waiting part is optional. Plugins that can wait should call
176 * this function, sleep the given amount of time, and call it again
177 * (with zero bytes read) UNTIL it returns zero and only then read.
180 * @param peer which peer did we read data from
181 * @param amount_recved number of bytes read (can be zero)
182 * @return how long to wait until reading more from this peer
183 * (to enforce inbound quotas); returning #GNUNET_TIME_UNIT_FOREVER_REL
184 * means that the connection should be closed
186 typedef struct GNUNET_TIME_Relative
187 (*GNUNET_TRANSPORT_TrafficReport) (void *cls,
188 const struct GNUNET_PeerIdentity *peer,
189 size_t amount_recved);
193 * Function that returns a HELLO message.
195 * @return HELLO message (FIXME with what?)
197 typedef const struct GNUNET_MessageHeader *
198 (*GNUNET_TRANSPORT_GetHelloCallback) (void);
202 * The transport service will pass a pointer to a struct
203 * of this type as the first and only argument to the
204 * entry point of each transport plugin.
206 struct GNUNET_TRANSPORT_PluginEnvironment
209 * Configuration to use.
211 const struct GNUNET_CONFIGURATION_Handle *cfg;
214 * Identity of this peer.
216 const struct GNUNET_PeerIdentity *my_identity;
219 * Closure for the various callbacks.
224 * Handle for reporting statistics.
226 struct GNUNET_STATISTICS_Handle *stats;
229 * Function that should be called by the transport plugin
230 * whenever a message is received. If this field is "NULL",
231 * the plugin should load in 'stub' mode and NOT fully
232 * initialize and instead only return an API with the
233 * @e address_pretty_printer, @e address_to_string and
234 * @e string_to_address functions.
236 GNUNET_TRANSPORT_PluginReceiveCallback receive;
239 * Function that returns our HELLO.
241 GNUNET_TRANSPORT_GetHelloCallback get_our_hello;
244 * Function that must be called by each plugin to notify the
245 * transport service about the addresses under which the transport
246 * provided by the plugin can be reached.
248 GNUNET_TRANSPORT_AddressNotification notify_address;
251 * Function that must be called by the plugin when a non-NULL
252 * session handle stops being valid (is destroyed).
254 GNUNET_TRANSPORT_SessionEnd session_end;
257 * Function called by the plugin when a new (incoming) session was created
258 * not explicitly created using the the get_session function
260 GNUNET_TRANSPORT_SessionStart session_start;
263 * Function that will be called to figure if an address is an loopback,
264 * LAN, WAN etc. address
266 GNUNET_TRANSPORT_AddressToType get_address_type;
269 * Function that will be called by DV to update distance for
272 GNUNET_TRANSPORT_UpdateAddressDistance update_address_distance;
275 * What is the maximum number of connections that this transport
276 * should allow? Transports that do not have sessions (such as
277 * UDP) can ignore this value.
279 uint32_t max_connections;
285 * Function called by the #GNUNET_TRANSPORT_TransmitFunction
286 * upon "completion". In the case that a peer disconnects,
287 * this function must be called for each pending request
288 * (with a 'failure' indication) AFTER notifying the service
289 * about the disconnect event (so that the service won't try
290 * to transmit more messages, believing the connection still
294 * @param target who was the recipient of the message?
295 * @param result #GNUNET_OK on success
296 * #GNUNET_SYSERR if the target disconnected;
297 * disconnect will ALSO be signalled using
298 * the ReceiveCallback.
299 * @param size_payload bytes of payload from transport service in message
300 * @param size_on_wire bytes required on wire for transmission,
301 * 0 if result == #GNUNET_SYSERR
304 (*GNUNET_TRANSPORT_TransmitContinuation) (void *cls,
305 const struct GNUNET_PeerIdentity *target,
308 size_t size_on_wire);
312 * The new send function with just the session and no address
314 * Function that can be used by the transport service to transmit
315 * a message using the plugin. Note that in the case of a
316 * peer disconnecting, the continuation MUST be called
317 * prior to the disconnect notification itself. This function
318 * will be called with this peer's HELLO message to initiate
319 * a fresh connection to another peer.
322 * @param session which session must be used
323 * @param msgbuf the message to transmit
324 * @param msgbuf_size number of bytes in @a msgbuf
325 * @param priority how important is the message (most plugins will
326 * ignore message priority and just FIFO)
327 * @param to how long to wait at most for the transmission (does not
328 * require plugins to discard the message after the timeout,
329 * just advisory for the desired delay; most plugins will ignore
331 * @param cont continuation to call once the message has
332 * been transmitted (or if the transport is ready
333 * for the next transmission call; or if the
334 * peer disconnected...); can be NULL
335 * @param cont_cls closure for @a cont
336 * @return number of bytes used (on the physical network, with overheads);
337 * -1 on hard errors (i.e. address invalid); 0 is a legal value
338 * and does NOT mean that the message was not transmitted (DV)
341 (*GNUNET_TRANSPORT_TransmitFunction) (void *cls,
342 struct GNUNET_ATS_Session *session,
345 unsigned int priority,
346 struct GNUNET_TIME_Relative to,
347 GNUNET_TRANSPORT_TransmitContinuation cont,
352 * Function that can be called to force a disconnect from the
353 * specified neighbour for the given session only. . This should
354 * also cancel all previously scheduled transmissions for this
355 * session. Obviously the transmission may have been partially
356 * completed already, which is OK. The plugin is supposed to close
357 * the connection (if applicable).
359 * @param cls closure with the `struct Plugin`
360 * @param session session to destroy
361 * @return #GNUNET_OK on success
364 (*GNUNET_TRANSPORT_DisconnectSessionFunction) (void *cls,
365 struct GNUNET_ATS_Session *session);
369 * Function that is called to get the keepalive factor.
370 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
371 * calculate the interval between keepalive packets.
373 * @param cls closure with the `struct Plugin`
374 * @return keepalive factor
377 (*GNUNET_TRANSPORT_QueryKeepaliveFactorFunction) (void *cls);
381 * Function that can be called to force a disconnect from the
382 * specified neighbour. This should also cancel all previously
383 * scheduled transmissions. Obviously the transmission may have been
384 * partially completed already, which is OK. The plugin is supposed
385 * to close the connection (if applicable) and no longer call the
386 * transmit continuation(s).
389 * @param target peer for which the last transmission is
393 (*GNUNET_TRANSPORT_DisconnectPeerFunction) (void *cls,
394 const struct GNUNET_PeerIdentity *target);
398 * Function called by the pretty printer for the resolved address for
399 * each human-readable address obtained. The callback can be called
400 * several times. The last invocation must be with a @a address of
401 * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
402 * errors, the callback might be called first with @a address NULL and
403 * @a res being #GNUNET_SYSERR. In that case, there must still be a
404 * subsequent call later with @a address NULL and @a res #GNUNET_OK.
407 * @param address one of the names for the host, NULL on last callback
408 * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on failure,
409 * #GNUNET_OK on last callback
412 (*GNUNET_TRANSPORT_AddressStringCallback) (void *cls,
418 * Convert the transports address to a nice, human-readable
422 * @param type name of the transport that generated the address
423 * @param addr one of the addresses of the host, NULL for the last address
424 * the specific address format depends on the transport
425 * @param addrlen length of the @a addr
426 * @param numeric should (IP) addresses be displayed in numeric form?
427 * @param timeout after how long should we give up?
428 * @param asc function to call on each string
429 * @param asc_cls closure for @a asc
432 (*GNUNET_TRANSPORT_AddressPrettyPrinter) (void *cls,
437 struct GNUNET_TIME_Relative timeout,
438 GNUNET_TRANSPORT_AddressStringCallback asc,
443 * Another peer has suggested an address for this peer and transport
444 * plugin. Check that this could be a valid address. This function
445 * is not expected to 'validate' the address in the sense of trying to
446 * connect to it but simply to see if the binary format is technically
447 * legal for establishing a connection to this peer (and make sure that
448 * the address really corresponds to our network connection/settings
449 * and not some potential man-in-the-middle).
451 * @param addr pointer to the address
452 * @param addrlen length of @a addr
453 * @return #GNUNET_OK if this is a plausible address for this peer
454 * and transport, #GNUNET_SYSERR if not
457 (*GNUNET_TRANSPORT_CheckAddress) (void *cls,
463 * Create a new session to transmit data to the target
464 * This session will used to send data to this peer and the plugin will
465 * notify us by calling the env->session_end function
467 * @param cls the plugin
468 * @param address the hello address
469 * @return the session if the address is valid, NULL otherwise
471 typedef struct GNUNET_ATS_Session *
472 (*GNUNET_TRANSPORT_CreateSession) (void *cls,
473 const struct GNUNET_HELLO_Address *address);
477 * Function that will be called whenever the transport service wants to
478 * notify the plugin that a session is still active and in use and
479 * therefore the session timeout for this session has to be updated
482 * @param peer which peer was the session for
483 * @param session which session is being updated
486 (*GNUNET_TRANSPORT_UpdateSessionTimeout) (void *cls,
487 const struct GNUNET_PeerIdentity *peer,
488 struct GNUNET_ATS_Session *session);
492 * Function that will be called whenever the transport service wants to
493 * notify the plugin that the inbound quota changed and that the plugin
494 * should update it's delay for the next receive value
497 * @param peer which peer was the session for
498 * @param session which session is being updated
499 * @param delay new delay to use for receiving
502 (*GNUNET_TRANSPORT_UpdateInboundDelay) (void *cls,
503 const struct GNUNET_PeerIdentity *peer,
504 struct GNUNET_ATS_Session *session,
505 struct GNUNET_TIME_Relative delay);
509 * Function called for a quick conversion of the binary address to
510 * a numeric address. Note that the caller must not free the
511 * address and that the next call to this function is allowed
512 * to override the address again.
515 * @param addr binary address
516 * @param addr_len length of the @a addr
517 * @return string representing the same address
520 (*GNUNET_TRANSPORT_AddressToString) (void *cls,
526 * Function called to convert a string address to
529 * @param cls closure (`struct Plugin*`)
530 * @param addr string address
531 * @param addrlen length of the @a addr including \0 termination
532 * @param buf location to store the buffer
533 * If the function returns #GNUNET_SYSERR, its contents are undefined.
534 * @param added length of created address
535 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
538 (*GNUNET_TRANSPORT_StringToAddress) (void *cls,
546 * Function to obtain the network type for a session
548 * @param cls closure (`struct Plugin *`)
549 * @param session the session
550 * @return the network type
552 typedef enum GNUNET_NetworkType
553 (*GNUNET_TRANSPORT_GetNetworkType) (void *cls,
554 struct GNUNET_ATS_Session *session);
558 * Function to obtain the network type for an address.
560 * @param cls closure (`struct Plugin *`)
561 * @param address the address
562 * @return the network type
564 typedef enum GNUNET_NetworkType
565 (*GNUNET_TRANSPORT_GetNetworkTypeForAddress) (void *cls,
566 const struct GNUNET_HELLO_Address *address);
570 * Function called by the plugin with information about the
571 * current sessions managed by the plugin (for monitoring).
574 * @param session session handle this information is about,
575 * NULL to indicate that we are "in sync" (initial
576 * iteration complete)
577 * @param info information about the state of the session,
578 * NULL if @a session is also NULL and we are
579 * merely signalling that the initial iteration is over
582 (*GNUNET_TRANSPORT_SessionInfoCallback) (void *cls,
583 struct GNUNET_ATS_Session *session,
584 const struct GNUNET_TRANSPORT_SessionInfo *info);
588 * Begin monitoring sessions of a plugin. There can only
589 * be one active monitor per plugin (i.e. if there are
590 * multiple monitors, the transport service needs to
591 * multiplex the generated events over all of them).
593 * @param cls closure of the plugin
594 * @param sic callback to invoke, NULL to disable monitor;
595 * plugin will being by iterating over all active
596 * sessions immediately and then enter monitor mode
597 * @param sic_cls closure for @a sic
600 (*GNUNET_TRANSPORT_SessionMonitorSetup) (void *cls,
601 GNUNET_TRANSPORT_SessionInfoCallback sic,
606 * Each plugin is required to return a pointer to a struct of this
607 * type as the return value from its entry point.
609 struct GNUNET_TRANSPORT_PluginFunctions
613 * Closure for all of the callbacks.
618 * Function that the transport service will use to transmit data to
619 * another peer. May be NULL for plugins that only support
620 * receiving data. After this call, the plugin call the specified
621 * continuation with success or error before notifying us about the
622 * target having disconnected.
624 GNUNET_TRANSPORT_TransmitFunction send;
627 * Function that can be used to force the plugin to disconnect from
628 * the given peer and cancel all previous transmissions (and their
631 GNUNET_TRANSPORT_DisconnectPeerFunction disconnect_peer;
634 * Function that can be used to force the plugin to disconnect from
635 * the given peer and cancel all previous transmissions (and their
638 GNUNET_TRANSPORT_DisconnectSessionFunction disconnect_session;
641 * Function that will be called whenever the transport service wants to
642 * notify the plugin that a session is still active and in use and
643 * therefore the session timeout for this session has to be updated
645 GNUNET_TRANSPORT_UpdateSessionTimeout update_session_timeout;
648 * Function that will be called whenever the transport service wants to
649 * notify the plugin that the inbound quota changed and that the plugin
650 * should update it's delay for the next receive value
652 GNUNET_TRANSPORT_UpdateInboundDelay update_inbound_delay;
655 * Function that is used to query keepalive factor.
656 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
657 * calculate the interval between keepalive packets.
659 GNUNET_TRANSPORT_QueryKeepaliveFactorFunction query_keepalive_factor;
662 * Function to pretty-print addresses.
664 GNUNET_TRANSPORT_AddressPrettyPrinter address_pretty_printer;
667 * Function that will be called to check if a binary address
668 * for this plugin is well-formed and corresponds to an
669 * address for THIS peer (as per our configuration). Naturally,
670 * if absolutely necessary, plugins can be a bit conservative in
671 * their answer, but in general plugins should make sure that the
672 * address does not redirect traffic to a 3rd party that might
673 * try to man-in-the-middle our traffic.
675 GNUNET_TRANSPORT_CheckAddress check_address;
678 * Function that will be called to convert a binary address
679 * to a string (numeric conversion only).
681 GNUNET_TRANSPORT_AddressToString address_to_string;
684 * Function that will be called to convert a string address
685 * to binary (numeric conversion only).
687 GNUNET_TRANSPORT_StringToAddress string_to_address;
690 * Function that will be called tell the plugin to create a session
693 GNUNET_TRANSPORT_CreateSession get_session;
696 * Function to obtain the network type for a session
698 GNUNET_TRANSPORT_GetNetworkType get_network;
701 * Function to obtain the network type for an address
703 GNUNET_TRANSPORT_GetNetworkTypeForAddress get_network_for_address;
706 * Function to monitor the sessions managed by the plugin.
708 GNUNET_TRANSPORT_SessionMonitorSetup setup_monitor;
712 /*#ifndef PLUGIN_TRANSPORT_H*/
715 /** @} */ /* end of group */
717 /* end of gnunet_transport_plugin.h */