2 This file is part of GNUnet.
3 Copyright (C) 2010-2015 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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, 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_ats.h"
29 #include "gnunet-service-transport_blacklist.h"
30 #include "gnunet-service-transport_clients.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport_manipulation.h"
33 #include "gnunet-service-transport_plugins.h"
34 #include "gnunet-service-transport_validation.h"
35 #include "gnunet-service-transport.h"
36 #include "gnunet_peerinfo_service.h"
37 #include "gnunet_constants.h"
38 #include "transport.h"
41 * Experimental option to ignore SessionQuotaMessages from
44 #define IGNORE_INBOUND_QUOTA GNUNET_YES
47 * Size of the neighbour hash map.
49 #define NEIGHBOUR_TABLE_SIZE 256
52 * Time we give plugin to transmit DISCONNECT message before the
53 * neighbour entry self-destructs.
55 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
58 * How often must a peer violate bandwidth quotas before we start
59 * to simply drop its messages?
61 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
64 * How long are we willing to wait for a response from ATS before timing out?
66 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
69 * How long are we willing to wait for an ACK from the other peer before
70 * giving up on our connect operation?
72 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
75 * How long are we willing to wait for a successful reconnect if
76 * an existing connection went down? Much shorter than the
77 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
78 * higher layers about the disconnect during this period.
80 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_UNIT_SECONDS
83 * Interval to send utilization data
85 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_UNIT_SECONDS
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 GNUNET_ATS_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;
167 * Message a peer sends to another when connected to indicate that
168 * the other peer should limit transmissions to the indicated
171 struct GNUNET_ATS_SessionQuotaMessage
174 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA.
176 struct GNUNET_MessageHeader header;
179 * Quota to use (for sending), in bytes per second.
181 uint32_t quota GNUNET_PACKED;
186 * Message we send to the other peer to notify him that we intentionally
187 * are disconnecting (to reduce timeouts). This is just a friendly
188 * notification, peers must not rely on always receiving disconnect
191 struct GNUNET_ATS_SessionDisconnectMessage
194 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
196 struct GNUNET_MessageHeader header;
201 uint32_t reserved GNUNET_PACKED;
204 * Purpose of the signature. Extends over the timestamp.
205 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
207 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
210 * Absolute time at the sender. Only the most recent connect
211 * message implies which session is preferred by the sender.
213 struct GNUNET_TIME_AbsoluteNBO timestamp;
216 * Public key of the sender.
218 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
221 * Signature of the peer that sends us the disconnect. Only
222 * valid if the timestamp is AFTER the timestamp from the
223 * corresponding 'SYN' message.
225 struct GNUNET_CRYPTO_EddsaSignature signature;
229 GNUNET_NETWORK_STRUCT_END
233 * For each neighbour we keep a list of messages
234 * that we still want to transmit to the neighbour.
240 * This is a doubly linked list.
242 struct MessageQueue *next;
245 * This is a doubly linked list.
247 struct MessageQueue *prev;
250 * Function to call once we're done.
252 GST_NeighbourSendContinuation cont;
255 * Closure for @e cont
260 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
261 * stuck together in memory. Allocated at the end of this struct.
263 const char *message_buf;
266 * Size of the message buf
268 size_t message_buf_size;
271 * At what time should we fail?
273 struct GNUNET_TIME_Absolute timeout;
279 * A possible address we could use to communicate with a neighbour.
281 struct NeighbourAddress
285 * Active session for this address.
287 struct GNUNET_ATS_Session *session;
290 * Network-level address information.
292 struct GNUNET_HELLO_Address *address;
295 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
296 * peer for this address. Use to check that the ACK is in response
297 * to our most recent 'SYN'.
299 struct GNUNET_TIME_Absolute connect_timestamp;
302 * Inbound bandwidth from ATS for this address.
304 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
307 * Outbound bandwidth from ATS for this address.
309 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
312 * Did we tell ATS that this is our 'active' address?
317 * The current nonce sent in the last keep alive messages
319 uint32_t keep_alive_nonce;
324 * Entry in neighbours.
326 struct NeighbourMapEntry
330 * Head of list of messages we would like to send to this peer;
331 * must contain at most one message per client.
333 struct MessageQueue *messages_head;
336 * Tail of list of messages we would like to send to this peer; must
337 * contain at most one message per client.
339 struct MessageQueue *messages_tail;
342 * Are we currently trying to send a message? If so, which one?
344 struct MessageQueue *is_active;
347 * Primary address we currently use to communicate with the neighbour.
349 struct NeighbourAddress primary_address;
352 * Alternative address currently under consideration for communicating
353 * with the neighbour.
355 struct NeighbourAddress alternative_address;
358 * Identity of this neighbour.
360 struct GNUNET_PeerIdentity id;
363 * Main task that drives this peer (timeouts, keepalives, etc.).
364 * Always runs the #master_task().
366 struct GNUNET_SCHEDULER_Task *task;
369 * Task to disconnect neighbour after we received a DISCONNECT message
371 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
374 * At what time should we sent the next keep-alive message?
376 struct GNUNET_TIME_Absolute keep_alive_time;
379 * At what time did we sent the last keep-alive message? Used
380 * to calculate round-trip time ("latency").
382 struct GNUNET_TIME_Absolute last_keep_alive_time;
385 * Timestamp we should include in our next SYN_ACK message.
386 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
387 * our SYN_ACK message.
389 struct GNUNET_TIME_Absolute connect_ack_timestamp;
392 * ATS address suggest handle
394 struct GNUNET_ATS_ConnectivitySuggestHandle *suggest_handle;
397 * Time where we should cut the connection (timeout) if we don't
398 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
399 * if we are in #GNUNET_TRANSPORT_PS_CONNECTED).
401 struct GNUNET_TIME_Absolute timeout;
404 * Tracker for inbound bandwidth.
406 struct GNUNET_BANDWIDTH_Tracker in_tracker;
409 * How often has the other peer (recently) violated the inbound
410 * traffic limit? Incremented by 10 per violation, decremented by 1
411 * per non-violation (for each time interval).
413 unsigned int quota_violation_count;
416 * Latest quota the other peer send us in bytes per second.
417 * We should not send more, least the other peer throttle
418 * receiving our traffic.
420 struct GNUNET_BANDWIDTH_Value32NBO neighbour_receive_quota;
423 * The current state of the peer.
425 enum GNUNET_TRANSPORT_PeerState state;
428 * Did we sent an KEEP_ALIVE message and are we expecting a response?
430 int expect_latency_response;
433 * When a peer wants to connect we have to reply to the 1st SYN message
434 * with a SYN_ACK message. But sometime we cannot send this message
435 * immediately since we do not have an address and then we have to remember
436 * to send this message as soon as we have an address.
438 * Flag to set if we still need to send a SYN_ACK message to the other peer
439 * (once we have an address to use and the peer has been allowed by our
440 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
441 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
442 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
443 * 'ACK' (regardless of what our own state machine might say).
445 enum GST_ACK_State ack_state;
448 * Tracking utilization of outbound bandwidth
450 uint32_t util_total_bytes_sent;
453 * Tracking utilization of inbound bandwidth
455 uint32_t util_total_bytes_recv;
458 * Date of last utilization transmission
460 struct GNUNET_TIME_Absolute last_util_transmission;
465 * Context for blacklist checks and the #try_connect_bl_check_cont()
466 * function. Stores information about ongoing blacklist checks.
468 struct BlackListCheckContext
472 * We keep blacklist checks in a DLL.
474 struct BlackListCheckContext *next;
477 * We keep blacklist checks in a DLL.
479 struct BlackListCheckContext *prev;
482 * Address that is being checked.
484 struct NeighbourAddress na;
487 * Handle to the ongoing blacklist check.
489 struct GST_BlacklistCheck *bc;
494 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
496 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
499 * We keep blacklist checks in a DLL so that we can find
500 * the 'sessions' in their 'struct NeighbourAddress' if
501 * a session goes down.
503 static struct BlackListCheckContext *bc_head;
506 * We keep blacklist checks in a DLL.
508 static struct BlackListCheckContext *bc_tail;
511 * List of pending blacklist checks: head
513 static struct BlacklistCheckSwitchContext *pending_bc_head;
516 * List of pending blacklist checks: tail
518 static struct BlacklistCheckSwitchContext *pending_bc_tail;
521 * counter for connected neighbours
523 static unsigned int neighbours_connected;
526 * Number of bytes we have currently queued for transmission.
528 static unsigned long long bytes_in_send_queue;
531 * Task transmitting utilization data
533 static struct GNUNET_SCHEDULER_Task *util_transmission_tk;
537 * Convert the given ACK state to a string.
540 * @return corresponding human-readable string
543 print_ack_state (enum GST_ACK_State s)
548 case ACK_SEND_SYN_ACK:
549 return "SEND_SYN_ACK";
560 * Notify our clients that another peer connected to us.
562 * @param n the peer that connected
565 neighbours_connect_notification (struct NeighbourMapEntry *n)
567 size_t len = sizeof(struct ConnectInfoMessage);
568 char buf[len] GNUNET_ALIGN;
569 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
570 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
572 #if IGNORE_INBOUND_QUOTA
573 bandwidth_min = n->primary_address.bandwidth_out;
575 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
576 n->neighbour_receive_quota);
578 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
579 "We are now connected to peer `%s'\n",
580 GNUNET_i2s (&n->id));
581 connect_msg->header.size = htons (sizeof(buf));
582 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
583 connect_msg->id = n->id;
584 connect_msg->quota_in = n->primary_address.bandwidth_in;
585 connect_msg->quota_out = bandwidth_min;
586 GST_clients_broadcast (&connect_msg->header,
592 * Notify our clients (and manipulation) that a peer disconnected from
595 * @param n the peer that disconnected
598 neighbours_disconnect_notification (struct NeighbourMapEntry *n)
600 struct DisconnectInfoMessage disconnect_msg;
602 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
603 "Peer `%s' disconnected\n",
604 GNUNET_i2s (&n->id));
605 GST_manipulation_peer_disconnect (&n->id);
606 disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
607 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
608 disconnect_msg.reserved = htonl (0);
609 disconnect_msg.peer = n->id;
610 GST_clients_broadcast (&disconnect_msg.header,
616 * Notify transport clients that a neighbour peer changed its active
619 * @param peer identity of the peer
620 * @param address address possibly NULL if peer is not connected
621 * @param state current state this peer is in
622 * @param state_timeout timeout for the current state of the peer
623 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
624 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
627 neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer,
628 const struct GNUNET_HELLO_Address *address,
629 enum GNUNET_TRANSPORT_PeerState state,
630 struct GNUNET_TIME_Absolute state_timeout,
631 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
632 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
635 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
637 GST_plugins_a2s (address),
638 GNUNET_TRANSPORT_ps2s (state),
639 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
640 /* FIXME: include bandwidth in notification! */
641 GST_clients_broadcast_peer_notification (peer,
649 * Lookup a neighbour entry in the neighbours hash map.
651 * @param pid identity of the peer to look up
652 * @return the entry, NULL if there is no existing record
654 static struct NeighbourMapEntry *
655 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
657 if (NULL == neighbours)
659 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
664 * Test if we're connected to the given peer.
666 * @param n neighbour entry of peer to test
667 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
670 test_connected (struct NeighbourMapEntry *n)
674 return GNUNET_TRANSPORT_is_connected (n->state);
679 * Send information about a new outbound quota to our clients.
680 * Note that the outbound quota is enforced client-side (i.e.
681 * in libgnunettransport).
683 * @param n affected peer
686 send_outbound_quota_to_clients (struct NeighbourMapEntry *n)
688 struct QuotaSetMessage q_msg;
689 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_min;
691 if (! GNUNET_TRANSPORT_is_connected (n->state))
693 #if IGNORE_INBOUND_QUOTA
694 bandwidth_min = n->primary_address.bandwidth_out;
696 bandwidth_min = GNUNET_BANDWIDTH_value_min (n->primary_address.bandwidth_out,
697 n->neighbour_receive_quota);
700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
701 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
702 ntohl (bandwidth_min.value__),
703 GNUNET_i2s (&n->id));
704 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
705 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
706 q_msg.quota = bandwidth_min;
708 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
713 * We don't need a given neighbour address any more.
714 * Release its resources and give appropriate notifications
715 * to ATS and other subsystems.
717 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
720 free_address (struct NeighbourAddress *na)
722 if (GNUNET_YES == na->ats_active)
723 GST_validation_set_address_use (na->address,
725 if (NULL != na->address)
727 GST_ats_block_address (na->address,
729 GNUNET_HELLO_address_free (na->address);
732 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
733 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
734 na->ats_active = GNUNET_NO;
735 na->keep_alive_nonce = 0;
741 * Master task run for every neighbour. Performs all of the time-related
742 * activities (keep alive, send next message, disconnect if idle, finish
743 * clean up after disconnect).
745 * @param cls the `struct NeighbourMapEntry` for which we are running
746 * @param tc scheduler context (unused)
749 master_task (void *cls,
750 const struct GNUNET_SCHEDULER_TaskContext *tc);
754 * Set net state and state timeout for this neighbour and notify monitoring
756 * @param n the respective neighbour
757 * @param s the new state
758 * @param timeout the new timeout
761 set_state_and_timeout (struct NeighbourMapEntry *n,
762 enum GNUNET_TRANSPORT_PeerState s,
763 struct GNUNET_TIME_Absolute timeout)
765 if (GNUNET_TRANSPORT_is_connected (s) &&
766 ! GNUNET_TRANSPORT_is_connected (n->state) )
768 neighbours_connect_notification (n);
769 GNUNET_STATISTICS_set (GST_stats,
770 gettext_noop ("# peers connected"),
771 ++neighbours_connected,
774 if (! GNUNET_TRANSPORT_is_connected (s) &&
775 GNUNET_TRANSPORT_is_connected (n->state) )
777 GNUNET_STATISTICS_set (GST_stats,
778 gettext_noop ("# peers connected"),
779 --neighbours_connected,
781 neighbours_disconnect_notification (n);
784 if ( (timeout.abs_value_us < n->timeout.abs_value_us) &&
787 /* new timeout is earlier, reschedule master task */
788 GNUNET_SCHEDULER_cancel (n->task);
789 n->task = GNUNET_SCHEDULER_add_delayed (GNUNET_TIME_absolute_get_remaining (timeout),
793 n->timeout = timeout;
794 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
795 "Neighbour `%s' changed state to %s with timeout %s\n",
797 GNUNET_TRANSPORT_ps2s(s),
798 GNUNET_STRINGS_absolute_time_to_string (timeout));
799 neighbours_changed_notification (&n->id,
800 n->primary_address.address,
803 n->primary_address.bandwidth_in,
804 n->primary_address.bandwidth_out);
809 * Initialize the alternative address of a neighbour
811 * @param n the neighbour
812 * @param address address of the other peer, NULL if other peer
814 * @param session session to use (or NULL, in which case an
815 * address must be setup)
816 * @param bandwidth_in inbound quota to be used when connection is up
817 * @param bandwidth_out outbound quota to be used when connection is up
820 set_alternative_address (struct NeighbourMapEntry *n,
821 const struct GNUNET_HELLO_Address *address,
822 struct GNUNET_ATS_Session *session,
823 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
824 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
826 struct GNUNET_TRANSPORT_PluginFunctions *papi;
828 if (NULL == (papi = GST_plugins_find (address->transport_name)))
833 if (session == n->alternative_address.session)
835 n->alternative_address.bandwidth_in = bandwidth_in;
836 n->alternative_address.bandwidth_out = bandwidth_out;
839 if (NULL != n->alternative_address.address)
841 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
842 "Replacing existing alternative address with another one\n");
843 free_address (&n->alternative_address);
846 session = papi->get_session (papi->cls,
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Failed to obtain new session for peer `%s' and address '%s'\n",
852 GNUNET_i2s (&address->peer),
853 GST_plugins_a2s (address));
854 GNUNET_STATISTICS_update (GST_stats,
855 gettext_noop ("# session creation failed"),
860 GST_ats_new_session (address,
862 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
863 "Neighbour `%s' configured alternative address %s\n",
865 GST_plugins_a2s(address));
867 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
868 n->alternative_address.bandwidth_in = bandwidth_in;
869 n->alternative_address.bandwidth_out = bandwidth_out;
870 n->alternative_address.session = session;
871 n->alternative_address.ats_active = GNUNET_NO;
872 n->alternative_address.keep_alive_nonce = 0;
873 GNUNET_assert (GNUNET_YES ==
874 GST_ats_is_known (n->alternative_address.address,
875 n->alternative_address.session));
880 * Transmit a message using the current session of the given
883 * @param n entry for the recipient
884 * @param msgbuf buffer to transmit
885 * @param msgbuf_size number of bytes in @a msgbuf buffer
886 * @param priority transmission priority
887 * @param timeout transmission timeout
888 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
889 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
890 * @param cont continuation to call when finished (can be NULL)
891 * @param cont_cls closure for @a cont
892 * @return timeout (copy of @a timeout or a calculated one if
893 * @a use_keepalive_timeout is #GNUNET_YES.
895 static struct GNUNET_TIME_Relative
896 send_with_session (struct NeighbourMapEntry *n,
900 struct GNUNET_TIME_Relative timeout,
901 unsigned int use_keepalive_timeout,
902 GNUNET_TRANSPORT_TransmitContinuation cont,
905 struct GNUNET_TRANSPORT_PluginFunctions *papi;
906 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
908 GNUNET_assert (NULL != n->primary_address.session);
909 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
910 (-1 == papi->send (papi->cls,
911 n->primary_address.session,
915 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
916 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
917 papi->query_keepalive_factor (papi->cls))),
926 GST_neighbours_notify_data_sent (n->primary_address.address,
927 n->primary_address.session,
929 GNUNET_break (NULL != papi);
935 * Clear the primary address of a neighbour since this address is not
936 * valid anymore and notify monitoring about it
938 * @param n the neighbour
941 unset_primary_address (struct NeighbourMapEntry *n)
943 /* Notify monitoring about change */
944 if (NULL == n->primary_address.address)
946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
947 "Disabling primary address\n");
948 neighbours_changed_notification (&n->id,
949 n->primary_address.address,
952 GNUNET_BANDWIDTH_value_init (0),
953 GNUNET_BANDWIDTH_value_init (0));
954 free_address (&n->primary_address);
959 * Free a neighbour map entry.
961 * @param n entry to free
964 free_neighbour (struct NeighbourMapEntry *n)
966 struct MessageQueue *mq;
968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
969 "Freeing neighbour state of peer `%s'\n",
970 GNUNET_i2s (&n->id));
971 n->is_active = NULL; /* always free'd by its own continuation! */
973 /* fail messages currently in the queue */
974 while (NULL != (mq = n->messages_head))
976 GNUNET_CONTAINER_DLL_remove (n->messages_head,
979 if (NULL != mq->cont)
980 mq->cont (mq->cont_cls,
982 mq->message_buf_size,
986 /* Mark peer as disconnected */
987 set_state_and_timeout (n,
988 GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED,
989 GNUNET_TIME_UNIT_FOREVER_ABS);
990 /* free addresses and mark as unused */
991 unset_primary_address (n);
993 if (NULL != n->alternative_address.address)
995 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
996 "Cleaning up alternative address\n");
997 free_address (&n->alternative_address);
999 GNUNET_assert (GNUNET_YES ==
1000 GNUNET_CONTAINER_multipeermap_remove (neighbours,
1003 /* Cancel address requests for this peer */
1004 if (NULL != n->suggest_handle)
1006 GNUNET_ATS_connectivity_suggest_cancel (n->suggest_handle);
1007 n->suggest_handle = NULL;
1010 /* Cancel the disconnect task */
1011 if (NULL != n->delayed_disconnect_task)
1013 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
1014 n->delayed_disconnect_task = NULL;
1017 /* Cancel the master task */
1018 if (NULL != n->task)
1020 GNUNET_SCHEDULER_cancel (n->task);
1023 /* free rest of memory */
1029 * Function called when the 'DISCONNECT' message has been sent by the
1030 * plugin. Frees the neighbour --- if the entry still exists.
1033 * @param target identity of the neighbour that was disconnected
1034 * @param result #GNUNET_OK if the disconnect got out successfully
1035 * @param payload bytes payload
1036 * @param physical bytes on wire
1039 send_disconnect_cont (void *cls,
1040 const struct GNUNET_PeerIdentity *target,
1045 struct NeighbourMapEntry *n;
1047 n = lookup_neighbour (target);
1049 return; /* already gone */
1050 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1051 return; /* have created a fresh entry since */
1052 if (NULL != n->task)
1053 GNUNET_SCHEDULER_cancel (n->task);
1054 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1059 * Transmit a DISCONNECT message to the other peer.
1061 * @param n neighbour to send DISCONNECT message.
1064 send_disconnect (struct NeighbourMapEntry *n)
1066 struct GNUNET_ATS_SessionDisconnectMessage disconnect_msg;
1068 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1069 "Sending DISCONNECT message to peer `%4s'\n",
1070 GNUNET_i2s (&n->id));
1071 disconnect_msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionDisconnectMessage));
1072 disconnect_msg.header.type =
1073 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1074 disconnect_msg.reserved = htonl (0);
1075 disconnect_msg.purpose.size =
1076 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1077 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1078 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1079 disconnect_msg.purpose.purpose =
1080 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1081 disconnect_msg.timestamp =
1082 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1083 disconnect_msg.public_key = GST_my_identity.public_key;
1084 GNUNET_assert (GNUNET_OK ==
1085 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1086 &disconnect_msg.purpose,
1087 &disconnect_msg.signature));
1089 (void) send_with_session (n,
1091 sizeof (disconnect_msg),
1093 GNUNET_TIME_UNIT_FOREVER_REL,
1095 &send_disconnect_cont,
1097 GNUNET_STATISTICS_update (GST_stats,
1098 gettext_noop ("# DISCONNECT messages sent"),
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 */
1125 case GNUNET_TRANSPORT_PS_SYN_SENT:
1126 send_disconnect (n);
1127 set_state_and_timeout (n,
1128 GNUNET_TRANSPORT_PS_DISCONNECT,
1129 GNUNET_TIME_UNIT_FOREVER_ABS);
1131 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1132 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1135 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1136 /* we DID ACK the other peer's request, must send DISCONNECT */
1137 send_disconnect (n);
1138 set_state_and_timeout (n,
1139 GNUNET_TRANSPORT_PS_DISCONNECT,
1140 GNUNET_TIME_UNIT_FOREVER_ABS);
1142 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1143 case GNUNET_TRANSPORT_PS_CONNECTED:
1144 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1145 /* we are currently connected, need to send disconnect and do
1146 internal notifications and update statistics */
1147 send_disconnect (n);
1148 set_state_and_timeout (n,
1149 GNUNET_TRANSPORT_PS_DISCONNECT,
1150 GNUNET_TIME_UNIT_FOREVER_ABS);
1152 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1153 /* Disconnecting while waiting for an ATS address to reconnect,
1154 * cannot send DISCONNECT */
1157 case GNUNET_TRANSPORT_PS_DISCONNECT:
1158 /* already disconnected, ignore */
1160 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1161 /* already cleaned up, how did we get here!? */
1165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1166 "Unhandled state `%s'\n",
1167 GNUNET_TRANSPORT_ps2s (n->state));
1171 /* schedule timeout to clean up */
1172 if (NULL != n->task)
1173 GNUNET_SCHEDULER_cancel (n->task);
1174 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1181 * Change the incoming quota for the given peer. Updates
1182 * our own receive rate and informs the neighbour about
1185 * @param n neighbour entry to change qutoa for
1186 * @param quota new quota
1189 set_incoming_quota (struct NeighbourMapEntry *n,
1190 struct GNUNET_BANDWIDTH_Value32NBO quota)
1192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1193 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
1194 ntohl (quota.value__), GNUNET_i2s (&n->id));
1195 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker,
1197 if (0 != ntohl (quota.value__))
1199 struct GNUNET_ATS_SessionQuotaMessage sqm;
1201 sqm.header.size = htons (sizeof (struct GNUNET_ATS_SessionQuotaMessage));
1202 sqm.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_QUOTA);
1203 sqm.quota = quota.value__;
1204 (void) send_with_session (n,
1208 GNUNET_TIME_UNIT_FOREVER_REL,
1213 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1214 "Disconnecting peer `%4s' due to SET_QUOTA\n",
1215 GNUNET_i2s (&n->id));
1216 if (GNUNET_YES == test_connected (n))
1217 GNUNET_STATISTICS_update (GST_stats,
1218 gettext_noop ("# disconnects due to quota of 0"),
1220 disconnect_neighbour (n);
1225 * Initialize the primary address of a neighbour
1227 * @param n the neighbour
1228 * @param address address of the other peer, NULL if other peer
1230 * @param session session to use (or NULL, in which case an
1231 * address must be setup)
1232 * @param bandwidth_in inbound quota to be used when connection is up
1233 * @param bandwidth_out outbound quota to be used when connection is up
1236 set_primary_address (struct NeighbourMapEntry *n,
1237 const struct GNUNET_HELLO_Address *address,
1238 struct GNUNET_ATS_Session *session,
1239 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1240 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
1242 if (session == n->primary_address.session)
1244 GST_validation_set_address_use (n->primary_address.address,
1246 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
1248 n->primary_address.bandwidth_in = bandwidth_in;
1249 set_incoming_quota (n,
1252 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
1254 n->primary_address.bandwidth_out = bandwidth_out;
1255 send_outbound_quota_to_clients (n);
1259 if ( (NULL != n->primary_address.address) &&
1260 (0 == GNUNET_HELLO_address_cmp (address,
1261 n->primary_address.address)) )
1266 if (NULL == session)
1269 GST_ats_block_address (address,
1273 if (NULL != n->primary_address.address)
1275 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1276 "Replacing existing primary address with another one\n");
1277 free_address (&n->primary_address);
1279 n->primary_address.address = GNUNET_HELLO_address_copy (address);
1280 n->primary_address.bandwidth_in = bandwidth_in;
1281 n->primary_address.bandwidth_out = bandwidth_out;
1282 n->primary_address.session = session;
1283 n->primary_address.keep_alive_nonce = 0;
1284 GNUNET_assert (GNUNET_YES ==
1285 GST_ats_is_known (n->primary_address.address,
1286 n->primary_address.session));
1287 /* subsystems about address use */
1288 GST_validation_set_address_use (n->primary_address.address,
1290 set_incoming_quota (n,
1292 send_outbound_quota_to_clients (n);
1293 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1294 "Neighbour `%s' switched to address `%s'\n",
1295 GNUNET_i2s (&n->id),
1296 GST_plugins_a2s(address));
1298 neighbours_changed_notification (&n->id,
1299 n->primary_address.address,
1302 n->primary_address.bandwidth_in,
1303 n->primary_address.bandwidth_out);
1308 * We're done with our transmission attempt, continue processing.
1310 * @param cls the `struct MessageQueue` of the message
1311 * @param receiver intended receiver
1312 * @param success whether it worked or not
1313 * @param size_payload bytes payload sent
1314 * @param physical bytes sent on wire
1317 transmit_send_continuation (void *cls,
1318 const struct GNUNET_PeerIdentity *receiver,
1320 size_t size_payload,
1323 struct MessageQueue *mq = cls;
1324 struct NeighbourMapEntry *n;
1326 if (NULL == (n = lookup_neighbour (receiver)))
1328 if (NULL != mq->cont)
1329 mq->cont (mq->cont_cls,
1330 GNUNET_SYSERR /* not connected */,
1334 return; /* disconnect or other error while transmitting, can happen */
1336 if (n->is_active == mq)
1338 /* this is still "our" neighbour, remove us from its queue
1339 and allow it to send the next message now */
1340 n->is_active = NULL;
1341 if (NULL != n->task)
1342 GNUNET_SCHEDULER_cancel (n->task);
1343 n->task = GNUNET_SCHEDULER_add_now (&master_task,
1346 if (bytes_in_send_queue < mq->message_buf_size)
1348 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1349 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1350 bytes_in_send_queue,
1351 mq->message_buf_size,
1352 (GNUNET_OK == success) ? "OK" : "FAIL",
1358 GNUNET_break (size_payload == mq->message_buf_size);
1359 bytes_in_send_queue -= mq->message_buf_size;
1360 GNUNET_STATISTICS_set (GST_stats,
1361 gettext_noop ("# bytes in message queue for other peers"),
1362 bytes_in_send_queue,
1364 if (GNUNET_OK == success)
1365 GNUNET_STATISTICS_update (GST_stats,
1366 gettext_noop ("# messages transmitted to other peers"),
1370 GNUNET_STATISTICS_update (GST_stats,
1372 ("# transmission failures for messages to other peers"),
1374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1375 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1376 GNUNET_i2s (receiver),
1377 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1378 mq->message_buf_size,
1379 (success == GNUNET_OK) ? "success" : "FAILURE");
1380 if (NULL != mq->cont)
1381 mq->cont (mq->cont_cls,
1390 * Check the message list for the given neighbour and if we can
1391 * send a message, do so. This function should only be called
1392 * if the connection is at least generally ready for transmission.
1393 * While we will only send one message at a time, no bandwidth
1394 * quota management is performed here. If a message was given to
1395 * the plugin, the continuation will automatically re-schedule
1396 * the 'master' task once the next message might be transmitted.
1398 * @param n target peer for which to transmit
1401 try_transmission_to_peer (struct NeighbourMapEntry *n)
1403 struct MessageQueue *mq;
1404 struct GNUNET_TIME_Relative timeout;
1406 if (NULL == n->primary_address.address)
1408 /* no address, why are we here? */
1412 if ((0 == n->primary_address.address->address_length) &&
1413 (NULL == n->primary_address.session))
1415 /* no address, why are we here? */
1419 if (NULL != n->is_active)
1421 /* transmission already pending */
1425 /* timeout messages from the queue that are past their due date */
1426 while (NULL != (mq = n->messages_head))
1428 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1429 if (timeout.rel_value_us > 0)
1431 GNUNET_STATISTICS_update (GST_stats,
1432 gettext_noop ("# messages timed out while in transport queue"),
1435 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1439 transmit_send_continuation (mq,
1442 mq->message_buf_size,
1446 return; /* no more messages */
1447 GNUNET_CONTAINER_DLL_remove (n->messages_head,
1452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1453 "Giving message with %u bytes to plugin session %p\n",
1454 mq->message_buf_size,
1455 n->primary_address.session);
1456 (void) send_with_session (n,
1458 mq->message_buf_size,
1462 &transmit_send_continuation,
1468 * Send keepalive message to the neighbour. Must only be called
1469 * if we are on 'connected' state or while trying to switch addresses.
1470 * Will internally determine if a keepalive is truly needed (so can
1471 * always be called).
1473 * @param n neighbour that went idle and needs a keepalive
1476 send_keepalive (struct NeighbourMapEntry *n)
1478 struct GNUNET_ATS_SessionKeepAliveMessage m;
1479 struct GNUNET_TIME_Relative timeout;
1482 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1483 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state));
1484 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1485 return; /* no keepalive needed at this time */
1487 nonce = 0; /* 0 indicates 'not set' */
1489 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE,
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 "Sending KEEPALIVE to peer `%s' with nonce %u\n",
1494 GNUNET_i2s (&n->id),
1496 m.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1497 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1498 m.nonce = htonl (nonce);
1500 timeout = send_with_session (n,
1503 UINT32_MAX /* priority */,
1504 GNUNET_TIME_UNIT_FOREVER_REL,
1507 GNUNET_STATISTICS_update (GST_stats,
1508 gettext_noop ("# KEEPALIVES sent"),
1511 n->primary_address.keep_alive_nonce = nonce;
1512 n->expect_latency_response = GNUNET_YES;
1513 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1514 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1519 * Keep the connection to the given neighbour alive longer,
1520 * we received a KEEPALIVE (or equivalent); send a response.
1522 * @param neighbour neighbour to keep alive (by sending keep alive response)
1523 * @param m the keep alive message containing the nonce to respond to
1526 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1527 const struct GNUNET_MessageHeader *m)
1529 struct NeighbourMapEntry *n;
1530 const struct GNUNET_ATS_SessionKeepAliveMessage *msg_in;
1531 struct GNUNET_ATS_SessionKeepAliveMessage msg;
1533 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1535 GNUNET_break_op (0);
1539 msg_in = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1540 if (NULL == (n = lookup_neighbour (neighbour)))
1542 GNUNET_STATISTICS_update (GST_stats,
1544 ("# KEEPALIVE messages discarded (peer unknown)"),
1548 if (NULL == n->primary_address.session)
1550 GNUNET_STATISTICS_update (GST_stats,
1552 ("# KEEPALIVE messages discarded (no session)"),
1557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1558 "Received KEEPALIVE request from peer `%s' with nonce %u\n",
1559 GNUNET_i2s (&n->id),
1560 ntohl (msg_in->nonce));
1561 GNUNET_STATISTICS_update (GST_stats,
1562 gettext_noop ("# KEEPALIVES received in good order"),
1566 /* send reply to allow neighbour to measure latency */
1567 msg.header.size = htons (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage));
1568 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1569 msg.nonce = msg_in->nonce;
1570 (void) send_with_session (n,
1572 sizeof (struct GNUNET_ATS_SessionKeepAliveMessage),
1573 UINT32_MAX /* priority */,
1574 GNUNET_TIME_UNIT_FOREVER_REL,
1581 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1582 * latency to this peer. Pass the updated information (existing ats
1583 * plus calculated latency) to ATS.
1585 * @param neighbour neighbour to keep alive
1586 * @param m the message containing the keep alive response
1589 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1590 const struct GNUNET_MessageHeader *m)
1592 struct NeighbourMapEntry *n;
1593 const struct GNUNET_ATS_SessionKeepAliveMessage *msg;
1594 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1595 struct GNUNET_TIME_Relative latency;
1597 if (sizeof (struct GNUNET_ATS_SessionKeepAliveMessage) != ntohs (m->size))
1599 GNUNET_break_op (0);
1603 msg = (const struct GNUNET_ATS_SessionKeepAliveMessage *) m;
1604 if (NULL == (n = lookup_neighbour (neighbour)))
1606 GNUNET_STATISTICS_update (GST_stats,
1607 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not connected)"),
1612 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1613 (GNUNET_YES != n->expect_latency_response) )
1615 GNUNET_STATISTICS_update (GST_stats,
1616 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (not expected)"),
1621 if (NULL == n->primary_address.address)
1623 GNUNET_STATISTICS_update (GST_stats,
1624 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (address changed)"),
1629 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1631 if (0 == n->primary_address.keep_alive_nonce)
1632 GNUNET_STATISTICS_update (GST_stats,
1633 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (no nonce)"),
1637 GNUNET_STATISTICS_update (GST_stats,
1638 gettext_noop ("# KEEPALIVE_RESPONSEs discarded (bad nonce)"),
1643 GNUNET_STATISTICS_update (GST_stats,
1644 gettext_noop ("# KEEPALIVE_RESPONSEs received (OK)"),
1649 /* Update session timeout here */
1650 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1653 "Updating session for peer `%s' for session %p\n",
1654 GNUNET_i2s (&n->id),
1655 n->primary_address.session);
1656 papi->update_session_timeout (papi->cls,
1658 n->primary_address.session);
1665 n->primary_address.keep_alive_nonce = 0;
1666 n->expect_latency_response = GNUNET_NO;
1667 set_state_and_timeout (n,
1669 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1671 latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1673 "Received KEEPALIVE_RESPONSE from peer `%s', latency is %s\n",
1674 GNUNET_i2s (&n->id),
1675 GNUNET_STRINGS_relative_time_to_string (latency,
1677 GST_ats_update_delay (n->primary_address.address,
1678 GNUNET_TIME_relative_divide (latency,
1684 * We have received a message from the given sender. How long should
1685 * we delay before receiving more? (Also used to keep the peer marked
1688 * @param sender sender of the message
1689 * @param size size of the message
1690 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1691 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1692 * #GNUNET_SYSERR if the connection is not fully up yet
1693 * @return how long to wait before reading more from this sender
1695 struct GNUNET_TIME_Relative
1696 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity *sender,
1700 struct NeighbourMapEntry *n;
1701 struct GNUNET_TIME_Relative ret;
1703 if (NULL == neighbours)
1705 *do_forward = GNUNET_NO;
1706 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1708 if (NULL == (n = lookup_neighbour (sender)))
1710 GST_neighbours_try_connect (sender);
1711 if (NULL == (n = lookup_neighbour (sender)))
1713 GNUNET_STATISTICS_update (GST_stats,
1715 ("# messages discarded due to lack of neighbour record"),
1717 *do_forward = GNUNET_NO;
1718 return GNUNET_TIME_UNIT_ZERO;
1721 if (! test_connected (n))
1723 *do_forward = GNUNET_SYSERR;
1724 return GNUNET_TIME_UNIT_ZERO;
1726 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1728 n->quota_violation_count++;
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1730 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1731 n->in_tracker.available_bytes_per_s__,
1732 n->quota_violation_count);
1733 /* Discount 32k per violation */
1734 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1738 if (n->quota_violation_count > 0)
1740 /* try to add 32k back */
1741 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1742 n->quota_violation_count--;
1745 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1747 GNUNET_STATISTICS_update (GST_stats,
1749 ("# bandwidth quota violations by other peers"),
1751 *do_forward = GNUNET_NO;
1752 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1754 *do_forward = GNUNET_YES;
1755 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1756 if (ret.rel_value_us > 0)
1758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1759 "Throttling read (%lld bytes excess at %u b/s), waiting %s before reading more.\n",
1760 (long long) n->in_tracker.consumption_since_last_update__,
1761 (unsigned int) n->in_tracker.available_bytes_per_s__,
1762 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1763 GNUNET_STATISTICS_update (GST_stats,
1764 gettext_noop ("# ms throttling suggested"),
1765 (int64_t) ret.rel_value_us / 1000LL,
1773 * Transmit a message to the given target using the active connection.
1775 * @param target destination
1776 * @param msg message to send
1777 * @param msg_size number of bytes in msg
1778 * @param timeout when to fail with timeout
1779 * @param cont function to call when done
1780 * @param cont_cls closure for @a cont
1783 GST_neighbours_send (const struct GNUNET_PeerIdentity *target,
1786 struct GNUNET_TIME_Relative timeout,
1787 GST_NeighbourSendContinuation cont,
1790 struct NeighbourMapEntry *n;
1791 struct MessageQueue *mq;
1793 /* All ove these cases should never happen; they are all API violations.
1794 But we check anyway, just to be sure. */
1795 if (NULL == (n = lookup_neighbour (target)))
1805 if (GNUNET_YES != test_connected (n))
1815 bytes_in_send_queue += msg_size;
1816 GNUNET_STATISTICS_set (GST_stats,
1818 ("# bytes in message queue for other peers"),
1819 bytes_in_send_queue, GNUNET_NO);
1820 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1822 mq->cont_cls = cont_cls;
1823 memcpy (&mq[1], msg, msg_size);
1824 mq->message_buf = (const char *) &mq[1];
1825 mq->message_buf_size = msg_size;
1826 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "Enqueueing %u bytes to send to peer %s\n",
1831 GNUNET_i2s (target));
1832 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head,
1835 if (NULL != n->task)
1836 GNUNET_SCHEDULER_cancel (n->task);
1837 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1842 * Continuation called from our attempt to transmitted our
1843 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN to the specified @a
1844 * target. Continue processing based on the @a result. Specifically,
1845 * if we failed to transmit, discard the address we used.
1848 * @param target which peer received the transmission
1849 * @param result #GNUNET_OK if sending worked
1850 * @param size_payload how many bytes of payload were sent (ignored)
1851 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
1854 send_session_syn_cont (void *cls,
1855 const struct GNUNET_PeerIdentity *target,
1857 size_t size_payload,
1858 size_t size_on_wire)
1860 struct NeighbourMapEntry *n;
1862 n = lookup_neighbour (target);
1865 /* SYN continuation was called after neighbor was freed,
1866 * for example due to a time out for the state or the session
1867 * used was already terminated: nothing to do here... */
1871 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1872 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1873 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1875 /* SYN continuation was called after neighbor changed state,
1876 * for example due to a time out for the state or the session
1877 * used was already terminated: nothing to do here... */
1880 if (GNUNET_OK == result)
1883 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1884 _("Failed to send SYN message to peer `%s'\n"),
1885 GNUNET_i2s (target));
1887 case GNUNET_TRANSPORT_PS_SYN_SENT:
1888 /* Remove address and request an additional one */
1889 unset_primary_address (n);
1890 set_state_and_timeout (n,
1891 GNUNET_TRANSPORT_PS_INIT_ATS,
1892 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1894 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1895 /* Remove address and request an additional one */
1896 unset_primary_address (n);
1897 set_state_and_timeout (n,
1898 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1899 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1901 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1902 /* Remove address and request and go back to primary address */
1903 GNUNET_STATISTICS_update (GST_stats,
1904 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN CONT)"),
1907 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1908 "Switch failed, cleaning up alternative address\n");
1909 free_address (&n->alternative_address);
1910 set_state_and_timeout (n,
1911 GNUNET_TRANSPORT_PS_CONNECTED,
1912 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1915 disconnect_neighbour (n);
1922 * Send a SYN message via the given address.
1924 * @param na address to use
1927 send_syn (struct NeighbourAddress *na)
1929 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1930 struct TransportSynMessage connect_msg;
1931 struct NeighbourMapEntry *n;
1933 GNUNET_assert (NULL != na->session);
1934 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1935 "Sending SYN message to peer `%s' at %s\n",
1936 GNUNET_i2s (&na->address->peer),
1937 GST_plugins_a2s (na->address));
1939 papi = GST_plugins_find (na->address->transport_name);
1940 GNUNET_assert (NULL != papi);
1941 GNUNET_STATISTICS_update (GST_stats,
1943 ("# SYN messages sent"),
1945 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1946 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1947 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1948 connect_msg.reserved = htonl (0);
1949 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1951 papi->send (papi->cls,
1953 (const char *) &connect_msg,
1954 sizeof (struct TransportSynMessage),
1956 SETUP_CONNECTION_TIMEOUT,
1957 &send_session_syn_cont, NULL))
1959 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1960 _("Failed to transmit SYN message to %s\n"),
1961 GST_plugins_a2s (na->address));
1962 n = lookup_neighbour (&na->address->peer);
1969 case GNUNET_TRANSPORT_PS_SYN_SENT:
1970 /* Remove address and request and additional one */
1971 GNUNET_assert (na == &n->primary_address);
1972 unset_primary_address (n);
1973 set_state_and_timeout (n,
1974 GNUNET_TRANSPORT_PS_INIT_ATS,
1975 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1976 /* Hard failure to send the SYN message with this address:
1977 Destroy address and session */
1979 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1980 /* Remove address and request an additional one */
1981 GNUNET_assert (na == &n->primary_address);
1982 unset_primary_address (n);
1983 set_state_and_timeout (n,
1984 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1985 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1987 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1988 GNUNET_assert (na == &n->alternative_address);
1989 GNUNET_STATISTICS_update (GST_stats,
1990 gettext_noop ("# Failed attempts to switch addresses (failed to send SYN)"),
1993 /* Remove address and request an additional one */
1994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1995 "Switch failed, cleaning up alternative address\n");
1996 free_address (&n->alternative_address);
1997 set_state_and_timeout (n,
1998 GNUNET_TRANSPORT_PS_CONNECTED,
1999 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2003 disconnect_neighbour (n);
2008 GST_neighbours_notify_data_sent (na->address,
2010 sizeof (struct TransportSynMessage));
2015 * Continuation called from our attempt to transmitted our
2016 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK to the specified @a
2017 * target. Continue processing based on the @a result. Specifically,
2018 * if we failed to transmit, discard the address we used.
2021 * @param target which peer received the transmission
2022 * @param result #GNUNET_OK if sending worked
2023 * @param size_payload how many bytes of payload were sent (ignored)
2024 * @param size_on_wire how much bandwidth was consumed on the wire (ignored)
2027 send_session_syn_ack_cont (void *cls,
2028 const struct GNUNET_PeerIdentity *target,
2030 size_t size_payload,
2031 size_t size_on_wire)
2033 struct NeighbourMapEntry *n;
2035 n = lookup_neighbour (target);
2038 /* SYN_ACK continuation was called after neighbor was freed,
2039 * for example due to a time out for the state or the session
2040 * used was already terminated: nothing to do here... */
2044 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
2046 /* SYN_ACK continuation was called after neighbor changed state,
2047 * for example due to a time out for the state or the session
2048 * used was already terminated: nothing to do here... */
2051 if (GNUNET_OK == result)
2054 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2055 _("Failed to send SYN_ACK message to peer `%s' using address `%s'\n"),
2056 GNUNET_i2s (target),
2057 GST_plugins_a2s (n->primary_address.address));
2059 /* Remove address and request and additional one */
2060 /* FIXME: what if the neighbour's primary address
2061 changed in the meantime? Might want to instead
2062 pass "something" around in closure to be sure. */
2063 unset_primary_address (n);
2064 n->ack_state = ACK_SEND_SYN_ACK;
2065 set_state_and_timeout (n,
2066 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2067 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2072 * Send a SYN_ACK message via the given address.
2074 * @param na address and session to use
2075 * @param timestamp timestamp to use for the ACK message
2076 * @return #GNUNET_SYSERR if sending immediately failed, #GNUNET_OK otherwise
2079 send_syn_ack_message (struct NeighbourAddress *na,
2080 struct GNUNET_TIME_Absolute timestamp)
2082 const struct GNUNET_HELLO_Address *address = na->address;
2083 struct GNUNET_ATS_Session *session = na->session;
2084 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2085 struct TransportSynMessage connect_msg;
2086 struct NeighbourMapEntry *n;
2088 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2089 "Sending SYN_ACK to peer `%s'\n",
2090 GNUNET_i2s (&address->peer));
2092 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2097 if (NULL == session)
2098 session = papi->get_session (papi->cls,
2100 if (NULL == session)
2105 GST_ats_new_session (address,
2107 GNUNET_STATISTICS_update (GST_stats,
2109 ("# SYN_ACK messages sent"),
2111 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
2112 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
2113 connect_msg.reserved = htonl (0);
2114 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
2116 if (GNUNET_SYSERR ==
2117 papi->send (papi->cls,
2119 (const char *) &connect_msg,
2120 sizeof (struct TransportSynMessage),
2122 GNUNET_TIME_UNIT_FOREVER_REL,
2123 &send_session_syn_ack_cont, NULL))
2125 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2126 _("Failed to transmit SYN_ACK message to %s\n"),
2127 GST_plugins_a2s (address));
2129 n = lookup_neighbour (&address->peer);
2135 /* Remove address and request and additional one */
2136 unset_primary_address (n);
2137 n->ack_state = ACK_SEND_SYN_ACK;
2138 set_state_and_timeout (n,
2139 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2140 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2147 * Function called by the bandwidth tracker for a peer whenever
2148 * the tracker's state changed such that we need to recalculate
2149 * the delay for flow control. We calculate the latest delay
2150 * and inform the plugin (if applicable).
2152 * @param cls the `struct NeighbourMapEntry` to update calculations for
2155 inbound_bw_tracker_update (void *cls)
2157 struct NeighbourMapEntry *n = cls;
2158 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2159 struct GNUNET_TIME_Relative delay;
2162 if (NULL == n->primary_address.address)
2163 return; /* not active, ignore */
2164 papi = GST_plugins_find (n->primary_address.address->transport_name);
2165 GNUNET_assert (NULL != papi);
2166 if (NULL == papi->update_inbound_delay)
2168 delay = GST_neighbours_calculate_receive_delay (&n->id,
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 "New inbound delay for peer `%s' is %llu ms\n",
2173 GNUNET_i2s (&n->id),
2174 delay.rel_value_us / 1000);
2175 papi->update_inbound_delay (papi->cls,
2177 n->primary_address.session,
2183 * Create a fresh entry in the neighbour map for the given peer
2185 * @param peer peer to create an entry for
2186 * @return new neighbour map entry
2188 static struct NeighbourMapEntry *
2189 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2191 struct NeighbourMapEntry *n;
2193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2194 "Creating new neighbour entry for `%s'\n",
2197 memcmp (&GST_my_identity,
2199 sizeof (struct GNUNET_PeerIdentity)));
2200 n = GNUNET_new (struct NeighbourMapEntry);
2202 n->ack_state = ACK_UNDEFINED;
2203 n->last_util_transmission = GNUNET_TIME_absolute_get();
2204 n->neighbour_receive_quota = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2205 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2206 &inbound_bw_tracker_update,
2208 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2209 MAX_BANDWIDTH_CARRY_S);
2210 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2211 set_state_and_timeout (n,
2212 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2213 GNUNET_TIME_UNIT_FOREVER_ABS);
2214 GNUNET_assert (GNUNET_OK ==
2215 GNUNET_CONTAINER_multipeermap_put (neighbours,
2218 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2219 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2228 * Entry in a DLL we use to keep track of pending blacklist checks.
2230 struct BlacklistCheckSwitchContext
2235 struct BlacklistCheckSwitchContext *prev;
2240 struct BlacklistCheckSwitchContext *next;
2243 * Handle to the blacklist check we are performing.
2245 struct GST_BlacklistCheck *blc;
2248 * Inbound bandwidth that was assigned to @e address.
2250 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2253 * Outbound bandwidth that was assigned to @e address.
2255 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2260 * Black list check result for try_connect call
2261 * If connection to the peer is allowed request adddress and
2263 * @param cls blc_ctx bl context
2264 * @param peer the peer
2265 * @param address address associated with the request
2266 * @param session session associated with the request
2267 * @param result #GNUNET_OK if the connection is allowed,
2268 * #GNUNET_NO if not,
2269 * #GNUNET_SYSERR if operation was aborted
2272 try_connect_bl_check_cont (void *cls,
2273 const struct GNUNET_PeerIdentity *peer,
2274 const struct GNUNET_HELLO_Address *address,
2275 struct GNUNET_ATS_Session *session,
2278 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2279 struct NeighbourMapEntry *n;
2281 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2284 GNUNET_free (blc_ctx);
2285 if (GNUNET_OK != result)
2287 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2288 _("Blacklisting disapproved to connect to peer `%s'\n"),
2293 /* Setup a new neighbour */
2294 if (NULL != lookup_neighbour(peer))
2295 return; /* The neighbor was created in the meantime while waited for BL clients */
2297 n = setup_neighbour (peer);
2299 /* Request address suggestions for this peer */
2300 set_state_and_timeout (n,
2301 GNUNET_TRANSPORT_PS_INIT_ATS,
2302 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2307 * Try to create a connection to the given target (eventually).
2309 * @param target peer to try to connect to
2312 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2314 struct NeighbourMapEntry *n;
2315 struct GST_BlacklistCheck *blc;
2316 struct BlacklistCheckSwitchContext *blc_ctx;
2318 if (NULL == neighbours)
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2321 "Asked to connect to peer `%s' during shutdown\n",
2322 GNUNET_i2s (target));
2323 return; /* during shutdown, do nothing */
2325 n = lookup_neighbour (target);
2326 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2327 "Asked to connect to peer `%s' (state: %s)\n",
2328 GNUNET_i2s (target),
2329 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2334 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2335 /* this should not be possible */
2339 case GNUNET_TRANSPORT_PS_INIT_ATS:
2340 case GNUNET_TRANSPORT_PS_SYN_SENT:
2341 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2342 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2343 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2344 "Ignoring request to try to connect to `%s', already trying!\n",
2345 GNUNET_i2s (target));
2346 return; /* already trying */
2347 case GNUNET_TRANSPORT_PS_CONNECTED:
2348 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2349 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2350 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2351 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2352 "Ignoring request to try to connect, already connected to `%s'!\n",
2353 GNUNET_i2s (target));
2354 return; /* already connected */
2355 case GNUNET_TRANSPORT_PS_DISCONNECT:
2356 /* get rid of remains, ready to re-try immediately */
2359 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2360 /* should not be possible */
2364 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2365 "Unhandled state `%s'\n",
2366 GNUNET_TRANSPORT_ps2s (n->state));
2373 /* Do blacklist check if connecting to this peer is allowed */
2374 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2375 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2380 (blc = GST_blacklist_test_allowed (target,
2382 &try_connect_bl_check_cont,
2393 * We received a 'SYN' message from the other peer.
2394 * Consider switching to it.
2396 * @param message possibly a `struct TransportSynMessage` (check format)
2397 * @param peer identity of the peer to switch the address for
2398 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2401 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2402 const struct GNUNET_PeerIdentity *peer)
2404 const struct TransportSynMessage *scm;
2405 struct NeighbourMapEntry *n;
2406 struct GNUNET_TIME_Absolute ts;
2408 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2410 GNUNET_break_op (0);
2411 return GNUNET_SYSERR;
2413 GNUNET_STATISTICS_update (GST_stats,
2415 ("# SYN messages received"),
2417 if (NULL == neighbours)
2419 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2420 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2422 return GNUNET_OK; /* we're shutting down */
2424 scm = (const struct TransportSynMessage *) message;
2425 GNUNET_break_op (0 == ntohl (scm->reserved));
2426 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2427 n = lookup_neighbour (peer);
2430 /* This is a new neighbour and set to not connected */
2431 n = setup_neighbour (peer);
2434 /* Remember this SYN message in neighbour */
2435 n->ack_state = ACK_SEND_SYN_ACK;
2436 n->connect_ack_timestamp = ts;
2438 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2439 "Received SYN for peer `%s' in state %s/%s\n",
2441 GNUNET_TRANSPORT_ps2s (n->state),
2442 print_ack_state (n->ack_state));
2446 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2447 /* Request an address from ATS to send SYN_ACK to this peer */
2448 set_state_and_timeout (n,
2449 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2450 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2452 case GNUNET_TRANSPORT_PS_INIT_ATS:
2453 /* SYN message takes priority over us asking ATS for address:
2454 * Wait for ATS to suggest an address and send SYN_ACK */
2455 set_state_and_timeout (n,
2456 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2457 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2459 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2460 /* We already wait for an address to send an SYN_ACK */
2462 case GNUNET_TRANSPORT_PS_SYN_SENT:
2463 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2464 /* Send ACK immediately */
2465 n->ack_state = ACK_SEND_ACK;
2466 send_syn_ack_message (&n->primary_address,
2469 case GNUNET_TRANSPORT_PS_CONNECTED:
2470 /* we are already connected and can thus send the ACK immediately */
2471 GNUNET_assert (NULL != n->primary_address.address);
2472 GNUNET_assert (NULL != n->primary_address.session);
2473 n->ack_state = ACK_SEND_ACK;
2474 send_syn_ack_message (&n->primary_address,
2477 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2478 /* We wait for ATS address suggestion */
2480 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2481 /* We received a SYN message while waiting for a SYN_ACK in fast
2482 * reconnect. Send SYN_ACK immediately */
2483 n->ack_state = ACK_SEND_ACK;
2484 send_syn_ack_message (&n->primary_address,
2485 n->connect_ack_timestamp);
2487 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2488 /* We are already connected and can thus send the ACK immediately;
2489 still, it can never hurt to have an alternative address, so also
2490 tell ATS about it */
2491 GNUNET_assert (NULL != n->primary_address.address);
2492 GNUNET_assert (NULL != n->primary_address.session);
2493 n->ack_state = ACK_SEND_ACK;
2494 send_syn_ack_message (&n->primary_address,
2497 case GNUNET_TRANSPORT_PS_DISCONNECT:
2498 /* Get rid of remains and re-try */
2500 n = setup_neighbour (peer);
2501 /* Remember the SYN time stamp for ACK message */
2502 n->ack_state = ACK_SEND_SYN_ACK;
2503 n->connect_ack_timestamp = ts;
2504 /* Request an address for the peer */
2505 set_state_and_timeout (n,
2506 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2507 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2509 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2510 /* should not be possible */
2514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2515 "Unhandled state `%s'\n",
2516 GNUNET_TRANSPORT_ps2s (n->state));
2518 return GNUNET_SYSERR;
2525 * Check if the given @a address is the same that we are already
2526 * using for the respective neighbour. If so, update the bandwidth
2527 * assignment and possibly the session and return #GNUNET_OK.
2528 * If the new address is different from what the neighbour is
2529 * using right now, return #GNUNET_NO.
2531 * @param address address of the other peer,
2532 * @param session session to use or NULL if transport should initiate a session
2533 * @param bandwidth_in inbound quota to be used when connection is up,
2534 * 0 to disconnect from peer
2535 * @param bandwidth_out outbound quota to be used when connection is up,
2536 * 0 to disconnect from peer
2537 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2538 * #GNUNET_NO if more extensive changes are required (address changed)
2541 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2542 struct GNUNET_ATS_Session *session,
2543 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2544 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2546 struct NeighbourMapEntry *n;
2548 n = lookup_neighbour (&address->peer);
2550 (NULL == n->primary_address.address) ||
2551 (0 != GNUNET_HELLO_address_cmp (address,
2552 n->primary_address.address)) )
2554 /* We are not really switching addresses, but merely adjusting
2555 session and/or bandwidth, can do fast ATS update! */
2556 if (session != n->primary_address.session)
2558 /* switch to a different session, but keeping same address; could
2559 happen if there is a 2nd inbound connection */
2560 n->primary_address.session = session;
2561 GNUNET_assert (GNUNET_YES ==
2562 GST_ats_is_known (n->primary_address.address,
2563 n->primary_address.session));
2565 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
2567 n->primary_address.bandwidth_in = bandwidth_in;
2568 set_incoming_quota (n,
2571 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
2573 n->primary_address.bandwidth_out = bandwidth_out;
2574 send_outbound_quota_to_clients (n);
2581 * We've been asked to switch addresses, and just now got the result
2582 * from the blacklist check to see if this is allowed.
2584 * @param cls the `struct BlacklistCheckSwitchContext` with
2585 * the information about the future address
2586 * @param peer the peer we may switch addresses on
2587 * @param address address associated with the request
2588 * @param session session associated with the request
2589 * @param result #GNUNET_OK if the connection is allowed,
2590 * #GNUNET_NO if not,
2591 * #GNUNET_SYSERR if operation was aborted
2594 switch_address_bl_check_cont (void *cls,
2595 const struct GNUNET_PeerIdentity *peer,
2596 const struct GNUNET_HELLO_Address *address,
2597 struct GNUNET_ATS_Session *session,
2600 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2601 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2602 struct NeighbourMapEntry *n;
2604 if (GNUNET_SYSERR == result)
2607 papi = GST_plugins_find (address->transport_name);
2608 GNUNET_assert (NULL != papi);
2610 if (GNUNET_NO == result)
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2613 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2614 GST_plugins_a2s (address),
2617 GNUNET_STATISTICS_update (GST_stats,
2618 "# ATS suggestions ignored (blacklist denied)",
2621 if (NULL != session)
2622 papi->disconnect_session (papi->cls,
2625 GNUNET_HELLO_address_check_option (address,
2626 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2627 GST_ats_block_address (address,
2633 if (NULL == session)
2635 /* need to create a session, ATS only gave us an address */
2636 session = papi->get_session (papi->cls,
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2639 "Obtained new session for peer `%s' and address '%s': %p\n",
2640 GNUNET_i2s (&address->peer),
2641 GST_plugins_a2s (address),
2643 if (NULL != session)
2644 GST_ats_new_session (address,
2647 if (NULL == session)
2649 /* session creation failed, bad!, fail! */
2650 GNUNET_STATISTICS_update (GST_stats,
2651 "# ATS suggestions ignored (failed to create session)",
2654 /* No session could be obtained, remove blacklist check and clean up */
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2656 "Failed to obtain new session for peer `%s' and address '%s'\n",
2657 GNUNET_i2s (&address->peer),
2658 GST_plugins_a2s (address));
2659 GST_ats_block_address (address,
2664 /* We did this check already before going into blacklist, but
2665 it is theoretically possible that the situation changed in
2666 the meantime, hence we check again here */
2668 try_run_fast_ats_update (address,
2670 blc_ctx->bandwidth_in,
2671 blc_ctx->bandwidth_out))
2672 goto cleanup; /* was just a minor update, we're done */
2674 /* check if we also need to setup the neighbour entry */
2675 if (NULL == (n = lookup_neighbour (peer)))
2677 n = setup_neighbour (peer);
2678 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2681 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2682 "Peer `%s' switches to address `%s'\n",
2683 GNUNET_i2s (&address->peer),
2684 GST_plugins_a2s (address));
2688 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2690 GST_ats_block_address (address,
2694 case GNUNET_TRANSPORT_PS_INIT_ATS:
2695 /* We requested an address and ATS suggests one:
2696 * set primary address and send SYN message*/
2697 set_primary_address (n,
2700 blc_ctx->bandwidth_in,
2701 blc_ctx->bandwidth_out);
2702 if (ACK_SEND_SYN_ACK == n->ack_state)
2704 /* Send pending SYN_ACK message */
2705 n->ack_state = ACK_SEND_ACK;
2706 send_syn_ack_message (&n->primary_address,
2707 n->connect_ack_timestamp);
2709 set_state_and_timeout (n,
2710 GNUNET_TRANSPORT_PS_SYN_SENT,
2711 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2712 send_syn (&n->primary_address);
2714 case GNUNET_TRANSPORT_PS_SYN_SENT:
2715 /* ATS suggested a new address while waiting for an SYN_ACK:
2716 * Switch and send new SYN */
2717 /* ATS suggests a different address, switch again */
2718 set_primary_address (n,
2721 blc_ctx->bandwidth_in,
2722 blc_ctx->bandwidth_out);
2723 if (ACK_SEND_SYN_ACK == n->ack_state)
2725 /* Send pending SYN_ACK message */
2726 n->ack_state = ACK_SEND_ACK;
2727 send_syn_ack_message (&n->primary_address,
2728 n->connect_ack_timestamp);
2730 set_state_and_timeout (n,
2731 GNUNET_TRANSPORT_PS_SYN_SENT,
2732 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2733 send_syn (&n->primary_address);
2735 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2736 /* We requested an address and ATS suggests one:
2737 * set primary address and send SYN_ACK message*/
2738 set_primary_address (n,
2741 blc_ctx->bandwidth_in,
2742 blc_ctx->bandwidth_out);
2743 /* Send an ACK message as a response to the SYN msg */
2744 set_state_and_timeout (n,
2745 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2746 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2747 send_syn_ack_message (&n->primary_address,
2748 n->connect_ack_timestamp);
2749 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2750 (ACK_UNDEFINED == n->ack_state) )
2751 n->ack_state = ACK_SEND_ACK;
2753 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2754 /* ATS asks us to switch while we were trying to connect; switch to new
2755 address and check blacklist again */
2756 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2758 n->ack_state = ACK_SEND_ACK;
2759 send_syn_ack_message (&n->primary_address,
2760 n->connect_ack_timestamp);
2762 set_primary_address (n,
2765 blc_ctx->bandwidth_in,
2766 blc_ctx->bandwidth_out);
2767 set_state_and_timeout (n,
2768 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2769 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2771 case GNUNET_TRANSPORT_PS_CONNECTED:
2772 GNUNET_assert (NULL != n->primary_address.address);
2773 GNUNET_assert (NULL != n->primary_address.session);
2774 GNUNET_break (n->primary_address.session != session);
2775 /* ATS asks us to switch a life connection; see if we can get
2776 a SYN_ACK on it before we actually do this! */
2777 set_alternative_address (n,
2780 blc_ctx->bandwidth_in,
2781 blc_ctx->bandwidth_out);
2782 set_state_and_timeout (n,
2783 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2784 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2785 GNUNET_STATISTICS_update (GST_stats,
2786 gettext_noop ("# Attempts to switch addresses"),
2789 send_syn (&n->alternative_address);
2791 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2792 set_primary_address (n,
2795 blc_ctx->bandwidth_in,
2796 blc_ctx->bandwidth_out);
2797 if (ACK_SEND_SYN_ACK == n->ack_state)
2799 /* Send pending SYN_ACK message */
2800 n->ack_state = ACK_SEND_ACK;
2801 send_syn_ack_message (&n->primary_address,
2802 n->connect_ack_timestamp);
2804 set_state_and_timeout (n,
2805 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2806 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2807 send_syn (&n->primary_address);
2809 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2810 /* ATS asks us to switch while we were trying to reconnect; switch to new
2811 address and send SYN again */
2812 set_primary_address (n,
2815 blc_ctx->bandwidth_in,
2816 blc_ctx->bandwidth_out);
2817 set_state_and_timeout (n,
2818 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2819 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2820 send_syn (&n->primary_address);
2822 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2823 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2825 (n->primary_address.session == session) )
2827 /* ATS switches back to still-active session */
2828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2829 "ATS double-switched, cleaning up alternative address\n");
2830 free_address (&n->alternative_address);
2831 set_state_and_timeout (n,
2832 GNUNET_TRANSPORT_PS_CONNECTED,
2836 /* ATS asks us to switch a life connection, send */
2837 set_alternative_address (n,
2840 blc_ctx->bandwidth_in,
2841 blc_ctx->bandwidth_out);
2842 set_state_and_timeout (n,
2843 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2844 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2845 send_syn (&n->alternative_address);
2847 case GNUNET_TRANSPORT_PS_DISCONNECT:
2848 /* not going to switch addresses while disconnecting */
2849 GNUNET_STATISTICS_update (GST_stats,
2850 "# ATS suggestion ignored (disconnecting)",
2854 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2858 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2859 "Unhandled state `%s'\n",
2860 GNUNET_TRANSPORT_ps2s (n->state));
2865 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2868 GNUNET_free (blc_ctx);
2873 * For the given peer, switch to this address.
2875 * Before accepting this addresses and actively using it, a blacklist check
2878 * If any check fails or the suggestion can somehow not be followed, we
2879 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2880 * could not be satisfied and force ATS to do something else.
2882 * @param address address of the other peer,
2883 * @param session session to use or NULL if transport should initiate a session
2884 * @param bandwidth_in inbound quota to be used when connection is up,
2885 * 0 to disconnect from peer
2886 * @param bandwidth_out outbound quota to be used when connection is up,
2887 * 0 to disconnect from peer
2890 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2891 struct GNUNET_ATS_Session *session,
2892 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2893 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2895 struct GST_BlacklistCheck *blc;
2896 struct BlacklistCheckSwitchContext *blc_ctx;
2898 GNUNET_assert (NULL != address->transport_name);
2900 try_run_fast_ats_update (address,
2906 /* Check if plugin is available */
2907 if (NULL == (GST_plugins_find (address->transport_name)))
2909 /* we don't have the plugin for this address */
2911 GST_ats_block_address (address,
2915 if ((NULL == session) &&
2916 (GNUNET_HELLO_address_check_option (address,
2917 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2919 /* This is a inbound address and we do not have a session to use! */
2921 GST_ats_block_address (address,
2926 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2927 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2928 GST_plugins_a2s (address),
2929 GNUNET_i2s (&address->peer),
2930 (unsigned int) ntohl (bandwidth_in.value__),
2931 (unsigned int) ntohl (bandwidth_out.value__));
2933 /* Perform blacklist check */
2934 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2935 blc_ctx->bandwidth_in = bandwidth_in;
2936 blc_ctx->bandwidth_out = bandwidth_out;
2937 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2940 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2941 address->transport_name,
2942 &switch_address_bl_check_cont,
2953 * Function called to send network utilization data to ATS for
2954 * each active connection.
2957 * @param key peer we send utilization data for
2958 * @param value the `struct NeighbourMapEntry *` with data to send
2959 * @return #GNUNET_OK (continue to iterate)
2962 send_utilization_data (void *cls,
2963 const struct GNUNET_PeerIdentity *key,
2966 struct NeighbourMapEntry *n = value;
2969 struct GNUNET_TIME_Relative delta;
2971 if ( (GNUNET_YES != test_connected (n)) ||
2972 (NULL == n->primary_address.address) )
2974 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2975 GNUNET_TIME_absolute_get ());
2977 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2978 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2980 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2981 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2984 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2988 GST_ats_update_utilization (n->primary_address.address,
2991 n->util_total_bytes_recv = 0;
2992 n->util_total_bytes_sent = 0;
2993 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2999 * Task transmitting utilization in a regular interval
3001 * @param cls the 'struct NeighbourMapEntry' for which we are running
3002 * @param tc scheduler context (unused)
3005 utilization_transmission (void *cls,
3006 const struct GNUNET_SCHEDULER_TaskContext *tc)
3008 util_transmission_tk = NULL;
3009 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3010 &send_utilization_data,
3012 util_transmission_tk
3013 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3014 &utilization_transmission,
3020 * Track information about data we received from the
3021 * given address (used to notify ATS about our utilization
3022 * of allocated resources).
3024 * @param address the address we got data from
3025 * @param message the message we received (really only the size is used)
3028 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
3029 const struct GNUNET_MessageHeader *message)
3031 struct NeighbourMapEntry *n;
3033 n = lookup_neighbour (&address->peer);
3036 n->util_total_bytes_recv += ntohs (message->size);
3041 * Track information about data we transmitted using the given @a
3042 * address and @a session (used to notify ATS about our utilization of
3043 * allocated resources).
3045 * @param address the address we transmitted data to
3046 * @param session session we used to transmit data
3047 * @param message the message we sent (really only the size is used)
3050 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
3051 struct GNUNET_ATS_Session *session,
3054 struct NeighbourMapEntry *n;
3056 n = lookup_neighbour (&address->peer);
3059 if (n->primary_address.session != session)
3061 n->util_total_bytes_sent += size;
3066 * Master task run for every neighbour. Performs all of the time-related
3067 * activities (keep alive, send next message, disconnect if idle, finish
3068 * clean up after disconnect).
3070 * @param cls the 'struct NeighbourMapEntry' for which we are running
3071 * @param tc scheduler context (unused)
3074 master_task (void *cls,
3075 const struct GNUNET_SCHEDULER_TaskContext *tc)
3077 struct NeighbourMapEntry *n = cls;
3078 struct GNUNET_TIME_Relative delay;
3081 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3083 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3084 GNUNET_i2s (&n->id),
3085 GNUNET_TRANSPORT_ps2s(n->state),
3086 GNUNET_STRINGS_relative_time_to_string (delay,
3090 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3091 /* invalid state for master task, clean up */
3095 case GNUNET_TRANSPORT_PS_INIT_ATS:
3096 if (0 == delay.rel_value_us)
3098 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3099 "Connection to `%s' timed out waiting for ATS to provide address\n",
3100 GNUNET_i2s (&n->id));
3105 case GNUNET_TRANSPORT_PS_SYN_SENT:
3106 if (0 == delay.rel_value_us)
3108 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3109 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
3110 GNUNET_i2s (&n->id));
3111 /* Remove address and request and additional one */
3112 unset_primary_address (n);
3113 set_state_and_timeout (n,
3114 GNUNET_TRANSPORT_PS_INIT_ATS,
3115 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3119 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3120 if (0 == delay.rel_value_us)
3122 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3123 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
3124 GNUNET_i2s (&n->id));
3129 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3130 if (0 == delay.rel_value_us)
3132 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3133 "Connection to `%s' timed out waiting for other peer to send ACK\n",
3134 GNUNET_i2s (&n->id));
3135 disconnect_neighbour (n);
3139 case GNUNET_TRANSPORT_PS_CONNECTED:
3140 if (0 == delay.rel_value_us)
3142 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3143 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3144 GNUNET_i2s (&n->id));
3145 disconnect_neighbour (n);
3148 try_transmission_to_peer (n);
3151 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3152 if (0 == delay.rel_value_us)
3154 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3155 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3156 GNUNET_i2s (&n->id));
3157 disconnect_neighbour (n);
3161 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3162 if (0 == delay.rel_value_us)
3164 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3165 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
3166 GNUNET_i2s (&n->id));
3167 disconnect_neighbour (n);
3171 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3172 if (0 == delay.rel_value_us)
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3175 "Switch failed, cleaning up alternative address\n");
3176 free_address (&n->alternative_address);
3177 set_state_and_timeout (n,
3178 GNUNET_TRANSPORT_PS_CONNECTED,
3179 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
3181 try_transmission_to_peer (n);
3184 case GNUNET_TRANSPORT_PS_DISCONNECT:
3185 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3186 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3187 GNUNET_i2s (&n->id));
3190 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3191 /* how did we get here!? */
3195 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3196 "Unhandled state `%s'\n",
3197 GNUNET_TRANSPORT_ps2s (n->state));
3201 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3202 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3203 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3205 /* if we are *now* in one of the two states, we're sending
3206 keep alive messages, so we need to consider the keepalive
3207 delay, not just the connection timeout */
3208 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3211 if (NULL == n->task)
3212 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3219 * Send a ACK message to the neighbour to confirm that we
3222 * @param n neighbour to send the ACK to
3225 send_session_ack_message (struct NeighbourMapEntry *n)
3227 struct GNUNET_MessageHeader msg;
3229 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3230 "Sending ACK message to peer `%s'\n",
3231 GNUNET_i2s (&n->id));
3233 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3234 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3235 (void) send_with_session (n,
3237 sizeof (struct GNUNET_MessageHeader),
3239 GNUNET_TIME_UNIT_FOREVER_REL,
3246 * We received a 'SESSION_SYN_ACK' message from the other peer.
3247 * Consider switching to it.
3249 * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
3250 * @param peer identity of the peer to switch the address for
3251 * @param address address of the other peer, NULL if other peer
3253 * @param session session to use (or NULL)
3254 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3257 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3258 const struct GNUNET_HELLO_Address *address,
3259 struct GNUNET_ATS_Session *session)
3261 const struct TransportSynMessage *scm;
3262 struct GNUNET_TIME_Absolute ts;
3263 struct NeighbourMapEntry *n;
3265 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3267 GNUNET_break_op (0);
3268 return GNUNET_SYSERR;
3270 GNUNET_STATISTICS_update (GST_stats,
3272 ("# SYN_ACK messages received"),
3274 scm = (const struct TransportSynMessage *) message;
3275 GNUNET_break_op (ntohl (scm->reserved) == 0);
3276 if (NULL == (n = lookup_neighbour (&address->peer)))
3278 GNUNET_STATISTICS_update (GST_stats,
3280 ("# unexpected SYN_ACK messages (no peer)"),
3282 return GNUNET_SYSERR;
3284 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3285 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3286 GNUNET_i2s (&address->peer),
3287 GNUNET_TRANSPORT_ps2s (n->state),
3288 print_ack_state (n->ack_state));
3289 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3292 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3295 return GNUNET_SYSERR;
3296 case GNUNET_TRANSPORT_PS_INIT_ATS:
3297 GNUNET_STATISTICS_update (GST_stats,
3298 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3302 case GNUNET_TRANSPORT_PS_SYN_SENT:
3303 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3305 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3306 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3309 set_state_and_timeout (n,
3310 GNUNET_TRANSPORT_PS_CONNECTED,
3311 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3312 set_primary_address (n,
3313 n->primary_address.address,
3314 n->primary_address.session,
3315 n->primary_address.bandwidth_in,
3316 n->primary_address.bandwidth_out);
3317 send_session_ack_message (n);
3319 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3320 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3321 GNUNET_STATISTICS_update (GST_stats,
3322 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3326 case GNUNET_TRANSPORT_PS_CONNECTED:
3327 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3328 send_session_ack_message (n);
3330 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3331 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3332 to give us a new address... */
3333 GNUNET_STATISTICS_update (GST_stats,
3334 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3338 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3339 /* Reconnecting with new address address worked; go back to connected! */
3340 set_state_and_timeout (n,
3341 GNUNET_TRANSPORT_PS_CONNECTED,
3342 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3343 send_session_ack_message (n);
3345 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3346 /* new address worked; adopt it and go back to connected! */
3347 set_state_and_timeout (n,
3348 GNUNET_TRANSPORT_PS_CONNECTED,
3349 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3350 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3352 /* Set primary addresses */
3353 set_primary_address (n,
3354 n->alternative_address.address,
3355 n->alternative_address.session,
3356 n->alternative_address.bandwidth_in,
3357 n->alternative_address.bandwidth_out);
3358 GNUNET_STATISTICS_update (GST_stats,
3359 gettext_noop ("# Successful attempts to switch addresses"),
3363 GNUNET_HELLO_address_free (n->alternative_address.address);
3364 memset (&n->alternative_address,
3366 sizeof (n->alternative_address));
3367 send_session_ack_message (n);
3369 case GNUNET_TRANSPORT_PS_DISCONNECT:
3370 GNUNET_STATISTICS_update (GST_stats,
3372 ("# unexpected SYN_ACK messages (disconnecting)"),
3374 return GNUNET_SYSERR;
3375 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3379 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3380 "Unhandled state `%s'\n",
3381 GNUNET_TRANSPORT_ps2s (n->state));
3383 return GNUNET_SYSERR;
3390 * A session was terminated. Take note; if needed, try to get
3391 * an alternative address from ATS.
3393 * @param peer identity of the peer where the session died
3394 * @param session session that is gone
3395 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3396 * this session was not in use
3399 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3400 struct GNUNET_ATS_Session *session)
3402 struct NeighbourMapEntry *n;
3403 struct BlackListCheckContext *bcc;
3404 struct BlackListCheckContext *bcc_next;
3406 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3408 while (NULL != (bcc = bcc_next))
3410 bcc_next = bcc->next;
3411 if (bcc->na.session == session)
3413 if (NULL != bcc->bc)
3414 GST_blacklist_test_cancel (bcc->bc);
3415 GNUNET_HELLO_address_free (bcc->na.address);
3416 GNUNET_CONTAINER_DLL_remove (bc_head,
3422 if (NULL == (n = lookup_neighbour (peer)))
3423 return GNUNET_NO; /* can't affect us */
3424 if (session != n->primary_address.session)
3426 /* Free alternative address */
3427 if (session == n->alternative_address.session)
3429 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3430 set_state_and_timeout (n,
3431 GNUNET_TRANSPORT_PS_CONNECTED,
3433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3434 "Session died, cleaning up alternative address\n");
3435 free_address (&n->alternative_address);
3437 return GNUNET_NO; /* doesn't affect us further */
3440 n->expect_latency_response = GNUNET_NO;
3441 /* The session for neighbour's primary address died */
3444 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3448 case GNUNET_TRANSPORT_PS_INIT_ATS:
3452 case GNUNET_TRANSPORT_PS_SYN_SENT:
3453 /* The session used to send the SYN terminated:
3454 * this implies a connect error*/
3455 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3456 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3458 GST_plugins_a2s (n->primary_address.address),
3459 n->primary_address.session,
3462 /* Destroy the address since it cannot be used */
3463 unset_primary_address (n);
3464 set_state_and_timeout (n,
3465 GNUNET_TRANSPORT_PS_INIT_ATS,
3466 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3468 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3469 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3470 /* error on inbound session; free neighbour entirely */
3473 case GNUNET_TRANSPORT_PS_CONNECTED:
3474 /* Our primary connection died, try a fast reconnect */
3475 unset_primary_address (n);
3476 set_state_and_timeout (n,
3477 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3478 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3480 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3481 /* we don't have an address, how can it go down? */
3484 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3485 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3486 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3488 GST_plugins_a2s (n->primary_address.address),
3489 n->primary_address.session,
3491 /* Destroy the address since it cannot be used */
3492 unset_primary_address (n);
3493 set_state_and_timeout (n,
3494 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3495 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3497 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3498 /* primary went down while we were waiting for SYN_ACK on secondary;
3499 secondary as primary */
3501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3502 "Connection `%s' %p to peer `%s' was terminated while switching, "
3503 "switching to alternative address `%s' %p\n",
3504 GST_plugins_a2s (n->primary_address.address),
3505 n->primary_address.session,
3507 GST_plugins_a2s (n->alternative_address.address),
3508 n->alternative_address.session);
3510 /* Destroy the inbound address since it cannot be used */
3511 free_address (&n->primary_address);
3512 n->primary_address = n->alternative_address;
3513 GNUNET_assert (GNUNET_YES ==
3514 GST_ats_is_known (n->primary_address.address,
3515 n->primary_address.session));
3516 memset (&n->alternative_address,
3518 sizeof (struct NeighbourAddress));
3519 set_state_and_timeout (n,
3520 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3521 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3523 case GNUNET_TRANSPORT_PS_DISCONNECT:
3524 unset_primary_address (n);
3526 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3527 /* neighbour was freed and plugins told to terminate session */
3530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3531 "Unhandled state `%s'\n",
3532 GNUNET_TRANSPORT_ps2s (n->state));
3536 if (NULL != n->task)
3537 GNUNET_SCHEDULER_cancel (n->task);
3538 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3544 * We received a 'ACK' message from the other peer.
3545 * If we sent a 'SYN_ACK' last, this means we are now
3546 * connected. Otherwise, do nothing.
3548 * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
3549 * @param address address of the other peer
3550 * @param session session to use (or NULL)
3551 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3554 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3555 const struct GNUNET_HELLO_Address *address,
3556 struct GNUNET_ATS_Session *session)
3558 struct NeighbourMapEntry *n;
3560 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3562 GNUNET_break_op (0);
3563 return GNUNET_SYSERR;
3565 GNUNET_STATISTICS_update (GST_stats,
3566 gettext_noop ("# ACK messages received"),
3569 if (NULL == (n = lookup_neighbour (&address->peer)))
3571 GNUNET_break_op (0);
3572 return GNUNET_SYSERR;
3574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3575 "Received ACK for peer `%s' in state %s/%s\n",
3576 GNUNET_i2s (&address->peer),
3577 GNUNET_TRANSPORT_ps2s (n->state),
3578 print_ack_state (n->ack_state));
3580 /* Check if we are in a plausible state for having sent
3581 a SYN_ACK. If not, return, otherwise break.
3583 The remote peers sends a ACK as a response for a SYN_ACK
3587 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3588 now wait for the ACK to finally be connected
3589 - If we sent a SYN_ACK to this peer before */
3591 if ( ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3592 (ACK_SEND_ACK != n->ack_state) ) ||
3593 (NULL == n->primary_address.address) )
3595 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3596 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3597 GNUNET_i2s (&address->peer),
3598 GNUNET_TRANSPORT_ps2s (n->state),
3599 print_ack_state (n->ack_state));
3601 GNUNET_STATISTICS_update (GST_stats,
3602 gettext_noop ("# unexpected ACK messages"),
3607 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3609 /* We tried to switch addresses while being connect. We explicitly wait
3610 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3611 * so we do not want to set the address as in use! */
3614 set_state_and_timeout (n,
3615 GNUNET_TRANSPORT_PS_CONNECTED,
3616 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3618 if (NULL == n->primary_address.address) {
3620 * We are in state = PSY_SYN_RECV_ACK or ack_state = ACK_SEND_ACK, which
3621 * really means we did try (and succeed) to send a SYN and are waiting for
3623 * That suggests that the primary_address used to be non-NULL, but maybe it
3624 * got reset to NULL without the state being changed appropriately?
3630 /* Reset backoff for primary address */
3631 GST_ats_block_reset (n->primary_address.address,
3632 n->primary_address.session);
3638 * Test if we're connected to the given peer.
3640 * @param target peer to test
3641 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3644 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3646 return test_connected (lookup_neighbour (target));
3651 * Task to asynchronously run #free_neighbour().
3653 * @param cls the `struct NeighbourMapEntry` to free
3657 delayed_disconnect (void *cls,
3658 const struct GNUNET_SCHEDULER_TaskContext* tc)
3660 struct NeighbourMapEntry *n = cls;
3662 n->delayed_disconnect_task = NULL;
3663 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3664 "Disconnecting by request from peer %s\n",
3665 GNUNET_i2s (&n->id));
3671 * We received a quota message from the given peer,
3672 * validate and process.
3674 * @param peer sender of the message
3675 * @param msg the quota message
3678 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
3679 const struct GNUNET_MessageHeader *msg)
3681 struct NeighbourMapEntry *n;
3682 const struct GNUNET_ATS_SessionQuotaMessage *sqm;
3684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3685 "Received QUOTA message from peer `%s'\n",
3687 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionQuotaMessage))
3689 GNUNET_break_op (0);
3690 GNUNET_STATISTICS_update (GST_stats,
3691 gettext_noop ("# quota messages ignored (malformed)"),
3696 GNUNET_STATISTICS_update (GST_stats,
3698 ("# QUOTA messages received"),
3700 sqm = (const struct GNUNET_ATS_SessionQuotaMessage *) msg;
3701 if (NULL == (n = lookup_neighbour (peer)))
3706 n->neighbour_receive_quota
3707 = GNUNET_BANDWIDTH_value_max (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3708 GNUNET_BANDWIDTH_value_init (ntohl (sqm->quota)));
3709 send_outbound_quota_to_clients (n);
3714 * We received a disconnect message from the given peer,
3715 * validate and process.
3717 * @param peer sender of the message
3718 * @param msg the disconnect message
3721 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3722 const struct GNUNET_MessageHeader *msg)
3724 struct NeighbourMapEntry *n;
3725 const struct GNUNET_ATS_SessionDisconnectMessage *sdm;
3727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3728 "Received DISCONNECT message from peer `%s'\n",
3730 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionDisconnectMessage))
3732 GNUNET_break_op (0);
3733 GNUNET_STATISTICS_update (GST_stats,
3735 ("# disconnect messages ignored (malformed)"),
3740 GNUNET_STATISTICS_update (GST_stats,
3742 ("# DISCONNECT messages received"),
3744 sdm = (const struct GNUNET_ATS_SessionDisconnectMessage *) msg;
3745 if (NULL == (n = lookup_neighbour (peer)))
3750 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3752 GNUNET_STATISTICS_update (GST_stats,
3753 gettext_noop ("# disconnect messages ignored (timestamp)"),
3758 if (0 != memcmp (peer,
3760 sizeof (struct GNUNET_PeerIdentity)))
3762 GNUNET_break_op (0);
3765 if (ntohl (sdm->purpose.size) !=
3766 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3767 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3768 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3771 "DISCONNECT message from peer `%s' has invalid size\n",
3773 GNUNET_break_op (0);
3777 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3783 "DISCONNECT message from peer `%s' cannot be verified \n",
3785 GNUNET_break_op (0);
3788 if (NULL == n->delayed_disconnect_task)
3790 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect,
3797 * Closure for the #neighbours_iterate() function.
3799 struct IteratorContext
3802 * Function to call on each connected neighbour.
3804 GST_NeighbourIterator cb;
3807 * Closure for @e cb.
3814 * Call the callback from the closure for each neighbour.
3816 * @param cls the `struct IteratorContext`
3817 * @param key the hash of the public key of the neighbour
3818 * @param value the `struct NeighbourMapEntry`
3819 * @return #GNUNET_OK (continue to iterate)
3822 neighbours_iterate (void *cls,
3823 const struct GNUNET_PeerIdentity *key,
3826 struct IteratorContext *ic = cls;
3827 struct NeighbourMapEntry *n = value;
3828 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3829 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3831 if (NULL != n->primary_address.address)
3833 bandwidth_in = n->primary_address.bandwidth_in;
3834 bandwidth_out = n->primary_address.bandwidth_out;
3838 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3839 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3843 n->primary_address.address,
3846 bandwidth_in, bandwidth_out);
3852 * Iterate over all connected neighbours.
3854 * @param cb function to call
3855 * @param cb_cls closure for @a cb
3858 GST_neighbours_iterate (GST_NeighbourIterator cb,
3861 struct IteratorContext ic;
3863 if (NULL == neighbours)
3864 return; /* can happen during shutdown */
3867 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3868 &neighbours_iterate,
3874 * If we have an active connection to the given target, it must be shutdown.
3876 * @param target peer to disconnect from
3879 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3881 struct NeighbourMapEntry *n;
3883 if (NULL == (n = lookup_neighbour (target)))
3884 return; /* not active */
3885 if (GNUNET_YES == test_connected (n))
3886 GNUNET_STATISTICS_update (GST_stats,
3887 gettext_noop ("# disconnected from peer upon explicit request"),
3890 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3891 "Forced disconnect from peer %s\n",
3892 GNUNET_i2s (target));
3893 disconnect_neighbour (n);
3898 * Obtain current address information for the given neighbour.
3901 * @return address currently used
3903 const struct GNUNET_HELLO_Address *
3904 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3906 struct NeighbourMapEntry *n;
3908 n = lookup_neighbour (peer);
3911 return n->primary_address.address;
3916 * Initialize the neighbours subsystem.
3918 * @param max_fds maximum number of fds to use
3921 GST_neighbours_start (unsigned int max_fds)
3923 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3925 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3926 &utilization_transmission,
3932 * Disconnect from the given neighbour.
3935 * @param key hash of neighbour's public key (not used)
3936 * @param value the `struct NeighbourMapEntry` of the neighbour
3937 * @return #GNUNET_OK (continue to iterate)
3940 disconnect_all_neighbours (void *cls,
3941 const struct GNUNET_PeerIdentity *key,
3944 struct NeighbourMapEntry *n = value;
3946 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3947 "Disconnecting peer `%4s' during shutdown\n",
3948 GNUNET_i2s (&n->id));
3955 * Cleanup the neighbours subsystem.
3958 GST_neighbours_stop ()
3960 struct BlacklistCheckSwitchContext *cur;
3961 struct BlacklistCheckSwitchContext *next;
3963 if (NULL == neighbours)
3965 if (NULL != util_transmission_tk)
3967 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3968 util_transmission_tk = NULL;
3970 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3971 &disconnect_all_neighbours,
3973 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3975 next = pending_bc_head;
3976 for (cur = next; NULL != cur; cur = next)
3979 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
3983 if (NULL != cur->blc)
3985 GST_blacklist_test_cancel (cur->blc);
3993 /* end of file gnunet-service-transport_neighbours.c */