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, 500)
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 * Task to disconnect neighbour after we received a DISCONNECT message
348 GNUNET_SCHEDULER_TaskIdentifier delayed_disconnect_task;
351 * At what time should we sent the next keep-alive message?
353 struct GNUNET_TIME_Absolute keep_alive_time;
356 * At what time did we sent the last keep-alive message? Used
357 * to calculate round-trip time ("latency").
359 struct GNUNET_TIME_Absolute last_keep_alive_time;
362 * Timestamp we should include in our next CONNECT_ACK message.
363 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
364 * our CONNECT_ACK message.
366 struct GNUNET_TIME_Absolute connect_ack_timestamp;
369 * ATS address suggest handle
371 struct GNUNET_ATS_SuggestHandle *suggest_handle;
374 * Time where we should cut the connection (timeout) if we don't
375 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
376 * if we are in S_CONNECTED).
378 struct GNUNET_TIME_Absolute timeout;
381 * Latest calculated latency value
383 struct GNUNET_TIME_Relative latency;
386 * Tracker for inbound bandwidth.
388 struct GNUNET_BANDWIDTH_Tracker in_tracker;
391 * How often has the other peer (recently) violated the inbound
392 * traffic limit? Incremented by 10 per violation, decremented by 1
393 * per non-violation (for each time interval).
395 unsigned int quota_violation_count;
398 * The current state of the peer.
400 enum GNUNET_TRANSPORT_PeerState state;
403 * Did we sent an KEEP_ALIVE message and are we expecting a response?
405 int expect_latency_response;
408 * When a peer wants to connect we have to reply to the 1st CONNECT message
409 * with a CONNECT_ACK message. But sometime we cannot send this message
410 * immediately since we do not have an address and then we have to remember
411 * to send this message as soon as we have an address.
413 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
414 * (once we have an address to use and the peer has been allowed by our
415 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_CONNECT_ACK
416 * if we need to send a CONNECT_ACK. Set to #ACK_SEND_SESSION_ACK if we did
417 * send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving a
418 * 'SESSION_ACK' (regardless of what our own state machine might say).
420 enum GST_ACK_State ack_state;
423 * Tracking utilization of outbound bandwidth
425 uint32_t util_payload_bytes_sent;
428 * Tracking utilization of inbound bandwidth
430 uint32_t util_payload_bytes_recv;
433 * Tracking utilization of outbound bandwidth
435 uint32_t util_total_bytes_sent;
438 * Tracking utilization of inbound bandwidth
440 uint32_t util_total_bytes_recv;
443 * Date of last utilization transmission
445 struct GNUNET_TIME_Absolute last_util_transmission;
450 * Context for blacklist checks and the #try_connect_bl_check_cont()
451 * function. Stores information about ongoing blacklist checks.
453 struct BlackListCheckContext
457 * We keep blacklist checks in a DLL.
459 struct BlackListCheckContext *next;
462 * We keep blacklist checks in a DLL.
464 struct BlackListCheckContext *prev;
467 * Address that is being checked.
469 struct NeighbourAddress na;
472 * Handle to the ongoing blacklist check.
474 struct GST_BlacklistCheck *bc;
479 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
481 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
484 * We keep blacklist checks in a DLL so that we can find
485 * the 'sessions' in their 'struct NeighbourAddress' if
486 * a session goes down.
488 static struct BlackListCheckContext *bc_head;
491 * We keep blacklist checks in a DLL.
493 static struct BlackListCheckContext *bc_tail;
496 * List of pending blacklist checks: head
498 static struct BlacklistCheckSwitchContext *pending_bc_head;
501 * List of pending blacklist checks: tail
503 static struct BlacklistCheckSwitchContext *pending_bc_tail;
506 * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb
508 static void *callback_cls;
511 * Function to call when we connected to a neighbour.
513 static NotifyConnect connect_notify_cb;
516 * Function to call when we disconnected from a neighbour.
518 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
521 * Function to call when a neighbour changed address, state or bandwidth.
523 static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb;
526 * counter for connected neighbours
528 static unsigned int neighbours_connected;
531 * Number of bytes we have currently queued for transmission.
533 static unsigned long long bytes_in_send_queue;
536 * Task transmitting utilization data
538 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
541 static struct GNUNET_CONTAINER_MultiPeerMap *registered_quota_notifications;
544 print_ack_state (enum GST_ACK_State s)
549 case ACK_SEND_CONNECT_ACK:
550 return "SEND_CONNECT_ACK";
551 case ACK_SEND_SESSION_ACK:
552 return "SEND_SESSION_ACK";
560 * Lookup a neighbour entry in the neighbours hash map.
562 * @param pid identity of the peer to look up
563 * @return the entry, NULL if there is no existing record
565 static struct NeighbourMapEntry *
566 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
568 if (NULL == neighbours)
570 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
575 * Test if we're connected to the given peer.
577 * @param n neighbour entry of peer to test
578 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
581 test_connected (struct NeighbourMapEntry *n)
585 return GNUNET_TRANSPORT_is_connected (n->state);
589 * Send information about a new outbound quota to our clients.
591 * @param target affected peer
592 * @param quota new quota
595 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
596 struct GNUNET_BANDWIDTH_Value32NBO quota)
598 struct QuotaSetMessage q_msg;
600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
601 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
602 ntohl (quota.value__), GNUNET_i2s (target));
603 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
604 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
606 q_msg.peer = (*target);
607 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
612 * We don't need a given neighbour address any more.
613 * Release its resources and give appropriate notifications
614 * to ATS and other subsystems.
616 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
619 free_address (struct NeighbourAddress *na)
621 if (GNUNET_YES == na->ats_active)
623 GST_validation_set_address_use (na->address, na->session, GNUNET_NO);
624 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
627 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
628 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
629 na->ats_active = GNUNET_NO;
630 na->keep_alive_nonce = 0;
631 if (NULL != na->address)
633 GNUNET_HELLO_address_free (na->address);
641 * Set net state for this neighbour and notify monitoring
643 * @param n the respective neighbour
644 * @param s the new state
647 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
650 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
652 GNUNET_TRANSPORT_ps2s(s));
653 neighbour_change_cb (callback_cls,
655 n->primary_address.address,
656 n->state, n->timeout,
657 n->primary_address.bandwidth_in,
658 n->primary_address.bandwidth_out);
663 * Set net state and state timeout for this neighbour and notify monitoring
665 * @param n the respective neighbour
666 * @param s the new state
667 * @param timeout the new timeout
670 set_state_and_timeout (struct NeighbourMapEntry *n,
671 enum GNUNET_TRANSPORT_PeerState s,
672 struct GNUNET_TIME_Absolute timeout)
675 n->timeout = timeout;
676 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
678 GNUNET_TRANSPORT_ps2s(s),
679 GNUNET_STRINGS_absolute_time_to_string (timeout));
680 neighbour_change_cb (callback_cls,
682 n->primary_address.address,
683 n->state, n->timeout,
684 n->primary_address.bandwidth_in,
685 n->primary_address.bandwidth_out);
690 * Set new state timeout for this neighbour and notify monitoring
692 * @param n the respective neighbour
693 * @param timeout the new timeout
696 set_timeout (struct NeighbourMapEntry *n,
697 struct GNUNET_TIME_Absolute timeout)
699 n->timeout = timeout;
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Neighbour `%s' changed timeout %s\n",
702 GNUNET_STRINGS_absolute_time_to_string (timeout));
703 neighbour_change_cb (callback_cls,
705 n->primary_address.address,
706 n->state, n->timeout,
707 n->primary_address.bandwidth_in,
708 n->primary_address.bandwidth_out);
713 * Initialize the alternative address of a neighbour
715 * @param n the neighbour
716 * @param address address of the other peer, NULL if other peer
718 * @param session session to use (or NULL, in which case an
719 * address must be setup)
720 * @param bandwidth_in inbound quota to be used when connection is up
721 * @param bandwidth_out outbound quota to be used when connection is up
724 set_alternative_address (struct NeighbourMapEntry *n,
725 const struct GNUNET_HELLO_Address *address,
726 struct Session *session,
727 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
728 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
730 struct GNUNET_TRANSPORT_PluginFunctions *papi;
731 if (NULL == (papi = GST_plugins_find (address->transport_name)))
736 if (session == n->alternative_address.session)
738 n->alternative_address.bandwidth_in = bandwidth_in;
739 n->alternative_address.bandwidth_out = bandwidth_out;
742 free_address (&n->alternative_address);
744 session = papi->get_session (papi->cls, address);
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
748 "Failed to obtain new session for peer `%s' and address '%s'\n",
749 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
750 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
754 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
756 GST_plugins_a2s(address));
758 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
759 n->alternative_address.bandwidth_in = bandwidth_in;
760 n->alternative_address.bandwidth_out = bandwidth_out;
761 n->alternative_address.session = session;
762 n->alternative_address.ats_active = GNUNET_NO;
763 n->alternative_address.keep_alive_nonce = 0;
768 * Initialize the primary address of a neighbour
770 * @param n the neighbour
771 * @param address address of the other peer, NULL if other peer
773 * @param session session to use (or NULL, in which case an
774 * address must be setup)
775 * @param bandwidth_in inbound quota to be used when connection is up
776 * @param bandwidth_out outbound quota to be used when connection is up
777 * @param is_active #GNUNET_YES to mark this as the active address with ATS
780 set_primary_address (struct NeighbourMapEntry *n,
781 const struct GNUNET_HELLO_Address *address,
782 struct Session *session,
783 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
784 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
787 struct GNUNET_TRANSPORT_PluginFunctions *papi;
789 if (NULL == (papi = GST_plugins_find (address->transport_name)))
794 if (session == n->primary_address.session)
796 n->primary_address.bandwidth_in = bandwidth_in;
797 n->primary_address.bandwidth_out = bandwidth_out;
798 if (is_active != n->primary_address.ats_active)
800 n->primary_address.ats_active = is_active;
801 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
802 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
804 if (GNUNET_YES == is_active)
806 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
807 send_outbound_quota (&address->peer, bandwidth_out);
811 free_address (&n->primary_address);
813 session = papi->get_session (papi->cls, address);
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817 "Failed to obtain new session for peer `%s' and address '%s'\n",
818 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
819 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
823 n->primary_address.address = GNUNET_HELLO_address_copy (address);
824 n->primary_address.bandwidth_in = bandwidth_in;
825 n->primary_address.bandwidth_out = bandwidth_out;
826 n->primary_address.session = session;
827 n->primary_address.ats_active = is_active;
828 n->primary_address.keep_alive_nonce = 0;
829 if (GNUNET_YES == is_active)
831 /* Telling ATS about new session */
832 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
833 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
834 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
835 send_outbound_quota (&address->peer, bandwidth_out);
838 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
840 GST_plugins_a2s(address));
842 neighbour_change_cb (callback_cls,
844 n->primary_address.address,
845 n->state, n->timeout,
846 n->primary_address.bandwidth_in,
847 n->primary_address.bandwidth_out);
851 * Clear the primary address of a neighbour since this address is not
852 * valid anymore and notify monitoring about it
854 * @param n the neighbour
857 unset_primary_address (struct NeighbourMapEntry *n)
859 /* Unset primary address */
860 free_address (&n->primary_address);
862 /* Notify monitoring about it */
863 neighbour_change_cb (callback_cls,
866 n->state, n->timeout,
867 n->primary_address.bandwidth_in,
868 n->primary_address.bandwidth_out);
872 * Clear the alternative address of a neighbour since this address is not
875 * @param n the neighbour
878 unset_alternative_address (struct NeighbourMapEntry *n)
880 /* Unset primary address */
881 free_address (&n->alternative_address);
885 * Free a neighbour map entry.
887 * @param n entry to free
888 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
889 * #GNUNET_YES to keep all sessions
892 free_neighbour (struct NeighbourMapEntry *n,
895 struct MessageQueue *mq;
896 struct GNUNET_TRANSPORT_PluginFunctions *papi;
897 struct GNUNET_HELLO_Address *backup_primary;
899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
900 "Freeing neighbour state of peer `%s'\n",
901 GNUNET_i2s (&n->id));
902 n->is_active = NULL; /* always free'd by its own continuation! */
904 /* fail messages currently in the queue */
905 while (NULL != (mq = n->messages_head))
907 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
908 if (NULL != mq->cont)
909 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
912 /* It is too late to send other peer disconnect notifications, but at
913 least internally we need to get clean... */
914 if (GNUNET_YES == test_connected (n))
916 GNUNET_STATISTICS_set (GST_stats,
917 gettext_noop ("# peers connected"),
918 --neighbours_connected,
920 disconnect_notify_cb (callback_cls, &n->id);
923 /* Mark peer as disconnected */
924 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
926 if (NULL != n->primary_address.address)
927 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
929 backup_primary = NULL;
931 /* free addresses and mark as unused */
932 unset_primary_address (n);
933 free_address (&n->alternative_address);
935 /* cut all transport-level connection for this peer */
936 if ((GNUNET_NO == keep_sessions) &&
937 (NULL != backup_primary) &&
938 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
939 papi->disconnect_peer (papi->cls, &n->id);
941 if (NULL != backup_primary)
942 GNUNET_HELLO_address_free (backup_primary);
943 GNUNET_assert (GNUNET_YES ==
944 GNUNET_CONTAINER_multipeermap_remove (neighbours,
947 /* Cancel address requests for this peer */
948 if (NULL != n->suggest_handle)
950 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
951 n->suggest_handle = NULL;
954 /* Cancel the disconnect task */
955 if (GNUNET_SCHEDULER_NO_TASK != n->delayed_disconnect_task)
957 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
958 n->delayed_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
961 /* Cancel the master task */
962 if (GNUNET_SCHEDULER_NO_TASK != n->task)
964 GNUNET_SCHEDULER_cancel (n->task);
965 n->task = GNUNET_SCHEDULER_NO_TASK;
967 /* free rest of memory */
973 * Transmit a message using the current session of the given
976 * @param n entry for the recipient
977 * @param msgbuf buffer to transmit
978 * @param msgbuf_size number of bytes in @a msgbuf buffer
979 * @param priority transmission priority
980 * @param timeout transmission timeout
981 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
982 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
983 * @param cont continuation to call when finished (can be NULL)
984 * @param cont_cls closure for @a cont
985 * @return timeout (copy of @a timeout or a calculated one if
986 * @a use_keepalive_timeout is #GNUNET_YES.
988 static struct GNUNET_TIME_Relative
989 send_with_session (struct NeighbourMapEntry *n,
990 const char *msgbuf, size_t msgbuf_size,
992 struct GNUNET_TIME_Relative timeout,
993 unsigned int use_keepalive_timeout,
994 GNUNET_TRANSPORT_TransmitContinuation cont,
997 struct GNUNET_TRANSPORT_PluginFunctions *papi;
998 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
1000 GNUNET_assert (n->primary_address.session != NULL);
1001 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1002 (-1 == papi->send (papi->cls,
1003 n->primary_address.session,
1004 msgbuf, msgbuf_size,
1006 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
1007 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1008 papi->query_keepalive_factor (papi->cls))),
1009 cont, cont_cls)))) &&
1011 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1012 GST_neighbours_notify_data_sent (&n->id,
1013 n->primary_address.address, n->primary_address.session, msgbuf_size);
1014 GNUNET_break (NULL != papi);
1020 * Master task run for every neighbour. Performs all of the time-related
1021 * activities (keep alive, send next message, disconnect if idle, finish
1022 * clean up after disconnect).
1024 * @param cls the `struct NeighbourMapEntry` for which we are running
1025 * @param tc scheduler context (unused)
1028 master_task (void *cls,
1029 const struct GNUNET_SCHEDULER_TaskContext *tc);
1033 * Function called when the 'DISCONNECT' message has been sent by the
1034 * plugin. Frees the neighbour --- if the entry still exists.
1037 * @param target identity of the neighbour that was disconnected
1038 * @param result #GNUNET_OK if the disconnect got out successfully
1039 * @param payload bytes payload
1040 * @param physical bytes physical
1043 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1044 int result, size_t payload, size_t physical)
1046 struct NeighbourMapEntry *n;
1048 n = lookup_neighbour (target);
1050 return; /* already gone */
1051 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1052 return; /* have created a fresh entry since */
1053 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1054 GNUNET_SCHEDULER_cancel (n->task);
1055 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1060 * Transmit a DISCONNECT message to the other peer.
1062 * @param n neighbour to send DISCONNECT message.
1065 send_disconnect (struct NeighbourMapEntry *n)
1067 struct SessionDisconnectMessage disconnect_msg;
1069 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1070 "Sending DISCONNECT message to peer `%4s'\n",
1071 GNUNET_i2s (&n->id));
1072 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1073 disconnect_msg.header.type =
1074 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1075 disconnect_msg.reserved = htonl (0);
1076 disconnect_msg.purpose.size =
1077 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1078 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1079 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1080 disconnect_msg.purpose.purpose =
1081 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1082 disconnect_msg.timestamp =
1083 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1084 disconnect_msg.public_key = GST_my_identity.public_key;
1085 GNUNET_assert (GNUNET_OK ==
1086 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1087 &disconnect_msg.purpose,
1088 &disconnect_msg.signature));
1090 (void) send_with_session (n, (const char *) &disconnect_msg,
1091 sizeof (disconnect_msg), UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1092 GNUNET_NO, &send_disconnect_cont, NULL );
1093 GNUNET_STATISTICS_update (GST_stats,
1095 ("# DISCONNECT messages sent"), 1,
1101 * Disconnect from the given neighbour, clean up the record.
1103 * @param n neighbour to disconnect from
1106 disconnect_neighbour (struct NeighbourMapEntry *n)
1108 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1109 "Disconnecting from peer %s in state %s\n",
1110 GNUNET_i2s (&n->id),
1111 GNUNET_TRANSPORT_ps2s (n->state));
1112 /* depending on state, notify neighbour and/or upper layers of this peer
1116 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1117 case GNUNET_TRANSPORT_PS_INIT_ATS:
1118 /* other peer is completely unaware of us, no need to send DISCONNECT */
1119 free_neighbour (n, GNUNET_NO);
1121 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1122 send_disconnect (n);
1123 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1125 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1126 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1127 free_neighbour (n, GNUNET_NO);
1129 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1130 /* we DID ACK the other peer's request, must send DISCONNECT */
1131 send_disconnect (n);
1132 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1134 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1135 case GNUNET_TRANSPORT_PS_CONNECTED:
1136 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1137 /* we are currently connected, need to send disconnect and do
1138 internal notifications and update statistics */
1139 send_disconnect (n);
1140 GNUNET_STATISTICS_set (GST_stats,
1141 gettext_noop ("# peers connected"),
1142 --neighbours_connected,
1144 disconnect_notify_cb (callback_cls, &n->id);
1145 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1147 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1148 /* Disconnecting while waiting for an ATS address to reconnect,
1149 * cannot send DISCONNECT */
1150 free_neighbour (n, GNUNET_NO);
1152 case GNUNET_TRANSPORT_PS_DISCONNECT:
1153 /* already disconnected, ignore */
1155 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1156 /* already cleaned up, how did we get here!? */
1160 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1161 "Unhandled state `%s'\n",
1162 GNUNET_TRANSPORT_ps2s (n->state));
1166 /* schedule timeout to clean up */
1167 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1168 GNUNET_SCHEDULER_cancel (n->task);
1169 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1175 * We're done with our transmission attempt, continue processing.
1177 * @param cls the `struct MessageQueue` of the message
1178 * @param receiver intended receiver
1179 * @param success whether it worked or not
1180 * @param size_payload bytes payload sent
1181 * @param physical bytes sent on wire
1184 transmit_send_continuation (void *cls,
1185 const struct GNUNET_PeerIdentity *receiver,
1186 int success, size_t size_payload, size_t physical)
1188 struct MessageQueue *mq = cls;
1189 struct NeighbourMapEntry *n;
1191 if (NULL == (n = lookup_neighbour (receiver)))
1194 return; /* disconnect or other error while transmitting, can happen */
1196 if (n->is_active == mq)
1198 /* this is still "our" neighbour, remove us from its queue
1199 and allow it to send the next message now */
1200 n->is_active = NULL;
1201 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1202 GNUNET_SCHEDULER_cancel (n->task);
1203 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1205 if (bytes_in_send_queue < mq->message_buf_size)
1207 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1208 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1209 bytes_in_send_queue, mq->message_buf_size,
1210 (GNUNET_OK == success) ? "OK" : "FAIL",
1211 size_payload, physical);
1216 GNUNET_break (size_payload == mq->message_buf_size);
1217 bytes_in_send_queue -= mq->message_buf_size;
1218 GNUNET_STATISTICS_set (GST_stats,
1220 ("# bytes in message queue for other peers"),
1221 bytes_in_send_queue, GNUNET_NO);
1222 if (GNUNET_OK == success)
1223 GNUNET_STATISTICS_update (GST_stats,
1225 ("# messages transmitted to other peers"),
1228 GNUNET_STATISTICS_update (GST_stats,
1230 ("# transmission failures for messages to other peers"),
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 "Sending message to `%s' of type %u was a %s\n",
1234 GNUNET_i2s (receiver),
1235 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1236 (success == GNUNET_OK) ? "success" : "FAILURE");
1237 if (NULL != mq->cont)
1238 mq->cont (mq->cont_cls, success, size_payload, physical);
1244 * Check the message list for the given neighbour and if we can
1245 * send a message, do so. This function should only be called
1246 * if the connection is at least generally ready for transmission.
1247 * While we will only send one message at a time, no bandwidth
1248 * quota management is performed here. If a message was given to
1249 * the plugin, the continuation will automatically re-schedule
1250 * the 'master' task once the next message might be transmitted.
1252 * @param n target peer for which to transmit
1255 try_transmission_to_peer (struct NeighbourMapEntry *n)
1257 struct MessageQueue *mq;
1258 struct GNUNET_TIME_Relative timeout;
1260 if (NULL == n->primary_address.address)
1262 /* no address, why are we here? */
1266 if ((0 == n->primary_address.address->address_length) &&
1267 (NULL == n->primary_address.session))
1269 /* no address, why are we here? */
1273 if (NULL != n->is_active)
1275 /* transmission already pending */
1279 /* timeout messages from the queue that are past their due date */
1280 while (NULL != (mq = n->messages_head))
1282 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1283 if (timeout.rel_value_us > 0)
1285 GNUNET_STATISTICS_update (GST_stats,
1287 ("# messages timed out while in transport queue"),
1289 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1291 transmit_send_continuation (mq, &n->id,
1293 mq->message_buf_size, 0); /* timeout */
1296 return; /* no more messages */
1297 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1299 (void) send_with_session (n,
1300 mq->message_buf, mq->message_buf_size,
1301 0 /* priority */, timeout, GNUNET_NO,
1302 &transmit_send_continuation, mq);
1307 * Send keepalive message to the neighbour. Must only be called
1308 * if we are on 'connected' state or while trying to switch addresses.
1309 * Will internally determine if a keepalive is truly needed (so can
1310 * always be called).
1312 * @param n neighbour that went idle and needs a keepalive
1315 send_keepalive (struct NeighbourMapEntry *n)
1317 struct SessionKeepAliveMessage m;
1318 struct GNUNET_TIME_Relative timeout;
1321 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1322 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1323 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1324 return; /* no keepalive needed at this time */
1326 nonce = 0; /* 0 indicates 'not set' */
1328 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1331 "Sending keep alive to peer `%s' with nonce %u\n",
1332 GNUNET_i2s (&n->id), nonce);
1334 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1335 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1336 m.nonce = htonl (nonce);
1338 timeout = send_with_session (n,
1339 (const void *) &m, sizeof (m),
1340 UINT32_MAX /* priority */,
1341 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1343 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1345 n->primary_address.keep_alive_nonce = nonce;
1346 n->expect_latency_response = GNUNET_YES;
1347 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1348 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1354 * Keep the connection to the given neighbour alive longer,
1355 * we received a KEEPALIVE (or equivalent); send a response.
1357 * @param neighbour neighbour to keep alive (by sending keep alive response)
1358 * @param m the keep alive message containing the nonce to respond to
1361 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1362 const struct GNUNET_MessageHeader *m)
1364 struct NeighbourMapEntry *n;
1365 const struct SessionKeepAliveMessage *msg_in;
1366 struct SessionKeepAliveMessage msg;
1368 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1371 msg_in = (struct SessionKeepAliveMessage *) m;
1372 if (NULL == (n = lookup_neighbour (neighbour)))
1374 GNUNET_STATISTICS_update (GST_stats,
1376 ("# KEEPALIVE messages discarded (peer unknown)"),
1380 if (NULL == n->primary_address.session)
1382 GNUNET_STATISTICS_update (GST_stats,
1384 ("# KEEPALIVE messages discarded (no session)"),
1389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1390 "Received keep alive request from peer `%s' with nonce %u\n",
1391 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1393 /* send reply to allow neighbour to measure latency */
1394 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1395 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1396 msg.nonce = msg_in->nonce;
1397 (void) send_with_session(n,
1398 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1399 UINT32_MAX /* priority */,
1400 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1406 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1407 * latency to this peer. Pass the updated information (existing ats
1408 * plus calculated latency) to ATS.
1410 * @param neighbour neighbour to keep alive
1411 * @param m the message containing the keep alive response
1414 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1415 const struct GNUNET_MessageHeader *m)
1417 struct NeighbourMapEntry *n;
1418 const struct SessionKeepAliveMessage *msg;
1419 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1421 struct GNUNET_ATS_Information ats;
1423 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1426 msg = (const struct SessionKeepAliveMessage *) m;
1427 if (NULL == (n = lookup_neighbour (neighbour)))
1429 GNUNET_STATISTICS_update (GST_stats,
1431 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1435 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1436 (GNUNET_YES != n->expect_latency_response) )
1438 GNUNET_STATISTICS_update (GST_stats,
1440 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1444 if (NULL == n->primary_address.address)
1446 GNUNET_STATISTICS_update (GST_stats,
1448 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1452 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1454 GNUNET_STATISTICS_update (GST_stats,
1456 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1463 "Received keep alive response from peer `%s' for session %p\n",
1464 GNUNET_i2s (&n->id), n->primary_address.session);
1468 /* Update session timeout here */
1469 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1472 "Updating session for peer `%s' for session %p\n",
1473 GNUNET_i2s (&n->id), n->primary_address.session);
1474 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1481 n->primary_address.keep_alive_nonce = 0;
1482 n->expect_latency_response = GNUNET_NO;
1483 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1484 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1487 "Latency for peer `%s' is %s\n",
1488 GNUNET_i2s (&n->id),
1489 GNUNET_STRINGS_relative_time_to_string (n->latency,
1491 /* append latency */
1492 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1493 if (n->latency.rel_value_us > UINT32_MAX)
1494 latency = UINT32_MAX;
1496 latency = n->latency.rel_value_us;
1497 ats.value = htonl (latency);
1498 GST_ats_update_metrics (&n->id, n->primary_address.address,
1499 n->primary_address.session, &ats, 1);
1504 * We have received a message from the given sender. How long should
1505 * we delay before receiving more? (Also used to keep the peer marked
1508 * @param sender sender of the message
1509 * @param size size of the message
1510 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1511 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1512 * #GNUNET_SYSERR if the connection is not fully up yet
1513 * @return how long to wait before reading more from this sender
1515 struct GNUNET_TIME_Relative
1516 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1517 *sender, ssize_t size, int *do_forward)
1519 struct NeighbourMapEntry *n;
1520 struct GNUNET_TIME_Relative ret;
1522 if (NULL == neighbours)
1524 *do_forward = GNUNET_NO;
1525 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1527 if (NULL == (n = lookup_neighbour (sender)))
1529 GST_neighbours_try_connect (sender);
1530 if (NULL == (n = lookup_neighbour (sender)))
1532 GNUNET_STATISTICS_update (GST_stats,
1534 ("# messages discarded due to lack of neighbour record"),
1536 *do_forward = GNUNET_NO;
1537 return GNUNET_TIME_UNIT_ZERO;
1540 if (! test_connected (n))
1542 *do_forward = GNUNET_SYSERR;
1543 return GNUNET_TIME_UNIT_ZERO;
1545 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1547 n->quota_violation_count++;
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1550 n->in_tracker.available_bytes_per_s__,
1551 n->quota_violation_count);
1552 /* Discount 32k per violation */
1553 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1557 if (n->quota_violation_count > 0)
1559 /* try to add 32k back */
1560 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1561 n->quota_violation_count--;
1564 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1566 GNUNET_STATISTICS_update (GST_stats,
1568 ("# bandwidth quota violations by other peers"),
1570 *do_forward = GNUNET_NO;
1571 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1573 *do_forward = GNUNET_YES;
1574 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1575 if (ret.rel_value_us > 0)
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1579 (unsigned long long) n->in_tracker.
1580 consumption_since_last_update__,
1581 (unsigned int) n->in_tracker.available_bytes_per_s__,
1582 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1583 GNUNET_STATISTICS_update (GST_stats,
1584 gettext_noop ("# ms throttling suggested"),
1585 (int64_t) ret.rel_value_us / 1000LL,
1593 * Transmit a message to the given target using the active connection.
1595 * @param target destination
1596 * @param msg message to send
1597 * @param msg_size number of bytes in msg
1598 * @param timeout when to fail with timeout
1599 * @param cont function to call when done
1600 * @param cont_cls closure for 'cont'
1603 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1604 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1605 GST_NeighbourSendContinuation cont, void *cont_cls)
1607 struct NeighbourMapEntry *n;
1608 struct MessageQueue *mq;
1610 /* All ove these cases should never happen; they are all API violations.
1611 But we check anyway, just to be sure. */
1612 if (NULL == (n = lookup_neighbour (target)))
1616 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1619 if (GNUNET_YES != test_connected (n))
1623 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1626 bytes_in_send_queue += msg_size;
1627 GNUNET_STATISTICS_set (GST_stats,
1629 ("# bytes in message queue for other peers"),
1630 bytes_in_send_queue, GNUNET_NO);
1631 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1633 mq->cont_cls = cont_cls;
1634 memcpy (&mq[1], msg, msg_size);
1635 mq->message_buf = (const char *) &mq[1];
1636 mq->message_buf_size = msg_size;
1637 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1638 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1639 if ( (NULL != n->is_active) ||
1640 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1642 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1643 GNUNET_SCHEDULER_cancel (n->task);
1644 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1648 send_session_connect_cont (void *cls,
1649 const struct GNUNET_PeerIdentity *target,
1651 size_t size_payload,
1652 size_t size_on_wire)
1654 struct NeighbourMapEntry *n;
1656 n = lookup_neighbour (target);
1659 /* CONNECT continuation was called after neighbor was freed,
1660 * for example due to a time out for the state or the session
1661 * used was already terminated: nothing to do here... */
1665 if ( (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) &&
1666 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1667 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT != n->state))
1669 /* CONNECT continuation was called after neighbor changed state,
1670 * for example due to a time out for the state or the session
1671 * used was already terminated: nothing to do here... */
1674 if (GNUNET_OK == result)
1677 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1678 _("Failed to send CONNECT message to peer `%s' using address `%s' session %p\n"),
1679 GNUNET_i2s (target),
1680 GST_plugins_a2s (n->primary_address.address),
1681 n->primary_address.session);
1684 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1685 /* Remove address and request and additional one */
1686 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1687 n->primary_address.session);
1688 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL );
1689 unset_primary_address (n);
1690 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1691 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1693 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1694 /* Remove address and request and additional one */
1695 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1696 n->primary_address.session);
1697 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL );
1698 unset_primary_address (n);
1699 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1700 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1702 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1703 /* Remove address and request and go back to primary address */
1704 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1705 ("# Failed attempts to switch addresses (failed to send CONNECT CONT)"), 1, GNUNET_NO);
1706 GNUNET_ATS_address_destroyed (GST_ats, n->alternative_address.address,
1707 n->alternative_address.session);
1708 GNUNET_ATS_address_destroyed (GST_ats, n->alternative_address.address,
1710 unset_alternative_address (n);
1711 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1712 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1716 disconnect_neighbour (n);
1722 * Send a SESSION_CONNECT message via the given address.
1724 * @param na address to use
1727 send_session_connect (struct NeighbourAddress *na)
1729 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1730 struct SessionConnectMessage connect_msg;
1731 struct NeighbourMapEntry *n;
1733 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1734 "Sending SESSION_CONNECT message to peer `%s'\n",
1735 GNUNET_i2s (&na->address->peer));
1737 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1742 if (NULL == na->session)
1743 na->session = papi->get_session (papi->cls, na->address);
1744 if (NULL == na->session)
1749 GNUNET_STATISTICS_update (GST_stats,
1751 ("# SESSION_CONNECT messages sent"),
1753 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1754 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1755 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1756 connect_msg.reserved = htonl (0);
1757 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1759 papi->send (papi->cls,
1761 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1763 SETUP_CONNECTION_TIMEOUT,
1764 send_session_connect_cont, NULL))
1766 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1767 _("Failed to transmit CONNECT message via plugin to %s\n"),
1768 GST_plugins_a2s (na->address));
1770 n = lookup_neighbour (&na->address->peer);
1778 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1779 /* Remove address and request and additional one */
1780 unset_primary_address (n);
1781 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1782 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1783 /* Hard failure to send the CONNECT message with this address:
1784 Destroy address and session */
1786 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1787 /* Remove address and request and additional one */
1788 unset_primary_address (n);
1789 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1790 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1792 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1793 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1794 ("# Failed attempts to switch addresses (failed to send CONNECT)"), 1, GNUNET_NO);
1795 /* Remove address and request and additional one */
1796 unset_alternative_address (n);
1797 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1798 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1802 disconnect_neighbour (n);
1805 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1806 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1808 GST_neighbours_notify_data_sent (&na->address->peer,
1811 sizeof (struct SessionConnectMessage));
1816 send_session_connect_ack_cont (void *cls,
1817 const struct GNUNET_PeerIdentity *target,
1819 size_t size_payload,
1820 size_t size_on_wire)
1822 struct NeighbourMapEntry *n;
1824 n = lookup_neighbour (target);
1827 /* CONNECT_ACK continuation was called after neighbor was freed,
1828 * for example due to a time out for the state or the session
1829 * used was already terminated: nothing to do here... */
1833 if (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state)
1835 /* CONNECT_ACK continuation was called after neighbor changed state,
1836 * for example due to a time out for the state or the session
1837 * used was already terminated: nothing to do here... */
1840 if (GNUNET_OK == result)
1843 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1844 _("Failed to send CONNECT_ACK message to peer `%s' using address `%s' session %p\n"),
1845 GNUNET_i2s (target),
1846 GST_plugins_a2s (n->primary_address.address),
1847 n->primary_address.session);
1849 /* Failed to send CONNECT_ACK message with this address */
1850 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1851 n->primary_address.session);
1852 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1855 /* Remove address and request and additional one */
1856 unset_primary_address (n);
1857 n->ack_state = ACK_SEND_CONNECT_ACK;
1858 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1859 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1865 * Send a CONNECT_ACK message via the given address.
1867 * @param address address to use
1868 * @param session session to use
1869 * @param timestamp timestamp to use for the ACK message
1870 * @return GNUNET_SYSERR if sending immediately failed, GNUNET_OK otherwise
1873 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1874 struct Session *session,
1875 struct GNUNET_TIME_Absolute timestamp)
1877 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1878 struct SessionConnectMessage connect_msg;
1879 struct NeighbourMapEntry *n;
1881 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1882 "Sending CONNECT_ACK to peer `%s'\n",
1883 GNUNET_i2s (&address->peer));
1885 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1890 if (NULL == session)
1891 session = papi->get_session (papi->cls, address);
1892 if (NULL == session)
1897 GNUNET_STATISTICS_update (GST_stats,
1899 ("# CONNECT_ACK messages sent"),
1901 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1902 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1903 connect_msg.reserved = htonl (0);
1904 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1906 if (GNUNET_SYSERR == papi->send (papi->cls,
1908 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1910 GNUNET_TIME_UNIT_FOREVER_REL,
1911 send_session_connect_ack_cont, NULL))
1913 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1914 _("Failed to transmit CONNECT_ACK message via plugin to %s\n"),
1915 GST_plugins_a2s (address));
1917 n = lookup_neighbour (&address->peer);
1923 /* Hard failure to send the CONNECT_ACK message with this address:
1924 Destroy session (and address) */
1925 if (GNUNET_YES == GNUNET_HELLO_address_check_option(address,
1926 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1928 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1929 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1932 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1934 /* Remove address and request and additional one */
1935 unset_primary_address (n);
1936 n->ack_state = ACK_SEND_CONNECT_ACK;
1937 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
1938 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1944 struct QuotaNotificationRequest
1946 struct GNUNET_PeerIdentity peer;
1947 struct Session *session;
1951 struct QNR_LookContext
1953 struct GNUNET_PeerIdentity peer;
1954 struct Session *session;
1957 struct QuotaNotificationRequest *res;
1961 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1963 struct QNR_LookContext *qnr_ctx = cls;
1964 struct QuotaNotificationRequest *qnr = value;
1966 if ((qnr->session == qnr_ctx->session) &&
1967 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1968 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1970 qnr_ctx->res = value;
1977 GST_neighbours_register_quota_notification(void *cls,
1978 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1979 struct Session *session)
1981 struct QuotaNotificationRequest *qnr;
1982 struct QNR_LookContext qnr_ctx;
1984 if (NULL == registered_quota_notifications)
1986 return; /* init or shutdown */
1989 qnr_ctx.peer = (*peer);
1990 qnr_ctx.plugin = plugin;
1991 qnr_ctx.session = session;
1994 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1995 peer, &find_notification_request, &qnr_ctx);
1996 if (NULL != qnr_ctx.res)
2002 qnr = GNUNET_new (struct QuotaNotificationRequest);
2003 qnr->peer = (*peer);
2004 qnr->plugin = GNUNET_strdup (plugin);
2005 qnr->session = session;
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 "Adding notification for peer `%s' plugin `%s' session %p \n",
2009 GNUNET_i2s (peer), plugin, session);
2011 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
2012 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2017 GST_neighbours_unregister_quota_notification(void *cls,
2018 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
2020 struct QNR_LookContext qnr_ctx;
2022 if (NULL == registered_quota_notifications)
2024 return; /* init or shutdown */
2027 qnr_ctx.peer = (*peer);
2028 qnr_ctx.plugin = plugin;
2029 qnr_ctx.session = session;
2032 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
2033 &find_notification_request, &qnr_ctx);
2034 if (NULL == qnr_ctx.res)
2040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2041 "Removing notification for peer `%s' plugin `%s' session %p \n",
2042 GNUNET_i2s (peer), plugin, session);
2044 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
2046 GNUNET_free (qnr_ctx.res->plugin);
2047 GNUNET_free (qnr_ctx.res);
2051 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2053 /* struct NeighbourMapEntry *n = cls; */
2054 struct QuotaNotificationRequest *qnr = value;
2055 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2056 struct GNUNET_TIME_Relative delay;
2059 papi = GST_plugins_find(qnr->plugin);
2066 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
2067 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2068 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
2069 delay.rel_value_us / 1000);
2071 if (NULL != papi->update_inbound_delay)
2072 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
2078 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
2081 /* struct NeighbourMapEntry *n = cls; */
2082 struct QuotaNotificationRequest *qnr = value;
2084 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
2086 GNUNET_free(qnr->plugin);
2093 inbound_bw_tracker_update(void *cls)
2095 struct NeighbourMapEntry *n = cls;
2097 /* Quota was updated, tell plugins to update the time to receive next */
2098 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2099 &n->id, ¬ification_cb, n);
2104 * Create a fresh entry in the neighbour map for the given peer
2106 * @param peer peer to create an entry for
2107 * @return new neighbour map entry
2109 static struct NeighbourMapEntry *
2110 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2112 struct NeighbourMapEntry *n;
2114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2115 "Creating new neighbour entry for `%s'\n",
2117 n = GNUNET_new (struct NeighbourMapEntry);
2119 n->ack_state = ACK_UNDEFINED;
2120 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2121 n->last_util_transmission = GNUNET_TIME_absolute_get();
2122 n->util_payload_bytes_recv = 0;
2123 n->util_payload_bytes_sent = 0;
2124 n->util_total_bytes_recv = 0;
2125 n->util_total_bytes_sent = 0;
2126 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
2127 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2128 MAX_BANDWIDTH_CARRY_S);
2129 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2130 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2131 GNUNET_assert (GNUNET_OK ==
2132 GNUNET_CONTAINER_multipeermap_put (neighbours,
2134 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2138 /* We received a address suggestion after requesting an address in
2139 * try_connect or after receiving a connect, switch to address
2142 address_suggest_cont (void *cls,
2143 const struct GNUNET_PeerIdentity *peer,
2144 const struct GNUNET_HELLO_Address *address, struct Session *session,
2145 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2146 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2147 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2149 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2150 bandwidth_in, bandwidth_out);
2154 struct BlacklistCheckSwitchContext
2156 struct BlacklistCheckSwitchContext *prev;
2157 struct BlacklistCheckSwitchContext *next;
2160 struct GST_BlacklistCheck *blc;
2162 struct GNUNET_HELLO_Address *address;
2163 struct Session *session;
2164 struct GNUNET_ATS_Information *ats;
2167 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2168 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2172 * Black list check result for try_connect call
2173 * If connection to the peer is allowed request adddress and
2175 * @param cls blc_ctx bl context
2176 * @param peer the peer
2177 * @param result the result
2180 try_connect_bl_check_cont (void *cls,
2181 const struct GNUNET_PeerIdentity *peer, int result)
2183 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2184 struct NeighbourMapEntry *n;
2186 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2187 GNUNET_free (blc_ctx);
2189 if (GNUNET_OK != result)
2191 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2192 _("Blacklisting disapproved to connect to peer `%s'\n"),
2197 /* Setup a new neighbour */
2198 n = setup_neighbour (peer);
2200 /* Request address suggestions for this peer */
2201 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2202 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2203 GNUNET_ATS_reset_backoff (GST_ats, peer);
2204 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2205 &address_suggest_cont, n);
2211 * Try to create a connection to the given target (eventually).
2213 * @param target peer to try to connect to
2216 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2218 struct NeighbourMapEntry *n;
2219 struct GST_BlacklistCheck *blc;
2220 struct BlacklistCheckSwitchContext *blc_ctx;
2222 if (NULL == neighbours)
2224 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2225 "Asked to connect to peer `%s' during shutdown\n",
2226 GNUNET_i2s (target));
2227 return; /* during shutdown, do nothing */
2229 n = lookup_neighbour (target);
2230 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2231 "Asked to connect to peer `%s' (state: %s)\n",
2232 GNUNET_i2s (target),
2233 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2238 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2239 /* this should not be possible */
2241 free_neighbour (n, GNUNET_NO);
2243 case GNUNET_TRANSPORT_PS_INIT_ATS:
2244 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2245 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2246 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2247 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2248 "Ignoring request to try to connect to `%s', already trying!\n",
2249 GNUNET_i2s (target));
2250 return; /* already trying */
2251 case GNUNET_TRANSPORT_PS_CONNECTED:
2252 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2253 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2254 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2255 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2256 "Ignoring request to try to connect, already connected to `%s'!\n",
2257 GNUNET_i2s (target));
2258 return; /* already connected */
2259 case GNUNET_TRANSPORT_PS_DISCONNECT:
2260 /* get rid of remains, ready to re-try immediately */
2261 free_neighbour (n, GNUNET_NO);
2263 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2264 /* should not be possible */
2268 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2269 "Unhandled state `%s'\n",
2270 GNUNET_TRANSPORT_ps2s (n->state));
2272 free_neighbour (n, GNUNET_NO);
2277 /* Do blacklist check if connecting to this peer is allowed */
2278 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2279 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2281 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2282 &try_connect_bl_check_cont, blc_ctx)))
2290 * We received a 'SESSION_CONNECT' message from the other peer.
2291 * Consider switching to it.
2293 * @param message possibly a 'struct SessionConnectMessage' (check format)
2294 * @param peer identity of the peer to switch the address for
2295 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2298 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2299 const struct GNUNET_PeerIdentity *peer)
2301 const struct SessionConnectMessage *scm;
2302 struct NeighbourMapEntry *n;
2303 struct GNUNET_TIME_Absolute ts;
2305 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2307 GNUNET_break_op (0);
2308 return GNUNET_SYSERR;
2310 GNUNET_STATISTICS_update (GST_stats,
2312 ("# CONNECT messages received"),
2314 if (NULL == neighbours)
2316 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2317 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2319 return GNUNET_OK; /* we're shutting down */
2321 scm = (const struct SessionConnectMessage *) message;
2322 GNUNET_break_op (0 == ntohl (scm->reserved));
2323 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2324 n = lookup_neighbour (peer);
2327 /* This is a new neighbour and set to not connected */
2328 n = setup_neighbour (peer);
2331 /* Remember this CONNECT message in neighbour */
2332 n->ack_state = ACK_SEND_CONNECT_ACK;
2333 n->connect_ack_timestamp = ts;
2335 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2336 "Received CONNECT for peer `%s' in state %s/%s\n",
2338 GNUNET_TRANSPORT_ps2s (n->state),
2339 print_ack_state (n->ack_state));
2343 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2344 /* Request an address from ATS to send CONNECT_ACK to this peer */
2345 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2346 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2347 if (NULL == n->suggest_handle)
2348 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2350 case GNUNET_TRANSPORT_PS_INIT_ATS:
2351 /* CONNECT message takes priority over us asking ATS for address:
2352 * Wait for ATS to suggest an address and send CONNECT_ACK */
2353 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2354 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2356 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2357 /* We already wait for an address to send an CONNECT_ACK */
2359 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2360 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2361 /* Send ACK immediately */
2362 n->ack_state = ACK_SEND_SESSION_ACK;
2363 send_connect_ack_message (n->primary_address.address,
2364 n->primary_address.session, ts);
2366 case GNUNET_TRANSPORT_PS_CONNECTED:
2367 /* we are already connected and can thus send the ACK immediately */
2368 GNUNET_assert (NULL != n->primary_address.address);
2369 GNUNET_assert (NULL != n->primary_address.session);
2370 n->ack_state = ACK_SEND_SESSION_ACK;
2371 send_connect_ack_message (n->primary_address.address,
2372 n->primary_address.session, ts);
2374 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2375 /* We wait for ATS address suggestion */
2377 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2378 /* We received a CONNECT message while waiting for a CONNECT_ACK in fast
2379 * reconnect. Send CONNECT_ACK immediately */
2380 n->ack_state = ACK_SEND_SESSION_ACK;
2381 send_connect_ack_message (n->primary_address.address,
2382 n->primary_address.session, n->connect_ack_timestamp);
2384 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2385 /* We are already connected and can thus send the ACK immediately;
2386 still, it can never hurt to have an alternative address, so also
2387 tell ATS about it */
2388 GNUNET_assert (NULL != n->primary_address.address);
2389 GNUNET_assert (NULL != n->primary_address.session);
2390 n->ack_state = ACK_SEND_SESSION_ACK;
2391 send_connect_ack_message (n->primary_address.address,
2392 n->primary_address.session, ts);
2394 case GNUNET_TRANSPORT_PS_DISCONNECT:
2395 /* Get rid of remains without terminating sessions, ready to re-try */
2396 free_neighbour (n, GNUNET_YES);
2397 n = setup_neighbour (peer);
2398 /* Remember the CONNECT time stamp for ACK message */
2399 n->ack_state = ACK_SEND_CONNECT_ACK;
2400 n->connect_ack_timestamp = ts;
2401 /* Request an address for the peer */
2402 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2403 GNUNET_ATS_reset_backoff (GST_ats, peer);
2404 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2406 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2407 /* should not be possible */
2411 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2412 "Unhandled state `%s'\n",
2413 GNUNET_TRANSPORT_ps2s (n->state));
2415 return GNUNET_SYSERR;
2421 switch_address_bl_check_cont (void *cls,
2422 const struct GNUNET_PeerIdentity *peer, int result)
2424 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2425 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2426 struct NeighbourMapEntry *n;
2428 papi = GST_plugins_find (blc_ctx->address->transport_name);
2430 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2436 "Peer %s is unknown, suggestion ignored\n",
2439 if (result == GNUNET_NO)
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2442 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2443 GST_plugins_a2s (blc_ctx->address),
2445 GNUNET_i2s (&blc_ctx->address->peer));
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2450 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2451 blc_ctx->address->transport_name,
2452 GST_plugins_a2s (blc_ctx->address),
2454 GNUNET_i2s (&blc_ctx->address->peer));
2457 /* This address is blacklisted, delete address and session (if existing) in ATS */
2458 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2460 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2461 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2463 /* This is an inbound address, destroy full address */
2464 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2467 /* Remove blacklist check and clean up */
2468 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2469 GNUNET_HELLO_address_free (blc_ctx->address);
2470 GNUNET_free_non_null (blc_ctx->ats);
2471 GNUNET_free (blc_ctx);
2475 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2476 "Blacklist accepted address `%s' session %p for peer `%s'\n",
2477 GST_plugins_a2s (blc_ctx->address),
2479 GNUNET_i2s (&blc_ctx->address->peer));
2481 if (NULL == blc_ctx->session)
2483 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2485 "Obtained new session for peer `%s' and address '%s': %p\n",
2486 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2488 if (NULL == blc_ctx->session)
2490 /* No session could be obtained, remove blacklist check and clean up */
2491 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2492 "Failed to obtain new session for peer `%s' and address '%s'\n",
2493 GNUNET_i2s (&blc_ctx->address->peer),
2494 GST_plugins_a2s (blc_ctx->address));
2495 /* Delete address in ATS */
2496 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2498 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2499 GNUNET_HELLO_address_free (blc_ctx->address);
2500 GNUNET_free_non_null (blc_ctx->ats);
2501 GNUNET_free (blc_ctx);
2507 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2509 free_neighbour (n, GNUNET_NO);
2511 case GNUNET_TRANSPORT_PS_INIT_ATS:
2512 /* We requested an address and ATS suggests one:
2513 * set primary address and send CONNECT message*/
2514 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2515 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2516 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) )
2518 /* Send pending CONNECT_ACK message */
2519 n->ack_state = ACK_SEND_SESSION_ACK;
2520 send_connect_ack_message (n->primary_address.address,
2521 n->primary_address.session, n->connect_ack_timestamp);
2523 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2524 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2525 send_session_connect (&n->primary_address);
2527 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2528 /* ATS suggested a new address while waiting for an CONNECT_ACK:
2529 * Switch and send new CONNECT */
2530 /* ATS suggests a different address, switch again */
2531 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2532 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2533 if (ACK_SEND_CONNECT_ACK == n->ack_state)
2535 /* Send pending CONNECT_ACK message */
2536 n->ack_state = ACK_SEND_SESSION_ACK;
2537 send_connect_ack_message (n->primary_address.address,
2538 n->primary_address.session, n->connect_ack_timestamp);
2540 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2541 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2542 send_session_connect (&n->primary_address);
2544 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2545 /* We requested an address and ATS suggests one:
2546 * set primary address and send CONNECT_ACK message*/
2547 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2548 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2549 /* Send an ACK message as a response to the CONNECT msg */
2550 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2551 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2552 send_connect_ack_message (n->primary_address.address,
2553 n->primary_address.session,
2554 n->connect_ack_timestamp);
2555 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) ||
2556 (ACK_UNDEFINED == n->ack_state) )
2557 n->ack_state = ACK_SEND_SESSION_ACK;
2559 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2560 /* ATS asks us to switch while we were trying to connect; switch to new
2561 address and check blacklist again */
2562 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) )
2564 n->ack_state = ACK_SEND_SESSION_ACK;
2565 send_connect_ack_message (n->primary_address.address,
2566 n->primary_address.session, n->connect_ack_timestamp);
2568 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2569 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2570 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2571 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2573 case GNUNET_TRANSPORT_PS_CONNECTED:
2574 GNUNET_assert (NULL != n->primary_address.address);
2575 GNUNET_assert (NULL != n->primary_address.session);
2576 if (n->primary_address.session == blc_ctx->session)
2578 /* not an address change, just a quota change */
2579 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2580 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2583 /* ATS asks us to switch a life connection; see if we can get
2584 a CONNECT_ACK on it before we actually do this! */
2585 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2586 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2587 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT,
2588 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2589 GNUNET_STATISTICS_update (GST_stats, gettext_noop
2590 ("# Attempts to switch addresses"), 1, GNUNET_NO);
2591 send_session_connect (&n->alternative_address);
2593 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2594 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2595 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2596 if ( (ACK_SEND_CONNECT_ACK == n->ack_state) )
2598 /* Send pending CONNECT_ACK message */
2599 n->ack_state = ACK_SEND_SESSION_ACK;
2600 send_connect_ack_message (n->primary_address.address,
2601 n->primary_address.session, n->connect_ack_timestamp);
2603 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2604 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2605 send_session_connect (&n->primary_address);
2607 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2608 /* ATS asks us to switch while we were trying to reconnect; switch to new
2609 address and send CONNECT again */
2610 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2611 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2612 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2613 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2614 send_session_connect (&n->primary_address);
2616 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2617 if ( (0 == GNUNET_HELLO_address_cmp(n->primary_address.address,
2618 blc_ctx->address) && n->primary_address.session == blc_ctx->session) )
2620 /* ATS switches back to still-active session */
2621 free_address (&n->alternative_address);
2622 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2625 /* ATS asks us to switch a life connection, send */
2626 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2627 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2628 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT,
2629 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2630 send_session_connect (&n->alternative_address);
2632 case GNUNET_TRANSPORT_PS_DISCONNECT:
2633 /* not going to switch addresses while disconnecting */
2635 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2639 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2640 "Unhandled state `%s'\n",
2641 GNUNET_TRANSPORT_ps2s (n->state));
2646 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2647 GNUNET_HELLO_address_free(blc_ctx->address);
2648 GNUNET_free_non_null (blc_ctx->ats);
2649 GNUNET_free (blc_ctx);
2655 * For the given peer, switch to this address.
2657 * Before accepting this addresses and actively using it, a blacklist check
2658 * is performed. If this blacklist check fails the address will be destroyed.
2660 * @param peer identity of the peer to switch the address for
2661 * @param address address of the other peer,
2662 * @param session session to use or NULL if transport should initiate a session
2663 * @param ats performance data
2664 * @param ats_count number of entries in ats
2665 * @param bandwidth_in inbound quota to be used when connection is up,
2666 * 0 to disconnect from peer
2667 * @param bandwidth_out outbound quota to be used when connection is up,
2668 * 0 to disconnect from peer
2671 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2672 const struct GNUNET_HELLO_Address *address,
2673 struct Session *session,
2674 const struct GNUNET_ATS_Information *ats,
2676 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2677 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2679 struct NeighbourMapEntry *n;
2680 struct GST_BlacklistCheck *blc;
2681 struct BlacklistCheckSwitchContext *blc_ctx;
2684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2685 "ATS has decided on an address for peer %s\n",
2687 GNUNET_assert (NULL != address->transport_name);
2688 if (NULL == (n = lookup_neighbour (peer)))
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2691 "Peer %s is unknown, suggestion ignored\n",
2696 /* Check if plugin is available */
2697 if (NULL == (GST_plugins_find (address->transport_name)))
2699 /* we don't have the plugin for this address */
2700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2701 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2702 address->transport_name,
2704 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2707 if ((NULL == session) &&
2708 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2710 /* This is a inbound address and we do not have a session to use! */
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2712 "Inbound address without session `%s'! Destroying address...\n",
2713 GST_plugins_a2s (address));
2714 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2718 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2719 "ATS suggests %s address '%s' session %p for "
2720 "peer `%s' in state %s/%s (quota in/out %u %u )\n",
2721 GNUNET_HELLO_address_check_option (address,
2722 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2723 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2724 GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state),
2725 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2727 /* Perform blacklist check */
2728 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2729 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2730 blc_ctx->session = session;
2731 blc_ctx->bandwidth_in = bandwidth_in;
2732 blc_ctx->bandwidth_out = bandwidth_out;
2733 blc_ctx->ats_count = ats_count;
2734 blc_ctx->ats = NULL;
2737 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2738 for (c = 0; c < ats_count; c++)
2740 blc_ctx->ats[c].type = ats[c].type;
2741 blc_ctx->ats[c].value = ats[c].value;
2745 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2746 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2747 &switch_address_bl_check_cont, blc_ctx)))
2755 send_utilization_data (void *cls,
2756 const struct GNUNET_PeerIdentity *key,
2759 struct NeighbourMapEntry *n = value;
2760 struct GNUNET_ATS_Information atsi[4];
2762 uint32_t bps_pl_out;
2765 struct GNUNET_TIME_Relative delta;
2767 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2768 GNUNET_TIME_absolute_get ());
2772 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2773 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2775 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2776 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2778 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2783 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2784 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2786 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2787 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2791 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2795 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2796 atsi[0].value = htonl (bps_out);
2797 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2798 atsi[1].value = htonl (bps_in);
2800 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2801 atsi[2].value = htonl (bps_pl_out);
2802 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2803 atsi[3].value = htonl (bps_pl_in);
2805 GST_ats_update_metrics (key, n->primary_address.address,
2806 n->primary_address.session, atsi, 4);
2807 n->util_payload_bytes_recv = 0;
2808 n->util_payload_bytes_sent = 0;
2809 n->util_total_bytes_recv = 0;
2810 n->util_total_bytes_sent = 0;
2811 n->last_util_transmission = GNUNET_TIME_absolute_get();
2817 * Task transmitting utilization in a regular interval
2819 * @param cls the 'struct NeighbourMapEntry' for which we are running
2820 * @param tc scheduler context (unused)
2823 utilization_transmission (void *cls,
2824 const struct GNUNET_SCHEDULER_TaskContext *tc)
2826 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2828 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2829 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2831 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2832 utilization_transmission, NULL);
2838 GST_neighbours_notify_data_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;
2845 n = lookup_neighbour (peer);
2848 n->util_total_bytes_recv += ntohs(message->size);
2853 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2854 const struct GNUNET_HELLO_Address *address,
2855 struct Session *session,
2856 const struct GNUNET_MessageHeader *message)
2858 struct NeighbourMapEntry *n;
2859 n = lookup_neighbour (peer);
2862 n->util_payload_bytes_recv += ntohs(message->size);
2867 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2868 const struct GNUNET_HELLO_Address *address,
2869 struct Session *session,
2872 struct NeighbourMapEntry *n;
2873 n = lookup_neighbour (peer);
2876 if (n->primary_address.session != session)
2878 n->util_total_bytes_sent += size;
2883 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2886 struct NeighbourMapEntry *n;
2887 n = lookup_neighbour (peer);
2890 n->util_payload_bytes_sent += size;
2895 * Master task run for every neighbour. Performs all of the time-related
2896 * activities (keep alive, send next message, disconnect if idle, finish
2897 * clean up after disconnect).
2899 * @param cls the 'struct NeighbourMapEntry' for which we are running
2900 * @param tc scheduler context (unused)
2903 master_task (void *cls,
2904 const struct GNUNET_SCHEDULER_TaskContext *tc)
2906 struct NeighbourMapEntry *n = cls;
2907 struct GNUNET_TIME_Relative delay;
2909 n->task = GNUNET_SCHEDULER_NO_TASK;
2910 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2912 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2913 GNUNET_i2s (&n->id),
2914 GNUNET_TRANSPORT_ps2s(n->state),
2915 GNUNET_STRINGS_relative_time_to_string (delay,
2919 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2920 /* invalid state for master task, clean up */
2922 free_neighbour (n, GNUNET_NO);
2924 case GNUNET_TRANSPORT_PS_INIT_ATS:
2925 if (0 == delay.rel_value_us)
2927 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2928 "Connection to `%s' timed out waiting for ATS to provide address\n",
2929 GNUNET_i2s (&n->id));
2930 free_neighbour (n, GNUNET_NO);
2934 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2935 if (0 == delay.rel_value_us)
2937 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2938 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2939 GNUNET_i2s (&n->id));
2940 /* We could not send to this address, delete address and session */
2941 if (NULL != n->primary_address.session)
2942 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
2943 n->primary_address.session);
2944 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
2946 /* Remove address and request and additional one */
2947 unset_primary_address (n);
2948 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2949 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2953 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2954 if (0 == delay.rel_value_us)
2956 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2957 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2958 GNUNET_i2s (&n->id));
2959 free_neighbour (n, GNUNET_NO);
2963 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2964 if (0 == delay.rel_value_us)
2966 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2967 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2968 GNUNET_i2s (&n->id));
2969 disconnect_neighbour (n);
2973 case GNUNET_TRANSPORT_PS_CONNECTED:
2974 if (0 == delay.rel_value_us)
2976 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2977 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2978 GNUNET_i2s (&n->id));
2979 disconnect_neighbour (n);
2982 try_transmission_to_peer (n);
2985 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2986 if (0 == delay.rel_value_us)
2988 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2989 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2990 GNUNET_i2s (&n->id));
2991 disconnect_neighbour (n);
2995 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2996 if (0 == delay.rel_value_us)
2998 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2999 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3000 GNUNET_i2s (&n->id));
3001 disconnect_neighbour (n);
3005 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3006 if (0 == delay.rel_value_us)
3008 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3009 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3010 GNUNET_i2s (&n->id));
3011 GNUNET_STATISTICS_update (GST_stats, gettext_noop
3012 ("# Failed attempts to switch addresses (no response)"), 1, GNUNET_NO);
3013 disconnect_neighbour (n);
3016 try_transmission_to_peer (n);
3019 case GNUNET_TRANSPORT_PS_DISCONNECT:
3020 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3021 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3022 GNUNET_i2s (&n->id));
3023 free_neighbour (n, GNUNET_NO);
3025 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3026 /* how did we get here!? */
3030 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3031 "Unhandled state `%s'\n",
3032 GNUNET_TRANSPORT_ps2s (n->state));
3036 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3037 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3039 /* if we are *now* in one of the two states, we're sending
3040 keep alive messages, so we need to consider the keepalive
3041 delay, not just the connection timeout */
3042 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3045 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3046 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3053 * Send a SESSION_ACK message to the neighbour to confirm that we
3054 * got his CONNECT_ACK.
3056 * @param n neighbour to send the SESSION_ACK to
3059 send_session_ack_message (struct NeighbourMapEntry *n)
3061 struct GNUNET_MessageHeader msg;
3063 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sending SESSION_ACK message to peer `%s'\n",
3064 GNUNET_i2s (&n->id));
3066 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3067 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3068 (void) send_with_session(n,
3069 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3070 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3076 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3077 * Consider switching to it.
3079 * @param message possibly a 'struct SessionConnectMessage' (check format)
3080 * @param peer identity of the peer to switch the address for
3081 * @param address address of the other peer, NULL if other peer
3083 * @param session session to use (or NULL)
3084 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3087 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3088 const struct GNUNET_PeerIdentity *peer,
3089 const struct GNUNET_HELLO_Address *address,
3090 struct Session *session)
3092 const struct SessionConnectMessage *scm;
3093 struct GNUNET_TIME_Absolute ts;
3094 struct NeighbourMapEntry *n;
3096 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3097 "Received CONNECT_ACK message from peer `%s'\n",
3100 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3102 GNUNET_break_op (0);
3103 return GNUNET_SYSERR;
3105 GNUNET_STATISTICS_update (GST_stats,
3107 ("# CONNECT_ACK messages received"),
3109 scm = (const struct SessionConnectMessage *) message;
3110 GNUNET_break_op (ntohl (scm->reserved) == 0);
3111 if (NULL == (n = lookup_neighbour (peer)))
3113 GNUNET_STATISTICS_update (GST_stats,
3115 ("# unexpected CONNECT_ACK messages (no peer)"),
3117 return GNUNET_SYSERR;
3119 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3122 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3124 free_neighbour (n, GNUNET_NO);
3125 return GNUNET_SYSERR;
3126 case GNUNET_TRANSPORT_PS_INIT_ATS:
3127 GNUNET_STATISTICS_update (GST_stats,
3129 ("# unexpected CONNECT_ACK messages (not ready)"),
3132 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3133 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3135 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3136 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3139 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3140 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3141 GNUNET_STATISTICS_set (GST_stats,
3142 gettext_noop ("# peers connected"),
3143 ++neighbours_connected,
3145 connect_notify_cb (callback_cls, &n->id,
3146 n->primary_address.bandwidth_in,
3147 n->primary_address.bandwidth_out);
3148 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3149 GST_ats_add_address (n->primary_address.address,
3150 n->primary_address.session,
3152 set_primary_address (n,
3153 n->primary_address.address,
3154 n->primary_address.session,
3155 n->primary_address.bandwidth_in,
3156 n->primary_address.bandwidth_out,
3158 send_session_ack_message (n);
3160 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3161 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3162 GNUNET_STATISTICS_update (GST_stats,
3164 ("# unexpected CONNECT_ACK messages (not ready)"),
3167 case GNUNET_TRANSPORT_PS_CONNECTED:
3168 /* duplicate CONNECT_ACK, let's answer by duplicate SESSION_ACK just in case */
3169 send_session_ack_message (n);
3171 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3172 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3173 to give us a new address... */
3174 GNUNET_STATISTICS_update (GST_stats,
3176 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3179 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3180 /* Reconnecting with new address address worked; go back to connected! */
3181 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3182 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3183 send_session_ack_message (n);
3185 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3186 /* new address worked; adopt it and go back to connected! */
3187 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3188 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3189 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3191 /* Notify about session... perhaps we obtained it */
3192 GST_ats_add_address (n->alternative_address.address,
3193 n->alternative_address.session, NULL, 0);
3194 /* Set primary addresses */
3195 set_primary_address (n, n->alternative_address.address,
3196 n->alternative_address.session, n->alternative_address.bandwidth_in,
3197 n->alternative_address.bandwidth_out, GNUNET_YES);
3199 GNUNET_STATISTICS_update (GST_stats, gettext_noop
3200 ("# Successful attempts to switch addresses"), 1, GNUNET_NO);
3202 free_address (&n->alternative_address);
3203 send_session_ack_message (n);
3205 case GNUNET_TRANSPORT_PS_DISCONNECT:
3206 GNUNET_STATISTICS_update (GST_stats,
3208 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3210 return GNUNET_SYSERR;
3211 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3215 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3216 "Unhandled state `%s'\n",
3217 GNUNET_TRANSPORT_ps2s (n->state));
3219 return GNUNET_SYSERR;
3226 * A session was terminated. Take note; if needed, try to get
3227 * an alternative address from ATS.
3229 * @param peer identity of the peer where the session died
3230 * @param session session that is gone
3231 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3232 * this session was not in use
3235 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3236 struct Session *session)
3238 struct NeighbourMapEntry *n;
3239 struct BlackListCheckContext *bcc;
3240 struct BlackListCheckContext *bcc_next;
3242 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3244 while (NULL != (bcc = bcc_next))
3246 bcc_next = bcc->next;
3247 if (bcc->na.session == session)
3249 if (NULL != bcc->bc)
3250 GST_blacklist_test_cancel (bcc->bc);
3251 GNUNET_HELLO_address_free (bcc->na.address);
3252 GNUNET_CONTAINER_DLL_remove (bc_head,
3258 if (NULL == (n = lookup_neighbour (peer)))
3259 return GNUNET_NO; /* can't affect us */
3260 if (session != n->primary_address.session)
3262 if (session == n->alternative_address.session)
3264 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3265 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3267 free_address (&n->alternative_address);
3269 return GNUNET_NO; /* doesn't affect us further */
3272 n->expect_latency_response = GNUNET_NO;
3273 /* The session for neighbour's primary address died */
3276 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3278 free_neighbour (n, GNUNET_NO);
3280 case GNUNET_TRANSPORT_PS_INIT_ATS:
3282 free_neighbour (n, GNUNET_NO);
3284 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3285 /* The session used to send the CONNECT terminated:
3286 * this implies a connect error*/
3287 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3288 "Failed to send CONNECT in %s with `%s' %p: session terminated\n",
3290 GST_plugins_a2s (n->primary_address.address),
3291 n->primary_address.session,
3294 /* Destroy the address since it cannot be used */
3295 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3296 unset_primary_address (n);
3297 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3298 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3300 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3301 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3302 /* error on inbound session; free neighbour entirely */
3303 free_address (&n->primary_address);
3304 free_neighbour (n, GNUNET_NO);
3306 case GNUNET_TRANSPORT_PS_CONNECTED:
3307 /* Our primary connection died, try a fast reconnect */
3308 unset_primary_address (n);
3309 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3310 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3312 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3313 /* we don't have an address, how can it go down? */
3316 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3317 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3318 "Failed to send CONNECT in %s with `%s' %p: session terminated\n",
3320 GST_plugins_a2s (n->primary_address.address),
3321 n->primary_address.session,
3324 /* Destroy the address since it cannot be used */
3325 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3326 unset_primary_address (n);
3327 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3328 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3330 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3331 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3332 secondary as primary */
3334 /* Destroy the inbound address since it cannot be used */
3336 == GNUNET_HELLO_address_check_option (n->primary_address.address,
3337 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
3338 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3339 free_address (&n->primary_address);
3340 n->primary_address = n->alternative_address;
3341 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3342 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3343 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3345 case GNUNET_TRANSPORT_PS_DISCONNECT:
3346 free_address (&n->primary_address);
3348 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3349 /* neighbour was freed and plugins told to terminate session */
3353 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3354 "Unhandled state `%s'\n",
3355 GNUNET_TRANSPORT_ps2s (n->state));
3359 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3360 GNUNET_SCHEDULER_cancel (n->task);
3361 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3367 * We received a 'SESSION_ACK' message from the other peer.
3368 * If we sent a 'CONNECT_ACK' last, this means we are now
3369 * connected. Otherwise, do nothing.
3371 * @param message possibly a 'struct SessionConnectMessage' (check format)
3372 * @param peer identity of the peer to switch the address for
3373 * @param address address of the other peer, NULL if other peer
3375 * @param session session to use (or NULL)
3376 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3379 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3380 const struct GNUNET_PeerIdentity *peer,
3381 const struct GNUNET_HELLO_Address *address,
3382 struct Session *session)
3384 struct NeighbourMapEntry *n;
3386 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3387 "Received SESSION_ACK message from peer `%s'\n",
3389 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3391 GNUNET_break_op (0);
3392 return GNUNET_SYSERR;
3394 GNUNET_STATISTICS_update (GST_stats,
3396 ("# SESSION_ACK messages received"),
3398 if (NULL == (n = lookup_neighbour (peer)))
3400 GNUNET_break_op (0);
3401 return GNUNET_SYSERR;
3403 /* Check if we are in a plausible state for having sent
3404 a CONNECT_ACK. If not, return, otherwise break.
3406 The remote peers sends a SESSION_ACK as a response for a CONNECT_ACK
3409 We expect a SESSION_ACK:
3410 - If a remote peer has sent a CONNECT, we responded with a CONNECT_ACK and
3411 now wait for the ACK to finally be connected
3412 - If we sent a CONNECT_ACK to this peer before */
3414 if ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3415 (ACK_SEND_SESSION_ACK != n->ack_state))
3417 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3418 "Received unexpected SESSION_ACK message from peer `%s' in state %s/%s\n",
3420 GNUNET_TRANSPORT_ps2s (n->state),
3421 print_ack_state (n->ack_state));
3423 GNUNET_STATISTICS_update (GST_stats,
3424 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3429 /* We are connected */
3430 if (GNUNET_NO == GST_neighbours_test_connected(&n->id))
3432 /* Notify about connection */
3433 connect_notify_cb (callback_cls, &n->id,
3434 n->primary_address.bandwidth_in,
3435 n->primary_address.bandwidth_out);\
3437 GNUNET_STATISTICS_set (GST_stats,
3438 gettext_noop ("# peers connected"),
3439 ++neighbours_connected,
3443 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3444 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3446 /* Add session to ATS since no session was given (NULL) and we may have
3447 * obtained a new session */
3448 GST_ats_add_address (n->primary_address.address, n->primary_address.session,
3451 /* Set primary address to used */
3452 set_primary_address (n,
3453 n->primary_address.address,
3454 n->primary_address.session,
3455 n->primary_address.bandwidth_in,
3456 n->primary_address.bandwidth_out,
3463 * Test if we're connected to the given peer.
3465 * @param target peer to test
3466 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3469 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3471 return test_connected (lookup_neighbour (target));
3475 * Change the incoming quota for the given peer.
3477 * @param neighbour identity of peer to change qutoa for
3478 * @param quota new quota
3481 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3482 struct GNUNET_BANDWIDTH_Value32NBO quota)
3484 struct NeighbourMapEntry *n;
3486 if (NULL == (n = lookup_neighbour (neighbour)))
3488 GNUNET_STATISTICS_update (GST_stats,
3490 ("# SET QUOTA messages ignored (no such peer)"),
3494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3495 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3496 ntohl (quota.value__), GNUNET_i2s (&n->id));
3497 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3498 if (0 != ntohl (quota.value__))
3500 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3501 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3502 GNUNET_i2s (&n->id));
3503 if (GNUNET_YES == test_connected (n))
3504 GNUNET_STATISTICS_update (GST_stats,
3505 gettext_noop ("# disconnects due to quota of 0"),
3507 disconnect_neighbour (n);
3510 void delayed_disconnect (void *cls,
3511 const struct GNUNET_SCHEDULER_TaskContext* tc)
3513 struct NeighbourMapEntry *n = cls;
3515 n->delayed_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
3516 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3517 "Disconnecting by request from peer %s\n",
3518 GNUNET_i2s (&n->id));
3519 free_neighbour (n, GNUNET_NO);
3524 * We received a disconnect message from the given peer,
3525 * validate and process.
3527 * @param peer sender of the message
3528 * @param msg the disconnect message
3531 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3532 const struct GNUNET_MessageHeader *msg)
3534 struct NeighbourMapEntry *n;
3535 const struct SessionDisconnectMessage *sdm;
3537 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3538 "Received DISCONNECT message from peer `%s'\n",
3540 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3542 GNUNET_break_op (0);
3543 GNUNET_STATISTICS_update (GST_stats,
3545 ("# disconnect messages ignored (malformed)"), 1,
3549 GNUNET_STATISTICS_update (GST_stats,
3551 ("# DISCONNECT messages received"),
3553 sdm = (const struct SessionDisconnectMessage *) msg;
3554 if (NULL == (n = lookup_neighbour (peer)))
3555 return; /* gone already */
3556 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3558 GNUNET_STATISTICS_update (GST_stats,
3560 ("# disconnect messages ignored (timestamp)"), 1,
3564 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3566 GNUNET_break_op (0);
3569 if (ntohl (sdm->purpose.size) !=
3570 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3571 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3572 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3574 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3575 "%s message from peer `%s' has invalid size \n",
3578 GNUNET_break_op (0);
3582 GNUNET_CRYPTO_eddsa_verify
3583 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3584 &sdm->signature, &sdm->public_key))
3586 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3587 "%s message from peer `%s' cannot be verified \n",
3590 GNUNET_break_op (0);
3593 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect, n);
3598 * Closure for the neighbours_iterate function.
3600 struct IteratorContext
3603 * Function to call on each connected neighbour.
3605 GST_NeighbourIterator cb;
3615 * Call the callback from the closure for each neighbour.
3617 * @param cls the `struct IteratorContext`
3618 * @param key the hash of the public key of the neighbour
3619 * @param value the `struct NeighbourMapEntry`
3620 * @return #GNUNET_OK (continue to iterate)
3623 neighbours_iterate (void *cls,
3624 const struct GNUNET_PeerIdentity *key,
3627 struct IteratorContext *ic = cls;
3628 struct NeighbourMapEntry *n = value;
3629 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3630 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3633 if (NULL != n->primary_address.address)
3635 bandwidth_in = n->primary_address.bandwidth_in;
3636 bandwidth_out = n->primary_address.bandwidth_out;
3640 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3641 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3645 n->primary_address.address,
3648 bandwidth_in, bandwidth_out);
3654 * Iterate over all connected neighbours.
3656 * @param cb function to call
3657 * @param cb_cls closure for cb
3660 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3662 struct IteratorContext ic;
3664 if (NULL == neighbours)
3665 return; /* can happen during shutdown */
3668 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3673 * If we have an active connection to the given target, it must be shutdown.
3675 * @param target peer to disconnect from
3678 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3680 struct NeighbourMapEntry *n;
3682 if (NULL == (n = lookup_neighbour (target)))
3683 return; /* not active */
3684 if (GNUNET_YES == test_connected (n))
3685 GNUNET_STATISTICS_update (GST_stats,
3687 ("# disconnected from peer upon explicit request"), 1,
3689 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3690 "Forced disconnect from peer %s\n",
3691 GNUNET_i2s (target));
3692 disconnect_neighbour (n);
3697 * Obtain current latency information for the given neighbour.
3699 * @param peer to get the latency for
3700 * @return observed latency of the address, FOREVER if the
3701 * the connection is not up
3703 struct GNUNET_TIME_Relative
3704 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3706 struct NeighbourMapEntry *n;
3708 n = lookup_neighbour (peer);
3710 return GNUNET_TIME_UNIT_FOREVER_REL;
3713 case GNUNET_TRANSPORT_PS_CONNECTED:
3714 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3715 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3716 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3718 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3719 case GNUNET_TRANSPORT_PS_INIT_ATS:
3720 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3721 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3722 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3723 case GNUNET_TRANSPORT_PS_DISCONNECT:
3724 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3725 return GNUNET_TIME_UNIT_FOREVER_REL;
3727 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3728 "Unhandled state `%s'\n",
3729 GNUNET_TRANSPORT_ps2s (n->state));
3733 return GNUNET_TIME_UNIT_FOREVER_REL;
3738 * Obtain current address information for the given neighbour.
3741 * @return address currently used
3743 struct GNUNET_HELLO_Address *
3744 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3746 struct NeighbourMapEntry *n;
3748 n = lookup_neighbour (peer);
3751 return n->primary_address.address;
3756 * Initialize the neighbours subsystem.
3758 * @param cls closure for callbacks
3759 * @param connect_cb function to call if we connect to a peer
3760 * @param disconnect_cb function to call if we disconnect from a peer
3761 * @param peer_address_cb function to call if we change an active address
3763 * @param max_fds maximum number of fds to use
3766 GST_neighbours_start (void *cls,
3767 NotifyConnect connect_cb,
3768 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3769 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3770 unsigned int max_fds)
3773 connect_notify_cb = connect_cb;
3774 disconnect_notify_cb = disconnect_cb;
3775 neighbour_change_cb = peer_address_cb;
3776 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3777 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3778 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3779 utilization_transmission, NULL);
3784 * Disconnect from the given neighbour.
3787 * @param key hash of neighbour's public key (not used)
3788 * @param value the 'struct NeighbourMapEntry' of the neighbour
3789 * @return #GNUNET_OK (continue to iterate)
3792 disconnect_all_neighbours (void *cls,
3793 const struct GNUNET_PeerIdentity *key,
3796 struct NeighbourMapEntry *n = value;
3798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3799 "Disconnecting peer `%4s', %s\n",
3800 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3801 free_neighbour (n, GNUNET_NO);
3807 * Cleanup the neighbours subsystem.
3810 GST_neighbours_stop ()
3812 struct BlacklistCheckSwitchContext *cur;
3813 struct BlacklistCheckSwitchContext *next;
3815 if (NULL == neighbours)
3817 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3819 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3820 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3823 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &disconnect_all_neighbours,
3825 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3827 next = pending_bc_head;
3828 for (cur = next; NULL != cur; cur = next )
3831 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3833 if (NULL != cur->blc)
3835 GST_blacklist_test_cancel (cur->blc);
3838 if (NULL != cur->address)
3839 GNUNET_HELLO_address_free (cur->address);
3840 GNUNET_free_non_null (cur->ats);
3844 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
3845 &free_notification_cb, NULL);
3846 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
3847 registered_quota_notifications = NULL;
3850 callback_cls = NULL;
3851 connect_notify_cb = NULL;
3852 disconnect_notify_cb = NULL;
3853 neighbour_change_cb = NULL;
3857 /* end of file gnunet-service-transport_neighbours.c */