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",
2196 n = GNUNET_new (struct NeighbourMapEntry);
2198 n->ack_state = ACK_UNDEFINED;
2199 n->last_util_transmission = GNUNET_TIME_absolute_get();
2200 n->neighbour_receive_quota = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
2201 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2202 &inbound_bw_tracker_update,
2204 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2205 MAX_BANDWIDTH_CARRY_S);
2206 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2207 set_state_and_timeout (n,
2208 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2209 GNUNET_TIME_UNIT_FOREVER_ABS);
2210 GNUNET_assert (GNUNET_OK ==
2211 GNUNET_CONTAINER_multipeermap_put (neighbours,
2214 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2215 n->suggest_handle = GNUNET_ATS_connectivity_suggest (GST_ats_connect,
2224 * Entry in a DLL we use to keep track of pending blacklist checks.
2226 struct BlacklistCheckSwitchContext
2231 struct BlacklistCheckSwitchContext *prev;
2236 struct BlacklistCheckSwitchContext *next;
2239 * Handle to the blacklist check we are performing.
2241 struct GST_BlacklistCheck *blc;
2244 * Inbound bandwidth that was assigned to @e address.
2246 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2249 * Outbound bandwidth that was assigned to @e address.
2251 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2256 * Black list check result for try_connect call
2257 * If connection to the peer is allowed request adddress and
2259 * @param cls blc_ctx bl context
2260 * @param peer the peer
2261 * @param address address associated with the request
2262 * @param session session associated with the request
2263 * @param result #GNUNET_OK if the connection is allowed,
2264 * #GNUNET_NO if not,
2265 * #GNUNET_SYSERR if operation was aborted
2268 try_connect_bl_check_cont (void *cls,
2269 const struct GNUNET_PeerIdentity *peer,
2270 const struct GNUNET_HELLO_Address *address,
2271 struct GNUNET_ATS_Session *session,
2274 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2275 struct NeighbourMapEntry *n;
2277 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2280 GNUNET_free (blc_ctx);
2281 if (GNUNET_OK != result)
2283 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2284 _("Blacklisting disapproved to connect to peer `%s'\n"),
2289 /* Setup a new neighbour */
2290 if (NULL != lookup_neighbour(peer))
2291 return; /* The neighbor was created in the meantime while waited for BL clients */
2293 n = setup_neighbour (peer);
2295 /* Request address suggestions for this peer */
2296 set_state_and_timeout (n,
2297 GNUNET_TRANSPORT_PS_INIT_ATS,
2298 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2303 * Try to create a connection to the given target (eventually).
2305 * @param target peer to try to connect to
2308 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2310 struct NeighbourMapEntry *n;
2311 struct GST_BlacklistCheck *blc;
2312 struct BlacklistCheckSwitchContext *blc_ctx;
2314 if (NULL == neighbours)
2316 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2317 "Asked to connect to peer `%s' during shutdown\n",
2318 GNUNET_i2s (target));
2319 return; /* during shutdown, do nothing */
2321 n = lookup_neighbour (target);
2322 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2323 "Asked to connect to peer `%s' (state: %s)\n",
2324 GNUNET_i2s (target),
2325 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2330 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2331 /* this should not be possible */
2335 case GNUNET_TRANSPORT_PS_INIT_ATS:
2336 case GNUNET_TRANSPORT_PS_SYN_SENT:
2337 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2338 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2339 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2340 "Ignoring request to try to connect to `%s', already trying!\n",
2341 GNUNET_i2s (target));
2342 return; /* already trying */
2343 case GNUNET_TRANSPORT_PS_CONNECTED:
2344 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2345 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2346 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2347 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2348 "Ignoring request to try to connect, already connected to `%s'!\n",
2349 GNUNET_i2s (target));
2350 return; /* already connected */
2351 case GNUNET_TRANSPORT_PS_DISCONNECT:
2352 /* get rid of remains, ready to re-try immediately */
2355 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2356 /* should not be possible */
2360 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2361 "Unhandled state `%s'\n",
2362 GNUNET_TRANSPORT_ps2s (n->state));
2369 /* Do blacklist check if connecting to this peer is allowed */
2370 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2371 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2376 (blc = GST_blacklist_test_allowed (target,
2378 &try_connect_bl_check_cont,
2389 * We received a 'SYN' message from the other peer.
2390 * Consider switching to it.
2392 * @param message possibly a `struct TransportSynMessage` (check format)
2393 * @param peer identity of the peer to switch the address for
2394 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2397 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2398 const struct GNUNET_PeerIdentity *peer)
2400 const struct TransportSynMessage *scm;
2401 struct NeighbourMapEntry *n;
2402 struct GNUNET_TIME_Absolute ts;
2404 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2406 GNUNET_break_op (0);
2407 return GNUNET_SYSERR;
2409 GNUNET_STATISTICS_update (GST_stats,
2411 ("# SYN messages received"),
2413 if (NULL == neighbours)
2415 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2416 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2418 return GNUNET_OK; /* we're shutting down */
2420 scm = (const struct TransportSynMessage *) message;
2421 GNUNET_break_op (0 == ntohl (scm->reserved));
2422 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2423 n = lookup_neighbour (peer);
2426 /* This is a new neighbour and set to not connected */
2427 n = setup_neighbour (peer);
2430 /* Remember this SYN message in neighbour */
2431 n->ack_state = ACK_SEND_SYN_ACK;
2432 n->connect_ack_timestamp = ts;
2434 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2435 "Received SYN for peer `%s' in state %s/%s\n",
2437 GNUNET_TRANSPORT_ps2s (n->state),
2438 print_ack_state (n->ack_state));
2442 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2443 /* Request an address from ATS to send SYN_ACK to this peer */
2444 set_state_and_timeout (n,
2445 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2446 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2448 case GNUNET_TRANSPORT_PS_INIT_ATS:
2449 /* SYN message takes priority over us asking ATS for address:
2450 * Wait for ATS to suggest an address and send SYN_ACK */
2451 set_state_and_timeout (n,
2452 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2453 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2455 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2456 /* We already wait for an address to send an SYN_ACK */
2458 case GNUNET_TRANSPORT_PS_SYN_SENT:
2459 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2460 /* Send ACK immediately */
2461 n->ack_state = ACK_SEND_ACK;
2462 send_syn_ack_message (&n->primary_address,
2465 case GNUNET_TRANSPORT_PS_CONNECTED:
2466 /* we are already connected and can thus send the ACK immediately */
2467 GNUNET_assert (NULL != n->primary_address.address);
2468 GNUNET_assert (NULL != n->primary_address.session);
2469 n->ack_state = ACK_SEND_ACK;
2470 send_syn_ack_message (&n->primary_address,
2473 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2474 /* We wait for ATS address suggestion */
2476 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2477 /* We received a SYN message while waiting for a SYN_ACK in fast
2478 * reconnect. Send SYN_ACK immediately */
2479 n->ack_state = ACK_SEND_ACK;
2480 send_syn_ack_message (&n->primary_address,
2481 n->connect_ack_timestamp);
2483 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2484 /* We are already connected and can thus send the ACK immediately;
2485 still, it can never hurt to have an alternative address, so also
2486 tell ATS about it */
2487 GNUNET_assert (NULL != n->primary_address.address);
2488 GNUNET_assert (NULL != n->primary_address.session);
2489 n->ack_state = ACK_SEND_ACK;
2490 send_syn_ack_message (&n->primary_address,
2493 case GNUNET_TRANSPORT_PS_DISCONNECT:
2494 /* Get rid of remains and re-try */
2496 n = setup_neighbour (peer);
2497 /* Remember the SYN time stamp for ACK message */
2498 n->ack_state = ACK_SEND_SYN_ACK;
2499 n->connect_ack_timestamp = ts;
2500 /* Request an address for the peer */
2501 set_state_and_timeout (n,
2502 GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2503 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2505 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2506 /* should not be possible */
2510 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2511 "Unhandled state `%s'\n",
2512 GNUNET_TRANSPORT_ps2s (n->state));
2514 return GNUNET_SYSERR;
2521 * Check if the given @a address is the same that we are already
2522 * using for the respective neighbour. If so, update the bandwidth
2523 * assignment and possibly the session and return #GNUNET_OK.
2524 * If the new address is different from what the neighbour is
2525 * using right now, return #GNUNET_NO.
2527 * @param address address of the other peer,
2528 * @param session session to use or NULL if transport should initiate a session
2529 * @param bandwidth_in inbound quota to be used when connection is up,
2530 * 0 to disconnect from peer
2531 * @param bandwidth_out outbound quota to be used when connection is up,
2532 * 0 to disconnect from peer
2533 * @return #GNUNET_OK if we were able to just update the bandwidth and session,
2534 * #GNUNET_NO if more extensive changes are required (address changed)
2537 try_run_fast_ats_update (const struct GNUNET_HELLO_Address *address,
2538 struct GNUNET_ATS_Session *session,
2539 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2540 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2542 struct NeighbourMapEntry *n;
2544 n = lookup_neighbour (&address->peer);
2546 (NULL == n->primary_address.address) ||
2547 (0 != GNUNET_HELLO_address_cmp (address,
2548 n->primary_address.address)) )
2550 /* We are not really switching addresses, but merely adjusting
2551 session and/or bandwidth, can do fast ATS update! */
2552 if (session != n->primary_address.session)
2554 /* switch to a different session, but keeping same address; could
2555 happen if there is a 2nd inbound connection */
2556 n->primary_address.session = session;
2557 GNUNET_assert (GNUNET_YES ==
2558 GST_ats_is_known (n->primary_address.address,
2559 n->primary_address.session));
2561 if (n->primary_address.bandwidth_in.value__ != bandwidth_in.value__)
2563 n->primary_address.bandwidth_in = bandwidth_in;
2564 set_incoming_quota (n,
2567 if (n->primary_address.bandwidth_out.value__ != bandwidth_out.value__)
2569 n->primary_address.bandwidth_out = bandwidth_out;
2570 send_outbound_quota_to_clients (n);
2577 * We've been asked to switch addresses, and just now got the result
2578 * from the blacklist check to see if this is allowed.
2580 * @param cls the `struct BlacklistCheckSwitchContext` with
2581 * the information about the future address
2582 * @param peer the peer we may switch addresses on
2583 * @param address address associated with the request
2584 * @param session session associated with the request
2585 * @param result #GNUNET_OK if the connection is allowed,
2586 * #GNUNET_NO if not,
2587 * #GNUNET_SYSERR if operation was aborted
2590 switch_address_bl_check_cont (void *cls,
2591 const struct GNUNET_PeerIdentity *peer,
2592 const struct GNUNET_HELLO_Address *address,
2593 struct GNUNET_ATS_Session *session,
2596 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2597 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2598 struct NeighbourMapEntry *n;
2600 if (GNUNET_SYSERR == result)
2603 papi = GST_plugins_find (address->transport_name);
2604 GNUNET_assert (NULL != papi);
2606 if (GNUNET_NO == result)
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2610 GST_plugins_a2s (address),
2613 GNUNET_STATISTICS_update (GST_stats,
2614 "# ATS suggestions ignored (blacklist denied)",
2617 papi->disconnect_session (papi->cls,
2620 GNUNET_HELLO_address_check_option (address,
2621 GNUNET_HELLO_ADDRESS_INFO_INBOUND))
2622 GST_ats_block_address (address,
2628 if (NULL == session)
2630 /* need to create a session, ATS only gave us an address */
2631 session = papi->get_session (papi->cls,
2633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2634 "Obtained new session for peer `%s' and address '%s': %p\n",
2635 GNUNET_i2s (&address->peer),
2636 GST_plugins_a2s (address),
2638 if (NULL != session)
2639 GST_ats_new_session (address,
2642 if (NULL == session)
2644 /* session creation failed, bad!, fail! */
2645 GNUNET_STATISTICS_update (GST_stats,
2646 "# ATS suggestions ignored (failed to create session)",
2649 /* No session could be obtained, remove blacklist check and clean up */
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2651 "Failed to obtain new session for peer `%s' and address '%s'\n",
2652 GNUNET_i2s (&address->peer),
2653 GST_plugins_a2s (address));
2654 GST_ats_block_address (address,
2659 /* We did this check already before going into blacklist, but
2660 it is theoretically possible that the situation changed in
2661 the meantime, hence we check again here */
2663 try_run_fast_ats_update (address,
2665 blc_ctx->bandwidth_in,
2666 blc_ctx->bandwidth_out))
2667 goto cleanup; /* was just a minor update, we're done */
2669 /* check if we also need to setup the neighbour entry */
2670 if (NULL == (n = lookup_neighbour (peer)))
2672 n = setup_neighbour (peer);
2673 n->state = GNUNET_TRANSPORT_PS_INIT_ATS;
2676 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2677 "Peer `%s' switches to address `%s'\n",
2678 GNUNET_i2s (&address->peer),
2679 GST_plugins_a2s (address));
2683 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2685 GST_ats_block_address (address,
2689 case GNUNET_TRANSPORT_PS_INIT_ATS:
2690 /* We requested an address and ATS suggests one:
2691 * set primary address and send SYN message*/
2692 set_primary_address (n,
2695 blc_ctx->bandwidth_in,
2696 blc_ctx->bandwidth_out);
2697 if (ACK_SEND_SYN_ACK == n->ack_state)
2699 /* Send pending SYN_ACK message */
2700 n->ack_state = ACK_SEND_ACK;
2701 send_syn_ack_message (&n->primary_address,
2702 n->connect_ack_timestamp);
2704 set_state_and_timeout (n,
2705 GNUNET_TRANSPORT_PS_SYN_SENT,
2706 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2707 send_syn (&n->primary_address);
2709 case GNUNET_TRANSPORT_PS_SYN_SENT:
2710 /* ATS suggested a new address while waiting for an SYN_ACK:
2711 * Switch and send new SYN */
2712 /* ATS suggests a different address, switch again */
2713 set_primary_address (n,
2716 blc_ctx->bandwidth_in,
2717 blc_ctx->bandwidth_out);
2718 if (ACK_SEND_SYN_ACK == n->ack_state)
2720 /* Send pending SYN_ACK message */
2721 n->ack_state = ACK_SEND_ACK;
2722 send_syn_ack_message (&n->primary_address,
2723 n->connect_ack_timestamp);
2725 set_state_and_timeout (n,
2726 GNUNET_TRANSPORT_PS_SYN_SENT,
2727 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2728 send_syn (&n->primary_address);
2730 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2731 /* We requested an address and ATS suggests one:
2732 * set primary address and send SYN_ACK message*/
2733 set_primary_address (n,
2736 blc_ctx->bandwidth_in,
2737 blc_ctx->bandwidth_out);
2738 /* Send an ACK message as a response to the SYN msg */
2739 set_state_and_timeout (n,
2740 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2741 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2742 send_syn_ack_message (&n->primary_address,
2743 n->connect_ack_timestamp);
2744 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2745 (ACK_UNDEFINED == n->ack_state) )
2746 n->ack_state = ACK_SEND_ACK;
2748 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2749 /* ATS asks us to switch while we were trying to connect; switch to new
2750 address and check blacklist again */
2751 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2753 n->ack_state = ACK_SEND_ACK;
2754 send_syn_ack_message (&n->primary_address,
2755 n->connect_ack_timestamp);
2757 set_primary_address (n,
2760 blc_ctx->bandwidth_in,
2761 blc_ctx->bandwidth_out);
2762 set_state_and_timeout (n,
2763 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2764 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2766 case GNUNET_TRANSPORT_PS_CONNECTED:
2767 GNUNET_assert (NULL != n->primary_address.address);
2768 GNUNET_assert (NULL != n->primary_address.session);
2769 GNUNET_break (n->primary_address.session != session);
2770 /* ATS asks us to switch a life connection; see if we can get
2771 a SYN_ACK on it before we actually do this! */
2772 set_alternative_address (n,
2775 blc_ctx->bandwidth_in,
2776 blc_ctx->bandwidth_out);
2777 set_state_and_timeout (n,
2778 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2779 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2780 GNUNET_STATISTICS_update (GST_stats,
2781 gettext_noop ("# Attempts to switch addresses"),
2784 send_syn (&n->alternative_address);
2786 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2787 set_primary_address (n,
2790 blc_ctx->bandwidth_in,
2791 blc_ctx->bandwidth_out);
2792 if (ACK_SEND_SYN_ACK == n->ack_state)
2794 /* Send pending SYN_ACK message */
2795 n->ack_state = ACK_SEND_ACK;
2796 send_syn_ack_message (&n->primary_address,
2797 n->connect_ack_timestamp);
2799 set_state_and_timeout (n,
2800 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2801 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2802 send_syn (&n->primary_address);
2804 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2805 /* ATS asks us to switch while we were trying to reconnect; switch to new
2806 address and send SYN again */
2807 set_primary_address (n,
2810 blc_ctx->bandwidth_in,
2811 blc_ctx->bandwidth_out);
2812 set_state_and_timeout (n,
2813 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2814 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2815 send_syn (&n->primary_address);
2817 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2818 if ( (0 == GNUNET_HELLO_address_cmp (n->primary_address.address,
2820 (n->primary_address.session == session) )
2822 /* ATS switches back to still-active session */
2823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2824 "ATS double-switched, cleaning up alternative address\n");
2825 free_address (&n->alternative_address);
2826 set_state_and_timeout (n,
2827 GNUNET_TRANSPORT_PS_CONNECTED,
2831 /* ATS asks us to switch a life connection, send */
2832 set_alternative_address (n,
2835 blc_ctx->bandwidth_in,
2836 blc_ctx->bandwidth_out);
2837 set_state_and_timeout (n,
2838 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2839 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2840 send_syn (&n->alternative_address);
2842 case GNUNET_TRANSPORT_PS_DISCONNECT:
2843 /* not going to switch addresses while disconnecting */
2844 GNUNET_STATISTICS_update (GST_stats,
2845 "# ATS suggestion ignored (disconnecting)",
2849 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2853 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2854 "Unhandled state `%s'\n",
2855 GNUNET_TRANSPORT_ps2s (n->state));
2860 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2863 GNUNET_free (blc_ctx);
2868 * For the given peer, switch to this address.
2870 * Before accepting this addresses and actively using it, a blacklist check
2873 * If any check fails or the suggestion can somehow not be followed, we
2874 * MUST call #GST_ats_block_address() to tell ATS that the suggestion
2875 * could not be satisfied and force ATS to do something else.
2877 * @param address address of the other peer,
2878 * @param session session to use or NULL if transport should initiate a session
2879 * @param bandwidth_in inbound quota to be used when connection is up,
2880 * 0 to disconnect from peer
2881 * @param bandwidth_out outbound quota to be used when connection is up,
2882 * 0 to disconnect from peer
2885 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2886 struct GNUNET_ATS_Session *session,
2887 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2888 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2890 struct GST_BlacklistCheck *blc;
2891 struct BlacklistCheckSwitchContext *blc_ctx;
2893 GNUNET_assert (NULL != address->transport_name);
2895 try_run_fast_ats_update (address,
2901 /* Check if plugin is available */
2902 if (NULL == (GST_plugins_find (address->transport_name)))
2904 /* we don't have the plugin for this address */
2906 GST_ats_block_address (address,
2910 if ((NULL == session) &&
2911 (GNUNET_HELLO_address_check_option (address,
2912 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2914 /* This is a inbound address and we do not have a session to use! */
2916 GST_ats_block_address (address,
2921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2922 "ATS suggests address '%s' for peer `%s' at %u/%u speed\n",
2923 GST_plugins_a2s (address),
2924 GNUNET_i2s (&address->peer),
2925 (unsigned int) ntohl (bandwidth_in.value__),
2926 (unsigned int) ntohl (bandwidth_out.value__));
2928 /* Perform blacklist check */
2929 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2930 blc_ctx->bandwidth_in = bandwidth_in;
2931 blc_ctx->bandwidth_out = bandwidth_out;
2932 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2935 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2936 address->transport_name,
2937 &switch_address_bl_check_cont,
2948 * Function called to send network utilization data to ATS for
2949 * each active connection.
2952 * @param key peer we send utilization data for
2953 * @param value the `struct NeighbourMapEntry *` with data to send
2954 * @return #GNUNET_OK (continue to iterate)
2957 send_utilization_data (void *cls,
2958 const struct GNUNET_PeerIdentity *key,
2961 struct NeighbourMapEntry *n = value;
2964 struct GNUNET_TIME_Relative delta;
2966 if ( (GNUNET_YES != test_connected (n)) ||
2967 (NULL == n->primary_address.address) )
2969 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2970 GNUNET_TIME_absolute_get ());
2972 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2973 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2975 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2976 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2979 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2983 GST_ats_update_utilization (n->primary_address.address,
2986 n->util_total_bytes_recv = 0;
2987 n->util_total_bytes_sent = 0;
2988 n->last_util_transmission = GNUNET_TIME_absolute_get ();
2994 * Task transmitting utilization in a regular interval
2996 * @param cls the 'struct NeighbourMapEntry' for which we are running
2997 * @param tc scheduler context (unused)
3000 utilization_transmission (void *cls,
3001 const struct GNUNET_SCHEDULER_TaskContext *tc)
3003 util_transmission_tk = NULL;
3004 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3005 &send_utilization_data,
3007 util_transmission_tk
3008 = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3009 &utilization_transmission,
3015 * Track information about data we received from the
3016 * given address (used to notify ATS about our utilization
3017 * of allocated resources).
3019 * @param address the address we got data from
3020 * @param message the message we received (really only the size is used)
3023 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
3024 const struct GNUNET_MessageHeader *message)
3026 struct NeighbourMapEntry *n;
3028 n = lookup_neighbour (&address->peer);
3031 n->util_total_bytes_recv += ntohs (message->size);
3036 * Track information about data we transmitted using the given @a
3037 * address and @a session (used to notify ATS about our utilization of
3038 * allocated resources).
3040 * @param address the address we transmitted data to
3041 * @param session session we used to transmit data
3042 * @param message the message we sent (really only the size is used)
3045 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
3046 struct GNUNET_ATS_Session *session,
3049 struct NeighbourMapEntry *n;
3051 n = lookup_neighbour (&address->peer);
3054 if (n->primary_address.session != session)
3056 n->util_total_bytes_sent += size;
3061 * Master task run for every neighbour. Performs all of the time-related
3062 * activities (keep alive, send next message, disconnect if idle, finish
3063 * clean up after disconnect).
3065 * @param cls the 'struct NeighbourMapEntry' for which we are running
3066 * @param tc scheduler context (unused)
3069 master_task (void *cls,
3070 const struct GNUNET_SCHEDULER_TaskContext *tc)
3072 struct NeighbourMapEntry *n = cls;
3073 struct GNUNET_TIME_Relative delay;
3076 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3078 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3079 GNUNET_i2s (&n->id),
3080 GNUNET_TRANSPORT_ps2s(n->state),
3081 GNUNET_STRINGS_relative_time_to_string (delay,
3085 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3086 /* invalid state for master task, clean up */
3090 case GNUNET_TRANSPORT_PS_INIT_ATS:
3091 if (0 == delay.rel_value_us)
3093 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3094 "Connection to `%s' timed out waiting for ATS to provide address\n",
3095 GNUNET_i2s (&n->id));
3100 case GNUNET_TRANSPORT_PS_SYN_SENT:
3101 if (0 == delay.rel_value_us)
3103 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3104 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
3105 GNUNET_i2s (&n->id));
3106 /* Remove address and request and additional one */
3107 unset_primary_address (n);
3108 set_state_and_timeout (n,
3109 GNUNET_TRANSPORT_PS_INIT_ATS,
3110 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3114 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3115 if (0 == delay.rel_value_us)
3117 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3118 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
3119 GNUNET_i2s (&n->id));
3124 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3125 if (0 == delay.rel_value_us)
3127 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3128 "Connection to `%s' timed out waiting for other peer to send ACK\n",
3129 GNUNET_i2s (&n->id));
3130 disconnect_neighbour (n);
3134 case GNUNET_TRANSPORT_PS_CONNECTED:
3135 if (0 == delay.rel_value_us)
3137 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3138 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3139 GNUNET_i2s (&n->id));
3140 disconnect_neighbour (n);
3143 try_transmission_to_peer (n);
3146 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3147 if (0 == delay.rel_value_us)
3149 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3150 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3151 GNUNET_i2s (&n->id));
3152 disconnect_neighbour (n);
3156 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3157 if (0 == delay.rel_value_us)
3159 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3160 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
3161 GNUNET_i2s (&n->id));
3162 disconnect_neighbour (n);
3166 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3167 if (0 == delay.rel_value_us)
3169 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3170 "Switch failed, cleaning up alternative address\n");
3171 free_address (&n->alternative_address);
3172 set_state_and_timeout (n,
3173 GNUNET_TRANSPORT_PS_CONNECTED,
3174 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
3176 try_transmission_to_peer (n);
3179 case GNUNET_TRANSPORT_PS_DISCONNECT:
3180 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3181 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3182 GNUNET_i2s (&n->id));
3185 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3186 /* how did we get here!? */
3190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3191 "Unhandled state `%s'\n",
3192 GNUNET_TRANSPORT_ps2s (n->state));
3196 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3197 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3198 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3200 /* if we are *now* in one of the two states, we're sending
3201 keep alive messages, so we need to consider the keepalive
3202 delay, not just the connection timeout */
3203 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3206 if (NULL == n->task)
3207 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3214 * Send a ACK message to the neighbour to confirm that we
3217 * @param n neighbour to send the ACK to
3220 send_session_ack_message (struct NeighbourMapEntry *n)
3222 struct GNUNET_MessageHeader msg;
3224 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3225 "Sending ACK message to peer `%s'\n",
3226 GNUNET_i2s (&n->id));
3228 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3229 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3230 (void) send_with_session (n,
3232 sizeof (struct GNUNET_MessageHeader),
3234 GNUNET_TIME_UNIT_FOREVER_REL,
3241 * We received a 'SESSION_SYN_ACK' message from the other peer.
3242 * Consider switching to it.
3244 * @param message possibly a `struct GNUNET_ATS_SessionConnectMessage` (check format)
3245 * @param peer identity of the peer to switch the address for
3246 * @param address address of the other peer, NULL if other peer
3248 * @param session session to use (or NULL)
3249 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3252 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3253 const struct GNUNET_HELLO_Address *address,
3254 struct GNUNET_ATS_Session *session)
3256 const struct TransportSynMessage *scm;
3257 struct GNUNET_TIME_Absolute ts;
3258 struct NeighbourMapEntry *n;
3260 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3262 GNUNET_break_op (0);
3263 return GNUNET_SYSERR;
3265 GNUNET_STATISTICS_update (GST_stats,
3267 ("# SYN_ACK messages received"),
3269 scm = (const struct TransportSynMessage *) message;
3270 GNUNET_break_op (ntohl (scm->reserved) == 0);
3271 if (NULL == (n = lookup_neighbour (&address->peer)))
3273 GNUNET_STATISTICS_update (GST_stats,
3275 ("# unexpected SYN_ACK messages (no peer)"),
3277 return GNUNET_SYSERR;
3279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3280 "Received SYN_ACK message from peer `%s' in state %s/%s\n",
3281 GNUNET_i2s (&address->peer),
3282 GNUNET_TRANSPORT_ps2s (n->state),
3283 print_ack_state (n->ack_state));
3284 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3287 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3290 return GNUNET_SYSERR;
3291 case GNUNET_TRANSPORT_PS_INIT_ATS:
3292 GNUNET_STATISTICS_update (GST_stats,
3293 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3297 case GNUNET_TRANSPORT_PS_SYN_SENT:
3298 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3300 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3301 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3304 set_state_and_timeout (n,
3305 GNUNET_TRANSPORT_PS_CONNECTED,
3306 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3307 set_primary_address (n,
3308 n->primary_address.address,
3309 n->primary_address.session,
3310 n->primary_address.bandwidth_in,
3311 n->primary_address.bandwidth_out);
3312 send_session_ack_message (n);
3314 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3315 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3316 GNUNET_STATISTICS_update (GST_stats,
3317 gettext_noop ("# unexpected SYN_ACK messages (not ready)"),
3321 case GNUNET_TRANSPORT_PS_CONNECTED:
3322 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3323 send_session_ack_message (n);
3325 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3326 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3327 to give us a new address... */
3328 GNUNET_STATISTICS_update (GST_stats,
3329 gettext_noop ("# unexpected SYN_ACK messages (waiting on ATS)"),
3333 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3334 /* Reconnecting with new address address worked; go back to connected! */
3335 set_state_and_timeout (n,
3336 GNUNET_TRANSPORT_PS_CONNECTED,
3337 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3338 send_session_ack_message (n);
3340 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3341 /* new address worked; adopt it and go back to connected! */
3342 set_state_and_timeout (n,
3343 GNUNET_TRANSPORT_PS_CONNECTED,
3344 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3345 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3347 /* Set primary addresses */
3348 set_primary_address (n,
3349 n->alternative_address.address,
3350 n->alternative_address.session,
3351 n->alternative_address.bandwidth_in,
3352 n->alternative_address.bandwidth_out);
3353 GNUNET_STATISTICS_update (GST_stats,
3354 gettext_noop ("# Successful attempts to switch addresses"),
3358 GNUNET_HELLO_address_free (n->alternative_address.address);
3359 memset (&n->alternative_address,
3361 sizeof (n->alternative_address));
3362 send_session_ack_message (n);
3364 case GNUNET_TRANSPORT_PS_DISCONNECT:
3365 GNUNET_STATISTICS_update (GST_stats,
3367 ("# unexpected SYN_ACK messages (disconnecting)"),
3369 return GNUNET_SYSERR;
3370 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3374 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3375 "Unhandled state `%s'\n",
3376 GNUNET_TRANSPORT_ps2s (n->state));
3378 return GNUNET_SYSERR;
3385 * A session was terminated. Take note; if needed, try to get
3386 * an alternative address from ATS.
3388 * @param peer identity of the peer where the session died
3389 * @param session session that is gone
3390 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3391 * this session was not in use
3394 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3395 struct GNUNET_ATS_Session *session)
3397 struct NeighbourMapEntry *n;
3398 struct BlackListCheckContext *bcc;
3399 struct BlackListCheckContext *bcc_next;
3401 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3403 while (NULL != (bcc = bcc_next))
3405 bcc_next = bcc->next;
3406 if (bcc->na.session == session)
3408 if (NULL != bcc->bc)
3409 GST_blacklist_test_cancel (bcc->bc);
3410 GNUNET_HELLO_address_free (bcc->na.address);
3411 GNUNET_CONTAINER_DLL_remove (bc_head,
3417 if (NULL == (n = lookup_neighbour (peer)))
3418 return GNUNET_NO; /* can't affect us */
3419 if (session != n->primary_address.session)
3421 /* Free alternative address */
3422 if (session == n->alternative_address.session)
3424 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3425 set_state_and_timeout (n,
3426 GNUNET_TRANSPORT_PS_CONNECTED,
3428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3429 "Session died, cleaning up alternative address\n");
3430 free_address (&n->alternative_address);
3432 return GNUNET_NO; /* doesn't affect us further */
3435 n->expect_latency_response = GNUNET_NO;
3436 /* The session for neighbour's primary address died */
3439 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3443 case GNUNET_TRANSPORT_PS_INIT_ATS:
3447 case GNUNET_TRANSPORT_PS_SYN_SENT:
3448 /* The session used to send the SYN terminated:
3449 * this implies a connect error*/
3450 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3451 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3453 GST_plugins_a2s (n->primary_address.address),
3454 n->primary_address.session,
3457 /* Destroy the address since it cannot be used */
3458 unset_primary_address (n);
3459 set_state_and_timeout (n,
3460 GNUNET_TRANSPORT_PS_INIT_ATS,
3461 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3463 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3464 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3465 /* error on inbound session; free neighbour entirely */
3468 case GNUNET_TRANSPORT_PS_CONNECTED:
3469 /* Our primary connection died, try a fast reconnect */
3470 unset_primary_address (n);
3471 set_state_and_timeout (n,
3472 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3473 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3475 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3476 /* we don't have an address, how can it go down? */
3479 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3480 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3481 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3483 GST_plugins_a2s (n->primary_address.address),
3484 n->primary_address.session,
3486 /* Destroy the address since it cannot be used */
3487 unset_primary_address (n);
3488 set_state_and_timeout (n,
3489 GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3490 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3492 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3493 /* primary went down while we were waiting for SYN_ACK on secondary;
3494 secondary as primary */
3496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3497 "Connection `%s' %p to peer `%s' was terminated while switching, "
3498 "switching to alternative address `%s' %p\n",
3499 GST_plugins_a2s (n->primary_address.address),
3500 n->primary_address.session,
3502 GST_plugins_a2s (n->alternative_address.address),
3503 n->alternative_address.session);
3505 /* Destroy the inbound address since it cannot be used */
3506 free_address (&n->primary_address);
3507 n->primary_address = n->alternative_address;
3508 GNUNET_assert (GNUNET_YES ==
3509 GST_ats_is_known (n->primary_address.address,
3510 n->primary_address.session));
3511 memset (&n->alternative_address,
3513 sizeof (struct NeighbourAddress));
3514 set_state_and_timeout (n,
3515 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3516 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3518 case GNUNET_TRANSPORT_PS_DISCONNECT:
3519 unset_primary_address (n);
3521 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3522 /* neighbour was freed and plugins told to terminate session */
3525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3526 "Unhandled state `%s'\n",
3527 GNUNET_TRANSPORT_ps2s (n->state));
3531 if (NULL != n->task)
3532 GNUNET_SCHEDULER_cancel (n->task);
3533 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3539 * We received a 'ACK' message from the other peer.
3540 * If we sent a 'SYN_ACK' last, this means we are now
3541 * connected. Otherwise, do nothing.
3543 * @param message possibly a 'struct GNUNET_ATS_SessionConnectMessage' (check format)
3544 * @param address address of the other peer
3545 * @param session session to use (or NULL)
3546 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3549 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3550 const struct GNUNET_HELLO_Address *address,
3551 struct GNUNET_ATS_Session *session)
3553 struct NeighbourMapEntry *n;
3555 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3557 GNUNET_break_op (0);
3558 return GNUNET_SYSERR;
3560 GNUNET_STATISTICS_update (GST_stats,
3561 gettext_noop ("# ACK messages received"),
3564 if (NULL == (n = lookup_neighbour (&address->peer)))
3566 GNUNET_break_op (0);
3567 return GNUNET_SYSERR;
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3570 "Received ACK for peer `%s' in state %s/%s\n",
3571 GNUNET_i2s (&address->peer),
3572 GNUNET_TRANSPORT_ps2s (n->state),
3573 print_ack_state (n->ack_state));
3575 /* Check if we are in a plausible state for having sent
3576 a SYN_ACK. If not, return, otherwise break.
3578 The remote peers sends a ACK as a response for a SYN_ACK
3582 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3583 now wait for the ACK to finally be connected
3584 - If we sent a SYN_ACK to this peer before */
3586 if ( ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3587 (ACK_SEND_ACK != n->ack_state) ) ||
3588 (NULL == n->primary_address.address) )
3590 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3591 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3592 GNUNET_i2s (&address->peer),
3593 GNUNET_TRANSPORT_ps2s (n->state),
3594 print_ack_state (n->ack_state));
3596 GNUNET_STATISTICS_update (GST_stats,
3597 gettext_noop ("# unexpected ACK messages"),
3602 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3604 /* We tried to switch addresses while being connect. We explicitly wait
3605 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3606 * so we do not want to set the address as in use! */
3609 set_state_and_timeout (n,
3610 GNUNET_TRANSPORT_PS_CONNECTED,
3611 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3613 if (NULL == n->primary_address.address) {
3615 * We are in state = PSY_SYN_RECV_ACK or ack_state = ACK_SEND_ACK, which
3616 * really means we did try (and succeed) to send a SYN and are waiting for
3618 * That suggests that the primary_address used to be non-NULL, but maybe it
3619 * got reset to NULL without the state being changed appropriately?
3625 /* Reset backoff for primary address */
3626 GST_ats_block_reset (n->primary_address.address,
3627 n->primary_address.session);
3633 * Test if we're connected to the given peer.
3635 * @param target peer to test
3636 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3639 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3641 return test_connected (lookup_neighbour (target));
3646 * Task to asynchronously run #free_neighbour().
3648 * @param cls the `struct NeighbourMapEntry` to free
3652 delayed_disconnect (void *cls,
3653 const struct GNUNET_SCHEDULER_TaskContext* tc)
3655 struct NeighbourMapEntry *n = cls;
3657 n->delayed_disconnect_task = NULL;
3658 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3659 "Disconnecting by request from peer %s\n",
3660 GNUNET_i2s (&n->id));
3666 * We received a quota message from the given peer,
3667 * validate and process.
3669 * @param peer sender of the message
3670 * @param msg the quota message
3673 GST_neighbours_handle_quota_message (const struct GNUNET_PeerIdentity *peer,
3674 const struct GNUNET_MessageHeader *msg)
3676 struct NeighbourMapEntry *n;
3677 const struct GNUNET_ATS_SessionQuotaMessage *sqm;
3679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3680 "Received QUOTA message from peer `%s'\n",
3682 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionQuotaMessage))
3684 GNUNET_break_op (0);
3685 GNUNET_STATISTICS_update (GST_stats,
3686 gettext_noop ("# quota messages ignored (malformed)"),
3691 GNUNET_STATISTICS_update (GST_stats,
3693 ("# QUOTA messages received"),
3695 sqm = (const struct GNUNET_ATS_SessionQuotaMessage *) msg;
3696 if (NULL == (n = lookup_neighbour (peer)))
3701 n->neighbour_receive_quota
3702 = GNUNET_BANDWIDTH_value_max (GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
3703 GNUNET_BANDWIDTH_value_init (ntohl (sqm->quota)));
3704 send_outbound_quota_to_clients (n);
3709 * We received a disconnect message from the given peer,
3710 * validate and process.
3712 * @param peer sender of the message
3713 * @param msg the disconnect message
3716 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3717 const struct GNUNET_MessageHeader *msg)
3719 struct NeighbourMapEntry *n;
3720 const struct GNUNET_ATS_SessionDisconnectMessage *sdm;
3722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3723 "Received DISCONNECT message from peer `%s'\n",
3725 if (ntohs (msg->size) != sizeof (struct GNUNET_ATS_SessionDisconnectMessage))
3727 GNUNET_break_op (0);
3728 GNUNET_STATISTICS_update (GST_stats,
3730 ("# disconnect messages ignored (malformed)"),
3735 GNUNET_STATISTICS_update (GST_stats,
3737 ("# DISCONNECT messages received"),
3739 sdm = (const struct GNUNET_ATS_SessionDisconnectMessage *) msg;
3740 if (NULL == (n = lookup_neighbour (peer)))
3745 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3747 GNUNET_STATISTICS_update (GST_stats,
3748 gettext_noop ("# disconnect messages ignored (timestamp)"),
3753 if (0 != memcmp (peer,
3755 sizeof (struct GNUNET_PeerIdentity)))
3757 GNUNET_break_op (0);
3760 if (ntohl (sdm->purpose.size) !=
3761 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3762 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3763 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3766 "DISCONNECT message from peer `%s' has invalid size\n",
3768 GNUNET_break_op (0);
3772 GNUNET_CRYPTO_eddsa_verify (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT,
3777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3778 "DISCONNECT message from peer `%s' cannot be verified \n",
3780 GNUNET_break_op (0);
3783 if (NULL == n->delayed_disconnect_task)
3785 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect,
3792 * Closure for the #neighbours_iterate() function.
3794 struct IteratorContext
3797 * Function to call on each connected neighbour.
3799 GST_NeighbourIterator cb;
3802 * Closure for @e cb.
3809 * Call the callback from the closure for each neighbour.
3811 * @param cls the `struct IteratorContext`
3812 * @param key the hash of the public key of the neighbour
3813 * @param value the `struct NeighbourMapEntry`
3814 * @return #GNUNET_OK (continue to iterate)
3817 neighbours_iterate (void *cls,
3818 const struct GNUNET_PeerIdentity *key,
3821 struct IteratorContext *ic = cls;
3822 struct NeighbourMapEntry *n = value;
3823 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3824 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3826 if (NULL != n->primary_address.address)
3828 bandwidth_in = n->primary_address.bandwidth_in;
3829 bandwidth_out = n->primary_address.bandwidth_out;
3833 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3834 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3838 n->primary_address.address,
3841 bandwidth_in, bandwidth_out);
3847 * Iterate over all connected neighbours.
3849 * @param cb function to call
3850 * @param cb_cls closure for @a cb
3853 GST_neighbours_iterate (GST_NeighbourIterator cb,
3856 struct IteratorContext ic;
3858 if (NULL == neighbours)
3859 return; /* can happen during shutdown */
3862 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3863 &neighbours_iterate,
3869 * If we have an active connection to the given target, it must be shutdown.
3871 * @param target peer to disconnect from
3874 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3876 struct NeighbourMapEntry *n;
3878 if (NULL == (n = lookup_neighbour (target)))
3879 return; /* not active */
3880 if (GNUNET_YES == test_connected (n))
3881 GNUNET_STATISTICS_update (GST_stats,
3882 gettext_noop ("# disconnected from peer upon explicit request"),
3885 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3886 "Forced disconnect from peer %s\n",
3887 GNUNET_i2s (target));
3888 disconnect_neighbour (n);
3893 * Obtain current address information for the given neighbour.
3896 * @return address currently used
3898 const struct GNUNET_HELLO_Address *
3899 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3901 struct NeighbourMapEntry *n;
3903 n = lookup_neighbour (peer);
3906 return n->primary_address.address;
3911 * Initialize the neighbours subsystem.
3913 * @param max_fds maximum number of fds to use
3916 GST_neighbours_start (unsigned int max_fds)
3918 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE,
3920 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3921 &utilization_transmission,
3927 * Disconnect from the given neighbour.
3930 * @param key hash of neighbour's public key (not used)
3931 * @param value the `struct NeighbourMapEntry` of the neighbour
3932 * @return #GNUNET_OK (continue to iterate)
3935 disconnect_all_neighbours (void *cls,
3936 const struct GNUNET_PeerIdentity *key,
3939 struct NeighbourMapEntry *n = value;
3941 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3942 "Disconnecting peer `%4s' during shutdown\n",
3943 GNUNET_i2s (&n->id));
3950 * Cleanup the neighbours subsystem.
3953 GST_neighbours_stop ()
3955 struct BlacklistCheckSwitchContext *cur;
3956 struct BlacklistCheckSwitchContext *next;
3958 if (NULL == neighbours)
3960 if (NULL != util_transmission_tk)
3962 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3963 util_transmission_tk = NULL;
3965 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3966 &disconnect_all_neighbours,
3968 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3970 next = pending_bc_head;
3971 for (cur = next; NULL != cur; cur = next)
3974 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
3978 if (NULL != cur->blc)
3980 GST_blacklist_test_cancel (cur->blc);
3988 /* end of file gnunet-service-transport_neighbours.c */