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));
1680 * Send a SESSION_CONNECT message via the given address.
1682 * @param na address to use
1685 send_session_connect (struct NeighbourAddress *na)
1687 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1688 struct SessionConnectMessage connect_msg;
1689 struct NeighbourMapEntry *n;
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Sending SESSION_CONNECT message to peer %s\n",
1693 GNUNET_i2s (&na->address->peer));
1694 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1699 if (NULL == na->session)
1700 na->session = papi->get_session (papi->cls, na->address);
1701 if (NULL == na->session)
1706 GNUNET_STATISTICS_update (GST_stats,
1708 ("# SESSION_CONNECT messages sent"),
1710 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1711 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1712 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1713 connect_msg.reserved = htonl (0);
1714 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1716 papi->send (papi->cls,
1718 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1720 SETUP_CONNECTION_TIMEOUT,
1721 send_session_connect_cont, NULL))
1723 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1724 _("Failed to transmit CONNECT message via plugin to %s\n"),
1725 GST_plugins_a2s (na->address));
1727 n = lookup_neighbour (&na->address->peer);
1733 /* Hard failure to send the CONNECT message with this address:
1734 Destroy address and session */
1735 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1736 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1738 /* Remove address and request and additional one */
1739 unset_primary_address (n);
1740 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1741 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1745 GST_neighbours_notify_data_sent (&na->address->peer,
1748 sizeof (struct SessionConnectMessage));
1754 * Send a SESSION_CONNECT_ACK message via the given address.
1756 * @param address address to use
1757 * @param session session to use
1758 * @param timestamp timestamp to use for the ACK message
1761 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1762 struct Session *session,
1763 struct GNUNET_TIME_Absolute timestamp)
1765 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1766 struct SessionConnectMessage connect_msg;
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 "Sending CONNECT_ACK to peer `%s'\n",
1770 GNUNET_i2s (&address->peer));
1771 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1776 if (NULL == session)
1777 session = papi->get_session (papi->cls, address);
1778 if (NULL == session)
1783 GNUNET_STATISTICS_update (GST_stats,
1785 ("# CONNECT_ACK messages sent"),
1787 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1788 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1789 connect_msg.reserved = htonl (0);
1790 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1791 (void) papi->send (papi->cls,
1793 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1795 GNUNET_TIME_UNIT_FOREVER_REL,
1800 struct QuotaNotificationRequest
1802 struct GNUNET_PeerIdentity peer;
1803 struct Session *session;
1807 struct QNR_LookContext
1809 struct GNUNET_PeerIdentity peer;
1810 struct Session *session;
1813 struct QuotaNotificationRequest *res;
1817 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1819 struct QNR_LookContext *qnr_ctx = cls;
1820 struct QuotaNotificationRequest *qnr = value;
1822 if ((qnr->session == qnr_ctx->session) &&
1823 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1824 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1826 qnr_ctx->res = value;
1833 GST_neighbours_register_quota_notification(void *cls,
1834 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1835 struct Session *session)
1837 struct QuotaNotificationRequest *qnr;
1838 struct QNR_LookContext qnr_ctx;
1840 if (NULL == registered_quota_notifications)
1842 return; /* init or shutdown */
1845 qnr_ctx.peer = (*peer);
1846 qnr_ctx.plugin = plugin;
1847 qnr_ctx.session = session;
1850 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1851 peer, &find_notification_request, &qnr_ctx);
1852 if (NULL != qnr_ctx.res)
1858 qnr = GNUNET_new (struct QuotaNotificationRequest);
1859 qnr->peer = (*peer);
1860 qnr->plugin = GNUNET_strdup (plugin);
1861 qnr->session = session;
1863 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1864 "Adding notification for peer `%s' plugin `%s' session %p \n",
1865 GNUNET_i2s (peer), plugin, session);
1867 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1868 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1873 GST_neighbours_unregister_quota_notification(void *cls,
1874 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1876 struct QNR_LookContext qnr_ctx;
1878 if (NULL == registered_quota_notifications)
1880 return; /* init or shutdown */
1883 qnr_ctx.peer = (*peer);
1884 qnr_ctx.plugin = plugin;
1885 qnr_ctx.session = session;
1888 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1889 &find_notification_request, &qnr_ctx);
1890 if (NULL == qnr_ctx.res)
1896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897 "Removing notification for peer `%s' plugin `%s' session %p \n",
1898 GNUNET_i2s (peer), plugin, session);
1900 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
1902 GNUNET_free (qnr_ctx.res->plugin);
1903 GNUNET_free (qnr_ctx.res);
1907 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1909 /* struct NeighbourMapEntry *n = cls; */
1910 struct QuotaNotificationRequest *qnr = value;
1911 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1912 struct GNUNET_TIME_Relative delay;
1915 papi = GST_plugins_find(qnr->plugin);
1922 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
1923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1924 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
1925 delay.rel_value_us / 1000);
1927 if (NULL != papi->update_inbound_delay)
1928 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
1934 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
1937 /* struct NeighbourMapEntry *n = cls; */
1938 struct QuotaNotificationRequest *qnr = value;
1940 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
1942 GNUNET_free(qnr->plugin);
1949 inbound_bw_tracker_update(void *cls)
1951 struct NeighbourMapEntry *n = cls;
1953 /* Quota was updated, tell plugins to update the time to receive next */
1954 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1955 &n->id, ¬ification_cb, n);
1960 * Create a fresh entry in the neighbour map for the given peer
1962 * @param peer peer to create an entry for
1963 * @return new neighbour map entry
1965 static struct NeighbourMapEntry *
1966 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1968 struct NeighbourMapEntry *n;
1970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1971 "Creating new neighbour entry for `%s'\n",
1973 n = GNUNET_new (struct NeighbourMapEntry);
1975 n->ack_state = ACK_UNDEFINED;
1976 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1977 n->last_util_transmission = GNUNET_TIME_absolute_get();
1978 n->util_payload_bytes_recv = 0;
1979 n->util_payload_bytes_sent = 0;
1980 n->util_total_bytes_recv = 0;
1981 n->util_total_bytes_sent = 0;
1982 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
1983 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1984 MAX_BANDWIDTH_CARRY_S);
1985 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1986 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
1987 GNUNET_assert (GNUNET_OK ==
1988 GNUNET_CONTAINER_multipeermap_put (neighbours,
1990 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1996 * Check if the two given addresses are the same.
1997 * Actually only checks if the sessions are non-NULL
1998 * (which they should be) and then if they are identical;
1999 * the actual addresses don't matter if the session
2000 * pointers match anyway, and we must have session pointers
2003 * @param a1 first address to compare
2004 * @param a2 other address to compare
2005 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
2008 address_matches (const struct NeighbourAddress *a1,
2009 const struct NeighbourAddress *a2)
2011 if ( (NULL == a1->session) ||
2012 (NULL == a2->session) )
2017 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
2021 /* We received a address suggestion after requesting an address in
2022 * try_connect or after receiving a connect, switch to address
2025 address_suggest_cont (void *cls,
2026 const struct GNUNET_PeerIdentity *peer,
2027 const struct GNUNET_HELLO_Address *address, struct Session *session,
2028 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2029 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2030 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2032 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2033 bandwidth_in, bandwidth_out);
2037 struct BlacklistCheckSwitchContext
2039 struct BlacklistCheckSwitchContext *prev;
2040 struct BlacklistCheckSwitchContext *next;
2043 struct GST_BlacklistCheck *blc;
2045 struct GNUNET_HELLO_Address *address;
2046 struct Session *session;
2047 struct GNUNET_ATS_Information *ats;
2050 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2051 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2055 * Black list check result for try_connect call
2056 * If connection to the peer is allowed request adddress and
2058 * @param cls blc_ctx bl context
2059 * @param peer the peer
2060 * @param result the result
2063 try_connect_bl_check_cont (void *cls,
2064 const struct GNUNET_PeerIdentity *peer, int result)
2066 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2067 struct NeighbourMapEntry *n;
2069 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2070 GNUNET_free (blc_ctx);
2072 if (GNUNET_OK != result)
2074 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2075 _("Blacklisting disapproved to connect to peer `%s'\n"),
2080 /* Setup a new neighbour */
2081 n = setup_neighbour (peer);
2083 /* Request address suggestions for this peer */
2084 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2085 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2086 GNUNET_ATS_reset_backoff (GST_ats, peer);
2087 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2088 &address_suggest_cont, n);
2094 * Try to create a connection to the given target (eventually).
2096 * @param target peer to try to connect to
2099 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2101 struct NeighbourMapEntry *n;
2102 struct GST_BlacklistCheck *blc;
2103 struct BlacklistCheckSwitchContext *blc_ctx;
2105 if (NULL == neighbours)
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 "Asked to connect to peer `%s' during shutdown\n",
2109 GNUNET_i2s (target));
2110 return; /* during shutdown, do nothing */
2112 n = lookup_neighbour (target);
2113 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2114 "Asked to connect to peer `%s' (state: %s)\n",
2115 GNUNET_i2s (target),
2116 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2121 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2122 /* this should not be possible */
2124 free_neighbour (n, GNUNET_NO);
2126 case GNUNET_TRANSPORT_PS_INIT_ATS:
2127 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2128 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2129 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2130 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2131 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2132 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2133 "Ignoring request to try to connect to `%s', already trying!\n",
2134 GNUNET_i2s (target));
2135 return; /* already trying */
2136 case GNUNET_TRANSPORT_PS_CONNECTED:
2137 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2138 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2139 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2140 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2141 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2143 "Ignoring request to try to connect, already connected to `%s'!\n",
2144 GNUNET_i2s (target));
2145 return; /* already connected */
2146 case GNUNET_TRANSPORT_PS_DISCONNECT:
2147 /* get rid of remains, ready to re-try immediately */
2148 free_neighbour (n, GNUNET_NO);
2150 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2151 /* should not be possible */
2154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2155 "Unhandled state `%s'\n",
2156 GNUNET_TRANSPORT_ps2s (n->state));
2158 free_neighbour (n, GNUNET_NO);
2163 /* Do blacklist check if connecting to this peer is allowed */
2164 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2165 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2167 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2168 &try_connect_bl_check_cont, blc_ctx)))
2176 * Function called with the result of a blacklist check.
2178 * @param cls closure with the `struct BlackListCheckContext`
2179 * @param peer peer this check affects
2180 * @param result #GNUNET_OK if the address is allowed
2183 handle_test_blacklist_cont (void *cls,
2184 const struct GNUNET_PeerIdentity *peer,
2187 struct BlackListCheckContext *bcc = cls;
2188 struct NeighbourMapEntry *n;
2191 GNUNET_CONTAINER_DLL_remove (bc_head,
2194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2195 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2197 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2198 if (GNUNET_OK == result)
2200 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2204 /* Blacklist disagreed on connecting to a peer with this address
2205 * Destroy address because we are not allowed to use it
2207 if (NULL != bcc->na.session)
2208 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
2209 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
2211 if (NULL == (n = lookup_neighbour (peer)))
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2216 goto cleanup; /* nobody left to care about new address */
2218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2219 "Received blacklist result for peer `%s' in state %s/%s\n",
2221 GNUNET_TRANSPORT_ps2s (n->state),
2222 print_ack_state (n->ack_state));
2225 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2226 /* this should not be possible */
2228 free_neighbour (n, GNUNET_NO);
2230 case GNUNET_TRANSPORT_PS_INIT_ATS:
2231 /* waiting on ATS suggestion; still, pass address to ATS as a
2234 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2235 /* waiting on CONNECT_ACK, send ACK if one is pending */
2236 if ( (GNUNET_OK == result) &&
2237 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2239 n->ack_state = ACK_SEND_SESSION_ACK;
2240 send_session_connect_ack_message (n->primary_address.address,
2241 n->primary_address.session,
2242 n->connect_ack_timestamp);
2245 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2246 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2247 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2248 GNUNET_ATS_reset_backoff (GST_ats, peer);
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250 "Suggesting address for peer %s to ATS\n",
2252 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2253 &address_suggest_cont, n);
2255 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2256 /* waiting on ATS suggestion, don't care about blacklist */
2258 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2259 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2262 "Blacklist result ignored, as it is not for our primary address\n");
2263 break; /* result for an address we currently don't care about */
2265 if (GNUNET_OK == result)
2267 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2268 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2269 send_session_connect_ack_message (bcc->na.address,
2271 n->connect_ack_timestamp);
2272 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2273 n->ack_state = ACK_SEND_SESSION_ACK;
2277 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2279 plugin = GST_plugins_find (bcc->na.address->transport_name);
2280 if ( (NULL != plugin) &&
2281 (NULL != bcc->na.session) )
2283 plugin->disconnect_session (plugin->cls,
2287 GNUNET_break (NULL != plugin);
2288 free_address (&n->primary_address);
2289 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2290 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2291 GNUNET_ATS_reset_backoff (GST_ats, peer);
2294 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2295 /* waiting on SESSION_ACK, send ACK if one is pending */
2296 if ( (GNUNET_OK == result) &&
2297 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2299 n->ack_state = ACK_SEND_SESSION_ACK;
2300 send_session_connect_ack_message (n->primary_address.address,
2301 n->primary_address.session,
2302 n->connect_ack_timestamp);
2305 case GNUNET_TRANSPORT_PS_CONNECTED:
2306 /* already connected, don't care about blacklist */
2308 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2309 /* still waiting on ATS suggestion, don't care about blacklist */
2311 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2312 if ( (GNUNET_OK == result) &&
2313 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2315 n->ack_state = ACK_SEND_SESSION_ACK;
2316 send_session_connect_ack_message (bcc->na.address,
2318 n->connect_ack_timestamp);
2320 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2322 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2323 "Blacklist result ignored, as it is not for our primary address\n");
2324 break; /* result for an address we currently don't care about */
2326 if (GNUNET_OK == result)
2328 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2329 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2330 send_session_connect (&n->primary_address);
2334 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
2335 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2338 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2339 /* waiting on CONNECT_ACK, don't care about blacklist */
2340 if ( (GNUNET_OK == result) &&
2341 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2343 n->ack_state = ACK_SEND_SESSION_ACK;
2344 send_session_connect_ack_message (n->primary_address.address,
2345 n->primary_address.session,
2346 n->connect_ack_timestamp);
2349 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2350 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2353 "Blacklist result ignored, as it is not for our primary address\n");
2354 break; /* result for an address we currently don't care about */
2356 if (GNUNET_OK == result)
2358 send_session_connect (&n->alternative_address);
2359 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2363 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2364 free_address (&n->alternative_address);
2367 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2368 /* waiting on CONNECT_ACK, don't care about blacklist */
2369 if ( (GNUNET_OK == result) &&
2370 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2372 n->ack_state = ACK_SEND_SESSION_ACK;
2373 send_session_connect_ack_message (n->primary_address.address,
2374 n->primary_address.session,
2375 n->connect_ack_timestamp);
2378 case GNUNET_TRANSPORT_PS_DISCONNECT:
2379 /* Nothing to do here, ATS will already do what can be done */
2381 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2382 /* should not be possible */
2386 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2387 "Unhandled state `%s'\n",
2388 GNUNET_TRANSPORT_ps2s (n->state));
2390 free_neighbour (n, GNUNET_NO);
2394 GNUNET_HELLO_address_free (bcc->na.address);
2400 * We want to know if connecting to a particular peer via
2401 * a particular address is allowed. Check it!
2403 * @param peer identity of the peer to switch the address for
2404 * @param ts time at which the check was initiated
2405 * @param address address of the other peer, NULL if other peer
2407 * @param session session to use (or NULL)
2410 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2411 struct GNUNET_TIME_Absolute ts,
2412 const struct GNUNET_HELLO_Address *address,
2413 struct Session *session)
2415 struct BlackListCheckContext *bcc;
2416 struct GST_BlacklistCheck *bc;
2418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2419 "Checking peer `%s' against blacklist\n",
2421 bcc = GNUNET_new (struct BlackListCheckContext);
2422 bcc->na.address = GNUNET_HELLO_address_copy (address);
2423 bcc->na.session = session;
2424 bcc->na.connect_timestamp = ts;
2425 GNUNET_CONTAINER_DLL_insert (bc_head,
2428 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2429 address->transport_name,
2430 &handle_test_blacklist_cont, bcc)))
2432 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2433 we must only store 'bc' if 'bc' is non-NULL... */
2438 * We received a 'SESSION_CONNECT' message from the other peer.
2439 * Consider switching to it.
2441 * @param message possibly a 'struct SessionConnectMessage' (check format)
2442 * @param peer identity of the peer to switch the address for
2443 * @param address address of the other peer, NULL if other peer
2445 * @param session session to use (or NULL)
2446 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2449 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2450 const struct GNUNET_PeerIdentity *peer,
2451 const struct GNUNET_HELLO_Address *address,
2452 struct Session *session)
2454 const struct SessionConnectMessage *scm;
2455 struct NeighbourMapEntry *n;
2456 struct GNUNET_TIME_Absolute ts;
2458 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2459 "Received CONNECT message from peer `%s' with `%s' %p\n",
2460 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
2461 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2463 GNUNET_break_op (0);
2464 return GNUNET_SYSERR;
2466 GNUNET_STATISTICS_update (GST_stats,
2468 ("# CONNECT messages received"),
2470 if (NULL == neighbours)
2472 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2473 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2475 return GNUNET_OK; /* we're shutting down */
2477 scm = (const struct SessionConnectMessage *) message;
2478 GNUNET_break_op (0 == ntohl (scm->reserved));
2479 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2480 n = lookup_neighbour (peer);
2482 n = setup_neighbour (peer);
2484 /* Remember this CONNECT message in neighbour */
2485 n->ack_state = ACK_SEND_CONNECT_ACK;
2486 n->connect_ack_timestamp = ts;
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489 "Received SESSION_CONNECT for peer `%s' in state %s/%s\n",
2491 GNUNET_TRANSPORT_ps2s (n->state),
2492 print_ack_state (n->ack_state));
2495 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2496 /* Do a blacklist check for the new address */
2497 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2498 check_blacklist (peer, ts, address, session);
2500 case GNUNET_TRANSPORT_PS_INIT_ATS:
2501 /* CONNECT message takes priority over us asking ATS for address */
2502 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2504 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2505 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2506 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2507 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2508 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2509 /* It can never hurt to have an alternative address in the above cases,
2510 see if it is allowed */
2511 check_blacklist (peer, ts, address, session);
2513 case GNUNET_TRANSPORT_PS_CONNECTED:
2514 /* we are already connected and can thus send the ACK immediately;
2515 still, it can never hurt to have an alternative address, so also
2516 tell ATS about it */
2517 GNUNET_assert (NULL != n->primary_address.address);
2518 GNUNET_assert (NULL != n->primary_address.session);
2519 n->ack_state = ACK_UNDEFINED;
2520 send_session_connect_ack_message (n->primary_address.address,
2521 n->primary_address.session, ts);
2522 check_blacklist (peer, ts, address, session);
2524 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2525 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2526 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2527 /* It can never hurt to have an alternative address in the above cases,
2528 see if it is allowed */
2529 check_blacklist (peer, ts, address, session);
2531 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2532 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2533 /* we are already connected and can thus send the ACK immediately;
2534 still, it can never hurt to have an alternative address, so also
2535 tell ATS about it */
2536 GNUNET_assert (NULL != n->primary_address.address);
2537 GNUNET_assert (NULL != n->primary_address.session);
2538 n->ack_state = ACK_UNDEFINED;
2539 send_session_connect_ack_message (n->primary_address.address,
2540 n->primary_address.session, ts);
2541 check_blacklist (peer, ts, address, session);
2543 case GNUNET_TRANSPORT_PS_DISCONNECT:
2544 /* get rid of remains without terminating sessions, ready to re-try */
2545 free_neighbour (n, GNUNET_YES);
2546 n = setup_neighbour (peer);
2547 /* Remember the CONNECT timestamp for ACK message */
2548 n->ack_state = ACK_SEND_CONNECT_ACK;
2549 n->connect_ack_timestamp = ts;
2550 /* Request an address for the peer */
2551 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2552 GNUNET_ATS_reset_backoff (GST_ats, peer);
2553 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2555 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2556 /* should not be possible */
2560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2561 "Unhandled state `%s'\n",
2562 GNUNET_TRANSPORT_ps2s (n->state));
2564 return GNUNET_SYSERR;
2570 switch_address_bl_check_cont (void *cls,
2571 const struct GNUNET_PeerIdentity *peer, int result)
2573 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2574 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2575 struct NeighbourMapEntry *n;
2577 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2578 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583 "Peer %s is unknown, suggestion ignored\n",
2586 if (result == GNUNET_NO)
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2589 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2590 GST_plugins_a2s (blc_ctx->address),
2592 GNUNET_i2s (&blc_ctx->address->peer));
2594 if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2596 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2597 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2598 blc_ctx->address->transport_name,
2599 GST_plugins_a2s (blc_ctx->address),
2601 GNUNET_i2s (&blc_ctx->address->peer));
2604 /* This address is blacklisted, delete address and session (if existing) in ATS */
2605 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2607 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2608 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2610 /* This is an inbound address, destroy full address */
2611 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2614 /* Remove blacklist check and clean up */
2615 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2616 GNUNET_HELLO_address_free (blc_ctx->address);
2617 GNUNET_free_non_null (blc_ctx->ats);
2618 GNUNET_free (blc_ctx);
2622 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2623 "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2624 GST_plugins_a2s (blc_ctx->address),
2626 GNUNET_i2s (&blc_ctx->address->peer));
2628 if (NULL == blc_ctx->session)
2630 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632 "Obtained new session for peer `%s' and address '%s': %p\n",
2633 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2635 if (NULL == blc_ctx->session)
2637 /* No session could be obtained, remove blacklist check and clean up */
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2639 "Failed to obtain new session for peer `%s' and address '%s'\n",
2640 GNUNET_i2s (&blc_ctx->address->peer),
2641 GST_plugins_a2s (blc_ctx->address));
2642 /* Delete address in ATS */
2643 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2645 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2646 GNUNET_HELLO_address_free (blc_ctx->address);
2647 GNUNET_free_non_null (blc_ctx->ats);
2648 GNUNET_free (blc_ctx);
2654 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2656 free_neighbour (n, GNUNET_NO);
2658 case GNUNET_TRANSPORT_PS_INIT_ATS:
2659 /* We requested an address and ATS suggests one:
2660 * set primary address and send CONNECT message*/
2661 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2662 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2663 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2664 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2665 send_session_connect (&n->primary_address);
2667 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2668 /* waiting on CONNECT_ACK, send ACK if one is pending */
2669 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2671 n->ack_state = ACK_SEND_SESSION_ACK;
2672 send_session_connect_ack_message (n->primary_address.address,
2673 n->primary_address.session,
2674 n->connect_ack_timestamp);
2676 /* ATS suggests a different address, switch again */
2677 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2678 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2679 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2680 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2681 send_session_connect (&n->primary_address);
2683 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2684 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2685 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2686 /* Send an ACK message as a response to the CONNECT msg */
2687 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2688 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2689 send_session_connect_ack_message (n->primary_address.address,
2690 n->primary_address.session,
2691 n->connect_ack_timestamp);
2692 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2693 n->ack_state = ACK_SEND_SESSION_ACK;
2696 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2697 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2698 check_blacklist (&n->id, n->connect_ack_timestamp,
2699 blc_ctx->address, blc_ctx->session);
2701 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2702 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2703 /* ATS asks us to switch while we were trying to connect; switch to new
2704 address and check blacklist again */
2705 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2706 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2707 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2708 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2709 check_blacklist (&n->id, n->connect_ack_timestamp,
2710 blc_ctx->address, blc_ctx->session);
2712 case GNUNET_TRANSPORT_PS_CONNECTED:
2713 GNUNET_assert (NULL != n->primary_address.address);
2714 GNUNET_assert (NULL != n->primary_address.session);
2715 if (n->primary_address.session == blc_ctx->session)
2717 /* not an address change, just a quota change */
2718 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2719 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2722 /* ATS asks us to switch a life connection; see if we can get
2723 a CONNECT_ACK on it before we actually do this! */
2724 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2725 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2726 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2727 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2728 blc_ctx->address, blc_ctx->session);
2730 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2731 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2732 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2733 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2734 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2735 check_blacklist (&n->id, n->connect_ack_timestamp,
2736 blc_ctx->address, blc_ctx->session);
2738 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2739 /* ATS asks us to switch while we were trying to reconnect; switch to new
2740 address and check blacklist again */
2741 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2742 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2743 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2744 check_blacklist (&n->id, n->connect_ack_timestamp,
2745 blc_ctx->address, blc_ctx->session);
2747 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2748 /* ATS asks us to switch while we were trying to reconnect; switch to new
2749 address and check blacklist again */
2750 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2751 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2752 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2753 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2754 check_blacklist (&n->id, n->connect_ack_timestamp,
2755 blc_ctx->address, blc_ctx->session);
2757 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2758 if (n->primary_address.session == blc_ctx->session)
2760 /* ATS switches back to still-active session */
2761 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2762 free_address (&n->alternative_address);
2765 /* ATS asks us to switch a life connection, update blacklist check */
2766 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2767 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2768 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2769 blc_ctx->address, blc_ctx->session);
2771 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2772 if (n->primary_address.session == blc_ctx->session)
2774 /* ATS switches back to still-active session */
2775 free_address (&n->alternative_address);
2776 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2779 /* ATS asks us to switch a life connection, update blacklist check */
2780 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2781 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2782 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2783 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2784 blc_ctx->address, blc_ctx->session);
2786 case GNUNET_TRANSPORT_PS_DISCONNECT:
2787 /* not going to switch addresses while disconnecting */
2789 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2793 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2794 "Unhandled state `%s'\n",
2795 GNUNET_TRANSPORT_ps2s (n->state));
2800 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2801 GNUNET_HELLO_address_free(blc_ctx->address);
2802 GNUNET_free_non_null (blc_ctx->ats);
2803 GNUNET_free (blc_ctx);
2809 * For the given peer, switch to this address.
2811 * Before accepting this addresses and actively using it, a blacklist check
2812 * is performed. If this blacklist check fails the address will be destroyed.
2814 * @param peer identity of the peer to switch the address for
2815 * @param address address of the other peer,
2816 * @param session session to use or NULL if transport should initiate a session
2817 * @param ats performance data
2818 * @param ats_count number of entries in ats
2819 * @param bandwidth_in inbound quota to be used when connection is up,
2820 * 0 to disconnect from peer
2821 * @param bandwidth_out outbound quota to be used when connection is up,
2822 * 0 to disconnect from peer
2825 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2826 const struct GNUNET_HELLO_Address *address,
2827 struct Session *session,
2828 const struct GNUNET_ATS_Information *ats,
2830 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2831 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2833 struct NeighbourMapEntry *n;
2834 struct GST_BlacklistCheck *blc;
2835 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2836 struct BlacklistCheckSwitchContext *blc_ctx;
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2840 "ATS has decided on an address for peer %s\n",
2842 GNUNET_assert (NULL != address->transport_name);
2843 if (NULL == (n = lookup_neighbour (peer)))
2845 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2846 "Peer %s is unknown, suggestion ignored\n",
2851 /* Check if plugin is available */
2852 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2854 /* we don't have the plugin for this address */
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2856 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2857 address->transport_name,
2859 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2862 if ((NULL == session) &&
2863 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2865 /* This is a inbound address and we do not have a session to use! */
2866 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2867 "Inbound address without session `%s'! Destroying address...\n",
2868 GST_plugins_a2s (address));
2869 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2873 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2874 "ATS tells us to switch to %s address '%s' session %p for "
2875 "peer `%s' in state %s/%s (quota in/out %u %u )\n",
2876 GNUNET_HELLO_address_check_option (address,
2877 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2878 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2879 GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state),
2880 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2882 /* Perform blacklist check */
2883 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2884 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2885 blc_ctx->session = session;
2886 blc_ctx->bandwidth_in = bandwidth_in;
2887 blc_ctx->bandwidth_out = bandwidth_out;
2888 blc_ctx->ats_count = ats_count;
2889 blc_ctx->ats = NULL;
2892 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2893 for (c = 0; c < ats_count; c++)
2895 blc_ctx->ats[c].type = ats[c].type;
2896 blc_ctx->ats[c].value = ats[c].value;
2900 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2901 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2902 &switch_address_bl_check_cont, blc_ctx)))
2910 send_utilization_data (void *cls,
2911 const struct GNUNET_PeerIdentity *key,
2914 struct NeighbourMapEntry *n = value;
2915 struct GNUNET_ATS_Information atsi[4];
2917 uint32_t bps_pl_out;
2920 struct GNUNET_TIME_Relative delta;
2922 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2923 GNUNET_TIME_absolute_get ());
2927 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2928 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2930 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2931 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2933 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2938 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2939 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2941 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2942 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2946 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2950 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2951 atsi[0].value = htonl (bps_out);
2952 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2953 atsi[1].value = htonl (bps_in);
2955 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2956 atsi[2].value = htonl (bps_pl_out);
2957 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2958 atsi[3].value = htonl (bps_pl_in);
2960 GST_ats_update_metrics (key, n->primary_address.address,
2961 n->primary_address.session, atsi, 4);
2962 n->util_payload_bytes_recv = 0;
2963 n->util_payload_bytes_sent = 0;
2964 n->util_total_bytes_recv = 0;
2965 n->util_total_bytes_sent = 0;
2966 n->last_util_transmission = GNUNET_TIME_absolute_get();
2972 * Task transmitting utilization in a regular interval
2974 * @param cls the 'struct NeighbourMapEntry' for which we are running
2975 * @param tc scheduler context (unused)
2978 utilization_transmission (void *cls,
2979 const struct GNUNET_SCHEDULER_TaskContext *tc)
2981 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2983 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2984 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2986 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2987 utilization_transmission, NULL);
2993 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2994 const struct GNUNET_HELLO_Address *address,
2995 struct Session *session,
2996 const struct GNUNET_MessageHeader *message)
2998 struct NeighbourMapEntry *n;
3000 n = lookup_neighbour (peer);
3003 n->util_total_bytes_recv += ntohs(message->size);
3008 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
3009 const struct GNUNET_HELLO_Address *address,
3010 struct Session *session,
3011 const struct GNUNET_MessageHeader *message)
3013 struct NeighbourMapEntry *n;
3014 n = lookup_neighbour (peer);
3017 n->util_payload_bytes_recv += ntohs(message->size);
3022 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
3023 const struct GNUNET_HELLO_Address *address,
3024 struct Session *session,
3027 struct NeighbourMapEntry *n;
3028 n = lookup_neighbour (peer);
3031 if (n->primary_address.session != session)
3033 n->util_total_bytes_sent += size;
3038 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
3041 struct NeighbourMapEntry *n;
3042 n = lookup_neighbour (peer);
3045 n->util_payload_bytes_sent += size;
3050 * Master task run for every neighbour. Performs all of the time-related
3051 * activities (keep alive, send next message, disconnect if idle, finish
3052 * clean up after disconnect).
3054 * @param cls the 'struct NeighbourMapEntry' for which we are running
3055 * @param tc scheduler context (unused)
3058 master_task (void *cls,
3059 const struct GNUNET_SCHEDULER_TaskContext *tc)
3061 struct NeighbourMapEntry *n = cls;
3062 struct GNUNET_TIME_Relative delay;
3064 n->task = GNUNET_SCHEDULER_NO_TASK;
3065 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3067 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3068 GNUNET_i2s (&n->id),
3069 GNUNET_TRANSPORT_ps2s(n->state),
3070 GNUNET_STRINGS_relative_time_to_string (delay,
3074 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3075 /* invalid state for master task, clean up */
3077 free_neighbour (n, GNUNET_NO);
3079 case GNUNET_TRANSPORT_PS_INIT_ATS:
3080 if (0 == delay.rel_value_us)
3082 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3083 "Connection to `%s' timed out waiting for ATS to provide address\n",
3084 GNUNET_i2s (&n->id));
3085 free_neighbour (n, GNUNET_NO);
3089 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3090 if (0 == delay.rel_value_us)
3092 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3093 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
3094 GNUNET_i2s (&n->id));
3095 /* We could not send to this address, delete address and session */
3096 if (NULL != n->primary_address.session)
3097 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
3098 n->primary_address.session);
3099 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3101 /* Remove address and request and additional one */
3103 unset_primary_address (n);
3104 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3105 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3109 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3110 if (0 == delay.rel_value_us)
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3113 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
3114 GNUNET_i2s (&n->id));
3115 free_neighbour (n, GNUNET_NO);
3119 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3120 if (0 == delay.rel_value_us)
3122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3123 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3124 GNUNET_i2s (&n->id));
3125 free_neighbour (n, GNUNET_NO);
3129 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3130 if (0 == delay.rel_value_us)
3132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3133 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
3134 GNUNET_i2s (&n->id));
3135 free_neighbour (n, GNUNET_NO);
3139 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3140 if (0 == delay.rel_value_us)
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3143 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3144 GNUNET_i2s (&n->id));
3145 disconnect_neighbour (n);
3149 case GNUNET_TRANSPORT_PS_CONNECTED:
3150 if (0 == delay.rel_value_us)
3152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3153 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3154 GNUNET_i2s (&n->id));
3155 disconnect_neighbour (n);
3158 try_transmission_to_peer (n);
3161 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3162 if (0 == delay.rel_value_us)
3164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3165 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3166 GNUNET_i2s (&n->id));
3167 disconnect_neighbour (n);
3171 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3172 if (0 == delay.rel_value_us)
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3175 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
3176 GNUNET_i2s (&n->id));
3177 disconnect_neighbour (n);
3181 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3182 if (0 == delay.rel_value_us)
3184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3185 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3186 GNUNET_i2s (&n->id));
3187 disconnect_neighbour (n);
3191 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3192 if (0 == delay.rel_value_us)
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3196 GNUNET_i2s (&n->id));
3197 disconnect_neighbour (n);
3200 try_transmission_to_peer (n);
3203 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3204 if (0 == delay.rel_value_us)
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3207 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3208 GNUNET_i2s (&n->id));
3209 disconnect_neighbour (n);
3212 try_transmission_to_peer (n);
3215 case GNUNET_TRANSPORT_PS_DISCONNECT:
3216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3217 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3218 GNUNET_i2s (&n->id));
3219 free_neighbour (n, GNUNET_NO);
3221 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3222 /* how did we get here!? */
3226 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3227 "Unhandled state `%s'\n",
3228 GNUNET_TRANSPORT_ps2s (n->state));
3232 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3233 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3234 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3236 /* if we are *now* in one of these three states, we're sending
3237 keep alive messages, so we need to consider the keepalive
3238 delay, not just the connection timeout */
3239 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3242 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3243 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3250 * Send a SESSION_ACK message to the neighbour to confirm that we
3251 * got his CONNECT_ACK.
3253 * @param n neighbour to send the SESSION_ACK to
3256 send_session_ack_message (struct NeighbourMapEntry *n)
3258 struct GNUNET_MessageHeader msg;
3260 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3261 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3262 (void) send_with_session(n,
3263 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3264 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3270 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3271 * Consider switching to it.
3273 * @param message possibly a 'struct SessionConnectMessage' (check format)
3274 * @param peer identity of the peer to switch the address for
3275 * @param address address of the other peer, NULL if other peer
3277 * @param session session to use (or NULL)
3278 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3281 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3282 const struct GNUNET_PeerIdentity *peer,
3283 const struct GNUNET_HELLO_Address *address,
3284 struct Session *session)
3286 const struct SessionConnectMessage *scm;
3287 struct GNUNET_TIME_Absolute ts;
3288 struct NeighbourMapEntry *n;
3290 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3291 "Received CONNECT_ACK message from peer `%s'\n",
3294 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3296 GNUNET_break_op (0);
3297 return GNUNET_SYSERR;
3299 GNUNET_STATISTICS_update (GST_stats,
3301 ("# CONNECT_ACK messages received"),
3303 scm = (const struct SessionConnectMessage *) message;
3304 GNUNET_break_op (ntohl (scm->reserved) == 0);
3305 if (NULL == (n = lookup_neighbour (peer)))
3307 GNUNET_STATISTICS_update (GST_stats,
3309 ("# unexpected CONNECT_ACK messages (no peer)"),
3311 return GNUNET_SYSERR;
3313 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3316 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3318 free_neighbour (n, GNUNET_NO);
3319 return GNUNET_SYSERR;
3320 case GNUNET_TRANSPORT_PS_INIT_ATS:
3321 GNUNET_STATISTICS_update (GST_stats,
3323 ("# unexpected CONNECT_ACK messages (not ready)"),
3326 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3327 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3329 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3330 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3333 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3334 GNUNET_STATISTICS_set (GST_stats,
3335 gettext_noop ("# peers connected"),
3336 ++neighbours_connected,
3338 connect_notify_cb (callback_cls, &n->id,
3339 n->primary_address.bandwidth_in,
3340 n->primary_address.bandwidth_out);
3341 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3342 GST_ats_add_address (n->primary_address.address,
3343 n->primary_address.session,
3345 set_primary_address (n,
3346 n->primary_address.address,
3347 n->primary_address.session,
3348 n->primary_address.bandwidth_in,
3349 n->primary_address.bandwidth_out,
3351 send_session_ack_message (n);
3353 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3354 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3355 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3356 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3357 GNUNET_STATISTICS_update (GST_stats,
3359 ("# unexpected CONNECT_ACK messages (not ready)"),
3362 case GNUNET_TRANSPORT_PS_CONNECTED:
3363 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3364 send_session_ack_message (n);
3366 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3367 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3368 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3369 to give us a new address... */
3370 GNUNET_STATISTICS_update (GST_stats,
3372 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3375 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3376 /* new address worked; go back to connected! */
3377 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3378 send_session_ack_message (n);
3380 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3381 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3382 send_session_ack_message (n);
3384 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3385 /* new address worked; adopt it and go back to connected! */
3386 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3387 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3389 GST_ats_add_address (n->alternative_address.address,
3390 n->alternative_address.session,
3392 set_primary_address (n, n->alternative_address.address,
3393 n->alternative_address.session, n->alternative_address.bandwidth_in,
3394 n->alternative_address.bandwidth_out, GNUNET_YES);
3396 free_address (&n->alternative_address);
3397 send_session_ack_message (n);
3399 case GNUNET_TRANSPORT_PS_DISCONNECT:
3400 GNUNET_STATISTICS_update (GST_stats,
3402 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3404 return GNUNET_SYSERR;
3405 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3410 "Unhandled state `%s'\n",
3411 GNUNET_TRANSPORT_ps2s (n->state));
3413 return GNUNET_SYSERR;
3420 * A session was terminated. Take note; if needed, try to get
3421 * an alternative address from ATS.
3423 * @param peer identity of the peer where the session died
3424 * @param session session that is gone
3425 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3426 * this session was not in use
3429 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3430 struct Session *session)
3432 struct NeighbourMapEntry *n;
3433 struct BlackListCheckContext *bcc;
3434 struct BlackListCheckContext *bcc_next;
3436 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3438 while (NULL != (bcc = bcc_next))
3440 bcc_next = bcc->next;
3441 if (bcc->na.session == session)
3443 if (NULL != bcc->bc)
3444 GST_blacklist_test_cancel (bcc->bc);
3445 GNUNET_HELLO_address_free (bcc->na.address);
3446 GNUNET_CONTAINER_DLL_remove (bc_head,
3452 if (NULL == (n = lookup_neighbour (peer)))
3453 return GNUNET_NO; /* can't affect us */
3454 if (session != n->primary_address.session)
3456 if (session == n->alternative_address.session)
3458 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3459 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3460 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3463 free_address (&n->alternative_address);
3465 return GNUNET_NO; /* doesn't affect us further */
3468 n->expect_latency_response = GNUNET_NO;
3469 /* The session for neighbour's primary address died */
3472 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3474 free_neighbour (n, GNUNET_NO);
3476 case GNUNET_TRANSPORT_PS_INIT_ATS:
3478 free_neighbour (n, GNUNET_NO);
3480 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3481 /* The session used to send the CONNECT terminated:
3482 * this implies a connect error*/
3483 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3484 "Could not send CONNECT message with address `%s' session %p: session terminated, requesting new address\n",
3485 GST_plugins_a2s (n->primary_address.address), n->primary_address.session,
3487 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3488 unset_primary_address (n);
3489 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3490 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3492 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3493 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3494 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3495 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3496 /* error on inbound session; free neighbour entirely */
3497 free_address (&n->primary_address);
3498 free_neighbour (n, GNUNET_NO);
3500 case GNUNET_TRANSPORT_PS_CONNECTED:
3501 unset_primary_address (n);
3502 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3503 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3505 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3506 /* we don't have an address, how can it go down? */
3509 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3510 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3511 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3513 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3514 /* primary went down while we were checking secondary against
3515 blacklist, adopt secondary as primary */
3516 free_address (&n->primary_address);
3517 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3518 n->primary_address = n->alternative_address;
3519 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3521 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3522 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3523 secondary as primary */
3524 free_address (&n->primary_address);
3525 n->primary_address = n->alternative_address;
3526 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3527 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3529 case GNUNET_TRANSPORT_PS_DISCONNECT:
3530 free_address (&n->primary_address);
3532 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3533 /* neighbour was freed and plugins told to terminate session */
3537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3538 "Unhandled state `%s'\n",
3539 GNUNET_TRANSPORT_ps2s (n->state));
3543 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3544 GNUNET_SCHEDULER_cancel (n->task);
3545 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3551 * We received a 'SESSION_ACK' message from the other peer.
3552 * If we sent a 'CONNECT_ACK' last, this means we are now
3553 * connected. Otherwise, do nothing.
3555 * @param message possibly a 'struct SessionConnectMessage' (check format)
3556 * @param peer identity of the peer to switch the address for
3557 * @param address address of the other peer, NULL if other peer
3559 * @param session session to use (or NULL)
3560 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3563 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3564 const struct GNUNET_PeerIdentity *peer,
3565 const struct GNUNET_HELLO_Address *address,
3566 struct Session *session)
3568 struct NeighbourMapEntry *n;
3570 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3571 "Received SESSION_ACK message from peer `%s'\n",
3573 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3575 GNUNET_break_op (0);
3576 return GNUNET_SYSERR;
3578 GNUNET_STATISTICS_update (GST_stats,
3580 ("# SESSION_ACK messages received"),
3582 if (NULL == (n = lookup_neighbour (peer)))
3584 GNUNET_break_op (0);
3585 return GNUNET_SYSERR;
3587 /* check if we are in a plausible state for having sent
3588 a CONNECT_ACK. If not, return, otherwise break */
3589 if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3590 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3591 (ACK_SEND_SESSION_ACK != n->ack_state) )
3593 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3594 "Received SESSION_ACK message from peer `%s' in state %s/%s\n",
3596 GNUNET_TRANSPORT_ps2s (n->state),
3597 print_ack_state (n->ack_state));
3598 GNUNET_STATISTICS_update (GST_stats,
3599 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3603 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3604 GNUNET_STATISTICS_set (GST_stats,
3605 gettext_noop ("# peers connected"),
3606 ++neighbours_connected,
3608 connect_notify_cb (callback_cls, &n->id,
3609 n->primary_address.bandwidth_in,
3610 n->primary_address.bandwidth_out);
3612 GST_ats_add_address (n->primary_address.address,
3613 n->primary_address.session,
3615 set_primary_address (n,
3616 n->primary_address.address,
3617 n->primary_address.session,
3618 n->primary_address.bandwidth_in,
3619 n->primary_address.bandwidth_out,
3626 * Test if we're connected to the given peer.
3628 * @param target peer to test
3629 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3632 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3634 return test_connected (lookup_neighbour (target));
3638 * Change the incoming quota for the given peer.
3640 * @param neighbour identity of peer to change qutoa for
3641 * @param quota new quota
3644 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3645 struct GNUNET_BANDWIDTH_Value32NBO quota)
3647 struct NeighbourMapEntry *n;
3649 if (NULL == (n = lookup_neighbour (neighbour)))
3651 GNUNET_STATISTICS_update (GST_stats,
3653 ("# SET QUOTA messages ignored (no such peer)"),
3657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3658 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3659 ntohl (quota.value__), GNUNET_i2s (&n->id));
3660 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3661 if (0 != ntohl (quota.value__))
3663 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3664 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3665 GNUNET_i2s (&n->id));
3666 if (GNUNET_YES == test_connected (n))
3667 GNUNET_STATISTICS_update (GST_stats,
3668 gettext_noop ("# disconnects due to quota of 0"),
3670 disconnect_neighbour (n);
3675 * We received a disconnect message from the given peer,
3676 * validate and process.
3678 * @param peer sender of the message
3679 * @param msg the disconnect message
3682 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3683 const struct GNUNET_MessageHeader *msg)
3685 struct NeighbourMapEntry *n;
3686 const struct SessionDisconnectMessage *sdm;
3688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3689 "Received DISCONNECT message from peer `%s'\n",
3691 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3693 // GNUNET_break_op (0);
3694 GNUNET_STATISTICS_update (GST_stats,
3696 ("# disconnect messages ignored (old format)"), 1,
3700 GNUNET_STATISTICS_update (GST_stats,
3702 ("# DISCONNECT messages received"),
3704 sdm = (const struct SessionDisconnectMessage *) msg;
3705 if (NULL == (n = lookup_neighbour (peer)))
3706 return; /* gone already */
3707 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3709 GNUNET_STATISTICS_update (GST_stats,
3711 ("# disconnect messages ignored (timestamp)"), 1,
3715 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3717 GNUNET_break_op (0);
3720 if (ntohl (sdm->purpose.size) !=
3721 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3722 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3723 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3725 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3726 "%s message from peer `%s' has invalid size \n",
3729 GNUNET_break_op (0);
3733 GNUNET_CRYPTO_eddsa_verify
3734 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3735 &sdm->signature, &sdm->public_key))
3737 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3738 "%s message from peer `%s' cannot be verified \n",
3741 GNUNET_break_op (0);
3744 if (GNUNET_YES == test_connected (n))
3745 GNUNET_STATISTICS_update (GST_stats,
3747 ("# other peer asked to disconnect from us"), 1,
3749 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3750 "Disconnecting by request from peer %s\n",
3752 disconnect_neighbour (n);
3757 * Closure for the neighbours_iterate function.
3759 struct IteratorContext
3762 * Function to call on each connected neighbour.
3764 GST_NeighbourIterator cb;
3774 * Call the callback from the closure for each neighbour.
3776 * @param cls the `struct IteratorContext`
3777 * @param key the hash of the public key of the neighbour
3778 * @param value the `struct NeighbourMapEntry`
3779 * @return #GNUNET_OK (continue to iterate)
3782 neighbours_iterate (void *cls,
3783 const struct GNUNET_PeerIdentity *key,
3786 struct IteratorContext *ic = cls;
3787 struct NeighbourMapEntry *n = value;
3788 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3789 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3792 if (NULL != n->primary_address.address)
3794 bandwidth_in = n->primary_address.bandwidth_in;
3795 bandwidth_out = n->primary_address.bandwidth_out;
3799 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3800 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3804 n->primary_address.address,
3807 bandwidth_in, bandwidth_out);
3813 * Iterate over all connected neighbours.
3815 * @param cb function to call
3816 * @param cb_cls closure for cb
3819 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3821 struct IteratorContext ic;
3823 if (NULL == neighbours)
3824 return; /* can happen during shutdown */
3827 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3832 * If we have an active connection to the given target, it must be shutdown.
3834 * @param target peer to disconnect from
3837 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3839 struct NeighbourMapEntry *n;
3841 if (NULL == (n = lookup_neighbour (target)))
3842 return; /* not active */
3843 if (GNUNET_YES == test_connected (n))
3844 GNUNET_STATISTICS_update (GST_stats,
3846 ("# disconnected from peer upon explicit request"), 1,
3848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3849 "Forced disconnect from peer %s\n",
3850 GNUNET_i2s (target));
3851 disconnect_neighbour (n);
3856 * Obtain current latency information for the given neighbour.
3858 * @param peer to get the latency for
3859 * @return observed latency of the address, FOREVER if the
3860 * the connection is not up
3862 struct GNUNET_TIME_Relative
3863 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3865 struct NeighbourMapEntry *n;
3867 n = lookup_neighbour (peer);
3869 return GNUNET_TIME_UNIT_FOREVER_REL;
3872 case GNUNET_TRANSPORT_PS_CONNECTED:
3873 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3874 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3875 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3876 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3877 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3879 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3880 case GNUNET_TRANSPORT_PS_INIT_ATS:
3881 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3882 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3883 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3884 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3885 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3886 case GNUNET_TRANSPORT_PS_DISCONNECT:
3887 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3888 return GNUNET_TIME_UNIT_FOREVER_REL;
3890 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3891 "Unhandled state `%s'\n",
3892 GNUNET_TRANSPORT_ps2s (n->state));
3896 return GNUNET_TIME_UNIT_FOREVER_REL;
3901 * Obtain current address information for the given neighbour.
3904 * @return address currently used
3906 struct GNUNET_HELLO_Address *
3907 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3909 struct NeighbourMapEntry *n;
3911 n = lookup_neighbour (peer);
3914 return n->primary_address.address;
3919 * Initialize the neighbours subsystem.
3921 * @param cls closure for callbacks
3922 * @param connect_cb function to call if we connect to a peer
3923 * @param disconnect_cb function to call if we disconnect from a peer
3924 * @param peer_address_cb function to call if we change an active address
3926 * @param max_fds maximum number of fds to use
3929 GST_neighbours_start (void *cls,
3930 NotifyConnect connect_cb,
3931 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3932 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3933 unsigned int max_fds)
3936 connect_notify_cb = connect_cb;
3937 disconnect_notify_cb = disconnect_cb;
3938 neighbour_change_cb = peer_address_cb;
3939 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3940 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3941 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3942 utilization_transmission, NULL);
3947 * Disconnect from the given neighbour.
3950 * @param key hash of neighbour's public key (not used)
3951 * @param value the 'struct NeighbourMapEntry' of the neighbour
3952 * @return #GNUNET_OK (continue to iterate)
3955 disconnect_all_neighbours (void *cls,
3956 const struct GNUNET_PeerIdentity *key,
3959 struct NeighbourMapEntry *n = value;
3961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3962 "Disconnecting peer `%4s', %s\n",
3963 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3964 free_neighbour (n, GNUNET_NO);
3970 * Cleanup the neighbours subsystem.
3973 GST_neighbours_stop ()
3975 struct BlacklistCheckSwitchContext *cur;
3976 struct BlacklistCheckSwitchContext *next;
3978 if (NULL == neighbours)
3980 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3982 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3983 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3986 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3987 &disconnect_all_neighbours,
3989 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3991 next = pending_bc_head;
3992 for (cur = next; NULL != cur; cur = next )
3995 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3997 if (NULL != cur->blc)
3999 GST_blacklist_test_cancel (cur->blc);
4002 if (NULL != cur->address)
4003 GNUNET_HELLO_address_free (cur->address);
4004 GNUNET_free_non_null (cur->ats);
4008 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
4009 &free_notification_cb, NULL);
4010 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
4011 registered_quota_notifications = NULL;
4014 callback_cls = NULL;
4015 connect_notify_cb = NULL;
4016 disconnect_notify_cb = NULL;
4017 neighbour_change_cb = NULL;
4021 /* end of file gnunet-service-transport_neighbours.c */