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 * When a peer wants to connect we have to reply to the 1st CONNECT message
404 * with a CONNECT_ACK message. But sometime we cannot send this message
405 * immediately since we do not have an address and then we have to remember
406 * to send this message as soon as we have an address.
408 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
409 * (once we have an address to use and the peer has been allowed by our
410 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_CONNECT_ACK
411 * if we need to send a CONNECT_ACK. Set to #SESSION_ACK if we did
412 * send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving a
413 * 'SESSION_ACK' (regardless of what our own state machine might say).
415 enum GST_ACK_State ack_state;
418 * Tracking utilization of outbound bandwidth
420 uint32_t util_payload_bytes_sent;
423 * Tracking utilization of inbound bandwidth
425 uint32_t util_payload_bytes_recv;
428 * Tracking utilization of outbound bandwidth
430 uint32_t util_total_bytes_sent;
433 * Tracking utilization of inbound bandwidth
435 uint32_t util_total_bytes_recv;
438 * Date of last utilization transmission
440 struct GNUNET_TIME_Absolute last_util_transmission;
445 * Context for blacklist checks and the #handle_test_blacklist_cont()
446 * function. Stores information about ongoing blacklist checks.
448 struct BlackListCheckContext
452 * We keep blacklist checks in a DLL.
454 struct BlackListCheckContext *next;
457 * We keep blacklist checks in a DLL.
459 struct BlackListCheckContext *prev;
462 * Address that is being checked.
464 struct NeighbourAddress na;
467 * Handle to the ongoing blacklist check.
469 struct GST_BlacklistCheck *bc;
474 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
476 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
479 * We keep blacklist checks in a DLL so that we can find
480 * the 'sessions' in their 'struct NeighbourAddress' if
481 * a session goes down.
483 static struct BlackListCheckContext *bc_head;
486 * We keep blacklist checks in a DLL.
488 static struct BlackListCheckContext *bc_tail;
491 * List of pending blacklist checks: head
493 static struct BlacklistCheckSwitchContext *pending_bc_head;
496 * List of pending blacklist checks: tail
498 static struct BlacklistCheckSwitchContext *pending_bc_tail;
501 * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb
503 static void *callback_cls;
506 * Function to call when we connected to a neighbour.
508 static NotifyConnect connect_notify_cb;
511 * Function to call when we disconnected from a neighbour.
513 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
516 * Function to call when a neighbour changed address, state or bandwidth.
518 static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb;
521 * counter for connected neighbours
523 static unsigned int neighbours_connected;
526 * Number of bytes we have currently queued for transmission.
528 static unsigned long long bytes_in_send_queue;
531 * Task transmitting utilization data
533 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
536 static struct GNUNET_CONTAINER_MultiPeerMap *registered_quota_notifications;
539 print_ack_state (enum GST_ACK_State s)
544 case ACK_SEND_CONNECT_ACK:
545 return "SEND_CONNECT_ACK";
546 case ACK_SEND_SESSION_ACK:
547 return "SEND_SESSION_ACK";
555 * Lookup a neighbour entry in the neighbours hash map.
557 * @param pid identity of the peer to look up
558 * @return the entry, NULL if there is no existing record
560 static struct NeighbourMapEntry *
561 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
563 if (NULL == neighbours)
565 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
570 * Test if we're connected to the given peer.
572 * @param n neighbour entry of peer to test
573 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
576 test_connected (struct NeighbourMapEntry *n)
580 return GNUNET_TRANSPORT_is_connected (n->state);
584 * Send information about a new outbound quota to our clients.
586 * @param target affected peer
587 * @param quota new quota
590 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
591 struct GNUNET_BANDWIDTH_Value32NBO quota)
593 struct QuotaSetMessage q_msg;
595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
596 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
597 ntohl (quota.value__), GNUNET_i2s (target));
598 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
599 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
601 q_msg.peer = (*target);
602 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
607 * We don't need a given neighbour address any more.
608 * Release its resources and give appropriate notifications
609 * to ATS and other subsystems.
611 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
614 free_address (struct NeighbourAddress *na)
616 if (GNUNET_YES == na->ats_active)
618 GST_validation_set_address_use (na->address, na->session, GNUNET_NO);
619 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
622 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
623 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
624 na->ats_active = GNUNET_NO;
625 na->keep_alive_nonce = 0;
626 if (NULL != na->address)
628 GNUNET_HELLO_address_free (na->address);
636 * Set net state for this neighbour and notify monitoring
638 * @param n the respective neighbour
639 * @param s the new state
642 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
645 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
647 GNUNET_TRANSPORT_ps2s(s));
648 neighbour_change_cb (callback_cls,
650 n->primary_address.address,
651 n->state, n->timeout,
652 n->primary_address.bandwidth_in,
653 n->primary_address.bandwidth_out);
658 * Set net state and state timeout for this neighbour and notify monitoring
660 * @param n the respective neighbour
661 * @param s the new state
662 * @param timeout the new timeout
665 set_state_and_timeout (struct NeighbourMapEntry *n,
666 enum GNUNET_TRANSPORT_PeerState s,
667 struct GNUNET_TIME_Absolute timeout)
670 n->timeout = timeout;
671 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
673 GNUNET_TRANSPORT_ps2s(s),
674 GNUNET_STRINGS_absolute_time_to_string (timeout));
675 neighbour_change_cb (callback_cls,
677 n->primary_address.address,
678 n->state, n->timeout,
679 n->primary_address.bandwidth_in,
680 n->primary_address.bandwidth_out);
685 * Set new state timeout for this neighbour and notify monitoring
687 * @param n the respective neighbour
688 * @param timeout the new timeout
691 set_timeout (struct NeighbourMapEntry *n,
692 struct GNUNET_TIME_Absolute timeout)
694 n->timeout = timeout;
695 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed timeout %s\n",
697 GNUNET_STRINGS_absolute_time_to_string (timeout));
698 neighbour_change_cb (callback_cls,
700 n->primary_address.address,
701 n->state, n->timeout,
702 n->primary_address.bandwidth_in,
703 n->primary_address.bandwidth_out);
708 * Initialize the alternative address of a neighbour
710 * @param n the neighbour
711 * @param address address of the other peer, NULL if other peer
713 * @param session session to use (or NULL, in which case an
714 * address must be setup)
715 * @param bandwidth_in inbound quota to be used when connection is up
716 * @param bandwidth_out outbound quota to be used when connection is up
719 set_alternative_address (struct NeighbourMapEntry *n,
720 const struct GNUNET_HELLO_Address *address,
721 struct Session *session,
722 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
723 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
725 struct GNUNET_TRANSPORT_PluginFunctions *papi;
726 if (NULL == (papi = GST_plugins_find (address->transport_name)))
731 if (session == n->alternative_address.session)
733 n->alternative_address.bandwidth_in = bandwidth_in;
734 n->alternative_address.bandwidth_out = bandwidth_out;
737 free_address (&n->alternative_address);
739 session = papi->get_session (papi->cls, address);
742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
743 "Failed to obtain new session for peer `%s' and address '%s'\n",
744 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
745 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
749 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
751 GST_plugins_a2s(address));
753 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
754 n->alternative_address.bandwidth_in = bandwidth_in;
755 n->alternative_address.bandwidth_out = bandwidth_out;
756 n->alternative_address.session = session;
757 n->alternative_address.ats_active = GNUNET_NO;
758 n->alternative_address.keep_alive_nonce = 0;
763 * Initialize the primary address of a neighbour
765 * @param n the neighbour
766 * @param address address of the other peer, NULL if other peer
768 * @param session session to use (or NULL, in which case an
769 * address must be setup)
770 * @param bandwidth_in inbound quota to be used when connection is up
771 * @param bandwidth_out outbound quota to be used when connection is up
772 * @param is_active #GNUNET_YES to mark this as the active address with ATS
775 set_primary_address (struct NeighbourMapEntry *n,
776 const struct GNUNET_HELLO_Address *address,
777 struct Session *session,
778 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
779 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
782 struct GNUNET_TRANSPORT_PluginFunctions *papi;
784 if (NULL == (papi = GST_plugins_find (address->transport_name)))
789 if (session == n->primary_address.session)
791 n->primary_address.bandwidth_in = bandwidth_in;
792 n->primary_address.bandwidth_out = bandwidth_out;
793 if (is_active != n->primary_address.ats_active)
795 n->primary_address.ats_active = is_active;
796 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
797 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
799 if (GNUNET_YES == is_active)
801 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
802 send_outbound_quota (&address->peer, bandwidth_out);
806 free_address (&n->primary_address);
808 session = papi->get_session (papi->cls, address);
811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
812 "Failed to obtain new session for peer `%s' and address '%s'\n",
813 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
814 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
818 n->primary_address.address = GNUNET_HELLO_address_copy (address);
819 n->primary_address.bandwidth_in = bandwidth_in;
820 n->primary_address.bandwidth_out = bandwidth_out;
821 n->primary_address.session = session;
822 n->primary_address.ats_active = is_active;
823 n->primary_address.keep_alive_nonce = 0;
824 if (GNUNET_YES == is_active)
826 /* Telling ATS about new session */
827 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
828 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
829 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
830 send_outbound_quota (&address->peer, bandwidth_out);
833 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
835 GST_plugins_a2s(address));
837 neighbour_change_cb (callback_cls,
839 n->primary_address.address,
840 n->state, n->timeout,
841 n->primary_address.bandwidth_in,
842 n->primary_address.bandwidth_out);
846 * Clear the primary address of a neighbour since this address is not
847 * valid anymore and notify monitoring about it
849 * @param n the neighbour
852 unset_primary_address (struct NeighbourMapEntry *n)
854 /* Unset primary address */
855 free_address (&n->primary_address);
857 /* Notify monitoring about it */
858 neighbour_change_cb (callback_cls,
861 n->state, n->timeout,
862 n->primary_address.bandwidth_in,
863 n->primary_address.bandwidth_out);
867 * Clear the alternative address of a neighbour since this address is not
870 * @param n the neighbour
873 unset_alternative_address (struct NeighbourMapEntry *n)
875 /* Unset primary address */
876 free_address (&n->alternative_address);
880 * Free a neighbour map entry.
882 * @param n entry to free
883 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
884 * #GNUNET_YES to keep all sessions
887 free_neighbour (struct NeighbourMapEntry *n,
890 struct MessageQueue *mq;
891 struct GNUNET_TRANSPORT_PluginFunctions *papi;
892 struct GNUNET_HELLO_Address *backup_primary;
894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
895 "Freeing neighbour state of peer `%s'\n",
896 GNUNET_i2s (&n->id));
897 n->is_active = NULL; /* always free'd by its own continuation! */
899 /* fail messages currently in the queue */
900 while (NULL != (mq = n->messages_head))
902 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
903 if (NULL != mq->cont)
904 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
907 /* It is too late to send other peer disconnect notifications, but at
908 least internally we need to get clean... */
909 if (GNUNET_YES == test_connected (n))
911 GNUNET_STATISTICS_set (GST_stats,
912 gettext_noop ("# peers connected"),
913 --neighbours_connected,
915 disconnect_notify_cb (callback_cls, &n->id);
918 /* Mark peer as disconnected */
919 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
921 if (NULL != n->primary_address.address)
922 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
924 backup_primary = NULL;
926 /* free addresses and mark as unused */
927 unset_primary_address (n);
928 free_address (&n->alternative_address);
930 /* cut all transport-level connection for this peer */
931 if ((GNUNET_NO == keep_sessions) &&
932 (NULL != backup_primary) &&
933 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
934 papi->disconnect_peer (papi->cls, &n->id);
936 if (NULL != backup_primary)
937 GNUNET_HELLO_address_free (backup_primary);
938 GNUNET_assert (GNUNET_YES ==
939 GNUNET_CONTAINER_multipeermap_remove (neighbours,
942 /* Cancel address requests for this peer */
943 if (NULL != n->suggest_handle)
945 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
946 n->suggest_handle = NULL;
949 /* Cancel the master task */
950 if (GNUNET_SCHEDULER_NO_TASK != n->task)
952 GNUNET_SCHEDULER_cancel (n->task);
953 n->task = GNUNET_SCHEDULER_NO_TASK;
955 /* free rest of memory */
961 * Transmit a message using the current session of the given
964 * @param n entry for the recipient
965 * @param msgbuf buffer to transmit
966 * @param msgbuf_size number of bytes in @a msgbuf buffer
967 * @param priority transmission priority
968 * @param timeout transmission timeout
969 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
970 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
971 * @param cont continuation to call when finished (can be NULL)
972 * @param cont_cls closure for @a cont
973 * @return timeout (copy of @a timeout or a calculated one if
974 * @a use_keepalive_timeout is #GNUNET_YES.
976 static struct GNUNET_TIME_Relative
977 send_with_session (struct NeighbourMapEntry *n,
978 const char *msgbuf, size_t msgbuf_size,
980 struct GNUNET_TIME_Relative timeout,
981 unsigned int use_keepalive_timeout,
982 GNUNET_TRANSPORT_TransmitContinuation cont,
985 struct GNUNET_TRANSPORT_PluginFunctions *papi;
986 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
988 GNUNET_assert (n->primary_address.session != NULL);
989 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
990 (-1 == papi->send (papi->cls,
991 n->primary_address.session,
994 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
995 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
996 papi->query_keepalive_factor (papi->cls))),
997 cont, cont_cls)))) &&
999 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1000 GST_neighbours_notify_data_sent (&n->id,
1001 n->primary_address.address, n->primary_address.session, msgbuf_size);
1002 GNUNET_break (NULL != papi);
1008 * Master task run for every neighbour. Performs all of the time-related
1009 * activities (keep alive, send next message, disconnect if idle, finish
1010 * clean up after disconnect).
1012 * @param cls the `struct NeighbourMapEntry` for which we are running
1013 * @param tc scheduler context (unused)
1016 master_task (void *cls,
1017 const struct GNUNET_SCHEDULER_TaskContext *tc);
1021 * Function called when the 'DISCONNECT' message has been sent by the
1022 * plugin. Frees the neighbour --- if the entry still exists.
1025 * @param target identity of the neighbour that was disconnected
1026 * @param result #GNUNET_OK if the disconnect got out successfully
1027 * @param payload bytes payload
1028 * @param physical bytes physical
1031 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1032 int result, size_t payload, size_t physical)
1034 struct NeighbourMapEntry *n;
1036 n = lookup_neighbour (target);
1038 return; /* already gone */
1039 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1040 return; /* have created a fresh entry since */
1041 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1042 GNUNET_SCHEDULER_cancel (n->task);
1043 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1048 * Transmit a DISCONNECT message to the other peer.
1050 * @param n neighbour to send DISCONNECT message.
1053 send_disconnect (struct NeighbourMapEntry *n)
1055 struct SessionDisconnectMessage disconnect_msg;
1057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1058 "Sending DISCONNECT message to peer `%4s'\n",
1059 GNUNET_i2s (&n->id));
1060 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1061 disconnect_msg.header.type =
1062 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1063 disconnect_msg.reserved = htonl (0);
1064 disconnect_msg.purpose.size =
1065 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1066 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1067 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1068 disconnect_msg.purpose.purpose =
1069 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1070 disconnect_msg.timestamp =
1071 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1072 disconnect_msg.public_key = GST_my_identity.public_key;
1073 GNUNET_assert (GNUNET_OK ==
1074 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1075 &disconnect_msg.purpose,
1076 &disconnect_msg.signature));
1078 (void) send_with_session (n,
1079 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1080 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1081 GNUNET_NO, &send_disconnect_cont, NULL);
1082 GNUNET_STATISTICS_update (GST_stats,
1084 ("# DISCONNECT messages sent"), 1,
1090 * Disconnect from the given neighbour, clean up the record.
1092 * @param n neighbour to disconnect from
1095 disconnect_neighbour (struct NeighbourMapEntry *n)
1097 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1098 "Disconnecting from peer %s in state %s\n",
1099 GNUNET_i2s (&n->id),
1100 GNUNET_TRANSPORT_ps2s (n->state));
1101 /* depending on state, notify neighbour and/or upper layers of this peer
1105 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1106 case GNUNET_TRANSPORT_PS_INIT_ATS:
1107 /* other peer is completely unaware of us, no need to send DISCONNECT */
1108 free_neighbour (n, GNUNET_NO);
1110 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1111 send_disconnect (n);
1112 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1114 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1115 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1116 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1117 free_neighbour (n, GNUNET_NO);
1119 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1120 /* we DID ACK the other peer's request, must send DISCONNECT */
1121 send_disconnect (n);
1122 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1124 case GNUNET_TRANSPORT_PS_CONNECTED:
1125 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1126 /* we are currently connected, need to send disconnect and do
1127 internal notifications and update statistics */
1128 send_disconnect (n);
1129 GNUNET_STATISTICS_set (GST_stats,
1130 gettext_noop ("# peers connected"),
1131 --neighbours_connected,
1133 disconnect_notify_cb (callback_cls, &n->id);
1134 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1136 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1137 /* ATS address request timeout, disconnect without sending disconnect message */
1138 GNUNET_STATISTICS_set (GST_stats,
1139 gettext_noop ("# peers connected"),
1140 --neighbours_connected,
1142 disconnect_notify_cb (callback_cls, &n->id);
1143 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1145 case GNUNET_TRANSPORT_PS_DISCONNECT:
1146 /* already disconnected, ignore */
1148 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1149 /* already cleaned up, how did we get here!? */
1153 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1154 "Unhandled state `%s'\n",
1155 GNUNET_TRANSPORT_ps2s (n->state));
1159 /* schedule timeout to clean up */
1160 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1161 GNUNET_SCHEDULER_cancel (n->task);
1162 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1168 * We're done with our transmission attempt, continue processing.
1170 * @param cls the `struct MessageQueue` of the message
1171 * @param receiver intended receiver
1172 * @param success whether it worked or not
1173 * @param size_payload bytes payload sent
1174 * @param physical bytes sent on wire
1177 transmit_send_continuation (void *cls,
1178 const struct GNUNET_PeerIdentity *receiver,
1179 int success, size_t size_payload, size_t physical)
1181 struct MessageQueue *mq = cls;
1182 struct NeighbourMapEntry *n;
1184 if (NULL == (n = lookup_neighbour (receiver)))
1187 return; /* disconnect or other error while transmitting, can happen */
1189 if (n->is_active == mq)
1191 /* this is still "our" neighbour, remove us from its queue
1192 and allow it to send the next message now */
1193 n->is_active = NULL;
1194 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1195 GNUNET_SCHEDULER_cancel (n->task);
1196 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1198 if (bytes_in_send_queue < mq->message_buf_size)
1200 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1201 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1202 bytes_in_send_queue, mq->message_buf_size,
1203 (GNUNET_OK == success) ? "OK" : "FAIL",
1204 size_payload, physical);
1209 GNUNET_break (size_payload == mq->message_buf_size);
1210 bytes_in_send_queue -= mq->message_buf_size;
1211 GNUNET_STATISTICS_set (GST_stats,
1213 ("# bytes in message queue for other peers"),
1214 bytes_in_send_queue, GNUNET_NO);
1215 if (GNUNET_OK == success)
1216 GNUNET_STATISTICS_update (GST_stats,
1218 ("# messages transmitted to other peers"),
1221 GNUNET_STATISTICS_update (GST_stats,
1223 ("# transmission failures for messages to other peers"),
1225 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1226 "Sending message to `%s' of type %u was a %s\n",
1227 GNUNET_i2s (receiver),
1228 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1229 (success == GNUNET_OK) ? "success" : "FAILURE");
1230 if (NULL != mq->cont)
1231 mq->cont (mq->cont_cls, success, size_payload, physical);
1237 * Check the message list for the given neighbour and if we can
1238 * send a message, do so. This function should only be called
1239 * if the connection is at least generally ready for transmission.
1240 * While we will only send one message at a time, no bandwidth
1241 * quota management is performed here. If a message was given to
1242 * the plugin, the continuation will automatically re-schedule
1243 * the 'master' task once the next message might be transmitted.
1245 * @param n target peer for which to transmit
1248 try_transmission_to_peer (struct NeighbourMapEntry *n)
1250 struct MessageQueue *mq;
1251 struct GNUNET_TIME_Relative timeout;
1253 if (NULL == n->primary_address.address)
1255 /* no address, why are we here? */
1259 if ((0 == n->primary_address.address->address_length) &&
1260 (NULL == n->primary_address.session))
1262 /* no address, why are we here? */
1266 if (NULL != n->is_active)
1268 /* transmission already pending */
1272 /* timeout messages from the queue that are past their due date */
1273 while (NULL != (mq = n->messages_head))
1275 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1276 if (timeout.rel_value_us > 0)
1278 GNUNET_STATISTICS_update (GST_stats,
1280 ("# messages timed out while in transport queue"),
1282 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1284 transmit_send_continuation (mq, &n->id,
1286 mq->message_buf_size, 0); /* timeout */
1289 return; /* no more messages */
1290 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1292 (void) send_with_session (n,
1293 mq->message_buf, mq->message_buf_size,
1294 0 /* priority */, timeout, GNUNET_NO,
1295 &transmit_send_continuation, mq);
1300 * Send keepalive message to the neighbour. Must only be called
1301 * if we are on 'connected' state or while trying to switch addresses.
1302 * Will internally determine if a keepalive is truly needed (so can
1303 * always be called).
1305 * @param n neighbour that went idle and needs a keepalive
1308 send_keepalive (struct NeighbourMapEntry *n)
1310 struct SessionKeepAliveMessage m;
1311 struct GNUNET_TIME_Relative timeout;
1314 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1315 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1316 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1317 return; /* no keepalive needed at this time */
1319 nonce = 0; /* 0 indicates 'not set' */
1321 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1323 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1324 "Sending keep alive to peer `%s' with nonce %u\n",
1325 GNUNET_i2s (&n->id), nonce);
1327 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1328 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1329 m.nonce = htonl (nonce);
1331 timeout = send_with_session (n,
1332 (const void *) &m, sizeof (m),
1333 UINT32_MAX /* priority */,
1334 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1336 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1338 n->primary_address.keep_alive_nonce = nonce;
1339 n->expect_latency_response = GNUNET_YES;
1340 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1341 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1347 * Keep the connection to the given neighbour alive longer,
1348 * we received a KEEPALIVE (or equivalent); send a response.
1350 * @param neighbour neighbour to keep alive (by sending keep alive response)
1351 * @param m the keep alive message containing the nonce to respond to
1354 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1355 const struct GNUNET_MessageHeader *m)
1357 struct NeighbourMapEntry *n;
1358 const struct SessionKeepAliveMessage *msg_in;
1359 struct SessionKeepAliveMessage msg;
1361 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1364 msg_in = (struct SessionKeepAliveMessage *) m;
1365 if (NULL == (n = lookup_neighbour (neighbour)))
1367 GNUNET_STATISTICS_update (GST_stats,
1369 ("# KEEPALIVE messages discarded (peer unknown)"),
1373 if (NULL == n->primary_address.session)
1375 GNUNET_STATISTICS_update (GST_stats,
1377 ("# KEEPALIVE messages discarded (no session)"),
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 "Received keep alive request from peer `%s' with nonce %u\n",
1384 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1386 /* send reply to allow neighbour to measure latency */
1387 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1388 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1389 msg.nonce = msg_in->nonce;
1390 (void) send_with_session(n,
1391 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1392 UINT32_MAX /* priority */,
1393 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1399 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1400 * latency to this peer. Pass the updated information (existing ats
1401 * plus calculated latency) to ATS.
1403 * @param neighbour neighbour to keep alive
1404 * @param m the message containing the keep alive response
1407 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1408 const struct GNUNET_MessageHeader *m)
1410 struct NeighbourMapEntry *n;
1411 const struct SessionKeepAliveMessage *msg;
1412 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1414 struct GNUNET_ATS_Information ats;
1416 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1419 msg = (const struct SessionKeepAliveMessage *) m;
1420 if (NULL == (n = lookup_neighbour (neighbour)))
1422 GNUNET_STATISTICS_update (GST_stats,
1424 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1428 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1429 (GNUNET_YES != n->expect_latency_response) )
1431 GNUNET_STATISTICS_update (GST_stats,
1433 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1437 if (NULL == n->primary_address.address)
1439 GNUNET_STATISTICS_update (GST_stats,
1441 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1445 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1447 GNUNET_STATISTICS_update (GST_stats,
1449 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Received keep alive response from peer `%s' for session %p\n",
1457 GNUNET_i2s (&n->id), n->primary_address.session);
1461 /* Update session timeout here */
1462 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1464 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1465 "Updating session for peer `%s' for session %p\n",
1466 GNUNET_i2s (&n->id), n->primary_address.session);
1467 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1474 n->primary_address.keep_alive_nonce = 0;
1475 n->expect_latency_response = GNUNET_NO;
1476 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1477 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1479 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1480 "Latency for peer `%s' is %s\n",
1481 GNUNET_i2s (&n->id),
1482 GNUNET_STRINGS_relative_time_to_string (n->latency,
1484 /* append latency */
1485 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1486 if (n->latency.rel_value_us > UINT32_MAX)
1487 latency = UINT32_MAX;
1489 latency = n->latency.rel_value_us;
1490 ats.value = htonl (latency);
1491 GST_ats_update_metrics (&n->id, n->primary_address.address,
1492 n->primary_address.session, &ats, 1);
1497 * We have received a message from the given sender. How long should
1498 * we delay before receiving more? (Also used to keep the peer marked
1501 * @param sender sender of the message
1502 * @param size size of the message
1503 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1504 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1505 * #GNUNET_SYSERR if the connection is not fully up yet
1506 * @return how long to wait before reading more from this sender
1508 struct GNUNET_TIME_Relative
1509 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1510 *sender, ssize_t size, int *do_forward)
1512 struct NeighbourMapEntry *n;
1513 struct GNUNET_TIME_Relative ret;
1515 if (NULL == neighbours)
1517 *do_forward = GNUNET_NO;
1518 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1520 if (NULL == (n = lookup_neighbour (sender)))
1522 GST_neighbours_try_connect (sender);
1523 if (NULL == (n = lookup_neighbour (sender)))
1525 GNUNET_STATISTICS_update (GST_stats,
1527 ("# messages discarded due to lack of neighbour record"),
1529 *do_forward = GNUNET_NO;
1530 return GNUNET_TIME_UNIT_ZERO;
1533 if (! test_connected (n))
1535 *do_forward = GNUNET_SYSERR;
1536 return GNUNET_TIME_UNIT_ZERO;
1538 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1540 n->quota_violation_count++;
1541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1542 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1543 n->in_tracker.available_bytes_per_s__,
1544 n->quota_violation_count);
1545 /* Discount 32k per violation */
1546 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1550 if (n->quota_violation_count > 0)
1552 /* try to add 32k back */
1553 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1554 n->quota_violation_count--;
1557 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1559 GNUNET_STATISTICS_update (GST_stats,
1561 ("# bandwidth quota violations by other peers"),
1563 *do_forward = GNUNET_NO;
1564 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1566 *do_forward = GNUNET_YES;
1567 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1568 if (ret.rel_value_us > 0)
1570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1571 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1572 (unsigned long long) n->in_tracker.
1573 consumption_since_last_update__,
1574 (unsigned int) n->in_tracker.available_bytes_per_s__,
1575 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1576 GNUNET_STATISTICS_update (GST_stats,
1577 gettext_noop ("# ms throttling suggested"),
1578 (int64_t) ret.rel_value_us / 1000LL,
1586 * Transmit a message to the given target using the active connection.
1588 * @param target destination
1589 * @param msg message to send
1590 * @param msg_size number of bytes in msg
1591 * @param timeout when to fail with timeout
1592 * @param cont function to call when done
1593 * @param cont_cls closure for 'cont'
1596 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1597 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1598 GST_NeighbourSendContinuation cont, void *cont_cls)
1600 struct NeighbourMapEntry *n;
1601 struct MessageQueue *mq;
1603 /* All ove these cases should never happen; they are all API violations.
1604 But we check anyway, just to be sure. */
1605 if (NULL == (n = lookup_neighbour (target)))
1609 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1612 if (GNUNET_YES != test_connected (n))
1616 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1619 bytes_in_send_queue += msg_size;
1620 GNUNET_STATISTICS_set (GST_stats,
1622 ("# bytes in message queue for other peers"),
1623 bytes_in_send_queue, GNUNET_NO);
1624 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1626 mq->cont_cls = cont_cls;
1627 memcpy (&mq[1], msg, msg_size);
1628 mq->message_buf = (const char *) &mq[1];
1629 mq->message_buf_size = msg_size;
1630 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1631 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1632 if ( (NULL != n->is_active) ||
1633 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1635 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1636 GNUNET_SCHEDULER_cancel (n->task);
1637 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1641 send_session_connect_cont (void *cls,
1642 const struct GNUNET_PeerIdentity *target,
1644 size_t size_payload,
1645 size_t size_on_wire)
1647 struct NeighbourMapEntry *n;
1649 n = lookup_neighbour (target);
1652 /* CONNECT continuation was called after neighbor was freed,
1653 * for example due to a time out for the state or the session
1654 * used was already terminated: nothing to do here... */
1658 if ( (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) &&
1659 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1660 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT != n->state))
1662 /* CONNECT continuation was called after neighbor changed state,
1663 * for example due to a time out for the state or the session
1664 * used was already terminated: nothing to do here... */
1667 if (GNUNET_OK == result)
1670 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1671 _("Failed to send CONNECT message to peer `%s' using address `%s' session %p\n"),
1672 GNUNET_i2s (target),
1673 GST_plugins_a2s (n->primary_address.address),
1674 n->primary_address.session);
1677 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1678 /* Remove address and request and additional one */
1679 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1680 n->primary_address.session);
1681 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL );
1682 unset_primary_address (n);
1683 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1684 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1686 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1687 /* Remove address and request and additional one */
1688 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1689 n->primary_address.session);
1690 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL );
1691 unset_primary_address (n);
1692 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1693 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1695 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1696 /* Remove address and request and go back to primary address */
1697 GNUNET_ATS_address_destroyed (GST_ats, n->alternative_address.address,
1698 n->alternative_address.session);
1699 GNUNET_ATS_address_destroyed (GST_ats, n->alternative_address.address,
1701 unset_alternative_address (n);
1702 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1703 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1707 disconnect_neighbour (n);
1713 * Send a SESSION_CONNECT message via the given address.
1715 * @param na address to use
1718 send_session_connect (struct NeighbourAddress *na)
1720 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1721 struct SessionConnectMessage connect_msg;
1722 struct NeighbourMapEntry *n;
1724 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1725 "Sending SESSION_CONNECT message to peer `%s'\n",
1726 GNUNET_i2s (&na->address->peer));
1728 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1733 if (NULL == na->session)
1734 na->session = papi->get_session (papi->cls, na->address);
1735 if (NULL == na->session)
1740 GNUNET_STATISTICS_update (GST_stats,
1742 ("# SESSION_CONNECT messages sent"),
1744 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1745 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1746 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1747 connect_msg.reserved = htonl (0);
1748 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1750 papi->send (papi->cls,
1752 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1754 SETUP_CONNECTION_TIMEOUT,
1755 send_session_connect_cont, NULL))
1757 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1758 _("Failed to transmit CONNECT message via plugin to %s\n"),
1759 GST_plugins_a2s (na->address));
1761 n = lookup_neighbour (&na->address->peer);
1769 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1770 /* Remove address and request and additional one */
1771 unset_primary_address (n);
1772 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1773 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1774 /* Hard failure to send the CONNECT message with this address:
1775 Destroy address and session */
1777 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1778 /* Remove address and request and additional one */
1779 unset_primary_address (n);
1780 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1781 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1783 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1784 /* Remove address and request and additional one */
1785 unset_alternative_address (n);
1786 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1787 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1791 disconnect_neighbour (n);
1794 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1795 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1797 GST_neighbours_notify_data_sent (&na->address->peer,
1800 sizeof (struct SessionConnectMessage));
1805 send_session_connect_ack_cont (void *cls,
1806 const struct GNUNET_PeerIdentity *target,
1808 size_t size_payload,
1809 size_t size_on_wire)
1811 struct NeighbourMapEntry *n;
1813 n = lookup_neighbour (target);
1816 /* CONNECT_ACK continuation was called after neighbor was freed,
1817 * for example due to a time out for the state or the session
1818 * used was already terminated: nothing to do here... */
1822 if (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state)
1824 /* CONNECT_ACK continuation was called after neighbor changed state,
1825 * for example due to a time out for the state or the session
1826 * used was already terminated: nothing to do here... */
1829 if (GNUNET_OK == result)
1832 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1833 _("Failed to send CONNECT_ACK message to peer `%s' using address `%s' session %p\n"),
1834 GNUNET_i2s (target),
1835 GST_plugins_a2s (n->primary_address.address),
1836 n->primary_address.session);
1838 /* Failed to send CONNECT_ACK message with this address */
1839 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1840 n->primary_address.session);
1841 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1844 /* Remove address and request and additional one */
1845 unset_primary_address (n);
1846 n->ack_state = ACK_SEND_CONNECT_ACK;
1847 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1848 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1854 * Send a CONNECT_ACK message via the given address.
1856 * @param address address to use
1857 * @param session session to use
1858 * @param timestamp timestamp to use for the ACK message
1859 * @return GNUNET_SYSERR if sending immediately failed, GNUNET_OK otherwise
1862 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1863 struct Session *session,
1864 struct GNUNET_TIME_Absolute timestamp)
1866 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1867 struct SessionConnectMessage connect_msg;
1868 struct NeighbourMapEntry *n;
1870 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1871 "Sending CONNECT_ACK to peer `%s'\n",
1872 GNUNET_i2s (&address->peer));
1874 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1879 if (NULL == session)
1880 session = papi->get_session (papi->cls, address);
1881 if (NULL == session)
1886 GNUNET_STATISTICS_update (GST_stats,
1888 ("# CONNECT_ACK messages sent"),
1890 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1891 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1892 connect_msg.reserved = htonl (0);
1893 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1895 if (GNUNET_SYSERR == papi->send (papi->cls,
1897 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1899 GNUNET_TIME_UNIT_FOREVER_REL,
1900 send_session_connect_ack_cont, NULL))
1902 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1903 _("Failed to transmit CONNECT_ACK message via plugin to %s\n"),
1904 GST_plugins_a2s (address));
1906 n = lookup_neighbour (&address->peer);
1912 /* Hard failure to send the CONNECT_ACK message with this address:
1913 Destroy session (and address) */
1914 if (GNUNET_YES == GNUNET_HELLO_address_check_option(address,
1915 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1917 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1918 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1921 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1923 /* Remove address and request and additional one */
1924 unset_primary_address (n);
1925 n->ack_state = ACK_SEND_CONNECT_ACK;
1926 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1927 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1933 struct QuotaNotificationRequest
1935 struct GNUNET_PeerIdentity peer;
1936 struct Session *session;
1940 struct QNR_LookContext
1942 struct GNUNET_PeerIdentity peer;
1943 struct Session *session;
1946 struct QuotaNotificationRequest *res;
1950 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1952 struct QNR_LookContext *qnr_ctx = cls;
1953 struct QuotaNotificationRequest *qnr = value;
1955 if ((qnr->session == qnr_ctx->session) &&
1956 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1957 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1959 qnr_ctx->res = value;
1966 GST_neighbours_register_quota_notification(void *cls,
1967 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1968 struct Session *session)
1970 struct QuotaNotificationRequest *qnr;
1971 struct QNR_LookContext qnr_ctx;
1973 if (NULL == registered_quota_notifications)
1975 return; /* init or shutdown */
1978 qnr_ctx.peer = (*peer);
1979 qnr_ctx.plugin = plugin;
1980 qnr_ctx.session = session;
1983 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1984 peer, &find_notification_request, &qnr_ctx);
1985 if (NULL != qnr_ctx.res)
1991 qnr = GNUNET_new (struct QuotaNotificationRequest);
1992 qnr->peer = (*peer);
1993 qnr->plugin = GNUNET_strdup (plugin);
1994 qnr->session = session;
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1997 "Adding notification for peer `%s' plugin `%s' session %p \n",
1998 GNUNET_i2s (peer), plugin, session);
2000 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
2001 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2006 GST_neighbours_unregister_quota_notification(void *cls,
2007 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
2009 struct QNR_LookContext qnr_ctx;
2011 if (NULL == registered_quota_notifications)
2013 return; /* init or shutdown */
2016 qnr_ctx.peer = (*peer);
2017 qnr_ctx.plugin = plugin;
2018 qnr_ctx.session = session;
2021 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
2022 &find_notification_request, &qnr_ctx);
2023 if (NULL == qnr_ctx.res)
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2030 "Removing notification for peer `%s' plugin `%s' session %p \n",
2031 GNUNET_i2s (peer), plugin, session);
2033 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
2035 GNUNET_free (qnr_ctx.res->plugin);
2036 GNUNET_free (qnr_ctx.res);
2040 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2042 /* struct NeighbourMapEntry *n = cls; */
2043 struct QuotaNotificationRequest *qnr = value;
2044 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2045 struct GNUNET_TIME_Relative delay;
2048 papi = GST_plugins_find(qnr->plugin);
2055 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
2056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2057 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
2058 delay.rel_value_us / 1000);
2060 if (NULL != papi->update_inbound_delay)
2061 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
2067 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
2070 /* struct NeighbourMapEntry *n = cls; */
2071 struct QuotaNotificationRequest *qnr = value;
2073 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
2075 GNUNET_free(qnr->plugin);
2082 inbound_bw_tracker_update(void *cls)
2084 struct NeighbourMapEntry *n = cls;
2086 /* Quota was updated, tell plugins to update the time to receive next */
2087 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2088 &n->id, ¬ification_cb, n);
2093 * Create a fresh entry in the neighbour map for the given peer
2095 * @param peer peer to create an entry for
2096 * @return new neighbour map entry
2098 static struct NeighbourMapEntry *
2099 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2101 struct NeighbourMapEntry *n;
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2104 "Creating new neighbour entry for `%s'\n",
2106 n = GNUNET_new (struct NeighbourMapEntry);
2108 n->ack_state = ACK_UNDEFINED;
2109 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2110 n->last_util_transmission = GNUNET_TIME_absolute_get();
2111 n->util_payload_bytes_recv = 0;
2112 n->util_payload_bytes_sent = 0;
2113 n->util_total_bytes_recv = 0;
2114 n->util_total_bytes_sent = 0;
2115 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
2116 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2117 MAX_BANDWIDTH_CARRY_S);
2118 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2119 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2120 GNUNET_assert (GNUNET_OK ==
2121 GNUNET_CONTAINER_multipeermap_put (neighbours,
2123 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2127 /* We received a address suggestion after requesting an address in
2128 * try_connect or after receiving a connect, switch to address
2131 address_suggest_cont (void *cls,
2132 const struct GNUNET_PeerIdentity *peer,
2133 const struct GNUNET_HELLO_Address *address, struct Session *session,
2134 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2135 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2136 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2138 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2139 bandwidth_in, bandwidth_out);
2143 struct BlacklistCheckSwitchContext
2145 struct BlacklistCheckSwitchContext *prev;
2146 struct BlacklistCheckSwitchContext *next;
2149 struct GST_BlacklistCheck *blc;
2151 struct GNUNET_HELLO_Address *address;
2152 struct Session *session;
2153 struct GNUNET_ATS_Information *ats;
2156 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2157 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2161 * Black list check result for try_connect call
2162 * If connection to the peer is allowed request adddress and
2164 * @param cls blc_ctx bl context
2165 * @param peer the peer
2166 * @param result the result
2169 try_connect_bl_check_cont (void *cls,
2170 const struct GNUNET_PeerIdentity *peer, int result)
2172 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2173 struct NeighbourMapEntry *n;
2175 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2176 GNUNET_free (blc_ctx);
2178 if (GNUNET_OK != result)
2180 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2181 _("Blacklisting disapproved to connect to peer `%s'\n"),
2186 /* Setup a new neighbour */
2187 n = setup_neighbour (peer);
2189 /* Request address suggestions for this peer */
2190 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2191 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2192 GNUNET_ATS_reset_backoff (GST_ats, peer);
2193 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2194 &address_suggest_cont, n);
2200 * Try to create a connection to the given target (eventually).
2202 * @param target peer to try to connect to
2205 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2207 struct NeighbourMapEntry *n;
2208 struct GST_BlacklistCheck *blc;
2209 struct BlacklistCheckSwitchContext *blc_ctx;
2211 if (NULL == neighbours)
2213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2214 "Asked to connect to peer `%s' during shutdown\n",
2215 GNUNET_i2s (target));
2216 return; /* during shutdown, do nothing */
2218 n = lookup_neighbour (target);
2219 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2220 "Asked to connect to peer `%s' (state: %s)\n",
2221 GNUNET_i2s (target),
2222 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2227 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2228 /* this should not be possible */
2230 free_neighbour (n, GNUNET_NO);
2232 case GNUNET_TRANSPORT_PS_INIT_ATS:
2233 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2234 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2235 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2236 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2237 "Ignoring request to try to connect to `%s', already trying!\n",
2238 GNUNET_i2s (target));
2239 return; /* already trying */
2240 case GNUNET_TRANSPORT_PS_CONNECTED:
2241 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2242 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2243 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2245 "Ignoring request to try to connect, already connected to `%s'!\n",
2246 GNUNET_i2s (target));
2247 return; /* already connected */
2248 case GNUNET_TRANSPORT_PS_DISCONNECT:
2249 /* get rid of remains, ready to re-try immediately */
2250 free_neighbour (n, GNUNET_NO);
2252 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2253 /* should not be possible */
2257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2258 "Unhandled state `%s'\n",
2259 GNUNET_TRANSPORT_ps2s (n->state));
2261 free_neighbour (n, GNUNET_NO);
2266 /* Do blacklist check if connecting to this peer is allowed */
2267 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2268 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2270 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2271 &try_connect_bl_check_cont, blc_ctx)))
2279 * We received a 'SESSION_CONNECT' message from the other peer.
2280 * Consider switching to it.
2282 * @param message possibly a 'struct SessionConnectMessage' (check format)
2283 * @param peer identity of the peer to switch the address for
2284 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2287 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2288 const struct GNUNET_PeerIdentity *peer)
2290 const struct SessionConnectMessage *scm;
2291 struct NeighbourMapEntry *n;
2292 struct GNUNET_TIME_Absolute ts;
2294 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2296 GNUNET_break_op (0);
2297 return GNUNET_SYSERR;
2299 GNUNET_STATISTICS_update (GST_stats,
2301 ("# CONNECT messages received"),
2303 if (NULL == neighbours)
2305 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2306 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2308 return GNUNET_OK; /* we're shutting down */
2310 scm = (const struct SessionConnectMessage *) message;
2311 GNUNET_break_op (0 == ntohl (scm->reserved));
2312 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2313 n = lookup_neighbour (peer);
2316 /* This is a new neighbour and set to not connected */
2317 n = setup_neighbour (peer);
2320 /* Remember this CONNECT message in neighbour */
2321 n->ack_state = ACK_SEND_CONNECT_ACK;
2322 n->connect_ack_timestamp = ts;
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "Received CONNECT for peer `%s' in state %s/%s\n",
2327 GNUNET_TRANSPORT_ps2s (n->state),
2328 print_ack_state (n->ack_state));
2332 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2333 /* Request an address from ATS to send CONNECT_ACK to this peer */
2334 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2335 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2336 if (NULL == n->suggest_handle)
2337 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2339 case GNUNET_TRANSPORT_PS_INIT_ATS:
2340 /* CONNECT message takes priority over us asking ATS for address:
2341 * Wait for ATS to suggest an address and send CONNECT_ACK */
2342 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2343 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2345 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2346 /* We already wait for an address to send an CONNECT_ACK */
2348 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2349 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2350 /* Send ACK immediately */
2351 n->ack_state = ACK_SEND_SESSION_ACK;
2352 send_connect_ack_message (n->primary_address.address,
2353 n->primary_address.session, ts);
2355 case GNUNET_TRANSPORT_PS_CONNECTED:
2356 /* we are already connected and can thus send the ACK immediately */
2357 GNUNET_assert (NULL != n->primary_address.address);
2358 GNUNET_assert (NULL != n->primary_address.session);
2359 n->ack_state = ACK_SEND_SESSION_ACK;
2360 send_connect_ack_message (n->primary_address.address,
2361 n->primary_address.session, ts);
2363 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2364 /* We wait for ATS address suggestion */
2366 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2367 /* We received a CONNECT message while waiting for a CONNECT_ACK in fast
2368 * reconnect. Send CONNECT_ACK immediately */
2369 n->ack_state = ACK_SEND_SESSION_ACK;
2370 send_connect_ack_message (n->primary_address.address,
2371 n->primary_address.session, n->connect_ack_timestamp);
2373 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2374 /* We are already connected and can thus send the ACK immediately;
2375 still, it can never hurt to have an alternative address, so also
2376 tell ATS about it */
2377 GNUNET_assert (NULL != n->primary_address.address);
2378 GNUNET_assert (NULL != n->primary_address.session);
2379 n->ack_state = ACK_SEND_SESSION_ACK;
2380 send_connect_ack_message (n->primary_address.address,
2381 n->primary_address.session, ts);
2383 case GNUNET_TRANSPORT_PS_DISCONNECT:
2384 /* Get rid of remains without terminating sessions, ready to re-try */
2385 free_neighbour (n, GNUNET_YES);
2386 n = setup_neighbour (peer);
2387 /* Remember the CONNECT time stamp for ACK message */
2388 n->ack_state = ACK_SEND_CONNECT_ACK;
2389 n->connect_ack_timestamp = ts;
2390 /* Request an address for the peer */
2391 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2392 GNUNET_ATS_reset_backoff (GST_ats, peer);
2393 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2395 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2396 /* should not be possible */
2400 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2401 "Unhandled state `%s'\n",
2402 GNUNET_TRANSPORT_ps2s (n->state));
2404 return GNUNET_SYSERR;
2410 switch_address_bl_check_cont (void *cls,
2411 const struct GNUNET_PeerIdentity *peer, int result)
2413 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2414 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2415 struct NeighbourMapEntry *n;
2417 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2418 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2423 "Peer %s is unknown, suggestion ignored\n",
2426 if (result == GNUNET_NO)
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2429 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2430 GST_plugins_a2s (blc_ctx->address),
2432 GNUNET_i2s (&blc_ctx->address->peer));
2434 if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2437 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2438 blc_ctx->address->transport_name,
2439 GST_plugins_a2s (blc_ctx->address),
2441 GNUNET_i2s (&blc_ctx->address->peer));
2444 /* This address is blacklisted, delete address and session (if existing) in ATS */
2445 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2447 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2448 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2450 /* This is an inbound address, destroy full address */
2451 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2454 /* Remove blacklist check and clean up */
2455 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2456 GNUNET_HELLO_address_free (blc_ctx->address);
2457 GNUNET_free_non_null (blc_ctx->ats);
2458 GNUNET_free (blc_ctx);
2462 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2463 "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2464 GST_plugins_a2s (blc_ctx->address),
2466 GNUNET_i2s (&blc_ctx->address->peer));
2468 if (NULL == blc_ctx->session)
2470 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2472 "Obtained new session for peer `%s' and address '%s': %p\n",
2473 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2475 if (NULL == blc_ctx->session)
2477 /* No session could be obtained, remove blacklist check and clean up */
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479 "Failed to obtain new session for peer `%s' and address '%s'\n",
2480 GNUNET_i2s (&blc_ctx->address->peer),
2481 GST_plugins_a2s (blc_ctx->address));
2482 /* Delete address in ATS */
2483 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2485 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2486 GNUNET_HELLO_address_free (blc_ctx->address);
2487 GNUNET_free_non_null (blc_ctx->ats);
2488 GNUNET_free (blc_ctx);
2494 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2496 free_neighbour (n, GNUNET_NO);
2498 case GNUNET_TRANSPORT_PS_INIT_ATS:
2499 /* We requested an address and ATS suggests one:
2500 * set primary address and send CONNECT message*/
2501 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2502 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2503 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) )
2505 /* Send pending CONNECT_ACK message */
2506 n->ack_state = ACK_SEND_SESSION_ACK;
2507 send_connect_ack_message (n->primary_address.address,
2508 n->primary_address.session, n->connect_ack_timestamp);
2510 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2511 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2512 send_session_connect (&n->primary_address);
2514 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2515 /* ATS suggested a new address while waiting for an CONNECT_ACK:
2516 * Switch and send new CONNECT */
2517 /* ATS suggests a different address, switch again */
2518 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2519 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2520 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2522 /* Send pending CONNECT_ACK message */
2523 n->ack_state = ACK_SEND_SESSION_ACK;
2524 send_connect_ack_message (n->primary_address.address,
2525 n->primary_address.session, n->connect_ack_timestamp);
2527 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2528 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2529 send_session_connect (&n->primary_address);
2531 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2532 /* We requested an address and ATS suggests one:
2533 * set primary address and send CONNECT_ACK message*/
2534 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2535 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2536 /* Send an ACK message as a response to the CONNECT msg */
2537 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2538 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2539 send_connect_ack_message (n->primary_address.address,
2540 n->primary_address.session,
2541 n->connect_ack_timestamp);
2542 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) ||
2543 (ACK_UNDEFINED == n->ack_state) )
2544 n->ack_state = ACK_SEND_SESSION_ACK;
2546 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2547 /* ATS asks us to switch while we were trying to connect; switch to new
2548 address and check blacklist again */
2549 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) )
2551 n->ack_state = ACK_SEND_SESSION_ACK;
2552 send_connect_ack_message (n->primary_address.address,
2553 n->primary_address.session, n->connect_ack_timestamp);
2555 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2556 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2557 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2558 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2560 case GNUNET_TRANSPORT_PS_CONNECTED:
2561 GNUNET_assert (NULL != n->primary_address.address);
2562 GNUNET_assert (NULL != n->primary_address.session);
2563 if (n->primary_address.session == blc_ctx->session)
2565 /* not an address change, just a quota change */
2566 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2567 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2570 /* ATS asks us to switch a life connection; see if we can get
2571 a CONNECT_ACK on it before we actually do this! */
2572 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2573 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2574 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT,
2575 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2576 send_session_connect (&n->alternative_address);
2578 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2579 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2580 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2581 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) )
2583 /* Send pending CONNECT_ACK message */
2584 n->ack_state = ACK_SEND_SESSION_ACK;
2585 send_connect_ack_message (n->primary_address.address,
2586 n->primary_address.session, n->connect_ack_timestamp);
2588 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2589 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2590 send_session_connect (&n->primary_address);
2592 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2593 /* ATS asks us to switch while we were trying to reconnect; switch to new
2594 address and send CONNECT again */
2595 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2596 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2597 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2598 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2599 send_session_connect (&n->primary_address);
2601 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2602 if (n->primary_address.session == blc_ctx->session)
2604 /* ATS switches back to still-active session */
2605 free_address (&n->alternative_address);
2606 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2609 /* ATS asks us to switch a life connection, send */
2610 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2611 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2612 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT,
2613 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2614 send_session_connect (&n->alternative_address);
2616 case GNUNET_TRANSPORT_PS_DISCONNECT:
2617 /* not going to switch addresses while disconnecting */
2619 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2623 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2624 "Unhandled state `%s'\n",
2625 GNUNET_TRANSPORT_ps2s (n->state));
2630 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2631 GNUNET_HELLO_address_free(blc_ctx->address);
2632 GNUNET_free_non_null (blc_ctx->ats);
2633 GNUNET_free (blc_ctx);
2639 * For the given peer, switch to this address.
2641 * Before accepting this addresses and actively using it, a blacklist check
2642 * is performed. If this blacklist check fails the address will be destroyed.
2644 * @param peer identity of the peer to switch the address for
2645 * @param address address of the other peer,
2646 * @param session session to use or NULL if transport should initiate a session
2647 * @param ats performance data
2648 * @param ats_count number of entries in ats
2649 * @param bandwidth_in inbound quota to be used when connection is up,
2650 * 0 to disconnect from peer
2651 * @param bandwidth_out outbound quota to be used when connection is up,
2652 * 0 to disconnect from peer
2655 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2656 const struct GNUNET_HELLO_Address *address,
2657 struct Session *session,
2658 const struct GNUNET_ATS_Information *ats,
2660 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2661 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2663 struct NeighbourMapEntry *n;
2664 struct GST_BlacklistCheck *blc;
2665 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2666 struct BlacklistCheckSwitchContext *blc_ctx;
2669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2670 "ATS has decided on an address for peer %s\n",
2672 GNUNET_assert (NULL != address->transport_name);
2673 if (NULL == (n = lookup_neighbour (peer)))
2675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2676 "Peer %s is unknown, suggestion ignored\n",
2681 /* Check if plugin is available */
2682 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2684 /* we don't have the plugin for this address */
2685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2686 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2687 address->transport_name,
2689 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2692 if ((NULL == session) &&
2693 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2695 /* This is a inbound address and we do not have a session to use! */
2696 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2697 "Inbound address without session `%s'! Destroying address...\n",
2698 GST_plugins_a2s (address));
2699 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2703 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2704 "ATS tells us to switch to %s address '%s' session %p for "
2705 "peer `%s' in state %s/%s (quota in/out %u %u )\n",
2706 GNUNET_HELLO_address_check_option (address,
2707 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2708 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2709 GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state),
2710 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2712 /* Perform blacklist check */
2713 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2714 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2715 blc_ctx->session = session;
2716 blc_ctx->bandwidth_in = bandwidth_in;
2717 blc_ctx->bandwidth_out = bandwidth_out;
2718 blc_ctx->ats_count = ats_count;
2719 blc_ctx->ats = NULL;
2722 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2723 for (c = 0; c < ats_count; c++)
2725 blc_ctx->ats[c].type = ats[c].type;
2726 blc_ctx->ats[c].value = ats[c].value;
2730 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2731 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2732 &switch_address_bl_check_cont, blc_ctx)))
2740 send_utilization_data (void *cls,
2741 const struct GNUNET_PeerIdentity *key,
2744 struct NeighbourMapEntry *n = value;
2745 struct GNUNET_ATS_Information atsi[4];
2747 uint32_t bps_pl_out;
2750 struct GNUNET_TIME_Relative delta;
2752 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2753 GNUNET_TIME_absolute_get ());
2757 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2758 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2760 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2761 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2763 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2768 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2769 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2771 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2772 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2776 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2780 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2781 atsi[0].value = htonl (bps_out);
2782 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2783 atsi[1].value = htonl (bps_in);
2785 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2786 atsi[2].value = htonl (bps_pl_out);
2787 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2788 atsi[3].value = htonl (bps_pl_in);
2790 GST_ats_update_metrics (key, n->primary_address.address,
2791 n->primary_address.session, atsi, 4);
2792 n->util_payload_bytes_recv = 0;
2793 n->util_payload_bytes_sent = 0;
2794 n->util_total_bytes_recv = 0;
2795 n->util_total_bytes_sent = 0;
2796 n->last_util_transmission = GNUNET_TIME_absolute_get();
2802 * Task transmitting utilization in a regular interval
2804 * @param cls the 'struct NeighbourMapEntry' for which we are running
2805 * @param tc scheduler context (unused)
2808 utilization_transmission (void *cls,
2809 const struct GNUNET_SCHEDULER_TaskContext *tc)
2811 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2813 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2814 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2816 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2817 utilization_transmission, NULL);
2823 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2824 const struct GNUNET_HELLO_Address *address,
2825 struct Session *session,
2826 const struct GNUNET_MessageHeader *message)
2828 struct NeighbourMapEntry *n;
2830 n = lookup_neighbour (peer);
2833 n->util_total_bytes_recv += ntohs(message->size);
2838 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2839 const struct GNUNET_HELLO_Address *address,
2840 struct Session *session,
2841 const struct GNUNET_MessageHeader *message)
2843 struct NeighbourMapEntry *n;
2844 n = lookup_neighbour (peer);
2847 n->util_payload_bytes_recv += ntohs(message->size);
2852 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2853 const struct GNUNET_HELLO_Address *address,
2854 struct Session *session,
2857 struct NeighbourMapEntry *n;
2858 n = lookup_neighbour (peer);
2861 if (n->primary_address.session != session)
2863 n->util_total_bytes_sent += size;
2868 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2871 struct NeighbourMapEntry *n;
2872 n = lookup_neighbour (peer);
2875 n->util_payload_bytes_sent += size;
2880 * Master task run for every neighbour. Performs all of the time-related
2881 * activities (keep alive, send next message, disconnect if idle, finish
2882 * clean up after disconnect).
2884 * @param cls the 'struct NeighbourMapEntry' for which we are running
2885 * @param tc scheduler context (unused)
2888 master_task (void *cls,
2889 const struct GNUNET_SCHEDULER_TaskContext *tc)
2891 struct NeighbourMapEntry *n = cls;
2892 struct GNUNET_TIME_Relative delay;
2894 n->task = GNUNET_SCHEDULER_NO_TASK;
2895 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2897 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2898 GNUNET_i2s (&n->id),
2899 GNUNET_TRANSPORT_ps2s(n->state),
2900 GNUNET_STRINGS_relative_time_to_string (delay,
2904 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2905 /* invalid state for master task, clean up */
2907 free_neighbour (n, GNUNET_NO);
2909 case GNUNET_TRANSPORT_PS_INIT_ATS:
2910 if (0 == delay.rel_value_us)
2912 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2913 "Connection to `%s' timed out waiting for ATS to provide address\n",
2914 GNUNET_i2s (&n->id));
2915 free_neighbour (n, GNUNET_NO);
2919 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2920 if (0 == delay.rel_value_us)
2922 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2923 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2924 GNUNET_i2s (&n->id));
2925 /* We could not send to this address, delete address and session */
2926 if (NULL != n->primary_address.session)
2927 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
2928 n->primary_address.session);
2929 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
2931 /* Remove address and request and additional one */
2932 unset_primary_address (n);
2933 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2934 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2938 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2939 if (0 == delay.rel_value_us)
2941 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2942 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2943 GNUNET_i2s (&n->id));
2944 free_neighbour (n, GNUNET_NO);
2948 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2949 if (0 == delay.rel_value_us)
2951 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2952 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2953 GNUNET_i2s (&n->id));
2954 disconnect_neighbour (n);
2958 case GNUNET_TRANSPORT_PS_CONNECTED:
2959 if (0 == delay.rel_value_us)
2961 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2962 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2963 GNUNET_i2s (&n->id));
2964 disconnect_neighbour (n);
2967 try_transmission_to_peer (n);
2970 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2971 if (0 == delay.rel_value_us)
2973 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2974 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2975 GNUNET_i2s (&n->id));
2976 disconnect_neighbour (n);
2980 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2981 if (0 == delay.rel_value_us)
2983 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2984 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2985 GNUNET_i2s (&n->id));
2986 disconnect_neighbour (n);
2990 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2991 if (0 == delay.rel_value_us)
2993 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2994 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2995 GNUNET_i2s (&n->id));
2996 disconnect_neighbour (n);
2999 try_transmission_to_peer (n);
3002 case GNUNET_TRANSPORT_PS_DISCONNECT:
3003 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3004 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3005 GNUNET_i2s (&n->id));
3006 free_neighbour (n, GNUNET_NO);
3008 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3009 /* how did we get here!? */
3013 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3014 "Unhandled state `%s'\n",
3015 GNUNET_TRANSPORT_ps2s (n->state));
3019 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3020 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3022 /* if we are *now* in one of the two states, we're sending
3023 keep alive messages, so we need to consider the keepalive
3024 delay, not just the connection timeout */
3025 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3028 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3029 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3036 * Send a SESSION_ACK message to the neighbour to confirm that we
3037 * got his CONNECT_ACK.
3039 * @param n neighbour to send the SESSION_ACK to
3042 send_session_ack_message (struct NeighbourMapEntry *n)
3044 struct GNUNET_MessageHeader msg;
3046 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sending SESSION_ACK message to peer `%s'\n",
3047 GNUNET_i2s (&n->id));
3049 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3050 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3051 (void) send_with_session(n,
3052 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3053 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3059 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3060 * Consider switching to it.
3062 * @param message possibly a 'struct SessionConnectMessage' (check format)
3063 * @param peer identity of the peer to switch the address for
3064 * @param address address of the other peer, NULL if other peer
3066 * @param session session to use (or NULL)
3067 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3070 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3071 const struct GNUNET_PeerIdentity *peer,
3072 const struct GNUNET_HELLO_Address *address,
3073 struct Session *session)
3075 const struct SessionConnectMessage *scm;
3076 struct GNUNET_TIME_Absolute ts;
3077 struct NeighbourMapEntry *n;
3079 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3080 "Received CONNECT_ACK message from peer `%s'\n",
3083 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3085 GNUNET_break_op (0);
3086 return GNUNET_SYSERR;
3088 GNUNET_STATISTICS_update (GST_stats,
3090 ("# CONNECT_ACK messages received"),
3092 scm = (const struct SessionConnectMessage *) message;
3093 GNUNET_break_op (ntohl (scm->reserved) == 0);
3094 if (NULL == (n = lookup_neighbour (peer)))
3096 GNUNET_STATISTICS_update (GST_stats,
3098 ("# unexpected CONNECT_ACK messages (no peer)"),
3100 return GNUNET_SYSERR;
3102 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3105 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3107 free_neighbour (n, GNUNET_NO);
3108 return GNUNET_SYSERR;
3109 case GNUNET_TRANSPORT_PS_INIT_ATS:
3110 GNUNET_STATISTICS_update (GST_stats,
3112 ("# unexpected CONNECT_ACK messages (not ready)"),
3115 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3116 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3118 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3119 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3122 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3123 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3124 GNUNET_STATISTICS_set (GST_stats,
3125 gettext_noop ("# peers connected"),
3126 ++neighbours_connected,
3128 connect_notify_cb (callback_cls, &n->id,
3129 n->primary_address.bandwidth_in,
3130 n->primary_address.bandwidth_out);
3131 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3132 GST_ats_add_address (n->primary_address.address,
3133 n->primary_address.session,
3135 set_primary_address (n,
3136 n->primary_address.address,
3137 n->primary_address.session,
3138 n->primary_address.bandwidth_in,
3139 n->primary_address.bandwidth_out,
3141 send_session_ack_message (n);
3143 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3144 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3145 GNUNET_STATISTICS_update (GST_stats,
3147 ("# unexpected CONNECT_ACK messages (not ready)"),
3150 case GNUNET_TRANSPORT_PS_CONNECTED:
3151 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3152 send_session_ack_message (n);
3154 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3155 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3156 to give us a new address... */
3157 GNUNET_STATISTICS_update (GST_stats,
3159 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3162 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3163 /* Reconnecting with new address address worked; go back to connected! */
3164 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3165 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3166 send_session_ack_message (n);
3168 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3169 /* new address worked; adopt it and go back to connected! */
3170 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3171 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3172 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3174 GST_ats_add_address (n->alternative_address.address,
3175 n->alternative_address.session,
3177 set_primary_address (n, n->alternative_address.address,
3178 n->alternative_address.session, n->alternative_address.bandwidth_in,
3179 n->alternative_address.bandwidth_out, GNUNET_YES);
3181 free_address (&n->alternative_address);
3182 send_session_ack_message (n);
3184 case GNUNET_TRANSPORT_PS_DISCONNECT:
3185 GNUNET_STATISTICS_update (GST_stats,
3187 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3189 return GNUNET_SYSERR;
3190 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3194 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3195 "Unhandled state `%s'\n",
3196 GNUNET_TRANSPORT_ps2s (n->state));
3198 return GNUNET_SYSERR;
3205 * A session was terminated. Take note; if needed, try to get
3206 * an alternative address from ATS.
3208 * @param peer identity of the peer where the session died
3209 * @param session session that is gone
3210 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3211 * this session was not in use
3214 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3215 struct Session *session)
3217 struct NeighbourMapEntry *n;
3218 struct BlackListCheckContext *bcc;
3219 struct BlackListCheckContext *bcc_next;
3221 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3223 while (NULL != (bcc = bcc_next))
3225 bcc_next = bcc->next;
3226 if (bcc->na.session == session)
3228 if (NULL != bcc->bc)
3229 GST_blacklist_test_cancel (bcc->bc);
3230 GNUNET_HELLO_address_free (bcc->na.address);
3231 GNUNET_CONTAINER_DLL_remove (bc_head,
3237 if (NULL == (n = lookup_neighbour (peer)))
3238 return GNUNET_NO; /* can't affect us */
3239 if (session != n->primary_address.session)
3241 if (session == n->alternative_address.session)
3243 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3244 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3247 free_address (&n->alternative_address);
3249 return GNUNET_NO; /* doesn't affect us further */
3252 n->expect_latency_response = GNUNET_NO;
3253 /* The session for neighbour's primary address died */
3256 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3258 free_neighbour (n, GNUNET_NO);
3260 case GNUNET_TRANSPORT_PS_INIT_ATS:
3262 free_neighbour (n, GNUNET_NO);
3264 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3265 /* The session used to send the CONNECT terminated:
3266 * this implies a connect error*/
3267 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3268 "Failed to send CONNECT in %s with `%s' %p: session terminated\n",
3270 GST_plugins_a2s (n->primary_address.address),
3271 n->primary_address.session,
3274 /* Destroy the address since it cannot be used */
3275 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3276 unset_primary_address (n);
3277 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3278 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3280 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3281 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3282 /* error on inbound session; free neighbour entirely */
3283 free_address (&n->primary_address);
3284 free_neighbour (n, GNUNET_NO);
3286 case GNUNET_TRANSPORT_PS_CONNECTED:
3287 unset_primary_address (n);
3288 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3289 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3291 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3292 /* we don't have an address, how can it go down? */
3295 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3296 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3297 "Failed to send CONNECT in %s with `%s' %p: session terminated\n",
3299 GST_plugins_a2s (n->primary_address.address),
3300 n->primary_address.session,
3303 /* Destroy the address since it cannot be used */
3304 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3305 unset_primary_address (n);
3306 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3307 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3309 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3310 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3311 secondary as primary */
3313 /* Destroy the address since it cannot be used */
3314 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3315 free_address (&n->primary_address);
3316 n->primary_address = n->alternative_address;
3317 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3318 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3320 case GNUNET_TRANSPORT_PS_DISCONNECT:
3321 free_address (&n->primary_address);
3323 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3324 /* neighbour was freed and plugins told to terminate session */
3328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3329 "Unhandled state `%s'\n",
3330 GNUNET_TRANSPORT_ps2s (n->state));
3334 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3335 GNUNET_SCHEDULER_cancel (n->task);
3336 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3342 * We received a 'SESSION_ACK' message from the other peer.
3343 * If we sent a 'CONNECT_ACK' last, this means we are now
3344 * connected. Otherwise, do nothing.
3346 * @param message possibly a 'struct SessionConnectMessage' (check format)
3347 * @param peer identity of the peer to switch the address for
3348 * @param address address of the other peer, NULL if other peer
3350 * @param session session to use (or NULL)
3351 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3354 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3355 const struct GNUNET_PeerIdentity *peer,
3356 const struct GNUNET_HELLO_Address *address,
3357 struct Session *session)
3359 struct NeighbourMapEntry *n;
3361 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3362 "Received SESSION_ACK message from peer `%s'\n",
3364 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3366 GNUNET_break_op (0);
3367 return GNUNET_SYSERR;
3369 GNUNET_STATISTICS_update (GST_stats,
3371 ("# SESSION_ACK messages received"),
3373 if (NULL == (n = lookup_neighbour (peer)))
3375 GNUNET_break_op (0);
3376 return GNUNET_SYSERR;
3378 /* check if we are in a plausible state for having sent
3379 a CONNECT_ACK. If not, return, otherwise break */
3381 /* TODO I have no idea we we should state GNUNET_TRANSPORT_PS_CONNECT_SENT
3382 * Perhaps SWITCHING? Have to check */
3383 if ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) /*&&
3384 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) ) ||
3385 (ACK_SEND_SESSION_ACK != n->ack_state)*/)
3387 GNUNET_break (0); /* TESTING */
3389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3390 "Received unexpected SESSION_ACK message from peer `%s' in state %s/%s\n",
3392 GNUNET_TRANSPORT_ps2s (n->state),
3393 print_ack_state (n->ack_state));
3394 GNUNET_STATISTICS_update (GST_stats,
3395 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3400 /* We are connected */
3401 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3402 GNUNET_STATISTICS_set (GST_stats,
3403 gettext_noop ("# peers connected"),
3404 ++neighbours_connected,
3407 /* Notify about connection */
3408 connect_notify_cb (callback_cls, &n->id,
3409 n->primary_address.bandwidth_in,
3410 n->primary_address.bandwidth_out);
3412 /* Add session to ATS since no session was given (NULL) and we may have
3413 * obtained a new session */
3414 GST_ats_add_address (n->primary_address.address, n->primary_address.session,
3417 /* Set primary address to used */
3418 set_primary_address (n,
3419 n->primary_address.address,
3420 n->primary_address.session,
3421 n->primary_address.bandwidth_in,
3422 n->primary_address.bandwidth_out,
3429 * Test if we're connected to the given peer.
3431 * @param target peer to test
3432 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3435 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3437 return test_connected (lookup_neighbour (target));
3441 * Change the incoming quota for the given peer.
3443 * @param neighbour identity of peer to change qutoa for
3444 * @param quota new quota
3447 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3448 struct GNUNET_BANDWIDTH_Value32NBO quota)
3450 struct NeighbourMapEntry *n;
3452 if (NULL == (n = lookup_neighbour (neighbour)))
3454 GNUNET_STATISTICS_update (GST_stats,
3456 ("# SET QUOTA messages ignored (no such peer)"),
3460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3461 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3462 ntohl (quota.value__), GNUNET_i2s (&n->id));
3463 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3464 if (0 != ntohl (quota.value__))
3466 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3467 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3468 GNUNET_i2s (&n->id));
3469 if (GNUNET_YES == test_connected (n))
3470 GNUNET_STATISTICS_update (GST_stats,
3471 gettext_noop ("# disconnects due to quota of 0"),
3473 disconnect_neighbour (n);
3478 * We received a disconnect message from the given peer,
3479 * validate and process.
3481 * @param peer sender of the message
3482 * @param msg the disconnect message
3485 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3486 const struct GNUNET_MessageHeader *msg)
3488 struct NeighbourMapEntry *n;
3489 const struct SessionDisconnectMessage *sdm;
3491 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3492 "Received DISCONNECT message from peer `%s'\n",
3494 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3496 // GNUNET_break_op (0);
3497 GNUNET_STATISTICS_update (GST_stats,
3499 ("# disconnect messages ignored (old format)"), 1,
3503 GNUNET_STATISTICS_update (GST_stats,
3505 ("# DISCONNECT messages received"),
3507 sdm = (const struct SessionDisconnectMessage *) msg;
3508 if (NULL == (n = lookup_neighbour (peer)))
3509 return; /* gone already */
3510 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3512 GNUNET_STATISTICS_update (GST_stats,
3514 ("# disconnect messages ignored (timestamp)"), 1,
3518 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3520 GNUNET_break_op (0);
3523 if (ntohl (sdm->purpose.size) !=
3524 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3525 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3526 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3528 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3529 "%s message from peer `%s' has invalid size \n",
3532 GNUNET_break_op (0);
3536 GNUNET_CRYPTO_eddsa_verify
3537 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3538 &sdm->signature, &sdm->public_key))
3540 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3541 "%s message from peer `%s' cannot be verified \n",
3544 GNUNET_break_op (0);
3547 if (GNUNET_YES == test_connected (n))
3548 GNUNET_STATISTICS_update (GST_stats,
3550 ("# other peer asked to disconnect from us"), 1,
3552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3553 "Disconnecting by request from peer %s\n",
3555 disconnect_neighbour (n);
3560 * Closure for the neighbours_iterate function.
3562 struct IteratorContext
3565 * Function to call on each connected neighbour.
3567 GST_NeighbourIterator cb;
3577 * Call the callback from the closure for each neighbour.
3579 * @param cls the `struct IteratorContext`
3580 * @param key the hash of the public key of the neighbour
3581 * @param value the `struct NeighbourMapEntry`
3582 * @return #GNUNET_OK (continue to iterate)
3585 neighbours_iterate (void *cls,
3586 const struct GNUNET_PeerIdentity *key,
3589 struct IteratorContext *ic = cls;
3590 struct NeighbourMapEntry *n = value;
3591 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3592 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3595 if (NULL != n->primary_address.address)
3597 bandwidth_in = n->primary_address.bandwidth_in;
3598 bandwidth_out = n->primary_address.bandwidth_out;
3602 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3603 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3607 n->primary_address.address,
3610 bandwidth_in, bandwidth_out);
3616 * Iterate over all connected neighbours.
3618 * @param cb function to call
3619 * @param cb_cls closure for cb
3622 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3624 struct IteratorContext ic;
3626 if (NULL == neighbours)
3627 return; /* can happen during shutdown */
3630 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3635 * If we have an active connection to the given target, it must be shutdown.
3637 * @param target peer to disconnect from
3640 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3642 struct NeighbourMapEntry *n;
3644 if (NULL == (n = lookup_neighbour (target)))
3645 return; /* not active */
3646 if (GNUNET_YES == test_connected (n))
3647 GNUNET_STATISTICS_update (GST_stats,
3649 ("# disconnected from peer upon explicit request"), 1,
3651 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3652 "Forced disconnect from peer %s\n",
3653 GNUNET_i2s (target));
3654 disconnect_neighbour (n);
3659 * Obtain current latency information for the given neighbour.
3661 * @param peer to get the latency for
3662 * @return observed latency of the address, FOREVER if the
3663 * the connection is not up
3665 struct GNUNET_TIME_Relative
3666 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3668 struct NeighbourMapEntry *n;
3670 n = lookup_neighbour (peer);
3672 return GNUNET_TIME_UNIT_FOREVER_REL;
3675 case GNUNET_TRANSPORT_PS_CONNECTED:
3676 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3677 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3678 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3680 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3681 case GNUNET_TRANSPORT_PS_INIT_ATS:
3682 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3683 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3684 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3685 case GNUNET_TRANSPORT_PS_DISCONNECT:
3686 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3687 return GNUNET_TIME_UNIT_FOREVER_REL;
3689 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3690 "Unhandled state `%s'\n",
3691 GNUNET_TRANSPORT_ps2s (n->state));
3695 return GNUNET_TIME_UNIT_FOREVER_REL;
3700 * Obtain current address information for the given neighbour.
3703 * @return address currently used
3705 struct GNUNET_HELLO_Address *
3706 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3708 struct NeighbourMapEntry *n;
3710 n = lookup_neighbour (peer);
3713 return n->primary_address.address;
3718 * Initialize the neighbours subsystem.
3720 * @param cls closure for callbacks
3721 * @param connect_cb function to call if we connect to a peer
3722 * @param disconnect_cb function to call if we disconnect from a peer
3723 * @param peer_address_cb function to call if we change an active address
3725 * @param max_fds maximum number of fds to use
3728 GST_neighbours_start (void *cls,
3729 NotifyConnect connect_cb,
3730 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3731 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3732 unsigned int max_fds)
3735 connect_notify_cb = connect_cb;
3736 disconnect_notify_cb = disconnect_cb;
3737 neighbour_change_cb = peer_address_cb;
3738 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3739 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3740 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3741 utilization_transmission, NULL);
3746 * Disconnect from the given neighbour.
3749 * @param key hash of neighbour's public key (not used)
3750 * @param value the 'struct NeighbourMapEntry' of the neighbour
3751 * @return #GNUNET_OK (continue to iterate)
3754 disconnect_all_neighbours (void *cls,
3755 const struct GNUNET_PeerIdentity *key,
3758 struct NeighbourMapEntry *n = value;
3760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3761 "Disconnecting peer `%4s', %s\n",
3762 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3763 free_neighbour (n, GNUNET_NO);
3769 * Cleanup the neighbours subsystem.
3772 GST_neighbours_stop ()
3774 struct BlacklistCheckSwitchContext *cur;
3775 struct BlacklistCheckSwitchContext *next;
3777 if (NULL == neighbours)
3779 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3781 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3782 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3785 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3786 &disconnect_all_neighbours,
3788 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3790 next = pending_bc_head;
3791 for (cur = next; NULL != cur; cur = next )
3794 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3796 if (NULL != cur->blc)
3798 GST_blacklist_test_cancel (cur->blc);
3801 if (NULL != cur->address)
3802 GNUNET_HELLO_address_free (cur->address);
3803 GNUNET_free_non_null (cur->ats);
3807 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
3808 &free_notification_cb, NULL);
3809 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
3810 registered_quota_notifications = NULL;
3813 callback_cls = NULL;
3814 connect_notify_cb = NULL;
3815 disconnect_notify_cb = NULL;
3816 neighbour_change_cb = NULL;
3820 /* end of file gnunet-service-transport_neighbours.c */