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 it
6 under the terms of the GNU Affero General Public License as published
7 by the Free Software Foundation, either version 3 of the License,
8 or (at your 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 Affero General Public License for more details.
15 You should have received a copy of the GNU Affero General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 * @file transport/gnunet-service-transport_neighbours.c
21 * @brief neighbour management
22 * @author Christian Grothoff
25 #include "gnunet_ats_service.h"
26 #include "gnunet-service-transport_ats.h"
27 #include "gnunet-service-transport_neighbours.h"
28 #include "gnunet-service-transport_manipulation.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_constants.h"
34 #include "transport.h"
37 * Experimental option to ignore SessionQuotaMessages from
40 #define IGNORE_INBOUND_QUOTA GNUNET_YES
43 * Size of the neighbour hash map.
45 #define NEIGHBOUR_TABLE_SIZE 256
48 * Time we give plugin to transmit DISCONNECT message before the
49 * neighbour entry self-destructs.
51 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
54 * How often must a peer violate bandwidth quotas before we start
55 * to simply drop its messages?
57 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
60 * How long are we willing to wait for a response from ATS before timing out?
62 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
65 * How long are we willing to wait for an ACK from the other peer before
66 * giving up on our connect operation?
68 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
71 * How long are we willing to wait for a successful reconnect if
72 * an existing connection went down? Much shorter than the
73 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
74 * higher layers about the disconnect during this period.
76 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
79 * Interval to send utilization data
81 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_UNIT_SECONDS
84 * State describing which kind a reply this neighbour should send
89 * We did not receive a SYN message for this neighbour
94 * The neighbour received a SYN message and has to send a SYN_ACK
100 * The neighbour sent a SYN_ACK message and has to send a ACK
107 GNUNET_NETWORK_STRUCT_BEGIN
110 * Message a peer sends to another to indicate that it intends to
111 * setup a connection/session for data exchange. A 'SESSION_SYN'
112 * should be answered with a 'SESSION_SYN_ACK' with the same body
113 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
114 * a 'ACK'. Once the 'ACK' is received, both peers
115 * should be connected.
117 struct TransportSynMessage
120 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
121 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
123 struct GNUNET_MessageHeader header;
128 uint32_t reserved GNUNET_PACKED;
131 * Absolute time at the sender. Only the most recent connect
132 * message implies which session is preferred by the sender.
134 struct GNUNET_TIME_AbsoluteNBO timestamp;
140 * Message a peer sends to another when connected to indicate that a
141 * session is in use and the peer is still alive or to respond to a keep alive.
142 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
143 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
144 * When the keep alive response with type is received, transport service
145 * will call the respective plugin to update the session timeout
147 struct GNUNET_ATS_SessionKeepAliveMessage
150 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
151 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
153 struct GNUNET_MessageHeader header;
156 * A nonce to identify the session the keep alive is used for
158 uint32_t nonce GNUNET_PACKED;
163 * Message a peer sends to another when connected to indicate that
164 * the other peer should limit transmissions to the indicated
167 struct GNUNET_ATS_SessionQuotaMessage
170 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA.
172 struct GNUNET_MessageHeader header;
175 * Quota to use (for sending), in bytes per second.
177 uint32_t quota GNUNET_PACKED;
182 * Message we send to the other peer to notify him that we intentionally
183 * are disconnecting (to reduce timeouts). This is just a friendly
184 * notification, peers must not rely on always receiving disconnect
187 struct GNUNET_ATS_SessionDisconnectMessage
190 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
192 struct GNUNET_MessageHeader header;
197 uint32_t reserved GNUNET_PACKED;
200 * Purpose of the signature. Extends over the timestamp.
201 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
203 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
206 * Absolute time at the sender. Only the most recent connect
207 * message implies which session is preferred by the sender.
209 struct GNUNET_TIME_AbsoluteNBO timestamp;
212 * Public key of the sender.
214 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
217 * Signature of the peer that sends us the disconnect. Only
218 * valid if the timestamp is AFTER the timestamp from the
219 * corresponding 'SYN' message.
221 struct GNUNET_CRYPTO_EddsaSignature signature;
225 GNUNET_NETWORK_STRUCT_END
229 * For each neighbour we keep a list of messages
230 * that we still want to transmit to the neighbour.
236 * This is a doubly linked list.
238 struct MessageQueue *next;
241 * This is a doubly linked list.
243 struct MessageQueue *prev;
246 * Function to call once we're done.
248 GST_NeighbourSendContinuation cont;
251 * Closure for @e cont
256 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
257 * stuck together in memory. Allocated at the end of this struct.
259 const char *message_buf;
262 * Size of the message buf
264 size_t message_buf_size;
267 * At what time should we fail?
269 struct GNUNET_TIME_Absolute timeout;
275 * A possible address we could use to communicate with a neighbour.
277 struct NeighbourAddress
281 * Active session for this address.
283 struct GNUNET_ATS_Session *session;
286 * Network-level address information.
288 struct GNUNET_HELLO_Address *address;
291 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
292 * peer for this address. Use to check that the ACK is in response
293 * to our most recent 'SYN'.
295 struct GNUNET_TIME_Absolute connect_timestamp;
298 * Inbound bandwidth from ATS for this address.
300 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
303 * Outbound bandwidth from ATS for this address.
305 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
308 * Did we tell ATS that this is our 'active' address?
313 * The current nonce sent in the last keep alive messages
315 uint32_t keep_alive_nonce;
320 * Entry in neighbours.
322 struct NeighbourMapEntry
326 * Head of list of messages we would like to send to this peer;
327 * must contain at most one message per client.
329 struct MessageQueue *messages_head;
332 * Tail of list of messages we would like to send to this peer; must
333 * contain at most one message per client.
335 struct MessageQueue *messages_tail;
338 * Are we currently trying to send a message? If so, which one?
340 struct MessageQueue *is_active;
343 * Primary address we currently use to communicate with the neighbour.
345 struct NeighbourAddress primary_address;
348 * Alternative address currently under consideration for communicating
349 * with the neighbour.
351 struct NeighbourAddress alternative_address;
354 * Identity of this neighbour.
356 struct GNUNET_PeerIdentity id;
359 * Main task that drives this peer (timeouts, keepalives, etc.).
360 * Always runs the #master_task().
362 struct GNUNET_SCHEDULER_Task *task;
365 * Task to disconnect neighbour after we received a DISCONNECT message
367 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
370 * At what time should we sent the next keep-alive message?
372 struct GNUNET_TIME_Absolute keep_alive_time;
375 * At what time did we sent the last keep-alive message? Used
376 * to calculate round-trip time ("latency").
378 struct GNUNET_TIME_Absolute last_keep_alive_time;
381 * Timestamp we should include in our next SYN_ACK message.
382 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
383 * our SYN_ACK message.
385 struct GNUNET_TIME_Absolute connect_ack_timestamp;
388 * ATS address suggest handle
390 struct GNUNET_ATS_ConnectivitySuggestHandle *suggest_handle;
393 * Time where we should cut the connection (timeout) if we don't
394 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
395 * if we are in #GNUNET_TRANSPORT_PS_CONNECTED).
397 struct GNUNET_TIME_Absolute timeout;
400 * Tracker for inbound bandwidth.
402 struct GNUNET_BANDWIDTH_Tracker in_tracker;
405 * How often has the other peer (recently) violated the inbound
406 * traffic limit? Incremented by 10 per violation, decremented by 1
407 * per non-violation (for each time interval).
409 unsigned int quota_violation_count;
412 * Latest quota the other peer send us in bytes per second.
413 * We should not send more, least the other peer throttle
414 * receiving our traffic.
416 struct GNUNET_BANDWIDTH_Value32NBO neighbour_receive_quota;
419 * The current state of the peer.
421 enum GNUNET_TRANSPORT_PeerState state;
424 * Did we sent an KEEP_ALIVE message and are we expecting a response?
426 int expect_latency_response;
429 * When a peer wants to connect we have to reply to the 1st SYN message
430 * with a SYN_ACK message. But sometime we cannot send this message
431 * immediately since we do not have an address and then we have to remember
432 * to send this message as soon as we have an address.
434 * Flag to set if we still need to send a SYN_ACK message to the other peer
435 * (once we have an address to use and the peer has been allowed by our
436 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
437 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
438 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
439 * 'ACK' (regardless of what our own state machine might say).
441 enum GST_ACK_State ack_state;
444 * Tracking utilization of outbound bandwidth
446 uint32_t util_total_bytes_sent;
449 * Tracking utilization of inbound bandwidth
451 uint32_t util_total_bytes_recv;
454 * Date of last utilization transmission
456 struct GNUNET_TIME_Absolute last_util_transmission;
461 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
463 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
466 * List of pending blacklist checks: head
468 static struct BlacklistCheckSwitchContext *pending_bc_head;
471 * List of pending blacklist checks: tail
473 static struct BlacklistCheckSwitchContext *pending_bc_tail;
476 * counter for connected neighbours
478 static unsigned int neighbours_connected;
481 * Number of bytes we have currently queued for transmission.
483 static unsigned long long bytes_in_send_queue;
486 * Task transmitting utilization data
488 static struct GNUNET_SCHEDULER_Task *util_transmission_tk;
492 * Convert the given ACK state to a string.
495 * @return corresponding human-readable string
498 print_ack_state (enum GST_ACK_State s)
503 case ACK_SEND_SYN_ACK:
504 return "SEND_SYN_ACK";
515 * Send information about a new outbound quota to our clients.
516 * Note that the outbound quota is enforced client-side (i.e.
517 * in libgnunettransport).
519 * @param n affected peer
522 send_outbound_quota_to_clients (struct NeighbourMapEntry *n)
524 struct QuotaSetMessage q_msg;
525 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
527 if (! GNUNET_TRANSPORT_is_connected (n->state))
529 #if IGNORE_INBOUND_QUOTA
530 bandwidth_min = n->primary_address.bandwidth_out;
532 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
533 n->neighbour_receive_quota);
536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
537 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
538 ntohl (bandwidth_min.value__),
539 GNUNET_i2s (&n->id));
540 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
541 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
542 q_msg.quota = bandwidth_min;
544 GST_clients_broadcast (&q_msg.header,
550 * Notify our clients that another peer connected to us.
552 * @param n the peer that connected
555 neighbours_connect_notification (struct NeighbourMapEntry *n)
557 size_t len = sizeof(struct ConnectInfoMessage);
558 char buf[len] GNUNET_ALIGN;
559 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
560 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
562 #if IGNORE_INBOUND_QUOTA
563 bandwidth_min = n->primary_address.bandwidth_out;
565 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
566 n->neighbour_receive_quota);
568 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
569 "We are now connected to peer `%s'\n",
570 GNUNET_i2s (&n->id));
571 connect_msg->header.size = htons (sizeof(buf));
572 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
573 connect_msg->id = n->id;
574 connect_msg->quota_in = n->primary_address.bandwidth_in;
575 connect_msg->quota_out = bandwidth_min;
576 GST_clients_broadcast (&connect_msg->header,
582 * Notify our clients (and manipulation) that a peer disconnected from
585 * @param n the peer that disconnected
588 neighbours_disconnect_notification (struct NeighbourMapEntry *n)
590 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
591 "Peer `%s' disconnected\n",
592 GNUNET_i2s (&n->id));
593 GST_manipulation_peer_disconnect (&n->id);
594 GST_clients_broadcast_disconnect (&n->id);
599 * Notify transport clients that a neighbour peer changed its active
602 * @param peer identity of the peer
603 * @param address address possibly NULL if peer is not connected
604 * @param state current state this peer is in
605 * @param state_timeout timeout for the current state of the peer
606 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
607 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
610 neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer,
611 const struct GNUNET_HELLO_Address *address,
612 enum GNUNET_TRANSPORT_PeerState state,
613 struct GNUNET_TIME_Absolute state_timeout,
614 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
615 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
618 (void) 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;
1000 n = lookup_neighbour (target);
1002 return; /* already gone */
1003 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1004 return; /* have created a fresh entry since */
1005 if (NULL != n->task)
1006 GNUNET_SCHEDULER_cancel (n->task);
1007 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1012 * Transmit a DISCONNECT message to the other peer.
1014 * @param n neighbour to send DISCONNECT message.
1017 send_disconnect (struct NeighbourMapEntry *n)
1019 struct GNUNET_ATS_SessionDisconnectMessage disconnect_msg;
1021 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1022 "Sending DISCONNECT message to peer `%4s'\n",
1023 GNUNET_i2s (&n->id));
1024 disconnect_msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionDisconnectMessage));
1025 disconnect_msg.header.type =
1026 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1027 disconnect_msg.reserved = htonl (0);
1028 disconnect_msg.purpose.size =
1029 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1030 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1031 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1032 disconnect_msg.purpose.purpose =
1033 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1034 disconnect_msg.timestamp =
1035 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1036 disconnect_msg.public_key = GST_my_identity.public_key;
1037 GNUNET_assert (GNUNET_OK ==
1038 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1039 &disconnect_msg.purpose,
1040 &disconnect_msg.signature));
1042 (void) send_with_session (n,
1044 sizeof (disconnect_msg),
1046 GNUNET_TIME_UNIT_FOREVER_REL,
1048 &send_disconnect_cont,
1050 GNUNET_STATISTICS_update (GST_stats,
1051 gettext_noop ("# DISCONNECT messages sent"),
1058 * Disconnect from the given neighbour, clean up the record.
1060 * @param n neighbour to disconnect from
1063 disconnect_neighbour (struct NeighbourMapEntry *n)
1065 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1066 "Disconnecting from peer %s in state %s\n",
1067 GNUNET_i2s (&n->id),
1068 GNUNET_TRANSPORT_ps2s (n->state));
1069 /* depending on state, notify neighbour and/or upper layers of this peer
1073 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1074 case GNUNET_TRANSPORT_PS_INIT_ATS:
1075 /* other peer is completely unaware of us, no need to send DISCONNECT */
1078 case GNUNET_TRANSPORT_PS_SYN_SENT:
1079 send_disconnect (n);
1080 set_state_and_timeout (n,
1081 GNUNET_TRANSPORT_PS_DISCONNECT,
1082 GNUNET_TIME_UNIT_FOREVER_ABS);
1084 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1085 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1088 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1089 /* we DID ACK the other peer's request, must send DISCONNECT */
1090 send_disconnect (n);
1091 set_state_and_timeout (n,
1092 GNUNET_TRANSPORT_PS_DISCONNECT,
1093 GNUNET_TIME_UNIT_FOREVER_ABS);
1095 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1096 case GNUNET_TRANSPORT_PS_CONNECTED:
1097 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1098 /* we are currently connected, need to send disconnect and do
1099 internal notifications and update statistics */
1100 send_disconnect (n);
1101 set_state_and_timeout (n,
1102 GNUNET_TRANSPORT_PS_DISCONNECT,
1103 GNUNET_TIME_UNIT_FOREVER_ABS);
1105 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1106 /* Disconnecting while waiting for an ATS address to reconnect,
1107 * cannot send DISCONNECT */
1110 case GNUNET_TRANSPORT_PS_DISCONNECT:
1111 /* already disconnected, ignore */
1113 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1114 /* already cleaned up, how did we get here!? */
1118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1119 "Unhandled state `%s'\n",
1120 GNUNET_TRANSPORT_ps2s (n->state));
1124 /* schedule timeout to clean up */
1125 if (NULL != n->task)
1126 GNUNET_SCHEDULER_cancel (n->task);
1127 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1134 * Change the incoming quota for the given peer. Updates
1135 * our own receive rate and informs the neighbour about
1138 * @param n neighbour entry to change quota for
1139 * @param quota new quota
1140 * @return #GNUNET_YES if @a n is still valid, #GNUNET_NO if
1144 set_incoming_quota (struct NeighbourMapEntry *n,
1145 struct GNUNET_BANDWIDTH_Value32NBO quota)
1147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1148 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
1149 ntohl (quota.value__), GNUNET_i2s (&n->id));
1150 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
1152 if (0 != ntohl (quota.value__))
1154 struct GNUNET_ATS_SessionQuotaMessage sqm;
1156 sqm.header.size = htons (sizeof (struct GNUNET_ATS_SessionQuotaMessage));
1157 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA);
1158 sqm.quota = quota.value__;
1159 if (NULL != n->primary_address.session)
1160 (void) send_with_session (n,
1164 GNUNET_TIME_UNIT_FOREVER_REL,
1169 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1170 "Disconnecting peer `%s' due to SET_QUOTA\n",
1171 GNUNET_i2s (&n->id));
1172 if (GNUNET_YES == test_connected (n))
1173 GNUNET_STATISTICS_update (GST_stats,
1174 gettext_noop ("# disconnects due to quota of 0"),
1176 disconnect_neighbour (n);
1182 * Initialize the primary address of a neighbour
1184 * @param n the neighbour
1185 * @param address address of the other peer, NULL if other peer
1187 * @param session session to use (or NULL, in which case an
1188 * address must be setup)
1189 * @param bandwidth_in inbound quota to be used when connection is up
1190 * @param bandwidth_out outbound quota to be used when connection is up
1193 set_primary_address (struct NeighbourMapEntry *n,
1194 const struct GNUNET_HELLO_Address *address,
1195 struct GNUNET_ATS_Session *session,
1196 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1197 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1199 if (session == n->primary_address.session)
1201 GST_validation_set_address_use (n->primary_address.address,
1203 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
1205 n->primary_address.bandwidth_in = bandwidth_in;
1207 set_incoming_quota (n,
1211 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
1213 n->primary_address.bandwidth_out = bandwidth_out;
1214 send_outbound_quota_to_clients (n);
1218 if ( (NULL != n->primary_address.address) &&
1219 (0 == GNUNET_HELLO_address_cmp (address,
1220 n->primary_address.address)) )
1225 if (NULL == session)
1228 GST_ats_block_address (address,
1232 if (NULL != n->primary_address.address)
1234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1235 "Replacing existing primary address with another one\n");
1236 free_address (&n->primary_address);
1238 n->primary_address.address = GNUNET_HELLO_address_copy (address);
1239 n->primary_address.bandwidth_in = bandwidth_in;
1240 n->primary_address.bandwidth_out = bandwidth_out;
1241 n->primary_address.session = session;
1242 n->primary_address.keep_alive_nonce = 0;
1243 GNUNET_assert (GNUNET_YES ==
1244 GST_ats_is_known (n->primary_address.address,
1245 n->primary_address.session));
1246 /* subsystems about address use */
1247 GST_validation_set_address_use (n->primary_address.address,
1250 set_incoming_quota (n,
1253 send_outbound_quota_to_clients (n);
1254 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1255 "Neighbour `%s' switched to address `%s'\n",
1256 GNUNET_i2s (&n->id),
1257 GST_plugins_a2s(address));
1259 neighbours_changed_notification (&n->id,
1260 n->primary_address.address,
1263 n->primary_address.bandwidth_in,
1264 n->primary_address.bandwidth_out);
1269 * We're done with our transmission attempt, continue processing.
1271 * @param cls the `struct MessageQueue` of the message
1272 * @param receiver intended receiver
1273 * @param success whether it worked or not
1274 * @param size_payload bytes payload sent
1275 * @param physical bytes sent on wire
1278 transmit_send_continuation (void *cls,
1279 const struct GNUNET_PeerIdentity *receiver,
1281 size_t size_payload,
1284 struct MessageQueue *mq = cls;
1285 struct NeighbourMapEntry *n;
1287 if (NULL == (n = lookup_neighbour (receiver)))
1289 if (NULL != mq->cont)
1290 mq->cont (mq->cont_cls,
1291 GNUNET_SYSERR /* not connected */,
1295 return; /* disconnect or other error while transmitting, can happen */
1297 if (n->is_active == mq)
1299 /* this is still "our" neighbour, remove us from its queue
1300 and allow it to send the next message now */
1301 n->is_active = NULL;
1302 if (NULL != n->task)
1303 GNUNET_SCHEDULER_cancel (n->task);
1304 n->task = GNUNET_SCHEDULER_add_now (&master_task,
1307 if (bytes_in_send_queue < mq->message_buf_size)
1309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1310 "Bytes_in_send_queue `%llu', Message_size %u, result: %s, payload %u, on wire %u\n",
1311 bytes_in_send_queue,
1312 (unsigned int) mq->message_buf_size,
1313 (GNUNET_OK == success) ? "OK" : "FAIL",
1314 (unsigned int) size_payload,
1315 (unsigned int) physical);
1319 GNUNET_break (size_payload == mq->message_buf_size);
1320 bytes_in_send_queue -= mq->message_buf_size;
1321 GNUNET_STATISTICS_set (GST_stats,
1322 gettext_noop ("# bytes in message queue for other peers"),
1323 bytes_in_send_queue,
1325 if (GNUNET_OK == success)
1326 GNUNET_STATISTICS_update (GST_stats,
1327 gettext_noop ("# messages transmitted to other peers"),
1331 GNUNET_STATISTICS_update (GST_stats,
1333 ("# transmission failures for messages to other peers"),
1335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1336 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1337 GNUNET_i2s (receiver),
1338 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1339 (unsigned int) mq->message_buf_size,
1340 (success == GNUNET_OK) ? "success" : "FAILURE");
1341 if (NULL != mq->cont)
1342 mq->cont (mq->cont_cls,
1351 * Check the message list for the given neighbour and if we can
1352 * send a message, do so. This function should only be called
1353 * if the connection is at least generally ready for transmission.
1354 * While we will only send one message at a time, no bandwidth
1355 * quota management is performed here. If a message was given to
1356 * the plugin, the continuation will automatically re-schedule
1357 * the 'master' task once the next message might be transmitted.
1359 * @param n target peer for which to transmit
1362 try_transmission_to_peer (struct NeighbourMapEntry *n)
1364 struct MessageQueue *mq;
1365 struct GNUNET_TIME_Relative timeout;
1367 if (NULL == n->primary_address.address)
1369 /* no address, why are we here? */
1373 if ((0 == n->primary_address.address->address_length) &&
1374 (NULL == n->primary_address.session))
1376 /* no address, why are we here? */
1380 if (NULL != n->is_active)
1382 /* transmission already pending */
1386 /* timeout messages from the queue that are past their due date */
1387 while (NULL != (mq = n->messages_head))
1389 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1390 if (timeout.rel_value_us > 0)
1392 GNUNET_STATISTICS_update (GST_stats,
1393 gettext_noop ("# messages timed out while in transport queue"),
1396 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1400 transmit_send_continuation (mq,
1403 mq->message_buf_size,
1407 return; /* no more messages */
1408 if (NULL == n->primary_address.address)
1410 /* transmit_send_continuation() caused us to drop session,
1411 can't try transmission anymore. */
1416 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1422 "Giving message with %u bytes to plugin session %p\n",
1423 (unsigned int) mq->message_buf_size,
1424 n->primary_address.session);
1425 (void) send_with_session (n,
1427 mq->message_buf_size,
1431 &transmit_send_continuation,
1437 * Send keepalive message to the neighbour. Must only be called
1438 * if we are on 'connected' state or while trying to switch addresses.
1439 * Will internally determine if a keepalive is truly needed (so can
1440 * always be called).
1442 * @param n neighbour that went idle and needs a keepalive
1445 send_keepalive (struct NeighbourMapEntry *n)
1447 struct GNUNET_ATS_SessionKeepAliveMessage m;
1448 struct GNUNET_TIME_Relative timeout;
1451 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1452 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state));
1453 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1454 return; /* no keepalive needed at this time */
1456 nonce = 0; /* 0 indicates 'not set' */
1458 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1462 "Sending KEEPALIVE to peer `%s' with nonce %u\n",
1463 GNUNET_i2s (&n->id),
1465 m.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1466 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1467 m.nonce = htonl (nonce);
1469 timeout = send_with_session (n,
1472 UINT32_MAX /* priority */,
1473 GNUNET_TIME_UNIT_FOREVER_REL,
1476 GNUNET_STATISTICS_update (GST_stats,
1477 gettext_noop ("# KEEPALIVES sent"),
1480 n->primary_address.keep_alive_nonce = nonce;
1481 n->expect_latency_response = GNUNET_YES;
1482 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1483 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1488 * Keep the connection to the given neighbour alive longer,
1489 * we received a KEEPALIVE (or equivalent); send a response.
1491 * @param neighbour neighbour to keep alive (by sending keep alive response)
1492 * @param m the keep alive message containing the nonce to respond to
1495 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1496 const struct GNUNET_MessageHeader *m)
1498 struct NeighbourMapEntry *n;
1499 const struct GNUNET_ATS_SessionKeepAliveMessage *msg_in;
1500 struct GNUNET_ATS_SessionKeepAliveMessage msg;
1502 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1504 GNUNET_break_op (0);
1508 msg_in = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1509 if (NULL == (n = lookup_neighbour (neighbour)))
1511 GNUNET_STATISTICS_update (GST_stats,
1513 ("# KEEPALIVE messages discarded (peer unknown)"),
1517 if (NULL == n->primary_address.session)
1519 GNUNET_STATISTICS_update (GST_stats,
1521 ("# KEEPALIVE messages discarded (no session)"),
1526 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1527 "Received KEEPALIVE request from peer `%s' with nonce %u\n",
1528 GNUNET_i2s (&n->id),
1529 ntohl (msg_in->nonce));
1530 GNUNET_STATISTICS_update (GST_stats,
1531 gettext_noop ("# KEEPALIVES received in good order"),
1535 /* send reply to allow neighbour to measure latency */
1536 msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1537 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1538 msg.nonce = msg_in->nonce;
1539 (void) send_with_session (n,
1541 sizeof (struct GNUNET_ATS_SessionKeepAliveMessage),
1542 UINT32_MAX /* priority */,
1543 GNUNET_TIME_UNIT_FOREVER_REL,
1550 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1551 * latency to this peer. Pass the updated information (existing ats
1552 * plus calculated latency) to ATS.
1554 * @param neighbour neighbour to keep alive
1555 * @param m the message containing the keep alive response
1558 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1559 const struct GNUNET_MessageHeader *m)
1561 struct NeighbourMapEntry *n;
1562 const struct GNUNET_ATS_SessionKeepAliveMessage *msg;
1563 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1564 struct GNUNET_TIME_Relative latency;
1566 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1568 GNUNET_break_op (0);
1572 msg = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1573 if (NULL == (n = lookup_neighbour (neighbour)))
1575 GNUNET_STATISTICS_update (GST_stats,
1576 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not connected)"),
1581 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1582 (GNUNET_YES != n->expect_latency_response) )
1584 GNUNET_STATISTICS_update (GST_stats,
1585 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not expected)"),
1590 if (NULL == n->primary_address.address)
1592 GNUNET_STATISTICS_update (GST_stats,
1593 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (address changed)"),
1598 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1600 if (0 == n->primary_address.keep_alive_nonce)
1601 GNUNET_STATISTICS_update (GST_stats,
1602 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (no nonce)"),
1606 GNUNET_STATISTICS_update (GST_stats,
1607 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (bad nonce)"),
1612 GNUNET_STATISTICS_update (GST_stats,
1613 gettext_noop ("# KEEPALIVE_RESPONSEs received (OK)"),
1618 /* Update session timeout here */
1619 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1622 "Updating session for peer `%s' for session %p\n",
1623 GNUNET_i2s (&n->id),
1624 n->primary_address.session);
1625 papi->update_session_timeout (papi->cls,
1627 n->primary_address.session);
1634 n->primary_address.keep_alive_nonce = 0;
1635 n->expect_latency_response = GNUNET_NO;
1636 set_state_and_timeout (n,
1638 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1640 latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1642 "Received KEEPALIVE_RESPONSE from peer `%s', latency is %s\n",
1643 GNUNET_i2s (&n->id),
1644 GNUNET_STRINGS_relative_time_to_string (latency,
1646 GST_ats_update_delay (n->primary_address.address,
1647 GNUNET_TIME_relative_divide (latency,
1653 * We have received a message from the given sender. How long should
1654 * we delay before receiving more? (Also used to keep the peer marked
1657 * @param sender sender of the message
1658 * @param size size of the message
1659 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1660 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1661 * #GNUNET_SYSERR if the connection is not fully up yet
1662 * @return how long to wait before reading more from this sender
1664 struct GNUNET_TIME_Relative
1665 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
1669 struct NeighbourMapEntry *n;
1670 struct GNUNET_TIME_Relative ret;
1672 if (NULL == neighbours)
1674 *do_forward = GNUNET_NO;
1675 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1677 if (NULL == (n = lookup_neighbour (sender)))
1679 GNUNET_STATISTICS_update (GST_stats,
1680 gettext_noop ("# messages discarded due to lack of neighbour record"),
1683 *do_forward = GNUNET_NO;
1684 return GNUNET_TIME_UNIT_ZERO;
1686 if (! test_connected (n))
1688 *do_forward = GNUNET_SYSERR;
1689 return GNUNET_TIME_UNIT_ZERO;
1691 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1693 n->quota_violation_count++;
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1696 n->in_tracker.available_bytes_per_s__,
1697 n->quota_violation_count);
1698 /* Discount 32k per violation */
1699 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1703 if (n->quota_violation_count > 0)
1705 /* try to add 32k back */
1706 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1707 n->quota_violation_count--;
1710 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1712 GNUNET_STATISTICS_update (GST_stats,
1714 ("# bandwidth quota violations by other peers"),
1716 *do_forward = GNUNET_NO;
1717 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1719 *do_forward = GNUNET_YES;
1720 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1721 if (ret.rel_value_us > 0)
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "Throttling read (%lld bytes excess at %u b/s), waiting %s before reading more.\n",
1725 (long long) n->in_tracker.consumption_since_last_update__,
1726 (unsigned int) n->in_tracker.available_bytes_per_s__,
1727 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1728 GNUNET_STATISTICS_update (GST_stats,
1729 gettext_noop ("# ms throttling suggested"),
1730 (int64_t) ret.rel_value_us / 1000LL,
1738 * Transmit a message to the given target using the active connection.
1740 * @param target destination
1741 * @param msg message to send
1742 * @param msg_size number of bytes in msg
1743 * @param timeout when to fail with timeout
1744 * @param cont function to call when done
1745 * @param cont_cls closure for @a cont
1748 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
1751 struct GNUNET_TIME_Relative timeout,
1752 GST_NeighbourSendContinuation cont,
1755 struct NeighbourMapEntry *n;
1756 struct MessageQueue *mq;
1758 /* All ove these cases should never happen; they are all API violations.
1759 But we check anyway, just to be sure. */
1760 if (NULL == (n = lookup_neighbour (target)))
1770 if (GNUNET_YES != test_connected (n))
1780 bytes_in_send_queue += msg_size;
1781 GNUNET_STATISTICS_set (GST_stats,
1783 ("# bytes in message queue for other peers"),
1784 bytes_in_send_queue, GNUNET_NO);
1785 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1787 mq->cont_cls = cont_cls;
1788 GNUNET_memcpy (&mq[1], msg, msg_size);
1789 mq->message_buf = (const char *) &mq[1];
1790 mq->message_buf_size = msg_size;
1791 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1794 "Enqueueing %u bytes to send to peer %s\n",
1795 (unsigned int) msg_size,
1796 GNUNET_i2s (target));
1797 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
1800 if (NULL != n->task)
1801 GNUNET_SCHEDULER_cancel (n->task);
1802 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1807 * Continuation called from our attempt to transmitted our
1808 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN to the specified @a
1809 * target. Continue processing based on the @a result. Specifically,
1810 * if we failed to transmit, discard the address we used.
1813 * @param target which peer received the transmission
1814 * @param result #GNUNET_OK if sending worked
1815 * @param size_payload how many bytes of payload were sent (ignored)
1816 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1819 send_session_syn_cont (void *cls,
1820 const struct GNUNET_PeerIdentity *target,
1822 size_t size_payload,
1823 size_t size_on_wire)
1825 struct NeighbourMapEntry *n;
1828 (void) size_payload;
1829 (void) size_on_wire;
1830 n = lookup_neighbour (target);
1833 /* SYN continuation was called after neighbor was freed,
1834 * for example due to a time out for the state or the session
1835 * used was already terminated: nothing to do here... */
1839 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1840 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1841 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1843 /* SYN continuation was called after neighbor changed state,
1844 * for example due to a time out for the state or the session
1845 * used was already terminated: nothing to do here... */
1848 if (GNUNET_OK == result)
1851 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1852 _("Failed to send SYN message to peer `%s'\n"),
1853 GNUNET_i2s (target));
1855 case GNUNET_TRANSPORT_PS_SYN_SENT:
1856 /* Remove address and request an additional one */
1857 unset_primary_address (n);
1858 set_state_and_timeout (n,
1859 GNUNET_TRANSPORT_PS_INIT_ATS,
1860 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1862 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1863 /* Remove address and request an additional one */
1864 unset_primary_address (n);
1865 set_state_and_timeout (n,
1866 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1867 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1869 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1870 /* Remove address and request and go back to primary address */
1871 GNUNET_STATISTICS_update (GST_stats,
1872 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN CONT)"),
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "Switch failed, cleaning up alternative address\n");
1877 free_address (&n->alternative_address);
1878 set_state_and_timeout (n,
1879 GNUNET_TRANSPORT_PS_CONNECTED,
1880 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1883 disconnect_neighbour (n);
1890 * Send a SYN message via the given address.
1892 * @param na address to use
1895 send_syn (struct NeighbourAddress *na)
1897 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1898 struct TransportSynMessage connect_msg;
1899 struct NeighbourMapEntry *n;
1901 GNUNET_assert (NULL != na->session);
1902 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1903 "Sending SYN message to peer `%s' at %s\n",
1904 GNUNET_i2s (&na->address->peer),
1905 GST_plugins_a2s (na->address));
1907 papi = GST_plugins_find (na->address->transport_name);
1908 GNUNET_assert (NULL != papi);
1909 GNUNET_STATISTICS_update (GST_stats,
1911 ("# SYN messages sent"),
1913 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1914 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1915 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1916 connect_msg.reserved = htonl (0);
1917 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1919 papi->send (papi->cls,
1921 (const char *) &connect_msg,
1922 sizeof (struct TransportSynMessage),
1924 SETUP_CONNECTION_TIMEOUT,
1925 &send_session_syn_cont, NULL))
1927 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1928 _("Failed to transmit SYN message to %s\n"),
1929 GST_plugins_a2s (na->address));
1930 n = lookup_neighbour (&na->address->peer);
1937 case GNUNET_TRANSPORT_PS_SYN_SENT:
1938 /* Remove address and request and additional one */
1939 GNUNET_assert (na == &n->primary_address);
1940 unset_primary_address (n);
1941 set_state_and_timeout (n,
1942 GNUNET_TRANSPORT_PS_INIT_ATS,
1943 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1944 /* Hard failure to send the SYN message with this address:
1945 Destroy address and session */
1947 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1948 /* Remove address and request an additional one */
1949 GNUNET_assert (na == &n->primary_address);
1950 unset_primary_address (n);
1951 set_state_and_timeout (n,
1952 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1953 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1955 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1956 GNUNET_assert (na == &n->alternative_address);
1957 GNUNET_STATISTICS_update (GST_stats,
1958 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN)"),
1961 /* Remove address and request an additional one */
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1963 "Switch failed, cleaning up alternative address\n");
1964 free_address (&n->alternative_address);
1965 set_state_and_timeout (n,
1966 GNUNET_TRANSPORT_PS_CONNECTED,
1967 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1971 disconnect_neighbour (n);
1976 GST_neighbours_notify_data_sent (na->address,
1978 sizeof (struct TransportSynMessage));
1983 * Continuation called from our attempt to transmitted our
1984 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK to the specified @a
1985 * target. Continue processing based on the @a result. Specifically,
1986 * if we failed to transmit, discard the address we used.
1989 * @param target which peer received the transmission
1990 * @param result #GNUNET_OK if sending worked
1991 * @param size_payload how many bytes of payload were sent (ignored)
1992 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1995 send_session_syn_ack_cont (void *cls,
1996 const struct GNUNET_PeerIdentity *target,
1998 size_t size_payload,
1999 size_t size_on_wire)
2001 struct NeighbourMapEntry *n;
2004 (void) size_payload;
2005 (void) size_on_wire;
2006 n = lookup_neighbour (target);
2009 /* SYN_ACK continuation was called after neighbor was freed,
2010 * for example due to a time out for the state or the session
2011 * used was already terminated: nothing to do here... */
2015 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
2017 /* SYN_ACK continuation was called after neighbor changed state,
2018 * for example due to a time out for the state or the session
2019 * used was already terminated: nothing to do here... */
2022 if (GNUNET_OK == result)
2025 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2026 _("Failed to send SYN_ACK message to peer `%s' using address `%s'\n"),
2027 GNUNET_i2s (target),
2028 GST_plugins_a2s (n->primary_address.address));
2030 /* Remove address and request and additional one */
2031 /* FIXME: what if the neighbour's primary address
2032 changed in the meantime? Might want to instead
2033 pass "something" around in closure to be sure. */
2034 unset_primary_address (n);
2035 n->ack_state = ACK_SEND_SYN_ACK;
2036 set_state_and_timeout (n,
2037 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2038 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2043 * Send a SYN_ACK message via the given address.
2045 * @param na address and session to use
2046 * @param timestamp timestamp to use for the ACK message
2047 * @return #GNUNET_SYSERR if sending immediately failed, #GNUNET_OK otherwise
2050 send_syn_ack_message (struct NeighbourAddress *na,
2051 struct GNUNET_TIME_Absolute timestamp)
2053 const struct GNUNET_HELLO_Address *address = na->address;
2054 struct GNUNET_ATS_Session *session = na->session;
2055 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2056 struct TransportSynMessage connect_msg;
2057 struct NeighbourMapEntry *n;
2059 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2060 "Sending SYN_ACK to peer `%s'\n",
2061 GNUNET_i2s (&address->peer));
2063 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2068 if (NULL == session)
2069 session = papi->get_session (papi->cls,
2071 if (NULL == session)
2076 GST_ats_new_session (address,
2078 GNUNET_STATISTICS_update (GST_stats,
2080 ("# SYN_ACK messages sent"),
2082 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
2083 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
2084 connect_msg.reserved = htonl (0);
2085 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
2087 if (GNUNET_SYSERR ==
2088 papi->send (papi->cls,
2090 (const char *) &connect_msg,
2091 sizeof (struct TransportSynMessage),
2093 GNUNET_TIME_UNIT_FOREVER_REL,
2094 &send_session_syn_ack_cont, NULL))
2096 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2097 _("Failed to transmit SYN_ACK message to %s\n"),
2098 GST_plugins_a2s (address));
2100 n = lookup_neighbour (&address->peer);
2106 /* Remove address and request and additional one */
2107 unset_primary_address (n);
2108 n->ack_state = ACK_SEND_SYN_ACK;
2109 set_state_and_timeout (n,
2110 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2111 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2118 * Function called by the bandwidth tracker for a peer whenever
2119 * the tracker's state changed such that we need to recalculate
2120 * the delay for flow control. We calculate the latest delay
2121 * and inform the plugin (if applicable).
2123 * @param cls the `struct NeighbourMapEntry` to update calculations for
2126 inbound_bw_tracker_update (void *cls)
2128 struct NeighbourMapEntry *n = cls;
2129 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2130 struct GNUNET_TIME_Relative delay;
2133 if (NULL == n->primary_address.address)
2134 return; /* not active, ignore */
2135 papi = GST_plugins_find (n->primary_address.address->transport_name);
2136 GNUNET_assert (NULL != papi);
2137 if (NULL == papi->update_inbound_delay)
2139 delay = GST_neighbours_calculate_receive_delay (&n->id,
2142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2143 "New inbound delay for peer `%s' is %llu ms\n",
2144 GNUNET_i2s (&n->id),
2145 (unsigned long long) delay.rel_value_us / 1000LL);
2146 if (NULL == n->primary_address.session)
2148 papi->update_inbound_delay (papi->cls,
2150 n->primary_address.session,
2156 * Create a fresh entry in the neighbour map for the given peer
2158 * @param peer peer to create an entry for
2159 * @return new neighbour map entry
2161 static struct NeighbourMapEntry *
2162 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2164 struct NeighbourMapEntry *n;
2167 memcmp (&GST_my_identity,
2169 sizeof (struct GNUNET_PeerIdentity)))
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 "Cowardly refusing to consider myself my neighbour!\n");
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 "Creating new neighbour entry for `%s'\n",
2178 n = GNUNET_new (struct NeighbourMapEntry);
2180 n->ack_state = ACK_UNDEFINED;
2181 n->last_util_transmission = GNUNET_TIME_absolute_get();
2182 n->neighbour_receive_quota = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2183 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2184 &inbound_bw_tracker_update,
2186 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2187 MAX_BANDWIDTH_CARRY_S);
2188 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2189 set_state_and_timeout (n,
2190 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2191 GNUNET_TIME_UNIT_FOREVER_ABS);
2192 GNUNET_assert (GNUNET_OK ==
2193 GNUNET_CONTAINER_multipeermap_put (neighbours,
2196 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2197 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2206 * Entry in a DLL we use to keep track of pending blacklist checks.
2208 struct BlacklistCheckSwitchContext
2213 struct BlacklistCheckSwitchContext *prev;
2218 struct BlacklistCheckSwitchContext *next;
2221 * Handle to the blacklist check we are performing.
2223 struct GST_BlacklistCheck *blc;
2226 * Inbound bandwidth that was assigned to @e address.
2228 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2231 * Outbound bandwidth that was assigned to @e address.
2233 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2238 * We received a 'SYN' message from the other peer.
2239 * Consider switching to it.
2241 * @param message possibly a `struct TransportSynMessage` (check format)
2242 * @param peer identity of the peer to switch the address for
2243 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2246 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2247 const struct GNUNET_PeerIdentity *peer)
2249 const struct TransportSynMessage *scm;
2250 struct NeighbourMapEntry *n;
2251 struct GNUNET_TIME_Absolute ts;
2253 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2255 GNUNET_break_op (0);
2256 return GNUNET_SYSERR;
2258 GNUNET_STATISTICS_update (GST_stats,
2260 ("# SYN messages received"),
2262 if (NULL == neighbours)
2264 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2265 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2267 return GNUNET_OK; /* we're shutting down */
2269 scm = (const struct TransportSynMessage *) message;
2270 GNUNET_break_op (0 == ntohl (scm->reserved));
2271 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2273 memcmp (&GST_my_identity,
2275 sizeof (struct GNUNET_PeerIdentity)))
2277 /* loopback connection-to-self, ignore */
2278 return GNUNET_SYSERR;
2280 n = lookup_neighbour (peer);
2283 /* This is a new neighbour and set to not connected */
2284 n = setup_neighbour (peer);
2285 GNUNET_assert (NULL != n);
2288 /* Remember this SYN message in neighbour */
2289 n->ack_state = ACK_SEND_SYN_ACK;
2290 n->connect_ack_timestamp = ts;
2292 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2293 "Received SYN for peer `%s' in state %s/%s\n",
2295 GNUNET_TRANSPORT_ps2s (n->state),
2296 print_ack_state (n->ack_state));
2300 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2301 /* Request an address from ATS to send SYN_ACK to this peer */
2302 set_state_and_timeout (n,
2303 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2304 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2306 case GNUNET_TRANSPORT_PS_INIT_ATS:
2307 /* SYN message takes priority over us asking ATS for address:
2308 * Wait for ATS to suggest an address and send SYN_ACK */
2309 set_state_and_timeout (n,
2310 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2311 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2313 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2314 /* We already wait for an address to send an SYN_ACK */
2316 case GNUNET_TRANSPORT_PS_SYN_SENT:
2317 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2318 /* Send ACK immediately */
2319 n->ack_state = ACK_SEND_ACK;
2320 send_syn_ack_message (&n->primary_address,
2323 case GNUNET_TRANSPORT_PS_CONNECTED:
2324 /* we are already connected and can thus send the ACK immediately */
2325 GNUNET_assert (NULL != n->primary_address.address);
2326 GNUNET_assert (NULL != n->primary_address.session);
2327 n->ack_state = ACK_SEND_ACK;
2328 send_syn_ack_message (&n->primary_address,
2331 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2332 /* We wait for ATS address suggestion */
2334 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2335 /* We received a SYN message while waiting for a SYN_ACK in fast
2336 * reconnect. Send SYN_ACK immediately */
2337 n->ack_state = ACK_SEND_ACK;
2338 send_syn_ack_message (&n->primary_address,
2339 n->connect_ack_timestamp);
2341 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2342 /* We are already connected and can thus send the ACK immediately;
2343 still, it can never hurt to have an alternative address, so also
2344 tell ATS about it */
2345 GNUNET_assert (NULL != n->primary_address.address);
2346 GNUNET_assert (NULL != n->primary_address.session);
2347 n->ack_state = ACK_SEND_ACK;
2348 send_syn_ack_message (&n->primary_address,
2351 case GNUNET_TRANSPORT_PS_DISCONNECT:
2352 /* Get rid of remains and re-try */
2354 n = setup_neighbour (peer);
2355 GNUNET_assert (NULL != n);
2356 /* Remember the SYN time stamp for ACK message */
2357 n->ack_state = ACK_SEND_SYN_ACK;
2358 n->connect_ack_timestamp = ts;
2359 /* Request an address for the peer */
2360 set_state_and_timeout (n,
2361 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2362 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2364 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2365 /* should not be possible */
2369 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2370 "Unhandled state `%s'\n",
2371 GNUNET_TRANSPORT_ps2s (n->state));
2373 return GNUNET_SYSERR;
2380 * Check if the given @a address is the same that we are already
2381 * using for the respective neighbour. If so, update the bandwidth
2382 * assignment and possibly the session and return #GNUNET_OK.
2383 * If the new address is different from what the neighbour is
2384 * using right now, return #GNUNET_NO.
2386 * @param address address of the other peer,
2387 * @param session session to use or NULL if transport should initiate a session
2388 * @param bandwidth_in inbound quota to be used when connection is up,
2389 * 0 to disconnect from peer
2390 * @param bandwidth_out outbound quota to be used when connection is up,
2391 * 0 to disconnect from peer
2392 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2393 * #GNUNET_NO if more extensive changes are required (address changed)
2396 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2397 struct GNUNET_ATS_Session *session,
2398 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2399 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2401 struct NeighbourMapEntry *n;
2403 n = lookup_neighbour (&address->peer);
2405 (NULL == n->primary_address.address) ||
2406 (0 != GNUNET_HELLO_address_cmp (address,
2407 n->primary_address.address)) )
2409 /* We are not really switching addresses, but merely adjusting
2410 session and/or bandwidth, can do fast ATS update! */
2411 if (session != n->primary_address.session)
2413 /* switch to a different session, but keeping same address; could
2414 happen if there is a 2nd inbound connection */
2415 n->primary_address.session = session;
2416 GNUNET_assert (GNUNET_YES ==
2417 GST_ats_is_known (n->primary_address.address,
2418 n->primary_address.session));
2420 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
2422 n->primary_address.bandwidth_in = bandwidth_in;
2424 set_incoming_quota (n,
2428 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
2430 n->primary_address.bandwidth_out = bandwidth_out;
2431 send_outbound_quota_to_clients (n);
2438 * We've been asked to switch addresses, and just now got the result
2439 * from the blacklist check to see if this is allowed.
2441 * @param cls the `struct BlacklistCheckSwitchContext` with
2442 * the information about the future address
2443 * @param peer the peer we may switch addresses on
2444 * @param address address associated with the request
2445 * @param session session associated with the request
2446 * @param result #GNUNET_OK if the connection is allowed,
2447 * #GNUNET_NO if not,
2448 * #GNUNET_SYSERR if operation was aborted
2451 switch_address_bl_check_cont (void *cls,
2452 const struct GNUNET_PeerIdentity *peer,
2453 const struct GNUNET_HELLO_Address *address,
2454 struct GNUNET_ATS_Session *session,
2457 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2458 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2459 struct NeighbourMapEntry *n;
2461 if (GNUNET_SYSERR == result)
2464 papi = GST_plugins_find (address->transport_name);
2466 /* This can happen during shutdown. */
2470 if (GNUNET_NO == result)
2472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2473 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2474 GST_plugins_a2s (address),
2477 GNUNET_STATISTICS_update (GST_stats,
2478 "# ATS suggestions ignored (blacklist denied)",
2481 if (NULL != session)
2482 papi->disconnect_session (papi->cls,
2485 GNUNET_HELLO_address_check_option (address,
2486 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2487 GST_ats_block_address (address,
2493 if (NULL == session)
2495 /* need to create a session, ATS only gave us an address */
2496 session = papi->get_session (papi->cls,
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "Obtained new session for peer `%s' and address '%s': %p\n",
2500 GNUNET_i2s (&address->peer),
2501 GST_plugins_a2s (address),
2503 if (NULL != session)
2504 GST_ats_new_session (address,
2507 if (NULL == session)
2509 /* session creation failed, bad!, fail! */
2510 GNUNET_STATISTICS_update (GST_stats,
2511 "# ATS suggestions ignored (failed to create session)",
2514 /* No session could be obtained, remove blacklist check and clean up */
2515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2516 "Failed to obtain new session for peer `%s' and address '%s'\n",
2517 GNUNET_i2s (&address->peer),
2518 GST_plugins_a2s (address));
2519 GST_ats_block_address (address,
2524 /* We did this check already before going into blacklist, but
2525 it is theoretically possible that the situation changed in
2526 the meantime, hence we check again here */
2528 try_run_fast_ats_update (address,
2530 blc_ctx->bandwidth_in,
2531 blc_ctx->bandwidth_out))
2532 goto cleanup; /* was just a minor update, we're done */
2534 /* check if we also need to setup the neighbour entry */
2535 if (NULL == (n = lookup_neighbour (peer)))
2537 n = setup_neighbour (peer);
2540 /* not sure how this can happen... */
2544 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2547 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2548 "Peer `%s' switches to address `%s'\n",
2549 GNUNET_i2s (&address->peer),
2550 GST_plugins_a2s (address));
2554 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2556 GST_ats_block_address (address,
2560 case GNUNET_TRANSPORT_PS_INIT_ATS:
2561 /* We requested an address and ATS suggests one:
2562 * set primary address and send SYN message*/
2563 set_primary_address (n,
2566 blc_ctx->bandwidth_in,
2567 blc_ctx->bandwidth_out);
2568 if (ACK_SEND_SYN_ACK == n->ack_state)
2570 /* Send pending SYN_ACK message */
2571 n->ack_state = ACK_SEND_ACK;
2572 send_syn_ack_message (&n->primary_address,
2573 n->connect_ack_timestamp);
2575 set_state_and_timeout (n,
2576 GNUNET_TRANSPORT_PS_SYN_SENT,
2577 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2578 send_syn (&n->primary_address);
2580 case GNUNET_TRANSPORT_PS_SYN_SENT:
2581 /* ATS suggested a new address while waiting for an SYN_ACK:
2582 * Switch and send new SYN */
2583 /* ATS suggests a different address, switch again */
2584 set_primary_address (n,
2587 blc_ctx->bandwidth_in,
2588 blc_ctx->bandwidth_out);
2589 if (ACK_SEND_SYN_ACK == n->ack_state)
2591 /* Send pending SYN_ACK message */
2592 n->ack_state = ACK_SEND_ACK;
2593 send_syn_ack_message (&n->primary_address,
2594 n->connect_ack_timestamp);
2596 set_state_and_timeout (n,
2597 GNUNET_TRANSPORT_PS_SYN_SENT,
2598 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2599 send_syn (&n->primary_address);
2601 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2602 /* We requested an address and ATS suggests one:
2603 * set primary address and send SYN_ACK message*/
2604 set_primary_address (n,
2607 blc_ctx->bandwidth_in,
2608 blc_ctx->bandwidth_out);
2609 /* Send an ACK message as a response to the SYN msg */
2610 set_state_and_timeout (n,
2611 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2612 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2613 send_syn_ack_message (&n->primary_address,
2614 n->connect_ack_timestamp);
2615 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2616 (ACK_UNDEFINED == n->ack_state) )
2617 n->ack_state = ACK_SEND_ACK;
2619 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2620 /* ATS asks us to switch while we were trying to connect; switch to new
2621 address and check blacklist again */
2622 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2624 n->ack_state = ACK_SEND_ACK;
2625 send_syn_ack_message (&n->primary_address,
2626 n->connect_ack_timestamp);
2628 set_primary_address (n,
2631 blc_ctx->bandwidth_in,
2632 blc_ctx->bandwidth_out);
2633 set_state_and_timeout (n,
2634 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2635 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2637 case GNUNET_TRANSPORT_PS_CONNECTED:
2638 GNUNET_assert (NULL != n->primary_address.address);
2639 GNUNET_assert (NULL != n->primary_address.session);
2640 GNUNET_break (n->primary_address.session != session);
2641 /* ATS asks us to switch a life connection; see if we can get
2642 a SYN_ACK on it before we actually do this! */
2643 set_alternative_address (n,
2646 blc_ctx->bandwidth_in,
2647 blc_ctx->bandwidth_out);
2648 set_state_and_timeout (n,
2649 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2650 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2651 GNUNET_STATISTICS_update (GST_stats,
2652 gettext_noop ("# Attempts to switch addresses"),
2655 send_syn (&n->alternative_address);
2657 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2658 set_primary_address (n,
2661 blc_ctx->bandwidth_in,
2662 blc_ctx->bandwidth_out);
2663 if (ACK_SEND_SYN_ACK == n->ack_state)
2665 /* Send pending SYN_ACK message */
2666 n->ack_state = ACK_SEND_ACK;
2667 send_syn_ack_message (&n->primary_address,
2668 n->connect_ack_timestamp);
2670 set_state_and_timeout (n,
2671 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2672 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2673 send_syn (&n->primary_address);
2675 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2676 /* ATS asks us to switch while we were trying to reconnect; switch to new
2677 address and send SYN again */
2678 set_primary_address (n,
2681 blc_ctx->bandwidth_in,
2682 blc_ctx->bandwidth_out);
2683 set_state_and_timeout (n,
2684 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2685 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2686 send_syn (&n->primary_address);
2688 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2689 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2691 (n->primary_address.session == session) )
2693 /* ATS switches back to still-active session */
2694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2695 "ATS double-switched, cleaning up alternative address\n");
2696 free_address (&n->alternative_address);
2697 set_state_and_timeout (n,
2698 GNUNET_TRANSPORT_PS_CONNECTED,
2702 /* ATS asks us to switch a life connection, send */
2703 set_alternative_address (n,
2706 blc_ctx->bandwidth_in,
2707 blc_ctx->bandwidth_out);
2708 set_state_and_timeout (n,
2709 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2710 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2711 send_syn (&n->alternative_address);
2713 case GNUNET_TRANSPORT_PS_DISCONNECT:
2714 /* not going to switch addresses while disconnecting */
2715 GNUNET_STATISTICS_update (GST_stats,
2716 "# ATS suggestion ignored (disconnecting)",
2720 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2724 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2725 "Unhandled state `%s'\n",
2726 GNUNET_TRANSPORT_ps2s (n->state));
2731 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2734 GNUNET_free (blc_ctx);
2739 * For the given peer, switch to this address.
2741 * Before accepting this addresses and actively using it, a blacklist check
2744 * If any check fails or the suggestion can somehow not be followed, we
2745 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2746 * could not be satisfied and force ATS to do something else.
2748 * @param address address of the other peer,
2749 * @param session session to use or NULL if transport should initiate a session
2750 * @param bandwidth_in inbound quota to be used when connection is up,
2751 * 0 to disconnect from peer
2752 * @param bandwidth_out outbound quota to be used when connection is up,
2753 * 0 to disconnect from peer
2756 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2757 struct GNUNET_ATS_Session *session,
2758 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2759 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2761 struct GST_BlacklistCheck *blc;
2762 struct BlacklistCheckSwitchContext *blc_ctx;
2764 GNUNET_assert (NULL != address->transport_name);
2766 try_run_fast_ats_update (address,
2772 /* Check if plugin is available */
2773 if (NULL == (GST_plugins_find (address->transport_name)))
2775 /* we don't have the plugin for this address */
2777 GST_ats_block_address (address,
2781 if ((NULL == session) &&
2782 (GNUNET_HELLO_address_check_option (address,
2783 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2785 /* This is a inbound address and we do not have a session to use! */
2787 GST_ats_block_address (address,
2792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2793 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2794 GST_plugins_a2s (address),
2795 GNUNET_i2s (&address->peer),
2796 (unsigned int) ntohl (bandwidth_in.value__),
2797 (unsigned int) ntohl (bandwidth_out.value__));
2799 /* Perform blacklist check */
2800 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2801 blc_ctx->bandwidth_in = bandwidth_in;
2802 blc_ctx->bandwidth_out = bandwidth_out;
2803 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2806 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2807 address->transport_name,
2808 &switch_address_bl_check_cont,
2819 * Function called to send network utilization data to ATS for
2820 * each active connection.
2823 * @param key peer we send utilization data for
2824 * @param value the `struct NeighbourMapEntry *` with data to send
2825 * @return #GNUNET_OK (continue to iterate)
2828 send_utilization_data (void *cls,
2829 const struct GNUNET_PeerIdentity *key,
2832 struct NeighbourMapEntry *n = value;
2835 struct GNUNET_TIME_Relative delta;
2838 if ( (GNUNET_YES != test_connected (n)) ||
2839 (NULL == n->primary_address.address) )
2841 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2842 GNUNET_TIME_absolute_get ());
2844 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2845 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2847 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2848 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2851 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2855 GST_ats_update_utilization (n->primary_address.address,
2858 n->util_total_bytes_recv = 0;
2859 n->util_total_bytes_sent = 0;
2860 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2866 * Task transmitting utilization in a regular interval
2868 * @param cls the `struct NeighbourMapEntry` for which we are running
2871 utilization_transmission (void *cls)
2874 util_transmission_tk = NULL;
2875 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
2876 &send_utilization_data,
2878 util_transmission_tk
2879 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2880 &utilization_transmission,
2886 * Track information about data we received from the
2887 * given address (used to notify ATS about our utilization
2888 * of allocated resources).
2890 * @param address the address we got data from
2891 * @param message the message we received (really only the size is used)
2894 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2895 const struct GNUNET_MessageHeader *message)
2897 struct NeighbourMapEntry *n;
2899 n = lookup_neighbour (&address->peer);
2902 n->util_total_bytes_recv += ntohs (message->size);
2907 * Track information about data we transmitted using the given @a
2908 * address and @a session (used to notify ATS about our utilization of
2909 * allocated resources).
2911 * @param address the address we transmitted data to
2912 * @param session session we used to transmit data
2913 * @param message the message we sent (really only the size is used)
2916 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2917 struct GNUNET_ATS_Session *session,
2920 struct NeighbourMapEntry *n;
2922 n = lookup_neighbour (&address->peer);
2925 if (n->primary_address.session != session)
2927 n->util_total_bytes_sent += size;
2932 * Master task run for every neighbour. Performs all of the time-related
2933 * activities (keep alive, send next message, disconnect if idle, finish
2934 * clean up after disconnect).
2936 * @param cls the 'struct NeighbourMapEntry' for which we are running
2939 master_task (void *cls)
2941 struct NeighbourMapEntry *n = cls;
2942 struct GNUNET_TIME_Relative delay;
2945 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2947 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2948 GNUNET_i2s (&n->id),
2949 GNUNET_TRANSPORT_ps2s(n->state),
2950 GNUNET_STRINGS_relative_time_to_string (delay,
2954 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2955 /* invalid state for master task, clean up */
2959 case GNUNET_TRANSPORT_PS_INIT_ATS:
2960 if (0 == delay.rel_value_us)
2962 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2963 "Connection to `%s' timed out waiting for ATS to provide address\n",
2964 GNUNET_i2s (&n->id));
2969 case GNUNET_TRANSPORT_PS_SYN_SENT:
2970 if (0 == delay.rel_value_us)
2972 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2973 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2974 GNUNET_i2s (&n->id));
2975 /* Remove address and request and additional one */
2976 unset_primary_address (n);
2977 set_state_and_timeout (n,
2978 GNUNET_TRANSPORT_PS_INIT_ATS,
2979 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2983 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2984 if (0 == delay.rel_value_us)
2986 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2987 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2988 GNUNET_i2s (&n->id));
2993 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2994 if (0 == delay.rel_value_us)
2996 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2997 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2998 GNUNET_i2s (&n->id));
2999 disconnect_neighbour (n);
3003 case GNUNET_TRANSPORT_PS_CONNECTED:
3004 if (0 == delay.rel_value_us)
3006 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3007 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3008 GNUNET_i2s (&n->id));
3009 disconnect_neighbour (n);
3012 try_transmission_to_peer (n);
3015 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3016 if (0 == delay.rel_value_us)
3018 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3019 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3020 GNUNET_i2s (&n->id));
3021 disconnect_neighbour (n);
3025 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3026 if (0 == delay.rel_value_us)
3028 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3029 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
3030 GNUNET_i2s (&n->id));
3031 disconnect_neighbour (n);
3035 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3036 if (0 == delay.rel_value_us)
3038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3039 "Switch failed, cleaning up alternative address\n");
3040 free_address (&n->alternative_address);
3041 set_state_and_timeout (n,
3042 GNUNET_TRANSPORT_PS_CONNECTED,
3043 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
3045 try_transmission_to_peer (n);
3048 case GNUNET_TRANSPORT_PS_DISCONNECT:
3049 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3050 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3051 GNUNET_i2s (&n->id));
3054 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3055 /* how did we get here!? */
3059 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3060 "Unhandled state `%s'\n",
3061 GNUNET_TRANSPORT_ps2s (n->state));
3065 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3066 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3067 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3069 /* if we are *now* in one of the two states, we're sending
3070 keep alive messages, so we need to consider the keepalive
3071 delay, not just the connection timeout */
3072 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3075 if (NULL == n->task)
3076 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3083 * Send a ACK message to the neighbour to confirm that we
3086 * @param n neighbour to send the ACK to
3089 send_session_ack_message (struct NeighbourMapEntry *n)
3091 struct GNUNET_MessageHeader msg;
3093 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3094 "Sending ACK message to peer `%s'\n",
3095 GNUNET_i2s (&n->id));
3097 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3098 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3099 (void) send_with_session (n,
3101 sizeof (struct GNUNET_MessageHeader),
3103 GNUNET_TIME_UNIT_FOREVER_REL,
3110 * We received a 'SESSION_SYN_ACK' message from the other peer.
3111 * Consider switching to it.
3113 * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
3114 * @param peer identity of the peer to switch the address for
3115 * @param address address of the other peer, NULL if other peer
3117 * @param session session to use (or NULL)
3118 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3121 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3122 const struct GNUNET_HELLO_Address *address,
3123 struct GNUNET_ATS_Session *session)
3125 const struct TransportSynMessage *scm;
3126 struct GNUNET_TIME_Absolute ts;
3127 struct NeighbourMapEntry *n;
3130 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3132 GNUNET_break_op (0);
3133 return GNUNET_SYSERR;
3135 GNUNET_STATISTICS_update (GST_stats,
3137 ("# SYN_ACK messages received"),
3139 scm = (const struct TransportSynMessage *) message;
3140 GNUNET_break_op (ntohl (scm->reserved) == 0);
3141 if (NULL == (n = lookup_neighbour (&address->peer)))
3143 GNUNET_STATISTICS_update (GST_stats,
3145 ("# unexpected SYN_ACK messages (no peer)"),
3147 return GNUNET_SYSERR;
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3150 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3151 GNUNET_i2s (&address->peer),
3152 GNUNET_TRANSPORT_ps2s (n->state),
3153 print_ack_state (n->ack_state));
3154 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3157 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3160 return GNUNET_SYSERR;
3161 case GNUNET_TRANSPORT_PS_INIT_ATS:
3162 GNUNET_STATISTICS_update (GST_stats,
3163 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3167 case GNUNET_TRANSPORT_PS_SYN_SENT:
3168 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3170 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3171 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3174 set_state_and_timeout (n,
3175 GNUNET_TRANSPORT_PS_CONNECTED,
3176 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3177 set_primary_address (n,
3178 n->primary_address.address,
3179 n->primary_address.session,
3180 n->primary_address.bandwidth_in,
3181 n->primary_address.bandwidth_out);
3182 send_session_ack_message (n);
3184 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3185 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3186 GNUNET_STATISTICS_update (GST_stats,
3187 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3191 case GNUNET_TRANSPORT_PS_CONNECTED:
3192 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3193 send_session_ack_message (n);
3195 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3196 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3197 to give us a new address... */
3198 GNUNET_STATISTICS_update (GST_stats,
3199 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3203 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3204 /* Reconnecting with new address address worked; go back to connected! */
3205 set_state_and_timeout (n,
3206 GNUNET_TRANSPORT_PS_CONNECTED,
3207 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3208 send_session_ack_message (n);
3210 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3211 /* new address worked; adopt it and go back to connected! */
3212 set_state_and_timeout (n,
3213 GNUNET_TRANSPORT_PS_CONNECTED,
3214 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3215 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3217 /* Set primary addresses */
3218 set_primary_address (n,
3219 n->alternative_address.address,
3220 n->alternative_address.session,
3221 n->alternative_address.bandwidth_in,
3222 n->alternative_address.bandwidth_out);
3223 GNUNET_STATISTICS_update (GST_stats,
3224 gettext_noop ("# Successful attempts to switch addresses"),
3228 GNUNET_HELLO_address_free (n->alternative_address.address);
3229 memset (&n->alternative_address,
3231 sizeof (n->alternative_address));
3232 send_session_ack_message (n);
3234 case GNUNET_TRANSPORT_PS_DISCONNECT:
3235 GNUNET_STATISTICS_update (GST_stats,
3237 ("# unexpected SYN_ACK messages (disconnecting)"),
3239 return GNUNET_SYSERR;
3240 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3245 "Unhandled state `%s'\n",
3246 GNUNET_TRANSPORT_ps2s (n->state));
3248 return GNUNET_SYSERR;
3255 * A session was terminated. Take note; if needed, try to get
3256 * an alternative address from ATS.
3258 * @param peer identity of the peer where the session died
3259 * @param session session that is gone
3260 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3261 * this session was not in use
3264 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3265 struct GNUNET_ATS_Session *session)
3267 struct NeighbourMapEntry *n;
3269 if (NULL == (n = lookup_neighbour (peer)))
3270 return GNUNET_NO; /* can't affect us */
3271 if (session != n->primary_address.session)
3273 /* Free alternative address */
3274 if (session == n->alternative_address.session)
3276 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3277 set_state_and_timeout (n,
3278 GNUNET_TRANSPORT_PS_CONNECTED,
3280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3281 "Session died, cleaning up alternative address\n");
3282 free_address (&n->alternative_address);
3284 return GNUNET_NO; /* doesn't affect us further */
3287 n->expect_latency_response = GNUNET_NO;
3288 /* The session for neighbour's primary address died */
3291 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3295 case GNUNET_TRANSPORT_PS_INIT_ATS:
3299 case GNUNET_TRANSPORT_PS_SYN_SENT:
3300 /* The session used to send the SYN terminated:
3301 * this implies a connect error*/
3302 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3303 "Failed to send SYN in CONNECT_SENT with `%s' %p: session terminated\n",
3304 GST_plugins_a2s (n->primary_address.address),
3305 n->primary_address.session);
3307 /* Destroy the address since it cannot be used */
3308 unset_primary_address (n);
3309 set_state_and_timeout (n,
3310 GNUNET_TRANSPORT_PS_INIT_ATS,
3311 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3313 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3314 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3315 /* error on inbound session; free neighbour entirely */
3318 case GNUNET_TRANSPORT_PS_CONNECTED:
3319 /* Our primary connection died, try a fast reconnect */
3320 unset_primary_address (n);
3321 set_state_and_timeout (n,
3322 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3323 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3325 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3326 /* we don't have an address, how can it go down? */
3329 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3330 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3331 "Failed to send SYN in RECONNECT_SENT with `%s' %p: session terminated\n",
3332 GST_plugins_a2s (n->primary_address.address),
3333 n->primary_address.session);
3334 /* Destroy the address since it cannot be used */
3335 unset_primary_address (n);
3336 set_state_and_timeout (n,
3337 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3338 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3340 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3341 /* primary went down while we were waiting for SYN_ACK on secondary;
3342 secondary as primary */
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3345 "Connection `%s' %p to peer `%s' was terminated while switching, "
3346 "switching to alternative address `%s' %p\n",
3347 GST_plugins_a2s (n->primary_address.address),
3348 n->primary_address.session,
3350 GST_plugins_a2s (n->alternative_address.address),
3351 n->alternative_address.session);
3353 /* Destroy the inbound address since it cannot be used */
3354 free_address (&n->primary_address);
3355 n->primary_address = n->alternative_address;
3356 GNUNET_assert (GNUNET_YES ==
3357 GST_ats_is_known (n->primary_address.address,
3358 n->primary_address.session));
3359 memset (&n->alternative_address,
3361 sizeof (struct NeighbourAddress));
3362 set_state_and_timeout (n,
3363 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3364 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3366 case GNUNET_TRANSPORT_PS_DISCONNECT:
3367 unset_primary_address (n);
3369 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3370 /* neighbour was freed and plugins told to terminate session */
3373 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3374 "Unhandled state `%s'\n",
3375 GNUNET_TRANSPORT_ps2s (n->state));
3379 if (NULL != n->task)
3380 GNUNET_SCHEDULER_cancel (n->task);
3381 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3387 * We received a 'ACK' message from the other peer.
3388 * If we sent a 'SYN_ACK' last, this means we are now
3389 * connected. Otherwise, do nothing.
3391 * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
3392 * @param address address of the other peer
3393 * @param session session to use (or NULL)
3394 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3397 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3398 const struct GNUNET_HELLO_Address *address,
3399 struct GNUNET_ATS_Session *session)
3401 struct NeighbourMapEntry *n;
3404 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3406 GNUNET_break_op (0);
3407 return GNUNET_SYSERR;
3409 GNUNET_STATISTICS_update (GST_stats,
3410 gettext_noop ("# ACK messages received"),
3413 if (NULL == (n = lookup_neighbour (&address->peer)))
3415 GNUNET_break_op (0);
3416 return GNUNET_SYSERR;
3418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3419 "Received ACK for peer `%s' in state %s/%s\n",
3420 GNUNET_i2s (&address->peer),
3421 GNUNET_TRANSPORT_ps2s (n->state),
3422 print_ack_state (n->ack_state));
3424 /* Check if we are in a plausible state for having sent
3425 a SYN_ACK. If not, return, otherwise break.
3427 The remote peers sends a ACK as a response for a SYN_ACK
3431 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3432 now wait for the ACK to finally be connected
3433 - If we sent a SYN_ACK to this peer before */
3435 if ( ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3436 (ACK_SEND_ACK != n->ack_state) ) ||
3437 (NULL == n->primary_address.address) )
3439 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3440 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3441 GNUNET_i2s (&address->peer),
3442 GNUNET_TRANSPORT_ps2s (n->state),
3443 print_ack_state (n->ack_state));
3445 GNUNET_STATISTICS_update (GST_stats,
3446 gettext_noop ("# unexpected ACK messages"),
3451 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3453 /* We tried to switch addresses while being connect. We explicitly wait
3454 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3455 * so we do not want to set the address as in use! */
3458 set_state_and_timeout (n,
3459 GNUNET_TRANSPORT_PS_CONNECTED,
3460 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3462 if (NULL == n->primary_address.address) {
3464 * We are in state = PSY_SYN_RECV_ACK or ack_state = ACK_SEND_ACK, which
3465 * really means we did try (and succeed) to send a SYN and are waiting for
3467 * That suggests that the primary_address used to be non-NULL, but maybe it
3468 * got reset to NULL without the state being changed appropriately?
3474 /* Reset backoff for primary address */
3475 GST_ats_block_reset (n->primary_address.address,
3476 n->primary_address.session);
3482 * Test if we're connected to the given peer.
3484 * @param target peer to test
3485 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3488 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3490 return test_connected (lookup_neighbour (target));
3495 * Task to asynchronously run #free_neighbour().
3497 * @param cls the `struct NeighbourMapEntry` to free
3500 delayed_disconnect (void *cls)
3502 struct NeighbourMapEntry *n = cls;
3504 n->delayed_disconnect_task = NULL;
3505 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3506 "Disconnecting by request from peer %s\n",
3507 GNUNET_i2s (&n->id));
3513 * We received a quota message from the given peer,
3514 * validate and process.
3516 * @param peer sender of the message
3517 * @param msg the quota message
3520 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
3521 const struct GNUNET_MessageHeader *msg)
3523 struct NeighbourMapEntry *n;
3524 const struct GNUNET_ATS_SessionQuotaMessage *sqm;
3525 struct GNUNET_BANDWIDTH_Value32NBO last;
3527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3528 "Received QUOTA message from peer `%s'\n",
3530 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionQuotaMessage))
3532 GNUNET_break_op (0);
3533 GNUNET_STATISTICS_update (GST_stats,
3534 gettext_noop ("# quota messages ignored (malformed)"),
3539 GNUNET_STATISTICS_update (GST_stats,
3541 ("# QUOTA messages received"),
3543 sqm = (const struct GNUNET_ATS_SessionQuotaMessage *) msg;
3544 if (NULL == (n = lookup_neighbour (peer)))
3549 last = GNUNET_BANDWIDTH_value_max (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3550 GNUNET_BANDWIDTH_value_init (ntohl (sqm->quota)));
3551 if (last.value__ != n->neighbour_receive_quota.value__)
3553 n->neighbour_receive_quota = last;
3554 send_outbound_quota_to_clients (n);
3560 * We received a disconnect message from the given peer,
3561 * validate and process.
3563 * @param peer sender of the message
3564 * @param msg the disconnect message
3567 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3568 const struct GNUNET_MessageHeader *msg)
3570 struct NeighbourMapEntry *n;
3571 const struct GNUNET_ATS_SessionDisconnectMessage *sdm;
3573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3574 "Received DISCONNECT message from peer `%s'\n",
3576 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionDisconnectMessage))
3578 GNUNET_break_op (0);
3579 GNUNET_STATISTICS_update (GST_stats,
3581 ("# disconnect messages ignored (malformed)"),
3586 GNUNET_STATISTICS_update (GST_stats,
3588 ("# DISCONNECT messages received"),
3590 sdm = (const struct GNUNET_ATS_SessionDisconnectMessage *) msg;
3591 if (NULL == (n = lookup_neighbour (peer)))
3596 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3598 GNUNET_STATISTICS_update (GST_stats,
3599 gettext_noop ("# disconnect messages ignored (timestamp)"),
3604 if (0 != memcmp (peer,
3606 sizeof (struct GNUNET_PeerIdentity)))
3608 GNUNET_break_op (0);
3611 if (ntohl (sdm->purpose.size) !=
3612 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3613 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3614 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3617 "DISCONNECT message from peer `%s' has invalid size\n",
3619 GNUNET_break_op (0);
3623 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3629 "DISCONNECT message from peer `%s' cannot be verified \n",
3631 GNUNET_break_op (0);
3634 if (NULL == n->delayed_disconnect_task)
3636 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect,
3643 * Closure for the #neighbours_iterate() function.
3645 struct IteratorContext
3648 * Function to call on each connected neighbour.
3650 GST_NeighbourIterator cb;
3653 * Closure for @e cb.
3660 * Call the callback from the closure for each neighbour.
3662 * @param cls the `struct IteratorContext`
3663 * @param key the hash of the public key of the neighbour
3664 * @param value the `struct NeighbourMapEntry`
3665 * @return #GNUNET_OK (continue to iterate)
3668 neighbours_iterate (void *cls,
3669 const struct GNUNET_PeerIdentity *key,
3672 struct IteratorContext *ic = cls;
3673 struct NeighbourMapEntry *n = value;
3674 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3675 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3678 if (NULL != n->primary_address.address)
3680 bandwidth_in = n->primary_address.bandwidth_in;
3681 bandwidth_out = n->primary_address.bandwidth_out;
3685 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3686 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3690 n->primary_address.address,
3693 bandwidth_in, bandwidth_out);
3699 * Iterate over all connected neighbours.
3701 * @param cb function to call
3702 * @param cb_cls closure for @a cb
3705 GST_neighbours_iterate (GST_NeighbourIterator cb,
3708 struct IteratorContext ic;
3710 if (NULL == neighbours)
3711 return; /* can happen during shutdown */
3714 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3715 &neighbours_iterate,
3721 * If we have an active connection to the given target, it must be shutdown.
3723 * @param target peer to disconnect from
3726 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3728 struct NeighbourMapEntry *n;
3730 if (NULL == (n = lookup_neighbour (target)))
3731 return; /* not active */
3732 if (GNUNET_YES == test_connected (n))
3733 GNUNET_STATISTICS_update (GST_stats,
3734 gettext_noop ("# disconnected from peer upon explicit request"),
3737 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3738 "Forced disconnect from peer %s\n",
3739 GNUNET_i2s (target));
3740 disconnect_neighbour (n);
3745 * Obtain current address information for the given neighbour.
3748 * @return address currently used
3750 const struct GNUNET_HELLO_Address *
3751 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3753 struct NeighbourMapEntry *n;
3755 n = lookup_neighbour (peer);
3758 return n->primary_address.address;
3763 * Initialize the neighbours subsystem.
3765 * @param max_fds maximum number of fds to use
3768 GST_neighbours_start (unsigned int max_fds)
3771 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3773 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3774 &utilization_transmission,
3780 * Disconnect from the given neighbour.
3783 * @param key hash of neighbour's public key (not used)
3784 * @param value the `struct NeighbourMapEntry` of the neighbour
3785 * @return #GNUNET_OK (continue to iterate)
3788 disconnect_all_neighbours (void *cls,
3789 const struct GNUNET_PeerIdentity *key,
3792 struct NeighbourMapEntry *n = value;
3796 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3797 "Disconnecting peer `%4s' during shutdown\n",
3798 GNUNET_i2s (&n->id));
3805 * Cleanup the neighbours subsystem.
3808 GST_neighbours_stop ()
3810 if (NULL == neighbours)
3812 if (NULL != util_transmission_tk)
3814 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3815 util_transmission_tk = NULL;
3817 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3818 &disconnect_all_neighbours,
3820 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3825 /* end of file gnunet-service-transport_neighbours.c */