2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 GNUnet e.V.
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @file transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_ats.h"
29 #include "gnunet-service-transport_neighbours.h"
30 #include "gnunet-service-transport_manipulation.h"
31 #include "gnunet-service-transport_plugins.h"
32 #include "gnunet-service-transport_validation.h"
33 #include "gnunet-service-transport.h"
34 #include "gnunet_peerinfo_service.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
39 * Experimental option to ignore SessionQuotaMessages from
42 #define IGNORE_INBOUND_QUOTA GNUNET_YES
45 * Size of the neighbour hash map.
47 #define NEIGHBOUR_TABLE_SIZE 256
50 * Time we give plugin to transmit DISCONNECT message before the
51 * neighbour entry self-destructs.
53 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
56 * How often must a peer violate bandwidth quotas before we start
57 * to simply drop its messages?
59 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
62 * How long are we willing to wait for a response from ATS before timing out?
64 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
67 * How long are we willing to wait for an ACK from the other peer before
68 * giving up on our connect operation?
70 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
73 * How long are we willing to wait for a successful reconnect if
74 * an existing connection went down? Much shorter than the
75 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
76 * higher layers about the disconnect during this period.
78 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
81 * Interval to send utilization data
83 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_UNIT_SECONDS
86 * State describing which kind a reply this neighbour should send
91 * We did not receive a SYN message for this neighbour
96 * The neighbour received a SYN message and has to send a SYN_ACK
102 * The neighbour sent a SYN_ACK message and has to send a ACK
109 GNUNET_NETWORK_STRUCT_BEGIN
112 * Message a peer sends to another to indicate that it intends to
113 * setup a connection/session for data exchange. A 'SESSION_SYN'
114 * should be answered with a 'SESSION_SYN_ACK' with the same body
115 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
116 * a 'ACK'. Once the 'ACK' is received, both peers
117 * should be connected.
119 struct TransportSynMessage
122 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
123 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
125 struct GNUNET_MessageHeader header;
130 uint32_t reserved GNUNET_PACKED;
133 * Absolute time at the sender. Only the most recent connect
134 * message implies which session is preferred by the sender.
136 struct GNUNET_TIME_AbsoluteNBO timestamp;
142 * Message a peer sends to another when connected to indicate that a
143 * session is in use and the peer is still alive or to respond to a keep alive.
144 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
145 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
146 * When the keep alive response with type is received, transport service
147 * will call the respective plugin to update the session timeout
149 struct GNUNET_ATS_SessionKeepAliveMessage
152 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
153 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
155 struct GNUNET_MessageHeader header;
158 * A nonce to identify the session the keep alive is used for
160 uint32_t nonce GNUNET_PACKED;
165 * Message a peer sends to another when connected to indicate that
166 * the other peer should limit transmissions to the indicated
169 struct GNUNET_ATS_SessionQuotaMessage
172 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA.
174 struct GNUNET_MessageHeader header;
177 * Quota to use (for sending), in bytes per second.
179 uint32_t quota GNUNET_PACKED;
184 * Message we send to the other peer to notify him that we intentionally
185 * are disconnecting (to reduce timeouts). This is just a friendly
186 * notification, peers must not rely on always receiving disconnect
189 struct GNUNET_ATS_SessionDisconnectMessage
192 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
194 struct GNUNET_MessageHeader header;
199 uint32_t reserved GNUNET_PACKED;
202 * Purpose of the signature. Extends over the timestamp.
203 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
205 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
208 * Absolute time at the sender. Only the most recent connect
209 * message implies which session is preferred by the sender.
211 struct GNUNET_TIME_AbsoluteNBO timestamp;
214 * Public key of the sender.
216 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
219 * Signature of the peer that sends us the disconnect. Only
220 * valid if the timestamp is AFTER the timestamp from the
221 * corresponding 'SYN' message.
223 struct GNUNET_CRYPTO_EddsaSignature signature;
227 GNUNET_NETWORK_STRUCT_END
231 * For each neighbour we keep a list of messages
232 * that we still want to transmit to the neighbour.
238 * This is a doubly linked list.
240 struct MessageQueue *next;
243 * This is a doubly linked list.
245 struct MessageQueue *prev;
248 * Function to call once we're done.
250 GST_NeighbourSendContinuation cont;
253 * Closure for @e cont
258 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
259 * stuck together in memory. Allocated at the end of this struct.
261 const char *message_buf;
264 * Size of the message buf
266 size_t message_buf_size;
269 * At what time should we fail?
271 struct GNUNET_TIME_Absolute timeout;
277 * A possible address we could use to communicate with a neighbour.
279 struct NeighbourAddress
283 * Active session for this address.
285 struct GNUNET_ATS_Session *session;
288 * Network-level address information.
290 struct GNUNET_HELLO_Address *address;
293 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
294 * peer for this address. Use to check that the ACK is in response
295 * to our most recent 'SYN'.
297 struct GNUNET_TIME_Absolute connect_timestamp;
300 * Inbound bandwidth from ATS for this address.
302 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
305 * Outbound bandwidth from ATS for this address.
307 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
310 * Did we tell ATS that this is our 'active' address?
315 * The current nonce sent in the last keep alive messages
317 uint32_t keep_alive_nonce;
322 * Entry in neighbours.
324 struct NeighbourMapEntry
328 * Head of list of messages we would like to send to this peer;
329 * must contain at most one message per client.
331 struct MessageQueue *messages_head;
334 * Tail of list of messages we would like to send to this peer; must
335 * contain at most one message per client.
337 struct MessageQueue *messages_tail;
340 * Are we currently trying to send a message? If so, which one?
342 struct MessageQueue *is_active;
345 * Primary address we currently use to communicate with the neighbour.
347 struct NeighbourAddress primary_address;
350 * Alternative address currently under consideration for communicating
351 * with the neighbour.
353 struct NeighbourAddress alternative_address;
356 * Identity of this neighbour.
358 struct GNUNET_PeerIdentity id;
361 * Main task that drives this peer (timeouts, keepalives, etc.).
362 * Always runs the #master_task().
364 struct GNUNET_SCHEDULER_Task *task;
367 * Task to disconnect neighbour after we received a DISCONNECT message
369 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
372 * At what time should we sent the next keep-alive message?
374 struct GNUNET_TIME_Absolute keep_alive_time;
377 * At what time did we sent the last keep-alive message? Used
378 * to calculate round-trip time ("latency").
380 struct GNUNET_TIME_Absolute last_keep_alive_time;
383 * Timestamp we should include in our next SYN_ACK message.
384 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
385 * our SYN_ACK message.
387 struct GNUNET_TIME_Absolute connect_ack_timestamp;
390 * ATS address suggest handle
392 struct GNUNET_ATS_ConnectivitySuggestHandle *suggest_handle;
395 * Time where we should cut the connection (timeout) if we don't
396 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
397 * if we are in #GNUNET_TRANSPORT_PS_CONNECTED).
399 struct GNUNET_TIME_Absolute timeout;
402 * Tracker for inbound bandwidth.
404 struct GNUNET_BANDWIDTH_Tracker in_tracker;
407 * How often has the other peer (recently) violated the inbound
408 * traffic limit? Incremented by 10 per violation, decremented by 1
409 * per non-violation (for each time interval).
411 unsigned int quota_violation_count;
414 * Latest quota the other peer send us in bytes per second.
415 * We should not send more, least the other peer throttle
416 * receiving our traffic.
418 struct GNUNET_BANDWIDTH_Value32NBO neighbour_receive_quota;
421 * The current state of the peer.
423 enum GNUNET_TRANSPORT_PeerState state;
426 * Did we sent an KEEP_ALIVE message and are we expecting a response?
428 int expect_latency_response;
431 * When a peer wants to connect we have to reply to the 1st SYN message
432 * with a SYN_ACK message. But sometime we cannot send this message
433 * immediately since we do not have an address and then we have to remember
434 * to send this message as soon as we have an address.
436 * Flag to set if we still need to send a SYN_ACK message to the other peer
437 * (once we have an address to use and the peer has been allowed by our
438 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
439 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
440 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
441 * 'ACK' (regardless of what our own state machine might say).
443 enum GST_ACK_State ack_state;
446 * Tracking utilization of outbound bandwidth
448 uint32_t util_total_bytes_sent;
451 * Tracking utilization of inbound bandwidth
453 uint32_t util_total_bytes_recv;
456 * Date of last utilization transmission
458 struct GNUNET_TIME_Absolute last_util_transmission;
463 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
465 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
468 * List of pending blacklist checks: head
470 static struct BlacklistCheckSwitchContext *pending_bc_head;
473 * List of pending blacklist checks: tail
475 static struct BlacklistCheckSwitchContext *pending_bc_tail;
478 * counter for connected neighbours
480 static unsigned int neighbours_connected;
483 * Number of bytes we have currently queued for transmission.
485 static unsigned long long bytes_in_send_queue;
488 * Task transmitting utilization data
490 static struct GNUNET_SCHEDULER_Task *util_transmission_tk;
494 * Convert the given ACK state to a string.
497 * @return corresponding human-readable string
500 print_ack_state (enum GST_ACK_State s)
505 case ACK_SEND_SYN_ACK:
506 return "SEND_SYN_ACK";
517 * Send information about a new outbound quota to our clients.
518 * Note that the outbound quota is enforced client-side (i.e.
519 * in libgnunettransport).
521 * @param n affected peer
524 send_outbound_quota_to_clients (struct NeighbourMapEntry *n)
526 struct QuotaSetMessage q_msg;
527 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
529 if (! GNUNET_TRANSPORT_is_connected (n->state))
531 #if IGNORE_INBOUND_QUOTA
532 bandwidth_min = n->primary_address.bandwidth_out;
534 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
535 n->neighbour_receive_quota);
538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
539 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
540 ntohl (bandwidth_min.value__),
541 GNUNET_i2s (&n->id));
542 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
543 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
544 q_msg.quota = bandwidth_min;
546 GST_clients_broadcast (&q_msg.header,
552 * Notify our clients that another peer connected to us.
554 * @param n the peer that connected
557 neighbours_connect_notification (struct NeighbourMapEntry *n)
559 size_t len = sizeof(struct ConnectInfoMessage);
560 char buf[len] GNUNET_ALIGN;
561 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
562 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
564 #if IGNORE_INBOUND_QUOTA
565 bandwidth_min = n->primary_address.bandwidth_out;
567 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
568 n->neighbour_receive_quota);
570 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
571 "We are now connected to peer `%s'\n",
572 GNUNET_i2s (&n->id));
573 connect_msg->header.size = htons (sizeof(buf));
574 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
575 connect_msg->id = n->id;
576 connect_msg->quota_in = n->primary_address.bandwidth_in;
577 connect_msg->quota_out = bandwidth_min;
578 GST_clients_broadcast (&connect_msg->header,
584 * Notify our clients (and manipulation) that a peer disconnected from
587 * @param n the peer that disconnected
590 neighbours_disconnect_notification (struct NeighbourMapEntry *n)
592 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
593 "Peer `%s' disconnected\n",
594 GNUNET_i2s (&n->id));
595 GST_manipulation_peer_disconnect (&n->id);
596 GST_clients_broadcast_disconnect (&n->id);
601 * Notify transport clients that a neighbour peer changed its active
604 * @param peer identity of the peer
605 * @param address address possibly NULL if peer is not connected
606 * @param state current state this peer is in
607 * @param state_timeout timeout for the current state of the peer
608 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
609 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
612 neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer,
613 const struct GNUNET_HELLO_Address *address,
614 enum GNUNET_TRANSPORT_PeerState state,
615 struct GNUNET_TIME_Absolute state_timeout,
616 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
617 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
620 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
622 GST_plugins_a2s (address),
623 GNUNET_TRANSPORT_ps2s (state),
624 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
625 /* FIXME: include bandwidth in notification! */
626 GST_clients_broadcast_peer_notification (peer,
634 * Lookup a neighbour entry in the neighbours hash map.
636 * @param pid identity of the peer to look up
637 * @return the entry, NULL if there is no existing record
639 static struct NeighbourMapEntry *
640 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
642 if (NULL == neighbours)
644 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
649 * Test if we're connected to the given peer.
651 * @param n neighbour entry of peer to test
652 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
655 test_connected (struct NeighbourMapEntry *n)
659 return GNUNET_TRANSPORT_is_connected (n->state);
664 * We don't need a given neighbour address any more.
665 * Release its resources and give appropriate notifications
666 * to ATS and other subsystems.
668 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
671 free_address (struct NeighbourAddress *na)
673 if (GNUNET_YES == na->ats_active)
674 GST_validation_set_address_use (na->address,
676 if (NULL != na->address)
678 GST_ats_block_address (na->address,
680 GNUNET_HELLO_address_free (na->address);
683 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
684 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
685 na->ats_active = GNUNET_NO;
686 na->keep_alive_nonce = 0;
692 * Master task run for every neighbour. Performs all of the time-related
693 * activities (keep alive, send next message, disconnect if idle, finish
694 * clean up after disconnect).
696 * @param cls the `struct NeighbourMapEntry` for which we are running
699 master_task (void *cls);
703 * Set net state and state timeout for this neighbour and notify monitoring
705 * @param n the respective neighbour
706 * @param s the new state
707 * @param timeout the new timeout
710 set_state_and_timeout (struct NeighbourMapEntry *n,
711 enum GNUNET_TRANSPORT_PeerState s,
712 struct GNUNET_TIME_Absolute timeout)
714 if (GNUNET_TRANSPORT_is_connected (s) &&
715 ! GNUNET_TRANSPORT_is_connected (n->state) )
717 neighbours_connect_notification (n);
718 GNUNET_STATISTICS_set (GST_stats,
719 gettext_noop ("# peers connected"),
720 ++neighbours_connected,
723 if (! GNUNET_TRANSPORT_is_connected (s) &&
724 GNUNET_TRANSPORT_is_connected (n->state) )
726 GNUNET_STATISTICS_set (GST_stats,
727 gettext_noop ("# peers connected"),
728 --neighbours_connected,
730 neighbours_disconnect_notification (n);
733 if ( (timeout.abs_value_us < n->timeout.abs_value_us) &&
736 /* new timeout is earlier, reschedule master task */
737 GNUNET_SCHEDULER_cancel (n->task);
738 n->task = GNUNET_SCHEDULER_add_at (timeout,
742 n->timeout = timeout;
743 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
744 "Neighbour `%s' changed state to %s with timeout %s\n",
746 GNUNET_TRANSPORT_ps2s(s),
747 GNUNET_STRINGS_absolute_time_to_string (timeout));
748 neighbours_changed_notification (&n->id,
749 n->primary_address.address,
752 n->primary_address.bandwidth_in,
753 n->primary_address.bandwidth_out);
758 * Initialize the alternative address of a neighbour
760 * @param n the neighbour
761 * @param address address of the other peer, NULL if other peer
763 * @param session session to use (or NULL, in which case an
764 * address must be setup)
765 * @param bandwidth_in inbound quota to be used when connection is up
766 * @param bandwidth_out outbound quota to be used when connection is up
769 set_alternative_address (struct NeighbourMapEntry *n,
770 const struct GNUNET_HELLO_Address *address,
771 struct GNUNET_ATS_Session *session,
772 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
773 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
775 struct GNUNET_TRANSPORT_PluginFunctions *papi;
777 if (NULL == (papi = GST_plugins_find (address->transport_name)))
782 if (session == n->alternative_address.session)
784 n->alternative_address.bandwidth_in = bandwidth_in;
785 n->alternative_address.bandwidth_out = bandwidth_out;
788 if (NULL != n->alternative_address.address)
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
791 "Replacing existing alternative address with another one\n");
792 free_address (&n->alternative_address);
795 session = papi->get_session (papi->cls,
799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
800 "Failed to obtain new session for peer `%s' and address '%s'\n",
801 GNUNET_i2s (&address->peer),
802 GST_plugins_a2s (address));
803 GNUNET_STATISTICS_update (GST_stats,
804 gettext_noop ("# session creation failed"),
809 GST_ats_new_session (address,
811 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
812 "Neighbour `%s' configured alternative address %s\n",
814 GST_plugins_a2s(address));
816 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
817 n->alternative_address.bandwidth_in = bandwidth_in;
818 n->alternative_address.bandwidth_out = bandwidth_out;
819 n->alternative_address.session = session;
820 n->alternative_address.ats_active = GNUNET_NO;
821 n->alternative_address.keep_alive_nonce = 0;
822 GNUNET_assert (GNUNET_YES ==
823 GST_ats_is_known (n->alternative_address.address,
824 n->alternative_address.session));
829 * Transmit a message using the current session of the given
832 * @param n entry for the recipient
833 * @param msgbuf buffer to transmit
834 * @param msgbuf_size number of bytes in @a msgbuf buffer
835 * @param priority transmission priority
836 * @param timeout transmission timeout
837 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
838 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
839 * @param cont continuation to call when finished (can be NULL)
840 * @param cont_cls closure for @a cont
841 * @return timeout (copy of @a timeout or a calculated one if
842 * @a use_keepalive_timeout is #GNUNET_YES.
844 static struct GNUNET_TIME_Relative
845 send_with_session (struct NeighbourMapEntry *n,
849 struct GNUNET_TIME_Relative timeout,
850 unsigned int use_keepalive_timeout,
851 GNUNET_TRANSPORT_TransmitContinuation cont,
854 struct GNUNET_TRANSPORT_PluginFunctions *papi;
855 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
857 GNUNET_assert (NULL != n->primary_address.session);
858 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
859 (-1 == papi->send (papi->cls,
860 n->primary_address.session,
864 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
865 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
866 papi->query_keepalive_factor (papi->cls))),
875 GST_neighbours_notify_data_sent (n->primary_address.address,
876 n->primary_address.session,
878 GNUNET_break (NULL != papi);
884 * Clear the primary address of a neighbour since this address is not
885 * valid anymore and notify monitoring about it
887 * @param n the neighbour
890 unset_primary_address (struct NeighbourMapEntry *n)
892 /* Notify monitoring about change */
893 if (NULL == n->primary_address.address)
895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
896 "Disabling primary address\n");
897 neighbours_changed_notification (&n->id,
898 n->primary_address.address,
901 GNUNET_BANDWIDTH_value_init (0),
902 GNUNET_BANDWIDTH_value_init (0));
903 free_address (&n->primary_address);
908 * Free a neighbour map entry.
910 * @param n entry to free
913 free_neighbour (struct NeighbourMapEntry *n)
915 struct MessageQueue *mq;
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
918 "Freeing neighbour state of peer `%s'\n",
919 GNUNET_i2s (&n->id));
920 n->is_active = NULL; /* always free'd by its own continuation! */
922 /* fail messages currently in the queue */
923 while (NULL != (mq = n->messages_head))
925 GNUNET_CONTAINER_DLL_remove (n->messages_head,
928 if (NULL != mq->cont)
929 mq->cont (mq->cont_cls,
931 mq->message_buf_size,
935 /* Mark peer as disconnected */
936 set_state_and_timeout (n,
937 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED,
938 GNUNET_TIME_UNIT_FOREVER_ABS);
939 /* free addresses and mark as unused */
940 unset_primary_address (n);
942 if (NULL != n->alternative_address.address)
944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
945 "Cleaning up alternative address\n");
946 free_address (&n->alternative_address);
948 GNUNET_assert (GNUNET_YES ==
949 GNUNET_CONTAINER_multipeermap_remove (neighbours,
952 /* Cancel address requests for this peer */
953 if (NULL != n->suggest_handle)
955 GNUNET_ATS_connectivity_suggest_cancel (n->suggest_handle);
956 n->suggest_handle = NULL;
959 /* Cancel the disconnect task */
960 if (NULL != n->delayed_disconnect_task)
962 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
963 n->delayed_disconnect_task = NULL;
966 /* Cancel the master task */
969 GNUNET_SCHEDULER_cancel (n->task);
972 /* free rest of memory */
978 * Function called when the 'DISCONNECT' message has been sent by the
979 * plugin. Frees the neighbour --- if the entry still exists.
982 * @param target identity of the neighbour that was disconnected
983 * @param result #GNUNET_OK if the disconnect got out successfully
984 * @param payload bytes payload
985 * @param physical bytes on wire
988 send_disconnect_cont (void *cls,
989 const struct GNUNET_PeerIdentity *target,
994 struct NeighbourMapEntry *n;
996 n = lookup_neighbour (target);
998 return; /* already gone */
999 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1000 return; /* have created a fresh entry since */
1001 if (NULL != n->task)
1002 GNUNET_SCHEDULER_cancel (n->task);
1003 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1008 * Transmit a DISCONNECT message to the other peer.
1010 * @param n neighbour to send DISCONNECT message.
1013 send_disconnect (struct NeighbourMapEntry *n)
1015 struct GNUNET_ATS_SessionDisconnectMessage disconnect_msg;
1017 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1018 "Sending DISCONNECT message to peer `%4s'\n",
1019 GNUNET_i2s (&n->id));
1020 disconnect_msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionDisconnectMessage));
1021 disconnect_msg.header.type =
1022 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1023 disconnect_msg.reserved = htonl (0);
1024 disconnect_msg.purpose.size =
1025 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1026 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1027 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1028 disconnect_msg.purpose.purpose =
1029 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1030 disconnect_msg.timestamp =
1031 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1032 disconnect_msg.public_key = GST_my_identity.public_key;
1033 GNUNET_assert (GNUNET_OK ==
1034 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1035 &disconnect_msg.purpose,
1036 &disconnect_msg.signature));
1038 (void) send_with_session (n,
1040 sizeof (disconnect_msg),
1042 GNUNET_TIME_UNIT_FOREVER_REL,
1044 &send_disconnect_cont,
1046 GNUNET_STATISTICS_update (GST_stats,
1047 gettext_noop ("# DISCONNECT messages sent"),
1054 * Disconnect from the given neighbour, clean up the record.
1056 * @param n neighbour to disconnect from
1059 disconnect_neighbour (struct NeighbourMapEntry *n)
1061 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1062 "Disconnecting from peer %s in state %s\n",
1063 GNUNET_i2s (&n->id),
1064 GNUNET_TRANSPORT_ps2s (n->state));
1065 /* depending on state, notify neighbour and/or upper layers of this peer
1069 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1070 case GNUNET_TRANSPORT_PS_INIT_ATS:
1071 /* other peer is completely unaware of us, no need to send DISCONNECT */
1074 case GNUNET_TRANSPORT_PS_SYN_SENT:
1075 send_disconnect (n);
1076 set_state_and_timeout (n,
1077 GNUNET_TRANSPORT_PS_DISCONNECT,
1078 GNUNET_TIME_UNIT_FOREVER_ABS);
1080 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1081 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1084 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1085 /* we DID ACK the other peer's request, must send DISCONNECT */
1086 send_disconnect (n);
1087 set_state_and_timeout (n,
1088 GNUNET_TRANSPORT_PS_DISCONNECT,
1089 GNUNET_TIME_UNIT_FOREVER_ABS);
1091 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1092 case GNUNET_TRANSPORT_PS_CONNECTED:
1093 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1094 /* we are currently connected, need to send disconnect and do
1095 internal notifications and update statistics */
1096 send_disconnect (n);
1097 set_state_and_timeout (n,
1098 GNUNET_TRANSPORT_PS_DISCONNECT,
1099 GNUNET_TIME_UNIT_FOREVER_ABS);
1101 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1102 /* Disconnecting while waiting for an ATS address to reconnect,
1103 * cannot send DISCONNECT */
1106 case GNUNET_TRANSPORT_PS_DISCONNECT:
1107 /* already disconnected, ignore */
1109 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1110 /* already cleaned up, how did we get here!? */
1114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1115 "Unhandled state `%s'\n",
1116 GNUNET_TRANSPORT_ps2s (n->state));
1120 /* schedule timeout to clean up */
1121 if (NULL != n->task)
1122 GNUNET_SCHEDULER_cancel (n->task);
1123 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1130 * Change the incoming quota for the given peer. Updates
1131 * our own receive rate and informs the neighbour about
1134 * @param n neighbour entry to change qutoa for
1135 * @param quota new quota
1138 set_incoming_quota (struct NeighbourMapEntry *n,
1139 struct GNUNET_BANDWIDTH_Value32NBO quota)
1141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1142 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
1143 ntohl (quota.value__), GNUNET_i2s (&n->id));
1144 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
1146 if (0 != ntohl (quota.value__))
1148 struct GNUNET_ATS_SessionQuotaMessage sqm;
1150 sqm.header.size = htons (sizeof (struct GNUNET_ATS_SessionQuotaMessage));
1151 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA);
1152 sqm.quota = quota.value__;
1153 if (NULL != n->primary_address.session)
1154 (void) send_with_session (n,
1158 GNUNET_TIME_UNIT_FOREVER_REL,
1163 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1164 "Disconnecting peer `%s' due to SET_QUOTA\n",
1165 GNUNET_i2s (&n->id));
1166 if (GNUNET_YES == test_connected (n))
1167 GNUNET_STATISTICS_update (GST_stats,
1168 gettext_noop ("# disconnects due to quota of 0"),
1170 disconnect_neighbour (n);
1175 * Initialize the primary address of a neighbour
1177 * @param n the neighbour
1178 * @param address address of the other peer, NULL if other peer
1180 * @param session session to use (or NULL, in which case an
1181 * address must be setup)
1182 * @param bandwidth_in inbound quota to be used when connection is up
1183 * @param bandwidth_out outbound quota to be used when connection is up
1186 set_primary_address (struct NeighbourMapEntry *n,
1187 const struct GNUNET_HELLO_Address *address,
1188 struct GNUNET_ATS_Session *session,
1189 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1190 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1192 if (session == n->primary_address.session)
1194 GST_validation_set_address_use (n->primary_address.address,
1196 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
1198 n->primary_address.bandwidth_in = bandwidth_in;
1199 set_incoming_quota (n,
1202 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
1204 n->primary_address.bandwidth_out = bandwidth_out;
1205 send_outbound_quota_to_clients (n);
1209 if ( (NULL != n->primary_address.address) &&
1210 (0 == GNUNET_HELLO_address_cmp (address,
1211 n->primary_address.address)) )
1216 if (NULL == session)
1219 GST_ats_block_address (address,
1223 if (NULL != n->primary_address.address)
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "Replacing existing primary address with another one\n");
1227 free_address (&n->primary_address);
1229 n->primary_address.address = GNUNET_HELLO_address_copy (address);
1230 n->primary_address.bandwidth_in = bandwidth_in;
1231 n->primary_address.bandwidth_out = bandwidth_out;
1232 n->primary_address.session = session;
1233 n->primary_address.keep_alive_nonce = 0;
1234 GNUNET_assert (GNUNET_YES ==
1235 GST_ats_is_known (n->primary_address.address,
1236 n->primary_address.session));
1237 /* subsystems about address use */
1238 GST_validation_set_address_use (n->primary_address.address,
1240 set_incoming_quota (n,
1242 send_outbound_quota_to_clients (n);
1243 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1244 "Neighbour `%s' switched to address `%s'\n",
1245 GNUNET_i2s (&n->id),
1246 GST_plugins_a2s(address));
1248 neighbours_changed_notification (&n->id,
1249 n->primary_address.address,
1252 n->primary_address.bandwidth_in,
1253 n->primary_address.bandwidth_out);
1258 * We're done with our transmission attempt, continue processing.
1260 * @param cls the `struct MessageQueue` of the message
1261 * @param receiver intended receiver
1262 * @param success whether it worked or not
1263 * @param size_payload bytes payload sent
1264 * @param physical bytes sent on wire
1267 transmit_send_continuation (void *cls,
1268 const struct GNUNET_PeerIdentity *receiver,
1270 size_t size_payload,
1273 struct MessageQueue *mq = cls;
1274 struct NeighbourMapEntry *n;
1276 if (NULL == (n = lookup_neighbour (receiver)))
1278 if (NULL != mq->cont)
1279 mq->cont (mq->cont_cls,
1280 GNUNET_SYSERR /* not connected */,
1284 return; /* disconnect or other error while transmitting, can happen */
1286 if (n->is_active == mq)
1288 /* this is still "our" neighbour, remove us from its queue
1289 and allow it to send the next message now */
1290 n->is_active = NULL;
1291 if (NULL != n->task)
1292 GNUNET_SCHEDULER_cancel (n->task);
1293 n->task = GNUNET_SCHEDULER_add_now (&master_task,
1296 if (bytes_in_send_queue < mq->message_buf_size)
1298 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1299 "Bytes_in_send_queue `%llu', Message_size %u, result: %s, payload %u, on wire %u\n",
1300 bytes_in_send_queue,
1301 (unsigned int) mq->message_buf_size,
1302 (GNUNET_OK == success) ? "OK" : "FAIL",
1303 (unsigned int) size_payload,
1304 (unsigned int) physical);
1308 GNUNET_break (size_payload == mq->message_buf_size);
1309 bytes_in_send_queue -= mq->message_buf_size;
1310 GNUNET_STATISTICS_set (GST_stats,
1311 gettext_noop ("# bytes in message queue for other peers"),
1312 bytes_in_send_queue,
1314 if (GNUNET_OK == success)
1315 GNUNET_STATISTICS_update (GST_stats,
1316 gettext_noop ("# messages transmitted to other peers"),
1320 GNUNET_STATISTICS_update (GST_stats,
1322 ("# transmission failures for messages to other peers"),
1324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1325 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1326 GNUNET_i2s (receiver),
1327 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1328 (unsigned int) mq->message_buf_size,
1329 (success == GNUNET_OK) ? "success" : "FAILURE");
1330 if (NULL != mq->cont)
1331 mq->cont (mq->cont_cls,
1340 * Check the message list for the given neighbour and if we can
1341 * send a message, do so. This function should only be called
1342 * if the connection is at least generally ready for transmission.
1343 * While we will only send one message at a time, no bandwidth
1344 * quota management is performed here. If a message was given to
1345 * the plugin, the continuation will automatically re-schedule
1346 * the 'master' task once the next message might be transmitted.
1348 * @param n target peer for which to transmit
1351 try_transmission_to_peer (struct NeighbourMapEntry *n)
1353 struct MessageQueue *mq;
1354 struct GNUNET_TIME_Relative timeout;
1356 if (NULL == n->primary_address.address)
1358 /* no address, why are we here? */
1362 if ((0 == n->primary_address.address->address_length) &&
1363 (NULL == n->primary_address.session))
1365 /* no address, why are we here? */
1369 if (NULL != n->is_active)
1371 /* transmission already pending */
1375 /* timeout messages from the queue that are past their due date */
1376 while (NULL != (mq = n->messages_head))
1378 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1379 if (timeout.rel_value_us > 0)
1381 GNUNET_STATISTICS_update (GST_stats,
1382 gettext_noop ("# messages timed out while in transport queue"),
1385 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1389 transmit_send_continuation (mq,
1392 mq->message_buf_size,
1396 return; /* no more messages */
1397 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Giving message with %u bytes to plugin session %p\n",
1404 (unsigned int) mq->message_buf_size,
1405 n->primary_address.session);
1406 (void) send_with_session (n,
1408 mq->message_buf_size,
1412 &transmit_send_continuation,
1418 * Send keepalive message to the neighbour. Must only be called
1419 * if we are on 'connected' state or while trying to switch addresses.
1420 * Will internally determine if a keepalive is truly needed (so can
1421 * always be called).
1423 * @param n neighbour that went idle and needs a keepalive
1426 send_keepalive (struct NeighbourMapEntry *n)
1428 struct GNUNET_ATS_SessionKeepAliveMessage m;
1429 struct GNUNET_TIME_Relative timeout;
1432 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1433 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state));
1434 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1435 return; /* no keepalive needed at this time */
1437 nonce = 0; /* 0 indicates 'not set' */
1439 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1443 "Sending KEEPALIVE to peer `%s' with nonce %u\n",
1444 GNUNET_i2s (&n->id),
1446 m.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1447 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1448 m.nonce = htonl (nonce);
1450 timeout = send_with_session (n,
1453 UINT32_MAX /* priority */,
1454 GNUNET_TIME_UNIT_FOREVER_REL,
1457 GNUNET_STATISTICS_update (GST_stats,
1458 gettext_noop ("# KEEPALIVES sent"),
1461 n->primary_address.keep_alive_nonce = nonce;
1462 n->expect_latency_response = GNUNET_YES;
1463 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1464 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1469 * Keep the connection to the given neighbour alive longer,
1470 * we received a KEEPALIVE (or equivalent); send a response.
1472 * @param neighbour neighbour to keep alive (by sending keep alive response)
1473 * @param m the keep alive message containing the nonce to respond to
1476 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1477 const struct GNUNET_MessageHeader *m)
1479 struct NeighbourMapEntry *n;
1480 const struct GNUNET_ATS_SessionKeepAliveMessage *msg_in;
1481 struct GNUNET_ATS_SessionKeepAliveMessage msg;
1483 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1485 GNUNET_break_op (0);
1489 msg_in = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1490 if (NULL == (n = lookup_neighbour (neighbour)))
1492 GNUNET_STATISTICS_update (GST_stats,
1494 ("# KEEPALIVE messages discarded (peer unknown)"),
1498 if (NULL == n->primary_address.session)
1500 GNUNET_STATISTICS_update (GST_stats,
1502 ("# KEEPALIVE messages discarded (no session)"),
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Received KEEPALIVE request from peer `%s' with nonce %u\n",
1509 GNUNET_i2s (&n->id),
1510 ntohl (msg_in->nonce));
1511 GNUNET_STATISTICS_update (GST_stats,
1512 gettext_noop ("# KEEPALIVES received in good order"),
1516 /* send reply to allow neighbour to measure latency */
1517 msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1518 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1519 msg.nonce = msg_in->nonce;
1520 (void) send_with_session (n,
1522 sizeof (struct GNUNET_ATS_SessionKeepAliveMessage),
1523 UINT32_MAX /* priority */,
1524 GNUNET_TIME_UNIT_FOREVER_REL,
1531 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1532 * latency to this peer. Pass the updated information (existing ats
1533 * plus calculated latency) to ATS.
1535 * @param neighbour neighbour to keep alive
1536 * @param m the message containing the keep alive response
1539 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1540 const struct GNUNET_MessageHeader *m)
1542 struct NeighbourMapEntry *n;
1543 const struct GNUNET_ATS_SessionKeepAliveMessage *msg;
1544 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1545 struct GNUNET_TIME_Relative latency;
1547 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1549 GNUNET_break_op (0);
1553 msg = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1554 if (NULL == (n = lookup_neighbour (neighbour)))
1556 GNUNET_STATISTICS_update (GST_stats,
1557 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not connected)"),
1562 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1563 (GNUNET_YES != n->expect_latency_response) )
1565 GNUNET_STATISTICS_update (GST_stats,
1566 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not expected)"),
1571 if (NULL == n->primary_address.address)
1573 GNUNET_STATISTICS_update (GST_stats,
1574 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (address changed)"),
1579 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1581 if (0 == n->primary_address.keep_alive_nonce)
1582 GNUNET_STATISTICS_update (GST_stats,
1583 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (no nonce)"),
1587 GNUNET_STATISTICS_update (GST_stats,
1588 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (bad nonce)"),
1593 GNUNET_STATISTICS_update (GST_stats,
1594 gettext_noop ("# KEEPALIVE_RESPONSEs received (OK)"),
1599 /* Update session timeout here */
1600 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Updating session for peer `%s' for session %p\n",
1604 GNUNET_i2s (&n->id),
1605 n->primary_address.session);
1606 papi->update_session_timeout (papi->cls,
1608 n->primary_address.session);
1615 n->primary_address.keep_alive_nonce = 0;
1616 n->expect_latency_response = GNUNET_NO;
1617 set_state_and_timeout (n,
1619 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1621 latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1623 "Received KEEPALIVE_RESPONSE from peer `%s', latency is %s\n",
1624 GNUNET_i2s (&n->id),
1625 GNUNET_STRINGS_relative_time_to_string (latency,
1627 GST_ats_update_delay (n->primary_address.address,
1628 GNUNET_TIME_relative_divide (latency,
1634 * We have received a message from the given sender. How long should
1635 * we delay before receiving more? (Also used to keep the peer marked
1638 * @param sender sender of the message
1639 * @param size size of the message
1640 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1641 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1642 * #GNUNET_SYSERR if the connection is not fully up yet
1643 * @return how long to wait before reading more from this sender
1645 struct GNUNET_TIME_Relative
1646 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
1650 struct NeighbourMapEntry *n;
1651 struct GNUNET_TIME_Relative ret;
1653 if (NULL == neighbours)
1655 *do_forward = GNUNET_NO;
1656 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1658 if (NULL == (n = lookup_neighbour (sender)))
1660 GNUNET_STATISTICS_update (GST_stats,
1661 gettext_noop ("# messages discarded due to lack of neighbour record"),
1664 *do_forward = GNUNET_NO;
1665 return GNUNET_TIME_UNIT_ZERO;
1667 if (! test_connected (n))
1669 *do_forward = GNUNET_SYSERR;
1670 return GNUNET_TIME_UNIT_ZERO;
1672 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1674 n->quota_violation_count++;
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1677 n->in_tracker.available_bytes_per_s__,
1678 n->quota_violation_count);
1679 /* Discount 32k per violation */
1680 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1684 if (n->quota_violation_count > 0)
1686 /* try to add 32k back */
1687 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1688 n->quota_violation_count--;
1691 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1693 GNUNET_STATISTICS_update (GST_stats,
1695 ("# bandwidth quota violations by other peers"),
1697 *do_forward = GNUNET_NO;
1698 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1700 *do_forward = GNUNET_YES;
1701 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1702 if (ret.rel_value_us > 0)
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "Throttling read (%lld bytes excess at %u b/s), waiting %s before reading more.\n",
1706 (long long) n->in_tracker.consumption_since_last_update__,
1707 (unsigned int) n->in_tracker.available_bytes_per_s__,
1708 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1709 GNUNET_STATISTICS_update (GST_stats,
1710 gettext_noop ("# ms throttling suggested"),
1711 (int64_t) ret.rel_value_us / 1000LL,
1719 * Transmit a message to the given target using the active connection.
1721 * @param target destination
1722 * @param msg message to send
1723 * @param msg_size number of bytes in msg
1724 * @param timeout when to fail with timeout
1725 * @param cont function to call when done
1726 * @param cont_cls closure for @a cont
1729 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
1732 struct GNUNET_TIME_Relative timeout,
1733 GST_NeighbourSendContinuation cont,
1736 struct NeighbourMapEntry *n;
1737 struct MessageQueue *mq;
1739 /* All ove these cases should never happen; they are all API violations.
1740 But we check anyway, just to be sure. */
1741 if (NULL == (n = lookup_neighbour (target)))
1751 if (GNUNET_YES != test_connected (n))
1761 bytes_in_send_queue += msg_size;
1762 GNUNET_STATISTICS_set (GST_stats,
1764 ("# bytes in message queue for other peers"),
1765 bytes_in_send_queue, GNUNET_NO);
1766 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1768 mq->cont_cls = cont_cls;
1769 GNUNET_memcpy (&mq[1], msg, msg_size);
1770 mq->message_buf = (const char *) &mq[1];
1771 mq->message_buf_size = msg_size;
1772 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1774 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1775 "Enqueueing %u bytes to send to peer %s\n",
1776 (unsigned int) msg_size,
1777 GNUNET_i2s (target));
1778 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
1781 if (NULL != n->task)
1782 GNUNET_SCHEDULER_cancel (n->task);
1783 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1788 * Continuation called from our attempt to transmitted our
1789 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN to the specified @a
1790 * target. Continue processing based on the @a result. Specifically,
1791 * if we failed to transmit, discard the address we used.
1794 * @param target which peer received the transmission
1795 * @param result #GNUNET_OK if sending worked
1796 * @param size_payload how many bytes of payload were sent (ignored)
1797 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1800 send_session_syn_cont (void *cls,
1801 const struct GNUNET_PeerIdentity *target,
1803 size_t size_payload,
1804 size_t size_on_wire)
1806 struct NeighbourMapEntry *n;
1808 n = lookup_neighbour (target);
1811 /* SYN continuation was called after neighbor was freed,
1812 * for example due to a time out for the state or the session
1813 * used was already terminated: nothing to do here... */
1817 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1818 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1819 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1821 /* SYN continuation was called after neighbor changed state,
1822 * for example due to a time out for the state or the session
1823 * used was already terminated: nothing to do here... */
1826 if (GNUNET_OK == result)
1829 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1830 _("Failed to send SYN message to peer `%s'\n"),
1831 GNUNET_i2s (target));
1833 case GNUNET_TRANSPORT_PS_SYN_SENT:
1834 /* Remove address and request an additional one */
1835 unset_primary_address (n);
1836 set_state_and_timeout (n,
1837 GNUNET_TRANSPORT_PS_INIT_ATS,
1838 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1840 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1841 /* Remove address and request an additional one */
1842 unset_primary_address (n);
1843 set_state_and_timeout (n,
1844 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1845 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1847 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1848 /* Remove address and request and go back to primary address */
1849 GNUNET_STATISTICS_update (GST_stats,
1850 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN CONT)"),
1853 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1854 "Switch failed, cleaning up alternative address\n");
1855 free_address (&n->alternative_address);
1856 set_state_and_timeout (n,
1857 GNUNET_TRANSPORT_PS_CONNECTED,
1858 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1861 disconnect_neighbour (n);
1868 * Send a SYN message via the given address.
1870 * @param na address to use
1873 send_syn (struct NeighbourAddress *na)
1875 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1876 struct TransportSynMessage connect_msg;
1877 struct NeighbourMapEntry *n;
1879 GNUNET_assert (NULL != na->session);
1880 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1881 "Sending SYN message to peer `%s' at %s\n",
1882 GNUNET_i2s (&na->address->peer),
1883 GST_plugins_a2s (na->address));
1885 papi = GST_plugins_find (na->address->transport_name);
1886 GNUNET_assert (NULL != papi);
1887 GNUNET_STATISTICS_update (GST_stats,
1889 ("# SYN messages sent"),
1891 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1892 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1893 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1894 connect_msg.reserved = htonl (0);
1895 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1897 papi->send (papi->cls,
1899 (const char *) &connect_msg,
1900 sizeof (struct TransportSynMessage),
1902 SETUP_CONNECTION_TIMEOUT,
1903 &send_session_syn_cont, NULL))
1905 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1906 _("Failed to transmit SYN message to %s\n"),
1907 GST_plugins_a2s (na->address));
1908 n = lookup_neighbour (&na->address->peer);
1915 case GNUNET_TRANSPORT_PS_SYN_SENT:
1916 /* Remove address and request and additional one */
1917 GNUNET_assert (na == &n->primary_address);
1918 unset_primary_address (n);
1919 set_state_and_timeout (n,
1920 GNUNET_TRANSPORT_PS_INIT_ATS,
1921 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1922 /* Hard failure to send the SYN message with this address:
1923 Destroy address and session */
1925 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1926 /* Remove address and request an additional one */
1927 GNUNET_assert (na == &n->primary_address);
1928 unset_primary_address (n);
1929 set_state_and_timeout (n,
1930 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1931 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1933 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1934 GNUNET_assert (na == &n->alternative_address);
1935 GNUNET_STATISTICS_update (GST_stats,
1936 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN)"),
1939 /* Remove address and request an additional one */
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 "Switch failed, cleaning up alternative address\n");
1942 free_address (&n->alternative_address);
1943 set_state_and_timeout (n,
1944 GNUNET_TRANSPORT_PS_CONNECTED,
1945 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1949 disconnect_neighbour (n);
1954 GST_neighbours_notify_data_sent (na->address,
1956 sizeof (struct TransportSynMessage));
1961 * Continuation called from our attempt to transmitted our
1962 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK to the specified @a
1963 * target. Continue processing based on the @a result. Specifically,
1964 * if we failed to transmit, discard the address we used.
1967 * @param target which peer received the transmission
1968 * @param result #GNUNET_OK if sending worked
1969 * @param size_payload how many bytes of payload were sent (ignored)
1970 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1973 send_session_syn_ack_cont (void *cls,
1974 const struct GNUNET_PeerIdentity *target,
1976 size_t size_payload,
1977 size_t size_on_wire)
1979 struct NeighbourMapEntry *n;
1981 n = lookup_neighbour (target);
1984 /* SYN_ACK continuation was called after neighbor was freed,
1985 * for example due to a time out for the state or the session
1986 * used was already terminated: nothing to do here... */
1990 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1992 /* SYN_ACK continuation was called after neighbor changed state,
1993 * for example due to a time out for the state or the session
1994 * used was already terminated: nothing to do here... */
1997 if (GNUNET_OK == result)
2000 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2001 _("Failed to send SYN_ACK message to peer `%s' using address `%s'\n"),
2002 GNUNET_i2s (target),
2003 GST_plugins_a2s (n->primary_address.address));
2005 /* Remove address and request and additional one */
2006 /* FIXME: what if the neighbour's primary address
2007 changed in the meantime? Might want to instead
2008 pass "something" around in closure to be sure. */
2009 unset_primary_address (n);
2010 n->ack_state = ACK_SEND_SYN_ACK;
2011 set_state_and_timeout (n,
2012 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2013 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2018 * Send a SYN_ACK message via the given address.
2020 * @param na address and session to use
2021 * @param timestamp timestamp to use for the ACK message
2022 * @return #GNUNET_SYSERR if sending immediately failed, #GNUNET_OK otherwise
2025 send_syn_ack_message (struct NeighbourAddress *na,
2026 struct GNUNET_TIME_Absolute timestamp)
2028 const struct GNUNET_HELLO_Address *address = na->address;
2029 struct GNUNET_ATS_Session *session = na->session;
2030 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2031 struct TransportSynMessage connect_msg;
2032 struct NeighbourMapEntry *n;
2034 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2035 "Sending SYN_ACK to peer `%s'\n",
2036 GNUNET_i2s (&address->peer));
2038 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2043 if (NULL == session)
2044 session = papi->get_session (papi->cls,
2046 if (NULL == session)
2051 GST_ats_new_session (address,
2053 GNUNET_STATISTICS_update (GST_stats,
2055 ("# SYN_ACK messages sent"),
2057 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
2058 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
2059 connect_msg.reserved = htonl (0);
2060 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
2062 if (GNUNET_SYSERR ==
2063 papi->send (papi->cls,
2065 (const char *) &connect_msg,
2066 sizeof (struct TransportSynMessage),
2068 GNUNET_TIME_UNIT_FOREVER_REL,
2069 &send_session_syn_ack_cont, NULL))
2071 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2072 _("Failed to transmit SYN_ACK message to %s\n"),
2073 GST_plugins_a2s (address));
2075 n = lookup_neighbour (&address->peer);
2081 /* Remove address and request and additional one */
2082 unset_primary_address (n);
2083 n->ack_state = ACK_SEND_SYN_ACK;
2084 set_state_and_timeout (n,
2085 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2086 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2093 * Function called by the bandwidth tracker for a peer whenever
2094 * the tracker's state changed such that we need to recalculate
2095 * the delay for flow control. We calculate the latest delay
2096 * and inform the plugin (if applicable).
2098 * @param cls the `struct NeighbourMapEntry` to update calculations for
2101 inbound_bw_tracker_update (void *cls)
2103 struct NeighbourMapEntry *n = cls;
2104 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2105 struct GNUNET_TIME_Relative delay;
2108 if (NULL == n->primary_address.address)
2109 return; /* not active, ignore */
2110 papi = GST_plugins_find (n->primary_address.address->transport_name);
2111 GNUNET_assert (NULL != papi);
2112 if (NULL == papi->update_inbound_delay)
2114 delay = GST_neighbours_calculate_receive_delay (&n->id,
2117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2118 "New inbound delay for peer `%s' is %llu ms\n",
2119 GNUNET_i2s (&n->id),
2120 (unsigned long long) delay.rel_value_us / 1000LL);
2121 papi->update_inbound_delay (papi->cls,
2123 n->primary_address.session,
2129 * Create a fresh entry in the neighbour map for the given peer
2131 * @param peer peer to create an entry for
2132 * @return new neighbour map entry
2134 static struct NeighbourMapEntry *
2135 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2137 struct NeighbourMapEntry *n;
2140 memcmp (&GST_my_identity,
2142 sizeof (struct GNUNET_PeerIdentity)))
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 "Cowardly refusing to consider myself my neighbour!\n");
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 "Creating new neighbour entry for `%s'\n",
2151 n = GNUNET_new (struct NeighbourMapEntry);
2153 n->ack_state = ACK_UNDEFINED;
2154 n->last_util_transmission = GNUNET_TIME_absolute_get();
2155 n->neighbour_receive_quota = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2156 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2157 &inbound_bw_tracker_update,
2159 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2160 MAX_BANDWIDTH_CARRY_S);
2161 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2162 set_state_and_timeout (n,
2163 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2164 GNUNET_TIME_UNIT_FOREVER_ABS);
2165 GNUNET_assert (GNUNET_OK ==
2166 GNUNET_CONTAINER_multipeermap_put (neighbours,
2169 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2170 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2179 * Entry in a DLL we use to keep track of pending blacklist checks.
2181 struct BlacklistCheckSwitchContext
2186 struct BlacklistCheckSwitchContext *prev;
2191 struct BlacklistCheckSwitchContext *next;
2194 * Handle to the blacklist check we are performing.
2196 struct GST_BlacklistCheck *blc;
2199 * Inbound bandwidth that was assigned to @e address.
2201 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2204 * Outbound bandwidth that was assigned to @e address.
2206 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2211 * We received a 'SYN' message from the other peer.
2212 * Consider switching to it.
2214 * @param message possibly a `struct TransportSynMessage` (check format)
2215 * @param peer identity of the peer to switch the address for
2216 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2219 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2220 const struct GNUNET_PeerIdentity *peer)
2222 const struct TransportSynMessage *scm;
2223 struct NeighbourMapEntry *n;
2224 struct GNUNET_TIME_Absolute ts;
2226 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2228 GNUNET_break_op (0);
2229 return GNUNET_SYSERR;
2231 GNUNET_STATISTICS_update (GST_stats,
2233 ("# SYN messages received"),
2235 if (NULL == neighbours)
2237 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2238 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2240 return GNUNET_OK; /* we're shutting down */
2242 scm = (const struct TransportSynMessage *) message;
2243 GNUNET_break_op (0 == ntohl (scm->reserved));
2244 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2246 memcmp (&GST_my_identity,
2248 sizeof (struct GNUNET_PeerIdentity)))
2250 /* loopback connection-to-self, ignore */
2251 return GNUNET_SYSERR;
2253 n = lookup_neighbour (peer);
2256 /* This is a new neighbour and set to not connected */
2257 n = setup_neighbour (peer);
2258 GNUNET_assert (NULL != n);
2261 /* Remember this SYN message in neighbour */
2262 n->ack_state = ACK_SEND_SYN_ACK;
2263 n->connect_ack_timestamp = ts;
2265 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2266 "Received SYN for peer `%s' in state %s/%s\n",
2268 GNUNET_TRANSPORT_ps2s (n->state),
2269 print_ack_state (n->ack_state));
2273 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2274 /* Request an address from ATS to send SYN_ACK to this peer */
2275 set_state_and_timeout (n,
2276 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2277 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2279 case GNUNET_TRANSPORT_PS_INIT_ATS:
2280 /* SYN message takes priority over us asking ATS for address:
2281 * Wait for ATS to suggest an address and send SYN_ACK */
2282 set_state_and_timeout (n,
2283 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2284 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2286 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2287 /* We already wait for an address to send an SYN_ACK */
2289 case GNUNET_TRANSPORT_PS_SYN_SENT:
2290 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2291 /* Send ACK immediately */
2292 n->ack_state = ACK_SEND_ACK;
2293 send_syn_ack_message (&n->primary_address,
2296 case GNUNET_TRANSPORT_PS_CONNECTED:
2297 /* we are already connected and can thus send the ACK immediately */
2298 GNUNET_assert (NULL != n->primary_address.address);
2299 GNUNET_assert (NULL != n->primary_address.session);
2300 n->ack_state = ACK_SEND_ACK;
2301 send_syn_ack_message (&n->primary_address,
2304 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2305 /* We wait for ATS address suggestion */
2307 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2308 /* We received a SYN message while waiting for a SYN_ACK in fast
2309 * reconnect. Send SYN_ACK immediately */
2310 n->ack_state = ACK_SEND_ACK;
2311 send_syn_ack_message (&n->primary_address,
2312 n->connect_ack_timestamp);
2314 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2315 /* We are already connected and can thus send the ACK immediately;
2316 still, it can never hurt to have an alternative address, so also
2317 tell ATS about it */
2318 GNUNET_assert (NULL != n->primary_address.address);
2319 GNUNET_assert (NULL != n->primary_address.session);
2320 n->ack_state = ACK_SEND_ACK;
2321 send_syn_ack_message (&n->primary_address,
2324 case GNUNET_TRANSPORT_PS_DISCONNECT:
2325 /* Get rid of remains and re-try */
2327 n = setup_neighbour (peer);
2328 GNUNET_assert (NULL != n);
2329 /* Remember the SYN time stamp for ACK message */
2330 n->ack_state = ACK_SEND_SYN_ACK;
2331 n->connect_ack_timestamp = ts;
2332 /* Request an address for the peer */
2333 set_state_and_timeout (n,
2334 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2335 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2337 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2338 /* should not be possible */
2342 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2343 "Unhandled state `%s'\n",
2344 GNUNET_TRANSPORT_ps2s (n->state));
2346 return GNUNET_SYSERR;
2353 * Check if the given @a address is the same that we are already
2354 * using for the respective neighbour. If so, update the bandwidth
2355 * assignment and possibly the session and return #GNUNET_OK.
2356 * If the new address is different from what the neighbour is
2357 * using right now, return #GNUNET_NO.
2359 * @param address address of the other peer,
2360 * @param session session to use or NULL if transport should initiate a session
2361 * @param bandwidth_in inbound quota to be used when connection is up,
2362 * 0 to disconnect from peer
2363 * @param bandwidth_out outbound quota to be used when connection is up,
2364 * 0 to disconnect from peer
2365 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2366 * #GNUNET_NO if more extensive changes are required (address changed)
2369 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2370 struct GNUNET_ATS_Session *session,
2371 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2372 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2374 struct NeighbourMapEntry *n;
2376 n = lookup_neighbour (&address->peer);
2378 (NULL == n->primary_address.address) ||
2379 (0 != GNUNET_HELLO_address_cmp (address,
2380 n->primary_address.address)) )
2382 /* We are not really switching addresses, but merely adjusting
2383 session and/or bandwidth, can do fast ATS update! */
2384 if (session != n->primary_address.session)
2386 /* switch to a different session, but keeping same address; could
2387 happen if there is a 2nd inbound connection */
2388 n->primary_address.session = session;
2389 GNUNET_assert (GNUNET_YES ==
2390 GST_ats_is_known (n->primary_address.address,
2391 n->primary_address.session));
2393 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
2395 n->primary_address.bandwidth_in = bandwidth_in;
2396 set_incoming_quota (n,
2399 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
2401 n->primary_address.bandwidth_out = bandwidth_out;
2402 send_outbound_quota_to_clients (n);
2409 * We've been asked to switch addresses, and just now got the result
2410 * from the blacklist check to see if this is allowed.
2412 * @param cls the `struct BlacklistCheckSwitchContext` with
2413 * the information about the future address
2414 * @param peer the peer we may switch addresses on
2415 * @param address address associated with the request
2416 * @param session session associated with the request
2417 * @param result #GNUNET_OK if the connection is allowed,
2418 * #GNUNET_NO if not,
2419 * #GNUNET_SYSERR if operation was aborted
2422 switch_address_bl_check_cont (void *cls,
2423 const struct GNUNET_PeerIdentity *peer,
2424 const struct GNUNET_HELLO_Address *address,
2425 struct GNUNET_ATS_Session *session,
2428 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2429 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2430 struct NeighbourMapEntry *n;
2432 if (GNUNET_SYSERR == result)
2435 papi = GST_plugins_find (address->transport_name);
2436 GNUNET_assert (NULL != papi);
2438 if (GNUNET_NO == result)
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2442 GST_plugins_a2s (address),
2445 GNUNET_STATISTICS_update (GST_stats,
2446 "# ATS suggestions ignored (blacklist denied)",
2449 if (NULL != session)
2450 papi->disconnect_session (papi->cls,
2453 GNUNET_HELLO_address_check_option (address,
2454 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2455 GST_ats_block_address (address,
2461 if (NULL == session)
2463 /* need to create a session, ATS only gave us an address */
2464 session = papi->get_session (papi->cls,
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "Obtained new session for peer `%s' and address '%s': %p\n",
2468 GNUNET_i2s (&address->peer),
2469 GST_plugins_a2s (address),
2471 if (NULL != session)
2472 GST_ats_new_session (address,
2475 if (NULL == session)
2477 /* session creation failed, bad!, fail! */
2478 GNUNET_STATISTICS_update (GST_stats,
2479 "# ATS suggestions ignored (failed to create session)",
2482 /* No session could be obtained, remove blacklist check and clean up */
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2484 "Failed to obtain new session for peer `%s' and address '%s'\n",
2485 GNUNET_i2s (&address->peer),
2486 GST_plugins_a2s (address));
2487 GST_ats_block_address (address,
2492 /* We did this check already before going into blacklist, but
2493 it is theoretically possible that the situation changed in
2494 the meantime, hence we check again here */
2496 try_run_fast_ats_update (address,
2498 blc_ctx->bandwidth_in,
2499 blc_ctx->bandwidth_out))
2500 goto cleanup; /* was just a minor update, we're done */
2502 /* check if we also need to setup the neighbour entry */
2503 if (NULL == (n = lookup_neighbour (peer)))
2505 n = setup_neighbour (peer);
2508 /* not sure how this can happen... */
2512 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2515 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2516 "Peer `%s' switches to address `%s'\n",
2517 GNUNET_i2s (&address->peer),
2518 GST_plugins_a2s (address));
2522 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2524 GST_ats_block_address (address,
2528 case GNUNET_TRANSPORT_PS_INIT_ATS:
2529 /* We requested an address and ATS suggests one:
2530 * set primary address and send SYN message*/
2531 set_primary_address (n,
2534 blc_ctx->bandwidth_in,
2535 blc_ctx->bandwidth_out);
2536 if (ACK_SEND_SYN_ACK == n->ack_state)
2538 /* Send pending SYN_ACK message */
2539 n->ack_state = ACK_SEND_ACK;
2540 send_syn_ack_message (&n->primary_address,
2541 n->connect_ack_timestamp);
2543 set_state_and_timeout (n,
2544 GNUNET_TRANSPORT_PS_SYN_SENT,
2545 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2546 send_syn (&n->primary_address);
2548 case GNUNET_TRANSPORT_PS_SYN_SENT:
2549 /* ATS suggested a new address while waiting for an SYN_ACK:
2550 * Switch and send new SYN */
2551 /* ATS suggests a different address, switch again */
2552 set_primary_address (n,
2555 blc_ctx->bandwidth_in,
2556 blc_ctx->bandwidth_out);
2557 if (ACK_SEND_SYN_ACK == n->ack_state)
2559 /* Send pending SYN_ACK message */
2560 n->ack_state = ACK_SEND_ACK;
2561 send_syn_ack_message (&n->primary_address,
2562 n->connect_ack_timestamp);
2564 set_state_and_timeout (n,
2565 GNUNET_TRANSPORT_PS_SYN_SENT,
2566 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2567 send_syn (&n->primary_address);
2569 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2570 /* We requested an address and ATS suggests one:
2571 * set primary address and send SYN_ACK message*/
2572 set_primary_address (n,
2575 blc_ctx->bandwidth_in,
2576 blc_ctx->bandwidth_out);
2577 /* Send an ACK message as a response to the SYN msg */
2578 set_state_and_timeout (n,
2579 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2580 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2581 send_syn_ack_message (&n->primary_address,
2582 n->connect_ack_timestamp);
2583 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2584 (ACK_UNDEFINED == n->ack_state) )
2585 n->ack_state = ACK_SEND_ACK;
2587 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2588 /* ATS asks us to switch while we were trying to connect; switch to new
2589 address and check blacklist again */
2590 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2592 n->ack_state = ACK_SEND_ACK;
2593 send_syn_ack_message (&n->primary_address,
2594 n->connect_ack_timestamp);
2596 set_primary_address (n,
2599 blc_ctx->bandwidth_in,
2600 blc_ctx->bandwidth_out);
2601 set_state_and_timeout (n,
2602 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2603 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2605 case GNUNET_TRANSPORT_PS_CONNECTED:
2606 GNUNET_assert (NULL != n->primary_address.address);
2607 GNUNET_assert (NULL != n->primary_address.session);
2608 GNUNET_break (n->primary_address.session != session);
2609 /* ATS asks us to switch a life connection; see if we can get
2610 a SYN_ACK on it before we actually do this! */
2611 set_alternative_address (n,
2614 blc_ctx->bandwidth_in,
2615 blc_ctx->bandwidth_out);
2616 set_state_and_timeout (n,
2617 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2618 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2619 GNUNET_STATISTICS_update (GST_stats,
2620 gettext_noop ("# Attempts to switch addresses"),
2623 send_syn (&n->alternative_address);
2625 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2626 set_primary_address (n,
2629 blc_ctx->bandwidth_in,
2630 blc_ctx->bandwidth_out);
2631 if (ACK_SEND_SYN_ACK == n->ack_state)
2633 /* Send pending SYN_ACK message */
2634 n->ack_state = ACK_SEND_ACK;
2635 send_syn_ack_message (&n->primary_address,
2636 n->connect_ack_timestamp);
2638 set_state_and_timeout (n,
2639 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2640 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2641 send_syn (&n->primary_address);
2643 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2644 /* ATS asks us to switch while we were trying to reconnect; switch to new
2645 address and send SYN again */
2646 set_primary_address (n,
2649 blc_ctx->bandwidth_in,
2650 blc_ctx->bandwidth_out);
2651 set_state_and_timeout (n,
2652 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2653 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2654 send_syn (&n->primary_address);
2656 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2657 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2659 (n->primary_address.session == session) )
2661 /* ATS switches back to still-active session */
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2663 "ATS double-switched, cleaning up alternative address\n");
2664 free_address (&n->alternative_address);
2665 set_state_and_timeout (n,
2666 GNUNET_TRANSPORT_PS_CONNECTED,
2670 /* ATS asks us to switch a life connection, send */
2671 set_alternative_address (n,
2674 blc_ctx->bandwidth_in,
2675 blc_ctx->bandwidth_out);
2676 set_state_and_timeout (n,
2677 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2678 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2679 send_syn (&n->alternative_address);
2681 case GNUNET_TRANSPORT_PS_DISCONNECT:
2682 /* not going to switch addresses while disconnecting */
2683 GNUNET_STATISTICS_update (GST_stats,
2684 "# ATS suggestion ignored (disconnecting)",
2688 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2692 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2693 "Unhandled state `%s'\n",
2694 GNUNET_TRANSPORT_ps2s (n->state));
2699 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2702 GNUNET_free (blc_ctx);
2707 * For the given peer, switch to this address.
2709 * Before accepting this addresses and actively using it, a blacklist check
2712 * If any check fails or the suggestion can somehow not be followed, we
2713 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2714 * could not be satisfied and force ATS to do something else.
2716 * @param address address of the other peer,
2717 * @param session session to use or NULL if transport should initiate a session
2718 * @param bandwidth_in inbound quota to be used when connection is up,
2719 * 0 to disconnect from peer
2720 * @param bandwidth_out outbound quota to be used when connection is up,
2721 * 0 to disconnect from peer
2724 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2725 struct GNUNET_ATS_Session *session,
2726 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2727 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2729 struct GST_BlacklistCheck *blc;
2730 struct BlacklistCheckSwitchContext *blc_ctx;
2732 GNUNET_assert (NULL != address->transport_name);
2734 try_run_fast_ats_update (address,
2740 /* Check if plugin is available */
2741 if (NULL == (GST_plugins_find (address->transport_name)))
2743 /* we don't have the plugin for this address */
2745 GST_ats_block_address (address,
2749 if ((NULL == session) &&
2750 (GNUNET_HELLO_address_check_option (address,
2751 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2753 /* This is a inbound address and we do not have a session to use! */
2755 GST_ats_block_address (address,
2760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2761 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2762 GST_plugins_a2s (address),
2763 GNUNET_i2s (&address->peer),
2764 (unsigned int) ntohl (bandwidth_in.value__),
2765 (unsigned int) ntohl (bandwidth_out.value__));
2767 /* Perform blacklist check */
2768 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2769 blc_ctx->bandwidth_in = bandwidth_in;
2770 blc_ctx->bandwidth_out = bandwidth_out;
2771 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2774 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2775 address->transport_name,
2776 &switch_address_bl_check_cont,
2787 * Function called to send network utilization data to ATS for
2788 * each active connection.
2791 * @param key peer we send utilization data for
2792 * @param value the `struct NeighbourMapEntry *` with data to send
2793 * @return #GNUNET_OK (continue to iterate)
2796 send_utilization_data (void *cls,
2797 const struct GNUNET_PeerIdentity *key,
2800 struct NeighbourMapEntry *n = value;
2803 struct GNUNET_TIME_Relative delta;
2805 if ( (GNUNET_YES != test_connected (n)) ||
2806 (NULL == n->primary_address.address) )
2808 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2809 GNUNET_TIME_absolute_get ());
2811 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2812 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2814 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2815 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2818 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2822 GST_ats_update_utilization (n->primary_address.address,
2825 n->util_total_bytes_recv = 0;
2826 n->util_total_bytes_sent = 0;
2827 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2833 * Task transmitting utilization in a regular interval
2835 * @param cls the 'struct NeighbourMapEntry' for which we are running
2838 utilization_transmission (void *cls)
2840 util_transmission_tk = NULL;
2841 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
2842 &send_utilization_data,
2844 util_transmission_tk
2845 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2846 &utilization_transmission,
2852 * Track information about data we received from the
2853 * given address (used to notify ATS about our utilization
2854 * of allocated resources).
2856 * @param address the address we got data from
2857 * @param message the message we received (really only the size is used)
2860 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2861 const struct GNUNET_MessageHeader *message)
2863 struct NeighbourMapEntry *n;
2865 n = lookup_neighbour (&address->peer);
2868 n->util_total_bytes_recv += ntohs (message->size);
2873 * Track information about data we transmitted using the given @a
2874 * address and @a session (used to notify ATS about our utilization of
2875 * allocated resources).
2877 * @param address the address we transmitted data to
2878 * @param session session we used to transmit data
2879 * @param message the message we sent (really only the size is used)
2882 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2883 struct GNUNET_ATS_Session *session,
2886 struct NeighbourMapEntry *n;
2888 n = lookup_neighbour (&address->peer);
2891 if (n->primary_address.session != session)
2893 n->util_total_bytes_sent += size;
2898 * Master task run for every neighbour. Performs all of the time-related
2899 * activities (keep alive, send next message, disconnect if idle, finish
2900 * clean up after disconnect).
2902 * @param cls the 'struct NeighbourMapEntry' for which we are running
2905 master_task (void *cls)
2907 struct NeighbourMapEntry *n = cls;
2908 struct GNUNET_TIME_Relative delay;
2911 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2913 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2914 GNUNET_i2s (&n->id),
2915 GNUNET_TRANSPORT_ps2s(n->state),
2916 GNUNET_STRINGS_relative_time_to_string (delay,
2920 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2921 /* invalid state for master task, clean up */
2925 case GNUNET_TRANSPORT_PS_INIT_ATS:
2926 if (0 == delay.rel_value_us)
2928 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2929 "Connection to `%s' timed out waiting for ATS to provide address\n",
2930 GNUNET_i2s (&n->id));
2935 case GNUNET_TRANSPORT_PS_SYN_SENT:
2936 if (0 == delay.rel_value_us)
2938 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2939 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2940 GNUNET_i2s (&n->id));
2941 /* Remove address and request and additional one */
2942 unset_primary_address (n);
2943 set_state_and_timeout (n,
2944 GNUNET_TRANSPORT_PS_INIT_ATS,
2945 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2949 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2950 if (0 == delay.rel_value_us)
2952 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2953 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2954 GNUNET_i2s (&n->id));
2959 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2960 if (0 == delay.rel_value_us)
2962 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2963 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2964 GNUNET_i2s (&n->id));
2965 disconnect_neighbour (n);
2969 case GNUNET_TRANSPORT_PS_CONNECTED:
2970 if (0 == delay.rel_value_us)
2972 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2973 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2974 GNUNET_i2s (&n->id));
2975 disconnect_neighbour (n);
2978 try_transmission_to_peer (n);
2981 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2982 if (0 == delay.rel_value_us)
2984 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2985 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2986 GNUNET_i2s (&n->id));
2987 disconnect_neighbour (n);
2991 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2992 if (0 == delay.rel_value_us)
2994 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2995 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
2996 GNUNET_i2s (&n->id));
2997 disconnect_neighbour (n);
3001 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3002 if (0 == delay.rel_value_us)
3004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3005 "Switch failed, cleaning up alternative address\n");
3006 free_address (&n->alternative_address);
3007 set_state_and_timeout (n,
3008 GNUNET_TRANSPORT_PS_CONNECTED,
3009 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
3011 try_transmission_to_peer (n);
3014 case GNUNET_TRANSPORT_PS_DISCONNECT:
3015 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3016 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3017 GNUNET_i2s (&n->id));
3020 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3021 /* how did we get here!? */
3025 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3026 "Unhandled state `%s'\n",
3027 GNUNET_TRANSPORT_ps2s (n->state));
3031 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3032 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3033 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3035 /* if we are *now* in one of the two states, we're sending
3036 keep alive messages, so we need to consider the keepalive
3037 delay, not just the connection timeout */
3038 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3041 if (NULL == n->task)
3042 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3049 * Send a ACK message to the neighbour to confirm that we
3052 * @param n neighbour to send the ACK to
3055 send_session_ack_message (struct NeighbourMapEntry *n)
3057 struct GNUNET_MessageHeader msg;
3059 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3060 "Sending ACK message to peer `%s'\n",
3061 GNUNET_i2s (&n->id));
3063 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3064 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3065 (void) send_with_session (n,
3067 sizeof (struct GNUNET_MessageHeader),
3069 GNUNET_TIME_UNIT_FOREVER_REL,
3076 * We received a 'SESSION_SYN_ACK' message from the other peer.
3077 * Consider switching to it.
3079 * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
3080 * @param peer identity of the peer to switch the address for
3081 * @param address address of the other peer, NULL if other peer
3083 * @param session session to use (or NULL)
3084 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3087 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3088 const struct GNUNET_HELLO_Address *address,
3089 struct GNUNET_ATS_Session *session)
3091 const struct TransportSynMessage *scm;
3092 struct GNUNET_TIME_Absolute ts;
3093 struct NeighbourMapEntry *n;
3095 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3097 GNUNET_break_op (0);
3098 return GNUNET_SYSERR;
3100 GNUNET_STATISTICS_update (GST_stats,
3102 ("# SYN_ACK messages received"),
3104 scm = (const struct TransportSynMessage *) message;
3105 GNUNET_break_op (ntohl (scm->reserved) == 0);
3106 if (NULL == (n = lookup_neighbour (&address->peer)))
3108 GNUNET_STATISTICS_update (GST_stats,
3110 ("# unexpected SYN_ACK messages (no peer)"),
3112 return GNUNET_SYSERR;
3114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3115 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3116 GNUNET_i2s (&address->peer),
3117 GNUNET_TRANSPORT_ps2s (n->state),
3118 print_ack_state (n->ack_state));
3119 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3122 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3125 return GNUNET_SYSERR;
3126 case GNUNET_TRANSPORT_PS_INIT_ATS:
3127 GNUNET_STATISTICS_update (GST_stats,
3128 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3132 case GNUNET_TRANSPORT_PS_SYN_SENT:
3133 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3135 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3136 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3139 set_state_and_timeout (n,
3140 GNUNET_TRANSPORT_PS_CONNECTED,
3141 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3142 set_primary_address (n,
3143 n->primary_address.address,
3144 n->primary_address.session,
3145 n->primary_address.bandwidth_in,
3146 n->primary_address.bandwidth_out);
3147 send_session_ack_message (n);
3149 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3150 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3151 GNUNET_STATISTICS_update (GST_stats,
3152 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3156 case GNUNET_TRANSPORT_PS_CONNECTED:
3157 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3158 send_session_ack_message (n);
3160 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3161 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3162 to give us a new address... */
3163 GNUNET_STATISTICS_update (GST_stats,
3164 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3168 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3169 /* Reconnecting with new address address worked; go back to connected! */
3170 set_state_and_timeout (n,
3171 GNUNET_TRANSPORT_PS_CONNECTED,
3172 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3173 send_session_ack_message (n);
3175 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3176 /* new address worked; adopt it and go back to connected! */
3177 set_state_and_timeout (n,
3178 GNUNET_TRANSPORT_PS_CONNECTED,
3179 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3180 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3182 /* Set primary addresses */
3183 set_primary_address (n,
3184 n->alternative_address.address,
3185 n->alternative_address.session,
3186 n->alternative_address.bandwidth_in,
3187 n->alternative_address.bandwidth_out);
3188 GNUNET_STATISTICS_update (GST_stats,
3189 gettext_noop ("# Successful attempts to switch addresses"),
3193 GNUNET_HELLO_address_free (n->alternative_address.address);
3194 memset (&n->alternative_address,
3196 sizeof (n->alternative_address));
3197 send_session_ack_message (n);
3199 case GNUNET_TRANSPORT_PS_DISCONNECT:
3200 GNUNET_STATISTICS_update (GST_stats,
3202 ("# unexpected SYN_ACK messages (disconnecting)"),
3204 return GNUNET_SYSERR;
3205 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3209 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3210 "Unhandled state `%s'\n",
3211 GNUNET_TRANSPORT_ps2s (n->state));
3213 return GNUNET_SYSERR;
3220 * A session was terminated. Take note; if needed, try to get
3221 * an alternative address from ATS.
3223 * @param peer identity of the peer where the session died
3224 * @param session session that is gone
3225 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3226 * this session was not in use
3229 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3230 struct GNUNET_ATS_Session *session)
3232 struct NeighbourMapEntry *n;
3234 if (NULL == (n = lookup_neighbour (peer)))
3235 return GNUNET_NO; /* can't affect us */
3236 if (session != n->primary_address.session)
3238 /* Free alternative address */
3239 if (session == n->alternative_address.session)
3241 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3242 set_state_and_timeout (n,
3243 GNUNET_TRANSPORT_PS_CONNECTED,
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3246 "Session died, cleaning up alternative address\n");
3247 free_address (&n->alternative_address);
3249 return GNUNET_NO; /* doesn't affect us further */
3252 n->expect_latency_response = GNUNET_NO;
3253 /* The session for neighbour's primary address died */
3256 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3260 case GNUNET_TRANSPORT_PS_INIT_ATS:
3264 case GNUNET_TRANSPORT_PS_SYN_SENT:
3265 /* The session used to send the SYN terminated:
3266 * this implies a connect error*/
3267 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3268 "Failed to send SYN in CONNECT_SENT with `%s' %p: session terminated\n",
3269 GST_plugins_a2s (n->primary_address.address),
3270 n->primary_address.session);
3272 /* Destroy the address since it cannot be used */
3273 unset_primary_address (n);
3274 set_state_and_timeout (n,
3275 GNUNET_TRANSPORT_PS_INIT_ATS,
3276 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3278 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3279 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3280 /* error on inbound session; free neighbour entirely */
3283 case GNUNET_TRANSPORT_PS_CONNECTED:
3284 /* Our primary connection died, try a fast reconnect */
3285 unset_primary_address (n);
3286 set_state_and_timeout (n,
3287 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3288 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3290 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3291 /* we don't have an address, how can it go down? */
3294 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3295 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3296 "Failed to send SYN in RECONNECT_SENT with `%s' %p: session terminated\n",
3297 GST_plugins_a2s (n->primary_address.address),
3298 n->primary_address.session);
3299 /* Destroy the address since it cannot be used */
3300 unset_primary_address (n);
3301 set_state_and_timeout (n,
3302 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3303 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3305 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3306 /* primary went down while we were waiting for SYN_ACK on secondary;
3307 secondary as primary */
3309 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3310 "Connection `%s' %p to peer `%s' was terminated while switching, "
3311 "switching to alternative address `%s' %p\n",
3312 GST_plugins_a2s (n->primary_address.address),
3313 n->primary_address.session,
3315 GST_plugins_a2s (n->alternative_address.address),
3316 n->alternative_address.session);
3318 /* Destroy the inbound address since it cannot be used */
3319 free_address (&n->primary_address);
3320 n->primary_address = n->alternative_address;
3321 GNUNET_assert (GNUNET_YES ==
3322 GST_ats_is_known (n->primary_address.address,
3323 n->primary_address.session));
3324 memset (&n->alternative_address,
3326 sizeof (struct NeighbourAddress));
3327 set_state_and_timeout (n,
3328 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3329 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3331 case GNUNET_TRANSPORT_PS_DISCONNECT:
3332 unset_primary_address (n);
3334 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3335 /* neighbour was freed and plugins told to terminate session */
3338 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3339 "Unhandled state `%s'\n",
3340 GNUNET_TRANSPORT_ps2s (n->state));
3344 if (NULL != n->task)
3345 GNUNET_SCHEDULER_cancel (n->task);
3346 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3352 * We received a 'ACK' message from the other peer.
3353 * If we sent a 'SYN_ACK' last, this means we are now
3354 * connected. Otherwise, do nothing.
3356 * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
3357 * @param address address of the other peer
3358 * @param session session to use (or NULL)
3359 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3362 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3363 const struct GNUNET_HELLO_Address *address,
3364 struct GNUNET_ATS_Session *session)
3366 struct NeighbourMapEntry *n;
3368 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3370 GNUNET_break_op (0);
3371 return GNUNET_SYSERR;
3373 GNUNET_STATISTICS_update (GST_stats,
3374 gettext_noop ("# ACK messages received"),
3377 if (NULL == (n = lookup_neighbour (&address->peer)))
3379 GNUNET_break_op (0);
3380 return GNUNET_SYSERR;
3382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3383 "Received ACK for peer `%s' in state %s/%s\n",
3384 GNUNET_i2s (&address->peer),
3385 GNUNET_TRANSPORT_ps2s (n->state),
3386 print_ack_state (n->ack_state));
3388 /* Check if we are in a plausible state for having sent
3389 a SYN_ACK. If not, return, otherwise break.
3391 The remote peers sends a ACK as a response for a SYN_ACK
3395 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3396 now wait for the ACK to finally be connected
3397 - If we sent a SYN_ACK to this peer before */
3399 if ( ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3400 (ACK_SEND_ACK != n->ack_state) ) ||
3401 (NULL == n->primary_address.address) )
3403 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3404 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3405 GNUNET_i2s (&address->peer),
3406 GNUNET_TRANSPORT_ps2s (n->state),
3407 print_ack_state (n->ack_state));
3409 GNUNET_STATISTICS_update (GST_stats,
3410 gettext_noop ("# unexpected ACK messages"),
3415 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3417 /* We tried to switch addresses while being connect. We explicitly wait
3418 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3419 * so we do not want to set the address as in use! */
3422 set_state_and_timeout (n,
3423 GNUNET_TRANSPORT_PS_CONNECTED,
3424 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3426 if (NULL == n->primary_address.address) {
3428 * We are in state = PSY_SYN_RECV_ACK or ack_state = ACK_SEND_ACK, which
3429 * really means we did try (and succeed) to send a SYN and are waiting for
3431 * That suggests that the primary_address used to be non-NULL, but maybe it
3432 * got reset to NULL without the state being changed appropriately?
3438 /* Reset backoff for primary address */
3439 GST_ats_block_reset (n->primary_address.address,
3440 n->primary_address.session);
3446 * Test if we're connected to the given peer.
3448 * @param target peer to test
3449 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3452 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3454 return test_connected (lookup_neighbour (target));
3459 * Task to asynchronously run #free_neighbour().
3461 * @param cls the `struct NeighbourMapEntry` to free
3464 delayed_disconnect (void *cls)
3466 struct NeighbourMapEntry *n = cls;
3468 n->delayed_disconnect_task = NULL;
3469 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3470 "Disconnecting by request from peer %s\n",
3471 GNUNET_i2s (&n->id));
3477 * We received a quota message from the given peer,
3478 * validate and process.
3480 * @param peer sender of the message
3481 * @param msg the quota message
3484 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
3485 const struct GNUNET_MessageHeader *msg)
3487 struct NeighbourMapEntry *n;
3488 const struct GNUNET_ATS_SessionQuotaMessage *sqm;
3489 struct GNUNET_BANDWIDTH_Value32NBO last;
3491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3492 "Received QUOTA message from peer `%s'\n",
3494 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionQuotaMessage))
3496 GNUNET_break_op (0);
3497 GNUNET_STATISTICS_update (GST_stats,
3498 gettext_noop ("# quota messages ignored (malformed)"),
3503 GNUNET_STATISTICS_update (GST_stats,
3505 ("# QUOTA messages received"),
3507 sqm = (const struct GNUNET_ATS_SessionQuotaMessage *) msg;
3508 if (NULL == (n = lookup_neighbour (peer)))
3513 last = GNUNET_BANDWIDTH_value_max (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3514 GNUNET_BANDWIDTH_value_init (ntohl (sqm->quota)));
3515 if (last.value__ != n->neighbour_receive_quota.value__)
3517 n->neighbour_receive_quota = last;
3518 send_outbound_quota_to_clients (n);
3524 * We received a disconnect message from the given peer,
3525 * validate and process.
3527 * @param peer sender of the message
3528 * @param msg the disconnect message
3531 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3532 const struct GNUNET_MessageHeader *msg)
3534 struct NeighbourMapEntry *n;
3535 const struct GNUNET_ATS_SessionDisconnectMessage *sdm;
3537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3538 "Received DISCONNECT message from peer `%s'\n",
3540 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionDisconnectMessage))
3542 GNUNET_break_op (0);
3543 GNUNET_STATISTICS_update (GST_stats,
3545 ("# disconnect messages ignored (malformed)"),
3550 GNUNET_STATISTICS_update (GST_stats,
3552 ("# DISCONNECT messages received"),
3554 sdm = (const struct GNUNET_ATS_SessionDisconnectMessage *) msg;
3555 if (NULL == (n = lookup_neighbour (peer)))
3560 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3562 GNUNET_STATISTICS_update (GST_stats,
3563 gettext_noop ("# disconnect messages ignored (timestamp)"),
3568 if (0 != memcmp (peer,
3570 sizeof (struct GNUNET_PeerIdentity)))
3572 GNUNET_break_op (0);
3575 if (ntohl (sdm->purpose.size) !=
3576 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3577 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3578 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3581 "DISCONNECT message from peer `%s' has invalid size\n",
3583 GNUNET_break_op (0);
3587 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3593 "DISCONNECT message from peer `%s' cannot be verified \n",
3595 GNUNET_break_op (0);
3598 if (NULL == n->delayed_disconnect_task)
3600 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect,
3607 * Closure for the #neighbours_iterate() function.
3609 struct IteratorContext
3612 * Function to call on each connected neighbour.
3614 GST_NeighbourIterator cb;
3617 * Closure for @e cb.
3624 * Call the callback from the closure for each neighbour.
3626 * @param cls the `struct IteratorContext`
3627 * @param key the hash of the public key of the neighbour
3628 * @param value the `struct NeighbourMapEntry`
3629 * @return #GNUNET_OK (continue to iterate)
3632 neighbours_iterate (void *cls,
3633 const struct GNUNET_PeerIdentity *key,
3636 struct IteratorContext *ic = cls;
3637 struct NeighbourMapEntry *n = value;
3638 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3639 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3641 if (NULL != n->primary_address.address)
3643 bandwidth_in = n->primary_address.bandwidth_in;
3644 bandwidth_out = n->primary_address.bandwidth_out;
3648 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3649 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3653 n->primary_address.address,
3656 bandwidth_in, bandwidth_out);
3662 * Iterate over all connected neighbours.
3664 * @param cb function to call
3665 * @param cb_cls closure for @a cb
3668 GST_neighbours_iterate (GST_NeighbourIterator cb,
3671 struct IteratorContext ic;
3673 if (NULL == neighbours)
3674 return; /* can happen during shutdown */
3677 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3678 &neighbours_iterate,
3684 * If we have an active connection to the given target, it must be shutdown.
3686 * @param target peer to disconnect from
3689 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3691 struct NeighbourMapEntry *n;
3693 if (NULL == (n = lookup_neighbour (target)))
3694 return; /* not active */
3695 if (GNUNET_YES == test_connected (n))
3696 GNUNET_STATISTICS_update (GST_stats,
3697 gettext_noop ("# disconnected from peer upon explicit request"),
3700 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3701 "Forced disconnect from peer %s\n",
3702 GNUNET_i2s (target));
3703 disconnect_neighbour (n);
3708 * Obtain current address information for the given neighbour.
3711 * @return address currently used
3713 const struct GNUNET_HELLO_Address *
3714 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3716 struct NeighbourMapEntry *n;
3718 n = lookup_neighbour (peer);
3721 return n->primary_address.address;
3726 * Initialize the neighbours subsystem.
3728 * @param max_fds maximum number of fds to use
3731 GST_neighbours_start (unsigned int max_fds)
3733 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3735 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3736 &utilization_transmission,
3742 * Disconnect from the given neighbour.
3745 * @param key hash of neighbour's public key (not used)
3746 * @param value the `struct NeighbourMapEntry` of the neighbour
3747 * @return #GNUNET_OK (continue to iterate)
3750 disconnect_all_neighbours (void *cls,
3751 const struct GNUNET_PeerIdentity *key,
3754 struct NeighbourMapEntry *n = value;
3756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3757 "Disconnecting peer `%4s' during shutdown\n",
3758 GNUNET_i2s (&n->id));
3765 * Cleanup the neighbours subsystem.
3768 GST_neighbours_stop ()
3770 if (NULL == neighbours)
3772 if (NULL != util_transmission_tk)
3774 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3775 util_transmission_tk = NULL;
3777 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3778 &disconnect_all_neighbours,
3780 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3785 /* end of file gnunet-service-transport_neighbours.c */