2 This file is part of GNUnet.
3 (C) 2010-2013 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_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport_clients.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
41 * Size of the neighbour hash map.
43 #define NEIGHBOUR_TABLE_SIZE 256
46 * Time we give plugin to transmit DISCONNECT message before the
47 * neighbour entry self-destructs.
49 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
52 * How often must a peer violate bandwidth quotas before we start
53 * to simply drop its messages?
55 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
58 * How long are we willing to wait for a response from ATS before timing out?
60 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
63 * How long are we willing to wait for an ACK from the other peer before
64 * giving up on our connect operation?
66 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
69 * How long are we willing to wait for a successful reconnect if
70 * an existing connection went down? Much shorter than the
71 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
72 * higher layers about the disconnect during this period.
74 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
77 * How long are we willing to wait for a response from the blacklist
78 * subsystem before timing out?
80 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
83 * Interval to send utilization data
85 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
88 * State describing which kind a reply this neighbour should send
93 * We did not receive a CONNECT message for this neighbour
97 /* The neighbour received a CONNECT message and has to send a CONNECT_ACK
99 ACK_SEND_CONNECT_ACK = 1,
101 /* The neighbour sent a CONNECT_ACK message and has to send a SESSION_ACK
103 ACK_SEND_SESSION_ACK = 2
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_CONNECT'
112 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
113 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
114 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
115 * should be connected.
117 struct SessionConnectMessage
120 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT
121 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_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 'CONNECT' 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;
257 * A possible address we could use to communicate with a neighbour.
259 struct NeighbourAddress
263 * Active session for this address.
265 struct Session *session;
268 * Network-level address information.
270 struct GNUNET_HELLO_Address *address;
273 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
274 * peer for this address. Use to check that the ACK is in response
275 * to our most recent 'CONNECT'.
277 struct GNUNET_TIME_Absolute connect_timestamp;
280 * Inbound bandwidth from ATS for this address.
282 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
285 * Outbound bandwidth from ATS for this address.
287 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
290 * Did we tell ATS that this is our 'active' address?
295 * The current nonce sent in the last keep alive messages
297 uint32_t keep_alive_nonce;
301 * Entry in neighbours.
303 struct NeighbourMapEntry
307 * Head of list of messages we would like to send to this peer;
308 * must contain at most one message per client.
310 struct MessageQueue *messages_head;
313 * Tail of list of messages we would like to send to this peer; must
314 * contain at most one message per client.
316 struct MessageQueue *messages_tail;
319 * Are we currently trying to send a message? If so, which one?
321 struct MessageQueue *is_active;
324 * Primary address we currently use to communicate with the neighbour.
326 struct NeighbourAddress primary_address;
329 * Alternative address currently under consideration for communicating
330 * with the neighbour.
332 struct NeighbourAddress alternative_address;
335 * Identity of this neighbour.
337 struct GNUNET_PeerIdentity id;
340 * Main task that drives this peer (timeouts, keepalives, etc.).
341 * Always runs the 'master_task'.
343 GNUNET_SCHEDULER_TaskIdentifier task;
346 * At what time should we sent the next keep-alive message?
348 struct GNUNET_TIME_Absolute keep_alive_time;
351 * At what time did we sent the last keep-alive message? Used
352 * to calculate round-trip time ("latency").
354 struct GNUNET_TIME_Absolute last_keep_alive_time;
357 * Timestamp we should include in our next CONNECT_ACK message.
358 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
359 * our CONNECT_ACK message.
361 struct GNUNET_TIME_Absolute connect_ack_timestamp;
364 * ATS address suggest handle
366 struct GNUNET_ATS_SuggestHandle *suggest_handle;
369 * Time where we should cut the connection (timeout) if we don't
370 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
371 * if we are in S_CONNECTED).
373 struct GNUNET_TIME_Absolute timeout;
376 * Latest calculated latency value
378 struct GNUNET_TIME_Relative latency;
381 * Tracker for inbound bandwidth.
383 struct GNUNET_BANDWIDTH_Tracker in_tracker;
386 * How often has the other peer (recently) violated the inbound
387 * traffic limit? Incremented by 10 per violation, decremented by 1
388 * per non-violation (for each time interval).
390 unsigned int quota_violation_count;
393 * The current state of the peer.
395 enum GNUNET_TRANSPORT_PeerState state;
398 * Did we sent an KEEP_ALIVE message and are we expecting a response?
400 int expect_latency_response;
403 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
404 * (once we have an address to use and the peer has been allowed by our
405 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_CONNECT_ACK
406 * if we need to send a CONNECT_ACK. Set to #ACK_SEND_CONNECT_ACK if we did
407 * send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving a
408 * 'SESSION_ACK' (regardless of what our own state machine might say).
410 enum GST_ACK_State ack_state;
413 * Tracking utilization of outbound bandwidth
415 uint32_t util_payload_bytes_sent;
418 * Tracking utilization of inbound bandwidth
420 uint32_t util_payload_bytes_recv;
423 * Tracking utilization of outbound bandwidth
425 uint32_t util_total_bytes_sent;
428 * Tracking utilization of inbound bandwidth
430 uint32_t util_total_bytes_recv;
433 * Date of last utilization transmission
435 struct GNUNET_TIME_Absolute last_util_transmission;
440 * Context for blacklist checks and the #handle_test_blacklist_cont()
441 * function. Stores information about ongoing blacklist checks.
443 struct BlackListCheckContext
447 * We keep blacklist checks in a DLL.
449 struct BlackListCheckContext *next;
452 * We keep blacklist checks in a DLL.
454 struct BlackListCheckContext *prev;
457 * Address that is being checked.
459 struct NeighbourAddress na;
462 * Handle to the ongoing blacklist check.
464 struct GST_BlacklistCheck *bc;
469 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
471 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
474 * We keep blacklist checks in a DLL so that we can find
475 * the 'sessions' in their 'struct NeighbourAddress' if
476 * a session goes down.
478 static struct BlackListCheckContext *bc_head;
481 * We keep blacklist checks in a DLL.
483 static struct BlackListCheckContext *bc_tail;
486 * List of pending blacklist checks: head
488 static struct BlacklistCheckSwitchContext *pending_bc_head;
491 * List of pending blacklist checks: tail
493 static struct BlacklistCheckSwitchContext *pending_bc_tail;
496 * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb
498 static void *callback_cls;
501 * Function to call when we connected to a neighbour.
503 static NotifyConnect connect_notify_cb;
506 * Function to call when we disconnected from a neighbour.
508 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
511 * Function to call when a neighbour changed address, state or bandwidth.
513 static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb;
516 * counter for connected neighbours
518 static unsigned int neighbours_connected;
521 * Number of bytes we have currently queued for transmission.
523 static unsigned long long bytes_in_send_queue;
526 * Task transmitting utilization data
528 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
531 static struct GNUNET_CONTAINER_MultiPeerMap *registered_quota_notifications;
534 print_ack_state (enum GST_ACK_State s)
539 case ACK_SEND_CONNECT_ACK:
540 return "SEND_CONNECT_ACK";
541 case ACK_SEND_SESSION_ACK:
542 return "SEND_SESSION_ACK";
550 * Lookup a neighbour entry in the neighbours hash map.
552 * @param pid identity of the peer to look up
553 * @return the entry, NULL if there is no existing record
555 static struct NeighbourMapEntry *
556 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
558 if (NULL == neighbours)
560 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
565 * Test if we're connected to the given peer.
567 * @param n neighbour entry of peer to test
568 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
571 test_connected (struct NeighbourMapEntry *n)
575 return GNUNET_TRANSPORT_is_connected (n->state);
579 * Send information about a new outbound quota to our clients.
581 * @param target affected peer
582 * @param quota new quota
585 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
586 struct GNUNET_BANDWIDTH_Value32NBO quota)
588 struct QuotaSetMessage q_msg;
590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
591 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
592 ntohl (quota.value__), GNUNET_i2s (target));
593 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
594 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
596 q_msg.peer = (*target);
597 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
602 * We don't need a given neighbour address any more.
603 * Release its resources and give appropriate notifications
604 * to ATS and other subsystems.
606 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
609 free_address (struct NeighbourAddress *na)
611 if (GNUNET_YES == na->ats_active)
613 GST_validation_set_address_use (na->address, na->session, GNUNET_NO);
614 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
617 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
618 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
619 na->ats_active = GNUNET_NO;
620 na->keep_alive_nonce = 0;
621 if (NULL != na->address)
623 GNUNET_HELLO_address_free (na->address);
631 * Set net state for this neighbour and notify monitoring
633 * @param n the respective neighbour
634 * @param s the new state
637 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
640 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
642 GNUNET_TRANSPORT_ps2s(s));
643 neighbour_change_cb (callback_cls,
645 n->primary_address.address,
646 n->state, n->timeout,
647 n->primary_address.bandwidth_in,
648 n->primary_address.bandwidth_out);
653 * Set net state and state timeout for this neighbour and notify monitoring
655 * @param n the respective neighbour
656 * @param s the new state
657 * @param timeout the new timeout
660 set_state_and_timeout (struct NeighbourMapEntry *n,
661 enum GNUNET_TRANSPORT_PeerState s,
662 struct GNUNET_TIME_Absolute timeout)
665 n->timeout = timeout;
666 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
668 GNUNET_TRANSPORT_ps2s(s),
669 GNUNET_STRINGS_absolute_time_to_string (timeout));
670 neighbour_change_cb (callback_cls,
672 n->primary_address.address,
673 n->state, n->timeout,
674 n->primary_address.bandwidth_in,
675 n->primary_address.bandwidth_out);
680 * Set new state timeout for this neighbour and notify monitoring
682 * @param n the respective neighbour
683 * @param timeout the new timeout
686 set_timeout (struct NeighbourMapEntry *n,
687 struct GNUNET_TIME_Absolute timeout)
689 n->timeout = timeout;
690 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed timeout %s\n",
692 GNUNET_STRINGS_absolute_time_to_string (timeout));
693 neighbour_change_cb (callback_cls,
695 n->primary_address.address,
696 n->state, n->timeout,
697 n->primary_address.bandwidth_in,
698 n->primary_address.bandwidth_out);
703 * Initialize the alternative address of a neighbour
705 * @param n the neighbour
706 * @param address address of the other peer, NULL if other peer
708 * @param session session to use (or NULL, in which case an
709 * address must be setup)
710 * @param bandwidth_in inbound quota to be used when connection is up
711 * @param bandwidth_out outbound quota to be used when connection is up
714 set_alternative_address (struct NeighbourMapEntry *n,
715 const struct GNUNET_HELLO_Address *address,
716 struct Session *session,
717 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
718 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
720 struct GNUNET_TRANSPORT_PluginFunctions *papi;
721 if (NULL == (papi = GST_plugins_find (address->transport_name)))
726 if (session == n->alternative_address.session)
728 n->alternative_address.bandwidth_in = bandwidth_in;
729 n->alternative_address.bandwidth_out = bandwidth_out;
732 free_address (&n->alternative_address);
734 session = papi->get_session (papi->cls, address);
737 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
738 "Failed to obtain new session for peer `%s' and address '%s'\n",
739 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
740 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
744 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
746 GST_plugins_a2s(address));
748 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
749 n->alternative_address.bandwidth_in = bandwidth_in;
750 n->alternative_address.bandwidth_out = bandwidth_out;
751 n->alternative_address.session = session;
752 n->alternative_address.ats_active = GNUNET_NO;
753 n->alternative_address.keep_alive_nonce = 0;
758 * Initialize the primary address of a neighbour
760 * @param n the neighbour
761 * @param address address of the other peer, NULL if other peer
763 * @param session session to use (or NULL, in which case an
764 * address must be setup)
765 * @param bandwidth_in inbound quota to be used when connection is up
766 * @param bandwidth_out outbound quota to be used when connection is up
767 * @param is_active #GNUNET_YES to mark this as the active address with ATS
770 set_primary_address (struct NeighbourMapEntry *n,
771 const struct GNUNET_HELLO_Address *address,
772 struct Session *session,
773 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
774 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
777 struct GNUNET_TRANSPORT_PluginFunctions *papi;
779 if (NULL == (papi = GST_plugins_find (address->transport_name)))
784 if (session == n->primary_address.session)
786 n->primary_address.bandwidth_in = bandwidth_in;
787 n->primary_address.bandwidth_out = bandwidth_out;
788 if (is_active != n->primary_address.ats_active)
790 n->primary_address.ats_active = is_active;
791 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
792 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
794 if (GNUNET_YES == is_active)
796 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
797 send_outbound_quota (&address->peer, bandwidth_out);
801 free_address (&n->primary_address);
803 session = papi->get_session (papi->cls, address);
806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
807 "Failed to obtain new session for peer `%s' and address '%s'\n",
808 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
809 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
813 n->primary_address.address = GNUNET_HELLO_address_copy (address);
814 n->primary_address.bandwidth_in = bandwidth_in;
815 n->primary_address.bandwidth_out = bandwidth_out;
816 n->primary_address.session = session;
817 n->primary_address.ats_active = is_active;
818 n->primary_address.keep_alive_nonce = 0;
819 if (GNUNET_YES == is_active)
821 /* Telling ATS about new session */
822 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
823 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
824 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
825 send_outbound_quota (&address->peer, bandwidth_out);
828 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
830 GST_plugins_a2s(address));
832 neighbour_change_cb (callback_cls,
834 n->primary_address.address,
835 n->state, n->timeout,
836 n->primary_address.bandwidth_in,
837 n->primary_address.bandwidth_out);
841 * Clear the primary address of a neighbour since this primary address is not
842 * valid anymore and notify monitoring about it
844 * @param n the neighbour
847 unset_primary_address (struct NeighbourMapEntry *n)
849 /* Unset primary address */
850 free_address (&n->primary_address);
852 /* Notify monitoring about it */
853 neighbour_change_cb (callback_cls,
856 n->state, n->timeout,
857 n->primary_address.bandwidth_in,
858 n->primary_address.bandwidth_out);
864 * Free a neighbour map entry.
866 * @param n entry to free
867 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
868 * #GNUNET_YES to keep all sessions
871 free_neighbour (struct NeighbourMapEntry *n,
874 struct MessageQueue *mq;
875 struct GNUNET_TRANSPORT_PluginFunctions *papi;
876 struct GNUNET_HELLO_Address *backup_primary;
878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
879 "Freeing neighbour state of peer `%s'\n",
880 GNUNET_i2s (&n->id));
881 n->is_active = NULL; /* always free'd by its own continuation! */
883 /* fail messages currently in the queue */
884 while (NULL != (mq = n->messages_head))
886 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
887 if (NULL != mq->cont)
888 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
891 /* It is too late to send other peer disconnect notifications, but at
892 least internally we need to get clean... */
893 if (GNUNET_YES == test_connected (n))
895 GNUNET_STATISTICS_set (GST_stats,
896 gettext_noop ("# peers connected"),
897 --neighbours_connected,
899 disconnect_notify_cb (callback_cls, &n->id);
902 /* Mark peer as disconnected */
903 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
905 if (NULL != n->primary_address.address)
906 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
908 backup_primary = NULL;
910 /* free addresses and mark as unused */
911 unset_primary_address (n);
912 free_address (&n->alternative_address);
914 /* cut all transport-level connection for this peer */
915 if ((GNUNET_NO == keep_sessions) &&
916 (NULL != backup_primary) &&
917 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
918 papi->disconnect_peer (papi->cls, &n->id);
920 if (NULL != backup_primary)
921 GNUNET_HELLO_address_free (backup_primary);
922 GNUNET_assert (GNUNET_YES ==
923 GNUNET_CONTAINER_multipeermap_remove (neighbours,
926 /* Cancel address requests for this peer */
927 if (NULL != n->suggest_handle)
929 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
930 n->suggest_handle = NULL;
933 /* Cancel the master task */
934 if (GNUNET_SCHEDULER_NO_TASK != n->task)
936 GNUNET_SCHEDULER_cancel (n->task);
937 n->task = GNUNET_SCHEDULER_NO_TASK;
939 /* free rest of memory */
945 * Transmit a message using the current session of the given
948 * @param n entry for the recipient
949 * @param msgbuf buffer to transmit
950 * @param msgbuf_size number of bytes in @a msgbuf buffer
951 * @param priority transmission priority
952 * @param timeout transmission timeout
953 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
954 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
955 * @param cont continuation to call when finished (can be NULL)
956 * @param cont_cls closure for @a cont
957 * @return timeout (copy of @a timeout or a calculated one if
958 * @a use_keepalive_timeout is #GNUNET_YES.
960 static struct GNUNET_TIME_Relative
961 send_with_session (struct NeighbourMapEntry *n,
962 const char *msgbuf, size_t msgbuf_size,
964 struct GNUNET_TIME_Relative timeout,
965 unsigned int use_keepalive_timeout,
966 GNUNET_TRANSPORT_TransmitContinuation cont,
969 struct GNUNET_TRANSPORT_PluginFunctions *papi;
970 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
972 GNUNET_assert (n->primary_address.session != NULL);
973 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
974 (-1 == papi->send (papi->cls,
975 n->primary_address.session,
978 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
979 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
980 papi->query_keepalive_factor (papi->cls))),
981 cont, cont_cls)))) &&
983 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
984 GST_neighbours_notify_data_sent (&n->id,
985 n->primary_address.address, n->primary_address.session, msgbuf_size);
986 GNUNET_break (NULL != papi);
992 * Master task run for every neighbour. Performs all of the time-related
993 * activities (keep alive, send next message, disconnect if idle, finish
994 * clean up after disconnect).
996 * @param cls the `struct NeighbourMapEntry` for which we are running
997 * @param tc scheduler context (unused)
1000 master_task (void *cls,
1001 const struct GNUNET_SCHEDULER_TaskContext *tc);
1005 * Function called when the 'DISCONNECT' message has been sent by the
1006 * plugin. Frees the neighbour --- if the entry still exists.
1009 * @param target identity of the neighbour that was disconnected
1010 * @param result #GNUNET_OK if the disconnect got out successfully
1011 * @param payload bytes payload
1012 * @param physical bytes physical
1015 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1016 int result, size_t payload, size_t physical)
1018 struct NeighbourMapEntry *n;
1020 n = lookup_neighbour (target);
1022 return; /* already gone */
1023 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1024 return; /* have created a fresh entry since */
1025 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1026 GNUNET_SCHEDULER_cancel (n->task);
1027 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1032 * Transmit a DISCONNECT message to the other peer.
1034 * @param n neighbour to send DISCONNECT message.
1037 send_disconnect (struct NeighbourMapEntry *n)
1039 struct SessionDisconnectMessage disconnect_msg;
1041 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1042 "Sending DISCONNECT message to peer `%4s'\n",
1043 GNUNET_i2s (&n->id));
1044 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1045 disconnect_msg.header.type =
1046 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1047 disconnect_msg.reserved = htonl (0);
1048 disconnect_msg.purpose.size =
1049 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1050 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1051 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1052 disconnect_msg.purpose.purpose =
1053 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1054 disconnect_msg.timestamp =
1055 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1056 disconnect_msg.public_key = GST_my_identity.public_key;
1057 GNUNET_assert (GNUNET_OK ==
1058 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1059 &disconnect_msg.purpose,
1060 &disconnect_msg.signature));
1062 (void) send_with_session (n,
1063 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1064 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1065 GNUNET_NO, &send_disconnect_cont, NULL);
1066 GNUNET_STATISTICS_update (GST_stats,
1068 ("# DISCONNECT messages sent"), 1,
1074 * Disconnect from the given neighbour, clean up the record.
1076 * @param n neighbour to disconnect from
1079 disconnect_neighbour (struct NeighbourMapEntry *n)
1081 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1082 "Disconnecting from peer %s in state %s\n",
1083 GNUNET_i2s (&n->id),
1084 GNUNET_TRANSPORT_ps2s (n->state));
1085 /* depending on state, notify neighbour and/or upper layers of this peer
1089 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1090 case GNUNET_TRANSPORT_PS_INIT_ATS:
1091 /* other peer is completely unaware of us, no need to send DISCONNECT */
1092 free_neighbour (n, GNUNET_NO);
1094 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1095 send_disconnect (n);
1096 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1098 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1099 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1100 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1101 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1102 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1103 free_neighbour (n, GNUNET_NO);
1105 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1106 /* we DID ACK the other peer's request, must send DISCONNECT */
1107 send_disconnect (n);
1108 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1110 case GNUNET_TRANSPORT_PS_CONNECTED:
1111 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1112 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1113 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1114 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1115 /* we are currently connected, need to send disconnect and do
1116 internal notifications and update statistics */
1117 send_disconnect (n);
1118 GNUNET_STATISTICS_set (GST_stats,
1119 gettext_noop ("# peers connected"),
1120 --neighbours_connected,
1122 disconnect_notify_cb (callback_cls, &n->id);
1123 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1125 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1126 /* ATS address request timeout, disconnect without sending disconnect message */
1127 GNUNET_STATISTICS_set (GST_stats,
1128 gettext_noop ("# peers connected"),
1129 --neighbours_connected,
1131 disconnect_notify_cb (callback_cls, &n->id);
1132 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1134 case GNUNET_TRANSPORT_PS_DISCONNECT:
1135 /* already disconnected, ignore */
1137 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1138 /* already cleaned up, how did we get here!? */
1142 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1143 "Unhandled state `%s'\n",
1144 GNUNET_TRANSPORT_ps2s (n->state));
1148 /* schedule timeout to clean up */
1149 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1150 GNUNET_SCHEDULER_cancel (n->task);
1151 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1157 * We're done with our transmission attempt, continue processing.
1159 * @param cls the `struct MessageQueue` of the message
1160 * @param receiver intended receiver
1161 * @param success whether it worked or not
1162 * @param size_payload bytes payload sent
1163 * @param physical bytes sent on wire
1166 transmit_send_continuation (void *cls,
1167 const struct GNUNET_PeerIdentity *receiver,
1168 int success, size_t size_payload, size_t physical)
1170 struct MessageQueue *mq = cls;
1171 struct NeighbourMapEntry *n;
1173 if (NULL == (n = lookup_neighbour (receiver)))
1176 return; /* disconnect or other error while transmitting, can happen */
1178 if (n->is_active == mq)
1180 /* this is still "our" neighbour, remove us from its queue
1181 and allow it to send the next message now */
1182 n->is_active = NULL;
1183 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1184 GNUNET_SCHEDULER_cancel (n->task);
1185 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1187 if (bytes_in_send_queue < mq->message_buf_size)
1189 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1190 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1191 bytes_in_send_queue, mq->message_buf_size,
1192 (GNUNET_OK == success) ? "OK" : "FAIL",
1193 size_payload, physical);
1198 GNUNET_break (size_payload == mq->message_buf_size);
1199 bytes_in_send_queue -= mq->message_buf_size;
1200 GNUNET_STATISTICS_set (GST_stats,
1202 ("# bytes in message queue for other peers"),
1203 bytes_in_send_queue, GNUNET_NO);
1204 if (GNUNET_OK == success)
1205 GNUNET_STATISTICS_update (GST_stats,
1207 ("# messages transmitted to other peers"),
1210 GNUNET_STATISTICS_update (GST_stats,
1212 ("# transmission failures for messages to other peers"),
1214 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1215 "Sending message to `%s' of type %u was a %s\n",
1216 GNUNET_i2s (receiver),
1217 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1218 (success == GNUNET_OK) ? "success" : "FAILURE");
1219 if (NULL != mq->cont)
1220 mq->cont (mq->cont_cls, success, size_payload, physical);
1226 * Check the message list for the given neighbour and if we can
1227 * send a message, do so. This function should only be called
1228 * if the connection is at least generally ready for transmission.
1229 * While we will only send one message at a time, no bandwidth
1230 * quota management is performed here. If a message was given to
1231 * the plugin, the continuation will automatically re-schedule
1232 * the 'master' task once the next message might be transmitted.
1234 * @param n target peer for which to transmit
1237 try_transmission_to_peer (struct NeighbourMapEntry *n)
1239 struct MessageQueue *mq;
1240 struct GNUNET_TIME_Relative timeout;
1242 if (NULL == n->primary_address.address)
1244 /* no address, why are we here? */
1248 if ((0 == n->primary_address.address->address_length) &&
1249 (NULL == n->primary_address.session))
1251 /* no address, why are we here? */
1255 if (NULL != n->is_active)
1257 /* transmission already pending */
1261 /* timeout messages from the queue that are past their due date */
1262 while (NULL != (mq = n->messages_head))
1264 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1265 if (timeout.rel_value_us > 0)
1267 GNUNET_STATISTICS_update (GST_stats,
1269 ("# messages timed out while in transport queue"),
1271 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1273 transmit_send_continuation (mq, &n->id,
1275 mq->message_buf_size, 0); /* timeout */
1278 return; /* no more messages */
1279 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1281 (void) send_with_session (n,
1282 mq->message_buf, mq->message_buf_size,
1283 0 /* priority */, timeout, GNUNET_NO,
1284 &transmit_send_continuation, mq);
1289 * Send keepalive message to the neighbour. Must only be called
1290 * if we are on 'connected' state or while trying to switch addresses.
1291 * Will internally determine if a keepalive is truly needed (so can
1292 * always be called).
1294 * @param n neighbour that went idle and needs a keepalive
1297 send_keepalive (struct NeighbourMapEntry *n)
1299 struct SessionKeepAliveMessage m;
1300 struct GNUNET_TIME_Relative timeout;
1303 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1304 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1305 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1306 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1307 return; /* no keepalive needed at this time */
1309 nonce = 0; /* 0 indicates 'not set' */
1311 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1314 "Sending keep alive to peer `%s' with nonce %u\n",
1315 GNUNET_i2s (&n->id), nonce);
1317 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1318 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1319 m.nonce = htonl (nonce);
1321 timeout = send_with_session (n,
1322 (const void *) &m, sizeof (m),
1323 UINT32_MAX /* priority */,
1324 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1326 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1328 n->primary_address.keep_alive_nonce = nonce;
1329 n->expect_latency_response = GNUNET_YES;
1330 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1331 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1337 * Keep the connection to the given neighbour alive longer,
1338 * we received a KEEPALIVE (or equivalent); send a response.
1340 * @param neighbour neighbour to keep alive (by sending keep alive response)
1341 * @param m the keep alive message containing the nonce to respond to
1344 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1345 const struct GNUNET_MessageHeader *m)
1347 struct NeighbourMapEntry *n;
1348 const struct SessionKeepAliveMessage *msg_in;
1349 struct SessionKeepAliveMessage msg;
1351 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1354 msg_in = (struct SessionKeepAliveMessage *) m;
1355 if (NULL == (n = lookup_neighbour (neighbour)))
1357 GNUNET_STATISTICS_update (GST_stats,
1359 ("# KEEPALIVE messages discarded (peer unknown)"),
1363 if (NULL == n->primary_address.session)
1365 GNUNET_STATISTICS_update (GST_stats,
1367 ("# KEEPALIVE messages discarded (no session)"),
1372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1373 "Received keep alive request from peer `%s' with nonce %u\n",
1374 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1376 /* send reply to allow neighbour to measure latency */
1377 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1378 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1379 msg.nonce = msg_in->nonce;
1380 (void) send_with_session(n,
1381 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1382 UINT32_MAX /* priority */,
1383 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1389 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1390 * latency to this peer. Pass the updated information (existing ats
1391 * plus calculated latency) to ATS.
1393 * @param neighbour neighbour to keep alive
1394 * @param m the message containing the keep alive response
1397 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1398 const struct GNUNET_MessageHeader *m)
1400 struct NeighbourMapEntry *n;
1401 const struct SessionKeepAliveMessage *msg;
1402 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1404 struct GNUNET_ATS_Information ats;
1406 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1409 msg = (const struct SessionKeepAliveMessage *) m;
1410 if (NULL == (n = lookup_neighbour (neighbour)))
1412 GNUNET_STATISTICS_update (GST_stats,
1414 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1418 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1419 (GNUNET_YES != n->expect_latency_response) )
1421 GNUNET_STATISTICS_update (GST_stats,
1423 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1427 if (NULL == n->primary_address.address)
1429 GNUNET_STATISTICS_update (GST_stats,
1431 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1435 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1437 GNUNET_STATISTICS_update (GST_stats,
1439 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1446 "Received keep alive response from peer `%s' for session %p\n",
1447 GNUNET_i2s (&n->id), n->primary_address.session);
1451 /* Update session timeout here */
1452 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1455 "Updating session for peer `%s' for session %p\n",
1456 GNUNET_i2s (&n->id), n->primary_address.session);
1457 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1464 n->primary_address.keep_alive_nonce = 0;
1465 n->expect_latency_response = GNUNET_NO;
1466 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1467 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1469 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1470 "Latency for peer `%s' is %s\n",
1471 GNUNET_i2s (&n->id),
1472 GNUNET_STRINGS_relative_time_to_string (n->latency,
1474 /* append latency */
1475 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1476 if (n->latency.rel_value_us > UINT32_MAX)
1477 latency = UINT32_MAX;
1479 latency = n->latency.rel_value_us;
1480 ats.value = htonl (latency);
1481 GST_ats_update_metrics (&n->id, n->primary_address.address,
1482 n->primary_address.session, &ats, 1);
1487 * We have received a message from the given sender. How long should
1488 * we delay before receiving more? (Also used to keep the peer marked
1491 * @param sender sender of the message
1492 * @param size size of the message
1493 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1494 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1495 * #GNUNET_SYSERR if the connection is not fully up yet
1496 * @return how long to wait before reading more from this sender
1498 struct GNUNET_TIME_Relative
1499 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1500 *sender, ssize_t size, int *do_forward)
1502 struct NeighbourMapEntry *n;
1503 struct GNUNET_TIME_Relative ret;
1505 if (NULL == neighbours)
1507 *do_forward = GNUNET_NO;
1508 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1510 if (NULL == (n = lookup_neighbour (sender)))
1512 GST_neighbours_try_connect (sender);
1513 if (NULL == (n = lookup_neighbour (sender)))
1515 GNUNET_STATISTICS_update (GST_stats,
1517 ("# messages discarded due to lack of neighbour record"),
1519 *do_forward = GNUNET_NO;
1520 return GNUNET_TIME_UNIT_ZERO;
1523 if (! test_connected (n))
1525 *do_forward = GNUNET_SYSERR;
1526 return GNUNET_TIME_UNIT_ZERO;
1528 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1530 n->quota_violation_count++;
1531 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1532 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1533 n->in_tracker.available_bytes_per_s__,
1534 n->quota_violation_count);
1535 /* Discount 32k per violation */
1536 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1540 if (n->quota_violation_count > 0)
1542 /* try to add 32k back */
1543 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1544 n->quota_violation_count--;
1547 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1549 GNUNET_STATISTICS_update (GST_stats,
1551 ("# bandwidth quota violations by other peers"),
1553 *do_forward = GNUNET_NO;
1554 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1556 *do_forward = GNUNET_YES;
1557 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1558 if (ret.rel_value_us > 0)
1560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1561 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1562 (unsigned long long) n->in_tracker.
1563 consumption_since_last_update__,
1564 (unsigned int) n->in_tracker.available_bytes_per_s__,
1565 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1566 GNUNET_STATISTICS_update (GST_stats,
1567 gettext_noop ("# ms throttling suggested"),
1568 (int64_t) ret.rel_value_us / 1000LL,
1576 * Transmit a message to the given target using the active connection.
1578 * @param target destination
1579 * @param msg message to send
1580 * @param msg_size number of bytes in msg
1581 * @param timeout when to fail with timeout
1582 * @param cont function to call when done
1583 * @param cont_cls closure for 'cont'
1586 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1587 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1588 GST_NeighbourSendContinuation cont, void *cont_cls)
1590 struct NeighbourMapEntry *n;
1591 struct MessageQueue *mq;
1593 /* All ove these cases should never happen; they are all API violations.
1594 But we check anyway, just to be sure. */
1595 if (NULL == (n = lookup_neighbour (target)))
1599 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1602 if (GNUNET_YES != test_connected (n))
1606 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1609 bytes_in_send_queue += msg_size;
1610 GNUNET_STATISTICS_set (GST_stats,
1612 ("# bytes in message queue for other peers"),
1613 bytes_in_send_queue, GNUNET_NO);
1614 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1616 mq->cont_cls = cont_cls;
1617 memcpy (&mq[1], msg, msg_size);
1618 mq->message_buf = (const char *) &mq[1];
1619 mq->message_buf_size = msg_size;
1620 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1621 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1622 if ( (NULL != n->is_active) ||
1623 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1625 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1626 GNUNET_SCHEDULER_cancel (n->task);
1627 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1631 send_session_connect_cont (void *cls,
1632 const struct GNUNET_PeerIdentity *target,
1634 size_t size_payload,
1635 size_t size_on_wire)
1637 struct NeighbourMapEntry *n;
1639 n = lookup_neighbour (target);
1642 /* CONNECT continuation was called after neighbor was freed,
1643 * for example due to a time out for the state or the session
1644 * used was already terminated: nothing to do here... */
1648 if (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state)
1650 /* CONNECT continuation was called after neighbor changed state,
1651 * for example due to a time out for the state or the session
1652 * used was already terminated: nothing to do here... */
1655 if (GNUNET_OK == result)
1658 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1659 _("Failed to send CONNECT message to peer `%s' using address `%s' session %p\n"),
1660 GNUNET_i2s (target),
1661 GST_plugins_a2s (n->primary_address.address),
1662 n->primary_address.session);
1664 /* Failed to send CONNECT message with this address */
1665 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1666 n->primary_address.session);
1667 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1670 /* Remove address and request and additional one */
1671 unset_primary_address (n);
1673 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1674 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1679 * Send a SESSION_CONNECT message via the given address.
1681 * @param na address to use
1684 send_session_connect (struct NeighbourAddress *na)
1686 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1687 struct SessionConnectMessage connect_msg;
1688 struct NeighbourMapEntry *n;
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691 "Sending SESSION_CONNECT message to peer %s\n",
1692 GNUNET_i2s (&na->address->peer));
1693 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1698 if (NULL == na->session)
1699 na->session = papi->get_session (papi->cls, na->address);
1700 if (NULL == na->session)
1705 GNUNET_STATISTICS_update (GST_stats,
1707 ("# SESSION_CONNECT messages sent"),
1709 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1710 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1711 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1712 connect_msg.reserved = htonl (0);
1713 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1715 papi->send (papi->cls,
1717 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1719 SETUP_CONNECTION_TIMEOUT,
1720 send_session_connect_cont, NULL))
1722 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1723 _("Failed to transmit CONNECT message via plugin to %s\n"),
1724 GST_plugins_a2s (na->address));
1726 n = lookup_neighbour (&na->address->peer);
1732 /* Hard failure to send the CONNECT message with this address:
1733 Destroy address and session */
1734 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1735 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1737 /* Remove address and request and additional one */
1738 unset_primary_address (n);
1739 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1740 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1744 GST_neighbours_notify_data_sent (&na->address->peer,
1747 sizeof (struct SessionConnectMessage));
1753 send_session_connect_ack_cont (void *cls,
1754 const struct GNUNET_PeerIdentity *target,
1756 size_t size_payload,
1757 size_t size_on_wire)
1759 struct NeighbourMapEntry *n;
1761 n = lookup_neighbour (target);
1764 /* CONNECT_ACK continuation was called after neighbor was freed,
1765 * for example due to a time out for the state or the session
1766 * used was already terminated: nothing to do here... */
1770 if (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state)
1772 /* CONNECT_ACK continuation was called after neighbor changed state,
1773 * for example due to a time out for the state or the session
1774 * used was already terminated: nothing to do here... */
1777 if (GNUNET_OK == result)
1780 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1781 _("Failed to send CONNECT_ACK message to peer `%s' using address `%s' session %p\n"),
1782 GNUNET_i2s (target),
1783 GST_plugins_a2s (n->primary_address.address),
1784 n->primary_address.session);
1786 /* Failed to send CONNECT_ACK message with this address */
1787 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1788 n->primary_address.session);
1789 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1792 /* Remove address and request and additional one */
1793 unset_primary_address (n);
1795 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1796 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1802 * Send a CONNECT_ACK message via the given address.
1804 * @param address address to use
1805 * @param session session to use
1806 * @param timestamp timestamp to use for the ACK message
1809 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1810 struct Session *session,
1811 struct GNUNET_TIME_Absolute timestamp)
1813 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1814 struct SessionConnectMessage connect_msg;
1815 struct NeighbourMapEntry *n;
1817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1818 "Sending CONNECT_ACK to peer `%s'\n",
1819 GNUNET_i2s (&address->peer));
1820 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1825 if (NULL == session)
1826 session = papi->get_session (papi->cls, address);
1827 if (NULL == session)
1832 GNUNET_STATISTICS_update (GST_stats,
1834 ("# CONNECT_ACK messages sent"),
1836 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1837 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1838 connect_msg.reserved = htonl (0);
1839 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1841 if (GNUNET_SYSERR == papi->send (papi->cls,
1843 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1845 GNUNET_TIME_UNIT_FOREVER_REL,
1846 send_session_connect_ack_cont, NULL))
1848 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1849 _("Failed to transmit CONNECT_ACK message via plugin to %s\n"),
1850 GST_plugins_a2s (address));
1852 n = lookup_neighbour (&address->peer);
1858 /* Hard failure to send the CONNECT_ACK message with this address:
1859 Destroy session (and address) */
1860 if (GNUNET_YES == GNUNET_HELLO_address_check_option(address,
1861 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1863 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1864 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1867 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1869 /* Remove address and request and additional one */
1870 unset_primary_address (n);
1871 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1872 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1878 struct QuotaNotificationRequest
1880 struct GNUNET_PeerIdentity peer;
1881 struct Session *session;
1885 struct QNR_LookContext
1887 struct GNUNET_PeerIdentity peer;
1888 struct Session *session;
1891 struct QuotaNotificationRequest *res;
1895 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1897 struct QNR_LookContext *qnr_ctx = cls;
1898 struct QuotaNotificationRequest *qnr = value;
1900 if ((qnr->session == qnr_ctx->session) &&
1901 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1902 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1904 qnr_ctx->res = value;
1911 GST_neighbours_register_quota_notification(void *cls,
1912 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1913 struct Session *session)
1915 struct QuotaNotificationRequest *qnr;
1916 struct QNR_LookContext qnr_ctx;
1918 if (NULL == registered_quota_notifications)
1920 return; /* init or shutdown */
1923 qnr_ctx.peer = (*peer);
1924 qnr_ctx.plugin = plugin;
1925 qnr_ctx.session = session;
1928 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1929 peer, &find_notification_request, &qnr_ctx);
1930 if (NULL != qnr_ctx.res)
1936 qnr = GNUNET_new (struct QuotaNotificationRequest);
1937 qnr->peer = (*peer);
1938 qnr->plugin = GNUNET_strdup (plugin);
1939 qnr->session = session;
1941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1942 "Adding notification for peer `%s' plugin `%s' session %p \n",
1943 GNUNET_i2s (peer), plugin, session);
1945 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1946 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1951 GST_neighbours_unregister_quota_notification(void *cls,
1952 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1954 struct QNR_LookContext qnr_ctx;
1956 if (NULL == registered_quota_notifications)
1958 return; /* init or shutdown */
1961 qnr_ctx.peer = (*peer);
1962 qnr_ctx.plugin = plugin;
1963 qnr_ctx.session = session;
1966 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1967 &find_notification_request, &qnr_ctx);
1968 if (NULL == qnr_ctx.res)
1974 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1975 "Removing notification for peer `%s' plugin `%s' session %p \n",
1976 GNUNET_i2s (peer), plugin, session);
1978 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
1980 GNUNET_free (qnr_ctx.res->plugin);
1981 GNUNET_free (qnr_ctx.res);
1985 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1987 /* struct NeighbourMapEntry *n = cls; */
1988 struct QuotaNotificationRequest *qnr = value;
1989 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1990 struct GNUNET_TIME_Relative delay;
1993 papi = GST_plugins_find(qnr->plugin);
2000 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
2001 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2002 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
2003 delay.rel_value_us / 1000);
2005 if (NULL != papi->update_inbound_delay)
2006 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
2012 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
2015 /* struct NeighbourMapEntry *n = cls; */
2016 struct QuotaNotificationRequest *qnr = value;
2018 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
2020 GNUNET_free(qnr->plugin);
2027 inbound_bw_tracker_update(void *cls)
2029 struct NeighbourMapEntry *n = cls;
2031 /* Quota was updated, tell plugins to update the time to receive next */
2032 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2033 &n->id, ¬ification_cb, n);
2038 * Create a fresh entry in the neighbour map for the given peer
2040 * @param peer peer to create an entry for
2041 * @return new neighbour map entry
2043 static struct NeighbourMapEntry *
2044 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2046 struct NeighbourMapEntry *n;
2048 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2049 "Creating new neighbour entry for `%s'\n",
2051 n = GNUNET_new (struct NeighbourMapEntry);
2053 n->ack_state = ACK_UNDEFINED;
2054 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2055 n->last_util_transmission = GNUNET_TIME_absolute_get();
2056 n->util_payload_bytes_recv = 0;
2057 n->util_payload_bytes_sent = 0;
2058 n->util_total_bytes_recv = 0;
2059 n->util_total_bytes_sent = 0;
2060 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
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, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2065 GNUNET_assert (GNUNET_OK ==
2066 GNUNET_CONTAINER_multipeermap_put (neighbours,
2068 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2074 * Check if the two given addresses are the same.
2075 * Actually only checks if the sessions are non-NULL
2076 * (which they should be) and then if they are identical;
2077 * the actual addresses don't matter if the session
2078 * pointers match anyway, and we must have session pointers
2081 * @param a1 first address to compare
2082 * @param a2 other address to compare
2083 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
2086 address_matches (const struct NeighbourAddress *a1,
2087 const struct NeighbourAddress *a2)
2089 if ( (NULL == a1->session) ||
2090 (NULL == a2->session) )
2095 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
2099 /* We received a address suggestion after requesting an address in
2100 * try_connect or after receiving a connect, switch to address
2103 address_suggest_cont (void *cls,
2104 const struct GNUNET_PeerIdentity *peer,
2105 const struct GNUNET_HELLO_Address *address, struct Session *session,
2106 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2107 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2108 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2110 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2111 bandwidth_in, bandwidth_out);
2115 struct BlacklistCheckSwitchContext
2117 struct BlacklistCheckSwitchContext *prev;
2118 struct BlacklistCheckSwitchContext *next;
2121 struct GST_BlacklistCheck *blc;
2123 struct GNUNET_HELLO_Address *address;
2124 struct Session *session;
2125 struct GNUNET_ATS_Information *ats;
2128 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2129 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2133 * Black list check result for try_connect call
2134 * If connection to the peer is allowed request adddress and
2136 * @param cls blc_ctx bl context
2137 * @param peer the peer
2138 * @param result the result
2141 try_connect_bl_check_cont (void *cls,
2142 const struct GNUNET_PeerIdentity *peer, int result)
2144 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2145 struct NeighbourMapEntry *n;
2147 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2148 GNUNET_free (blc_ctx);
2150 if (GNUNET_OK != result)
2152 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2153 _("Blacklisting disapproved to connect to peer `%s'\n"),
2158 /* Setup a new neighbour */
2159 n = setup_neighbour (peer);
2161 /* Request address suggestions for this peer */
2162 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2163 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2164 GNUNET_ATS_reset_backoff (GST_ats, peer);
2165 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2166 &address_suggest_cont, n);
2172 * Try to create a connection to the given target (eventually).
2174 * @param target peer to try to connect to
2177 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2179 struct NeighbourMapEntry *n;
2180 struct GST_BlacklistCheck *blc;
2181 struct BlacklistCheckSwitchContext *blc_ctx;
2183 if (NULL == neighbours)
2185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2186 "Asked to connect to peer `%s' during shutdown\n",
2187 GNUNET_i2s (target));
2188 return; /* during shutdown, do nothing */
2190 n = lookup_neighbour (target);
2191 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2192 "Asked to connect to peer `%s' (state: %s)\n",
2193 GNUNET_i2s (target),
2194 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2199 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2200 /* this should not be possible */
2202 free_neighbour (n, GNUNET_NO);
2204 case GNUNET_TRANSPORT_PS_INIT_ATS:
2205 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2206 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2207 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2208 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2209 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2211 "Ignoring request to try to connect to `%s', already trying!\n",
2212 GNUNET_i2s (target));
2213 return; /* already trying */
2214 case GNUNET_TRANSPORT_PS_CONNECTED:
2215 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2216 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2217 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2218 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2219 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2220 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2221 "Ignoring request to try to connect, already connected to `%s'!\n",
2222 GNUNET_i2s (target));
2223 return; /* already connected */
2224 case GNUNET_TRANSPORT_PS_DISCONNECT:
2225 /* get rid of remains, ready to re-try immediately */
2226 free_neighbour (n, GNUNET_NO);
2228 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2229 /* should not be possible */
2233 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2234 "Unhandled state `%s'\n",
2235 GNUNET_TRANSPORT_ps2s (n->state));
2237 free_neighbour (n, GNUNET_NO);
2242 /* Do blacklist check if connecting to this peer is allowed */
2243 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2244 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2246 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2247 &try_connect_bl_check_cont, blc_ctx)))
2255 * Function called with the result of a blacklist check.
2257 * @param cls closure with the `struct BlackListCheckContext`
2258 * @param peer peer this check affects
2259 * @param result #GNUNET_OK if the address is allowed
2262 handle_connect_blacklist_check_cont (void *cls,
2263 const struct GNUNET_PeerIdentity *peer,
2266 struct BlackListCheckContext *bcc = cls;
2267 struct NeighbourMapEntry *n;
2270 GNUNET_CONTAINER_DLL_remove (bc_head,
2273 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2274 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2276 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2278 if (NULL == (n = lookup_neighbour (peer)))
2280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2281 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2283 goto cleanup; /* nobody left to care about new address */
2286 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2287 "Blacklist check after CONNECT for peer `%s' in state %s/%s: %s\n",
2289 GNUNET_TRANSPORT_ps2s (n->state),
2290 print_ack_state (n->ack_state),
2291 (GNUNET_OK == result) ? "OK" : "FAIL");
2293 if (GNUNET_OK == result)
2295 /* Blacklist agreed on connecting to a peer with this address, notify ATS */
2296 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2297 "Notifying ATS peer's `%s' %s address `%s' session %p\n",
2299 (GNUNET_YES == GNUNET_HELLO_address_check_option(bcc->na.address,
2300 GNUNET_HELLO_ADDRESS_INFO_INBOUND)) ? "inbound" : "outbound",
2301 GST_plugins_a2s (bcc->na.address), bcc->na.session);
2302 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2307 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2308 /* This should not be possible */
2310 free_neighbour (n, GNUNET_NO);
2312 case GNUNET_TRANSPORT_PS_INIT_ATS:
2313 /* Waiting on ATS suggestion */
2315 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2317 /* TODO Why should I send an connect ACK message */
2318 /* waiting on CONNECT_ACK, send ACK if one is pending */
2320 if ( (GNUNET_OK == result) &&
2321 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2323 n->ack_state = ACK_SEND_SESSION_ACK;
2324 send_connect_ack_message (n->primary_address.address,
2325 n->primary_address.session,
2326 n->connect_ack_timestamp);
2330 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2331 if (GNUNET_OK == result)
2333 /* We received a connect request and blacklist allowed to communicate
2334 * with this peer, request an address from ATS*/
2335 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2336 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2339 "Requesting address for peer %s to ATS\n",
2341 if (NULL == n->suggest_handle)
2342 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2343 &address_suggest_cont, n);
2344 GNUNET_ATS_reset_backoff (GST_ats, peer);
2348 /* We received a CONNECT message from a peer, but blacklist denies to
2349 * communicate with this peer and this address
2350 * - Previous state: NOT_CONNECTED:
2351 * We can free the neighbour, since the CONNECT created it
2352 * - Previous state INIT_ATS:
2355 free_neighbour (n, GNUNET_NO);
2358 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2359 /* waiting on ATS suggestion, don't care about blacklist */
2361 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2362 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2365 "Blacklist result ignored, as it is not for our primary address\n");
2366 break; /* result for an address we currently don't care about */
2368 if (GNUNET_OK == result)
2370 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2371 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2372 send_connect_ack_message (bcc->na.address,
2374 n->connect_ack_timestamp);
2375 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2376 n->ack_state = ACK_SEND_SESSION_ACK;
2380 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2382 plugin = GST_plugins_find (bcc->na.address->transport_name);
2383 if ( (NULL != plugin) &&
2384 (NULL != bcc->na.session) )
2386 plugin->disconnect_session (plugin->cls,
2390 GNUNET_break (NULL != plugin);
2391 free_address (&n->primary_address);
2392 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2393 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2394 GNUNET_ATS_reset_backoff (GST_ats, peer);
2397 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2398 /* waiting on SESSION_ACK, send ACK if one is pending */
2399 if ( (GNUNET_OK == result) &&
2400 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2402 /* TODO: Why should this happen? */
2403 /* *Debug message: */ GNUNET_break (0);
2405 n->ack_state = ACK_SEND_SESSION_ACK;
2406 send_connect_ack_message (n->primary_address.address,
2407 n->primary_address.session,
2408 n->connect_ack_timestamp);
2411 case GNUNET_TRANSPORT_PS_CONNECTED:
2412 /* already connected, don't care about blacklist */
2414 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2415 /* still waiting on ATS suggestion, don't care about blacklist */
2417 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2418 if ( (GNUNET_OK == result) &&
2419 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2421 n->ack_state = ACK_SEND_SESSION_ACK;
2422 send_connect_ack_message (bcc->na.address,
2424 n->connect_ack_timestamp);
2426 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2429 "Blacklist result ignored, as it is not for our primary address\n");
2430 break; /* result for an address we currently don't care about */
2432 if (GNUNET_OK == result)
2434 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2435 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2436 send_session_connect (&n->primary_address);
2440 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
2441 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2444 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2445 /* waiting on CONNECT_ACK, don't care about blacklist */
2446 if ( (GNUNET_OK == result) &&
2447 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2449 n->ack_state = ACK_SEND_SESSION_ACK;
2450 send_connect_ack_message (n->primary_address.address,
2451 n->primary_address.session,
2452 n->connect_ack_timestamp);
2455 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2456 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2459 "Blacklist result ignored, as it is not for our primary address\n");
2460 break; /* result for an address we currently don't care about */
2462 if (GNUNET_OK == result)
2464 send_session_connect (&n->alternative_address);
2465 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2469 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2470 free_address (&n->alternative_address);
2473 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2474 /* waiting on CONNECT_ACK, don't care about blacklist */
2475 if ( (GNUNET_OK == result) &&
2476 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2478 n->ack_state = ACK_SEND_SESSION_ACK;
2479 send_connect_ack_message (n->primary_address.address,
2480 n->primary_address.session,
2481 n->connect_ack_timestamp);
2484 case GNUNET_TRANSPORT_PS_DISCONNECT:
2485 /* Nothing to do here, ATS will already do what can be done */
2487 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2488 /* should not be possible */
2492 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2493 "Unhandled state `%s'\n",
2494 GNUNET_TRANSPORT_ps2s (n->state));
2496 free_neighbour (n, GNUNET_NO);
2500 GNUNET_HELLO_address_free (bcc->na.address);
2506 * We received a CONNECT message and want to know if connecting to a particular
2507 * peer via a particular address is allowed. Check it!
2509 * @param peer identity of the peer to switch the address for
2510 * @param ts time at which the check was initiated
2511 * @param address address of the other peer, NULL if other peer
2513 * @param session session to use (or NULL)
2516 connect_check_blacklist (const struct GNUNET_PeerIdentity *peer,
2517 struct GNUNET_TIME_Absolute ts,
2518 const struct GNUNET_HELLO_Address *address,
2519 struct Session *session)
2521 struct BlackListCheckContext *bcc;
2522 struct GST_BlacklistCheck *bc;
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 "Checking peer `%s' against blacklist\n",
2527 bcc = GNUNET_new (struct BlackListCheckContext);
2528 bcc->na.address = GNUNET_HELLO_address_copy (address);
2529 bcc->na.session = session;
2530 bcc->na.connect_timestamp = ts;
2531 GNUNET_CONTAINER_DLL_insert (bc_head,
2534 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2535 (NULL != address) ? address->transport_name : NULL,
2536 &handle_connect_blacklist_check_cont, bcc)))
2538 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2539 we must only store 'bc' if 'bc' is non-NULL... */
2544 * We received a 'SESSION_CONNECT' message from the other peer.
2545 * Consider switching to it.
2547 * @param message possibly a 'struct SessionConnectMessage' (check format)
2548 * @param peer identity of the peer to switch the address for
2549 * @param address address of the other peer, NULL if other peer
2551 * @param session session to use (or NULL)
2552 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2555 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2556 const struct GNUNET_PeerIdentity *peer,
2557 const struct GNUNET_HELLO_Address *address,
2558 struct Session *session)
2560 const struct SessionConnectMessage *scm;
2561 struct NeighbourMapEntry *n;
2562 struct GNUNET_TIME_Absolute ts;
2564 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2565 "Received CONNECT message from peer `%s' with `%s' %p\n",
2566 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
2567 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2569 GNUNET_break_op (0);
2570 return GNUNET_SYSERR;
2572 GNUNET_STATISTICS_update (GST_stats,
2574 ("# CONNECT messages received"),
2576 if (NULL == neighbours)
2578 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2579 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2581 return GNUNET_OK; /* we're shutting down */
2583 scm = (const struct SessionConnectMessage *) message;
2584 GNUNET_break_op (0 == ntohl (scm->reserved));
2585 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2586 n = lookup_neighbour (peer);
2589 /* This is a new neighbour and set to not connected */
2590 n = setup_neighbour (peer);
2593 /* Remember this CONNECT message in neighbour */
2594 n->ack_state = ACK_SEND_CONNECT_ACK;
2595 n->connect_ack_timestamp = ts;
2597 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2598 "Received CONNECT for peer `%s' in state %s/%s\n",
2600 GNUNET_TRANSPORT_ps2s (n->state),
2601 print_ack_state (n->ack_state));
2605 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2606 /* Check if we are allowed to connect with this peer and this address */
2607 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND,
2608 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2609 connect_check_blacklist (peer, ts, address, session);
2611 case GNUNET_TRANSPORT_PS_INIT_ATS:
2612 /* CONNECT message takes priority over us asking ATS for address */
2613 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND,
2614 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2615 connect_check_blacklist (peer, ts, address, session);
2617 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2618 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2619 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2620 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2621 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2622 /* It can never hurt to have an alternative address in the above cases,
2623 see if it is allowed */
2624 connect_check_blacklist (peer, ts, address, session);
2626 case GNUNET_TRANSPORT_PS_CONNECTED:
2627 /* we are already connected and can thus send the ACK immediately;
2628 still, it can never hurt to have an alternative address, so also
2629 tell ATS about it */
2630 GNUNET_assert (NULL != n->primary_address.address);
2631 GNUNET_assert (NULL != n->primary_address.session);
2632 n->ack_state = ACK_UNDEFINED;
2633 send_connect_ack_message (n->primary_address.address,
2634 n->primary_address.session, ts);
2635 connect_check_blacklist (peer, ts, address, session);
2637 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2638 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2639 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2640 /* It can never hurt to have an alternative address in the above cases,
2641 see if it is allowed */
2642 connect_check_blacklist (peer, ts, address, session);
2644 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2645 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2646 /* we are already connected and can thus send the ACK immediately;
2647 still, it can never hurt to have an alternative address, so also
2648 tell ATS about it */
2649 GNUNET_assert (NULL != n->primary_address.address);
2650 GNUNET_assert (NULL != n->primary_address.session);
2651 n->ack_state = ACK_UNDEFINED;
2652 send_connect_ack_message (n->primary_address.address,
2653 n->primary_address.session, ts);
2654 connect_check_blacklist (peer, ts, address, session);
2656 case GNUNET_TRANSPORT_PS_DISCONNECT:
2657 /* get rid of remains without terminating sessions, ready to re-try */
2658 free_neighbour (n, GNUNET_YES);
2659 n = setup_neighbour (peer);
2660 /* Remember the CONNECT timestamp for ACK message */
2661 n->ack_state = ACK_SEND_CONNECT_ACK;
2662 n->connect_ack_timestamp = ts;
2663 /* Request an address for the peer */
2664 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2665 GNUNET_ATS_reset_backoff (GST_ats, peer);
2666 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2668 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2669 /* should not be possible */
2673 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2674 "Unhandled state `%s'\n",
2675 GNUNET_TRANSPORT_ps2s (n->state));
2677 return GNUNET_SYSERR;
2683 switch_address_bl_check_cont (void *cls,
2684 const struct GNUNET_PeerIdentity *peer, int result)
2686 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2687 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2688 struct NeighbourMapEntry *n;
2690 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2691 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2696 "Peer %s is unknown, suggestion ignored\n",
2699 if (result == GNUNET_NO)
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2702 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2703 GST_plugins_a2s (blc_ctx->address),
2705 GNUNET_i2s (&blc_ctx->address->peer));
2707 if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2709 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2710 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2711 blc_ctx->address->transport_name,
2712 GST_plugins_a2s (blc_ctx->address),
2714 GNUNET_i2s (&blc_ctx->address->peer));
2717 /* This address is blacklisted, delete address and session (if existing) in ATS */
2718 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2720 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2721 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2723 /* This is an inbound address, destroy full address */
2724 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2727 /* Remove blacklist check and clean up */
2728 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2729 GNUNET_HELLO_address_free (blc_ctx->address);
2730 GNUNET_free_non_null (blc_ctx->ats);
2731 GNUNET_free (blc_ctx);
2735 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2736 "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2737 GST_plugins_a2s (blc_ctx->address),
2739 GNUNET_i2s (&blc_ctx->address->peer));
2741 if (NULL == blc_ctx->session)
2743 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2745 "Obtained new session for peer `%s' and address '%s': %p\n",
2746 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2748 if (NULL == blc_ctx->session)
2750 /* No session could be obtained, remove blacklist check and clean up */
2751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2752 "Failed to obtain new session for peer `%s' and address '%s'\n",
2753 GNUNET_i2s (&blc_ctx->address->peer),
2754 GST_plugins_a2s (blc_ctx->address));
2755 /* Delete address in ATS */
2756 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2758 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2759 GNUNET_HELLO_address_free (blc_ctx->address);
2760 GNUNET_free_non_null (blc_ctx->ats);
2761 GNUNET_free (blc_ctx);
2767 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2769 free_neighbour (n, GNUNET_NO);
2771 case GNUNET_TRANSPORT_PS_INIT_ATS:
2772 /* We requested an address and ATS suggests one:
2773 * set primary address and send CONNECT message*/
2774 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2775 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2776 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2777 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2778 send_session_connect (&n->primary_address);
2780 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2781 /* waiting on CONNECT_ACK, send ACK if one is pending */
2782 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2784 n->ack_state = ACK_SEND_SESSION_ACK;
2785 send_connect_ack_message (n->primary_address.address,
2786 n->primary_address.session,
2787 n->connect_ack_timestamp);
2789 /* ATS suggests a different address, switch again */
2790 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2791 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2792 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2793 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2794 send_session_connect (&n->primary_address);
2796 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2797 /* We received an suggestion while waiting for a CONNECT blacklist check,
2798 * this suggestion was permitted by a blacklist check, so send ACK*/
2799 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2800 /* We requested an address and ATS suggests one:
2801 * set primary address and send CONNECT_ACK message*/
2802 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2803 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2804 /* Send an ACK message as a response to the CONNECT msg */
2805 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2806 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2807 send_connect_ack_message (n->primary_address.address,
2808 n->primary_address.session,
2809 n->connect_ack_timestamp);
2810 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2811 n->ack_state = ACK_SEND_SESSION_ACK;
2814 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2815 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2816 /* ATS asks us to switch while we were trying to connect; switch to new
2817 address and check blacklist again */
2818 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2819 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2820 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2821 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2822 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2823 blc_ctx->address, blc_ctx->session);
2825 case GNUNET_TRANSPORT_PS_CONNECTED:
2826 GNUNET_assert (NULL != n->primary_address.address);
2827 GNUNET_assert (NULL != n->primary_address.session);
2828 if (n->primary_address.session == blc_ctx->session)
2830 /* not an address change, just a quota change */
2831 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2832 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2835 /* ATS asks us to switch a life connection; see if we can get
2836 a CONNECT_ACK on it before we actually do this! */
2837 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2838 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2839 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2840 /* REMOVE */ connect_check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2841 blc_ctx->address, blc_ctx->session);
2843 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2844 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2845 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2846 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2847 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2848 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2849 blc_ctx->address, blc_ctx->session);
2851 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2852 /* ATS asks us to switch while we were trying to reconnect; switch to new
2853 address and check blacklist again */
2854 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2855 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2856 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2857 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2858 blc_ctx->address, blc_ctx->session);
2860 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2861 /* ATS asks us to switch while we were trying to reconnect; switch to new
2862 address and check blacklist again */
2863 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2864 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2865 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2866 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2867 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2868 blc_ctx->address, blc_ctx->session);
2870 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2871 if (n->primary_address.session == blc_ctx->session)
2873 /* ATS switches back to still-active session */
2874 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2875 free_address (&n->alternative_address);
2878 /* ATS asks us to switch a life connection, update blacklist check */
2879 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2880 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2881 /* REMOVE */ connect_check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2882 blc_ctx->address, blc_ctx->session);
2884 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2885 if (n->primary_address.session == blc_ctx->session)
2887 /* ATS switches back to still-active session */
2888 free_address (&n->alternative_address);
2889 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2892 /* ATS asks us to switch a life connection, update blacklist check */
2893 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2894 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2895 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2896 /* REMOVE */ connect_check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2897 blc_ctx->address, blc_ctx->session);
2899 case GNUNET_TRANSPORT_PS_DISCONNECT:
2900 /* not going to switch addresses while disconnecting */
2902 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2906 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2907 "Unhandled state `%s'\n",
2908 GNUNET_TRANSPORT_ps2s (n->state));
2913 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2914 GNUNET_HELLO_address_free(blc_ctx->address);
2915 GNUNET_free_non_null (blc_ctx->ats);
2916 GNUNET_free (blc_ctx);
2922 * For the given peer, switch to this address.
2924 * Before accepting this addresses and actively using it, a blacklist check
2925 * is performed. If this blacklist check fails the address will be destroyed.
2927 * @param peer identity of the peer to switch the address for
2928 * @param address address of the other peer,
2929 * @param session session to use or NULL if transport should initiate a session
2930 * @param ats performance data
2931 * @param ats_count number of entries in ats
2932 * @param bandwidth_in inbound quota to be used when connection is up,
2933 * 0 to disconnect from peer
2934 * @param bandwidth_out outbound quota to be used when connection is up,
2935 * 0 to disconnect from peer
2938 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2939 const struct GNUNET_HELLO_Address *address,
2940 struct Session *session,
2941 const struct GNUNET_ATS_Information *ats,
2943 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2944 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2946 struct NeighbourMapEntry *n;
2947 struct GST_BlacklistCheck *blc;
2948 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2949 struct BlacklistCheckSwitchContext *blc_ctx;
2952 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2953 "ATS has decided on an address for peer %s\n",
2955 GNUNET_assert (NULL != address->transport_name);
2956 if (NULL == (n = lookup_neighbour (peer)))
2958 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2959 "Peer %s is unknown, suggestion ignored\n",
2964 /* Check if plugin is available */
2965 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2967 /* we don't have the plugin for this address */
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2969 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2970 address->transport_name,
2972 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2975 if ((NULL == session) &&
2976 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2978 /* This is a inbound address and we do not have a session to use! */
2979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2980 "Inbound address without session `%s'! Destroying address...\n",
2981 GST_plugins_a2s (address));
2982 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2986 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2987 "ATS tells us to switch to %s address '%s' session %p for "
2988 "peer `%s' in state %s/%s (quota in/out %u %u )\n",
2989 GNUNET_HELLO_address_check_option (address,
2990 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2991 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2992 GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state),
2993 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2995 /* Perform blacklist check */
2996 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2997 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2998 blc_ctx->session = session;
2999 blc_ctx->bandwidth_in = bandwidth_in;
3000 blc_ctx->bandwidth_out = bandwidth_out;
3001 blc_ctx->ats_count = ats_count;
3002 blc_ctx->ats = NULL;
3005 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
3006 for (c = 0; c < ats_count; c++)
3008 blc_ctx->ats[c].type = ats[c].type;
3009 blc_ctx->ats[c].value = ats[c].value;
3013 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
3014 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
3015 &switch_address_bl_check_cont, blc_ctx)))
3023 send_utilization_data (void *cls,
3024 const struct GNUNET_PeerIdentity *key,
3027 struct NeighbourMapEntry *n = value;
3028 struct GNUNET_ATS_Information atsi[4];
3030 uint32_t bps_pl_out;
3033 struct GNUNET_TIME_Relative delta;
3035 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
3036 GNUNET_TIME_absolute_get ());
3040 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
3041 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
3043 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
3044 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
3045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3046 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
3051 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
3052 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
3054 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
3055 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
3058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3059 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
3063 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
3064 atsi[0].value = htonl (bps_out);
3065 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
3066 atsi[1].value = htonl (bps_in);
3068 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
3069 atsi[2].value = htonl (bps_pl_out);
3070 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
3071 atsi[3].value = htonl (bps_pl_in);
3073 GST_ats_update_metrics (key, n->primary_address.address,
3074 n->primary_address.session, atsi, 4);
3075 n->util_payload_bytes_recv = 0;
3076 n->util_payload_bytes_sent = 0;
3077 n->util_total_bytes_recv = 0;
3078 n->util_total_bytes_sent = 0;
3079 n->last_util_transmission = GNUNET_TIME_absolute_get();
3085 * Task transmitting utilization in a regular interval
3087 * @param cls the 'struct NeighbourMapEntry' for which we are running
3088 * @param tc scheduler context (unused)
3091 utilization_transmission (void *cls,
3092 const struct GNUNET_SCHEDULER_TaskContext *tc)
3094 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3096 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
3097 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
3099 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3100 utilization_transmission, NULL);
3106 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
3107 const struct GNUNET_HELLO_Address *address,
3108 struct Session *session,
3109 const struct GNUNET_MessageHeader *message)
3111 struct NeighbourMapEntry *n;
3113 n = lookup_neighbour (peer);
3116 n->util_total_bytes_recv += ntohs(message->size);
3121 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
3122 const struct GNUNET_HELLO_Address *address,
3123 struct Session *session,
3124 const struct GNUNET_MessageHeader *message)
3126 struct NeighbourMapEntry *n;
3127 n = lookup_neighbour (peer);
3130 n->util_payload_bytes_recv += ntohs(message->size);
3135 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
3136 const struct GNUNET_HELLO_Address *address,
3137 struct Session *session,
3140 struct NeighbourMapEntry *n;
3141 n = lookup_neighbour (peer);
3144 if (n->primary_address.session != session)
3146 n->util_total_bytes_sent += size;
3151 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
3154 struct NeighbourMapEntry *n;
3155 n = lookup_neighbour (peer);
3158 n->util_payload_bytes_sent += size;
3163 * Master task run for every neighbour. Performs all of the time-related
3164 * activities (keep alive, send next message, disconnect if idle, finish
3165 * clean up after disconnect).
3167 * @param cls the 'struct NeighbourMapEntry' for which we are running
3168 * @param tc scheduler context (unused)
3171 master_task (void *cls,
3172 const struct GNUNET_SCHEDULER_TaskContext *tc)
3174 struct NeighbourMapEntry *n = cls;
3175 struct GNUNET_TIME_Relative delay;
3177 n->task = GNUNET_SCHEDULER_NO_TASK;
3178 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3179 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3180 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3181 GNUNET_i2s (&n->id),
3182 GNUNET_TRANSPORT_ps2s(n->state),
3183 GNUNET_STRINGS_relative_time_to_string (delay,
3187 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3188 /* invalid state for master task, clean up */
3190 free_neighbour (n, GNUNET_NO);
3192 case GNUNET_TRANSPORT_PS_INIT_ATS:
3193 if (0 == delay.rel_value_us)
3195 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3196 "Connection to `%s' timed out waiting for ATS to provide address\n",
3197 GNUNET_i2s (&n->id));
3198 free_neighbour (n, GNUNET_NO);
3202 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3203 if (0 == delay.rel_value_us)
3205 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3206 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
3207 GNUNET_i2s (&n->id));
3208 /* We could not send to this address, delete address and session */
3209 if (NULL != n->primary_address.session)
3210 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
3211 n->primary_address.session);
3212 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3214 /* Remove address and request and additional one */
3216 unset_primary_address (n);
3217 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3218 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3222 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3223 if (0 == delay.rel_value_us)
3225 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3226 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
3227 GNUNET_i2s (&n->id));
3228 free_neighbour (n, GNUNET_NO);
3232 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3233 if (0 == delay.rel_value_us)
3235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3236 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3237 GNUNET_i2s (&n->id));
3238 free_neighbour (n, GNUNET_NO);
3242 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3243 if (0 == delay.rel_value_us)
3245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3246 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
3247 GNUNET_i2s (&n->id));
3248 free_neighbour (n, GNUNET_NO);
3252 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3253 if (0 == delay.rel_value_us)
3255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3256 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3257 GNUNET_i2s (&n->id));
3258 disconnect_neighbour (n);
3262 case GNUNET_TRANSPORT_PS_CONNECTED:
3263 if (0 == delay.rel_value_us)
3265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3266 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3267 GNUNET_i2s (&n->id));
3268 disconnect_neighbour (n);
3271 try_transmission_to_peer (n);
3274 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3275 if (0 == delay.rel_value_us)
3277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3278 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3279 GNUNET_i2s (&n->id));
3280 disconnect_neighbour (n);
3284 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3285 if (0 == delay.rel_value_us)
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
3289 GNUNET_i2s (&n->id));
3290 disconnect_neighbour (n);
3294 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3295 if (0 == delay.rel_value_us)
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3298 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3299 GNUNET_i2s (&n->id));
3300 disconnect_neighbour (n);
3304 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3305 if (0 == delay.rel_value_us)
3307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3308 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3309 GNUNET_i2s (&n->id));
3310 disconnect_neighbour (n);
3313 try_transmission_to_peer (n);
3316 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3317 if (0 == delay.rel_value_us)
3319 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3320 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3321 GNUNET_i2s (&n->id));
3322 disconnect_neighbour (n);
3325 try_transmission_to_peer (n);
3328 case GNUNET_TRANSPORT_PS_DISCONNECT:
3329 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3330 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3331 GNUNET_i2s (&n->id));
3332 free_neighbour (n, GNUNET_NO);
3334 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3335 /* how did we get here!? */
3339 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3340 "Unhandled state `%s'\n",
3341 GNUNET_TRANSPORT_ps2s (n->state));
3345 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3346 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3347 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3349 /* if we are *now* in one of these three states, we're sending
3350 keep alive messages, so we need to consider the keepalive
3351 delay, not just the connection timeout */
3352 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3355 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3356 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3363 * Send a SESSION_ACK message to the neighbour to confirm that we
3364 * got his CONNECT_ACK.
3366 * @param n neighbour to send the SESSION_ACK to
3369 send_session_ack_message (struct NeighbourMapEntry *n)
3371 struct GNUNET_MessageHeader msg;
3373 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3374 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3375 (void) send_with_session(n,
3376 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3377 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3383 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3384 * Consider switching to it.
3386 * @param message possibly a 'struct SessionConnectMessage' (check format)
3387 * @param peer identity of the peer to switch the address for
3388 * @param address address of the other peer, NULL if other peer
3390 * @param session session to use (or NULL)
3391 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3394 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3395 const struct GNUNET_PeerIdentity *peer,
3396 const struct GNUNET_HELLO_Address *address,
3397 struct Session *session)
3399 const struct SessionConnectMessage *scm;
3400 struct GNUNET_TIME_Absolute ts;
3401 struct NeighbourMapEntry *n;
3403 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3404 "Received CONNECT_ACK message from peer `%s'\n",
3407 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3409 GNUNET_break_op (0);
3410 return GNUNET_SYSERR;
3412 GNUNET_STATISTICS_update (GST_stats,
3414 ("# CONNECT_ACK messages received"),
3416 scm = (const struct SessionConnectMessage *) message;
3417 GNUNET_break_op (ntohl (scm->reserved) == 0);
3418 if (NULL == (n = lookup_neighbour (peer)))
3420 GNUNET_STATISTICS_update (GST_stats,
3422 ("# unexpected CONNECT_ACK messages (no peer)"),
3424 return GNUNET_SYSERR;
3426 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3429 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3431 free_neighbour (n, GNUNET_NO);
3432 return GNUNET_SYSERR;
3433 case GNUNET_TRANSPORT_PS_INIT_ATS:
3434 GNUNET_STATISTICS_update (GST_stats,
3436 ("# unexpected CONNECT_ACK messages (not ready)"),
3439 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3440 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3442 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3443 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3446 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3447 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3448 GNUNET_STATISTICS_set (GST_stats,
3449 gettext_noop ("# peers connected"),
3450 ++neighbours_connected,
3452 connect_notify_cb (callback_cls, &n->id,
3453 n->primary_address.bandwidth_in,
3454 n->primary_address.bandwidth_out);
3455 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3456 GST_ats_add_address (n->primary_address.address,
3457 n->primary_address.session,
3459 set_primary_address (n,
3460 n->primary_address.address,
3461 n->primary_address.session,
3462 n->primary_address.bandwidth_in,
3463 n->primary_address.bandwidth_out,
3465 send_session_ack_message (n);
3467 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3468 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3469 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3470 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3471 GNUNET_STATISTICS_update (GST_stats,
3473 ("# unexpected CONNECT_ACK messages (not ready)"),
3476 case GNUNET_TRANSPORT_PS_CONNECTED:
3477 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3478 send_session_ack_message (n);
3480 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3481 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3482 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3483 to give us a new address... */
3484 GNUNET_STATISTICS_update (GST_stats,
3486 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3489 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3490 /* new address worked; go back to connected! */
3491 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3492 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3493 send_session_ack_message (n);
3495 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3496 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3497 send_session_ack_message (n);
3499 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3500 /* new address worked; adopt it and go back to connected! */
3501 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3502 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3503 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3505 GST_ats_add_address (n->alternative_address.address,
3506 n->alternative_address.session,
3508 set_primary_address (n, n->alternative_address.address,
3509 n->alternative_address.session, n->alternative_address.bandwidth_in,
3510 n->alternative_address.bandwidth_out, GNUNET_YES);
3512 free_address (&n->alternative_address);
3513 send_session_ack_message (n);
3515 case GNUNET_TRANSPORT_PS_DISCONNECT:
3516 GNUNET_STATISTICS_update (GST_stats,
3518 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3520 return GNUNET_SYSERR;
3521 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3526 "Unhandled state `%s'\n",
3527 GNUNET_TRANSPORT_ps2s (n->state));
3529 return GNUNET_SYSERR;
3536 * A session was terminated. Take note; if needed, try to get
3537 * an alternative address from ATS.
3539 * @param peer identity of the peer where the session died
3540 * @param session session that is gone
3541 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3542 * this session was not in use
3545 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3546 struct Session *session)
3548 struct NeighbourMapEntry *n;
3549 struct BlackListCheckContext *bcc;
3550 struct BlackListCheckContext *bcc_next;
3552 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3554 while (NULL != (bcc = bcc_next))
3556 bcc_next = bcc->next;
3557 if (bcc->na.session == session)
3559 if (NULL != bcc->bc)
3560 GST_blacklist_test_cancel (bcc->bc);
3561 GNUNET_HELLO_address_free (bcc->na.address);
3562 GNUNET_CONTAINER_DLL_remove (bc_head,
3568 if (NULL == (n = lookup_neighbour (peer)))
3569 return GNUNET_NO; /* can't affect us */
3570 if (session != n->primary_address.session)
3572 if (session == n->alternative_address.session)
3574 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3575 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3576 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3579 free_address (&n->alternative_address);
3581 return GNUNET_NO; /* doesn't affect us further */
3584 n->expect_latency_response = GNUNET_NO;
3585 /* The session for neighbour's primary address died */
3588 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3590 free_neighbour (n, GNUNET_NO);
3592 case GNUNET_TRANSPORT_PS_INIT_ATS:
3594 free_neighbour (n, GNUNET_NO);
3596 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3597 /* The session used to send the CONNECT terminated:
3598 * this implies a connect error*/
3599 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3600 "Could not send CONNECT message with address `%s' session %p: session terminated, requesting new address\n",
3601 GST_plugins_a2s (n->primary_address.address), n->primary_address.session,
3603 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3604 unset_primary_address (n);
3605 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3606 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3608 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3609 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3610 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3611 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3612 /* error on inbound session; free neighbour entirely */
3613 free_address (&n->primary_address);
3614 free_neighbour (n, GNUNET_NO);
3616 case GNUNET_TRANSPORT_PS_CONNECTED:
3617 unset_primary_address (n);
3618 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3619 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3621 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3622 /* we don't have an address, how can it go down? */
3625 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3626 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3627 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3629 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3630 /* primary went down while we were checking secondary against
3631 blacklist, adopt secondary as primary */
3632 free_address (&n->primary_address);
3633 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3634 n->primary_address = n->alternative_address;
3635 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3637 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3638 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3639 secondary as primary */
3640 free_address (&n->primary_address);
3641 n->primary_address = n->alternative_address;
3642 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3643 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3645 case GNUNET_TRANSPORT_PS_DISCONNECT:
3646 free_address (&n->primary_address);
3648 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3649 /* neighbour was freed and plugins told to terminate session */
3653 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3654 "Unhandled state `%s'\n",
3655 GNUNET_TRANSPORT_ps2s (n->state));
3659 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3660 GNUNET_SCHEDULER_cancel (n->task);
3661 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3667 * We received a 'SESSION_ACK' message from the other peer.
3668 * If we sent a 'CONNECT_ACK' last, this means we are now
3669 * connected. Otherwise, do nothing.
3671 * @param message possibly a 'struct SessionConnectMessage' (check format)
3672 * @param peer identity of the peer to switch the address for
3673 * @param address address of the other peer, NULL if other peer
3675 * @param session session to use (or NULL)
3676 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3679 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3680 const struct GNUNET_PeerIdentity *peer,
3681 const struct GNUNET_HELLO_Address *address,
3682 struct Session *session)
3684 struct NeighbourMapEntry *n;
3686 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3687 "Received SESSION_ACK message from peer `%s'\n",
3689 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3691 GNUNET_break_op (0);
3692 return GNUNET_SYSERR;
3694 GNUNET_STATISTICS_update (GST_stats,
3696 ("# SESSION_ACK messages received"),
3698 if (NULL == (n = lookup_neighbour (peer)))
3700 GNUNET_break_op (0);
3701 return GNUNET_SYSERR;
3703 /* check if we are in a plausible state for having sent
3704 a CONNECT_ACK. If not, return, otherwise break */
3705 if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3706 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3707 (ACK_SEND_SESSION_ACK != n->ack_state) )
3709 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3710 "Received SESSION_ACK message from peer `%s' in state %s/%s\n",
3712 GNUNET_TRANSPORT_ps2s (n->state),
3713 print_ack_state (n->ack_state));
3714 GNUNET_STATISTICS_update (GST_stats,
3715 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3719 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3720 GNUNET_STATISTICS_set (GST_stats,
3721 gettext_noop ("# peers connected"),
3722 ++neighbours_connected,
3724 connect_notify_cb (callback_cls, &n->id,
3725 n->primary_address.bandwidth_in,
3726 n->primary_address.bandwidth_out);
3728 GST_ats_add_address (n->primary_address.address,
3729 n->primary_address.session,
3731 set_primary_address (n,
3732 n->primary_address.address,
3733 n->primary_address.session,
3734 n->primary_address.bandwidth_in,
3735 n->primary_address.bandwidth_out,
3742 * Test if we're connected to the given peer.
3744 * @param target peer to test
3745 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3748 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3750 return test_connected (lookup_neighbour (target));
3754 * Change the incoming quota for the given peer.
3756 * @param neighbour identity of peer to change qutoa for
3757 * @param quota new quota
3760 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3761 struct GNUNET_BANDWIDTH_Value32NBO quota)
3763 struct NeighbourMapEntry *n;
3765 if (NULL == (n = lookup_neighbour (neighbour)))
3767 GNUNET_STATISTICS_update (GST_stats,
3769 ("# SET QUOTA messages ignored (no such peer)"),
3773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3774 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3775 ntohl (quota.value__), GNUNET_i2s (&n->id));
3776 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3777 if (0 != ntohl (quota.value__))
3779 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3780 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3781 GNUNET_i2s (&n->id));
3782 if (GNUNET_YES == test_connected (n))
3783 GNUNET_STATISTICS_update (GST_stats,
3784 gettext_noop ("# disconnects due to quota of 0"),
3786 disconnect_neighbour (n);
3791 * We received a disconnect message from the given peer,
3792 * validate and process.
3794 * @param peer sender of the message
3795 * @param msg the disconnect message
3798 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3799 const struct GNUNET_MessageHeader *msg)
3801 struct NeighbourMapEntry *n;
3802 const struct SessionDisconnectMessage *sdm;
3804 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3805 "Received DISCONNECT message from peer `%s'\n",
3807 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3809 // GNUNET_break_op (0);
3810 GNUNET_STATISTICS_update (GST_stats,
3812 ("# disconnect messages ignored (old format)"), 1,
3816 GNUNET_STATISTICS_update (GST_stats,
3818 ("# DISCONNECT messages received"),
3820 sdm = (const struct SessionDisconnectMessage *) msg;
3821 if (NULL == (n = lookup_neighbour (peer)))
3822 return; /* gone already */
3823 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3825 GNUNET_STATISTICS_update (GST_stats,
3827 ("# disconnect messages ignored (timestamp)"), 1,
3831 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3833 GNUNET_break_op (0);
3836 if (ntohl (sdm->purpose.size) !=
3837 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3838 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3839 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3841 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3842 "%s message from peer `%s' has invalid size \n",
3845 GNUNET_break_op (0);
3849 GNUNET_CRYPTO_eddsa_verify
3850 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3851 &sdm->signature, &sdm->public_key))
3853 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3854 "%s message from peer `%s' cannot be verified \n",
3857 GNUNET_break_op (0);
3860 if (GNUNET_YES == test_connected (n))
3861 GNUNET_STATISTICS_update (GST_stats,
3863 ("# other peer asked to disconnect from us"), 1,
3865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3866 "Disconnecting by request from peer %s\n",
3868 disconnect_neighbour (n);
3873 * Closure for the neighbours_iterate function.
3875 struct IteratorContext
3878 * Function to call on each connected neighbour.
3880 GST_NeighbourIterator cb;
3890 * Call the callback from the closure for each neighbour.
3892 * @param cls the `struct IteratorContext`
3893 * @param key the hash of the public key of the neighbour
3894 * @param value the `struct NeighbourMapEntry`
3895 * @return #GNUNET_OK (continue to iterate)
3898 neighbours_iterate (void *cls,
3899 const struct GNUNET_PeerIdentity *key,
3902 struct IteratorContext *ic = cls;
3903 struct NeighbourMapEntry *n = value;
3904 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3905 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3908 if (NULL != n->primary_address.address)
3910 bandwidth_in = n->primary_address.bandwidth_in;
3911 bandwidth_out = n->primary_address.bandwidth_out;
3915 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3916 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3920 n->primary_address.address,
3923 bandwidth_in, bandwidth_out);
3929 * Iterate over all connected neighbours.
3931 * @param cb function to call
3932 * @param cb_cls closure for cb
3935 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3937 struct IteratorContext ic;
3939 if (NULL == neighbours)
3940 return; /* can happen during shutdown */
3943 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3948 * If we have an active connection to the given target, it must be shutdown.
3950 * @param target peer to disconnect from
3953 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3955 struct NeighbourMapEntry *n;
3957 if (NULL == (n = lookup_neighbour (target)))
3958 return; /* not active */
3959 if (GNUNET_YES == test_connected (n))
3960 GNUNET_STATISTICS_update (GST_stats,
3962 ("# disconnected from peer upon explicit request"), 1,
3964 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3965 "Forced disconnect from peer %s\n",
3966 GNUNET_i2s (target));
3967 disconnect_neighbour (n);
3972 * Obtain current latency information for the given neighbour.
3974 * @param peer to get the latency for
3975 * @return observed latency of the address, FOREVER if the
3976 * the connection is not up
3978 struct GNUNET_TIME_Relative
3979 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3981 struct NeighbourMapEntry *n;
3983 n = lookup_neighbour (peer);
3985 return GNUNET_TIME_UNIT_FOREVER_REL;
3988 case GNUNET_TRANSPORT_PS_CONNECTED:
3989 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3990 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3991 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3992 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3993 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3995 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3996 case GNUNET_TRANSPORT_PS_INIT_ATS:
3997 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3998 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3999 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
4000 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
4001 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
4002 case GNUNET_TRANSPORT_PS_DISCONNECT:
4003 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
4004 return GNUNET_TIME_UNIT_FOREVER_REL;
4006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
4007 "Unhandled state `%s'\n",
4008 GNUNET_TRANSPORT_ps2s (n->state));
4012 return GNUNET_TIME_UNIT_FOREVER_REL;
4017 * Obtain current address information for the given neighbour.
4020 * @return address currently used
4022 struct GNUNET_HELLO_Address *
4023 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
4025 struct NeighbourMapEntry *n;
4027 n = lookup_neighbour (peer);
4030 return n->primary_address.address;
4035 * Initialize the neighbours subsystem.
4037 * @param cls closure for callbacks
4038 * @param connect_cb function to call if we connect to a peer
4039 * @param disconnect_cb function to call if we disconnect from a peer
4040 * @param peer_address_cb function to call if we change an active address
4042 * @param max_fds maximum number of fds to use
4045 GST_neighbours_start (void *cls,
4046 NotifyConnect connect_cb,
4047 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
4048 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
4049 unsigned int max_fds)
4052 connect_notify_cb = connect_cb;
4053 disconnect_notify_cb = disconnect_cb;
4054 neighbour_change_cb = peer_address_cb;
4055 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
4056 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
4057 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
4058 utilization_transmission, NULL);
4063 * Disconnect from the given neighbour.
4066 * @param key hash of neighbour's public key (not used)
4067 * @param value the 'struct NeighbourMapEntry' of the neighbour
4068 * @return #GNUNET_OK (continue to iterate)
4071 disconnect_all_neighbours (void *cls,
4072 const struct GNUNET_PeerIdentity *key,
4075 struct NeighbourMapEntry *n = value;
4077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4078 "Disconnecting peer `%4s', %s\n",
4079 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
4080 free_neighbour (n, GNUNET_NO);
4086 * Cleanup the neighbours subsystem.
4089 GST_neighbours_stop ()
4091 struct BlacklistCheckSwitchContext *cur;
4092 struct BlacklistCheckSwitchContext *next;
4094 if (NULL == neighbours)
4096 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
4098 GNUNET_SCHEDULER_cancel (util_transmission_tk);
4099 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
4102 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
4103 &disconnect_all_neighbours,
4105 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
4107 next = pending_bc_head;
4108 for (cur = next; NULL != cur; cur = next )
4111 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
4113 if (NULL != cur->blc)
4115 GST_blacklist_test_cancel (cur->blc);
4118 if (NULL != cur->address)
4119 GNUNET_HELLO_address_free (cur->address);
4120 GNUNET_free_non_null (cur->ats);
4124 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
4125 &free_notification_cb, NULL);
4126 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
4127 registered_quota_notifications = NULL;
4130 callback_cls = NULL;
4131 connect_notify_cb = NULL;
4132 disconnect_notify_cb = NULL;
4133 neighbour_change_cb = NULL;
4137 /* end of file gnunet-service-transport_neighbours.c */