2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 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 * Notify our clients that another peer connected to us.
521 * @param n the peer that connected
524 neighbours_connect_notification (struct NeighbourMapEntry *n)
526 size_t len = sizeof(struct ConnectInfoMessage);
527 char buf[len] GNUNET_ALIGN;
528 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
529 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
531 #if IGNORE_INBOUND_QUOTA
532 bandwidth_min = n->primary_address.bandwidth_out;
534 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
535 n->neighbour_receive_quota);
537 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
538 "We are now connected to peer `%s'\n",
539 GNUNET_i2s (&n->id));
540 connect_msg->header.size = htons (sizeof(buf));
541 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
542 connect_msg->id = n->id;
543 connect_msg->quota_in = n->primary_address.bandwidth_in;
544 connect_msg->quota_out = bandwidth_min;
545 GST_clients_broadcast (&connect_msg->header,
551 * Notify our clients (and manipulation) that a peer disconnected from
554 * @param n the peer that disconnected
557 neighbours_disconnect_notification (struct NeighbourMapEntry *n)
559 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
560 "Peer `%s' disconnected\n",
561 GNUNET_i2s (&n->id));
562 GST_manipulation_peer_disconnect (&n->id);
563 GST_clients_broadcast_disconnect (&n->id);
568 * Notify transport clients that a neighbour peer changed its active
571 * @param peer identity of the peer
572 * @param address address possibly NULL if peer is not connected
573 * @param state current state this peer is in
574 * @param state_timeout timeout for the current state of the peer
575 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
576 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
579 neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer,
580 const struct GNUNET_HELLO_Address *address,
581 enum GNUNET_TRANSPORT_PeerState state,
582 struct GNUNET_TIME_Absolute state_timeout,
583 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
584 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
586 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
587 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
589 GST_plugins_a2s (address),
590 GNUNET_TRANSPORT_ps2s (state),
591 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
592 /* FIXME: include bandwidth in notification! */
593 GST_clients_broadcast_peer_notification (peer,
601 * Lookup a neighbour entry in the neighbours hash map.
603 * @param pid identity of the peer to look up
604 * @return the entry, NULL if there is no existing record
606 static struct NeighbourMapEntry *
607 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
609 if (NULL == neighbours)
611 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
616 * Test if we're connected to the given peer.
618 * @param n neighbour entry of peer to test
619 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
622 test_connected (struct NeighbourMapEntry *n)
626 return GNUNET_TRANSPORT_is_connected (n->state);
631 * Send information about a new outbound quota to our clients.
632 * Note that the outbound quota is enforced client-side (i.e.
633 * in libgnunettransport).
635 * @param n affected peer
638 send_outbound_quota_to_clients (struct NeighbourMapEntry *n)
640 struct QuotaSetMessage q_msg;
641 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
643 if (! GNUNET_TRANSPORT_is_connected (n->state))
645 #if IGNORE_INBOUND_QUOTA
646 bandwidth_min = n->primary_address.bandwidth_out;
648 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
649 n->neighbour_receive_quota);
652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
653 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
654 ntohl (bandwidth_min.value__),
655 GNUNET_i2s (&n->id));
656 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
657 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
658 q_msg.quota = bandwidth_min;
660 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
665 * We don't need a given neighbour address any more.
666 * Release its resources and give appropriate notifications
667 * to ATS and other subsystems.
669 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
672 free_address (struct NeighbourAddress *na)
674 if (GNUNET_YES == na->ats_active)
675 GST_validation_set_address_use (na->address,
677 if (NULL != na->address)
679 GST_ats_block_address (na->address,
681 GNUNET_HELLO_address_free (na->address);
684 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
685 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
686 na->ats_active = GNUNET_NO;
687 na->keep_alive_nonce = 0;
693 * Master task run for every neighbour. Performs all of the time-related
694 * activities (keep alive, send next message, disconnect if idle, finish
695 * clean up after disconnect).
697 * @param cls the `struct NeighbourMapEntry` for which we are running
698 * @param tc scheduler context (unused)
701 master_task (void *cls,
702 const struct GNUNET_SCHEDULER_TaskContext *tc);
706 * Set net state and state timeout for this neighbour and notify monitoring
708 * @param n the respective neighbour
709 * @param s the new state
710 * @param timeout the new timeout
713 set_state_and_timeout (struct NeighbourMapEntry *n,
714 enum GNUNET_TRANSPORT_PeerState s,
715 struct GNUNET_TIME_Absolute timeout)
717 if (GNUNET_TRANSPORT_is_connected (s) &&
718 ! GNUNET_TRANSPORT_is_connected (n->state) )
720 neighbours_connect_notification (n);
721 GNUNET_STATISTICS_set (GST_stats,
722 gettext_noop ("# peers connected"),
723 ++neighbours_connected,
726 if (! GNUNET_TRANSPORT_is_connected (s) &&
727 GNUNET_TRANSPORT_is_connected (n->state) )
729 GNUNET_STATISTICS_set (GST_stats,
730 gettext_noop ("# peers connected"),
731 --neighbours_connected,
733 neighbours_disconnect_notification (n);
736 if ( (timeout.abs_value_us < n->timeout.abs_value_us) &&
739 /* new timeout is earlier, reschedule master task */
740 GNUNET_SCHEDULER_cancel (n->task);
741 n->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (timeout),
745 n->timeout = timeout;
746 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
747 "Neighbour `%s' changed state to %s with timeout %s\n",
749 GNUNET_TRANSPORT_ps2s(s),
750 GNUNET_STRINGS_absolute_time_to_string (timeout));
751 neighbours_changed_notification (&n->id,
752 n->primary_address.address,
755 n->primary_address.bandwidth_in,
756 n->primary_address.bandwidth_out);
761 * Initialize the alternative address of a neighbour
763 * @param n the neighbour
764 * @param address address of the other peer, NULL if other peer
766 * @param session session to use (or NULL, in which case an
767 * address must be setup)
768 * @param bandwidth_in inbound quota to be used when connection is up
769 * @param bandwidth_out outbound quota to be used when connection is up
772 set_alternative_address (struct NeighbourMapEntry *n,
773 const struct GNUNET_HELLO_Address *address,
774 struct GNUNET_ATS_Session *session,
775 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
776 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
778 struct GNUNET_TRANSPORT_PluginFunctions *papi;
780 if (NULL == (papi = GST_plugins_find (address->transport_name)))
785 if (session == n->alternative_address.session)
787 n->alternative_address.bandwidth_in = bandwidth_in;
788 n->alternative_address.bandwidth_out = bandwidth_out;
791 if (NULL != n->alternative_address.address)
793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
794 "Replacing existing alternative address with another one\n");
795 free_address (&n->alternative_address);
798 session = papi->get_session (papi->cls,
802 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
803 "Failed to obtain new session for peer `%s' and address '%s'\n",
804 GNUNET_i2s (&address->peer),
805 GST_plugins_a2s (address));
806 GNUNET_STATISTICS_update (GST_stats,
807 gettext_noop ("# session creation failed"),
812 GST_ats_new_session (address,
814 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
815 "Neighbour `%s' configured alternative address %s\n",
817 GST_plugins_a2s(address));
819 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
820 n->alternative_address.bandwidth_in = bandwidth_in;
821 n->alternative_address.bandwidth_out = bandwidth_out;
822 n->alternative_address.session = session;
823 n->alternative_address.ats_active = GNUNET_NO;
824 n->alternative_address.keep_alive_nonce = 0;
825 GNUNET_assert (GNUNET_YES ==
826 GST_ats_is_known (n->alternative_address.address,
827 n->alternative_address.session));
832 * Transmit a message using the current session of the given
835 * @param n entry for the recipient
836 * @param msgbuf buffer to transmit
837 * @param msgbuf_size number of bytes in @a msgbuf buffer
838 * @param priority transmission priority
839 * @param timeout transmission timeout
840 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
841 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
842 * @param cont continuation to call when finished (can be NULL)
843 * @param cont_cls closure for @a cont
844 * @return timeout (copy of @a timeout or a calculated one if
845 * @a use_keepalive_timeout is #GNUNET_YES.
847 static struct GNUNET_TIME_Relative
848 send_with_session (struct NeighbourMapEntry *n,
852 struct GNUNET_TIME_Relative timeout,
853 unsigned int use_keepalive_timeout,
854 GNUNET_TRANSPORT_TransmitContinuation cont,
857 struct GNUNET_TRANSPORT_PluginFunctions *papi;
858 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
860 GNUNET_assert (NULL != n->primary_address.session);
861 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
862 (-1 == papi->send (papi->cls,
863 n->primary_address.session,
867 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
868 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
869 papi->query_keepalive_factor (papi->cls))),
878 GST_neighbours_notify_data_sent (n->primary_address.address,
879 n->primary_address.session,
881 GNUNET_break (NULL != papi);
887 * Clear the primary address of a neighbour since this address is not
888 * valid anymore and notify monitoring about it
890 * @param n the neighbour
893 unset_primary_address (struct NeighbourMapEntry *n)
895 /* Notify monitoring about change */
896 if (NULL == n->primary_address.address)
898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
899 "Disabling primary address\n");
900 neighbours_changed_notification (&n->id,
901 n->primary_address.address,
904 GNUNET_BANDWIDTH_value_init (0),
905 GNUNET_BANDWIDTH_value_init (0));
906 free_address (&n->primary_address);
911 * Free a neighbour map entry.
913 * @param n entry to free
916 free_neighbour (struct NeighbourMapEntry *n)
918 struct MessageQueue *mq;
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Freeing neighbour state of peer `%s'\n",
922 GNUNET_i2s (&n->id));
923 n->is_active = NULL; /* always free'd by its own continuation! */
925 /* fail messages currently in the queue */
926 while (NULL != (mq = n->messages_head))
928 GNUNET_CONTAINER_DLL_remove (n->messages_head,
931 if (NULL != mq->cont)
932 mq->cont (mq->cont_cls,
934 mq->message_buf_size,
938 /* Mark peer as disconnected */
939 set_state_and_timeout (n,
940 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED,
941 GNUNET_TIME_UNIT_FOREVER_ABS);
942 /* free addresses and mark as unused */
943 unset_primary_address (n);
945 if (NULL != n->alternative_address.address)
947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
948 "Cleaning up alternative address\n");
949 free_address (&n->alternative_address);
951 GNUNET_assert (GNUNET_YES ==
952 GNUNET_CONTAINER_multipeermap_remove (neighbours,
955 /* Cancel address requests for this peer */
956 if (NULL != n->suggest_handle)
958 GNUNET_ATS_connectivity_suggest_cancel (n->suggest_handle);
959 n->suggest_handle = NULL;
962 /* Cancel the disconnect task */
963 if (NULL != n->delayed_disconnect_task)
965 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
966 n->delayed_disconnect_task = NULL;
969 /* Cancel the master task */
972 GNUNET_SCHEDULER_cancel (n->task);
975 /* free rest of memory */
981 * Function called when the 'DISCONNECT' message has been sent by the
982 * plugin. Frees the neighbour --- if the entry still exists.
985 * @param target identity of the neighbour that was disconnected
986 * @param result #GNUNET_OK if the disconnect got out successfully
987 * @param payload bytes payload
988 * @param physical bytes on wire
991 send_disconnect_cont (void *cls,
992 const struct GNUNET_PeerIdentity *target,
997 struct NeighbourMapEntry *n;
999 n = lookup_neighbour (target);
1001 return; /* already gone */
1002 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1003 return; /* have created a fresh entry since */
1004 if (NULL != n->task)
1005 GNUNET_SCHEDULER_cancel (n->task);
1006 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1011 * Transmit a DISCONNECT message to the other peer.
1013 * @param n neighbour to send DISCONNECT message.
1016 send_disconnect (struct NeighbourMapEntry *n)
1018 struct GNUNET_ATS_SessionDisconnectMessage disconnect_msg;
1020 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1021 "Sending DISCONNECT message to peer `%4s'\n",
1022 GNUNET_i2s (&n->id));
1023 disconnect_msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionDisconnectMessage));
1024 disconnect_msg.header.type =
1025 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1026 disconnect_msg.reserved = htonl (0);
1027 disconnect_msg.purpose.size =
1028 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1029 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1030 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1031 disconnect_msg.purpose.purpose =
1032 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1033 disconnect_msg.timestamp =
1034 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1035 disconnect_msg.public_key = GST_my_identity.public_key;
1036 GNUNET_assert (GNUNET_OK ==
1037 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1038 &disconnect_msg.purpose,
1039 &disconnect_msg.signature));
1041 (void) send_with_session (n,
1043 sizeof (disconnect_msg),
1045 GNUNET_TIME_UNIT_FOREVER_REL,
1047 &send_disconnect_cont,
1049 GNUNET_STATISTICS_update (GST_stats,
1050 gettext_noop ("# DISCONNECT messages sent"),
1057 * Disconnect from the given neighbour, clean up the record.
1059 * @param n neighbour to disconnect from
1062 disconnect_neighbour (struct NeighbourMapEntry *n)
1064 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1065 "Disconnecting from peer %s in state %s\n",
1066 GNUNET_i2s (&n->id),
1067 GNUNET_TRANSPORT_ps2s (n->state));
1068 /* depending on state, notify neighbour and/or upper layers of this peer
1072 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1073 case GNUNET_TRANSPORT_PS_INIT_ATS:
1074 /* other peer is completely unaware of us, no need to send DISCONNECT */
1077 case GNUNET_TRANSPORT_PS_SYN_SENT:
1078 send_disconnect (n);
1079 set_state_and_timeout (n,
1080 GNUNET_TRANSPORT_PS_DISCONNECT,
1081 GNUNET_TIME_UNIT_FOREVER_ABS);
1083 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1084 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1087 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1088 /* we DID ACK the other peer's request, must send DISCONNECT */
1089 send_disconnect (n);
1090 set_state_and_timeout (n,
1091 GNUNET_TRANSPORT_PS_DISCONNECT,
1092 GNUNET_TIME_UNIT_FOREVER_ABS);
1094 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1095 case GNUNET_TRANSPORT_PS_CONNECTED:
1096 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1097 /* we are currently connected, need to send disconnect and do
1098 internal notifications and update statistics */
1099 send_disconnect (n);
1100 set_state_and_timeout (n,
1101 GNUNET_TRANSPORT_PS_DISCONNECT,
1102 GNUNET_TIME_UNIT_FOREVER_ABS);
1104 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1105 /* Disconnecting while waiting for an ATS address to reconnect,
1106 * cannot send DISCONNECT */
1109 case GNUNET_TRANSPORT_PS_DISCONNECT:
1110 /* already disconnected, ignore */
1112 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1113 /* already cleaned up, how did we get here!? */
1117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1118 "Unhandled state `%s'\n",
1119 GNUNET_TRANSPORT_ps2s (n->state));
1123 /* schedule timeout to clean up */
1124 if (NULL != n->task)
1125 GNUNET_SCHEDULER_cancel (n->task);
1126 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1133 * Change the incoming quota for the given peer. Updates
1134 * our own receive rate and informs the neighbour about
1137 * @param n neighbour entry to change qutoa for
1138 * @param quota new quota
1141 set_incoming_quota (struct NeighbourMapEntry *n,
1142 struct GNUNET_BANDWIDTH_Value32NBO quota)
1144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1145 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
1146 ntohl (quota.value__), GNUNET_i2s (&n->id));
1147 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
1149 if (0 != ntohl (quota.value__))
1151 struct GNUNET_ATS_SessionQuotaMessage sqm;
1153 sqm.header.size = htons (sizeof (struct GNUNET_ATS_SessionQuotaMessage));
1154 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA);
1155 sqm.quota = quota.value__;
1156 (void) send_with_session (n,
1160 GNUNET_TIME_UNIT_FOREVER_REL,
1165 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1166 "Disconnecting peer `%4s' due to SET_QUOTA\n",
1167 GNUNET_i2s (&n->id));
1168 if (GNUNET_YES == test_connected (n))
1169 GNUNET_STATISTICS_update (GST_stats,
1170 gettext_noop ("# disconnects due to quota of 0"),
1172 disconnect_neighbour (n);
1177 * Initialize the primary address of a neighbour
1179 * @param n the neighbour
1180 * @param address address of the other peer, NULL if other peer
1182 * @param session session to use (or NULL, in which case an
1183 * address must be setup)
1184 * @param bandwidth_in inbound quota to be used when connection is up
1185 * @param bandwidth_out outbound quota to be used when connection is up
1188 set_primary_address (struct NeighbourMapEntry *n,
1189 const struct GNUNET_HELLO_Address *address,
1190 struct GNUNET_ATS_Session *session,
1191 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1192 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1194 if (session == n->primary_address.session)
1196 GST_validation_set_address_use (n->primary_address.address,
1198 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
1200 n->primary_address.bandwidth_in = bandwidth_in;
1201 set_incoming_quota (n,
1204 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
1206 n->primary_address.bandwidth_out = bandwidth_out;
1207 send_outbound_quota_to_clients (n);
1211 if ( (NULL != n->primary_address.address) &&
1212 (0 == GNUNET_HELLO_address_cmp (address,
1213 n->primary_address.address)) )
1218 if (NULL == session)
1221 GST_ats_block_address (address,
1225 if (NULL != n->primary_address.address)
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Replacing existing primary address with another one\n");
1229 free_address (&n->primary_address);
1231 n->primary_address.address = GNUNET_HELLO_address_copy (address);
1232 n->primary_address.bandwidth_in = bandwidth_in;
1233 n->primary_address.bandwidth_out = bandwidth_out;
1234 n->primary_address.session = session;
1235 n->primary_address.keep_alive_nonce = 0;
1236 GNUNET_assert (GNUNET_YES ==
1237 GST_ats_is_known (n->primary_address.address,
1238 n->primary_address.session));
1239 /* subsystems about address use */
1240 GST_validation_set_address_use (n->primary_address.address,
1242 set_incoming_quota (n,
1244 send_outbound_quota_to_clients (n);
1245 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1246 "Neighbour `%s' switched to address `%s'\n",
1247 GNUNET_i2s (&n->id),
1248 GST_plugins_a2s(address));
1250 neighbours_changed_notification (&n->id,
1251 n->primary_address.address,
1254 n->primary_address.bandwidth_in,
1255 n->primary_address.bandwidth_out);
1260 * We're done with our transmission attempt, continue processing.
1262 * @param cls the `struct MessageQueue` of the message
1263 * @param receiver intended receiver
1264 * @param success whether it worked or not
1265 * @param size_payload bytes payload sent
1266 * @param physical bytes sent on wire
1269 transmit_send_continuation (void *cls,
1270 const struct GNUNET_PeerIdentity *receiver,
1272 size_t size_payload,
1275 struct MessageQueue *mq = cls;
1276 struct NeighbourMapEntry *n;
1278 if (NULL == (n = lookup_neighbour (receiver)))
1280 if (NULL != mq->cont)
1281 mq->cont (mq->cont_cls,
1282 GNUNET_SYSERR /* not connected */,
1286 return; /* disconnect or other error while transmitting, can happen */
1288 if (n->is_active == mq)
1290 /* this is still "our" neighbour, remove us from its queue
1291 and allow it to send the next message now */
1292 n->is_active = NULL;
1293 if (NULL != n->task)
1294 GNUNET_SCHEDULER_cancel (n->task);
1295 n->task = GNUNET_SCHEDULER_add_now (&master_task,
1298 if (bytes_in_send_queue < mq->message_buf_size)
1300 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1301 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1302 bytes_in_send_queue,
1303 mq->message_buf_size,
1304 (GNUNET_OK == success) ? "OK" : "FAIL",
1310 GNUNET_break (size_payload == mq->message_buf_size);
1311 bytes_in_send_queue -= mq->message_buf_size;
1312 GNUNET_STATISTICS_set (GST_stats,
1313 gettext_noop ("# bytes in message queue for other peers"),
1314 bytes_in_send_queue,
1316 if (GNUNET_OK == success)
1317 GNUNET_STATISTICS_update (GST_stats,
1318 gettext_noop ("# messages transmitted to other peers"),
1322 GNUNET_STATISTICS_update (GST_stats,
1324 ("# transmission failures for messages to other peers"),
1326 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1327 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1328 GNUNET_i2s (receiver),
1329 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1330 mq->message_buf_size,
1331 (success == GNUNET_OK) ? "success" : "FAILURE");
1332 if (NULL != mq->cont)
1333 mq->cont (mq->cont_cls,
1342 * Check the message list for the given neighbour and if we can
1343 * send a message, do so. This function should only be called
1344 * if the connection is at least generally ready for transmission.
1345 * While we will only send one message at a time, no bandwidth
1346 * quota management is performed here. If a message was given to
1347 * the plugin, the continuation will automatically re-schedule
1348 * the 'master' task once the next message might be transmitted.
1350 * @param n target peer for which to transmit
1353 try_transmission_to_peer (struct NeighbourMapEntry *n)
1355 struct MessageQueue *mq;
1356 struct GNUNET_TIME_Relative timeout;
1358 if (NULL == n->primary_address.address)
1360 /* no address, why are we here? */
1364 if ((0 == n->primary_address.address->address_length) &&
1365 (NULL == n->primary_address.session))
1367 /* no address, why are we here? */
1371 if (NULL != n->is_active)
1373 /* transmission already pending */
1377 /* timeout messages from the queue that are past their due date */
1378 while (NULL != (mq = n->messages_head))
1380 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1381 if (timeout.rel_value_us > 0)
1383 GNUNET_STATISTICS_update (GST_stats,
1384 gettext_noop ("# messages timed out while in transport queue"),
1387 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1391 transmit_send_continuation (mq,
1394 mq->message_buf_size,
1398 return; /* no more messages */
1399 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 "Giving message with %u bytes to plugin session %p\n",
1406 mq->message_buf_size,
1407 n->primary_address.session);
1408 (void) send_with_session (n,
1410 mq->message_buf_size,
1414 &transmit_send_continuation,
1420 * Send keepalive message to the neighbour. Must only be called
1421 * if we are on 'connected' state or while trying to switch addresses.
1422 * Will internally determine if a keepalive is truly needed (so can
1423 * always be called).
1425 * @param n neighbour that went idle and needs a keepalive
1428 send_keepalive (struct NeighbourMapEntry *n)
1430 struct GNUNET_ATS_SessionKeepAliveMessage m;
1431 struct GNUNET_TIME_Relative timeout;
1434 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1435 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state));
1436 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1437 return; /* no keepalive needed at this time */
1439 nonce = 0; /* 0 indicates 'not set' */
1441 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1445 "Sending KEEPALIVE to peer `%s' with nonce %u\n",
1446 GNUNET_i2s (&n->id),
1448 m.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1449 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1450 m.nonce = htonl (nonce);
1452 timeout = send_with_session (n,
1455 UINT32_MAX /* priority */,
1456 GNUNET_TIME_UNIT_FOREVER_REL,
1459 GNUNET_STATISTICS_update (GST_stats,
1460 gettext_noop ("# KEEPALIVES sent"),
1463 n->primary_address.keep_alive_nonce = nonce;
1464 n->expect_latency_response = GNUNET_YES;
1465 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1466 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1471 * Keep the connection to the given neighbour alive longer,
1472 * we received a KEEPALIVE (or equivalent); send a response.
1474 * @param neighbour neighbour to keep alive (by sending keep alive response)
1475 * @param m the keep alive message containing the nonce to respond to
1478 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1479 const struct GNUNET_MessageHeader *m)
1481 struct NeighbourMapEntry *n;
1482 const struct GNUNET_ATS_SessionKeepAliveMessage *msg_in;
1483 struct GNUNET_ATS_SessionKeepAliveMessage msg;
1485 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1487 GNUNET_break_op (0);
1491 msg_in = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1492 if (NULL == (n = lookup_neighbour (neighbour)))
1494 GNUNET_STATISTICS_update (GST_stats,
1496 ("# KEEPALIVE messages discarded (peer unknown)"),
1500 if (NULL == n->primary_address.session)
1502 GNUNET_STATISTICS_update (GST_stats,
1504 ("# KEEPALIVE messages discarded (no session)"),
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "Received KEEPALIVE request from peer `%s' with nonce %u\n",
1511 GNUNET_i2s (&n->id),
1512 ntohl (msg_in->nonce));
1513 GNUNET_STATISTICS_update (GST_stats,
1514 gettext_noop ("# KEEPALIVES received in good order"),
1518 /* send reply to allow neighbour to measure latency */
1519 msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1520 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1521 msg.nonce = msg_in->nonce;
1522 (void) send_with_session (n,
1524 sizeof (struct GNUNET_ATS_SessionKeepAliveMessage),
1525 UINT32_MAX /* priority */,
1526 GNUNET_TIME_UNIT_FOREVER_REL,
1533 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1534 * latency to this peer. Pass the updated information (existing ats
1535 * plus calculated latency) to ATS.
1537 * @param neighbour neighbour to keep alive
1538 * @param m the message containing the keep alive response
1541 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1542 const struct GNUNET_MessageHeader *m)
1544 struct NeighbourMapEntry *n;
1545 const struct GNUNET_ATS_SessionKeepAliveMessage *msg;
1546 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1547 struct GNUNET_TIME_Relative latency;
1549 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1551 GNUNET_break_op (0);
1555 msg = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1556 if (NULL == (n = lookup_neighbour (neighbour)))
1558 GNUNET_STATISTICS_update (GST_stats,
1559 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not connected)"),
1564 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1565 (GNUNET_YES != n->expect_latency_response) )
1567 GNUNET_STATISTICS_update (GST_stats,
1568 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not expected)"),
1573 if (NULL == n->primary_address.address)
1575 GNUNET_STATISTICS_update (GST_stats,
1576 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (address changed)"),
1581 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1583 if (0 == n->primary_address.keep_alive_nonce)
1584 GNUNET_STATISTICS_update (GST_stats,
1585 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (no nonce)"),
1589 GNUNET_STATISTICS_update (GST_stats,
1590 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (bad nonce)"),
1595 GNUNET_STATISTICS_update (GST_stats,
1596 gettext_noop ("# KEEPALIVE_RESPONSEs received (OK)"),
1601 /* Update session timeout here */
1602 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1605 "Updating session for peer `%s' for session %p\n",
1606 GNUNET_i2s (&n->id),
1607 n->primary_address.session);
1608 papi->update_session_timeout (papi->cls,
1610 n->primary_address.session);
1617 n->primary_address.keep_alive_nonce = 0;
1618 n->expect_latency_response = GNUNET_NO;
1619 set_state_and_timeout (n,
1621 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1623 latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1625 "Received KEEPALIVE_RESPONSE from peer `%s', latency is %s\n",
1626 GNUNET_i2s (&n->id),
1627 GNUNET_STRINGS_relative_time_to_string (latency,
1629 GST_ats_update_delay (n->primary_address.address,
1630 GNUNET_TIME_relative_divide (latency,
1636 * We have received a message from the given sender. How long should
1637 * we delay before receiving more? (Also used to keep the peer marked
1640 * @param sender sender of the message
1641 * @param size size of the message
1642 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1643 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1644 * #GNUNET_SYSERR if the connection is not fully up yet
1645 * @return how long to wait before reading more from this sender
1647 struct GNUNET_TIME_Relative
1648 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
1652 struct NeighbourMapEntry *n;
1653 struct GNUNET_TIME_Relative ret;
1655 if (NULL == neighbours)
1657 *do_forward = GNUNET_NO;
1658 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1660 if (NULL == (n = lookup_neighbour (sender)))
1662 GNUNET_STATISTICS_update (GST_stats,
1663 gettext_noop ("# messages discarded due to lack of neighbour record"),
1666 *do_forward = GNUNET_NO;
1667 return GNUNET_TIME_UNIT_ZERO;
1669 if (! test_connected (n))
1671 *do_forward = GNUNET_SYSERR;
1672 return GNUNET_TIME_UNIT_ZERO;
1674 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1676 n->quota_violation_count++;
1677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1678 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1679 n->in_tracker.available_bytes_per_s__,
1680 n->quota_violation_count);
1681 /* Discount 32k per violation */
1682 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1686 if (n->quota_violation_count > 0)
1688 /* try to add 32k back */
1689 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1690 n->quota_violation_count--;
1693 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1695 GNUNET_STATISTICS_update (GST_stats,
1697 ("# bandwidth quota violations by other peers"),
1699 *do_forward = GNUNET_NO;
1700 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1702 *do_forward = GNUNET_YES;
1703 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1704 if (ret.rel_value_us > 0)
1706 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1707 "Throttling read (%lld bytes excess at %u b/s), waiting %s before reading more.\n",
1708 (long long) n->in_tracker.consumption_since_last_update__,
1709 (unsigned int) n->in_tracker.available_bytes_per_s__,
1710 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1711 GNUNET_STATISTICS_update (GST_stats,
1712 gettext_noop ("# ms throttling suggested"),
1713 (int64_t) ret.rel_value_us / 1000LL,
1721 * Transmit a message to the given target using the active connection.
1723 * @param target destination
1724 * @param msg message to send
1725 * @param msg_size number of bytes in msg
1726 * @param timeout when to fail with timeout
1727 * @param cont function to call when done
1728 * @param cont_cls closure for @a cont
1731 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
1734 struct GNUNET_TIME_Relative timeout,
1735 GST_NeighbourSendContinuation cont,
1738 struct NeighbourMapEntry *n;
1739 struct MessageQueue *mq;
1741 /* All ove these cases should never happen; they are all API violations.
1742 But we check anyway, just to be sure. */
1743 if (NULL == (n = lookup_neighbour (target)))
1753 if (GNUNET_YES != test_connected (n))
1763 bytes_in_send_queue += msg_size;
1764 GNUNET_STATISTICS_set (GST_stats,
1766 ("# bytes in message queue for other peers"),
1767 bytes_in_send_queue, GNUNET_NO);
1768 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1770 mq->cont_cls = cont_cls;
1771 memcpy (&mq[1], msg, msg_size);
1772 mq->message_buf = (const char *) &mq[1];
1773 mq->message_buf_size = msg_size;
1774 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1777 "Enqueueing %u bytes to send to peer %s\n",
1779 GNUNET_i2s (target));
1780 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
1783 if (NULL != n->task)
1784 GNUNET_SCHEDULER_cancel (n->task);
1785 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1790 * Continuation called from our attempt to transmitted our
1791 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN to the specified @a
1792 * target. Continue processing based on the @a result. Specifically,
1793 * if we failed to transmit, discard the address we used.
1796 * @param target which peer received the transmission
1797 * @param result #GNUNET_OK if sending worked
1798 * @param size_payload how many bytes of payload were sent (ignored)
1799 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1802 send_session_syn_cont (void *cls,
1803 const struct GNUNET_PeerIdentity *target,
1805 size_t size_payload,
1806 size_t size_on_wire)
1808 struct NeighbourMapEntry *n;
1810 n = lookup_neighbour (target);
1813 /* SYN continuation was called after neighbor was freed,
1814 * for example due to a time out for the state or the session
1815 * used was already terminated: nothing to do here... */
1819 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1820 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1821 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1823 /* SYN continuation was called after neighbor changed state,
1824 * for example due to a time out for the state or the session
1825 * used was already terminated: nothing to do here... */
1828 if (GNUNET_OK == result)
1831 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1832 _("Failed to send SYN message to peer `%s'\n"),
1833 GNUNET_i2s (target));
1835 case GNUNET_TRANSPORT_PS_SYN_SENT:
1836 /* Remove address and request an additional one */
1837 unset_primary_address (n);
1838 set_state_and_timeout (n,
1839 GNUNET_TRANSPORT_PS_INIT_ATS,
1840 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1842 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1843 /* Remove address and request an additional one */
1844 unset_primary_address (n);
1845 set_state_and_timeout (n,
1846 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1847 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1849 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1850 /* Remove address and request and go back to primary address */
1851 GNUNET_STATISTICS_update (GST_stats,
1852 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN CONT)"),
1855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1856 "Switch failed, cleaning up alternative address\n");
1857 free_address (&n->alternative_address);
1858 set_state_and_timeout (n,
1859 GNUNET_TRANSPORT_PS_CONNECTED,
1860 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1863 disconnect_neighbour (n);
1870 * Send a SYN message via the given address.
1872 * @param na address to use
1875 send_syn (struct NeighbourAddress *na)
1877 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1878 struct TransportSynMessage connect_msg;
1879 struct NeighbourMapEntry *n;
1881 GNUNET_assert (NULL != na->session);
1882 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1883 "Sending SYN message to peer `%s' at %s\n",
1884 GNUNET_i2s (&na->address->peer),
1885 GST_plugins_a2s (na->address));
1887 papi = GST_plugins_find (na->address->transport_name);
1888 GNUNET_assert (NULL != papi);
1889 GNUNET_STATISTICS_update (GST_stats,
1891 ("# SYN messages sent"),
1893 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1894 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1895 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1896 connect_msg.reserved = htonl (0);
1897 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1899 papi->send (papi->cls,
1901 (const char *) &connect_msg,
1902 sizeof (struct TransportSynMessage),
1904 SETUP_CONNECTION_TIMEOUT,
1905 &send_session_syn_cont, NULL))
1907 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1908 _("Failed to transmit SYN message to %s\n"),
1909 GST_plugins_a2s (na->address));
1910 n = lookup_neighbour (&na->address->peer);
1917 case GNUNET_TRANSPORT_PS_SYN_SENT:
1918 /* Remove address and request and additional one */
1919 GNUNET_assert (na == &n->primary_address);
1920 unset_primary_address (n);
1921 set_state_and_timeout (n,
1922 GNUNET_TRANSPORT_PS_INIT_ATS,
1923 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1924 /* Hard failure to send the SYN message with this address:
1925 Destroy address and session */
1927 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1928 /* Remove address and request an additional one */
1929 GNUNET_assert (na == &n->primary_address);
1930 unset_primary_address (n);
1931 set_state_and_timeout (n,
1932 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1933 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1935 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1936 GNUNET_assert (na == &n->alternative_address);
1937 GNUNET_STATISTICS_update (GST_stats,
1938 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN)"),
1941 /* Remove address and request an additional one */
1942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1943 "Switch failed, cleaning up alternative address\n");
1944 free_address (&n->alternative_address);
1945 set_state_and_timeout (n,
1946 GNUNET_TRANSPORT_PS_CONNECTED,
1947 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1951 disconnect_neighbour (n);
1956 GST_neighbours_notify_data_sent (na->address,
1958 sizeof (struct TransportSynMessage));
1963 * Continuation called from our attempt to transmitted our
1964 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK to the specified @a
1965 * target. Continue processing based on the @a result. Specifically,
1966 * if we failed to transmit, discard the address we used.
1969 * @param target which peer received the transmission
1970 * @param result #GNUNET_OK if sending worked
1971 * @param size_payload how many bytes of payload were sent (ignored)
1972 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1975 send_session_syn_ack_cont (void *cls,
1976 const struct GNUNET_PeerIdentity *target,
1978 size_t size_payload,
1979 size_t size_on_wire)
1981 struct NeighbourMapEntry *n;
1983 n = lookup_neighbour (target);
1986 /* SYN_ACK continuation was called after neighbor was freed,
1987 * for example due to a time out for the state or the session
1988 * used was already terminated: nothing to do here... */
1992 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1994 /* SYN_ACK continuation was called after neighbor changed state,
1995 * for example due to a time out for the state or the session
1996 * used was already terminated: nothing to do here... */
1999 if (GNUNET_OK == result)
2002 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2003 _("Failed to send SYN_ACK message to peer `%s' using address `%s'\n"),
2004 GNUNET_i2s (target),
2005 GST_plugins_a2s (n->primary_address.address));
2007 /* Remove address and request and additional one */
2008 /* FIXME: what if the neighbour's primary address
2009 changed in the meantime? Might want to instead
2010 pass "something" around in closure to be sure. */
2011 unset_primary_address (n);
2012 n->ack_state = ACK_SEND_SYN_ACK;
2013 set_state_and_timeout (n,
2014 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2015 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2020 * Send a SYN_ACK message via the given address.
2022 * @param na address and session to use
2023 * @param timestamp timestamp to use for the ACK message
2024 * @return #GNUNET_SYSERR if sending immediately failed, #GNUNET_OK otherwise
2027 send_syn_ack_message (struct NeighbourAddress *na,
2028 struct GNUNET_TIME_Absolute timestamp)
2030 const struct GNUNET_HELLO_Address *address = na->address;
2031 struct GNUNET_ATS_Session *session = na->session;
2032 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2033 struct TransportSynMessage connect_msg;
2034 struct NeighbourMapEntry *n;
2036 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2037 "Sending SYN_ACK to peer `%s'\n",
2038 GNUNET_i2s (&address->peer));
2040 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2045 if (NULL == session)
2046 session = papi->get_session (papi->cls,
2048 if (NULL == session)
2053 GST_ats_new_session (address,
2055 GNUNET_STATISTICS_update (GST_stats,
2057 ("# SYN_ACK messages sent"),
2059 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
2060 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
2061 connect_msg.reserved = htonl (0);
2062 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
2064 if (GNUNET_SYSERR ==
2065 papi->send (papi->cls,
2067 (const char *) &connect_msg,
2068 sizeof (struct TransportSynMessage),
2070 GNUNET_TIME_UNIT_FOREVER_REL,
2071 &send_session_syn_ack_cont, NULL))
2073 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2074 _("Failed to transmit SYN_ACK message to %s\n"),
2075 GST_plugins_a2s (address));
2077 n = lookup_neighbour (&address->peer);
2083 /* Remove address and request and additional one */
2084 unset_primary_address (n);
2085 n->ack_state = ACK_SEND_SYN_ACK;
2086 set_state_and_timeout (n,
2087 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2088 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2095 * Function called by the bandwidth tracker for a peer whenever
2096 * the tracker's state changed such that we need to recalculate
2097 * the delay for flow control. We calculate the latest delay
2098 * and inform the plugin (if applicable).
2100 * @param cls the `struct NeighbourMapEntry` to update calculations for
2103 inbound_bw_tracker_update (void *cls)
2105 struct NeighbourMapEntry *n = cls;
2106 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2107 struct GNUNET_TIME_Relative delay;
2110 if (NULL == n->primary_address.address)
2111 return; /* not active, ignore */
2112 papi = GST_plugins_find (n->primary_address.address->transport_name);
2113 GNUNET_assert (NULL != papi);
2114 if (NULL == papi->update_inbound_delay)
2116 delay = GST_neighbours_calculate_receive_delay (&n->id,
2119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2120 "New inbound delay for peer `%s' is %llu ms\n",
2121 GNUNET_i2s (&n->id),
2122 delay.rel_value_us / 1000);
2123 papi->update_inbound_delay (papi->cls,
2125 n->primary_address.session,
2131 * Create a fresh entry in the neighbour map for the given peer
2133 * @param peer peer to create an entry for
2134 * @return new neighbour map entry
2136 static struct NeighbourMapEntry *
2137 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2139 struct NeighbourMapEntry *n;
2141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2142 "Creating new neighbour entry for `%s'\n",
2145 memcmp (&GST_my_identity,
2147 sizeof (struct GNUNET_PeerIdentity)));
2148 n = GNUNET_new (struct NeighbourMapEntry);
2150 n->ack_state = ACK_UNDEFINED;
2151 n->last_util_transmission = GNUNET_TIME_absolute_get();
2152 n->neighbour_receive_quota = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2153 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2154 &inbound_bw_tracker_update,
2156 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2157 MAX_BANDWIDTH_CARRY_S);
2158 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2159 set_state_and_timeout (n,
2160 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2161 GNUNET_TIME_UNIT_FOREVER_ABS);
2162 GNUNET_assert (GNUNET_OK ==
2163 GNUNET_CONTAINER_multipeermap_put (neighbours,
2166 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2167 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2176 * Entry in a DLL we use to keep track of pending blacklist checks.
2178 struct BlacklistCheckSwitchContext
2183 struct BlacklistCheckSwitchContext *prev;
2188 struct BlacklistCheckSwitchContext *next;
2191 * Handle to the blacklist check we are performing.
2193 struct GST_BlacklistCheck *blc;
2196 * Inbound bandwidth that was assigned to @e address.
2198 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2201 * Outbound bandwidth that was assigned to @e address.
2203 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2208 * We received a 'SYN' message from the other peer.
2209 * Consider switching to it.
2211 * @param message possibly a `struct TransportSynMessage` (check format)
2212 * @param peer identity of the peer to switch the address for
2213 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2216 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2217 const struct GNUNET_PeerIdentity *peer)
2219 const struct TransportSynMessage *scm;
2220 struct NeighbourMapEntry *n;
2221 struct GNUNET_TIME_Absolute ts;
2223 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2225 GNUNET_break_op (0);
2226 return GNUNET_SYSERR;
2228 GNUNET_STATISTICS_update (GST_stats,
2230 ("# SYN messages received"),
2232 if (NULL == neighbours)
2234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2235 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2237 return GNUNET_OK; /* we're shutting down */
2239 scm = (const struct TransportSynMessage *) message;
2240 GNUNET_break_op (0 == ntohl (scm->reserved));
2241 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2242 n = lookup_neighbour (peer);
2245 /* This is a new neighbour and set to not connected */
2246 n = setup_neighbour (peer);
2249 /* Remember this SYN message in neighbour */
2250 n->ack_state = ACK_SEND_SYN_ACK;
2251 n->connect_ack_timestamp = ts;
2253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2254 "Received SYN for peer `%s' in state %s/%s\n",
2256 GNUNET_TRANSPORT_ps2s (n->state),
2257 print_ack_state (n->ack_state));
2261 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2262 /* Request an address from ATS to send SYN_ACK to this peer */
2263 set_state_and_timeout (n,
2264 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2265 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2267 case GNUNET_TRANSPORT_PS_INIT_ATS:
2268 /* SYN message takes priority over us asking ATS for address:
2269 * Wait for ATS to suggest an address and send SYN_ACK */
2270 set_state_and_timeout (n,
2271 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2272 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2274 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2275 /* We already wait for an address to send an SYN_ACK */
2277 case GNUNET_TRANSPORT_PS_SYN_SENT:
2278 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2279 /* Send ACK immediately */
2280 n->ack_state = ACK_SEND_ACK;
2281 send_syn_ack_message (&n->primary_address,
2284 case GNUNET_TRANSPORT_PS_CONNECTED:
2285 /* we are already connected and can thus send the ACK immediately */
2286 GNUNET_assert (NULL != n->primary_address.address);
2287 GNUNET_assert (NULL != n->primary_address.session);
2288 n->ack_state = ACK_SEND_ACK;
2289 send_syn_ack_message (&n->primary_address,
2292 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2293 /* We wait for ATS address suggestion */
2295 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2296 /* We received a SYN message while waiting for a SYN_ACK in fast
2297 * reconnect. Send SYN_ACK immediately */
2298 n->ack_state = ACK_SEND_ACK;
2299 send_syn_ack_message (&n->primary_address,
2300 n->connect_ack_timestamp);
2302 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2303 /* We are already connected and can thus send the ACK immediately;
2304 still, it can never hurt to have an alternative address, so also
2305 tell ATS about it */
2306 GNUNET_assert (NULL != n->primary_address.address);
2307 GNUNET_assert (NULL != n->primary_address.session);
2308 n->ack_state = ACK_SEND_ACK;
2309 send_syn_ack_message (&n->primary_address,
2312 case GNUNET_TRANSPORT_PS_DISCONNECT:
2313 /* Get rid of remains and re-try */
2315 n = setup_neighbour (peer);
2316 /* Remember the SYN time stamp for ACK message */
2317 n->ack_state = ACK_SEND_SYN_ACK;
2318 n->connect_ack_timestamp = ts;
2319 /* Request an address for the peer */
2320 set_state_and_timeout (n,
2321 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2322 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2324 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2325 /* should not be possible */
2329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2330 "Unhandled state `%s'\n",
2331 GNUNET_TRANSPORT_ps2s (n->state));
2333 return GNUNET_SYSERR;
2340 * Check if the given @a address is the same that we are already
2341 * using for the respective neighbour. If so, update the bandwidth
2342 * assignment and possibly the session and return #GNUNET_OK.
2343 * If the new address is different from what the neighbour is
2344 * using right now, return #GNUNET_NO.
2346 * @param address address of the other peer,
2347 * @param session session to use or NULL if transport should initiate a session
2348 * @param bandwidth_in inbound quota to be used when connection is up,
2349 * 0 to disconnect from peer
2350 * @param bandwidth_out outbound quota to be used when connection is up,
2351 * 0 to disconnect from peer
2352 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2353 * #GNUNET_NO if more extensive changes are required (address changed)
2356 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2357 struct GNUNET_ATS_Session *session,
2358 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2359 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2361 struct NeighbourMapEntry *n;
2363 n = lookup_neighbour (&address->peer);
2365 (NULL == n->primary_address.address) ||
2366 (0 != GNUNET_HELLO_address_cmp (address,
2367 n->primary_address.address)) )
2369 /* We are not really switching addresses, but merely adjusting
2370 session and/or bandwidth, can do fast ATS update! */
2371 if (session != n->primary_address.session)
2373 /* switch to a different session, but keeping same address; could
2374 happen if there is a 2nd inbound connection */
2375 n->primary_address.session = session;
2376 GNUNET_assert (GNUNET_YES ==
2377 GST_ats_is_known (n->primary_address.address,
2378 n->primary_address.session));
2380 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
2382 n->primary_address.bandwidth_in = bandwidth_in;
2383 set_incoming_quota (n,
2386 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
2388 n->primary_address.bandwidth_out = bandwidth_out;
2389 send_outbound_quota_to_clients (n);
2396 * We've been asked to switch addresses, and just now got the result
2397 * from the blacklist check to see if this is allowed.
2399 * @param cls the `struct BlacklistCheckSwitchContext` with
2400 * the information about the future address
2401 * @param peer the peer we may switch addresses on
2402 * @param address address associated with the request
2403 * @param session session associated with the request
2404 * @param result #GNUNET_OK if the connection is allowed,
2405 * #GNUNET_NO if not,
2406 * #GNUNET_SYSERR if operation was aborted
2409 switch_address_bl_check_cont (void *cls,
2410 const struct GNUNET_PeerIdentity *peer,
2411 const struct GNUNET_HELLO_Address *address,
2412 struct GNUNET_ATS_Session *session,
2415 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2416 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2417 struct NeighbourMapEntry *n;
2419 if (GNUNET_SYSERR == result)
2422 papi = GST_plugins_find (address->transport_name);
2423 GNUNET_assert (NULL != papi);
2425 if (GNUNET_NO == result)
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2429 GST_plugins_a2s (address),
2432 GNUNET_STATISTICS_update (GST_stats,
2433 "# ATS suggestions ignored (blacklist denied)",
2436 if (NULL != session)
2437 papi->disconnect_session (papi->cls,
2440 GNUNET_HELLO_address_check_option (address,
2441 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2442 GST_ats_block_address (address,
2448 if (NULL == session)
2450 /* need to create a session, ATS only gave us an address */
2451 session = papi->get_session (papi->cls,
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2454 "Obtained new session for peer `%s' and address '%s': %p\n",
2455 GNUNET_i2s (&address->peer),
2456 GST_plugins_a2s (address),
2458 if (NULL != session)
2459 GST_ats_new_session (address,
2462 if (NULL == session)
2464 /* session creation failed, bad!, fail! */
2465 GNUNET_STATISTICS_update (GST_stats,
2466 "# ATS suggestions ignored (failed to create session)",
2469 /* No session could be obtained, remove blacklist check and clean up */
2470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2471 "Failed to obtain new session for peer `%s' and address '%s'\n",
2472 GNUNET_i2s (&address->peer),
2473 GST_plugins_a2s (address));
2474 GST_ats_block_address (address,
2479 /* We did this check already before going into blacklist, but
2480 it is theoretically possible that the situation changed in
2481 the meantime, hence we check again here */
2483 try_run_fast_ats_update (address,
2485 blc_ctx->bandwidth_in,
2486 blc_ctx->bandwidth_out))
2487 goto cleanup; /* was just a minor update, we're done */
2489 /* check if we also need to setup the neighbour entry */
2490 if (NULL == (n = lookup_neighbour (peer)))
2492 n = setup_neighbour (peer);
2493 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2496 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2497 "Peer `%s' switches to address `%s'\n",
2498 GNUNET_i2s (&address->peer),
2499 GST_plugins_a2s (address));
2503 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2505 GST_ats_block_address (address,
2509 case GNUNET_TRANSPORT_PS_INIT_ATS:
2510 /* We requested an address and ATS suggests one:
2511 * set primary address and send SYN message*/
2512 set_primary_address (n,
2515 blc_ctx->bandwidth_in,
2516 blc_ctx->bandwidth_out);
2517 if (ACK_SEND_SYN_ACK == n->ack_state)
2519 /* Send pending SYN_ACK message */
2520 n->ack_state = ACK_SEND_ACK;
2521 send_syn_ack_message (&n->primary_address,
2522 n->connect_ack_timestamp);
2524 set_state_and_timeout (n,
2525 GNUNET_TRANSPORT_PS_SYN_SENT,
2526 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2527 send_syn (&n->primary_address);
2529 case GNUNET_TRANSPORT_PS_SYN_SENT:
2530 /* ATS suggested a new address while waiting for an SYN_ACK:
2531 * Switch and send new SYN */
2532 /* ATS suggests a different address, switch again */
2533 set_primary_address (n,
2536 blc_ctx->bandwidth_in,
2537 blc_ctx->bandwidth_out);
2538 if (ACK_SEND_SYN_ACK == n->ack_state)
2540 /* Send pending SYN_ACK message */
2541 n->ack_state = ACK_SEND_ACK;
2542 send_syn_ack_message (&n->primary_address,
2543 n->connect_ack_timestamp);
2545 set_state_and_timeout (n,
2546 GNUNET_TRANSPORT_PS_SYN_SENT,
2547 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2548 send_syn (&n->primary_address);
2550 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2551 /* We requested an address and ATS suggests one:
2552 * set primary address and send SYN_ACK message*/
2553 set_primary_address (n,
2556 blc_ctx->bandwidth_in,
2557 blc_ctx->bandwidth_out);
2558 /* Send an ACK message as a response to the SYN msg */
2559 set_state_and_timeout (n,
2560 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2561 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2562 send_syn_ack_message (&n->primary_address,
2563 n->connect_ack_timestamp);
2564 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2565 (ACK_UNDEFINED == n->ack_state) )
2566 n->ack_state = ACK_SEND_ACK;
2568 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2569 /* ATS asks us to switch while we were trying to connect; switch to new
2570 address and check blacklist again */
2571 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2573 n->ack_state = ACK_SEND_ACK;
2574 send_syn_ack_message (&n->primary_address,
2575 n->connect_ack_timestamp);
2577 set_primary_address (n,
2580 blc_ctx->bandwidth_in,
2581 blc_ctx->bandwidth_out);
2582 set_state_and_timeout (n,
2583 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2584 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2586 case GNUNET_TRANSPORT_PS_CONNECTED:
2587 GNUNET_assert (NULL != n->primary_address.address);
2588 GNUNET_assert (NULL != n->primary_address.session);
2589 GNUNET_break (n->primary_address.session != session);
2590 /* ATS asks us to switch a life connection; see if we can get
2591 a SYN_ACK on it before we actually do this! */
2592 set_alternative_address (n,
2595 blc_ctx->bandwidth_in,
2596 blc_ctx->bandwidth_out);
2597 set_state_and_timeout (n,
2598 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2599 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2600 GNUNET_STATISTICS_update (GST_stats,
2601 gettext_noop ("# Attempts to switch addresses"),
2604 send_syn (&n->alternative_address);
2606 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2607 set_primary_address (n,
2610 blc_ctx->bandwidth_in,
2611 blc_ctx->bandwidth_out);
2612 if (ACK_SEND_SYN_ACK == n->ack_state)
2614 /* Send pending SYN_ACK message */
2615 n->ack_state = ACK_SEND_ACK;
2616 send_syn_ack_message (&n->primary_address,
2617 n->connect_ack_timestamp);
2619 set_state_and_timeout (n,
2620 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2621 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2622 send_syn (&n->primary_address);
2624 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2625 /* ATS asks us to switch while we were trying to reconnect; switch to new
2626 address and send SYN again */
2627 set_primary_address (n,
2630 blc_ctx->bandwidth_in,
2631 blc_ctx->bandwidth_out);
2632 set_state_and_timeout (n,
2633 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2634 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2635 send_syn (&n->primary_address);
2637 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2638 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2640 (n->primary_address.session == session) )
2642 /* ATS switches back to still-active session */
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2644 "ATS double-switched, cleaning up alternative address\n");
2645 free_address (&n->alternative_address);
2646 set_state_and_timeout (n,
2647 GNUNET_TRANSPORT_PS_CONNECTED,
2651 /* ATS asks us to switch a life connection, send */
2652 set_alternative_address (n,
2655 blc_ctx->bandwidth_in,
2656 blc_ctx->bandwidth_out);
2657 set_state_and_timeout (n,
2658 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2659 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2660 send_syn (&n->alternative_address);
2662 case GNUNET_TRANSPORT_PS_DISCONNECT:
2663 /* not going to switch addresses while disconnecting */
2664 GNUNET_STATISTICS_update (GST_stats,
2665 "# ATS suggestion ignored (disconnecting)",
2669 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2674 "Unhandled state `%s'\n",
2675 GNUNET_TRANSPORT_ps2s (n->state));
2680 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2683 GNUNET_free (blc_ctx);
2688 * For the given peer, switch to this address.
2690 * Before accepting this addresses and actively using it, a blacklist check
2693 * If any check fails or the suggestion can somehow not be followed, we
2694 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2695 * could not be satisfied and force ATS to do something else.
2697 * @param address address of the other peer,
2698 * @param session session to use or NULL if transport should initiate a session
2699 * @param bandwidth_in inbound quota to be used when connection is up,
2700 * 0 to disconnect from peer
2701 * @param bandwidth_out outbound quota to be used when connection is up,
2702 * 0 to disconnect from peer
2705 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2706 struct GNUNET_ATS_Session *session,
2707 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2708 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2710 struct GST_BlacklistCheck *blc;
2711 struct BlacklistCheckSwitchContext *blc_ctx;
2713 GNUNET_assert (NULL != address->transport_name);
2715 try_run_fast_ats_update (address,
2721 /* Check if plugin is available */
2722 if (NULL == (GST_plugins_find (address->transport_name)))
2724 /* we don't have the plugin for this address */
2726 GST_ats_block_address (address,
2730 if ((NULL == session) &&
2731 (GNUNET_HELLO_address_check_option (address,
2732 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2734 /* This is a inbound address and we do not have a session to use! */
2736 GST_ats_block_address (address,
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2742 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2743 GST_plugins_a2s (address),
2744 GNUNET_i2s (&address->peer),
2745 (unsigned int) ntohl (bandwidth_in.value__),
2746 (unsigned int) ntohl (bandwidth_out.value__));
2748 /* Perform blacklist check */
2749 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2750 blc_ctx->bandwidth_in = bandwidth_in;
2751 blc_ctx->bandwidth_out = bandwidth_out;
2752 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2755 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2756 address->transport_name,
2757 &switch_address_bl_check_cont,
2768 * Function called to send network utilization data to ATS for
2769 * each active connection.
2772 * @param key peer we send utilization data for
2773 * @param value the `struct NeighbourMapEntry *` with data to send
2774 * @return #GNUNET_OK (continue to iterate)
2777 send_utilization_data (void *cls,
2778 const struct GNUNET_PeerIdentity *key,
2781 struct NeighbourMapEntry *n = value;
2784 struct GNUNET_TIME_Relative delta;
2786 if ( (GNUNET_YES != test_connected (n)) ||
2787 (NULL == n->primary_address.address) )
2789 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2790 GNUNET_TIME_absolute_get ());
2792 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2793 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2795 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2796 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2799 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2803 GST_ats_update_utilization (n->primary_address.address,
2806 n->util_total_bytes_recv = 0;
2807 n->util_total_bytes_sent = 0;
2808 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2814 * Task transmitting utilization in a regular interval
2816 * @param cls the 'struct NeighbourMapEntry' for which we are running
2817 * @param tc scheduler context (unused)
2820 utilization_transmission (void *cls,
2821 const struct GNUNET_SCHEDULER_TaskContext *tc)
2823 util_transmission_tk = NULL;
2824 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
2825 &send_utilization_data,
2827 util_transmission_tk
2828 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2829 &utilization_transmission,
2835 * Track information about data we received from the
2836 * given address (used to notify ATS about our utilization
2837 * of allocated resources).
2839 * @param address the address we got data from
2840 * @param message the message we received (really only the size is used)
2843 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2844 const struct GNUNET_MessageHeader *message)
2846 struct NeighbourMapEntry *n;
2848 n = lookup_neighbour (&address->peer);
2851 n->util_total_bytes_recv += ntohs (message->size);
2856 * Track information about data we transmitted using the given @a
2857 * address and @a session (used to notify ATS about our utilization of
2858 * allocated resources).
2860 * @param address the address we transmitted data to
2861 * @param session session we used to transmit data
2862 * @param message the message we sent (really only the size is used)
2865 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2866 struct GNUNET_ATS_Session *session,
2869 struct NeighbourMapEntry *n;
2871 n = lookup_neighbour (&address->peer);
2874 if (n->primary_address.session != session)
2876 n->util_total_bytes_sent += size;
2881 * Master task run for every neighbour. Performs all of the time-related
2882 * activities (keep alive, send next message, disconnect if idle, finish
2883 * clean up after disconnect).
2885 * @param cls the 'struct NeighbourMapEntry' for which we are running
2886 * @param tc scheduler context (unused)
2889 master_task (void *cls,
2890 const struct GNUNET_SCHEDULER_TaskContext *tc)
2892 struct NeighbourMapEntry *n = cls;
2893 struct GNUNET_TIME_Relative delay;
2896 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2898 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2899 GNUNET_i2s (&n->id),
2900 GNUNET_TRANSPORT_ps2s(n->state),
2901 GNUNET_STRINGS_relative_time_to_string (delay,
2905 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2906 /* invalid state for master task, clean up */
2910 case GNUNET_TRANSPORT_PS_INIT_ATS:
2911 if (0 == delay.rel_value_us)
2913 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2914 "Connection to `%s' timed out waiting for ATS to provide address\n",
2915 GNUNET_i2s (&n->id));
2920 case GNUNET_TRANSPORT_PS_SYN_SENT:
2921 if (0 == delay.rel_value_us)
2923 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2924 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2925 GNUNET_i2s (&n->id));
2926 /* Remove address and request and additional one */
2927 unset_primary_address (n);
2928 set_state_and_timeout (n,
2929 GNUNET_TRANSPORT_PS_INIT_ATS,
2930 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2934 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2935 if (0 == delay.rel_value_us)
2937 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2938 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2939 GNUNET_i2s (&n->id));
2944 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2945 if (0 == delay.rel_value_us)
2947 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2948 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2949 GNUNET_i2s (&n->id));
2950 disconnect_neighbour (n);
2954 case GNUNET_TRANSPORT_PS_CONNECTED:
2955 if (0 == delay.rel_value_us)
2957 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2958 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2959 GNUNET_i2s (&n->id));
2960 disconnect_neighbour (n);
2963 try_transmission_to_peer (n);
2966 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2967 if (0 == delay.rel_value_us)
2969 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2970 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2971 GNUNET_i2s (&n->id));
2972 disconnect_neighbour (n);
2976 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2977 if (0 == delay.rel_value_us)
2979 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2980 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
2981 GNUNET_i2s (&n->id));
2982 disconnect_neighbour (n);
2986 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2987 if (0 == delay.rel_value_us)
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2990 "Switch failed, cleaning up alternative address\n");
2991 free_address (&n->alternative_address);
2992 set_state_and_timeout (n,
2993 GNUNET_TRANSPORT_PS_CONNECTED,
2994 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2996 try_transmission_to_peer (n);
2999 case GNUNET_TRANSPORT_PS_DISCONNECT:
3000 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3001 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3002 GNUNET_i2s (&n->id));
3005 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3006 /* how did we get here!? */
3010 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3011 "Unhandled state `%s'\n",
3012 GNUNET_TRANSPORT_ps2s (n->state));
3016 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3017 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3018 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3020 /* if we are *now* in one of the two states, we're sending
3021 keep alive messages, so we need to consider the keepalive
3022 delay, not just the connection timeout */
3023 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3026 if (NULL == n->task)
3027 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3034 * Send a ACK message to the neighbour to confirm that we
3037 * @param n neighbour to send the ACK to
3040 send_session_ack_message (struct NeighbourMapEntry *n)
3042 struct GNUNET_MessageHeader msg;
3044 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3045 "Sending ACK message to peer `%s'\n",
3046 GNUNET_i2s (&n->id));
3048 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3049 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3050 (void) send_with_session (n,
3052 sizeof (struct GNUNET_MessageHeader),
3054 GNUNET_TIME_UNIT_FOREVER_REL,
3061 * We received a 'SESSION_SYN_ACK' message from the other peer.
3062 * Consider switching to it.
3064 * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
3065 * @param peer identity of the peer to switch the address for
3066 * @param address address of the other peer, NULL if other peer
3068 * @param session session to use (or NULL)
3069 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3072 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3073 const struct GNUNET_HELLO_Address *address,
3074 struct GNUNET_ATS_Session *session)
3076 const struct TransportSynMessage *scm;
3077 struct GNUNET_TIME_Absolute ts;
3078 struct NeighbourMapEntry *n;
3080 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3082 GNUNET_break_op (0);
3083 return GNUNET_SYSERR;
3085 GNUNET_STATISTICS_update (GST_stats,
3087 ("# SYN_ACK messages received"),
3089 scm = (const struct TransportSynMessage *) message;
3090 GNUNET_break_op (ntohl (scm->reserved) == 0);
3091 if (NULL == (n = lookup_neighbour (&address->peer)))
3093 GNUNET_STATISTICS_update (GST_stats,
3095 ("# unexpected SYN_ACK messages (no peer)"),
3097 return GNUNET_SYSERR;
3099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3100 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3101 GNUNET_i2s (&address->peer),
3102 GNUNET_TRANSPORT_ps2s (n->state),
3103 print_ack_state (n->ack_state));
3104 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3107 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3110 return GNUNET_SYSERR;
3111 case GNUNET_TRANSPORT_PS_INIT_ATS:
3112 GNUNET_STATISTICS_update (GST_stats,
3113 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3117 case GNUNET_TRANSPORT_PS_SYN_SENT:
3118 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3120 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3121 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3124 set_state_and_timeout (n,
3125 GNUNET_TRANSPORT_PS_CONNECTED,
3126 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3127 set_primary_address (n,
3128 n->primary_address.address,
3129 n->primary_address.session,
3130 n->primary_address.bandwidth_in,
3131 n->primary_address.bandwidth_out);
3132 send_session_ack_message (n);
3134 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3135 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3136 GNUNET_STATISTICS_update (GST_stats,
3137 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3141 case GNUNET_TRANSPORT_PS_CONNECTED:
3142 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3143 send_session_ack_message (n);
3145 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3146 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3147 to give us a new address... */
3148 GNUNET_STATISTICS_update (GST_stats,
3149 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3153 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3154 /* Reconnecting with new address address worked; go back to connected! */
3155 set_state_and_timeout (n,
3156 GNUNET_TRANSPORT_PS_CONNECTED,
3157 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3158 send_session_ack_message (n);
3160 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3161 /* new address worked; adopt it and go back to connected! */
3162 set_state_and_timeout (n,
3163 GNUNET_TRANSPORT_PS_CONNECTED,
3164 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3165 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3167 /* Set primary addresses */
3168 set_primary_address (n,
3169 n->alternative_address.address,
3170 n->alternative_address.session,
3171 n->alternative_address.bandwidth_in,
3172 n->alternative_address.bandwidth_out);
3173 GNUNET_STATISTICS_update (GST_stats,
3174 gettext_noop ("# Successful attempts to switch addresses"),
3178 GNUNET_HELLO_address_free (n->alternative_address.address);
3179 memset (&n->alternative_address,
3181 sizeof (n->alternative_address));
3182 send_session_ack_message (n);
3184 case GNUNET_TRANSPORT_PS_DISCONNECT:
3185 GNUNET_STATISTICS_update (GST_stats,
3187 ("# unexpected SYN_ACK messages (disconnecting)"),
3189 return GNUNET_SYSERR;
3190 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3195 "Unhandled state `%s'\n",
3196 GNUNET_TRANSPORT_ps2s (n->state));
3198 return GNUNET_SYSERR;
3205 * A session was terminated. Take note; if needed, try to get
3206 * an alternative address from ATS.
3208 * @param peer identity of the peer where the session died
3209 * @param session session that is gone
3210 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3211 * this session was not in use
3214 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3215 struct GNUNET_ATS_Session *session)
3217 struct NeighbourMapEntry *n;
3219 if (NULL == (n = lookup_neighbour (peer)))
3220 return GNUNET_NO; /* can't affect us */
3221 if (session != n->primary_address.session)
3223 /* Free alternative address */
3224 if (session == n->alternative_address.session)
3226 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3227 set_state_and_timeout (n,
3228 GNUNET_TRANSPORT_PS_CONNECTED,
3230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3231 "Session died, cleaning up alternative address\n");
3232 free_address (&n->alternative_address);
3234 return GNUNET_NO; /* doesn't affect us further */
3237 n->expect_latency_response = GNUNET_NO;
3238 /* The session for neighbour's primary address died */
3241 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3245 case GNUNET_TRANSPORT_PS_INIT_ATS:
3249 case GNUNET_TRANSPORT_PS_SYN_SENT:
3250 /* The session used to send the SYN terminated:
3251 * this implies a connect error*/
3252 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3253 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3255 GST_plugins_a2s (n->primary_address.address),
3256 n->primary_address.session,
3259 /* Destroy the address since it cannot be used */
3260 unset_primary_address (n);
3261 set_state_and_timeout (n,
3262 GNUNET_TRANSPORT_PS_INIT_ATS,
3263 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3265 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3266 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3267 /* error on inbound session; free neighbour entirely */
3270 case GNUNET_TRANSPORT_PS_CONNECTED:
3271 /* Our primary connection died, try a fast reconnect */
3272 unset_primary_address (n);
3273 set_state_and_timeout (n,
3274 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3275 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3277 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3278 /* we don't have an address, how can it go down? */
3281 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3282 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3283 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3285 GST_plugins_a2s (n->primary_address.address),
3286 n->primary_address.session,
3288 /* Destroy the address since it cannot be used */
3289 unset_primary_address (n);
3290 set_state_and_timeout (n,
3291 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3292 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3294 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3295 /* primary went down while we were waiting for SYN_ACK on secondary;
3296 secondary as primary */
3298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3299 "Connection `%s' %p to peer `%s' was terminated while switching, "
3300 "switching to alternative address `%s' %p\n",
3301 GST_plugins_a2s (n->primary_address.address),
3302 n->primary_address.session,
3304 GST_plugins_a2s (n->alternative_address.address),
3305 n->alternative_address.session);
3307 /* Destroy the inbound address since it cannot be used */
3308 free_address (&n->primary_address);
3309 n->primary_address = n->alternative_address;
3310 GNUNET_assert (GNUNET_YES ==
3311 GST_ats_is_known (n->primary_address.address,
3312 n->primary_address.session));
3313 memset (&n->alternative_address,
3315 sizeof (struct NeighbourAddress));
3316 set_state_and_timeout (n,
3317 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3318 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3320 case GNUNET_TRANSPORT_PS_DISCONNECT:
3321 unset_primary_address (n);
3323 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3324 /* neighbour was freed and plugins told to terminate session */
3327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3328 "Unhandled state `%s'\n",
3329 GNUNET_TRANSPORT_ps2s (n->state));
3333 if (NULL != n->task)
3334 GNUNET_SCHEDULER_cancel (n->task);
3335 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3341 * We received a 'ACK' message from the other peer.
3342 * If we sent a 'SYN_ACK' last, this means we are now
3343 * connected. Otherwise, do nothing.
3345 * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
3346 * @param address address of the other peer
3347 * @param session session to use (or NULL)
3348 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3351 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3352 const struct GNUNET_HELLO_Address *address,
3353 struct GNUNET_ATS_Session *session)
3355 struct NeighbourMapEntry *n;
3357 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3359 GNUNET_break_op (0);
3360 return GNUNET_SYSERR;
3362 GNUNET_STATISTICS_update (GST_stats,
3363 gettext_noop ("# ACK messages received"),
3366 if (NULL == (n = lookup_neighbour (&address->peer)))
3368 GNUNET_break_op (0);
3369 return GNUNET_SYSERR;
3371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3372 "Received ACK for peer `%s' in state %s/%s\n",
3373 GNUNET_i2s (&address->peer),
3374 GNUNET_TRANSPORT_ps2s (n->state),
3375 print_ack_state (n->ack_state));
3377 /* Check if we are in a plausible state for having sent
3378 a SYN_ACK. If not, return, otherwise break.
3380 The remote peers sends a ACK as a response for a SYN_ACK
3384 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3385 now wait for the ACK to finally be connected
3386 - If we sent a SYN_ACK to this peer before */
3388 if ( ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3389 (ACK_SEND_ACK != n->ack_state) ) ||
3390 (NULL == n->primary_address.address) )
3392 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3393 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3394 GNUNET_i2s (&address->peer),
3395 GNUNET_TRANSPORT_ps2s (n->state),
3396 print_ack_state (n->ack_state));
3398 GNUNET_STATISTICS_update (GST_stats,
3399 gettext_noop ("# unexpected ACK messages"),
3404 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3406 /* We tried to switch addresses while being connect. We explicitly wait
3407 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3408 * so we do not want to set the address as in use! */
3411 set_state_and_timeout (n,
3412 GNUNET_TRANSPORT_PS_CONNECTED,
3413 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3415 if (NULL == n->primary_address.address) {
3417 * We are in state = PSY_SYN_RECV_ACK or ack_state = ACK_SEND_ACK, which
3418 * really means we did try (and succeed) to send a SYN and are waiting for
3420 * That suggests that the primary_address used to be non-NULL, but maybe it
3421 * got reset to NULL without the state being changed appropriately?
3427 /* Reset backoff for primary address */
3428 GST_ats_block_reset (n->primary_address.address,
3429 n->primary_address.session);
3435 * Test if we're connected to the given peer.
3437 * @param target peer to test
3438 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3441 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3443 return test_connected (lookup_neighbour (target));
3448 * Task to asynchronously run #free_neighbour().
3450 * @param cls the `struct NeighbourMapEntry` to free
3454 delayed_disconnect (void *cls,
3455 const struct GNUNET_SCHEDULER_TaskContext* tc)
3457 struct NeighbourMapEntry *n = cls;
3459 n->delayed_disconnect_task = NULL;
3460 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3461 "Disconnecting by request from peer %s\n",
3462 GNUNET_i2s (&n->id));
3468 * We received a quota message from the given peer,
3469 * validate and process.
3471 * @param peer sender of the message
3472 * @param msg the quota message
3475 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
3476 const struct GNUNET_MessageHeader *msg)
3478 struct NeighbourMapEntry *n;
3479 const struct GNUNET_ATS_SessionQuotaMessage *sqm;
3480 struct GNUNET_BANDWIDTH_Value32NBO last;
3482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3483 "Received QUOTA message from peer `%s'\n",
3485 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionQuotaMessage))
3487 GNUNET_break_op (0);
3488 GNUNET_STATISTICS_update (GST_stats,
3489 gettext_noop ("# quota messages ignored (malformed)"),
3494 GNUNET_STATISTICS_update (GST_stats,
3496 ("# QUOTA messages received"),
3498 sqm = (const struct GNUNET_ATS_SessionQuotaMessage *) msg;
3499 if (NULL == (n = lookup_neighbour (peer)))
3504 last = GNUNET_BANDWIDTH_value_max (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3505 GNUNET_BANDWIDTH_value_init (ntohl (sqm->quota)));
3506 if (last.value__ != n->neighbour_receive_quota.value__)
3508 n->neighbour_receive_quota = last;
3509 send_outbound_quota_to_clients (n);
3515 * We received a disconnect message from the given peer,
3516 * validate and process.
3518 * @param peer sender of the message
3519 * @param msg the disconnect message
3522 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3523 const struct GNUNET_MessageHeader *msg)
3525 struct NeighbourMapEntry *n;
3526 const struct GNUNET_ATS_SessionDisconnectMessage *sdm;
3528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3529 "Received DISCONNECT message from peer `%s'\n",
3531 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionDisconnectMessage))
3533 GNUNET_break_op (0);
3534 GNUNET_STATISTICS_update (GST_stats,
3536 ("# disconnect messages ignored (malformed)"),
3541 GNUNET_STATISTICS_update (GST_stats,
3543 ("# DISCONNECT messages received"),
3545 sdm = (const struct GNUNET_ATS_SessionDisconnectMessage *) msg;
3546 if (NULL == (n = lookup_neighbour (peer)))
3551 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3553 GNUNET_STATISTICS_update (GST_stats,
3554 gettext_noop ("# disconnect messages ignored (timestamp)"),
3559 if (0 != memcmp (peer,
3561 sizeof (struct GNUNET_PeerIdentity)))
3563 GNUNET_break_op (0);
3566 if (ntohl (sdm->purpose.size) !=
3567 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3568 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3569 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3572 "DISCONNECT message from peer `%s' has invalid size\n",
3574 GNUNET_break_op (0);
3578 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3584 "DISCONNECT message from peer `%s' cannot be verified \n",
3586 GNUNET_break_op (0);
3589 if (NULL == n->delayed_disconnect_task)
3591 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect,
3598 * Closure for the #neighbours_iterate() function.
3600 struct IteratorContext
3603 * Function to call on each connected neighbour.
3605 GST_NeighbourIterator cb;
3608 * Closure for @e cb.
3615 * Call the callback from the closure for each neighbour.
3617 * @param cls the `struct IteratorContext`
3618 * @param key the hash of the public key of the neighbour
3619 * @param value the `struct NeighbourMapEntry`
3620 * @return #GNUNET_OK (continue to iterate)
3623 neighbours_iterate (void *cls,
3624 const struct GNUNET_PeerIdentity *key,
3627 struct IteratorContext *ic = cls;
3628 struct NeighbourMapEntry *n = value;
3629 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3630 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3632 if (NULL != n->primary_address.address)
3634 bandwidth_in = n->primary_address.bandwidth_in;
3635 bandwidth_out = n->primary_address.bandwidth_out;
3639 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3640 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3644 n->primary_address.address,
3647 bandwidth_in, bandwidth_out);
3653 * Iterate over all connected neighbours.
3655 * @param cb function to call
3656 * @param cb_cls closure for @a cb
3659 GST_neighbours_iterate (GST_NeighbourIterator cb,
3662 struct IteratorContext ic;
3664 if (NULL == neighbours)
3665 return; /* can happen during shutdown */
3668 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3669 &neighbours_iterate,
3675 * If we have an active connection to the given target, it must be shutdown.
3677 * @param target peer to disconnect from
3680 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3682 struct NeighbourMapEntry *n;
3684 if (NULL == (n = lookup_neighbour (target)))
3685 return; /* not active */
3686 if (GNUNET_YES == test_connected (n))
3687 GNUNET_STATISTICS_update (GST_stats,
3688 gettext_noop ("# disconnected from peer upon explicit request"),
3691 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3692 "Forced disconnect from peer %s\n",
3693 GNUNET_i2s (target));
3694 disconnect_neighbour (n);
3699 * Obtain current address information for the given neighbour.
3702 * @return address currently used
3704 const struct GNUNET_HELLO_Address *
3705 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3707 struct NeighbourMapEntry *n;
3709 n = lookup_neighbour (peer);
3712 return n->primary_address.address;
3717 * Initialize the neighbours subsystem.
3719 * @param max_fds maximum number of fds to use
3722 GST_neighbours_start (unsigned int max_fds)
3724 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3726 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3727 &utilization_transmission,
3733 * Disconnect from the given neighbour.
3736 * @param key hash of neighbour's public key (not used)
3737 * @param value the `struct NeighbourMapEntry` of the neighbour
3738 * @return #GNUNET_OK (continue to iterate)
3741 disconnect_all_neighbours (void *cls,
3742 const struct GNUNET_PeerIdentity *key,
3745 struct NeighbourMapEntry *n = value;
3747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3748 "Disconnecting peer `%4s' during shutdown\n",
3749 GNUNET_i2s (&n->id));
3756 * Cleanup the neighbours subsystem.
3759 GST_neighbours_stop ()
3761 if (NULL == neighbours)
3763 if (NULL != util_transmission_tk)
3765 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3766 util_transmission_tk = NULL;
3768 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3769 &disconnect_all_neighbours,
3771 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3776 /* end of file gnunet-service-transport_neighbours.c */