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 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_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;
1767 struct NeighbourMapEntry *n;
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "Sending CONNECT_ACK to peer `%s'\n",
1771 GNUNET_i2s (&address->peer));
1772 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1777 if (NULL == session)
1778 session = papi->get_session (papi->cls, address);
1779 if (NULL == session)
1784 GNUNET_STATISTICS_update (GST_stats,
1786 ("# CONNECT_ACK messages sent"),
1788 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1789 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1790 connect_msg.reserved = htonl (0);
1791 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1793 if (GNUNET_SYSERR == papi->send (papi->cls,
1795 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1797 GNUNET_TIME_UNIT_FOREVER_REL,
1800 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1801 _("Failed to transmit CONNECT_ACK message via plugin to %s\n"),
1802 GST_plugins_a2s (address));
1804 n = lookup_neighbour (&address->peer);
1810 /* Hard failure to send the CONNECT_ACK message with this address:
1811 Destroy session (and address) */
1812 if (GNUNET_YES == GNUNET_HELLO_address_check_option(address,
1813 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1815 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1816 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1820 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1823 /* Remove address and request and additional one */
1824 unset_primary_address (n);
1825 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1826 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1832 struct QuotaNotificationRequest
1834 struct GNUNET_PeerIdentity peer;
1835 struct Session *session;
1839 struct QNR_LookContext
1841 struct GNUNET_PeerIdentity peer;
1842 struct Session *session;
1845 struct QuotaNotificationRequest *res;
1849 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1851 struct QNR_LookContext *qnr_ctx = cls;
1852 struct QuotaNotificationRequest *qnr = value;
1854 if ((qnr->session == qnr_ctx->session) &&
1855 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1856 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1858 qnr_ctx->res = value;
1865 GST_neighbours_register_quota_notification(void *cls,
1866 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1867 struct Session *session)
1869 struct QuotaNotificationRequest *qnr;
1870 struct QNR_LookContext qnr_ctx;
1872 if (NULL == registered_quota_notifications)
1874 return; /* init or shutdown */
1877 qnr_ctx.peer = (*peer);
1878 qnr_ctx.plugin = plugin;
1879 qnr_ctx.session = session;
1882 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1883 peer, &find_notification_request, &qnr_ctx);
1884 if (NULL != qnr_ctx.res)
1890 qnr = GNUNET_new (struct QuotaNotificationRequest);
1891 qnr->peer = (*peer);
1892 qnr->plugin = GNUNET_strdup (plugin);
1893 qnr->session = session;
1895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1896 "Adding notification for peer `%s' plugin `%s' session %p \n",
1897 GNUNET_i2s (peer), plugin, session);
1899 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1900 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1905 GST_neighbours_unregister_quota_notification(void *cls,
1906 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1908 struct QNR_LookContext qnr_ctx;
1910 if (NULL == registered_quota_notifications)
1912 return; /* init or shutdown */
1915 qnr_ctx.peer = (*peer);
1916 qnr_ctx.plugin = plugin;
1917 qnr_ctx.session = session;
1920 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1921 &find_notification_request, &qnr_ctx);
1922 if (NULL == qnr_ctx.res)
1928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1929 "Removing notification for peer `%s' plugin `%s' session %p \n",
1930 GNUNET_i2s (peer), plugin, session);
1932 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
1934 GNUNET_free (qnr_ctx.res->plugin);
1935 GNUNET_free (qnr_ctx.res);
1939 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1941 /* struct NeighbourMapEntry *n = cls; */
1942 struct QuotaNotificationRequest *qnr = value;
1943 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1944 struct GNUNET_TIME_Relative delay;
1947 papi = GST_plugins_find(qnr->plugin);
1954 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
1957 delay.rel_value_us / 1000);
1959 if (NULL != papi->update_inbound_delay)
1960 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
1966 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
1969 /* struct NeighbourMapEntry *n = cls; */
1970 struct QuotaNotificationRequest *qnr = value;
1972 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
1974 GNUNET_free(qnr->plugin);
1981 inbound_bw_tracker_update(void *cls)
1983 struct NeighbourMapEntry *n = cls;
1985 /* Quota was updated, tell plugins to update the time to receive next */
1986 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1987 &n->id, ¬ification_cb, n);
1992 * Create a fresh entry in the neighbour map for the given peer
1994 * @param peer peer to create an entry for
1995 * @return new neighbour map entry
1997 static struct NeighbourMapEntry *
1998 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2000 struct NeighbourMapEntry *n;
2002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2003 "Creating new neighbour entry for `%s'\n",
2005 n = GNUNET_new (struct NeighbourMapEntry);
2007 n->ack_state = ACK_UNDEFINED;
2008 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2009 n->last_util_transmission = GNUNET_TIME_absolute_get();
2010 n->util_payload_bytes_recv = 0;
2011 n->util_payload_bytes_sent = 0;
2012 n->util_total_bytes_recv = 0;
2013 n->util_total_bytes_sent = 0;
2014 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
2015 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2016 MAX_BANDWIDTH_CARRY_S);
2017 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2018 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2019 GNUNET_assert (GNUNET_OK ==
2020 GNUNET_CONTAINER_multipeermap_put (neighbours,
2022 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2028 * Check if the two given addresses are the same.
2029 * Actually only checks if the sessions are non-NULL
2030 * (which they should be) and then if they are identical;
2031 * the actual addresses don't matter if the session
2032 * pointers match anyway, and we must have session pointers
2035 * @param a1 first address to compare
2036 * @param a2 other address to compare
2037 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
2040 address_matches (const struct NeighbourAddress *a1,
2041 const struct NeighbourAddress *a2)
2043 if ( (NULL == a1->session) ||
2044 (NULL == a2->session) )
2049 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
2053 /* We received a address suggestion after requesting an address in
2054 * try_connect or after receiving a connect, switch to address
2057 address_suggest_cont (void *cls,
2058 const struct GNUNET_PeerIdentity *peer,
2059 const struct GNUNET_HELLO_Address *address, struct Session *session,
2060 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2061 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2062 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2064 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2065 bandwidth_in, bandwidth_out);
2069 struct BlacklistCheckSwitchContext
2071 struct BlacklistCheckSwitchContext *prev;
2072 struct BlacklistCheckSwitchContext *next;
2075 struct GST_BlacklistCheck *blc;
2077 struct GNUNET_HELLO_Address *address;
2078 struct Session *session;
2079 struct GNUNET_ATS_Information *ats;
2082 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2083 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2087 * Black list check result for try_connect call
2088 * If connection to the peer is allowed request adddress and
2090 * @param cls blc_ctx bl context
2091 * @param peer the peer
2092 * @param result the result
2095 try_connect_bl_check_cont (void *cls,
2096 const struct GNUNET_PeerIdentity *peer, int result)
2098 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2099 struct NeighbourMapEntry *n;
2101 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2102 GNUNET_free (blc_ctx);
2104 if (GNUNET_OK != result)
2106 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2107 _("Blacklisting disapproved to connect to peer `%s'\n"),
2112 /* Setup a new neighbour */
2113 n = setup_neighbour (peer);
2115 /* Request address suggestions for this peer */
2116 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2117 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2118 GNUNET_ATS_reset_backoff (GST_ats, peer);
2119 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2120 &address_suggest_cont, n);
2126 * Try to create a connection to the given target (eventually).
2128 * @param target peer to try to connect to
2131 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2133 struct NeighbourMapEntry *n;
2134 struct GST_BlacklistCheck *blc;
2135 struct BlacklistCheckSwitchContext *blc_ctx;
2137 if (NULL == neighbours)
2139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2140 "Asked to connect to peer `%s' during shutdown\n",
2141 GNUNET_i2s (target));
2142 return; /* during shutdown, do nothing */
2144 n = lookup_neighbour (target);
2145 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2146 "Asked to connect to peer `%s' (state: %s)\n",
2147 GNUNET_i2s (target),
2148 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2153 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2154 /* this should not be possible */
2156 free_neighbour (n, GNUNET_NO);
2158 case GNUNET_TRANSPORT_PS_INIT_ATS:
2159 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2160 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2161 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2162 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2163 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2164 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2165 "Ignoring request to try to connect to `%s', already trying!\n",
2166 GNUNET_i2s (target));
2167 return; /* already trying */
2168 case GNUNET_TRANSPORT_PS_CONNECTED:
2169 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2170 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2171 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2172 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2173 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2174 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2175 "Ignoring request to try to connect, already connected to `%s'!\n",
2176 GNUNET_i2s (target));
2177 return; /* already connected */
2178 case GNUNET_TRANSPORT_PS_DISCONNECT:
2179 /* get rid of remains, ready to re-try immediately */
2180 free_neighbour (n, GNUNET_NO);
2182 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2183 /* should not be possible */
2186 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2187 "Unhandled state `%s'\n",
2188 GNUNET_TRANSPORT_ps2s (n->state));
2190 free_neighbour (n, GNUNET_NO);
2195 /* Do blacklist check if connecting to this peer is allowed */
2196 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2197 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2199 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2200 &try_connect_bl_check_cont, blc_ctx)))
2208 * Function called with the result of a blacklist check.
2210 * @param cls closure with the `struct BlackListCheckContext`
2211 * @param peer peer this check affects
2212 * @param result #GNUNET_OK if the address is allowed
2215 handle_connect_blacklist_check_cont (void *cls,
2216 const struct GNUNET_PeerIdentity *peer,
2219 struct BlackListCheckContext *bcc = cls;
2220 struct NeighbourMapEntry *n;
2223 GNUNET_CONTAINER_DLL_remove (bc_head,
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2227 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2229 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2230 if (GNUNET_OK == result)
2232 /* Blacklist agreed on connecting to a peer with this address */
2233 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2234 "Notifying ATS peer's `%s' %s address `%s' session %p\n",
2236 (GNUNET_YES == GNUNET_HELLO_address_check_option(bcc->na.address,
2237 GNUNET_HELLO_ADDRESS_INFO_INBOUND)) ? "inbound" : "outbound",
2238 GST_plugins_a2s (bcc->na.address), bcc->na.session);
2239 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2242 if (NULL == (n = lookup_neighbour (peer)))
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2245 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2247 goto cleanup; /* nobody left to care about new address */
2249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2250 "Received connect blacklist check result for peer `%s' in state %s/%s\n",
2252 GNUNET_TRANSPORT_ps2s (n->state),
2253 print_ack_state (n->ack_state));
2256 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2257 /* this should not be possible */
2259 free_neighbour (n, GNUNET_NO);
2261 case GNUNET_TRANSPORT_PS_INIT_ATS:
2262 /* waiting on ATS suggestion; still, pass address to ATS as a
2265 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2267 /* TODO Why should I send an connect ACK message */
2268 /* waiting on CONNECT_ACK, send ACK if one is pending */
2270 if ( (GNUNET_OK == result) &&
2271 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2273 n->ack_state = ACK_SEND_SESSION_ACK;
2274 send_connect_ack_message (n->primary_address.address,
2275 n->primary_address.session,
2276 n->connect_ack_timestamp);
2280 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2281 if (GNUNET_OK == result)
2283 /* We received a connect request and blacklist allowed to communicate
2284 * with this peer, request an address from ATS*/
2285 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2286 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2287 GNUNET_ATS_reset_backoff (GST_ats, peer);
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2289 "Requesting address for peer %s to ATS\n",
2291 if (NULL == n->suggest_handle)
2292 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2293 &address_suggest_cont, n);
2295 GNUNET_ATS_reset_backoff (GST_ats, peer);
2299 /* FIXME: state handling required! */
2302 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2303 /* waiting on ATS suggestion, don't care about blacklist */
2305 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2306 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2309 "Blacklist result ignored, as it is not for our primary address\n");
2310 break; /* result for an address we currently don't care about */
2312 if (GNUNET_OK == result)
2314 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2315 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2316 send_connect_ack_message (bcc->na.address,
2318 n->connect_ack_timestamp);
2319 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2320 n->ack_state = ACK_SEND_SESSION_ACK;
2324 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2326 plugin = GST_plugins_find (bcc->na.address->transport_name);
2327 if ( (NULL != plugin) &&
2328 (NULL != bcc->na.session) )
2330 plugin->disconnect_session (plugin->cls,
2334 GNUNET_break (NULL != plugin);
2335 free_address (&n->primary_address);
2336 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2337 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2338 GNUNET_ATS_reset_backoff (GST_ats, peer);
2341 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2342 /* waiting on SESSION_ACK, send ACK if one is pending */
2343 if ( (GNUNET_OK == result) &&
2344 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2346 n->ack_state = ACK_SEND_SESSION_ACK;
2347 send_connect_ack_message (n->primary_address.address,
2348 n->primary_address.session,
2349 n->connect_ack_timestamp);
2352 case GNUNET_TRANSPORT_PS_CONNECTED:
2353 /* already connected, don't care about blacklist */
2355 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2356 /* still waiting on ATS suggestion, don't care about blacklist */
2358 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2359 if ( (GNUNET_OK == result) &&
2360 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2362 n->ack_state = ACK_SEND_SESSION_ACK;
2363 send_connect_ack_message (bcc->na.address,
2365 n->connect_ack_timestamp);
2367 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2370 "Blacklist result ignored, as it is not for our primary address\n");
2371 break; /* result for an address we currently don't care about */
2373 if (GNUNET_OK == result)
2375 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2376 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2377 send_session_connect (&n->primary_address);
2381 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
2382 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2385 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2386 /* waiting on CONNECT_ACK, don't care about blacklist */
2387 if ( (GNUNET_OK == result) &&
2388 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2390 n->ack_state = ACK_SEND_SESSION_ACK;
2391 send_connect_ack_message (n->primary_address.address,
2392 n->primary_address.session,
2393 n->connect_ack_timestamp);
2396 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2397 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400 "Blacklist result ignored, as it is not for our primary address\n");
2401 break; /* result for an address we currently don't care about */
2403 if (GNUNET_OK == result)
2405 send_session_connect (&n->alternative_address);
2406 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2410 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2411 free_address (&n->alternative_address);
2414 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2415 /* waiting on CONNECT_ACK, don't care about blacklist */
2416 if ( (GNUNET_OK == result) &&
2417 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2419 n->ack_state = ACK_SEND_SESSION_ACK;
2420 send_connect_ack_message (n->primary_address.address,
2421 n->primary_address.session,
2422 n->connect_ack_timestamp);
2425 case GNUNET_TRANSPORT_PS_DISCONNECT:
2426 /* Nothing to do here, ATS will already do what can be done */
2428 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2429 /* should not be possible */
2433 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2434 "Unhandled state `%s'\n",
2435 GNUNET_TRANSPORT_ps2s (n->state));
2437 free_neighbour (n, GNUNET_NO);
2441 GNUNET_HELLO_address_free (bcc->na.address);
2447 * We received a CONNECT message and want to know if connecting to a particular
2448 * peer via a particular address is allowed. Check it!
2450 * @param peer identity of the peer to switch the address for
2451 * @param ts time at which the check was initiated
2452 * @param address address of the other peer, NULL if other peer
2454 * @param session session to use (or NULL)
2457 connect_check_blacklist (const struct GNUNET_PeerIdentity *peer,
2458 struct GNUNET_TIME_Absolute ts,
2459 const struct GNUNET_HELLO_Address *address,
2460 struct Session *session)
2462 struct BlackListCheckContext *bcc;
2463 struct GST_BlacklistCheck *bc;
2465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2466 "Checking peer `%s' against blacklist\n",
2468 bcc = GNUNET_new (struct BlackListCheckContext);
2469 bcc->na.address = GNUNET_HELLO_address_copy (address);
2470 bcc->na.session = session;
2471 bcc->na.connect_timestamp = ts;
2472 GNUNET_CONTAINER_DLL_insert (bc_head,
2475 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2476 (NULL != address) ? address->transport_name : NULL,
2477 &handle_connect_blacklist_check_cont, bcc)))
2479 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2480 we must only store 'bc' if 'bc' is non-NULL... */
2485 * We received a 'SESSION_CONNECT' message from the other peer.
2486 * Consider switching to it.
2488 * @param message possibly a 'struct SessionConnectMessage' (check format)
2489 * @param peer identity of the peer to switch the address for
2490 * @param address address of the other peer, NULL if other peer
2492 * @param session session to use (or NULL)
2493 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2496 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2497 const struct GNUNET_PeerIdentity *peer,
2498 const struct GNUNET_HELLO_Address *address,
2499 struct Session *session)
2501 const struct SessionConnectMessage *scm;
2502 struct NeighbourMapEntry *n;
2503 struct GNUNET_TIME_Absolute ts;
2505 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2506 "Received CONNECT message from peer `%s' with `%s' %p\n",
2507 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
2508 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2510 GNUNET_break_op (0);
2511 return GNUNET_SYSERR;
2513 GNUNET_STATISTICS_update (GST_stats,
2515 ("# CONNECT messages received"),
2517 if (NULL == neighbours)
2519 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2520 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2522 return GNUNET_OK; /* we're shutting down */
2524 scm = (const struct SessionConnectMessage *) message;
2525 GNUNET_break_op (0 == ntohl (scm->reserved));
2526 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2527 n = lookup_neighbour (peer);
2530 /* This is a new neighbour and set to not connected */
2531 n = setup_neighbour (peer);
2534 /* Remember this CONNECT message in neighbour */
2535 n->ack_state = ACK_SEND_CONNECT_ACK;
2536 n->connect_ack_timestamp = ts;
2538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2539 "Received SESSION_CONNECT for peer `%s' in state %s/%s\n",
2541 GNUNET_TRANSPORT_ps2s (n->state),
2542 print_ack_state (n->ack_state));
2545 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2546 /* Check if we are allowed to connect with this peer and this address */
2547 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND,
2548 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2549 connect_check_blacklist (peer, ts, address, session);
2551 case GNUNET_TRANSPORT_PS_INIT_ATS:
2552 /* CONNECT message takes priority over us asking ATS for address */
2553 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND,
2554 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2556 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2557 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2558 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2559 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2560 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2561 /* It can never hurt to have an alternative address in the above cases,
2562 see if it is allowed */
2563 connect_check_blacklist (peer, ts, address, session);
2565 case GNUNET_TRANSPORT_PS_CONNECTED:
2566 /* we are already connected and can thus send the ACK immediately;
2567 still, it can never hurt to have an alternative address, so also
2568 tell ATS about it */
2569 GNUNET_assert (NULL != n->primary_address.address);
2570 GNUNET_assert (NULL != n->primary_address.session);
2571 n->ack_state = ACK_UNDEFINED;
2572 send_connect_ack_message (n->primary_address.address,
2573 n->primary_address.session, ts);
2574 connect_check_blacklist (peer, ts, address, session);
2576 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2577 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2578 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2579 /* It can never hurt to have an alternative address in the above cases,
2580 see if it is allowed */
2581 connect_check_blacklist (peer, ts, address, session);
2583 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2584 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2585 /* we are already connected and can thus send the ACK immediately;
2586 still, it can never hurt to have an alternative address, so also
2587 tell ATS about it */
2588 GNUNET_assert (NULL != n->primary_address.address);
2589 GNUNET_assert (NULL != n->primary_address.session);
2590 n->ack_state = ACK_UNDEFINED;
2591 send_connect_ack_message (n->primary_address.address,
2592 n->primary_address.session, ts);
2593 connect_check_blacklist (peer, ts, address, session);
2595 case GNUNET_TRANSPORT_PS_DISCONNECT:
2596 /* get rid of remains without terminating sessions, ready to re-try */
2597 free_neighbour (n, GNUNET_YES);
2598 n = setup_neighbour (peer);
2599 /* Remember the CONNECT timestamp for ACK message */
2600 n->ack_state = ACK_SEND_CONNECT_ACK;
2601 n->connect_ack_timestamp = ts;
2602 /* Request an address for the peer */
2603 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2604 GNUNET_ATS_reset_backoff (GST_ats, peer);
2605 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2607 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2608 /* should not be possible */
2612 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2613 "Unhandled state `%s'\n",
2614 GNUNET_TRANSPORT_ps2s (n->state));
2616 return GNUNET_SYSERR;
2622 switch_address_bl_check_cont (void *cls,
2623 const struct GNUNET_PeerIdentity *peer, int result)
2625 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2626 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2627 struct NeighbourMapEntry *n;
2629 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2630 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2635 "Peer %s is unknown, suggestion ignored\n",
2638 if (result == GNUNET_NO)
2640 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2641 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2642 GST_plugins_a2s (blc_ctx->address),
2644 GNUNET_i2s (&blc_ctx->address->peer));
2646 if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2649 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2650 blc_ctx->address->transport_name,
2651 GST_plugins_a2s (blc_ctx->address),
2653 GNUNET_i2s (&blc_ctx->address->peer));
2656 /* This address is blacklisted, delete address and session (if existing) in ATS */
2657 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2659 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2660 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2662 /* This is an inbound address, destroy full address */
2663 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2666 /* Remove blacklist check and clean up */
2667 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2668 GNUNET_HELLO_address_free (blc_ctx->address);
2669 GNUNET_free_non_null (blc_ctx->ats);
2670 GNUNET_free (blc_ctx);
2674 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2675 "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2676 GST_plugins_a2s (blc_ctx->address),
2678 GNUNET_i2s (&blc_ctx->address->peer));
2680 if (NULL == blc_ctx->session)
2682 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2684 "Obtained new session for peer `%s' and address '%s': %p\n",
2685 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2687 if (NULL == blc_ctx->session)
2689 /* No session could be obtained, remove blacklist check and clean up */
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2691 "Failed to obtain new session for peer `%s' and address '%s'\n",
2692 GNUNET_i2s (&blc_ctx->address->peer),
2693 GST_plugins_a2s (blc_ctx->address));
2694 /* Delete address in ATS */
2695 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2697 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2698 GNUNET_HELLO_address_free (blc_ctx->address);
2699 GNUNET_free_non_null (blc_ctx->ats);
2700 GNUNET_free (blc_ctx);
2706 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2708 free_neighbour (n, GNUNET_NO);
2710 case GNUNET_TRANSPORT_PS_INIT_ATS:
2711 /* We requested an address and ATS suggests one:
2712 * set primary address and send CONNECT message*/
2713 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2714 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2715 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2716 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2717 send_session_connect (&n->primary_address);
2719 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2720 /* waiting on CONNECT_ACK, send ACK if one is pending */
2721 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2723 n->ack_state = ACK_SEND_SESSION_ACK;
2724 send_connect_ack_message (n->primary_address.address,
2725 n->primary_address.session,
2726 n->connect_ack_timestamp);
2728 /* ATS suggests a different address, switch again */
2729 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2730 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2731 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2732 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2733 send_session_connect (&n->primary_address);
2735 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2736 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2737 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2738 /* Send an ACK message as a response to the CONNECT msg */
2739 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2740 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2741 send_connect_ack_message (n->primary_address.address,
2742 n->primary_address.session,
2743 n->connect_ack_timestamp);
2744 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2745 n->ack_state = ACK_SEND_SESSION_ACK;
2748 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2749 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2750 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2751 blc_ctx->address, blc_ctx->session);
2753 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2754 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2755 /* ATS asks us to switch while we were trying to connect; switch to new
2756 address and check blacklist again */
2757 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2758 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2759 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2760 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2761 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2762 blc_ctx->address, blc_ctx->session);
2764 case GNUNET_TRANSPORT_PS_CONNECTED:
2765 GNUNET_assert (NULL != n->primary_address.address);
2766 GNUNET_assert (NULL != n->primary_address.session);
2767 if (n->primary_address.session == blc_ctx->session)
2769 /* not an address change, just a quota change */
2770 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2771 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2774 /* ATS asks us to switch a life connection; see if we can get
2775 a CONNECT_ACK on it before we actually do this! */
2776 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2777 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2778 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2779 /* REMOVE */ connect_check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2780 blc_ctx->address, blc_ctx->session);
2782 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2783 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2784 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2785 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2786 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2787 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2788 blc_ctx->address, blc_ctx->session);
2790 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2791 /* ATS asks us to switch while we were trying to reconnect; switch to new
2792 address and check blacklist again */
2793 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2794 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2795 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2796 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2797 blc_ctx->address, blc_ctx->session);
2799 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2800 /* ATS asks us to switch while we were trying to reconnect; switch to new
2801 address and check blacklist again */
2802 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2803 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2804 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2805 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2806 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2807 blc_ctx->address, blc_ctx->session);
2809 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2810 if (n->primary_address.session == blc_ctx->session)
2812 /* ATS switches back to still-active session */
2813 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2814 free_address (&n->alternative_address);
2817 /* ATS asks us to switch a life connection, update blacklist check */
2818 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2819 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2820 /* REMOVE */ connect_check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2821 blc_ctx->address, blc_ctx->session);
2823 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2824 if (n->primary_address.session == blc_ctx->session)
2826 /* ATS switches back to still-active session */
2827 free_address (&n->alternative_address);
2828 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2831 /* ATS asks us to switch a life connection, update blacklist check */
2832 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2833 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2834 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2835 /* REMOVE */ connect_check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2836 blc_ctx->address, blc_ctx->session);
2838 case GNUNET_TRANSPORT_PS_DISCONNECT:
2839 /* not going to switch addresses while disconnecting */
2841 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2845 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2846 "Unhandled state `%s'\n",
2847 GNUNET_TRANSPORT_ps2s (n->state));
2852 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2853 GNUNET_HELLO_address_free(blc_ctx->address);
2854 GNUNET_free_non_null (blc_ctx->ats);
2855 GNUNET_free (blc_ctx);
2861 * For the given peer, switch to this address.
2863 * Before accepting this addresses and actively using it, a blacklist check
2864 * is performed. If this blacklist check fails the address will be destroyed.
2866 * @param peer identity of the peer to switch the address for
2867 * @param address address of the other peer,
2868 * @param session session to use or NULL if transport should initiate a session
2869 * @param ats performance data
2870 * @param ats_count number of entries in ats
2871 * @param bandwidth_in inbound quota to be used when connection is up,
2872 * 0 to disconnect from peer
2873 * @param bandwidth_out outbound quota to be used when connection is up,
2874 * 0 to disconnect from peer
2877 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2878 const struct GNUNET_HELLO_Address *address,
2879 struct Session *session,
2880 const struct GNUNET_ATS_Information *ats,
2882 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2883 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2885 struct NeighbourMapEntry *n;
2886 struct GST_BlacklistCheck *blc;
2887 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2888 struct BlacklistCheckSwitchContext *blc_ctx;
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2892 "ATS has decided on an address for peer %s\n",
2894 GNUNET_assert (NULL != address->transport_name);
2895 if (NULL == (n = lookup_neighbour (peer)))
2897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2898 "Peer %s is unknown, suggestion ignored\n",
2903 /* Check if plugin is available */
2904 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2906 /* we don't have the plugin for this address */
2907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2908 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2909 address->transport_name,
2911 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2914 if ((NULL == session) &&
2915 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2917 /* This is a inbound address and we do not have a session to use! */
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2919 "Inbound address without session `%s'! Destroying address...\n",
2920 GST_plugins_a2s (address));
2921 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2925 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2926 "ATS tells us to switch to %s address '%s' session %p for "
2927 "peer `%s' in state %s/%s (quota in/out %u %u )\n",
2928 GNUNET_HELLO_address_check_option (address,
2929 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2930 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2931 GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state),
2932 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2934 /* Perform blacklist check */
2935 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2936 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2937 blc_ctx->session = session;
2938 blc_ctx->bandwidth_in = bandwidth_in;
2939 blc_ctx->bandwidth_out = bandwidth_out;
2940 blc_ctx->ats_count = ats_count;
2941 blc_ctx->ats = NULL;
2944 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2945 for (c = 0; c < ats_count; c++)
2947 blc_ctx->ats[c].type = ats[c].type;
2948 blc_ctx->ats[c].value = ats[c].value;
2952 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2953 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2954 &switch_address_bl_check_cont, blc_ctx)))
2962 send_utilization_data (void *cls,
2963 const struct GNUNET_PeerIdentity *key,
2966 struct NeighbourMapEntry *n = value;
2967 struct GNUNET_ATS_Information atsi[4];
2969 uint32_t bps_pl_out;
2972 struct GNUNET_TIME_Relative delta;
2974 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2975 GNUNET_TIME_absolute_get ());
2979 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2980 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2982 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2983 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2985 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2990 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2991 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2993 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2994 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2998 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
3002 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
3003 atsi[0].value = htonl (bps_out);
3004 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
3005 atsi[1].value = htonl (bps_in);
3007 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
3008 atsi[2].value = htonl (bps_pl_out);
3009 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
3010 atsi[3].value = htonl (bps_pl_in);
3012 GST_ats_update_metrics (key, n->primary_address.address,
3013 n->primary_address.session, atsi, 4);
3014 n->util_payload_bytes_recv = 0;
3015 n->util_payload_bytes_sent = 0;
3016 n->util_total_bytes_recv = 0;
3017 n->util_total_bytes_sent = 0;
3018 n->last_util_transmission = GNUNET_TIME_absolute_get();
3024 * Task transmitting utilization in a regular interval
3026 * @param cls the 'struct NeighbourMapEntry' for which we are running
3027 * @param tc scheduler context (unused)
3030 utilization_transmission (void *cls,
3031 const struct GNUNET_SCHEDULER_TaskContext *tc)
3033 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3035 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
3036 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
3038 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3039 utilization_transmission, NULL);
3045 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
3046 const struct GNUNET_HELLO_Address *address,
3047 struct Session *session,
3048 const struct GNUNET_MessageHeader *message)
3050 struct NeighbourMapEntry *n;
3052 n = lookup_neighbour (peer);
3055 n->util_total_bytes_recv += ntohs(message->size);
3060 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
3061 const struct GNUNET_HELLO_Address *address,
3062 struct Session *session,
3063 const struct GNUNET_MessageHeader *message)
3065 struct NeighbourMapEntry *n;
3066 n = lookup_neighbour (peer);
3069 n->util_payload_bytes_recv += ntohs(message->size);
3074 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
3075 const struct GNUNET_HELLO_Address *address,
3076 struct Session *session,
3079 struct NeighbourMapEntry *n;
3080 n = lookup_neighbour (peer);
3083 if (n->primary_address.session != session)
3085 n->util_total_bytes_sent += size;
3090 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
3093 struct NeighbourMapEntry *n;
3094 n = lookup_neighbour (peer);
3097 n->util_payload_bytes_sent += size;
3102 * Master task run for every neighbour. Performs all of the time-related
3103 * activities (keep alive, send next message, disconnect if idle, finish
3104 * clean up after disconnect).
3106 * @param cls the 'struct NeighbourMapEntry' for which we are running
3107 * @param tc scheduler context (unused)
3110 master_task (void *cls,
3111 const struct GNUNET_SCHEDULER_TaskContext *tc)
3113 struct NeighbourMapEntry *n = cls;
3114 struct GNUNET_TIME_Relative delay;
3116 n->task = GNUNET_SCHEDULER_NO_TASK;
3117 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3118 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3119 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3120 GNUNET_i2s (&n->id),
3121 GNUNET_TRANSPORT_ps2s(n->state),
3122 GNUNET_STRINGS_relative_time_to_string (delay,
3126 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3127 /* invalid state for master task, clean up */
3129 free_neighbour (n, GNUNET_NO);
3131 case GNUNET_TRANSPORT_PS_INIT_ATS:
3132 if (0 == delay.rel_value_us)
3134 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3135 "Connection to `%s' timed out waiting for ATS to provide address\n",
3136 GNUNET_i2s (&n->id));
3137 free_neighbour (n, GNUNET_NO);
3141 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3142 if (0 == delay.rel_value_us)
3144 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3145 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
3146 GNUNET_i2s (&n->id));
3147 /* We could not send to this address, delete address and session */
3148 if (NULL != n->primary_address.session)
3149 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
3150 n->primary_address.session);
3151 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3153 /* Remove address and request and additional one */
3155 unset_primary_address (n);
3156 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3157 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3161 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3162 if (0 == delay.rel_value_us)
3164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3165 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
3166 GNUNET_i2s (&n->id));
3167 free_neighbour (n, GNUNET_NO);
3171 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3172 if (0 == delay.rel_value_us)
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3175 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3176 GNUNET_i2s (&n->id));
3177 free_neighbour (n, GNUNET_NO);
3181 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3182 if (0 == delay.rel_value_us)
3184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3185 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
3186 GNUNET_i2s (&n->id));
3187 free_neighbour (n, GNUNET_NO);
3191 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3192 if (0 == delay.rel_value_us)
3194 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3195 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3196 GNUNET_i2s (&n->id));
3197 disconnect_neighbour (n);
3201 case GNUNET_TRANSPORT_PS_CONNECTED:
3202 if (0 == delay.rel_value_us)
3204 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3205 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3206 GNUNET_i2s (&n->id));
3207 disconnect_neighbour (n);
3210 try_transmission_to_peer (n);
3213 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3214 if (0 == delay.rel_value_us)
3216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3217 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3218 GNUNET_i2s (&n->id));
3219 disconnect_neighbour (n);
3223 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3224 if (0 == delay.rel_value_us)
3226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3227 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
3228 GNUNET_i2s (&n->id));
3229 disconnect_neighbour (n);
3233 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3234 if (0 == delay.rel_value_us)
3236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3237 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3238 GNUNET_i2s (&n->id));
3239 disconnect_neighbour (n);
3243 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3244 if (0 == delay.rel_value_us)
3246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3247 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3248 GNUNET_i2s (&n->id));
3249 disconnect_neighbour (n);
3252 try_transmission_to_peer (n);
3255 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3256 if (0 == delay.rel_value_us)
3258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3259 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3260 GNUNET_i2s (&n->id));
3261 disconnect_neighbour (n);
3264 try_transmission_to_peer (n);
3267 case GNUNET_TRANSPORT_PS_DISCONNECT:
3268 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3269 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3270 GNUNET_i2s (&n->id));
3271 free_neighbour (n, GNUNET_NO);
3273 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3274 /* how did we get here!? */
3278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3279 "Unhandled state `%s'\n",
3280 GNUNET_TRANSPORT_ps2s (n->state));
3284 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3285 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3286 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3288 /* if we are *now* in one of these three states, we're sending
3289 keep alive messages, so we need to consider the keepalive
3290 delay, not just the connection timeout */
3291 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3294 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3295 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3302 * Send a SESSION_ACK message to the neighbour to confirm that we
3303 * got his CONNECT_ACK.
3305 * @param n neighbour to send the SESSION_ACK to
3308 send_session_ack_message (struct NeighbourMapEntry *n)
3310 struct GNUNET_MessageHeader msg;
3312 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3313 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3314 (void) send_with_session(n,
3315 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3316 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3322 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3323 * Consider switching to it.
3325 * @param message possibly a 'struct SessionConnectMessage' (check format)
3326 * @param peer identity of the peer to switch the address for
3327 * @param address address of the other peer, NULL if other peer
3329 * @param session session to use (or NULL)
3330 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3333 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3334 const struct GNUNET_PeerIdentity *peer,
3335 const struct GNUNET_HELLO_Address *address,
3336 struct Session *session)
3338 const struct SessionConnectMessage *scm;
3339 struct GNUNET_TIME_Absolute ts;
3340 struct NeighbourMapEntry *n;
3342 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3343 "Received CONNECT_ACK message from peer `%s'\n",
3346 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3348 GNUNET_break_op (0);
3349 return GNUNET_SYSERR;
3351 GNUNET_STATISTICS_update (GST_stats,
3353 ("# CONNECT_ACK messages received"),
3355 scm = (const struct SessionConnectMessage *) message;
3356 GNUNET_break_op (ntohl (scm->reserved) == 0);
3357 if (NULL == (n = lookup_neighbour (peer)))
3359 GNUNET_STATISTICS_update (GST_stats,
3361 ("# unexpected CONNECT_ACK messages (no peer)"),
3363 return GNUNET_SYSERR;
3365 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3368 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3370 free_neighbour (n, GNUNET_NO);
3371 return GNUNET_SYSERR;
3372 case GNUNET_TRANSPORT_PS_INIT_ATS:
3373 GNUNET_STATISTICS_update (GST_stats,
3375 ("# unexpected CONNECT_ACK messages (not ready)"),
3378 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3379 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3381 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3382 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3385 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3386 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3387 GNUNET_STATISTICS_set (GST_stats,
3388 gettext_noop ("# peers connected"),
3389 ++neighbours_connected,
3391 connect_notify_cb (callback_cls, &n->id,
3392 n->primary_address.bandwidth_in,
3393 n->primary_address.bandwidth_out);
3394 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3395 GST_ats_add_address (n->primary_address.address,
3396 n->primary_address.session,
3398 set_primary_address (n,
3399 n->primary_address.address,
3400 n->primary_address.session,
3401 n->primary_address.bandwidth_in,
3402 n->primary_address.bandwidth_out,
3404 send_session_ack_message (n);
3406 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3407 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3408 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3409 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3410 GNUNET_STATISTICS_update (GST_stats,
3412 ("# unexpected CONNECT_ACK messages (not ready)"),
3415 case GNUNET_TRANSPORT_PS_CONNECTED:
3416 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3417 send_session_ack_message (n);
3419 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3420 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3421 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3422 to give us a new address... */
3423 GNUNET_STATISTICS_update (GST_stats,
3425 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3428 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3429 /* new address worked; go back to connected! */
3430 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3431 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3432 send_session_ack_message (n);
3434 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3435 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3436 send_session_ack_message (n);
3438 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3439 /* new address worked; adopt it and go back to connected! */
3440 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3441 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3442 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3444 GST_ats_add_address (n->alternative_address.address,
3445 n->alternative_address.session,
3447 set_primary_address (n, n->alternative_address.address,
3448 n->alternative_address.session, n->alternative_address.bandwidth_in,
3449 n->alternative_address.bandwidth_out, GNUNET_YES);
3451 free_address (&n->alternative_address);
3452 send_session_ack_message (n);
3454 case GNUNET_TRANSPORT_PS_DISCONNECT:
3455 GNUNET_STATISTICS_update (GST_stats,
3457 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3459 return GNUNET_SYSERR;
3460 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3464 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3465 "Unhandled state `%s'\n",
3466 GNUNET_TRANSPORT_ps2s (n->state));
3468 return GNUNET_SYSERR;
3475 * A session was terminated. Take note; if needed, try to get
3476 * an alternative address from ATS.
3478 * @param peer identity of the peer where the session died
3479 * @param session session that is gone
3480 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3481 * this session was not in use
3484 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3485 struct Session *session)
3487 struct NeighbourMapEntry *n;
3488 struct BlackListCheckContext *bcc;
3489 struct BlackListCheckContext *bcc_next;
3491 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3493 while (NULL != (bcc = bcc_next))
3495 bcc_next = bcc->next;
3496 if (bcc->na.session == session)
3498 if (NULL != bcc->bc)
3499 GST_blacklist_test_cancel (bcc->bc);
3500 GNUNET_HELLO_address_free (bcc->na.address);
3501 GNUNET_CONTAINER_DLL_remove (bc_head,
3507 if (NULL == (n = lookup_neighbour (peer)))
3508 return GNUNET_NO; /* can't affect us */
3509 if (session != n->primary_address.session)
3511 if (session == n->alternative_address.session)
3513 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3514 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3515 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3518 free_address (&n->alternative_address);
3520 return GNUNET_NO; /* doesn't affect us further */
3523 n->expect_latency_response = GNUNET_NO;
3524 /* The session for neighbour's primary address died */
3527 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3529 free_neighbour (n, GNUNET_NO);
3531 case GNUNET_TRANSPORT_PS_INIT_ATS:
3533 free_neighbour (n, GNUNET_NO);
3535 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3536 /* The session used to send the CONNECT terminated:
3537 * this implies a connect error*/
3538 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3539 "Could not send CONNECT message with address `%s' session %p: session terminated, requesting new address\n",
3540 GST_plugins_a2s (n->primary_address.address), n->primary_address.session,
3542 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3543 unset_primary_address (n);
3544 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3545 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3547 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3548 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3549 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3550 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3551 /* error on inbound session; free neighbour entirely */
3552 free_address (&n->primary_address);
3553 free_neighbour (n, GNUNET_NO);
3555 case GNUNET_TRANSPORT_PS_CONNECTED:
3556 unset_primary_address (n);
3557 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3558 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3560 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3561 /* we don't have an address, how can it go down? */
3564 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3565 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3566 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3568 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3569 /* primary went down while we were checking secondary against
3570 blacklist, adopt secondary as primary */
3571 free_address (&n->primary_address);
3572 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3573 n->primary_address = n->alternative_address;
3574 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3576 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3577 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3578 secondary as primary */
3579 free_address (&n->primary_address);
3580 n->primary_address = n->alternative_address;
3581 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3582 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3584 case GNUNET_TRANSPORT_PS_DISCONNECT:
3585 free_address (&n->primary_address);
3587 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3588 /* neighbour was freed and plugins told to terminate session */
3592 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3593 "Unhandled state `%s'\n",
3594 GNUNET_TRANSPORT_ps2s (n->state));
3598 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3599 GNUNET_SCHEDULER_cancel (n->task);
3600 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3606 * We received a 'SESSION_ACK' message from the other peer.
3607 * If we sent a 'CONNECT_ACK' last, this means we are now
3608 * connected. Otherwise, do nothing.
3610 * @param message possibly a 'struct SessionConnectMessage' (check format)
3611 * @param peer identity of the peer to switch the address for
3612 * @param address address of the other peer, NULL if other peer
3614 * @param session session to use (or NULL)
3615 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3618 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3619 const struct GNUNET_PeerIdentity *peer,
3620 const struct GNUNET_HELLO_Address *address,
3621 struct Session *session)
3623 struct NeighbourMapEntry *n;
3625 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3626 "Received SESSION_ACK message from peer `%s'\n",
3628 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3630 GNUNET_break_op (0);
3631 return GNUNET_SYSERR;
3633 GNUNET_STATISTICS_update (GST_stats,
3635 ("# SESSION_ACK messages received"),
3637 if (NULL == (n = lookup_neighbour (peer)))
3639 GNUNET_break_op (0);
3640 return GNUNET_SYSERR;
3642 /* check if we are in a plausible state for having sent
3643 a CONNECT_ACK. If not, return, otherwise break */
3644 if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3645 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3646 (ACK_SEND_SESSION_ACK != n->ack_state) )
3648 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3649 "Received SESSION_ACK message from peer `%s' in state %s/%s\n",
3651 GNUNET_TRANSPORT_ps2s (n->state),
3652 print_ack_state (n->ack_state));
3653 GNUNET_STATISTICS_update (GST_stats,
3654 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3658 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3659 GNUNET_STATISTICS_set (GST_stats,
3660 gettext_noop ("# peers connected"),
3661 ++neighbours_connected,
3663 connect_notify_cb (callback_cls, &n->id,
3664 n->primary_address.bandwidth_in,
3665 n->primary_address.bandwidth_out);
3667 GST_ats_add_address (n->primary_address.address,
3668 n->primary_address.session,
3670 set_primary_address (n,
3671 n->primary_address.address,
3672 n->primary_address.session,
3673 n->primary_address.bandwidth_in,
3674 n->primary_address.bandwidth_out,
3681 * Test if we're connected to the given peer.
3683 * @param target peer to test
3684 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3687 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3689 return test_connected (lookup_neighbour (target));
3693 * Change the incoming quota for the given peer.
3695 * @param neighbour identity of peer to change qutoa for
3696 * @param quota new quota
3699 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3700 struct GNUNET_BANDWIDTH_Value32NBO quota)
3702 struct NeighbourMapEntry *n;
3704 if (NULL == (n = lookup_neighbour (neighbour)))
3706 GNUNET_STATISTICS_update (GST_stats,
3708 ("# SET QUOTA messages ignored (no such peer)"),
3712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3713 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3714 ntohl (quota.value__), GNUNET_i2s (&n->id));
3715 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3716 if (0 != ntohl (quota.value__))
3718 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3719 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3720 GNUNET_i2s (&n->id));
3721 if (GNUNET_YES == test_connected (n))
3722 GNUNET_STATISTICS_update (GST_stats,
3723 gettext_noop ("# disconnects due to quota of 0"),
3725 disconnect_neighbour (n);
3730 * We received a disconnect message from the given peer,
3731 * validate and process.
3733 * @param peer sender of the message
3734 * @param msg the disconnect message
3737 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3738 const struct GNUNET_MessageHeader *msg)
3740 struct NeighbourMapEntry *n;
3741 const struct SessionDisconnectMessage *sdm;
3743 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3744 "Received DISCONNECT message from peer `%s'\n",
3746 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3748 // GNUNET_break_op (0);
3749 GNUNET_STATISTICS_update (GST_stats,
3751 ("# disconnect messages ignored (old format)"), 1,
3755 GNUNET_STATISTICS_update (GST_stats,
3757 ("# DISCONNECT messages received"),
3759 sdm = (const struct SessionDisconnectMessage *) msg;
3760 if (NULL == (n = lookup_neighbour (peer)))
3761 return; /* gone already */
3762 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3764 GNUNET_STATISTICS_update (GST_stats,
3766 ("# disconnect messages ignored (timestamp)"), 1,
3770 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3772 GNUNET_break_op (0);
3775 if (ntohl (sdm->purpose.size) !=
3776 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3777 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3778 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3780 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3781 "%s message from peer `%s' has invalid size \n",
3784 GNUNET_break_op (0);
3788 GNUNET_CRYPTO_eddsa_verify
3789 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3790 &sdm->signature, &sdm->public_key))
3792 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3793 "%s message from peer `%s' cannot be verified \n",
3796 GNUNET_break_op (0);
3799 if (GNUNET_YES == test_connected (n))
3800 GNUNET_STATISTICS_update (GST_stats,
3802 ("# other peer asked to disconnect from us"), 1,
3804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3805 "Disconnecting by request from peer %s\n",
3807 disconnect_neighbour (n);
3812 * Closure for the neighbours_iterate function.
3814 struct IteratorContext
3817 * Function to call on each connected neighbour.
3819 GST_NeighbourIterator cb;
3829 * Call the callback from the closure for each neighbour.
3831 * @param cls the `struct IteratorContext`
3832 * @param key the hash of the public key of the neighbour
3833 * @param value the `struct NeighbourMapEntry`
3834 * @return #GNUNET_OK (continue to iterate)
3837 neighbours_iterate (void *cls,
3838 const struct GNUNET_PeerIdentity *key,
3841 struct IteratorContext *ic = cls;
3842 struct NeighbourMapEntry *n = value;
3843 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3844 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3847 if (NULL != n->primary_address.address)
3849 bandwidth_in = n->primary_address.bandwidth_in;
3850 bandwidth_out = n->primary_address.bandwidth_out;
3854 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3855 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3859 n->primary_address.address,
3862 bandwidth_in, bandwidth_out);
3868 * Iterate over all connected neighbours.
3870 * @param cb function to call
3871 * @param cb_cls closure for cb
3874 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3876 struct IteratorContext ic;
3878 if (NULL == neighbours)
3879 return; /* can happen during shutdown */
3882 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3887 * If we have an active connection to the given target, it must be shutdown.
3889 * @param target peer to disconnect from
3892 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3894 struct NeighbourMapEntry *n;
3896 if (NULL == (n = lookup_neighbour (target)))
3897 return; /* not active */
3898 if (GNUNET_YES == test_connected (n))
3899 GNUNET_STATISTICS_update (GST_stats,
3901 ("# disconnected from peer upon explicit request"), 1,
3903 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3904 "Forced disconnect from peer %s\n",
3905 GNUNET_i2s (target));
3906 disconnect_neighbour (n);
3911 * Obtain current latency information for the given neighbour.
3913 * @param peer to get the latency for
3914 * @return observed latency of the address, FOREVER if the
3915 * the connection is not up
3917 struct GNUNET_TIME_Relative
3918 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3920 struct NeighbourMapEntry *n;
3922 n = lookup_neighbour (peer);
3924 return GNUNET_TIME_UNIT_FOREVER_REL;
3927 case GNUNET_TRANSPORT_PS_CONNECTED:
3928 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3929 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3930 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3931 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3932 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3934 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3935 case GNUNET_TRANSPORT_PS_INIT_ATS:
3936 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3937 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3938 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3939 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3940 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3941 case GNUNET_TRANSPORT_PS_DISCONNECT:
3942 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3943 return GNUNET_TIME_UNIT_FOREVER_REL;
3945 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3946 "Unhandled state `%s'\n",
3947 GNUNET_TRANSPORT_ps2s (n->state));
3951 return GNUNET_TIME_UNIT_FOREVER_REL;
3956 * Obtain current address information for the given neighbour.
3959 * @return address currently used
3961 struct GNUNET_HELLO_Address *
3962 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3964 struct NeighbourMapEntry *n;
3966 n = lookup_neighbour (peer);
3969 return n->primary_address.address;
3974 * Initialize the neighbours subsystem.
3976 * @param cls closure for callbacks
3977 * @param connect_cb function to call if we connect to a peer
3978 * @param disconnect_cb function to call if we disconnect from a peer
3979 * @param peer_address_cb function to call if we change an active address
3981 * @param max_fds maximum number of fds to use
3984 GST_neighbours_start (void *cls,
3985 NotifyConnect connect_cb,
3986 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3987 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3988 unsigned int max_fds)
3991 connect_notify_cb = connect_cb;
3992 disconnect_notify_cb = disconnect_cb;
3993 neighbour_change_cb = peer_address_cb;
3994 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3995 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3996 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3997 utilization_transmission, NULL);
4002 * Disconnect from the given neighbour.
4005 * @param key hash of neighbour's public key (not used)
4006 * @param value the 'struct NeighbourMapEntry' of the neighbour
4007 * @return #GNUNET_OK (continue to iterate)
4010 disconnect_all_neighbours (void *cls,
4011 const struct GNUNET_PeerIdentity *key,
4014 struct NeighbourMapEntry *n = value;
4016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
4017 "Disconnecting peer `%4s', %s\n",
4018 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
4019 free_neighbour (n, GNUNET_NO);
4025 * Cleanup the neighbours subsystem.
4028 GST_neighbours_stop ()
4030 struct BlacklistCheckSwitchContext *cur;
4031 struct BlacklistCheckSwitchContext *next;
4033 if (NULL == neighbours)
4035 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
4037 GNUNET_SCHEDULER_cancel (util_transmission_tk);
4038 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
4041 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
4042 &disconnect_all_neighbours,
4044 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
4046 next = pending_bc_head;
4047 for (cur = next; NULL != cur; cur = next )
4050 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
4052 if (NULL != cur->blc)
4054 GST_blacklist_test_cancel (cur->blc);
4057 if (NULL != cur->address)
4058 GNUNET_HELLO_address_free (cur->address);
4059 GNUNET_free_non_null (cur->ats);
4063 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
4064 &free_notification_cb, NULL);
4065 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
4066 registered_quota_notifications = NULL;
4069 callback_cls = NULL;
4070 connect_notify_cb = NULL;
4071 disconnect_notify_cb = NULL;
4072 neighbour_change_cb = NULL;
4076 /* end of file gnunet-service-transport_neighbours.c */