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
98 * The neighbour received a SYN message and has to send a SYN_ACK
101 ACK_SEND_SYN_ACK = 1,
104 * The neighbour sent a SYN_ACK message and has to send a ACK
111 GNUNET_NETWORK_STRUCT_BEGIN
114 * Message a peer sends to another to indicate that it intends to
115 * setup a connection/session for data exchange. A 'SESSION_SYN'
116 * should be answered with a 'SESSION_SYN_ACK' with the same body
117 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
118 * a 'ACK'. Once the 'ACK' is received, both peers
119 * should be connected.
121 struct TransportSynMessage
124 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
125 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
127 struct GNUNET_MessageHeader header;
132 uint32_t reserved GNUNET_PACKED;
135 * Absolute time at the sender. Only the most recent connect
136 * message implies which session is preferred by the sender.
138 struct GNUNET_TIME_AbsoluteNBO timestamp;
144 * Message a peer sends to another when connected to indicate that a
145 * session is in use and the peer is still alive or to respond to a keep alive.
146 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
147 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
148 * When the keep alive response with type is received, transport service
149 * will call the respective plugin to update the session timeout
151 struct SessionKeepAliveMessage
154 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
155 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
157 struct GNUNET_MessageHeader header;
160 * A nonce to identify the session the keep alive is used for
162 uint32_t nonce GNUNET_PACKED;
166 * Message we send to the other peer to notify him that we intentionally
167 * are disconnecting (to reduce timeouts). This is just a friendly
168 * notification, peers must not rely on always receiving disconnect
171 struct SessionDisconnectMessage
174 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
176 struct GNUNET_MessageHeader header;
181 uint32_t reserved GNUNET_PACKED;
184 * Purpose of the signature. Extends over the timestamp.
185 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
187 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
190 * Absolute time at the sender. Only the most recent connect
191 * message implies which session is preferred by the sender.
193 struct GNUNET_TIME_AbsoluteNBO timestamp;
196 * Public key of the sender.
198 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
201 * Signature of the peer that sends us the disconnect. Only
202 * valid if the timestamp is AFTER the timestamp from the
203 * corresponding 'SYN' message.
205 struct GNUNET_CRYPTO_EddsaSignature signature;
209 GNUNET_NETWORK_STRUCT_END
213 * For each neighbour we keep a list of messages
214 * that we still want to transmit to the neighbour.
220 * This is a doubly linked list.
222 struct MessageQueue *next;
225 * This is a doubly linked list.
227 struct MessageQueue *prev;
230 * Function to call once we're done.
232 GST_NeighbourSendContinuation cont;
235 * Closure for @e cont
240 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
241 * stuck together in memory. Allocated at the end of this struct.
243 const char *message_buf;
246 * Size of the message buf
248 size_t message_buf_size;
251 * At what time should we fail?
253 struct GNUNET_TIME_Absolute timeout;
261 * A possible address we could use to communicate with a neighbour.
263 struct NeighbourAddress
267 * Active session for this address.
269 struct Session *session;
272 * Network-level address information.
274 struct GNUNET_HELLO_Address *address;
277 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
278 * peer for this address. Use to check that the ACK is in response
279 * to our most recent 'SYN'.
281 struct GNUNET_TIME_Absolute connect_timestamp;
284 * Inbound bandwidth from ATS for this address.
286 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
289 * Outbound bandwidth from ATS for this address.
291 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
294 * Did we tell ATS that this is our 'active' address?
299 * The current nonce sent in the last keep alive messages
301 uint32_t keep_alive_nonce;
305 * Entry in neighbours.
307 struct NeighbourMapEntry
311 * Head of list of messages we would like to send to this peer;
312 * must contain at most one message per client.
314 struct MessageQueue *messages_head;
317 * Tail of list of messages we would like to send to this peer; must
318 * contain at most one message per client.
320 struct MessageQueue *messages_tail;
323 * Are we currently trying to send a message? If so, which one?
325 struct MessageQueue *is_active;
328 * Primary address we currently use to communicate with the neighbour.
330 struct NeighbourAddress primary_address;
333 * Alternative address currently under consideration for communicating
334 * with the neighbour.
336 struct NeighbourAddress alternative_address;
339 * Identity of this neighbour.
341 struct GNUNET_PeerIdentity id;
344 * Main task that drives this peer (timeouts, keepalives, etc.).
345 * Always runs the 'master_task'.
347 struct GNUNET_SCHEDULER_Task * task;
350 * Task to disconnect neighbour after we received a DISCONNECT message
352 struct GNUNET_SCHEDULER_Task * delayed_disconnect_task;
355 * At what time should we sent the next keep-alive message?
357 struct GNUNET_TIME_Absolute keep_alive_time;
360 * At what time did we sent the last keep-alive message? Used
361 * to calculate round-trip time ("latency").
363 struct GNUNET_TIME_Absolute last_keep_alive_time;
366 * Timestamp we should include in our next SYN_ACK message.
367 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
368 * our SYN_ACK message.
370 struct GNUNET_TIME_Absolute connect_ack_timestamp;
373 * ATS address suggest handle
375 struct GNUNET_ATS_SuggestHandle *suggest_handle;
378 * Time where we should cut the connection (timeout) if we don't
379 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
380 * if we are in S_CONNECTED).
382 struct GNUNET_TIME_Absolute timeout;
385 * Latest calculated latency value
387 struct GNUNET_TIME_Relative latency;
390 * Tracker for inbound bandwidth.
392 struct GNUNET_BANDWIDTH_Tracker in_tracker;
395 * How often has the other peer (recently) violated the inbound
396 * traffic limit? Incremented by 10 per violation, decremented by 1
397 * per non-violation (for each time interval).
399 unsigned int quota_violation_count;
402 * The current state of the peer.
404 enum GNUNET_TRANSPORT_PeerState state;
407 * Did we sent an KEEP_ALIVE message and are we expecting a response?
409 int expect_latency_response;
412 * When a peer wants to connect we have to reply to the 1st SYN message
413 * with a SYN_ACK message. But sometime we cannot send this message
414 * immediately since we do not have an address and then we have to remember
415 * to send this message as soon as we have an address.
417 * Flag to set if we still need to send a SYN_ACK message to the other peer
418 * (once we have an address to use and the peer has been allowed by our
419 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
420 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
421 * send a SYN_ACK and should go to 'S_CONNECTED' upon receiving a
422 * 'ACK' (regardless of what our own state machine might say).
424 enum GST_ACK_State ack_state;
427 * Tracking utilization of outbound bandwidth
429 uint32_t util_payload_bytes_sent;
432 * Tracking utilization of inbound bandwidth
434 uint32_t util_payload_bytes_recv;
437 * Tracking utilization of outbound bandwidth
439 uint32_t util_total_bytes_sent;
442 * Tracking utilization of inbound bandwidth
444 uint32_t util_total_bytes_recv;
447 * Date of last utilization transmission
449 struct GNUNET_TIME_Absolute last_util_transmission;
454 * Context for blacklist checks and the #try_connect_bl_check_cont()
455 * function. Stores information about ongoing blacklist checks.
457 struct BlackListCheckContext
461 * We keep blacklist checks in a DLL.
463 struct BlackListCheckContext *next;
466 * We keep blacklist checks in a DLL.
468 struct BlackListCheckContext *prev;
471 * Address that is being checked.
473 struct NeighbourAddress na;
476 * Handle to the ongoing blacklist check.
478 struct GST_BlacklistCheck *bc;
483 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
485 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
488 * We keep blacklist checks in a DLL so that we can find
489 * the 'sessions' in their 'struct NeighbourAddress' if
490 * a session goes down.
492 static struct BlackListCheckContext *bc_head;
495 * We keep blacklist checks in a DLL.
497 static struct BlackListCheckContext *bc_tail;
500 * List of pending blacklist checks: head
502 static struct BlacklistCheckSwitchContext *pending_bc_head;
505 * List of pending blacklist checks: tail
507 static struct BlacklistCheckSwitchContext *pending_bc_tail;
510 * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb
512 static void *callback_cls;
515 * Function to call when we connected to a neighbour.
517 static NotifyConnect connect_notify_cb;
520 * Function to call when we disconnected from a neighbour.
522 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
525 * Function to call when a neighbour changed address, state or bandwidth.
527 static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb;
530 * counter for connected neighbours
532 static unsigned int neighbours_connected;
535 * Number of bytes we have currently queued for transmission.
537 static unsigned long long bytes_in_send_queue;
540 * Task transmitting utilization data
542 static struct GNUNET_SCHEDULER_Task * util_transmission_tk;
545 static struct GNUNET_CONTAINER_MultiPeerMap *registered_quota_notifications;
548 print_ack_state (enum GST_ACK_State s)
553 case ACK_SEND_SYN_ACK:
554 return "SEND_SYN_ACK";
564 * Lookup a neighbour entry in the neighbours hash map.
566 * @param pid identity of the peer to look up
567 * @return the entry, NULL if there is no existing record
569 static struct NeighbourMapEntry *
570 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
572 if (NULL == neighbours)
574 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
579 * Test if we're connected to the given peer.
581 * @param n neighbour entry of peer to test
582 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
585 test_connected (struct NeighbourMapEntry *n)
589 return GNUNET_TRANSPORT_is_connected (n->state);
593 * Send information about a new outbound quota to our clients.
595 * @param target affected peer
596 * @param quota new quota
599 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
600 struct GNUNET_BANDWIDTH_Value32NBO quota)
602 struct QuotaSetMessage q_msg;
604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
605 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
606 ntohl (quota.value__), GNUNET_i2s (target));
607 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
608 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
610 q_msg.peer = (*target);
611 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
616 * We don't need a given neighbour address any more.
617 * Release its resources and give appropriate notifications
618 * to ATS and other subsystems.
620 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
623 free_address (struct NeighbourAddress *na)
625 if (GNUNET_YES == na->ats_active)
627 GST_validation_set_address_use (na->address, na->session, GNUNET_NO);
628 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
631 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
632 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
633 na->ats_active = GNUNET_NO;
634 na->keep_alive_nonce = 0;
635 if (NULL != na->address)
637 GNUNET_HELLO_address_free (na->address);
645 * Set net state for this neighbour and notify monitoring
647 * @param n the respective neighbour
648 * @param s the new state
651 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
654 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
656 GNUNET_TRANSPORT_ps2s(s));
657 neighbour_change_cb (callback_cls,
659 n->primary_address.address,
660 n->state, n->timeout,
661 n->primary_address.bandwidth_in,
662 n->primary_address.bandwidth_out);
667 * Set net state and state timeout for this neighbour and notify monitoring
669 * @param n the respective neighbour
670 * @param s the new state
671 * @param timeout the new timeout
674 set_state_and_timeout (struct NeighbourMapEntry *n,
675 enum GNUNET_TRANSPORT_PeerState s,
676 struct GNUNET_TIME_Absolute timeout)
679 n->timeout = timeout;
680 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
682 GNUNET_TRANSPORT_ps2s(s),
683 GNUNET_STRINGS_absolute_time_to_string (timeout));
684 neighbour_change_cb (callback_cls,
686 n->primary_address.address,
687 n->state, n->timeout,
688 n->primary_address.bandwidth_in,
689 n->primary_address.bandwidth_out);
694 * Set new state timeout for this neighbour and notify monitoring
696 * @param n the respective neighbour
697 * @param timeout the new timeout
700 set_timeout (struct NeighbourMapEntry *n,
701 struct GNUNET_TIME_Absolute timeout)
703 n->timeout = timeout;
704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Neighbour `%s' changed timeout %s\n",
706 GNUNET_STRINGS_absolute_time_to_string (timeout));
707 neighbour_change_cb (callback_cls,
709 n->primary_address.address,
710 n->state, n->timeout,
711 n->primary_address.bandwidth_in,
712 n->primary_address.bandwidth_out);
717 * Initialize the alternative address of a neighbour
719 * @param n the neighbour
720 * @param address address of the other peer, NULL if other peer
722 * @param session session to use (or NULL, in which case an
723 * address must be setup)
724 * @param bandwidth_in inbound quota to be used when connection is up
725 * @param bandwidth_out outbound quota to be used when connection is up
728 set_alternative_address (struct NeighbourMapEntry *n,
729 const struct GNUNET_HELLO_Address *address,
730 struct Session *session,
731 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
732 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
734 struct GNUNET_TRANSPORT_PluginFunctions *papi;
735 if (NULL == (papi = GST_plugins_find (address->transport_name)))
740 if (session == n->alternative_address.session)
742 n->alternative_address.bandwidth_in = bandwidth_in;
743 n->alternative_address.bandwidth_out = bandwidth_out;
746 free_address (&n->alternative_address);
748 session = papi->get_session (papi->cls, address);
751 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
752 "Failed to obtain new session for peer `%s' and address '%s'\n",
753 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
754 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
758 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
760 GST_plugins_a2s(address));
762 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
763 n->alternative_address.bandwidth_in = bandwidth_in;
764 n->alternative_address.bandwidth_out = bandwidth_out;
765 n->alternative_address.session = session;
766 n->alternative_address.ats_active = GNUNET_NO;
767 n->alternative_address.keep_alive_nonce = 0;
772 * Initialize the primary address of a neighbour
774 * @param n the neighbour
775 * @param address address of the other peer, NULL if other peer
777 * @param session session to use (or NULL, in which case an
778 * address must be setup)
779 * @param bandwidth_in inbound quota to be used when connection is up
780 * @param bandwidth_out outbound quota to be used when connection is up
781 * @param is_active #GNUNET_YES to mark this as the active address with ATS
784 set_primary_address (struct NeighbourMapEntry *n,
785 const struct GNUNET_HELLO_Address *address,
786 struct Session *session,
787 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
788 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
791 struct GNUNET_TRANSPORT_PluginFunctions *papi;
793 if (NULL == (papi = GST_plugins_find (address->transport_name)))
798 if (session == n->primary_address.session)
800 n->primary_address.bandwidth_in = bandwidth_in;
801 n->primary_address.bandwidth_out = bandwidth_out;
802 if (is_active != n->primary_address.ats_active)
804 n->primary_address.ats_active = is_active;
805 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
806 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
808 if (GNUNET_YES == is_active)
810 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
811 send_outbound_quota (&address->peer, bandwidth_out);
815 free_address (&n->primary_address);
817 session = papi->get_session (papi->cls, address);
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Failed to obtain new session for peer `%s' and address '%s'\n",
822 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
823 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
827 n->primary_address.address = GNUNET_HELLO_address_copy (address);
828 n->primary_address.bandwidth_in = bandwidth_in;
829 n->primary_address.bandwidth_out = bandwidth_out;
830 n->primary_address.session = session;
831 n->primary_address.ats_active = is_active;
832 n->primary_address.keep_alive_nonce = 0;
833 if (GNUNET_YES == is_active)
835 /* Telling ATS about new session */
836 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
837 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
838 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
839 send_outbound_quota (&address->peer, bandwidth_out);
842 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
844 GST_plugins_a2s(address));
846 neighbour_change_cb (callback_cls,
848 n->primary_address.address,
849 n->state, n->timeout,
850 n->primary_address.bandwidth_in,
851 n->primary_address.bandwidth_out);
855 * Clear the primary address of a neighbour since this address is not
856 * valid anymore and notify monitoring about it
858 * @param n the neighbour
861 unset_primary_address (struct NeighbourMapEntry *n)
863 /* Unset primary address */
864 free_address (&n->primary_address);
866 /* Notify monitoring about it */
867 neighbour_change_cb (callback_cls,
870 n->state, n->timeout,
871 n->primary_address.bandwidth_in,
872 n->primary_address.bandwidth_out);
876 * Clear the alternative address of a neighbour since this address is not
879 * @param n the neighbour
882 unset_alternative_address (struct NeighbourMapEntry *n)
884 /* Unset primary address */
885 free_address (&n->alternative_address);
889 * Free a neighbour map entry.
891 * @param n entry to free
892 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
893 * #GNUNET_YES to keep all sessions
896 free_neighbour (struct NeighbourMapEntry *n,
899 struct MessageQueue *mq;
900 struct GNUNET_TRANSPORT_PluginFunctions *papi;
901 struct GNUNET_HELLO_Address *backup_primary;
903 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
904 "Freeing neighbour state of peer `%s'\n",
905 GNUNET_i2s (&n->id));
906 n->is_active = NULL; /* always free'd by its own continuation! */
908 /* fail messages currently in the queue */
909 while (NULL != (mq = n->messages_head))
911 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
912 if (NULL != mq->cont)
913 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
916 /* It is too late to send other peer disconnect notifications, but at
917 least internally we need to get clean... */
918 if (GNUNET_YES == test_connected (n))
920 GNUNET_STATISTICS_set (GST_stats,
921 gettext_noop ("# peers connected"),
922 --neighbours_connected,
924 disconnect_notify_cb (callback_cls, &n->id);
927 /* Mark peer as disconnected */
928 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
930 if (NULL != n->primary_address.address)
931 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
933 backup_primary = NULL;
935 /* free addresses and mark as unused */
936 unset_primary_address (n);
937 free_address (&n->alternative_address);
939 /* cut all transport-level connection for this peer */
940 if ((GNUNET_NO == keep_sessions) &&
941 (NULL != backup_primary) &&
942 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
943 papi->disconnect_peer (papi->cls, &n->id);
945 if (NULL != backup_primary)
946 GNUNET_HELLO_address_free (backup_primary);
947 GNUNET_assert (GNUNET_YES ==
948 GNUNET_CONTAINER_multipeermap_remove (neighbours,
951 /* Cancel address requests for this peer */
952 if (NULL != n->suggest_handle)
954 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
955 n->suggest_handle = NULL;
958 /* Cancel the disconnect task */
959 if (NULL != n->delayed_disconnect_task)
961 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
962 n->delayed_disconnect_task = NULL;
965 /* Cancel the master task */
968 GNUNET_SCHEDULER_cancel (n->task);
971 /* free rest of memory */
977 * Transmit a message using the current session of the given
980 * @param n entry for the recipient
981 * @param msgbuf buffer to transmit
982 * @param msgbuf_size number of bytes in @a msgbuf buffer
983 * @param priority transmission priority
984 * @param timeout transmission timeout
985 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
986 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
987 * @param cont continuation to call when finished (can be NULL)
988 * @param cont_cls closure for @a cont
989 * @return timeout (copy of @a timeout or a calculated one if
990 * @a use_keepalive_timeout is #GNUNET_YES.
992 static struct GNUNET_TIME_Relative
993 send_with_session (struct NeighbourMapEntry *n,
994 const char *msgbuf, size_t msgbuf_size,
996 struct GNUNET_TIME_Relative timeout,
997 unsigned int use_keepalive_timeout,
998 GNUNET_TRANSPORT_TransmitContinuation cont,
1001 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1002 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
1004 GNUNET_assert (n->primary_address.session != NULL);
1005 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1006 (-1 == papi->send (papi->cls,
1007 n->primary_address.session,
1008 msgbuf, msgbuf_size,
1010 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
1011 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1012 papi->query_keepalive_factor (papi->cls))),
1013 cont, cont_cls)))) &&
1015 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1016 GST_neighbours_notify_data_sent (&n->id,
1017 n->primary_address.address, n->primary_address.session, msgbuf_size);
1018 GNUNET_break (NULL != papi);
1024 * Master task run for every neighbour. Performs all of the time-related
1025 * activities (keep alive, send next message, disconnect if idle, finish
1026 * clean up after disconnect).
1028 * @param cls the `struct NeighbourMapEntry` for which we are running
1029 * @param tc scheduler context (unused)
1032 master_task (void *cls,
1033 const struct GNUNET_SCHEDULER_TaskContext *tc);
1037 * Function called when the 'DISCONNECT' message has been sent by the
1038 * plugin. Frees the neighbour --- if the entry still exists.
1041 * @param target identity of the neighbour that was disconnected
1042 * @param result #GNUNET_OK if the disconnect got out successfully
1043 * @param payload bytes payload
1044 * @param physical bytes physical
1047 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1048 int result, size_t payload, size_t physical)
1050 struct NeighbourMapEntry *n;
1052 n = lookup_neighbour (target);
1054 return; /* already gone */
1055 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1056 return; /* have created a fresh entry since */
1057 if (NULL != n->task)
1058 GNUNET_SCHEDULER_cancel (n->task);
1059 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1064 * Transmit a DISCONNECT message to the other peer.
1066 * @param n neighbour to send DISCONNECT message.
1069 send_disconnect (struct NeighbourMapEntry *n)
1071 struct SessionDisconnectMessage disconnect_msg;
1073 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1074 "Sending DISCONNECT message to peer `%4s'\n",
1075 GNUNET_i2s (&n->id));
1076 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1077 disconnect_msg.header.type =
1078 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1079 disconnect_msg.reserved = htonl (0);
1080 disconnect_msg.purpose.size =
1081 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1082 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1083 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1084 disconnect_msg.purpose.purpose =
1085 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1086 disconnect_msg.timestamp =
1087 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1088 disconnect_msg.public_key = GST_my_identity.public_key;
1089 GNUNET_assert (GNUNET_OK ==
1090 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1091 &disconnect_msg.purpose,
1092 &disconnect_msg.signature));
1094 (void) send_with_session (n, (const char *) &disconnect_msg,
1095 sizeof (disconnect_msg), UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1096 GNUNET_NO, &send_disconnect_cont, NULL );
1097 GNUNET_STATISTICS_update (GST_stats,
1099 ("# DISCONNECT messages sent"), 1,
1105 * Disconnect from the given neighbour, clean up the record.
1107 * @param n neighbour to disconnect from
1110 disconnect_neighbour (struct NeighbourMapEntry *n)
1112 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1113 "Disconnecting from peer %s in state %s\n",
1114 GNUNET_i2s (&n->id),
1115 GNUNET_TRANSPORT_ps2s (n->state));
1116 /* depending on state, notify neighbour and/or upper layers of this peer
1120 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1121 case GNUNET_TRANSPORT_PS_INIT_ATS:
1122 /* other peer is completely unaware of us, no need to send DISCONNECT */
1123 free_neighbour (n, GNUNET_NO);
1125 case GNUNET_TRANSPORT_PS_SYN_SENT:
1126 send_disconnect (n);
1127 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1129 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1130 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1131 free_neighbour (n, GNUNET_NO);
1133 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1134 /* we DID ACK the other peer's request, must send DISCONNECT */
1135 send_disconnect (n);
1136 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1138 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1139 case GNUNET_TRANSPORT_PS_CONNECTED:
1140 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1141 /* we are currently connected, need to send disconnect and do
1142 internal notifications and update statistics */
1143 send_disconnect (n);
1144 GNUNET_STATISTICS_set (GST_stats,
1145 gettext_noop ("# peers connected"),
1146 --neighbours_connected,
1148 disconnect_notify_cb (callback_cls, &n->id);
1149 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1151 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1152 /* Disconnecting while waiting for an ATS address to reconnect,
1153 * cannot send DISCONNECT */
1154 free_neighbour (n, GNUNET_NO);
1156 case GNUNET_TRANSPORT_PS_DISCONNECT:
1157 /* already disconnected, ignore */
1159 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1160 /* already cleaned up, how did we get here!? */
1164 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1165 "Unhandled state `%s'\n",
1166 GNUNET_TRANSPORT_ps2s (n->state));
1170 /* schedule timeout to clean up */
1171 if (NULL != n->task)
1172 GNUNET_SCHEDULER_cancel (n->task);
1173 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1179 * We're done with our transmission attempt, continue processing.
1181 * @param cls the `struct MessageQueue` of the message
1182 * @param receiver intended receiver
1183 * @param success whether it worked or not
1184 * @param size_payload bytes payload sent
1185 * @param physical bytes sent on wire
1188 transmit_send_continuation (void *cls,
1189 const struct GNUNET_PeerIdentity *receiver,
1190 int success, size_t size_payload, size_t physical)
1192 struct MessageQueue *mq = cls;
1193 struct NeighbourMapEntry *n;
1195 if (NULL == (n = lookup_neighbour (receiver)))
1198 return; /* disconnect or other error while transmitting, can happen */
1200 if (n->is_active == mq)
1202 /* this is still "our" neighbour, remove us from its queue
1203 and allow it to send the next message now */
1204 n->is_active = NULL;
1205 if (NULL != n->task)
1206 GNUNET_SCHEDULER_cancel (n->task);
1207 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1209 if (bytes_in_send_queue < mq->message_buf_size)
1211 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1212 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1213 bytes_in_send_queue, mq->message_buf_size,
1214 (GNUNET_OK == success) ? "OK" : "FAIL",
1215 size_payload, physical);
1220 GNUNET_break (size_payload == mq->message_buf_size);
1221 bytes_in_send_queue -= mq->message_buf_size;
1222 GNUNET_STATISTICS_set (GST_stats,
1224 ("# bytes in message queue for other peers"),
1225 bytes_in_send_queue, GNUNET_NO);
1226 if (GNUNET_OK == success)
1227 GNUNET_STATISTICS_update (GST_stats,
1229 ("# messages transmitted to other peers"),
1232 GNUNET_STATISTICS_update (GST_stats,
1234 ("# transmission failures for messages to other peers"),
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1238 GNUNET_i2s (receiver),
1239 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1240 mq->message_buf_size,
1241 (success == GNUNET_OK) ? "success" : "FAILURE");
1242 if (NULL != mq->cont)
1243 mq->cont (mq->cont_cls, success, size_payload, physical);
1249 * Check the message list for the given neighbour and if we can
1250 * send a message, do so. This function should only be called
1251 * if the connection is at least generally ready for transmission.
1252 * While we will only send one message at a time, no bandwidth
1253 * quota management is performed here. If a message was given to
1254 * the plugin, the continuation will automatically re-schedule
1255 * the 'master' task once the next message might be transmitted.
1257 * @param n target peer for which to transmit
1260 try_transmission_to_peer (struct NeighbourMapEntry *n)
1262 struct MessageQueue *mq;
1263 struct GNUNET_TIME_Relative timeout;
1265 if (NULL == n->primary_address.address)
1267 /* no address, why are we here? */
1271 if ((0 == n->primary_address.address->address_length) &&
1272 (NULL == n->primary_address.session))
1274 /* no address, why are we here? */
1278 if (NULL != n->is_active)
1280 /* transmission already pending */
1284 /* timeout messages from the queue that are past their due date */
1285 while (NULL != (mq = n->messages_head))
1287 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1288 if (timeout.rel_value_us > 0)
1290 GNUNET_STATISTICS_update (GST_stats,
1292 ("# messages timed out while in transport queue"),
1294 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1296 transmit_send_continuation (mq, &n->id,
1298 mq->message_buf_size, 0); /* timeout */
1301 return; /* no more messages */
1302 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1306 "Giving message with %u bytes to plugin session %p\n",
1307 mq->message_buf_size, n->primary_address.session);
1309 (void) send_with_session (n,
1310 mq->message_buf, mq->message_buf_size,
1311 0 /* priority */, timeout, GNUNET_NO,
1312 &transmit_send_continuation, mq);
1317 * Send keepalive message to the neighbour. Must only be called
1318 * if we are on 'connected' state or while trying to switch addresses.
1319 * Will internally determine if a keepalive is truly needed (so can
1320 * always be called).
1322 * @param n neighbour that went idle and needs a keepalive
1325 send_keepalive (struct NeighbourMapEntry *n)
1327 struct SessionKeepAliveMessage m;
1328 struct GNUNET_TIME_Relative timeout;
1331 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1332 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT));
1333 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1334 return; /* no keepalive needed at this time */
1336 nonce = 0; /* 0 indicates 'not set' */
1338 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1341 "Sending keep alive to peer `%s' with nonce %u\n",
1342 GNUNET_i2s (&n->id), nonce);
1344 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1345 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1346 m.nonce = htonl (nonce);
1348 timeout = send_with_session (n,
1349 (const void *) &m, sizeof (m),
1350 UINT32_MAX /* priority */,
1351 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1353 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1355 n->primary_address.keep_alive_nonce = nonce;
1356 n->expect_latency_response = GNUNET_YES;
1357 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1358 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1364 * Keep the connection to the given neighbour alive longer,
1365 * we received a KEEPALIVE (or equivalent); send a response.
1367 * @param neighbour neighbour to keep alive (by sending keep alive response)
1368 * @param m the keep alive message containing the nonce to respond to
1371 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1372 const struct GNUNET_MessageHeader *m)
1374 struct NeighbourMapEntry *n;
1375 const struct SessionKeepAliveMessage *msg_in;
1376 struct SessionKeepAliveMessage msg;
1378 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1381 msg_in = (struct SessionKeepAliveMessage *) m;
1382 if (NULL == (n = lookup_neighbour (neighbour)))
1384 GNUNET_STATISTICS_update (GST_stats,
1386 ("# KEEPALIVE messages discarded (peer unknown)"),
1390 if (NULL == n->primary_address.session)
1392 GNUNET_STATISTICS_update (GST_stats,
1394 ("# KEEPALIVE messages discarded (no session)"),
1399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1400 "Received keep alive request from peer `%s' with nonce %u\n",
1401 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1403 /* send reply to allow neighbour to measure latency */
1404 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1405 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1406 msg.nonce = msg_in->nonce;
1407 (void) send_with_session(n,
1408 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1409 UINT32_MAX /* priority */,
1410 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1416 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1417 * latency to this peer. Pass the updated information (existing ats
1418 * plus calculated latency) to ATS.
1420 * @param neighbour neighbour to keep alive
1421 * @param m the message containing the keep alive response
1424 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1425 const struct GNUNET_MessageHeader *m)
1427 struct NeighbourMapEntry *n;
1428 const struct SessionKeepAliveMessage *msg;
1429 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1431 struct GNUNET_ATS_Information ats;
1433 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1436 msg = (const struct SessionKeepAliveMessage *) m;
1437 if (NULL == (n = lookup_neighbour (neighbour)))
1439 GNUNET_STATISTICS_update (GST_stats,
1441 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1445 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1446 (GNUNET_YES != n->expect_latency_response) )
1448 GNUNET_STATISTICS_update (GST_stats,
1450 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1454 if (NULL == n->primary_address.address)
1456 GNUNET_STATISTICS_update (GST_stats,
1458 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1462 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1464 GNUNET_STATISTICS_update (GST_stats,
1466 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1472 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1473 "Received keep alive response from peer `%s' for session %p\n",
1474 GNUNET_i2s (&n->id), n->primary_address.session);
1478 /* Update session timeout here */
1479 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1482 "Updating session for peer `%s' for session %p\n",
1483 GNUNET_i2s (&n->id), n->primary_address.session);
1484 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1491 n->primary_address.keep_alive_nonce = 0;
1492 n->expect_latency_response = GNUNET_NO;
1493 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1494 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Latency for peer `%s' is %s\n",
1498 GNUNET_i2s (&n->id),
1499 GNUNET_STRINGS_relative_time_to_string (n->latency,
1501 /* append latency */
1502 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1503 if (n->latency.rel_value_us > UINT32_MAX)
1504 latency = UINT32_MAX;
1506 latency = n->latency.rel_value_us;
1507 ats.value = htonl (latency);
1508 GST_ats_update_metrics (&n->id, n->primary_address.address,
1509 n->primary_address.session, &ats, 1);
1514 * We have received a message from the given sender. How long should
1515 * we delay before receiving more? (Also used to keep the peer marked
1518 * @param sender sender of the message
1519 * @param size size of the message
1520 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1521 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1522 * #GNUNET_SYSERR if the connection is not fully up yet
1523 * @return how long to wait before reading more from this sender
1525 struct GNUNET_TIME_Relative
1526 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1527 *sender, ssize_t size, int *do_forward)
1529 struct NeighbourMapEntry *n;
1530 struct GNUNET_TIME_Relative ret;
1532 if (NULL == neighbours)
1534 *do_forward = GNUNET_NO;
1535 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1537 if (NULL == (n = lookup_neighbour (sender)))
1539 GST_neighbours_try_connect (sender);
1540 if (NULL == (n = lookup_neighbour (sender)))
1542 GNUNET_STATISTICS_update (GST_stats,
1544 ("# messages discarded due to lack of neighbour record"),
1546 *do_forward = GNUNET_NO;
1547 return GNUNET_TIME_UNIT_ZERO;
1550 if (! test_connected (n))
1552 *do_forward = GNUNET_SYSERR;
1553 return GNUNET_TIME_UNIT_ZERO;
1555 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1557 n->quota_violation_count++;
1558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1559 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1560 n->in_tracker.available_bytes_per_s__,
1561 n->quota_violation_count);
1562 /* Discount 32k per violation */
1563 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1567 if (n->quota_violation_count > 0)
1569 /* try to add 32k back */
1570 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1571 n->quota_violation_count--;
1574 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1576 GNUNET_STATISTICS_update (GST_stats,
1578 ("# bandwidth quota violations by other peers"),
1580 *do_forward = GNUNET_NO;
1581 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1583 *do_forward = GNUNET_YES;
1584 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1585 if (ret.rel_value_us > 0)
1587 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1588 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1589 (unsigned long long) n->in_tracker.
1590 consumption_since_last_update__,
1591 (unsigned int) n->in_tracker.available_bytes_per_s__,
1592 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1593 GNUNET_STATISTICS_update (GST_stats,
1594 gettext_noop ("# ms throttling suggested"),
1595 (int64_t) ret.rel_value_us / 1000LL,
1603 * Transmit a message to the given target using the active connection.
1605 * @param target destination
1606 * @param msg message to send
1607 * @param msg_size number of bytes in msg
1608 * @param timeout when to fail with timeout
1609 * @param cont function to call when done
1610 * @param cont_cls closure for 'cont'
1613 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1614 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1615 GST_NeighbourSendContinuation cont, void *cont_cls)
1617 struct NeighbourMapEntry *n;
1618 struct MessageQueue *mq;
1620 /* All ove these cases should never happen; they are all API violations.
1621 But we check anyway, just to be sure. */
1622 if (NULL == (n = lookup_neighbour (target)))
1626 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1629 if (GNUNET_YES != test_connected (n))
1633 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1636 bytes_in_send_queue += msg_size;
1637 GNUNET_STATISTICS_set (GST_stats,
1639 ("# bytes in message queue for other peers"),
1640 bytes_in_send_queue, GNUNET_NO);
1641 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1643 mq->cont_cls = cont_cls;
1644 memcpy (&mq[1], msg, msg_size);
1645 mq->message_buf = (const char *) &mq[1];
1646 mq->message_buf_size = msg_size;
1647 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueueing %u bytes to send to peer %s\n",
1650 msg_size, GNUNET_i2s (target));
1652 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1653 if (NULL != n->task)
1654 GNUNET_SCHEDULER_cancel (n->task);
1655 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1659 send_session_connect_cont (void *cls,
1660 const struct GNUNET_PeerIdentity *target,
1662 size_t size_payload,
1663 size_t size_on_wire)
1665 struct NeighbourMapEntry *n;
1667 n = lookup_neighbour (target);
1670 /* SYN continuation was called after neighbor was freed,
1671 * for example due to a time out for the state or the session
1672 * used was already terminated: nothing to do here... */
1676 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1677 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1678 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1680 /* SYN continuation was called after neighbor changed state,
1681 * for example due to a time out for the state or the session
1682 * used was already terminated: nothing to do here... */
1685 if (GNUNET_OK == result)
1688 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1689 _("Failed to send SYN message to peer `%s' using address `%s' session %p\n"),
1690 GNUNET_i2s (target),
1691 GST_plugins_a2s (n->primary_address.address),
1692 n->primary_address.session);
1695 case GNUNET_TRANSPORT_PS_SYN_SENT:
1696 /* Remove address and request and additional one */
1697 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1698 n->primary_address.session);
1699 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL );
1700 unset_primary_address (n);
1701 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1702 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1704 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1705 /* Remove address and request and additional one */
1706 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1707 n->primary_address.session);
1708 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL );
1709 unset_primary_address (n);
1710 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1711 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1713 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1714 /* Remove address and request and go back to primary address */
1715 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1716 ("# Failed attempts to switch addresses (failed to send SYN CONT)"), 1, GNUNET_NO);
1717 GNUNET_ATS_address_destroyed (GST_ats, n->alternative_address.address,
1718 n->alternative_address.session);
1719 GNUNET_ATS_address_destroyed (GST_ats, n->alternative_address.address,
1721 unset_alternative_address (n);
1722 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1723 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1726 disconnect_neighbour (n);
1732 * Send a SYN message via the given address.
1734 * @param na address to use
1737 send_syn (struct NeighbourAddress *na)
1739 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1740 struct TransportSynMessage connect_msg;
1741 struct NeighbourMapEntry *n;
1743 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1744 "Sending SYN message to peer `%s'\n",
1745 GNUNET_i2s (&na->address->peer));
1747 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1752 if (NULL == na->session)
1753 na->session = papi->get_session (papi->cls, na->address);
1754 if (NULL == na->session)
1759 GNUNET_STATISTICS_update (GST_stats,
1761 ("# SYN messages sent"),
1763 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1764 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1765 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1766 connect_msg.reserved = htonl (0);
1767 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1769 papi->send (papi->cls,
1771 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
1773 SETUP_CONNECTION_TIMEOUT,
1774 send_session_connect_cont, NULL))
1776 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1777 _("Failed to transmit SYN message via plugin to %s\n"),
1778 GST_plugins_a2s (na->address));
1780 n = lookup_neighbour (&na->address->peer);
1788 case GNUNET_TRANSPORT_PS_SYN_SENT:
1789 /* Remove address and request and additional one */
1790 unset_primary_address (n);
1791 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1792 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1793 /* Hard failure to send the SYN message with this address:
1794 Destroy address and session */
1796 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1797 /* Remove address and request and additional one */
1798 unset_primary_address (n);
1799 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1800 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1802 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1803 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1804 ("# Failed attempts to switch addresses (failed to send SYN)"), 1, GNUNET_NO);
1805 /* Remove address and request and additional one */
1806 unset_alternative_address (n);
1807 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1808 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1811 disconnect_neighbour (n);
1814 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1815 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1817 GST_neighbours_notify_data_sent (&na->address->peer,
1820 sizeof (struct TransportSynMessage));
1825 send_session_connect_ack_cont (void *cls,
1826 const struct GNUNET_PeerIdentity *target,
1828 size_t size_payload,
1829 size_t size_on_wire)
1831 struct NeighbourMapEntry *n;
1833 n = lookup_neighbour (target);
1836 /* SYN_ACK continuation was called after neighbor was freed,
1837 * for example due to a time out for the state or the session
1838 * used was already terminated: nothing to do here... */
1842 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1844 /* SYN_ACK continuation was called after neighbor changed state,
1845 * for example due to a time out for the state or the session
1846 * used was already terminated: nothing to do here... */
1849 if (GNUNET_OK == result)
1852 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1853 _("Failed to send SYN_ACK message to peer `%s' using address `%s' session %p\n"),
1854 GNUNET_i2s (target),
1855 GST_plugins_a2s (n->primary_address.address),
1856 n->primary_address.session);
1858 /* Failed to send SYN_ACK message with this address */
1859 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1860 n->primary_address.session);
1861 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1864 /* Remove address and request and additional one */
1865 unset_primary_address (n);
1866 n->ack_state = ACK_SEND_SYN_ACK;
1867 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1868 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1874 * Send a SYN_ACK message via the given address.
1876 * @param address address to use
1877 * @param session session to use
1878 * @param timestamp timestamp to use for the ACK message
1879 * @return GNUNET_SYSERR if sending immediately failed, GNUNET_OK otherwise
1882 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1883 struct Session *session,
1884 struct GNUNET_TIME_Absolute timestamp)
1886 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1887 struct TransportSynMessage connect_msg;
1888 struct NeighbourMapEntry *n;
1890 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1891 "Sending SYN_ACK to peer `%s'\n",
1892 GNUNET_i2s (&address->peer));
1894 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1899 if (NULL == session)
1900 session = papi->get_session (papi->cls, address);
1901 if (NULL == session)
1906 GNUNET_STATISTICS_update (GST_stats,
1908 ("# SYN_ACK messages sent"),
1910 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1911 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
1912 connect_msg.reserved = htonl (0);
1913 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1915 if (GNUNET_SYSERR == papi->send (papi->cls,
1917 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
1919 GNUNET_TIME_UNIT_FOREVER_REL,
1920 send_session_connect_ack_cont, NULL))
1922 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1923 _("Failed to transmit SYN_ACK message via plugin to %s\n"),
1924 GST_plugins_a2s (address));
1926 n = lookup_neighbour (&address->peer);
1932 /* Hard failure to send the SYN_ACK message with this address:
1933 Destroy session (and address) */
1934 if (GNUNET_YES == GNUNET_HELLO_address_check_option(address,
1935 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
1937 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1938 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1941 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1943 /* Remove address and request and additional one */
1944 unset_primary_address (n);
1945 n->ack_state = ACK_SEND_SYN_ACK;
1946 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1947 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1953 struct QuotaNotificationRequest
1955 struct GNUNET_PeerIdentity peer;
1956 struct Session *session;
1960 struct QNR_LookContext
1962 struct GNUNET_PeerIdentity peer;
1963 struct Session *session;
1966 struct QuotaNotificationRequest *res;
1970 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1972 struct QNR_LookContext *qnr_ctx = cls;
1973 struct QuotaNotificationRequest *qnr = value;
1975 if ((qnr->session == qnr_ctx->session) &&
1976 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1977 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1979 qnr_ctx->res = value;
1986 GST_neighbours_register_quota_notification(void *cls,
1987 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1988 struct Session *session)
1990 struct QuotaNotificationRequest *qnr;
1991 struct QNR_LookContext qnr_ctx;
1993 if (NULL == registered_quota_notifications)
1995 return; /* init or shutdown */
1998 qnr_ctx.peer = (*peer);
1999 qnr_ctx.plugin = plugin;
2000 qnr_ctx.session = session;
2003 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2004 peer, &find_notification_request, &qnr_ctx);
2005 if (NULL != qnr_ctx.res)
2011 qnr = GNUNET_new (struct QuotaNotificationRequest);
2012 qnr->peer = (*peer);
2013 qnr->plugin = GNUNET_strdup (plugin);
2014 qnr->session = session;
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2017 "Adding notification for peer `%s' plugin `%s' session %p \n",
2018 GNUNET_i2s (peer), plugin, session);
2020 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
2021 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2026 GST_neighbours_unregister_quota_notification(void *cls,
2027 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
2029 struct QNR_LookContext qnr_ctx;
2031 if (NULL == registered_quota_notifications)
2033 return; /* init or shutdown */
2036 qnr_ctx.peer = (*peer);
2037 qnr_ctx.plugin = plugin;
2038 qnr_ctx.session = session;
2041 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
2042 &find_notification_request, &qnr_ctx);
2043 if (NULL == qnr_ctx.res)
2049 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2050 "Removing notification for peer `%s' plugin `%s' session %p \n",
2051 GNUNET_i2s (peer), plugin, session);
2053 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
2055 GNUNET_free (qnr_ctx.res->plugin);
2056 GNUNET_free (qnr_ctx.res);
2060 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
2062 /* struct NeighbourMapEntry *n = cls; */
2063 struct QuotaNotificationRequest *qnr = value;
2064 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2065 struct GNUNET_TIME_Relative delay;
2068 papi = GST_plugins_find(qnr->plugin);
2075 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
2076 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2077 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
2078 delay.rel_value_us / 1000);
2080 if (NULL != papi->update_inbound_delay)
2081 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
2086 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
2089 /* struct NeighbourMapEntry *n = cls; */
2090 struct QuotaNotificationRequest *qnr = value;
2092 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
2094 GNUNET_free(qnr->plugin);
2101 inbound_bw_tracker_update(void *cls)
2103 struct NeighbourMapEntry *n = cls;
2105 /* Quota was updated, tell plugins to update the time to receive next */
2106 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2107 &n->id, ¬ification_cb, n);
2112 * Create a fresh entry in the neighbour map for the given peer
2114 * @param peer peer to create an entry for
2115 * @return new neighbour map entry
2117 static struct NeighbourMapEntry *
2118 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2120 struct NeighbourMapEntry *n;
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 "Creating new neighbour entry for `%s'\n",
2125 n = GNUNET_new (struct NeighbourMapEntry);
2127 n->ack_state = ACK_UNDEFINED;
2128 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2129 n->last_util_transmission = GNUNET_TIME_absolute_get();
2130 n->util_payload_bytes_recv = 0;
2131 n->util_payload_bytes_sent = 0;
2132 n->util_total_bytes_recv = 0;
2133 n->util_total_bytes_sent = 0;
2134 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
2135 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2136 MAX_BANDWIDTH_CARRY_S);
2137 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2138 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2139 GNUNET_assert (GNUNET_OK ==
2140 GNUNET_CONTAINER_multipeermap_put (neighbours,
2142 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2147 struct BlacklistCheckSwitchContext
2149 struct BlacklistCheckSwitchContext *prev;
2150 struct BlacklistCheckSwitchContext *next;
2153 struct GST_BlacklistCheck *blc;
2155 struct GNUNET_HELLO_Address *address;
2156 struct Session *session;
2158 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2159 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2164 * Black list check result for try_connect call
2165 * If connection to the peer is allowed request adddress and
2167 * @param cls blc_ctx bl context
2168 * @param peer the peer
2169 * @param result the result
2172 try_connect_bl_check_cont (void *cls,
2173 const struct GNUNET_PeerIdentity *peer,
2176 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2177 struct NeighbourMapEntry *n;
2179 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2182 GNUNET_free (blc_ctx);
2183 if (GNUNET_OK != result)
2185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2186 _("Blacklisting disapproved to connect to peer `%s'\n"),
2191 /* Setup a new neighbour */
2192 if (NULL != lookup_neighbour(peer))
2193 return; /* The neighbor was created in the meantime while waited for BL clients */
2195 n = setup_neighbour (peer);
2197 /* Request address suggestions for this peer */
2198 set_state_and_timeout (n,
2199 GNUNET_TRANSPORT_PS_INIT_ATS,
2200 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2201 GNUNET_ATS_reset_backoff (GST_ats, peer);
2202 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats,
2209 * Try to create a connection to the given target (eventually).
2211 * @param target peer to try to connect to
2214 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2216 struct NeighbourMapEntry *n;
2217 struct GST_BlacklistCheck *blc;
2218 struct BlacklistCheckSwitchContext *blc_ctx;
2220 if (NULL == neighbours)
2222 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2223 "Asked to connect to peer `%s' during shutdown\n",
2224 GNUNET_i2s (target));
2225 return; /* during shutdown, do nothing */
2227 n = lookup_neighbour (target);
2228 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2229 "Asked to connect to peer `%s' (state: %s)\n",
2230 GNUNET_i2s (target),
2231 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2236 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2237 /* this should not be possible */
2239 free_neighbour (n, GNUNET_NO);
2241 case GNUNET_TRANSPORT_PS_INIT_ATS:
2242 case GNUNET_TRANSPORT_PS_SYN_SENT:
2243 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2244 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2245 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2246 "Ignoring request to try to connect to `%s', already trying!\n",
2247 GNUNET_i2s (target));
2248 return; /* already trying */
2249 case GNUNET_TRANSPORT_PS_CONNECTED:
2250 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2251 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2252 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2253 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2254 "Ignoring request to try to connect, already connected to `%s'!\n",
2255 GNUNET_i2s (target));
2256 return; /* already connected */
2257 case GNUNET_TRANSPORT_PS_DISCONNECT:
2258 /* get rid of remains, ready to re-try immediately */
2259 free_neighbour (n, GNUNET_NO);
2261 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2262 /* should not be possible */
2266 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2267 "Unhandled state `%s'\n",
2268 GNUNET_TRANSPORT_ps2s (n->state));
2270 free_neighbour (n, GNUNET_NO);
2275 /* Do blacklist check if connecting to this peer is allowed */
2276 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2277 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2279 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2280 &try_connect_bl_check_cont, blc_ctx)))
2288 * We received a 'SYN' message from the other peer.
2289 * Consider switching to it.
2291 * @param message possibly a 'struct TransportSynMessage' (check format)
2292 * @param peer identity of the peer to switch the address for
2293 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2296 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2297 const struct GNUNET_PeerIdentity *peer)
2299 const struct TransportSynMessage *scm;
2300 struct NeighbourMapEntry *n;
2301 struct GNUNET_TIME_Absolute ts;
2303 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2305 GNUNET_break_op (0);
2306 return GNUNET_SYSERR;
2308 GNUNET_STATISTICS_update (GST_stats,
2310 ("# SYN messages received"),
2312 if (NULL == neighbours)
2314 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2315 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2317 return GNUNET_OK; /* we're shutting down */
2319 scm = (const struct TransportSynMessage *) message;
2320 GNUNET_break_op (0 == ntohl (scm->reserved));
2321 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2322 n = lookup_neighbour (peer);
2325 /* This is a new neighbour and set to not connected */
2326 n = setup_neighbour (peer);
2329 /* Remember this SYN message in neighbour */
2330 n->ack_state = ACK_SEND_SYN_ACK;
2331 n->connect_ack_timestamp = ts;
2333 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2334 "Received SYN for peer `%s' in state %s/%s\n",
2336 GNUNET_TRANSPORT_ps2s (n->state),
2337 print_ack_state (n->ack_state));
2341 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2342 /* Request an address from ATS to send SYN_ACK to this peer */
2343 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2344 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2345 if (NULL == n->suggest_handle)
2346 GNUNET_ATS_suggest_address (GST_ats, peer);
2348 case GNUNET_TRANSPORT_PS_INIT_ATS:
2349 /* SYN message takes priority over us asking ATS for address:
2350 * Wait for ATS to suggest an address and send SYN_ACK */
2351 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2352 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2354 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2355 /* We already wait for an address to send an SYN_ACK */
2357 case GNUNET_TRANSPORT_PS_SYN_SENT:
2358 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2359 /* Send ACK immediately */
2360 n->ack_state = ACK_SEND_ACK;
2361 send_connect_ack_message (n->primary_address.address,
2362 n->primary_address.session, ts);
2364 case GNUNET_TRANSPORT_PS_CONNECTED:
2365 /* we are already connected and can thus send the ACK immediately */
2366 GNUNET_assert (NULL != n->primary_address.address);
2367 GNUNET_assert (NULL != n->primary_address.session);
2368 n->ack_state = ACK_SEND_ACK;
2369 send_connect_ack_message (n->primary_address.address,
2370 n->primary_address.session, ts);
2372 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2373 /* We wait for ATS address suggestion */
2375 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2376 /* We received a SYN message while waiting for a SYN_ACK in fast
2377 * reconnect. Send SYN_ACK immediately */
2378 n->ack_state = ACK_SEND_ACK;
2379 send_connect_ack_message (n->primary_address.address,
2380 n->primary_address.session, n->connect_ack_timestamp);
2382 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2383 /* We are already connected and can thus send the ACK immediately;
2384 still, it can never hurt to have an alternative address, so also
2385 tell ATS about it */
2386 GNUNET_assert (NULL != n->primary_address.address);
2387 GNUNET_assert (NULL != n->primary_address.session);
2388 n->ack_state = ACK_SEND_ACK;
2389 send_connect_ack_message (n->primary_address.address,
2390 n->primary_address.session, ts);
2392 case GNUNET_TRANSPORT_PS_DISCONNECT:
2393 /* Get rid of remains without terminating sessions, ready to re-try */
2394 free_neighbour (n, GNUNET_YES);
2395 n = setup_neighbour (peer);
2396 /* Remember the SYN time stamp for ACK message */
2397 n->ack_state = ACK_SEND_SYN_ACK;
2398 n->connect_ack_timestamp = ts;
2399 /* Request an address for the peer */
2400 GNUNET_ATS_suggest_address (GST_ats, peer);
2401 GNUNET_ATS_reset_backoff (GST_ats, peer);
2402 set_state (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS);
2404 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2405 /* should not be possible */
2409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2410 "Unhandled state `%s'\n",
2411 GNUNET_TRANSPORT_ps2s (n->state));
2413 return GNUNET_SYSERR;
2420 * We've been asked to switch addresses, and just now
2421 * got the result from the blacklist check to see if this
2424 * @param cls the `struct BlacklistCheckSwitchContext` with
2425 * the information about the future address
2426 * @param peer the peer we may switch addresses on
2427 * @param result #GNUNET_NO if we are not allowed to use the new
2431 switch_address_bl_check_cont (void *cls,
2432 const struct GNUNET_PeerIdentity *peer,
2435 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2436 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2437 struct NeighbourMapEntry *n;
2439 papi = GST_plugins_find (blc_ctx->address->transport_name);
2441 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2447 "Peer %s is unknown, suggestion ignored\n",
2450 if (result == GNUNET_NO)
2452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2453 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2454 GST_plugins_a2s (blc_ctx->address),
2456 GNUNET_i2s (&blc_ctx->address->peer));
2460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2461 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2462 blc_ctx->address->transport_name,
2463 GST_plugins_a2s (blc_ctx->address),
2465 GNUNET_i2s (&blc_ctx->address->peer));
2468 /* This address is blacklisted, delete address and session (if existing) in ATS */
2469 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2471 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2472 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2474 /* This is an inbound address, destroy full address */
2475 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2478 /* Remove blacklist check and clean up */
2479 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2480 GNUNET_HELLO_address_free (blc_ctx->address);
2481 GNUNET_free (blc_ctx);
2485 if (NULL == blc_ctx->session)
2487 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489 "Obtained new session for peer `%s' and address '%s': %p\n",
2490 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2492 if (NULL == blc_ctx->session)
2494 /* No session could be obtained, remove blacklist check and clean up */
2495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2496 "Failed to obtain new session for peer `%s' and address '%s'\n",
2497 GNUNET_i2s (&blc_ctx->address->peer),
2498 GST_plugins_a2s (blc_ctx->address));
2499 /* Delete address in ATS */
2500 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2502 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2503 GNUNET_HELLO_address_free (blc_ctx->address);
2504 GNUNET_free (blc_ctx);
2508 if ( (NULL != n->primary_address.address) &&
2509 (0 == GNUNET_HELLO_address_cmp(blc_ctx->address, n->primary_address.address)) )
2511 if (blc_ctx->session == n->primary_address.session)
2513 /* This address is already primary, update only quotas */
2514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2515 "Updating quota for peer `%s' address `%s' session %p\n",
2516 GNUNET_i2s (&blc_ctx->address->peer),
2517 GST_plugins_a2s (blc_ctx->address),
2520 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2521 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2523 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2524 GNUNET_HELLO_address_free(blc_ctx->address);
2525 GNUNET_free (blc_ctx);
2530 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2531 "Peer `%s' switches to address `%s' session %p\n",
2532 GNUNET_i2s (&blc_ctx->address->peer),
2533 GST_plugins_a2s (blc_ctx->address),
2538 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2540 free_neighbour (n, GNUNET_NO);
2542 case GNUNET_TRANSPORT_PS_INIT_ATS:
2543 /* We requested an address and ATS suggests one:
2544 * set primary address and send SYN message*/
2545 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2546 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2547 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2549 /* Send pending SYN_ACK message */
2550 n->ack_state = ACK_SEND_ACK;
2551 send_connect_ack_message (n->primary_address.address,
2552 n->primary_address.session, n->connect_ack_timestamp);
2554 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_SENT,
2555 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2556 send_syn (&n->primary_address);
2558 case GNUNET_TRANSPORT_PS_SYN_SENT:
2559 /* ATS suggested a new address while waiting for an SYN_ACK:
2560 * Switch and send new SYN */
2561 /* ATS suggests a different address, switch again */
2562 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2563 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2564 if (ACK_SEND_SYN_ACK == n->ack_state)
2566 /* Send pending SYN_ACK message */
2567 n->ack_state = ACK_SEND_ACK;
2568 send_connect_ack_message (n->primary_address.address,
2569 n->primary_address.session, n->connect_ack_timestamp);
2571 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_SENT,
2572 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2573 send_syn (&n->primary_address);
2575 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2576 /* We requested an address and ATS suggests one:
2577 * set primary address and send SYN_ACK message*/
2578 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2579 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2580 /* Send an ACK message as a response to the SYN msg */
2581 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2582 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2583 send_connect_ack_message (n->primary_address.address,
2584 n->primary_address.session,
2585 n->connect_ack_timestamp);
2586 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2587 (ACK_UNDEFINED == n->ack_state) )
2588 n->ack_state = ACK_SEND_ACK;
2590 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2591 /* ATS asks us to switch while we were trying to connect; switch to new
2592 address and check blacklist again */
2593 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2595 n->ack_state = ACK_SEND_ACK;
2596 send_connect_ack_message (n->primary_address.address,
2597 n->primary_address.session, n->connect_ack_timestamp);
2599 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2600 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2601 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2602 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2604 case GNUNET_TRANSPORT_PS_CONNECTED:
2605 GNUNET_assert (NULL != n->primary_address.address);
2606 GNUNET_assert (NULL != n->primary_address.session);
2607 if (n->primary_address.session == blc_ctx->session)
2609 /* not an address change, just a quota change */
2610 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2611 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2614 /* ATS asks us to switch a life connection; see if we can get
2615 a SYN_ACK on it before we actually do this! */
2616 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2617 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2618 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2619 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2620 GNUNET_STATISTICS_update (GST_stats, gettext_noop
2621 ("# Attempts to switch addresses"), 1, GNUNET_NO);
2622 send_syn (&n->alternative_address);
2624 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2625 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2626 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2627 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2629 /* Send pending SYN_ACK message */
2630 n->ack_state = ACK_SEND_ACK;
2631 send_connect_ack_message (n->primary_address.address,
2632 n->primary_address.session, n->connect_ack_timestamp);
2634 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2635 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2636 send_syn (&n->primary_address);
2638 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2639 /* ATS asks us to switch while we were trying to reconnect; switch to new
2640 address and send SYN again */
2641 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2642 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2643 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2644 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2645 send_syn (&n->primary_address);
2647 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2648 if ( (0 == GNUNET_HELLO_address_cmp(n->primary_address.address,
2649 blc_ctx->address) && n->primary_address.session == blc_ctx->session) )
2651 /* ATS switches back to still-active session */
2652 free_address (&n->alternative_address);
2653 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2656 /* ATS asks us to switch a life connection, send */
2657 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2658 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2659 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2660 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2661 send_syn (&n->alternative_address);
2663 case GNUNET_TRANSPORT_PS_DISCONNECT:
2664 /* not going to switch addresses while disconnecting */
2666 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2670 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2671 "Unhandled state `%s'\n",
2672 GNUNET_TRANSPORT_ps2s (n->state));
2677 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2678 GNUNET_HELLO_address_free (blc_ctx->address);
2679 GNUNET_free (blc_ctx);
2684 * For the given peer, switch to this address.
2686 * Before accepting this addresses and actively using it, a blacklist check
2687 * is performed. If this blacklist check fails the address will be destroyed.
2689 * @param peer identity of the peer to switch the address for
2690 * @param address address of the other peer,
2691 * @param session session to use or NULL if transport should initiate a session
2692 * @param bandwidth_in inbound quota to be used when connection is up,
2693 * 0 to disconnect from peer
2694 * @param bandwidth_out outbound quota to be used when connection is up,
2695 * 0 to disconnect from peer
2698 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2699 const struct GNUNET_HELLO_Address *address,
2700 struct Session *session,
2701 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2702 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2704 struct NeighbourMapEntry *n;
2705 struct GST_BlacklistCheck *blc;
2706 struct BlacklistCheckSwitchContext *blc_ctx;
2708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2709 "ATS has decided on an address for peer %s\n",
2711 GNUNET_assert (NULL != address->transport_name);
2712 if (NULL == (n = lookup_neighbour (peer)))
2714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2715 "Peer %s is unknown, suggestion ignored\n",
2720 /* Check if plugin is available */
2721 if (NULL == (GST_plugins_find (address->transport_name)))
2723 /* we don't have the plugin for this address */
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2725 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2726 address->transport_name,
2728 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2731 if ((NULL == session) &&
2732 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2734 /* This is a inbound address and we do not have a session to use! */
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2736 "Inbound address without session `%s'! Destroying address...\n",
2737 GST_plugins_a2s (address));
2738 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2743 "ATS suggests %s address '%s' session %p for "
2744 "peer `%s' in state %s/%s \n",
2745 GNUNET_HELLO_address_check_option (address,
2746 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2747 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2748 GNUNET_TRANSPORT_ps2s (n->state), print_ack_state (n->ack_state));
2750 /* Perform blacklist check */
2751 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2752 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2753 blc_ctx->session = session;
2754 blc_ctx->bandwidth_in = bandwidth_in;
2755 blc_ctx->bandwidth_out = bandwidth_out;
2756 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2759 if (NULL != (blc = GST_blacklist_test_allowed (peer,
2760 address->transport_name,
2761 &switch_address_bl_check_cont,
2770 send_utilization_data (void *cls,
2771 const struct GNUNET_PeerIdentity *key,
2774 struct NeighbourMapEntry *n = value;
2775 struct GNUNET_ATS_Information atsi[4];
2777 uint32_t bps_pl_out;
2780 struct GNUNET_TIME_Relative delta;
2782 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2783 GNUNET_TIME_absolute_get ());
2787 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2788 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2790 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2791 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2793 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2798 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2799 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2801 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2802 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2806 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2810 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2811 atsi[0].value = htonl (bps_out);
2812 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2813 atsi[1].value = htonl (bps_in);
2815 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2816 atsi[2].value = htonl (bps_pl_out);
2817 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2818 atsi[3].value = htonl (bps_pl_in);
2820 GST_ats_update_metrics (key, n->primary_address.address,
2821 n->primary_address.session, atsi, 4);
2822 n->util_payload_bytes_recv = 0;
2823 n->util_payload_bytes_sent = 0;
2824 n->util_total_bytes_recv = 0;
2825 n->util_total_bytes_sent = 0;
2826 n->last_util_transmission = GNUNET_TIME_absolute_get();
2832 * Task transmitting utilization in a regular interval
2834 * @param cls the 'struct NeighbourMapEntry' for which we are running
2835 * @param tc scheduler context (unused)
2838 utilization_transmission (void *cls,
2839 const struct GNUNET_SCHEDULER_TaskContext *tc)
2841 util_transmission_tk = NULL;
2843 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2844 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2846 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2847 utilization_transmission, NULL);
2853 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2854 const struct GNUNET_HELLO_Address *address,
2855 struct Session *session,
2856 const struct GNUNET_MessageHeader *message)
2858 struct NeighbourMapEntry *n;
2860 n = lookup_neighbour (peer);
2863 n->util_total_bytes_recv += ntohs(message->size);
2868 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2869 const struct GNUNET_HELLO_Address *address,
2870 struct Session *session,
2871 const struct GNUNET_MessageHeader *message)
2873 struct NeighbourMapEntry *n;
2874 n = lookup_neighbour (peer);
2877 n->util_payload_bytes_recv += ntohs(message->size);
2882 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2883 const struct GNUNET_HELLO_Address *address,
2884 struct Session *session,
2887 struct NeighbourMapEntry *n;
2888 n = lookup_neighbour (peer);
2891 if (n->primary_address.session != session)
2893 n->util_total_bytes_sent += size;
2898 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2901 struct NeighbourMapEntry *n;
2902 n = lookup_neighbour (peer);
2905 n->util_payload_bytes_sent += size;
2910 * Master task run for every neighbour. Performs all of the time-related
2911 * activities (keep alive, send next message, disconnect if idle, finish
2912 * clean up after disconnect).
2914 * @param cls the 'struct NeighbourMapEntry' for which we are running
2915 * @param tc scheduler context (unused)
2918 master_task (void *cls,
2919 const struct GNUNET_SCHEDULER_TaskContext *tc)
2921 struct NeighbourMapEntry *n = cls;
2922 struct GNUNET_TIME_Relative delay;
2925 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2927 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2928 GNUNET_i2s (&n->id),
2929 GNUNET_TRANSPORT_ps2s(n->state),
2930 GNUNET_STRINGS_relative_time_to_string (delay,
2934 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2935 /* invalid state for master task, clean up */
2937 free_neighbour (n, GNUNET_NO);
2939 case GNUNET_TRANSPORT_PS_INIT_ATS:
2940 if (0 == delay.rel_value_us)
2942 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2943 "Connection to `%s' timed out waiting for ATS to provide address\n",
2944 GNUNET_i2s (&n->id));
2945 free_neighbour (n, GNUNET_NO);
2949 case GNUNET_TRANSPORT_PS_SYN_SENT:
2950 if (0 == delay.rel_value_us)
2952 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2953 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
2954 GNUNET_i2s (&n->id));
2955 /* We could not send to this address, delete address and session */
2956 if (NULL != n->primary_address.session)
2957 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
2958 n->primary_address.session);
2959 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
2961 /* Remove address and request and additional one */
2962 unset_primary_address (n);
2963 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2964 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2968 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2969 if (0 == delay.rel_value_us)
2971 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2972 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
2973 GNUNET_i2s (&n->id));
2974 free_neighbour (n, GNUNET_NO);
2978 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2979 if (0 == delay.rel_value_us)
2981 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2982 "Connection to `%s' timed out waiting for other peer to send ACK\n",
2983 GNUNET_i2s (&n->id));
2984 disconnect_neighbour (n);
2988 case GNUNET_TRANSPORT_PS_CONNECTED:
2989 if (0 == delay.rel_value_us)
2991 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2992 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2993 GNUNET_i2s (&n->id));
2994 disconnect_neighbour (n);
2997 try_transmission_to_peer (n);
3000 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3001 if (0 == delay.rel_value_us)
3003 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3004 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3005 GNUNET_i2s (&n->id));
3006 disconnect_neighbour (n);
3010 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3011 if (0 == delay.rel_value_us)
3013 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3014 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
3015 GNUNET_i2s (&n->id));
3016 disconnect_neighbour (n);
3020 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3021 if (0 == delay.rel_value_us)
3023 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3024 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to SYN on alternative address)\n",
3025 GNUNET_i2s (&n->id));
3026 GNUNET_STATISTICS_update (GST_stats, gettext_noop
3027 ("# Failed attempts to switch addresses (no response)"), 1, GNUNET_NO);
3028 disconnect_neighbour (n);
3031 try_transmission_to_peer (n);
3034 case GNUNET_TRANSPORT_PS_DISCONNECT:
3035 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3036 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3037 GNUNET_i2s (&n->id));
3038 free_neighbour (n, GNUNET_NO);
3040 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3041 /* how did we get here!? */
3045 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3046 "Unhandled state `%s'\n",
3047 GNUNET_TRANSPORT_ps2s (n->state));
3051 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3052 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3054 /* if we are *now* in one of the two states, we're sending
3055 keep alive messages, so we need to consider the keepalive
3056 delay, not just the connection timeout */
3057 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3060 if (NULL == n->task)
3061 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3068 * Send a ACK message to the neighbour to confirm that we
3071 * @param n neighbour to send the ACK to
3074 send_session_ack_message (struct NeighbourMapEntry *n)
3076 struct GNUNET_MessageHeader msg;
3078 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sending ACK message to peer `%s'\n",
3079 GNUNET_i2s (&n->id));
3081 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3082 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3083 (void) send_with_session(n,
3084 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3085 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3091 * We received a 'SESSION_SYN_ACK' message from the other peer.
3092 * Consider switching to it.
3094 * @param message possibly a 'struct SessionConnectMessage' (check format)
3095 * @param peer identity of the peer to switch the address for
3096 * @param address address of the other peer, NULL if other peer
3098 * @param session session to use (or NULL)
3099 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3102 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3103 const struct GNUNET_PeerIdentity *peer,
3104 const struct GNUNET_HELLO_Address *address,
3105 struct Session *session)
3107 const struct TransportSynMessage *scm;
3108 struct GNUNET_TIME_Absolute ts;
3109 struct NeighbourMapEntry *n;
3111 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3112 "Received SYN_ACK message from peer `%s'\n",
3115 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3117 GNUNET_break_op (0);
3118 return GNUNET_SYSERR;
3120 GNUNET_STATISTICS_update (GST_stats,
3122 ("# SYN_ACK messages received"),
3124 scm = (const struct TransportSynMessage *) message;
3125 GNUNET_break_op (ntohl (scm->reserved) == 0);
3126 if (NULL == (n = lookup_neighbour (peer)))
3128 GNUNET_STATISTICS_update (GST_stats,
3130 ("# unexpected SYN_ACK messages (no peer)"),
3132 return GNUNET_SYSERR;
3134 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3137 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3139 free_neighbour (n, GNUNET_NO);
3140 return GNUNET_SYSERR;
3141 case GNUNET_TRANSPORT_PS_INIT_ATS:
3142 GNUNET_STATISTICS_update (GST_stats,
3144 ("# unexpected SYN_ACK messages (not ready)"),
3147 case GNUNET_TRANSPORT_PS_SYN_SENT:
3148 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3150 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3151 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3154 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3155 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3156 GNUNET_STATISTICS_set (GST_stats,
3157 gettext_noop ("# peers connected"),
3158 ++neighbours_connected,
3160 connect_notify_cb (callback_cls, &n->id,
3161 n->primary_address.bandwidth_in,
3162 n->primary_address.bandwidth_out);
3163 /* Tell ATS that the outbound session we created to send SYN was successful */
3164 // FIXME: shouldn't ATS already know about *outbound* sessions
3166 GST_ats_add_address (n->primary_address.address,
3167 n->primary_address.session,
3169 set_primary_address (n,
3170 n->primary_address.address,
3171 n->primary_address.session,
3172 n->primary_address.bandwidth_in,
3173 n->primary_address.bandwidth_out,
3175 send_session_ack_message (n);
3177 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3178 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3179 GNUNET_STATISTICS_update (GST_stats,
3181 ("# unexpected SYN_ACK messages (not ready)"),
3184 case GNUNET_TRANSPORT_PS_CONNECTED:
3185 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3186 send_session_ack_message (n);
3188 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3189 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3190 to give us a new address... */
3191 GNUNET_STATISTICS_update (GST_stats,
3193 ("# unexpected SYN_ACK messages (waiting on ATS)"),
3196 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3197 /* Reconnecting with new address address worked; go back to connected! */
3198 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3199 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3200 send_session_ack_message (n);
3202 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3203 /* new address worked; adopt it and go back to connected! */
3204 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3205 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3206 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3208 /* Notify about session... perhaps we obtained it */
3209 // FIXME: why is this needed?
3210 GST_ats_add_address (n->alternative_address.address,
3211 n->alternative_address.session, NULL, 0);
3212 /* Set primary addresses */
3213 set_primary_address (n, n->alternative_address.address,
3214 n->alternative_address.session, n->alternative_address.bandwidth_in,
3215 n->alternative_address.bandwidth_out, GNUNET_YES);
3216 GNUNET_STATISTICS_update (GST_stats, gettext_noop
3217 ("# Successful attempts to switch addresses"), 1, GNUNET_NO);
3219 free_address (&n->alternative_address);
3220 send_session_ack_message (n);
3222 case GNUNET_TRANSPORT_PS_DISCONNECT:
3223 GNUNET_STATISTICS_update (GST_stats,
3225 ("# unexpected SYN_ACK messages (disconnecting)"),
3227 return GNUNET_SYSERR;
3228 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3232 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3233 "Unhandled state `%s'\n",
3234 GNUNET_TRANSPORT_ps2s (n->state));
3236 return GNUNET_SYSERR;
3243 * A session was terminated. Take note; if needed, try to get
3244 * an alternative address from ATS.
3246 * @param peer identity of the peer where the session died
3247 * @param session session that is gone
3248 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3249 * this session was not in use
3252 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3253 struct Session *session)
3255 struct NeighbourMapEntry *n;
3256 struct BlackListCheckContext *bcc;
3257 struct BlackListCheckContext *bcc_next;
3259 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3261 while (NULL != (bcc = bcc_next))
3263 bcc_next = bcc->next;
3264 if (bcc->na.session == session)
3266 if (NULL != bcc->bc)
3267 GST_blacklist_test_cancel (bcc->bc);
3268 GNUNET_HELLO_address_free (bcc->na.address);
3269 GNUNET_CONTAINER_DLL_remove (bc_head,
3275 if (NULL == (n = lookup_neighbour (peer)))
3276 return GNUNET_NO; /* can't affect us */
3277 if (session != n->primary_address.session)
3279 /* Free alternative address */
3280 if (session == n->alternative_address.session)
3282 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) )
3283 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3284 free_address (&n->alternative_address);
3286 return GNUNET_NO; /* doesn't affect us further */
3289 n->expect_latency_response = GNUNET_NO;
3290 /* The session for neighbour's primary address died */
3293 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3295 free_neighbour (n, GNUNET_NO);
3297 case GNUNET_TRANSPORT_PS_INIT_ATS:
3299 free_neighbour (n, GNUNET_NO);
3301 case GNUNET_TRANSPORT_PS_SYN_SENT:
3302 /* The session used to send the SYN terminated:
3303 * this implies a connect error*/
3304 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3305 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3307 GST_plugins_a2s (n->primary_address.address),
3308 n->primary_address.session,
3311 /* Destroy the address since it cannot be used */
3312 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3313 unset_primary_address (n);
3314 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3315 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3317 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3318 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3319 /* error on inbound session; free neighbour entirely */
3320 free_address (&n->primary_address);
3321 free_neighbour (n, GNUNET_NO);
3323 case GNUNET_TRANSPORT_PS_CONNECTED:
3324 /* Our primary connection died, try a fast reconnect */
3325 unset_primary_address (n);
3326 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3327 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3329 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3330 /* we don't have an address, how can it go down? */
3333 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3334 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3335 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3337 GST_plugins_a2s (n->primary_address.address),
3338 n->primary_address.session,
3341 /* Destroy the address since it cannot be used */
3342 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3343 unset_primary_address (n);
3344 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3345 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3347 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3348 /* primary went down while we were waiting for SYN_ACK on secondary;
3349 secondary as primary */
3351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3352 "Connection `%s' %p to peer `%s' was terminated while switching, "
3353 "switching to alternative address `%s' %p\n",
3354 GST_plugins_a2s (n->primary_address.address),
3355 n->primary_address.session,
3357 GST_plugins_a2s (n->alternative_address.address),
3358 n->alternative_address.session);
3360 /* Destroy the inbound address since it cannot be used */
3362 == GNUNET_HELLO_address_check_option (n->primary_address.address,
3363 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
3364 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3365 free_address (&n->primary_address);
3368 n->primary_address = n->alternative_address;
3369 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3370 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3371 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3373 case GNUNET_TRANSPORT_PS_DISCONNECT:
3374 free_address (&n->primary_address);
3376 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3377 /* neighbour was freed and plugins told to terminate session */
3381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3382 "Unhandled state `%s'\n",
3383 GNUNET_TRANSPORT_ps2s (n->state));
3387 if (NULL != n->task)
3388 GNUNET_SCHEDULER_cancel (n->task);
3389 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3395 * We received a 'ACK' message from the other peer.
3396 * If we sent a 'SYN_ACK' last, this means we are now
3397 * connected. Otherwise, do nothing.
3399 * @param message possibly a 'struct SessionConnectMessage' (check format)
3400 * @param peer identity of the peer to switch the address for
3401 * @param address address of the other peer, NULL if other peer
3403 * @param session session to use (or NULL)
3404 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3407 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3408 const struct GNUNET_PeerIdentity *peer,
3409 const struct GNUNET_HELLO_Address *address,
3410 struct Session *session)
3412 struct NeighbourMapEntry *n;
3414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3415 "Received ACK message from peer `%s'\n",
3417 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3419 GNUNET_break_op (0);
3420 return GNUNET_SYSERR;
3422 GNUNET_STATISTICS_update (GST_stats,
3424 ("# ACK messages received"),
3426 if (NULL == (n = lookup_neighbour (peer)))
3428 GNUNET_break_op (0);
3429 return GNUNET_SYSERR;
3432 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3433 "Received %s for peer `%s' in state %s/%s\n",
3436 GNUNET_TRANSPORT_ps2s (n->state),
3437 print_ack_state (n->ack_state));
3439 /* Check if we are in a plausible state for having sent
3440 a SYN_ACK. If not, return, otherwise break.
3442 The remote peers sends a ACK as a response for a SYN_ACK
3446 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3447 now wait for the ACK to finally be connected
3448 - If we sent a SYN_ACK to this peer before */
3450 if ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3451 (ACK_SEND_ACK != n->ack_state))
3453 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3454 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3456 GNUNET_TRANSPORT_ps2s (n->state),
3457 print_ack_state (n->ack_state));
3459 GNUNET_STATISTICS_update (GST_stats,
3460 gettext_noop ("# unexpected ACK messages"), 1,
3465 /* We are connected */
3466 if (GNUNET_NO == GST_neighbours_test_connected(&n->id))
3468 /* Notify about connection */
3469 connect_notify_cb (callback_cls, &n->id,
3470 n->primary_address.bandwidth_in,
3471 n->primary_address.bandwidth_out);\
3473 GNUNET_STATISTICS_set (GST_stats,
3474 gettext_noop ("# peers connected"),
3475 ++neighbours_connected,
3479 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3481 /* We tried to switch addresses while being connect. We explicitly wait
3482 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3483 * so we do not want to set the address as in use! */
3487 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3488 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3490 /* Add session to ATS since no session was given (NULL) and we may have
3491 * obtained a new session */
3492 // FIXME: likely not the best place to do this...
3493 GST_ats_add_address (n->primary_address.address,
3494 n->primary_address.session,
3497 /* Set primary address to used */
3498 set_primary_address (n,
3499 n->primary_address.address,
3500 n->primary_address.session,
3501 n->primary_address.bandwidth_in,
3502 n->primary_address.bandwidth_out,
3509 * Test if we're connected to the given peer.
3511 * @param target peer to test
3512 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3515 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3517 return test_connected (lookup_neighbour (target));
3522 * Change the incoming quota for the given peer.
3524 * @param neighbour identity of peer to change qutoa for
3525 * @param quota new quota
3528 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3529 struct GNUNET_BANDWIDTH_Value32NBO quota)
3531 struct NeighbourMapEntry *n;
3533 if (NULL == (n = lookup_neighbour (neighbour)))
3535 GNUNET_STATISTICS_update (GST_stats,
3537 ("# SET QUOTA messages ignored (no such peer)"),
3541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3542 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3543 ntohl (quota.value__), GNUNET_i2s (&n->id));
3544 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3545 if (0 != ntohl (quota.value__))
3547 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3548 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3549 GNUNET_i2s (&n->id));
3550 if (GNUNET_YES == test_connected (n))
3551 GNUNET_STATISTICS_update (GST_stats,
3552 gettext_noop ("# disconnects due to quota of 0"),
3554 disconnect_neighbour (n);
3557 void delayed_disconnect (void *cls,
3558 const struct GNUNET_SCHEDULER_TaskContext* tc)
3560 struct NeighbourMapEntry *n = cls;
3562 n->delayed_disconnect_task = NULL;
3563 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3564 "Disconnecting by request from peer %s\n",
3565 GNUNET_i2s (&n->id));
3567 if (NULL != n->primary_address.address)
3568 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
3569 n->primary_address.session);
3571 free_neighbour (n, GNUNET_NO);
3576 * We received a disconnect message from the given peer,
3577 * validate and process.
3579 * @param peer sender of the message
3580 * @param msg the disconnect message
3583 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3584 const struct GNUNET_MessageHeader *msg)
3586 struct NeighbourMapEntry *n;
3587 const struct SessionDisconnectMessage *sdm;
3589 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3590 "Received DISCONNECT message from peer `%s'\n",
3592 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3594 GNUNET_break_op (0);
3595 GNUNET_STATISTICS_update (GST_stats,
3597 ("# disconnect messages ignored (malformed)"), 1,
3601 GNUNET_STATISTICS_update (GST_stats,
3603 ("# DISCONNECT messages received"),
3605 sdm = (const struct SessionDisconnectMessage *) msg;
3606 if (NULL == (n = lookup_neighbour (peer)))
3607 return; /* gone already */
3608 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3610 GNUNET_STATISTICS_update (GST_stats,
3612 ("# disconnect messages ignored (timestamp)"), 1,
3616 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3618 GNUNET_break_op (0);
3621 if (ntohl (sdm->purpose.size) !=
3622 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3623 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3624 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3626 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3627 "%s message from peer `%s' has invalid size \n",
3630 GNUNET_break_op (0);
3634 GNUNET_CRYPTO_eddsa_verify
3635 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3636 &sdm->signature, &sdm->public_key))
3638 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3639 "%s message from peer `%s' cannot be verified \n",
3642 GNUNET_break_op (0);
3645 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect, n);
3650 * Closure for the neighbours_iterate function.
3652 struct IteratorContext
3655 * Function to call on each connected neighbour.
3657 GST_NeighbourIterator cb;
3667 * Call the callback from the closure for each neighbour.
3669 * @param cls the `struct IteratorContext`
3670 * @param key the hash of the public key of the neighbour
3671 * @param value the `struct NeighbourMapEntry`
3672 * @return #GNUNET_OK (continue to iterate)
3675 neighbours_iterate (void *cls,
3676 const struct GNUNET_PeerIdentity *key,
3679 struct IteratorContext *ic = cls;
3680 struct NeighbourMapEntry *n = value;
3681 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3682 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3685 if (NULL != n->primary_address.address)
3687 bandwidth_in = n->primary_address.bandwidth_in;
3688 bandwidth_out = n->primary_address.bandwidth_out;
3692 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3693 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3697 n->primary_address.address,
3700 bandwidth_in, bandwidth_out);
3706 * Iterate over all connected neighbours.
3708 * @param cb function to call
3709 * @param cb_cls closure for cb
3712 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3714 struct IteratorContext ic;
3716 if (NULL == neighbours)
3717 return; /* can happen during shutdown */
3720 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3725 * If we have an active connection to the given target, it must be shutdown.
3727 * @param target peer to disconnect from
3730 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3732 struct NeighbourMapEntry *n;
3734 if (NULL == (n = lookup_neighbour (target)))
3735 return; /* not active */
3736 if (GNUNET_YES == test_connected (n))
3737 GNUNET_STATISTICS_update (GST_stats,
3739 ("# disconnected from peer upon explicit request"), 1,
3741 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3742 "Forced disconnect from peer %s\n",
3743 GNUNET_i2s (target));
3744 disconnect_neighbour (n);
3749 * Obtain current latency information for the given neighbour.
3751 * @param peer to get the latency for
3752 * @return observed latency of the address, FOREVER if the
3753 * the connection is not up
3755 struct GNUNET_TIME_Relative
3756 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3758 struct NeighbourMapEntry *n;
3760 n = lookup_neighbour (peer);
3762 return GNUNET_TIME_UNIT_FOREVER_REL;
3765 case GNUNET_TRANSPORT_PS_CONNECTED:
3766 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3767 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3768 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3770 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3771 case GNUNET_TRANSPORT_PS_INIT_ATS:
3772 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3773 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3774 case GNUNET_TRANSPORT_PS_SYN_SENT:
3775 case GNUNET_TRANSPORT_PS_DISCONNECT:
3776 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3777 return GNUNET_TIME_UNIT_FOREVER_REL;
3779 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3780 "Unhandled state `%s'\n",
3781 GNUNET_TRANSPORT_ps2s (n->state));
3785 return GNUNET_TIME_UNIT_FOREVER_REL;
3790 * Obtain current address information for the given neighbour.
3793 * @return address currently used
3795 struct GNUNET_HELLO_Address *
3796 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3798 struct NeighbourMapEntry *n;
3800 n = lookup_neighbour (peer);
3803 return n->primary_address.address;
3808 * Initialize the neighbours subsystem.
3810 * @param cls closure for callbacks
3811 * @param connect_cb function to call if we connect to a peer
3812 * @param disconnect_cb function to call if we disconnect from a peer
3813 * @param peer_address_cb function to call if we change an active address
3815 * @param max_fds maximum number of fds to use
3818 GST_neighbours_start (void *cls,
3819 NotifyConnect connect_cb,
3820 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3821 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3822 unsigned int max_fds)
3825 connect_notify_cb = connect_cb;
3826 disconnect_notify_cb = disconnect_cb;
3827 neighbour_change_cb = peer_address_cb;
3828 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3829 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3830 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3831 utilization_transmission, NULL);
3836 * Disconnect from the given neighbour.
3839 * @param key hash of neighbour's public key (not used)
3840 * @param value the 'struct NeighbourMapEntry' of the neighbour
3841 * @return #GNUNET_OK (continue to iterate)
3844 disconnect_all_neighbours (void *cls,
3845 const struct GNUNET_PeerIdentity *key,
3848 struct NeighbourMapEntry *n = value;
3850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3851 "Disconnecting peer `%4s', %s\n",
3852 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3853 free_neighbour (n, GNUNET_NO);
3859 * Cleanup the neighbours subsystem.
3862 GST_neighbours_stop ()
3864 struct BlacklistCheckSwitchContext *cur;
3865 struct BlacklistCheckSwitchContext *next;
3867 if (NULL == neighbours)
3869 if (NULL != util_transmission_tk)
3871 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3872 util_transmission_tk = NULL;
3875 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &disconnect_all_neighbours,
3877 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3879 next = pending_bc_head;
3880 for (cur = next; NULL != cur; cur = next )
3883 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3885 if (NULL != cur->blc)
3887 GST_blacklist_test_cancel (cur->blc);
3890 if (NULL != cur->address)
3891 GNUNET_HELLO_address_free (cur->address);
3895 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
3896 &free_notification_cb, NULL);
3897 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
3898 registered_quota_notifications = NULL;
3901 callback_cls = NULL;
3902 connect_notify_cb = NULL;
3903 disconnect_notify_cb = NULL;
3904 neighbour_change_cb = NULL;
3908 /* end of file gnunet-service-transport_neighbours.c */