2 This file is part of GNUnet.
3 (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"
43 * Size of the neighbour hash map.
45 #define NEIGHBOUR_TABLE_SIZE 256
48 * Time we give plugin to transmit DISCONNECT message before the
49 * neighbour entry self-destructs.
51 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
54 * How often must a peer violate bandwidth quotas before we start
55 * to simply drop its messages?
57 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
60 * How long are we willing to wait for a response from ATS before timing out?
62 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
65 * How long are we willing to wait for an ACK from the other peer before
66 * giving up on our connect operation?
68 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
71 * How long are we willing to wait for a successful reconnect if
72 * an existing connection went down? Much shorter than the
73 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
74 * higher layers about the disconnect during this period.
76 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
79 * Interval to send utilization data
81 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_UNIT_SECONDS
84 * State describing which kind a reply this neighbour should send
89 * We did not receive a SYN message for this neighbour
94 * The neighbour received a SYN message and has to send a SYN_ACK
100 * The neighbour sent a SYN_ACK message and has to send a ACK
107 GNUNET_NETWORK_STRUCT_BEGIN
110 * Message a peer sends to another to indicate that it intends to
111 * setup a connection/session for data exchange. A 'SESSION_SYN'
112 * should be answered with a 'SESSION_SYN_ACK' with the same body
113 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
114 * a 'ACK'. Once the 'ACK' is received, both peers
115 * should be connected.
117 struct TransportSynMessage
120 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
121 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
123 struct GNUNET_MessageHeader header;
128 uint32_t reserved GNUNET_PACKED;
131 * Absolute time at the sender. Only the most recent connect
132 * message implies which session is preferred by the sender.
134 struct GNUNET_TIME_AbsoluteNBO timestamp;
140 * Message a peer sends to another when connected to indicate that a
141 * session is in use and the peer is still alive or to respond to a keep alive.
142 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
143 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
144 * When the keep alive response with type is received, transport service
145 * will call the respective plugin to update the session timeout
147 struct SessionKeepAliveMessage
150 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
151 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
153 struct GNUNET_MessageHeader header;
156 * A nonce to identify the session the keep alive is used for
158 uint32_t nonce GNUNET_PACKED;
162 * Message we send to the other peer to notify him that we intentionally
163 * are disconnecting (to reduce timeouts). This is just a friendly
164 * notification, peers must not rely on always receiving disconnect
167 struct SessionDisconnectMessage
170 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
172 struct GNUNET_MessageHeader header;
177 uint32_t reserved GNUNET_PACKED;
180 * Purpose of the signature. Extends over the timestamp.
181 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
183 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
186 * Absolute time at the sender. Only the most recent connect
187 * message implies which session is preferred by the sender.
189 struct GNUNET_TIME_AbsoluteNBO timestamp;
192 * Public key of the sender.
194 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
197 * Signature of the peer that sends us the disconnect. Only
198 * valid if the timestamp is AFTER the timestamp from the
199 * corresponding 'SYN' message.
201 struct GNUNET_CRYPTO_EddsaSignature signature;
205 GNUNET_NETWORK_STRUCT_END
209 * For each neighbour we keep a list of messages
210 * that we still want to transmit to the neighbour.
216 * This is a doubly linked list.
218 struct MessageQueue *next;
221 * This is a doubly linked list.
223 struct MessageQueue *prev;
226 * Function to call once we're done.
228 GST_NeighbourSendContinuation cont;
231 * Closure for @e cont
236 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
237 * stuck together in memory. Allocated at the end of this struct.
239 const char *message_buf;
242 * Size of the message buf
244 size_t message_buf_size;
247 * At what time should we fail?
249 struct GNUNET_TIME_Absolute timeout;
255 * A possible address we could use to communicate with a neighbour.
257 struct NeighbourAddress
261 * Active session for this address.
263 struct Session *session;
266 * Network-level address information.
268 struct GNUNET_HELLO_Address *address;
271 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
272 * peer for this address. Use to check that the ACK is in response
273 * to our most recent 'SYN'.
275 struct GNUNET_TIME_Absolute connect_timestamp;
278 * Inbound bandwidth from ATS for this address.
280 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
283 * Outbound bandwidth from ATS for this address.
285 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
288 * Did we tell ATS that this is our 'active' address?
293 * The current nonce sent in the last keep alive messages
295 uint32_t keep_alive_nonce;
300 * Entry in neighbours.
302 struct NeighbourMapEntry
306 * Head of list of messages we would like to send to this peer;
307 * must contain at most one message per client.
309 struct MessageQueue *messages_head;
312 * Tail of list of messages we would like to send to this peer; must
313 * contain at most one message per client.
315 struct MessageQueue *messages_tail;
318 * Are we currently trying to send a message? If so, which one?
320 struct MessageQueue *is_active;
323 * Primary address we currently use to communicate with the neighbour.
325 struct NeighbourAddress primary_address;
328 * Alternative address currently under consideration for communicating
329 * with the neighbour.
331 struct NeighbourAddress alternative_address;
334 * Identity of this neighbour.
336 struct GNUNET_PeerIdentity id;
339 * Main task that drives this peer (timeouts, keepalives, etc.).
340 * Always runs the 'master_task'.
342 struct GNUNET_SCHEDULER_Task *task;
345 * Task to disconnect neighbour after we received a DISCONNECT message
347 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
350 * At what time should we sent the next keep-alive message?
352 struct GNUNET_TIME_Absolute keep_alive_time;
355 * At what time did we sent the last keep-alive message? Used
356 * to calculate round-trip time ("latency").
358 struct GNUNET_TIME_Absolute last_keep_alive_time;
361 * Timestamp we should include in our next SYN_ACK message.
362 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
363 * our SYN_ACK message.
365 struct GNUNET_TIME_Absolute connect_ack_timestamp;
368 * ATS address suggest handle
370 struct GNUNET_ATS_ConnectivitySuggestHandle *suggest_handle;
373 * Time where we should cut the connection (timeout) if we don't
374 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
375 * if we are in #S_CONNECTED).
377 struct GNUNET_TIME_Absolute timeout;
380 * Tracker for inbound bandwidth.
382 struct GNUNET_BANDWIDTH_Tracker in_tracker;
385 * How often has the other peer (recently) violated the inbound
386 * traffic limit? Incremented by 10 per violation, decremented by 1
387 * per non-violation (for each time interval).
389 unsigned int quota_violation_count;
392 * The current state of the peer.
394 enum GNUNET_TRANSPORT_PeerState state;
397 * Did we sent an KEEP_ALIVE message and are we expecting a response?
399 int expect_latency_response;
402 * When a peer wants to connect we have to reply to the 1st SYN message
403 * with a SYN_ACK message. But sometime we cannot send this message
404 * immediately since we do not have an address and then we have to remember
405 * to send this message as soon as we have an address.
407 * Flag to set if we still need to send a SYN_ACK message to the other peer
408 * (once we have an address to use and the peer has been allowed by our
409 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
410 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
411 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
412 * 'ACK' (regardless of what our own state machine might say).
414 enum GST_ACK_State ack_state;
417 * Tracking utilization of outbound bandwidth
419 uint32_t util_payload_bytes_sent;
422 * Tracking utilization of inbound bandwidth
424 uint32_t util_payload_bytes_recv;
427 * Tracking utilization of outbound bandwidth
429 uint32_t util_total_bytes_sent;
432 * Tracking utilization of inbound bandwidth
434 uint32_t util_total_bytes_recv;
437 * Date of last utilization transmission
439 struct GNUNET_TIME_Absolute last_util_transmission;
444 * Context for blacklist checks and the #try_connect_bl_check_cont()
445 * function. Stores information about ongoing blacklist checks.
447 struct BlackListCheckContext
451 * We keep blacklist checks in a DLL.
453 struct BlackListCheckContext *next;
456 * We keep blacklist checks in a DLL.
458 struct BlackListCheckContext *prev;
461 * Address that is being checked.
463 struct NeighbourAddress na;
466 * Handle to the ongoing blacklist check.
468 struct GST_BlacklistCheck *bc;
473 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
475 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
478 * We keep blacklist checks in a DLL so that we can find
479 * the 'sessions' in their 'struct NeighbourAddress' if
480 * a session goes down.
482 static struct BlackListCheckContext *bc_head;
485 * We keep blacklist checks in a DLL.
487 static struct BlackListCheckContext *bc_tail;
490 * List of pending blacklist checks: head
492 static struct BlacklistCheckSwitchContext *pending_bc_head;
495 * List of pending blacklist checks: tail
497 static struct BlacklistCheckSwitchContext *pending_bc_tail;
500 * counter for connected neighbours
502 static unsigned int neighbours_connected;
505 * Number of bytes we have currently queued for transmission.
507 static unsigned long long bytes_in_send_queue;
510 * Task transmitting utilization data
512 static struct GNUNET_SCHEDULER_Task * util_transmission_tk;
519 print_ack_state (enum GST_ACK_State s)
524 case ACK_SEND_SYN_ACK:
525 return "SEND_SYN_ACK";
536 * Notify our clients that another peer connected to us.
538 * @param peer the peer that connected
539 * @param bandwidth_in inbound bandwidth in NBO
540 * @param bandwidth_out outbound bandwidth in NBO
543 neighbours_connect_notification (const struct GNUNET_PeerIdentity *peer,
544 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
545 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
547 size_t len = sizeof(struct ConnectInfoMessage);
548 char buf[len] GNUNET_ALIGN;
549 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
551 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
552 "We are now connected to peer `%s'\n",
554 connect_msg->header.size = htons (sizeof(buf));
555 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
556 connect_msg->id = *peer;
557 connect_msg->quota_in = bandwidth_in;
558 connect_msg->quota_out = bandwidth_out;
559 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
564 * Notify our clients (and manipulation) that a peer disconnected from
567 * @param peer the peer that disconnected
570 neighbours_disconnect_notification (const struct GNUNET_PeerIdentity *peer)
572 struct DisconnectInfoMessage disconnect_msg;
574 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
575 "Peer `%s' disconnected\n",
577 GST_manipulation_peer_disconnect (peer);
578 disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
579 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
580 disconnect_msg.reserved = htonl (0);
581 disconnect_msg.peer = *peer;
582 GST_clients_broadcast (&disconnect_msg.header,
588 * Notify transport clients that a neighbour peer changed its active
591 * @param peer identity of the peer
592 * @param address address possibly NULL if peer is not connected
593 * @param state current state this peer is in
594 * @param state_timeout timeout for the current state of the peer
595 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
596 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
599 neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer,
600 const struct GNUNET_HELLO_Address *address,
601 enum GNUNET_TRANSPORT_PeerState state,
602 struct GNUNET_TIME_Absolute state_timeout,
603 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
604 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
607 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
609 GST_plugins_a2s (address),
610 GNUNET_TRANSPORT_ps2s (state),
611 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
612 /* FIXME: include bandwidth in notification! */
613 GST_clients_broadcast_peer_notification (peer,
622 * Lookup a neighbour entry in the neighbours hash map.
624 * @param pid identity of the peer to look up
625 * @return the entry, NULL if there is no existing record
627 static struct NeighbourMapEntry *
628 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
630 if (NULL == neighbours)
632 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
637 * Test if we're connected to the given peer.
639 * @param n neighbour entry of peer to test
640 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
643 test_connected (struct NeighbourMapEntry *n)
647 return GNUNET_TRANSPORT_is_connected (n->state);
652 * Send information about a new outbound quota to our clients.
654 * @param target affected peer
655 * @param quota new quota
658 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
659 struct GNUNET_BANDWIDTH_Value32NBO quota)
661 struct QuotaSetMessage q_msg;
663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
664 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
665 ntohl (quota.value__), GNUNET_i2s (target));
666 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
667 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
669 q_msg.peer = (*target);
670 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
675 * We don't need a given neighbour address any more.
676 * Release its resources and give appropriate notifications
677 * to ATS and other subsystems.
679 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
682 free_address (struct NeighbourAddress *na)
684 if (GNUNET_YES == na->ats_active)
686 GST_validation_set_address_use (na->address,
688 GST_ats_set_in_use (na->address,
693 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
694 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
695 na->ats_active = GNUNET_NO;
696 na->keep_alive_nonce = 0;
697 if (NULL != na->address)
699 GNUNET_HELLO_address_free (na->address);
707 * Set net state for this neighbour and notify monitoring
709 * @param n the respective neighbour
710 * @param s the new state
713 set_state (struct NeighbourMapEntry *n,
714 enum GNUNET_TRANSPORT_PeerState s)
717 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
718 "Neighbour `%s' changed state to %s\n",
720 GNUNET_TRANSPORT_ps2s(s));
721 neighbours_changed_notification (&n->id,
722 n->primary_address.address,
725 n->primary_address.bandwidth_in,
726 n->primary_address.bandwidth_out);
731 * Set net state and state timeout for this neighbour and notify monitoring
733 * @param n the respective neighbour
734 * @param s the new state
735 * @param timeout the new timeout
738 set_state_and_timeout (struct NeighbourMapEntry *n,
739 enum GNUNET_TRANSPORT_PeerState s,
740 struct GNUNET_TIME_Absolute timeout)
743 n->timeout = timeout;
744 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
745 "Neighbour `%s' changed state to %s with timeout %s\n",
747 GNUNET_TRANSPORT_ps2s(s),
748 GNUNET_STRINGS_absolute_time_to_string (timeout));
749 neighbours_changed_notification (&n->id,
750 n->primary_address.address,
753 n->primary_address.bandwidth_in,
754 n->primary_address.bandwidth_out);
759 * Set new state timeout for this neighbour and notify monitoring
761 * @param n the respective neighbour
762 * @param timeout the new timeout
765 set_timeout (struct NeighbourMapEntry *n,
766 struct GNUNET_TIME_Absolute timeout)
768 n->timeout = timeout;
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
770 "Neighbour `%s' changed timeout %s\n",
772 GNUNET_STRINGS_absolute_time_to_string (timeout));
773 neighbours_changed_notification (&n->id,
774 n->primary_address.address,
775 n->state, n->timeout,
776 n->primary_address.bandwidth_in,
777 n->primary_address.bandwidth_out);
782 * Initialize the alternative address of a neighbour
784 * @param n the neighbour
785 * @param address address of the other peer, NULL if other peer
787 * @param session session to use (or NULL, in which case an
788 * address must be setup)
789 * @param bandwidth_in inbound quota to be used when connection is up
790 * @param bandwidth_out outbound quota to be used when connection is up
793 set_alternative_address (struct NeighbourMapEntry *n,
794 const struct GNUNET_HELLO_Address *address,
795 struct Session *session,
796 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
797 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
799 struct GNUNET_TRANSPORT_PluginFunctions *papi;
801 if (NULL == (papi = GST_plugins_find (address->transport_name)))
806 if (session == n->alternative_address.session)
808 n->alternative_address.bandwidth_in = bandwidth_in;
809 n->alternative_address.bandwidth_out = bandwidth_out;
812 free_address (&n->alternative_address);
814 session = papi->get_session (papi->cls, address);
817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
818 "Failed to obtain new session for peer `%s' and address '%s'\n",
819 GNUNET_i2s (&address->peer),
820 GST_plugins_a2s (address));
821 GNUNET_STATISTICS_update (GST_stats,
822 gettext_noop ("# session creation failed"),
827 GST_ats_new_session (address, session);
828 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
829 "Neighbour `%s' configured alternative address %s\n",
831 GST_plugins_a2s(address));
833 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
834 n->alternative_address.bandwidth_in = bandwidth_in;
835 n->alternative_address.bandwidth_out = bandwidth_out;
836 n->alternative_address.session = session;
837 n->alternative_address.ats_active = GNUNET_NO;
838 n->alternative_address.keep_alive_nonce = 0;
843 * Initialize the primary address of a neighbour
845 * @param n the neighbour
846 * @param address address of the other peer, NULL if other peer
848 * @param session session to use (or NULL, in which case an
849 * address must be setup)
850 * @param bandwidth_in inbound quota to be used when connection is up
851 * @param bandwidth_out outbound quota to be used when connection is up
852 * @param is_active #GNUNET_YES to mark this as the active address with ATS
855 set_primary_address (struct NeighbourMapEntry *n,
856 const struct GNUNET_HELLO_Address *address,
857 struct Session *session,
858 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
859 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
862 struct GNUNET_TRANSPORT_PluginFunctions *papi;
864 if (NULL == (papi = GST_plugins_find (address->transport_name)))
869 if (session == n->primary_address.session)
871 n->primary_address.bandwidth_in = bandwidth_in;
872 n->primary_address.bandwidth_out = bandwidth_out;
873 if (is_active != n->primary_address.ats_active)
875 n->primary_address.ats_active = is_active;
876 GST_ats_set_in_use (n->primary_address.address,
877 n->primary_address.session,
879 GST_validation_set_address_use (n->primary_address.address,
882 if (GNUNET_YES == is_active)
884 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
885 send_outbound_quota (&address->peer, bandwidth_out);
889 free_address (&n->primary_address);
891 session = papi->get_session (papi->cls, address);
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Failed to obtain new session for peer `%s' and address '%s'\n",
896 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
897 GNUNET_STATISTICS_update (GST_stats,
898 gettext_noop ("# session creation failed"),
903 GST_ats_new_session (address, session);
904 n->primary_address.address = GNUNET_HELLO_address_copy (address);
905 n->primary_address.bandwidth_in = bandwidth_in;
906 n->primary_address.bandwidth_out = bandwidth_out;
907 n->primary_address.session = session;
908 n->primary_address.ats_active = is_active;
909 n->primary_address.keep_alive_nonce = 0;
910 if (GNUNET_YES == is_active)
912 /* Telling ATS about new session */
913 GST_ats_set_in_use (n->primary_address.address,
914 n->primary_address.session,
916 GST_validation_set_address_use (n->primary_address.address,
918 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
919 send_outbound_quota (&address->peer, bandwidth_out);
922 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
923 "Neighbour `%s' switched to address `%s'\n",
925 GST_plugins_a2s(address));
927 neighbours_changed_notification (&n->id,
928 n->primary_address.address,
931 n->primary_address.bandwidth_in,
932 n->primary_address.bandwidth_out);
937 * Clear the primary address of a neighbour since this address is not
938 * valid anymore and notify monitoring about it
940 * @param n the neighbour
943 unset_primary_address (struct NeighbourMapEntry *n)
945 /* Notify monitoring about change */
946 neighbours_changed_notification (&n->id,
947 n->primary_address.address,
950 GNUNET_BANDWIDTH_value_init (0),
951 GNUNET_BANDWIDTH_value_init (0));
952 /* Unset primary address */
953 free_address (&n->primary_address);
958 * Clear the alternative address of a neighbour since this address is not
961 * @param n the neighbour
964 unset_alternative_address (struct NeighbourMapEntry *n)
966 /* Unset primary address */
967 free_address (&n->alternative_address);
972 * Free a neighbour map entry.
974 * @param n entry to free
977 free_neighbour (struct NeighbourMapEntry *n)
979 struct MessageQueue *mq;
981 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
982 "Freeing neighbour state of peer `%s'\n",
983 GNUNET_i2s (&n->id));
984 n->is_active = NULL; /* always free'd by its own continuation! */
986 /* fail messages currently in the queue */
987 while (NULL != (mq = n->messages_head))
989 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
990 if (NULL != mq->cont)
991 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
994 /* It is too late to send other peer disconnect notifications, but at
995 least internally we need to get clean... */
996 if (GNUNET_YES == test_connected (n))
998 GNUNET_STATISTICS_set (GST_stats,
999 gettext_noop ("# peers connected"),
1000 --neighbours_connected,
1002 neighbours_disconnect_notification (&n->id);
1005 /* Mark peer as disconnected */
1006 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1008 /* free addresses and mark as unused */
1009 unset_primary_address (n);
1010 free_address (&n->alternative_address);
1012 GNUNET_assert (GNUNET_YES ==
1013 GNUNET_CONTAINER_multipeermap_remove (neighbours,
1016 /* Cancel address requests for this peer */
1017 if (NULL != n->suggest_handle)
1019 GNUNET_ATS_connectivity_suggest_cancel (n->suggest_handle);
1020 n->suggest_handle = NULL;
1023 /* Cancel the disconnect task */
1024 if (NULL != n->delayed_disconnect_task)
1026 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
1027 n->delayed_disconnect_task = NULL;
1030 /* Cancel the master task */
1031 if (NULL != n->task)
1033 GNUNET_SCHEDULER_cancel (n->task);
1036 /* free rest of memory */
1042 * Transmit a message using the current session of the given
1045 * @param n entry for the recipient
1046 * @param msgbuf buffer to transmit
1047 * @param msgbuf_size number of bytes in @a msgbuf buffer
1048 * @param priority transmission priority
1049 * @param timeout transmission timeout
1050 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
1051 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
1052 * @param cont continuation to call when finished (can be NULL)
1053 * @param cont_cls closure for @a cont
1054 * @return timeout (copy of @a timeout or a calculated one if
1055 * @a use_keepalive_timeout is #GNUNET_YES.
1057 static struct GNUNET_TIME_Relative
1058 send_with_session (struct NeighbourMapEntry *n,
1059 const char *msgbuf, size_t msgbuf_size,
1061 struct GNUNET_TIME_Relative timeout,
1062 unsigned int use_keepalive_timeout,
1063 GNUNET_TRANSPORT_TransmitContinuation cont,
1066 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1067 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
1069 GNUNET_assert (n->primary_address.session != NULL);
1070 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1071 (-1 == papi->send (papi->cls,
1072 n->primary_address.session,
1073 msgbuf, msgbuf_size,
1075 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
1076 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1077 papi->query_keepalive_factor (papi->cls))),
1078 cont, cont_cls)))) &&
1080 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1081 GST_neighbours_notify_data_sent (n->primary_address.address,
1082 n->primary_address.session,
1084 GNUNET_break (NULL != papi);
1090 * Master task run for every neighbour. Performs all of the time-related
1091 * activities (keep alive, send next message, disconnect if idle, finish
1092 * clean up after disconnect).
1094 * @param cls the `struct NeighbourMapEntry` for which we are running
1095 * @param tc scheduler context (unused)
1098 master_task (void *cls,
1099 const struct GNUNET_SCHEDULER_TaskContext *tc);
1103 * Function called when the 'DISCONNECT' message has been sent by the
1104 * plugin. Frees the neighbour --- if the entry still exists.
1107 * @param target identity of the neighbour that was disconnected
1108 * @param result #GNUNET_OK if the disconnect got out successfully
1109 * @param payload bytes payload
1110 * @param physical bytes physical
1113 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1114 int result, size_t payload, size_t physical)
1116 struct NeighbourMapEntry *n;
1118 n = lookup_neighbour (target);
1120 return; /* already gone */
1121 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1122 return; /* have created a fresh entry since */
1123 if (NULL != n->task)
1124 GNUNET_SCHEDULER_cancel (n->task);
1125 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1130 * Transmit a DISCONNECT message to the other peer.
1132 * @param n neighbour to send DISCONNECT message.
1135 send_disconnect (struct NeighbourMapEntry *n)
1137 struct SessionDisconnectMessage disconnect_msg;
1139 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1140 "Sending DISCONNECT message to peer `%4s'\n",
1141 GNUNET_i2s (&n->id));
1142 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1143 disconnect_msg.header.type =
1144 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1145 disconnect_msg.reserved = htonl (0);
1146 disconnect_msg.purpose.size =
1147 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1148 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1149 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1150 disconnect_msg.purpose.purpose =
1151 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1152 disconnect_msg.timestamp =
1153 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1154 disconnect_msg.public_key = GST_my_identity.public_key;
1155 GNUNET_assert (GNUNET_OK ==
1156 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1157 &disconnect_msg.purpose,
1158 &disconnect_msg.signature));
1160 (void) send_with_session (n, (const char *) &disconnect_msg,
1161 sizeof (disconnect_msg), UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1162 GNUNET_NO, &send_disconnect_cont, NULL );
1163 GNUNET_STATISTICS_update (GST_stats,
1165 ("# DISCONNECT messages sent"), 1,
1171 * Disconnect from the given neighbour, clean up the record.
1173 * @param n neighbour to disconnect from
1176 disconnect_neighbour (struct NeighbourMapEntry *n)
1178 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1179 "Disconnecting from peer %s in state %s\n",
1180 GNUNET_i2s (&n->id),
1181 GNUNET_TRANSPORT_ps2s (n->state));
1182 /* depending on state, notify neighbour and/or upper layers of this peer
1186 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1187 case GNUNET_TRANSPORT_PS_INIT_ATS:
1188 /* other peer is completely unaware of us, no need to send DISCONNECT */
1191 case GNUNET_TRANSPORT_PS_SYN_SENT:
1192 send_disconnect (n);
1193 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1195 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1196 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1199 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1200 /* we DID ACK the other peer's request, must send DISCONNECT */
1201 send_disconnect (n);
1202 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1204 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1205 case GNUNET_TRANSPORT_PS_CONNECTED:
1206 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1207 /* we are currently connected, need to send disconnect and do
1208 internal notifications and update statistics */
1209 send_disconnect (n);
1210 GNUNET_STATISTICS_set (GST_stats,
1211 gettext_noop ("# peers connected"),
1212 --neighbours_connected,
1214 neighbours_disconnect_notification (&n->id);
1215 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1217 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1218 /* Disconnecting while waiting for an ATS address to reconnect,
1219 * cannot send DISCONNECT */
1222 case GNUNET_TRANSPORT_PS_DISCONNECT:
1223 /* already disconnected, ignore */
1225 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1226 /* already cleaned up, how did we get here!? */
1230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1231 "Unhandled state `%s'\n",
1232 GNUNET_TRANSPORT_ps2s (n->state));
1236 /* schedule timeout to clean up */
1237 if (NULL != n->task)
1238 GNUNET_SCHEDULER_cancel (n->task);
1239 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1245 * We're done with our transmission attempt, continue processing.
1247 * @param cls the `struct MessageQueue` of the message
1248 * @param receiver intended receiver
1249 * @param success whether it worked or not
1250 * @param size_payload bytes payload sent
1251 * @param physical bytes sent on wire
1254 transmit_send_continuation (void *cls,
1255 const struct GNUNET_PeerIdentity *receiver,
1256 int success, size_t size_payload, size_t physical)
1258 struct MessageQueue *mq = cls;
1259 struct NeighbourMapEntry *n;
1261 if (NULL == (n = lookup_neighbour (receiver)))
1264 return; /* disconnect or other error while transmitting, can happen */
1266 if (n->is_active == mq)
1268 /* this is still "our" neighbour, remove us from its queue
1269 and allow it to send the next message now */
1270 n->is_active = NULL;
1271 if (NULL != n->task)
1272 GNUNET_SCHEDULER_cancel (n->task);
1273 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1275 if (bytes_in_send_queue < mq->message_buf_size)
1277 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1278 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1279 bytes_in_send_queue, mq->message_buf_size,
1280 (GNUNET_OK == success) ? "OK" : "FAIL",
1281 size_payload, physical);
1286 GNUNET_break (size_payload == mq->message_buf_size);
1287 bytes_in_send_queue -= mq->message_buf_size;
1288 GNUNET_STATISTICS_set (GST_stats,
1290 ("# bytes in message queue for other peers"),
1291 bytes_in_send_queue, GNUNET_NO);
1292 if (GNUNET_OK == success)
1293 GNUNET_STATISTICS_update (GST_stats,
1295 ("# messages transmitted to other peers"),
1298 GNUNET_STATISTICS_update (GST_stats,
1300 ("# transmission failures for messages to other peers"),
1302 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1303 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1304 GNUNET_i2s (receiver),
1305 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1306 mq->message_buf_size,
1307 (success == GNUNET_OK) ? "success" : "FAILURE");
1308 if (NULL != mq->cont)
1309 mq->cont (mq->cont_cls, success, size_payload, physical);
1315 * Check the message list for the given neighbour and if we can
1316 * send a message, do so. This function should only be called
1317 * if the connection is at least generally ready for transmission.
1318 * While we will only send one message at a time, no bandwidth
1319 * quota management is performed here. If a message was given to
1320 * the plugin, the continuation will automatically re-schedule
1321 * the 'master' task once the next message might be transmitted.
1323 * @param n target peer for which to transmit
1326 try_transmission_to_peer (struct NeighbourMapEntry *n)
1328 struct MessageQueue *mq;
1329 struct GNUNET_TIME_Relative timeout;
1331 if (NULL == n->primary_address.address)
1333 /* no address, why are we here? */
1337 if ((0 == n->primary_address.address->address_length) &&
1338 (NULL == n->primary_address.session))
1340 /* no address, why are we here? */
1344 if (NULL != n->is_active)
1346 /* transmission already pending */
1350 /* timeout messages from the queue that are past their due date */
1351 while (NULL != (mq = n->messages_head))
1353 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1354 if (timeout.rel_value_us > 0)
1356 GNUNET_STATISTICS_update (GST_stats,
1358 ("# messages timed out while in transport queue"),
1360 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1362 transmit_send_continuation (mq, &n->id,
1364 mq->message_buf_size, 0); /* timeout */
1367 return; /* no more messages */
1368 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1372 "Giving message with %u bytes to plugin session %p\n",
1373 mq->message_buf_size, n->primary_address.session);
1375 (void) send_with_session (n,
1377 mq->message_buf_size,
1381 &transmit_send_continuation, mq);
1386 * Send keepalive message to the neighbour. Must only be called
1387 * if we are on 'connected' state or while trying to switch addresses.
1388 * Will internally determine if a keepalive is truly needed (so can
1389 * always be called).
1391 * @param n neighbour that went idle and needs a keepalive
1394 send_keepalive (struct NeighbourMapEntry *n)
1396 struct SessionKeepAliveMessage m;
1397 struct GNUNET_TIME_Relative timeout;
1400 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1401 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT));
1402 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1403 return; /* no keepalive needed at this time */
1405 nonce = 0; /* 0 indicates 'not set' */
1407 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1410 "Sending keep alive to peer `%s' with nonce %u\n",
1411 GNUNET_i2s (&n->id), nonce);
1413 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1414 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1415 m.nonce = htonl (nonce);
1417 timeout = send_with_session (n,
1418 (const void *) &m, sizeof (m),
1419 UINT32_MAX /* priority */,
1420 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1422 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1424 n->primary_address.keep_alive_nonce = nonce;
1425 n->expect_latency_response = GNUNET_YES;
1426 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1427 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1433 * Keep the connection to the given neighbour alive longer,
1434 * we received a KEEPALIVE (or equivalent); send a response.
1436 * @param neighbour neighbour to keep alive (by sending keep alive response)
1437 * @param m the keep alive message containing the nonce to respond to
1440 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1441 const struct GNUNET_MessageHeader *m)
1443 struct NeighbourMapEntry *n;
1444 const struct SessionKeepAliveMessage *msg_in;
1445 struct SessionKeepAliveMessage msg;
1447 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1450 msg_in = (struct SessionKeepAliveMessage *) m;
1451 if (NULL == (n = lookup_neighbour (neighbour)))
1453 GNUNET_STATISTICS_update (GST_stats,
1455 ("# KEEPALIVE messages discarded (peer unknown)"),
1459 if (NULL == n->primary_address.session)
1461 GNUNET_STATISTICS_update (GST_stats,
1463 ("# KEEPALIVE messages discarded (no session)"),
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1469 "Received keep alive request from peer `%s' with nonce %u\n",
1470 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1472 /* send reply to allow neighbour to measure latency */
1473 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1474 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1475 msg.nonce = msg_in->nonce;
1476 (void) send_with_session(n,
1477 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1478 UINT32_MAX /* priority */,
1479 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1485 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1486 * latency to this peer. Pass the updated information (existing ats
1487 * plus calculated latency) to ATS.
1489 * @param neighbour neighbour to keep alive
1490 * @param m the message containing the keep alive response
1493 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1494 const struct GNUNET_MessageHeader *m)
1496 struct NeighbourMapEntry *n;
1497 const struct SessionKeepAliveMessage *msg;
1498 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1499 struct GNUNET_ATS_Information ats;
1500 struct GNUNET_TIME_Relative latency;
1502 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1505 msg = (const struct SessionKeepAliveMessage *) m;
1506 if (NULL == (n = lookup_neighbour (neighbour)))
1508 GNUNET_STATISTICS_update (GST_stats,
1510 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1514 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1515 (GNUNET_YES != n->expect_latency_response) )
1517 GNUNET_STATISTICS_update (GST_stats,
1519 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1523 if (NULL == n->primary_address.address)
1525 GNUNET_STATISTICS_update (GST_stats,
1527 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1531 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1533 GNUNET_STATISTICS_update (GST_stats,
1535 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 "Received keep alive response from peer `%s' for session %p\n",
1543 GNUNET_i2s (&n->id), n->primary_address.session);
1547 /* Update session timeout here */
1548 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1551 "Updating session for peer `%s' for session %p\n",
1552 GNUNET_i2s (&n->id), n->primary_address.session);
1553 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1560 n->primary_address.keep_alive_nonce = 0;
1561 n->expect_latency_response = GNUNET_NO;
1562 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1564 latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1566 "Latency for peer `%s' is %s\n",
1567 GNUNET_i2s (&n->id),
1568 GNUNET_STRINGS_relative_time_to_string (latency,
1570 /* append latency */
1571 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1572 ats.value = htonl ( (latency.rel_value_us > UINT32_MAX)
1574 : (uint32_t) latency.rel_value_us );
1575 GST_ats_update_metrics (n->primary_address.address,
1576 n->primary_address.session,
1582 * We have received a message from the given sender. How long should
1583 * we delay before receiving more? (Also used to keep the peer marked
1586 * @param sender sender of the message
1587 * @param size size of the message
1588 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1589 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1590 * #GNUNET_SYSERR if the connection is not fully up yet
1591 * @return how long to wait before reading more from this sender
1593 struct GNUNET_TIME_Relative
1594 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1595 *sender, ssize_t size, int *do_forward)
1597 struct NeighbourMapEntry *n;
1598 struct GNUNET_TIME_Relative ret;
1600 if (NULL == neighbours)
1602 *do_forward = GNUNET_NO;
1603 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1605 if (NULL == (n = lookup_neighbour (sender)))
1607 GST_neighbours_try_connect (sender);
1608 if (NULL == (n = lookup_neighbour (sender)))
1610 GNUNET_STATISTICS_update (GST_stats,
1612 ("# messages discarded due to lack of neighbour record"),
1614 *do_forward = GNUNET_NO;
1615 return GNUNET_TIME_UNIT_ZERO;
1618 if (! test_connected (n))
1620 *do_forward = GNUNET_SYSERR;
1621 return GNUNET_TIME_UNIT_ZERO;
1623 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1625 n->quota_violation_count++;
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1628 n->in_tracker.available_bytes_per_s__,
1629 n->quota_violation_count);
1630 /* Discount 32k per violation */
1631 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1635 if (n->quota_violation_count > 0)
1637 /* try to add 32k back */
1638 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1639 n->quota_violation_count--;
1642 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1644 GNUNET_STATISTICS_update (GST_stats,
1646 ("# bandwidth quota violations by other peers"),
1648 *do_forward = GNUNET_NO;
1649 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1651 *do_forward = GNUNET_YES;
1652 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1653 if (ret.rel_value_us > 0)
1655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1656 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1657 (unsigned long long) n->in_tracker.
1658 consumption_since_last_update__,
1659 (unsigned int) n->in_tracker.available_bytes_per_s__,
1660 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1661 GNUNET_STATISTICS_update (GST_stats,
1662 gettext_noop ("# ms throttling suggested"),
1663 (int64_t) ret.rel_value_us / 1000LL,
1671 * Transmit a message to the given target using the active connection.
1673 * @param target destination
1674 * @param msg message to send
1675 * @param msg_size number of bytes in msg
1676 * @param timeout when to fail with timeout
1677 * @param cont function to call when done
1678 * @param cont_cls closure for @a cont
1681 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
1684 struct GNUNET_TIME_Relative timeout,
1685 GST_NeighbourSendContinuation cont,
1688 struct NeighbourMapEntry *n;
1689 struct MessageQueue *mq;
1691 /* All ove these cases should never happen; they are all API violations.
1692 But we check anyway, just to be sure. */
1693 if (NULL == (n = lookup_neighbour (target)))
1697 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1700 if (GNUNET_YES != test_connected (n))
1704 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1707 bytes_in_send_queue += msg_size;
1708 GNUNET_STATISTICS_set (GST_stats,
1710 ("# bytes in message queue for other peers"),
1711 bytes_in_send_queue, GNUNET_NO);
1712 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1714 mq->cont_cls = cont_cls;
1715 memcpy (&mq[1], msg, msg_size);
1716 mq->message_buf = (const char *) &mq[1];
1717 mq->message_buf_size = msg_size;
1718 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueueing %u bytes to send to peer %s\n",
1721 msg_size, GNUNET_i2s (target));
1723 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1724 if (NULL != n->task)
1725 GNUNET_SCHEDULER_cancel (n->task);
1726 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1734 send_session_connect_cont (void *cls,
1735 const struct GNUNET_PeerIdentity *target,
1737 size_t size_payload,
1738 size_t size_on_wire)
1740 struct NeighbourMapEntry *n;
1742 n = lookup_neighbour (target);
1745 /* SYN continuation was called after neighbor was freed,
1746 * for example due to a time out for the state or the session
1747 * used was already terminated: nothing to do here... */
1751 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1752 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1753 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1755 /* SYN continuation was called after neighbor changed state,
1756 * for example due to a time out for the state or the session
1757 * used was already terminated: nothing to do here... */
1760 if (GNUNET_OK == result)
1763 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1764 _("Failed to send SYN message to peer `%s' using address `%s' session %p\n"),
1765 GNUNET_i2s (target),
1766 GST_plugins_a2s (n->primary_address.address),
1767 n->primary_address.session);
1770 case GNUNET_TRANSPORT_PS_SYN_SENT:
1771 /* Remove address and request an additional one */
1772 unset_primary_address (n);
1773 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1774 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1776 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1777 /* Remove address and request an additional one */
1778 unset_primary_address (n);
1779 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1780 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1782 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1783 /* Remove address and request and go back to primary address */
1784 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1785 ("# Failed attempts to switch addresses (failed to send SYN CONT)"), 1, GNUNET_NO);
1786 unset_alternative_address (n);
1787 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1788 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1791 disconnect_neighbour (n);
1797 * Send a SYN message via the given address.
1799 * @param na address to use
1802 send_syn (struct NeighbourAddress *na)
1804 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1805 struct TransportSynMessage connect_msg;
1806 struct NeighbourMapEntry *n;
1808 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1809 "Sending SYN message to peer `%s'\n",
1810 GNUNET_i2s (&na->address->peer));
1812 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1817 if (NULL == na->session)
1818 na->session = papi->get_session (papi->cls, na->address);
1819 if (NULL == na->session)
1824 GST_ats_new_session (na->address,
1826 GNUNET_STATISTICS_update (GST_stats,
1828 ("# SYN messages sent"),
1830 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1831 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1832 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1833 connect_msg.reserved = htonl (0);
1834 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1836 papi->send (papi->cls,
1838 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
1840 SETUP_CONNECTION_TIMEOUT,
1841 send_session_connect_cont, NULL))
1843 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1844 _("Failed to transmit SYN message via plugin to %s\n"),
1845 GST_plugins_a2s (na->address));
1847 n = lookup_neighbour (&na->address->peer);
1855 case GNUNET_TRANSPORT_PS_SYN_SENT:
1856 /* Remove address and request and additional one */
1857 unset_primary_address (n);
1858 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1859 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1860 /* Hard failure to send the SYN message with this address:
1861 Destroy address and session */
1863 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1864 /* Remove address and request an additional one */
1865 unset_primary_address (n);
1866 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1867 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1869 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1870 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1871 ("# Failed attempts to switch addresses (failed to send SYN)"), 1, GNUNET_NO);
1872 /* Remove address and request an additional one */
1873 unset_alternative_address (n);
1874 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1875 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1878 disconnect_neighbour (n);
1882 GST_neighbours_notify_data_sent (na->address,
1884 sizeof (struct TransportSynMessage));
1892 send_session_connect_ack_cont (void *cls,
1893 const struct GNUNET_PeerIdentity *target,
1895 size_t size_payload,
1896 size_t size_on_wire)
1898 struct NeighbourMapEntry *n;
1900 n = lookup_neighbour (target);
1903 /* SYN_ACK continuation was called after neighbor was freed,
1904 * for example due to a time out for the state or the session
1905 * used was already terminated: nothing to do here... */
1909 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1911 /* SYN_ACK continuation was called after neighbor changed state,
1912 * for example due to a time out for the state or the session
1913 * used was already terminated: nothing to do here... */
1916 if (GNUNET_OK == result)
1919 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1920 _("Failed to send SYN_ACK message to peer `%s' using address `%s' session %p\n"),
1921 GNUNET_i2s (target),
1922 GST_plugins_a2s (n->primary_address.address),
1923 n->primary_address.session);
1925 /* Remove address and request and additional one */
1926 unset_primary_address (n);
1927 n->ack_state = ACK_SEND_SYN_ACK;
1928 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1929 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1935 * Send a SYN_ACK message via the given address.
1937 * @param address address to use
1938 * @param session session to use
1939 * @param timestamp timestamp to use for the ACK message
1940 * @return GNUNET_SYSERR if sending immediately failed, GNUNET_OK otherwise
1943 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1944 struct Session *session,
1945 struct GNUNET_TIME_Absolute timestamp)
1947 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1948 struct TransportSynMessage connect_msg;
1949 struct NeighbourMapEntry *n;
1951 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1952 "Sending SYN_ACK to peer `%s'\n",
1953 GNUNET_i2s (&address->peer));
1955 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1960 if (NULL == session)
1961 session = papi->get_session (papi->cls, address);
1962 if (NULL == session)
1967 GST_ats_new_session (address, session);
1968 GNUNET_STATISTICS_update (GST_stats,
1970 ("# SYN_ACK messages sent"),
1972 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1973 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
1974 connect_msg.reserved = htonl (0);
1975 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1977 if (GNUNET_SYSERR == papi->send (papi->cls,
1979 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
1981 GNUNET_TIME_UNIT_FOREVER_REL,
1982 send_session_connect_ack_cont, NULL))
1984 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1985 _("Failed to transmit SYN_ACK message via plugin to %s\n"),
1986 GST_plugins_a2s (address));
1988 n = lookup_neighbour (&address->peer);
1994 /* Remove address and request and additional one */
1995 unset_primary_address (n);
1996 n->ack_state = ACK_SEND_SYN_ACK;
1997 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1998 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2009 inbound_bw_tracker_update (void *cls)
2011 struct NeighbourMapEntry *n = cls;
2012 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2013 struct GNUNET_TIME_Relative delay;
2016 if (NULL == n->primary_address.address)
2017 return; /* not active, ignore */
2018 papi = GST_plugins_find (n->primary_address.address->transport_name);
2019 GNUNET_assert (NULL != papi);
2020 if (NULL == papi->update_inbound_delay)
2022 delay = GST_neighbours_calculate_receive_delay (&n->id,
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2026 "New inbound delay for peer `%s' is %llu ms\n",
2027 GNUNET_i2s (&n->id),
2028 delay.rel_value_us / 1000);
2029 papi->update_inbound_delay (papi->cls,
2031 n->primary_address.session,
2037 * Create a fresh entry in the neighbour map for the given peer
2039 * @param peer peer to create an entry for
2040 * @return new neighbour map entry
2042 static struct NeighbourMapEntry *
2043 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2045 struct NeighbourMapEntry *n;
2047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2048 "Creating new neighbour entry for `%s'\n",
2050 n = GNUNET_new (struct NeighbourMapEntry);
2052 n->ack_state = ACK_UNDEFINED;
2053 n->last_util_transmission = GNUNET_TIME_absolute_get();
2054 n->util_payload_bytes_recv = 0;
2055 n->util_payload_bytes_sent = 0;
2056 n->util_total_bytes_recv = 0;
2057 n->util_total_bytes_sent = 0;
2058 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2059 &inbound_bw_tracker_update,
2061 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2062 MAX_BANDWIDTH_CARRY_S);
2063 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2064 set_state_and_timeout (n,
2065 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2066 GNUNET_TIME_UNIT_FOREVER_ABS);
2067 GNUNET_assert (GNUNET_OK ==
2068 GNUNET_CONTAINER_multipeermap_put (neighbours,
2070 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2076 * Entry in a DLL we use to keep track of pending blacklist checks.
2078 struct BlacklistCheckSwitchContext
2083 struct BlacklistCheckSwitchContext *prev;
2088 struct BlacklistCheckSwitchContext *next;
2091 * Handle to the blacklist check we are performing.
2093 struct GST_BlacklistCheck *blc;
2096 * Address we are asking the blacklist subsystem about.
2098 struct GNUNET_HELLO_Address *address;
2101 * Session we should use in conjunction with @e address, can be NULL.
2103 struct Session *session;
2106 * Inbound bandwidth that was assigned to @e address.
2108 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2111 * Outbound bandwidth that was assigned to @e address.
2113 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2118 * Black list check result for try_connect call
2119 * If connection to the peer is allowed request adddress and
2121 * @param cls blc_ctx bl context
2122 * @param peer the peer
2123 * @param result the result
2126 try_connect_bl_check_cont (void *cls,
2127 const struct GNUNET_PeerIdentity *peer,
2130 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2131 struct NeighbourMapEntry *n;
2133 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2136 GNUNET_free (blc_ctx);
2137 if (GNUNET_OK != result)
2139 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2140 _("Blacklisting disapproved to connect to peer `%s'\n"),
2145 /* Setup a new neighbour */
2146 if (NULL != lookup_neighbour(peer))
2147 return; /* The neighbor was created in the meantime while waited for BL clients */
2149 n = setup_neighbour (peer);
2151 /* Request address suggestions for this peer */
2152 set_state_and_timeout (n,
2153 GNUNET_TRANSPORT_PS_INIT_ATS,
2154 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2155 GNUNET_ATS_reset_backoff (GST_ats, peer);
2156 GNUNET_assert (NULL == n->suggest_handle);
2157 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2163 * Try to create a connection to the given target (eventually).
2165 * @param target peer to try to connect to
2168 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2170 struct NeighbourMapEntry *n;
2171 struct GST_BlacklistCheck *blc;
2172 struct BlacklistCheckSwitchContext *blc_ctx;
2174 if (NULL == neighbours)
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 "Asked to connect to peer `%s' during shutdown\n",
2178 GNUNET_i2s (target));
2179 return; /* during shutdown, do nothing */
2181 n = lookup_neighbour (target);
2182 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2183 "Asked to connect to peer `%s' (state: %s)\n",
2184 GNUNET_i2s (target),
2185 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2190 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2191 /* this should not be possible */
2195 case GNUNET_TRANSPORT_PS_INIT_ATS:
2196 case GNUNET_TRANSPORT_PS_SYN_SENT:
2197 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2198 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2199 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2200 "Ignoring request to try to connect to `%s', already trying!\n",
2201 GNUNET_i2s (target));
2202 return; /* already trying */
2203 case GNUNET_TRANSPORT_PS_CONNECTED:
2204 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2205 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2206 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2207 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2208 "Ignoring request to try to connect, already connected to `%s'!\n",
2209 GNUNET_i2s (target));
2210 return; /* already connected */
2211 case GNUNET_TRANSPORT_PS_DISCONNECT:
2212 /* get rid of remains, ready to re-try immediately */
2215 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2216 /* should not be possible */
2220 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2221 "Unhandled state `%s'\n",
2222 GNUNET_TRANSPORT_ps2s (n->state));
2229 /* Do blacklist check if connecting to this peer is allowed */
2230 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2231 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2236 (blc = GST_blacklist_test_allowed (target,
2238 &try_connect_bl_check_cont,
2247 * We received a 'SYN' message from the other peer.
2248 * Consider switching to it.
2250 * @param message possibly a 'struct TransportSynMessage' (check format)
2251 * @param peer identity of the peer to switch the address for
2252 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2255 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2256 const struct GNUNET_PeerIdentity *peer)
2258 const struct TransportSynMessage *scm;
2259 struct NeighbourMapEntry *n;
2260 struct GNUNET_TIME_Absolute ts;
2262 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2264 GNUNET_break_op (0);
2265 return GNUNET_SYSERR;
2267 GNUNET_STATISTICS_update (GST_stats,
2269 ("# SYN messages received"),
2271 if (NULL == neighbours)
2273 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2274 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2276 return GNUNET_OK; /* we're shutting down */
2278 scm = (const struct TransportSynMessage *) message;
2279 GNUNET_break_op (0 == ntohl (scm->reserved));
2280 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2281 n = lookup_neighbour (peer);
2284 /* This is a new neighbour and set to not connected */
2285 n = setup_neighbour (peer);
2288 /* Remember this SYN message in neighbour */
2289 n->ack_state = ACK_SEND_SYN_ACK;
2290 n->connect_ack_timestamp = ts;
2292 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2293 "Received SYN for peer `%s' in state %s/%s\n",
2295 GNUNET_TRANSPORT_ps2s (n->state),
2296 print_ack_state (n->ack_state));
2300 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2301 /* Request an address from ATS to send SYN_ACK to this peer */
2302 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2303 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2304 if (NULL == n->suggest_handle)
2305 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2308 case GNUNET_TRANSPORT_PS_INIT_ATS:
2309 /* SYN message takes priority over us asking ATS for address:
2310 * Wait for ATS to suggest an address and send SYN_ACK */
2311 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2312 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2314 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2315 /* We already wait for an address to send an SYN_ACK */
2317 case GNUNET_TRANSPORT_PS_SYN_SENT:
2318 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2319 /* Send ACK immediately */
2320 n->ack_state = ACK_SEND_ACK;
2321 send_connect_ack_message (n->primary_address.address,
2322 n->primary_address.session, ts);
2324 case GNUNET_TRANSPORT_PS_CONNECTED:
2325 /* we are already connected and can thus send the ACK immediately */
2326 GNUNET_assert (NULL != n->primary_address.address);
2327 GNUNET_assert (NULL != n->primary_address.session);
2328 n->ack_state = ACK_SEND_ACK;
2329 send_connect_ack_message (n->primary_address.address,
2330 n->primary_address.session, ts);
2332 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2333 /* We wait for ATS address suggestion */
2335 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2336 /* We received a SYN message while waiting for a SYN_ACK in fast
2337 * reconnect. Send SYN_ACK immediately */
2338 n->ack_state = ACK_SEND_ACK;
2339 send_connect_ack_message (n->primary_address.address,
2340 n->primary_address.session, n->connect_ack_timestamp);
2342 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2343 /* We are already connected and can thus send the ACK immediately;
2344 still, it can never hurt to have an alternative address, so also
2345 tell ATS about it */
2346 GNUNET_assert (NULL != n->primary_address.address);
2347 GNUNET_assert (NULL != n->primary_address.session);
2348 n->ack_state = ACK_SEND_ACK;
2349 send_connect_ack_message (n->primary_address.address,
2350 n->primary_address.session, ts);
2352 case GNUNET_TRANSPORT_PS_DISCONNECT:
2353 /* Get rid of remains and re-try */
2355 n = setup_neighbour (peer);
2356 /* Remember the SYN time stamp for ACK message */
2357 n->ack_state = ACK_SEND_SYN_ACK;
2358 n->connect_ack_timestamp = ts;
2359 /* Request an address for the peer */
2360 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2362 GNUNET_ATS_reset_backoff (GST_ats, peer);
2363 set_state (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS);
2365 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2366 /* should not be possible */
2370 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2371 "Unhandled state `%s'\n",
2372 GNUNET_TRANSPORT_ps2s (n->state));
2374 return GNUNET_SYSERR;
2381 * Check if the given @a address is the same that we are already
2382 * using for the respective neighbour. If so, update the bandwidth
2383 * assignment and possibly the session and return #GNUNET_OK.
2384 * If the new address is different from what the neighbour is
2385 * using right now, return #GNUNET_NO.
2387 * @param address address of the other peer,
2388 * @param session session to use or NULL if transport should initiate a session
2389 * @param bandwidth_in inbound quota to be used when connection is up,
2390 * 0 to disconnect from peer
2391 * @param bandwidth_out outbound quota to be used when connection is up,
2392 * 0 to disconnect from peer
2393 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2394 * #GNUNET_NO if more extensive changes are required (address changed)
2397 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2398 struct Session *session,
2399 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2400 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2402 struct NeighbourMapEntry *n;
2405 n = lookup_neighbour (&address->peer);
2407 (NULL == n->primary_address.address) ||
2408 (0 != GNUNET_HELLO_address_cmp (address,
2409 n->primary_address.address)) )
2411 /* We are not really switching addresses, but merely adjusting
2412 session and/or bandwidth, can do fast ATS update! */
2413 if (session != n->primary_address.session)
2415 /* switch to a different session, but keeping same address; could
2416 happen if there is a 2nd inbound connection */
2417 connected = GNUNET_TRANSPORT_is_connected (n->state);
2418 if (GNUNET_YES == connected)
2419 GST_ats_set_in_use (n->primary_address.address,
2420 n->primary_address.session,
2422 n->primary_address.session = session;
2423 if (GNUNET_YES == connected)
2424 GST_ats_set_in_use (n->primary_address.address,
2425 n->primary_address.session,
2428 n->primary_address.bandwidth_in = bandwidth_in;
2429 n->primary_address.bandwidth_out = bandwidth_out;
2430 GST_neighbours_set_incoming_quota (&address->peer,
2432 send_outbound_quota (&address->peer,
2439 * We've been asked to switch addresses, and just now got the result
2440 * from the blacklist check to see if this is allowed.
2442 * @param cls the `struct BlacklistCheckSwitchContext` with
2443 * the information about the future address
2444 * @param peer the peer we may switch addresses on
2445 * @param result #GNUNET_NO if we are not allowed to use the new
2449 switch_address_bl_check_cont (void *cls,
2450 const struct GNUNET_PeerIdentity *peer,
2453 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2454 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2455 struct NeighbourMapEntry *n;
2457 if (result == GNUNET_NO)
2459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2460 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2461 GST_plugins_a2s (blc_ctx->address),
2463 GNUNET_i2s (&blc_ctx->address->peer));
2464 GNUNET_STATISTICS_update (GST_stats,
2465 "# ATS suggestions ignored (blacklist denied)",
2468 /* FIXME: tell plugin to force killing session here and now! */
2469 /* FIXME: Let ATS know that the suggested address did not work! */
2473 papi = GST_plugins_find (blc_ctx->address->transport_name);
2474 GNUNET_assert (NULL != papi);
2476 if (NULL == blc_ctx->session)
2478 /* need to create a session, ATS only gave us an address */
2479 blc_ctx->session = papi->get_session (papi->cls,
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2482 "Obtained new session for peer `%s' and address '%s': %p\n",
2483 GNUNET_i2s (&blc_ctx->address->peer),
2484 GST_plugins_a2s (blc_ctx->address),
2486 if (NULL != blc_ctx->session)
2487 GST_ats_new_session (blc_ctx->address,
2490 if (NULL == blc_ctx->session)
2492 /* session creation failed, bad!, fail! */
2493 GNUNET_STATISTICS_update (GST_stats,
2494 "# ATS suggestions ignored (failed to create session)",
2497 /* No session could be obtained, remove blacklist check and clean up */
2498 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2499 "Failed to obtain new session for peer `%s' and address '%s'\n",
2500 GNUNET_i2s (&blc_ctx->address->peer),
2501 GST_plugins_a2s (blc_ctx->address));
2502 /* FIXME: Let ATS know that the suggested address did not work! */
2506 /* We did this check already before going into blacklist, but
2507 it is theoretically possible that the situation changed in
2508 the meantime, hence we check again here */
2510 try_run_fast_ats_update (blc_ctx->address,
2512 blc_ctx->bandwidth_in,
2513 blc_ctx->bandwidth_out))
2514 goto cleanup; /* was just a minor update, we're done */
2516 /* check if we also need to setup the neighbour entry */
2517 if (NULL == (n = lookup_neighbour (peer)))
2519 n = setup_neighbour (peer);
2520 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2523 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2524 "Peer `%s' switches to address `%s' session %p\n",
2525 GNUNET_i2s (&blc_ctx->address->peer),
2526 GST_plugins_a2s (blc_ctx->address),
2531 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2535 case GNUNET_TRANSPORT_PS_INIT_ATS:
2536 /* We requested an address and ATS suggests one:
2537 * set primary address and send SYN message*/
2538 set_primary_address (n,
2541 blc_ctx->bandwidth_in,
2542 blc_ctx->bandwidth_out,
2544 if (ACK_SEND_SYN_ACK == n->ack_state)
2546 /* Send pending SYN_ACK message */
2547 n->ack_state = ACK_SEND_ACK;
2548 send_connect_ack_message (n->primary_address.address,
2549 n->primary_address.session,
2550 n->connect_ack_timestamp);
2552 set_state_and_timeout (n,
2553 GNUNET_TRANSPORT_PS_SYN_SENT,
2554 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2555 send_syn (&n->primary_address);
2557 case GNUNET_TRANSPORT_PS_SYN_SENT:
2558 /* ATS suggested a new address while waiting for an SYN_ACK:
2559 * Switch and send new SYN */
2560 /* ATS suggests a different address, switch again */
2561 set_primary_address (n,
2564 blc_ctx->bandwidth_in,
2565 blc_ctx->bandwidth_out,
2567 if (ACK_SEND_SYN_ACK == n->ack_state)
2569 /* Send pending SYN_ACK message */
2570 n->ack_state = ACK_SEND_ACK;
2571 send_connect_ack_message (n->primary_address.address,
2572 n->primary_address.session,
2573 n->connect_ack_timestamp);
2575 set_state_and_timeout (n,
2576 GNUNET_TRANSPORT_PS_SYN_SENT,
2577 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2578 send_syn (&n->primary_address);
2580 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2581 /* We requested an address and ATS suggests one:
2582 * set primary address and send SYN_ACK message*/
2583 set_primary_address (n,
2586 blc_ctx->bandwidth_in,
2587 blc_ctx->bandwidth_out,
2589 /* Send an ACK message as a response to the SYN msg */
2590 set_state_and_timeout (n,
2591 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2592 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2593 send_connect_ack_message (n->primary_address.address,
2594 n->primary_address.session,
2595 n->connect_ack_timestamp);
2596 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2597 (ACK_UNDEFINED == n->ack_state) )
2598 n->ack_state = ACK_SEND_ACK;
2600 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2601 /* ATS asks us to switch while we were trying to connect; switch to new
2602 address and check blacklist again */
2603 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2605 n->ack_state = ACK_SEND_ACK;
2606 send_connect_ack_message (n->primary_address.address,
2607 n->primary_address.session,
2608 n->connect_ack_timestamp);
2610 set_primary_address (n,
2613 blc_ctx->bandwidth_in,
2614 blc_ctx->bandwidth_out,
2616 set_state_and_timeout (n,
2617 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2618 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2620 case GNUNET_TRANSPORT_PS_CONNECTED:
2621 GNUNET_assert (NULL != n->primary_address.address);
2622 GNUNET_assert (NULL != n->primary_address.session);
2623 if (n->primary_address.session == blc_ctx->session)
2625 /* not an address change, just a quota change */
2626 // FIXME: this case should have been caught above!
2627 set_primary_address (n,
2630 blc_ctx->bandwidth_in,
2631 blc_ctx->bandwidth_out,
2635 /* ATS asks us to switch a life connection; see if we can get
2636 a SYN_ACK on it before we actually do this! */
2637 set_alternative_address (n,
2640 blc_ctx->bandwidth_in,
2641 blc_ctx->bandwidth_out);
2642 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2643 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2644 GNUNET_STATISTICS_update (GST_stats,
2645 gettext_noop ("# Attempts to switch addresses"),
2648 send_syn (&n->alternative_address);
2650 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2651 set_primary_address (n,
2654 blc_ctx->bandwidth_in,
2655 blc_ctx->bandwidth_out,
2657 if (ACK_SEND_SYN_ACK == n->ack_state)
2659 /* Send pending SYN_ACK message */
2660 n->ack_state = ACK_SEND_ACK;
2661 send_connect_ack_message (n->primary_address.address,
2662 n->primary_address.session,
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,
2679 set_state_and_timeout (n,
2680 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2681 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2682 send_syn (&n->primary_address);
2684 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2685 if ( (0 == GNUNET_HELLO_address_cmp(n->primary_address.address,
2686 blc_ctx->address)) &&
2687 (n->primary_address.session == blc_ctx->session) )
2689 /* ATS switches back to still-active session */
2690 free_address (&n->alternative_address);
2691 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2694 /* ATS asks us to switch a life connection, send */
2695 set_alternative_address (n,
2698 blc_ctx->bandwidth_in,
2699 blc_ctx->bandwidth_out);
2700 set_state_and_timeout (n,
2701 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2702 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2703 send_syn (&n->alternative_address);
2705 case GNUNET_TRANSPORT_PS_DISCONNECT:
2706 /* not going to switch addresses while disconnecting */
2707 GNUNET_STATISTICS_update (GST_stats,
2708 "# ATS suggestion ignored (disconnecting)",
2712 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2716 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2717 "Unhandled state `%s'\n",
2718 GNUNET_TRANSPORT_ps2s (n->state));
2723 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2726 GNUNET_HELLO_address_free (blc_ctx->address);
2727 GNUNET_free (blc_ctx);
2732 * For the given peer, switch to this address.
2734 * Before accepting this addresses and actively using it, a blacklist check
2735 * is performed. If this blacklist check fails the address will be destroyed.
2737 * @param address address of the other peer,
2738 * @param session session to use or NULL if transport should initiate a session
2739 * @param bandwidth_in inbound quota to be used when connection is up,
2740 * 0 to disconnect from peer
2741 * @param bandwidth_out outbound quota to be used when connection is up,
2742 * 0 to disconnect from peer
2745 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2746 struct Session *session,
2747 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2748 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2750 struct GST_BlacklistCheck *blc;
2751 struct BlacklistCheckSwitchContext *blc_ctx;
2753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2754 "ATS has decided on an address for peer %s\n",
2755 GNUNET_i2s (&address->peer));
2756 GNUNET_assert (NULL != address->transport_name);
2758 try_run_fast_ats_update (address,
2764 /* Check if plugin is available */
2765 if (NULL == (GST_plugins_find (address->transport_name)))
2767 /* we don't have the plugin for this address */
2771 if ((NULL == session) &&
2772 (GNUNET_HELLO_address_check_option (address,
2773 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2775 /* This is a inbound address and we do not have a session to use! */
2780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2781 "ATS suggests %s address '%s' for peer `%s'\n",
2782 GNUNET_HELLO_address_check_option (address,
2783 GNUNET_HELLO_ADDRESS_INFO_INBOUND)
2784 ? "inbound" : "outbound",
2785 GST_plugins_a2s (address),
2786 GNUNET_i2s (&address->peer));
2788 /* Perform blacklist check */
2789 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2790 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2791 blc_ctx->session = session;
2792 blc_ctx->bandwidth_in = bandwidth_in;
2793 blc_ctx->bandwidth_out = bandwidth_out;
2794 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2797 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2798 address->transport_name,
2799 &switch_address_bl_check_cont,
2808 * Function called to send network utilization data to ATS for
2809 * each active connection.
2812 * @param key peer we send utilization data for
2813 * @param value the `struct NeighbourMapEntry *` with data to send
2814 * @return #GNUNET_OK (continue to iterate)
2817 send_utilization_data (void *cls,
2818 const struct GNUNET_PeerIdentity *key,
2821 struct NeighbourMapEntry *n = value;
2822 struct GNUNET_ATS_Information atsi[4];
2824 uint32_t bps_pl_out;
2827 struct GNUNET_TIME_Relative delta;
2829 if (GNUNET_TRANSPORT_PS_CONNECTED != n->state)
2831 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2832 GNUNET_TIME_absolute_get ());
2836 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2837 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2839 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2840 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2842 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2847 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2848 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2850 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2851 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2855 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2859 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2860 atsi[0].value = htonl (bps_out);
2861 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2862 atsi[1].value = htonl (bps_in);
2864 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2865 atsi[2].value = htonl (bps_pl_out);
2866 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2867 atsi[3].value = htonl (bps_pl_in);
2869 GST_ats_update_metrics (n->primary_address.address,
2870 n->primary_address.session,
2872 n->util_payload_bytes_recv = 0;
2873 n->util_payload_bytes_sent = 0;
2874 n->util_total_bytes_recv = 0;
2875 n->util_total_bytes_sent = 0;
2876 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2882 * Task transmitting utilization in a regular interval
2884 * @param cls the 'struct NeighbourMapEntry' for which we are running
2885 * @param tc scheduler context (unused)
2888 utilization_transmission (void *cls,
2889 const struct GNUNET_SCHEDULER_TaskContext *tc)
2891 util_transmission_tk = NULL;
2893 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2894 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2896 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2897 utilization_transmission, NULL);
2903 * Track information about data we received from the
2904 * given address (used to notify ATS about our utilization
2905 * of allocated resources).
2907 * @param address the address we got data from
2908 * @param message the message we received (really only the size is used)
2911 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2912 const struct GNUNET_MessageHeader *message)
2914 struct NeighbourMapEntry *n;
2916 n = lookup_neighbour (&address->peer);
2919 n->util_total_bytes_recv += ntohs (message->size);
2924 * Track information about payload (useful data) we received from the
2925 * given address (used to notify ATS about our utilization of
2926 * allocated resources).
2928 * @param address the address we got data from
2929 * @param message the message we received (really only the size is used)
2932 GST_neighbours_notify_payload_recv (const struct GNUNET_HELLO_Address *address,
2933 const struct GNUNET_MessageHeader *message)
2935 struct NeighbourMapEntry *n;
2937 n = lookup_neighbour (&address->peer);
2940 n->util_payload_bytes_recv += ntohs (message->size);
2945 * Track information about data we transmitted using the given @a
2946 * address and @a session (used to notify ATS about our utilization of
2947 * allocated resources).
2949 * @param address the address we transmitted data to
2950 * @param session session we used to transmit data
2951 * @param message the message we sent (really only the size is used)
2954 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2955 struct Session *session,
2958 struct NeighbourMapEntry *n;
2960 n = lookup_neighbour (&address->peer);
2963 if (n->primary_address.session != session)
2965 n->util_total_bytes_sent += size;
2970 * Track information about payload (useful data) we transmitted using the
2971 * given address (used to notify ATS about our utilization of
2972 * allocated resources).
2974 * @param address the address we transmitted data to
2975 * @param message the message we sent (really only the size is used)
2978 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2981 struct NeighbourMapEntry *n;
2983 n = lookup_neighbour (peer);
2986 n->util_payload_bytes_sent += size;
2991 * Master task run for every neighbour. Performs all of the time-related
2992 * activities (keep alive, send next message, disconnect if idle, finish
2993 * clean up after disconnect).
2995 * @param cls the 'struct NeighbourMapEntry' for which we are running
2996 * @param tc scheduler context (unused)
2999 master_task (void *cls,
3000 const struct GNUNET_SCHEDULER_TaskContext *tc)
3002 struct NeighbourMapEntry *n = cls;
3003 struct GNUNET_TIME_Relative delay;
3006 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3008 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3009 GNUNET_i2s (&n->id),
3010 GNUNET_TRANSPORT_ps2s(n->state),
3011 GNUNET_STRINGS_relative_time_to_string (delay,
3015 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3016 /* invalid state for master task, clean up */
3020 case GNUNET_TRANSPORT_PS_INIT_ATS:
3021 if (0 == delay.rel_value_us)
3023 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3024 "Connection to `%s' timed out waiting for ATS to provide address\n",
3025 GNUNET_i2s (&n->id));
3030 case GNUNET_TRANSPORT_PS_SYN_SENT:
3031 if (0 == delay.rel_value_us)
3033 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3034 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
3035 GNUNET_i2s (&n->id));
3036 /* Remove address and request and additional one */
3037 unset_primary_address (n);
3038 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3039 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3043 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3044 if (0 == delay.rel_value_us)
3046 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3047 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
3048 GNUNET_i2s (&n->id));
3053 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3054 if (0 == delay.rel_value_us)
3056 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3057 "Connection to `%s' timed out waiting for other peer to send ACK\n",
3058 GNUNET_i2s (&n->id));
3059 disconnect_neighbour (n);
3063 case GNUNET_TRANSPORT_PS_CONNECTED:
3064 if (0 == delay.rel_value_us)
3066 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3067 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3068 GNUNET_i2s (&n->id));
3069 disconnect_neighbour (n);
3072 try_transmission_to_peer (n);
3075 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3076 if (0 == delay.rel_value_us)
3078 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3079 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3080 GNUNET_i2s (&n->id));
3081 disconnect_neighbour (n);
3085 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3086 if (0 == delay.rel_value_us)
3088 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3089 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
3090 GNUNET_i2s (&n->id));
3091 disconnect_neighbour (n);
3095 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3096 if (0 == delay.rel_value_us)
3098 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3099 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to SYN on alternative address)\n",
3100 GNUNET_i2s (&n->id));
3101 GNUNET_STATISTICS_update (GST_stats,
3102 gettext_noop ("# Failed attempts to switch addresses (no response)"),
3105 disconnect_neighbour (n);
3108 try_transmission_to_peer (n);
3111 case GNUNET_TRANSPORT_PS_DISCONNECT:
3112 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3113 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3114 GNUNET_i2s (&n->id));
3117 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3118 /* how did we get here!? */
3122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3123 "Unhandled state `%s'\n",
3124 GNUNET_TRANSPORT_ps2s (n->state));
3128 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3129 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3131 /* if we are *now* in one of the two states, we're sending
3132 keep alive messages, so we need to consider the keepalive
3133 delay, not just the connection timeout */
3134 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3137 if (NULL == n->task)
3138 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3145 * Send a ACK message to the neighbour to confirm that we
3148 * @param n neighbour to send the ACK to
3151 send_session_ack_message (struct NeighbourMapEntry *n)
3153 struct GNUNET_MessageHeader msg;
3155 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sending ACK message to peer `%s'\n",
3156 GNUNET_i2s (&n->id));
3158 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3159 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3160 (void) send_with_session(n,
3161 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3162 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3168 * We received a 'SESSION_SYN_ACK' message from the other peer.
3169 * Consider switching to it.
3171 * @param message possibly a 'struct SessionConnectMessage' (check format)
3172 * @param peer identity of the peer to switch the address for
3173 * @param address address of the other peer, NULL if other peer
3175 * @param session session to use (or NULL)
3176 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3179 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3180 const struct GNUNET_HELLO_Address *address,
3181 struct Session *session)
3183 const struct TransportSynMessage *scm;
3184 struct GNUNET_TIME_Absolute ts;
3185 struct NeighbourMapEntry *n;
3187 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3188 "Received SYN_ACK message from peer `%s'\n",
3189 GNUNET_i2s (&address->peer));
3191 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3193 GNUNET_break_op (0);
3194 return GNUNET_SYSERR;
3196 GNUNET_STATISTICS_update (GST_stats,
3198 ("# SYN_ACK messages received"),
3200 scm = (const struct TransportSynMessage *) message;
3201 GNUNET_break_op (ntohl (scm->reserved) == 0);
3202 if (NULL == (n = lookup_neighbour (&address->peer)))
3204 GNUNET_STATISTICS_update (GST_stats,
3206 ("# unexpected SYN_ACK messages (no peer)"),
3208 return GNUNET_SYSERR;
3210 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3213 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3216 return GNUNET_SYSERR;
3217 case GNUNET_TRANSPORT_PS_INIT_ATS:
3218 GNUNET_STATISTICS_update (GST_stats,
3220 ("# unexpected SYN_ACK messages (not ready)"),
3223 case GNUNET_TRANSPORT_PS_SYN_SENT:
3224 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3226 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3227 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3230 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3231 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3232 GNUNET_STATISTICS_set (GST_stats,
3233 gettext_noop ("# peers connected"),
3234 ++neighbours_connected,
3236 neighbours_connect_notification (&n->id,
3237 n->primary_address.bandwidth_in,
3238 n->primary_address.bandwidth_out);
3239 /* Tell ATS that the outbound session we created to send SYN was successful */
3240 set_primary_address (n,
3241 n->primary_address.address,
3242 n->primary_address.session,
3243 n->primary_address.bandwidth_in,
3244 n->primary_address.bandwidth_out,
3246 send_session_ack_message (n);
3248 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3249 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3250 GNUNET_STATISTICS_update (GST_stats,
3252 ("# unexpected SYN_ACK messages (not ready)"),
3255 case GNUNET_TRANSPORT_PS_CONNECTED:
3256 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3257 send_session_ack_message (n);
3259 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3260 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3261 to give us a new address... */
3262 GNUNET_STATISTICS_update (GST_stats,
3264 ("# unexpected SYN_ACK messages (waiting on ATS)"),
3267 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3268 /* Reconnecting with new address address worked; go back to connected! */
3269 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3270 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3271 send_session_ack_message (n);
3273 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3274 /* new address worked; adopt it and go back to connected! */
3275 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3276 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3277 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3279 /* Set primary addresses */
3280 set_primary_address (n, n->alternative_address.address,
3281 n->alternative_address.session, n->alternative_address.bandwidth_in,
3282 n->alternative_address.bandwidth_out, GNUNET_YES);
3283 GNUNET_STATISTICS_update (GST_stats, gettext_noop
3284 ("# Successful attempts to switch addresses"), 1, GNUNET_NO);
3286 free_address (&n->alternative_address);
3287 send_session_ack_message (n);
3289 case GNUNET_TRANSPORT_PS_DISCONNECT:
3290 GNUNET_STATISTICS_update (GST_stats,
3292 ("# unexpected SYN_ACK messages (disconnecting)"),
3294 return GNUNET_SYSERR;
3295 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3300 "Unhandled state `%s'\n",
3301 GNUNET_TRANSPORT_ps2s (n->state));
3303 return GNUNET_SYSERR;
3310 * A session was terminated. Take note; if needed, try to get
3311 * an alternative address from ATS.
3313 * @param peer identity of the peer where the session died
3314 * @param session session that is gone
3315 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3316 * this session was not in use
3319 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3320 struct Session *session)
3322 struct NeighbourMapEntry *n;
3323 struct BlackListCheckContext *bcc;
3324 struct BlackListCheckContext *bcc_next;
3326 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3328 while (NULL != (bcc = bcc_next))
3330 bcc_next = bcc->next;
3331 if (bcc->na.session == session)
3333 if (NULL != bcc->bc)
3334 GST_blacklist_test_cancel (bcc->bc);
3335 GNUNET_HELLO_address_free (bcc->na.address);
3336 GNUNET_CONTAINER_DLL_remove (bc_head,
3342 if (NULL == (n = lookup_neighbour (peer)))
3343 return GNUNET_NO; /* can't affect us */
3344 if (session != n->primary_address.session)
3346 /* Free alternative address */
3347 if (session == n->alternative_address.session)
3349 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) )
3350 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3351 free_address (&n->alternative_address);
3353 return GNUNET_NO; /* doesn't affect us further */
3356 n->expect_latency_response = GNUNET_NO;
3357 /* The session for neighbour's primary address died */
3360 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3364 case GNUNET_TRANSPORT_PS_INIT_ATS:
3368 case GNUNET_TRANSPORT_PS_SYN_SENT:
3369 /* The session used to send the SYN terminated:
3370 * this implies a connect error*/
3371 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3372 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3374 GST_plugins_a2s (n->primary_address.address),
3375 n->primary_address.session,
3378 /* Destroy the address since it cannot be used */
3379 unset_primary_address (n);
3380 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3381 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3383 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3384 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3385 /* error on inbound session; free neighbour entirely */
3386 free_address (&n->primary_address);
3389 case GNUNET_TRANSPORT_PS_CONNECTED:
3390 /* Our primary connection died, try a fast reconnect */
3391 unset_primary_address (n);
3392 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3393 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3395 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3396 /* we don't have an address, how can it go down? */
3399 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3400 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3401 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3403 GST_plugins_a2s (n->primary_address.address),
3404 n->primary_address.session,
3406 /* Destroy the address since it cannot be used */
3407 unset_primary_address (n);
3408 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3409 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3411 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3412 /* primary went down while we were waiting for SYN_ACK on secondary;
3413 secondary as primary */
3415 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3416 "Connection `%s' %p to peer `%s' was terminated while switching, "
3417 "switching to alternative address `%s' %p\n",
3418 GST_plugins_a2s (n->primary_address.address),
3419 n->primary_address.session,
3421 GST_plugins_a2s (n->alternative_address.address),
3422 n->alternative_address.session);
3424 /* Destroy the inbound address since it cannot be used */
3425 free_address (&n->primary_address);
3426 n->primary_address = n->alternative_address;
3427 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3428 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3429 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3431 case GNUNET_TRANSPORT_PS_DISCONNECT:
3432 free_address (&n->primary_address);
3434 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3435 /* neighbour was freed and plugins told to terminate session */
3439 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3440 "Unhandled state `%s'\n",
3441 GNUNET_TRANSPORT_ps2s (n->state));
3445 if (NULL != n->task)
3446 GNUNET_SCHEDULER_cancel (n->task);
3447 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3453 * We received a 'ACK' message from the other peer.
3454 * If we sent a 'SYN_ACK' last, this means we are now
3455 * connected. Otherwise, do nothing.
3457 * @param message possibly a 'struct SessionConnectMessage' (check format)
3458 * @param address address of the other peer
3459 * @param session session to use (or NULL)
3460 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3463 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3464 const struct GNUNET_HELLO_Address *address,
3465 struct Session *session)
3467 struct NeighbourMapEntry *n;
3469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3470 "Received ACK message from peer `%s'\n",
3471 GNUNET_i2s (&address->peer));
3472 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3474 GNUNET_break_op (0);
3475 return GNUNET_SYSERR;
3477 GNUNET_STATISTICS_update (GST_stats,
3479 ("# ACK messages received"),
3481 if (NULL == (n = lookup_neighbour (&address->peer)))
3483 GNUNET_break_op (0);
3484 return GNUNET_SYSERR;
3487 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3488 "Received %s for peer `%s' in state %s/%s\n",
3490 GNUNET_i2s (&address->peer),
3491 GNUNET_TRANSPORT_ps2s (n->state),
3492 print_ack_state (n->ack_state));
3494 /* Check if we are in a plausible state for having sent
3495 a SYN_ACK. If not, return, otherwise break.
3497 The remote peers sends a ACK as a response for a SYN_ACK
3501 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3502 now wait for the ACK to finally be connected
3503 - If we sent a SYN_ACK to this peer before */
3505 if ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3506 (ACK_SEND_ACK != n->ack_state))
3508 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3509 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3510 GNUNET_i2s (&address->peer),
3511 GNUNET_TRANSPORT_ps2s (n->state),
3512 print_ack_state (n->ack_state));
3514 GNUNET_STATISTICS_update (GST_stats,
3515 gettext_noop ("# unexpected ACK messages"), 1,
3520 /* We are connected */
3521 if (GNUNET_NO == GST_neighbours_test_connected(&n->id))
3523 /* Notify about connection */
3524 neighbours_connect_notification (&n->id,
3525 n->primary_address.bandwidth_in,
3526 n->primary_address.bandwidth_out);
3527 GNUNET_STATISTICS_set (GST_stats,
3528 gettext_noop ("# peers connected"),
3529 ++neighbours_connected,
3533 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3535 /* We tried to switch addresses while being connect. We explicitly wait
3536 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3537 * so we do not want to set the address as in use! */
3541 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3542 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3544 /* Set primary address to used */
3545 set_primary_address (n,
3546 n->primary_address.address,
3547 n->primary_address.session,
3548 n->primary_address.bandwidth_in,
3549 n->primary_address.bandwidth_out,
3556 * Test if we're connected to the given peer.
3558 * @param target peer to test
3559 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3562 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3564 return test_connected (lookup_neighbour (target));
3569 * Change the incoming quota for the given peer.
3571 * @param neighbour identity of peer to change qutoa for
3572 * @param quota new quota
3575 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3576 struct GNUNET_BANDWIDTH_Value32NBO quota)
3578 struct NeighbourMapEntry *n;
3580 if (NULL == (n = lookup_neighbour (neighbour)))
3582 GNUNET_STATISTICS_update (GST_stats,
3584 ("# SET QUOTA messages ignored (no such peer)"),
3588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3589 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3590 ntohl (quota.value__), GNUNET_i2s (&n->id));
3591 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3592 if (0 != ntohl (quota.value__))
3594 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3595 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3596 GNUNET_i2s (&n->id));
3597 if (GNUNET_YES == test_connected (n))
3598 GNUNET_STATISTICS_update (GST_stats,
3599 gettext_noop ("# disconnects due to quota of 0"),
3601 disconnect_neighbour (n);
3606 * Task to asynchronously run #free_neighbour().
3608 * @param cls the `struct NeighbourMapEntry` to free
3612 delayed_disconnect (void *cls,
3613 const struct GNUNET_SCHEDULER_TaskContext* tc)
3615 struct NeighbourMapEntry *n = cls;
3617 n->delayed_disconnect_task = NULL;
3618 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3619 "Disconnecting by request from peer %s\n",
3620 GNUNET_i2s (&n->id));
3626 * We received a disconnect message from the given peer,
3627 * validate and process.
3629 * @param peer sender of the message
3630 * @param msg the disconnect message
3633 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3634 const struct GNUNET_MessageHeader *msg)
3636 struct NeighbourMapEntry *n;
3637 const struct SessionDisconnectMessage *sdm;
3639 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3640 "Received DISCONNECT message from peer `%s'\n",
3642 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3644 GNUNET_break_op (0);
3645 GNUNET_STATISTICS_update (GST_stats,
3647 ("# disconnect messages ignored (malformed)"), 1,
3651 GNUNET_STATISTICS_update (GST_stats,
3653 ("# DISCONNECT messages received"),
3655 sdm = (const struct SessionDisconnectMessage *) msg;
3656 if (NULL == (n = lookup_neighbour (peer)))
3661 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3663 GNUNET_STATISTICS_update (GST_stats,
3664 gettext_noop ("# disconnect messages ignored (timestamp)"),
3669 if (0 != memcmp (peer,
3671 sizeof (struct GNUNET_PeerIdentity)))
3673 GNUNET_break_op (0);
3676 if (ntohl (sdm->purpose.size) !=
3677 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3678 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3679 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3682 "DISCONNECT message from peer `%s' has invalid size\n",
3684 GNUNET_break_op (0);
3688 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3694 "DISCONNECT message from peer `%s' cannot be verified \n",
3696 GNUNET_break_op (0);
3699 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect, n);
3704 * Closure for the #neighbours_iterate() function.
3706 struct IteratorContext
3709 * Function to call on each connected neighbour.
3711 GST_NeighbourIterator cb;
3714 * Closure for @e cb.
3721 * Call the callback from the closure for each neighbour.
3723 * @param cls the `struct IteratorContext`
3724 * @param key the hash of the public key of the neighbour
3725 * @param value the `struct NeighbourMapEntry`
3726 * @return #GNUNET_OK (continue to iterate)
3729 neighbours_iterate (void *cls,
3730 const struct GNUNET_PeerIdentity *key,
3733 struct IteratorContext *ic = cls;
3734 struct NeighbourMapEntry *n = value;
3735 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3736 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3738 if (NULL != n->primary_address.address)
3740 bandwidth_in = n->primary_address.bandwidth_in;
3741 bandwidth_out = n->primary_address.bandwidth_out;
3745 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3746 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3750 n->primary_address.address,
3753 bandwidth_in, bandwidth_out);
3759 * Iterate over all connected neighbours.
3761 * @param cb function to call
3762 * @param cb_cls closure for cb
3765 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3767 struct IteratorContext ic;
3769 if (NULL == neighbours)
3770 return; /* can happen during shutdown */
3773 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3778 * If we have an active connection to the given target, it must be shutdown.
3780 * @param target peer to disconnect from
3783 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3785 struct NeighbourMapEntry *n;
3787 if (NULL == (n = lookup_neighbour (target)))
3788 return; /* not active */
3789 if (GNUNET_YES == test_connected (n))
3790 GNUNET_STATISTICS_update (GST_stats,
3791 gettext_noop ("# disconnected from peer upon explicit request"),
3794 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3795 "Forced disconnect from peer %s\n",
3796 GNUNET_i2s (target));
3797 disconnect_neighbour (n);
3802 * Obtain current address information for the given neighbour.
3805 * @return address currently used
3807 struct GNUNET_HELLO_Address *
3808 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3810 struct NeighbourMapEntry *n;
3812 n = lookup_neighbour (peer);
3815 return n->primary_address.address;
3820 * Initialize the neighbours subsystem.
3822 * @param max_fds maximum number of fds to use
3825 GST_neighbours_start (unsigned int max_fds)
3827 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3829 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3830 &utilization_transmission,
3836 * Disconnect from the given neighbour.
3839 * @param key hash of neighbour's public key (not used)
3840 * @param value the 'struct NeighbourMapEntry' of the neighbour
3841 * @return #GNUNET_OK (continue to iterate)
3844 disconnect_all_neighbours (void *cls,
3845 const struct GNUNET_PeerIdentity *key,
3848 struct NeighbourMapEntry *n = value;
3850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3851 "Disconnecting peer `%4s' during shutdown\n",
3852 GNUNET_i2s (&n->id));
3859 * Cleanup the neighbours subsystem.
3862 GST_neighbours_stop ()
3864 struct BlacklistCheckSwitchContext *cur;
3865 struct BlacklistCheckSwitchContext *next;
3867 if (NULL == neighbours)
3869 if (NULL != util_transmission_tk)
3871 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3872 util_transmission_tk = NULL;
3874 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3875 &disconnect_all_neighbours,
3877 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3879 next = pending_bc_head;
3880 for (cur = next; NULL != cur; cur = next)
3883 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
3887 if (NULL != cur->blc)
3889 GST_blacklist_test_cancel (cur->blc);
3892 if (NULL != cur->address)
3893 GNUNET_HELLO_address_free (cur->address);
3900 /* end of file gnunet-service-transport_neighbours.c */