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/>.
20 * @author Christian Grothoff
23 * Transport service plugin API
25 * @defgroup transport-plugin Transport Service plugin API
27 * Specifies the struct that is given to the plugin's entry method and the other
28 * struct that must be returned. Note that the destructors of transport plugins
29 * will be given the value returned by the constructor and is expected to return
34 #ifndef PLUGIN_TRANSPORT_H
35 #define PLUGIN_TRANSPORT_H
37 #include "gnunet_configuration_lib.h"
38 #include "gnunet_scheduler_lib.h"
39 #include "gnunet_statistics_service.h"
40 #include "gnunet_transport_service.h"
41 #include "gnunet_ats_service.h"
43 #define TRANSPORT_SESSION_INBOUND_STRING "<inbound>"
46 * Opaque pointer that plugins can use to distinguish specific
47 * connections to a given peer. Typically used by stateful plugins to
48 * allow the service to refer to specific streams instead of a more
49 * general notion of "some connection" to the given peer. This is
50 * useful since sometimes (i.e. for inbound TCP connections) a
51 * connection may not have an address that can be used for meaningful
52 * distinction between sessions to the same peer.
54 * Each 'struct GNUNET_ATS_Session' MUST start with the 'struct GNUNET_PeerIdentity'
55 * of the peer the session is for (which will be used for some error
56 * checking by the ATS code).
58 struct GNUNET_ATS_Session;
62 * Function that will be called whenever the plugin internally
63 * cleans up a session pointer and hence the service needs to
64 * discard all of those sessions as well. Plugins that do not
65 * use sessions can simply omit calling this function and always
66 * use NULL wherever a session pointer is needed. This function
67 * should be called BEFORE a potential "TransmitContinuation"
68 * from the "TransmitFunction".
71 * @param peer which peer was the session for
72 * @param session which session is being destroyed
75 (*GNUNET_TRANSPORT_SessionEnd) (void *cls,
76 const struct GNUNET_HELLO_Address *address,
77 struct GNUNET_ATS_Session *session);
81 * Plugin tells transport service about a new inbound session
84 * @param address the address
85 * @param session the new session
86 * @param net network information
89 (*GNUNET_TRANSPORT_SessionStart) (void *cls,
90 const struct GNUNET_HELLO_Address *address,
91 struct GNUNET_ATS_Session *session,
92 enum GNUNET_ATS_Network_Type net);
96 * Function called by the transport for each received message.
97 * This function should also be called with "NULL" for the
98 * message to signal that the other peer disconnected.
101 * @param peer (claimed) identity of the other peer
102 * @param message the message, NULL if we only care about
103 * learning about the delay until we should receive again
104 * @param session identifier used for this session (NULL for plugins
105 * that do not offer bi-directional communication to the sender
106 * using the same "connection")
107 * @param sender_address binary address of the sender (if we established the
108 * connection or are otherwise sure of it; should be NULL
109 * for inbound TCP/UDP connections since it it not clear
110 * that we could establish ourselves a connection to that
111 * IP address and get the same system)
112 * @param sender_address_len number of bytes in @a sender_address
113 * @return how long the plugin should wait until receiving more data;
114 * returning #GNUNET_TIME_UNIT_FOREVER_REL means that the
115 * connection should be closed
117 typedef struct GNUNET_TIME_Relative
118 (*GNUNET_TRANSPORT_PluginReceiveCallback) (void *cls,
119 const struct GNUNET_HELLO_Address *address,
120 struct GNUNET_ATS_Session *session,
121 const struct GNUNET_MessageHeader *message);
125 * Function that can be called by plugins to figure if an address is
126 * an loopback, LAN or WAN address. Ultimately invokes
127 * #GNUNET_ATS_address_get_type().
130 * @param addr binary address
131 * @param addrlen length of the @a addr
132 * @return type of the network the address belongs to
134 typedef enum GNUNET_ATS_Network_Type
135 (*GNUNET_TRANSPORT_AddressToType) (void *cls,
136 const struct sockaddr *addr,
141 * Function called when distance of an address changes.
145 * @param distance new distance
148 (*GNUNET_TRANSPORT_UpdateAddressDistance) (void *cls,
149 const struct GNUNET_HELLO_Address *address,
154 * Function that will be called for each address the transport
155 * is aware that it might be reachable under.
158 * @param add_remove should the address added (#GNUNET_YES) or removed (#GNUNET_NO) from the
159 * set of valid addresses?
160 * @param address the address to add or remove
163 (*GNUNET_TRANSPORT_AddressNotification) (void *cls,
165 const struct GNUNET_HELLO_Address *address);
169 * Function that will be called whenever the plugin receives data over
170 * the network and wants to determine how long it should wait until
171 * the next time it reads from the given peer. Note that some plugins
172 * (such as UDP) may not be able to wait (for a particular peer), so
173 * the waiting part is optional. Plugins that can wait should call
174 * this function, sleep the given amount of time, and call it again
175 * (with zero bytes read) UNTIL it returns zero and only then read.
178 * @param peer which peer did we read data from
179 * @param amount_recved number of bytes read (can be zero)
180 * @return how long to wait until reading more from this peer
181 * (to enforce inbound quotas); returning #GNUNET_TIME_UNIT_FOREVER_REL
182 * means that the connection should be closed
184 typedef struct GNUNET_TIME_Relative
185 (*GNUNET_TRANSPORT_TrafficReport) (void *cls,
186 const struct GNUNET_PeerIdentity *peer,
187 size_t amount_recved);
191 * Function that returns a HELLO message.
193 * @return HELLO message (FIXME with what?)
195 typedef const struct GNUNET_MessageHeader *
196 (*GNUNET_TRANSPORT_GetHelloCallback) (void);
200 * The transport service will pass a pointer to a struct
201 * of this type as the first and only argument to the
202 * entry point of each transport plugin.
204 struct GNUNET_TRANSPORT_PluginEnvironment
207 * Configuration to use.
209 const struct GNUNET_CONFIGURATION_Handle *cfg;
212 * Identity of this peer.
214 const struct GNUNET_PeerIdentity *my_identity;
217 * Closure for the various callbacks.
222 * Handle for reporting statistics.
224 struct GNUNET_STATISTICS_Handle *stats;
227 * Function that should be called by the transport plugin
228 * whenever a message is received. If this field is "NULL",
229 * the plugin should load in 'stub' mode and NOT fully
230 * initialize and instead only return an API with the
231 * @e address_pretty_printer, @e address_to_string and
232 * @e string_to_address functions.
234 GNUNET_TRANSPORT_PluginReceiveCallback receive;
237 * Function that returns our HELLO.
239 GNUNET_TRANSPORT_GetHelloCallback get_our_hello;
242 * Function that must be called by each plugin to notify the
243 * transport service about the addresses under which the transport
244 * provided by the plugin can be reached.
246 GNUNET_TRANSPORT_AddressNotification notify_address;
249 * Function that must be called by the plugin when a non-NULL
250 * session handle stops being valid (is destroyed).
252 GNUNET_TRANSPORT_SessionEnd session_end;
255 * Function called by the plugin when a new (incoming) session was created
256 * not explicitly created using the the get_session function
258 GNUNET_TRANSPORT_SessionStart session_start;
261 * Function that will be called to figure if an address is an loopback,
262 * LAN, WAN etc. address
264 GNUNET_TRANSPORT_AddressToType get_address_type;
267 * Function that will be called by DV to update distance for
270 GNUNET_TRANSPORT_UpdateAddressDistance update_address_distance;
273 * What is the maximum number of connections that this transport
274 * should allow? Transports that do not have sessions (such as
275 * UDP) can ignore this value.
277 uint32_t max_connections;
283 * Function called by the #GNUNET_TRANSPORT_TransmitFunction
284 * upon "completion". In the case that a peer disconnects,
285 * this function must be called for each pending request
286 * (with a 'failure' indication) AFTER notifying the service
287 * about the disconnect event (so that the service won't try
288 * to transmit more messages, believing the connection still
292 * @param target who was the recipient of the message?
293 * @param result #GNUNET_OK on success
294 * #GNUNET_SYSERR if the target disconnected;
295 * disconnect will ALSO be signalled using
296 * the ReceiveCallback.
297 * @param size_payload bytes of payload from transport service in message
298 * @param size_on_wire bytes required on wire for transmission,
299 * 0 if result == #GNUNET_SYSERR
302 (*GNUNET_TRANSPORT_TransmitContinuation) (void *cls,
303 const struct GNUNET_PeerIdentity *target,
306 size_t size_on_wire);
310 * The new send function with just the session and no address
312 * Function that can be used by the transport service to transmit
313 * a message using the plugin. Note that in the case of a
314 * peer disconnecting, the continuation MUST be called
315 * prior to the disconnect notification itself. This function
316 * will be called with this peer's HELLO message to initiate
317 * a fresh connection to another peer.
320 * @param session which session must be used
321 * @param msgbuf the message to transmit
322 * @param msgbuf_size number of bytes in @a msgbuf
323 * @param priority how important is the message (most plugins will
324 * ignore message priority and just FIFO)
325 * @param to how long to wait at most for the transmission (does not
326 * require plugins to discard the message after the timeout,
327 * just advisory for the desired delay; most plugins will ignore
329 * @param cont continuation to call once the message has
330 * been transmitted (or if the transport is ready
331 * for the next transmission call; or if the
332 * peer disconnected...); can be NULL
333 * @param cont_cls closure for @a cont
334 * @return number of bytes used (on the physical network, with overheads);
335 * -1 on hard errors (i.e. address invalid); 0 is a legal value
336 * and does NOT mean that the message was not transmitted (DV)
339 (*GNUNET_TRANSPORT_TransmitFunction) (void *cls,
340 struct GNUNET_ATS_Session *session,
343 unsigned int priority,
344 struct GNUNET_TIME_Relative to,
345 GNUNET_TRANSPORT_TransmitContinuation cont,
350 * Function that can be called to force a disconnect from the
351 * specified neighbour for the given session only. . This should
352 * also cancel all previously scheduled transmissions for this
353 * session. Obviously the transmission may have been partially
354 * completed already, which is OK. The plugin is supposed to close
355 * the connection (if applicable).
357 * @param cls closure with the `struct Plugin`
358 * @param session session to destroy
359 * @return #GNUNET_OK on success
362 (*GNUNET_TRANSPORT_DisconnectSessionFunction) (void *cls,
363 struct GNUNET_ATS_Session *session);
367 * Function that is called to get the keepalive factor.
368 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
369 * calculate the interval between keepalive packets.
371 * @param cls closure with the `struct Plugin`
372 * @return keepalive factor
375 (*GNUNET_TRANSPORT_QueryKeepaliveFactorFunction) (void *cls);
379 * Function that can be called to force a disconnect from the
380 * specified neighbour. This should also cancel all previously
381 * scheduled transmissions. Obviously the transmission may have been
382 * partially completed already, which is OK. The plugin is supposed
383 * to close the connection (if applicable) and no longer call the
384 * transmit continuation(s).
387 * @param target peer for which the last transmission is
391 (*GNUNET_TRANSPORT_DisconnectPeerFunction) (void *cls,
392 const struct GNUNET_PeerIdentity *target);
396 * Function called by the pretty printer for the resolved address for
397 * each human-readable address obtained. The callback can be called
398 * several times. The last invocation must be with a @a address of
399 * NULL and a @a res of #GNUNET_OK. Thus, to indicate conversion
400 * errors, the callback might be called first with @a address NULL and
401 * @a res being #GNUNET_SYSERR. In that case, there must still be a
402 * subsequent call later with @a address NULL and @a res #GNUNET_OK.
405 * @param address one of the names for the host, NULL on last callback
406 * @param res #GNUNET_OK if conversion was successful, #GNUNET_SYSERR on failure,
407 * #GNUNET_OK on last callback
410 (*GNUNET_TRANSPORT_AddressStringCallback) (void *cls,
416 * Convert the transports address to a nice, human-readable
420 * @param type name of the transport that generated the address
421 * @param addr one of the addresses of the host, NULL for the last address
422 * the specific address format depends on the transport
423 * @param addrlen length of the @a addr
424 * @param numeric should (IP) addresses be displayed in numeric form?
425 * @param timeout after how long should we give up?
426 * @param asc function to call on each string
427 * @param asc_cls closure for @a asc
430 (*GNUNET_TRANSPORT_AddressPrettyPrinter) (void *cls,
435 struct GNUNET_TIME_Relative timeout,
436 GNUNET_TRANSPORT_AddressStringCallback asc,
441 * Another peer has suggested an address for this peer and transport
442 * plugin. Check that this could be a valid address. This function
443 * is not expected to 'validate' the address in the sense of trying to
444 * connect to it but simply to see if the binary format is technically
445 * legal for establishing a connection to this peer (and make sure that
446 * the address really corresponds to our network connection/settings
447 * and not some potential man-in-the-middle).
449 * @param addr pointer to the address
450 * @param addrlen length of @a addr
451 * @return #GNUNET_OK if this is a plausible address for this peer
452 * and transport, #GNUNET_SYSERR if not
455 (*GNUNET_TRANSPORT_CheckAddress) (void *cls,
461 * Create a new session to transmit data to the target
462 * This session will used to send data to this peer and the plugin will
463 * notify us by calling the env->session_end function
465 * @param cls the plugin
466 * @param address the hello address
467 * @return the session if the address is valid, NULL otherwise
469 typedef struct GNUNET_ATS_Session *
470 (*GNUNET_TRANSPORT_CreateSession) (void *cls,
471 const struct GNUNET_HELLO_Address *address);
475 * Function that will be called whenever the transport service wants to
476 * notify the plugin that a session is still active and in use and
477 * therefore the session timeout for this session has to be updated
480 * @param peer which peer was the session for
481 * @param session which session is being updated
484 (*GNUNET_TRANSPORT_UpdateSessionTimeout) (void *cls,
485 const struct GNUNET_PeerIdentity *peer,
486 struct GNUNET_ATS_Session *session);
490 * Function that will be called whenever the transport service wants to
491 * notify the plugin that the inbound quota changed and that the plugin
492 * should update it's delay for the next receive value
495 * @param peer which peer was the session for
496 * @param session which session is being updated
497 * @param delay new delay to use for receiving
500 (*GNUNET_TRANSPORT_UpdateInboundDelay) (void *cls,
501 const struct GNUNET_PeerIdentity *peer,
502 struct GNUNET_ATS_Session *session,
503 struct GNUNET_TIME_Relative delay);
507 * Function called for a quick conversion of the binary address to
508 * a numeric address. Note that the caller must not free the
509 * address and that the next call to this function is allowed
510 * to override the address again.
513 * @param addr binary address
514 * @param addr_len length of the @a addr
515 * @return string representing the same address
518 (*GNUNET_TRANSPORT_AddressToString) (void *cls,
524 * Function called to convert a string address to
527 * @param cls closure (`struct Plugin*`)
528 * @param addr string address
529 * @param addrlen length of the @a addr including \0 termination
530 * @param buf location to store the buffer
531 * If the function returns #GNUNET_SYSERR, its contents are undefined.
532 * @param added length of created address
533 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
536 (*GNUNET_TRANSPORT_StringToAddress) (void *cls,
544 * Function to obtain the network type for a session
546 * @param cls closure (`struct Plugin *`)
547 * @param session the session
548 * @return the network type
550 typedef enum GNUNET_ATS_Network_Type
551 (*GNUNET_TRANSPORT_GetNetworkType) (void *cls,
552 struct GNUNET_ATS_Session *session);
556 * Function to obtain the network type for an address.
558 * @param cls closure (`struct Plugin *`)
559 * @param address the address
560 * @return the network type
562 typedef enum GNUNET_ATS_Network_Type
563 (*GNUNET_TRANSPORT_GetNetworkTypeForAddress) (void *cls,
564 const struct GNUNET_HELLO_Address *address);
568 * Function called by the plugin with information about the
569 * current sessions managed by the plugin (for monitoring).
572 * @param session session handle this information is about,
573 * NULL to indicate that we are "in sync" (initial
574 * iteration complete)
575 * @param info information about the state of the session,
576 * NULL if @a session is also NULL and we are
577 * merely signalling that the initial iteration is over
580 (*GNUNET_TRANSPORT_SessionInfoCallback) (void *cls,
581 struct GNUNET_ATS_Session *session,
582 const struct GNUNET_TRANSPORT_SessionInfo *info);
586 * Begin monitoring sessions of a plugin. There can only
587 * be one active monitor per plugin (i.e. if there are
588 * multiple monitors, the transport service needs to
589 * multiplex the generated events over all of them).
591 * @param cls closure of the plugin
592 * @param sic callback to invoke, NULL to disable monitor;
593 * plugin will being by iterating over all active
594 * sessions immediately and then enter monitor mode
595 * @param sic_cls closure for @a sic
598 (*GNUNET_TRANSPORT_SessionMonitorSetup) (void *cls,
599 GNUNET_TRANSPORT_SessionInfoCallback sic,
604 * Each plugin is required to return a pointer to a struct of this
605 * type as the return value from its entry point.
607 struct GNUNET_TRANSPORT_PluginFunctions
611 * Closure for all of the callbacks.
616 * Function that the transport service will use to transmit data to
617 * another peer. May be NULL for plugins that only support
618 * receiving data. After this call, the plugin call the specified
619 * continuation with success or error before notifying us about the
620 * target having disconnected.
622 GNUNET_TRANSPORT_TransmitFunction send;
625 * Function that can be used to force the plugin to disconnect from
626 * the given peer and cancel all previous transmissions (and their
629 GNUNET_TRANSPORT_DisconnectPeerFunction disconnect_peer;
632 * Function that can be used to force the plugin to disconnect from
633 * the given peer and cancel all previous transmissions (and their
636 GNUNET_TRANSPORT_DisconnectSessionFunction disconnect_session;
639 * Function that will be called whenever the transport service wants to
640 * notify the plugin that a session is still active and in use and
641 * therefore the session timeout for this session has to be updated
643 GNUNET_TRANSPORT_UpdateSessionTimeout update_session_timeout;
646 * Function that will be called whenever the transport service wants to
647 * notify the plugin that the inbound quota changed and that the plugin
648 * should update it's delay for the next receive value
650 GNUNET_TRANSPORT_UpdateInboundDelay update_inbound_delay;
653 * Function that is used to query keepalive factor.
654 * #GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT is divided by this number to
655 * calculate the interval between keepalive packets.
657 GNUNET_TRANSPORT_QueryKeepaliveFactorFunction query_keepalive_factor;
660 * Function to pretty-print addresses.
662 GNUNET_TRANSPORT_AddressPrettyPrinter address_pretty_printer;
665 * Function that will be called to check if a binary address
666 * for this plugin is well-formed and corresponds to an
667 * address for THIS peer (as per our configuration). Naturally,
668 * if absolutely necessary, plugins can be a bit conservative in
669 * their answer, but in general plugins should make sure that the
670 * address does not redirect traffic to a 3rd party that might
671 * try to man-in-the-middle our traffic.
673 GNUNET_TRANSPORT_CheckAddress check_address;
676 * Function that will be called to convert a binary address
677 * to a string (numeric conversion only).
679 GNUNET_TRANSPORT_AddressToString address_to_string;
682 * Function that will be called to convert a string address
683 * to binary (numeric conversion only).
685 GNUNET_TRANSPORT_StringToAddress string_to_address;
688 * Function that will be called tell the plugin to create a session
691 GNUNET_TRANSPORT_CreateSession get_session;
694 * Function to obtain the network type for a session
696 GNUNET_TRANSPORT_GetNetworkType get_network;
699 * Function to obtain the network type for an address
701 GNUNET_TRANSPORT_GetNetworkTypeForAddress get_network_for_address;
704 * Function to monitor the sessions managed by the plugin.
706 GNUNET_TRANSPORT_SessionMonitorSetup setup_monitor;
710 /*#ifndef PLUGIN_TRANSPORT_H*/
713 /** @} */ /* end of group */
715 /* end of gnunet_transport_plugin.h */