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_SENT:
1110 /* we are currently connected, need to send disconnect and do
1111 internal notifications and update statistics */
1112 send_disconnect (n);
1113 GNUNET_STATISTICS_set (GST_stats,
1114 gettext_noop ("# peers connected"),
1115 --neighbours_connected,
1117 disconnect_notify_cb (callback_cls, &n->id);
1118 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1120 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1121 /* ATS address request timeout, disconnect without sending disconnect message */
1122 GNUNET_STATISTICS_set (GST_stats,
1123 gettext_noop ("# peers connected"),
1124 --neighbours_connected,
1126 disconnect_notify_cb (callback_cls, &n->id);
1127 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1129 case GNUNET_TRANSPORT_PS_DISCONNECT:
1130 /* already disconnected, ignore */
1132 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1133 /* already cleaned up, how did we get here!? */
1137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1138 "Unhandled state `%s'\n",
1139 GNUNET_TRANSPORT_ps2s (n->state));
1143 /* schedule timeout to clean up */
1144 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1145 GNUNET_SCHEDULER_cancel (n->task);
1146 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1152 * We're done with our transmission attempt, continue processing.
1154 * @param cls the `struct MessageQueue` of the message
1155 * @param receiver intended receiver
1156 * @param success whether it worked or not
1157 * @param size_payload bytes payload sent
1158 * @param physical bytes sent on wire
1161 transmit_send_continuation (void *cls,
1162 const struct GNUNET_PeerIdentity *receiver,
1163 int success, size_t size_payload, size_t physical)
1165 struct MessageQueue *mq = cls;
1166 struct NeighbourMapEntry *n;
1168 if (NULL == (n = lookup_neighbour (receiver)))
1171 return; /* disconnect or other error while transmitting, can happen */
1173 if (n->is_active == mq)
1175 /* this is still "our" neighbour, remove us from its queue
1176 and allow it to send the next message now */
1177 n->is_active = NULL;
1178 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1179 GNUNET_SCHEDULER_cancel (n->task);
1180 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1182 if (bytes_in_send_queue < mq->message_buf_size)
1184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1185 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1186 bytes_in_send_queue, mq->message_buf_size,
1187 (GNUNET_OK == success) ? "OK" : "FAIL",
1188 size_payload, physical);
1193 GNUNET_break (size_payload == mq->message_buf_size);
1194 bytes_in_send_queue -= mq->message_buf_size;
1195 GNUNET_STATISTICS_set (GST_stats,
1197 ("# bytes in message queue for other peers"),
1198 bytes_in_send_queue, GNUNET_NO);
1199 if (GNUNET_OK == success)
1200 GNUNET_STATISTICS_update (GST_stats,
1202 ("# messages transmitted to other peers"),
1205 GNUNET_STATISTICS_update (GST_stats,
1207 ("# transmission failures for messages to other peers"),
1209 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1210 "Sending message to `%s' of type %u was a %s\n",
1211 GNUNET_i2s (receiver),
1212 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1213 (success == GNUNET_OK) ? "success" : "FAILURE");
1214 if (NULL != mq->cont)
1215 mq->cont (mq->cont_cls, success, size_payload, physical);
1221 * Check the message list for the given neighbour and if we can
1222 * send a message, do so. This function should only be called
1223 * if the connection is at least generally ready for transmission.
1224 * While we will only send one message at a time, no bandwidth
1225 * quota management is performed here. If a message was given to
1226 * the plugin, the continuation will automatically re-schedule
1227 * the 'master' task once the next message might be transmitted.
1229 * @param n target peer for which to transmit
1232 try_transmission_to_peer (struct NeighbourMapEntry *n)
1234 struct MessageQueue *mq;
1235 struct GNUNET_TIME_Relative timeout;
1237 if (NULL == n->primary_address.address)
1239 /* no address, why are we here? */
1243 if ((0 == n->primary_address.address->address_length) &&
1244 (NULL == n->primary_address.session))
1246 /* no address, why are we here? */
1250 if (NULL != n->is_active)
1252 /* transmission already pending */
1256 /* timeout messages from the queue that are past their due date */
1257 while (NULL != (mq = n->messages_head))
1259 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1260 if (timeout.rel_value_us > 0)
1262 GNUNET_STATISTICS_update (GST_stats,
1264 ("# messages timed out while in transport queue"),
1266 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1268 transmit_send_continuation (mq, &n->id,
1270 mq->message_buf_size, 0); /* timeout */
1273 return; /* no more messages */
1274 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1276 (void) send_with_session (n,
1277 mq->message_buf, mq->message_buf_size,
1278 0 /* priority */, timeout, GNUNET_NO,
1279 &transmit_send_continuation, mq);
1284 * Send keepalive message to the neighbour. Must only be called
1285 * if we are on 'connected' state or while trying to switch addresses.
1286 * Will internally determine if a keepalive is truly needed (so can
1287 * always be called).
1289 * @param n neighbour that went idle and needs a keepalive
1292 send_keepalive (struct NeighbourMapEntry *n)
1294 struct SessionKeepAliveMessage m;
1295 struct GNUNET_TIME_Relative timeout;
1298 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1299 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1300 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1301 return; /* no keepalive needed at this time */
1303 nonce = 0; /* 0 indicates 'not set' */
1305 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1307 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1308 "Sending keep alive to peer `%s' with nonce %u\n",
1309 GNUNET_i2s (&n->id), nonce);
1311 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1312 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1313 m.nonce = htonl (nonce);
1315 timeout = send_with_session (n,
1316 (const void *) &m, sizeof (m),
1317 UINT32_MAX /* priority */,
1318 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1320 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1322 n->primary_address.keep_alive_nonce = nonce;
1323 n->expect_latency_response = GNUNET_YES;
1324 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1325 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1331 * Keep the connection to the given neighbour alive longer,
1332 * we received a KEEPALIVE (or equivalent); send a response.
1334 * @param neighbour neighbour to keep alive (by sending keep alive response)
1335 * @param m the keep alive message containing the nonce to respond to
1338 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1339 const struct GNUNET_MessageHeader *m)
1341 struct NeighbourMapEntry *n;
1342 const struct SessionKeepAliveMessage *msg_in;
1343 struct SessionKeepAliveMessage msg;
1345 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1348 msg_in = (struct SessionKeepAliveMessage *) m;
1349 if (NULL == (n = lookup_neighbour (neighbour)))
1351 GNUNET_STATISTICS_update (GST_stats,
1353 ("# KEEPALIVE messages discarded (peer unknown)"),
1357 if (NULL == n->primary_address.session)
1359 GNUNET_STATISTICS_update (GST_stats,
1361 ("# KEEPALIVE messages discarded (no session)"),
1366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1367 "Received keep alive request from peer `%s' with nonce %u\n",
1368 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1370 /* send reply to allow neighbour to measure latency */
1371 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1372 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1373 msg.nonce = msg_in->nonce;
1374 (void) send_with_session(n,
1375 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1376 UINT32_MAX /* priority */,
1377 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1383 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1384 * latency to this peer. Pass the updated information (existing ats
1385 * plus calculated latency) to ATS.
1387 * @param neighbour neighbour to keep alive
1388 * @param m the message containing the keep alive response
1391 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1392 const struct GNUNET_MessageHeader *m)
1394 struct NeighbourMapEntry *n;
1395 const struct SessionKeepAliveMessage *msg;
1396 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1398 struct GNUNET_ATS_Information ats;
1400 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1403 msg = (const struct SessionKeepAliveMessage *) m;
1404 if (NULL == (n = lookup_neighbour (neighbour)))
1406 GNUNET_STATISTICS_update (GST_stats,
1408 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1412 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1413 (GNUNET_YES != n->expect_latency_response) )
1415 GNUNET_STATISTICS_update (GST_stats,
1417 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1421 if (NULL == n->primary_address.address)
1423 GNUNET_STATISTICS_update (GST_stats,
1425 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1429 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1431 GNUNET_STATISTICS_update (GST_stats,
1433 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1440 "Received keep alive response from peer `%s' for session %p\n",
1441 GNUNET_i2s (&n->id), n->primary_address.session);
1445 /* Update session timeout here */
1446 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1448 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1449 "Updating session for peer `%s' for session %p\n",
1450 GNUNET_i2s (&n->id), n->primary_address.session);
1451 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1458 n->primary_address.keep_alive_nonce = 0;
1459 n->expect_latency_response = GNUNET_NO;
1460 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1461 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1464 "Latency for peer `%s' is %s\n",
1465 GNUNET_i2s (&n->id),
1466 GNUNET_STRINGS_relative_time_to_string (n->latency,
1468 /* append latency */
1469 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1470 if (n->latency.rel_value_us > UINT32_MAX)
1471 latency = UINT32_MAX;
1473 latency = n->latency.rel_value_us;
1474 ats.value = htonl (latency);
1475 GST_ats_update_metrics (&n->id, n->primary_address.address,
1476 n->primary_address.session, &ats, 1);
1481 * We have received a message from the given sender. How long should
1482 * we delay before receiving more? (Also used to keep the peer marked
1485 * @param sender sender of the message
1486 * @param size size of the message
1487 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1488 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1489 * #GNUNET_SYSERR if the connection is not fully up yet
1490 * @return how long to wait before reading more from this sender
1492 struct GNUNET_TIME_Relative
1493 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1494 *sender, ssize_t size, int *do_forward)
1496 struct NeighbourMapEntry *n;
1497 struct GNUNET_TIME_Relative ret;
1499 if (NULL == neighbours)
1501 *do_forward = GNUNET_NO;
1502 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1504 if (NULL == (n = lookup_neighbour (sender)))
1506 GST_neighbours_try_connect (sender);
1507 if (NULL == (n = lookup_neighbour (sender)))
1509 GNUNET_STATISTICS_update (GST_stats,
1511 ("# messages discarded due to lack of neighbour record"),
1513 *do_forward = GNUNET_NO;
1514 return GNUNET_TIME_UNIT_ZERO;
1517 if (! test_connected (n))
1519 *do_forward = GNUNET_SYSERR;
1520 return GNUNET_TIME_UNIT_ZERO;
1522 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1524 n->quota_violation_count++;
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1527 n->in_tracker.available_bytes_per_s__,
1528 n->quota_violation_count);
1529 /* Discount 32k per violation */
1530 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1534 if (n->quota_violation_count > 0)
1536 /* try to add 32k back */
1537 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1538 n->quota_violation_count--;
1541 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1543 GNUNET_STATISTICS_update (GST_stats,
1545 ("# bandwidth quota violations by other peers"),
1547 *do_forward = GNUNET_NO;
1548 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1550 *do_forward = GNUNET_YES;
1551 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1552 if (ret.rel_value_us > 0)
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1556 (unsigned long long) n->in_tracker.
1557 consumption_since_last_update__,
1558 (unsigned int) n->in_tracker.available_bytes_per_s__,
1559 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1560 GNUNET_STATISTICS_update (GST_stats,
1561 gettext_noop ("# ms throttling suggested"),
1562 (int64_t) ret.rel_value_us / 1000LL,
1570 * Transmit a message to the given target using the active connection.
1572 * @param target destination
1573 * @param msg message to send
1574 * @param msg_size number of bytes in msg
1575 * @param timeout when to fail with timeout
1576 * @param cont function to call when done
1577 * @param cont_cls closure for 'cont'
1580 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1581 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1582 GST_NeighbourSendContinuation cont, void *cont_cls)
1584 struct NeighbourMapEntry *n;
1585 struct MessageQueue *mq;
1587 /* All ove these cases should never happen; they are all API violations.
1588 But we check anyway, just to be sure. */
1589 if (NULL == (n = lookup_neighbour (target)))
1593 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1596 if (GNUNET_YES != test_connected (n))
1600 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1603 bytes_in_send_queue += msg_size;
1604 GNUNET_STATISTICS_set (GST_stats,
1606 ("# bytes in message queue for other peers"),
1607 bytes_in_send_queue, GNUNET_NO);
1608 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1610 mq->cont_cls = cont_cls;
1611 memcpy (&mq[1], msg, msg_size);
1612 mq->message_buf = (const char *) &mq[1];
1613 mq->message_buf_size = msg_size;
1614 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1615 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1616 if ( (NULL != n->is_active) ||
1617 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1619 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1620 GNUNET_SCHEDULER_cancel (n->task);
1621 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1625 send_session_connect_cont (void *cls,
1626 const struct GNUNET_PeerIdentity *target,
1628 size_t size_payload,
1629 size_t size_on_wire)
1631 struct NeighbourMapEntry *n;
1633 n = lookup_neighbour (target);
1636 /* CONNECT continuation was called after neighbor was freed,
1637 * for example due to a time out for the state or the session
1638 * used was already terminated: nothing to do here... */
1642 if ( (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) &&
1643 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) )
1645 /* CONNECT continuation was called after neighbor changed state,
1646 * for example due to a time out for the state or the session
1647 * used was already terminated: nothing to do here... */
1650 if (GNUNET_OK == result)
1653 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1654 _("Failed to send CONNECT message to peer `%s' using address `%s' session %p\n"),
1655 GNUNET_i2s (target),
1656 GST_plugins_a2s (n->primary_address.address),
1657 n->primary_address.session);
1659 /* Failed to send CONNECT message with this address */
1660 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1661 n->primary_address.session);
1662 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1665 /* Remove address and request and additional one */
1666 unset_primary_address (n);
1668 if (n->state == GNUNET_TRANSPORT_PS_RECONNECT_SENT)
1670 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1671 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1674 else if (n->state == GNUNET_TRANSPORT_PS_INIT_ATS)
1676 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1677 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1682 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1683 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1690 * Send a SESSION_CONNECT message via the given address.
1692 * @param na address to use
1695 send_session_connect (struct NeighbourAddress *na)
1697 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1698 struct SessionConnectMessage connect_msg;
1699 struct NeighbourMapEntry *n;
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 "Sending SESSION_CONNECT message to peer %s\n",
1703 GNUNET_i2s (&na->address->peer));
1704 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1709 if (NULL == na->session)
1710 na->session = papi->get_session (papi->cls, na->address);
1711 if (NULL == na->session)
1716 GNUNET_STATISTICS_update (GST_stats,
1718 ("# SESSION_CONNECT messages sent"),
1720 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1721 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1722 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1723 connect_msg.reserved = htonl (0);
1724 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1726 papi->send (papi->cls,
1728 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1730 SETUP_CONNECTION_TIMEOUT,
1731 send_session_connect_cont, NULL))
1733 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1734 _("Failed to transmit CONNECT message via plugin to %s\n"),
1735 GST_plugins_a2s (na->address));
1737 n = lookup_neighbour (&na->address->peer);
1743 /* Hard failure to send the CONNECT message with this address:
1744 Destroy address and session */
1745 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1746 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1748 /* Remove address and request and additional one */
1749 unset_primary_address (n);
1750 if (n->state == GNUNET_TRANSPORT_PS_RECONNECT_SENT)
1752 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1753 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1756 else if (n->state == GNUNET_TRANSPORT_PS_INIT_ATS)
1758 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1759 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1764 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1765 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1769 GST_neighbours_notify_data_sent (&na->address->peer,
1772 sizeof (struct SessionConnectMessage));
1777 send_session_connect_ack_cont (void *cls,
1778 const struct GNUNET_PeerIdentity *target,
1780 size_t size_payload,
1781 size_t size_on_wire)
1783 struct NeighbourMapEntry *n;
1785 n = lookup_neighbour (target);
1788 /* CONNECT_ACK continuation was called after neighbor was freed,
1789 * for example due to a time out for the state or the session
1790 * used was already terminated: nothing to do here... */
1794 if (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state)
1796 /* CONNECT_ACK continuation was called after neighbor changed state,
1797 * for example due to a time out for the state or the session
1798 * used was already terminated: nothing to do here... */
1801 if (GNUNET_OK == result)
1804 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1805 _("Failed to send CONNECT_ACK message to peer `%s' using address `%s' session %p\n"),
1806 GNUNET_i2s (target),
1807 GST_plugins_a2s (n->primary_address.address),
1808 n->primary_address.session);
1810 /* Failed to send CONNECT_ACK message with this address */
1811 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1812 n->primary_address.session);
1813 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1816 /* Remove address and request and additional one */
1817 unset_primary_address (n);
1818 n->ack_state = ACK_SEND_CONNECT_ACK;
1819 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1820 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1826 * Send a CONNECT_ACK message via the given address.
1828 * @param address address to use
1829 * @param session session to use
1830 * @param timestamp timestamp to use for the ACK message
1833 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1834 struct Session *session,
1835 struct GNUNET_TIME_Absolute timestamp)
1837 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1838 struct SessionConnectMessage connect_msg;
1839 struct NeighbourMapEntry *n;
1841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1842 "Sending CONNECT_ACK to peer `%s'\n",
1843 GNUNET_i2s (&address->peer));
1844 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1849 if (NULL == session)
1850 session = papi->get_session (papi->cls, address);
1851 if (NULL == session)
1856 GNUNET_STATISTICS_update (GST_stats,
1858 ("# CONNECT_ACK messages sent"),
1860 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1861 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1862 connect_msg.reserved = htonl (0);
1863 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1865 if (GNUNET_SYSERR == papi->send (papi->cls,
1867 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1869 GNUNET_TIME_UNIT_FOREVER_REL,
1870 send_session_connect_ack_cont, NULL))
1872 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1873 _("Failed to transmit CONNECT_ACK message via plugin to %s\n"),
1874 GST_plugins_a2s (address));
1876 n = lookup_neighbour (&address->peer);
1882 /* Hard failure to send the CONNECT_ACK message with this address:
1883 Destroy session (and address) */
1884 if (GNUNET_YES == GNUNET_HELLO_address_check_option(address,
1885 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1887 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1888 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1891 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1893 /* Remove address and request and additional one */
1894 unset_primary_address (n);
1895 n->ack_state = ACK_SEND_CONNECT_ACK;
1896 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1897 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1903 struct QuotaNotificationRequest
1905 struct GNUNET_PeerIdentity peer;
1906 struct Session *session;
1910 struct QNR_LookContext
1912 struct GNUNET_PeerIdentity peer;
1913 struct Session *session;
1916 struct QuotaNotificationRequest *res;
1920 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1922 struct QNR_LookContext *qnr_ctx = cls;
1923 struct QuotaNotificationRequest *qnr = value;
1925 if ((qnr->session == qnr_ctx->session) &&
1926 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1927 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1929 qnr_ctx->res = value;
1936 GST_neighbours_register_quota_notification(void *cls,
1937 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1938 struct Session *session)
1940 struct QuotaNotificationRequest *qnr;
1941 struct QNR_LookContext qnr_ctx;
1943 if (NULL == registered_quota_notifications)
1945 return; /* init or shutdown */
1948 qnr_ctx.peer = (*peer);
1949 qnr_ctx.plugin = plugin;
1950 qnr_ctx.session = session;
1953 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1954 peer, &find_notification_request, &qnr_ctx);
1955 if (NULL != qnr_ctx.res)
1961 qnr = GNUNET_new (struct QuotaNotificationRequest);
1962 qnr->peer = (*peer);
1963 qnr->plugin = GNUNET_strdup (plugin);
1964 qnr->session = session;
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "Adding notification for peer `%s' plugin `%s' session %p \n",
1968 GNUNET_i2s (peer), plugin, session);
1970 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1971 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1976 GST_neighbours_unregister_quota_notification(void *cls,
1977 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1979 struct QNR_LookContext qnr_ctx;
1981 if (NULL == registered_quota_notifications)
1983 return; /* init or shutdown */
1986 qnr_ctx.peer = (*peer);
1987 qnr_ctx.plugin = plugin;
1988 qnr_ctx.session = session;
1991 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1992 &find_notification_request, &qnr_ctx);
1993 if (NULL == qnr_ctx.res)
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000 "Removing notification for peer `%s' plugin `%s' session %p \n",
2001 GNUNET_i2s (peer), plugin, session);
2003 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
2005 GNUNET_free (qnr_ctx.res->plugin);
2006 GNUNET_free (qnr_ctx.res);
2010 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2012 /* struct NeighbourMapEntry *n = cls; */
2013 struct QuotaNotificationRequest *qnr = value;
2014 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2015 struct GNUNET_TIME_Relative delay;
2018 papi = GST_plugins_find(qnr->plugin);
2025 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
2026 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2027 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
2028 delay.rel_value_us / 1000);
2030 if (NULL != papi->update_inbound_delay)
2031 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
2037 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
2040 /* struct NeighbourMapEntry *n = cls; */
2041 struct QuotaNotificationRequest *qnr = value;
2043 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
2045 GNUNET_free(qnr->plugin);
2052 inbound_bw_tracker_update(void *cls)
2054 struct NeighbourMapEntry *n = cls;
2056 /* Quota was updated, tell plugins to update the time to receive next */
2057 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2058 &n->id, ¬ification_cb, n);
2063 * Create a fresh entry in the neighbour map for the given peer
2065 * @param peer peer to create an entry for
2066 * @return new neighbour map entry
2068 static struct NeighbourMapEntry *
2069 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2071 struct NeighbourMapEntry *n;
2073 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2074 "Creating new neighbour entry for `%s'\n",
2076 n = GNUNET_new (struct NeighbourMapEntry);
2078 n->ack_state = ACK_UNDEFINED;
2079 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2080 n->last_util_transmission = GNUNET_TIME_absolute_get();
2081 n->util_payload_bytes_recv = 0;
2082 n->util_payload_bytes_sent = 0;
2083 n->util_total_bytes_recv = 0;
2084 n->util_total_bytes_sent = 0;
2085 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
2086 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2087 MAX_BANDWIDTH_CARRY_S);
2088 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2089 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2090 GNUNET_assert (GNUNET_OK ==
2091 GNUNET_CONTAINER_multipeermap_put (neighbours,
2093 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2097 /* We received a address suggestion after requesting an address in
2098 * try_connect or after receiving a connect, switch to address
2101 address_suggest_cont (void *cls,
2102 const struct GNUNET_PeerIdentity *peer,
2103 const struct GNUNET_HELLO_Address *address, struct Session *session,
2104 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2105 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2106 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2108 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2109 bandwidth_in, bandwidth_out);
2113 struct BlacklistCheckSwitchContext
2115 struct BlacklistCheckSwitchContext *prev;
2116 struct BlacklistCheckSwitchContext *next;
2119 struct GST_BlacklistCheck *blc;
2121 struct GNUNET_HELLO_Address *address;
2122 struct Session *session;
2123 struct GNUNET_ATS_Information *ats;
2126 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2127 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2131 * Black list check result for try_connect call
2132 * If connection to the peer is allowed request adddress and
2134 * @param cls blc_ctx bl context
2135 * @param peer the peer
2136 * @param result the result
2139 try_connect_bl_check_cont (void *cls,
2140 const struct GNUNET_PeerIdentity *peer, int result)
2142 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2143 struct NeighbourMapEntry *n;
2145 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2146 GNUNET_free (blc_ctx);
2148 if (GNUNET_OK != result)
2150 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2151 _("Blacklisting disapproved to connect to peer `%s'\n"),
2156 /* Setup a new neighbour */
2157 n = setup_neighbour (peer);
2159 /* Request address suggestions for this peer */
2160 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2161 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2162 GNUNET_ATS_reset_backoff (GST_ats, peer);
2163 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2164 &address_suggest_cont, n);
2170 * Try to create a connection to the given target (eventually).
2172 * @param target peer to try to connect to
2175 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2177 struct NeighbourMapEntry *n;
2178 struct GST_BlacklistCheck *blc;
2179 struct BlacklistCheckSwitchContext *blc_ctx;
2181 if (NULL == neighbours)
2183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2184 "Asked to connect to peer `%s' during shutdown\n",
2185 GNUNET_i2s (target));
2186 return; /* during shutdown, do nothing */
2188 n = lookup_neighbour (target);
2189 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2190 "Asked to connect to peer `%s' (state: %s)\n",
2191 GNUNET_i2s (target),
2192 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2197 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2198 /* this should not be possible */
2200 free_neighbour (n, GNUNET_NO);
2202 case GNUNET_TRANSPORT_PS_INIT_ATS:
2203 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2204 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2205 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2206 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2207 "Ignoring request to try to connect to `%s', already trying!\n",
2208 GNUNET_i2s (target));
2209 return; /* already trying */
2210 case GNUNET_TRANSPORT_PS_CONNECTED:
2211 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2212 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2213 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2214 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2215 "Ignoring request to try to connect, already connected to `%s'!\n",
2216 GNUNET_i2s (target));
2217 return; /* already connected */
2218 case GNUNET_TRANSPORT_PS_DISCONNECT:
2219 /* get rid of remains, ready to re-try immediately */
2220 free_neighbour (n, GNUNET_NO);
2222 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2223 /* should not be possible */
2227 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2228 "Unhandled state `%s'\n",
2229 GNUNET_TRANSPORT_ps2s (n->state));
2231 free_neighbour (n, GNUNET_NO);
2236 /* Do blacklist check if connecting to this peer is allowed */
2237 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2238 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2240 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2241 &try_connect_bl_check_cont, blc_ctx)))
2249 * Function called with the result of a blacklist check.
2251 * @param cls closure with the `struct BlackListCheckContext`
2252 * @param peer peer this check affects
2253 * @param result #GNUNET_OK if the address is allowed
2256 handle_connect_blacklist_check_cont (void *cls,
2257 const struct GNUNET_PeerIdentity *peer,
2260 struct BlackListCheckContext *bcc = cls;
2261 struct NeighbourMapEntry *n;
2264 GNUNET_CONTAINER_DLL_remove (bc_head,
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2270 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2272 if (NULL == (n = lookup_neighbour (peer)))
2274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2275 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2277 goto cleanup; /* nobody left to care about new address */
2280 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2281 "Blacklist check after CONNECT for peer `%s' in state %s/%s: %s\n",
2283 GNUNET_TRANSPORT_ps2s (n->state),
2284 print_ack_state (n->ack_state),
2285 (GNUNET_OK == result) ? "OK" : "FAIL");
2287 if (GNUNET_OK == result)
2289 /* Blacklist agreed on connecting to a peer with this address, notify ATS */
2290 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2291 "Notifying ATS peer's `%s' %s address `%s' session %p\n",
2293 (GNUNET_YES == GNUNET_HELLO_address_check_option(bcc->na.address,
2294 GNUNET_HELLO_ADDRESS_INFO_INBOUND)) ? "inbound" : "outbound",
2295 GST_plugins_a2s (bcc->na.address), bcc->na.session);
2296 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2301 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2302 /* This should not be possible */
2304 free_neighbour (n, GNUNET_NO);
2306 case GNUNET_TRANSPORT_PS_INIT_ATS:
2307 /* Waiting on ATS suggestion */
2309 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2311 /* TODO Why should I send an connect ACK message */
2312 /* waiting on CONNECT_ACK, send ACK if one is pending */
2314 if ( (GNUNET_OK == result) &&
2315 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2317 n->ack_state = ACK_SEND_SESSION_ACK;
2318 send_connect_ack_message (n->primary_address.address,
2319 n->primary_address.session,
2320 n->connect_ack_timestamp);
2324 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2325 /* waiting on ATS suggestion, don't care about blacklist */
2327 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2328 /* waiting on SESSION_ACK, send ACK if one is pending */
2329 if ( (GNUNET_OK == result) &&
2330 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2332 /* TODO: Why should this happen? */
2333 /* *Debug message: */ GNUNET_break (0);
2335 n->ack_state = ACK_SEND_SESSION_ACK;
2336 send_connect_ack_message (n->primary_address.address,
2337 n->primary_address.session,
2338 n->connect_ack_timestamp);
2341 case GNUNET_TRANSPORT_PS_CONNECTED:
2342 /* already connected, don't care about blacklist */
2344 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2345 /* still waiting on ATS suggestion, don't care about blacklist */
2347 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2348 /* waiting on CONNECT_ACK, don't care about blacklist */
2349 if ( (GNUNET_OK == result) &&
2350 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2352 n->ack_state = ACK_SEND_SESSION_ACK;
2353 send_connect_ack_message (n->primary_address.address,
2354 n->primary_address.session,
2355 n->connect_ack_timestamp);
2358 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2359 /* waiting on CONNECT_ACK, don't care about blacklist */
2360 if ( (GNUNET_OK == result) &&
2361 (ACK_SEND_CONNECT_ACK == n->ack_state) )
2363 n->ack_state = ACK_SEND_SESSION_ACK;
2364 send_connect_ack_message (n->primary_address.address,
2365 n->primary_address.session,
2366 n->connect_ack_timestamp);
2369 case GNUNET_TRANSPORT_PS_DISCONNECT:
2370 /* Nothing to do here, ATS will already do what can be done */
2372 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2373 /* should not be possible */
2377 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2378 "Unhandled state `%s'\n",
2379 GNUNET_TRANSPORT_ps2s (n->state));
2381 free_neighbour (n, GNUNET_NO);
2385 GNUNET_HELLO_address_free (bcc->na.address);
2391 * We received a CONNECT message and want to know if connecting to a particular
2392 * peer via a particular address is allowed. Check it!
2394 * @param peer identity of the peer to switch the address for
2395 * @param ts time at which the check was initiated
2396 * @param address address of the other peer, NULL if other peer
2398 * @param session session to use (or NULL)
2401 connect_check_blacklist (const struct GNUNET_PeerIdentity *peer,
2402 struct GNUNET_TIME_Absolute ts,
2403 const struct GNUNET_HELLO_Address *address,
2404 struct Session *session)
2406 struct BlackListCheckContext *bcc;
2407 struct GST_BlacklistCheck *bc;
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "Checking peer `%s' against blacklist\n",
2412 bcc = GNUNET_new (struct BlackListCheckContext);
2413 bcc->na.address = GNUNET_HELLO_address_copy (address);
2414 bcc->na.session = session;
2415 bcc->na.connect_timestamp = ts;
2416 GNUNET_CONTAINER_DLL_insert (bc_head,
2419 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2420 (NULL != address) ? address->transport_name : NULL,
2421 &handle_connect_blacklist_check_cont, bcc)))
2423 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2424 we must only store 'bc' if 'bc' is non-NULL... */
2429 * We received a 'SESSION_CONNECT' message from the other peer.
2430 * Consider switching to it.
2432 * @param message possibly a 'struct SessionConnectMessage' (check format)
2433 * @param peer identity of the peer to switch the address for
2434 * @param address address of the other peer, NULL if other peer
2436 * @param session session to use (or NULL)
2437 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2440 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2441 const struct GNUNET_PeerIdentity *peer,
2442 const struct GNUNET_HELLO_Address *address,
2443 struct Session *session)
2445 const struct SessionConnectMessage *scm;
2446 struct NeighbourMapEntry *n;
2447 struct GNUNET_TIME_Absolute ts;
2449 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2450 "Received CONNECT message from peer `%s' with `%s' %p\n",
2451 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
2452 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2454 GNUNET_break_op (0);
2455 return GNUNET_SYSERR;
2457 GNUNET_STATISTICS_update (GST_stats,
2459 ("# CONNECT messages received"),
2461 if (NULL == neighbours)
2463 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2464 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2466 return GNUNET_OK; /* we're shutting down */
2468 scm = (const struct SessionConnectMessage *) message;
2469 GNUNET_break_op (0 == ntohl (scm->reserved));
2470 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2471 n = lookup_neighbour (peer);
2474 /* This is a new neighbour and set to not connected */
2475 n = setup_neighbour (peer);
2478 /* Remember this CONNECT message in neighbour */
2479 n->ack_state = ACK_SEND_CONNECT_ACK;
2480 n->connect_ack_timestamp = ts;
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483 "Received CONNECT for peer `%s' in state %s/%s\n",
2485 GNUNET_TRANSPORT_ps2s (n->state),
2486 print_ack_state (n->ack_state));
2490 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2491 /* Request an address from ATS to send CONNECT_ACK to this peer */
2492 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2493 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2494 if (NULL == n->suggest_handle)
2495 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2497 case GNUNET_TRANSPORT_PS_INIT_ATS:
2498 /* CONNECT message takes priority over us asking ATS for address:
2499 * Wait for ATS to suggest an address and send CONNECT_ACK */
2500 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2501 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2503 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2504 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2505 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2506 /* It can never hurt to have an alternative address in the above cases,
2507 see if it is allowed */
2508 connect_check_blacklist (peer, ts, address, session);
2510 case GNUNET_TRANSPORT_PS_CONNECTED:
2511 /* we are already connected and can thus send the ACK immediately;
2512 still, it can never hurt to have an alternative address, so also
2513 tell ATS about it */
2514 GNUNET_assert (NULL != n->primary_address.address);
2515 GNUNET_assert (NULL != n->primary_address.session);
2516 n->ack_state = ACK_UNDEFINED;
2517 send_connect_ack_message (n->primary_address.address,
2518 n->primary_address.session, ts);
2519 connect_check_blacklist (peer, ts, address, session);
2521 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2522 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2523 /* It can never hurt to have an alternative address in the above cases,
2524 see if it is allowed */
2525 connect_check_blacklist (peer, ts, address, session);
2527 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2528 /* we are already connected and can thus send the ACK immediately;
2529 still, it can never hurt to have an alternative address, so also
2530 tell ATS about it */
2531 GNUNET_assert (NULL != n->primary_address.address);
2532 GNUNET_assert (NULL != n->primary_address.session);
2533 n->ack_state = ACK_UNDEFINED;
2534 send_connect_ack_message (n->primary_address.address,
2535 n->primary_address.session, ts);
2536 connect_check_blacklist (peer, ts, address, session);
2538 case GNUNET_TRANSPORT_PS_DISCONNECT:
2539 /* get rid of remains without terminating sessions, ready to re-try */
2540 free_neighbour (n, GNUNET_YES);
2541 n = setup_neighbour (peer);
2542 /* Remember the CONNECT timestamp for ACK message */
2543 n->ack_state = ACK_SEND_CONNECT_ACK;
2544 n->connect_ack_timestamp = ts;
2545 /* Request an address for the peer */
2546 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2547 GNUNET_ATS_reset_backoff (GST_ats, peer);
2548 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2550 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2551 /* should not be possible */
2555 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2556 "Unhandled state `%s'\n",
2557 GNUNET_TRANSPORT_ps2s (n->state));
2559 return GNUNET_SYSERR;
2565 switch_address_bl_check_cont (void *cls,
2566 const struct GNUNET_PeerIdentity *peer, int result)
2568 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2569 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2570 struct NeighbourMapEntry *n;
2572 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2573 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2578 "Peer %s is unknown, suggestion ignored\n",
2581 if (result == GNUNET_NO)
2583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2584 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2585 GST_plugins_a2s (blc_ctx->address),
2587 GNUNET_i2s (&blc_ctx->address->peer));
2589 if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2592 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2593 blc_ctx->address->transport_name,
2594 GST_plugins_a2s (blc_ctx->address),
2596 GNUNET_i2s (&blc_ctx->address->peer));
2599 /* This address is blacklisted, delete address and session (if existing) in ATS */
2600 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2602 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2603 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2605 /* This is an inbound address, destroy full address */
2606 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2609 /* Remove blacklist check and clean up */
2610 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2611 GNUNET_HELLO_address_free (blc_ctx->address);
2612 GNUNET_free_non_null (blc_ctx->ats);
2613 GNUNET_free (blc_ctx);
2617 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2618 "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2619 GST_plugins_a2s (blc_ctx->address),
2621 GNUNET_i2s (&blc_ctx->address->peer));
2623 if (NULL == blc_ctx->session)
2625 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2627 "Obtained new session for peer `%s' and address '%s': %p\n",
2628 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2630 if (NULL == blc_ctx->session)
2632 /* No session could be obtained, remove blacklist check and clean up */
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2634 "Failed to obtain new session for peer `%s' and address '%s'\n",
2635 GNUNET_i2s (&blc_ctx->address->peer),
2636 GST_plugins_a2s (blc_ctx->address));
2637 /* Delete address in ATS */
2638 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2640 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2641 GNUNET_HELLO_address_free (blc_ctx->address);
2642 GNUNET_free_non_null (blc_ctx->ats);
2643 GNUNET_free (blc_ctx);
2649 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2651 free_neighbour (n, GNUNET_NO);
2653 case GNUNET_TRANSPORT_PS_INIT_ATS:
2654 /* We requested an address and ATS suggests one:
2655 * set primary address and send CONNECT message*/
2656 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2657 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2658 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2659 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2660 send_session_connect (&n->primary_address);
2662 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2663 /* waiting on CONNECT_ACK, send ACK if one is pending */
2664 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2666 n->ack_state = ACK_SEND_SESSION_ACK;
2667 send_connect_ack_message (n->primary_address.address,
2668 n->primary_address.session, n->connect_ack_timestamp);
2670 /* ATS suggests a different address, switch again */
2671 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2672 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2673 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2674 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2675 send_session_connect (&n->primary_address);
2677 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2678 /* We requested an address and ATS suggests one:
2679 * set primary address and send CONNECT_ACK message*/
2680 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2681 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2682 /* Send an ACK message as a response to the CONNECT msg */
2683 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2684 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2685 send_connect_ack_message (n->primary_address.address,
2686 n->primary_address.session,
2687 n->connect_ack_timestamp);
2688 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2689 n->ack_state = ACK_SEND_SESSION_ACK;
2691 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2692 /* ATS asks us to switch while we were trying to connect; switch to new
2693 address and check blacklist again */
2694 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2695 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2696 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2697 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2698 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) )
2700 n->ack_state = ACK_SEND_SESSION_ACK;
2701 send_connect_ack_message (n->primary_address.address,
2702 n->primary_address.session, n->connect_ack_timestamp);
2705 case GNUNET_TRANSPORT_PS_CONNECTED:
2706 GNUNET_assert (NULL != n->primary_address.address);
2707 GNUNET_assert (NULL != n->primary_address.session);
2708 if (n->primary_address.session == blc_ctx->session)
2710 /* not an address change, just a quota change */
2711 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2712 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2715 /* ATS asks us to switch a life connection; see if we can get
2716 a CONNECT_ACK on it before we actually do this! */
2717 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2718 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2719 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT,
2720 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2721 send_session_connect (&n->alternative_address);
2723 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2724 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2725 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2726 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2727 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2728 send_session_connect (&n->primary_address);
2730 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2731 /* ATS asks us to switch while we were trying to reconnect; switch to new
2732 address and send CONNECT again */
2733 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2734 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2735 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2736 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2737 send_session_connect (&n->primary_address);
2739 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2740 if (n->primary_address.session == blc_ctx->session)
2742 /* ATS switches back to still-active session */
2743 free_address (&n->alternative_address);
2744 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2747 /* ATS asks us to switch a life connection, send */
2748 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2749 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2750 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT,
2751 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2752 send_session_connect (&n->alternative_address);
2754 case GNUNET_TRANSPORT_PS_DISCONNECT:
2755 /* not going to switch addresses while disconnecting */
2757 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2761 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2762 "Unhandled state `%s'\n",
2763 GNUNET_TRANSPORT_ps2s (n->state));
2768 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2769 GNUNET_HELLO_address_free(blc_ctx->address);
2770 GNUNET_free_non_null (blc_ctx->ats);
2771 GNUNET_free (blc_ctx);
2777 * For the given peer, switch to this address.
2779 * Before accepting this addresses and actively using it, a blacklist check
2780 * is performed. If this blacklist check fails the address will be destroyed.
2782 * @param peer identity of the peer to switch the address for
2783 * @param address address of the other peer,
2784 * @param session session to use or NULL if transport should initiate a session
2785 * @param ats performance data
2786 * @param ats_count number of entries in ats
2787 * @param bandwidth_in inbound quota to be used when connection is up,
2788 * 0 to disconnect from peer
2789 * @param bandwidth_out outbound quota to be used when connection is up,
2790 * 0 to disconnect from peer
2793 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2794 const struct GNUNET_HELLO_Address *address,
2795 struct Session *session,
2796 const struct GNUNET_ATS_Information *ats,
2798 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2799 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2801 struct NeighbourMapEntry *n;
2802 struct GST_BlacklistCheck *blc;
2803 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2804 struct BlacklistCheckSwitchContext *blc_ctx;
2807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2808 "ATS has decided on an address for peer %s\n",
2810 GNUNET_assert (NULL != address->transport_name);
2811 if (NULL == (n = lookup_neighbour (peer)))
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2814 "Peer %s is unknown, suggestion ignored\n",
2819 /* Check if plugin is available */
2820 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2822 /* we don't have the plugin for this address */
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2824 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2825 address->transport_name,
2827 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2830 if ((NULL == session) &&
2831 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2833 /* This is a inbound address and we do not have a session to use! */
2834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2835 "Inbound address without session `%s'! Destroying address...\n",
2836 GST_plugins_a2s (address));
2837 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2841 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2842 "ATS tells us to switch to %s address '%s' session %p for "
2843 "peer `%s' in state %s/%s (quota in/out %u %u )\n",
2844 GNUNET_HELLO_address_check_option (address,
2845 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2846 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2847 GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state),
2848 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2850 /* Perform blacklist check */
2851 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2852 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2853 blc_ctx->session = session;
2854 blc_ctx->bandwidth_in = bandwidth_in;
2855 blc_ctx->bandwidth_out = bandwidth_out;
2856 blc_ctx->ats_count = ats_count;
2857 blc_ctx->ats = NULL;
2860 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2861 for (c = 0; c < ats_count; c++)
2863 blc_ctx->ats[c].type = ats[c].type;
2864 blc_ctx->ats[c].value = ats[c].value;
2868 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2869 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2870 &switch_address_bl_check_cont, blc_ctx)))
2878 send_utilization_data (void *cls,
2879 const struct GNUNET_PeerIdentity *key,
2882 struct NeighbourMapEntry *n = value;
2883 struct GNUNET_ATS_Information atsi[4];
2885 uint32_t bps_pl_out;
2888 struct GNUNET_TIME_Relative delta;
2890 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2891 GNUNET_TIME_absolute_get ());
2895 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2896 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2898 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2899 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2900 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2901 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2906 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2907 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2909 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2910 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2914 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2918 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2919 atsi[0].value = htonl (bps_out);
2920 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2921 atsi[1].value = htonl (bps_in);
2923 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2924 atsi[2].value = htonl (bps_pl_out);
2925 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2926 atsi[3].value = htonl (bps_pl_in);
2928 GST_ats_update_metrics (key, n->primary_address.address,
2929 n->primary_address.session, atsi, 4);
2930 n->util_payload_bytes_recv = 0;
2931 n->util_payload_bytes_sent = 0;
2932 n->util_total_bytes_recv = 0;
2933 n->util_total_bytes_sent = 0;
2934 n->last_util_transmission = GNUNET_TIME_absolute_get();
2940 * Task transmitting utilization in a regular interval
2942 * @param cls the 'struct NeighbourMapEntry' for which we are running
2943 * @param tc scheduler context (unused)
2946 utilization_transmission (void *cls,
2947 const struct GNUNET_SCHEDULER_TaskContext *tc)
2949 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2951 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2952 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2954 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2955 utilization_transmission, NULL);
2961 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2962 const struct GNUNET_HELLO_Address *address,
2963 struct Session *session,
2964 const struct GNUNET_MessageHeader *message)
2966 struct NeighbourMapEntry *n;
2968 n = lookup_neighbour (peer);
2971 n->util_total_bytes_recv += ntohs(message->size);
2976 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2977 const struct GNUNET_HELLO_Address *address,
2978 struct Session *session,
2979 const struct GNUNET_MessageHeader *message)
2981 struct NeighbourMapEntry *n;
2982 n = lookup_neighbour (peer);
2985 n->util_payload_bytes_recv += ntohs(message->size);
2990 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2991 const struct GNUNET_HELLO_Address *address,
2992 struct Session *session,
2995 struct NeighbourMapEntry *n;
2996 n = lookup_neighbour (peer);
2999 if (n->primary_address.session != session)
3001 n->util_total_bytes_sent += size;
3006 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
3009 struct NeighbourMapEntry *n;
3010 n = lookup_neighbour (peer);
3013 n->util_payload_bytes_sent += size;
3018 * Master task run for every neighbour. Performs all of the time-related
3019 * activities (keep alive, send next message, disconnect if idle, finish
3020 * clean up after disconnect).
3022 * @param cls the 'struct NeighbourMapEntry' for which we are running
3023 * @param tc scheduler context (unused)
3026 master_task (void *cls,
3027 const struct GNUNET_SCHEDULER_TaskContext *tc)
3029 struct NeighbourMapEntry *n = cls;
3030 struct GNUNET_TIME_Relative delay;
3032 n->task = GNUNET_SCHEDULER_NO_TASK;
3033 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3034 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3035 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3036 GNUNET_i2s (&n->id),
3037 GNUNET_TRANSPORT_ps2s(n->state),
3038 GNUNET_STRINGS_relative_time_to_string (delay,
3042 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3043 /* invalid state for master task, clean up */
3045 free_neighbour (n, GNUNET_NO);
3047 case GNUNET_TRANSPORT_PS_INIT_ATS:
3048 if (0 == delay.rel_value_us)
3050 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3051 "Connection to `%s' timed out waiting for ATS to provide address\n",
3052 GNUNET_i2s (&n->id));
3053 free_neighbour (n, GNUNET_NO);
3057 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3058 if (0 == delay.rel_value_us)
3060 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3061 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
3062 GNUNET_i2s (&n->id));
3063 /* We could not send to this address, delete address and session */
3064 if (NULL != n->primary_address.session)
3065 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
3066 n->primary_address.session);
3067 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3069 /* Remove address and request and additional one */
3070 unset_primary_address (n);
3071 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3072 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3076 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3077 if (0 == delay.rel_value_us)
3079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3080 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3081 GNUNET_i2s (&n->id));
3082 free_neighbour (n, GNUNET_NO);
3086 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3087 if (0 == delay.rel_value_us)
3089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3090 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3091 GNUNET_i2s (&n->id));
3092 disconnect_neighbour (n);
3096 case GNUNET_TRANSPORT_PS_CONNECTED:
3097 if (0 == delay.rel_value_us)
3099 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3100 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3101 GNUNET_i2s (&n->id));
3102 disconnect_neighbour (n);
3105 try_transmission_to_peer (n);
3108 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3109 if (0 == delay.rel_value_us)
3111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3112 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3113 GNUNET_i2s (&n->id));
3114 disconnect_neighbour (n);
3118 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3119 if (0 == delay.rel_value_us)
3121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3122 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3123 GNUNET_i2s (&n->id));
3124 disconnect_neighbour (n);
3128 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3129 if (0 == delay.rel_value_us)
3131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3132 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3133 GNUNET_i2s (&n->id));
3134 disconnect_neighbour (n);
3137 try_transmission_to_peer (n);
3140 case GNUNET_TRANSPORT_PS_DISCONNECT:
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3142 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3143 GNUNET_i2s (&n->id));
3144 free_neighbour (n, GNUNET_NO);
3146 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3147 /* how did we get here!? */
3151 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3152 "Unhandled state `%s'\n",
3153 GNUNET_TRANSPORT_ps2s (n->state));
3157 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3158 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3160 /* if we are *now* in one of the two states, we're sending
3161 keep alive messages, so we need to consider the keepalive
3162 delay, not just the connection timeout */
3163 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3166 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3167 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3174 * Send a SESSION_ACK message to the neighbour to confirm that we
3175 * got his CONNECT_ACK.
3177 * @param n neighbour to send the SESSION_ACK to
3180 send_session_ack_message (struct NeighbourMapEntry *n)
3182 struct GNUNET_MessageHeader msg;
3184 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3185 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3186 (void) send_with_session(n,
3187 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3188 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3194 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3195 * Consider switching to it.
3197 * @param message possibly a 'struct SessionConnectMessage' (check format)
3198 * @param peer identity of the peer to switch the address for
3199 * @param address address of the other peer, NULL if other peer
3201 * @param session session to use (or NULL)
3202 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3205 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3206 const struct GNUNET_PeerIdentity *peer,
3207 const struct GNUNET_HELLO_Address *address,
3208 struct Session *session)
3210 const struct SessionConnectMessage *scm;
3211 struct GNUNET_TIME_Absolute ts;
3212 struct NeighbourMapEntry *n;
3214 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3215 "Received CONNECT_ACK message from peer `%s'\n",
3218 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3220 GNUNET_break_op (0);
3221 return GNUNET_SYSERR;
3223 GNUNET_STATISTICS_update (GST_stats,
3225 ("# CONNECT_ACK messages received"),
3227 scm = (const struct SessionConnectMessage *) message;
3228 GNUNET_break_op (ntohl (scm->reserved) == 0);
3229 if (NULL == (n = lookup_neighbour (peer)))
3231 GNUNET_STATISTICS_update (GST_stats,
3233 ("# unexpected CONNECT_ACK messages (no peer)"),
3235 return GNUNET_SYSERR;
3237 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3240 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3242 free_neighbour (n, GNUNET_NO);
3243 return GNUNET_SYSERR;
3244 case GNUNET_TRANSPORT_PS_INIT_ATS:
3245 GNUNET_STATISTICS_update (GST_stats,
3247 ("# unexpected CONNECT_ACK messages (not ready)"),
3250 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3251 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3254 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3257 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3258 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3259 GNUNET_STATISTICS_set (GST_stats,
3260 gettext_noop ("# peers connected"),
3261 ++neighbours_connected,
3263 connect_notify_cb (callback_cls, &n->id,
3264 n->primary_address.bandwidth_in,
3265 n->primary_address.bandwidth_out);
3266 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3267 GST_ats_add_address (n->primary_address.address,
3268 n->primary_address.session,
3270 set_primary_address (n,
3271 n->primary_address.address,
3272 n->primary_address.session,
3273 n->primary_address.bandwidth_in,
3274 n->primary_address.bandwidth_out,
3276 send_session_ack_message (n);
3278 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3279 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3280 GNUNET_STATISTICS_update (GST_stats,
3282 ("# unexpected CONNECT_ACK messages (not ready)"),
3285 case GNUNET_TRANSPORT_PS_CONNECTED:
3286 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3287 send_session_ack_message (n);
3289 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3290 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3291 to give us a new address... */
3292 GNUNET_STATISTICS_update (GST_stats,
3294 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3297 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3298 /* new address worked; go back to connected! */
3299 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3300 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3301 send_session_ack_message (n);
3303 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3304 /* new address worked; adopt it and go back to connected! */
3305 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3306 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3307 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3309 GST_ats_add_address (n->alternative_address.address,
3310 n->alternative_address.session,
3312 set_primary_address (n, n->alternative_address.address,
3313 n->alternative_address.session, n->alternative_address.bandwidth_in,
3314 n->alternative_address.bandwidth_out, GNUNET_YES);
3316 free_address (&n->alternative_address);
3317 send_session_ack_message (n);
3319 case GNUNET_TRANSPORT_PS_DISCONNECT:
3320 GNUNET_STATISTICS_update (GST_stats,
3322 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3324 return GNUNET_SYSERR;
3325 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3330 "Unhandled state `%s'\n",
3331 GNUNET_TRANSPORT_ps2s (n->state));
3333 return GNUNET_SYSERR;
3340 * A session was terminated. Take note; if needed, try to get
3341 * an alternative address from ATS.
3343 * @param peer identity of the peer where the session died
3344 * @param session session that is gone
3345 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3346 * this session was not in use
3349 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3350 struct Session *session)
3352 struct NeighbourMapEntry *n;
3353 struct BlackListCheckContext *bcc;
3354 struct BlackListCheckContext *bcc_next;
3356 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3358 while (NULL != (bcc = bcc_next))
3360 bcc_next = bcc->next;
3361 if (bcc->na.session == session)
3363 if (NULL != bcc->bc)
3364 GST_blacklist_test_cancel (bcc->bc);
3365 GNUNET_HELLO_address_free (bcc->na.address);
3366 GNUNET_CONTAINER_DLL_remove (bc_head,
3372 if (NULL == (n = lookup_neighbour (peer)))
3373 return GNUNET_NO; /* can't affect us */
3374 if (session != n->primary_address.session)
3376 if (session == n->alternative_address.session)
3378 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3379 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3382 free_address (&n->alternative_address);
3384 return GNUNET_NO; /* doesn't affect us further */
3387 n->expect_latency_response = GNUNET_NO;
3388 /* The session for neighbour's primary address died */
3391 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3393 free_neighbour (n, GNUNET_NO);
3395 case GNUNET_TRANSPORT_PS_INIT_ATS:
3397 free_neighbour (n, GNUNET_NO);
3399 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3400 /* The session used to send the CONNECT terminated:
3401 * this implies a connect error*/
3402 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3403 "Could not send CONNECT message with address `%s' session %p: session terminated, requesting new address\n",
3404 GST_plugins_a2s (n->primary_address.address), n->primary_address.session,
3406 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3407 unset_primary_address (n);
3408 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3409 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3411 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3412 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3413 /* error on inbound session; free neighbour entirely */
3414 free_address (&n->primary_address);
3415 free_neighbour (n, GNUNET_NO);
3417 case GNUNET_TRANSPORT_PS_CONNECTED:
3418 unset_primary_address (n);
3419 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3420 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3422 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3423 /* we don't have an address, how can it go down? */
3426 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3427 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3429 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3430 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3431 secondary as primary */
3432 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3433 free_address (&n->primary_address);
3434 n->primary_address = n->alternative_address;
3435 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3436 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3438 case GNUNET_TRANSPORT_PS_DISCONNECT:
3439 free_address (&n->primary_address);
3441 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3442 /* neighbour was freed and plugins told to terminate session */
3446 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3447 "Unhandled state `%s'\n",
3448 GNUNET_TRANSPORT_ps2s (n->state));
3452 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3453 GNUNET_SCHEDULER_cancel (n->task);
3454 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3460 * We received a 'SESSION_ACK' message from the other peer.
3461 * If we sent a 'CONNECT_ACK' last, this means we are now
3462 * connected. Otherwise, do nothing.
3464 * @param message possibly a 'struct SessionConnectMessage' (check format)
3465 * @param peer identity of the peer to switch the address for
3466 * @param address address of the other peer, NULL if other peer
3468 * @param session session to use (or NULL)
3469 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3472 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3473 const struct GNUNET_PeerIdentity *peer,
3474 const struct GNUNET_HELLO_Address *address,
3475 struct Session *session)
3477 struct NeighbourMapEntry *n;
3479 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3480 "Received SESSION_ACK message from peer `%s'\n",
3482 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3484 GNUNET_break_op (0);
3485 return GNUNET_SYSERR;
3487 GNUNET_STATISTICS_update (GST_stats,
3489 ("# SESSION_ACK messages received"),
3491 if (NULL == (n = lookup_neighbour (peer)))
3493 GNUNET_break_op (0);
3494 return GNUNET_SYSERR;
3496 /* check if we are in a plausible state for having sent
3497 a CONNECT_ACK. If not, return, otherwise break */
3499 /* TODO I have no idea we we should state GNUNET_TRANSPORT_PS_CONNECT_SENT
3500 * Perhaps SWITCHING? Have to check */
3501 if ( /*(*/ (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) /* &&
3502 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3503 (ACK_SEND_SESSION_ACK != n->ack_state) */)
3505 if (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state)
3506 GNUNET_break (0); /* TESTING */
3508 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3509 "Received SESSION_ACK message from peer `%s' in state %s/%s\n",
3511 GNUNET_TRANSPORT_ps2s (n->state),
3512 print_ack_state (n->ack_state));
3513 GNUNET_STATISTICS_update (GST_stats,
3514 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3518 /* We are connected */
3519 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3520 GNUNET_STATISTICS_set (GST_stats,
3521 gettext_noop ("# peers connected"),
3522 ++neighbours_connected,
3525 /* Notify about connection */
3526 connect_notify_cb (callback_cls, &n->id,
3527 n->primary_address.bandwidth_in,
3528 n->primary_address.bandwidth_out);
3530 /* Add session to ATS since no session was given (NULL) and we may have
3531 * obtained a new session */
3532 GST_ats_add_address (n->primary_address.address, n->primary_address.session,
3535 /* Set primary address to used */
3536 set_primary_address (n,
3537 n->primary_address.address,
3538 n->primary_address.session,
3539 n->primary_address.bandwidth_in,
3540 n->primary_address.bandwidth_out,
3547 * Test if we're connected to the given peer.
3549 * @param target peer to test
3550 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3553 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3555 return test_connected (lookup_neighbour (target));
3559 * Change the incoming quota for the given peer.
3561 * @param neighbour identity of peer to change qutoa for
3562 * @param quota new quota
3565 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3566 struct GNUNET_BANDWIDTH_Value32NBO quota)
3568 struct NeighbourMapEntry *n;
3570 if (NULL == (n = lookup_neighbour (neighbour)))
3572 GNUNET_STATISTICS_update (GST_stats,
3574 ("# SET QUOTA messages ignored (no such peer)"),
3578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3579 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3580 ntohl (quota.value__), GNUNET_i2s (&n->id));
3581 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3582 if (0 != ntohl (quota.value__))
3584 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3585 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3586 GNUNET_i2s (&n->id));
3587 if (GNUNET_YES == test_connected (n))
3588 GNUNET_STATISTICS_update (GST_stats,
3589 gettext_noop ("# disconnects due to quota of 0"),
3591 disconnect_neighbour (n);
3596 * We received a disconnect message from the given peer,
3597 * validate and process.
3599 * @param peer sender of the message
3600 * @param msg the disconnect message
3603 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3604 const struct GNUNET_MessageHeader *msg)
3606 struct NeighbourMapEntry *n;
3607 const struct SessionDisconnectMessage *sdm;
3609 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3610 "Received DISCONNECT message from peer `%s'\n",
3612 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3614 // GNUNET_break_op (0);
3615 GNUNET_STATISTICS_update (GST_stats,
3617 ("# disconnect messages ignored (old format)"), 1,
3621 GNUNET_STATISTICS_update (GST_stats,
3623 ("# DISCONNECT messages received"),
3625 sdm = (const struct SessionDisconnectMessage *) msg;
3626 if (NULL == (n = lookup_neighbour (peer)))
3627 return; /* gone already */
3628 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3630 GNUNET_STATISTICS_update (GST_stats,
3632 ("# disconnect messages ignored (timestamp)"), 1,
3636 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3638 GNUNET_break_op (0);
3641 if (ntohl (sdm->purpose.size) !=
3642 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3643 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3644 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3646 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3647 "%s message from peer `%s' has invalid size \n",
3650 GNUNET_break_op (0);
3654 GNUNET_CRYPTO_eddsa_verify
3655 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3656 &sdm->signature, &sdm->public_key))
3658 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3659 "%s message from peer `%s' cannot be verified \n",
3662 GNUNET_break_op (0);
3665 if (GNUNET_YES == test_connected (n))
3666 GNUNET_STATISTICS_update (GST_stats,
3668 ("# other peer asked to disconnect from us"), 1,
3670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3671 "Disconnecting by request from peer %s\n",
3673 disconnect_neighbour (n);
3678 * Closure for the neighbours_iterate function.
3680 struct IteratorContext
3683 * Function to call on each connected neighbour.
3685 GST_NeighbourIterator cb;
3695 * Call the callback from the closure for each neighbour.
3697 * @param cls the `struct IteratorContext`
3698 * @param key the hash of the public key of the neighbour
3699 * @param value the `struct NeighbourMapEntry`
3700 * @return #GNUNET_OK (continue to iterate)
3703 neighbours_iterate (void *cls,
3704 const struct GNUNET_PeerIdentity *key,
3707 struct IteratorContext *ic = cls;
3708 struct NeighbourMapEntry *n = value;
3709 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3710 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3713 if (NULL != n->primary_address.address)
3715 bandwidth_in = n->primary_address.bandwidth_in;
3716 bandwidth_out = n->primary_address.bandwidth_out;
3720 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3721 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3725 n->primary_address.address,
3728 bandwidth_in, bandwidth_out);
3734 * Iterate over all connected neighbours.
3736 * @param cb function to call
3737 * @param cb_cls closure for cb
3740 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3742 struct IteratorContext ic;
3744 if (NULL == neighbours)
3745 return; /* can happen during shutdown */
3748 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3753 * If we have an active connection to the given target, it must be shutdown.
3755 * @param target peer to disconnect from
3758 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3760 struct NeighbourMapEntry *n;
3762 if (NULL == (n = lookup_neighbour (target)))
3763 return; /* not active */
3764 if (GNUNET_YES == test_connected (n))
3765 GNUNET_STATISTICS_update (GST_stats,
3767 ("# disconnected from peer upon explicit request"), 1,
3769 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3770 "Forced disconnect from peer %s\n",
3771 GNUNET_i2s (target));
3772 disconnect_neighbour (n);
3777 * Obtain current latency information for the given neighbour.
3779 * @param peer to get the latency for
3780 * @return observed latency of the address, FOREVER if the
3781 * the connection is not up
3783 struct GNUNET_TIME_Relative
3784 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3786 struct NeighbourMapEntry *n;
3788 n = lookup_neighbour (peer);
3790 return GNUNET_TIME_UNIT_FOREVER_REL;
3793 case GNUNET_TRANSPORT_PS_CONNECTED:
3794 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3795 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3796 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3798 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3799 case GNUNET_TRANSPORT_PS_INIT_ATS:
3800 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3801 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3802 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3803 case GNUNET_TRANSPORT_PS_DISCONNECT:
3804 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3805 return GNUNET_TIME_UNIT_FOREVER_REL;
3807 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3808 "Unhandled state `%s'\n",
3809 GNUNET_TRANSPORT_ps2s (n->state));
3813 return GNUNET_TIME_UNIT_FOREVER_REL;
3818 * Obtain current address information for the given neighbour.
3821 * @return address currently used
3823 struct GNUNET_HELLO_Address *
3824 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3826 struct NeighbourMapEntry *n;
3828 n = lookup_neighbour (peer);
3831 return n->primary_address.address;
3836 * Initialize the neighbours subsystem.
3838 * @param cls closure for callbacks
3839 * @param connect_cb function to call if we connect to a peer
3840 * @param disconnect_cb function to call if we disconnect from a peer
3841 * @param peer_address_cb function to call if we change an active address
3843 * @param max_fds maximum number of fds to use
3846 GST_neighbours_start (void *cls,
3847 NotifyConnect connect_cb,
3848 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3849 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3850 unsigned int max_fds)
3853 connect_notify_cb = connect_cb;
3854 disconnect_notify_cb = disconnect_cb;
3855 neighbour_change_cb = peer_address_cb;
3856 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3857 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3858 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3859 utilization_transmission, NULL);
3864 * Disconnect from the given neighbour.
3867 * @param key hash of neighbour's public key (not used)
3868 * @param value the 'struct NeighbourMapEntry' of the neighbour
3869 * @return #GNUNET_OK (continue to iterate)
3872 disconnect_all_neighbours (void *cls,
3873 const struct GNUNET_PeerIdentity *key,
3876 struct NeighbourMapEntry *n = value;
3878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3879 "Disconnecting peer `%4s', %s\n",
3880 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3881 free_neighbour (n, GNUNET_NO);
3887 * Cleanup the neighbours subsystem.
3890 GST_neighbours_stop ()
3892 struct BlacklistCheckSwitchContext *cur;
3893 struct BlacklistCheckSwitchContext *next;
3895 if (NULL == neighbours)
3897 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3899 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3900 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3903 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3904 &disconnect_all_neighbours,
3906 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3908 next = pending_bc_head;
3909 for (cur = next; NULL != cur; cur = next )
3912 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3914 if (NULL != cur->blc)
3916 GST_blacklist_test_cancel (cur->blc);
3919 if (NULL != cur->address)
3920 GNUNET_HELLO_address_free (cur->address);
3921 GNUNET_free_non_null (cur->ats);
3925 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
3926 &free_notification_cb, NULL);
3927 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
3928 registered_quota_notifications = NULL;
3931 callback_cls = NULL;
3932 connect_notify_cb = NULL;
3933 disconnect_notify_cb = NULL;
3934 neighbour_change_cb = NULL;
3938 /* end of file gnunet-service-transport_neighbours.c */