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_neighbours.h"
30 #include "gnunet-service-transport_manipulation.h"
31 #include "gnunet-service-transport_plugins.h"
32 #include "gnunet-service-transport_validation.h"
33 #include "gnunet-service-transport.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
39 * Experimental option to ignore SessionQuotaMessages from
42 #define IGNORE_INBOUND_QUOTA GNUNET_YES
45 * Size of the neighbour hash map.
47 #define NEIGHBOUR_TABLE_SIZE 256
50 * Time we give plugin to transmit DISCONNECT message before the
51 * neighbour entry self-destructs.
53 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
56 * How often must a peer violate bandwidth quotas before we start
57 * to simply drop its messages?
59 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
62 * How long are we willing to wait for a response from ATS before timing out?
64 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
67 * How long are we willing to wait for an ACK from the other peer before
68 * giving up on our connect operation?
70 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
73 * How long are we willing to wait for a successful reconnect if
74 * an existing connection went down? Much shorter than the
75 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
76 * higher layers about the disconnect during this period.
78 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
81 * Interval to send utilization data
83 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_UNIT_SECONDS
86 * State describing which kind a reply this neighbour should send
91 * We did not receive a SYN message for this neighbour
96 * The neighbour received a SYN message and has to send a SYN_ACK
102 * The neighbour sent a SYN_ACK message and has to send a ACK
109 GNUNET_NETWORK_STRUCT_BEGIN
112 * Message a peer sends to another to indicate that it intends to
113 * setup a connection/session for data exchange. A 'SESSION_SYN'
114 * should be answered with a 'SESSION_SYN_ACK' with the same body
115 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
116 * a 'ACK'. Once the 'ACK' is received, both peers
117 * should be connected.
119 struct TransportSynMessage
122 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
123 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
125 struct GNUNET_MessageHeader header;
130 uint32_t reserved GNUNET_PACKED;
133 * Absolute time at the sender. Only the most recent connect
134 * message implies which session is preferred by the sender.
136 struct GNUNET_TIME_AbsoluteNBO timestamp;
142 * Message a peer sends to another when connected to indicate that a
143 * session is in use and the peer is still alive or to respond to a keep alive.
144 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
145 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
146 * When the keep alive response with type is received, transport service
147 * will call the respective plugin to update the session timeout
149 struct GNUNET_ATS_SessionKeepAliveMessage
152 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
153 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
155 struct GNUNET_MessageHeader header;
158 * A nonce to identify the session the keep alive is used for
160 uint32_t nonce GNUNET_PACKED;
165 * Message a peer sends to another when connected to indicate that
166 * the other peer should limit transmissions to the indicated
169 struct GNUNET_ATS_SessionQuotaMessage
172 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA.
174 struct GNUNET_MessageHeader header;
177 * Quota to use (for sending), in bytes per second.
179 uint32_t quota GNUNET_PACKED;
184 * Message we send to the other peer to notify him that we intentionally
185 * are disconnecting (to reduce timeouts). This is just a friendly
186 * notification, peers must not rely on always receiving disconnect
189 struct GNUNET_ATS_SessionDisconnectMessage
192 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
194 struct GNUNET_MessageHeader header;
199 uint32_t reserved GNUNET_PACKED;
202 * Purpose of the signature. Extends over the timestamp.
203 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
205 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
208 * Absolute time at the sender. Only the most recent connect
209 * message implies which session is preferred by the sender.
211 struct GNUNET_TIME_AbsoluteNBO timestamp;
214 * Public key of the sender.
216 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
219 * Signature of the peer that sends us the disconnect. Only
220 * valid if the timestamp is AFTER the timestamp from the
221 * corresponding 'SYN' message.
223 struct GNUNET_CRYPTO_EddsaSignature signature;
227 GNUNET_NETWORK_STRUCT_END
231 * For each neighbour we keep a list of messages
232 * that we still want to transmit to the neighbour.
238 * This is a doubly linked list.
240 struct MessageQueue *next;
243 * This is a doubly linked list.
245 struct MessageQueue *prev;
248 * Function to call once we're done.
250 GST_NeighbourSendContinuation cont;
253 * Closure for @e cont
258 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
259 * stuck together in memory. Allocated at the end of this struct.
261 const char *message_buf;
264 * Size of the message buf
266 size_t message_buf_size;
269 * At what time should we fail?
271 struct GNUNET_TIME_Absolute timeout;
277 * A possible address we could use to communicate with a neighbour.
279 struct NeighbourAddress
283 * Active session for this address.
285 struct GNUNET_ATS_Session *session;
288 * Network-level address information.
290 struct GNUNET_HELLO_Address *address;
293 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
294 * peer for this address. Use to check that the ACK is in response
295 * to our most recent 'SYN'.
297 struct GNUNET_TIME_Absolute connect_timestamp;
300 * Inbound bandwidth from ATS for this address.
302 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
305 * Outbound bandwidth from ATS for this address.
307 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
310 * Did we tell ATS that this is our 'active' address?
315 * The current nonce sent in the last keep alive messages
317 uint32_t keep_alive_nonce;
322 * Entry in neighbours.
324 struct NeighbourMapEntry
328 * Head of list of messages we would like to send to this peer;
329 * must contain at most one message per client.
331 struct MessageQueue *messages_head;
334 * Tail of list of messages we would like to send to this peer; must
335 * contain at most one message per client.
337 struct MessageQueue *messages_tail;
340 * Are we currently trying to send a message? If so, which one?
342 struct MessageQueue *is_active;
345 * Primary address we currently use to communicate with the neighbour.
347 struct NeighbourAddress primary_address;
350 * Alternative address currently under consideration for communicating
351 * with the neighbour.
353 struct NeighbourAddress alternative_address;
356 * Identity of this neighbour.
358 struct GNUNET_PeerIdentity id;
361 * Main task that drives this peer (timeouts, keepalives, etc.).
362 * Always runs the #master_task().
364 struct GNUNET_SCHEDULER_Task *task;
367 * Task to disconnect neighbour after we received a DISCONNECT message
369 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
372 * At what time should we sent the next keep-alive message?
374 struct GNUNET_TIME_Absolute keep_alive_time;
377 * At what time did we sent the last keep-alive message? Used
378 * to calculate round-trip time ("latency").
380 struct GNUNET_TIME_Absolute last_keep_alive_time;
383 * Timestamp we should include in our next SYN_ACK message.
384 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
385 * our SYN_ACK message.
387 struct GNUNET_TIME_Absolute connect_ack_timestamp;
390 * ATS address suggest handle
392 struct GNUNET_ATS_ConnectivitySuggestHandle *suggest_handle;
395 * Time where we should cut the connection (timeout) if we don't
396 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
397 * if we are in #GNUNET_TRANSPORT_PS_CONNECTED).
399 struct GNUNET_TIME_Absolute timeout;
402 * Tracker for inbound bandwidth.
404 struct GNUNET_BANDWIDTH_Tracker in_tracker;
407 * How often has the other peer (recently) violated the inbound
408 * traffic limit? Incremented by 10 per violation, decremented by 1
409 * per non-violation (for each time interval).
411 unsigned int quota_violation_count;
414 * Latest quota the other peer send us in bytes per second.
415 * We should not send more, least the other peer throttle
416 * receiving our traffic.
418 struct GNUNET_BANDWIDTH_Value32NBO neighbour_receive_quota;
421 * The current state of the peer.
423 enum GNUNET_TRANSPORT_PeerState state;
426 * Did we sent an KEEP_ALIVE message and are we expecting a response?
428 int expect_latency_response;
431 * When a peer wants to connect we have to reply to the 1st SYN message
432 * with a SYN_ACK message. But sometime we cannot send this message
433 * immediately since we do not have an address and then we have to remember
434 * to send this message as soon as we have an address.
436 * Flag to set if we still need to send a SYN_ACK message to the other peer
437 * (once we have an address to use and the peer has been allowed by our
438 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
439 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
440 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
441 * 'ACK' (regardless of what our own state machine might say).
443 enum GST_ACK_State ack_state;
446 * Tracking utilization of outbound bandwidth
448 uint32_t util_total_bytes_sent;
451 * Tracking utilization of inbound bandwidth
453 uint32_t util_total_bytes_recv;
456 * Date of last utilization transmission
458 struct GNUNET_TIME_Absolute last_util_transmission;
463 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
465 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
468 * List of pending blacklist checks: head
470 static struct BlacklistCheckSwitchContext *pending_bc_head;
473 * List of pending blacklist checks: tail
475 static struct BlacklistCheckSwitchContext *pending_bc_tail;
478 * counter for connected neighbours
480 static unsigned int neighbours_connected;
483 * Number of bytes we have currently queued for transmission.
485 static unsigned long long bytes_in_send_queue;
488 * Task transmitting utilization data
490 static struct GNUNET_SCHEDULER_Task *util_transmission_tk;
494 * Convert the given ACK state to a string.
497 * @return corresponding human-readable string
500 print_ack_state (enum GST_ACK_State s)
505 case ACK_SEND_SYN_ACK:
506 return "SEND_SYN_ACK";
517 * Send information about a new outbound quota to our clients.
518 * Note that the outbound quota is enforced client-side (i.e.
519 * in libgnunettransport).
521 * @param n affected peer
524 send_outbound_quota_to_clients (struct NeighbourMapEntry *n)
526 struct QuotaSetMessage q_msg;
527 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
529 if (! GNUNET_TRANSPORT_is_connected (n->state))
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);
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
540 ntohl (bandwidth_min.value__),
541 GNUNET_i2s (&n->id));
542 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
543 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
544 q_msg.quota = bandwidth_min;
546 GST_clients_broadcast (&q_msg.header,
552 * Notify our clients that another peer connected to us.
554 * @param n the peer that connected
557 neighbours_connect_notification (struct NeighbourMapEntry *n)
559 size_t len = sizeof(struct ConnectInfoMessage);
560 char buf[len] GNUNET_ALIGN;
561 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
562 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
564 #if IGNORE_INBOUND_QUOTA
565 bandwidth_min = n->primary_address.bandwidth_out;
567 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
568 n->neighbour_receive_quota);
570 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
571 "We are now connected to peer `%s'\n",
572 GNUNET_i2s (&n->id));
573 connect_msg->header.size = htons (sizeof(buf));
574 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
575 connect_msg->id = n->id;
576 connect_msg->quota_in = n->primary_address.bandwidth_in;
577 connect_msg->quota_out = bandwidth_min;
578 GST_clients_broadcast (&connect_msg->header,
584 * Notify our clients (and manipulation) that a peer disconnected from
587 * @param n the peer that disconnected
590 neighbours_disconnect_notification (struct NeighbourMapEntry *n)
592 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
593 "Peer `%s' disconnected\n",
594 GNUNET_i2s (&n->id));
595 GST_manipulation_peer_disconnect (&n->id);
596 GST_clients_broadcast_disconnect (&n->id);
601 * Notify transport clients that a neighbour peer changed its active
604 * @param peer identity of the peer
605 * @param address address possibly NULL if peer is not connected
606 * @param state current state this peer is in
607 * @param state_timeout timeout for the current state of the peer
608 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
609 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
612 neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer,
613 const struct GNUNET_HELLO_Address *address,
614 enum GNUNET_TRANSPORT_PeerState state,
615 struct GNUNET_TIME_Absolute state_timeout,
616 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
617 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
620 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
622 GST_plugins_a2s (address),
623 GNUNET_TRANSPORT_ps2s (state),
624 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
625 /* FIXME: include bandwidth in notification! */
626 GST_clients_broadcast_peer_notification (peer,
634 * Lookup a neighbour entry in the neighbours hash map.
636 * @param pid identity of the peer to look up
637 * @return the entry, NULL if there is no existing record
639 static struct NeighbourMapEntry *
640 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
642 if (NULL == neighbours)
644 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
649 * Test if we're connected to the given peer.
651 * @param n neighbour entry of peer to test
652 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
655 test_connected (struct NeighbourMapEntry *n)
659 return GNUNET_TRANSPORT_is_connected (n->state);
664 * We don't need a given neighbour address any more.
665 * Release its resources and give appropriate notifications
666 * to ATS and other subsystems.
668 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
671 free_address (struct NeighbourAddress *na)
673 if (GNUNET_YES == na->ats_active)
674 GST_validation_set_address_use (na->address,
676 if (NULL != na->address)
678 GST_ats_block_address (na->address,
680 GNUNET_HELLO_address_free (na->address);
683 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
684 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
685 na->ats_active = GNUNET_NO;
686 na->keep_alive_nonce = 0;
692 * Master task run for every neighbour. Performs all of the time-related
693 * activities (keep alive, send next message, disconnect if idle, finish
694 * clean up after disconnect).
696 * @param cls the `struct NeighbourMapEntry` for which we are running
699 master_task (void *cls);
703 * Set net state and state timeout for this neighbour and notify monitoring
705 * @param n the respective neighbour
706 * @param s the new state
707 * @param timeout the new timeout
710 set_state_and_timeout (struct NeighbourMapEntry *n,
711 enum GNUNET_TRANSPORT_PeerState s,
712 struct GNUNET_TIME_Absolute timeout)
714 if (GNUNET_TRANSPORT_is_connected (s) &&
715 ! GNUNET_TRANSPORT_is_connected (n->state) )
717 neighbours_connect_notification (n);
718 GNUNET_STATISTICS_set (GST_stats,
719 gettext_noop ("# peers connected"),
720 ++neighbours_connected,
723 if (! GNUNET_TRANSPORT_is_connected (s) &&
724 GNUNET_TRANSPORT_is_connected (n->state) )
726 GNUNET_STATISTICS_set (GST_stats,
727 gettext_noop ("# peers connected"),
728 --neighbours_connected,
730 neighbours_disconnect_notification (n);
733 if ( (timeout.abs_value_us < n->timeout.abs_value_us) &&
736 /* new timeout is earlier, reschedule master task */
737 GNUNET_SCHEDULER_cancel (n->task);
738 n->task = GNUNET_SCHEDULER_add_at (timeout,
742 n->timeout = timeout;
743 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
744 "Neighbour `%s' changed state to %s with timeout %s\n",
746 GNUNET_TRANSPORT_ps2s(s),
747 GNUNET_STRINGS_absolute_time_to_string (timeout));
748 neighbours_changed_notification (&n->id,
749 n->primary_address.address,
752 n->primary_address.bandwidth_in,
753 n->primary_address.bandwidth_out);
758 * Initialize the alternative address of a neighbour
760 * @param n the neighbour
761 * @param address address of the other peer, NULL if other peer
763 * @param session session to use (or NULL, in which case an
764 * address must be setup)
765 * @param bandwidth_in inbound quota to be used when connection is up
766 * @param bandwidth_out outbound quota to be used when connection is up
769 set_alternative_address (struct NeighbourMapEntry *n,
770 const struct GNUNET_HELLO_Address *address,
771 struct GNUNET_ATS_Session *session,
772 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
773 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
775 struct GNUNET_TRANSPORT_PluginFunctions *papi;
777 if (NULL == (papi = GST_plugins_find (address->transport_name)))
782 if (session == n->alternative_address.session)
784 n->alternative_address.bandwidth_in = bandwidth_in;
785 n->alternative_address.bandwidth_out = bandwidth_out;
788 if (NULL != n->alternative_address.address)
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Replacing existing alternative address with another one\n");
792 free_address (&n->alternative_address);
795 session = papi->get_session (papi->cls,
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Failed to obtain new session for peer `%s' and address '%s'\n",
801 GNUNET_i2s (&address->peer),
802 GST_plugins_a2s (address));
803 GNUNET_STATISTICS_update (GST_stats,
804 gettext_noop ("# session creation failed"),
809 GST_ats_new_session (address,
811 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
812 "Neighbour `%s' configured alternative address %s\n",
814 GST_plugins_a2s(address));
816 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
817 n->alternative_address.bandwidth_in = bandwidth_in;
818 n->alternative_address.bandwidth_out = bandwidth_out;
819 n->alternative_address.session = session;
820 n->alternative_address.ats_active = GNUNET_NO;
821 n->alternative_address.keep_alive_nonce = 0;
822 GNUNET_assert (GNUNET_YES ==
823 GST_ats_is_known (n->alternative_address.address,
824 n->alternative_address.session));
829 * Transmit a message using the current session of the given
832 * @param n entry for the recipient
833 * @param msgbuf buffer to transmit
834 * @param msgbuf_size number of bytes in @a msgbuf buffer
835 * @param priority transmission priority
836 * @param timeout transmission timeout
837 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
838 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
839 * @param cont continuation to call when finished (can be NULL)
840 * @param cont_cls closure for @a cont
841 * @return timeout (copy of @a timeout or a calculated one if
842 * @a use_keepalive_timeout is #GNUNET_YES.
844 static struct GNUNET_TIME_Relative
845 send_with_session (struct NeighbourMapEntry *n,
849 struct GNUNET_TIME_Relative timeout,
850 unsigned int use_keepalive_timeout,
851 GNUNET_TRANSPORT_TransmitContinuation cont,
854 struct GNUNET_TRANSPORT_PluginFunctions *papi;
855 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
857 GNUNET_assert (NULL != n->primary_address.session);
858 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
859 (-1 == papi->send (papi->cls,
860 n->primary_address.session,
864 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
865 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
866 papi->query_keepalive_factor (papi->cls))),
875 GST_neighbours_notify_data_sent (n->primary_address.address,
876 n->primary_address.session,
878 GNUNET_break (NULL != papi);
884 * Clear the primary address of a neighbour since this address is not
885 * valid anymore and notify monitoring about it
887 * @param n the neighbour
890 unset_primary_address (struct NeighbourMapEntry *n)
892 /* Notify monitoring about change */
893 if (NULL == n->primary_address.address)
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Disabling primary address\n");
897 neighbours_changed_notification (&n->id,
898 n->primary_address.address,
901 GNUNET_BANDWIDTH_value_init (0),
902 GNUNET_BANDWIDTH_value_init (0));
903 free_address (&n->primary_address);
908 * Free a neighbour map entry.
910 * @param n entry to free
913 free_neighbour (struct NeighbourMapEntry *n)
915 struct MessageQueue *mq;
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "Freeing neighbour state of peer `%s'\n",
919 GNUNET_i2s (&n->id));
920 n->is_active = NULL; /* always free'd by its own continuation! */
922 /* fail messages currently in the queue */
923 while (NULL != (mq = n->messages_head))
925 GNUNET_CONTAINER_DLL_remove (n->messages_head,
928 if (NULL != mq->cont)
929 mq->cont (mq->cont_cls,
931 mq->message_buf_size,
935 /* Mark peer as disconnected */
936 set_state_and_timeout (n,
937 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED,
938 GNUNET_TIME_UNIT_FOREVER_ABS);
939 /* free addresses and mark as unused */
940 unset_primary_address (n);
942 if (NULL != n->alternative_address.address)
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "Cleaning up alternative address\n");
946 free_address (&n->alternative_address);
948 GNUNET_assert (GNUNET_YES ==
949 GNUNET_CONTAINER_multipeermap_remove (neighbours,
952 /* Cancel address requests for this peer */
953 if (NULL != n->suggest_handle)
955 GNUNET_ATS_connectivity_suggest_cancel (n->suggest_handle);
956 n->suggest_handle = NULL;
959 /* Cancel the disconnect task */
960 if (NULL != n->delayed_disconnect_task)
962 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
963 n->delayed_disconnect_task = NULL;
966 /* Cancel the master task */
969 GNUNET_SCHEDULER_cancel (n->task);
972 /* free rest of memory */
978 * Function called when the 'DISCONNECT' message has been sent by the
979 * plugin. Frees the neighbour --- if the entry still exists.
982 * @param target identity of the neighbour that was disconnected
983 * @param result #GNUNET_OK if the disconnect got out successfully
984 * @param payload bytes payload
985 * @param physical bytes on wire
988 send_disconnect_cont (void *cls,
989 const struct GNUNET_PeerIdentity *target,
994 struct NeighbourMapEntry *n;
996 n = lookup_neighbour (target);
998 return; /* already gone */
999 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1000 return; /* have created a fresh entry since */
1001 if (NULL != n->task)
1002 GNUNET_SCHEDULER_cancel (n->task);
1003 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1008 * Transmit a DISCONNECT message to the other peer.
1010 * @param n neighbour to send DISCONNECT message.
1013 send_disconnect (struct NeighbourMapEntry *n)
1015 struct GNUNET_ATS_SessionDisconnectMessage disconnect_msg;
1017 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1018 "Sending DISCONNECT message to peer `%4s'\n",
1019 GNUNET_i2s (&n->id));
1020 disconnect_msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionDisconnectMessage));
1021 disconnect_msg.header.type =
1022 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1023 disconnect_msg.reserved = htonl (0);
1024 disconnect_msg.purpose.size =
1025 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1026 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1027 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1028 disconnect_msg.purpose.purpose =
1029 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1030 disconnect_msg.timestamp =
1031 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1032 disconnect_msg.public_key = GST_my_identity.public_key;
1033 GNUNET_assert (GNUNET_OK ==
1034 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1035 &disconnect_msg.purpose,
1036 &disconnect_msg.signature));
1038 (void) send_with_session (n,
1040 sizeof (disconnect_msg),
1042 GNUNET_TIME_UNIT_FOREVER_REL,
1044 &send_disconnect_cont,
1046 GNUNET_STATISTICS_update (GST_stats,
1047 gettext_noop ("# DISCONNECT messages sent"),
1054 * Disconnect from the given neighbour, clean up the record.
1056 * @param n neighbour to disconnect from
1059 disconnect_neighbour (struct NeighbourMapEntry *n)
1061 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1062 "Disconnecting from peer %s in state %s\n",
1063 GNUNET_i2s (&n->id),
1064 GNUNET_TRANSPORT_ps2s (n->state));
1065 /* depending on state, notify neighbour and/or upper layers of this peer
1069 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1070 case GNUNET_TRANSPORT_PS_INIT_ATS:
1071 /* other peer is completely unaware of us, no need to send DISCONNECT */
1074 case GNUNET_TRANSPORT_PS_SYN_SENT:
1075 send_disconnect (n);
1076 set_state_and_timeout (n,
1077 GNUNET_TRANSPORT_PS_DISCONNECT,
1078 GNUNET_TIME_UNIT_FOREVER_ABS);
1080 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1081 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1084 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1085 /* we DID ACK the other peer's request, must send DISCONNECT */
1086 send_disconnect (n);
1087 set_state_and_timeout (n,
1088 GNUNET_TRANSPORT_PS_DISCONNECT,
1089 GNUNET_TIME_UNIT_FOREVER_ABS);
1091 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1092 case GNUNET_TRANSPORT_PS_CONNECTED:
1093 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1094 /* we are currently connected, need to send disconnect and do
1095 internal notifications and update statistics */
1096 send_disconnect (n);
1097 set_state_and_timeout (n,
1098 GNUNET_TRANSPORT_PS_DISCONNECT,
1099 GNUNET_TIME_UNIT_FOREVER_ABS);
1101 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1102 /* Disconnecting while waiting for an ATS address to reconnect,
1103 * cannot send DISCONNECT */
1106 case GNUNET_TRANSPORT_PS_DISCONNECT:
1107 /* already disconnected, ignore */
1109 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1110 /* already cleaned up, how did we get here!? */
1114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1115 "Unhandled state `%s'\n",
1116 GNUNET_TRANSPORT_ps2s (n->state));
1120 /* schedule timeout to clean up */
1121 if (NULL != n->task)
1122 GNUNET_SCHEDULER_cancel (n->task);
1123 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1130 * Change the incoming quota for the given peer. Updates
1131 * our own receive rate and informs the neighbour about
1134 * @param n neighbour entry to change qutoa for
1135 * @param quota new quota
1138 set_incoming_quota (struct NeighbourMapEntry *n,
1139 struct GNUNET_BANDWIDTH_Value32NBO quota)
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
1143 ntohl (quota.value__), GNUNET_i2s (&n->id));
1144 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
1146 if (0 != ntohl (quota.value__))
1148 struct GNUNET_ATS_SessionQuotaMessage sqm;
1150 sqm.header.size = htons (sizeof (struct GNUNET_ATS_SessionQuotaMessage));
1151 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA);
1152 sqm.quota = quota.value__;
1153 (void) send_with_session (n,
1157 GNUNET_TIME_UNIT_FOREVER_REL,
1162 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1163 "Disconnecting peer `%4s' due to SET_QUOTA\n",
1164 GNUNET_i2s (&n->id));
1165 if (GNUNET_YES == test_connected (n))
1166 GNUNET_STATISTICS_update (GST_stats,
1167 gettext_noop ("# disconnects due to quota of 0"),
1169 disconnect_neighbour (n);
1174 * Initialize the primary address of a neighbour
1176 * @param n the neighbour
1177 * @param address address of the other peer, NULL if other peer
1179 * @param session session to use (or NULL, in which case an
1180 * address must be setup)
1181 * @param bandwidth_in inbound quota to be used when connection is up
1182 * @param bandwidth_out outbound quota to be used when connection is up
1185 set_primary_address (struct NeighbourMapEntry *n,
1186 const struct GNUNET_HELLO_Address *address,
1187 struct GNUNET_ATS_Session *session,
1188 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1189 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1191 if (session == n->primary_address.session)
1193 GST_validation_set_address_use (n->primary_address.address,
1195 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
1197 n->primary_address.bandwidth_in = bandwidth_in;
1198 set_incoming_quota (n,
1201 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
1203 n->primary_address.bandwidth_out = bandwidth_out;
1204 send_outbound_quota_to_clients (n);
1208 if ( (NULL != n->primary_address.address) &&
1209 (0 == GNUNET_HELLO_address_cmp (address,
1210 n->primary_address.address)) )
1215 if (NULL == session)
1218 GST_ats_block_address (address,
1222 if (NULL != n->primary_address.address)
1224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1225 "Replacing existing primary address with another one\n");
1226 free_address (&n->primary_address);
1228 n->primary_address.address = GNUNET_HELLO_address_copy (address);
1229 n->primary_address.bandwidth_in = bandwidth_in;
1230 n->primary_address.bandwidth_out = bandwidth_out;
1231 n->primary_address.session = session;
1232 n->primary_address.keep_alive_nonce = 0;
1233 GNUNET_assert (GNUNET_YES ==
1234 GST_ats_is_known (n->primary_address.address,
1235 n->primary_address.session));
1236 /* subsystems about address use */
1237 GST_validation_set_address_use (n->primary_address.address,
1239 set_incoming_quota (n,
1241 send_outbound_quota_to_clients (n);
1242 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1243 "Neighbour `%s' switched to address `%s'\n",
1244 GNUNET_i2s (&n->id),
1245 GST_plugins_a2s(address));
1247 neighbours_changed_notification (&n->id,
1248 n->primary_address.address,
1251 n->primary_address.bandwidth_in,
1252 n->primary_address.bandwidth_out);
1257 * We're done with our transmission attempt, continue processing.
1259 * @param cls the `struct MessageQueue` of the message
1260 * @param receiver intended receiver
1261 * @param success whether it worked or not
1262 * @param size_payload bytes payload sent
1263 * @param physical bytes sent on wire
1266 transmit_send_continuation (void *cls,
1267 const struct GNUNET_PeerIdentity *receiver,
1269 size_t size_payload,
1272 struct MessageQueue *mq = cls;
1273 struct NeighbourMapEntry *n;
1275 if (NULL == (n = lookup_neighbour (receiver)))
1277 if (NULL != mq->cont)
1278 mq->cont (mq->cont_cls,
1279 GNUNET_SYSERR /* not connected */,
1283 return; /* disconnect or other error while transmitting, can happen */
1285 if (n->is_active == mq)
1287 /* this is still "our" neighbour, remove us from its queue
1288 and allow it to send the next message now */
1289 n->is_active = NULL;
1290 if (NULL != n->task)
1291 GNUNET_SCHEDULER_cancel (n->task);
1292 n->task = GNUNET_SCHEDULER_add_now (&master_task,
1295 if (bytes_in_send_queue < mq->message_buf_size)
1297 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1298 "Bytes_in_send_queue `%llu', Message_size %u, result: %s, payload %u, on wire %u\n",
1299 bytes_in_send_queue,
1300 (unsigned int) mq->message_buf_size,
1301 (GNUNET_OK == success) ? "OK" : "FAIL",
1302 (unsigned int) size_payload,
1303 (unsigned int) physical);
1307 GNUNET_break (size_payload == mq->message_buf_size);
1308 bytes_in_send_queue -= mq->message_buf_size;
1309 GNUNET_STATISTICS_set (GST_stats,
1310 gettext_noop ("# bytes in message queue for other peers"),
1311 bytes_in_send_queue,
1313 if (GNUNET_OK == success)
1314 GNUNET_STATISTICS_update (GST_stats,
1315 gettext_noop ("# messages transmitted to other peers"),
1319 GNUNET_STATISTICS_update (GST_stats,
1321 ("# transmission failures for messages to other peers"),
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1325 GNUNET_i2s (receiver),
1326 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1327 (unsigned int) mq->message_buf_size,
1328 (success == GNUNET_OK) ? "success" : "FAILURE");
1329 if (NULL != mq->cont)
1330 mq->cont (mq->cont_cls,
1339 * Check the message list for the given neighbour and if we can
1340 * send a message, do so. This function should only be called
1341 * if the connection is at least generally ready for transmission.
1342 * While we will only send one message at a time, no bandwidth
1343 * quota management is performed here. If a message was given to
1344 * the plugin, the continuation will automatically re-schedule
1345 * the 'master' task once the next message might be transmitted.
1347 * @param n target peer for which to transmit
1350 try_transmission_to_peer (struct NeighbourMapEntry *n)
1352 struct MessageQueue *mq;
1353 struct GNUNET_TIME_Relative timeout;
1355 if (NULL == n->primary_address.address)
1357 /* no address, why are we here? */
1361 if ((0 == n->primary_address.address->address_length) &&
1362 (NULL == n->primary_address.session))
1364 /* no address, why are we here? */
1368 if (NULL != n->is_active)
1370 /* transmission already pending */
1374 /* timeout messages from the queue that are past their due date */
1375 while (NULL != (mq = n->messages_head))
1377 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1378 if (timeout.rel_value_us > 0)
1380 GNUNET_STATISTICS_update (GST_stats,
1381 gettext_noop ("# messages timed out while in transport queue"),
1384 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1388 transmit_send_continuation (mq,
1391 mq->message_buf_size,
1395 return; /* no more messages */
1396 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Giving message with %u bytes to plugin session %p\n",
1403 (unsigned int) mq->message_buf_size,
1404 n->primary_address.session);
1405 (void) send_with_session (n,
1407 mq->message_buf_size,
1411 &transmit_send_continuation,
1417 * Send keepalive message to the neighbour. Must only be called
1418 * if we are on 'connected' state or while trying to switch addresses.
1419 * Will internally determine if a keepalive is truly needed (so can
1420 * always be called).
1422 * @param n neighbour that went idle and needs a keepalive
1425 send_keepalive (struct NeighbourMapEntry *n)
1427 struct GNUNET_ATS_SessionKeepAliveMessage m;
1428 struct GNUNET_TIME_Relative timeout;
1431 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1432 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state));
1433 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1434 return; /* no keepalive needed at this time */
1436 nonce = 0; /* 0 indicates 'not set' */
1438 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Sending KEEPALIVE to peer `%s' with nonce %u\n",
1443 GNUNET_i2s (&n->id),
1445 m.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1446 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1447 m.nonce = htonl (nonce);
1449 timeout = send_with_session (n,
1452 UINT32_MAX /* priority */,
1453 GNUNET_TIME_UNIT_FOREVER_REL,
1456 GNUNET_STATISTICS_update (GST_stats,
1457 gettext_noop ("# KEEPALIVES sent"),
1460 n->primary_address.keep_alive_nonce = nonce;
1461 n->expect_latency_response = GNUNET_YES;
1462 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1463 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1468 * Keep the connection to the given neighbour alive longer,
1469 * we received a KEEPALIVE (or equivalent); send a response.
1471 * @param neighbour neighbour to keep alive (by sending keep alive response)
1472 * @param m the keep alive message containing the nonce to respond to
1475 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1476 const struct GNUNET_MessageHeader *m)
1478 struct NeighbourMapEntry *n;
1479 const struct GNUNET_ATS_SessionKeepAliveMessage *msg_in;
1480 struct GNUNET_ATS_SessionKeepAliveMessage msg;
1482 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1484 GNUNET_break_op (0);
1488 msg_in = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1489 if (NULL == (n = lookup_neighbour (neighbour)))
1491 GNUNET_STATISTICS_update (GST_stats,
1493 ("# KEEPALIVE messages discarded (peer unknown)"),
1497 if (NULL == n->primary_address.session)
1499 GNUNET_STATISTICS_update (GST_stats,
1501 ("# KEEPALIVE messages discarded (no session)"),
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1507 "Received KEEPALIVE request from peer `%s' with nonce %u\n",
1508 GNUNET_i2s (&n->id),
1509 ntohl (msg_in->nonce));
1510 GNUNET_STATISTICS_update (GST_stats,
1511 gettext_noop ("# KEEPALIVES received in good order"),
1515 /* send reply to allow neighbour to measure latency */
1516 msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1517 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1518 msg.nonce = msg_in->nonce;
1519 (void) send_with_session (n,
1521 sizeof (struct GNUNET_ATS_SessionKeepAliveMessage),
1522 UINT32_MAX /* priority */,
1523 GNUNET_TIME_UNIT_FOREVER_REL,
1530 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1531 * latency to this peer. Pass the updated information (existing ats
1532 * plus calculated latency) to ATS.
1534 * @param neighbour neighbour to keep alive
1535 * @param m the message containing the keep alive response
1538 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1539 const struct GNUNET_MessageHeader *m)
1541 struct NeighbourMapEntry *n;
1542 const struct GNUNET_ATS_SessionKeepAliveMessage *msg;
1543 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1544 struct GNUNET_TIME_Relative latency;
1546 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1548 GNUNET_break_op (0);
1552 msg = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1553 if (NULL == (n = lookup_neighbour (neighbour)))
1555 GNUNET_STATISTICS_update (GST_stats,
1556 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not connected)"),
1561 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1562 (GNUNET_YES != n->expect_latency_response) )
1564 GNUNET_STATISTICS_update (GST_stats,
1565 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not expected)"),
1570 if (NULL == n->primary_address.address)
1572 GNUNET_STATISTICS_update (GST_stats,
1573 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (address changed)"),
1578 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1580 if (0 == n->primary_address.keep_alive_nonce)
1581 GNUNET_STATISTICS_update (GST_stats,
1582 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (no nonce)"),
1586 GNUNET_STATISTICS_update (GST_stats,
1587 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (bad nonce)"),
1592 GNUNET_STATISTICS_update (GST_stats,
1593 gettext_noop ("# KEEPALIVE_RESPONSEs received (OK)"),
1598 /* Update session timeout here */
1599 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Updating session for peer `%s' for session %p\n",
1603 GNUNET_i2s (&n->id),
1604 n->primary_address.session);
1605 papi->update_session_timeout (papi->cls,
1607 n->primary_address.session);
1614 n->primary_address.keep_alive_nonce = 0;
1615 n->expect_latency_response = GNUNET_NO;
1616 set_state_and_timeout (n,
1618 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1620 latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1622 "Received KEEPALIVE_RESPONSE from peer `%s', latency is %s\n",
1623 GNUNET_i2s (&n->id),
1624 GNUNET_STRINGS_relative_time_to_string (latency,
1626 GST_ats_update_delay (n->primary_address.address,
1627 GNUNET_TIME_relative_divide (latency,
1633 * We have received a message from the given sender. How long should
1634 * we delay before receiving more? (Also used to keep the peer marked
1637 * @param sender sender of the message
1638 * @param size size of the message
1639 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1640 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1641 * #GNUNET_SYSERR if the connection is not fully up yet
1642 * @return how long to wait before reading more from this sender
1644 struct GNUNET_TIME_Relative
1645 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
1649 struct NeighbourMapEntry *n;
1650 struct GNUNET_TIME_Relative ret;
1652 if (NULL == neighbours)
1654 *do_forward = GNUNET_NO;
1655 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1657 if (NULL == (n = lookup_neighbour (sender)))
1659 GNUNET_STATISTICS_update (GST_stats,
1660 gettext_noop ("# messages discarded due to lack of neighbour record"),
1663 *do_forward = GNUNET_NO;
1664 return GNUNET_TIME_UNIT_ZERO;
1666 if (! test_connected (n))
1668 *do_forward = GNUNET_SYSERR;
1669 return GNUNET_TIME_UNIT_ZERO;
1671 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1673 n->quota_violation_count++;
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1675 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1676 n->in_tracker.available_bytes_per_s__,
1677 n->quota_violation_count);
1678 /* Discount 32k per violation */
1679 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1683 if (n->quota_violation_count > 0)
1685 /* try to add 32k back */
1686 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1687 n->quota_violation_count--;
1690 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1692 GNUNET_STATISTICS_update (GST_stats,
1694 ("# bandwidth quota violations by other peers"),
1696 *do_forward = GNUNET_NO;
1697 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1699 *do_forward = GNUNET_YES;
1700 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1701 if (ret.rel_value_us > 0)
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "Throttling read (%lld bytes excess at %u b/s), waiting %s before reading more.\n",
1705 (long long) n->in_tracker.consumption_since_last_update__,
1706 (unsigned int) n->in_tracker.available_bytes_per_s__,
1707 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1708 GNUNET_STATISTICS_update (GST_stats,
1709 gettext_noop ("# ms throttling suggested"),
1710 (int64_t) ret.rel_value_us / 1000LL,
1718 * Transmit a message to the given target using the active connection.
1720 * @param target destination
1721 * @param msg message to send
1722 * @param msg_size number of bytes in msg
1723 * @param timeout when to fail with timeout
1724 * @param cont function to call when done
1725 * @param cont_cls closure for @a cont
1728 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
1731 struct GNUNET_TIME_Relative timeout,
1732 GST_NeighbourSendContinuation cont,
1735 struct NeighbourMapEntry *n;
1736 struct MessageQueue *mq;
1738 /* All ove these cases should never happen; they are all API violations.
1739 But we check anyway, just to be sure. */
1740 if (NULL == (n = lookup_neighbour (target)))
1750 if (GNUNET_YES != test_connected (n))
1760 bytes_in_send_queue += msg_size;
1761 GNUNET_STATISTICS_set (GST_stats,
1763 ("# bytes in message queue for other peers"),
1764 bytes_in_send_queue, GNUNET_NO);
1765 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1767 mq->cont_cls = cont_cls;
1768 GNUNET_memcpy (&mq[1], msg, msg_size);
1769 mq->message_buf = (const char *) &mq[1];
1770 mq->message_buf_size = msg_size;
1771 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1774 "Enqueueing %u bytes to send to peer %s\n",
1775 (unsigned int) msg_size,
1776 GNUNET_i2s (target));
1777 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
1780 if (NULL != n->task)
1781 GNUNET_SCHEDULER_cancel (n->task);
1782 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1787 * Continuation called from our attempt to transmitted our
1788 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN to the specified @a
1789 * target. Continue processing based on the @a result. Specifically,
1790 * if we failed to transmit, discard the address we used.
1793 * @param target which peer received the transmission
1794 * @param result #GNUNET_OK if sending worked
1795 * @param size_payload how many bytes of payload were sent (ignored)
1796 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1799 send_session_syn_cont (void *cls,
1800 const struct GNUNET_PeerIdentity *target,
1802 size_t size_payload,
1803 size_t size_on_wire)
1805 struct NeighbourMapEntry *n;
1807 n = lookup_neighbour (target);
1810 /* SYN continuation was called after neighbor was freed,
1811 * for example due to a time out for the state or the session
1812 * used was already terminated: nothing to do here... */
1816 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1817 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1818 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1820 /* SYN continuation was called after neighbor changed state,
1821 * for example due to a time out for the state or the session
1822 * used was already terminated: nothing to do here... */
1825 if (GNUNET_OK == result)
1828 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1829 _("Failed to send SYN message to peer `%s'\n"),
1830 GNUNET_i2s (target));
1832 case GNUNET_TRANSPORT_PS_SYN_SENT:
1833 /* Remove address and request an additional one */
1834 unset_primary_address (n);
1835 set_state_and_timeout (n,
1836 GNUNET_TRANSPORT_PS_INIT_ATS,
1837 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1839 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1840 /* Remove address and request an additional one */
1841 unset_primary_address (n);
1842 set_state_and_timeout (n,
1843 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1844 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1846 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1847 /* Remove address and request and go back to primary address */
1848 GNUNET_STATISTICS_update (GST_stats,
1849 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN CONT)"),
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1853 "Switch failed, cleaning up alternative address\n");
1854 free_address (&n->alternative_address);
1855 set_state_and_timeout (n,
1856 GNUNET_TRANSPORT_PS_CONNECTED,
1857 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1860 disconnect_neighbour (n);
1867 * Send a SYN message via the given address.
1869 * @param na address to use
1872 send_syn (struct NeighbourAddress *na)
1874 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1875 struct TransportSynMessage connect_msg;
1876 struct NeighbourMapEntry *n;
1878 GNUNET_assert (NULL != na->session);
1879 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1880 "Sending SYN message to peer `%s' at %s\n",
1881 GNUNET_i2s (&na->address->peer),
1882 GST_plugins_a2s (na->address));
1884 papi = GST_plugins_find (na->address->transport_name);
1885 GNUNET_assert (NULL != papi);
1886 GNUNET_STATISTICS_update (GST_stats,
1888 ("# SYN messages sent"),
1890 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1891 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1892 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1893 connect_msg.reserved = htonl (0);
1894 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1896 papi->send (papi->cls,
1898 (const char *) &connect_msg,
1899 sizeof (struct TransportSynMessage),
1901 SETUP_CONNECTION_TIMEOUT,
1902 &send_session_syn_cont, NULL))
1904 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1905 _("Failed to transmit SYN message to %s\n"),
1906 GST_plugins_a2s (na->address));
1907 n = lookup_neighbour (&na->address->peer);
1914 case GNUNET_TRANSPORT_PS_SYN_SENT:
1915 /* Remove address and request and additional one */
1916 GNUNET_assert (na == &n->primary_address);
1917 unset_primary_address (n);
1918 set_state_and_timeout (n,
1919 GNUNET_TRANSPORT_PS_INIT_ATS,
1920 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1921 /* Hard failure to send the SYN message with this address:
1922 Destroy address and session */
1924 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1925 /* Remove address and request an additional one */
1926 GNUNET_assert (na == &n->primary_address);
1927 unset_primary_address (n);
1928 set_state_and_timeout (n,
1929 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1930 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1932 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1933 GNUNET_assert (na == &n->alternative_address);
1934 GNUNET_STATISTICS_update (GST_stats,
1935 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN)"),
1938 /* Remove address and request an additional one */
1939 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1940 "Switch failed, cleaning up alternative address\n");
1941 free_address (&n->alternative_address);
1942 set_state_and_timeout (n,
1943 GNUNET_TRANSPORT_PS_CONNECTED,
1944 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1948 disconnect_neighbour (n);
1953 GST_neighbours_notify_data_sent (na->address,
1955 sizeof (struct TransportSynMessage));
1960 * Continuation called from our attempt to transmitted our
1961 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK to the specified @a
1962 * target. Continue processing based on the @a result. Specifically,
1963 * if we failed to transmit, discard the address we used.
1966 * @param target which peer received the transmission
1967 * @param result #GNUNET_OK if sending worked
1968 * @param size_payload how many bytes of payload were sent (ignored)
1969 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1972 send_session_syn_ack_cont (void *cls,
1973 const struct GNUNET_PeerIdentity *target,
1975 size_t size_payload,
1976 size_t size_on_wire)
1978 struct NeighbourMapEntry *n;
1980 n = lookup_neighbour (target);
1983 /* SYN_ACK continuation was called after neighbor was freed,
1984 * for example due to a time out for the state or the session
1985 * used was already terminated: nothing to do here... */
1989 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1991 /* SYN_ACK continuation was called after neighbor changed state,
1992 * for example due to a time out for the state or the session
1993 * used was already terminated: nothing to do here... */
1996 if (GNUNET_OK == result)
1999 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2000 _("Failed to send SYN_ACK message to peer `%s' using address `%s'\n"),
2001 GNUNET_i2s (target),
2002 GST_plugins_a2s (n->primary_address.address));
2004 /* Remove address and request and additional one */
2005 /* FIXME: what if the neighbour's primary address
2006 changed in the meantime? Might want to instead
2007 pass "something" around in closure to be sure. */
2008 unset_primary_address (n);
2009 n->ack_state = ACK_SEND_SYN_ACK;
2010 set_state_and_timeout (n,
2011 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2012 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2017 * Send a SYN_ACK message via the given address.
2019 * @param na address and session to use
2020 * @param timestamp timestamp to use for the ACK message
2021 * @return #GNUNET_SYSERR if sending immediately failed, #GNUNET_OK otherwise
2024 send_syn_ack_message (struct NeighbourAddress *na,
2025 struct GNUNET_TIME_Absolute timestamp)
2027 const struct GNUNET_HELLO_Address *address = na->address;
2028 struct GNUNET_ATS_Session *session = na->session;
2029 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2030 struct TransportSynMessage connect_msg;
2031 struct NeighbourMapEntry *n;
2033 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2034 "Sending SYN_ACK to peer `%s'\n",
2035 GNUNET_i2s (&address->peer));
2037 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2042 if (NULL == session)
2043 session = papi->get_session (papi->cls,
2045 if (NULL == session)
2050 GST_ats_new_session (address,
2052 GNUNET_STATISTICS_update (GST_stats,
2054 ("# SYN_ACK messages sent"),
2056 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
2057 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
2058 connect_msg.reserved = htonl (0);
2059 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
2061 if (GNUNET_SYSERR ==
2062 papi->send (papi->cls,
2064 (const char *) &connect_msg,
2065 sizeof (struct TransportSynMessage),
2067 GNUNET_TIME_UNIT_FOREVER_REL,
2068 &send_session_syn_ack_cont, NULL))
2070 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2071 _("Failed to transmit SYN_ACK message to %s\n"),
2072 GST_plugins_a2s (address));
2074 n = lookup_neighbour (&address->peer);
2080 /* Remove address and request and additional one */
2081 unset_primary_address (n);
2082 n->ack_state = ACK_SEND_SYN_ACK;
2083 set_state_and_timeout (n,
2084 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2085 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2092 * Function called by the bandwidth tracker for a peer whenever
2093 * the tracker's state changed such that we need to recalculate
2094 * the delay for flow control. We calculate the latest delay
2095 * and inform the plugin (if applicable).
2097 * @param cls the `struct NeighbourMapEntry` to update calculations for
2100 inbound_bw_tracker_update (void *cls)
2102 struct NeighbourMapEntry *n = cls;
2103 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2104 struct GNUNET_TIME_Relative delay;
2107 if (NULL == n->primary_address.address)
2108 return; /* not active, ignore */
2109 papi = GST_plugins_find (n->primary_address.address->transport_name);
2110 GNUNET_assert (NULL != papi);
2111 if (NULL == papi->update_inbound_delay)
2113 delay = GST_neighbours_calculate_receive_delay (&n->id,
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2117 "New inbound delay for peer `%s' is %llu ms\n",
2118 GNUNET_i2s (&n->id),
2119 (unsigned long long) delay.rel_value_us / 1000LL);
2120 papi->update_inbound_delay (papi->cls,
2122 n->primary_address.session,
2128 * Create a fresh entry in the neighbour map for the given peer
2130 * @param peer peer to create an entry for
2131 * @return new neighbour map entry
2133 static struct NeighbourMapEntry *
2134 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2136 struct NeighbourMapEntry *n;
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 "Creating new neighbour entry for `%s'\n",
2142 memcmp (&GST_my_identity,
2144 sizeof (struct GNUNET_PeerIdentity)));
2145 n = GNUNET_new (struct NeighbourMapEntry);
2147 n->ack_state = ACK_UNDEFINED;
2148 n->last_util_transmission = GNUNET_TIME_absolute_get();
2149 n->neighbour_receive_quota = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2150 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2151 &inbound_bw_tracker_update,
2153 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2154 MAX_BANDWIDTH_CARRY_S);
2155 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2156 set_state_and_timeout (n,
2157 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2158 GNUNET_TIME_UNIT_FOREVER_ABS);
2159 GNUNET_assert (GNUNET_OK ==
2160 GNUNET_CONTAINER_multipeermap_put (neighbours,
2163 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2164 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2173 * Entry in a DLL we use to keep track of pending blacklist checks.
2175 struct BlacklistCheckSwitchContext
2180 struct BlacklistCheckSwitchContext *prev;
2185 struct BlacklistCheckSwitchContext *next;
2188 * Handle to the blacklist check we are performing.
2190 struct GST_BlacklistCheck *blc;
2193 * Inbound bandwidth that was assigned to @e address.
2195 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2198 * Outbound bandwidth that was assigned to @e address.
2200 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2205 * We received a 'SYN' message from the other peer.
2206 * Consider switching to it.
2208 * @param message possibly a `struct TransportSynMessage` (check format)
2209 * @param peer identity of the peer to switch the address for
2210 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2213 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2214 const struct GNUNET_PeerIdentity *peer)
2216 const struct TransportSynMessage *scm;
2217 struct NeighbourMapEntry *n;
2218 struct GNUNET_TIME_Absolute ts;
2220 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2222 GNUNET_break_op (0);
2223 return GNUNET_SYSERR;
2225 GNUNET_STATISTICS_update (GST_stats,
2227 ("# SYN messages received"),
2229 if (NULL == neighbours)
2231 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2232 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2234 return GNUNET_OK; /* we're shutting down */
2236 scm = (const struct TransportSynMessage *) message;
2237 GNUNET_break_op (0 == ntohl (scm->reserved));
2238 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2240 memcmp (&GST_my_identity,
2242 sizeof (struct GNUNET_PeerIdentity)))
2244 /* loopback connection-to-self, ignore */
2245 return GNUNET_SYSERR;
2247 n = lookup_neighbour (peer);
2250 /* This is a new neighbour and set to not connected */
2251 n = setup_neighbour (peer);
2254 /* Remember this SYN message in neighbour */
2255 n->ack_state = ACK_SEND_SYN_ACK;
2256 n->connect_ack_timestamp = ts;
2258 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2259 "Received SYN for peer `%s' in state %s/%s\n",
2261 GNUNET_TRANSPORT_ps2s (n->state),
2262 print_ack_state (n->ack_state));
2266 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2267 /* Request an address from ATS to send SYN_ACK to this peer */
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_INIT_ATS:
2273 /* SYN message takes priority over us asking ATS for address:
2274 * Wait for ATS to suggest an address and send SYN_ACK */
2275 set_state_and_timeout (n,
2276 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2277 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2279 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2280 /* We already wait for an address to send an SYN_ACK */
2282 case GNUNET_TRANSPORT_PS_SYN_SENT:
2283 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2284 /* Send ACK immediately */
2285 n->ack_state = ACK_SEND_ACK;
2286 send_syn_ack_message (&n->primary_address,
2289 case GNUNET_TRANSPORT_PS_CONNECTED:
2290 /* we are already connected and can thus send the ACK immediately */
2291 GNUNET_assert (NULL != n->primary_address.address);
2292 GNUNET_assert (NULL != n->primary_address.session);
2293 n->ack_state = ACK_SEND_ACK;
2294 send_syn_ack_message (&n->primary_address,
2297 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2298 /* We wait for ATS address suggestion */
2300 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2301 /* We received a SYN message while waiting for a SYN_ACK in fast
2302 * reconnect. Send SYN_ACK immediately */
2303 n->ack_state = ACK_SEND_ACK;
2304 send_syn_ack_message (&n->primary_address,
2305 n->connect_ack_timestamp);
2307 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2308 /* We are already connected and can thus send the ACK immediately;
2309 still, it can never hurt to have an alternative address, so also
2310 tell ATS about it */
2311 GNUNET_assert (NULL != n->primary_address.address);
2312 GNUNET_assert (NULL != n->primary_address.session);
2313 n->ack_state = ACK_SEND_ACK;
2314 send_syn_ack_message (&n->primary_address,
2317 case GNUNET_TRANSPORT_PS_DISCONNECT:
2318 /* Get rid of remains and re-try */
2320 n = setup_neighbour (peer);
2321 /* Remember the SYN time stamp for ACK message */
2322 n->ack_state = ACK_SEND_SYN_ACK;
2323 n->connect_ack_timestamp = ts;
2324 /* Request an address for the peer */
2325 set_state_and_timeout (n,
2326 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2327 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2329 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2330 /* should not be possible */
2334 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2335 "Unhandled state `%s'\n",
2336 GNUNET_TRANSPORT_ps2s (n->state));
2338 return GNUNET_SYSERR;
2345 * Check if the given @a address is the same that we are already
2346 * using for the respective neighbour. If so, update the bandwidth
2347 * assignment and possibly the session and return #GNUNET_OK.
2348 * If the new address is different from what the neighbour is
2349 * using right now, return #GNUNET_NO.
2351 * @param address address of the other peer,
2352 * @param session session to use or NULL if transport should initiate a session
2353 * @param bandwidth_in inbound quota to be used when connection is up,
2354 * 0 to disconnect from peer
2355 * @param bandwidth_out outbound quota to be used when connection is up,
2356 * 0 to disconnect from peer
2357 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2358 * #GNUNET_NO if more extensive changes are required (address changed)
2361 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2362 struct GNUNET_ATS_Session *session,
2363 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2364 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2366 struct NeighbourMapEntry *n;
2368 n = lookup_neighbour (&address->peer);
2370 (NULL == n->primary_address.address) ||
2371 (0 != GNUNET_HELLO_address_cmp (address,
2372 n->primary_address.address)) )
2374 /* We are not really switching addresses, but merely adjusting
2375 session and/or bandwidth, can do fast ATS update! */
2376 if (session != n->primary_address.session)
2378 /* switch to a different session, but keeping same address; could
2379 happen if there is a 2nd inbound connection */
2380 n->primary_address.session = session;
2381 GNUNET_assert (GNUNET_YES ==
2382 GST_ats_is_known (n->primary_address.address,
2383 n->primary_address.session));
2385 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
2387 n->primary_address.bandwidth_in = bandwidth_in;
2388 set_incoming_quota (n,
2391 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
2393 n->primary_address.bandwidth_out = bandwidth_out;
2394 send_outbound_quota_to_clients (n);
2401 * We've been asked to switch addresses, and just now got the result
2402 * from the blacklist check to see if this is allowed.
2404 * @param cls the `struct BlacklistCheckSwitchContext` with
2405 * the information about the future address
2406 * @param peer the peer we may switch addresses on
2407 * @param address address associated with the request
2408 * @param session session associated with the request
2409 * @param result #GNUNET_OK if the connection is allowed,
2410 * #GNUNET_NO if not,
2411 * #GNUNET_SYSERR if operation was aborted
2414 switch_address_bl_check_cont (void *cls,
2415 const struct GNUNET_PeerIdentity *peer,
2416 const struct GNUNET_HELLO_Address *address,
2417 struct GNUNET_ATS_Session *session,
2420 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2421 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2422 struct NeighbourMapEntry *n;
2424 if (GNUNET_SYSERR == result)
2427 papi = GST_plugins_find (address->transport_name);
2428 GNUNET_assert (NULL != papi);
2430 if (GNUNET_NO == result)
2432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2433 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2434 GST_plugins_a2s (address),
2437 GNUNET_STATISTICS_update (GST_stats,
2438 "# ATS suggestions ignored (blacklist denied)",
2441 if (NULL != session)
2442 papi->disconnect_session (papi->cls,
2445 GNUNET_HELLO_address_check_option (address,
2446 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2447 GST_ats_block_address (address,
2453 if (NULL == session)
2455 /* need to create a session, ATS only gave us an address */
2456 session = papi->get_session (papi->cls,
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2459 "Obtained new session for peer `%s' and address '%s': %p\n",
2460 GNUNET_i2s (&address->peer),
2461 GST_plugins_a2s (address),
2463 if (NULL != session)
2464 GST_ats_new_session (address,
2467 if (NULL == session)
2469 /* session creation failed, bad!, fail! */
2470 GNUNET_STATISTICS_update (GST_stats,
2471 "# ATS suggestions ignored (failed to create session)",
2474 /* No session could be obtained, remove blacklist check and clean up */
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476 "Failed to obtain new session for peer `%s' and address '%s'\n",
2477 GNUNET_i2s (&address->peer),
2478 GST_plugins_a2s (address));
2479 GST_ats_block_address (address,
2484 /* We did this check already before going into blacklist, but
2485 it is theoretically possible that the situation changed in
2486 the meantime, hence we check again here */
2488 try_run_fast_ats_update (address,
2490 blc_ctx->bandwidth_in,
2491 blc_ctx->bandwidth_out))
2492 goto cleanup; /* was just a minor update, we're done */
2494 /* check if we also need to setup the neighbour entry */
2495 if (NULL == (n = lookup_neighbour (peer)))
2497 n = setup_neighbour (peer);
2498 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2501 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2502 "Peer `%s' switches to address `%s'\n",
2503 GNUNET_i2s (&address->peer),
2504 GST_plugins_a2s (address));
2508 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2510 GST_ats_block_address (address,
2514 case GNUNET_TRANSPORT_PS_INIT_ATS:
2515 /* We requested an address and ATS suggests one:
2516 * set primary address and send SYN message*/
2517 set_primary_address (n,
2520 blc_ctx->bandwidth_in,
2521 blc_ctx->bandwidth_out);
2522 if (ACK_SEND_SYN_ACK == n->ack_state)
2524 /* Send pending SYN_ACK message */
2525 n->ack_state = ACK_SEND_ACK;
2526 send_syn_ack_message (&n->primary_address,
2527 n->connect_ack_timestamp);
2529 set_state_and_timeout (n,
2530 GNUNET_TRANSPORT_PS_SYN_SENT,
2531 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2532 send_syn (&n->primary_address);
2534 case GNUNET_TRANSPORT_PS_SYN_SENT:
2535 /* ATS suggested a new address while waiting for an SYN_ACK:
2536 * Switch and send new SYN */
2537 /* ATS suggests a different address, switch again */
2538 set_primary_address (n,
2541 blc_ctx->bandwidth_in,
2542 blc_ctx->bandwidth_out);
2543 if (ACK_SEND_SYN_ACK == n->ack_state)
2545 /* Send pending SYN_ACK message */
2546 n->ack_state = ACK_SEND_ACK;
2547 send_syn_ack_message (&n->primary_address,
2548 n->connect_ack_timestamp);
2550 set_state_and_timeout (n,
2551 GNUNET_TRANSPORT_PS_SYN_SENT,
2552 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2553 send_syn (&n->primary_address);
2555 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2556 /* We requested an address and ATS suggests one:
2557 * set primary address and send SYN_ACK message*/
2558 set_primary_address (n,
2561 blc_ctx->bandwidth_in,
2562 blc_ctx->bandwidth_out);
2563 /* Send an ACK message as a response to the SYN msg */
2564 set_state_and_timeout (n,
2565 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2566 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2567 send_syn_ack_message (&n->primary_address,
2568 n->connect_ack_timestamp);
2569 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2570 (ACK_UNDEFINED == n->ack_state) )
2571 n->ack_state = ACK_SEND_ACK;
2573 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2574 /* ATS asks us to switch while we were trying to connect; switch to new
2575 address and check blacklist again */
2576 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2578 n->ack_state = ACK_SEND_ACK;
2579 send_syn_ack_message (&n->primary_address,
2580 n->connect_ack_timestamp);
2582 set_primary_address (n,
2585 blc_ctx->bandwidth_in,
2586 blc_ctx->bandwidth_out);
2587 set_state_and_timeout (n,
2588 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2589 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2591 case GNUNET_TRANSPORT_PS_CONNECTED:
2592 GNUNET_assert (NULL != n->primary_address.address);
2593 GNUNET_assert (NULL != n->primary_address.session);
2594 GNUNET_break (n->primary_address.session != session);
2595 /* ATS asks us to switch a life connection; see if we can get
2596 a SYN_ACK on it before we actually do this! */
2597 set_alternative_address (n,
2600 blc_ctx->bandwidth_in,
2601 blc_ctx->bandwidth_out);
2602 set_state_and_timeout (n,
2603 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2604 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2605 GNUNET_STATISTICS_update (GST_stats,
2606 gettext_noop ("# Attempts to switch addresses"),
2609 send_syn (&n->alternative_address);
2611 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2612 set_primary_address (n,
2615 blc_ctx->bandwidth_in,
2616 blc_ctx->bandwidth_out);
2617 if (ACK_SEND_SYN_ACK == n->ack_state)
2619 /* Send pending SYN_ACK message */
2620 n->ack_state = ACK_SEND_ACK;
2621 send_syn_ack_message (&n->primary_address,
2622 n->connect_ack_timestamp);
2624 set_state_and_timeout (n,
2625 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2626 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2627 send_syn (&n->primary_address);
2629 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2630 /* ATS asks us to switch while we were trying to reconnect; switch to new
2631 address and send SYN again */
2632 set_primary_address (n,
2635 blc_ctx->bandwidth_in,
2636 blc_ctx->bandwidth_out);
2637 set_state_and_timeout (n,
2638 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2639 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2640 send_syn (&n->primary_address);
2642 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2643 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2645 (n->primary_address.session == session) )
2647 /* ATS switches back to still-active session */
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2649 "ATS double-switched, cleaning up alternative address\n");
2650 free_address (&n->alternative_address);
2651 set_state_and_timeout (n,
2652 GNUNET_TRANSPORT_PS_CONNECTED,
2656 /* ATS asks us to switch a life connection, send */
2657 set_alternative_address (n,
2660 blc_ctx->bandwidth_in,
2661 blc_ctx->bandwidth_out);
2662 set_state_and_timeout (n,
2663 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2664 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2665 send_syn (&n->alternative_address);
2667 case GNUNET_TRANSPORT_PS_DISCONNECT:
2668 /* not going to switch addresses while disconnecting */
2669 GNUNET_STATISTICS_update (GST_stats,
2670 "# ATS suggestion ignored (disconnecting)",
2674 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2679 "Unhandled state `%s'\n",
2680 GNUNET_TRANSPORT_ps2s (n->state));
2685 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2688 GNUNET_free (blc_ctx);
2693 * For the given peer, switch to this address.
2695 * Before accepting this addresses and actively using it, a blacklist check
2698 * If any check fails or the suggestion can somehow not be followed, we
2699 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2700 * could not be satisfied and force ATS to do something else.
2702 * @param address address of the other peer,
2703 * @param session session to use or NULL if transport should initiate a session
2704 * @param bandwidth_in inbound quota to be used when connection is up,
2705 * 0 to disconnect from peer
2706 * @param bandwidth_out outbound quota to be used when connection is up,
2707 * 0 to disconnect from peer
2710 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2711 struct GNUNET_ATS_Session *session,
2712 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2713 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2715 struct GST_BlacklistCheck *blc;
2716 struct BlacklistCheckSwitchContext *blc_ctx;
2718 GNUNET_assert (NULL != address->transport_name);
2720 try_run_fast_ats_update (address,
2726 /* Check if plugin is available */
2727 if (NULL == (GST_plugins_find (address->transport_name)))
2729 /* we don't have the plugin for this address */
2731 GST_ats_block_address (address,
2735 if ((NULL == session) &&
2736 (GNUNET_HELLO_address_check_option (address,
2737 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2739 /* This is a inbound address and we do not have a session to use! */
2741 GST_ats_block_address (address,
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2747 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2748 GST_plugins_a2s (address),
2749 GNUNET_i2s (&address->peer),
2750 (unsigned int) ntohl (bandwidth_in.value__),
2751 (unsigned int) ntohl (bandwidth_out.value__));
2753 /* Perform blacklist check */
2754 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2755 blc_ctx->bandwidth_in = bandwidth_in;
2756 blc_ctx->bandwidth_out = bandwidth_out;
2757 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2760 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2761 address->transport_name,
2762 &switch_address_bl_check_cont,
2773 * Function called to send network utilization data to ATS for
2774 * each active connection.
2777 * @param key peer we send utilization data for
2778 * @param value the `struct NeighbourMapEntry *` with data to send
2779 * @return #GNUNET_OK (continue to iterate)
2782 send_utilization_data (void *cls,
2783 const struct GNUNET_PeerIdentity *key,
2786 struct NeighbourMapEntry *n = value;
2789 struct GNUNET_TIME_Relative delta;
2791 if ( (GNUNET_YES != test_connected (n)) ||
2792 (NULL == n->primary_address.address) )
2794 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2795 GNUNET_TIME_absolute_get ());
2797 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2798 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2800 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2801 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2804 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2808 GST_ats_update_utilization (n->primary_address.address,
2811 n->util_total_bytes_recv = 0;
2812 n->util_total_bytes_sent = 0;
2813 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2819 * Task transmitting utilization in a regular interval
2821 * @param cls the 'struct NeighbourMapEntry' for which we are running
2824 utilization_transmission (void *cls)
2826 util_transmission_tk = NULL;
2827 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
2828 &send_utilization_data,
2830 util_transmission_tk
2831 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2832 &utilization_transmission,
2838 * Track information about data we received from the
2839 * given address (used to notify ATS about our utilization
2840 * of allocated resources).
2842 * @param address the address we got data from
2843 * @param message the message we received (really only the size is used)
2846 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2847 const struct GNUNET_MessageHeader *message)
2849 struct NeighbourMapEntry *n;
2851 n = lookup_neighbour (&address->peer);
2854 n->util_total_bytes_recv += ntohs (message->size);
2859 * Track information about data we transmitted using the given @a
2860 * address and @a session (used to notify ATS about our utilization of
2861 * allocated resources).
2863 * @param address the address we transmitted data to
2864 * @param session session we used to transmit data
2865 * @param message the message we sent (really only the size is used)
2868 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2869 struct GNUNET_ATS_Session *session,
2872 struct NeighbourMapEntry *n;
2874 n = lookup_neighbour (&address->peer);
2877 if (n->primary_address.session != session)
2879 n->util_total_bytes_sent += size;
2884 * Master task run for every neighbour. Performs all of the time-related
2885 * activities (keep alive, send next message, disconnect if idle, finish
2886 * clean up after disconnect).
2888 * @param cls the 'struct NeighbourMapEntry' for which we are running
2891 master_task (void *cls)
2893 struct NeighbourMapEntry *n = cls;
2894 struct GNUNET_TIME_Relative delay;
2897 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2899 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2900 GNUNET_i2s (&n->id),
2901 GNUNET_TRANSPORT_ps2s(n->state),
2902 GNUNET_STRINGS_relative_time_to_string (delay,
2906 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2907 /* invalid state for master task, clean up */
2911 case GNUNET_TRANSPORT_PS_INIT_ATS:
2912 if (0 == delay.rel_value_us)
2914 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2915 "Connection to `%s' timed out waiting for ATS to provide address\n",
2916 GNUNET_i2s (&n->id));
2921 case GNUNET_TRANSPORT_PS_SYN_SENT:
2922 if (0 == delay.rel_value_us)
2924 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2925 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2926 GNUNET_i2s (&n->id));
2927 /* Remove address and request and additional one */
2928 unset_primary_address (n);
2929 set_state_and_timeout (n,
2930 GNUNET_TRANSPORT_PS_INIT_ATS,
2931 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2935 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2936 if (0 == delay.rel_value_us)
2938 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2939 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2940 GNUNET_i2s (&n->id));
2945 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2946 if (0 == delay.rel_value_us)
2948 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2949 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2950 GNUNET_i2s (&n->id));
2951 disconnect_neighbour (n);
2955 case GNUNET_TRANSPORT_PS_CONNECTED:
2956 if (0 == delay.rel_value_us)
2958 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2959 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2960 GNUNET_i2s (&n->id));
2961 disconnect_neighbour (n);
2964 try_transmission_to_peer (n);
2967 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2968 if (0 == delay.rel_value_us)
2970 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2971 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2972 GNUNET_i2s (&n->id));
2973 disconnect_neighbour (n);
2977 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2978 if (0 == delay.rel_value_us)
2980 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2981 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
2982 GNUNET_i2s (&n->id));
2983 disconnect_neighbour (n);
2987 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2988 if (0 == delay.rel_value_us)
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2991 "Switch failed, cleaning up alternative address\n");
2992 free_address (&n->alternative_address);
2993 set_state_and_timeout (n,
2994 GNUNET_TRANSPORT_PS_CONNECTED,
2995 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2997 try_transmission_to_peer (n);
3000 case GNUNET_TRANSPORT_PS_DISCONNECT:
3001 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3002 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3003 GNUNET_i2s (&n->id));
3006 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3007 /* how did we get here!? */
3011 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3012 "Unhandled state `%s'\n",
3013 GNUNET_TRANSPORT_ps2s (n->state));
3017 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3018 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3019 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3021 /* if we are *now* in one of the two states, we're sending
3022 keep alive messages, so we need to consider the keepalive
3023 delay, not just the connection timeout */
3024 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3027 if (NULL == n->task)
3028 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3035 * Send a ACK message to the neighbour to confirm that we
3038 * @param n neighbour to send the ACK to
3041 send_session_ack_message (struct NeighbourMapEntry *n)
3043 struct GNUNET_MessageHeader msg;
3045 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3046 "Sending ACK message to peer `%s'\n",
3047 GNUNET_i2s (&n->id));
3049 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3050 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3051 (void) send_with_session (n,
3053 sizeof (struct GNUNET_MessageHeader),
3055 GNUNET_TIME_UNIT_FOREVER_REL,
3062 * We received a 'SESSION_SYN_ACK' message from the other peer.
3063 * Consider switching to it.
3065 * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
3066 * @param peer identity of the peer to switch the address for
3067 * @param address address of the other peer, NULL if other peer
3069 * @param session session to use (or NULL)
3070 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3073 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3074 const struct GNUNET_HELLO_Address *address,
3075 struct GNUNET_ATS_Session *session)
3077 const struct TransportSynMessage *scm;
3078 struct GNUNET_TIME_Absolute ts;
3079 struct NeighbourMapEntry *n;
3081 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3083 GNUNET_break_op (0);
3084 return GNUNET_SYSERR;
3086 GNUNET_STATISTICS_update (GST_stats,
3088 ("# SYN_ACK messages received"),
3090 scm = (const struct TransportSynMessage *) message;
3091 GNUNET_break_op (ntohl (scm->reserved) == 0);
3092 if (NULL == (n = lookup_neighbour (&address->peer)))
3094 GNUNET_STATISTICS_update (GST_stats,
3096 ("# unexpected SYN_ACK messages (no peer)"),
3098 return GNUNET_SYSERR;
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3101 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3102 GNUNET_i2s (&address->peer),
3103 GNUNET_TRANSPORT_ps2s (n->state),
3104 print_ack_state (n->ack_state));
3105 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3108 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3111 return GNUNET_SYSERR;
3112 case GNUNET_TRANSPORT_PS_INIT_ATS:
3113 GNUNET_STATISTICS_update (GST_stats,
3114 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3118 case GNUNET_TRANSPORT_PS_SYN_SENT:
3119 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3121 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3122 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3125 set_state_and_timeout (n,
3126 GNUNET_TRANSPORT_PS_CONNECTED,
3127 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3128 set_primary_address (n,
3129 n->primary_address.address,
3130 n->primary_address.session,
3131 n->primary_address.bandwidth_in,
3132 n->primary_address.bandwidth_out);
3133 send_session_ack_message (n);
3135 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3136 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3137 GNUNET_STATISTICS_update (GST_stats,
3138 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3142 case GNUNET_TRANSPORT_PS_CONNECTED:
3143 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3144 send_session_ack_message (n);
3146 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3147 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3148 to give us a new address... */
3149 GNUNET_STATISTICS_update (GST_stats,
3150 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3154 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3155 /* Reconnecting with new address address worked; 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 send_session_ack_message (n);
3161 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3162 /* new address worked; adopt it and go back to connected! */
3163 set_state_and_timeout (n,
3164 GNUNET_TRANSPORT_PS_CONNECTED,
3165 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3166 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3168 /* Set primary addresses */
3169 set_primary_address (n,
3170 n->alternative_address.address,
3171 n->alternative_address.session,
3172 n->alternative_address.bandwidth_in,
3173 n->alternative_address.bandwidth_out);
3174 GNUNET_STATISTICS_update (GST_stats,
3175 gettext_noop ("# Successful attempts to switch addresses"),
3179 GNUNET_HELLO_address_free (n->alternative_address.address);
3180 memset (&n->alternative_address,
3182 sizeof (n->alternative_address));
3183 send_session_ack_message (n);
3185 case GNUNET_TRANSPORT_PS_DISCONNECT:
3186 GNUNET_STATISTICS_update (GST_stats,
3188 ("# unexpected SYN_ACK messages (disconnecting)"),
3190 return GNUNET_SYSERR;
3191 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3196 "Unhandled state `%s'\n",
3197 GNUNET_TRANSPORT_ps2s (n->state));
3199 return GNUNET_SYSERR;
3206 * A session was terminated. Take note; if needed, try to get
3207 * an alternative address from ATS.
3209 * @param peer identity of the peer where the session died
3210 * @param session session that is gone
3211 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3212 * this session was not in use
3215 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3216 struct GNUNET_ATS_Session *session)
3218 struct NeighbourMapEntry *n;
3220 if (NULL == (n = lookup_neighbour (peer)))
3221 return GNUNET_NO; /* can't affect us */
3222 if (session != n->primary_address.session)
3224 /* Free alternative address */
3225 if (session == n->alternative_address.session)
3227 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3228 set_state_and_timeout (n,
3229 GNUNET_TRANSPORT_PS_CONNECTED,
3231 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3232 "Session died, cleaning up alternative address\n");
3233 free_address (&n->alternative_address);
3235 return GNUNET_NO; /* doesn't affect us further */
3238 n->expect_latency_response = GNUNET_NO;
3239 /* The session for neighbour's primary address died */
3242 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3246 case GNUNET_TRANSPORT_PS_INIT_ATS:
3250 case GNUNET_TRANSPORT_PS_SYN_SENT:
3251 /* The session used to send the SYN terminated:
3252 * this implies a connect error*/
3253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3254 "Failed to send SYN in CONNECT_SENT with `%s' %p: session terminated\n",
3255 GST_plugins_a2s (n->primary_address.address),
3256 n->primary_address.session);
3258 /* Destroy the address since it cannot be used */
3259 unset_primary_address (n);
3260 set_state_and_timeout (n,
3261 GNUNET_TRANSPORT_PS_INIT_ATS,
3262 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3264 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3265 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3266 /* error on inbound session; free neighbour entirely */
3269 case GNUNET_TRANSPORT_PS_CONNECTED:
3270 /* Our primary connection died, try a fast reconnect */
3271 unset_primary_address (n);
3272 set_state_and_timeout (n,
3273 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3274 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3276 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3277 /* we don't have an address, how can it go down? */
3280 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3281 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3282 "Failed to send SYN in RECONNECT_SENT with `%s' %p: session terminated\n",
3283 GST_plugins_a2s (n->primary_address.address),
3284 n->primary_address.session);
3285 /* Destroy the address since it cannot be used */
3286 unset_primary_address (n);
3287 set_state_and_timeout (n,
3288 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3289 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3291 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3292 /* primary went down while we were waiting for SYN_ACK on secondary;
3293 secondary as primary */
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 "Connection `%s' %p to peer `%s' was terminated while switching, "
3297 "switching to alternative address `%s' %p\n",
3298 GST_plugins_a2s (n->primary_address.address),
3299 n->primary_address.session,
3301 GST_plugins_a2s (n->alternative_address.address),
3302 n->alternative_address.session);
3304 /* Destroy the inbound address since it cannot be used */
3305 free_address (&n->primary_address);
3306 n->primary_address = n->alternative_address;
3307 GNUNET_assert (GNUNET_YES ==
3308 GST_ats_is_known (n->primary_address.address,
3309 n->primary_address.session));
3310 memset (&n->alternative_address,
3312 sizeof (struct NeighbourAddress));
3313 set_state_and_timeout (n,
3314 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3315 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3317 case GNUNET_TRANSPORT_PS_DISCONNECT:
3318 unset_primary_address (n);
3320 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3321 /* neighbour was freed and plugins told to terminate session */
3324 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3325 "Unhandled state `%s'\n",
3326 GNUNET_TRANSPORT_ps2s (n->state));
3330 if (NULL != n->task)
3331 GNUNET_SCHEDULER_cancel (n->task);
3332 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3338 * We received a 'ACK' message from the other peer.
3339 * If we sent a 'SYN_ACK' last, this means we are now
3340 * connected. Otherwise, do nothing.
3342 * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
3343 * @param address address of the other peer
3344 * @param session session to use (or NULL)
3345 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3348 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3349 const struct GNUNET_HELLO_Address *address,
3350 struct GNUNET_ATS_Session *session)
3352 struct NeighbourMapEntry *n;
3354 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3356 GNUNET_break_op (0);
3357 return GNUNET_SYSERR;
3359 GNUNET_STATISTICS_update (GST_stats,
3360 gettext_noop ("# ACK messages received"),
3363 if (NULL == (n = lookup_neighbour (&address->peer)))
3365 GNUNET_break_op (0);
3366 return GNUNET_SYSERR;
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3369 "Received ACK for peer `%s' in state %s/%s\n",
3370 GNUNET_i2s (&address->peer),
3371 GNUNET_TRANSPORT_ps2s (n->state),
3372 print_ack_state (n->ack_state));
3374 /* Check if we are in a plausible state for having sent
3375 a SYN_ACK. If not, return, otherwise break.
3377 The remote peers sends a ACK as a response for a SYN_ACK
3381 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3382 now wait for the ACK to finally be connected
3383 - If we sent a SYN_ACK to this peer before */
3385 if ( ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3386 (ACK_SEND_ACK != n->ack_state) ) ||
3387 (NULL == n->primary_address.address) )
3389 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3390 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3391 GNUNET_i2s (&address->peer),
3392 GNUNET_TRANSPORT_ps2s (n->state),
3393 print_ack_state (n->ack_state));
3395 GNUNET_STATISTICS_update (GST_stats,
3396 gettext_noop ("# unexpected ACK messages"),
3401 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3403 /* We tried to switch addresses while being connect. We explicitly wait
3404 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3405 * so we do not want to set the address as in use! */
3408 set_state_and_timeout (n,
3409 GNUNET_TRANSPORT_PS_CONNECTED,
3410 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3412 if (NULL == n->primary_address.address) {
3414 * We are in state = PSY_SYN_RECV_ACK or ack_state = ACK_SEND_ACK, which
3415 * really means we did try (and succeed) to send a SYN and are waiting for
3417 * That suggests that the primary_address used to be non-NULL, but maybe it
3418 * got reset to NULL without the state being changed appropriately?
3424 /* Reset backoff for primary address */
3425 GST_ats_block_reset (n->primary_address.address,
3426 n->primary_address.session);
3432 * Test if we're connected to the given peer.
3434 * @param target peer to test
3435 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3438 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3440 return test_connected (lookup_neighbour (target));
3445 * Task to asynchronously run #free_neighbour().
3447 * @param cls the `struct NeighbourMapEntry` to free
3450 delayed_disconnect (void *cls)
3452 struct NeighbourMapEntry *n = cls;
3454 n->delayed_disconnect_task = NULL;
3455 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3456 "Disconnecting by request from peer %s\n",
3457 GNUNET_i2s (&n->id));
3463 * We received a quota message from the given peer,
3464 * validate and process.
3466 * @param peer sender of the message
3467 * @param msg the quota message
3470 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
3471 const struct GNUNET_MessageHeader *msg)
3473 struct NeighbourMapEntry *n;
3474 const struct GNUNET_ATS_SessionQuotaMessage *sqm;
3475 struct GNUNET_BANDWIDTH_Value32NBO last;
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3478 "Received QUOTA message from peer `%s'\n",
3480 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionQuotaMessage))
3482 GNUNET_break_op (0);
3483 GNUNET_STATISTICS_update (GST_stats,
3484 gettext_noop ("# quota messages ignored (malformed)"),
3489 GNUNET_STATISTICS_update (GST_stats,
3491 ("# QUOTA messages received"),
3493 sqm = (const struct GNUNET_ATS_SessionQuotaMessage *) msg;
3494 if (NULL == (n = lookup_neighbour (peer)))
3499 last = GNUNET_BANDWIDTH_value_max (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3500 GNUNET_BANDWIDTH_value_init (ntohl (sqm->quota)));
3501 if (last.value__ != n->neighbour_receive_quota.value__)
3503 n->neighbour_receive_quota = last;
3504 send_outbound_quota_to_clients (n);
3510 * We received a disconnect message from the given peer,
3511 * validate and process.
3513 * @param peer sender of the message
3514 * @param msg the disconnect message
3517 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3518 const struct GNUNET_MessageHeader *msg)
3520 struct NeighbourMapEntry *n;
3521 const struct GNUNET_ATS_SessionDisconnectMessage *sdm;
3523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3524 "Received DISCONNECT message from peer `%s'\n",
3526 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionDisconnectMessage))
3528 GNUNET_break_op (0);
3529 GNUNET_STATISTICS_update (GST_stats,
3531 ("# disconnect messages ignored (malformed)"),
3536 GNUNET_STATISTICS_update (GST_stats,
3538 ("# DISCONNECT messages received"),
3540 sdm = (const struct GNUNET_ATS_SessionDisconnectMessage *) msg;
3541 if (NULL == (n = lookup_neighbour (peer)))
3546 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3548 GNUNET_STATISTICS_update (GST_stats,
3549 gettext_noop ("# disconnect messages ignored (timestamp)"),
3554 if (0 != memcmp (peer,
3556 sizeof (struct GNUNET_PeerIdentity)))
3558 GNUNET_break_op (0);
3561 if (ntohl (sdm->purpose.size) !=
3562 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3563 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3564 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3567 "DISCONNECT message from peer `%s' has invalid size\n",
3569 GNUNET_break_op (0);
3573 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3579 "DISCONNECT message from peer `%s' cannot be verified \n",
3581 GNUNET_break_op (0);
3584 if (NULL == n->delayed_disconnect_task)
3586 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect,
3593 * Closure for the #neighbours_iterate() function.
3595 struct IteratorContext
3598 * Function to call on each connected neighbour.
3600 GST_NeighbourIterator cb;
3603 * Closure for @e cb.
3610 * Call the callback from the closure for each neighbour.
3612 * @param cls the `struct IteratorContext`
3613 * @param key the hash of the public key of the neighbour
3614 * @param value the `struct NeighbourMapEntry`
3615 * @return #GNUNET_OK (continue to iterate)
3618 neighbours_iterate (void *cls,
3619 const struct GNUNET_PeerIdentity *key,
3622 struct IteratorContext *ic = cls;
3623 struct NeighbourMapEntry *n = value;
3624 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3625 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3627 if (NULL != n->primary_address.address)
3629 bandwidth_in = n->primary_address.bandwidth_in;
3630 bandwidth_out = n->primary_address.bandwidth_out;
3634 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3635 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3639 n->primary_address.address,
3642 bandwidth_in, bandwidth_out);
3648 * Iterate over all connected neighbours.
3650 * @param cb function to call
3651 * @param cb_cls closure for @a cb
3654 GST_neighbours_iterate (GST_NeighbourIterator cb,
3657 struct IteratorContext ic;
3659 if (NULL == neighbours)
3660 return; /* can happen during shutdown */
3663 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3664 &neighbours_iterate,
3670 * If we have an active connection to the given target, it must be shutdown.
3672 * @param target peer to disconnect from
3675 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3677 struct NeighbourMapEntry *n;
3679 if (NULL == (n = lookup_neighbour (target)))
3680 return; /* not active */
3681 if (GNUNET_YES == test_connected (n))
3682 GNUNET_STATISTICS_update (GST_stats,
3683 gettext_noop ("# disconnected from peer upon explicit request"),
3686 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3687 "Forced disconnect from peer %s\n",
3688 GNUNET_i2s (target));
3689 disconnect_neighbour (n);
3694 * Obtain current address information for the given neighbour.
3697 * @return address currently used
3699 const struct GNUNET_HELLO_Address *
3700 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3702 struct NeighbourMapEntry *n;
3704 n = lookup_neighbour (peer);
3707 return n->primary_address.address;
3712 * Initialize the neighbours subsystem.
3714 * @param max_fds maximum number of fds to use
3717 GST_neighbours_start (unsigned int max_fds)
3719 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3721 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3722 &utilization_transmission,
3728 * Disconnect from the given neighbour.
3731 * @param key hash of neighbour's public key (not used)
3732 * @param value the `struct NeighbourMapEntry` of the neighbour
3733 * @return #GNUNET_OK (continue to iterate)
3736 disconnect_all_neighbours (void *cls,
3737 const struct GNUNET_PeerIdentity *key,
3740 struct NeighbourMapEntry *n = value;
3742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3743 "Disconnecting peer `%4s' during shutdown\n",
3744 GNUNET_i2s (&n->id));
3751 * Cleanup the neighbours subsystem.
3754 GST_neighbours_stop ()
3756 if (NULL == neighbours)
3758 if (NULL != util_transmission_tk)
3760 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3761 util_transmission_tk = NULL;
3763 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3764 &disconnect_all_neighbours,
3766 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3771 /* end of file gnunet-service-transport_neighbours.c */