2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_ats.h"
29 #include "gnunet-service-transport_blacklist.h"
30 #include "gnunet-service-transport_clients.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport_manipulation.h"
33 #include "gnunet-service-transport_plugins.h"
34 #include "gnunet-service-transport_validation.h"
35 #include "gnunet-service-transport.h"
36 #include "gnunet_peerinfo_service.h"
37 #include "gnunet_constants.h"
38 #include "transport.h"
42 * Size of the neighbour hash map.
44 #define NEIGHBOUR_TABLE_SIZE 256
47 * Time we give plugin to transmit DISCONNECT message before the
48 * neighbour entry self-destructs.
50 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
53 * How often must a peer violate bandwidth quotas before we start
54 * to simply drop its messages?
56 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
59 * How long are we willing to wait for a response from ATS before timing out?
61 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
64 * How long are we willing to wait for an ACK from the other peer before
65 * giving up on our connect operation?
67 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
70 * How long are we willing to wait for a successful reconnect if
71 * an existing connection went down? Much shorter than the
72 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
73 * higher layers about the disconnect during this period.
75 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
78 * Interval to send utilization data
80 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_UNIT_SECONDS
83 * State describing which kind a reply this neighbour should send
88 * We did not receive a SYN message for this neighbour
93 * The neighbour received a SYN message and has to send a SYN_ACK
99 * The neighbour sent a SYN_ACK message and has to send a ACK
106 GNUNET_NETWORK_STRUCT_BEGIN
109 * Message a peer sends to another to indicate that it intends to
110 * setup a connection/session for data exchange. A 'SESSION_SYN'
111 * should be answered with a 'SESSION_SYN_ACK' with the same body
112 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
113 * a 'ACK'. Once the 'ACK' is received, both peers
114 * should be connected.
116 struct TransportSynMessage
119 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
120 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
122 struct GNUNET_MessageHeader header;
127 uint32_t reserved GNUNET_PACKED;
130 * Absolute time at the sender. Only the most recent connect
131 * message implies which session is preferred by the sender.
133 struct GNUNET_TIME_AbsoluteNBO timestamp;
139 * Message a peer sends to another when connected to indicate that a
140 * session is in use and the peer is still alive or to respond to a keep alive.
141 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
142 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
143 * When the keep alive response with type is received, transport service
144 * will call the respective plugin to update the session timeout
146 struct SessionKeepAliveMessage
149 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
150 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
152 struct GNUNET_MessageHeader header;
155 * A nonce to identify the session the keep alive is used for
157 uint32_t nonce GNUNET_PACKED;
161 * Message we send to the other peer to notify him that we intentionally
162 * are disconnecting (to reduce timeouts). This is just a friendly
163 * notification, peers must not rely on always receiving disconnect
166 struct SessionDisconnectMessage
169 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
171 struct GNUNET_MessageHeader header;
176 uint32_t reserved GNUNET_PACKED;
179 * Purpose of the signature. Extends over the timestamp.
180 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
182 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
185 * Absolute time at the sender. Only the most recent connect
186 * message implies which session is preferred by the sender.
188 struct GNUNET_TIME_AbsoluteNBO timestamp;
191 * Public key of the sender.
193 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
196 * Signature of the peer that sends us the disconnect. Only
197 * valid if the timestamp is AFTER the timestamp from the
198 * corresponding 'SYN' message.
200 struct GNUNET_CRYPTO_EddsaSignature signature;
204 GNUNET_NETWORK_STRUCT_END
208 * For each neighbour we keep a list of messages
209 * that we still want to transmit to the neighbour.
215 * This is a doubly linked list.
217 struct MessageQueue *next;
220 * This is a doubly linked list.
222 struct MessageQueue *prev;
225 * Function to call once we're done.
227 GST_NeighbourSendContinuation cont;
230 * Closure for @e cont
235 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
236 * stuck together in memory. Allocated at the end of this struct.
238 const char *message_buf;
241 * Size of the message buf
243 size_t message_buf_size;
246 * At what time should we fail?
248 struct GNUNET_TIME_Absolute timeout;
254 * A possible address we could use to communicate with a neighbour.
256 struct NeighbourAddress
260 * Active session for this address.
262 struct Session *session;
265 * Network-level address information.
267 struct GNUNET_HELLO_Address *address;
270 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
271 * peer for this address. Use to check that the ACK is in response
272 * to our most recent 'SYN'.
274 struct GNUNET_TIME_Absolute connect_timestamp;
277 * Inbound bandwidth from ATS for this address.
279 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
282 * Outbound bandwidth from ATS for this address.
284 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
287 * Did we tell ATS that this is our 'active' address?
292 * The current nonce sent in the last keep alive messages
294 uint32_t keep_alive_nonce;
299 * Entry in neighbours.
301 struct NeighbourMapEntry
305 * Head of list of messages we would like to send to this peer;
306 * must contain at most one message per client.
308 struct MessageQueue *messages_head;
311 * Tail of list of messages we would like to send to this peer; must
312 * contain at most one message per client.
314 struct MessageQueue *messages_tail;
317 * Are we currently trying to send a message? If so, which one?
319 struct MessageQueue *is_active;
322 * Primary address we currently use to communicate with the neighbour.
324 struct NeighbourAddress primary_address;
327 * Alternative address currently under consideration for communicating
328 * with the neighbour.
330 struct NeighbourAddress alternative_address;
333 * Identity of this neighbour.
335 struct GNUNET_PeerIdentity id;
338 * Main task that drives this peer (timeouts, keepalives, etc.).
339 * Always runs the 'master_task'.
341 struct GNUNET_SCHEDULER_Task *task;
344 * Task to disconnect neighbour after we received a DISCONNECT message
346 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
349 * At what time should we sent the next keep-alive message?
351 struct GNUNET_TIME_Absolute keep_alive_time;
354 * At what time did we sent the last keep-alive message? Used
355 * to calculate round-trip time ("latency").
357 struct GNUNET_TIME_Absolute last_keep_alive_time;
360 * Timestamp we should include in our next SYN_ACK message.
361 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
362 * our SYN_ACK message.
364 struct GNUNET_TIME_Absolute connect_ack_timestamp;
367 * ATS address suggest handle
369 struct GNUNET_ATS_ConnectivitySuggestHandle *suggest_handle;
372 * Time where we should cut the connection (timeout) if we don't
373 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
374 * if we are in #S_CONNECTED).
376 struct GNUNET_TIME_Absolute timeout;
379 * Tracker for inbound bandwidth.
381 struct GNUNET_BANDWIDTH_Tracker in_tracker;
384 * How often has the other peer (recently) violated the inbound
385 * traffic limit? Incremented by 10 per violation, decremented by 1
386 * per non-violation (for each time interval).
388 unsigned int quota_violation_count;
391 * The current state of the peer.
393 enum GNUNET_TRANSPORT_PeerState state;
396 * Did we sent an KEEP_ALIVE message and are we expecting a response?
398 int expect_latency_response;
401 * When a peer wants to connect we have to reply to the 1st SYN message
402 * with a SYN_ACK message. But sometime we cannot send this message
403 * immediately since we do not have an address and then we have to remember
404 * to send this message as soon as we have an address.
406 * Flag to set if we still need to send a SYN_ACK message to the other peer
407 * (once we have an address to use and the peer has been allowed by our
408 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
409 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
410 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
411 * 'ACK' (regardless of what our own state machine might say).
413 enum GST_ACK_State ack_state;
416 * Tracking utilization of outbound bandwidth
418 uint32_t util_total_bytes_sent;
421 * Tracking utilization of inbound bandwidth
423 uint32_t util_total_bytes_recv;
426 * Date of last utilization transmission
428 struct GNUNET_TIME_Absolute last_util_transmission;
433 * Context for blacklist checks and the #try_connect_bl_check_cont()
434 * function. Stores information about ongoing blacklist checks.
436 struct BlackListCheckContext
440 * We keep blacklist checks in a DLL.
442 struct BlackListCheckContext *next;
445 * We keep blacklist checks in a DLL.
447 struct BlackListCheckContext *prev;
450 * Address that is being checked.
452 struct NeighbourAddress na;
455 * Handle to the ongoing blacklist check.
457 struct GST_BlacklistCheck *bc;
462 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
464 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
467 * We keep blacklist checks in a DLL so that we can find
468 * the 'sessions' in their 'struct NeighbourAddress' if
469 * a session goes down.
471 static struct BlackListCheckContext *bc_head;
474 * We keep blacklist checks in a DLL.
476 static struct BlackListCheckContext *bc_tail;
479 * List of pending blacklist checks: head
481 static struct BlacklistCheckSwitchContext *pending_bc_head;
484 * List of pending blacklist checks: tail
486 static struct BlacklistCheckSwitchContext *pending_bc_tail;
489 * counter for connected neighbours
491 static unsigned int neighbours_connected;
494 * Number of bytes we have currently queued for transmission.
496 static unsigned long long bytes_in_send_queue;
499 * Task transmitting utilization data
501 static struct GNUNET_SCHEDULER_Task *util_transmission_tk;
505 * Convert the given ACK state to a string.
508 * @return corresponding human-readable string
511 print_ack_state (enum GST_ACK_State s)
516 case ACK_SEND_SYN_ACK:
517 return "SEND_SYN_ACK";
528 * Notify our clients that another peer connected to us.
530 * @param peer the peer that connected
531 * @param bandwidth_in inbound bandwidth in NBO
532 * @param bandwidth_out outbound bandwidth in NBO
535 neighbours_connect_notification (const struct GNUNET_PeerIdentity *peer,
536 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
537 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
539 size_t len = sizeof(struct ConnectInfoMessage);
540 char buf[len] GNUNET_ALIGN;
541 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
543 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
544 "We are now connected to peer `%s'\n",
546 connect_msg->header.size = htons (sizeof(buf));
547 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
548 connect_msg->id = *peer;
549 connect_msg->quota_in = bandwidth_in;
550 connect_msg->quota_out = bandwidth_out;
551 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
556 * Notify our clients (and manipulation) that a peer disconnected from
559 * @param peer the peer that disconnected
562 neighbours_disconnect_notification (const struct GNUNET_PeerIdentity *peer)
564 struct DisconnectInfoMessage disconnect_msg;
566 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
567 "Peer `%s' disconnected\n",
569 GST_manipulation_peer_disconnect (peer);
570 disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
571 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
572 disconnect_msg.reserved = htonl (0);
573 disconnect_msg.peer = *peer;
574 GST_clients_broadcast (&disconnect_msg.header,
580 * Notify transport clients that a neighbour peer changed its active
583 * @param peer identity of the peer
584 * @param address address possibly NULL if peer is not connected
585 * @param state current state this peer is in
586 * @param state_timeout timeout for the current state of the peer
587 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
588 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
591 neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer,
592 const struct GNUNET_HELLO_Address *address,
593 enum GNUNET_TRANSPORT_PeerState state,
594 struct GNUNET_TIME_Absolute state_timeout,
595 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
596 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
599 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
601 GST_plugins_a2s (address),
602 GNUNET_TRANSPORT_ps2s (state),
603 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
604 /* FIXME: include bandwidth in notification! */
605 GST_clients_broadcast_peer_notification (peer,
613 * Lookup a neighbour entry in the neighbours hash map.
615 * @param pid identity of the peer to look up
616 * @return the entry, NULL if there is no existing record
618 static struct NeighbourMapEntry *
619 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
621 if (NULL == neighbours)
623 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
628 * Test if we're connected to the given peer.
630 * @param n neighbour entry of peer to test
631 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
634 test_connected (struct NeighbourMapEntry *n)
638 return GNUNET_TRANSPORT_is_connected (n->state);
643 * Send information about a new outbound quota to our clients.
645 * @param target affected peer
646 * @param quota new quota
649 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
650 struct GNUNET_BANDWIDTH_Value32NBO quota)
652 struct QuotaSetMessage q_msg;
654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
655 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
656 ntohl (quota.value__),
657 GNUNET_i2s (target));
658 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
659 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
661 q_msg.peer = (*target);
662 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
667 * We don't need a given neighbour address any more.
668 * Release its resources and give appropriate notifications
669 * to ATS and other subsystems.
671 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
674 free_address (struct NeighbourAddress *na)
676 if (GNUNET_YES == na->ats_active)
677 GST_validation_set_address_use (na->address,
679 if (NULL != na->address)
681 GST_ats_block_address (na->address,
683 GNUNET_HELLO_address_free (na->address);
686 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
687 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
688 na->ats_active = GNUNET_NO;
689 na->keep_alive_nonce = 0;
695 * Master task run for every neighbour. Performs all of the time-related
696 * activities (keep alive, send next message, disconnect if idle, finish
697 * clean up after disconnect).
699 * @param cls the `struct NeighbourMapEntry` for which we are running
700 * @param tc scheduler context (unused)
703 master_task (void *cls,
704 const struct GNUNET_SCHEDULER_TaskContext *tc);
708 * Set net state and state timeout for this neighbour and notify monitoring
710 * @param n the respective neighbour
711 * @param s the new state
712 * @param timeout the new timeout
715 set_state_and_timeout (struct NeighbourMapEntry *n,
716 enum GNUNET_TRANSPORT_PeerState s,
717 struct GNUNET_TIME_Absolute timeout)
719 if (GNUNET_TRANSPORT_is_connected (s) &&
720 ! GNUNET_TRANSPORT_is_connected (n->state) )
722 neighbours_connect_notification (&n->id,
723 n->primary_address.bandwidth_in,
724 n->primary_address.bandwidth_out);
725 GNUNET_STATISTICS_set (GST_stats,
726 gettext_noop ("# peers connected"),
727 ++neighbours_connected,
730 if (! GNUNET_TRANSPORT_is_connected (s) &&
731 GNUNET_TRANSPORT_is_connected (n->state) )
733 GNUNET_STATISTICS_set (GST_stats,
734 gettext_noop ("# peers connected"),
735 --neighbours_connected,
737 neighbours_disconnect_notification (&n->id);
740 if ( (timeout.abs_value_us < n->timeout.abs_value_us) &&
743 /* new timeout is earlier, reschedule master task */
744 GNUNET_SCHEDULER_cancel (n->task);
745 n->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (timeout),
749 n->timeout = timeout;
750 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
751 "Neighbour `%s' changed state to %s with timeout %s\n",
753 GNUNET_TRANSPORT_ps2s(s),
754 GNUNET_STRINGS_absolute_time_to_string (timeout));
755 neighbours_changed_notification (&n->id,
756 n->primary_address.address,
759 n->primary_address.bandwidth_in,
760 n->primary_address.bandwidth_out);
765 * Initialize the alternative address of a neighbour
767 * @param n the neighbour
768 * @param address address of the other peer, NULL if other peer
770 * @param session session to use (or NULL, in which case an
771 * address must be setup)
772 * @param bandwidth_in inbound quota to be used when connection is up
773 * @param bandwidth_out outbound quota to be used when connection is up
776 set_alternative_address (struct NeighbourMapEntry *n,
777 const struct GNUNET_HELLO_Address *address,
778 struct Session *session,
779 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
780 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
782 struct GNUNET_TRANSPORT_PluginFunctions *papi;
784 if (NULL == (papi = GST_plugins_find (address->transport_name)))
789 if (session == n->alternative_address.session)
791 n->alternative_address.bandwidth_in = bandwidth_in;
792 n->alternative_address.bandwidth_out = bandwidth_out;
795 if (NULL != n->alternative_address.address)
797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
798 "Replacing existing alternative address with another one\n");
799 free_address (&n->alternative_address);
802 session = papi->get_session (papi->cls,
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807 "Failed to obtain new session for peer `%s' and address '%s'\n",
808 GNUNET_i2s (&address->peer),
809 GST_plugins_a2s (address));
810 GNUNET_STATISTICS_update (GST_stats,
811 gettext_noop ("# session creation failed"),
816 GST_ats_new_session (address,
818 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
819 "Neighbour `%s' configured alternative address %s\n",
821 GST_plugins_a2s(address));
823 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
824 n->alternative_address.bandwidth_in = bandwidth_in;
825 n->alternative_address.bandwidth_out = bandwidth_out;
826 n->alternative_address.session = session;
827 n->alternative_address.ats_active = GNUNET_NO;
828 n->alternative_address.keep_alive_nonce = 0;
833 * Initialize the primary address of a neighbour
835 * @param n the neighbour
836 * @param address address of the other peer, NULL if other peer
838 * @param session session to use (or NULL, in which case an
839 * address must be setup)
840 * @param bandwidth_in inbound quota to be used when connection is up
841 * @param bandwidth_out outbound quota to be used when connection is up
844 set_primary_address (struct NeighbourMapEntry *n,
845 const struct GNUNET_HELLO_Address *address,
846 struct Session *session,
847 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
848 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
850 if (session == n->primary_address.session)
852 GST_validation_set_address_use (n->primary_address.address,
854 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
856 n->primary_address.bandwidth_in = bandwidth_in;
857 GST_neighbours_set_incoming_quota (&address->peer,
860 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
862 n->primary_address.bandwidth_out = bandwidth_out;
863 send_outbound_quota (&address->peer,
868 if ( (NULL != n->primary_address.address) &&
869 (0 == GNUNET_HELLO_address_cmp (address,
870 n->primary_address.address)) )
878 GST_ats_block_address (address,
882 if (NULL != n->primary_address.address)
884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
885 "Replacing existing primary address with another one\n");
886 free_address (&n->primary_address);
888 n->primary_address.address = GNUNET_HELLO_address_copy (address);
889 n->primary_address.bandwidth_in = bandwidth_in;
890 n->primary_address.bandwidth_out = bandwidth_out;
891 n->primary_address.session = session;
892 n->primary_address.keep_alive_nonce = 0;
893 /* subsystems about address use */
894 GST_validation_set_address_use (n->primary_address.address,
896 GST_neighbours_set_incoming_quota (&address->peer,
898 send_outbound_quota (&address->peer,
900 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
901 "Neighbour `%s' switched to address `%s'\n",
903 GST_plugins_a2s(address));
905 neighbours_changed_notification (&n->id,
906 n->primary_address.address,
909 n->primary_address.bandwidth_in,
910 n->primary_address.bandwidth_out);
915 * Clear the primary address of a neighbour since this address is not
916 * valid anymore and notify monitoring about it
918 * @param n the neighbour
921 unset_primary_address (struct NeighbourMapEntry *n)
923 /* Notify monitoring about change */
924 if (NULL == n->primary_address.address)
926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
927 "Disabling primary address\n");
928 neighbours_changed_notification (&n->id,
929 n->primary_address.address,
932 GNUNET_BANDWIDTH_value_init (0),
933 GNUNET_BANDWIDTH_value_init (0));
934 free_address (&n->primary_address);
939 * Free a neighbour map entry.
941 * @param n entry to free
944 free_neighbour (struct NeighbourMapEntry *n)
946 struct MessageQueue *mq;
948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
949 "Freeing neighbour state of peer `%s'\n",
950 GNUNET_i2s (&n->id));
951 n->is_active = NULL; /* always free'd by its own continuation! */
953 /* fail messages currently in the queue */
954 while (NULL != (mq = n->messages_head))
956 GNUNET_CONTAINER_DLL_remove (n->messages_head,
959 if (NULL != mq->cont)
960 mq->cont (mq->cont_cls,
962 mq->message_buf_size,
966 /* Mark peer as disconnected */
967 set_state_and_timeout (n,
968 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED,
969 GNUNET_TIME_UNIT_FOREVER_ABS);
970 /* free addresses and mark as unused */
971 unset_primary_address (n);
973 if (NULL != n->alternative_address.address)
975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
976 "Cleaning up alternative address\n");
977 free_address (&n->alternative_address);
979 GNUNET_assert (GNUNET_YES ==
980 GNUNET_CONTAINER_multipeermap_remove (neighbours,
983 /* Cancel address requests for this peer */
984 if (NULL != n->suggest_handle)
986 GNUNET_ATS_connectivity_suggest_cancel (n->suggest_handle);
987 n->suggest_handle = NULL;
990 /* Cancel the disconnect task */
991 if (NULL != n->delayed_disconnect_task)
993 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
994 n->delayed_disconnect_task = NULL;
997 /* Cancel the master task */
1000 GNUNET_SCHEDULER_cancel (n->task);
1003 /* free rest of memory */
1009 * Transmit a message using the current session of the given
1012 * @param n entry for the recipient
1013 * @param msgbuf buffer to transmit
1014 * @param msgbuf_size number of bytes in @a msgbuf buffer
1015 * @param priority transmission priority
1016 * @param timeout transmission timeout
1017 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
1018 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
1019 * @param cont continuation to call when finished (can be NULL)
1020 * @param cont_cls closure for @a cont
1021 * @return timeout (copy of @a timeout or a calculated one if
1022 * @a use_keepalive_timeout is #GNUNET_YES.
1024 static struct GNUNET_TIME_Relative
1025 send_with_session (struct NeighbourMapEntry *n,
1029 struct GNUNET_TIME_Relative timeout,
1030 unsigned int use_keepalive_timeout,
1031 GNUNET_TRANSPORT_TransmitContinuation cont,
1034 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1035 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
1037 GNUNET_assert (NULL != n->primary_address.session);
1038 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1039 (-1 == papi->send (papi->cls,
1040 n->primary_address.session,
1044 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
1045 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1046 papi->query_keepalive_factor (papi->cls))),
1055 GST_neighbours_notify_data_sent (n->primary_address.address,
1056 n->primary_address.session,
1058 GNUNET_break (NULL != papi);
1064 * Function called when the 'DISCONNECT' message has been sent by the
1065 * plugin. Frees the neighbour --- if the entry still exists.
1068 * @param target identity of the neighbour that was disconnected
1069 * @param result #GNUNET_OK if the disconnect got out successfully
1070 * @param payload bytes payload
1071 * @param physical bytes on wire
1074 send_disconnect_cont (void *cls,
1075 const struct GNUNET_PeerIdentity *target,
1080 struct NeighbourMapEntry *n;
1082 n = lookup_neighbour (target);
1084 return; /* already gone */
1085 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1086 return; /* have created a fresh entry since */
1087 if (NULL != n->task)
1088 GNUNET_SCHEDULER_cancel (n->task);
1089 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1094 * Transmit a DISCONNECT message to the other peer.
1096 * @param n neighbour to send DISCONNECT message.
1099 send_disconnect (struct NeighbourMapEntry *n)
1101 struct SessionDisconnectMessage disconnect_msg;
1103 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1104 "Sending DISCONNECT message to peer `%4s'\n",
1105 GNUNET_i2s (&n->id));
1106 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1107 disconnect_msg.header.type =
1108 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1109 disconnect_msg.reserved = htonl (0);
1110 disconnect_msg.purpose.size =
1111 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1112 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1113 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1114 disconnect_msg.purpose.purpose =
1115 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1116 disconnect_msg.timestamp =
1117 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1118 disconnect_msg.public_key = GST_my_identity.public_key;
1119 GNUNET_assert (GNUNET_OK ==
1120 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1121 &disconnect_msg.purpose,
1122 &disconnect_msg.signature));
1124 (void) send_with_session (n,
1126 sizeof (disconnect_msg),
1128 GNUNET_TIME_UNIT_FOREVER_REL,
1130 &send_disconnect_cont,
1132 GNUNET_STATISTICS_update (GST_stats,
1133 gettext_noop ("# DISCONNECT messages sent"),
1140 * Disconnect from the given neighbour, clean up the record.
1142 * @param n neighbour to disconnect from
1145 disconnect_neighbour (struct NeighbourMapEntry *n)
1147 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1148 "Disconnecting from peer %s in state %s\n",
1149 GNUNET_i2s (&n->id),
1150 GNUNET_TRANSPORT_ps2s (n->state));
1151 /* depending on state, notify neighbour and/or upper layers of this peer
1155 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1156 case GNUNET_TRANSPORT_PS_INIT_ATS:
1157 /* other peer is completely unaware of us, no need to send DISCONNECT */
1160 case GNUNET_TRANSPORT_PS_SYN_SENT:
1161 send_disconnect (n);
1162 set_state_and_timeout (n,
1163 GNUNET_TRANSPORT_PS_DISCONNECT,
1164 GNUNET_TIME_UNIT_FOREVER_ABS);
1166 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1167 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1170 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1171 /* we DID ACK the other peer's request, must send DISCONNECT */
1172 send_disconnect (n);
1173 set_state_and_timeout (n,
1174 GNUNET_TRANSPORT_PS_DISCONNECT,
1175 GNUNET_TIME_UNIT_FOREVER_ABS);
1177 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1178 case GNUNET_TRANSPORT_PS_CONNECTED:
1179 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1180 /* we are currently connected, need to send disconnect and do
1181 internal notifications and update statistics */
1182 send_disconnect (n);
1183 set_state_and_timeout (n,
1184 GNUNET_TRANSPORT_PS_DISCONNECT,
1185 GNUNET_TIME_UNIT_FOREVER_ABS);
1187 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1188 /* Disconnecting while waiting for an ATS address to reconnect,
1189 * cannot send DISCONNECT */
1192 case GNUNET_TRANSPORT_PS_DISCONNECT:
1193 /* already disconnected, ignore */
1195 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1196 /* already cleaned up, how did we get here!? */
1200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1201 "Unhandled state `%s'\n",
1202 GNUNET_TRANSPORT_ps2s (n->state));
1206 /* schedule timeout to clean up */
1207 if (NULL != n->task)
1208 GNUNET_SCHEDULER_cancel (n->task);
1209 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1216 * We're done with our transmission attempt, continue processing.
1218 * @param cls the `struct MessageQueue` of the message
1219 * @param receiver intended receiver
1220 * @param success whether it worked or not
1221 * @param size_payload bytes payload sent
1222 * @param physical bytes sent on wire
1225 transmit_send_continuation (void *cls,
1226 const struct GNUNET_PeerIdentity *receiver,
1227 int success, size_t size_payload, size_t physical)
1229 struct MessageQueue *mq = cls;
1230 struct NeighbourMapEntry *n;
1232 if (NULL == (n = lookup_neighbour (receiver)))
1235 return; /* disconnect or other error while transmitting, can happen */
1237 if (n->is_active == mq)
1239 /* this is still "our" neighbour, remove us from its queue
1240 and allow it to send the next message now */
1241 n->is_active = NULL;
1242 if (NULL != n->task)
1243 GNUNET_SCHEDULER_cancel (n->task);
1244 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1246 if (bytes_in_send_queue < mq->message_buf_size)
1248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1249 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1250 bytes_in_send_queue,
1251 mq->message_buf_size,
1252 (GNUNET_OK == success) ? "OK" : "FAIL",
1259 GNUNET_break (size_payload == mq->message_buf_size);
1260 bytes_in_send_queue -= mq->message_buf_size;
1261 GNUNET_STATISTICS_set (GST_stats,
1263 ("# bytes in message queue for other peers"),
1264 bytes_in_send_queue, GNUNET_NO);
1265 if (GNUNET_OK == success)
1266 GNUNET_STATISTICS_update (GST_stats,
1268 ("# messages transmitted to other peers"),
1271 GNUNET_STATISTICS_update (GST_stats,
1273 ("# transmission failures for messages to other peers"),
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1277 GNUNET_i2s (receiver),
1278 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1279 mq->message_buf_size,
1280 (success == GNUNET_OK) ? "success" : "FAILURE");
1281 if (NULL != mq->cont)
1282 mq->cont (mq->cont_cls, success, size_payload, physical);
1288 * Check the message list for the given neighbour and if we can
1289 * send a message, do so. This function should only be called
1290 * if the connection is at least generally ready for transmission.
1291 * While we will only send one message at a time, no bandwidth
1292 * quota management is performed here. If a message was given to
1293 * the plugin, the continuation will automatically re-schedule
1294 * the 'master' task once the next message might be transmitted.
1296 * @param n target peer for which to transmit
1299 try_transmission_to_peer (struct NeighbourMapEntry *n)
1301 struct MessageQueue *mq;
1302 struct GNUNET_TIME_Relative timeout;
1304 if (NULL == n->primary_address.address)
1306 /* no address, why are we here? */
1310 if ((0 == n->primary_address.address->address_length) &&
1311 (NULL == n->primary_address.session))
1313 /* no address, why are we here? */
1317 if (NULL != n->is_active)
1319 /* transmission already pending */
1323 /* timeout messages from the queue that are past their due date */
1324 while (NULL != (mq = n->messages_head))
1326 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1327 if (timeout.rel_value_us > 0)
1329 GNUNET_STATISTICS_update (GST_stats,
1331 ("# messages timed out while in transport queue"),
1333 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1337 transmit_send_continuation (mq,
1340 mq->message_buf_size,
1344 return; /* no more messages */
1345 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1351 "Giving message with %u bytes to plugin session %p\n",
1352 mq->message_buf_size,
1353 n->primary_address.session);
1354 (void) send_with_session (n,
1356 mq->message_buf_size,
1360 &transmit_send_continuation,
1366 * Send keepalive message to the neighbour. Must only be called
1367 * if we are on 'connected' state or while trying to switch addresses.
1368 * Will internally determine if a keepalive is truly needed (so can
1369 * always be called).
1371 * @param n neighbour that went idle and needs a keepalive
1374 send_keepalive (struct NeighbourMapEntry *n)
1376 struct SessionKeepAliveMessage m;
1377 struct GNUNET_TIME_Relative timeout;
1380 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1381 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state));
1382 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1383 return; /* no keepalive needed at this time */
1385 nonce = 0; /* 0 indicates 'not set' */
1387 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390 "Sending keep alive to peer `%s' with nonce %u\n",
1391 GNUNET_i2s (&n->id),
1393 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1394 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1395 m.nonce = htonl (nonce);
1397 timeout = send_with_session (n,
1400 UINT32_MAX /* priority */,
1401 GNUNET_TIME_UNIT_FOREVER_REL,
1404 GNUNET_STATISTICS_update (GST_stats,
1405 gettext_noop ("# KEEPALIVES sent"),
1408 n->primary_address.keep_alive_nonce = nonce;
1409 n->expect_latency_response = GNUNET_YES;
1410 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1411 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1416 * Keep the connection to the given neighbour alive longer,
1417 * we received a KEEPALIVE (or equivalent); send a response.
1419 * @param neighbour neighbour to keep alive (by sending keep alive response)
1420 * @param m the keep alive message containing the nonce to respond to
1423 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1424 const struct GNUNET_MessageHeader *m)
1426 struct NeighbourMapEntry *n;
1427 const struct SessionKeepAliveMessage *msg_in;
1428 struct SessionKeepAliveMessage msg;
1430 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1432 GNUNET_break_op (0);
1436 msg_in = (const struct SessionKeepAliveMessage *) m;
1437 if (NULL == (n = lookup_neighbour (neighbour)))
1439 GNUNET_STATISTICS_update (GST_stats,
1441 ("# KEEPALIVE messages discarded (peer unknown)"),
1445 if (NULL == n->primary_address.session)
1447 GNUNET_STATISTICS_update (GST_stats,
1449 ("# KEEPALIVE messages discarded (no session)"),
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 "Received KEEPALIVE request from peer `%s' with nonce %u\n",
1456 GNUNET_i2s (&n->id),
1457 ntohl (msg_in->nonce));
1459 /* send reply to allow neighbour to measure latency */
1460 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1461 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1462 msg.nonce = msg_in->nonce;
1463 (void) send_with_session (n,
1465 sizeof (struct SessionKeepAliveMessage),
1466 UINT32_MAX /* priority */,
1467 GNUNET_TIME_UNIT_FOREVER_REL,
1474 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1475 * latency to this peer. Pass the updated information (existing ats
1476 * plus calculated latency) to ATS.
1478 * @param neighbour neighbour to keep alive
1479 * @param m the message containing the keep alive response
1482 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1483 const struct GNUNET_MessageHeader *m)
1485 struct NeighbourMapEntry *n;
1486 const struct SessionKeepAliveMessage *msg;
1487 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1488 struct GNUNET_TIME_Relative latency;
1490 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1492 GNUNET_break_op (0);
1496 msg = (const struct SessionKeepAliveMessage *) m;
1497 if (NULL == (n = lookup_neighbour (neighbour)))
1499 GNUNET_STATISTICS_update (GST_stats,
1500 gettext_noop ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1505 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1506 (GNUNET_YES != n->expect_latency_response) )
1508 GNUNET_STATISTICS_update (GST_stats,
1509 gettext_noop ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1514 if (NULL == n->primary_address.address)
1516 GNUNET_STATISTICS_update (GST_stats,
1517 gettext_noop ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1522 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1524 GNUNET_STATISTICS_update (GST_stats,
1525 gettext_noop ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1530 GNUNET_STATISTICS_update (GST_stats,
1531 gettext_noop ("# KEEPALIVE_RESPONSE messages received in good order"),
1536 /* Update session timeout here */
1537 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1539 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1540 "Updating session for peer `%s' for session %p\n",
1541 GNUNET_i2s (&n->id),
1542 n->primary_address.session);
1543 papi->update_session_timeout (papi->cls,
1545 n->primary_address.session);
1552 n->primary_address.keep_alive_nonce = 0;
1553 n->expect_latency_response = GNUNET_NO;
1554 set_state_and_timeout (n,
1556 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1558 latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1560 "Received KEEPALIVE_RESPONSE from peer `%s', latency is %s\n",
1561 GNUNET_i2s (&n->id),
1562 GNUNET_STRINGS_relative_time_to_string (latency,
1564 GST_ats_update_delay (n->primary_address.address,
1565 GNUNET_TIME_relative_divide (latency,
1571 * We have received a message from the given sender. How long should
1572 * we delay before receiving more? (Also used to keep the peer marked
1575 * @param sender sender of the message
1576 * @param size size of the message
1577 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1578 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1579 * #GNUNET_SYSERR if the connection is not fully up yet
1580 * @return how long to wait before reading more from this sender
1582 struct GNUNET_TIME_Relative
1583 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
1587 struct NeighbourMapEntry *n;
1588 struct GNUNET_TIME_Relative ret;
1590 if (NULL == neighbours)
1592 *do_forward = GNUNET_NO;
1593 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1595 if (NULL == (n = lookup_neighbour (sender)))
1597 GST_neighbours_try_connect (sender);
1598 if (NULL == (n = lookup_neighbour (sender)))
1600 GNUNET_STATISTICS_update (GST_stats,
1602 ("# messages discarded due to lack of neighbour record"),
1604 *do_forward = GNUNET_NO;
1605 return GNUNET_TIME_UNIT_ZERO;
1608 if (! test_connected (n))
1610 *do_forward = GNUNET_SYSERR;
1611 return GNUNET_TIME_UNIT_ZERO;
1613 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1615 n->quota_violation_count++;
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1617 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1618 n->in_tracker.available_bytes_per_s__,
1619 n->quota_violation_count);
1620 /* Discount 32k per violation */
1621 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1625 if (n->quota_violation_count > 0)
1627 /* try to add 32k back */
1628 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1629 n->quota_violation_count--;
1632 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1634 GNUNET_STATISTICS_update (GST_stats,
1636 ("# bandwidth quota violations by other peers"),
1638 *do_forward = GNUNET_NO;
1639 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1641 *do_forward = GNUNET_YES;
1642 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1643 if (ret.rel_value_us > 0)
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1646 "Throttling read (%lld bytes excess at %u b/s), waiting %s before reading more.\n",
1647 (long long) n->in_tracker.consumption_since_last_update__,
1648 (unsigned int) n->in_tracker.available_bytes_per_s__,
1649 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1650 GNUNET_STATISTICS_update (GST_stats,
1651 gettext_noop ("# ms throttling suggested"),
1652 (int64_t) ret.rel_value_us / 1000LL,
1660 * Transmit a message to the given target using the active connection.
1662 * @param target destination
1663 * @param msg message to send
1664 * @param msg_size number of bytes in msg
1665 * @param timeout when to fail with timeout
1666 * @param cont function to call when done
1667 * @param cont_cls closure for @a cont
1670 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
1673 struct GNUNET_TIME_Relative timeout,
1674 GST_NeighbourSendContinuation cont,
1677 struct NeighbourMapEntry *n;
1678 struct MessageQueue *mq;
1680 /* All ove these cases should never happen; they are all API violations.
1681 But we check anyway, just to be sure. */
1682 if (NULL == (n = lookup_neighbour (target)))
1692 if (GNUNET_YES != test_connected (n))
1702 bytes_in_send_queue += msg_size;
1703 GNUNET_STATISTICS_set (GST_stats,
1705 ("# bytes in message queue for other peers"),
1706 bytes_in_send_queue, GNUNET_NO);
1707 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1709 mq->cont_cls = cont_cls;
1710 memcpy (&mq[1], msg, msg_size);
1711 mq->message_buf = (const char *) &mq[1];
1712 mq->message_buf_size = msg_size;
1713 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1715 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1716 "Enqueueing %u bytes to send to peer %s\n",
1718 GNUNET_i2s (target));
1719 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
1722 if (NULL != n->task)
1723 GNUNET_SCHEDULER_cancel (n->task);
1724 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1729 * Continuation called from our attempt to transmitted our
1730 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN to the specified @a
1731 * target. Continue processing based on the @a result. Specifically,
1732 * if we failed to transmit, discard the address we used.
1735 * @param target which peer received the transmission
1736 * @param result #GNUNET_OK if sending worked
1737 * @param size_payload how many bytes of payload were sent (ignored)
1738 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1741 send_session_syn_cont (void *cls,
1742 const struct GNUNET_PeerIdentity *target,
1744 size_t size_payload,
1745 size_t size_on_wire)
1747 struct NeighbourMapEntry *n;
1749 n = lookup_neighbour (target);
1752 /* SYN continuation was called after neighbor was freed,
1753 * for example due to a time out for the state or the session
1754 * used was already terminated: nothing to do here... */
1758 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1759 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1760 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1762 /* SYN continuation was called after neighbor changed state,
1763 * for example due to a time out for the state or the session
1764 * used was already terminated: nothing to do here... */
1767 if (GNUNET_OK == result)
1770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1771 _("Failed to send SYN message to peer `%s'\n"),
1772 GNUNET_i2s (target));
1774 case GNUNET_TRANSPORT_PS_SYN_SENT:
1775 /* Remove address and request an additional one */
1776 unset_primary_address (n);
1777 set_state_and_timeout (n,
1778 GNUNET_TRANSPORT_PS_INIT_ATS,
1779 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1781 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1782 /* Remove address and request an additional one */
1783 unset_primary_address (n);
1784 set_state_and_timeout (n,
1785 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1786 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1788 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1789 /* Remove address and request and go back to primary address */
1790 GNUNET_STATISTICS_update (GST_stats,
1791 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN CONT)"),
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1795 "Switch failed, cleaning up alternative address\n");
1796 free_address (&n->alternative_address);
1797 set_state_and_timeout (n,
1798 GNUNET_TRANSPORT_PS_CONNECTED,
1799 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1802 disconnect_neighbour (n);
1809 * Send a SYN message via the given address.
1811 * @param na address to use
1814 send_syn (struct NeighbourAddress *na)
1816 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1817 struct TransportSynMessage connect_msg;
1818 struct NeighbourMapEntry *n;
1820 GNUNET_assert (NULL != na->session);
1821 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1822 "Sending SYN message to peer `%s' at %s\n",
1823 GNUNET_i2s (&na->address->peer),
1824 GST_plugins_a2s (na->address));
1826 papi = GST_plugins_find (na->address->transport_name);
1827 GNUNET_assert (NULL != papi);
1828 GNUNET_STATISTICS_update (GST_stats,
1830 ("# SYN messages sent"),
1832 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1833 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1834 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1835 connect_msg.reserved = htonl (0);
1836 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1838 papi->send (papi->cls,
1840 (const char *) &connect_msg,
1841 sizeof (struct TransportSynMessage),
1843 SETUP_CONNECTION_TIMEOUT,
1844 &send_session_syn_cont, NULL))
1846 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1847 _("Failed to transmit SYN message to %s\n"),
1848 GST_plugins_a2s (na->address));
1849 n = lookup_neighbour (&na->address->peer);
1856 case GNUNET_TRANSPORT_PS_SYN_SENT:
1857 /* Remove address and request and additional one */
1858 GNUNET_assert (na == &n->primary_address);
1859 unset_primary_address (n);
1860 set_state_and_timeout (n,
1861 GNUNET_TRANSPORT_PS_INIT_ATS,
1862 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1863 /* Hard failure to send the SYN message with this address:
1864 Destroy address and session */
1866 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1867 /* Remove address and request an additional one */
1868 GNUNET_assert (na == &n->primary_address);
1869 unset_primary_address (n);
1870 set_state_and_timeout (n,
1871 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1872 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1874 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1875 GNUNET_assert (na == &n->alternative_address);
1876 GNUNET_STATISTICS_update (GST_stats,
1877 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN)"),
1880 /* Remove address and request an additional one */
1881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1882 "Switch failed, cleaning up alternative address\n");
1883 free_address (&n->alternative_address);
1884 set_state_and_timeout (n,
1885 GNUNET_TRANSPORT_PS_CONNECTED,
1886 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1890 disconnect_neighbour (n);
1895 GST_neighbours_notify_data_sent (na->address,
1897 sizeof (struct TransportSynMessage));
1902 * Continuation called from our attempt to transmitted our
1903 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK to the specified @a
1904 * target. Continue processing based on the @a result. Specifically,
1905 * if we failed to transmit, discard the address we used.
1908 * @param target which peer received the transmission
1909 * @param result #GNUNET_OK if sending worked
1910 * @param size_payload how many bytes of payload were sent (ignored)
1911 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1914 send_session_syn_ack_cont (void *cls,
1915 const struct GNUNET_PeerIdentity *target,
1917 size_t size_payload,
1918 size_t size_on_wire)
1920 struct NeighbourMapEntry *n;
1922 n = lookup_neighbour (target);
1925 /* SYN_ACK continuation was called after neighbor was freed,
1926 * for example due to a time out for the state or the session
1927 * used was already terminated: nothing to do here... */
1931 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1933 /* SYN_ACK continuation was called after neighbor changed state,
1934 * for example due to a time out for the state or the session
1935 * used was already terminated: nothing to do here... */
1938 if (GNUNET_OK == result)
1941 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1942 _("Failed to send SYN_ACK message to peer `%s' using address `%s'\n"),
1943 GNUNET_i2s (target),
1944 GST_plugins_a2s (n->primary_address.address));
1946 /* Remove address and request and additional one */
1947 /* FIXME: what if the neighbour's primary address
1948 changed in the meantime? Might want to instead
1949 pass "something" around in closure to be sure. */
1950 unset_primary_address (n);
1951 n->ack_state = ACK_SEND_SYN_ACK;
1952 set_state_and_timeout (n,
1953 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1954 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1959 * Send a SYN_ACK message via the given address.
1961 * @param na address and session to use
1962 * @param timestamp timestamp to use for the ACK message
1963 * @return #GNUNET_SYSERR if sending immediately failed, #GNUNET_OK otherwise
1966 send_syn_ack_message (struct NeighbourAddress *na,
1967 struct GNUNET_TIME_Absolute timestamp)
1969 const struct GNUNET_HELLO_Address *address = na->address;
1970 struct Session *session = na->session;
1971 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1972 struct TransportSynMessage connect_msg;
1973 struct NeighbourMapEntry *n;
1975 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1976 "Sending SYN_ACK to peer `%s'\n",
1977 GNUNET_i2s (&address->peer));
1979 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1984 if (NULL == session)
1985 session = papi->get_session (papi->cls,
1987 if (NULL == session)
1992 GST_ats_new_session (address,
1994 GNUNET_STATISTICS_update (GST_stats,
1996 ("# SYN_ACK messages sent"),
1998 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1999 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
2000 connect_msg.reserved = htonl (0);
2001 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
2003 if (GNUNET_SYSERR ==
2004 papi->send (papi->cls,
2006 (const char *) &connect_msg,
2007 sizeof (struct TransportSynMessage),
2009 GNUNET_TIME_UNIT_FOREVER_REL,
2010 &send_session_syn_ack_cont, NULL))
2012 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2013 _("Failed to transmit SYN_ACK message to %s\n"),
2014 GST_plugins_a2s (address));
2016 n = lookup_neighbour (&address->peer);
2022 /* Remove address and request and additional one */
2023 unset_primary_address (n);
2024 n->ack_state = ACK_SEND_SYN_ACK;
2025 set_state_and_timeout (n,
2026 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2027 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2034 * Function called by the bandwidth tracker for a peer whenever
2035 * the tracker's state changed such that we need to recalculate
2036 * the delay for flow control. We calculate the latest delay
2037 * and inform the plugin (if applicable).
2039 * @param cls the `struct NeighbourMapEntry` to update calculations for
2042 inbound_bw_tracker_update (void *cls)
2044 struct NeighbourMapEntry *n = cls;
2045 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2046 struct GNUNET_TIME_Relative delay;
2049 if (NULL == n->primary_address.address)
2050 return; /* not active, ignore */
2051 papi = GST_plugins_find (n->primary_address.address->transport_name);
2052 GNUNET_assert (NULL != papi);
2053 if (NULL == papi->update_inbound_delay)
2055 delay = GST_neighbours_calculate_receive_delay (&n->id,
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 "New inbound delay for peer `%s' is %llu ms\n",
2060 GNUNET_i2s (&n->id),
2061 delay.rel_value_us / 1000);
2062 papi->update_inbound_delay (papi->cls,
2064 n->primary_address.session,
2070 * Create a fresh entry in the neighbour map for the given peer
2072 * @param peer peer to create an entry for
2073 * @return new neighbour map entry
2075 static struct NeighbourMapEntry *
2076 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2078 struct NeighbourMapEntry *n;
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2081 "Creating new neighbour entry for `%s'\n",
2083 n = GNUNET_new (struct NeighbourMapEntry);
2085 n->ack_state = ACK_UNDEFINED;
2086 n->last_util_transmission = GNUNET_TIME_absolute_get();
2087 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2088 &inbound_bw_tracker_update,
2090 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2091 MAX_BANDWIDTH_CARRY_S);
2092 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2093 set_state_and_timeout (n,
2094 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2095 GNUNET_TIME_UNIT_FOREVER_ABS);
2096 GNUNET_assert (GNUNET_OK ==
2097 GNUNET_CONTAINER_multipeermap_put (neighbours,
2099 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2100 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2108 * Entry in a DLL we use to keep track of pending blacklist checks.
2110 struct BlacklistCheckSwitchContext
2115 struct BlacklistCheckSwitchContext *prev;
2120 struct BlacklistCheckSwitchContext *next;
2123 * Handle to the blacklist check we are performing.
2125 struct GST_BlacklistCheck *blc;
2128 * Address we are asking the blacklist subsystem about.
2130 struct GNUNET_HELLO_Address *address;
2133 * Session we should use in conjunction with @e address, can be NULL.
2135 struct Session *session;
2138 * Inbound bandwidth that was assigned to @e address.
2140 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2143 * Outbound bandwidth that was assigned to @e address.
2145 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2150 * Black list check result for try_connect call
2151 * If connection to the peer is allowed request adddress and
2153 * @param cls blc_ctx bl context
2154 * @param peer the peer
2155 * @param result the result
2158 try_connect_bl_check_cont (void *cls,
2159 const struct GNUNET_PeerIdentity *peer,
2162 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2163 struct NeighbourMapEntry *n;
2165 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2168 GNUNET_free (blc_ctx);
2169 if (GNUNET_OK != result)
2171 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2172 _("Blacklisting disapproved to connect to peer `%s'\n"),
2177 /* Setup a new neighbour */
2178 if (NULL != lookup_neighbour(peer))
2179 return; /* The neighbor was created in the meantime while waited for BL clients */
2181 n = setup_neighbour (peer);
2183 /* Request address suggestions for this peer */
2184 set_state_and_timeout (n,
2185 GNUNET_TRANSPORT_PS_INIT_ATS,
2186 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2191 * Try to create a connection to the given target (eventually).
2193 * @param target peer to try to connect to
2196 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2198 struct NeighbourMapEntry *n;
2199 struct GST_BlacklistCheck *blc;
2200 struct BlacklistCheckSwitchContext *blc_ctx;
2202 if (NULL == neighbours)
2204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2205 "Asked to connect to peer `%s' during shutdown\n",
2206 GNUNET_i2s (target));
2207 return; /* during shutdown, do nothing */
2209 n = lookup_neighbour (target);
2210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2211 "Asked to connect to peer `%s' (state: %s)\n",
2212 GNUNET_i2s (target),
2213 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2218 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2219 /* this should not be possible */
2223 case GNUNET_TRANSPORT_PS_INIT_ATS:
2224 case GNUNET_TRANSPORT_PS_SYN_SENT:
2225 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2226 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2227 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2228 "Ignoring request to try to connect to `%s', already trying!\n",
2229 GNUNET_i2s (target));
2230 return; /* already trying */
2231 case GNUNET_TRANSPORT_PS_CONNECTED:
2232 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2233 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2234 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2235 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2236 "Ignoring request to try to connect, already connected to `%s'!\n",
2237 GNUNET_i2s (target));
2238 return; /* already connected */
2239 case GNUNET_TRANSPORT_PS_DISCONNECT:
2240 /* get rid of remains, ready to re-try immediately */
2243 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2244 /* should not be possible */
2248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2249 "Unhandled state `%s'\n",
2250 GNUNET_TRANSPORT_ps2s (n->state));
2257 /* Do blacklist check if connecting to this peer is allowed */
2258 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2259 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2264 (blc = GST_blacklist_test_allowed (target,
2266 &try_connect_bl_check_cont,
2275 * We received a 'SYN' message from the other peer.
2276 * Consider switching to it.
2278 * @param message possibly a 'struct TransportSynMessage' (check format)
2279 * @param peer identity of the peer to switch the address for
2280 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2283 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2284 const struct GNUNET_PeerIdentity *peer)
2286 const struct TransportSynMessage *scm;
2287 struct NeighbourMapEntry *n;
2288 struct GNUNET_TIME_Absolute ts;
2290 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2292 GNUNET_break_op (0);
2293 return GNUNET_SYSERR;
2295 GNUNET_STATISTICS_update (GST_stats,
2297 ("# SYN messages received"),
2299 if (NULL == neighbours)
2301 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2302 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2304 return GNUNET_OK; /* we're shutting down */
2306 scm = (const struct TransportSynMessage *) message;
2307 GNUNET_break_op (0 == ntohl (scm->reserved));
2308 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2309 n = lookup_neighbour (peer);
2312 /* This is a new neighbour and set to not connected */
2313 n = setup_neighbour (peer);
2316 /* Remember this SYN message in neighbour */
2317 n->ack_state = ACK_SEND_SYN_ACK;
2318 n->connect_ack_timestamp = ts;
2320 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2321 "Received SYN for peer `%s' in state %s/%s\n",
2323 GNUNET_TRANSPORT_ps2s (n->state),
2324 print_ack_state (n->ack_state));
2328 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2329 /* Request an address from ATS to send SYN_ACK to this peer */
2330 set_state_and_timeout (n,
2331 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2332 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2334 case GNUNET_TRANSPORT_PS_INIT_ATS:
2335 /* SYN message takes priority over us asking ATS for address:
2336 * Wait for ATS to suggest an address and send SYN_ACK */
2337 set_state_and_timeout (n,
2338 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2339 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2341 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2342 /* We already wait for an address to send an SYN_ACK */
2344 case GNUNET_TRANSPORT_PS_SYN_SENT:
2345 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2346 /* Send ACK immediately */
2347 n->ack_state = ACK_SEND_ACK;
2348 send_syn_ack_message (&n->primary_address,
2351 case GNUNET_TRANSPORT_PS_CONNECTED:
2352 /* we are already connected and can thus send the ACK immediately */
2353 GNUNET_assert (NULL != n->primary_address.address);
2354 GNUNET_assert (NULL != n->primary_address.session);
2355 n->ack_state = ACK_SEND_ACK;
2356 send_syn_ack_message (&n->primary_address,
2359 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2360 /* We wait for ATS address suggestion */
2362 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2363 /* We received a SYN message while waiting for a SYN_ACK in fast
2364 * reconnect. Send SYN_ACK immediately */
2365 n->ack_state = ACK_SEND_ACK;
2366 send_syn_ack_message (&n->primary_address,
2367 n->connect_ack_timestamp);
2369 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2370 /* We are already connected and can thus send the ACK immediately;
2371 still, it can never hurt to have an alternative address, so also
2372 tell ATS about it */
2373 GNUNET_assert (NULL != n->primary_address.address);
2374 GNUNET_assert (NULL != n->primary_address.session);
2375 n->ack_state = ACK_SEND_ACK;
2376 send_syn_ack_message (&n->primary_address,
2379 case GNUNET_TRANSPORT_PS_DISCONNECT:
2380 /* Get rid of remains and re-try */
2382 n = setup_neighbour (peer);
2383 /* Remember the SYN time stamp for ACK message */
2384 n->ack_state = ACK_SEND_SYN_ACK;
2385 n->connect_ack_timestamp = ts;
2386 /* Request an address for the peer */
2387 set_state_and_timeout (n,
2388 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2389 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2391 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2392 /* should not be possible */
2396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2397 "Unhandled state `%s'\n",
2398 GNUNET_TRANSPORT_ps2s (n->state));
2400 return GNUNET_SYSERR;
2407 * Check if the given @a address is the same that we are already
2408 * using for the respective neighbour. If so, update the bandwidth
2409 * assignment and possibly the session and return #GNUNET_OK.
2410 * If the new address is different from what the neighbour is
2411 * using right now, return #GNUNET_NO.
2413 * @param address address of the other peer,
2414 * @param session session to use or NULL if transport should initiate a session
2415 * @param bandwidth_in inbound quota to be used when connection is up,
2416 * 0 to disconnect from peer
2417 * @param bandwidth_out outbound quota to be used when connection is up,
2418 * 0 to disconnect from peer
2419 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2420 * #GNUNET_NO if more extensive changes are required (address changed)
2423 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2424 struct Session *session,
2425 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2426 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2428 struct NeighbourMapEntry *n;
2430 n = lookup_neighbour (&address->peer);
2432 (NULL == n->primary_address.address) ||
2433 (0 != GNUNET_HELLO_address_cmp (address,
2434 n->primary_address.address)) )
2436 /* We are not really switching addresses, but merely adjusting
2437 session and/or bandwidth, can do fast ATS update! */
2438 if (session != n->primary_address.session)
2440 /* switch to a different session, but keeping same address; could
2441 happen if there is a 2nd inbound connection */
2442 n->primary_address.session = session;
2444 n->primary_address.bandwidth_in = bandwidth_in;
2445 n->primary_address.bandwidth_out = bandwidth_out;
2446 GST_neighbours_set_incoming_quota (&address->peer,
2448 send_outbound_quota (&address->peer,
2455 * We've been asked to switch addresses, and just now got the result
2456 * from the blacklist check to see if this is allowed.
2458 * @param cls the `struct BlacklistCheckSwitchContext` with
2459 * the information about the future address
2460 * @param peer the peer we may switch addresses on
2461 * @param result #GNUNET_NO if we are not allowed to use the new
2465 switch_address_bl_check_cont (void *cls,
2466 const struct GNUNET_PeerIdentity *peer,
2469 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2470 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2471 struct NeighbourMapEntry *n;
2473 if (result == GNUNET_NO)
2475 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2476 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2477 GST_plugins_a2s (blc_ctx->address),
2479 GNUNET_i2s (&blc_ctx->address->peer));
2480 GNUNET_STATISTICS_update (GST_stats,
2481 "# ATS suggestions ignored (blacklist denied)",
2484 /* FIXME: tell plugin to force killing session here and now
2485 (note: _proper_ plugin API for this does not yet exist) */
2486 GST_ats_block_address (blc_ctx->address,
2491 papi = GST_plugins_find (blc_ctx->address->transport_name);
2492 GNUNET_assert (NULL != papi);
2494 if (NULL == blc_ctx->session)
2496 /* need to create a session, ATS only gave us an address */
2497 blc_ctx->session = papi->get_session (papi->cls,
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 "Obtained new session for peer `%s' and address '%s': %p\n",
2501 GNUNET_i2s (&blc_ctx->address->peer),
2502 GST_plugins_a2s (blc_ctx->address),
2504 if (NULL != blc_ctx->session)
2505 GST_ats_new_session (blc_ctx->address,
2508 if (NULL == blc_ctx->session)
2510 /* session creation failed, bad!, fail! */
2511 GNUNET_STATISTICS_update (GST_stats,
2512 "# ATS suggestions ignored (failed to create session)",
2515 /* No session could be obtained, remove blacklist check and clean up */
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 "Failed to obtain new session for peer `%s' and address '%s'\n",
2518 GNUNET_i2s (&blc_ctx->address->peer),
2519 GST_plugins_a2s (blc_ctx->address));
2520 GST_ats_block_address (blc_ctx->address,
2525 /* We did this check already before going into blacklist, but
2526 it is theoretically possible that the situation changed in
2527 the meantime, hence we check again here */
2529 try_run_fast_ats_update (blc_ctx->address,
2531 blc_ctx->bandwidth_in,
2532 blc_ctx->bandwidth_out))
2533 goto cleanup; /* was just a minor update, we're done */
2535 /* check if we also need to setup the neighbour entry */
2536 if (NULL == (n = lookup_neighbour (peer)))
2538 n = setup_neighbour (peer);
2539 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2542 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2543 "Peer `%s' switches to address `%s'\n",
2544 GNUNET_i2s (&blc_ctx->address->peer),
2545 GST_plugins_a2s (blc_ctx->address));
2549 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2551 GST_ats_block_address (blc_ctx->address,
2555 case GNUNET_TRANSPORT_PS_INIT_ATS:
2556 /* We requested an address and ATS suggests one:
2557 * set primary address and send SYN message*/
2558 set_primary_address (n,
2561 blc_ctx->bandwidth_in,
2562 blc_ctx->bandwidth_out);
2563 if (ACK_SEND_SYN_ACK == n->ack_state)
2565 /* Send pending SYN_ACK message */
2566 n->ack_state = ACK_SEND_ACK;
2567 send_syn_ack_message (&n->primary_address,
2568 n->connect_ack_timestamp);
2570 set_state_and_timeout (n,
2571 GNUNET_TRANSPORT_PS_SYN_SENT,
2572 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2573 send_syn (&n->primary_address);
2575 case GNUNET_TRANSPORT_PS_SYN_SENT:
2576 /* ATS suggested a new address while waiting for an SYN_ACK:
2577 * Switch and send new SYN */
2578 /* ATS suggests a different address, switch again */
2579 set_primary_address (n,
2582 blc_ctx->bandwidth_in,
2583 blc_ctx->bandwidth_out);
2584 if (ACK_SEND_SYN_ACK == n->ack_state)
2586 /* Send pending SYN_ACK message */
2587 n->ack_state = ACK_SEND_ACK;
2588 send_syn_ack_message (&n->primary_address,
2589 n->connect_ack_timestamp);
2591 set_state_and_timeout (n,
2592 GNUNET_TRANSPORT_PS_SYN_SENT,
2593 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2594 send_syn (&n->primary_address);
2596 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2597 /* We requested an address and ATS suggests one:
2598 * set primary address and send SYN_ACK message*/
2599 set_primary_address (n,
2602 blc_ctx->bandwidth_in,
2603 blc_ctx->bandwidth_out);
2604 /* Send an ACK message as a response to the SYN msg */
2605 set_state_and_timeout (n,
2606 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2607 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2608 send_syn_ack_message (&n->primary_address,
2609 n->connect_ack_timestamp);
2610 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2611 (ACK_UNDEFINED == n->ack_state) )
2612 n->ack_state = ACK_SEND_ACK;
2614 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2615 /* ATS asks us to switch while we were trying to connect; switch to new
2616 address and check blacklist again */
2617 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2619 n->ack_state = ACK_SEND_ACK;
2620 send_syn_ack_message (&n->primary_address,
2621 n->connect_ack_timestamp);
2623 set_primary_address (n,
2626 blc_ctx->bandwidth_in,
2627 blc_ctx->bandwidth_out);
2628 set_state_and_timeout (n,
2629 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2630 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2632 case GNUNET_TRANSPORT_PS_CONNECTED:
2633 GNUNET_assert (NULL != n->primary_address.address);
2634 GNUNET_assert (NULL != n->primary_address.session);
2635 GNUNET_break (n->primary_address.session != blc_ctx->session);
2636 /* ATS asks us to switch a life connection; see if we can get
2637 a SYN_ACK on it before we actually do this! */
2638 set_alternative_address (n,
2641 blc_ctx->bandwidth_in,
2642 blc_ctx->bandwidth_out);
2643 set_state_and_timeout (n,
2644 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2645 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2646 GNUNET_STATISTICS_update (GST_stats,
2647 gettext_noop ("# Attempts to switch addresses"),
2650 send_syn (&n->alternative_address);
2652 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2653 set_primary_address (n,
2656 blc_ctx->bandwidth_in,
2657 blc_ctx->bandwidth_out);
2658 if (ACK_SEND_SYN_ACK == n->ack_state)
2660 /* Send pending SYN_ACK message */
2661 n->ack_state = ACK_SEND_ACK;
2662 send_syn_ack_message (&n->primary_address,
2663 n->connect_ack_timestamp);
2665 set_state_and_timeout (n,
2666 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2667 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2668 send_syn (&n->primary_address);
2670 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2671 /* ATS asks us to switch while we were trying to reconnect; switch to new
2672 address and send SYN again */
2673 set_primary_address (n,
2676 blc_ctx->bandwidth_in,
2677 blc_ctx->bandwidth_out);
2678 set_state_and_timeout (n,
2679 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2680 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2681 send_syn (&n->primary_address);
2683 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2684 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2685 blc_ctx->address)) &&
2686 (n->primary_address.session == blc_ctx->session) )
2688 /* ATS switches back to still-active session */
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2690 "ATS double-switched, cleaning up alternative address\n");
2691 free_address (&n->alternative_address);
2692 set_state_and_timeout (n,
2693 GNUNET_TRANSPORT_PS_CONNECTED,
2697 /* ATS asks us to switch a life connection, send */
2698 set_alternative_address (n,
2701 blc_ctx->bandwidth_in,
2702 blc_ctx->bandwidth_out);
2703 set_state_and_timeout (n,
2704 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2705 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2706 send_syn (&n->alternative_address);
2708 case GNUNET_TRANSPORT_PS_DISCONNECT:
2709 /* not going to switch addresses while disconnecting */
2710 GNUNET_STATISTICS_update (GST_stats,
2711 "# ATS suggestion ignored (disconnecting)",
2715 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2719 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2720 "Unhandled state `%s'\n",
2721 GNUNET_TRANSPORT_ps2s (n->state));
2726 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2729 GNUNET_HELLO_address_free (blc_ctx->address);
2730 GNUNET_free (blc_ctx);
2735 * For the given peer, switch to this address.
2737 * Before accepting this addresses and actively using it, a blacklist check
2740 * If any check fails or the suggestion can somehow not be followed, we
2741 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2742 * could not be satisfied and force ATS to do something else.
2744 * @param address address of the other peer,
2745 * @param session session to use or NULL if transport should initiate a session
2746 * @param bandwidth_in inbound quota to be used when connection is up,
2747 * 0 to disconnect from peer
2748 * @param bandwidth_out outbound quota to be used when connection is up,
2749 * 0 to disconnect from peer
2752 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2753 struct Session *session,
2754 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2755 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2757 struct GST_BlacklistCheck *blc;
2758 struct BlacklistCheckSwitchContext *blc_ctx;
2760 GNUNET_assert (NULL != address->transport_name);
2762 try_run_fast_ats_update (address,
2768 /* Check if plugin is available */
2769 if (NULL == (GST_plugins_find (address->transport_name)))
2771 /* we don't have the plugin for this address */
2773 GST_ats_block_address (address,
2777 if ((NULL == session) &&
2778 (GNUNET_HELLO_address_check_option (address,
2779 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2781 /* This is a inbound address and we do not have a session to use! */
2783 GST_ats_block_address (address,
2788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2789 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2790 GST_plugins_a2s (address),
2791 GNUNET_i2s (&address->peer),
2792 (unsigned int) ntohl (bandwidth_in.value__),
2793 (unsigned int) ntohl (bandwidth_out.value__));
2795 /* Perform blacklist check */
2796 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2797 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2798 blc_ctx->session = session;
2799 blc_ctx->bandwidth_in = bandwidth_in;
2800 blc_ctx->bandwidth_out = bandwidth_out;
2801 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2804 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2805 address->transport_name,
2806 &switch_address_bl_check_cont,
2815 * Function called to send network utilization data to ATS for
2816 * each active connection.
2819 * @param key peer we send utilization data for
2820 * @param value the `struct NeighbourMapEntry *` with data to send
2821 * @return #GNUNET_OK (continue to iterate)
2824 send_utilization_data (void *cls,
2825 const struct GNUNET_PeerIdentity *key,
2828 struct NeighbourMapEntry *n = value;
2831 struct GNUNET_TIME_Relative delta;
2833 if ( (GNUNET_YES != test_connected (n)) ||
2834 (NULL == n->primary_address.address) )
2836 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2837 GNUNET_TIME_absolute_get ());
2839 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2840 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2842 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2843 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2846 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2850 GST_ats_update_utilization (n->primary_address.address,
2853 n->util_total_bytes_recv = 0;
2854 n->util_total_bytes_sent = 0;
2855 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2861 * Task transmitting utilization in a regular interval
2863 * @param cls the 'struct NeighbourMapEntry' for which we are running
2864 * @param tc scheduler context (unused)
2867 utilization_transmission (void *cls,
2868 const struct GNUNET_SCHEDULER_TaskContext *tc)
2870 util_transmission_tk = NULL;
2871 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
2872 &send_utilization_data,
2874 util_transmission_tk
2875 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2876 &utilization_transmission,
2882 * Track information about data we received from the
2883 * given address (used to notify ATS about our utilization
2884 * of allocated resources).
2886 * @param address the address we got data from
2887 * @param message the message we received (really only the size is used)
2890 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2891 const struct GNUNET_MessageHeader *message)
2893 struct NeighbourMapEntry *n;
2895 n = lookup_neighbour (&address->peer);
2898 n->util_total_bytes_recv += ntohs (message->size);
2903 * Track information about data we transmitted using the given @a
2904 * address and @a session (used to notify ATS about our utilization of
2905 * allocated resources).
2907 * @param address the address we transmitted data to
2908 * @param session session we used to transmit data
2909 * @param message the message we sent (really only the size is used)
2912 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2913 struct Session *session,
2916 struct NeighbourMapEntry *n;
2918 n = lookup_neighbour (&address->peer);
2921 if (n->primary_address.session != session)
2923 n->util_total_bytes_sent += size;
2928 * Master task run for every neighbour. Performs all of the time-related
2929 * activities (keep alive, send next message, disconnect if idle, finish
2930 * clean up after disconnect).
2932 * @param cls the 'struct NeighbourMapEntry' for which we are running
2933 * @param tc scheduler context (unused)
2936 master_task (void *cls,
2937 const struct GNUNET_SCHEDULER_TaskContext *tc)
2939 struct NeighbourMapEntry *n = cls;
2940 struct GNUNET_TIME_Relative delay;
2943 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2945 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2946 GNUNET_i2s (&n->id),
2947 GNUNET_TRANSPORT_ps2s(n->state),
2948 GNUNET_STRINGS_relative_time_to_string (delay,
2952 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2953 /* invalid state for master task, clean up */
2957 case GNUNET_TRANSPORT_PS_INIT_ATS:
2958 if (0 == delay.rel_value_us)
2960 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2961 "Connection to `%s' timed out waiting for ATS to provide address\n",
2962 GNUNET_i2s (&n->id));
2967 case GNUNET_TRANSPORT_PS_SYN_SENT:
2968 if (0 == delay.rel_value_us)
2970 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2971 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2972 GNUNET_i2s (&n->id));
2973 /* Remove address and request and additional one */
2974 unset_primary_address (n);
2975 set_state_and_timeout (n,
2976 GNUNET_TRANSPORT_PS_INIT_ATS,
2977 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2981 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2982 if (0 == delay.rel_value_us)
2984 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2985 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2986 GNUNET_i2s (&n->id));
2991 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2992 if (0 == delay.rel_value_us)
2994 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2995 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2996 GNUNET_i2s (&n->id));
2997 disconnect_neighbour (n);
3001 case GNUNET_TRANSPORT_PS_CONNECTED:
3002 if (0 == delay.rel_value_us)
3004 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3005 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3006 GNUNET_i2s (&n->id));
3007 disconnect_neighbour (n);
3010 try_transmission_to_peer (n);
3013 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3014 if (0 == delay.rel_value_us)
3016 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3017 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3018 GNUNET_i2s (&n->id));
3019 disconnect_neighbour (n);
3023 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3024 if (0 == delay.rel_value_us)
3026 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3027 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
3028 GNUNET_i2s (&n->id));
3029 disconnect_neighbour (n);
3033 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3034 if (0 == delay.rel_value_us)
3036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3037 "Switch failed, cleaning up alternative address\n");
3038 free_address (&n->alternative_address);
3039 set_state_and_timeout (n,
3040 GNUNET_TRANSPORT_PS_CONNECTED,
3041 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
3043 try_transmission_to_peer (n);
3046 case GNUNET_TRANSPORT_PS_DISCONNECT:
3047 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3048 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3049 GNUNET_i2s (&n->id));
3052 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3053 /* how did we get here!? */
3057 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3058 "Unhandled state `%s'\n",
3059 GNUNET_TRANSPORT_ps2s (n->state));
3063 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3064 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3065 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3067 /* if we are *now* in one of the two states, we're sending
3068 keep alive messages, so we need to consider the keepalive
3069 delay, not just the connection timeout */
3070 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3073 if (NULL == n->task)
3074 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3081 * Send a ACK message to the neighbour to confirm that we
3084 * @param n neighbour to send the ACK to
3087 send_session_ack_message (struct NeighbourMapEntry *n)
3089 struct GNUNET_MessageHeader msg;
3091 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3092 "Sending ACK message to peer `%s'\n",
3093 GNUNET_i2s (&n->id));
3095 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3096 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3097 (void) send_with_session (n,
3099 sizeof (struct GNUNET_MessageHeader),
3101 GNUNET_TIME_UNIT_FOREVER_REL,
3108 * We received a 'SESSION_SYN_ACK' message from the other peer.
3109 * Consider switching to it.
3111 * @param message possibly a `struct SessionConnectMessage` (check format)
3112 * @param peer identity of the peer to switch the address for
3113 * @param address address of the other peer, NULL if other peer
3115 * @param session session to use (or NULL)
3116 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3119 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3120 const struct GNUNET_HELLO_Address *address,
3121 struct Session *session)
3123 const struct TransportSynMessage *scm;
3124 struct GNUNET_TIME_Absolute ts;
3125 struct NeighbourMapEntry *n;
3127 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3129 GNUNET_break_op (0);
3130 return GNUNET_SYSERR;
3132 GNUNET_STATISTICS_update (GST_stats,
3134 ("# SYN_ACK messages received"),
3136 scm = (const struct TransportSynMessage *) message;
3137 GNUNET_break_op (ntohl (scm->reserved) == 0);
3138 if (NULL == (n = lookup_neighbour (&address->peer)))
3140 GNUNET_STATISTICS_update (GST_stats,
3142 ("# unexpected SYN_ACK messages (no peer)"),
3144 return GNUNET_SYSERR;
3146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3147 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3148 GNUNET_i2s (&address->peer),
3149 GNUNET_TRANSPORT_ps2s (n->state),
3150 print_ack_state (n->ack_state));
3151 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3154 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3157 return GNUNET_SYSERR;
3158 case GNUNET_TRANSPORT_PS_INIT_ATS:
3159 GNUNET_STATISTICS_update (GST_stats,
3160 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3164 case GNUNET_TRANSPORT_PS_SYN_SENT:
3165 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3167 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3168 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3171 set_state_and_timeout (n,
3172 GNUNET_TRANSPORT_PS_CONNECTED,
3173 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3174 set_primary_address (n,
3175 n->primary_address.address,
3176 n->primary_address.session,
3177 n->primary_address.bandwidth_in,
3178 n->primary_address.bandwidth_out);
3179 send_session_ack_message (n);
3181 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3182 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3183 GNUNET_STATISTICS_update (GST_stats,
3184 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3188 case GNUNET_TRANSPORT_PS_CONNECTED:
3189 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3190 send_session_ack_message (n);
3192 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3193 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3194 to give us a new address... */
3195 GNUNET_STATISTICS_update (GST_stats,
3196 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3200 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3201 /* Reconnecting with new address address worked; go back to connected! */
3202 set_state_and_timeout (n,
3203 GNUNET_TRANSPORT_PS_CONNECTED,
3204 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3205 send_session_ack_message (n);
3207 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3208 /* new address worked; adopt it and go back to connected! */
3209 set_state_and_timeout (n,
3210 GNUNET_TRANSPORT_PS_CONNECTED,
3211 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3212 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3214 /* Set primary addresses */
3215 set_primary_address (n,
3216 n->alternative_address.address,
3217 n->alternative_address.session,
3218 n->alternative_address.bandwidth_in,
3219 n->alternative_address.bandwidth_out);
3220 GNUNET_STATISTICS_update (GST_stats,
3221 gettext_noop ("# Successful attempts to switch addresses"),
3225 GNUNET_HELLO_address_free (n->alternative_address.address);
3226 memset (&n->alternative_address,
3228 sizeof (n->alternative_address));
3229 send_session_ack_message (n);
3231 case GNUNET_TRANSPORT_PS_DISCONNECT:
3232 GNUNET_STATISTICS_update (GST_stats,
3234 ("# unexpected SYN_ACK messages (disconnecting)"),
3236 return GNUNET_SYSERR;
3237 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3242 "Unhandled state `%s'\n",
3243 GNUNET_TRANSPORT_ps2s (n->state));
3245 return GNUNET_SYSERR;
3252 * A session was terminated. Take note; if needed, try to get
3253 * an alternative address from ATS.
3255 * @param peer identity of the peer where the session died
3256 * @param session session that is gone
3257 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3258 * this session was not in use
3261 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3262 struct Session *session)
3264 struct NeighbourMapEntry *n;
3265 struct BlackListCheckContext *bcc;
3266 struct BlackListCheckContext *bcc_next;
3268 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3270 while (NULL != (bcc = bcc_next))
3272 bcc_next = bcc->next;
3273 if (bcc->na.session == session)
3275 if (NULL != bcc->bc)
3276 GST_blacklist_test_cancel (bcc->bc);
3277 GNUNET_HELLO_address_free (bcc->na.address);
3278 GNUNET_CONTAINER_DLL_remove (bc_head,
3284 if (NULL == (n = lookup_neighbour (peer)))
3285 return GNUNET_NO; /* can't affect us */
3286 if (session != n->primary_address.session)
3288 /* Free alternative address */
3289 if (session == n->alternative_address.session)
3291 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3292 set_state_and_timeout (n,
3293 GNUNET_TRANSPORT_PS_CONNECTED,
3295 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3296 "Session died, cleaning up alternative address\n");
3297 free_address (&n->alternative_address);
3299 return GNUNET_NO; /* doesn't affect us further */
3302 n->expect_latency_response = GNUNET_NO;
3303 /* The session for neighbour's primary address died */
3306 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3310 case GNUNET_TRANSPORT_PS_INIT_ATS:
3314 case GNUNET_TRANSPORT_PS_SYN_SENT:
3315 /* The session used to send the SYN terminated:
3316 * this implies a connect error*/
3317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3318 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3320 GST_plugins_a2s (n->primary_address.address),
3321 n->primary_address.session,
3324 /* Destroy the address since it cannot be used */
3325 unset_primary_address (n);
3326 set_state_and_timeout (n,
3327 GNUNET_TRANSPORT_PS_INIT_ATS,
3328 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3330 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3331 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3332 /* error on inbound session; free neighbour entirely */
3335 case GNUNET_TRANSPORT_PS_CONNECTED:
3336 /* Our primary connection died, try a fast reconnect */
3337 unset_primary_address (n);
3338 set_state_and_timeout (n,
3339 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3340 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3342 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3343 /* we don't have an address, how can it go down? */
3346 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3348 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3350 GST_plugins_a2s (n->primary_address.address),
3351 n->primary_address.session,
3353 /* Destroy the address since it cannot be used */
3354 unset_primary_address (n);
3355 set_state_and_timeout (n,
3356 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3357 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3359 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3360 /* primary went down while we were waiting for SYN_ACK on secondary;
3361 secondary as primary */
3363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3364 "Connection `%s' %p to peer `%s' was terminated while switching, "
3365 "switching to alternative address `%s' %p\n",
3366 GST_plugins_a2s (n->primary_address.address),
3367 n->primary_address.session,
3369 GST_plugins_a2s (n->alternative_address.address),
3370 n->alternative_address.session);
3372 /* Destroy the inbound address since it cannot be used */
3373 free_address (&n->primary_address);
3374 n->primary_address = n->alternative_address;
3375 memset (&n->alternative_address,
3377 sizeof (struct NeighbourAddress));
3378 set_state_and_timeout (n,
3379 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3380 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3382 case GNUNET_TRANSPORT_PS_DISCONNECT:
3383 unset_primary_address (n);
3385 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3386 /* neighbour was freed and plugins told to terminate session */
3389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3390 "Unhandled state `%s'\n",
3391 GNUNET_TRANSPORT_ps2s (n->state));
3395 if (NULL != n->task)
3396 GNUNET_SCHEDULER_cancel (n->task);
3397 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3403 * We received a 'ACK' message from the other peer.
3404 * If we sent a 'SYN_ACK' last, this means we are now
3405 * connected. Otherwise, do nothing.
3407 * @param message possibly a 'struct SessionConnectMessage' (check format)
3408 * @param address address of the other peer
3409 * @param session session to use (or NULL)
3410 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3413 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3414 const struct GNUNET_HELLO_Address *address,
3415 struct Session *session)
3417 struct NeighbourMapEntry *n;
3419 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3421 GNUNET_break_op (0);
3422 return GNUNET_SYSERR;
3424 GNUNET_STATISTICS_update (GST_stats,
3425 gettext_noop ("# ACK messages received"),
3428 if (NULL == (n = lookup_neighbour (&address->peer)))
3430 GNUNET_break_op (0);
3431 return GNUNET_SYSERR;
3433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3434 "Received ACK for peer `%s' in state %s/%s\n",
3435 GNUNET_i2s (&address->peer),
3436 GNUNET_TRANSPORT_ps2s (n->state),
3437 print_ack_state (n->ack_state));
3439 /* Check if we are in a plausible state for having sent
3440 a SYN_ACK. If not, return, otherwise break.
3442 The remote peers sends a ACK as a response for a SYN_ACK
3446 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3447 now wait for the ACK to finally be connected
3448 - If we sent a SYN_ACK to this peer before */
3450 if ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3451 (ACK_SEND_ACK != n->ack_state))
3453 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3454 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3455 GNUNET_i2s (&address->peer),
3456 GNUNET_TRANSPORT_ps2s (n->state),
3457 print_ack_state (n->ack_state));
3459 GNUNET_STATISTICS_update (GST_stats,
3460 gettext_noop ("# unexpected ACK messages"), 1,
3464 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3466 /* We tried to switch addresses while being connect. We explicitly wait
3467 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3468 * so we do not want to set the address as in use! */
3471 set_state_and_timeout (n,
3472 GNUNET_TRANSPORT_PS_CONNECTED,
3473 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3475 /* Reset backoff for primary address */
3476 GST_ats_block_reset (n->primary_address.address,
3477 n->primary_address.session);
3483 * Test if we're connected to the given peer.
3485 * @param target peer to test
3486 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3489 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3491 return test_connected (lookup_neighbour (target));
3496 * Change the incoming quota for the given peer.
3498 * @param neighbour identity of peer to change qutoa for
3499 * @param quota new quota
3502 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3503 struct GNUNET_BANDWIDTH_Value32NBO quota)
3505 struct NeighbourMapEntry *n;
3507 if (NULL == (n = lookup_neighbour (neighbour)))
3509 GNUNET_STATISTICS_update (GST_stats,
3511 ("# SET QUOTA messages ignored (no such peer)"),
3515 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3516 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3517 ntohl (quota.value__), GNUNET_i2s (&n->id));
3518 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3519 if (0 != ntohl (quota.value__))
3521 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3522 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3523 GNUNET_i2s (&n->id));
3524 if (GNUNET_YES == test_connected (n))
3525 GNUNET_STATISTICS_update (GST_stats,
3526 gettext_noop ("# disconnects due to quota of 0"),
3528 disconnect_neighbour (n);
3533 * Task to asynchronously run #free_neighbour().
3535 * @param cls the `struct NeighbourMapEntry` to free
3539 delayed_disconnect (void *cls,
3540 const struct GNUNET_SCHEDULER_TaskContext* tc)
3542 struct NeighbourMapEntry *n = cls;
3544 n->delayed_disconnect_task = NULL;
3545 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3546 "Disconnecting by request from peer %s\n",
3547 GNUNET_i2s (&n->id));
3553 * We received a disconnect message from the given peer,
3554 * validate and process.
3556 * @param peer sender of the message
3557 * @param msg the disconnect message
3560 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3561 const struct GNUNET_MessageHeader *msg)
3563 struct NeighbourMapEntry *n;
3564 const struct SessionDisconnectMessage *sdm;
3566 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3567 "Received DISCONNECT message from peer `%s'\n",
3569 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3571 GNUNET_break_op (0);
3572 GNUNET_STATISTICS_update (GST_stats,
3574 ("# disconnect messages ignored (malformed)"), 1,
3578 GNUNET_STATISTICS_update (GST_stats,
3580 ("# DISCONNECT messages received"),
3582 sdm = (const struct SessionDisconnectMessage *) msg;
3583 if (NULL == (n = lookup_neighbour (peer)))
3588 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3590 GNUNET_STATISTICS_update (GST_stats,
3591 gettext_noop ("# disconnect messages ignored (timestamp)"),
3596 if (0 != memcmp (peer,
3598 sizeof (struct GNUNET_PeerIdentity)))
3600 GNUNET_break_op (0);
3603 if (ntohl (sdm->purpose.size) !=
3604 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3605 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3606 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3609 "DISCONNECT message from peer `%s' has invalid size\n",
3611 GNUNET_break_op (0);
3615 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3621 "DISCONNECT message from peer `%s' cannot be verified \n",
3623 GNUNET_break_op (0);
3626 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect, n);
3631 * Closure for the #neighbours_iterate() function.
3633 struct IteratorContext
3636 * Function to call on each connected neighbour.
3638 GST_NeighbourIterator cb;
3641 * Closure for @e cb.
3648 * Call the callback from the closure for each neighbour.
3650 * @param cls the `struct IteratorContext`
3651 * @param key the hash of the public key of the neighbour
3652 * @param value the `struct NeighbourMapEntry`
3653 * @return #GNUNET_OK (continue to iterate)
3656 neighbours_iterate (void *cls,
3657 const struct GNUNET_PeerIdentity *key,
3660 struct IteratorContext *ic = cls;
3661 struct NeighbourMapEntry *n = value;
3662 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3663 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3665 if (NULL != n->primary_address.address)
3667 bandwidth_in = n->primary_address.bandwidth_in;
3668 bandwidth_out = n->primary_address.bandwidth_out;
3672 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3673 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3677 n->primary_address.address,
3680 bandwidth_in, bandwidth_out);
3686 * Iterate over all connected neighbours.
3688 * @param cb function to call
3689 * @param cb_cls closure for cb
3692 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3694 struct IteratorContext ic;
3696 if (NULL == neighbours)
3697 return; /* can happen during shutdown */
3700 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3701 &neighbours_iterate,
3707 * If we have an active connection to the given target, it must be shutdown.
3709 * @param target peer to disconnect from
3712 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3714 struct NeighbourMapEntry *n;
3716 if (NULL == (n = lookup_neighbour (target)))
3717 return; /* not active */
3718 if (GNUNET_YES == test_connected (n))
3719 GNUNET_STATISTICS_update (GST_stats,
3720 gettext_noop ("# disconnected from peer upon explicit request"),
3723 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3724 "Forced disconnect from peer %s\n",
3725 GNUNET_i2s (target));
3726 disconnect_neighbour (n);
3731 * Obtain current address information for the given neighbour.
3734 * @return address currently used
3736 struct GNUNET_HELLO_Address *
3737 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3739 struct NeighbourMapEntry *n;
3741 n = lookup_neighbour (peer);
3744 return n->primary_address.address;
3749 * Initialize the neighbours subsystem.
3751 * @param max_fds maximum number of fds to use
3754 GST_neighbours_start (unsigned int max_fds)
3756 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3758 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3759 &utilization_transmission,
3765 * Disconnect from the given neighbour.
3768 * @param key hash of neighbour's public key (not used)
3769 * @param value the 'struct NeighbourMapEntry' of the neighbour
3770 * @return #GNUNET_OK (continue to iterate)
3773 disconnect_all_neighbours (void *cls,
3774 const struct GNUNET_PeerIdentity *key,
3777 struct NeighbourMapEntry *n = value;
3779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3780 "Disconnecting peer `%4s' during shutdown\n",
3781 GNUNET_i2s (&n->id));
3788 * Cleanup the neighbours subsystem.
3791 GST_neighbours_stop ()
3793 struct BlacklistCheckSwitchContext *cur;
3794 struct BlacklistCheckSwitchContext *next;
3796 if (NULL == neighbours)
3798 if (NULL != util_transmission_tk)
3800 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3801 util_transmission_tk = NULL;
3803 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3804 &disconnect_all_neighbours,
3806 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3808 next = pending_bc_head;
3809 for (cur = next; NULL != cur; cur = next)
3812 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
3816 if (NULL != cur->blc)
3818 GST_blacklist_test_cancel (cur->blc);
3821 if (NULL != cur->address)
3822 GNUNET_HELLO_address_free (cur->address);
3828 /* end of file gnunet-service-transport_neighbours.c */