2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_ats.h"
29 #include "gnunet-service-transport_blacklist.h"
30 #include "gnunet-service-transport_clients.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport_manipulation.h"
33 #include "gnunet-service-transport_plugins.h"
34 #include "gnunet-service-transport_validation.h"
35 #include "gnunet-service-transport.h"
36 #include "gnunet_peerinfo_service.h"
37 #include "gnunet_constants.h"
38 #include "transport.h"
41 * Experimental option to ignore SessionQuotaMessages from
44 #define IGNORE_INBOUND_QUOTA GNUNET_YES
47 * Size of the neighbour hash map.
49 #define NEIGHBOUR_TABLE_SIZE 256
52 * Time we give plugin to transmit DISCONNECT message before the
53 * neighbour entry self-destructs.
55 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
58 * How often must a peer violate bandwidth quotas before we start
59 * to simply drop its messages?
61 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
64 * How long are we willing to wait for a response from ATS before timing out?
66 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
69 * How long are we willing to wait for an ACK from the other peer before
70 * giving up on our connect operation?
72 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
75 * How long are we willing to wait for a successful reconnect if
76 * an existing connection went down? Much shorter than the
77 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
78 * higher layers about the disconnect during this period.
80 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
83 * Interval to send utilization data
85 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_UNIT_SECONDS
88 * State describing which kind a reply this neighbour should send
93 * We did not receive a SYN message for this neighbour
98 * The neighbour received a SYN message and has to send a SYN_ACK
101 ACK_SEND_SYN_ACK = 1,
104 * The neighbour sent a SYN_ACK message and has to send a ACK
111 GNUNET_NETWORK_STRUCT_BEGIN
114 * Message a peer sends to another to indicate that it intends to
115 * setup a connection/session for data exchange. A 'SESSION_SYN'
116 * should be answered with a 'SESSION_SYN_ACK' with the same body
117 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
118 * a 'ACK'. Once the 'ACK' is received, both peers
119 * should be connected.
121 struct TransportSynMessage
124 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
125 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
127 struct GNUNET_MessageHeader header;
132 uint32_t reserved GNUNET_PACKED;
135 * Absolute time at the sender. Only the most recent connect
136 * message implies which session is preferred by the sender.
138 struct GNUNET_TIME_AbsoluteNBO timestamp;
144 * Message a peer sends to another when connected to indicate that a
145 * session is in use and the peer is still alive or to respond to a keep alive.
146 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
147 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
148 * When the keep alive response with type is received, transport service
149 * will call the respective plugin to update the session timeout
151 struct GNUNET_ATS_SessionKeepAliveMessage
154 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
155 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
157 struct GNUNET_MessageHeader header;
160 * A nonce to identify the session the keep alive is used for
162 uint32_t nonce GNUNET_PACKED;
167 * Message a peer sends to another when connected to indicate that
168 * the other peer should limit transmissions to the indicated
171 struct GNUNET_ATS_SessionQuotaMessage
174 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA.
176 struct GNUNET_MessageHeader header;
179 * Quota to use (for sending), in bytes per second.
181 uint32_t quota GNUNET_PACKED;
186 * Message we send to the other peer to notify him that we intentionally
187 * are disconnecting (to reduce timeouts). This is just a friendly
188 * notification, peers must not rely on always receiving disconnect
191 struct GNUNET_ATS_SessionDisconnectMessage
194 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
196 struct GNUNET_MessageHeader header;
201 uint32_t reserved GNUNET_PACKED;
204 * Purpose of the signature. Extends over the timestamp.
205 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
207 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
210 * Absolute time at the sender. Only the most recent connect
211 * message implies which session is preferred by the sender.
213 struct GNUNET_TIME_AbsoluteNBO timestamp;
216 * Public key of the sender.
218 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
221 * Signature of the peer that sends us the disconnect. Only
222 * valid if the timestamp is AFTER the timestamp from the
223 * corresponding 'SYN' message.
225 struct GNUNET_CRYPTO_EddsaSignature signature;
229 GNUNET_NETWORK_STRUCT_END
233 * For each neighbour we keep a list of messages
234 * that we still want to transmit to the neighbour.
240 * This is a doubly linked list.
242 struct MessageQueue *next;
245 * This is a doubly linked list.
247 struct MessageQueue *prev;
250 * Function to call once we're done.
252 GST_NeighbourSendContinuation cont;
255 * Closure for @e cont
260 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
261 * stuck together in memory. Allocated at the end of this struct.
263 const char *message_buf;
266 * Size of the message buf
268 size_t message_buf_size;
271 * At what time should we fail?
273 struct GNUNET_TIME_Absolute timeout;
279 * A possible address we could use to communicate with a neighbour.
281 struct NeighbourAddress
285 * Active session for this address.
287 struct GNUNET_ATS_Session *session;
290 * Network-level address information.
292 struct GNUNET_HELLO_Address *address;
295 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
296 * peer for this address. Use to check that the ACK is in response
297 * to our most recent 'SYN'.
299 struct GNUNET_TIME_Absolute connect_timestamp;
302 * Inbound bandwidth from ATS for this address.
304 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
307 * Outbound bandwidth from ATS for this address.
309 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
312 * Did we tell ATS that this is our 'active' address?
317 * The current nonce sent in the last keep alive messages
319 uint32_t keep_alive_nonce;
324 * Entry in neighbours.
326 struct NeighbourMapEntry
330 * Head of list of messages we would like to send to this peer;
331 * must contain at most one message per client.
333 struct MessageQueue *messages_head;
336 * Tail of list of messages we would like to send to this peer; must
337 * contain at most one message per client.
339 struct MessageQueue *messages_tail;
342 * Are we currently trying to send a message? If so, which one?
344 struct MessageQueue *is_active;
347 * Primary address we currently use to communicate with the neighbour.
349 struct NeighbourAddress primary_address;
352 * Alternative address currently under consideration for communicating
353 * with the neighbour.
355 struct NeighbourAddress alternative_address;
358 * Identity of this neighbour.
360 struct GNUNET_PeerIdentity id;
363 * Main task that drives this peer (timeouts, keepalives, etc.).
364 * Always runs the #master_task().
366 struct GNUNET_SCHEDULER_Task *task;
369 * Task to disconnect neighbour after we received a DISCONNECT message
371 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
374 * At what time should we sent the next keep-alive message?
376 struct GNUNET_TIME_Absolute keep_alive_time;
379 * At what time did we sent the last keep-alive message? Used
380 * to calculate round-trip time ("latency").
382 struct GNUNET_TIME_Absolute last_keep_alive_time;
385 * Timestamp we should include in our next SYN_ACK message.
386 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
387 * our SYN_ACK message.
389 struct GNUNET_TIME_Absolute connect_ack_timestamp;
392 * ATS address suggest handle
394 struct GNUNET_ATS_ConnectivitySuggestHandle *suggest_handle;
397 * Time where we should cut the connection (timeout) if we don't
398 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
399 * if we are in #GNUNET_TRANSPORT_PS_CONNECTED).
401 struct GNUNET_TIME_Absolute timeout;
404 * Tracker for inbound bandwidth.
406 struct GNUNET_BANDWIDTH_Tracker in_tracker;
409 * How often has the other peer (recently) violated the inbound
410 * traffic limit? Incremented by 10 per violation, decremented by 1
411 * per non-violation (for each time interval).
413 unsigned int quota_violation_count;
416 * Latest quota the other peer send us in bytes per second.
417 * We should not send more, least the other peer throttle
418 * receiving our traffic.
420 struct GNUNET_BANDWIDTH_Value32NBO neighbour_receive_quota;
423 * The current state of the peer.
425 enum GNUNET_TRANSPORT_PeerState state;
428 * Did we sent an KEEP_ALIVE message and are we expecting a response?
430 int expect_latency_response;
433 * When a peer wants to connect we have to reply to the 1st SYN message
434 * with a SYN_ACK message. But sometime we cannot send this message
435 * immediately since we do not have an address and then we have to remember
436 * to send this message as soon as we have an address.
438 * Flag to set if we still need to send a SYN_ACK message to the other peer
439 * (once we have an address to use and the peer has been allowed by our
440 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
441 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
442 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
443 * 'ACK' (regardless of what our own state machine might say).
445 enum GST_ACK_State ack_state;
448 * Tracking utilization of outbound bandwidth
450 uint32_t util_total_bytes_sent;
453 * Tracking utilization of inbound bandwidth
455 uint32_t util_total_bytes_recv;
458 * Date of last utilization transmission
460 struct GNUNET_TIME_Absolute last_util_transmission;
465 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
467 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
470 * List of pending blacklist checks: head
472 static struct BlacklistCheckSwitchContext *pending_bc_head;
475 * List of pending blacklist checks: tail
477 static struct BlacklistCheckSwitchContext *pending_bc_tail;
480 * counter for connected neighbours
482 static unsigned int neighbours_connected;
485 * Number of bytes we have currently queued for transmission.
487 static unsigned long long bytes_in_send_queue;
490 * Task transmitting utilization data
492 static struct GNUNET_SCHEDULER_Task *util_transmission_tk;
496 * Convert the given ACK state to a string.
499 * @return corresponding human-readable string
502 print_ack_state (enum GST_ACK_State s)
507 case ACK_SEND_SYN_ACK:
508 return "SEND_SYN_ACK";
519 * 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
700 master_task (void *cls);
704 * Set net state and state timeout for this neighbour and notify monitoring
706 * @param n the respective neighbour
707 * @param s the new state
708 * @param timeout the new timeout
711 set_state_and_timeout (struct NeighbourMapEntry *n,
712 enum GNUNET_TRANSPORT_PeerState s,
713 struct GNUNET_TIME_Absolute timeout)
715 if (GNUNET_TRANSPORT_is_connected (s) &&
716 ! GNUNET_TRANSPORT_is_connected (n->state) )
718 neighbours_connect_notification (n);
719 GNUNET_STATISTICS_set (GST_stats,
720 gettext_noop ("# peers connected"),
721 ++neighbours_connected,
724 if (! GNUNET_TRANSPORT_is_connected (s) &&
725 GNUNET_TRANSPORT_is_connected (n->state) )
727 GNUNET_STATISTICS_set (GST_stats,
728 gettext_noop ("# peers connected"),
729 --neighbours_connected,
731 neighbours_disconnect_notification (n);
734 if ( (timeout.abs_value_us < n->timeout.abs_value_us) &&
737 /* new timeout is earlier, reschedule master task */
738 GNUNET_SCHEDULER_cancel (n->task);
739 n->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (timeout),
743 n->timeout = timeout;
744 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
745 "Neighbour `%s' changed state to %s with timeout %s\n",
747 GNUNET_TRANSPORT_ps2s(s),
748 GNUNET_STRINGS_absolute_time_to_string (timeout));
749 neighbours_changed_notification (&n->id,
750 n->primary_address.address,
753 n->primary_address.bandwidth_in,
754 n->primary_address.bandwidth_out);
759 * Initialize the alternative address of a neighbour
761 * @param n the neighbour
762 * @param address address of the other peer, NULL if other peer
764 * @param session session to use (or NULL, in which case an
765 * address must be setup)
766 * @param bandwidth_in inbound quota to be used when connection is up
767 * @param bandwidth_out outbound quota to be used when connection is up
770 set_alternative_address (struct NeighbourMapEntry *n,
771 const struct GNUNET_HELLO_Address *address,
772 struct GNUNET_ATS_Session *session,
773 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
774 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
776 struct GNUNET_TRANSPORT_PluginFunctions *papi;
778 if (NULL == (papi = GST_plugins_find (address->transport_name)))
783 if (session == n->alternative_address.session)
785 n->alternative_address.bandwidth_in = bandwidth_in;
786 n->alternative_address.bandwidth_out = bandwidth_out;
789 if (NULL != n->alternative_address.address)
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
792 "Replacing existing alternative address with another one\n");
793 free_address (&n->alternative_address);
796 session = papi->get_session (papi->cls,
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Failed to obtain new session for peer `%s' and address '%s'\n",
802 GNUNET_i2s (&address->peer),
803 GST_plugins_a2s (address));
804 GNUNET_STATISTICS_update (GST_stats,
805 gettext_noop ("# session creation failed"),
810 GST_ats_new_session (address,
812 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
813 "Neighbour `%s' configured alternative address %s\n",
815 GST_plugins_a2s(address));
817 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
818 n->alternative_address.bandwidth_in = bandwidth_in;
819 n->alternative_address.bandwidth_out = bandwidth_out;
820 n->alternative_address.session = session;
821 n->alternative_address.ats_active = GNUNET_NO;
822 n->alternative_address.keep_alive_nonce = 0;
823 GNUNET_assert (GNUNET_YES ==
824 GST_ats_is_known (n->alternative_address.address,
825 n->alternative_address.session));
830 * Transmit a message using the current session of the given
833 * @param n entry for the recipient
834 * @param msgbuf buffer to transmit
835 * @param msgbuf_size number of bytes in @a msgbuf buffer
836 * @param priority transmission priority
837 * @param timeout transmission timeout
838 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
839 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
840 * @param cont continuation to call when finished (can be NULL)
841 * @param cont_cls closure for @a cont
842 * @return timeout (copy of @a timeout or a calculated one if
843 * @a use_keepalive_timeout is #GNUNET_YES.
845 static struct GNUNET_TIME_Relative
846 send_with_session (struct NeighbourMapEntry *n,
850 struct GNUNET_TIME_Relative timeout,
851 unsigned int use_keepalive_timeout,
852 GNUNET_TRANSPORT_TransmitContinuation cont,
855 struct GNUNET_TRANSPORT_PluginFunctions *papi;
856 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
858 GNUNET_assert (NULL != n->primary_address.session);
859 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
860 (-1 == papi->send (papi->cls,
861 n->primary_address.session,
865 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
866 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
867 papi->query_keepalive_factor (papi->cls))),
876 GST_neighbours_notify_data_sent (n->primary_address.address,
877 n->primary_address.session,
879 GNUNET_break (NULL != papi);
885 * Clear the primary address of a neighbour since this address is not
886 * valid anymore and notify monitoring about it
888 * @param n the neighbour
891 unset_primary_address (struct NeighbourMapEntry *n)
893 /* Notify monitoring about change */
894 if (NULL == n->primary_address.address)
896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
897 "Disabling primary address\n");
898 neighbours_changed_notification (&n->id,
899 n->primary_address.address,
902 GNUNET_BANDWIDTH_value_init (0),
903 GNUNET_BANDWIDTH_value_init (0));
904 free_address (&n->primary_address);
909 * Free a neighbour map entry.
911 * @param n entry to free
914 free_neighbour (struct NeighbourMapEntry *n)
916 struct MessageQueue *mq;
918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
919 "Freeing neighbour state of peer `%s'\n",
920 GNUNET_i2s (&n->id));
921 n->is_active = NULL; /* always free'd by its own continuation! */
923 /* fail messages currently in the queue */
924 while (NULL != (mq = n->messages_head))
926 GNUNET_CONTAINER_DLL_remove (n->messages_head,
929 if (NULL != mq->cont)
930 mq->cont (mq->cont_cls,
932 mq->message_buf_size,
936 /* Mark peer as disconnected */
937 set_state_and_timeout (n,
938 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED,
939 GNUNET_TIME_UNIT_FOREVER_ABS);
940 /* free addresses and mark as unused */
941 unset_primary_address (n);
943 if (NULL != n->alternative_address.address)
945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
946 "Cleaning up alternative address\n");
947 free_address (&n->alternative_address);
949 GNUNET_assert (GNUNET_YES ==
950 GNUNET_CONTAINER_multipeermap_remove (neighbours,
953 /* Cancel address requests for this peer */
954 if (NULL != n->suggest_handle)
956 GNUNET_ATS_connectivity_suggest_cancel (n->suggest_handle);
957 n->suggest_handle = NULL;
960 /* Cancel the disconnect task */
961 if (NULL != n->delayed_disconnect_task)
963 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
964 n->delayed_disconnect_task = NULL;
967 /* Cancel the master task */
970 GNUNET_SCHEDULER_cancel (n->task);
973 /* free rest of memory */
979 * Function called when the 'DISCONNECT' message has been sent by the
980 * plugin. Frees the neighbour --- if the entry still exists.
983 * @param target identity of the neighbour that was disconnected
984 * @param result #GNUNET_OK if the disconnect got out successfully
985 * @param payload bytes payload
986 * @param physical bytes on wire
989 send_disconnect_cont (void *cls,
990 const struct GNUNET_PeerIdentity *target,
995 struct NeighbourMapEntry *n;
997 n = lookup_neighbour (target);
999 return; /* already gone */
1000 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1001 return; /* have created a fresh entry since */
1002 if (NULL != n->task)
1003 GNUNET_SCHEDULER_cancel (n->task);
1004 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1009 * Transmit a DISCONNECT message to the other peer.
1011 * @param n neighbour to send DISCONNECT message.
1014 send_disconnect (struct NeighbourMapEntry *n)
1016 struct GNUNET_ATS_SessionDisconnectMessage disconnect_msg;
1018 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1019 "Sending DISCONNECT message to peer `%4s'\n",
1020 GNUNET_i2s (&n->id));
1021 disconnect_msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionDisconnectMessage));
1022 disconnect_msg.header.type =
1023 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1024 disconnect_msg.reserved = htonl (0);
1025 disconnect_msg.purpose.size =
1026 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1027 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1028 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1029 disconnect_msg.purpose.purpose =
1030 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1031 disconnect_msg.timestamp =
1032 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1033 disconnect_msg.public_key = GST_my_identity.public_key;
1034 GNUNET_assert (GNUNET_OK ==
1035 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1036 &disconnect_msg.purpose,
1037 &disconnect_msg.signature));
1039 (void) send_with_session (n,
1041 sizeof (disconnect_msg),
1043 GNUNET_TIME_UNIT_FOREVER_REL,
1045 &send_disconnect_cont,
1047 GNUNET_STATISTICS_update (GST_stats,
1048 gettext_noop ("# DISCONNECT messages sent"),
1055 * Disconnect from the given neighbour, clean up the record.
1057 * @param n neighbour to disconnect from
1060 disconnect_neighbour (struct NeighbourMapEntry *n)
1062 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1063 "Disconnecting from peer %s in state %s\n",
1064 GNUNET_i2s (&n->id),
1065 GNUNET_TRANSPORT_ps2s (n->state));
1066 /* depending on state, notify neighbour and/or upper layers of this peer
1070 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1071 case GNUNET_TRANSPORT_PS_INIT_ATS:
1072 /* other peer is completely unaware of us, no need to send DISCONNECT */
1075 case GNUNET_TRANSPORT_PS_SYN_SENT:
1076 send_disconnect (n);
1077 set_state_and_timeout (n,
1078 GNUNET_TRANSPORT_PS_DISCONNECT,
1079 GNUNET_TIME_UNIT_FOREVER_ABS);
1081 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1082 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1085 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1086 /* we DID ACK the other peer's request, must send DISCONNECT */
1087 send_disconnect (n);
1088 set_state_and_timeout (n,
1089 GNUNET_TRANSPORT_PS_DISCONNECT,
1090 GNUNET_TIME_UNIT_FOREVER_ABS);
1092 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1093 case GNUNET_TRANSPORT_PS_CONNECTED:
1094 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1095 /* we are currently connected, need to send disconnect and do
1096 internal notifications and update statistics */
1097 send_disconnect (n);
1098 set_state_and_timeout (n,
1099 GNUNET_TRANSPORT_PS_DISCONNECT,
1100 GNUNET_TIME_UNIT_FOREVER_ABS);
1102 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1103 /* Disconnecting while waiting for an ATS address to reconnect,
1104 * cannot send DISCONNECT */
1107 case GNUNET_TRANSPORT_PS_DISCONNECT:
1108 /* already disconnected, ignore */
1110 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1111 /* already cleaned up, how did we get here!? */
1115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1116 "Unhandled state `%s'\n",
1117 GNUNET_TRANSPORT_ps2s (n->state));
1121 /* schedule timeout to clean up */
1122 if (NULL != n->task)
1123 GNUNET_SCHEDULER_cancel (n->task);
1124 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1131 * Change the incoming quota for the given peer. Updates
1132 * our own receive rate and informs the neighbour about
1135 * @param n neighbour entry to change qutoa for
1136 * @param quota new quota
1139 set_incoming_quota (struct NeighbourMapEntry *n,
1140 struct GNUNET_BANDWIDTH_Value32NBO quota)
1142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1143 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
1144 ntohl (quota.value__), GNUNET_i2s (&n->id));
1145 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
1147 if (0 != ntohl (quota.value__))
1149 struct GNUNET_ATS_SessionQuotaMessage sqm;
1151 sqm.header.size = htons (sizeof (struct GNUNET_ATS_SessionQuotaMessage));
1152 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA);
1153 sqm.quota = quota.value__;
1154 (void) send_with_session (n,
1158 GNUNET_TIME_UNIT_FOREVER_REL,
1163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1164 "Disconnecting peer `%4s' due to SET_QUOTA\n",
1165 GNUNET_i2s (&n->id));
1166 if (GNUNET_YES == test_connected (n))
1167 GNUNET_STATISTICS_update (GST_stats,
1168 gettext_noop ("# disconnects due to quota of 0"),
1170 disconnect_neighbour (n);
1175 * Initialize the primary address of a neighbour
1177 * @param n the neighbour
1178 * @param address address of the other peer, NULL if other peer
1180 * @param session session to use (or NULL, in which case an
1181 * address must be setup)
1182 * @param bandwidth_in inbound quota to be used when connection is up
1183 * @param bandwidth_out outbound quota to be used when connection is up
1186 set_primary_address (struct NeighbourMapEntry *n,
1187 const struct GNUNET_HELLO_Address *address,
1188 struct GNUNET_ATS_Session *session,
1189 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1190 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1192 if (session == n->primary_address.session)
1194 GST_validation_set_address_use (n->primary_address.address,
1196 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
1198 n->primary_address.bandwidth_in = bandwidth_in;
1199 set_incoming_quota (n,
1202 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
1204 n->primary_address.bandwidth_out = bandwidth_out;
1205 send_outbound_quota_to_clients (n);
1209 if ( (NULL != n->primary_address.address) &&
1210 (0 == GNUNET_HELLO_address_cmp (address,
1211 n->primary_address.address)) )
1216 if (NULL == session)
1219 GST_ats_block_address (address,
1223 if (NULL != n->primary_address.address)
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "Replacing existing primary address with another one\n");
1227 free_address (&n->primary_address);
1229 n->primary_address.address = GNUNET_HELLO_address_copy (address);
1230 n->primary_address.bandwidth_in = bandwidth_in;
1231 n->primary_address.bandwidth_out = bandwidth_out;
1232 n->primary_address.session = session;
1233 n->primary_address.keep_alive_nonce = 0;
1234 GNUNET_assert (GNUNET_YES ==
1235 GST_ats_is_known (n->primary_address.address,
1236 n->primary_address.session));
1237 /* subsystems about address use */
1238 GST_validation_set_address_use (n->primary_address.address,
1240 set_incoming_quota (n,
1242 send_outbound_quota_to_clients (n);
1243 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1244 "Neighbour `%s' switched to address `%s'\n",
1245 GNUNET_i2s (&n->id),
1246 GST_plugins_a2s(address));
1248 neighbours_changed_notification (&n->id,
1249 n->primary_address.address,
1252 n->primary_address.bandwidth_in,
1253 n->primary_address.bandwidth_out);
1258 * We're done with our transmission attempt, continue processing.
1260 * @param cls the `struct MessageQueue` of the message
1261 * @param receiver intended receiver
1262 * @param success whether it worked or not
1263 * @param size_payload bytes payload sent
1264 * @param physical bytes sent on wire
1267 transmit_send_continuation (void *cls,
1268 const struct GNUNET_PeerIdentity *receiver,
1270 size_t size_payload,
1273 struct MessageQueue *mq = cls;
1274 struct NeighbourMapEntry *n;
1276 if (NULL == (n = lookup_neighbour (receiver)))
1278 if (NULL != mq->cont)
1279 mq->cont (mq->cont_cls,
1280 GNUNET_SYSERR /* not connected */,
1284 return; /* disconnect or other error while transmitting, can happen */
1286 if (n->is_active == mq)
1288 /* this is still "our" neighbour, remove us from its queue
1289 and allow it to send the next message now */
1290 n->is_active = NULL;
1291 if (NULL != n->task)
1292 GNUNET_SCHEDULER_cancel (n->task);
1293 n->task = GNUNET_SCHEDULER_add_now (&master_task,
1296 if (bytes_in_send_queue < mq->message_buf_size)
1298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1299 "Bytes_in_send_queue `%llu', Message_size %u, result: %s, payload %u, on wire %u\n",
1300 bytes_in_send_queue,
1301 (unsigned int) mq->message_buf_size,
1302 (GNUNET_OK == success) ? "OK" : "FAIL",
1303 (unsigned int) size_payload,
1304 (unsigned int) physical);
1308 GNUNET_break (size_payload == mq->message_buf_size);
1309 bytes_in_send_queue -= mq->message_buf_size;
1310 GNUNET_STATISTICS_set (GST_stats,
1311 gettext_noop ("# bytes in message queue for other peers"),
1312 bytes_in_send_queue,
1314 if (GNUNET_OK == success)
1315 GNUNET_STATISTICS_update (GST_stats,
1316 gettext_noop ("# messages transmitted to other peers"),
1320 GNUNET_STATISTICS_update (GST_stats,
1322 ("# transmission failures for messages to other peers"),
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1326 GNUNET_i2s (receiver),
1327 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1328 (unsigned int) mq->message_buf_size,
1329 (success == GNUNET_OK) ? "success" : "FAILURE");
1330 if (NULL != mq->cont)
1331 mq->cont (mq->cont_cls,
1340 * Check the message list for the given neighbour and if we can
1341 * send a message, do so. This function should only be called
1342 * if the connection is at least generally ready for transmission.
1343 * While we will only send one message at a time, no bandwidth
1344 * quota management is performed here. If a message was given to
1345 * the plugin, the continuation will automatically re-schedule
1346 * the 'master' task once the next message might be transmitted.
1348 * @param n target peer for which to transmit
1351 try_transmission_to_peer (struct NeighbourMapEntry *n)
1353 struct MessageQueue *mq;
1354 struct GNUNET_TIME_Relative timeout;
1356 if (NULL == n->primary_address.address)
1358 /* no address, why are we here? */
1362 if ((0 == n->primary_address.address->address_length) &&
1363 (NULL == n->primary_address.session))
1365 /* no address, why are we here? */
1369 if (NULL != n->is_active)
1371 /* transmission already pending */
1375 /* timeout messages from the queue that are past their due date */
1376 while (NULL != (mq = n->messages_head))
1378 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1379 if (timeout.rel_value_us > 0)
1381 GNUNET_STATISTICS_update (GST_stats,
1382 gettext_noop ("# messages timed out while in transport queue"),
1385 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1389 transmit_send_continuation (mq,
1392 mq->message_buf_size,
1396 return; /* no more messages */
1397 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Giving message with %u bytes to plugin session %p\n",
1404 (unsigned int) mq->message_buf_size,
1405 n->primary_address.session);
1406 (void) send_with_session (n,
1408 mq->message_buf_size,
1412 &transmit_send_continuation,
1418 * Send keepalive message to the neighbour. Must only be called
1419 * if we are on 'connected' state or while trying to switch addresses.
1420 * Will internally determine if a keepalive is truly needed (so can
1421 * always be called).
1423 * @param n neighbour that went idle and needs a keepalive
1426 send_keepalive (struct NeighbourMapEntry *n)
1428 struct GNUNET_ATS_SessionKeepAliveMessage m;
1429 struct GNUNET_TIME_Relative timeout;
1432 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1433 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state));
1434 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1435 return; /* no keepalive needed at this time */
1437 nonce = 0; /* 0 indicates 'not set' */
1439 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 "Sending KEEPALIVE to peer `%s' with nonce %u\n",
1444 GNUNET_i2s (&n->id),
1446 m.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1447 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1448 m.nonce = htonl (nonce);
1450 timeout = send_with_session (n,
1453 UINT32_MAX /* priority */,
1454 GNUNET_TIME_UNIT_FOREVER_REL,
1457 GNUNET_STATISTICS_update (GST_stats,
1458 gettext_noop ("# KEEPALIVES sent"),
1461 n->primary_address.keep_alive_nonce = nonce;
1462 n->expect_latency_response = GNUNET_YES;
1463 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1464 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1469 * Keep the connection to the given neighbour alive longer,
1470 * we received a KEEPALIVE (or equivalent); send a response.
1472 * @param neighbour neighbour to keep alive (by sending keep alive response)
1473 * @param m the keep alive message containing the nonce to respond to
1476 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1477 const struct GNUNET_MessageHeader *m)
1479 struct NeighbourMapEntry *n;
1480 const struct GNUNET_ATS_SessionKeepAliveMessage *msg_in;
1481 struct GNUNET_ATS_SessionKeepAliveMessage msg;
1483 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1485 GNUNET_break_op (0);
1489 msg_in = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1490 if (NULL == (n = lookup_neighbour (neighbour)))
1492 GNUNET_STATISTICS_update (GST_stats,
1494 ("# KEEPALIVE messages discarded (peer unknown)"),
1498 if (NULL == n->primary_address.session)
1500 GNUNET_STATISTICS_update (GST_stats,
1502 ("# KEEPALIVE messages discarded (no session)"),
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Received KEEPALIVE request from peer `%s' with nonce %u\n",
1509 GNUNET_i2s (&n->id),
1510 ntohl (msg_in->nonce));
1511 GNUNET_STATISTICS_update (GST_stats,
1512 gettext_noop ("# KEEPALIVES received in good order"),
1516 /* send reply to allow neighbour to measure latency */
1517 msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1519 msg.nonce = msg_in->nonce;
1520 (void) send_with_session (n,
1522 sizeof (struct GNUNET_ATS_SessionKeepAliveMessage),
1523 UINT32_MAX /* priority */,
1524 GNUNET_TIME_UNIT_FOREVER_REL,
1531 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1532 * latency to this peer. Pass the updated information (existing ats
1533 * plus calculated latency) to ATS.
1535 * @param neighbour neighbour to keep alive
1536 * @param m the message containing the keep alive response
1539 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1540 const struct GNUNET_MessageHeader *m)
1542 struct NeighbourMapEntry *n;
1543 const struct GNUNET_ATS_SessionKeepAliveMessage *msg;
1544 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1545 struct GNUNET_TIME_Relative latency;
1547 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1549 GNUNET_break_op (0);
1553 msg = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1554 if (NULL == (n = lookup_neighbour (neighbour)))
1556 GNUNET_STATISTICS_update (GST_stats,
1557 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not connected)"),
1562 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1563 (GNUNET_YES != n->expect_latency_response) )
1565 GNUNET_STATISTICS_update (GST_stats,
1566 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not expected)"),
1571 if (NULL == n->primary_address.address)
1573 GNUNET_STATISTICS_update (GST_stats,
1574 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (address changed)"),
1579 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1581 if (0 == n->primary_address.keep_alive_nonce)
1582 GNUNET_STATISTICS_update (GST_stats,
1583 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (no nonce)"),
1587 GNUNET_STATISTICS_update (GST_stats,
1588 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (bad nonce)"),
1593 GNUNET_STATISTICS_update (GST_stats,
1594 gettext_noop ("# KEEPALIVE_RESPONSEs received (OK)"),
1599 /* Update session timeout here */
1600 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Updating session for peer `%s' for session %p\n",
1604 GNUNET_i2s (&n->id),
1605 n->primary_address.session);
1606 papi->update_session_timeout (papi->cls,
1608 n->primary_address.session);
1615 n->primary_address.keep_alive_nonce = 0;
1616 n->expect_latency_response = GNUNET_NO;
1617 set_state_and_timeout (n,
1619 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1621 latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1623 "Received KEEPALIVE_RESPONSE from peer `%s', latency is %s\n",
1624 GNUNET_i2s (&n->id),
1625 GNUNET_STRINGS_relative_time_to_string (latency,
1627 GST_ats_update_delay (n->primary_address.address,
1628 GNUNET_TIME_relative_divide (latency,
1634 * We have received a message from the given sender. How long should
1635 * we delay before receiving more? (Also used to keep the peer marked
1638 * @param sender sender of the message
1639 * @param size size of the message
1640 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1641 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1642 * #GNUNET_SYSERR if the connection is not fully up yet
1643 * @return how long to wait before reading more from this sender
1645 struct GNUNET_TIME_Relative
1646 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
1650 struct NeighbourMapEntry *n;
1651 struct GNUNET_TIME_Relative ret;
1653 if (NULL == neighbours)
1655 *do_forward = GNUNET_NO;
1656 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1658 if (NULL == (n = lookup_neighbour (sender)))
1660 GNUNET_STATISTICS_update (GST_stats,
1661 gettext_noop ("# messages discarded due to lack of neighbour record"),
1664 *do_forward = GNUNET_NO;
1665 return GNUNET_TIME_UNIT_ZERO;
1667 if (! test_connected (n))
1669 *do_forward = GNUNET_SYSERR;
1670 return GNUNET_TIME_UNIT_ZERO;
1672 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1674 n->quota_violation_count++;
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1677 n->in_tracker.available_bytes_per_s__,
1678 n->quota_violation_count);
1679 /* Discount 32k per violation */
1680 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1684 if (n->quota_violation_count > 0)
1686 /* try to add 32k back */
1687 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1688 n->quota_violation_count--;
1691 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1693 GNUNET_STATISTICS_update (GST_stats,
1695 ("# bandwidth quota violations by other peers"),
1697 *do_forward = GNUNET_NO;
1698 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1700 *do_forward = GNUNET_YES;
1701 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1702 if (ret.rel_value_us > 0)
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "Throttling read (%lld bytes excess at %u b/s), waiting %s before reading more.\n",
1706 (long long) n->in_tracker.consumption_since_last_update__,
1707 (unsigned int) n->in_tracker.available_bytes_per_s__,
1708 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1709 GNUNET_STATISTICS_update (GST_stats,
1710 gettext_noop ("# ms throttling suggested"),
1711 (int64_t) ret.rel_value_us / 1000LL,
1719 * Transmit a message to the given target using the active connection.
1721 * @param target destination
1722 * @param msg message to send
1723 * @param msg_size number of bytes in msg
1724 * @param timeout when to fail with timeout
1725 * @param cont function to call when done
1726 * @param cont_cls closure for @a cont
1729 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
1732 struct GNUNET_TIME_Relative timeout,
1733 GST_NeighbourSendContinuation cont,
1736 struct NeighbourMapEntry *n;
1737 struct MessageQueue *mq;
1739 /* All ove these cases should never happen; they are all API violations.
1740 But we check anyway, just to be sure. */
1741 if (NULL == (n = lookup_neighbour (target)))
1751 if (GNUNET_YES != test_connected (n))
1761 bytes_in_send_queue += msg_size;
1762 GNUNET_STATISTICS_set (GST_stats,
1764 ("# bytes in message queue for other peers"),
1765 bytes_in_send_queue, GNUNET_NO);
1766 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1768 mq->cont_cls = cont_cls;
1769 memcpy (&mq[1], msg, msg_size);
1770 mq->message_buf = (const char *) &mq[1];
1771 mq->message_buf_size = msg_size;
1772 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1775 "Enqueueing %u bytes to send to peer %s\n",
1776 (unsigned int) msg_size,
1777 GNUNET_i2s (target));
1778 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
1781 if (NULL != n->task)
1782 GNUNET_SCHEDULER_cancel (n->task);
1783 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1788 * Continuation called from our attempt to transmitted our
1789 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN to the specified @a
1790 * target. Continue processing based on the @a result. Specifically,
1791 * if we failed to transmit, discard the address we used.
1794 * @param target which peer received the transmission
1795 * @param result #GNUNET_OK if sending worked
1796 * @param size_payload how many bytes of payload were sent (ignored)
1797 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1800 send_session_syn_cont (void *cls,
1801 const struct GNUNET_PeerIdentity *target,
1803 size_t size_payload,
1804 size_t size_on_wire)
1806 struct NeighbourMapEntry *n;
1808 n = lookup_neighbour (target);
1811 /* SYN continuation was called after neighbor was freed,
1812 * for example due to a time out for the state or the session
1813 * used was already terminated: nothing to do here... */
1817 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1818 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1819 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1821 /* SYN continuation was called after neighbor changed state,
1822 * for example due to a time out for the state or the session
1823 * used was already terminated: nothing to do here... */
1826 if (GNUNET_OK == result)
1829 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1830 _("Failed to send SYN message to peer `%s'\n"),
1831 GNUNET_i2s (target));
1833 case GNUNET_TRANSPORT_PS_SYN_SENT:
1834 /* Remove address and request an additional one */
1835 unset_primary_address (n);
1836 set_state_and_timeout (n,
1837 GNUNET_TRANSPORT_PS_INIT_ATS,
1838 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1840 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1841 /* Remove address and request an additional one */
1842 unset_primary_address (n);
1843 set_state_and_timeout (n,
1844 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1845 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1847 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1848 /* Remove address and request and go back to primary address */
1849 GNUNET_STATISTICS_update (GST_stats,
1850 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN CONT)"),
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1854 "Switch failed, cleaning up alternative address\n");
1855 free_address (&n->alternative_address);
1856 set_state_and_timeout (n,
1857 GNUNET_TRANSPORT_PS_CONNECTED,
1858 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1861 disconnect_neighbour (n);
1868 * Send a SYN message via the given address.
1870 * @param na address to use
1873 send_syn (struct NeighbourAddress *na)
1875 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1876 struct TransportSynMessage connect_msg;
1877 struct NeighbourMapEntry *n;
1879 GNUNET_assert (NULL != na->session);
1880 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1881 "Sending SYN message to peer `%s' at %s\n",
1882 GNUNET_i2s (&na->address->peer),
1883 GST_plugins_a2s (na->address));
1885 papi = GST_plugins_find (na->address->transport_name);
1886 GNUNET_assert (NULL != papi);
1887 GNUNET_STATISTICS_update (GST_stats,
1889 ("# SYN messages sent"),
1891 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1892 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1893 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1894 connect_msg.reserved = htonl (0);
1895 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1897 papi->send (papi->cls,
1899 (const char *) &connect_msg,
1900 sizeof (struct TransportSynMessage),
1902 SETUP_CONNECTION_TIMEOUT,
1903 &send_session_syn_cont, NULL))
1905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1906 _("Failed to transmit SYN message to %s\n"),
1907 GST_plugins_a2s (na->address));
1908 n = lookup_neighbour (&na->address->peer);
1915 case GNUNET_TRANSPORT_PS_SYN_SENT:
1916 /* Remove address and request and additional one */
1917 GNUNET_assert (na == &n->primary_address);
1918 unset_primary_address (n);
1919 set_state_and_timeout (n,
1920 GNUNET_TRANSPORT_PS_INIT_ATS,
1921 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1922 /* Hard failure to send the SYN message with this address:
1923 Destroy address and session */
1925 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1926 /* Remove address and request an additional one */
1927 GNUNET_assert (na == &n->primary_address);
1928 unset_primary_address (n);
1929 set_state_and_timeout (n,
1930 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1931 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1933 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1934 GNUNET_assert (na == &n->alternative_address);
1935 GNUNET_STATISTICS_update (GST_stats,
1936 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN)"),
1939 /* Remove address and request an additional one */
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 "Switch failed, cleaning up alternative address\n");
1942 free_address (&n->alternative_address);
1943 set_state_and_timeout (n,
1944 GNUNET_TRANSPORT_PS_CONNECTED,
1945 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1949 disconnect_neighbour (n);
1954 GST_neighbours_notify_data_sent (na->address,
1956 sizeof (struct TransportSynMessage));
1961 * Continuation called from our attempt to transmitted our
1962 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK to the specified @a
1963 * target. Continue processing based on the @a result. Specifically,
1964 * if we failed to transmit, discard the address we used.
1967 * @param target which peer received the transmission
1968 * @param result #GNUNET_OK if sending worked
1969 * @param size_payload how many bytes of payload were sent (ignored)
1970 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1973 send_session_syn_ack_cont (void *cls,
1974 const struct GNUNET_PeerIdentity *target,
1976 size_t size_payload,
1977 size_t size_on_wire)
1979 struct NeighbourMapEntry *n;
1981 n = lookup_neighbour (target);
1984 /* SYN_ACK continuation was called after neighbor was freed,
1985 * for example due to a time out for the state or the session
1986 * used was already terminated: nothing to do here... */
1990 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1992 /* SYN_ACK continuation was called after neighbor changed state,
1993 * for example due to a time out for the state or the session
1994 * used was already terminated: nothing to do here... */
1997 if (GNUNET_OK == result)
2000 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2001 _("Failed to send SYN_ACK message to peer `%s' using address `%s'\n"),
2002 GNUNET_i2s (target),
2003 GST_plugins_a2s (n->primary_address.address));
2005 /* Remove address and request and additional one */
2006 /* FIXME: what if the neighbour's primary address
2007 changed in the meantime? Might want to instead
2008 pass "something" around in closure to be sure. */
2009 unset_primary_address (n);
2010 n->ack_state = ACK_SEND_SYN_ACK;
2011 set_state_and_timeout (n,
2012 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2013 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2018 * Send a SYN_ACK message via the given address.
2020 * @param na address and session to use
2021 * @param timestamp timestamp to use for the ACK message
2022 * @return #GNUNET_SYSERR if sending immediately failed, #GNUNET_OK otherwise
2025 send_syn_ack_message (struct NeighbourAddress *na,
2026 struct GNUNET_TIME_Absolute timestamp)
2028 const struct GNUNET_HELLO_Address *address = na->address;
2029 struct GNUNET_ATS_Session *session = na->session;
2030 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2031 struct TransportSynMessage connect_msg;
2032 struct NeighbourMapEntry *n;
2034 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2035 "Sending SYN_ACK to peer `%s'\n",
2036 GNUNET_i2s (&address->peer));
2038 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2043 if (NULL == session)
2044 session = papi->get_session (papi->cls,
2046 if (NULL == session)
2051 GST_ats_new_session (address,
2053 GNUNET_STATISTICS_update (GST_stats,
2055 ("# SYN_ACK messages sent"),
2057 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
2058 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
2059 connect_msg.reserved = htonl (0);
2060 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
2062 if (GNUNET_SYSERR ==
2063 papi->send (papi->cls,
2065 (const char *) &connect_msg,
2066 sizeof (struct TransportSynMessage),
2068 GNUNET_TIME_UNIT_FOREVER_REL,
2069 &send_session_syn_ack_cont, NULL))
2071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2072 _("Failed to transmit SYN_ACK message to %s\n"),
2073 GST_plugins_a2s (address));
2075 n = lookup_neighbour (&address->peer);
2081 /* Remove address and request and additional one */
2082 unset_primary_address (n);
2083 n->ack_state = ACK_SEND_SYN_ACK;
2084 set_state_and_timeout (n,
2085 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2086 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2093 * Function called by the bandwidth tracker for a peer whenever
2094 * the tracker's state changed such that we need to recalculate
2095 * the delay for flow control. We calculate the latest delay
2096 * and inform the plugin (if applicable).
2098 * @param cls the `struct NeighbourMapEntry` to update calculations for
2101 inbound_bw_tracker_update (void *cls)
2103 struct NeighbourMapEntry *n = cls;
2104 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2105 struct GNUNET_TIME_Relative delay;
2108 if (NULL == n->primary_address.address)
2109 return; /* not active, ignore */
2110 papi = GST_plugins_find (n->primary_address.address->transport_name);
2111 GNUNET_assert (NULL != papi);
2112 if (NULL == papi->update_inbound_delay)
2114 delay = GST_neighbours_calculate_receive_delay (&n->id,
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2118 "New inbound delay for peer `%s' is %llu ms\n",
2119 GNUNET_i2s (&n->id),
2120 (unsigned long long) delay.rel_value_us / 1000LL);
2121 papi->update_inbound_delay (papi->cls,
2123 n->primary_address.session,
2129 * Create a fresh entry in the neighbour map for the given peer
2131 * @param peer peer to create an entry for
2132 * @return new neighbour map entry
2134 static struct NeighbourMapEntry *
2135 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2137 struct NeighbourMapEntry *n;
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2140 "Creating new neighbour entry for `%s'\n",
2143 memcmp (&GST_my_identity,
2145 sizeof (struct GNUNET_PeerIdentity)));
2146 n = GNUNET_new (struct NeighbourMapEntry);
2148 n->ack_state = ACK_UNDEFINED;
2149 n->last_util_transmission = GNUNET_TIME_absolute_get();
2150 n->neighbour_receive_quota = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2151 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2152 &inbound_bw_tracker_update,
2154 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2155 MAX_BANDWIDTH_CARRY_S);
2156 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2157 set_state_and_timeout (n,
2158 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2159 GNUNET_TIME_UNIT_FOREVER_ABS);
2160 GNUNET_assert (GNUNET_OK ==
2161 GNUNET_CONTAINER_multipeermap_put (neighbours,
2164 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2165 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2174 * Entry in a DLL we use to keep track of pending blacklist checks.
2176 struct BlacklistCheckSwitchContext
2181 struct BlacklistCheckSwitchContext *prev;
2186 struct BlacklistCheckSwitchContext *next;
2189 * Handle to the blacklist check we are performing.
2191 struct GST_BlacklistCheck *blc;
2194 * Inbound bandwidth that was assigned to @e address.
2196 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2199 * Outbound bandwidth that was assigned to @e address.
2201 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2206 * We received a 'SYN' message from the other peer.
2207 * Consider switching to it.
2209 * @param message possibly a `struct TransportSynMessage` (check format)
2210 * @param peer identity of the peer to switch the address for
2211 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2214 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2215 const struct GNUNET_PeerIdentity *peer)
2217 const struct TransportSynMessage *scm;
2218 struct NeighbourMapEntry *n;
2219 struct GNUNET_TIME_Absolute ts;
2221 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2223 GNUNET_break_op (0);
2224 return GNUNET_SYSERR;
2226 GNUNET_STATISTICS_update (GST_stats,
2228 ("# SYN messages received"),
2230 if (NULL == neighbours)
2232 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2233 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2235 return GNUNET_OK; /* we're shutting down */
2237 scm = (const struct TransportSynMessage *) message;
2238 GNUNET_break_op (0 == ntohl (scm->reserved));
2239 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2240 n = lookup_neighbour (peer);
2243 /* This is a new neighbour and set to not connected */
2244 n = setup_neighbour (peer);
2247 /* Remember this SYN message in neighbour */
2248 n->ack_state = ACK_SEND_SYN_ACK;
2249 n->connect_ack_timestamp = ts;
2251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2252 "Received SYN for peer `%s' in state %s/%s\n",
2254 GNUNET_TRANSPORT_ps2s (n->state),
2255 print_ack_state (n->ack_state));
2259 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2260 /* Request an address from ATS to send SYN_ACK to this peer */
2261 set_state_and_timeout (n,
2262 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2263 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2265 case GNUNET_TRANSPORT_PS_INIT_ATS:
2266 /* SYN message takes priority over us asking ATS for address:
2267 * Wait for ATS to suggest an address and send SYN_ACK */
2268 set_state_and_timeout (n,
2269 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2270 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2272 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2273 /* We already wait for an address to send an SYN_ACK */
2275 case GNUNET_TRANSPORT_PS_SYN_SENT:
2276 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2277 /* Send ACK immediately */
2278 n->ack_state = ACK_SEND_ACK;
2279 send_syn_ack_message (&n->primary_address,
2282 case GNUNET_TRANSPORT_PS_CONNECTED:
2283 /* we are already connected and can thus send the ACK immediately */
2284 GNUNET_assert (NULL != n->primary_address.address);
2285 GNUNET_assert (NULL != n->primary_address.session);
2286 n->ack_state = ACK_SEND_ACK;
2287 send_syn_ack_message (&n->primary_address,
2290 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2291 /* We wait for ATS address suggestion */
2293 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2294 /* We received a SYN message while waiting for a SYN_ACK in fast
2295 * reconnect. Send SYN_ACK immediately */
2296 n->ack_state = ACK_SEND_ACK;
2297 send_syn_ack_message (&n->primary_address,
2298 n->connect_ack_timestamp);
2300 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2301 /* We are already connected and can thus send the ACK immediately;
2302 still, it can never hurt to have an alternative address, so also
2303 tell ATS about it */
2304 GNUNET_assert (NULL != n->primary_address.address);
2305 GNUNET_assert (NULL != n->primary_address.session);
2306 n->ack_state = ACK_SEND_ACK;
2307 send_syn_ack_message (&n->primary_address,
2310 case GNUNET_TRANSPORT_PS_DISCONNECT:
2311 /* Get rid of remains and re-try */
2313 n = setup_neighbour (peer);
2314 /* Remember the SYN time stamp for ACK message */
2315 n->ack_state = ACK_SEND_SYN_ACK;
2316 n->connect_ack_timestamp = ts;
2317 /* Request an address for the peer */
2318 set_state_and_timeout (n,
2319 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2320 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2322 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2323 /* should not be possible */
2327 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2328 "Unhandled state `%s'\n",
2329 GNUNET_TRANSPORT_ps2s (n->state));
2331 return GNUNET_SYSERR;
2338 * Check if the given @a address is the same that we are already
2339 * using for the respective neighbour. If so, update the bandwidth
2340 * assignment and possibly the session and return #GNUNET_OK.
2341 * If the new address is different from what the neighbour is
2342 * using right now, return #GNUNET_NO.
2344 * @param address address of the other peer,
2345 * @param session session to use or NULL if transport should initiate a session
2346 * @param bandwidth_in inbound quota to be used when connection is up,
2347 * 0 to disconnect from peer
2348 * @param bandwidth_out outbound quota to be used when connection is up,
2349 * 0 to disconnect from peer
2350 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2351 * #GNUNET_NO if more extensive changes are required (address changed)
2354 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2355 struct GNUNET_ATS_Session *session,
2356 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2357 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2359 struct NeighbourMapEntry *n;
2361 n = lookup_neighbour (&address->peer);
2363 (NULL == n->primary_address.address) ||
2364 (0 != GNUNET_HELLO_address_cmp (address,
2365 n->primary_address.address)) )
2367 /* We are not really switching addresses, but merely adjusting
2368 session and/or bandwidth, can do fast ATS update! */
2369 if (session != n->primary_address.session)
2371 /* switch to a different session, but keeping same address; could
2372 happen if there is a 2nd inbound connection */
2373 n->primary_address.session = session;
2374 GNUNET_assert (GNUNET_YES ==
2375 GST_ats_is_known (n->primary_address.address,
2376 n->primary_address.session));
2378 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
2380 n->primary_address.bandwidth_in = bandwidth_in;
2381 set_incoming_quota (n,
2384 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
2386 n->primary_address.bandwidth_out = bandwidth_out;
2387 send_outbound_quota_to_clients (n);
2394 * We've been asked to switch addresses, and just now got the result
2395 * from the blacklist check to see if this is allowed.
2397 * @param cls the `struct BlacklistCheckSwitchContext` with
2398 * the information about the future address
2399 * @param peer the peer we may switch addresses on
2400 * @param address address associated with the request
2401 * @param session session associated with the request
2402 * @param result #GNUNET_OK if the connection is allowed,
2403 * #GNUNET_NO if not,
2404 * #GNUNET_SYSERR if operation was aborted
2407 switch_address_bl_check_cont (void *cls,
2408 const struct GNUNET_PeerIdentity *peer,
2409 const struct GNUNET_HELLO_Address *address,
2410 struct GNUNET_ATS_Session *session,
2413 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2414 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2415 struct NeighbourMapEntry *n;
2417 if (GNUNET_SYSERR == result)
2420 papi = GST_plugins_find (address->transport_name);
2421 GNUNET_assert (NULL != papi);
2423 if (GNUNET_NO == result)
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2426 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2427 GST_plugins_a2s (address),
2430 GNUNET_STATISTICS_update (GST_stats,
2431 "# ATS suggestions ignored (blacklist denied)",
2434 if (NULL != session)
2435 papi->disconnect_session (papi->cls,
2438 GNUNET_HELLO_address_check_option (address,
2439 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2440 GST_ats_block_address (address,
2446 if (NULL == session)
2448 /* need to create a session, ATS only gave us an address */
2449 session = papi->get_session (papi->cls,
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2452 "Obtained new session for peer `%s' and address '%s': %p\n",
2453 GNUNET_i2s (&address->peer),
2454 GST_plugins_a2s (address),
2456 if (NULL != session)
2457 GST_ats_new_session (address,
2460 if (NULL == session)
2462 /* session creation failed, bad!, fail! */
2463 GNUNET_STATISTICS_update (GST_stats,
2464 "# ATS suggestions ignored (failed to create session)",
2467 /* No session could be obtained, remove blacklist check and clean up */
2468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2469 "Failed to obtain new session for peer `%s' and address '%s'\n",
2470 GNUNET_i2s (&address->peer),
2471 GST_plugins_a2s (address));
2472 GST_ats_block_address (address,
2477 /* We did this check already before going into blacklist, but
2478 it is theoretically possible that the situation changed in
2479 the meantime, hence we check again here */
2481 try_run_fast_ats_update (address,
2483 blc_ctx->bandwidth_in,
2484 blc_ctx->bandwidth_out))
2485 goto cleanup; /* was just a minor update, we're done */
2487 /* check if we also need to setup the neighbour entry */
2488 if (NULL == (n = lookup_neighbour (peer)))
2490 n = setup_neighbour (peer);
2491 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2494 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2495 "Peer `%s' switches to address `%s'\n",
2496 GNUNET_i2s (&address->peer),
2497 GST_plugins_a2s (address));
2501 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2503 GST_ats_block_address (address,
2507 case GNUNET_TRANSPORT_PS_INIT_ATS:
2508 /* We requested an address and ATS suggests one:
2509 * set primary address and send SYN message*/
2510 set_primary_address (n,
2513 blc_ctx->bandwidth_in,
2514 blc_ctx->bandwidth_out);
2515 if (ACK_SEND_SYN_ACK == n->ack_state)
2517 /* Send pending SYN_ACK message */
2518 n->ack_state = ACK_SEND_ACK;
2519 send_syn_ack_message (&n->primary_address,
2520 n->connect_ack_timestamp);
2522 set_state_and_timeout (n,
2523 GNUNET_TRANSPORT_PS_SYN_SENT,
2524 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2525 send_syn (&n->primary_address);
2527 case GNUNET_TRANSPORT_PS_SYN_SENT:
2528 /* ATS suggested a new address while waiting for an SYN_ACK:
2529 * Switch and send new SYN */
2530 /* ATS suggests a different address, switch again */
2531 set_primary_address (n,
2534 blc_ctx->bandwidth_in,
2535 blc_ctx->bandwidth_out);
2536 if (ACK_SEND_SYN_ACK == n->ack_state)
2538 /* Send pending SYN_ACK message */
2539 n->ack_state = ACK_SEND_ACK;
2540 send_syn_ack_message (&n->primary_address,
2541 n->connect_ack_timestamp);
2543 set_state_and_timeout (n,
2544 GNUNET_TRANSPORT_PS_SYN_SENT,
2545 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2546 send_syn (&n->primary_address);
2548 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2549 /* We requested an address and ATS suggests one:
2550 * set primary address and send SYN_ACK message*/
2551 set_primary_address (n,
2554 blc_ctx->bandwidth_in,
2555 blc_ctx->bandwidth_out);
2556 /* Send an ACK message as a response to the SYN msg */
2557 set_state_and_timeout (n,
2558 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2559 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2560 send_syn_ack_message (&n->primary_address,
2561 n->connect_ack_timestamp);
2562 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2563 (ACK_UNDEFINED == n->ack_state) )
2564 n->ack_state = ACK_SEND_ACK;
2566 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2567 /* ATS asks us to switch while we were trying to connect; switch to new
2568 address and check blacklist again */
2569 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2571 n->ack_state = ACK_SEND_ACK;
2572 send_syn_ack_message (&n->primary_address,
2573 n->connect_ack_timestamp);
2575 set_primary_address (n,
2578 blc_ctx->bandwidth_in,
2579 blc_ctx->bandwidth_out);
2580 set_state_and_timeout (n,
2581 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2582 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2584 case GNUNET_TRANSPORT_PS_CONNECTED:
2585 GNUNET_assert (NULL != n->primary_address.address);
2586 GNUNET_assert (NULL != n->primary_address.session);
2587 GNUNET_break (n->primary_address.session != session);
2588 /* ATS asks us to switch a life connection; see if we can get
2589 a SYN_ACK on it before we actually do this! */
2590 set_alternative_address (n,
2593 blc_ctx->bandwidth_in,
2594 blc_ctx->bandwidth_out);
2595 set_state_and_timeout (n,
2596 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2597 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2598 GNUNET_STATISTICS_update (GST_stats,
2599 gettext_noop ("# Attempts to switch addresses"),
2602 send_syn (&n->alternative_address);
2604 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2605 set_primary_address (n,
2608 blc_ctx->bandwidth_in,
2609 blc_ctx->bandwidth_out);
2610 if (ACK_SEND_SYN_ACK == n->ack_state)
2612 /* Send pending SYN_ACK message */
2613 n->ack_state = ACK_SEND_ACK;
2614 send_syn_ack_message (&n->primary_address,
2615 n->connect_ack_timestamp);
2617 set_state_and_timeout (n,
2618 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2619 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2620 send_syn (&n->primary_address);
2622 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2623 /* ATS asks us to switch while we were trying to reconnect; switch to new
2624 address and send SYN again */
2625 set_primary_address (n,
2628 blc_ctx->bandwidth_in,
2629 blc_ctx->bandwidth_out);
2630 set_state_and_timeout (n,
2631 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2632 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2633 send_syn (&n->primary_address);
2635 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2636 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2638 (n->primary_address.session == session) )
2640 /* ATS switches back to still-active session */
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2642 "ATS double-switched, cleaning up alternative address\n");
2643 free_address (&n->alternative_address);
2644 set_state_and_timeout (n,
2645 GNUNET_TRANSPORT_PS_CONNECTED,
2649 /* ATS asks us to switch a life connection, send */
2650 set_alternative_address (n,
2653 blc_ctx->bandwidth_in,
2654 blc_ctx->bandwidth_out);
2655 set_state_and_timeout (n,
2656 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2657 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2658 send_syn (&n->alternative_address);
2660 case GNUNET_TRANSPORT_PS_DISCONNECT:
2661 /* not going to switch addresses while disconnecting */
2662 GNUNET_STATISTICS_update (GST_stats,
2663 "# ATS suggestion ignored (disconnecting)",
2667 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2671 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2672 "Unhandled state `%s'\n",
2673 GNUNET_TRANSPORT_ps2s (n->state));
2678 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2681 GNUNET_free (blc_ctx);
2686 * For the given peer, switch to this address.
2688 * Before accepting this addresses and actively using it, a blacklist check
2691 * If any check fails or the suggestion can somehow not be followed, we
2692 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2693 * could not be satisfied and force ATS to do something else.
2695 * @param address address of the other peer,
2696 * @param session session to use or NULL if transport should initiate a session
2697 * @param bandwidth_in inbound quota to be used when connection is up,
2698 * 0 to disconnect from peer
2699 * @param bandwidth_out outbound quota to be used when connection is up,
2700 * 0 to disconnect from peer
2703 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2704 struct GNUNET_ATS_Session *session,
2705 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2706 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2708 struct GST_BlacklistCheck *blc;
2709 struct BlacklistCheckSwitchContext *blc_ctx;
2711 GNUNET_assert (NULL != address->transport_name);
2713 try_run_fast_ats_update (address,
2719 /* Check if plugin is available */
2720 if (NULL == (GST_plugins_find (address->transport_name)))
2722 /* we don't have the plugin for this address */
2724 GST_ats_block_address (address,
2728 if ((NULL == session) &&
2729 (GNUNET_HELLO_address_check_option (address,
2730 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2732 /* This is a inbound address and we do not have a session to use! */
2734 GST_ats_block_address (address,
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2740 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2741 GST_plugins_a2s (address),
2742 GNUNET_i2s (&address->peer),
2743 (unsigned int) ntohl (bandwidth_in.value__),
2744 (unsigned int) ntohl (bandwidth_out.value__));
2746 /* Perform blacklist check */
2747 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2748 blc_ctx->bandwidth_in = bandwidth_in;
2749 blc_ctx->bandwidth_out = bandwidth_out;
2750 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2753 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2754 address->transport_name,
2755 &switch_address_bl_check_cont,
2766 * Function called to send network utilization data to ATS for
2767 * each active connection.
2770 * @param key peer we send utilization data for
2771 * @param value the `struct NeighbourMapEntry *` with data to send
2772 * @return #GNUNET_OK (continue to iterate)
2775 send_utilization_data (void *cls,
2776 const struct GNUNET_PeerIdentity *key,
2779 struct NeighbourMapEntry *n = value;
2782 struct GNUNET_TIME_Relative delta;
2784 if ( (GNUNET_YES != test_connected (n)) ||
2785 (NULL == n->primary_address.address) )
2787 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2788 GNUNET_TIME_absolute_get ());
2790 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2791 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2793 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2794 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2797 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2801 GST_ats_update_utilization (n->primary_address.address,
2804 n->util_total_bytes_recv = 0;
2805 n->util_total_bytes_sent = 0;
2806 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2812 * Task transmitting utilization in a regular interval
2814 * @param cls the 'struct NeighbourMapEntry' for which we are running
2817 utilization_transmission (void *cls)
2819 util_transmission_tk = NULL;
2820 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
2821 &send_utilization_data,
2823 util_transmission_tk
2824 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2825 &utilization_transmission,
2831 * Track information about data we received from the
2832 * given address (used to notify ATS about our utilization
2833 * of allocated resources).
2835 * @param address the address we got data from
2836 * @param message the message we received (really only the size is used)
2839 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2840 const struct GNUNET_MessageHeader *message)
2842 struct NeighbourMapEntry *n;
2844 n = lookup_neighbour (&address->peer);
2847 n->util_total_bytes_recv += ntohs (message->size);
2852 * Track information about data we transmitted using the given @a
2853 * address and @a session (used to notify ATS about our utilization of
2854 * allocated resources).
2856 * @param address the address we transmitted data to
2857 * @param session session we used to transmit data
2858 * @param message the message we sent (really only the size is used)
2861 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2862 struct GNUNET_ATS_Session *session,
2865 struct NeighbourMapEntry *n;
2867 n = lookup_neighbour (&address->peer);
2870 if (n->primary_address.session != session)
2872 n->util_total_bytes_sent += size;
2877 * Master task run for every neighbour. Performs all of the time-related
2878 * activities (keep alive, send next message, disconnect if idle, finish
2879 * clean up after disconnect).
2881 * @param cls the 'struct NeighbourMapEntry' for which we are running
2884 master_task (void *cls)
2886 struct NeighbourMapEntry *n = cls;
2887 struct GNUNET_TIME_Relative delay;
2890 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2892 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2893 GNUNET_i2s (&n->id),
2894 GNUNET_TRANSPORT_ps2s(n->state),
2895 GNUNET_STRINGS_relative_time_to_string (delay,
2899 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2900 /* invalid state for master task, clean up */
2904 case GNUNET_TRANSPORT_PS_INIT_ATS:
2905 if (0 == delay.rel_value_us)
2907 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2908 "Connection to `%s' timed out waiting for ATS to provide address\n",
2909 GNUNET_i2s (&n->id));
2914 case GNUNET_TRANSPORT_PS_SYN_SENT:
2915 if (0 == delay.rel_value_us)
2917 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2918 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2919 GNUNET_i2s (&n->id));
2920 /* Remove address and request and additional one */
2921 unset_primary_address (n);
2922 set_state_and_timeout (n,
2923 GNUNET_TRANSPORT_PS_INIT_ATS,
2924 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2928 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2929 if (0 == delay.rel_value_us)
2931 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2932 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2933 GNUNET_i2s (&n->id));
2938 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2939 if (0 == delay.rel_value_us)
2941 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2942 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2943 GNUNET_i2s (&n->id));
2944 disconnect_neighbour (n);
2948 case GNUNET_TRANSPORT_PS_CONNECTED:
2949 if (0 == delay.rel_value_us)
2951 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2952 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2953 GNUNET_i2s (&n->id));
2954 disconnect_neighbour (n);
2957 try_transmission_to_peer (n);
2960 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2961 if (0 == delay.rel_value_us)
2963 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2964 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2965 GNUNET_i2s (&n->id));
2966 disconnect_neighbour (n);
2970 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2971 if (0 == delay.rel_value_us)
2973 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2974 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
2975 GNUNET_i2s (&n->id));
2976 disconnect_neighbour (n);
2980 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2981 if (0 == delay.rel_value_us)
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2984 "Switch failed, cleaning up alternative address\n");
2985 free_address (&n->alternative_address);
2986 set_state_and_timeout (n,
2987 GNUNET_TRANSPORT_PS_CONNECTED,
2988 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2990 try_transmission_to_peer (n);
2993 case GNUNET_TRANSPORT_PS_DISCONNECT:
2994 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2995 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2996 GNUNET_i2s (&n->id));
2999 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3000 /* how did we get here!? */
3004 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3005 "Unhandled state `%s'\n",
3006 GNUNET_TRANSPORT_ps2s (n->state));
3010 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3011 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3012 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3014 /* if we are *now* in one of the two states, we're sending
3015 keep alive messages, so we need to consider the keepalive
3016 delay, not just the connection timeout */
3017 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3020 if (NULL == n->task)
3021 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3028 * Send a ACK message to the neighbour to confirm that we
3031 * @param n neighbour to send the ACK to
3034 send_session_ack_message (struct NeighbourMapEntry *n)
3036 struct GNUNET_MessageHeader msg;
3038 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3039 "Sending ACK message to peer `%s'\n",
3040 GNUNET_i2s (&n->id));
3042 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3043 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3044 (void) send_with_session (n,
3046 sizeof (struct GNUNET_MessageHeader),
3048 GNUNET_TIME_UNIT_FOREVER_REL,
3055 * We received a 'SESSION_SYN_ACK' message from the other peer.
3056 * Consider switching to it.
3058 * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
3059 * @param peer identity of the peer to switch the address for
3060 * @param address address of the other peer, NULL if other peer
3062 * @param session session to use (or NULL)
3063 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3066 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3067 const struct GNUNET_HELLO_Address *address,
3068 struct GNUNET_ATS_Session *session)
3070 const struct TransportSynMessage *scm;
3071 struct GNUNET_TIME_Absolute ts;
3072 struct NeighbourMapEntry *n;
3074 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3076 GNUNET_break_op (0);
3077 return GNUNET_SYSERR;
3079 GNUNET_STATISTICS_update (GST_stats,
3081 ("# SYN_ACK messages received"),
3083 scm = (const struct TransportSynMessage *) message;
3084 GNUNET_break_op (ntohl (scm->reserved) == 0);
3085 if (NULL == (n = lookup_neighbour (&address->peer)))
3087 GNUNET_STATISTICS_update (GST_stats,
3089 ("# unexpected SYN_ACK messages (no peer)"),
3091 return GNUNET_SYSERR;
3093 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3094 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3095 GNUNET_i2s (&address->peer),
3096 GNUNET_TRANSPORT_ps2s (n->state),
3097 print_ack_state (n->ack_state));
3098 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3101 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3104 return GNUNET_SYSERR;
3105 case GNUNET_TRANSPORT_PS_INIT_ATS:
3106 GNUNET_STATISTICS_update (GST_stats,
3107 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3111 case GNUNET_TRANSPORT_PS_SYN_SENT:
3112 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3114 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3115 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3118 set_state_and_timeout (n,
3119 GNUNET_TRANSPORT_PS_CONNECTED,
3120 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3121 set_primary_address (n,
3122 n->primary_address.address,
3123 n->primary_address.session,
3124 n->primary_address.bandwidth_in,
3125 n->primary_address.bandwidth_out);
3126 send_session_ack_message (n);
3128 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3129 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3130 GNUNET_STATISTICS_update (GST_stats,
3131 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3135 case GNUNET_TRANSPORT_PS_CONNECTED:
3136 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3137 send_session_ack_message (n);
3139 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3140 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3141 to give us a new address... */
3142 GNUNET_STATISTICS_update (GST_stats,
3143 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3147 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3148 /* Reconnecting with new address address worked; go back to connected! */
3149 set_state_and_timeout (n,
3150 GNUNET_TRANSPORT_PS_CONNECTED,
3151 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3152 send_session_ack_message (n);
3154 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3155 /* new address worked; adopt it and go back to connected! */
3156 set_state_and_timeout (n,
3157 GNUNET_TRANSPORT_PS_CONNECTED,
3158 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3159 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3161 /* Set primary addresses */
3162 set_primary_address (n,
3163 n->alternative_address.address,
3164 n->alternative_address.session,
3165 n->alternative_address.bandwidth_in,
3166 n->alternative_address.bandwidth_out);
3167 GNUNET_STATISTICS_update (GST_stats,
3168 gettext_noop ("# Successful attempts to switch addresses"),
3172 GNUNET_HELLO_address_free (n->alternative_address.address);
3173 memset (&n->alternative_address,
3175 sizeof (n->alternative_address));
3176 send_session_ack_message (n);
3178 case GNUNET_TRANSPORT_PS_DISCONNECT:
3179 GNUNET_STATISTICS_update (GST_stats,
3181 ("# unexpected SYN_ACK messages (disconnecting)"),
3183 return GNUNET_SYSERR;
3184 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3189 "Unhandled state `%s'\n",
3190 GNUNET_TRANSPORT_ps2s (n->state));
3192 return GNUNET_SYSERR;
3199 * A session was terminated. Take note; if needed, try to get
3200 * an alternative address from ATS.
3202 * @param peer identity of the peer where the session died
3203 * @param session session that is gone
3204 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3205 * this session was not in use
3208 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3209 struct GNUNET_ATS_Session *session)
3211 struct NeighbourMapEntry *n;
3213 if (NULL == (n = lookup_neighbour (peer)))
3214 return GNUNET_NO; /* can't affect us */
3215 if (session != n->primary_address.session)
3217 /* Free alternative address */
3218 if (session == n->alternative_address.session)
3220 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3221 set_state_and_timeout (n,
3222 GNUNET_TRANSPORT_PS_CONNECTED,
3224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3225 "Session died, cleaning up alternative address\n");
3226 free_address (&n->alternative_address);
3228 return GNUNET_NO; /* doesn't affect us further */
3231 n->expect_latency_response = GNUNET_NO;
3232 /* The session for neighbour's primary address died */
3235 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3239 case GNUNET_TRANSPORT_PS_INIT_ATS:
3243 case GNUNET_TRANSPORT_PS_SYN_SENT:
3244 /* The session used to send the SYN terminated:
3245 * this implies a connect error*/
3246 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3247 "Failed to send SYN in CONNECT_SENT with `%s' %p: session terminated\n",
3248 GST_plugins_a2s (n->primary_address.address),
3249 n->primary_address.session);
3251 /* Destroy the address since it cannot be used */
3252 unset_primary_address (n);
3253 set_state_and_timeout (n,
3254 GNUNET_TRANSPORT_PS_INIT_ATS,
3255 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3257 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3258 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3259 /* error on inbound session; free neighbour entirely */
3262 case GNUNET_TRANSPORT_PS_CONNECTED:
3263 /* Our primary connection died, try a fast reconnect */
3264 unset_primary_address (n);
3265 set_state_and_timeout (n,
3266 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3267 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3269 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3270 /* we don't have an address, how can it go down? */
3273 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3274 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3275 "Failed to send SYN in RECONNECT_SENT with `%s' %p: session terminated\n",
3276 GST_plugins_a2s (n->primary_address.address),
3277 n->primary_address.session);
3278 /* Destroy the address since it cannot be used */
3279 unset_primary_address (n);
3280 set_state_and_timeout (n,
3281 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3282 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3284 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3285 /* primary went down while we were waiting for SYN_ACK on secondary;
3286 secondary as primary */
3288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3289 "Connection `%s' %p to peer `%s' was terminated while switching, "
3290 "switching to alternative address `%s' %p\n",
3291 GST_plugins_a2s (n->primary_address.address),
3292 n->primary_address.session,
3294 GST_plugins_a2s (n->alternative_address.address),
3295 n->alternative_address.session);
3297 /* Destroy the inbound address since it cannot be used */
3298 free_address (&n->primary_address);
3299 n->primary_address = n->alternative_address;
3300 GNUNET_assert (GNUNET_YES ==
3301 GST_ats_is_known (n->primary_address.address,
3302 n->primary_address.session));
3303 memset (&n->alternative_address,
3305 sizeof (struct NeighbourAddress));
3306 set_state_and_timeout (n,
3307 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3308 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3310 case GNUNET_TRANSPORT_PS_DISCONNECT:
3311 unset_primary_address (n);
3313 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3314 /* neighbour was freed and plugins told to terminate session */
3317 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3318 "Unhandled state `%s'\n",
3319 GNUNET_TRANSPORT_ps2s (n->state));
3323 if (NULL != n->task)
3324 GNUNET_SCHEDULER_cancel (n->task);
3325 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3331 * We received a 'ACK' message from the other peer.
3332 * If we sent a 'SYN_ACK' last, this means we are now
3333 * connected. Otherwise, do nothing.
3335 * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
3336 * @param address address of the other peer
3337 * @param session session to use (or NULL)
3338 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3341 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3342 const struct GNUNET_HELLO_Address *address,
3343 struct GNUNET_ATS_Session *session)
3345 struct NeighbourMapEntry *n;
3347 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3349 GNUNET_break_op (0);
3350 return GNUNET_SYSERR;
3352 GNUNET_STATISTICS_update (GST_stats,
3353 gettext_noop ("# ACK messages received"),
3356 if (NULL == (n = lookup_neighbour (&address->peer)))
3358 GNUNET_break_op (0);
3359 return GNUNET_SYSERR;
3361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3362 "Received ACK for peer `%s' in state %s/%s\n",
3363 GNUNET_i2s (&address->peer),
3364 GNUNET_TRANSPORT_ps2s (n->state),
3365 print_ack_state (n->ack_state));
3367 /* Check if we are in a plausible state for having sent
3368 a SYN_ACK. If not, return, otherwise break.
3370 The remote peers sends a ACK as a response for a SYN_ACK
3374 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3375 now wait for the ACK to finally be connected
3376 - If we sent a SYN_ACK to this peer before */
3378 if ( ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3379 (ACK_SEND_ACK != n->ack_state) ) ||
3380 (NULL == n->primary_address.address) )
3382 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3383 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3384 GNUNET_i2s (&address->peer),
3385 GNUNET_TRANSPORT_ps2s (n->state),
3386 print_ack_state (n->ack_state));
3388 GNUNET_STATISTICS_update (GST_stats,
3389 gettext_noop ("# unexpected ACK messages"),
3394 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3396 /* We tried to switch addresses while being connect. We explicitly wait
3397 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3398 * so we do not want to set the address as in use! */
3401 set_state_and_timeout (n,
3402 GNUNET_TRANSPORT_PS_CONNECTED,
3403 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3405 if (NULL == n->primary_address.address) {
3407 * We are in state = PSY_SYN_RECV_ACK or ack_state = ACK_SEND_ACK, which
3408 * really means we did try (and succeed) to send a SYN and are waiting for
3410 * That suggests that the primary_address used to be non-NULL, but maybe it
3411 * got reset to NULL without the state being changed appropriately?
3417 /* Reset backoff for primary address */
3418 GST_ats_block_reset (n->primary_address.address,
3419 n->primary_address.session);
3425 * Test if we're connected to the given peer.
3427 * @param target peer to test
3428 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3431 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3433 return test_connected (lookup_neighbour (target));
3438 * Task to asynchronously run #free_neighbour().
3440 * @param cls the `struct NeighbourMapEntry` to free
3443 delayed_disconnect (void *cls)
3445 struct NeighbourMapEntry *n = cls;
3447 n->delayed_disconnect_task = NULL;
3448 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3449 "Disconnecting by request from peer %s\n",
3450 GNUNET_i2s (&n->id));
3456 * We received a quota message from the given peer,
3457 * validate and process.
3459 * @param peer sender of the message
3460 * @param msg the quota message
3463 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
3464 const struct GNUNET_MessageHeader *msg)
3466 struct NeighbourMapEntry *n;
3467 const struct GNUNET_ATS_SessionQuotaMessage *sqm;
3468 struct GNUNET_BANDWIDTH_Value32NBO last;
3470 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3471 "Received QUOTA message from peer `%s'\n",
3473 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionQuotaMessage))
3475 GNUNET_break_op (0);
3476 GNUNET_STATISTICS_update (GST_stats,
3477 gettext_noop ("# quota messages ignored (malformed)"),
3482 GNUNET_STATISTICS_update (GST_stats,
3484 ("# QUOTA messages received"),
3486 sqm = (const struct GNUNET_ATS_SessionQuotaMessage *) msg;
3487 if (NULL == (n = lookup_neighbour (peer)))
3492 last = GNUNET_BANDWIDTH_value_max (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3493 GNUNET_BANDWIDTH_value_init (ntohl (sqm->quota)));
3494 if (last.value__ != n->neighbour_receive_quota.value__)
3496 n->neighbour_receive_quota = last;
3497 send_outbound_quota_to_clients (n);
3503 * We received a disconnect message from the given peer,
3504 * validate and process.
3506 * @param peer sender of the message
3507 * @param msg the disconnect message
3510 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3511 const struct GNUNET_MessageHeader *msg)
3513 struct NeighbourMapEntry *n;
3514 const struct GNUNET_ATS_SessionDisconnectMessage *sdm;
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3517 "Received DISCONNECT message from peer `%s'\n",
3519 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionDisconnectMessage))
3521 GNUNET_break_op (0);
3522 GNUNET_STATISTICS_update (GST_stats,
3524 ("# disconnect messages ignored (malformed)"),
3529 GNUNET_STATISTICS_update (GST_stats,
3531 ("# DISCONNECT messages received"),
3533 sdm = (const struct GNUNET_ATS_SessionDisconnectMessage *) msg;
3534 if (NULL == (n = lookup_neighbour (peer)))
3539 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3541 GNUNET_STATISTICS_update (GST_stats,
3542 gettext_noop ("# disconnect messages ignored (timestamp)"),
3547 if (0 != memcmp (peer,
3549 sizeof (struct GNUNET_PeerIdentity)))
3551 GNUNET_break_op (0);
3554 if (ntohl (sdm->purpose.size) !=
3555 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3556 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3557 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3560 "DISCONNECT message from peer `%s' has invalid size\n",
3562 GNUNET_break_op (0);
3566 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3572 "DISCONNECT message from peer `%s' cannot be verified \n",
3574 GNUNET_break_op (0);
3577 if (NULL == n->delayed_disconnect_task)
3579 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect,
3586 * Closure for the #neighbours_iterate() function.
3588 struct IteratorContext
3591 * Function to call on each connected neighbour.
3593 GST_NeighbourIterator cb;
3596 * Closure for @e cb.
3603 * Call the callback from the closure for each neighbour.
3605 * @param cls the `struct IteratorContext`
3606 * @param key the hash of the public key of the neighbour
3607 * @param value the `struct NeighbourMapEntry`
3608 * @return #GNUNET_OK (continue to iterate)
3611 neighbours_iterate (void *cls,
3612 const struct GNUNET_PeerIdentity *key,
3615 struct IteratorContext *ic = cls;
3616 struct NeighbourMapEntry *n = value;
3617 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3618 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3620 if (NULL != n->primary_address.address)
3622 bandwidth_in = n->primary_address.bandwidth_in;
3623 bandwidth_out = n->primary_address.bandwidth_out;
3627 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3628 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3632 n->primary_address.address,
3635 bandwidth_in, bandwidth_out);
3641 * Iterate over all connected neighbours.
3643 * @param cb function to call
3644 * @param cb_cls closure for @a cb
3647 GST_neighbours_iterate (GST_NeighbourIterator cb,
3650 struct IteratorContext ic;
3652 if (NULL == neighbours)
3653 return; /* can happen during shutdown */
3656 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3657 &neighbours_iterate,
3663 * If we have an active connection to the given target, it must be shutdown.
3665 * @param target peer to disconnect from
3668 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3670 struct NeighbourMapEntry *n;
3672 if (NULL == (n = lookup_neighbour (target)))
3673 return; /* not active */
3674 if (GNUNET_YES == test_connected (n))
3675 GNUNET_STATISTICS_update (GST_stats,
3676 gettext_noop ("# disconnected from peer upon explicit request"),
3679 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3680 "Forced disconnect from peer %s\n",
3681 GNUNET_i2s (target));
3682 disconnect_neighbour (n);
3687 * Obtain current address information for the given neighbour.
3690 * @return address currently used
3692 const struct GNUNET_HELLO_Address *
3693 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3695 struct NeighbourMapEntry *n;
3697 n = lookup_neighbour (peer);
3700 return n->primary_address.address;
3705 * Initialize the neighbours subsystem.
3707 * @param max_fds maximum number of fds to use
3710 GST_neighbours_start (unsigned int max_fds)
3712 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3714 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3715 &utilization_transmission,
3721 * Disconnect from the given neighbour.
3724 * @param key hash of neighbour's public key (not used)
3725 * @param value the `struct NeighbourMapEntry` of the neighbour
3726 * @return #GNUNET_OK (continue to iterate)
3729 disconnect_all_neighbours (void *cls,
3730 const struct GNUNET_PeerIdentity *key,
3733 struct NeighbourMapEntry *n = value;
3735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3736 "Disconnecting peer `%4s' during shutdown\n",
3737 GNUNET_i2s (&n->id));
3744 * Cleanup the neighbours subsystem.
3747 GST_neighbours_stop ()
3749 if (NULL == neighbours)
3751 if (NULL != util_transmission_tk)
3753 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3754 util_transmission_tk = NULL;
3756 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3757 &disconnect_all_neighbours,
3759 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3764 /* end of file gnunet-service-transport_neighbours.c */