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 SYN message for this neighbour
97 /* The neighbour received a SYN message and has to send a SYN_ACK
101 /* The neighbour sent a SYN_ACK message and has to send a ACK
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_SYN'
112 * should be answered with a 'SESSION_SYN_ACK' with the same body
113 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
114 * a 'ACK'. Once the 'ACK' is received, both peers
115 * should be connected.
117 struct TransportSynMessage
120 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
121 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_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 'SYN' 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 'SYN'.
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 SYN_ACK message.
363 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
364 * our SYN_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 SYN message
409 * with a SYN_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 SYN_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_SYN_ACK
416 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
417 * send a SYN_ACK and should go to 'S_CONNECTED' upon receiving a
418 * '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_SYN_ACK:
550 return "SEND_SYN_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_SYN_SENT:
1122 send_disconnect (n);
1123 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1125 case GNUNET_TRANSPORT_PS_SYN_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_SYN_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_SYN_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 with %u bytes was a %s\n",
1234 GNUNET_i2s (receiver),
1235 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1236 mq->message_buf_size,
1237 (success == GNUNET_OK) ? "success" : "FAILURE");
1238 if (NULL != mq->cont)
1239 mq->cont (mq->cont_cls, success, size_payload, physical);
1245 * Check the message list for the given neighbour and if we can
1246 * send a message, do so. This function should only be called
1247 * if the connection is at least generally ready for transmission.
1248 * While we will only send one message at a time, no bandwidth
1249 * quota management is performed here. If a message was given to
1250 * the plugin, the continuation will automatically re-schedule
1251 * the 'master' task once the next message might be transmitted.
1253 * @param n target peer for which to transmit
1256 try_transmission_to_peer (struct NeighbourMapEntry *n)
1258 struct MessageQueue *mq;
1259 struct GNUNET_TIME_Relative timeout;
1261 if (NULL == n->primary_address.address)
1263 /* no address, why are we here? */
1267 if ((0 == n->primary_address.address->address_length) &&
1268 (NULL == n->primary_address.session))
1270 /* no address, why are we here? */
1274 if (NULL != n->is_active)
1276 /* transmission already pending */
1280 /* timeout messages from the queue that are past their due date */
1281 while (NULL != (mq = n->messages_head))
1283 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1284 if (timeout.rel_value_us > 0)
1286 GNUNET_STATISTICS_update (GST_stats,
1288 ("# messages timed out while in transport queue"),
1290 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1292 transmit_send_continuation (mq, &n->id,
1294 mq->message_buf_size, 0); /* timeout */
1297 return; /* no more messages */
1298 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "Giving message with %u bytes to plugin session %p\n",
1303 mq->message_buf_size, n->primary_address.session);
1305 (void) send_with_session (n,
1306 mq->message_buf, mq->message_buf_size,
1307 0 /* priority */, timeout, GNUNET_NO,
1308 &transmit_send_continuation, mq);
1313 * Send keepalive message to the neighbour. Must only be called
1314 * if we are on 'connected' state or while trying to switch addresses.
1315 * Will internally determine if a keepalive is truly needed (so can
1316 * always be called).
1318 * @param n neighbour that went idle and needs a keepalive
1321 send_keepalive (struct NeighbourMapEntry *n)
1323 struct SessionKeepAliveMessage m;
1324 struct GNUNET_TIME_Relative timeout;
1327 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1328 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT));
1329 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1330 return; /* no keepalive needed at this time */
1332 nonce = 0; /* 0 indicates 'not set' */
1334 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1336 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1337 "Sending keep alive to peer `%s' with nonce %u\n",
1338 GNUNET_i2s (&n->id), nonce);
1340 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1341 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1342 m.nonce = htonl (nonce);
1344 timeout = send_with_session (n,
1345 (const void *) &m, sizeof (m),
1346 UINT32_MAX /* priority */,
1347 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1349 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1351 n->primary_address.keep_alive_nonce = nonce;
1352 n->expect_latency_response = GNUNET_YES;
1353 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1354 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1360 * Keep the connection to the given neighbour alive longer,
1361 * we received a KEEPALIVE (or equivalent); send a response.
1363 * @param neighbour neighbour to keep alive (by sending keep alive response)
1364 * @param m the keep alive message containing the nonce to respond to
1367 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1368 const struct GNUNET_MessageHeader *m)
1370 struct NeighbourMapEntry *n;
1371 const struct SessionKeepAliveMessage *msg_in;
1372 struct SessionKeepAliveMessage msg;
1374 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1377 msg_in = (struct SessionKeepAliveMessage *) m;
1378 if (NULL == (n = lookup_neighbour (neighbour)))
1380 GNUNET_STATISTICS_update (GST_stats,
1382 ("# KEEPALIVE messages discarded (peer unknown)"),
1386 if (NULL == n->primary_address.session)
1388 GNUNET_STATISTICS_update (GST_stats,
1390 ("# KEEPALIVE messages discarded (no session)"),
1395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1396 "Received keep alive request from peer `%s' with nonce %u\n",
1397 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1399 /* send reply to allow neighbour to measure latency */
1400 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1401 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1402 msg.nonce = msg_in->nonce;
1403 (void) send_with_session(n,
1404 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1405 UINT32_MAX /* priority */,
1406 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1412 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1413 * latency to this peer. Pass the updated information (existing ats
1414 * plus calculated latency) to ATS.
1416 * @param neighbour neighbour to keep alive
1417 * @param m the message containing the keep alive response
1420 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1421 const struct GNUNET_MessageHeader *m)
1423 struct NeighbourMapEntry *n;
1424 const struct SessionKeepAliveMessage *msg;
1425 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1427 struct GNUNET_ATS_Information ats;
1429 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1432 msg = (const struct SessionKeepAliveMessage *) m;
1433 if (NULL == (n = lookup_neighbour (neighbour)))
1435 GNUNET_STATISTICS_update (GST_stats,
1437 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1441 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1442 (GNUNET_YES != n->expect_latency_response) )
1444 GNUNET_STATISTICS_update (GST_stats,
1446 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1450 if (NULL == n->primary_address.address)
1452 GNUNET_STATISTICS_update (GST_stats,
1454 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1458 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1460 GNUNET_STATISTICS_update (GST_stats,
1462 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1469 "Received keep alive response from peer `%s' for session %p\n",
1470 GNUNET_i2s (&n->id), n->primary_address.session);
1474 /* Update session timeout here */
1475 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1478 "Updating session for peer `%s' for session %p\n",
1479 GNUNET_i2s (&n->id), n->primary_address.session);
1480 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1487 n->primary_address.keep_alive_nonce = 0;
1488 n->expect_latency_response = GNUNET_NO;
1489 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1490 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 "Latency for peer `%s' is %s\n",
1494 GNUNET_i2s (&n->id),
1495 GNUNET_STRINGS_relative_time_to_string (n->latency,
1497 /* append latency */
1498 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1499 if (n->latency.rel_value_us > UINT32_MAX)
1500 latency = UINT32_MAX;
1502 latency = n->latency.rel_value_us;
1503 ats.value = htonl (latency);
1504 GST_ats_update_metrics (&n->id, n->primary_address.address,
1505 n->primary_address.session, &ats, 1);
1510 * We have received a message from the given sender. How long should
1511 * we delay before receiving more? (Also used to keep the peer marked
1514 * @param sender sender of the message
1515 * @param size size of the message
1516 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1517 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1518 * #GNUNET_SYSERR if the connection is not fully up yet
1519 * @return how long to wait before reading more from this sender
1521 struct GNUNET_TIME_Relative
1522 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1523 *sender, ssize_t size, int *do_forward)
1525 struct NeighbourMapEntry *n;
1526 struct GNUNET_TIME_Relative ret;
1528 if (NULL == neighbours)
1530 *do_forward = GNUNET_NO;
1531 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1533 if (NULL == (n = lookup_neighbour (sender)))
1535 GST_neighbours_try_connect (sender);
1536 if (NULL == (n = lookup_neighbour (sender)))
1538 GNUNET_STATISTICS_update (GST_stats,
1540 ("# messages discarded due to lack of neighbour record"),
1542 *do_forward = GNUNET_NO;
1543 return GNUNET_TIME_UNIT_ZERO;
1546 if (! test_connected (n))
1548 *do_forward = GNUNET_SYSERR;
1549 return GNUNET_TIME_UNIT_ZERO;
1551 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1553 n->quota_violation_count++;
1554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1555 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1556 n->in_tracker.available_bytes_per_s__,
1557 n->quota_violation_count);
1558 /* Discount 32k per violation */
1559 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1563 if (n->quota_violation_count > 0)
1565 /* try to add 32k back */
1566 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1567 n->quota_violation_count--;
1570 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1572 GNUNET_STATISTICS_update (GST_stats,
1574 ("# bandwidth quota violations by other peers"),
1576 *do_forward = GNUNET_NO;
1577 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1579 *do_forward = GNUNET_YES;
1580 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1581 if (ret.rel_value_us > 0)
1583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1584 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1585 (unsigned long long) n->in_tracker.
1586 consumption_since_last_update__,
1587 (unsigned int) n->in_tracker.available_bytes_per_s__,
1588 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1589 GNUNET_STATISTICS_update (GST_stats,
1590 gettext_noop ("# ms throttling suggested"),
1591 (int64_t) ret.rel_value_us / 1000LL,
1599 * Transmit a message to the given target using the active connection.
1601 * @param target destination
1602 * @param msg message to send
1603 * @param msg_size number of bytes in msg
1604 * @param timeout when to fail with timeout
1605 * @param cont function to call when done
1606 * @param cont_cls closure for 'cont'
1609 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1610 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1611 GST_NeighbourSendContinuation cont, void *cont_cls)
1613 struct NeighbourMapEntry *n;
1614 struct MessageQueue *mq;
1616 /* All ove these cases should never happen; they are all API violations.
1617 But we check anyway, just to be sure. */
1618 if (NULL == (n = lookup_neighbour (target)))
1622 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1625 if (GNUNET_YES != test_connected (n))
1629 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1632 bytes_in_send_queue += msg_size;
1633 GNUNET_STATISTICS_set (GST_stats,
1635 ("# bytes in message queue for other peers"),
1636 bytes_in_send_queue, GNUNET_NO);
1637 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1639 mq->cont_cls = cont_cls;
1640 memcpy (&mq[1], msg, msg_size);
1641 mq->message_buf = (const char *) &mq[1];
1642 mq->message_buf_size = msg_size;
1643 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueueing %u bytes to send to peer %s\n",
1646 msg_size, GNUNET_i2s (target));
1648 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1649 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1650 GNUNET_SCHEDULER_cancel (n->task);
1651 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1655 send_session_connect_cont (void *cls,
1656 const struct GNUNET_PeerIdentity *target,
1658 size_t size_payload,
1659 size_t size_on_wire)
1661 struct NeighbourMapEntry *n;
1663 n = lookup_neighbour (target);
1666 /* SYN continuation was called after neighbor was freed,
1667 * for example due to a time out for the state or the session
1668 * used was already terminated: nothing to do here... */
1672 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1673 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1674 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT != n->state))
1676 /* SYN continuation was called after neighbor changed state,
1677 * for example due to a time out for the state or the session
1678 * used was already terminated: nothing to do here... */
1681 if (GNUNET_OK == result)
1684 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1685 _("Failed to send SYN message to peer `%s' using address `%s' session %p\n"),
1686 GNUNET_i2s (target),
1687 GST_plugins_a2s (n->primary_address.address),
1688 n->primary_address.session);
1691 case GNUNET_TRANSPORT_PS_SYN_SENT:
1692 /* Remove address and request and additional one */
1693 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1694 n->primary_address.session);
1695 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL );
1696 unset_primary_address (n);
1697 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1698 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1700 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1701 /* Remove address and request and additional one */
1702 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1703 n->primary_address.session);
1704 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL );
1705 unset_primary_address (n);
1706 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1707 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1709 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
1710 /* Remove address and request and go back to primary address */
1711 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1712 ("# Failed attempts to switch addresses (failed to send SYN CONT)"), 1, GNUNET_NO);
1713 GNUNET_ATS_address_destroyed (GST_ats, n->alternative_address.address,
1714 n->alternative_address.session);
1715 GNUNET_ATS_address_destroyed (GST_ats, n->alternative_address.address,
1717 unset_alternative_address (n);
1718 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1719 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1722 disconnect_neighbour (n);
1728 * Send a SYN message via the given address.
1730 * @param na address to use
1733 send_syn (struct NeighbourAddress *na)
1735 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1736 struct TransportSynMessage connect_msg;
1737 struct NeighbourMapEntry *n;
1739 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1740 "Sending SYN message to peer `%s'\n",
1741 GNUNET_i2s (&na->address->peer));
1743 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1748 if (NULL == na->session)
1749 na->session = papi->get_session (papi->cls, na->address);
1750 if (NULL == na->session)
1755 GNUNET_STATISTICS_update (GST_stats,
1757 ("# SYN messages sent"),
1759 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1760 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1761 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1762 connect_msg.reserved = htonl (0);
1763 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1765 papi->send (papi->cls,
1767 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
1769 SETUP_CONNECTION_TIMEOUT,
1770 send_session_connect_cont, NULL))
1772 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1773 _("Failed to transmit SYN message via plugin to %s\n"),
1774 GST_plugins_a2s (na->address));
1776 n = lookup_neighbour (&na->address->peer);
1784 case GNUNET_TRANSPORT_PS_SYN_SENT:
1785 /* Remove address and request and additional one */
1786 unset_primary_address (n);
1787 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1788 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1789 /* Hard failure to send the SYN message with this address:
1790 Destroy address and session */
1792 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1793 /* Remove address and request and additional one */
1794 unset_primary_address (n);
1795 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1796 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1798 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
1799 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1800 ("# Failed attempts to switch addresses (failed to send SYN)"), 1, GNUNET_NO);
1801 /* Remove address and request and additional one */
1802 unset_alternative_address (n);
1803 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1804 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1807 disconnect_neighbour (n);
1810 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1811 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1813 GST_neighbours_notify_data_sent (&na->address->peer,
1816 sizeof (struct TransportSynMessage));
1821 send_session_connect_ack_cont (void *cls,
1822 const struct GNUNET_PeerIdentity *target,
1824 size_t size_payload,
1825 size_t size_on_wire)
1827 struct NeighbourMapEntry *n;
1829 n = lookup_neighbour (target);
1832 /* SYN_ACK continuation was called after neighbor was freed,
1833 * for example due to a time out for the state or the session
1834 * used was already terminated: nothing to do here... */
1838 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1840 /* SYN_ACK continuation was called after neighbor changed state,
1841 * for example due to a time out for the state or the session
1842 * used was already terminated: nothing to do here... */
1845 if (GNUNET_OK == result)
1848 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1849 _("Failed to send SYN_ACK message to peer `%s' using address `%s' session %p\n"),
1850 GNUNET_i2s (target),
1851 GST_plugins_a2s (n->primary_address.address),
1852 n->primary_address.session);
1854 /* Failed to send SYN_ACK message with this address */
1855 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1856 n->primary_address.session);
1857 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1860 /* Remove address and request and additional one */
1861 unset_primary_address (n);
1862 n->ack_state = ACK_SEND_SYN_ACK;
1863 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1864 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1870 * Send a SYN_ACK message via the given address.
1872 * @param address address to use
1873 * @param session session to use
1874 * @param timestamp timestamp to use for the ACK message
1875 * @return GNUNET_SYSERR if sending immediately failed, GNUNET_OK otherwise
1878 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1879 struct Session *session,
1880 struct GNUNET_TIME_Absolute timestamp)
1882 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1883 struct TransportSynMessage connect_msg;
1884 struct NeighbourMapEntry *n;
1886 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1887 "Sending SYN_ACK to peer `%s'\n",
1888 GNUNET_i2s (&address->peer));
1890 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1895 if (NULL == session)
1896 session = papi->get_session (papi->cls, address);
1897 if (NULL == session)
1902 GNUNET_STATISTICS_update (GST_stats,
1904 ("# SYN_ACK messages sent"),
1906 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1907 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
1908 connect_msg.reserved = htonl (0);
1909 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1911 if (GNUNET_SYSERR == papi->send (papi->cls,
1913 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
1915 GNUNET_TIME_UNIT_FOREVER_REL,
1916 send_session_connect_ack_cont, NULL))
1918 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1919 _("Failed to transmit SYN_ACK message via plugin to %s\n"),
1920 GST_plugins_a2s (address));
1922 n = lookup_neighbour (&address->peer);
1928 /* Hard failure to send the SYN_ACK message with this address:
1929 Destroy session (and address) */
1930 if (GNUNET_YES == GNUNET_HELLO_address_check_option(address,
1931 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1933 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1934 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1937 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1939 /* Remove address and request and additional one */
1940 unset_primary_address (n);
1941 n->ack_state = ACK_SEND_SYN_ACK;
1942 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1943 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1949 struct QuotaNotificationRequest
1951 struct GNUNET_PeerIdentity peer;
1952 struct Session *session;
1956 struct QNR_LookContext
1958 struct GNUNET_PeerIdentity peer;
1959 struct Session *session;
1962 struct QuotaNotificationRequest *res;
1966 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1968 struct QNR_LookContext *qnr_ctx = cls;
1969 struct QuotaNotificationRequest *qnr = value;
1971 if ((qnr->session == qnr_ctx->session) &&
1972 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1973 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1975 qnr_ctx->res = value;
1982 GST_neighbours_register_quota_notification(void *cls,
1983 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1984 struct Session *session)
1986 struct QuotaNotificationRequest *qnr;
1987 struct QNR_LookContext qnr_ctx;
1989 if (NULL == registered_quota_notifications)
1991 return; /* init or shutdown */
1994 qnr_ctx.peer = (*peer);
1995 qnr_ctx.plugin = plugin;
1996 qnr_ctx.session = session;
1999 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2000 peer, &find_notification_request, &qnr_ctx);
2001 if (NULL != qnr_ctx.res)
2007 qnr = GNUNET_new (struct QuotaNotificationRequest);
2008 qnr->peer = (*peer);
2009 qnr->plugin = GNUNET_strdup (plugin);
2010 qnr->session = session;
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2013 "Adding notification for peer `%s' plugin `%s' session %p \n",
2014 GNUNET_i2s (peer), plugin, session);
2016 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
2017 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2022 GST_neighbours_unregister_quota_notification(void *cls,
2023 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
2025 struct QNR_LookContext qnr_ctx;
2027 if (NULL == registered_quota_notifications)
2029 return; /* init or shutdown */
2032 qnr_ctx.peer = (*peer);
2033 qnr_ctx.plugin = plugin;
2034 qnr_ctx.session = session;
2037 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
2038 &find_notification_request, &qnr_ctx);
2039 if (NULL == qnr_ctx.res)
2045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2046 "Removing notification for peer `%s' plugin `%s' session %p \n",
2047 GNUNET_i2s (peer), plugin, session);
2049 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
2051 GNUNET_free (qnr_ctx.res->plugin);
2052 GNUNET_free (qnr_ctx.res);
2056 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2058 /* struct NeighbourMapEntry *n = cls; */
2059 struct QuotaNotificationRequest *qnr = value;
2060 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2061 struct GNUNET_TIME_Relative delay;
2064 papi = GST_plugins_find(qnr->plugin);
2071 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2073 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
2074 delay.rel_value_us / 1000);
2076 if (NULL != papi->update_inbound_delay)
2077 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
2082 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
2085 /* struct NeighbourMapEntry *n = cls; */
2086 struct QuotaNotificationRequest *qnr = value;
2088 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
2090 GNUNET_free(qnr->plugin);
2097 inbound_bw_tracker_update(void *cls)
2099 struct NeighbourMapEntry *n = cls;
2101 /* Quota was updated, tell plugins to update the time to receive next */
2102 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2103 &n->id, ¬ification_cb, n);
2108 * Create a fresh entry in the neighbour map for the given peer
2110 * @param peer peer to create an entry for
2111 * @return new neighbour map entry
2113 static struct NeighbourMapEntry *
2114 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2116 struct NeighbourMapEntry *n;
2118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2119 "Creating new neighbour entry for `%s'\n",
2121 n = GNUNET_new (struct NeighbourMapEntry);
2123 n->ack_state = ACK_UNDEFINED;
2124 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2125 n->last_util_transmission = GNUNET_TIME_absolute_get();
2126 n->util_payload_bytes_recv = 0;
2127 n->util_payload_bytes_sent = 0;
2128 n->util_total_bytes_recv = 0;
2129 n->util_total_bytes_sent = 0;
2130 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
2131 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2132 MAX_BANDWIDTH_CARRY_S);
2133 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2134 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2135 GNUNET_assert (GNUNET_OK ==
2136 GNUNET_CONTAINER_multipeermap_put (neighbours,
2138 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2142 /* We received a address suggestion after requesting an address in
2143 * try_connect or after receiving a connect, switch to address
2146 address_suggest_cont (void *cls,
2147 const struct GNUNET_PeerIdentity *peer,
2148 const struct GNUNET_HELLO_Address *address, struct Session *session,
2149 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
2150 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2151 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
2153 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
2154 bandwidth_in, bandwidth_out);
2158 struct BlacklistCheckSwitchContext
2160 struct BlacklistCheckSwitchContext *prev;
2161 struct BlacklistCheckSwitchContext *next;
2164 struct GST_BlacklistCheck *blc;
2166 struct GNUNET_HELLO_Address *address;
2167 struct Session *session;
2168 struct GNUNET_ATS_Information *ats;
2171 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2172 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2176 * Black list check result for try_connect call
2177 * If connection to the peer is allowed request adddress and
2179 * @param cls blc_ctx bl context
2180 * @param peer the peer
2181 * @param result the result
2184 try_connect_bl_check_cont (void *cls,
2185 const struct GNUNET_PeerIdentity *peer, int result)
2187 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2188 struct NeighbourMapEntry *n;
2190 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2191 GNUNET_free (blc_ctx);
2193 if (GNUNET_OK != result)
2195 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2196 _("Blacklisting disapproved to connect to peer `%s'\n"),
2201 /* Setup a new neighbour */
2202 n = setup_neighbour (peer);
2204 /* Request address suggestions for this peer */
2205 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2206 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2207 GNUNET_ATS_reset_backoff (GST_ats, peer);
2208 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2209 &address_suggest_cont, n);
2215 * Try to create a connection to the given target (eventually).
2217 * @param target peer to try to connect to
2220 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2222 struct NeighbourMapEntry *n;
2223 struct GST_BlacklistCheck *blc;
2224 struct BlacklistCheckSwitchContext *blc_ctx;
2226 if (NULL == neighbours)
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2229 "Asked to connect to peer `%s' during shutdown\n",
2230 GNUNET_i2s (target));
2231 return; /* during shutdown, do nothing */
2233 n = lookup_neighbour (target);
2234 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2235 "Asked to connect to peer `%s' (state: %s)\n",
2236 GNUNET_i2s (target),
2237 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2242 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2243 /* this should not be possible */
2245 free_neighbour (n, GNUNET_NO);
2247 case GNUNET_TRANSPORT_PS_INIT_ATS:
2248 case GNUNET_TRANSPORT_PS_SYN_SENT:
2249 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2250 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2251 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2252 "Ignoring request to try to connect to `%s', already trying!\n",
2253 GNUNET_i2s (target));
2254 return; /* already trying */
2255 case GNUNET_TRANSPORT_PS_CONNECTED:
2256 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2257 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2258 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
2259 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2260 "Ignoring request to try to connect, already connected to `%s'!\n",
2261 GNUNET_i2s (target));
2262 return; /* already connected */
2263 case GNUNET_TRANSPORT_PS_DISCONNECT:
2264 /* get rid of remains, ready to re-try immediately */
2265 free_neighbour (n, GNUNET_NO);
2267 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2268 /* should not be possible */
2272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2273 "Unhandled state `%s'\n",
2274 GNUNET_TRANSPORT_ps2s (n->state));
2276 free_neighbour (n, GNUNET_NO);
2281 /* Do blacklist check if connecting to this peer is allowed */
2282 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2283 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2285 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2286 &try_connect_bl_check_cont, blc_ctx)))
2294 * We received a 'SYN' message from the other peer.
2295 * Consider switching to it.
2297 * @param message possibly a 'struct TransportSynMessage' (check format)
2298 * @param peer identity of the peer to switch the address for
2299 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2302 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2303 const struct GNUNET_PeerIdentity *peer)
2305 const struct TransportSynMessage *scm;
2306 struct NeighbourMapEntry *n;
2307 struct GNUNET_TIME_Absolute ts;
2309 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2311 GNUNET_break_op (0);
2312 return GNUNET_SYSERR;
2314 GNUNET_STATISTICS_update (GST_stats,
2316 ("# SYN messages received"),
2318 if (NULL == neighbours)
2320 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2321 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2323 return GNUNET_OK; /* we're shutting down */
2325 scm = (const struct TransportSynMessage *) message;
2326 GNUNET_break_op (0 == ntohl (scm->reserved));
2327 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2328 n = lookup_neighbour (peer);
2331 /* This is a new neighbour and set to not connected */
2332 n = setup_neighbour (peer);
2335 /* Remember this SYN message in neighbour */
2336 n->ack_state = ACK_SEND_SYN_ACK;
2337 n->connect_ack_timestamp = ts;
2339 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2340 "Received SYN for peer `%s' in state %s/%s\n",
2342 GNUNET_TRANSPORT_ps2s (n->state),
2343 print_ack_state (n->ack_state));
2347 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2348 /* Request an address from ATS to send SYN_ACK to this peer */
2349 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2350 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2351 if (NULL == n->suggest_handle)
2352 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2354 case GNUNET_TRANSPORT_PS_INIT_ATS:
2355 /* SYN message takes priority over us asking ATS for address:
2356 * Wait for ATS to suggest an address and send SYN_ACK */
2357 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2358 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2360 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2361 /* We already wait for an address to send an SYN_ACK */
2363 case GNUNET_TRANSPORT_PS_SYN_SENT:
2364 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2365 /* Send ACK immediately */
2366 n->ack_state = ACK_SEND_ACK;
2367 send_connect_ack_message (n->primary_address.address,
2368 n->primary_address.session, ts);
2370 case GNUNET_TRANSPORT_PS_CONNECTED:
2371 /* we are already connected and can thus send the ACK immediately */
2372 GNUNET_assert (NULL != n->primary_address.address);
2373 GNUNET_assert (NULL != n->primary_address.session);
2374 n->ack_state = ACK_SEND_ACK;
2375 send_connect_ack_message (n->primary_address.address,
2376 n->primary_address.session, ts);
2378 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2379 /* We wait for ATS address suggestion */
2381 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2382 /* We received a SYN message while waiting for a SYN_ACK in fast
2383 * reconnect. Send SYN_ACK immediately */
2384 n->ack_state = ACK_SEND_ACK;
2385 send_connect_ack_message (n->primary_address.address,
2386 n->primary_address.session, n->connect_ack_timestamp);
2388 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
2389 /* We are already connected and can thus send the ACK immediately;
2390 still, it can never hurt to have an alternative address, so also
2391 tell ATS about it */
2392 GNUNET_assert (NULL != n->primary_address.address);
2393 GNUNET_assert (NULL != n->primary_address.session);
2394 n->ack_state = ACK_SEND_ACK;
2395 send_connect_ack_message (n->primary_address.address,
2396 n->primary_address.session, ts);
2398 case GNUNET_TRANSPORT_PS_DISCONNECT:
2399 /* Get rid of remains without terminating sessions, ready to re-try */
2400 free_neighbour (n, GNUNET_YES);
2401 n = setup_neighbour (peer);
2402 /* Remember the SYN time stamp for ACK message */
2403 n->ack_state = ACK_SEND_SYN_ACK;
2404 n->connect_ack_timestamp = ts;
2405 /* Request an address for the peer */
2406 GNUNET_ATS_suggest_address (GST_ats, peer, address_suggest_cont, n);
2407 GNUNET_ATS_reset_backoff (GST_ats, peer);
2408 set_state (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS);
2410 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2411 /* should not be possible */
2415 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2416 "Unhandled state `%s'\n",
2417 GNUNET_TRANSPORT_ps2s (n->state));
2419 return GNUNET_SYSERR;
2425 switch_address_bl_check_cont (void *cls,
2426 const struct GNUNET_PeerIdentity *peer, int result)
2428 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2429 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2430 struct NeighbourMapEntry *n;
2432 papi = GST_plugins_find (blc_ctx->address->transport_name);
2434 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2440 "Peer %s is unknown, suggestion ignored\n",
2443 if (result == GNUNET_NO)
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2447 GST_plugins_a2s (blc_ctx->address),
2449 GNUNET_i2s (&blc_ctx->address->peer));
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2454 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2455 blc_ctx->address->transport_name,
2456 GST_plugins_a2s (blc_ctx->address),
2458 GNUNET_i2s (&blc_ctx->address->peer));
2461 /* This address is blacklisted, delete address and session (if existing) in ATS */
2462 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2464 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2465 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2467 /* This is an inbound address, destroy full address */
2468 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2471 /* Remove blacklist check and clean up */
2472 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2473 GNUNET_HELLO_address_free (blc_ctx->address);
2474 GNUNET_free_non_null (blc_ctx->ats);
2475 GNUNET_free (blc_ctx);
2479 if (NULL == blc_ctx->session)
2481 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483 "Obtained new session for peer `%s' and address '%s': %p\n",
2484 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2486 if (NULL == blc_ctx->session)
2488 /* No session could be obtained, remove blacklist check and clean up */
2489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2490 "Failed to obtain new session for peer `%s' and address '%s'\n",
2491 GNUNET_i2s (&blc_ctx->address->peer),
2492 GST_plugins_a2s (blc_ctx->address));
2493 /* Delete address in ATS */
2494 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2496 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2497 GNUNET_HELLO_address_free (blc_ctx->address);
2498 GNUNET_free_non_null (blc_ctx->ats);
2499 GNUNET_free (blc_ctx);
2503 if ( (NULL != n->primary_address.address) &&
2504 (0 == GNUNET_HELLO_address_cmp(blc_ctx->address, n->primary_address.address)) )
2506 if (blc_ctx->session == n->primary_address.session)
2508 /* This address is already primary, update only quotas */
2509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2510 "Updating quota for peer `%s' address `%s' session %p\n",
2511 GNUNET_i2s (&blc_ctx->address->peer),
2512 GST_plugins_a2s (blc_ctx->address),
2515 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2516 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2518 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2519 GNUNET_HELLO_address_free(blc_ctx->address);
2520 GNUNET_free_non_null (blc_ctx->ats);
2521 GNUNET_free (blc_ctx);
2527 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2528 "Peer `%s' switches to address `%s' session %p\n",
2529 GNUNET_i2s (&blc_ctx->address->peer),
2530 GST_plugins_a2s (blc_ctx->address),
2535 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2537 free_neighbour (n, GNUNET_NO);
2539 case GNUNET_TRANSPORT_PS_INIT_ATS:
2540 /* We requested an address and ATS suggests one:
2541 * set primary address and send SYN message*/
2542 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2543 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2544 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2546 /* Send pending SYN_ACK message */
2547 n->ack_state = ACK_SEND_ACK;
2548 send_connect_ack_message (n->primary_address.address,
2549 n->primary_address.session, n->connect_ack_timestamp);
2551 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_SENT,
2552 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2553 send_syn (&n->primary_address);
2555 case GNUNET_TRANSPORT_PS_SYN_SENT:
2556 /* ATS suggested a new address while waiting for an SYN_ACK:
2557 * Switch and send new SYN */
2558 /* ATS suggests a different address, switch again */
2559 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2560 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2561 if (ACK_SEND_SYN_ACK == n->ack_state)
2563 /* Send pending SYN_ACK message */
2564 n->ack_state = ACK_SEND_ACK;
2565 send_connect_ack_message (n->primary_address.address,
2566 n->primary_address.session, n->connect_ack_timestamp);
2568 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_SENT,
2569 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2570 send_syn (&n->primary_address);
2572 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2573 /* We requested an address and ATS suggests one:
2574 * set primary address and send SYN_ACK message*/
2575 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2576 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2577 /* Send an ACK message as a response to the SYN msg */
2578 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2579 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2580 send_connect_ack_message (n->primary_address.address,
2581 n->primary_address.session,
2582 n->connect_ack_timestamp);
2583 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2584 (ACK_UNDEFINED == n->ack_state) )
2585 n->ack_state = ACK_SEND_ACK;
2587 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2588 /* ATS asks us to switch while we were trying to connect; switch to new
2589 address and check blacklist again */
2590 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2592 n->ack_state = ACK_SEND_ACK;
2593 send_connect_ack_message (n->primary_address.address,
2594 n->primary_address.session, n->connect_ack_timestamp);
2596 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2597 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2598 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2599 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2601 case GNUNET_TRANSPORT_PS_CONNECTED:
2602 GNUNET_assert (NULL != n->primary_address.address);
2603 GNUNET_assert (NULL != n->primary_address.session);
2604 if (n->primary_address.session == blc_ctx->session)
2606 /* not an address change, just a quota change */
2607 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2608 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2611 /* ATS asks us to switch a life connection; see if we can get
2612 a SYN_ACK on it before we actually do this! */
2613 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2614 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2615 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT,
2616 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2617 GNUNET_STATISTICS_update (GST_stats, gettext_noop
2618 ("# Attempts to switch addresses"), 1, GNUNET_NO);
2619 send_syn (&n->alternative_address);
2621 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2622 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2623 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2624 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2626 /* Send pending SYN_ACK message */
2627 n->ack_state = ACK_SEND_ACK;
2628 send_connect_ack_message (n->primary_address.address,
2629 n->primary_address.session, n->connect_ack_timestamp);
2631 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2632 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2633 send_syn (&n->primary_address);
2635 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2636 /* ATS asks us to switch while we were trying to reconnect; switch to new
2637 address and send SYN again */
2638 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2639 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2640 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2641 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2642 send_syn (&n->primary_address);
2644 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
2645 if ( (0 == GNUNET_HELLO_address_cmp(n->primary_address.address,
2646 blc_ctx->address) && n->primary_address.session == blc_ctx->session) )
2648 /* ATS switches back to still-active session */
2649 free_address (&n->alternative_address);
2650 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2653 /* ATS asks us to switch a life connection, send */
2654 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2655 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2656 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT,
2657 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2658 send_syn (&n->alternative_address);
2660 case GNUNET_TRANSPORT_PS_DISCONNECT:
2661 /* not going to switch addresses while disconnecting */
2663 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2667 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2668 "Unhandled state `%s'\n",
2669 GNUNET_TRANSPORT_ps2s (n->state));
2674 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2675 GNUNET_HELLO_address_free(blc_ctx->address);
2676 GNUNET_free_non_null (blc_ctx->ats);
2677 GNUNET_free (blc_ctx);
2683 * For the given peer, switch to this address.
2685 * Before accepting this addresses and actively using it, a blacklist check
2686 * is performed. If this blacklist check fails the address will be destroyed.
2688 * @param peer identity of the peer to switch the address for
2689 * @param address address of the other peer,
2690 * @param session session to use or NULL if transport should initiate a session
2691 * @param ats performance data
2692 * @param ats_count number of entries in ats
2693 * @param bandwidth_in inbound quota to be used when connection is up,
2694 * 0 to disconnect from peer
2695 * @param bandwidth_out outbound quota to be used when connection is up,
2696 * 0 to disconnect from peer
2699 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2700 const struct GNUNET_HELLO_Address *address,
2701 struct Session *session,
2702 const struct GNUNET_ATS_Information *ats,
2704 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2705 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2707 struct NeighbourMapEntry *n;
2708 struct GST_BlacklistCheck *blc;
2709 struct BlacklistCheckSwitchContext *blc_ctx;
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2713 "ATS has decided on an address for peer %s\n",
2715 GNUNET_assert (NULL != address->transport_name);
2716 if (NULL == (n = lookup_neighbour (peer)))
2718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2719 "Peer %s is unknown, suggestion ignored\n",
2724 /* Check if plugin is available */
2725 if (NULL == (GST_plugins_find (address->transport_name)))
2727 /* we don't have the plugin for this address */
2728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2729 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2730 address->transport_name,
2732 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2735 if ((NULL == session) &&
2736 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2738 /* This is a inbound address and we do not have a session to use! */
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2740 "Inbound address without session `%s'! Destroying address...\n",
2741 GST_plugins_a2s (address));
2742 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2747 "ATS suggests %s address '%s' session %p for "
2748 "peer `%s' in state %s/%s \n",
2749 GNUNET_HELLO_address_check_option (address,
2750 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2751 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2752 GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state));
2754 /* Perform blacklist check */
2755 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2756 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2757 blc_ctx->session = session;
2758 blc_ctx->bandwidth_in = bandwidth_in;
2759 blc_ctx->bandwidth_out = bandwidth_out;
2760 blc_ctx->ats_count = ats_count;
2761 blc_ctx->ats = NULL;
2764 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2765 for (c = 0; c < ats_count; c++)
2767 blc_ctx->ats[c].type = ats[c].type;
2768 blc_ctx->ats[c].value = ats[c].value;
2772 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2773 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2774 &switch_address_bl_check_cont, blc_ctx)))
2782 send_utilization_data (void *cls,
2783 const struct GNUNET_PeerIdentity *key,
2786 struct NeighbourMapEntry *n = value;
2787 struct GNUNET_ATS_Information atsi[4];
2789 uint32_t bps_pl_out;
2792 struct GNUNET_TIME_Relative delta;
2794 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2795 GNUNET_TIME_absolute_get ());
2799 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2800 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2802 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2803 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2805 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2810 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2811 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2813 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2814 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2818 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2822 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2823 atsi[0].value = htonl (bps_out);
2824 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2825 atsi[1].value = htonl (bps_in);
2827 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2828 atsi[2].value = htonl (bps_pl_out);
2829 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2830 atsi[3].value = htonl (bps_pl_in);
2832 GST_ats_update_metrics (key, n->primary_address.address,
2833 n->primary_address.session, atsi, 4);
2834 n->util_payload_bytes_recv = 0;
2835 n->util_payload_bytes_sent = 0;
2836 n->util_total_bytes_recv = 0;
2837 n->util_total_bytes_sent = 0;
2838 n->last_util_transmission = GNUNET_TIME_absolute_get();
2844 * Task transmitting utilization in a regular interval
2846 * @param cls the 'struct NeighbourMapEntry' for which we are running
2847 * @param tc scheduler context (unused)
2850 utilization_transmission (void *cls,
2851 const struct GNUNET_SCHEDULER_TaskContext *tc)
2853 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2855 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2856 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2858 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2859 utilization_transmission, NULL);
2865 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2866 const struct GNUNET_HELLO_Address *address,
2867 struct Session *session,
2868 const struct GNUNET_MessageHeader *message)
2870 struct NeighbourMapEntry *n;
2872 n = lookup_neighbour (peer);
2875 n->util_total_bytes_recv += ntohs(message->size);
2880 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2881 const struct GNUNET_HELLO_Address *address,
2882 struct Session *session,
2883 const struct GNUNET_MessageHeader *message)
2885 struct NeighbourMapEntry *n;
2886 n = lookup_neighbour (peer);
2889 n->util_payload_bytes_recv += ntohs(message->size);
2894 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2895 const struct GNUNET_HELLO_Address *address,
2896 struct Session *session,
2899 struct NeighbourMapEntry *n;
2900 n = lookup_neighbour (peer);
2903 if (n->primary_address.session != session)
2905 n->util_total_bytes_sent += size;
2910 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2913 struct NeighbourMapEntry *n;
2914 n = lookup_neighbour (peer);
2917 n->util_payload_bytes_sent += size;
2922 * Master task run for every neighbour. Performs all of the time-related
2923 * activities (keep alive, send next message, disconnect if idle, finish
2924 * clean up after disconnect).
2926 * @param cls the 'struct NeighbourMapEntry' for which we are running
2927 * @param tc scheduler context (unused)
2930 master_task (void *cls,
2931 const struct GNUNET_SCHEDULER_TaskContext *tc)
2933 struct NeighbourMapEntry *n = cls;
2934 struct GNUNET_TIME_Relative delay;
2936 n->task = GNUNET_SCHEDULER_NO_TASK;
2937 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2938 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2939 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2940 GNUNET_i2s (&n->id),
2941 GNUNET_TRANSPORT_ps2s(n->state),
2942 GNUNET_STRINGS_relative_time_to_string (delay,
2946 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2947 /* invalid state for master task, clean up */
2949 free_neighbour (n, GNUNET_NO);
2951 case GNUNET_TRANSPORT_PS_INIT_ATS:
2952 if (0 == delay.rel_value_us)
2954 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2955 "Connection to `%s' timed out waiting for ATS to provide address\n",
2956 GNUNET_i2s (&n->id));
2957 free_neighbour (n, GNUNET_NO);
2961 case GNUNET_TRANSPORT_PS_SYN_SENT:
2962 if (0 == delay.rel_value_us)
2964 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2965 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2966 GNUNET_i2s (&n->id));
2967 /* We could not send to this address, delete address and session */
2968 if (NULL != n->primary_address.session)
2969 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
2970 n->primary_address.session);
2971 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
2973 /* Remove address and request and additional one */
2974 unset_primary_address (n);
2975 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2976 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2980 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2981 if (0 == delay.rel_value_us)
2983 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2984 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2985 GNUNET_i2s (&n->id));
2986 free_neighbour (n, GNUNET_NO);
2990 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2991 if (0 == delay.rel_value_us)
2993 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2994 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2995 GNUNET_i2s (&n->id));
2996 disconnect_neighbour (n);
3000 case GNUNET_TRANSPORT_PS_CONNECTED:
3001 if (0 == delay.rel_value_us)
3003 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3004 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3005 GNUNET_i2s (&n->id));
3006 disconnect_neighbour (n);
3009 try_transmission_to_peer (n);
3012 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3013 if (0 == delay.rel_value_us)
3015 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3016 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3017 GNUNET_i2s (&n->id));
3018 disconnect_neighbour (n);
3022 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3023 if (0 == delay.rel_value_us)
3025 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3026 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
3027 GNUNET_i2s (&n->id));
3028 disconnect_neighbour (n);
3032 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
3033 if (0 == delay.rel_value_us)
3035 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3036 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to SYN on alternative address)\n",
3037 GNUNET_i2s (&n->id));
3038 GNUNET_STATISTICS_update (GST_stats, gettext_noop
3039 ("# Failed attempts to switch addresses (no response)"), 1, GNUNET_NO);
3040 disconnect_neighbour (n);
3043 try_transmission_to_peer (n);
3046 case GNUNET_TRANSPORT_PS_DISCONNECT:
3047 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3048 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3049 GNUNET_i2s (&n->id));
3050 free_neighbour (n, GNUNET_NO);
3052 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3053 /* how did we get here!? */
3057 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3058 "Unhandled state `%s'\n",
3059 GNUNET_TRANSPORT_ps2s (n->state));
3063 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT == n->state) ||
3064 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3066 /* if we are *now* in one of the two states, we're sending
3067 keep alive messages, so we need to consider the keepalive
3068 delay, not just the connection timeout */
3069 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3072 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3073 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3080 * Send a ACK message to the neighbour to confirm that we
3083 * @param n neighbour to send the ACK to
3086 send_session_ack_message (struct NeighbourMapEntry *n)
3088 struct GNUNET_MessageHeader msg;
3090 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sending ACK message to peer `%s'\n",
3091 GNUNET_i2s (&n->id));
3093 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3094 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3095 (void) send_with_session(n,
3096 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3097 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3103 * We received a 'SESSION_SYN_ACK' message from the other peer.
3104 * Consider switching to it.
3106 * @param message possibly a 'struct SessionConnectMessage' (check format)
3107 * @param peer identity of the peer to switch the address for
3108 * @param address address of the other peer, NULL if other peer
3110 * @param session session to use (or NULL)
3111 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3114 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3115 const struct GNUNET_PeerIdentity *peer,
3116 const struct GNUNET_HELLO_Address *address,
3117 struct Session *session)
3119 const struct TransportSynMessage *scm;
3120 struct GNUNET_TIME_Absolute ts;
3121 struct NeighbourMapEntry *n;
3123 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3124 "Received SYN_ACK message from peer `%s'\n",
3127 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3129 GNUNET_break_op (0);
3130 return GNUNET_SYSERR;
3132 GNUNET_STATISTICS_update (GST_stats,
3134 ("# SYN_ACK messages received"),
3136 scm = (const struct TransportSynMessage *) message;
3137 GNUNET_break_op (ntohl (scm->reserved) == 0);
3138 if (NULL == (n = lookup_neighbour (peer)))
3140 GNUNET_STATISTICS_update (GST_stats,
3142 ("# unexpected SYN_ACK messages (no peer)"),
3144 return GNUNET_SYSERR;
3146 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3149 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3151 free_neighbour (n, GNUNET_NO);
3152 return GNUNET_SYSERR;
3153 case GNUNET_TRANSPORT_PS_INIT_ATS:
3154 GNUNET_STATISTICS_update (GST_stats,
3156 ("# unexpected SYN_ACK messages (not ready)"),
3159 case GNUNET_TRANSPORT_PS_SYN_SENT:
3160 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3162 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3163 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3166 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3167 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3168 GNUNET_STATISTICS_set (GST_stats,
3169 gettext_noop ("# peers connected"),
3170 ++neighbours_connected,
3172 connect_notify_cb (callback_cls, &n->id,
3173 n->primary_address.bandwidth_in,
3174 n->primary_address.bandwidth_out);
3175 /* Tell ATS that the outbound session we created to send SYN was successful */
3176 GST_ats_add_address (n->primary_address.address,
3177 n->primary_address.session,
3179 set_primary_address (n,
3180 n->primary_address.address,
3181 n->primary_address.session,
3182 n->primary_address.bandwidth_in,
3183 n->primary_address.bandwidth_out,
3185 send_session_ack_message (n);
3187 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3188 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3189 GNUNET_STATISTICS_update (GST_stats,
3191 ("# unexpected SYN_ACK messages (not ready)"),
3194 case GNUNET_TRANSPORT_PS_CONNECTED:
3195 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3196 send_session_ack_message (n);
3198 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3199 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3200 to give us a new address... */
3201 GNUNET_STATISTICS_update (GST_stats,
3203 ("# unexpected SYN_ACK messages (waiting on ATS)"),
3206 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3207 /* Reconnecting with new address address worked; go back to connected! */
3208 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3209 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3210 send_session_ack_message (n);
3212 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
3213 /* new address worked; adopt it and go back to connected! */
3214 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3215 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3216 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3218 /* Notify about session... perhaps we obtained it */
3219 GST_ats_add_address (n->alternative_address.address,
3220 n->alternative_address.session, NULL, 0);
3221 /* Set primary addresses */
3222 set_primary_address (n, n->alternative_address.address,
3223 n->alternative_address.session, n->alternative_address.bandwidth_in,
3224 n->alternative_address.bandwidth_out, GNUNET_YES);
3225 GNUNET_STATISTICS_update (GST_stats, gettext_noop
3226 ("# Successful attempts to switch addresses"), 1, GNUNET_NO);
3228 free_address (&n->alternative_address);
3229 send_session_ack_message (n);
3231 case GNUNET_TRANSPORT_PS_DISCONNECT:
3232 GNUNET_STATISTICS_update (GST_stats,
3234 ("# unexpected SYN_ACK messages (disconnecting)"),
3236 return GNUNET_SYSERR;
3237 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3242 "Unhandled state `%s'\n",
3243 GNUNET_TRANSPORT_ps2s (n->state));
3245 return GNUNET_SYSERR;
3252 * A session was terminated. Take note; if needed, try to get
3253 * an alternative address from ATS.
3255 * @param peer identity of the peer where the session died
3256 * @param session session that is gone
3257 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3258 * this session was not in use
3261 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3262 struct Session *session)
3264 struct NeighbourMapEntry *n;
3265 struct BlackListCheckContext *bcc;
3266 struct BlackListCheckContext *bcc_next;
3268 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3270 while (NULL != (bcc = bcc_next))
3272 bcc_next = bcc->next;
3273 if (bcc->na.session == session)
3275 if (NULL != bcc->bc)
3276 GST_blacklist_test_cancel (bcc->bc);
3277 GNUNET_HELLO_address_free (bcc->na.address);
3278 GNUNET_CONTAINER_DLL_remove (bc_head,
3284 if (NULL == (n = lookup_neighbour (peer)))
3285 return GNUNET_NO; /* can't affect us */
3286 if (session != n->primary_address.session)
3288 /* Free alternative address */
3289 if (session == n->alternative_address.session)
3291 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT == n->state) )
3292 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3293 free_address (&n->alternative_address);
3295 return GNUNET_NO; /* doesn't affect us further */
3298 n->expect_latency_response = GNUNET_NO;
3299 /* The session for neighbour's primary address died */
3302 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3304 free_neighbour (n, GNUNET_NO);
3306 case GNUNET_TRANSPORT_PS_INIT_ATS:
3308 free_neighbour (n, GNUNET_NO);
3310 case GNUNET_TRANSPORT_PS_SYN_SENT:
3311 /* The session used to send the SYN terminated:
3312 * this implies a connect error*/
3313 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3314 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3316 GST_plugins_a2s (n->primary_address.address),
3317 n->primary_address.session,
3320 /* Destroy the address since it cannot be used */
3321 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3322 unset_primary_address (n);
3323 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3324 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3326 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3327 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3328 /* error on inbound session; free neighbour entirely */
3329 free_address (&n->primary_address);
3330 free_neighbour (n, GNUNET_NO);
3332 case GNUNET_TRANSPORT_PS_CONNECTED:
3333 /* Our primary connection died, try a fast reconnect */
3334 unset_primary_address (n);
3335 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3336 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3338 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3339 /* we don't have an address, how can it go down? */
3342 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3343 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3344 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3346 GST_plugins_a2s (n->primary_address.address),
3347 n->primary_address.session,
3350 /* Destroy the address since it cannot be used */
3351 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3352 unset_primary_address (n);
3353 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3354 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3356 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
3357 /* primary went down while we were waiting for SYN_ACK on secondary;
3358 secondary as primary */
3360 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3361 "Connection `%s' %p to peer `%s' was terminated while switching, "
3362 "switching to alternative address `%s' %p\n",
3363 GST_plugins_a2s (n->primary_address.address),
3364 n->primary_address.session,
3366 GST_plugins_a2s (n->alternative_address.address),
3367 n->alternative_address.session);
3369 /* Destroy the inbound address since it cannot be used */
3371 == GNUNET_HELLO_address_check_option (n->primary_address.address,
3372 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
3373 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3374 free_address (&n->primary_address);
3377 n->primary_address = n->alternative_address;
3378 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3379 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3380 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3382 case GNUNET_TRANSPORT_PS_DISCONNECT:
3383 free_address (&n->primary_address);
3385 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3386 /* neighbour was freed and plugins told to terminate session */
3390 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3391 "Unhandled state `%s'\n",
3392 GNUNET_TRANSPORT_ps2s (n->state));
3396 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3397 GNUNET_SCHEDULER_cancel (n->task);
3398 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3404 * We received a 'ACK' message from the other peer.
3405 * If we sent a 'SYN_ACK' last, this means we are now
3406 * connected. Otherwise, do nothing.
3408 * @param message possibly a 'struct SessionConnectMessage' (check format)
3409 * @param peer identity of the peer to switch the address for
3410 * @param address address of the other peer, NULL if other peer
3412 * @param session session to use (or NULL)
3413 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3416 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3417 const struct GNUNET_PeerIdentity *peer,
3418 const struct GNUNET_HELLO_Address *address,
3419 struct Session *session)
3421 struct NeighbourMapEntry *n;
3423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3424 "Received ACK message from peer `%s'\n",
3426 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3428 GNUNET_break_op (0);
3429 return GNUNET_SYSERR;
3431 GNUNET_STATISTICS_update (GST_stats,
3433 ("# ACK messages received"),
3435 if (NULL == (n = lookup_neighbour (peer)))
3437 GNUNET_break_op (0);
3438 return GNUNET_SYSERR;
3441 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3442 "Received %s for peer `%s' in state %s/%s\n",
3445 GNUNET_TRANSPORT_ps2s (n->state),
3446 print_ack_state (n->ack_state));
3448 /* Check if we are in a plausible state for having sent
3449 a SYN_ACK. If not, return, otherwise break.
3451 The remote peers sends a ACK as a response for a SYN_ACK
3455 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3456 now wait for the ACK to finally be connected
3457 - If we sent a SYN_ACK to this peer before */
3459 if ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3460 (ACK_SEND_ACK != n->ack_state))
3462 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3463 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3465 GNUNET_TRANSPORT_ps2s (n->state),
3466 print_ack_state (n->ack_state));
3468 GNUNET_STATISTICS_update (GST_stats,
3469 gettext_noop ("# unexpected ACK messages"), 1,
3474 /* We are connected */
3475 if (GNUNET_NO == GST_neighbours_test_connected(&n->id))
3477 /* Notify about connection */
3478 connect_notify_cb (callback_cls, &n->id,
3479 n->primary_address.bandwidth_in,
3480 n->primary_address.bandwidth_out);\
3482 GNUNET_STATISTICS_set (GST_stats,
3483 gettext_noop ("# peers connected"),
3484 ++neighbours_connected,
3488 if (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT == n->state)
3490 /* We tried to switch addresses while being connect. We explicitly wait
3491 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3492 * so we do not want to set the address as in use! */
3496 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3497 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3499 /* Add session to ATS since no session was given (NULL) and we may have
3500 * obtained a new session */
3501 GST_ats_add_address (n->primary_address.address, n->primary_address.session,
3504 /* Set primary address to used */
3505 set_primary_address (n,
3506 n->primary_address.address,
3507 n->primary_address.session,
3508 n->primary_address.bandwidth_in,
3509 n->primary_address.bandwidth_out,
3516 * Test if we're connected to the given peer.
3518 * @param target peer to test
3519 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3522 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3524 return test_connected (lookup_neighbour (target));
3528 * Change the incoming quota for the given peer.
3530 * @param neighbour identity of peer to change qutoa for
3531 * @param quota new quota
3534 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3535 struct GNUNET_BANDWIDTH_Value32NBO quota)
3537 struct NeighbourMapEntry *n;
3539 if (NULL == (n = lookup_neighbour (neighbour)))
3541 GNUNET_STATISTICS_update (GST_stats,
3543 ("# SET QUOTA messages ignored (no such peer)"),
3547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3548 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3549 ntohl (quota.value__), GNUNET_i2s (&n->id));
3550 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3551 if (0 != ntohl (quota.value__))
3553 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3554 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3555 GNUNET_i2s (&n->id));
3556 if (GNUNET_YES == test_connected (n))
3557 GNUNET_STATISTICS_update (GST_stats,
3558 gettext_noop ("# disconnects due to quota of 0"),
3560 disconnect_neighbour (n);
3563 void delayed_disconnect (void *cls,
3564 const struct GNUNET_SCHEDULER_TaskContext* tc)
3566 struct NeighbourMapEntry *n = cls;
3568 n->delayed_disconnect_task = GNUNET_SCHEDULER_NO_TASK;
3569 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3570 "Disconnecting by request from peer %s\n",
3571 GNUNET_i2s (&n->id));
3572 free_neighbour (n, GNUNET_NO);
3577 * We received a disconnect message from the given peer,
3578 * validate and process.
3580 * @param peer sender of the message
3581 * @param msg the disconnect message
3584 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3585 const struct GNUNET_MessageHeader *msg)
3587 struct NeighbourMapEntry *n;
3588 const struct SessionDisconnectMessage *sdm;
3590 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3591 "Received DISCONNECT message from peer `%s'\n",
3593 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3595 GNUNET_break_op (0);
3596 GNUNET_STATISTICS_update (GST_stats,
3598 ("# disconnect messages ignored (malformed)"), 1,
3602 GNUNET_STATISTICS_update (GST_stats,
3604 ("# DISCONNECT messages received"),
3606 sdm = (const struct SessionDisconnectMessage *) msg;
3607 if (NULL == (n = lookup_neighbour (peer)))
3608 return; /* gone already */
3609 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3611 GNUNET_STATISTICS_update (GST_stats,
3613 ("# disconnect messages ignored (timestamp)"), 1,
3617 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3619 GNUNET_break_op (0);
3622 if (ntohl (sdm->purpose.size) !=
3623 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3624 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3625 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3627 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3628 "%s message from peer `%s' has invalid size \n",
3631 GNUNET_break_op (0);
3635 GNUNET_CRYPTO_eddsa_verify
3636 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3637 &sdm->signature, &sdm->public_key))
3639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3640 "%s message from peer `%s' cannot be verified \n",
3643 GNUNET_break_op (0);
3646 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect, n);
3651 * Closure for the neighbours_iterate function.
3653 struct IteratorContext
3656 * Function to call on each connected neighbour.
3658 GST_NeighbourIterator cb;
3668 * Call the callback from the closure for each neighbour.
3670 * @param cls the `struct IteratorContext`
3671 * @param key the hash of the public key of the neighbour
3672 * @param value the `struct NeighbourMapEntry`
3673 * @return #GNUNET_OK (continue to iterate)
3676 neighbours_iterate (void *cls,
3677 const struct GNUNET_PeerIdentity *key,
3680 struct IteratorContext *ic = cls;
3681 struct NeighbourMapEntry *n = value;
3682 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3683 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3686 if (NULL != n->primary_address.address)
3688 bandwidth_in = n->primary_address.bandwidth_in;
3689 bandwidth_out = n->primary_address.bandwidth_out;
3693 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3694 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3698 n->primary_address.address,
3701 bandwidth_in, bandwidth_out);
3707 * Iterate over all connected neighbours.
3709 * @param cb function to call
3710 * @param cb_cls closure for cb
3713 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3715 struct IteratorContext ic;
3717 if (NULL == neighbours)
3718 return; /* can happen during shutdown */
3721 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3726 * If we have an active connection to the given target, it must be shutdown.
3728 * @param target peer to disconnect from
3731 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3733 struct NeighbourMapEntry *n;
3735 if (NULL == (n = lookup_neighbour (target)))
3736 return; /* not active */
3737 if (GNUNET_YES == test_connected (n))
3738 GNUNET_STATISTICS_update (GST_stats,
3740 ("# disconnected from peer upon explicit request"), 1,
3742 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3743 "Forced disconnect from peer %s\n",
3744 GNUNET_i2s (target));
3745 disconnect_neighbour (n);
3750 * Obtain current latency information for the given neighbour.
3752 * @param peer to get the latency for
3753 * @return observed latency of the address, FOREVER if the
3754 * the connection is not up
3756 struct GNUNET_TIME_Relative
3757 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3759 struct NeighbourMapEntry *n;
3761 n = lookup_neighbour (peer);
3763 return GNUNET_TIME_UNIT_FOREVER_REL;
3766 case GNUNET_TRANSPORT_PS_CONNECTED:
3767 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_SYN_SENT:
3768 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3769 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3771 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3772 case GNUNET_TRANSPORT_PS_INIT_ATS:
3773 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3774 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3775 case GNUNET_TRANSPORT_PS_SYN_SENT:
3776 case GNUNET_TRANSPORT_PS_DISCONNECT:
3777 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3778 return GNUNET_TIME_UNIT_FOREVER_REL;
3780 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3781 "Unhandled state `%s'\n",
3782 GNUNET_TRANSPORT_ps2s (n->state));
3786 return GNUNET_TIME_UNIT_FOREVER_REL;
3791 * Obtain current address information for the given neighbour.
3794 * @return address currently used
3796 struct GNUNET_HELLO_Address *
3797 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3799 struct NeighbourMapEntry *n;
3801 n = lookup_neighbour (peer);
3804 return n->primary_address.address;
3809 * Initialize the neighbours subsystem.
3811 * @param cls closure for callbacks
3812 * @param connect_cb function to call if we connect to a peer
3813 * @param disconnect_cb function to call if we disconnect from a peer
3814 * @param peer_address_cb function to call if we change an active address
3816 * @param max_fds maximum number of fds to use
3819 GST_neighbours_start (void *cls,
3820 NotifyConnect connect_cb,
3821 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3822 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3823 unsigned int max_fds)
3826 connect_notify_cb = connect_cb;
3827 disconnect_notify_cb = disconnect_cb;
3828 neighbour_change_cb = peer_address_cb;
3829 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3830 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3831 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3832 utilization_transmission, NULL);
3837 * Disconnect from the given neighbour.
3840 * @param key hash of neighbour's public key (not used)
3841 * @param value the 'struct NeighbourMapEntry' of the neighbour
3842 * @return #GNUNET_OK (continue to iterate)
3845 disconnect_all_neighbours (void *cls,
3846 const struct GNUNET_PeerIdentity *key,
3849 struct NeighbourMapEntry *n = value;
3851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3852 "Disconnecting peer `%4s', %s\n",
3853 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3854 free_neighbour (n, GNUNET_NO);
3860 * Cleanup the neighbours subsystem.
3863 GST_neighbours_stop ()
3865 struct BlacklistCheckSwitchContext *cur;
3866 struct BlacklistCheckSwitchContext *next;
3868 if (NULL == neighbours)
3870 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3872 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3873 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3876 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &disconnect_all_neighbours,
3878 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3880 next = pending_bc_head;
3881 for (cur = next; NULL != cur; cur = next )
3884 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3886 if (NULL != cur->blc)
3888 GST_blacklist_test_cancel (cur->blc);
3891 if (NULL != cur->address)
3892 GNUNET_HELLO_address_free (cur->address);
3893 GNUNET_free_non_null (cur->ats);
3897 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
3898 &free_notification_cb, NULL);
3899 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
3900 registered_quota_notifications = NULL;
3903 callback_cls = NULL;
3904 connect_notify_cb = NULL;
3905 disconnect_notify_cb = NULL;
3906 neighbour_change_cb = NULL;
3910 /* end of file gnunet-service-transport_neighbours.c */