2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 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 * @file transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_ats.h"
29 #include "gnunet-service-transport_blacklist.h"
30 #include "gnunet-service-transport_clients.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport_manipulation.h"
33 #include "gnunet-service-transport_plugins.h"
34 #include "gnunet-service-transport_validation.h"
35 #include "gnunet-service-transport.h"
36 #include "gnunet_peerinfo_service.h"
37 #include "gnunet_constants.h"
38 #include "transport.h"
41 * Experimental option to ignore SessionQuotaMessages from
44 #define IGNORE_INBOUND_QUOTA GNUNET_YES
47 * Size of the neighbour hash map.
49 #define NEIGHBOUR_TABLE_SIZE 256
52 * Time we give plugin to transmit DISCONNECT message before the
53 * neighbour entry self-destructs.
55 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
58 * How often must a peer violate bandwidth quotas before we start
59 * to simply drop its messages?
61 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
64 * How long are we willing to wait for a response from ATS before timing out?
66 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
69 * How long are we willing to wait for an ACK from the other peer before
70 * giving up on our connect operation?
72 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
75 * How long are we willing to wait for a successful reconnect if
76 * an existing connection went down? Much shorter than the
77 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
78 * higher layers about the disconnect during this period.
80 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
83 * Interval to send utilization data
85 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_UNIT_SECONDS
88 * State describing which kind a reply this neighbour should send
93 * We did not receive a SYN message for this neighbour
98 * The neighbour received a SYN message and has to send a SYN_ACK
101 ACK_SEND_SYN_ACK = 1,
104 * The neighbour sent a SYN_ACK message and has to send a ACK
111 GNUNET_NETWORK_STRUCT_BEGIN
114 * Message a peer sends to another to indicate that it intends to
115 * setup a connection/session for data exchange. A 'SESSION_SYN'
116 * should be answered with a 'SESSION_SYN_ACK' with the same body
117 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
118 * a 'ACK'. Once the 'ACK' is received, both peers
119 * should be connected.
121 struct TransportSynMessage
124 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
125 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
127 struct GNUNET_MessageHeader header;
132 uint32_t reserved GNUNET_PACKED;
135 * Absolute time at the sender. Only the most recent connect
136 * message implies which session is preferred by the sender.
138 struct GNUNET_TIME_AbsoluteNBO timestamp;
144 * Message a peer sends to another when connected to indicate that a
145 * session is in use and the peer is still alive or to respond to a keep alive.
146 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
147 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
148 * When the keep alive response with type is received, transport service
149 * will call the respective plugin to update the session timeout
151 struct GNUNET_ATS_SessionKeepAliveMessage
154 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
155 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
157 struct GNUNET_MessageHeader header;
160 * A nonce to identify the session the keep alive is used for
162 uint32_t nonce GNUNET_PACKED;
167 * Message a peer sends to another when connected to indicate that
168 * the other peer should limit transmissions to the indicated
171 struct GNUNET_ATS_SessionQuotaMessage
174 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA.
176 struct GNUNET_MessageHeader header;
179 * Quota to use (for sending), in bytes per second.
181 uint32_t quota GNUNET_PACKED;
186 * Message we send to the other peer to notify him that we intentionally
187 * are disconnecting (to reduce timeouts). This is just a friendly
188 * notification, peers must not rely on always receiving disconnect
191 struct GNUNET_ATS_SessionDisconnectMessage
194 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
196 struct GNUNET_MessageHeader header;
201 uint32_t reserved GNUNET_PACKED;
204 * Purpose of the signature. Extends over the timestamp.
205 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
207 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
210 * Absolute time at the sender. Only the most recent connect
211 * message implies which session is preferred by the sender.
213 struct GNUNET_TIME_AbsoluteNBO timestamp;
216 * Public key of the sender.
218 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
221 * Signature of the peer that sends us the disconnect. Only
222 * valid if the timestamp is AFTER the timestamp from the
223 * corresponding 'SYN' message.
225 struct GNUNET_CRYPTO_EddsaSignature signature;
229 GNUNET_NETWORK_STRUCT_END
233 * For each neighbour we keep a list of messages
234 * that we still want to transmit to the neighbour.
240 * This is a doubly linked list.
242 struct MessageQueue *next;
245 * This is a doubly linked list.
247 struct MessageQueue *prev;
250 * Function to call once we're done.
252 GST_NeighbourSendContinuation cont;
255 * Closure for @e cont
260 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
261 * stuck together in memory. Allocated at the end of this struct.
263 const char *message_buf;
266 * Size of the message buf
268 size_t message_buf_size;
271 * At what time should we fail?
273 struct GNUNET_TIME_Absolute timeout;
279 * A possible address we could use to communicate with a neighbour.
281 struct NeighbourAddress
285 * Active session for this address.
287 struct GNUNET_ATS_Session *session;
290 * Network-level address information.
292 struct GNUNET_HELLO_Address *address;
295 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
296 * peer for this address. Use to check that the ACK is in response
297 * to our most recent 'SYN'.
299 struct GNUNET_TIME_Absolute connect_timestamp;
302 * Inbound bandwidth from ATS for this address.
304 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
307 * Outbound bandwidth from ATS for this address.
309 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
312 * Did we tell ATS that this is our 'active' address?
317 * The current nonce sent in the last keep alive messages
319 uint32_t keep_alive_nonce;
324 * Entry in neighbours.
326 struct NeighbourMapEntry
330 * Head of list of messages we would like to send to this peer;
331 * must contain at most one message per client.
333 struct MessageQueue *messages_head;
336 * Tail of list of messages we would like to send to this peer; must
337 * contain at most one message per client.
339 struct MessageQueue *messages_tail;
342 * Are we currently trying to send a message? If so, which one?
344 struct MessageQueue *is_active;
347 * Primary address we currently use to communicate with the neighbour.
349 struct NeighbourAddress primary_address;
352 * Alternative address currently under consideration for communicating
353 * with the neighbour.
355 struct NeighbourAddress alternative_address;
358 * Identity of this neighbour.
360 struct GNUNET_PeerIdentity id;
363 * Main task that drives this peer (timeouts, keepalives, etc.).
364 * Always runs the #master_task().
366 struct GNUNET_SCHEDULER_Task *task;
369 * Task to disconnect neighbour after we received a DISCONNECT message
371 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
374 * At what time should we sent the next keep-alive message?
376 struct GNUNET_TIME_Absolute keep_alive_time;
379 * At what time did we sent the last keep-alive message? Used
380 * to calculate round-trip time ("latency").
382 struct GNUNET_TIME_Absolute last_keep_alive_time;
385 * Timestamp we should include in our next SYN_ACK message.
386 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
387 * our SYN_ACK message.
389 struct GNUNET_TIME_Absolute connect_ack_timestamp;
392 * ATS address suggest handle
394 struct GNUNET_ATS_ConnectivitySuggestHandle *suggest_handle;
397 * Time where we should cut the connection (timeout) if we don't
398 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
399 * if we are in #GNUNET_TRANSPORT_PS_CONNECTED).
401 struct GNUNET_TIME_Absolute timeout;
404 * Tracker for inbound bandwidth.
406 struct GNUNET_BANDWIDTH_Tracker in_tracker;
409 * How often has the other peer (recently) violated the inbound
410 * traffic limit? Incremented by 10 per violation, decremented by 1
411 * per non-violation (for each time interval).
413 unsigned int quota_violation_count;
416 * Latest quota the other peer send us in bytes per second.
417 * We should not send more, least the other peer throttle
418 * receiving our traffic.
420 struct GNUNET_BANDWIDTH_Value32NBO neighbour_receive_quota;
423 * The current state of the peer.
425 enum GNUNET_TRANSPORT_PeerState state;
428 * Did we sent an KEEP_ALIVE message and are we expecting a response?
430 int expect_latency_response;
433 * When a peer wants to connect we have to reply to the 1st SYN message
434 * with a SYN_ACK message. But sometime we cannot send this message
435 * immediately since we do not have an address and then we have to remember
436 * to send this message as soon as we have an address.
438 * Flag to set if we still need to send a SYN_ACK message to the other peer
439 * (once we have an address to use and the peer has been allowed by our
440 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
441 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
442 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
443 * 'ACK' (regardless of what our own state machine might say).
445 enum GST_ACK_State ack_state;
448 * Tracking utilization of outbound bandwidth
450 uint32_t util_total_bytes_sent;
453 * Tracking utilization of inbound bandwidth
455 uint32_t util_total_bytes_recv;
458 * Date of last utilization transmission
460 struct GNUNET_TIME_Absolute last_util_transmission;
465 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
467 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
470 * List of pending blacklist checks: head
472 static struct BlacklistCheckSwitchContext *pending_bc_head;
475 * List of pending blacklist checks: tail
477 static struct BlacklistCheckSwitchContext *pending_bc_tail;
480 * counter for connected neighbours
482 static unsigned int neighbours_connected;
485 * Number of bytes we have currently queued for transmission.
487 static unsigned long long bytes_in_send_queue;
490 * Task transmitting utilization data
492 static struct GNUNET_SCHEDULER_Task *util_transmission_tk;
496 * Convert the given ACK state to a string.
499 * @return corresponding human-readable string
502 print_ack_state (enum GST_ACK_State s)
507 case ACK_SEND_SYN_ACK:
508 return "SEND_SYN_ACK";
519 * Send information about a new outbound quota to our clients.
520 * Note that the outbound quota is enforced client-side (i.e.
521 * in libgnunettransport).
523 * @param n affected peer
526 send_outbound_quota_to_clients (struct NeighbourMapEntry *n)
528 struct QuotaSetMessage q_msg;
529 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
531 if (! GNUNET_TRANSPORT_is_connected (n->state))
533 #if IGNORE_INBOUND_QUOTA
534 bandwidth_min = n->primary_address.bandwidth_out;
536 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
537 n->neighbour_receive_quota);
540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
541 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
542 ntohl (bandwidth_min.value__),
543 GNUNET_i2s (&n->id));
544 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
545 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
546 q_msg.quota = bandwidth_min;
548 GST_clients_broadcast (&q_msg.header,
554 * Notify our clients that another peer connected to us.
556 * @param n the peer that connected
559 neighbours_connect_notification (struct NeighbourMapEntry *n)
561 size_t len = sizeof(struct ConnectInfoMessage);
562 char buf[len] GNUNET_ALIGN;
563 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
564 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
566 #if IGNORE_INBOUND_QUOTA
567 bandwidth_min = n->primary_address.bandwidth_out;
569 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
570 n->neighbour_receive_quota);
572 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
573 "We are now connected to peer `%s'\n",
574 GNUNET_i2s (&n->id));
575 connect_msg->header.size = htons (sizeof(buf));
576 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
577 connect_msg->id = n->id;
578 connect_msg->quota_in = n->primary_address.bandwidth_in;
579 connect_msg->quota_out = bandwidth_min;
580 GST_clients_broadcast (&connect_msg->header,
586 * Notify our clients (and manipulation) that a peer disconnected from
589 * @param n the peer that disconnected
592 neighbours_disconnect_notification (struct NeighbourMapEntry *n)
594 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
595 "Peer `%s' disconnected\n",
596 GNUNET_i2s (&n->id));
597 GST_manipulation_peer_disconnect (&n->id);
598 GST_clients_broadcast_disconnect (&n->id);
603 * Notify transport clients that a neighbour peer changed its active
606 * @param peer identity of the peer
607 * @param address address possibly NULL if peer is not connected
608 * @param state current state this peer is in
609 * @param state_timeout timeout for the current state of the peer
610 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
611 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
614 neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer,
615 const struct GNUNET_HELLO_Address *address,
616 enum GNUNET_TRANSPORT_PeerState state,
617 struct GNUNET_TIME_Absolute state_timeout,
618 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
619 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
624 GST_plugins_a2s (address),
625 GNUNET_TRANSPORT_ps2s (state),
626 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
627 /* FIXME: include bandwidth in notification! */
628 GST_clients_broadcast_peer_notification (peer,
636 * Lookup a neighbour entry in the neighbours hash map.
638 * @param pid identity of the peer to look up
639 * @return the entry, NULL if there is no existing record
641 static struct NeighbourMapEntry *
642 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
644 if (NULL == neighbours)
646 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
651 * Test if we're connected to the given peer.
653 * @param n neighbour entry of peer to test
654 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
657 test_connected (struct NeighbourMapEntry *n)
661 return GNUNET_TRANSPORT_is_connected (n->state);
666 * We don't need a given neighbour address any more.
667 * Release its resources and give appropriate notifications
668 * to ATS and other subsystems.
670 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
673 free_address (struct NeighbourAddress *na)
675 if (GNUNET_YES == na->ats_active)
676 GST_validation_set_address_use (na->address,
678 if (NULL != na->address)
680 GST_ats_block_address (na->address,
682 GNUNET_HELLO_address_free (na->address);
685 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
686 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
687 na->ats_active = GNUNET_NO;
688 na->keep_alive_nonce = 0;
694 * Master task run for every neighbour. Performs all of the time-related
695 * activities (keep alive, send next message, disconnect if idle, finish
696 * clean up after disconnect).
698 * @param cls the `struct NeighbourMapEntry` for which we are running
701 master_task (void *cls);
705 * Set net state and state timeout for this neighbour and notify monitoring
707 * @param n the respective neighbour
708 * @param s the new state
709 * @param timeout the new timeout
712 set_state_and_timeout (struct NeighbourMapEntry *n,
713 enum GNUNET_TRANSPORT_PeerState s,
714 struct GNUNET_TIME_Absolute timeout)
716 if (GNUNET_TRANSPORT_is_connected (s) &&
717 ! GNUNET_TRANSPORT_is_connected (n->state) )
719 neighbours_connect_notification (n);
720 GNUNET_STATISTICS_set (GST_stats,
721 gettext_noop ("# peers connected"),
722 ++neighbours_connected,
725 if (! GNUNET_TRANSPORT_is_connected (s) &&
726 GNUNET_TRANSPORT_is_connected (n->state) )
728 GNUNET_STATISTICS_set (GST_stats,
729 gettext_noop ("# peers connected"),
730 --neighbours_connected,
732 neighbours_disconnect_notification (n);
735 if ( (timeout.abs_value_us < n->timeout.abs_value_us) &&
738 /* new timeout is earlier, reschedule master task */
739 GNUNET_SCHEDULER_cancel (n->task);
740 n->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (timeout),
744 n->timeout = timeout;
745 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
746 "Neighbour `%s' changed state to %s with timeout %s\n",
748 GNUNET_TRANSPORT_ps2s(s),
749 GNUNET_STRINGS_absolute_time_to_string (timeout));
750 neighbours_changed_notification (&n->id,
751 n->primary_address.address,
754 n->primary_address.bandwidth_in,
755 n->primary_address.bandwidth_out);
760 * Initialize the alternative address of a neighbour
762 * @param n the neighbour
763 * @param address address of the other peer, NULL if other peer
765 * @param session session to use (or NULL, in which case an
766 * address must be setup)
767 * @param bandwidth_in inbound quota to be used when connection is up
768 * @param bandwidth_out outbound quota to be used when connection is up
771 set_alternative_address (struct NeighbourMapEntry *n,
772 const struct GNUNET_HELLO_Address *address,
773 struct GNUNET_ATS_Session *session,
774 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
775 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
777 struct GNUNET_TRANSPORT_PluginFunctions *papi;
779 if (NULL == (papi = GST_plugins_find (address->transport_name)))
784 if (session == n->alternative_address.session)
786 n->alternative_address.bandwidth_in = bandwidth_in;
787 n->alternative_address.bandwidth_out = bandwidth_out;
790 if (NULL != n->alternative_address.address)
792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
793 "Replacing existing alternative address with another one\n");
794 free_address (&n->alternative_address);
797 session = papi->get_session (papi->cls,
801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
802 "Failed to obtain new session for peer `%s' and address '%s'\n",
803 GNUNET_i2s (&address->peer),
804 GST_plugins_a2s (address));
805 GNUNET_STATISTICS_update (GST_stats,
806 gettext_noop ("# session creation failed"),
811 GST_ats_new_session (address,
813 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
814 "Neighbour `%s' configured alternative address %s\n",
816 GST_plugins_a2s(address));
818 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
819 n->alternative_address.bandwidth_in = bandwidth_in;
820 n->alternative_address.bandwidth_out = bandwidth_out;
821 n->alternative_address.session = session;
822 n->alternative_address.ats_active = GNUNET_NO;
823 n->alternative_address.keep_alive_nonce = 0;
824 GNUNET_assert (GNUNET_YES ==
825 GST_ats_is_known (n->alternative_address.address,
826 n->alternative_address.session));
831 * Transmit a message using the current session of the given
834 * @param n entry for the recipient
835 * @param msgbuf buffer to transmit
836 * @param msgbuf_size number of bytes in @a msgbuf buffer
837 * @param priority transmission priority
838 * @param timeout transmission timeout
839 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
840 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
841 * @param cont continuation to call when finished (can be NULL)
842 * @param cont_cls closure for @a cont
843 * @return timeout (copy of @a timeout or a calculated one if
844 * @a use_keepalive_timeout is #GNUNET_YES.
846 static struct GNUNET_TIME_Relative
847 send_with_session (struct NeighbourMapEntry *n,
851 struct GNUNET_TIME_Relative timeout,
852 unsigned int use_keepalive_timeout,
853 GNUNET_TRANSPORT_TransmitContinuation cont,
856 struct GNUNET_TRANSPORT_PluginFunctions *papi;
857 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
859 GNUNET_assert (NULL != n->primary_address.session);
860 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
861 (-1 == papi->send (papi->cls,
862 n->primary_address.session,
866 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
867 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
868 papi->query_keepalive_factor (papi->cls))),
877 GST_neighbours_notify_data_sent (n->primary_address.address,
878 n->primary_address.session,
880 GNUNET_break (NULL != papi);
886 * Clear the primary address of a neighbour since this address is not
887 * valid anymore and notify monitoring about it
889 * @param n the neighbour
892 unset_primary_address (struct NeighbourMapEntry *n)
894 /* Notify monitoring about change */
895 if (NULL == n->primary_address.address)
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Disabling primary address\n");
899 neighbours_changed_notification (&n->id,
900 n->primary_address.address,
903 GNUNET_BANDWIDTH_value_init (0),
904 GNUNET_BANDWIDTH_value_init (0));
905 free_address (&n->primary_address);
910 * Free a neighbour map entry.
912 * @param n entry to free
915 free_neighbour (struct NeighbourMapEntry *n)
917 struct MessageQueue *mq;
919 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
920 "Freeing neighbour state of peer `%s'\n",
921 GNUNET_i2s (&n->id));
922 n->is_active = NULL; /* always free'd by its own continuation! */
924 /* fail messages currently in the queue */
925 while (NULL != (mq = n->messages_head))
927 GNUNET_CONTAINER_DLL_remove (n->messages_head,
930 if (NULL != mq->cont)
931 mq->cont (mq->cont_cls,
933 mq->message_buf_size,
937 /* Mark peer as disconnected */
938 set_state_and_timeout (n,
939 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED,
940 GNUNET_TIME_UNIT_FOREVER_ABS);
941 /* free addresses and mark as unused */
942 unset_primary_address (n);
944 if (NULL != n->alternative_address.address)
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "Cleaning up alternative address\n");
948 free_address (&n->alternative_address);
950 GNUNET_assert (GNUNET_YES ==
951 GNUNET_CONTAINER_multipeermap_remove (neighbours,
954 /* Cancel address requests for this peer */
955 if (NULL != n->suggest_handle)
957 GNUNET_ATS_connectivity_suggest_cancel (n->suggest_handle);
958 n->suggest_handle = NULL;
961 /* Cancel the disconnect task */
962 if (NULL != n->delayed_disconnect_task)
964 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
965 n->delayed_disconnect_task = NULL;
968 /* Cancel the master task */
971 GNUNET_SCHEDULER_cancel (n->task);
974 /* free rest of memory */
980 * Function called when the 'DISCONNECT' message has been sent by the
981 * plugin. Frees the neighbour --- if the entry still exists.
984 * @param target identity of the neighbour that was disconnected
985 * @param result #GNUNET_OK if the disconnect got out successfully
986 * @param payload bytes payload
987 * @param physical bytes on wire
990 send_disconnect_cont (void *cls,
991 const struct GNUNET_PeerIdentity *target,
996 struct NeighbourMapEntry *n;
998 n = lookup_neighbour (target);
1000 return; /* already gone */
1001 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1002 return; /* have created a fresh entry since */
1003 if (NULL != n->task)
1004 GNUNET_SCHEDULER_cancel (n->task);
1005 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1010 * Transmit a DISCONNECT message to the other peer.
1012 * @param n neighbour to send DISCONNECT message.
1015 send_disconnect (struct NeighbourMapEntry *n)
1017 struct GNUNET_ATS_SessionDisconnectMessage disconnect_msg;
1019 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1020 "Sending DISCONNECT message to peer `%4s'\n",
1021 GNUNET_i2s (&n->id));
1022 disconnect_msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionDisconnectMessage));
1023 disconnect_msg.header.type =
1024 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1025 disconnect_msg.reserved = htonl (0);
1026 disconnect_msg.purpose.size =
1027 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1028 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1029 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1030 disconnect_msg.purpose.purpose =
1031 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1032 disconnect_msg.timestamp =
1033 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1034 disconnect_msg.public_key = GST_my_identity.public_key;
1035 GNUNET_assert (GNUNET_OK ==
1036 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1037 &disconnect_msg.purpose,
1038 &disconnect_msg.signature));
1040 (void) send_with_session (n,
1042 sizeof (disconnect_msg),
1044 GNUNET_TIME_UNIT_FOREVER_REL,
1046 &send_disconnect_cont,
1048 GNUNET_STATISTICS_update (GST_stats,
1049 gettext_noop ("# DISCONNECT messages sent"),
1056 * Disconnect from the given neighbour, clean up the record.
1058 * @param n neighbour to disconnect from
1061 disconnect_neighbour (struct NeighbourMapEntry *n)
1063 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1064 "Disconnecting from peer %s in state %s\n",
1065 GNUNET_i2s (&n->id),
1066 GNUNET_TRANSPORT_ps2s (n->state));
1067 /* depending on state, notify neighbour and/or upper layers of this peer
1071 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1072 case GNUNET_TRANSPORT_PS_INIT_ATS:
1073 /* other peer is completely unaware of us, no need to send DISCONNECT */
1076 case GNUNET_TRANSPORT_PS_SYN_SENT:
1077 send_disconnect (n);
1078 set_state_and_timeout (n,
1079 GNUNET_TRANSPORT_PS_DISCONNECT,
1080 GNUNET_TIME_UNIT_FOREVER_ABS);
1082 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1083 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1086 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1087 /* we DID ACK the other peer's request, must send DISCONNECT */
1088 send_disconnect (n);
1089 set_state_and_timeout (n,
1090 GNUNET_TRANSPORT_PS_DISCONNECT,
1091 GNUNET_TIME_UNIT_FOREVER_ABS);
1093 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1094 case GNUNET_TRANSPORT_PS_CONNECTED:
1095 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1096 /* we are currently connected, need to send disconnect and do
1097 internal notifications and update statistics */
1098 send_disconnect (n);
1099 set_state_and_timeout (n,
1100 GNUNET_TRANSPORT_PS_DISCONNECT,
1101 GNUNET_TIME_UNIT_FOREVER_ABS);
1103 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1104 /* Disconnecting while waiting for an ATS address to reconnect,
1105 * cannot send DISCONNECT */
1108 case GNUNET_TRANSPORT_PS_DISCONNECT:
1109 /* already disconnected, ignore */
1111 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1112 /* already cleaned up, how did we get here!? */
1116 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1117 "Unhandled state `%s'\n",
1118 GNUNET_TRANSPORT_ps2s (n->state));
1122 /* schedule timeout to clean up */
1123 if (NULL != n->task)
1124 GNUNET_SCHEDULER_cancel (n->task);
1125 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1132 * Change the incoming quota for the given peer. Updates
1133 * our own receive rate and informs the neighbour about
1136 * @param n neighbour entry to change qutoa for
1137 * @param quota new quota
1140 set_incoming_quota (struct NeighbourMapEntry *n,
1141 struct GNUNET_BANDWIDTH_Value32NBO quota)
1143 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1144 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
1145 ntohl (quota.value__), GNUNET_i2s (&n->id));
1146 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
1148 if (0 != ntohl (quota.value__))
1150 struct GNUNET_ATS_SessionQuotaMessage sqm;
1152 sqm.header.size = htons (sizeof (struct GNUNET_ATS_SessionQuotaMessage));
1153 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA);
1154 sqm.quota = quota.value__;
1155 (void) send_with_session (n,
1159 GNUNET_TIME_UNIT_FOREVER_REL,
1164 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1165 "Disconnecting peer `%4s' due to SET_QUOTA\n",
1166 GNUNET_i2s (&n->id));
1167 if (GNUNET_YES == test_connected (n))
1168 GNUNET_STATISTICS_update (GST_stats,
1169 gettext_noop ("# disconnects due to quota of 0"),
1171 disconnect_neighbour (n);
1176 * Initialize the primary address of a neighbour
1178 * @param n the neighbour
1179 * @param address address of the other peer, NULL if other peer
1181 * @param session session to use (or NULL, in which case an
1182 * address must be setup)
1183 * @param bandwidth_in inbound quota to be used when connection is up
1184 * @param bandwidth_out outbound quota to be used when connection is up
1187 set_primary_address (struct NeighbourMapEntry *n,
1188 const struct GNUNET_HELLO_Address *address,
1189 struct GNUNET_ATS_Session *session,
1190 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1191 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1193 if (session == n->primary_address.session)
1195 GST_validation_set_address_use (n->primary_address.address,
1197 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
1199 n->primary_address.bandwidth_in = bandwidth_in;
1200 set_incoming_quota (n,
1203 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
1205 n->primary_address.bandwidth_out = bandwidth_out;
1206 send_outbound_quota_to_clients (n);
1210 if ( (NULL != n->primary_address.address) &&
1211 (0 == GNUNET_HELLO_address_cmp (address,
1212 n->primary_address.address)) )
1217 if (NULL == session)
1220 GST_ats_block_address (address,
1224 if (NULL != n->primary_address.address)
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Replacing existing primary address with another one\n");
1228 free_address (&n->primary_address);
1230 n->primary_address.address = GNUNET_HELLO_address_copy (address);
1231 n->primary_address.bandwidth_in = bandwidth_in;
1232 n->primary_address.bandwidth_out = bandwidth_out;
1233 n->primary_address.session = session;
1234 n->primary_address.keep_alive_nonce = 0;
1235 GNUNET_assert (GNUNET_YES ==
1236 GST_ats_is_known (n->primary_address.address,
1237 n->primary_address.session));
1238 /* subsystems about address use */
1239 GST_validation_set_address_use (n->primary_address.address,
1241 set_incoming_quota (n,
1243 send_outbound_quota_to_clients (n);
1244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1245 "Neighbour `%s' switched to address `%s'\n",
1246 GNUNET_i2s (&n->id),
1247 GST_plugins_a2s(address));
1249 neighbours_changed_notification (&n->id,
1250 n->primary_address.address,
1253 n->primary_address.bandwidth_in,
1254 n->primary_address.bandwidth_out);
1259 * We're done with our transmission attempt, continue processing.
1261 * @param cls the `struct MessageQueue` of the message
1262 * @param receiver intended receiver
1263 * @param success whether it worked or not
1264 * @param size_payload bytes payload sent
1265 * @param physical bytes sent on wire
1268 transmit_send_continuation (void *cls,
1269 const struct GNUNET_PeerIdentity *receiver,
1271 size_t size_payload,
1274 struct MessageQueue *mq = cls;
1275 struct NeighbourMapEntry *n;
1277 if (NULL == (n = lookup_neighbour (receiver)))
1279 if (NULL != mq->cont)
1280 mq->cont (mq->cont_cls,
1281 GNUNET_SYSERR /* not connected */,
1285 return; /* disconnect or other error while transmitting, can happen */
1287 if (n->is_active == mq)
1289 /* this is still "our" neighbour, remove us from its queue
1290 and allow it to send the next message now */
1291 n->is_active = NULL;
1292 if (NULL != n->task)
1293 GNUNET_SCHEDULER_cancel (n->task);
1294 n->task = GNUNET_SCHEDULER_add_now (&master_task,
1297 if (bytes_in_send_queue < mq->message_buf_size)
1299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1300 "Bytes_in_send_queue `%llu', Message_size %u, result: %s, payload %u, on wire %u\n",
1301 bytes_in_send_queue,
1302 (unsigned int) mq->message_buf_size,
1303 (GNUNET_OK == success) ? "OK" : "FAIL",
1304 (unsigned int) size_payload,
1305 (unsigned int) physical);
1309 GNUNET_break (size_payload == mq->message_buf_size);
1310 bytes_in_send_queue -= mq->message_buf_size;
1311 GNUNET_STATISTICS_set (GST_stats,
1312 gettext_noop ("# bytes in message queue for other peers"),
1313 bytes_in_send_queue,
1315 if (GNUNET_OK == success)
1316 GNUNET_STATISTICS_update (GST_stats,
1317 gettext_noop ("# messages transmitted to other peers"),
1321 GNUNET_STATISTICS_update (GST_stats,
1323 ("# transmission failures for messages to other peers"),
1325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1326 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1327 GNUNET_i2s (receiver),
1328 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1329 (unsigned int) mq->message_buf_size,
1330 (success == GNUNET_OK) ? "success" : "FAILURE");
1331 if (NULL != mq->cont)
1332 mq->cont (mq->cont_cls,
1341 * Check the message list for the given neighbour and if we can
1342 * send a message, do so. This function should only be called
1343 * if the connection is at least generally ready for transmission.
1344 * While we will only send one message at a time, no bandwidth
1345 * quota management is performed here. If a message was given to
1346 * the plugin, the continuation will automatically re-schedule
1347 * the 'master' task once the next message might be transmitted.
1349 * @param n target peer for which to transmit
1352 try_transmission_to_peer (struct NeighbourMapEntry *n)
1354 struct MessageQueue *mq;
1355 struct GNUNET_TIME_Relative timeout;
1357 if (NULL == n->primary_address.address)
1359 /* no address, why are we here? */
1363 if ((0 == n->primary_address.address->address_length) &&
1364 (NULL == n->primary_address.session))
1366 /* no address, why are we here? */
1370 if (NULL != n->is_active)
1372 /* transmission already pending */
1376 /* timeout messages from the queue that are past their due date */
1377 while (NULL != (mq = n->messages_head))
1379 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1380 if (timeout.rel_value_us > 0)
1382 GNUNET_STATISTICS_update (GST_stats,
1383 gettext_noop ("# messages timed out while in transport queue"),
1386 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1390 transmit_send_continuation (mq,
1393 mq->message_buf_size,
1397 return; /* no more messages */
1398 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1404 "Giving message with %u bytes to plugin session %p\n",
1405 (unsigned int) mq->message_buf_size,
1406 n->primary_address.session);
1407 (void) send_with_session (n,
1409 mq->message_buf_size,
1413 &transmit_send_continuation,
1419 * Send keepalive message to the neighbour. Must only be called
1420 * if we are on 'connected' state or while trying to switch addresses.
1421 * Will internally determine if a keepalive is truly needed (so can
1422 * always be called).
1424 * @param n neighbour that went idle and needs a keepalive
1427 send_keepalive (struct NeighbourMapEntry *n)
1429 struct GNUNET_ATS_SessionKeepAliveMessage m;
1430 struct GNUNET_TIME_Relative timeout;
1433 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1434 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state));
1435 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1436 return; /* no keepalive needed at this time */
1438 nonce = 0; /* 0 indicates 'not set' */
1440 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1444 "Sending KEEPALIVE to peer `%s' with nonce %u\n",
1445 GNUNET_i2s (&n->id),
1447 m.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1448 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1449 m.nonce = htonl (nonce);
1451 timeout = send_with_session (n,
1454 UINT32_MAX /* priority */,
1455 GNUNET_TIME_UNIT_FOREVER_REL,
1458 GNUNET_STATISTICS_update (GST_stats,
1459 gettext_noop ("# KEEPALIVES sent"),
1462 n->primary_address.keep_alive_nonce = nonce;
1463 n->expect_latency_response = GNUNET_YES;
1464 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1465 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1470 * Keep the connection to the given neighbour alive longer,
1471 * we received a KEEPALIVE (or equivalent); send a response.
1473 * @param neighbour neighbour to keep alive (by sending keep alive response)
1474 * @param m the keep alive message containing the nonce to respond to
1477 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1478 const struct GNUNET_MessageHeader *m)
1480 struct NeighbourMapEntry *n;
1481 const struct GNUNET_ATS_SessionKeepAliveMessage *msg_in;
1482 struct GNUNET_ATS_SessionKeepAliveMessage msg;
1484 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1486 GNUNET_break_op (0);
1490 msg_in = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1491 if (NULL == (n = lookup_neighbour (neighbour)))
1493 GNUNET_STATISTICS_update (GST_stats,
1495 ("# KEEPALIVE messages discarded (peer unknown)"),
1499 if (NULL == n->primary_address.session)
1501 GNUNET_STATISTICS_update (GST_stats,
1503 ("# KEEPALIVE messages discarded (no session)"),
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1509 "Received KEEPALIVE request from peer `%s' with nonce %u\n",
1510 GNUNET_i2s (&n->id),
1511 ntohl (msg_in->nonce));
1512 GNUNET_STATISTICS_update (GST_stats,
1513 gettext_noop ("# KEEPALIVES received in good order"),
1517 /* send reply to allow neighbour to measure latency */
1518 msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1519 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1520 msg.nonce = msg_in->nonce;
1521 (void) send_with_session (n,
1523 sizeof (struct GNUNET_ATS_SessionKeepAliveMessage),
1524 UINT32_MAX /* priority */,
1525 GNUNET_TIME_UNIT_FOREVER_REL,
1532 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1533 * latency to this peer. Pass the updated information (existing ats
1534 * plus calculated latency) to ATS.
1536 * @param neighbour neighbour to keep alive
1537 * @param m the message containing the keep alive response
1540 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1541 const struct GNUNET_MessageHeader *m)
1543 struct NeighbourMapEntry *n;
1544 const struct GNUNET_ATS_SessionKeepAliveMessage *msg;
1545 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1546 struct GNUNET_TIME_Relative latency;
1548 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1550 GNUNET_break_op (0);
1554 msg = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1555 if (NULL == (n = lookup_neighbour (neighbour)))
1557 GNUNET_STATISTICS_update (GST_stats,
1558 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not connected)"),
1563 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1564 (GNUNET_YES != n->expect_latency_response) )
1566 GNUNET_STATISTICS_update (GST_stats,
1567 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not expected)"),
1572 if (NULL == n->primary_address.address)
1574 GNUNET_STATISTICS_update (GST_stats,
1575 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (address changed)"),
1580 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1582 if (0 == n->primary_address.keep_alive_nonce)
1583 GNUNET_STATISTICS_update (GST_stats,
1584 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (no nonce)"),
1588 GNUNET_STATISTICS_update (GST_stats,
1589 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (bad nonce)"),
1594 GNUNET_STATISTICS_update (GST_stats,
1595 gettext_noop ("# KEEPALIVE_RESPONSEs received (OK)"),
1600 /* Update session timeout here */
1601 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Updating session for peer `%s' for session %p\n",
1605 GNUNET_i2s (&n->id),
1606 n->primary_address.session);
1607 papi->update_session_timeout (papi->cls,
1609 n->primary_address.session);
1616 n->primary_address.keep_alive_nonce = 0;
1617 n->expect_latency_response = GNUNET_NO;
1618 set_state_and_timeout (n,
1620 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1622 latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1624 "Received KEEPALIVE_RESPONSE from peer `%s', latency is %s\n",
1625 GNUNET_i2s (&n->id),
1626 GNUNET_STRINGS_relative_time_to_string (latency,
1628 GST_ats_update_delay (n->primary_address.address,
1629 GNUNET_TIME_relative_divide (latency,
1635 * We have received a message from the given sender. How long should
1636 * we delay before receiving more? (Also used to keep the peer marked
1639 * @param sender sender of the message
1640 * @param size size of the message
1641 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1642 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1643 * #GNUNET_SYSERR if the connection is not fully up yet
1644 * @return how long to wait before reading more from this sender
1646 struct GNUNET_TIME_Relative
1647 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
1651 struct NeighbourMapEntry *n;
1652 struct GNUNET_TIME_Relative ret;
1654 if (NULL == neighbours)
1656 *do_forward = GNUNET_NO;
1657 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1659 if (NULL == (n = lookup_neighbour (sender)))
1661 GNUNET_STATISTICS_update (GST_stats,
1662 gettext_noop ("# messages discarded due to lack of neighbour record"),
1665 *do_forward = GNUNET_NO;
1666 return GNUNET_TIME_UNIT_ZERO;
1668 if (! test_connected (n))
1670 *do_forward = GNUNET_SYSERR;
1671 return GNUNET_TIME_UNIT_ZERO;
1673 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1675 n->quota_violation_count++;
1676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1677 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1678 n->in_tracker.available_bytes_per_s__,
1679 n->quota_violation_count);
1680 /* Discount 32k per violation */
1681 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1685 if (n->quota_violation_count > 0)
1687 /* try to add 32k back */
1688 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1689 n->quota_violation_count--;
1692 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1694 GNUNET_STATISTICS_update (GST_stats,
1696 ("# bandwidth quota violations by other peers"),
1698 *do_forward = GNUNET_NO;
1699 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1701 *do_forward = GNUNET_YES;
1702 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1703 if (ret.rel_value_us > 0)
1705 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1706 "Throttling read (%lld bytes excess at %u b/s), waiting %s before reading more.\n",
1707 (long long) n->in_tracker.consumption_since_last_update__,
1708 (unsigned int) n->in_tracker.available_bytes_per_s__,
1709 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1710 GNUNET_STATISTICS_update (GST_stats,
1711 gettext_noop ("# ms throttling suggested"),
1712 (int64_t) ret.rel_value_us / 1000LL,
1720 * Transmit a message to the given target using the active connection.
1722 * @param target destination
1723 * @param msg message to send
1724 * @param msg_size number of bytes in msg
1725 * @param timeout when to fail with timeout
1726 * @param cont function to call when done
1727 * @param cont_cls closure for @a cont
1730 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
1733 struct GNUNET_TIME_Relative timeout,
1734 GST_NeighbourSendContinuation cont,
1737 struct NeighbourMapEntry *n;
1738 struct MessageQueue *mq;
1740 /* All ove these cases should never happen; they are all API violations.
1741 But we check anyway, just to be sure. */
1742 if (NULL == (n = lookup_neighbour (target)))
1752 if (GNUNET_YES != test_connected (n))
1762 bytes_in_send_queue += msg_size;
1763 GNUNET_STATISTICS_set (GST_stats,
1765 ("# bytes in message queue for other peers"),
1766 bytes_in_send_queue, GNUNET_NO);
1767 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1769 mq->cont_cls = cont_cls;
1770 GNUNET_memcpy (&mq[1], msg, msg_size);
1771 mq->message_buf = (const char *) &mq[1];
1772 mq->message_buf_size = msg_size;
1773 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1776 "Enqueueing %u bytes to send to peer %s\n",
1777 (unsigned int) msg_size,
1778 GNUNET_i2s (target));
1779 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
1782 if (NULL != n->task)
1783 GNUNET_SCHEDULER_cancel (n->task);
1784 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1789 * Continuation called from our attempt to transmitted our
1790 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN to the specified @a
1791 * target. Continue processing based on the @a result. Specifically,
1792 * if we failed to transmit, discard the address we used.
1795 * @param target which peer received the transmission
1796 * @param result #GNUNET_OK if sending worked
1797 * @param size_payload how many bytes of payload were sent (ignored)
1798 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1801 send_session_syn_cont (void *cls,
1802 const struct GNUNET_PeerIdentity *target,
1804 size_t size_payload,
1805 size_t size_on_wire)
1807 struct NeighbourMapEntry *n;
1809 n = lookup_neighbour (target);
1812 /* SYN continuation was called after neighbor was freed,
1813 * for example due to a time out for the state or the session
1814 * used was already terminated: nothing to do here... */
1818 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1819 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1820 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1822 /* SYN continuation was called after neighbor changed state,
1823 * for example due to a time out for the state or the session
1824 * used was already terminated: nothing to do here... */
1827 if (GNUNET_OK == result)
1830 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1831 _("Failed to send SYN message to peer `%s'\n"),
1832 GNUNET_i2s (target));
1834 case GNUNET_TRANSPORT_PS_SYN_SENT:
1835 /* Remove address and request an additional one */
1836 unset_primary_address (n);
1837 set_state_and_timeout (n,
1838 GNUNET_TRANSPORT_PS_INIT_ATS,
1839 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1841 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1842 /* Remove address and request an additional one */
1843 unset_primary_address (n);
1844 set_state_and_timeout (n,
1845 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1846 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1848 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1849 /* Remove address and request and go back to primary address */
1850 GNUNET_STATISTICS_update (GST_stats,
1851 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN CONT)"),
1854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1855 "Switch failed, cleaning up alternative address\n");
1856 free_address (&n->alternative_address);
1857 set_state_and_timeout (n,
1858 GNUNET_TRANSPORT_PS_CONNECTED,
1859 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1862 disconnect_neighbour (n);
1869 * Send a SYN message via the given address.
1871 * @param na address to use
1874 send_syn (struct NeighbourAddress *na)
1876 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1877 struct TransportSynMessage connect_msg;
1878 struct NeighbourMapEntry *n;
1880 GNUNET_assert (NULL != na->session);
1881 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1882 "Sending SYN message to peer `%s' at %s\n",
1883 GNUNET_i2s (&na->address->peer),
1884 GST_plugins_a2s (na->address));
1886 papi = GST_plugins_find (na->address->transport_name);
1887 GNUNET_assert (NULL != papi);
1888 GNUNET_STATISTICS_update (GST_stats,
1890 ("# SYN messages sent"),
1892 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1893 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1894 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1895 connect_msg.reserved = htonl (0);
1896 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1898 papi->send (papi->cls,
1900 (const char *) &connect_msg,
1901 sizeof (struct TransportSynMessage),
1903 SETUP_CONNECTION_TIMEOUT,
1904 &send_session_syn_cont, NULL))
1906 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1907 _("Failed to transmit SYN message to %s\n"),
1908 GST_plugins_a2s (na->address));
1909 n = lookup_neighbour (&na->address->peer);
1916 case GNUNET_TRANSPORT_PS_SYN_SENT:
1917 /* Remove address and request and additional one */
1918 GNUNET_assert (na == &n->primary_address);
1919 unset_primary_address (n);
1920 set_state_and_timeout (n,
1921 GNUNET_TRANSPORT_PS_INIT_ATS,
1922 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1923 /* Hard failure to send the SYN message with this address:
1924 Destroy address and session */
1926 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1927 /* Remove address and request an additional one */
1928 GNUNET_assert (na == &n->primary_address);
1929 unset_primary_address (n);
1930 set_state_and_timeout (n,
1931 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1932 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1934 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1935 GNUNET_assert (na == &n->alternative_address);
1936 GNUNET_STATISTICS_update (GST_stats,
1937 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN)"),
1940 /* Remove address and request an additional one */
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1942 "Switch failed, cleaning up alternative address\n");
1943 free_address (&n->alternative_address);
1944 set_state_and_timeout (n,
1945 GNUNET_TRANSPORT_PS_CONNECTED,
1946 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1950 disconnect_neighbour (n);
1955 GST_neighbours_notify_data_sent (na->address,
1957 sizeof (struct TransportSynMessage));
1962 * Continuation called from our attempt to transmitted our
1963 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK to the specified @a
1964 * target. Continue processing based on the @a result. Specifically,
1965 * if we failed to transmit, discard the address we used.
1968 * @param target which peer received the transmission
1969 * @param result #GNUNET_OK if sending worked
1970 * @param size_payload how many bytes of payload were sent (ignored)
1971 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1974 send_session_syn_ack_cont (void *cls,
1975 const struct GNUNET_PeerIdentity *target,
1977 size_t size_payload,
1978 size_t size_on_wire)
1980 struct NeighbourMapEntry *n;
1982 n = lookup_neighbour (target);
1985 /* SYN_ACK continuation was called after neighbor was freed,
1986 * for example due to a time out for the state or the session
1987 * used was already terminated: nothing to do here... */
1991 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1993 /* SYN_ACK continuation was called after neighbor changed state,
1994 * for example due to a time out for the state or the session
1995 * used was already terminated: nothing to do here... */
1998 if (GNUNET_OK == result)
2001 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2002 _("Failed to send SYN_ACK message to peer `%s' using address `%s'\n"),
2003 GNUNET_i2s (target),
2004 GST_plugins_a2s (n->primary_address.address));
2006 /* Remove address and request and additional one */
2007 /* FIXME: what if the neighbour's primary address
2008 changed in the meantime? Might want to instead
2009 pass "something" around in closure to be sure. */
2010 unset_primary_address (n);
2011 n->ack_state = ACK_SEND_SYN_ACK;
2012 set_state_and_timeout (n,
2013 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2014 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2019 * Send a SYN_ACK message via the given address.
2021 * @param na address and session to use
2022 * @param timestamp timestamp to use for the ACK message
2023 * @return #GNUNET_SYSERR if sending immediately failed, #GNUNET_OK otherwise
2026 send_syn_ack_message (struct NeighbourAddress *na,
2027 struct GNUNET_TIME_Absolute timestamp)
2029 const struct GNUNET_HELLO_Address *address = na->address;
2030 struct GNUNET_ATS_Session *session = na->session;
2031 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2032 struct TransportSynMessage connect_msg;
2033 struct NeighbourMapEntry *n;
2035 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2036 "Sending SYN_ACK to peer `%s'\n",
2037 GNUNET_i2s (&address->peer));
2039 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2044 if (NULL == session)
2045 session = papi->get_session (papi->cls,
2047 if (NULL == session)
2052 GST_ats_new_session (address,
2054 GNUNET_STATISTICS_update (GST_stats,
2056 ("# SYN_ACK messages sent"),
2058 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
2059 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
2060 connect_msg.reserved = htonl (0);
2061 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
2063 if (GNUNET_SYSERR ==
2064 papi->send (papi->cls,
2066 (const char *) &connect_msg,
2067 sizeof (struct TransportSynMessage),
2069 GNUNET_TIME_UNIT_FOREVER_REL,
2070 &send_session_syn_ack_cont, NULL))
2072 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2073 _("Failed to transmit SYN_ACK message to %s\n"),
2074 GST_plugins_a2s (address));
2076 n = lookup_neighbour (&address->peer);
2082 /* Remove address and request and additional one */
2083 unset_primary_address (n);
2084 n->ack_state = ACK_SEND_SYN_ACK;
2085 set_state_and_timeout (n,
2086 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2087 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2094 * Function called by the bandwidth tracker for a peer whenever
2095 * the tracker's state changed such that we need to recalculate
2096 * the delay for flow control. We calculate the latest delay
2097 * and inform the plugin (if applicable).
2099 * @param cls the `struct NeighbourMapEntry` to update calculations for
2102 inbound_bw_tracker_update (void *cls)
2104 struct NeighbourMapEntry *n = cls;
2105 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2106 struct GNUNET_TIME_Relative delay;
2109 if (NULL == n->primary_address.address)
2110 return; /* not active, ignore */
2111 papi = GST_plugins_find (n->primary_address.address->transport_name);
2112 GNUNET_assert (NULL != papi);
2113 if (NULL == papi->update_inbound_delay)
2115 delay = GST_neighbours_calculate_receive_delay (&n->id,
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2119 "New inbound delay for peer `%s' is %llu ms\n",
2120 GNUNET_i2s (&n->id),
2121 (unsigned long long) delay.rel_value_us / 1000LL);
2122 papi->update_inbound_delay (papi->cls,
2124 n->primary_address.session,
2130 * Create a fresh entry in the neighbour map for the given peer
2132 * @param peer peer to create an entry for
2133 * @return new neighbour map entry
2135 static struct NeighbourMapEntry *
2136 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2138 struct NeighbourMapEntry *n;
2140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2141 "Creating new neighbour entry for `%s'\n",
2144 memcmp (&GST_my_identity,
2146 sizeof (struct GNUNET_PeerIdentity)));
2147 n = GNUNET_new (struct NeighbourMapEntry);
2149 n->ack_state = ACK_UNDEFINED;
2150 n->last_util_transmission = GNUNET_TIME_absolute_get();
2151 n->neighbour_receive_quota = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2152 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2153 &inbound_bw_tracker_update,
2155 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2156 MAX_BANDWIDTH_CARRY_S);
2157 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2158 set_state_and_timeout (n,
2159 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2160 GNUNET_TIME_UNIT_FOREVER_ABS);
2161 GNUNET_assert (GNUNET_OK ==
2162 GNUNET_CONTAINER_multipeermap_put (neighbours,
2165 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2166 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2175 * Entry in a DLL we use to keep track of pending blacklist checks.
2177 struct BlacklistCheckSwitchContext
2182 struct BlacklistCheckSwitchContext *prev;
2187 struct BlacklistCheckSwitchContext *next;
2190 * Handle to the blacklist check we are performing.
2192 struct GST_BlacklistCheck *blc;
2195 * Inbound bandwidth that was assigned to @e address.
2197 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2200 * Outbound bandwidth that was assigned to @e address.
2202 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2207 * We received a 'SYN' message from the other peer.
2208 * Consider switching to it.
2210 * @param message possibly a `struct TransportSynMessage` (check format)
2211 * @param peer identity of the peer to switch the address for
2212 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2215 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2216 const struct GNUNET_PeerIdentity *peer)
2218 const struct TransportSynMessage *scm;
2219 struct NeighbourMapEntry *n;
2220 struct GNUNET_TIME_Absolute ts;
2222 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2224 GNUNET_break_op (0);
2225 return GNUNET_SYSERR;
2227 GNUNET_STATISTICS_update (GST_stats,
2229 ("# SYN messages received"),
2231 if (NULL == neighbours)
2233 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2234 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2236 return GNUNET_OK; /* we're shutting down */
2238 scm = (const struct TransportSynMessage *) message;
2239 GNUNET_break_op (0 == ntohl (scm->reserved));
2240 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2241 n = lookup_neighbour (peer);
2244 /* This is a new neighbour and set to not connected */
2245 n = setup_neighbour (peer);
2248 /* Remember this SYN message in neighbour */
2249 n->ack_state = ACK_SEND_SYN_ACK;
2250 n->connect_ack_timestamp = ts;
2252 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2253 "Received SYN for peer `%s' in state %s/%s\n",
2255 GNUNET_TRANSPORT_ps2s (n->state),
2256 print_ack_state (n->ack_state));
2260 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2261 /* Request an address from ATS to send SYN_ACK to this peer */
2262 set_state_and_timeout (n,
2263 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2264 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2266 case GNUNET_TRANSPORT_PS_INIT_ATS:
2267 /* SYN message takes priority over us asking ATS for address:
2268 * Wait for ATS to suggest an address and send SYN_ACK */
2269 set_state_and_timeout (n,
2270 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2271 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2273 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2274 /* We already wait for an address to send an SYN_ACK */
2276 case GNUNET_TRANSPORT_PS_SYN_SENT:
2277 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2278 /* Send ACK immediately */
2279 n->ack_state = ACK_SEND_ACK;
2280 send_syn_ack_message (&n->primary_address,
2283 case GNUNET_TRANSPORT_PS_CONNECTED:
2284 /* we are already connected and can thus send the ACK immediately */
2285 GNUNET_assert (NULL != n->primary_address.address);
2286 GNUNET_assert (NULL != n->primary_address.session);
2287 n->ack_state = ACK_SEND_ACK;
2288 send_syn_ack_message (&n->primary_address,
2291 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2292 /* We wait for ATS address suggestion */
2294 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2295 /* We received a SYN message while waiting for a SYN_ACK in fast
2296 * reconnect. Send SYN_ACK immediately */
2297 n->ack_state = ACK_SEND_ACK;
2298 send_syn_ack_message (&n->primary_address,
2299 n->connect_ack_timestamp);
2301 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2302 /* We are already connected and can thus send the ACK immediately;
2303 still, it can never hurt to have an alternative address, so also
2304 tell ATS about it */
2305 GNUNET_assert (NULL != n->primary_address.address);
2306 GNUNET_assert (NULL != n->primary_address.session);
2307 n->ack_state = ACK_SEND_ACK;
2308 send_syn_ack_message (&n->primary_address,
2311 case GNUNET_TRANSPORT_PS_DISCONNECT:
2312 /* Get rid of remains and re-try */
2314 n = setup_neighbour (peer);
2315 /* Remember the SYN time stamp for ACK message */
2316 n->ack_state = ACK_SEND_SYN_ACK;
2317 n->connect_ack_timestamp = ts;
2318 /* Request an address for the peer */
2319 set_state_and_timeout (n,
2320 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2321 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2323 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2324 /* should not be possible */
2328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2329 "Unhandled state `%s'\n",
2330 GNUNET_TRANSPORT_ps2s (n->state));
2332 return GNUNET_SYSERR;
2339 * Check if the given @a address is the same that we are already
2340 * using for the respective neighbour. If so, update the bandwidth
2341 * assignment and possibly the session and return #GNUNET_OK.
2342 * If the new address is different from what the neighbour is
2343 * using right now, return #GNUNET_NO.
2345 * @param address address of the other peer,
2346 * @param session session to use or NULL if transport should initiate a session
2347 * @param bandwidth_in inbound quota to be used when connection is up,
2348 * 0 to disconnect from peer
2349 * @param bandwidth_out outbound quota to be used when connection is up,
2350 * 0 to disconnect from peer
2351 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2352 * #GNUNET_NO if more extensive changes are required (address changed)
2355 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2356 struct GNUNET_ATS_Session *session,
2357 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2358 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2360 struct NeighbourMapEntry *n;
2362 n = lookup_neighbour (&address->peer);
2364 (NULL == n->primary_address.address) ||
2365 (0 != GNUNET_HELLO_address_cmp (address,
2366 n->primary_address.address)) )
2368 /* We are not really switching addresses, but merely adjusting
2369 session and/or bandwidth, can do fast ATS update! */
2370 if (session != n->primary_address.session)
2372 /* switch to a different session, but keeping same address; could
2373 happen if there is a 2nd inbound connection */
2374 n->primary_address.session = session;
2375 GNUNET_assert (GNUNET_YES ==
2376 GST_ats_is_known (n->primary_address.address,
2377 n->primary_address.session));
2379 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
2381 n->primary_address.bandwidth_in = bandwidth_in;
2382 set_incoming_quota (n,
2385 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
2387 n->primary_address.bandwidth_out = bandwidth_out;
2388 send_outbound_quota_to_clients (n);
2395 * We've been asked to switch addresses, and just now got the result
2396 * from the blacklist check to see if this is allowed.
2398 * @param cls the `struct BlacklistCheckSwitchContext` with
2399 * the information about the future address
2400 * @param peer the peer we may switch addresses on
2401 * @param address address associated with the request
2402 * @param session session associated with the request
2403 * @param result #GNUNET_OK if the connection is allowed,
2404 * #GNUNET_NO if not,
2405 * #GNUNET_SYSERR if operation was aborted
2408 switch_address_bl_check_cont (void *cls,
2409 const struct GNUNET_PeerIdentity *peer,
2410 const struct GNUNET_HELLO_Address *address,
2411 struct GNUNET_ATS_Session *session,
2414 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2415 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2416 struct NeighbourMapEntry *n;
2418 if (GNUNET_SYSERR == result)
2421 papi = GST_plugins_find (address->transport_name);
2422 GNUNET_assert (NULL != papi);
2424 if (GNUNET_NO == result)
2426 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2427 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2428 GST_plugins_a2s (address),
2431 GNUNET_STATISTICS_update (GST_stats,
2432 "# ATS suggestions ignored (blacklist denied)",
2435 if (NULL != session)
2436 papi->disconnect_session (papi->cls,
2439 GNUNET_HELLO_address_check_option (address,
2440 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2441 GST_ats_block_address (address,
2447 if (NULL == session)
2449 /* need to create a session, ATS only gave us an address */
2450 session = papi->get_session (papi->cls,
2452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2453 "Obtained new session for peer `%s' and address '%s': %p\n",
2454 GNUNET_i2s (&address->peer),
2455 GST_plugins_a2s (address),
2457 if (NULL != session)
2458 GST_ats_new_session (address,
2461 if (NULL == session)
2463 /* session creation failed, bad!, fail! */
2464 GNUNET_STATISTICS_update (GST_stats,
2465 "# ATS suggestions ignored (failed to create session)",
2468 /* No session could be obtained, remove blacklist check and clean up */
2469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2470 "Failed to obtain new session for peer `%s' and address '%s'\n",
2471 GNUNET_i2s (&address->peer),
2472 GST_plugins_a2s (address));
2473 GST_ats_block_address (address,
2478 /* We did this check already before going into blacklist, but
2479 it is theoretically possible that the situation changed in
2480 the meantime, hence we check again here */
2482 try_run_fast_ats_update (address,
2484 blc_ctx->bandwidth_in,
2485 blc_ctx->bandwidth_out))
2486 goto cleanup; /* was just a minor update, we're done */
2488 /* check if we also need to setup the neighbour entry */
2489 if (NULL == (n = lookup_neighbour (peer)))
2491 n = setup_neighbour (peer);
2492 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2495 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2496 "Peer `%s' switches to address `%s'\n",
2497 GNUNET_i2s (&address->peer),
2498 GST_plugins_a2s (address));
2502 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2504 GST_ats_block_address (address,
2508 case GNUNET_TRANSPORT_PS_INIT_ATS:
2509 /* We requested an address and ATS suggests one:
2510 * set primary address and send SYN message*/
2511 set_primary_address (n,
2514 blc_ctx->bandwidth_in,
2515 blc_ctx->bandwidth_out);
2516 if (ACK_SEND_SYN_ACK == n->ack_state)
2518 /* Send pending SYN_ACK message */
2519 n->ack_state = ACK_SEND_ACK;
2520 send_syn_ack_message (&n->primary_address,
2521 n->connect_ack_timestamp);
2523 set_state_and_timeout (n,
2524 GNUNET_TRANSPORT_PS_SYN_SENT,
2525 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2526 send_syn (&n->primary_address);
2528 case GNUNET_TRANSPORT_PS_SYN_SENT:
2529 /* ATS suggested a new address while waiting for an SYN_ACK:
2530 * Switch and send new SYN */
2531 /* ATS suggests a different address, switch again */
2532 set_primary_address (n,
2535 blc_ctx->bandwidth_in,
2536 blc_ctx->bandwidth_out);
2537 if (ACK_SEND_SYN_ACK == n->ack_state)
2539 /* Send pending SYN_ACK message */
2540 n->ack_state = ACK_SEND_ACK;
2541 send_syn_ack_message (&n->primary_address,
2542 n->connect_ack_timestamp);
2544 set_state_and_timeout (n,
2545 GNUNET_TRANSPORT_PS_SYN_SENT,
2546 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2547 send_syn (&n->primary_address);
2549 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2550 /* We requested an address and ATS suggests one:
2551 * set primary address and send SYN_ACK message*/
2552 set_primary_address (n,
2555 blc_ctx->bandwidth_in,
2556 blc_ctx->bandwidth_out);
2557 /* Send an ACK message as a response to the SYN msg */
2558 set_state_and_timeout (n,
2559 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2560 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2561 send_syn_ack_message (&n->primary_address,
2562 n->connect_ack_timestamp);
2563 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2564 (ACK_UNDEFINED == n->ack_state) )
2565 n->ack_state = ACK_SEND_ACK;
2567 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2568 /* ATS asks us to switch while we were trying to connect; switch to new
2569 address and check blacklist again */
2570 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2572 n->ack_state = ACK_SEND_ACK;
2573 send_syn_ack_message (&n->primary_address,
2574 n->connect_ack_timestamp);
2576 set_primary_address (n,
2579 blc_ctx->bandwidth_in,
2580 blc_ctx->bandwidth_out);
2581 set_state_and_timeout (n,
2582 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2583 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2585 case GNUNET_TRANSPORT_PS_CONNECTED:
2586 GNUNET_assert (NULL != n->primary_address.address);
2587 GNUNET_assert (NULL != n->primary_address.session);
2588 GNUNET_break (n->primary_address.session != session);
2589 /* ATS asks us to switch a life connection; see if we can get
2590 a SYN_ACK on it before we actually do this! */
2591 set_alternative_address (n,
2594 blc_ctx->bandwidth_in,
2595 blc_ctx->bandwidth_out);
2596 set_state_and_timeout (n,
2597 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2598 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2599 GNUNET_STATISTICS_update (GST_stats,
2600 gettext_noop ("# Attempts to switch addresses"),
2603 send_syn (&n->alternative_address);
2605 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2606 set_primary_address (n,
2609 blc_ctx->bandwidth_in,
2610 blc_ctx->bandwidth_out);
2611 if (ACK_SEND_SYN_ACK == n->ack_state)
2613 /* Send pending SYN_ACK message */
2614 n->ack_state = ACK_SEND_ACK;
2615 send_syn_ack_message (&n->primary_address,
2616 n->connect_ack_timestamp);
2618 set_state_and_timeout (n,
2619 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2620 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2621 send_syn (&n->primary_address);
2623 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2624 /* ATS asks us to switch while we were trying to reconnect; switch to new
2625 address and send SYN again */
2626 set_primary_address (n,
2629 blc_ctx->bandwidth_in,
2630 blc_ctx->bandwidth_out);
2631 set_state_and_timeout (n,
2632 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2633 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2634 send_syn (&n->primary_address);
2636 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2637 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2639 (n->primary_address.session == session) )
2641 /* ATS switches back to still-active session */
2642 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2643 "ATS double-switched, cleaning up alternative address\n");
2644 free_address (&n->alternative_address);
2645 set_state_and_timeout (n,
2646 GNUNET_TRANSPORT_PS_CONNECTED,
2650 /* ATS asks us to switch a life connection, send */
2651 set_alternative_address (n,
2654 blc_ctx->bandwidth_in,
2655 blc_ctx->bandwidth_out);
2656 set_state_and_timeout (n,
2657 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2658 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2659 send_syn (&n->alternative_address);
2661 case GNUNET_TRANSPORT_PS_DISCONNECT:
2662 /* not going to switch addresses while disconnecting */
2663 GNUNET_STATISTICS_update (GST_stats,
2664 "# ATS suggestion ignored (disconnecting)",
2668 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2672 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2673 "Unhandled state `%s'\n",
2674 GNUNET_TRANSPORT_ps2s (n->state));
2679 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2682 GNUNET_free (blc_ctx);
2687 * For the given peer, switch to this address.
2689 * Before accepting this addresses and actively using it, a blacklist check
2692 * If any check fails or the suggestion can somehow not be followed, we
2693 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2694 * could not be satisfied and force ATS to do something else.
2696 * @param address address of the other peer,
2697 * @param session session to use or NULL if transport should initiate a session
2698 * @param bandwidth_in inbound quota to be used when connection is up,
2699 * 0 to disconnect from peer
2700 * @param bandwidth_out outbound quota to be used when connection is up,
2701 * 0 to disconnect from peer
2704 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2705 struct GNUNET_ATS_Session *session,
2706 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2707 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2709 struct GST_BlacklistCheck *blc;
2710 struct BlacklistCheckSwitchContext *blc_ctx;
2712 GNUNET_assert (NULL != address->transport_name);
2714 try_run_fast_ats_update (address,
2720 /* Check if plugin is available */
2721 if (NULL == (GST_plugins_find (address->transport_name)))
2723 /* we don't have the plugin for this address */
2725 GST_ats_block_address (address,
2729 if ((NULL == session) &&
2730 (GNUNET_HELLO_address_check_option (address,
2731 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2733 /* This is a inbound address and we do not have a session to use! */
2735 GST_ats_block_address (address,
2740 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2741 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2742 GST_plugins_a2s (address),
2743 GNUNET_i2s (&address->peer),
2744 (unsigned int) ntohl (bandwidth_in.value__),
2745 (unsigned int) ntohl (bandwidth_out.value__));
2747 /* Perform blacklist check */
2748 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2749 blc_ctx->bandwidth_in = bandwidth_in;
2750 blc_ctx->bandwidth_out = bandwidth_out;
2751 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2754 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2755 address->transport_name,
2756 &switch_address_bl_check_cont,
2767 * Function called to send network utilization data to ATS for
2768 * each active connection.
2771 * @param key peer we send utilization data for
2772 * @param value the `struct NeighbourMapEntry *` with data to send
2773 * @return #GNUNET_OK (continue to iterate)
2776 send_utilization_data (void *cls,
2777 const struct GNUNET_PeerIdentity *key,
2780 struct NeighbourMapEntry *n = value;
2783 struct GNUNET_TIME_Relative delta;
2785 if ( (GNUNET_YES != test_connected (n)) ||
2786 (NULL == n->primary_address.address) )
2788 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2789 GNUNET_TIME_absolute_get ());
2791 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2792 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2794 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2795 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2798 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2802 GST_ats_update_utilization (n->primary_address.address,
2805 n->util_total_bytes_recv = 0;
2806 n->util_total_bytes_sent = 0;
2807 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2813 * Task transmitting utilization in a regular interval
2815 * @param cls the 'struct NeighbourMapEntry' for which we are running
2818 utilization_transmission (void *cls)
2820 util_transmission_tk = NULL;
2821 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
2822 &send_utilization_data,
2824 util_transmission_tk
2825 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2826 &utilization_transmission,
2832 * Track information about data we received from the
2833 * given address (used to notify ATS about our utilization
2834 * of allocated resources).
2836 * @param address the address we got data from
2837 * @param message the message we received (really only the size is used)
2840 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2841 const struct GNUNET_MessageHeader *message)
2843 struct NeighbourMapEntry *n;
2845 n = lookup_neighbour (&address->peer);
2848 n->util_total_bytes_recv += ntohs (message->size);
2853 * Track information about data we transmitted using the given @a
2854 * address and @a session (used to notify ATS about our utilization of
2855 * allocated resources).
2857 * @param address the address we transmitted data to
2858 * @param session session we used to transmit data
2859 * @param message the message we sent (really only the size is used)
2862 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2863 struct GNUNET_ATS_Session *session,
2866 struct NeighbourMapEntry *n;
2868 n = lookup_neighbour (&address->peer);
2871 if (n->primary_address.session != session)
2873 n->util_total_bytes_sent += size;
2878 * Master task run for every neighbour. Performs all of the time-related
2879 * activities (keep alive, send next message, disconnect if idle, finish
2880 * clean up after disconnect).
2882 * @param cls the 'struct NeighbourMapEntry' for which we are running
2885 master_task (void *cls)
2887 struct NeighbourMapEntry *n = cls;
2888 struct GNUNET_TIME_Relative delay;
2891 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2893 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2894 GNUNET_i2s (&n->id),
2895 GNUNET_TRANSPORT_ps2s(n->state),
2896 GNUNET_STRINGS_relative_time_to_string (delay,
2900 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2901 /* invalid state for master task, clean up */
2905 case GNUNET_TRANSPORT_PS_INIT_ATS:
2906 if (0 == delay.rel_value_us)
2908 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2909 "Connection to `%s' timed out waiting for ATS to provide address\n",
2910 GNUNET_i2s (&n->id));
2915 case GNUNET_TRANSPORT_PS_SYN_SENT:
2916 if (0 == delay.rel_value_us)
2918 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2919 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2920 GNUNET_i2s (&n->id));
2921 /* Remove address and request and additional one */
2922 unset_primary_address (n);
2923 set_state_and_timeout (n,
2924 GNUNET_TRANSPORT_PS_INIT_ATS,
2925 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2929 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2930 if (0 == delay.rel_value_us)
2932 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2933 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2934 GNUNET_i2s (&n->id));
2939 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2940 if (0 == delay.rel_value_us)
2942 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2943 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2944 GNUNET_i2s (&n->id));
2945 disconnect_neighbour (n);
2949 case GNUNET_TRANSPORT_PS_CONNECTED:
2950 if (0 == delay.rel_value_us)
2952 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2953 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2954 GNUNET_i2s (&n->id));
2955 disconnect_neighbour (n);
2958 try_transmission_to_peer (n);
2961 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2962 if (0 == delay.rel_value_us)
2964 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2965 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2966 GNUNET_i2s (&n->id));
2967 disconnect_neighbour (n);
2971 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2972 if (0 == delay.rel_value_us)
2974 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2975 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
2976 GNUNET_i2s (&n->id));
2977 disconnect_neighbour (n);
2981 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2982 if (0 == delay.rel_value_us)
2984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2985 "Switch failed, cleaning up alternative address\n");
2986 free_address (&n->alternative_address);
2987 set_state_and_timeout (n,
2988 GNUNET_TRANSPORT_PS_CONNECTED,
2989 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2991 try_transmission_to_peer (n);
2994 case GNUNET_TRANSPORT_PS_DISCONNECT:
2995 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2996 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2997 GNUNET_i2s (&n->id));
3000 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3001 /* how did we get here!? */
3005 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3006 "Unhandled state `%s'\n",
3007 GNUNET_TRANSPORT_ps2s (n->state));
3011 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3012 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3013 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3015 /* if we are *now* in one of the two states, we're sending
3016 keep alive messages, so we need to consider the keepalive
3017 delay, not just the connection timeout */
3018 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3021 if (NULL == n->task)
3022 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3029 * Send a ACK message to the neighbour to confirm that we
3032 * @param n neighbour to send the ACK to
3035 send_session_ack_message (struct NeighbourMapEntry *n)
3037 struct GNUNET_MessageHeader msg;
3039 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3040 "Sending ACK message to peer `%s'\n",
3041 GNUNET_i2s (&n->id));
3043 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3044 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3045 (void) send_with_session (n,
3047 sizeof (struct GNUNET_MessageHeader),
3049 GNUNET_TIME_UNIT_FOREVER_REL,
3056 * We received a 'SESSION_SYN_ACK' message from the other peer.
3057 * Consider switching to it.
3059 * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
3060 * @param peer identity of the peer to switch the address for
3061 * @param address address of the other peer, NULL if other peer
3063 * @param session session to use (or NULL)
3064 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3067 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3068 const struct GNUNET_HELLO_Address *address,
3069 struct GNUNET_ATS_Session *session)
3071 const struct TransportSynMessage *scm;
3072 struct GNUNET_TIME_Absolute ts;
3073 struct NeighbourMapEntry *n;
3075 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3077 GNUNET_break_op (0);
3078 return GNUNET_SYSERR;
3080 GNUNET_STATISTICS_update (GST_stats,
3082 ("# SYN_ACK messages received"),
3084 scm = (const struct TransportSynMessage *) message;
3085 GNUNET_break_op (ntohl (scm->reserved) == 0);
3086 if (NULL == (n = lookup_neighbour (&address->peer)))
3088 GNUNET_STATISTICS_update (GST_stats,
3090 ("# unexpected SYN_ACK messages (no peer)"),
3092 return GNUNET_SYSERR;
3094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3095 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3096 GNUNET_i2s (&address->peer),
3097 GNUNET_TRANSPORT_ps2s (n->state),
3098 print_ack_state (n->ack_state));
3099 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3102 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3105 return GNUNET_SYSERR;
3106 case GNUNET_TRANSPORT_PS_INIT_ATS:
3107 GNUNET_STATISTICS_update (GST_stats,
3108 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3112 case GNUNET_TRANSPORT_PS_SYN_SENT:
3113 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3115 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3116 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3119 set_state_and_timeout (n,
3120 GNUNET_TRANSPORT_PS_CONNECTED,
3121 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3122 set_primary_address (n,
3123 n->primary_address.address,
3124 n->primary_address.session,
3125 n->primary_address.bandwidth_in,
3126 n->primary_address.bandwidth_out);
3127 send_session_ack_message (n);
3129 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3130 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3131 GNUNET_STATISTICS_update (GST_stats,
3132 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3136 case GNUNET_TRANSPORT_PS_CONNECTED:
3137 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3138 send_session_ack_message (n);
3140 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3141 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3142 to give us a new address... */
3143 GNUNET_STATISTICS_update (GST_stats,
3144 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3148 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3149 /* Reconnecting with new address address worked; go back to connected! */
3150 set_state_and_timeout (n,
3151 GNUNET_TRANSPORT_PS_CONNECTED,
3152 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3153 send_session_ack_message (n);
3155 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3156 /* new address worked; adopt it and go back to connected! */
3157 set_state_and_timeout (n,
3158 GNUNET_TRANSPORT_PS_CONNECTED,
3159 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3160 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3162 /* Set primary addresses */
3163 set_primary_address (n,
3164 n->alternative_address.address,
3165 n->alternative_address.session,
3166 n->alternative_address.bandwidth_in,
3167 n->alternative_address.bandwidth_out);
3168 GNUNET_STATISTICS_update (GST_stats,
3169 gettext_noop ("# Successful attempts to switch addresses"),
3173 GNUNET_HELLO_address_free (n->alternative_address.address);
3174 memset (&n->alternative_address,
3176 sizeof (n->alternative_address));
3177 send_session_ack_message (n);
3179 case GNUNET_TRANSPORT_PS_DISCONNECT:
3180 GNUNET_STATISTICS_update (GST_stats,
3182 ("# unexpected SYN_ACK messages (disconnecting)"),
3184 return GNUNET_SYSERR;
3185 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3190 "Unhandled state `%s'\n",
3191 GNUNET_TRANSPORT_ps2s (n->state));
3193 return GNUNET_SYSERR;
3200 * A session was terminated. Take note; if needed, try to get
3201 * an alternative address from ATS.
3203 * @param peer identity of the peer where the session died
3204 * @param session session that is gone
3205 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3206 * this session was not in use
3209 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3210 struct GNUNET_ATS_Session *session)
3212 struct NeighbourMapEntry *n;
3214 if (NULL == (n = lookup_neighbour (peer)))
3215 return GNUNET_NO; /* can't affect us */
3216 if (session != n->primary_address.session)
3218 /* Free alternative address */
3219 if (session == n->alternative_address.session)
3221 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3222 set_state_and_timeout (n,
3223 GNUNET_TRANSPORT_PS_CONNECTED,
3225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3226 "Session died, cleaning up alternative address\n");
3227 free_address (&n->alternative_address);
3229 return GNUNET_NO; /* doesn't affect us further */
3232 n->expect_latency_response = GNUNET_NO;
3233 /* The session for neighbour's primary address died */
3236 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3240 case GNUNET_TRANSPORT_PS_INIT_ATS:
3244 case GNUNET_TRANSPORT_PS_SYN_SENT:
3245 /* The session used to send the SYN terminated:
3246 * this implies a connect error*/
3247 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3248 "Failed to send SYN in CONNECT_SENT with `%s' %p: session terminated\n",
3249 GST_plugins_a2s (n->primary_address.address),
3250 n->primary_address.session);
3252 /* Destroy the address since it cannot be used */
3253 unset_primary_address (n);
3254 set_state_and_timeout (n,
3255 GNUNET_TRANSPORT_PS_INIT_ATS,
3256 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3258 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3259 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3260 /* error on inbound session; free neighbour entirely */
3263 case GNUNET_TRANSPORT_PS_CONNECTED:
3264 /* Our primary connection died, try a fast reconnect */
3265 unset_primary_address (n);
3266 set_state_and_timeout (n,
3267 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3268 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3270 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3271 /* we don't have an address, how can it go down? */
3274 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3275 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3276 "Failed to send SYN in RECONNECT_SENT with `%s' %p: session terminated\n",
3277 GST_plugins_a2s (n->primary_address.address),
3278 n->primary_address.session);
3279 /* Destroy the address since it cannot be used */
3280 unset_primary_address (n);
3281 set_state_and_timeout (n,
3282 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3283 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3285 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3286 /* primary went down while we were waiting for SYN_ACK on secondary;
3287 secondary as primary */
3289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3290 "Connection `%s' %p to peer `%s' was terminated while switching, "
3291 "switching to alternative address `%s' %p\n",
3292 GST_plugins_a2s (n->primary_address.address),
3293 n->primary_address.session,
3295 GST_plugins_a2s (n->alternative_address.address),
3296 n->alternative_address.session);
3298 /* Destroy the inbound address since it cannot be used */
3299 free_address (&n->primary_address);
3300 n->primary_address = n->alternative_address;
3301 GNUNET_assert (GNUNET_YES ==
3302 GST_ats_is_known (n->primary_address.address,
3303 n->primary_address.session));
3304 memset (&n->alternative_address,
3306 sizeof (struct NeighbourAddress));
3307 set_state_and_timeout (n,
3308 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3309 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3311 case GNUNET_TRANSPORT_PS_DISCONNECT:
3312 unset_primary_address (n);
3314 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3315 /* neighbour was freed and plugins told to terminate session */
3318 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3319 "Unhandled state `%s'\n",
3320 GNUNET_TRANSPORT_ps2s (n->state));
3324 if (NULL != n->task)
3325 GNUNET_SCHEDULER_cancel (n->task);
3326 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3332 * We received a 'ACK' message from the other peer.
3333 * If we sent a 'SYN_ACK' last, this means we are now
3334 * connected. Otherwise, do nothing.
3336 * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
3337 * @param address address of the other peer
3338 * @param session session to use (or NULL)
3339 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3342 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3343 const struct GNUNET_HELLO_Address *address,
3344 struct GNUNET_ATS_Session *session)
3346 struct NeighbourMapEntry *n;
3348 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3350 GNUNET_break_op (0);
3351 return GNUNET_SYSERR;
3353 GNUNET_STATISTICS_update (GST_stats,
3354 gettext_noop ("# ACK messages received"),
3357 if (NULL == (n = lookup_neighbour (&address->peer)))
3359 GNUNET_break_op (0);
3360 return GNUNET_SYSERR;
3362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3363 "Received ACK for peer `%s' in state %s/%s\n",
3364 GNUNET_i2s (&address->peer),
3365 GNUNET_TRANSPORT_ps2s (n->state),
3366 print_ack_state (n->ack_state));
3368 /* Check if we are in a plausible state for having sent
3369 a SYN_ACK. If not, return, otherwise break.
3371 The remote peers sends a ACK as a response for a SYN_ACK
3375 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3376 now wait for the ACK to finally be connected
3377 - If we sent a SYN_ACK to this peer before */
3379 if ( ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3380 (ACK_SEND_ACK != n->ack_state) ) ||
3381 (NULL == n->primary_address.address) )
3383 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3384 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3385 GNUNET_i2s (&address->peer),
3386 GNUNET_TRANSPORT_ps2s (n->state),
3387 print_ack_state (n->ack_state));
3389 GNUNET_STATISTICS_update (GST_stats,
3390 gettext_noop ("# unexpected ACK messages"),
3395 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3397 /* We tried to switch addresses while being connect. We explicitly wait
3398 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3399 * so we do not want to set the address as in use! */
3402 set_state_and_timeout (n,
3403 GNUNET_TRANSPORT_PS_CONNECTED,
3404 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3406 if (NULL == n->primary_address.address) {
3408 * We are in state = PSY_SYN_RECV_ACK or ack_state = ACK_SEND_ACK, which
3409 * really means we did try (and succeed) to send a SYN and are waiting for
3411 * That suggests that the primary_address used to be non-NULL, but maybe it
3412 * got reset to NULL without the state being changed appropriately?
3418 /* Reset backoff for primary address */
3419 GST_ats_block_reset (n->primary_address.address,
3420 n->primary_address.session);
3426 * Test if we're connected to the given peer.
3428 * @param target peer to test
3429 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3432 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3434 return test_connected (lookup_neighbour (target));
3439 * Task to asynchronously run #free_neighbour().
3441 * @param cls the `struct NeighbourMapEntry` to free
3444 delayed_disconnect (void *cls)
3446 struct NeighbourMapEntry *n = cls;
3448 n->delayed_disconnect_task = NULL;
3449 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3450 "Disconnecting by request from peer %s\n",
3451 GNUNET_i2s (&n->id));
3457 * We received a quota message from the given peer,
3458 * validate and process.
3460 * @param peer sender of the message
3461 * @param msg the quota message
3464 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
3465 const struct GNUNET_MessageHeader *msg)
3467 struct NeighbourMapEntry *n;
3468 const struct GNUNET_ATS_SessionQuotaMessage *sqm;
3469 struct GNUNET_BANDWIDTH_Value32NBO last;
3471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3472 "Received QUOTA message from peer `%s'\n",
3474 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionQuotaMessage))
3476 GNUNET_break_op (0);
3477 GNUNET_STATISTICS_update (GST_stats,
3478 gettext_noop ("# quota messages ignored (malformed)"),
3483 GNUNET_STATISTICS_update (GST_stats,
3485 ("# QUOTA messages received"),
3487 sqm = (const struct GNUNET_ATS_SessionQuotaMessage *) msg;
3488 if (NULL == (n = lookup_neighbour (peer)))
3493 last = GNUNET_BANDWIDTH_value_max (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3494 GNUNET_BANDWIDTH_value_init (ntohl (sqm->quota)));
3495 if (last.value__ != n->neighbour_receive_quota.value__)
3497 n->neighbour_receive_quota = last;
3498 send_outbound_quota_to_clients (n);
3504 * We received a disconnect message from the given peer,
3505 * validate and process.
3507 * @param peer sender of the message
3508 * @param msg the disconnect message
3511 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3512 const struct GNUNET_MessageHeader *msg)
3514 struct NeighbourMapEntry *n;
3515 const struct GNUNET_ATS_SessionDisconnectMessage *sdm;
3517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3518 "Received DISCONNECT message from peer `%s'\n",
3520 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionDisconnectMessage))
3522 GNUNET_break_op (0);
3523 GNUNET_STATISTICS_update (GST_stats,
3525 ("# disconnect messages ignored (malformed)"),
3530 GNUNET_STATISTICS_update (GST_stats,
3532 ("# DISCONNECT messages received"),
3534 sdm = (const struct GNUNET_ATS_SessionDisconnectMessage *) msg;
3535 if (NULL == (n = lookup_neighbour (peer)))
3540 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3542 GNUNET_STATISTICS_update (GST_stats,
3543 gettext_noop ("# disconnect messages ignored (timestamp)"),
3548 if (0 != memcmp (peer,
3550 sizeof (struct GNUNET_PeerIdentity)))
3552 GNUNET_break_op (0);
3555 if (ntohl (sdm->purpose.size) !=
3556 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3557 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3558 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3561 "DISCONNECT message from peer `%s' has invalid size\n",
3563 GNUNET_break_op (0);
3567 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3573 "DISCONNECT message from peer `%s' cannot be verified \n",
3575 GNUNET_break_op (0);
3578 if (NULL == n->delayed_disconnect_task)
3580 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect,
3587 * Closure for the #neighbours_iterate() function.
3589 struct IteratorContext
3592 * Function to call on each connected neighbour.
3594 GST_NeighbourIterator cb;
3597 * Closure for @e cb.
3604 * Call the callback from the closure for each neighbour.
3606 * @param cls the `struct IteratorContext`
3607 * @param key the hash of the public key of the neighbour
3608 * @param value the `struct NeighbourMapEntry`
3609 * @return #GNUNET_OK (continue to iterate)
3612 neighbours_iterate (void *cls,
3613 const struct GNUNET_PeerIdentity *key,
3616 struct IteratorContext *ic = cls;
3617 struct NeighbourMapEntry *n = value;
3618 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3619 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3621 if (NULL != n->primary_address.address)
3623 bandwidth_in = n->primary_address.bandwidth_in;
3624 bandwidth_out = n->primary_address.bandwidth_out;
3628 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3629 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3633 n->primary_address.address,
3636 bandwidth_in, bandwidth_out);
3642 * Iterate over all connected neighbours.
3644 * @param cb function to call
3645 * @param cb_cls closure for @a cb
3648 GST_neighbours_iterate (GST_NeighbourIterator cb,
3651 struct IteratorContext ic;
3653 if (NULL == neighbours)
3654 return; /* can happen during shutdown */
3657 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3658 &neighbours_iterate,
3664 * If we have an active connection to the given target, it must be shutdown.
3666 * @param target peer to disconnect from
3669 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3671 struct NeighbourMapEntry *n;
3673 if (NULL == (n = lookup_neighbour (target)))
3674 return; /* not active */
3675 if (GNUNET_YES == test_connected (n))
3676 GNUNET_STATISTICS_update (GST_stats,
3677 gettext_noop ("# disconnected from peer upon explicit request"),
3680 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3681 "Forced disconnect from peer %s\n",
3682 GNUNET_i2s (target));
3683 disconnect_neighbour (n);
3688 * Obtain current address information for the given neighbour.
3691 * @return address currently used
3693 const struct GNUNET_HELLO_Address *
3694 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3696 struct NeighbourMapEntry *n;
3698 n = lookup_neighbour (peer);
3701 return n->primary_address.address;
3706 * Initialize the neighbours subsystem.
3708 * @param max_fds maximum number of fds to use
3711 GST_neighbours_start (unsigned int max_fds)
3713 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3715 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3716 &utilization_transmission,
3722 * Disconnect from the given neighbour.
3725 * @param key hash of neighbour's public key (not used)
3726 * @param value the `struct NeighbourMapEntry` of the neighbour
3727 * @return #GNUNET_OK (continue to iterate)
3730 disconnect_all_neighbours (void *cls,
3731 const struct GNUNET_PeerIdentity *key,
3734 struct NeighbourMapEntry *n = value;
3736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3737 "Disconnecting peer `%4s' during shutdown\n",
3738 GNUNET_i2s (&n->id));
3745 * Cleanup the neighbours subsystem.
3748 GST_neighbours_stop ()
3750 if (NULL == neighbours)
3752 if (NULL != util_transmission_tk)
3754 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3755 util_transmission_tk = NULL;
3757 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3758 &disconnect_all_neighbours,
3760 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3765 /* end of file gnunet-service-transport_neighbours.c */