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_delayed (GNUNET_TIME_absolute_get_remaining (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);
2239 n = lookup_neighbour (peer);
2242 /* This is a new neighbour and set to not connected */
2243 n = setup_neighbour (peer);
2246 /* Remember this SYN message in neighbour */
2247 n->ack_state = ACK_SEND_SYN_ACK;
2248 n->connect_ack_timestamp = ts;
2250 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2251 "Received SYN for peer `%s' in state %s/%s\n",
2253 GNUNET_TRANSPORT_ps2s (n->state),
2254 print_ack_state (n->ack_state));
2258 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2259 /* Request an address from ATS to send SYN_ACK to this peer */
2260 set_state_and_timeout (n,
2261 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2262 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2264 case GNUNET_TRANSPORT_PS_INIT_ATS:
2265 /* SYN message takes priority over us asking ATS for address:
2266 * Wait for ATS to suggest an address and send SYN_ACK */
2267 set_state_and_timeout (n,
2268 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2269 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2271 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2272 /* We already wait for an address to send an SYN_ACK */
2274 case GNUNET_TRANSPORT_PS_SYN_SENT:
2275 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2276 /* Send ACK immediately */
2277 n->ack_state = ACK_SEND_ACK;
2278 send_syn_ack_message (&n->primary_address,
2281 case GNUNET_TRANSPORT_PS_CONNECTED:
2282 /* we are already connected and can thus send the ACK immediately */
2283 GNUNET_assert (NULL != n->primary_address.address);
2284 GNUNET_assert (NULL != n->primary_address.session);
2285 n->ack_state = ACK_SEND_ACK;
2286 send_syn_ack_message (&n->primary_address,
2289 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2290 /* We wait for ATS address suggestion */
2292 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2293 /* We received a SYN message while waiting for a SYN_ACK in fast
2294 * reconnect. Send SYN_ACK immediately */
2295 n->ack_state = ACK_SEND_ACK;
2296 send_syn_ack_message (&n->primary_address,
2297 n->connect_ack_timestamp);
2299 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2300 /* We are already connected and can thus send the ACK immediately;
2301 still, it can never hurt to have an alternative address, so also
2302 tell ATS about it */
2303 GNUNET_assert (NULL != n->primary_address.address);
2304 GNUNET_assert (NULL != n->primary_address.session);
2305 n->ack_state = ACK_SEND_ACK;
2306 send_syn_ack_message (&n->primary_address,
2309 case GNUNET_TRANSPORT_PS_DISCONNECT:
2310 /* Get rid of remains and re-try */
2312 n = setup_neighbour (peer);
2313 /* Remember the SYN time stamp for ACK message */
2314 n->ack_state = ACK_SEND_SYN_ACK;
2315 n->connect_ack_timestamp = ts;
2316 /* Request an address for the peer */
2317 set_state_and_timeout (n,
2318 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2319 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2321 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2322 /* should not be possible */
2326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2327 "Unhandled state `%s'\n",
2328 GNUNET_TRANSPORT_ps2s (n->state));
2330 return GNUNET_SYSERR;
2337 * Check if the given @a address is the same that we are already
2338 * using for the respective neighbour. If so, update the bandwidth
2339 * assignment and possibly the session and return #GNUNET_OK.
2340 * If the new address is different from what the neighbour is
2341 * using right now, return #GNUNET_NO.
2343 * @param address address of the other peer,
2344 * @param session session to use or NULL if transport should initiate a session
2345 * @param bandwidth_in inbound quota to be used when connection is up,
2346 * 0 to disconnect from peer
2347 * @param bandwidth_out outbound quota to be used when connection is up,
2348 * 0 to disconnect from peer
2349 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2350 * #GNUNET_NO if more extensive changes are required (address changed)
2353 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2354 struct GNUNET_ATS_Session *session,
2355 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2356 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2358 struct NeighbourMapEntry *n;
2360 n = lookup_neighbour (&address->peer);
2362 (NULL == n->primary_address.address) ||
2363 (0 != GNUNET_HELLO_address_cmp (address,
2364 n->primary_address.address)) )
2366 /* We are not really switching addresses, but merely adjusting
2367 session and/or bandwidth, can do fast ATS update! */
2368 if (session != n->primary_address.session)
2370 /* switch to a different session, but keeping same address; could
2371 happen if there is a 2nd inbound connection */
2372 n->primary_address.session = session;
2373 GNUNET_assert (GNUNET_YES ==
2374 GST_ats_is_known (n->primary_address.address,
2375 n->primary_address.session));
2377 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
2379 n->primary_address.bandwidth_in = bandwidth_in;
2380 set_incoming_quota (n,
2383 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
2385 n->primary_address.bandwidth_out = bandwidth_out;
2386 send_outbound_quota_to_clients (n);
2393 * We've been asked to switch addresses, and just now got the result
2394 * from the blacklist check to see if this is allowed.
2396 * @param cls the `struct BlacklistCheckSwitchContext` with
2397 * the information about the future address
2398 * @param peer the peer we may switch addresses on
2399 * @param address address associated with the request
2400 * @param session session associated with the request
2401 * @param result #GNUNET_OK if the connection is allowed,
2402 * #GNUNET_NO if not,
2403 * #GNUNET_SYSERR if operation was aborted
2406 switch_address_bl_check_cont (void *cls,
2407 const struct GNUNET_PeerIdentity *peer,
2408 const struct GNUNET_HELLO_Address *address,
2409 struct GNUNET_ATS_Session *session,
2412 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2413 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2414 struct NeighbourMapEntry *n;
2416 if (GNUNET_SYSERR == result)
2419 papi = GST_plugins_find (address->transport_name);
2420 GNUNET_assert (NULL != papi);
2422 if (GNUNET_NO == result)
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2426 GST_plugins_a2s (address),
2429 GNUNET_STATISTICS_update (GST_stats,
2430 "# ATS suggestions ignored (blacklist denied)",
2433 if (NULL != session)
2434 papi->disconnect_session (papi->cls,
2437 GNUNET_HELLO_address_check_option (address,
2438 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2439 GST_ats_block_address (address,
2445 if (NULL == session)
2447 /* need to create a session, ATS only gave us an address */
2448 session = papi->get_session (papi->cls,
2450 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2451 "Obtained new session for peer `%s' and address '%s': %p\n",
2452 GNUNET_i2s (&address->peer),
2453 GST_plugins_a2s (address),
2455 if (NULL != session)
2456 GST_ats_new_session (address,
2459 if (NULL == session)
2461 /* session creation failed, bad!, fail! */
2462 GNUNET_STATISTICS_update (GST_stats,
2463 "# ATS suggestions ignored (failed to create session)",
2466 /* No session could be obtained, remove blacklist check and clean up */
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2468 "Failed to obtain new session for peer `%s' and address '%s'\n",
2469 GNUNET_i2s (&address->peer),
2470 GST_plugins_a2s (address));
2471 GST_ats_block_address (address,
2476 /* We did this check already before going into blacklist, but
2477 it is theoretically possible that the situation changed in
2478 the meantime, hence we check again here */
2480 try_run_fast_ats_update (address,
2482 blc_ctx->bandwidth_in,
2483 blc_ctx->bandwidth_out))
2484 goto cleanup; /* was just a minor update, we're done */
2486 /* check if we also need to setup the neighbour entry */
2487 if (NULL == (n = lookup_neighbour (peer)))
2489 n = setup_neighbour (peer);
2490 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2493 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2494 "Peer `%s' switches to address `%s'\n",
2495 GNUNET_i2s (&address->peer),
2496 GST_plugins_a2s (address));
2500 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2502 GST_ats_block_address (address,
2506 case GNUNET_TRANSPORT_PS_INIT_ATS:
2507 /* We requested an address and ATS suggests one:
2508 * set primary address and send SYN message*/
2509 set_primary_address (n,
2512 blc_ctx->bandwidth_in,
2513 blc_ctx->bandwidth_out);
2514 if (ACK_SEND_SYN_ACK == n->ack_state)
2516 /* Send pending SYN_ACK message */
2517 n->ack_state = ACK_SEND_ACK;
2518 send_syn_ack_message (&n->primary_address,
2519 n->connect_ack_timestamp);
2521 set_state_and_timeout (n,
2522 GNUNET_TRANSPORT_PS_SYN_SENT,
2523 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2524 send_syn (&n->primary_address);
2526 case GNUNET_TRANSPORT_PS_SYN_SENT:
2527 /* ATS suggested a new address while waiting for an SYN_ACK:
2528 * Switch and send new SYN */
2529 /* ATS suggests a different address, switch again */
2530 set_primary_address (n,
2533 blc_ctx->bandwidth_in,
2534 blc_ctx->bandwidth_out);
2535 if (ACK_SEND_SYN_ACK == n->ack_state)
2537 /* Send pending SYN_ACK message */
2538 n->ack_state = ACK_SEND_ACK;
2539 send_syn_ack_message (&n->primary_address,
2540 n->connect_ack_timestamp);
2542 set_state_and_timeout (n,
2543 GNUNET_TRANSPORT_PS_SYN_SENT,
2544 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2545 send_syn (&n->primary_address);
2547 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2548 /* We requested an address and ATS suggests one:
2549 * set primary address and send SYN_ACK message*/
2550 set_primary_address (n,
2553 blc_ctx->bandwidth_in,
2554 blc_ctx->bandwidth_out);
2555 /* Send an ACK message as a response to the SYN msg */
2556 set_state_and_timeout (n,
2557 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2558 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2559 send_syn_ack_message (&n->primary_address,
2560 n->connect_ack_timestamp);
2561 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2562 (ACK_UNDEFINED == n->ack_state) )
2563 n->ack_state = ACK_SEND_ACK;
2565 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2566 /* ATS asks us to switch while we were trying to connect; switch to new
2567 address and check blacklist again */
2568 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2570 n->ack_state = ACK_SEND_ACK;
2571 send_syn_ack_message (&n->primary_address,
2572 n->connect_ack_timestamp);
2574 set_primary_address (n,
2577 blc_ctx->bandwidth_in,
2578 blc_ctx->bandwidth_out);
2579 set_state_and_timeout (n,
2580 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2581 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2583 case GNUNET_TRANSPORT_PS_CONNECTED:
2584 GNUNET_assert (NULL != n->primary_address.address);
2585 GNUNET_assert (NULL != n->primary_address.session);
2586 GNUNET_break (n->primary_address.session != session);
2587 /* ATS asks us to switch a life connection; see if we can get
2588 a SYN_ACK on it before we actually do this! */
2589 set_alternative_address (n,
2592 blc_ctx->bandwidth_in,
2593 blc_ctx->bandwidth_out);
2594 set_state_and_timeout (n,
2595 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2596 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2597 GNUNET_STATISTICS_update (GST_stats,
2598 gettext_noop ("# Attempts to switch addresses"),
2601 send_syn (&n->alternative_address);
2603 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2604 set_primary_address (n,
2607 blc_ctx->bandwidth_in,
2608 blc_ctx->bandwidth_out);
2609 if (ACK_SEND_SYN_ACK == n->ack_state)
2611 /* Send pending SYN_ACK message */
2612 n->ack_state = ACK_SEND_ACK;
2613 send_syn_ack_message (&n->primary_address,
2614 n->connect_ack_timestamp);
2616 set_state_and_timeout (n,
2617 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2618 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2619 send_syn (&n->primary_address);
2621 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2622 /* ATS asks us to switch while we were trying to reconnect; switch to new
2623 address and send SYN again */
2624 set_primary_address (n,
2627 blc_ctx->bandwidth_in,
2628 blc_ctx->bandwidth_out);
2629 set_state_and_timeout (n,
2630 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2631 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2632 send_syn (&n->primary_address);
2634 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2635 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2637 (n->primary_address.session == session) )
2639 /* ATS switches back to still-active session */
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 "ATS double-switched, cleaning up alternative address\n");
2642 free_address (&n->alternative_address);
2643 set_state_and_timeout (n,
2644 GNUNET_TRANSPORT_PS_CONNECTED,
2648 /* ATS asks us to switch a life connection, send */
2649 set_alternative_address (n,
2652 blc_ctx->bandwidth_in,
2653 blc_ctx->bandwidth_out);
2654 set_state_and_timeout (n,
2655 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2656 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2657 send_syn (&n->alternative_address);
2659 case GNUNET_TRANSPORT_PS_DISCONNECT:
2660 /* not going to switch addresses while disconnecting */
2661 GNUNET_STATISTICS_update (GST_stats,
2662 "# ATS suggestion ignored (disconnecting)",
2666 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2670 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2671 "Unhandled state `%s'\n",
2672 GNUNET_TRANSPORT_ps2s (n->state));
2677 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2680 GNUNET_free (blc_ctx);
2685 * For the given peer, switch to this address.
2687 * Before accepting this addresses and actively using it, a blacklist check
2690 * If any check fails or the suggestion can somehow not be followed, we
2691 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2692 * could not be satisfied and force ATS to do something else.
2694 * @param address address of the other peer,
2695 * @param session session to use or NULL if transport should initiate a session
2696 * @param bandwidth_in inbound quota to be used when connection is up,
2697 * 0 to disconnect from peer
2698 * @param bandwidth_out outbound quota to be used when connection is up,
2699 * 0 to disconnect from peer
2702 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2703 struct GNUNET_ATS_Session *session,
2704 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2705 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2707 struct GST_BlacklistCheck *blc;
2708 struct BlacklistCheckSwitchContext *blc_ctx;
2710 GNUNET_assert (NULL != address->transport_name);
2712 try_run_fast_ats_update (address,
2718 /* Check if plugin is available */
2719 if (NULL == (GST_plugins_find (address->transport_name)))
2721 /* we don't have the plugin for this address */
2723 GST_ats_block_address (address,
2727 if ((NULL == session) &&
2728 (GNUNET_HELLO_address_check_option (address,
2729 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2731 /* This is a inbound address and we do not have a session to use! */
2733 GST_ats_block_address (address,
2738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2739 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2740 GST_plugins_a2s (address),
2741 GNUNET_i2s (&address->peer),
2742 (unsigned int) ntohl (bandwidth_in.value__),
2743 (unsigned int) ntohl (bandwidth_out.value__));
2745 /* Perform blacklist check */
2746 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2747 blc_ctx->bandwidth_in = bandwidth_in;
2748 blc_ctx->bandwidth_out = bandwidth_out;
2749 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2752 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2753 address->transport_name,
2754 &switch_address_bl_check_cont,
2765 * Function called to send network utilization data to ATS for
2766 * each active connection.
2769 * @param key peer we send utilization data for
2770 * @param value the `struct NeighbourMapEntry *` with data to send
2771 * @return #GNUNET_OK (continue to iterate)
2774 send_utilization_data (void *cls,
2775 const struct GNUNET_PeerIdentity *key,
2778 struct NeighbourMapEntry *n = value;
2781 struct GNUNET_TIME_Relative delta;
2783 if ( (GNUNET_YES != test_connected (n)) ||
2784 (NULL == n->primary_address.address) )
2786 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2787 GNUNET_TIME_absolute_get ());
2789 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2790 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2792 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2793 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2796 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2800 GST_ats_update_utilization (n->primary_address.address,
2803 n->util_total_bytes_recv = 0;
2804 n->util_total_bytes_sent = 0;
2805 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2811 * Task transmitting utilization in a regular interval
2813 * @param cls the 'struct NeighbourMapEntry' for which we are running
2816 utilization_transmission (void *cls)
2818 util_transmission_tk = NULL;
2819 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
2820 &send_utilization_data,
2822 util_transmission_tk
2823 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2824 &utilization_transmission,
2830 * Track information about data we received from the
2831 * given address (used to notify ATS about our utilization
2832 * of allocated resources).
2834 * @param address the address we got data from
2835 * @param message the message we received (really only the size is used)
2838 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2839 const struct GNUNET_MessageHeader *message)
2841 struct NeighbourMapEntry *n;
2843 n = lookup_neighbour (&address->peer);
2846 n->util_total_bytes_recv += ntohs (message->size);
2851 * Track information about data we transmitted using the given @a
2852 * address and @a session (used to notify ATS about our utilization of
2853 * allocated resources).
2855 * @param address the address we transmitted data to
2856 * @param session session we used to transmit data
2857 * @param message the message we sent (really only the size is used)
2860 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2861 struct GNUNET_ATS_Session *session,
2864 struct NeighbourMapEntry *n;
2866 n = lookup_neighbour (&address->peer);
2869 if (n->primary_address.session != session)
2871 n->util_total_bytes_sent += size;
2876 * Master task run for every neighbour. Performs all of the time-related
2877 * activities (keep alive, send next message, disconnect if idle, finish
2878 * clean up after disconnect).
2880 * @param cls the 'struct NeighbourMapEntry' for which we are running
2883 master_task (void *cls)
2885 struct NeighbourMapEntry *n = cls;
2886 struct GNUNET_TIME_Relative delay;
2889 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2890 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2891 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2892 GNUNET_i2s (&n->id),
2893 GNUNET_TRANSPORT_ps2s(n->state),
2894 GNUNET_STRINGS_relative_time_to_string (delay,
2898 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2899 /* invalid state for master task, clean up */
2903 case GNUNET_TRANSPORT_PS_INIT_ATS:
2904 if (0 == delay.rel_value_us)
2906 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2907 "Connection to `%s' timed out waiting for ATS to provide address\n",
2908 GNUNET_i2s (&n->id));
2913 case GNUNET_TRANSPORT_PS_SYN_SENT:
2914 if (0 == delay.rel_value_us)
2916 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2917 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2918 GNUNET_i2s (&n->id));
2919 /* Remove address and request and additional one */
2920 unset_primary_address (n);
2921 set_state_and_timeout (n,
2922 GNUNET_TRANSPORT_PS_INIT_ATS,
2923 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2927 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2928 if (0 == delay.rel_value_us)
2930 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2931 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2932 GNUNET_i2s (&n->id));
2937 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2938 if (0 == delay.rel_value_us)
2940 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2941 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2942 GNUNET_i2s (&n->id));
2943 disconnect_neighbour (n);
2947 case GNUNET_TRANSPORT_PS_CONNECTED:
2948 if (0 == delay.rel_value_us)
2950 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2951 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2952 GNUNET_i2s (&n->id));
2953 disconnect_neighbour (n);
2956 try_transmission_to_peer (n);
2959 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2960 if (0 == delay.rel_value_us)
2962 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2963 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2964 GNUNET_i2s (&n->id));
2965 disconnect_neighbour (n);
2969 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2970 if (0 == delay.rel_value_us)
2972 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2973 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
2974 GNUNET_i2s (&n->id));
2975 disconnect_neighbour (n);
2979 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2980 if (0 == delay.rel_value_us)
2982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2983 "Switch failed, cleaning up alternative address\n");
2984 free_address (&n->alternative_address);
2985 set_state_and_timeout (n,
2986 GNUNET_TRANSPORT_PS_CONNECTED,
2987 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2989 try_transmission_to_peer (n);
2992 case GNUNET_TRANSPORT_PS_DISCONNECT:
2993 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2994 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2995 GNUNET_i2s (&n->id));
2998 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2999 /* how did we get here!? */
3003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3004 "Unhandled state `%s'\n",
3005 GNUNET_TRANSPORT_ps2s (n->state));
3009 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3010 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3011 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3013 /* if we are *now* in one of the two states, we're sending
3014 keep alive messages, so we need to consider the keepalive
3015 delay, not just the connection timeout */
3016 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3019 if (NULL == n->task)
3020 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3027 * Send a ACK message to the neighbour to confirm that we
3030 * @param n neighbour to send the ACK to
3033 send_session_ack_message (struct NeighbourMapEntry *n)
3035 struct GNUNET_MessageHeader msg;
3037 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3038 "Sending ACK message to peer `%s'\n",
3039 GNUNET_i2s (&n->id));
3041 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3042 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3043 (void) send_with_session (n,
3045 sizeof (struct GNUNET_MessageHeader),
3047 GNUNET_TIME_UNIT_FOREVER_REL,
3054 * We received a 'SESSION_SYN_ACK' message from the other peer.
3055 * Consider switching to it.
3057 * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
3058 * @param peer identity of the peer to switch the address for
3059 * @param address address of the other peer, NULL if other peer
3061 * @param session session to use (or NULL)
3062 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3065 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3066 const struct GNUNET_HELLO_Address *address,
3067 struct GNUNET_ATS_Session *session)
3069 const struct TransportSynMessage *scm;
3070 struct GNUNET_TIME_Absolute ts;
3071 struct NeighbourMapEntry *n;
3073 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3075 GNUNET_break_op (0);
3076 return GNUNET_SYSERR;
3078 GNUNET_STATISTICS_update (GST_stats,
3080 ("# SYN_ACK messages received"),
3082 scm = (const struct TransportSynMessage *) message;
3083 GNUNET_break_op (ntohl (scm->reserved) == 0);
3084 if (NULL == (n = lookup_neighbour (&address->peer)))
3086 GNUNET_STATISTICS_update (GST_stats,
3088 ("# unexpected SYN_ACK messages (no peer)"),
3090 return GNUNET_SYSERR;
3092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3093 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3094 GNUNET_i2s (&address->peer),
3095 GNUNET_TRANSPORT_ps2s (n->state),
3096 print_ack_state (n->ack_state));
3097 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3100 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3103 return GNUNET_SYSERR;
3104 case GNUNET_TRANSPORT_PS_INIT_ATS:
3105 GNUNET_STATISTICS_update (GST_stats,
3106 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3110 case GNUNET_TRANSPORT_PS_SYN_SENT:
3111 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3113 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3114 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3117 set_state_and_timeout (n,
3118 GNUNET_TRANSPORT_PS_CONNECTED,
3119 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3120 set_primary_address (n,
3121 n->primary_address.address,
3122 n->primary_address.session,
3123 n->primary_address.bandwidth_in,
3124 n->primary_address.bandwidth_out);
3125 send_session_ack_message (n);
3127 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3128 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3129 GNUNET_STATISTICS_update (GST_stats,
3130 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3134 case GNUNET_TRANSPORT_PS_CONNECTED:
3135 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3136 send_session_ack_message (n);
3138 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3139 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3140 to give us a new address... */
3141 GNUNET_STATISTICS_update (GST_stats,
3142 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3146 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3147 /* Reconnecting with new address address worked; go back to connected! */
3148 set_state_and_timeout (n,
3149 GNUNET_TRANSPORT_PS_CONNECTED,
3150 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3151 send_session_ack_message (n);
3153 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3154 /* new address worked; adopt it and go back to connected! */
3155 set_state_and_timeout (n,
3156 GNUNET_TRANSPORT_PS_CONNECTED,
3157 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3158 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3160 /* Set primary addresses */
3161 set_primary_address (n,
3162 n->alternative_address.address,
3163 n->alternative_address.session,
3164 n->alternative_address.bandwidth_in,
3165 n->alternative_address.bandwidth_out);
3166 GNUNET_STATISTICS_update (GST_stats,
3167 gettext_noop ("# Successful attempts to switch addresses"),
3171 GNUNET_HELLO_address_free (n->alternative_address.address);
3172 memset (&n->alternative_address,
3174 sizeof (n->alternative_address));
3175 send_session_ack_message (n);
3177 case GNUNET_TRANSPORT_PS_DISCONNECT:
3178 GNUNET_STATISTICS_update (GST_stats,
3180 ("# unexpected SYN_ACK messages (disconnecting)"),
3182 return GNUNET_SYSERR;
3183 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3188 "Unhandled state `%s'\n",
3189 GNUNET_TRANSPORT_ps2s (n->state));
3191 return GNUNET_SYSERR;
3198 * A session was terminated. Take note; if needed, try to get
3199 * an alternative address from ATS.
3201 * @param peer identity of the peer where the session died
3202 * @param session session that is gone
3203 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3204 * this session was not in use
3207 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3208 struct GNUNET_ATS_Session *session)
3210 struct NeighbourMapEntry *n;
3212 if (NULL == (n = lookup_neighbour (peer)))
3213 return GNUNET_NO; /* can't affect us */
3214 if (session != n->primary_address.session)
3216 /* Free alternative address */
3217 if (session == n->alternative_address.session)
3219 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3220 set_state_and_timeout (n,
3221 GNUNET_TRANSPORT_PS_CONNECTED,
3223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3224 "Session died, cleaning up alternative address\n");
3225 free_address (&n->alternative_address);
3227 return GNUNET_NO; /* doesn't affect us further */
3230 n->expect_latency_response = GNUNET_NO;
3231 /* The session for neighbour's primary address died */
3234 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3238 case GNUNET_TRANSPORT_PS_INIT_ATS:
3242 case GNUNET_TRANSPORT_PS_SYN_SENT:
3243 /* The session used to send the SYN terminated:
3244 * this implies a connect error*/
3245 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3246 "Failed to send SYN in CONNECT_SENT with `%s' %p: session terminated\n",
3247 GST_plugins_a2s (n->primary_address.address),
3248 n->primary_address.session);
3250 /* Destroy the address since it cannot be used */
3251 unset_primary_address (n);
3252 set_state_and_timeout (n,
3253 GNUNET_TRANSPORT_PS_INIT_ATS,
3254 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3256 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3257 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3258 /* error on inbound session; free neighbour entirely */
3261 case GNUNET_TRANSPORT_PS_CONNECTED:
3262 /* Our primary connection died, try a fast reconnect */
3263 unset_primary_address (n);
3264 set_state_and_timeout (n,
3265 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3266 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3268 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3269 /* we don't have an address, how can it go down? */
3272 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3273 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3274 "Failed to send SYN in RECONNECT_SENT with `%s' %p: session terminated\n",
3275 GST_plugins_a2s (n->primary_address.address),
3276 n->primary_address.session);
3277 /* Destroy the address since it cannot be used */
3278 unset_primary_address (n);
3279 set_state_and_timeout (n,
3280 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3281 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3283 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3284 /* primary went down while we were waiting for SYN_ACK on secondary;
3285 secondary as primary */
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 "Connection `%s' %p to peer `%s' was terminated while switching, "
3289 "switching to alternative address `%s' %p\n",
3290 GST_plugins_a2s (n->primary_address.address),
3291 n->primary_address.session,
3293 GST_plugins_a2s (n->alternative_address.address),
3294 n->alternative_address.session);
3296 /* Destroy the inbound address since it cannot be used */
3297 free_address (&n->primary_address);
3298 n->primary_address = n->alternative_address;
3299 GNUNET_assert (GNUNET_YES ==
3300 GST_ats_is_known (n->primary_address.address,
3301 n->primary_address.session));
3302 memset (&n->alternative_address,
3304 sizeof (struct NeighbourAddress));
3305 set_state_and_timeout (n,
3306 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3307 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3309 case GNUNET_TRANSPORT_PS_DISCONNECT:
3310 unset_primary_address (n);
3312 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3313 /* neighbour was freed and plugins told to terminate session */
3316 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3317 "Unhandled state `%s'\n",
3318 GNUNET_TRANSPORT_ps2s (n->state));
3322 if (NULL != n->task)
3323 GNUNET_SCHEDULER_cancel (n->task);
3324 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3330 * We received a 'ACK' message from the other peer.
3331 * If we sent a 'SYN_ACK' last, this means we are now
3332 * connected. Otherwise, do nothing.
3334 * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
3335 * @param address address of the other peer
3336 * @param session session to use (or NULL)
3337 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3340 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3341 const struct GNUNET_HELLO_Address *address,
3342 struct GNUNET_ATS_Session *session)
3344 struct NeighbourMapEntry *n;
3346 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3348 GNUNET_break_op (0);
3349 return GNUNET_SYSERR;
3351 GNUNET_STATISTICS_update (GST_stats,
3352 gettext_noop ("# ACK messages received"),
3355 if (NULL == (n = lookup_neighbour (&address->peer)))
3357 GNUNET_break_op (0);
3358 return GNUNET_SYSERR;
3360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3361 "Received ACK for peer `%s' in state %s/%s\n",
3362 GNUNET_i2s (&address->peer),
3363 GNUNET_TRANSPORT_ps2s (n->state),
3364 print_ack_state (n->ack_state));
3366 /* Check if we are in a plausible state for having sent
3367 a SYN_ACK. If not, return, otherwise break.
3369 The remote peers sends a ACK as a response for a SYN_ACK
3373 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3374 now wait for the ACK to finally be connected
3375 - If we sent a SYN_ACK to this peer before */
3377 if ( ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3378 (ACK_SEND_ACK != n->ack_state) ) ||
3379 (NULL == n->primary_address.address) )
3381 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3382 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3383 GNUNET_i2s (&address->peer),
3384 GNUNET_TRANSPORT_ps2s (n->state),
3385 print_ack_state (n->ack_state));
3387 GNUNET_STATISTICS_update (GST_stats,
3388 gettext_noop ("# unexpected ACK messages"),
3393 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3395 /* We tried to switch addresses while being connect. We explicitly wait
3396 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3397 * so we do not want to set the address as in use! */
3400 set_state_and_timeout (n,
3401 GNUNET_TRANSPORT_PS_CONNECTED,
3402 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3404 if (NULL == n->primary_address.address) {
3406 * We are in state = PSY_SYN_RECV_ACK or ack_state = ACK_SEND_ACK, which
3407 * really means we did try (and succeed) to send a SYN and are waiting for
3409 * That suggests that the primary_address used to be non-NULL, but maybe it
3410 * got reset to NULL without the state being changed appropriately?
3416 /* Reset backoff for primary address */
3417 GST_ats_block_reset (n->primary_address.address,
3418 n->primary_address.session);
3424 * Test if we're connected to the given peer.
3426 * @param target peer to test
3427 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3430 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3432 return test_connected (lookup_neighbour (target));
3437 * Task to asynchronously run #free_neighbour().
3439 * @param cls the `struct NeighbourMapEntry` to free
3442 delayed_disconnect (void *cls)
3444 struct NeighbourMapEntry *n = cls;
3446 n->delayed_disconnect_task = NULL;
3447 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3448 "Disconnecting by request from peer %s\n",
3449 GNUNET_i2s (&n->id));
3455 * We received a quota message from the given peer,
3456 * validate and process.
3458 * @param peer sender of the message
3459 * @param msg the quota message
3462 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
3463 const struct GNUNET_MessageHeader *msg)
3465 struct NeighbourMapEntry *n;
3466 const struct GNUNET_ATS_SessionQuotaMessage *sqm;
3467 struct GNUNET_BANDWIDTH_Value32NBO last;
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3470 "Received QUOTA message from peer `%s'\n",
3472 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionQuotaMessage))
3474 GNUNET_break_op (0);
3475 GNUNET_STATISTICS_update (GST_stats,
3476 gettext_noop ("# quota messages ignored (malformed)"),
3481 GNUNET_STATISTICS_update (GST_stats,
3483 ("# QUOTA messages received"),
3485 sqm = (const struct GNUNET_ATS_SessionQuotaMessage *) msg;
3486 if (NULL == (n = lookup_neighbour (peer)))
3491 last = GNUNET_BANDWIDTH_value_max (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3492 GNUNET_BANDWIDTH_value_init (ntohl (sqm->quota)));
3493 if (last.value__ != n->neighbour_receive_quota.value__)
3495 n->neighbour_receive_quota = last;
3496 send_outbound_quota_to_clients (n);
3502 * We received a disconnect message from the given peer,
3503 * validate and process.
3505 * @param peer sender of the message
3506 * @param msg the disconnect message
3509 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3510 const struct GNUNET_MessageHeader *msg)
3512 struct NeighbourMapEntry *n;
3513 const struct GNUNET_ATS_SessionDisconnectMessage *sdm;
3515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3516 "Received DISCONNECT message from peer `%s'\n",
3518 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionDisconnectMessage))
3520 GNUNET_break_op (0);
3521 GNUNET_STATISTICS_update (GST_stats,
3523 ("# disconnect messages ignored (malformed)"),
3528 GNUNET_STATISTICS_update (GST_stats,
3530 ("# DISCONNECT messages received"),
3532 sdm = (const struct GNUNET_ATS_SessionDisconnectMessage *) msg;
3533 if (NULL == (n = lookup_neighbour (peer)))
3538 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3540 GNUNET_STATISTICS_update (GST_stats,
3541 gettext_noop ("# disconnect messages ignored (timestamp)"),
3546 if (0 != memcmp (peer,
3548 sizeof (struct GNUNET_PeerIdentity)))
3550 GNUNET_break_op (0);
3553 if (ntohl (sdm->purpose.size) !=
3554 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3555 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3556 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3559 "DISCONNECT message from peer `%s' has invalid size\n",
3561 GNUNET_break_op (0);
3565 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3571 "DISCONNECT message from peer `%s' cannot be verified \n",
3573 GNUNET_break_op (0);
3576 if (NULL == n->delayed_disconnect_task)
3578 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect,
3585 * Closure for the #neighbours_iterate() function.
3587 struct IteratorContext
3590 * Function to call on each connected neighbour.
3592 GST_NeighbourIterator cb;
3595 * Closure for @e cb.
3602 * Call the callback from the closure for each neighbour.
3604 * @param cls the `struct IteratorContext`
3605 * @param key the hash of the public key of the neighbour
3606 * @param value the `struct NeighbourMapEntry`
3607 * @return #GNUNET_OK (continue to iterate)
3610 neighbours_iterate (void *cls,
3611 const struct GNUNET_PeerIdentity *key,
3614 struct IteratorContext *ic = cls;
3615 struct NeighbourMapEntry *n = value;
3616 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3617 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3619 if (NULL != n->primary_address.address)
3621 bandwidth_in = n->primary_address.bandwidth_in;
3622 bandwidth_out = n->primary_address.bandwidth_out;
3626 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3627 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3631 n->primary_address.address,
3634 bandwidth_in, bandwidth_out);
3640 * Iterate over all connected neighbours.
3642 * @param cb function to call
3643 * @param cb_cls closure for @a cb
3646 GST_neighbours_iterate (GST_NeighbourIterator cb,
3649 struct IteratorContext ic;
3651 if (NULL == neighbours)
3652 return; /* can happen during shutdown */
3655 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3656 &neighbours_iterate,
3662 * If we have an active connection to the given target, it must be shutdown.
3664 * @param target peer to disconnect from
3667 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3669 struct NeighbourMapEntry *n;
3671 if (NULL == (n = lookup_neighbour (target)))
3672 return; /* not active */
3673 if (GNUNET_YES == test_connected (n))
3674 GNUNET_STATISTICS_update (GST_stats,
3675 gettext_noop ("# disconnected from peer upon explicit request"),
3678 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3679 "Forced disconnect from peer %s\n",
3680 GNUNET_i2s (target));
3681 disconnect_neighbour (n);
3686 * Obtain current address information for the given neighbour.
3689 * @return address currently used
3691 const struct GNUNET_HELLO_Address *
3692 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3694 struct NeighbourMapEntry *n;
3696 n = lookup_neighbour (peer);
3699 return n->primary_address.address;
3704 * Initialize the neighbours subsystem.
3706 * @param max_fds maximum number of fds to use
3709 GST_neighbours_start (unsigned int max_fds)
3711 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3713 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3714 &utilization_transmission,
3720 * Disconnect from the given neighbour.
3723 * @param key hash of neighbour's public key (not used)
3724 * @param value the `struct NeighbourMapEntry` of the neighbour
3725 * @return #GNUNET_OK (continue to iterate)
3728 disconnect_all_neighbours (void *cls,
3729 const struct GNUNET_PeerIdentity *key,
3732 struct NeighbourMapEntry *n = value;
3734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3735 "Disconnecting peer `%4s' during shutdown\n",
3736 GNUNET_i2s (&n->id));
3743 * Cleanup the neighbours subsystem.
3746 GST_neighbours_stop ()
3748 if (NULL == neighbours)
3750 if (NULL != util_transmission_tk)
3752 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3753 util_transmission_tk = NULL;
3755 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3756 &disconnect_all_neighbours,
3758 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3763 /* end of file gnunet-service-transport_neighbours.c */