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_ATS:
1099 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1100 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1101 free_neighbour (n, GNUNET_NO);
1103 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1104 /* we DID ACK the other peer's request, must send DISCONNECT */
1105 send_disconnect (n);
1106 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1108 case GNUNET_TRANSPORT_PS_CONNECTED:
1109 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1110 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1111 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1112 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1113 /* we are currently connected, need to send disconnect and do
1114 internal notifications and update statistics */
1115 send_disconnect (n);
1116 GNUNET_STATISTICS_set (GST_stats,
1117 gettext_noop ("# peers connected"),
1118 --neighbours_connected,
1120 disconnect_notify_cb (callback_cls, &n->id);
1121 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1123 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1124 /* ATS address request timeout, disconnect without sending disconnect message */
1125 GNUNET_STATISTICS_set (GST_stats,
1126 gettext_noop ("# peers connected"),
1127 --neighbours_connected,
1129 disconnect_notify_cb (callback_cls, &n->id);
1130 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1132 case GNUNET_TRANSPORT_PS_DISCONNECT:
1133 /* already disconnected, ignore */
1135 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1136 /* already cleaned up, how did we get here!? */
1140 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1141 "Unhandled state `%s'\n",
1142 GNUNET_TRANSPORT_ps2s (n->state));
1146 /* schedule timeout to clean up */
1147 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1148 GNUNET_SCHEDULER_cancel (n->task);
1149 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1155 * We're done with our transmission attempt, continue processing.
1157 * @param cls the `struct MessageQueue` of the message
1158 * @param receiver intended receiver
1159 * @param success whether it worked or not
1160 * @param size_payload bytes payload sent
1161 * @param physical bytes sent on wire
1164 transmit_send_continuation (void *cls,
1165 const struct GNUNET_PeerIdentity *receiver,
1166 int success, size_t size_payload, size_t physical)
1168 struct MessageQueue *mq = cls;
1169 struct NeighbourMapEntry *n;
1171 if (NULL == (n = lookup_neighbour (receiver)))
1174 return; /* disconnect or other error while transmitting, can happen */
1176 if (n->is_active == mq)
1178 /* this is still "our" neighbour, remove us from its queue
1179 and allow it to send the next message now */
1180 n->is_active = NULL;
1181 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1182 GNUNET_SCHEDULER_cancel (n->task);
1183 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1185 if (bytes_in_send_queue < mq->message_buf_size)
1187 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1188 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1189 bytes_in_send_queue, mq->message_buf_size,
1190 (GNUNET_OK == success) ? "OK" : "FAIL",
1191 size_payload, physical);
1196 GNUNET_break (size_payload == mq->message_buf_size);
1197 bytes_in_send_queue -= mq->message_buf_size;
1198 GNUNET_STATISTICS_set (GST_stats,
1200 ("# bytes in message queue for other peers"),
1201 bytes_in_send_queue, GNUNET_NO);
1202 if (GNUNET_OK == success)
1203 GNUNET_STATISTICS_update (GST_stats,
1205 ("# messages transmitted to other peers"),
1208 GNUNET_STATISTICS_update (GST_stats,
1210 ("# transmission failures for messages to other peers"),
1212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1213 "Sending message to `%s' of type %u was a %s\n",
1214 GNUNET_i2s (receiver),
1215 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1216 (success == GNUNET_OK) ? "success" : "FAILURE");
1217 if (NULL != mq->cont)
1218 mq->cont (mq->cont_cls, success, size_payload, physical);
1224 * Check the message list for the given neighbour and if we can
1225 * send a message, do so. This function should only be called
1226 * if the connection is at least generally ready for transmission.
1227 * While we will only send one message at a time, no bandwidth
1228 * quota management is performed here. If a message was given to
1229 * the plugin, the continuation will automatically re-schedule
1230 * the 'master' task once the next message might be transmitted.
1232 * @param n target peer for which to transmit
1235 try_transmission_to_peer (struct NeighbourMapEntry *n)
1237 struct MessageQueue *mq;
1238 struct GNUNET_TIME_Relative timeout;
1240 if (NULL == n->primary_address.address)
1242 /* no address, why are we here? */
1246 if ((0 == n->primary_address.address->address_length) &&
1247 (NULL == n->primary_address.session))
1249 /* no address, why are we here? */
1253 if (NULL != n->is_active)
1255 /* transmission already pending */
1259 /* timeout messages from the queue that are past their due date */
1260 while (NULL != (mq = n->messages_head))
1262 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1263 if (timeout.rel_value_us > 0)
1265 GNUNET_STATISTICS_update (GST_stats,
1267 ("# messages timed out while in transport queue"),
1269 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1271 transmit_send_continuation (mq, &n->id,
1273 mq->message_buf_size, 0); /* timeout */
1276 return; /* no more messages */
1277 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1279 (void) send_with_session (n,
1280 mq->message_buf, mq->message_buf_size,
1281 0 /* priority */, timeout, GNUNET_NO,
1282 &transmit_send_continuation, mq);
1287 * Send keepalive message to the neighbour. Must only be called
1288 * if we are on 'connected' state or while trying to switch addresses.
1289 * Will internally determine if a keepalive is truly needed (so can
1290 * always be called).
1292 * @param n neighbour that went idle and needs a keepalive
1295 send_keepalive (struct NeighbourMapEntry *n)
1297 struct SessionKeepAliveMessage m;
1298 struct GNUNET_TIME_Relative timeout;
1301 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1302 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1303 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1304 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1305 return; /* no keepalive needed at this time */
1307 nonce = 0; /* 0 indicates 'not set' */
1309 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1311 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1312 "Sending keep alive to peer `%s' with nonce %u\n",
1313 GNUNET_i2s (&n->id), nonce);
1315 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1316 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1317 m.nonce = htonl (nonce);
1319 timeout = send_with_session (n,
1320 (const void *) &m, sizeof (m),
1321 UINT32_MAX /* priority */,
1322 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1324 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1326 n->primary_address.keep_alive_nonce = nonce;
1327 n->expect_latency_response = GNUNET_YES;
1328 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1329 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1335 * Keep the connection to the given neighbour alive longer,
1336 * we received a KEEPALIVE (or equivalent); send a response.
1338 * @param neighbour neighbour to keep alive (by sending keep alive response)
1339 * @param m the keep alive message containing the nonce to respond to
1342 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1343 const struct GNUNET_MessageHeader *m)
1345 struct NeighbourMapEntry *n;
1346 const struct SessionKeepAliveMessage *msg_in;
1347 struct SessionKeepAliveMessage msg;
1349 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1352 msg_in = (struct SessionKeepAliveMessage *) m;
1353 if (NULL == (n = lookup_neighbour (neighbour)))
1355 GNUNET_STATISTICS_update (GST_stats,
1357 ("# KEEPALIVE messages discarded (peer unknown)"),
1361 if (NULL == n->primary_address.session)
1363 GNUNET_STATISTICS_update (GST_stats,
1365 ("# KEEPALIVE messages discarded (no session)"),
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371 "Received keep alive request from peer `%s' with nonce %u\n",
1372 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1374 /* send reply to allow neighbour to measure latency */
1375 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1376 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1377 msg.nonce = msg_in->nonce;
1378 (void) send_with_session(n,
1379 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1380 UINT32_MAX /* priority */,
1381 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1387 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1388 * latency to this peer. Pass the updated information (existing ats
1389 * plus calculated latency) to ATS.
1391 * @param neighbour neighbour to keep alive
1392 * @param m the message containing the keep alive response
1395 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1396 const struct GNUNET_MessageHeader *m)
1398 struct NeighbourMapEntry *n;
1399 const struct SessionKeepAliveMessage *msg;
1400 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1402 struct GNUNET_ATS_Information ats;
1404 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1407 msg = (const struct SessionKeepAliveMessage *) m;
1408 if (NULL == (n = lookup_neighbour (neighbour)))
1410 GNUNET_STATISTICS_update (GST_stats,
1412 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1416 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1417 (GNUNET_YES != n->expect_latency_response) )
1419 GNUNET_STATISTICS_update (GST_stats,
1421 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1425 if (NULL == n->primary_address.address)
1427 GNUNET_STATISTICS_update (GST_stats,
1429 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1433 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1435 GNUNET_STATISTICS_update (GST_stats,
1437 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1444 "Received keep alive response from peer `%s' for session %p\n",
1445 GNUNET_i2s (&n->id), n->primary_address.session);
1449 /* Update session timeout here */
1450 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1453 "Updating session for peer `%s' for session %p\n",
1454 GNUNET_i2s (&n->id), n->primary_address.session);
1455 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1462 n->primary_address.keep_alive_nonce = 0;
1463 n->expect_latency_response = GNUNET_NO;
1464 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1465 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1468 "Latency for peer `%s' is %s\n",
1469 GNUNET_i2s (&n->id),
1470 GNUNET_STRINGS_relative_time_to_string (n->latency,
1472 /* append latency */
1473 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1474 if (n->latency.rel_value_us > UINT32_MAX)
1475 latency = UINT32_MAX;
1477 latency = n->latency.rel_value_us;
1478 ats.value = htonl (latency);
1479 GST_ats_update_metrics (&n->id, n->primary_address.address,
1480 n->primary_address.session, &ats, 1);
1485 * We have received a message from the given sender. How long should
1486 * we delay before receiving more? (Also used to keep the peer marked
1489 * @param sender sender of the message
1490 * @param size size of the message
1491 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1492 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1493 * #GNUNET_SYSERR if the connection is not fully up yet
1494 * @return how long to wait before reading more from this sender
1496 struct GNUNET_TIME_Relative
1497 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1498 *sender, ssize_t size, int *do_forward)
1500 struct NeighbourMapEntry *n;
1501 struct GNUNET_TIME_Relative ret;
1503 if (NULL == neighbours)
1505 *do_forward = GNUNET_NO;
1506 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1508 if (NULL == (n = lookup_neighbour (sender)))
1510 GST_neighbours_try_connect (sender);
1511 if (NULL == (n = lookup_neighbour (sender)))
1513 GNUNET_STATISTICS_update (GST_stats,
1515 ("# messages discarded due to lack of neighbour record"),
1517 *do_forward = GNUNET_NO;
1518 return GNUNET_TIME_UNIT_ZERO;
1521 if (! test_connected (n))
1523 *do_forward = GNUNET_SYSERR;
1524 return GNUNET_TIME_UNIT_ZERO;
1526 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1528 n->quota_violation_count++;
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1531 n->in_tracker.available_bytes_per_s__,
1532 n->quota_violation_count);
1533 /* Discount 32k per violation */
1534 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1538 if (n->quota_violation_count > 0)
1540 /* try to add 32k back */
1541 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1542 n->quota_violation_count--;
1545 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1547 GNUNET_STATISTICS_update (GST_stats,
1549 ("# bandwidth quota violations by other peers"),
1551 *do_forward = GNUNET_NO;
1552 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1554 *do_forward = GNUNET_YES;
1555 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1556 if (ret.rel_value_us > 0)
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1559 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1560 (unsigned long long) n->in_tracker.
1561 consumption_since_last_update__,
1562 (unsigned int) n->in_tracker.available_bytes_per_s__,
1563 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1564 GNUNET_STATISTICS_update (GST_stats,
1565 gettext_noop ("# ms throttling suggested"),
1566 (int64_t) ret.rel_value_us / 1000LL,
1574 * Transmit a message to the given target using the active connection.
1576 * @param target destination
1577 * @param msg message to send
1578 * @param msg_size number of bytes in msg
1579 * @param timeout when to fail with timeout
1580 * @param cont function to call when done
1581 * @param cont_cls closure for 'cont'
1584 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1585 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1586 GST_NeighbourSendContinuation cont, void *cont_cls)
1588 struct NeighbourMapEntry *n;
1589 struct MessageQueue *mq;
1591 /* All ove these cases should never happen; they are all API violations.
1592 But we check anyway, just to be sure. */
1593 if (NULL == (n = lookup_neighbour (target)))
1597 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1600 if (GNUNET_YES != test_connected (n))
1604 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1607 bytes_in_send_queue += msg_size;
1608 GNUNET_STATISTICS_set (GST_stats,
1610 ("# bytes in message queue for other peers"),
1611 bytes_in_send_queue, GNUNET_NO);
1612 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1614 mq->cont_cls = cont_cls;
1615 memcpy (&mq[1], msg, msg_size);
1616 mq->message_buf = (const char *) &mq[1];
1617 mq->message_buf_size = msg_size;
1618 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1619 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1620 if ( (NULL != n->is_active) ||
1621 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1623 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1624 GNUNET_SCHEDULER_cancel (n->task);
1625 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1629 send_session_connect_cont (void *cls,
1630 const struct GNUNET_PeerIdentity *target,
1632 size_t size_payload,
1633 size_t size_on_wire)
1635 struct NeighbourMapEntry *n;
1637 n = lookup_neighbour (target);
1640 /* CONNECT continuation was called after neighbor was freed,
1641 * for example due to a time out for the state or the session
1642 * used was already terminated: nothing to do here... */
1646 if (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state)
1648 /* CONNECT continuation was called after neighbor changed state,
1649 * for example due to a time out for the state or the session
1650 * used was already terminated: nothing to do here... */
1653 if (GNUNET_OK == result)
1656 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1657 _("Failed to send CONNECT message to peer `%s' using address `%s' session %p\n"),
1658 GNUNET_i2s (target),
1659 GST_plugins_a2s (n->primary_address.address),
1660 n->primary_address.session);
1662 /* Failed to send CONNECT message with this address */
1663 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1664 n->primary_address.session);
1665 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1668 /* Remove address and request and additional one */
1669 unset_primary_address (n);
1671 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1672 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1677 * Send a SESSION_CONNECT message via the given address.
1679 * @param na address to use
1682 send_session_connect (struct NeighbourAddress *na)
1684 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1685 struct SessionConnectMessage connect_msg;
1686 struct NeighbourMapEntry *n;
1688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1689 "Sending SESSION_CONNECT message to peer %s\n",
1690 GNUNET_i2s (&na->address->peer));
1691 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1696 if (NULL == na->session)
1697 na->session = papi->get_session (papi->cls, na->address);
1698 if (NULL == na->session)
1703 GNUNET_STATISTICS_update (GST_stats,
1705 ("# SESSION_CONNECT messages sent"),
1707 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1708 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1709 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1710 connect_msg.reserved = htonl (0);
1711 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1713 papi->send (papi->cls,
1715 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1717 SETUP_CONNECTION_TIMEOUT,
1718 send_session_connect_cont, NULL))
1720 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1721 _("Failed to transmit CONNECT message via plugin to %s\n"),
1722 GST_plugins_a2s (na->address));
1724 n = lookup_neighbour (&na->address->peer);
1730 /* Hard failure to send the CONNECT message with this address:
1731 Destroy address and session */
1732 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1733 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1735 /* Remove address and request and additional one */
1736 unset_primary_address (n);
1737 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1738 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1742 GST_neighbours_notify_data_sent (&na->address->peer,
1745 sizeof (struct SessionConnectMessage));
1751 send_session_connect_ack_cont (void *cls,
1752 const struct GNUNET_PeerIdentity *target,
1754 size_t size_payload,
1755 size_t size_on_wire)
1757 struct NeighbourMapEntry *n;
1759 n = lookup_neighbour (target);
1762 /* CONNECT_ACK continuation was called after neighbor was freed,
1763 * for example due to a time out for the state or the session
1764 * used was already terminated: nothing to do here... */
1768 if (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state)
1770 /* CONNECT_ACK continuation was called after neighbor changed state,
1771 * for example due to a time out for the state or the session
1772 * used was already terminated: nothing to do here... */
1775 if (GNUNET_OK == result)
1778 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1779 _("Failed to send CONNECT_ACK message to peer `%s' using address `%s' session %p\n"),
1780 GNUNET_i2s (target),
1781 GST_plugins_a2s (n->primary_address.address),
1782 n->primary_address.session);
1784 /* Failed to send CONNECT_ACK message with this address */
1785 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1786 n->primary_address.session);
1787 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1790 /* Remove address and request and additional one */
1791 unset_primary_address (n);
1792 n->ack_state = ACK_SEND_CONNECT_ACK;
1793 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1794 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1800 * Send a CONNECT_ACK message via the given address.
1802 * @param address address to use
1803 * @param session session to use
1804 * @param timestamp timestamp to use for the ACK message
1807 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1808 struct Session *session,
1809 struct GNUNET_TIME_Absolute timestamp)
1811 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1812 struct SessionConnectMessage connect_msg;
1813 struct NeighbourMapEntry *n;
1815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1816 "Sending CONNECT_ACK to peer `%s'\n",
1817 GNUNET_i2s (&address->peer));
1818 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1823 if (NULL == session)
1824 session = papi->get_session (papi->cls, address);
1825 if (NULL == session)
1830 GNUNET_STATISTICS_update (GST_stats,
1832 ("# CONNECT_ACK messages sent"),
1834 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1835 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1836 connect_msg.reserved = htonl (0);
1837 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1839 if (GNUNET_SYSERR == papi->send (papi->cls,
1841 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1843 GNUNET_TIME_UNIT_FOREVER_REL,
1844 send_session_connect_ack_cont, NULL))
1846 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1847 _("Failed to transmit CONNECT_ACK message via plugin to %s\n"),
1848 GST_plugins_a2s (address));
1850 n = lookup_neighbour (&address->peer);
1856 /* Hard failure to send the CONNECT_ACK message with this address:
1857 Destroy session (and address) */
1858 if (GNUNET_YES == GNUNET_HELLO_address_check_option(address,
1859 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1861 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1862 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1865 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1867 /* Remove address and request and additional one */
1868 unset_primary_address (n);
1869 n->ack_state = ACK_SEND_CONNECT_ACK;
1870 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1871 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1877 struct QuotaNotificationRequest
1879 struct GNUNET_PeerIdentity peer;
1880 struct Session *session;
1884 struct QNR_LookContext
1886 struct GNUNET_PeerIdentity peer;
1887 struct Session *session;
1890 struct QuotaNotificationRequest *res;
1894 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1896 struct QNR_LookContext *qnr_ctx = cls;
1897 struct QuotaNotificationRequest *qnr = value;
1899 if ((qnr->session == qnr_ctx->session) &&
1900 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1901 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1903 qnr_ctx->res = value;
1910 GST_neighbours_register_quota_notification(void *cls,
1911 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1912 struct Session *session)
1914 struct QuotaNotificationRequest *qnr;
1915 struct QNR_LookContext qnr_ctx;
1917 if (NULL == registered_quota_notifications)
1919 return; /* init or shutdown */
1922 qnr_ctx.peer = (*peer);
1923 qnr_ctx.plugin = plugin;
1924 qnr_ctx.session = session;
1927 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1928 peer, &find_notification_request, &qnr_ctx);
1929 if (NULL != qnr_ctx.res)
1935 qnr = GNUNET_new (struct QuotaNotificationRequest);
1936 qnr->peer = (*peer);
1937 qnr->plugin = GNUNET_strdup (plugin);
1938 qnr->session = session;
1940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1941 "Adding notification for peer `%s' plugin `%s' session %p \n",
1942 GNUNET_i2s (peer), plugin, session);
1944 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1945 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1950 GST_neighbours_unregister_quota_notification(void *cls,
1951 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1953 struct QNR_LookContext qnr_ctx;
1955 if (NULL == registered_quota_notifications)
1957 return; /* init or shutdown */
1960 qnr_ctx.peer = (*peer);
1961 qnr_ctx.plugin = plugin;
1962 qnr_ctx.session = session;
1965 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1966 &find_notification_request, &qnr_ctx);
1967 if (NULL == qnr_ctx.res)
1973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1974 "Removing notification for peer `%s' plugin `%s' session %p \n",
1975 GNUNET_i2s (peer), plugin, session);
1977 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
1979 GNUNET_free (qnr_ctx.res->plugin);
1980 GNUNET_free (qnr_ctx.res);
1984 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1986 /* struct NeighbourMapEntry *n = cls; */
1987 struct QuotaNotificationRequest *qnr = value;
1988 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1989 struct GNUNET_TIME_Relative delay;
1992 papi = GST_plugins_find(qnr->plugin);
1999 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
2002 delay.rel_value_us / 1000);
2004 if (NULL != papi->update_inbound_delay)
2005 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
2011 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
2014 /* struct NeighbourMapEntry *n = cls; */
2015 struct QuotaNotificationRequest *qnr = value;
2017 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
2019 GNUNET_free(qnr->plugin);
2026 inbound_bw_tracker_update(void *cls)
2028 struct NeighbourMapEntry *n = cls;
2030 /* Quota was updated, tell plugins to update the time to receive next */
2031 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2032 &n->id, ¬ification_cb, n);
2037 * Create a fresh entry in the neighbour map for the given peer
2039 * @param peer peer to create an entry for
2040 * @return new neighbour map entry
2042 static struct NeighbourMapEntry *
2043 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2045 struct NeighbourMapEntry *n;
2047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2048 "Creating new neighbour entry for `%s'\n",
2050 n = GNUNET_new (struct NeighbourMapEntry);
2052 n->ack_state = ACK_UNDEFINED;
2053 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2054 n->last_util_transmission = GNUNET_TIME_absolute_get();
2055 n->util_payload_bytes_recv = 0;
2056 n->util_payload_bytes_sent = 0;
2057 n->util_total_bytes_recv = 0;
2058 n->util_total_bytes_sent = 0;
2059 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
2060 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2061 MAX_BANDWIDTH_CARRY_S);
2062 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2063 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2064 GNUNET_assert (GNUNET_OK ==
2065 GNUNET_CONTAINER_multipeermap_put (neighbours,
2067 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2073 * Check if the two given addresses are the same.
2074 * Actually only checks if the sessions are non-NULL
2075 * (which they should be) and then if they are identical;
2076 * the actual addresses don't matter if the session
2077 * pointers match anyway, and we must have session pointers
2080 * @param a1 first address to compare
2081 * @param a2 other address to compare
2082 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
2085 address_matches (const struct NeighbourAddress *a1,
2086 const struct NeighbourAddress *a2)
2088 if ( (NULL == a1->session) ||
2089 (NULL == a2->session) )
2094 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
2098 /* We received a address suggestion after requesting an address in
2099 * try_connect or after receiving a connect, switch to address
2102 address_suggest_cont (void *cls,
2103 const struct GNUNET_PeerIdentity *peer,
2104 const struct GNUNET_HELLO_Address *address, struct Session *session,
2105 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2106 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2107 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2109 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2110 bandwidth_in, bandwidth_out);
2114 struct BlacklistCheckSwitchContext
2116 struct BlacklistCheckSwitchContext *prev;
2117 struct BlacklistCheckSwitchContext *next;
2120 struct GST_BlacklistCheck *blc;
2122 struct GNUNET_HELLO_Address *address;
2123 struct Session *session;
2124 struct GNUNET_ATS_Information *ats;
2127 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2128 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2132 * Black list check result for try_connect call
2133 * If connection to the peer is allowed request adddress and
2135 * @param cls blc_ctx bl context
2136 * @param peer the peer
2137 * @param result the result
2140 try_connect_bl_check_cont (void *cls,
2141 const struct GNUNET_PeerIdentity *peer, int result)
2143 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2144 struct NeighbourMapEntry *n;
2146 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2147 GNUNET_free (blc_ctx);
2149 if (GNUNET_OK != result)
2151 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2152 _("Blacklisting disapproved to connect to peer `%s'\n"),
2157 /* Setup a new neighbour */
2158 n = setup_neighbour (peer);
2160 /* Request address suggestions for this peer */
2161 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2162 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2163 GNUNET_ATS_reset_backoff (GST_ats, peer);
2164 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2165 &address_suggest_cont, n);
2171 * Try to create a connection to the given target (eventually).
2173 * @param target peer to try to connect to
2176 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2178 struct NeighbourMapEntry *n;
2179 struct GST_BlacklistCheck *blc;
2180 struct BlacklistCheckSwitchContext *blc_ctx;
2182 if (NULL == neighbours)
2184 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2185 "Asked to connect to peer `%s' during shutdown\n",
2186 GNUNET_i2s (target));
2187 return; /* during shutdown, do nothing */
2189 n = lookup_neighbour (target);
2190 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2191 "Asked to connect to peer `%s' (state: %s)\n",
2192 GNUNET_i2s (target),
2193 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2198 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2199 /* this should not be possible */
2201 free_neighbour (n, GNUNET_NO);
2203 case GNUNET_TRANSPORT_PS_INIT_ATS:
2204 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2205 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2206 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2207 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2208 "Ignoring request to try to connect to `%s', already trying!\n",
2209 GNUNET_i2s (target));
2210 return; /* already trying */
2211 case GNUNET_TRANSPORT_PS_CONNECTED:
2212 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2213 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2214 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2215 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2216 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2217 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2218 "Ignoring request to try to connect, already connected to `%s'!\n",
2219 GNUNET_i2s (target));
2220 return; /* already connected */
2221 case GNUNET_TRANSPORT_PS_DISCONNECT:
2222 /* get rid of remains, ready to re-try immediately */
2223 free_neighbour (n, GNUNET_NO);
2225 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2226 /* should not be possible */
2230 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2231 "Unhandled state `%s'\n",
2232 GNUNET_TRANSPORT_ps2s (n->state));
2234 free_neighbour (n, GNUNET_NO);
2239 /* Do blacklist check if connecting to this peer is allowed */
2240 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2241 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2243 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2244 &try_connect_bl_check_cont, blc_ctx)))
2252 * Function called with the result of a blacklist check.
2254 * @param cls closure with the `struct BlackListCheckContext`
2255 * @param peer peer this check affects
2256 * @param result #GNUNET_OK if the address is allowed
2259 handle_connect_blacklist_check_cont (void *cls,
2260 const struct GNUNET_PeerIdentity *peer,
2263 struct BlackListCheckContext *bcc = cls;
2264 struct NeighbourMapEntry *n;
2267 GNUNET_CONTAINER_DLL_remove (bc_head,
2270 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2271 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2273 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2275 if (NULL == (n = lookup_neighbour (peer)))
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2278 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2280 goto cleanup; /* nobody left to care about new address */
2283 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2284 "Blacklist check after CONNECT for peer `%s' in state %s/%s: %s\n",
2286 GNUNET_TRANSPORT_ps2s (n->state),
2287 print_ack_state (n->ack_state),
2288 (GNUNET_OK == result) ? "OK" : "FAIL");
2290 if (GNUNET_OK == result)
2292 /* Blacklist agreed on connecting to a peer with this address, notify ATS */
2293 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2294 "Notifying ATS peer's `%s' %s address `%s' session %p\n",
2296 (GNUNET_YES == GNUNET_HELLO_address_check_option(bcc->na.address,
2297 GNUNET_HELLO_ADDRESS_INFO_INBOUND)) ? "inbound" : "outbound",
2298 GST_plugins_a2s (bcc->na.address), bcc->na.session);
2299 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2304 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2305 /* This should not be possible */
2307 free_neighbour (n, GNUNET_NO);
2309 case GNUNET_TRANSPORT_PS_INIT_ATS:
2310 /* Waiting on ATS suggestion */
2312 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2314 /* TODO Why should I send an connect ACK message */
2315 /* waiting on CONNECT_ACK, send ACK if one is pending */
2317 if ( (GNUNET_OK == result) &&
2318 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2320 n->ack_state = ACK_SEND_SESSION_ACK;
2321 send_connect_ack_message (n->primary_address.address,
2322 n->primary_address.session,
2323 n->connect_ack_timestamp);
2327 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2328 /* waiting on ATS suggestion, don't care about blacklist */
2330 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2331 /* waiting on SESSION_ACK, send ACK if one is pending */
2332 if ( (GNUNET_OK == result) &&
2333 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2335 /* TODO: Why should this happen? */
2336 /* *Debug message: */ GNUNET_break (0);
2338 n->ack_state = ACK_SEND_SESSION_ACK;
2339 send_connect_ack_message (n->primary_address.address,
2340 n->primary_address.session,
2341 n->connect_ack_timestamp);
2344 case GNUNET_TRANSPORT_PS_CONNECTED:
2345 /* already connected, don't care about blacklist */
2347 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2348 /* still waiting on ATS suggestion, don't care about blacklist */
2350 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2351 if ( (GNUNET_OK == result) &&
2352 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2354 n->ack_state = ACK_SEND_SESSION_ACK;
2355 send_connect_ack_message (bcc->na.address,
2357 n->connect_ack_timestamp);
2359 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "Blacklist result ignored, as it is not for our primary address\n");
2363 break; /* result for an address we currently don't care about */
2365 if (GNUNET_OK == result)
2367 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2368 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2369 send_session_connect (&n->primary_address);
2373 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
2374 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2377 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2378 /* waiting on CONNECT_ACK, don't care about blacklist */
2379 if ( (GNUNET_OK == result) &&
2380 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2382 n->ack_state = ACK_SEND_SESSION_ACK;
2383 send_connect_ack_message (n->primary_address.address,
2384 n->primary_address.session,
2385 n->connect_ack_timestamp);
2388 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2389 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2392 "Blacklist result ignored, as it is not for our primary address\n");
2393 break; /* result for an address we currently don't care about */
2395 if (GNUNET_OK == result)
2397 send_session_connect (&n->alternative_address);
2398 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2402 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2403 free_address (&n->alternative_address);
2406 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2407 /* waiting on CONNECT_ACK, don't care about blacklist */
2408 if ( (GNUNET_OK == result) &&
2409 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2411 n->ack_state = ACK_SEND_SESSION_ACK;
2412 send_connect_ack_message (n->primary_address.address,
2413 n->primary_address.session,
2414 n->connect_ack_timestamp);
2417 case GNUNET_TRANSPORT_PS_DISCONNECT:
2418 /* Nothing to do here, ATS will already do what can be done */
2420 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2421 /* should not be possible */
2425 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2426 "Unhandled state `%s'\n",
2427 GNUNET_TRANSPORT_ps2s (n->state));
2429 free_neighbour (n, GNUNET_NO);
2433 GNUNET_HELLO_address_free (bcc->na.address);
2439 * We received a CONNECT message and want to know if connecting to a particular
2440 * peer via a particular address is allowed. Check it!
2442 * @param peer identity of the peer to switch the address for
2443 * @param ts time at which the check was initiated
2444 * @param address address of the other peer, NULL if other peer
2446 * @param session session to use (or NULL)
2449 connect_check_blacklist (const struct GNUNET_PeerIdentity *peer,
2450 struct GNUNET_TIME_Absolute ts,
2451 const struct GNUNET_HELLO_Address *address,
2452 struct Session *session)
2454 struct BlackListCheckContext *bcc;
2455 struct GST_BlacklistCheck *bc;
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 "Checking peer `%s' against blacklist\n",
2460 bcc = GNUNET_new (struct BlackListCheckContext);
2461 bcc->na.address = GNUNET_HELLO_address_copy (address);
2462 bcc->na.session = session;
2463 bcc->na.connect_timestamp = ts;
2464 GNUNET_CONTAINER_DLL_insert (bc_head,
2467 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2468 (NULL != address) ? address->transport_name : NULL,
2469 &handle_connect_blacklist_check_cont, bcc)))
2471 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2472 we must only store 'bc' if 'bc' is non-NULL... */
2477 * We received a 'SESSION_CONNECT' message from the other peer.
2478 * Consider switching to it.
2480 * @param message possibly a 'struct SessionConnectMessage' (check format)
2481 * @param peer identity of the peer to switch the address for
2482 * @param address address of the other peer, NULL if other peer
2484 * @param session session to use (or NULL)
2485 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2488 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2489 const struct GNUNET_PeerIdentity *peer,
2490 const struct GNUNET_HELLO_Address *address,
2491 struct Session *session)
2493 const struct SessionConnectMessage *scm;
2494 struct NeighbourMapEntry *n;
2495 struct GNUNET_TIME_Absolute ts;
2497 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2498 "Received CONNECT message from peer `%s' with `%s' %p\n",
2499 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
2500 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2502 GNUNET_break_op (0);
2503 return GNUNET_SYSERR;
2505 GNUNET_STATISTICS_update (GST_stats,
2507 ("# CONNECT messages received"),
2509 if (NULL == neighbours)
2511 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2512 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2514 return GNUNET_OK; /* we're shutting down */
2516 scm = (const struct SessionConnectMessage *) message;
2517 GNUNET_break_op (0 == ntohl (scm->reserved));
2518 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2519 n = lookup_neighbour (peer);
2522 /* This is a new neighbour and set to not connected */
2523 n = setup_neighbour (peer);
2526 /* Remember this CONNECT message in neighbour */
2527 n->ack_state = ACK_SEND_CONNECT_ACK;
2528 n->connect_ack_timestamp = ts;
2530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2531 "Received CONNECT for peer `%s' in state %s/%s\n",
2533 GNUNET_TRANSPORT_ps2s (n->state),
2534 print_ack_state (n->ack_state));
2538 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2539 /* Request an address from ATS to send CONNECT_ACK to this peer */
2540 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2541 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2542 if (NULL == n->suggest_handle)
2543 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2545 case GNUNET_TRANSPORT_PS_INIT_ATS:
2546 /* CONNECT message takes priority over us asking ATS for address:
2547 * Wait for ATS to suggest an address and send CONNECT_ACK */
2548 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2549 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2551 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2552 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2553 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2554 /* It can never hurt to have an alternative address in the above cases,
2555 see if it is allowed */
2556 connect_check_blacklist (peer, ts, address, session);
2558 case GNUNET_TRANSPORT_PS_CONNECTED:
2559 /* we are already connected and can thus send the ACK immediately;
2560 still, it can never hurt to have an alternative address, so also
2561 tell ATS about it */
2562 GNUNET_assert (NULL != n->primary_address.address);
2563 GNUNET_assert (NULL != n->primary_address.session);
2564 n->ack_state = ACK_UNDEFINED;
2565 send_connect_ack_message (n->primary_address.address,
2566 n->primary_address.session, ts);
2567 connect_check_blacklist (peer, ts, address, session);
2569 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2570 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2571 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2572 /* It can never hurt to have an alternative address in the above cases,
2573 see if it is allowed */
2574 connect_check_blacklist (peer, ts, address, session);
2576 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2577 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2578 /* we are already connected and can thus send the ACK immediately;
2579 still, it can never hurt to have an alternative address, so also
2580 tell ATS about it */
2581 GNUNET_assert (NULL != n->primary_address.address);
2582 GNUNET_assert (NULL != n->primary_address.session);
2583 n->ack_state = ACK_UNDEFINED;
2584 send_connect_ack_message (n->primary_address.address,
2585 n->primary_address.session, ts);
2586 connect_check_blacklist (peer, ts, address, session);
2588 case GNUNET_TRANSPORT_PS_DISCONNECT:
2589 /* get rid of remains without terminating sessions, ready to re-try */
2590 free_neighbour (n, GNUNET_YES);
2591 n = setup_neighbour (peer);
2592 /* Remember the CONNECT timestamp for ACK message */
2593 n->ack_state = ACK_SEND_CONNECT_ACK;
2594 n->connect_ack_timestamp = ts;
2595 /* Request an address for the peer */
2596 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2597 GNUNET_ATS_reset_backoff (GST_ats, peer);
2598 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2600 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2601 /* should not be possible */
2605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2606 "Unhandled state `%s'\n",
2607 GNUNET_TRANSPORT_ps2s (n->state));
2609 return GNUNET_SYSERR;
2615 switch_address_bl_check_cont (void *cls,
2616 const struct GNUNET_PeerIdentity *peer, int result)
2618 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2619 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2620 struct NeighbourMapEntry *n;
2622 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2623 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "Peer %s is unknown, suggestion ignored\n",
2631 if (result == GNUNET_NO)
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2634 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2635 GST_plugins_a2s (blc_ctx->address),
2637 GNUNET_i2s (&blc_ctx->address->peer));
2639 if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2641 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2642 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2643 blc_ctx->address->transport_name,
2644 GST_plugins_a2s (blc_ctx->address),
2646 GNUNET_i2s (&blc_ctx->address->peer));
2649 /* This address is blacklisted, delete address and session (if existing) in ATS */
2650 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2652 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2653 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2655 /* This is an inbound address, destroy full address */
2656 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2659 /* Remove blacklist check and clean up */
2660 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2661 GNUNET_HELLO_address_free (blc_ctx->address);
2662 GNUNET_free_non_null (blc_ctx->ats);
2663 GNUNET_free (blc_ctx);
2667 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2668 "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2669 GST_plugins_a2s (blc_ctx->address),
2671 GNUNET_i2s (&blc_ctx->address->peer));
2673 if (NULL == blc_ctx->session)
2675 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2677 "Obtained new session for peer `%s' and address '%s': %p\n",
2678 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2680 if (NULL == blc_ctx->session)
2682 /* No session could be obtained, remove blacklist check and clean up */
2683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2684 "Failed to obtain new session for peer `%s' and address '%s'\n",
2685 GNUNET_i2s (&blc_ctx->address->peer),
2686 GST_plugins_a2s (blc_ctx->address));
2687 /* Delete address in ATS */
2688 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2690 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2691 GNUNET_HELLO_address_free (blc_ctx->address);
2692 GNUNET_free_non_null (blc_ctx->ats);
2693 GNUNET_free (blc_ctx);
2699 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2701 free_neighbour (n, GNUNET_NO);
2703 case GNUNET_TRANSPORT_PS_INIT_ATS:
2704 /* We requested an address and ATS suggests one:
2705 * set primary address and send CONNECT message*/
2706 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2707 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2708 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2709 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2710 send_session_connect (&n->primary_address);
2712 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2713 /* waiting on CONNECT_ACK, send ACK if one is pending */
2714 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2716 n->ack_state = ACK_SEND_SESSION_ACK;
2717 send_connect_ack_message (n->primary_address.address,
2718 n->primary_address.session, n->connect_ack_timestamp);
2720 /* ATS suggests a different address, switch again */
2721 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2722 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2723 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2724 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2725 send_session_connect (&n->primary_address);
2727 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2728 /* We requested an address and ATS suggests one:
2729 * set primary address and send CONNECT_ACK message*/
2730 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2731 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2732 /* Send an ACK message as a response to the CONNECT msg */
2733 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2734 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2735 send_connect_ack_message (n->primary_address.address,
2736 n->primary_address.session,
2737 n->connect_ack_timestamp);
2738 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2739 n->ack_state = ACK_SEND_SESSION_ACK;
2741 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2742 /* ATS asks us to switch while we were trying to connect; switch to new
2743 address and check blacklist again */
2744 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2745 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2746 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2747 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2748 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) )
2750 n->ack_state = ACK_SEND_SESSION_ACK;
2751 send_connect_ack_message (n->primary_address.address,
2752 n->primary_address.session, n->connect_ack_timestamp);
2755 case GNUNET_TRANSPORT_PS_CONNECTED:
2756 GNUNET_assert (NULL != n->primary_address.address);
2757 GNUNET_assert (NULL != n->primary_address.session);
2758 if (n->primary_address.session == blc_ctx->session)
2760 /* not an address change, just a quota change */
2761 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2762 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2765 /* ATS asks us to switch a life connection; see if we can get
2766 a CONNECT_ACK on it before we actually do this! */
2767 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2768 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2769 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2770 /* REMOVE */ connect_check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2771 blc_ctx->address, blc_ctx->session);
2773 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2774 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2775 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2776 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2777 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2778 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2779 blc_ctx->address, blc_ctx->session);
2781 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2782 /* ATS asks us to switch while we were trying to reconnect; switch to new
2783 address and check blacklist again */
2784 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2785 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2786 set_timeout (n, 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_SENT:
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_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2796 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2797 /* REMOVE */ connect_check_blacklist (&n->id, n->connect_ack_timestamp,
2798 blc_ctx->address, blc_ctx->session);
2800 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2801 if (n->primary_address.session == blc_ctx->session)
2803 /* ATS switches back to still-active session */
2804 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2805 free_address (&n->alternative_address);
2808 /* ATS asks us to switch a life connection, update blacklist check */
2809 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2810 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2811 /* REMOVE */ connect_check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2812 blc_ctx->address, blc_ctx->session);
2814 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2815 if (n->primary_address.session == blc_ctx->session)
2817 /* ATS switches back to still-active session */
2818 free_address (&n->alternative_address);
2819 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2822 /* ATS asks us to switch a life connection, update blacklist check */
2823 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2824 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2825 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2826 /* REMOVE */ connect_check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2827 blc_ctx->address, blc_ctx->session);
2829 case GNUNET_TRANSPORT_PS_DISCONNECT:
2830 /* not going to switch addresses while disconnecting */
2832 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2836 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2837 "Unhandled state `%s'\n",
2838 GNUNET_TRANSPORT_ps2s (n->state));
2843 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2844 GNUNET_HELLO_address_free(blc_ctx->address);
2845 GNUNET_free_non_null (blc_ctx->ats);
2846 GNUNET_free (blc_ctx);
2852 * For the given peer, switch to this address.
2854 * Before accepting this addresses and actively using it, a blacklist check
2855 * is performed. If this blacklist check fails the address will be destroyed.
2857 * @param peer identity of the peer to switch the address for
2858 * @param address address of the other peer,
2859 * @param session session to use or NULL if transport should initiate a session
2860 * @param ats performance data
2861 * @param ats_count number of entries in ats
2862 * @param bandwidth_in inbound quota to be used when connection is up,
2863 * 0 to disconnect from peer
2864 * @param bandwidth_out outbound quota to be used when connection is up,
2865 * 0 to disconnect from peer
2868 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2869 const struct GNUNET_HELLO_Address *address,
2870 struct Session *session,
2871 const struct GNUNET_ATS_Information *ats,
2873 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2874 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2876 struct NeighbourMapEntry *n;
2877 struct GST_BlacklistCheck *blc;
2878 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2879 struct BlacklistCheckSwitchContext *blc_ctx;
2882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2883 "ATS has decided on an address for peer %s\n",
2885 GNUNET_assert (NULL != address->transport_name);
2886 if (NULL == (n = lookup_neighbour (peer)))
2888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2889 "Peer %s is unknown, suggestion ignored\n",
2894 /* Check if plugin is available */
2895 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2897 /* we don't have the plugin for this address */
2898 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2899 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2900 address->transport_name,
2902 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2905 if ((NULL == session) &&
2906 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2908 /* This is a inbound address and we do not have a session to use! */
2909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2910 "Inbound address without session `%s'! Destroying address...\n",
2911 GST_plugins_a2s (address));
2912 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2916 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2917 "ATS tells us to switch to %s address '%s' session %p for "
2918 "peer `%s' in state %s/%s (quota in/out %u %u )\n",
2919 GNUNET_HELLO_address_check_option (address,
2920 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2921 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2922 GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state),
2923 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2925 /* Perform blacklist check */
2926 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2927 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2928 blc_ctx->session = session;
2929 blc_ctx->bandwidth_in = bandwidth_in;
2930 blc_ctx->bandwidth_out = bandwidth_out;
2931 blc_ctx->ats_count = ats_count;
2932 blc_ctx->ats = NULL;
2935 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2936 for (c = 0; c < ats_count; c++)
2938 blc_ctx->ats[c].type = ats[c].type;
2939 blc_ctx->ats[c].value = ats[c].value;
2943 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2944 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2945 &switch_address_bl_check_cont, blc_ctx)))
2953 send_utilization_data (void *cls,
2954 const struct GNUNET_PeerIdentity *key,
2957 struct NeighbourMapEntry *n = value;
2958 struct GNUNET_ATS_Information atsi[4];
2960 uint32_t bps_pl_out;
2963 struct GNUNET_TIME_Relative delta;
2965 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2966 GNUNET_TIME_absolute_get ());
2970 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2971 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2973 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2974 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2976 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2981 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2982 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2984 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2985 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2989 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2993 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2994 atsi[0].value = htonl (bps_out);
2995 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2996 atsi[1].value = htonl (bps_in);
2998 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2999 atsi[2].value = htonl (bps_pl_out);
3000 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
3001 atsi[3].value = htonl (bps_pl_in);
3003 GST_ats_update_metrics (key, n->primary_address.address,
3004 n->primary_address.session, atsi, 4);
3005 n->util_payload_bytes_recv = 0;
3006 n->util_payload_bytes_sent = 0;
3007 n->util_total_bytes_recv = 0;
3008 n->util_total_bytes_sent = 0;
3009 n->last_util_transmission = GNUNET_TIME_absolute_get();
3015 * Task transmitting utilization in a regular interval
3017 * @param cls the 'struct NeighbourMapEntry' for which we are running
3018 * @param tc scheduler context (unused)
3021 utilization_transmission (void *cls,
3022 const struct GNUNET_SCHEDULER_TaskContext *tc)
3024 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3026 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
3027 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
3029 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3030 utilization_transmission, NULL);
3036 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
3037 const struct GNUNET_HELLO_Address *address,
3038 struct Session *session,
3039 const struct GNUNET_MessageHeader *message)
3041 struct NeighbourMapEntry *n;
3043 n = lookup_neighbour (peer);
3046 n->util_total_bytes_recv += ntohs(message->size);
3051 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
3052 const struct GNUNET_HELLO_Address *address,
3053 struct Session *session,
3054 const struct GNUNET_MessageHeader *message)
3056 struct NeighbourMapEntry *n;
3057 n = lookup_neighbour (peer);
3060 n->util_payload_bytes_recv += ntohs(message->size);
3065 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
3066 const struct GNUNET_HELLO_Address *address,
3067 struct Session *session,
3070 struct NeighbourMapEntry *n;
3071 n = lookup_neighbour (peer);
3074 if (n->primary_address.session != session)
3076 n->util_total_bytes_sent += size;
3081 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
3084 struct NeighbourMapEntry *n;
3085 n = lookup_neighbour (peer);
3088 n->util_payload_bytes_sent += size;
3093 * Master task run for every neighbour. Performs all of the time-related
3094 * activities (keep alive, send next message, disconnect if idle, finish
3095 * clean up after disconnect).
3097 * @param cls the 'struct NeighbourMapEntry' for which we are running
3098 * @param tc scheduler context (unused)
3101 master_task (void *cls,
3102 const struct GNUNET_SCHEDULER_TaskContext *tc)
3104 struct NeighbourMapEntry *n = cls;
3105 struct GNUNET_TIME_Relative delay;
3107 n->task = GNUNET_SCHEDULER_NO_TASK;
3108 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3109 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3110 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3111 GNUNET_i2s (&n->id),
3112 GNUNET_TRANSPORT_ps2s(n->state),
3113 GNUNET_STRINGS_relative_time_to_string (delay,
3117 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3118 /* invalid state for master task, clean up */
3120 free_neighbour (n, GNUNET_NO);
3122 case GNUNET_TRANSPORT_PS_INIT_ATS:
3123 if (0 == delay.rel_value_us)
3125 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3126 "Connection to `%s' timed out waiting for ATS to provide address\n",
3127 GNUNET_i2s (&n->id));
3128 free_neighbour (n, GNUNET_NO);
3132 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3133 if (0 == delay.rel_value_us)
3135 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3136 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
3137 GNUNET_i2s (&n->id));
3138 /* We could not send to this address, delete address and session */
3139 if (NULL != n->primary_address.session)
3140 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
3141 n->primary_address.session);
3142 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3144 /* Remove address and request and additional one */
3145 unset_primary_address (n);
3146 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3147 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3151 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3152 if (0 == delay.rel_value_us)
3154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3155 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3156 GNUNET_i2s (&n->id));
3157 free_neighbour (n, GNUNET_NO);
3161 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3162 if (0 == delay.rel_value_us)
3164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3165 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3166 GNUNET_i2s (&n->id));
3167 disconnect_neighbour (n);
3171 case GNUNET_TRANSPORT_PS_CONNECTED:
3172 if (0 == delay.rel_value_us)
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3175 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3176 GNUNET_i2s (&n->id));
3177 disconnect_neighbour (n);
3180 try_transmission_to_peer (n);
3183 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3184 if (0 == delay.rel_value_us)
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3187 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3188 GNUNET_i2s (&n->id));
3189 disconnect_neighbour (n);
3193 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3194 if (0 == delay.rel_value_us)
3196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3197 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
3198 GNUNET_i2s (&n->id));
3199 disconnect_neighbour (n);
3203 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3204 if (0 == delay.rel_value_us)
3206 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3207 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3208 GNUNET_i2s (&n->id));
3209 disconnect_neighbour (n);
3213 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3214 if (0 == delay.rel_value_us)
3216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3217 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3218 GNUNET_i2s (&n->id));
3219 disconnect_neighbour (n);
3222 try_transmission_to_peer (n);
3225 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3226 if (0 == delay.rel_value_us)
3228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3229 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3230 GNUNET_i2s (&n->id));
3231 disconnect_neighbour (n);
3234 try_transmission_to_peer (n);
3237 case GNUNET_TRANSPORT_PS_DISCONNECT:
3238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3239 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3240 GNUNET_i2s (&n->id));
3241 free_neighbour (n, GNUNET_NO);
3243 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3244 /* how did we get here!? */
3248 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3249 "Unhandled state `%s'\n",
3250 GNUNET_TRANSPORT_ps2s (n->state));
3254 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3255 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3256 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3258 /* if we are *now* in one of these three states, we're sending
3259 keep alive messages, so we need to consider the keepalive
3260 delay, not just the connection timeout */
3261 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3264 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3265 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3272 * Send a SESSION_ACK message to the neighbour to confirm that we
3273 * got his CONNECT_ACK.
3275 * @param n neighbour to send the SESSION_ACK to
3278 send_session_ack_message (struct NeighbourMapEntry *n)
3280 struct GNUNET_MessageHeader msg;
3282 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3283 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3284 (void) send_with_session(n,
3285 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3286 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3292 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3293 * Consider switching to it.
3295 * @param message possibly a 'struct SessionConnectMessage' (check format)
3296 * @param peer identity of the peer to switch the address for
3297 * @param address address of the other peer, NULL if other peer
3299 * @param session session to use (or NULL)
3300 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3303 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3304 const struct GNUNET_PeerIdentity *peer,
3305 const struct GNUNET_HELLO_Address *address,
3306 struct Session *session)
3308 const struct SessionConnectMessage *scm;
3309 struct GNUNET_TIME_Absolute ts;
3310 struct NeighbourMapEntry *n;
3312 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3313 "Received CONNECT_ACK message from peer `%s'\n",
3316 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3318 GNUNET_break_op (0);
3319 return GNUNET_SYSERR;
3321 GNUNET_STATISTICS_update (GST_stats,
3323 ("# CONNECT_ACK messages received"),
3325 scm = (const struct SessionConnectMessage *) message;
3326 GNUNET_break_op (ntohl (scm->reserved) == 0);
3327 if (NULL == (n = lookup_neighbour (peer)))
3329 GNUNET_STATISTICS_update (GST_stats,
3331 ("# unexpected CONNECT_ACK messages (no peer)"),
3333 return GNUNET_SYSERR;
3335 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3338 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3340 free_neighbour (n, GNUNET_NO);
3341 return GNUNET_SYSERR;
3342 case GNUNET_TRANSPORT_PS_INIT_ATS:
3343 GNUNET_STATISTICS_update (GST_stats,
3345 ("# unexpected CONNECT_ACK messages (not ready)"),
3348 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3349 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3352 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3355 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3356 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3357 GNUNET_STATISTICS_set (GST_stats,
3358 gettext_noop ("# peers connected"),
3359 ++neighbours_connected,
3361 connect_notify_cb (callback_cls, &n->id,
3362 n->primary_address.bandwidth_in,
3363 n->primary_address.bandwidth_out);
3364 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3365 GST_ats_add_address (n->primary_address.address,
3366 n->primary_address.session,
3368 set_primary_address (n,
3369 n->primary_address.address,
3370 n->primary_address.session,
3371 n->primary_address.bandwidth_in,
3372 n->primary_address.bandwidth_out,
3374 send_session_ack_message (n);
3376 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3377 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3378 GNUNET_STATISTICS_update (GST_stats,
3380 ("# unexpected CONNECT_ACK messages (not ready)"),
3383 case GNUNET_TRANSPORT_PS_CONNECTED:
3384 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3385 send_session_ack_message (n);
3387 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3388 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3389 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3390 to give us a new address... */
3391 GNUNET_STATISTICS_update (GST_stats,
3393 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3396 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3397 /* new address worked; go back to connected! */
3398 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3399 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3400 send_session_ack_message (n);
3402 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3403 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3404 send_session_ack_message (n);
3406 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3407 /* new address worked; adopt it and go back to connected! */
3408 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3409 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3410 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3412 GST_ats_add_address (n->alternative_address.address,
3413 n->alternative_address.session,
3415 set_primary_address (n, n->alternative_address.address,
3416 n->alternative_address.session, n->alternative_address.bandwidth_in,
3417 n->alternative_address.bandwidth_out, GNUNET_YES);
3419 free_address (&n->alternative_address);
3420 send_session_ack_message (n);
3422 case GNUNET_TRANSPORT_PS_DISCONNECT:
3423 GNUNET_STATISTICS_update (GST_stats,
3425 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3427 return GNUNET_SYSERR;
3428 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3432 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3433 "Unhandled state `%s'\n",
3434 GNUNET_TRANSPORT_ps2s (n->state));
3436 return GNUNET_SYSERR;
3443 * A session was terminated. Take note; if needed, try to get
3444 * an alternative address from ATS.
3446 * @param peer identity of the peer where the session died
3447 * @param session session that is gone
3448 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3449 * this session was not in use
3452 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3453 struct Session *session)
3455 struct NeighbourMapEntry *n;
3456 struct BlackListCheckContext *bcc;
3457 struct BlackListCheckContext *bcc_next;
3459 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3461 while (NULL != (bcc = bcc_next))
3463 bcc_next = bcc->next;
3464 if (bcc->na.session == session)
3466 if (NULL != bcc->bc)
3467 GST_blacklist_test_cancel (bcc->bc);
3468 GNUNET_HELLO_address_free (bcc->na.address);
3469 GNUNET_CONTAINER_DLL_remove (bc_head,
3475 if (NULL == (n = lookup_neighbour (peer)))
3476 return GNUNET_NO; /* can't affect us */
3477 if (session != n->primary_address.session)
3479 if (session == n->alternative_address.session)
3481 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3482 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3483 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3486 free_address (&n->alternative_address);
3488 return GNUNET_NO; /* doesn't affect us further */
3491 n->expect_latency_response = GNUNET_NO;
3492 /* The session for neighbour's primary address died */
3495 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3497 free_neighbour (n, GNUNET_NO);
3499 case GNUNET_TRANSPORT_PS_INIT_ATS:
3501 free_neighbour (n, GNUNET_NO);
3503 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3504 /* The session used to send the CONNECT terminated:
3505 * this implies a connect error*/
3506 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3507 "Could not send CONNECT message with address `%s' session %p: session terminated, requesting new address\n",
3508 GST_plugins_a2s (n->primary_address.address), n->primary_address.session,
3510 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3511 unset_primary_address (n);
3512 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3513 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3515 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3516 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3517 /* error on inbound session; free neighbour entirely */
3518 free_address (&n->primary_address);
3519 free_neighbour (n, GNUNET_NO);
3521 case GNUNET_TRANSPORT_PS_CONNECTED:
3522 unset_primary_address (n);
3523 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3524 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3526 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3527 /* we don't have an address, how can it go down? */
3530 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3531 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3532 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3534 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3535 /* primary went down while we were checking secondary against
3536 blacklist, adopt secondary as primary */
3537 free_address (&n->primary_address);
3538 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3539 n->primary_address = n->alternative_address;
3540 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3542 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3543 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3544 secondary as primary */
3545 free_address (&n->primary_address);
3546 n->primary_address = n->alternative_address;
3547 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3548 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3550 case GNUNET_TRANSPORT_PS_DISCONNECT:
3551 free_address (&n->primary_address);
3553 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3554 /* neighbour was freed and plugins told to terminate session */
3558 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3559 "Unhandled state `%s'\n",
3560 GNUNET_TRANSPORT_ps2s (n->state));
3564 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3565 GNUNET_SCHEDULER_cancel (n->task);
3566 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3572 * We received a 'SESSION_ACK' message from the other peer.
3573 * If we sent a 'CONNECT_ACK' last, this means we are now
3574 * connected. Otherwise, do nothing.
3576 * @param message possibly a 'struct SessionConnectMessage' (check format)
3577 * @param peer identity of the peer to switch the address for
3578 * @param address address of the other peer, NULL if other peer
3580 * @param session session to use (or NULL)
3581 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3584 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3585 const struct GNUNET_PeerIdentity *peer,
3586 const struct GNUNET_HELLO_Address *address,
3587 struct Session *session)
3589 struct NeighbourMapEntry *n;
3591 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3592 "Received SESSION_ACK message from peer `%s'\n",
3594 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3596 GNUNET_break_op (0);
3597 return GNUNET_SYSERR;
3599 GNUNET_STATISTICS_update (GST_stats,
3601 ("# SESSION_ACK messages received"),
3603 if (NULL == (n = lookup_neighbour (peer)))
3605 GNUNET_break_op (0);
3606 return GNUNET_SYSERR;
3608 /* check if we are in a plausible state for having sent
3609 a CONNECT_ACK. If not, return, otherwise break */
3611 /* TODO I have no idea we we should state GNUNET_TRANSPORT_PS_CONNECT_SENT
3612 * Perhaps SWITCHING? Have to check */
3613 if ( /*(*/ (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) /* &&
3614 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3615 (ACK_SEND_SESSION_ACK != n->ack_state) */)
3617 if (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state)
3618 GNUNET_break (0); /* TESTING */
3620 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3621 "Received SESSION_ACK message from peer `%s' in state %s/%s\n",
3623 GNUNET_TRANSPORT_ps2s (n->state),
3624 print_ack_state (n->ack_state));
3625 GNUNET_STATISTICS_update (GST_stats,
3626 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3630 /* We are connected */
3631 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3632 GNUNET_STATISTICS_set (GST_stats,
3633 gettext_noop ("# peers connected"),
3634 ++neighbours_connected,
3637 /* Notify about connection */
3638 connect_notify_cb (callback_cls, &n->id,
3639 n->primary_address.bandwidth_in,
3640 n->primary_address.bandwidth_out);
3642 /* Add session to ATS since no session was given (NULL) and we may have
3643 * obtained a new session */
3644 GST_ats_add_address (n->primary_address.address, n->primary_address.session,
3647 /* Set primary address to used */
3648 set_primary_address (n,
3649 n->primary_address.address,
3650 n->primary_address.session,
3651 n->primary_address.bandwidth_in,
3652 n->primary_address.bandwidth_out,
3659 * Test if we're connected to the given peer.
3661 * @param target peer to test
3662 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3665 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3667 return test_connected (lookup_neighbour (target));
3671 * Change the incoming quota for the given peer.
3673 * @param neighbour identity of peer to change qutoa for
3674 * @param quota new quota
3677 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3678 struct GNUNET_BANDWIDTH_Value32NBO quota)
3680 struct NeighbourMapEntry *n;
3682 if (NULL == (n = lookup_neighbour (neighbour)))
3684 GNUNET_STATISTICS_update (GST_stats,
3686 ("# SET QUOTA messages ignored (no such peer)"),
3690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3691 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3692 ntohl (quota.value__), GNUNET_i2s (&n->id));
3693 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3694 if (0 != ntohl (quota.value__))
3696 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3697 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3698 GNUNET_i2s (&n->id));
3699 if (GNUNET_YES == test_connected (n))
3700 GNUNET_STATISTICS_update (GST_stats,
3701 gettext_noop ("# disconnects due to quota of 0"),
3703 disconnect_neighbour (n);
3708 * We received a disconnect message from the given peer,
3709 * validate and process.
3711 * @param peer sender of the message
3712 * @param msg the disconnect message
3715 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3716 const struct GNUNET_MessageHeader *msg)
3718 struct NeighbourMapEntry *n;
3719 const struct SessionDisconnectMessage *sdm;
3721 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3722 "Received DISCONNECT message from peer `%s'\n",
3724 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3726 // GNUNET_break_op (0);
3727 GNUNET_STATISTICS_update (GST_stats,
3729 ("# disconnect messages ignored (old format)"), 1,
3733 GNUNET_STATISTICS_update (GST_stats,
3735 ("# DISCONNECT messages received"),
3737 sdm = (const struct SessionDisconnectMessage *) msg;
3738 if (NULL == (n = lookup_neighbour (peer)))
3739 return; /* gone already */
3740 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3742 GNUNET_STATISTICS_update (GST_stats,
3744 ("# disconnect messages ignored (timestamp)"), 1,
3748 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3750 GNUNET_break_op (0);
3753 if (ntohl (sdm->purpose.size) !=
3754 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3755 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3756 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3758 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3759 "%s message from peer `%s' has invalid size \n",
3762 GNUNET_break_op (0);
3766 GNUNET_CRYPTO_eddsa_verify
3767 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3768 &sdm->signature, &sdm->public_key))
3770 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3771 "%s message from peer `%s' cannot be verified \n",
3774 GNUNET_break_op (0);
3777 if (GNUNET_YES == test_connected (n))
3778 GNUNET_STATISTICS_update (GST_stats,
3780 ("# other peer asked to disconnect from us"), 1,
3782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3783 "Disconnecting by request from peer %s\n",
3785 disconnect_neighbour (n);
3790 * Closure for the neighbours_iterate function.
3792 struct IteratorContext
3795 * Function to call on each connected neighbour.
3797 GST_NeighbourIterator cb;
3807 * Call the callback from the closure for each neighbour.
3809 * @param cls the `struct IteratorContext`
3810 * @param key the hash of the public key of the neighbour
3811 * @param value the `struct NeighbourMapEntry`
3812 * @return #GNUNET_OK (continue to iterate)
3815 neighbours_iterate (void *cls,
3816 const struct GNUNET_PeerIdentity *key,
3819 struct IteratorContext *ic = cls;
3820 struct NeighbourMapEntry *n = value;
3821 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3822 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3825 if (NULL != n->primary_address.address)
3827 bandwidth_in = n->primary_address.bandwidth_in;
3828 bandwidth_out = n->primary_address.bandwidth_out;
3832 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3833 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3837 n->primary_address.address,
3840 bandwidth_in, bandwidth_out);
3846 * Iterate over all connected neighbours.
3848 * @param cb function to call
3849 * @param cb_cls closure for cb
3852 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3854 struct IteratorContext ic;
3856 if (NULL == neighbours)
3857 return; /* can happen during shutdown */
3860 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3865 * If we have an active connection to the given target, it must be shutdown.
3867 * @param target peer to disconnect from
3870 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3872 struct NeighbourMapEntry *n;
3874 if (NULL == (n = lookup_neighbour (target)))
3875 return; /* not active */
3876 if (GNUNET_YES == test_connected (n))
3877 GNUNET_STATISTICS_update (GST_stats,
3879 ("# disconnected from peer upon explicit request"), 1,
3881 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3882 "Forced disconnect from peer %s\n",
3883 GNUNET_i2s (target));
3884 disconnect_neighbour (n);
3889 * Obtain current latency information for the given neighbour.
3891 * @param peer to get the latency for
3892 * @return observed latency of the address, FOREVER if the
3893 * the connection is not up
3895 struct GNUNET_TIME_Relative
3896 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3898 struct NeighbourMapEntry *n;
3900 n = lookup_neighbour (peer);
3902 return GNUNET_TIME_UNIT_FOREVER_REL;
3905 case GNUNET_TRANSPORT_PS_CONNECTED:
3906 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3907 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3908 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3909 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3910 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3912 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3913 case GNUNET_TRANSPORT_PS_INIT_ATS:
3914 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3915 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3916 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3917 case GNUNET_TRANSPORT_PS_DISCONNECT:
3918 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3919 return GNUNET_TIME_UNIT_FOREVER_REL;
3921 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3922 "Unhandled state `%s'\n",
3923 GNUNET_TRANSPORT_ps2s (n->state));
3927 return GNUNET_TIME_UNIT_FOREVER_REL;
3932 * Obtain current address information for the given neighbour.
3935 * @return address currently used
3937 struct GNUNET_HELLO_Address *
3938 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3940 struct NeighbourMapEntry *n;
3942 n = lookup_neighbour (peer);
3945 return n->primary_address.address;
3950 * Initialize the neighbours subsystem.
3952 * @param cls closure for callbacks
3953 * @param connect_cb function to call if we connect to a peer
3954 * @param disconnect_cb function to call if we disconnect from a peer
3955 * @param peer_address_cb function to call if we change an active address
3957 * @param max_fds maximum number of fds to use
3960 GST_neighbours_start (void *cls,
3961 NotifyConnect connect_cb,
3962 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3963 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3964 unsigned int max_fds)
3967 connect_notify_cb = connect_cb;
3968 disconnect_notify_cb = disconnect_cb;
3969 neighbour_change_cb = peer_address_cb;
3970 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3971 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3972 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3973 utilization_transmission, NULL);
3978 * Disconnect from the given neighbour.
3981 * @param key hash of neighbour's public key (not used)
3982 * @param value the 'struct NeighbourMapEntry' of the neighbour
3983 * @return #GNUNET_OK (continue to iterate)
3986 disconnect_all_neighbours (void *cls,
3987 const struct GNUNET_PeerIdentity *key,
3990 struct NeighbourMapEntry *n = value;
3992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3993 "Disconnecting peer `%4s', %s\n",
3994 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3995 free_neighbour (n, GNUNET_NO);
4001 * Cleanup the neighbours subsystem.
4004 GST_neighbours_stop ()
4006 struct BlacklistCheckSwitchContext *cur;
4007 struct BlacklistCheckSwitchContext *next;
4009 if (NULL == neighbours)
4011 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
4013 GNUNET_SCHEDULER_cancel (util_transmission_tk);
4014 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
4017 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
4018 &disconnect_all_neighbours,
4020 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
4022 next = pending_bc_head;
4023 for (cur = next; NULL != cur; cur = next )
4026 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
4028 if (NULL != cur->blc)
4030 GST_blacklist_test_cancel (cur->blc);
4033 if (NULL != cur->address)
4034 GNUNET_HELLO_address_free (cur->address);
4035 GNUNET_free_non_null (cur->ats);
4039 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
4040 &free_notification_cb, NULL);
4041 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
4042 registered_quota_notifications = NULL;
4045 callback_cls = NULL;
4046 connect_notify_cb = NULL;
4047 disconnect_notify_cb = NULL;
4048 neighbour_change_cb = NULL;
4052 /* end of file gnunet-service-transport_neighbours.c */