2 This file is part of GNUnet.
3 (C) 2009, 2010, 2011 Christian Grothoff (and other contributing authors)
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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file include/gnunet_transport_service.h
23 * @brief low-level P2P IO
24 * @author Christian Grothoff
27 #ifndef GNUNET_TRANSPORT_SERVICE_H
28 #define GNUNET_TRANSPORT_SERVICE_H
33 #if 0 /* keep Emacsens' auto-indent happy */
38 #include "gnunet_util_lib.h"
39 #include "gnunet_ats_service.h"
42 * Version number of the transport API.
44 #define GNUNET_TRANSPORT_VERSION 0x00000000
48 * Possible state of a neighbour. Initially, we are #S_NOT_CONNECTED.
50 * Then, there are two main paths. If we receive a CONNECT message, we
51 * first run a check against the blacklist (#S_CONNECT_RECV_BLACKLIST_INBOUND).
52 * If this check is successful, we give the inbound address to ATS.
53 * After the check we ask ATS for a suggestion (S_CONNECT_RECV_ATS).
54 * If ATS makes a suggestion, we ALSO give that suggestion to the blacklist
55 * (#S_CONNECT_RECV_BLACKLIST). Once the blacklist approves the
56 * address we got from ATS, we send our CONNECT_ACK and go to
57 * #S_CONNECT_RECV_ACK. If we receive a SESSION_ACK, we go to
58 * #S_CONNECTED (and notify everyone about the new connection). If the
59 * operation times out, we go to #S_DISCONNECT.
61 * The other case is where we transmit a CONNECT message first. We
62 * start with #S_INIT_ATS. If we get an address, we enter
63 * #S_INIT_BLACKLIST and check the blacklist. If the blacklist is OK
64 * with the connection, we actually send the CONNECT message and go to
65 * state S_CONNECT_SENT. Once we receive a CONNECT_ACK, we go to
66 * #S_CONNECTED (and notify everyone about the new connection and send
67 * back a SESSION_ACK). If the operation times out, we go to
70 * If the session is in trouble (i.e. transport-level disconnect or
71 * timeout), we go to #S_RECONNECT_ATS where we ask ATS for a new
72 * address (we don't notify anyone about the disconnect yet). Once we
73 * have a new address, we go to #S_RECONNECT_BLACKLIST to check the new
74 * address against the blacklist. If the blacklist approves, we enter
75 * #S_RECONNECT_SENT and send a CONNECT message. If we receive a
76 * CONNECT_ACK, we go to #S_CONNECTED and nobody noticed that we had
77 * trouble; we also send a SESSION_ACK at this time just in case. If
78 * the operation times out, we go to S_DISCONNECT (and notify everyone
79 * about the lost connection).
81 * If ATS decides to switch addresses while we have a normal
82 * connection, we go to #S_CONNECTED_SWITCHING_BLACKLIST to check the
83 * new address against the blacklist. If the blacklist approves, we
84 * go to #S_CONNECTED_SWITCHING_CONNECT_SENT and send a
85 * SESSION_CONNECT. If we get a SESSION_ACK back, we switch the
86 * primary connection to the suggested alternative from ATS, go back
87 * to #S_CONNECTED and send a SESSION_ACK to the other peer just to be
88 * sure. If the operation times out (or the blacklist disapproves),
89 * we go to #S_CONNECTED (and notify ATS that the given alternative
90 * address is "invalid").
92 * Once a session is in #S_DISCONNECT, it is cleaned up and then goes
93 * to (#S_DISCONNECT_FINISHED). If we receive an explicit disconnect
94 * request, we can go from any state to #S_DISCONNECT, possibly after
95 * generating disconnect notifications.
97 * Note that it is quite possible that while we are in any of these
98 * states, we could receive a 'CONNECT' request from the other peer.
99 * We then enter a 'weird' state where we pursue our own primary state
100 * machine (as described above), but with the 'send_connect_ack' flag
101 * set to 1. If our state machine allows us to send a 'CONNECT_ACK'
102 * (because we have an acceptable address), we send the 'CONNECT_ACK'
103 * and set the 'send_connect_ack' to 2. If we then receive a
104 * 'SESSION_ACK', we go to #S_CONNECTED (and reset 'send_connect_ack'
108 enum GNUNET_TRANSPORT_PeerState
111 * fresh peer or completely disconnected
116 * Asked to initiate connection, trying to get address from ATS
121 * Asked to initiate connection, trying to get address approved
127 * Sent CONNECT message to other peer, waiting for CONNECT_ACK
132 * Received a CONNECT, do a blacklist check for inbound address
134 S_CONNECT_RECV_BLACKLIST_INBOUND,
137 * Received a CONNECT, asking ATS about address suggestions.
142 * Received CONNECT from other peer, got an address, checking with blacklist.
144 S_CONNECT_RECV_BLACKLIST,
147 * CONNECT request from other peer was SESSION_ACK'ed, waiting for
153 * Got our CONNECT_ACK/SESSION_ACK, connection is up.
158 * Connection got into trouble, rest of the system still believes
159 * it to be up, but we're getting a new address from ATS.
164 * Connection got into trouble, rest of the system still believes
165 * it to be up; we are checking the new address against the blacklist.
167 S_RECONNECT_BLACKLIST,
170 * Sent CONNECT over new address (either by ATS telling us to switch
171 * addresses or from RECONNECT_ATS); if this fails, we need to tell
172 * the rest of the system about a disconnect.
177 * We have some primary connection, but ATS suggested we switch
178 * to some alternative; we're now checking the alternative against
181 S_CONNECTED_SWITCHING_BLACKLIST,
184 * We have some primary connection, but ATS suggested we switch
185 * to some alternative; we now sent a CONNECT message for the
186 * alternative session to the other peer and waiting for a
187 * CONNECT_ACK to make this our primary connection.
189 S_CONNECTED_SWITCHING_CONNECT_SENT,
192 * Disconnect in progress (we're sending the DISCONNECT message to the
193 * other peer; after that is finished, the state will be cleaned up).
198 * We're finished with the disconnect; and are cleaning up the state
199 * now! We put the struct into this state when we are really in the
200 * task that calls 'free' on it and are about to remove the record
201 * from the map. We should never find a 'struct NeighbourMapEntry'
202 * in this state in the map. Accessing a 'struct NeighbourMapEntry'
203 * in this state virtually always means using memory that has been
204 * freed (the exception being the cleanup code in #free_neighbour()).
206 S_DISCONNECT_FINISHED
212 * Function called by the transport for each received message.
215 * @param peer (claimed) identity of the other peer
216 * @param message the message
217 * @param ats performance data
218 * @param ats_count number of entries in ats
220 typedef void (*GNUNET_TRANSPORT_ReceiveCallback) (void *cls,
222 GNUNET_PeerIdentity * peer,
224 GNUNET_MessageHeader *
229 * Opaque handle to the service.
231 struct GNUNET_TRANSPORT_Handle;
235 * Function called to notify transport users that another
236 * peer connected to us.
239 * @param peer the peer that connected
240 * @param ats performance data
241 * @param ats_count number of entries in ats (excluding 0-termination)
243 typedef void (*GNUNET_TRANSPORT_NotifyConnect) (void *cls,
244 const struct GNUNET_PeerIdentity
248 * Function called to notify transport users that another
249 * peer disconnected from us.
252 * @param peer the peer that disconnected
254 typedef void (*GNUNET_TRANSPORT_NotifyDisconnect) (void *cls,
256 GNUNET_PeerIdentity * peer);
260 * Function to call with result of the try connect request.
264 * @param result GNUNET_OK if message was transmitted to transport service
265 * GNUNET_SYSERR if message was not transmitted to transport service
267 typedef void (*GNUNET_TRANSPORT_TryConnectCallback) (void *cls,
272 * Function to call with a textual representation of an address.
273 * This function will be called several times with different possible
274 * textual representations, and a last time with NULL to signal the end
278 * @param address NULL on error or end of iteration,
279 * otherwise 0-terminated printable UTF-8 string
281 typedef void (*GNUNET_TRANSPORT_AddressToStringCallback) (void *cls,
282 const char *address);
286 * Function to call with information about a peer
289 * @param peer peer this update is about,
290 * NULL if this is the final last callback for a iteration operation
291 * @param address address, NULL for disconnect notification in monitor mode
292 * @param state current state this peer is in
293 * @param state_timeout timeout for the current state of the peer
295 typedef void (*GNUNET_TRANSPORT_PeerIterateCallback) (void *cls,
296 const struct GNUNET_PeerIdentity *peer,
297 const struct GNUNET_HELLO_Address *address,
298 enum GNUNET_TRANSPORT_PeerState state,
299 struct GNUNET_TIME_Absolute state_timeout);
303 * Function to call with validation information about a peer
306 * @param peer peer this update is about,
307 * NULL if this is the final last callback for a iteration operation
308 * @param address address, NULL for disconnect notification in monitor mode
309 * @param valid_until when does this address expire
310 * @param next_validation time of the next validation operation
313 typedef void (*GNUNET_TRANSPORT_ValidationIterateCallback) (void *cls,
314 const struct GNUNET_PeerIdentity *peer,
315 const struct GNUNET_HELLO_Address *address,
316 struct GNUNET_TIME_Absolute valid_until,
317 struct GNUNET_TIME_Absolute next_validation);
322 * Connect to the transport service. Note that the connection may
323 * complete (or fail) asynchronously.
325 * @param cfg configuration to use
326 * @param self our own identity (API should check that it matches
327 * the identity found by transport), or NULL (no check)
328 * @param cls closure for the callbacks
329 * @param rec receive function to call, or NULL
330 * @param nc function to call on connect events, or NULL
331 * @param nd function to call on disconnect events, or NULL
332 * @return NULL on error
334 struct GNUNET_TRANSPORT_Handle *
335 GNUNET_TRANSPORT_connect (const struct GNUNET_CONFIGURATION_Handle *cfg,
336 const struct GNUNET_PeerIdentity *self, void *cls,
337 GNUNET_TRANSPORT_ReceiveCallback rec,
338 GNUNET_TRANSPORT_NotifyConnect nc,
339 GNUNET_TRANSPORT_NotifyDisconnect nd);
343 * Disconnect from the transport service.
345 * @param handle handle returned from connect
348 GNUNET_TRANSPORT_disconnect (struct GNUNET_TRANSPORT_Handle *handle);
352 * Opaque handle for a transmission-ready request.
354 struct GNUNET_TRANSPORT_TryConnectHandle;
358 * Ask the transport service to establish a connection to
361 * @param handle connection to transport service
362 * @param target who we should try to connect to
363 * @param cb callback to be called when request was transmitted to transport
365 * @param cb_cls closure for the callback
366 * @return a GNUNET_TRANSPORT_TryConnectHandle handle or
367 * NULL on failure (cb will not be called)
369 struct GNUNET_TRANSPORT_TryConnectHandle *
370 GNUNET_TRANSPORT_try_connect (struct GNUNET_TRANSPORT_Handle *handle,
371 const struct GNUNET_PeerIdentity *target,
372 GNUNET_TRANSPORT_TryConnectCallback cb,
377 * Cancel the request to transport to try a connect
378 * Callback will not be called
380 * @param tch GNUNET_TRANSPORT_TryConnectHandle handle to cancel
383 GNUNET_TRANSPORT_try_connect_cancel (struct GNUNET_TRANSPORT_TryConnectHandle *tch);
387 * Opaque handle for a transmission-ready request.
389 struct GNUNET_TRANSPORT_TransmitHandle;
393 * Check if we could queue a message of the given size for
394 * transmission. The transport service will take both its internal
395 * buffers and bandwidth limits imposed by the other peer into
396 * consideration when answering this query.
398 * @param handle connection to transport service
399 * @param target who should receive the message
400 * @param size how big is the message we want to transmit?
401 * @param priority how important is the message? @deprecated - remove?
402 * @param timeout after how long should we give up (and call
403 * notify with buf NULL and size 0)?
404 * @param notify function to call when we are ready to
405 * send such a message
406 * @param notify_cls closure for notify
407 * @return NULL if someone else is already waiting to be notified
408 * non-NULL if the notify callback was queued (can be used to cancel
409 * using GNUNET_TRANSPORT_notify_transmit_ready_cancel)
411 struct GNUNET_TRANSPORT_TransmitHandle *
412 GNUNET_TRANSPORT_notify_transmit_ready (struct GNUNET_TRANSPORT_Handle *handle,
413 const struct GNUNET_PeerIdentity
414 *target, size_t size, uint32_t priority,
415 struct GNUNET_TIME_Relative timeout,
416 GNUNET_CONNECTION_TransmitReadyNotify
417 notify, void *notify_cls);
421 * Cancel the specified transmission-ready notification.
423 * @param th handle of the transmission notification request to cancel
426 GNUNET_TRANSPORT_notify_transmit_ready_cancel (struct
427 GNUNET_TRANSPORT_TransmitHandle
433 * Function called whenever there is an update to the
434 * HELLO of this peer.
437 * @param hello our updated HELLO
439 typedef void (*GNUNET_TRANSPORT_HelloUpdateCallback) (void *cls,
441 GNUNET_MessageHeader *
446 * Handle to cancel a 'GNUNET_TRANSPORT_get_hello' operation.
448 struct GNUNET_TRANSPORT_GetHelloHandle;
452 * Checks if a given peer is connected to us
454 * @param handle connection to transport service
455 * @param peer the peer to check
456 * @return #GNUNET_YES (connected) or #GNUNET_NO (disconnected)
459 GNUNET_TRANSPORT_check_peer_connected (struct GNUNET_TRANSPORT_Handle *handle,
460 const struct GNUNET_PeerIdentity *peer);
464 * Set transport metrics for a peer and a direction
466 * @param handle transport handle
467 * @param peer the peer to set the metric for
468 * @param inbound set inbound direction (GNUNET_YES or GNUNET_NO)
469 * @param outbound set outbound direction (GNUNET_YES or GNUNET_NO)
470 * @param ats the metric as ATS information
471 * @param ats_count the number of metrics
473 * Supported ATS values:
474 * GNUNET_ATS_QUALITY_NET_DELAY (value in ms)
475 * GNUNET_ATS_QUALITY_NET_DISTANCE (value in count(hops))
478 * To enforce a delay of 10 ms for peer p1 in sending direction use:
480 * struct GNUNET_ATS_Information ats;
481 * ats.type = ntohl (GNUNET_ATS_QUALITY_NET_DELAY);
482 * ats.value = ntohl (10);
483 * GNUNET_TRANSPORT_set_traffic_metric (th, p1, TM_SEND, &ats, 1);
486 * Delay restrictions in receiving direction will be enforced with
490 GNUNET_TRANSPORT_set_traffic_metric (struct GNUNET_TRANSPORT_Handle *handle,
491 const struct GNUNET_PeerIdentity *peer,
494 const struct GNUNET_ATS_Information *ats,
499 * Obtain updates on changes to the HELLO message for this peer. The callback
500 * given in this function is never called synchronously.
502 * @param handle connection to transport service
503 * @param rec function to call with the HELLO
504 * @param rec_cls closure for rec
505 * @return handle to cancel the operation
507 struct GNUNET_TRANSPORT_GetHelloHandle *
508 GNUNET_TRANSPORT_get_hello (struct GNUNET_TRANSPORT_Handle *handle,
509 GNUNET_TRANSPORT_HelloUpdateCallback rec,
514 * Stop receiving updates about changes to our HELLO message.
516 * @param ghh handle to cancel
519 GNUNET_TRANSPORT_get_hello_cancel (struct GNUNET_TRANSPORT_GetHelloHandle *ghh);
523 * Offer the transport service the HELLO of another peer. Note that
524 * the transport service may just ignore this message if the HELLO is
525 * malformed or useless due to our local configuration.
527 * @param handle connection to transport service
528 * @param hello the hello message
529 * @param cont continuation to call when HELLO has been sent,
530 * tc reason GNUNET_SCHEDULER_REASON_TIMEOUT for fail
531 * tc reasong GNUNET_SCHEDULER_REASON_READ_READY for success
532 * @param cls closure for continuation
533 * @return a GNUNET_TRANSPORT_OfferHelloHandle handle or NULL on failure,
534 * in case of failure cont will not be called
537 struct GNUNET_TRANSPORT_OfferHelloHandle *
538 GNUNET_TRANSPORT_offer_hello (struct GNUNET_TRANSPORT_Handle *handle,
539 const struct GNUNET_MessageHeader *hello,
540 GNUNET_SCHEDULER_Task cont, void *cls);
544 * Cancel the request to transport to offer the HELLO message
546 * @param ohh the GNUNET_TRANSPORT_OfferHelloHandle to cancel
549 GNUNET_TRANSPORT_offer_hello_cancel (struct GNUNET_TRANSPORT_OfferHelloHandle *ohh);
553 * Handle to cancel a pending address lookup.
555 struct GNUNET_TRANSPORT_AddressToStringContext;
559 * Convert a binary address into a human readable address.
561 * @param cfg configuration to use
562 * @param address address to convert (binary format)
563 * @param numeric should (IP) addresses be displayed in numeric form
564 * (otherwise do reverse DNS lookup)
565 * @param timeout how long is the lookup allowed to take at most
566 * @param aluc function to call with the results
567 * @param aluc_cls closure for aluc
568 * @return handle to cancel the operation, NULL on error
570 struct GNUNET_TRANSPORT_AddressToStringContext *
571 GNUNET_TRANSPORT_address_to_string (const struct GNUNET_CONFIGURATION_Handle
573 const struct GNUNET_HELLO_Address *address,
575 struct GNUNET_TIME_Relative timeout,
576 GNUNET_TRANSPORT_AddressToStringCallback
577 aluc, void *aluc_cls);
581 * Cancel request for address conversion.
583 * @param alc handle for the request to cancel
586 GNUNET_TRANSPORT_address_to_string_cancel (struct
587 GNUNET_TRANSPORT_AddressToStringContext
592 * Return information about a specific peer or all peers currently known to
593 * transport service once or in monitoring mode. To obtain information about
594 * a specific peer, a peer identity can be passed. To obtain information about
595 * all peers currently known to transport service, NULL can be passed as peer
598 * For each peer, the callback is called with information about the address used
599 * to communicate with this peer, the state this peer is currently in and the
600 * the current timeout for this state.
602 * Upon completion, the 'GNUNET_TRANSPORT_PeerIterateCallback' is called one
603 * more time with 'NULL'. After this, the operation must no longer be
604 * explicitly canceled.
606 * @param cfg configuration to use
607 * @param peer a specific peer identity to obtain information for,
609 * @param one_shot GNUNET_YES to return the current state and then end (with NULL+NULL),
610 * GNUNET_NO to monitor peers continuously
611 * @param timeout how long is the lookup allowed to take at most
612 * @param peer_address_callback function to call with the results
613 * @param peer_address_callback_cls closure for peer_address_callback
615 struct GNUNET_TRANSPORT_PeerMonitoringContext *
616 GNUNET_TRANSPORT_monitor_peers (const struct
617 GNUNET_CONFIGURATION_Handle *cfg,
618 const struct GNUNET_PeerIdentity *peer,
620 struct GNUNET_TIME_Relative timeout,
621 GNUNET_TRANSPORT_PeerIterateCallback peer_callback,
622 void *peer_callback_cls);
626 * Cancel request to monitor peers
628 * @param pic handle for the request to cancel
631 GNUNET_TRANSPORT_monitor_peers_cancel (struct GNUNET_TRANSPORT_PeerMonitoringContext *pic);
636 * Return information about pending address validation operations for a specific
639 * @param cfg configuration to use
640 * @param peer a specific peer identity to obtain validation entries for,
642 * @param one_shot GNUNET_YES to return all entries and then end (with NULL+NULL),
643 * GNUNET_NO to monitor validation entries continuously
644 * @param timeout how long is the lookup allowed to take at most
645 * @param validation_callback function to call with the results
646 * @param validation_callback_cls closure for peer_address_callback
648 struct GNUNET_TRANSPORT_ValidationMonitoringContext *
649 GNUNET_TRANSPORT_monitor_validation_entries (const struct
650 GNUNET_CONFIGURATION_Handle *cfg,
651 const struct GNUNET_PeerIdentity *peer,
653 struct GNUNET_TIME_Relative timeout,
654 GNUNET_TRANSPORT_ValidationIterateCallback validation_callback,
655 void *validation_callback_cls);
659 * Return information about all current pending validation operations
661 * @param vic handle for the request to cancel
664 GNUNET_TRANSPORT_monitor_validation_entries_cancel (struct GNUNET_TRANSPORT_ValidationMonitoringContext *vic);
669 * Handle for blacklisting peers.
671 struct GNUNET_TRANSPORT_Blacklist;
675 * Function that decides if a connection is acceptable or not.
678 * @param pid peer to approve or disapproave
679 * @return GNUNET_OK if the connection is allowed, GNUNET_SYSERR if not
681 typedef int (*GNUNET_TRANSPORT_BlacklistCallback) (void *cls,
683 GNUNET_PeerIdentity * pid);
687 * Install a blacklist callback. The service will be queried for all
688 * existing connections as well as any fresh connections to check if
689 * they are permitted. If the blacklisting callback is unregistered,
690 * all hosts that were denied in the past will automatically be
691 * whitelisted again. Cancelling the blacklist handle is also the
692 * only way to re-enable connections from peers that were previously
695 * @param cfg configuration to use
696 * @param cb callback to invoke to check if connections are allowed
697 * @param cb_cls closure for cb
698 * @return NULL on error, otherwise handle for cancellation
700 struct GNUNET_TRANSPORT_Blacklist *
701 GNUNET_TRANSPORT_blacklist (const struct GNUNET_CONFIGURATION_Handle *cfg,
702 GNUNET_TRANSPORT_BlacklistCallback cb,
707 * Abort the blacklist. Note that this function is the only way for
708 * removing a peer from the blacklist.
710 * @param br handle of the request that is to be cancelled
713 GNUNET_TRANSPORT_blacklist_cancel (struct GNUNET_TRANSPORT_Blacklist *br);
717 #if 0 /* keep Emacsens' auto-indent happy */
724 /* ifndef GNUNET_TRANSPORT_SERVICE_H */
726 /* end of gnunet_transport_service.h */