2 This file is part of GNUnet.
3 (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., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_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"
43 * Size of the neighbour hash map.
45 #define NEIGHBOUR_TABLE_SIZE 256
48 * Time we give plugin to transmit DISCONNECT message before the
49 * neighbour entry self-destructs.
51 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
54 * How often must a peer violate bandwidth quotas before we start
55 * to simply drop its messages?
57 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
60 * How long are we willing to wait for a response from ATS before timing out?
62 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
65 * How long are we willing to wait for an ACK from the other peer before
66 * giving up on our connect operation?
68 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
71 * How long are we willing to wait for a successful reconnect if
72 * an existing connection went down? Much shorter than the
73 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
74 * higher layers about the disconnect during this period.
76 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
79 * How long are we willing to wait for a response from the blacklist
80 * subsystem before timing out?
82 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
85 * Interval to send utilization data
87 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
90 * State describing which kind a reply this neighbour should send
95 * We did not receive a SYN message for this neighbour
100 * The neighbour received a SYN message and has to send a SYN_ACK
103 ACK_SEND_SYN_ACK = 1,
106 * The neighbour sent a SYN_ACK message and has to send a ACK
113 GNUNET_NETWORK_STRUCT_BEGIN
116 * Message a peer sends to another to indicate that it intends to
117 * setup a connection/session for data exchange. A 'SESSION_SYN'
118 * should be answered with a 'SESSION_SYN_ACK' with the same body
119 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
120 * a 'ACK'. Once the 'ACK' is received, both peers
121 * should be connected.
123 struct TransportSynMessage
126 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
127 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
129 struct GNUNET_MessageHeader header;
134 uint32_t reserved GNUNET_PACKED;
137 * Absolute time at the sender. Only the most recent connect
138 * message implies which session is preferred by the sender.
140 struct GNUNET_TIME_AbsoluteNBO timestamp;
146 * Message a peer sends to another when connected to indicate that a
147 * session is in use and the peer is still alive or to respond to a keep alive.
148 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
149 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
150 * When the keep alive response with type is received, transport service
151 * will call the respective plugin to update the session timeout
153 struct SessionKeepAliveMessage
156 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
157 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
159 struct GNUNET_MessageHeader header;
162 * A nonce to identify the session the keep alive is used for
164 uint32_t nonce GNUNET_PACKED;
168 * Message we send to the other peer to notify him that we intentionally
169 * are disconnecting (to reduce timeouts). This is just a friendly
170 * notification, peers must not rely on always receiving disconnect
173 struct SessionDisconnectMessage
176 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
178 struct GNUNET_MessageHeader header;
183 uint32_t reserved GNUNET_PACKED;
186 * Purpose of the signature. Extends over the timestamp.
187 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
189 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
192 * Absolute time at the sender. Only the most recent connect
193 * message implies which session is preferred by the sender.
195 struct GNUNET_TIME_AbsoluteNBO timestamp;
198 * Public key of the sender.
200 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
203 * Signature of the peer that sends us the disconnect. Only
204 * valid if the timestamp is AFTER the timestamp from the
205 * corresponding 'SYN' message.
207 struct GNUNET_CRYPTO_EddsaSignature signature;
211 GNUNET_NETWORK_STRUCT_END
215 * For each neighbour we keep a list of messages
216 * that we still want to transmit to the neighbour.
222 * This is a doubly linked list.
224 struct MessageQueue *next;
227 * This is a doubly linked list.
229 struct MessageQueue *prev;
232 * Function to call once we're done.
234 GST_NeighbourSendContinuation cont;
237 * Closure for @e cont
242 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
243 * stuck together in memory. Allocated at the end of this struct.
245 const char *message_buf;
248 * Size of the message buf
250 size_t message_buf_size;
253 * At what time should we fail?
255 struct GNUNET_TIME_Absolute timeout;
261 * A possible address we could use to communicate with a neighbour.
263 struct NeighbourAddress
267 * Active session for this address.
269 struct Session *session;
272 * Network-level address information.
274 struct GNUNET_HELLO_Address *address;
277 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
278 * peer for this address. Use to check that the ACK is in response
279 * to our most recent 'SYN'.
281 struct GNUNET_TIME_Absolute connect_timestamp;
284 * Inbound bandwidth from ATS for this address.
286 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
289 * Outbound bandwidth from ATS for this address.
291 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
294 * Did we tell ATS that this is our 'active' address?
299 * The current nonce sent in the last keep alive messages
301 uint32_t keep_alive_nonce;
306 * Entry in neighbours.
308 struct NeighbourMapEntry
312 * Head of list of messages we would like to send to this peer;
313 * must contain at most one message per client.
315 struct MessageQueue *messages_head;
318 * Tail of list of messages we would like to send to this peer; must
319 * contain at most one message per client.
321 struct MessageQueue *messages_tail;
324 * Are we currently trying to send a message? If so, which one?
326 struct MessageQueue *is_active;
329 * Primary address we currently use to communicate with the neighbour.
331 struct NeighbourAddress primary_address;
334 * Alternative address currently under consideration for communicating
335 * with the neighbour.
337 struct NeighbourAddress alternative_address;
340 * Identity of this neighbour.
342 struct GNUNET_PeerIdentity id;
345 * Main task that drives this peer (timeouts, keepalives, etc.).
346 * Always runs the 'master_task'.
348 struct GNUNET_SCHEDULER_Task *task;
351 * Task to disconnect neighbour after we received a DISCONNECT message
353 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
356 * At what time should we sent the next keep-alive message?
358 struct GNUNET_TIME_Absolute keep_alive_time;
361 * At what time did we sent the last keep-alive message? Used
362 * to calculate round-trip time ("latency").
364 struct GNUNET_TIME_Absolute last_keep_alive_time;
367 * Timestamp we should include in our next SYN_ACK message.
368 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
369 * our SYN_ACK message.
371 struct GNUNET_TIME_Absolute connect_ack_timestamp;
374 * ATS address suggest handle
376 struct GNUNET_ATS_SuggestHandle *suggest_handle;
379 * Time where we should cut the connection (timeout) if we don't
380 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
381 * if we are in #S_CONNECTED).
383 struct GNUNET_TIME_Absolute timeout;
386 * Latest calculated latency value
388 struct GNUNET_TIME_Relative latency;
391 * Tracker for inbound bandwidth.
393 struct GNUNET_BANDWIDTH_Tracker in_tracker;
396 * How often has the other peer (recently) violated the inbound
397 * traffic limit? Incremented by 10 per violation, decremented by 1
398 * per non-violation (for each time interval).
400 unsigned int quota_violation_count;
403 * The current state of the peer.
405 enum GNUNET_TRANSPORT_PeerState state;
408 * Did we sent an KEEP_ALIVE message and are we expecting a response?
410 int expect_latency_response;
413 * When a peer wants to connect we have to reply to the 1st SYN message
414 * with a SYN_ACK message. But sometime we cannot send this message
415 * immediately since we do not have an address and then we have to remember
416 * to send this message as soon as we have an address.
418 * Flag to set if we still need to send a SYN_ACK message to the other peer
419 * (once we have an address to use and the peer has been allowed by our
420 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
421 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
422 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
423 * 'ACK' (regardless of what our own state machine might say).
425 enum GST_ACK_State ack_state;
428 * Tracking utilization of outbound bandwidth
430 uint32_t util_payload_bytes_sent;
433 * Tracking utilization of inbound bandwidth
435 uint32_t util_payload_bytes_recv;
438 * Tracking utilization of outbound bandwidth
440 uint32_t util_total_bytes_sent;
443 * Tracking utilization of inbound bandwidth
445 uint32_t util_total_bytes_recv;
448 * Date of last utilization transmission
450 struct GNUNET_TIME_Absolute last_util_transmission;
455 * Context for blacklist checks and the #try_connect_bl_check_cont()
456 * function. Stores information about ongoing blacklist checks.
458 struct BlackListCheckContext
462 * We keep blacklist checks in a DLL.
464 struct BlackListCheckContext *next;
467 * We keep blacklist checks in a DLL.
469 struct BlackListCheckContext *prev;
472 * Address that is being checked.
474 struct NeighbourAddress na;
477 * Handle to the ongoing blacklist check.
479 struct GST_BlacklistCheck *bc;
484 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
486 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
489 * We keep blacklist checks in a DLL so that we can find
490 * the 'sessions' in their 'struct NeighbourAddress' if
491 * a session goes down.
493 static struct BlackListCheckContext *bc_head;
496 * We keep blacklist checks in a DLL.
498 static struct BlackListCheckContext *bc_tail;
501 * List of pending blacklist checks: head
503 static struct BlacklistCheckSwitchContext *pending_bc_head;
506 * List of pending blacklist checks: tail
508 static struct BlacklistCheckSwitchContext *pending_bc_tail;
511 * counter for connected neighbours
513 static unsigned int neighbours_connected;
516 * Number of bytes we have currently queued for transmission.
518 static unsigned long long bytes_in_send_queue;
521 * Task transmitting utilization data
523 static struct GNUNET_SCHEDULER_Task * util_transmission_tk;
530 print_ack_state (enum GST_ACK_State s)
535 case ACK_SEND_SYN_ACK:
536 return "SEND_SYN_ACK";
547 * Notify our clients that another peer connected to us.
549 * @param peer the peer that connected
550 * @param bandwidth_in inbound bandwidth in NBO
551 * @param bandwidth_out outbound bandwidth in NBO
554 neighbours_connect_notification (const struct GNUNET_PeerIdentity *peer,
555 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
556 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
558 size_t len = sizeof(struct ConnectInfoMessage);
559 char buf[len] GNUNET_ALIGN;
560 struct ConnectInfoMessage *connect_msg = (struct ConnectInfoMessage *) buf;
562 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
563 "We are now connected to peer `%s'\n",
565 connect_msg->header.size = htons (sizeof(buf));
566 connect_msg->header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_CONNECT);
567 connect_msg->id = *peer;
568 connect_msg->quota_in = bandwidth_in;
569 connect_msg->quota_out = bandwidth_out;
570 GST_clients_broadcast (&connect_msg->header, GNUNET_NO);
575 * Notify our clients (and manipulation) that a peer disconnected from
578 * @param peer the peer that disconnected
581 neighbours_disconnect_notification (const struct GNUNET_PeerIdentity *peer)
583 struct DisconnectInfoMessage disconnect_msg;
585 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
586 "Peer `%s' disconnected\n",
588 GST_manipulation_peer_disconnect (peer);
589 disconnect_msg.header.size = htons (sizeof(struct DisconnectInfoMessage));
590 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
591 disconnect_msg.reserved = htonl (0);
592 disconnect_msg.peer = *peer;
593 GST_clients_broadcast (&disconnect_msg.header,
599 * Notify transport clients that a neighbour peer changed its active
602 * @param peer identity of the peer
603 * @param address address possibly NULL if peer is not connected
604 * @param state current state this peer is in
605 * @param state_timeout timeout for the current state of the peer
606 * @param bandwidth_in bandwidth assigned inbound, 0 on disconnect
607 * @param bandwidth_out bandwidth assigned outbound, 0 on disconnect
610 neighbours_changed_notification (const struct GNUNET_PeerIdentity *peer,
611 const struct GNUNET_HELLO_Address *address,
612 enum GNUNET_TRANSPORT_PeerState state,
613 struct GNUNET_TIME_Absolute state_timeout,
614 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
615 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
618 "Notifying about change for peer `%s' with address `%s' in state `%s' timing out at %s\n",
620 GST_plugins_a2s (address),
621 GNUNET_TRANSPORT_ps2s (state),
622 GNUNET_STRINGS_absolute_time_to_string (state_timeout));
623 /* FIXME: include bandwidth in notification! */
624 GST_clients_broadcast_peer_notification (peer,
633 * Lookup a neighbour entry in the neighbours hash map.
635 * @param pid identity of the peer to look up
636 * @return the entry, NULL if there is no existing record
638 static struct NeighbourMapEntry *
639 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
641 if (NULL == neighbours)
643 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
648 * Test if we're connected to the given peer.
650 * @param n neighbour entry of peer to test
651 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
654 test_connected (struct NeighbourMapEntry *n)
658 return GNUNET_TRANSPORT_is_connected (n->state);
663 * Send information about a new outbound quota to our clients.
665 * @param target affected peer
666 * @param quota new quota
669 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
670 struct GNUNET_BANDWIDTH_Value32NBO quota)
672 struct QuotaSetMessage q_msg;
674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
675 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
676 ntohl (quota.value__), GNUNET_i2s (target));
677 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
678 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
680 q_msg.peer = (*target);
681 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
686 * We don't need a given neighbour address any more.
687 * Release its resources and give appropriate notifications
688 * to ATS and other subsystems.
690 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
693 free_address (struct NeighbourAddress *na)
695 if (GNUNET_YES == na->ats_active)
697 GST_validation_set_address_use (na->address,
699 GST_ats_set_in_use (na->address,
704 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
705 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
706 na->ats_active = GNUNET_NO;
707 na->keep_alive_nonce = 0;
708 if (NULL != na->address)
710 GNUNET_HELLO_address_free (na->address);
718 * Set net state for this neighbour and notify monitoring
720 * @param n the respective neighbour
721 * @param s the new state
724 set_state (struct NeighbourMapEntry *n,
725 enum GNUNET_TRANSPORT_PeerState s)
728 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
729 "Neighbour `%s' changed state to %s\n",
731 GNUNET_TRANSPORT_ps2s(s));
732 neighbours_changed_notification (&n->id,
733 n->primary_address.address,
736 n->primary_address.bandwidth_in,
737 n->primary_address.bandwidth_out);
742 * Set net state and state timeout for this neighbour and notify monitoring
744 * @param n the respective neighbour
745 * @param s the new state
746 * @param timeout the new timeout
749 set_state_and_timeout (struct NeighbourMapEntry *n,
750 enum GNUNET_TRANSPORT_PeerState s,
751 struct GNUNET_TIME_Absolute timeout)
754 n->timeout = timeout;
755 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
756 "Neighbour `%s' changed state to %s with timeout %s\n",
758 GNUNET_TRANSPORT_ps2s(s),
759 GNUNET_STRINGS_absolute_time_to_string (timeout));
760 neighbours_changed_notification (&n->id,
761 n->primary_address.address,
764 n->primary_address.bandwidth_in,
765 n->primary_address.bandwidth_out);
770 * Set new state timeout for this neighbour and notify monitoring
772 * @param n the respective neighbour
773 * @param timeout the new timeout
776 set_timeout (struct NeighbourMapEntry *n,
777 struct GNUNET_TIME_Absolute timeout)
779 n->timeout = timeout;
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "Neighbour `%s' changed timeout %s\n",
783 GNUNET_STRINGS_absolute_time_to_string (timeout));
784 neighbours_changed_notification (&n->id,
785 n->primary_address.address,
786 n->state, n->timeout,
787 n->primary_address.bandwidth_in,
788 n->primary_address.bandwidth_out);
793 * Initialize the alternative address of a neighbour
795 * @param n the neighbour
796 * @param address address of the other peer, NULL if other peer
798 * @param session session to use (or NULL, in which case an
799 * address must be setup)
800 * @param bandwidth_in inbound quota to be used when connection is up
801 * @param bandwidth_out outbound quota to be used when connection is up
804 set_alternative_address (struct NeighbourMapEntry *n,
805 const struct GNUNET_HELLO_Address *address,
806 struct Session *session,
807 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
808 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
810 struct GNUNET_TRANSPORT_PluginFunctions *papi;
812 if (NULL == (papi = GST_plugins_find (address->transport_name)))
817 if (session == n->alternative_address.session)
819 n->alternative_address.bandwidth_in = bandwidth_in;
820 n->alternative_address.bandwidth_out = bandwidth_out;
823 free_address (&n->alternative_address);
825 session = papi->get_session (papi->cls, address);
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Failed to obtain new session for peer `%s' and address '%s'\n",
830 GNUNET_i2s (&address->peer),
831 GST_plugins_a2s (address));
832 GNUNET_STATISTICS_update (GST_stats,
833 gettext_noop ("# session creation failed"),
838 GST_ats_new_session (address, session);
839 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
840 "Neighbour `%s' configured alternative address %s\n",
842 GST_plugins_a2s(address));
844 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
845 n->alternative_address.bandwidth_in = bandwidth_in;
846 n->alternative_address.bandwidth_out = bandwidth_out;
847 n->alternative_address.session = session;
848 n->alternative_address.ats_active = GNUNET_NO;
849 n->alternative_address.keep_alive_nonce = 0;
854 * Initialize the primary address of a neighbour
856 * @param n the neighbour
857 * @param address address of the other peer, NULL if other peer
859 * @param session session to use (or NULL, in which case an
860 * address must be setup)
861 * @param bandwidth_in inbound quota to be used when connection is up
862 * @param bandwidth_out outbound quota to be used when connection is up
863 * @param is_active #GNUNET_YES to mark this as the active address with ATS
866 set_primary_address (struct NeighbourMapEntry *n,
867 const struct GNUNET_HELLO_Address *address,
868 struct Session *session,
869 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
870 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
873 struct GNUNET_TRANSPORT_PluginFunctions *papi;
875 if (NULL == (papi = GST_plugins_find (address->transport_name)))
880 if (session == n->primary_address.session)
882 n->primary_address.bandwidth_in = bandwidth_in;
883 n->primary_address.bandwidth_out = bandwidth_out;
884 if (is_active != n->primary_address.ats_active)
886 n->primary_address.ats_active = is_active;
887 GST_ats_set_in_use (n->primary_address.address,
888 n->primary_address.session,
890 GST_validation_set_address_use (n->primary_address.address,
893 if (GNUNET_YES == is_active)
895 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
896 send_outbound_quota (&address->peer, bandwidth_out);
900 free_address (&n->primary_address);
902 session = papi->get_session (papi->cls, address);
905 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
906 "Failed to obtain new session for peer `%s' and address '%s'\n",
907 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
908 GNUNET_STATISTICS_update (GST_stats,
909 gettext_noop ("# session creation failed"),
914 GST_ats_new_session (address, session);
915 n->primary_address.address = GNUNET_HELLO_address_copy (address);
916 n->primary_address.bandwidth_in = bandwidth_in;
917 n->primary_address.bandwidth_out = bandwidth_out;
918 n->primary_address.session = session;
919 n->primary_address.ats_active = is_active;
920 n->primary_address.keep_alive_nonce = 0;
921 if (GNUNET_YES == is_active)
923 /* Telling ATS about new session */
924 GST_ats_set_in_use (n->primary_address.address,
925 n->primary_address.session,
927 GST_validation_set_address_use (n->primary_address.address,
929 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
930 send_outbound_quota (&address->peer, bandwidth_out);
933 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
934 "Neighbour `%s' switched to address `%s'\n",
936 GST_plugins_a2s(address));
938 neighbours_changed_notification (&n->id,
939 n->primary_address.address,
942 n->primary_address.bandwidth_in,
943 n->primary_address.bandwidth_out);
948 * Clear the primary address of a neighbour since this address is not
949 * valid anymore and notify monitoring about it
951 * @param n the neighbour
954 unset_primary_address (struct NeighbourMapEntry *n)
956 /* Notify monitoring about change */
957 neighbours_changed_notification (&n->id,
958 n->primary_address.address,
961 GNUNET_BANDWIDTH_value_init (0),
962 GNUNET_BANDWIDTH_value_init (0));
963 /* Unset primary address */
964 free_address (&n->primary_address);
969 * Clear the alternative address of a neighbour since this address is not
972 * @param n the neighbour
975 unset_alternative_address (struct NeighbourMapEntry *n)
977 /* Unset primary address */
978 free_address (&n->alternative_address);
983 * Free a neighbour map entry.
985 * @param n entry to free
986 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
987 * #GNUNET_YES to keep all sessions
990 free_neighbour (struct NeighbourMapEntry *n,
993 struct MessageQueue *mq;
994 struct GNUNET_TRANSPORT_PluginFunctions *papi;
995 struct GNUNET_HELLO_Address *backup_primary;
997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
998 "Freeing neighbour state of peer `%s'\n",
999 GNUNET_i2s (&n->id));
1000 n->is_active = NULL; /* always free'd by its own continuation! */
1002 /* fail messages currently in the queue */
1003 while (NULL != (mq = n->messages_head))
1005 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1006 if (NULL != mq->cont)
1007 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
1010 /* It is too late to send other peer disconnect notifications, but at
1011 least internally we need to get clean... */
1012 if (GNUNET_YES == test_connected (n))
1014 GNUNET_STATISTICS_set (GST_stats,
1015 gettext_noop ("# peers connected"),
1016 --neighbours_connected,
1018 neighbours_disconnect_notification (&n->id);
1021 /* Mark peer as disconnected */
1022 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1024 if (NULL != n->primary_address.address)
1025 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
1027 backup_primary = NULL;
1029 /* free addresses and mark as unused */
1030 unset_primary_address (n);
1031 free_address (&n->alternative_address);
1033 /* cut all transport-level connection for this peer */
1034 // FIXME: might want to revisit this; maybe just
1035 // shorten session timeout on plugin level?
1036 if ((GNUNET_NO == keep_sessions) &&
1037 (NULL != backup_primary) &&
1038 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
1039 papi->disconnect_peer (papi->cls, &n->id);
1041 if (NULL != backup_primary)
1042 GNUNET_HELLO_address_free (backup_primary);
1043 GNUNET_assert (GNUNET_YES ==
1044 GNUNET_CONTAINER_multipeermap_remove (neighbours,
1047 /* Cancel address requests for this peer */
1048 if (NULL != n->suggest_handle)
1050 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
1051 n->suggest_handle = NULL;
1054 /* Cancel the disconnect task */
1055 if (NULL != n->delayed_disconnect_task)
1057 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
1058 n->delayed_disconnect_task = NULL;
1061 /* Cancel the master task */
1062 if (NULL != n->task)
1064 GNUNET_SCHEDULER_cancel (n->task);
1067 /* free rest of memory */
1073 * Transmit a message using the current session of the given
1076 * @param n entry for the recipient
1077 * @param msgbuf buffer to transmit
1078 * @param msgbuf_size number of bytes in @a msgbuf buffer
1079 * @param priority transmission priority
1080 * @param timeout transmission timeout
1081 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
1082 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
1083 * @param cont continuation to call when finished (can be NULL)
1084 * @param cont_cls closure for @a cont
1085 * @return timeout (copy of @a timeout or a calculated one if
1086 * @a use_keepalive_timeout is #GNUNET_YES.
1088 static struct GNUNET_TIME_Relative
1089 send_with_session (struct NeighbourMapEntry *n,
1090 const char *msgbuf, size_t msgbuf_size,
1092 struct GNUNET_TIME_Relative timeout,
1093 unsigned int use_keepalive_timeout,
1094 GNUNET_TRANSPORT_TransmitContinuation cont,
1097 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1098 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
1100 GNUNET_assert (n->primary_address.session != NULL);
1101 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1102 (-1 == papi->send (papi->cls,
1103 n->primary_address.session,
1104 msgbuf, msgbuf_size,
1106 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
1107 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1108 papi->query_keepalive_factor (papi->cls))),
1109 cont, cont_cls)))) &&
1111 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1112 GST_neighbours_notify_data_sent (n->primary_address.address,
1113 n->primary_address.session,
1115 GNUNET_break (NULL != papi);
1121 * Master task run for every neighbour. Performs all of the time-related
1122 * activities (keep alive, send next message, disconnect if idle, finish
1123 * clean up after disconnect).
1125 * @param cls the `struct NeighbourMapEntry` for which we are running
1126 * @param tc scheduler context (unused)
1129 master_task (void *cls,
1130 const struct GNUNET_SCHEDULER_TaskContext *tc);
1134 * Function called when the 'DISCONNECT' message has been sent by the
1135 * plugin. Frees the neighbour --- if the entry still exists.
1138 * @param target identity of the neighbour that was disconnected
1139 * @param result #GNUNET_OK if the disconnect got out successfully
1140 * @param payload bytes payload
1141 * @param physical bytes physical
1144 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1145 int result, size_t payload, size_t physical)
1147 struct NeighbourMapEntry *n;
1149 n = lookup_neighbour (target);
1151 return; /* already gone */
1152 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1153 return; /* have created a fresh entry since */
1154 if (NULL != n->task)
1155 GNUNET_SCHEDULER_cancel (n->task);
1156 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1161 * Transmit a DISCONNECT message to the other peer.
1163 * @param n neighbour to send DISCONNECT message.
1166 send_disconnect (struct NeighbourMapEntry *n)
1168 struct SessionDisconnectMessage disconnect_msg;
1170 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1171 "Sending DISCONNECT message to peer `%4s'\n",
1172 GNUNET_i2s (&n->id));
1173 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1174 disconnect_msg.header.type =
1175 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1176 disconnect_msg.reserved = htonl (0);
1177 disconnect_msg.purpose.size =
1178 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1179 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1180 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1181 disconnect_msg.purpose.purpose =
1182 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1183 disconnect_msg.timestamp =
1184 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1185 disconnect_msg.public_key = GST_my_identity.public_key;
1186 GNUNET_assert (GNUNET_OK ==
1187 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1188 &disconnect_msg.purpose,
1189 &disconnect_msg.signature));
1191 (void) send_with_session (n, (const char *) &disconnect_msg,
1192 sizeof (disconnect_msg), UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1193 GNUNET_NO, &send_disconnect_cont, NULL );
1194 GNUNET_STATISTICS_update (GST_stats,
1196 ("# DISCONNECT messages sent"), 1,
1202 * Disconnect from the given neighbour, clean up the record.
1204 * @param n neighbour to disconnect from
1207 disconnect_neighbour (struct NeighbourMapEntry *n)
1209 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1210 "Disconnecting from peer %s in state %s\n",
1211 GNUNET_i2s (&n->id),
1212 GNUNET_TRANSPORT_ps2s (n->state));
1213 /* depending on state, notify neighbour and/or upper layers of this peer
1217 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1218 case GNUNET_TRANSPORT_PS_INIT_ATS:
1219 /* other peer is completely unaware of us, no need to send DISCONNECT */
1220 free_neighbour (n, GNUNET_NO);
1222 case GNUNET_TRANSPORT_PS_SYN_SENT:
1223 send_disconnect (n);
1224 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1226 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1227 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1228 free_neighbour (n, GNUNET_NO);
1230 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1231 /* we DID ACK the other peer's request, must send DISCONNECT */
1232 send_disconnect (n);
1233 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1235 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1236 case GNUNET_TRANSPORT_PS_CONNECTED:
1237 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1238 /* we are currently connected, need to send disconnect and do
1239 internal notifications and update statistics */
1240 send_disconnect (n);
1241 GNUNET_STATISTICS_set (GST_stats,
1242 gettext_noop ("# peers connected"),
1243 --neighbours_connected,
1245 neighbours_disconnect_notification (&n->id);
1246 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1248 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1249 /* Disconnecting while waiting for an ATS address to reconnect,
1250 * cannot send DISCONNECT */
1251 free_neighbour (n, GNUNET_NO);
1253 case GNUNET_TRANSPORT_PS_DISCONNECT:
1254 /* already disconnected, ignore */
1256 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1257 /* already cleaned up, how did we get here!? */
1261 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1262 "Unhandled state `%s'\n",
1263 GNUNET_TRANSPORT_ps2s (n->state));
1267 /* schedule timeout to clean up */
1268 if (NULL != n->task)
1269 GNUNET_SCHEDULER_cancel (n->task);
1270 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1276 * We're done with our transmission attempt, continue processing.
1278 * @param cls the `struct MessageQueue` of the message
1279 * @param receiver intended receiver
1280 * @param success whether it worked or not
1281 * @param size_payload bytes payload sent
1282 * @param physical bytes sent on wire
1285 transmit_send_continuation (void *cls,
1286 const struct GNUNET_PeerIdentity *receiver,
1287 int success, size_t size_payload, size_t physical)
1289 struct MessageQueue *mq = cls;
1290 struct NeighbourMapEntry *n;
1292 if (NULL == (n = lookup_neighbour (receiver)))
1295 return; /* disconnect or other error while transmitting, can happen */
1297 if (n->is_active == mq)
1299 /* this is still "our" neighbour, remove us from its queue
1300 and allow it to send the next message now */
1301 n->is_active = NULL;
1302 if (NULL != n->task)
1303 GNUNET_SCHEDULER_cancel (n->task);
1304 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1306 if (bytes_in_send_queue < mq->message_buf_size)
1308 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1309 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1310 bytes_in_send_queue, mq->message_buf_size,
1311 (GNUNET_OK == success) ? "OK" : "FAIL",
1312 size_payload, physical);
1317 GNUNET_break (size_payload == mq->message_buf_size);
1318 bytes_in_send_queue -= mq->message_buf_size;
1319 GNUNET_STATISTICS_set (GST_stats,
1321 ("# bytes in message queue for other peers"),
1322 bytes_in_send_queue, GNUNET_NO);
1323 if (GNUNET_OK == success)
1324 GNUNET_STATISTICS_update (GST_stats,
1326 ("# messages transmitted to other peers"),
1329 GNUNET_STATISTICS_update (GST_stats,
1331 ("# transmission failures for messages to other peers"),
1333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1334 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1335 GNUNET_i2s (receiver),
1336 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1337 mq->message_buf_size,
1338 (success == GNUNET_OK) ? "success" : "FAILURE");
1339 if (NULL != mq->cont)
1340 mq->cont (mq->cont_cls, success, size_payload, physical);
1346 * Check the message list for the given neighbour and if we can
1347 * send a message, do so. This function should only be called
1348 * if the connection is at least generally ready for transmission.
1349 * While we will only send one message at a time, no bandwidth
1350 * quota management is performed here. If a message was given to
1351 * the plugin, the continuation will automatically re-schedule
1352 * the 'master' task once the next message might be transmitted.
1354 * @param n target peer for which to transmit
1357 try_transmission_to_peer (struct NeighbourMapEntry *n)
1359 struct MessageQueue *mq;
1360 struct GNUNET_TIME_Relative timeout;
1362 if (NULL == n->primary_address.address)
1364 /* no address, why are we here? */
1368 if ((0 == n->primary_address.address->address_length) &&
1369 (NULL == n->primary_address.session))
1371 /* no address, why are we here? */
1375 if (NULL != n->is_active)
1377 /* transmission already pending */
1381 /* timeout messages from the queue that are past their due date */
1382 while (NULL != (mq = n->messages_head))
1384 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1385 if (timeout.rel_value_us > 0)
1387 GNUNET_STATISTICS_update (GST_stats,
1389 ("# messages timed out while in transport queue"),
1391 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1393 transmit_send_continuation (mq, &n->id,
1395 mq->message_buf_size, 0); /* timeout */
1398 return; /* no more messages */
1399 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Giving message with %u bytes to plugin session %p\n",
1404 mq->message_buf_size, n->primary_address.session);
1406 (void) send_with_session (n,
1407 mq->message_buf, mq->message_buf_size,
1408 0 /* priority */, timeout, GNUNET_NO,
1409 &transmit_send_continuation, mq);
1414 * Send keepalive message to the neighbour. Must only be called
1415 * if we are on 'connected' state or while trying to switch addresses.
1416 * Will internally determine if a keepalive is truly needed (so can
1417 * always be called).
1419 * @param n neighbour that went idle and needs a keepalive
1422 send_keepalive (struct NeighbourMapEntry *n)
1424 struct SessionKeepAliveMessage m;
1425 struct GNUNET_TIME_Relative timeout;
1428 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1429 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT));
1430 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1431 return; /* no keepalive needed at this time */
1433 nonce = 0; /* 0 indicates 'not set' */
1435 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1437 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1438 "Sending keep alive to peer `%s' with nonce %u\n",
1439 GNUNET_i2s (&n->id), nonce);
1441 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1442 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1443 m.nonce = htonl (nonce);
1445 timeout = send_with_session (n,
1446 (const void *) &m, sizeof (m),
1447 UINT32_MAX /* priority */,
1448 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1450 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1452 n->primary_address.keep_alive_nonce = nonce;
1453 n->expect_latency_response = GNUNET_YES;
1454 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1455 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1461 * Keep the connection to the given neighbour alive longer,
1462 * we received a KEEPALIVE (or equivalent); send a response.
1464 * @param neighbour neighbour to keep alive (by sending keep alive response)
1465 * @param m the keep alive message containing the nonce to respond to
1468 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1469 const struct GNUNET_MessageHeader *m)
1471 struct NeighbourMapEntry *n;
1472 const struct SessionKeepAliveMessage *msg_in;
1473 struct SessionKeepAliveMessage msg;
1475 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1478 msg_in = (struct SessionKeepAliveMessage *) m;
1479 if (NULL == (n = lookup_neighbour (neighbour)))
1481 GNUNET_STATISTICS_update (GST_stats,
1483 ("# KEEPALIVE messages discarded (peer unknown)"),
1487 if (NULL == n->primary_address.session)
1489 GNUNET_STATISTICS_update (GST_stats,
1491 ("# KEEPALIVE messages discarded (no session)"),
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Received keep alive request from peer `%s' with nonce %u\n",
1498 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1500 /* send reply to allow neighbour to measure latency */
1501 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1502 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1503 msg.nonce = msg_in->nonce;
1504 (void) send_with_session(n,
1505 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1506 UINT32_MAX /* priority */,
1507 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1513 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1514 * latency to this peer. Pass the updated information (existing ats
1515 * plus calculated latency) to ATS.
1517 * @param neighbour neighbour to keep alive
1518 * @param m the message containing the keep alive response
1521 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1522 const struct GNUNET_MessageHeader *m)
1524 struct NeighbourMapEntry *n;
1525 const struct SessionKeepAliveMessage *msg;
1526 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1528 struct GNUNET_ATS_Information ats;
1530 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1533 msg = (const struct SessionKeepAliveMessage *) m;
1534 if (NULL == (n = lookup_neighbour (neighbour)))
1536 GNUNET_STATISTICS_update (GST_stats,
1538 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1542 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1543 (GNUNET_YES != n->expect_latency_response) )
1545 GNUNET_STATISTICS_update (GST_stats,
1547 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1551 if (NULL == n->primary_address.address)
1553 GNUNET_STATISTICS_update (GST_stats,
1555 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1559 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1561 GNUNET_STATISTICS_update (GST_stats,
1563 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 "Received keep alive response from peer `%s' for session %p\n",
1571 GNUNET_i2s (&n->id), n->primary_address.session);
1575 /* Update session timeout here */
1576 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1579 "Updating session for peer `%s' for session %p\n",
1580 GNUNET_i2s (&n->id), n->primary_address.session);
1581 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1588 n->primary_address.keep_alive_nonce = 0;
1589 n->expect_latency_response = GNUNET_NO;
1590 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1591 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1594 "Latency for peer `%s' is %s\n",
1595 GNUNET_i2s (&n->id),
1596 GNUNET_STRINGS_relative_time_to_string (n->latency,
1598 /* append latency */
1599 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1600 if (n->latency.rel_value_us > UINT32_MAX)
1601 latency = UINT32_MAX;
1603 latency = n->latency.rel_value_us;
1604 ats.value = htonl (latency);
1605 GST_ats_update_metrics (n->primary_address.address,
1606 n->primary_address.session,
1612 * We have received a message from the given sender. How long should
1613 * we delay before receiving more? (Also used to keep the peer marked
1616 * @param sender sender of the message
1617 * @param size size of the message
1618 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1619 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1620 * #GNUNET_SYSERR if the connection is not fully up yet
1621 * @return how long to wait before reading more from this sender
1623 struct GNUNET_TIME_Relative
1624 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1625 *sender, ssize_t size, int *do_forward)
1627 struct NeighbourMapEntry *n;
1628 struct GNUNET_TIME_Relative ret;
1630 if (NULL == neighbours)
1632 *do_forward = GNUNET_NO;
1633 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1635 if (NULL == (n = lookup_neighbour (sender)))
1637 GST_neighbours_try_connect (sender);
1638 if (NULL == (n = lookup_neighbour (sender)))
1640 GNUNET_STATISTICS_update (GST_stats,
1642 ("# messages discarded due to lack of neighbour record"),
1644 *do_forward = GNUNET_NO;
1645 return GNUNET_TIME_UNIT_ZERO;
1648 if (! test_connected (n))
1650 *do_forward = GNUNET_SYSERR;
1651 return GNUNET_TIME_UNIT_ZERO;
1653 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1655 n->quota_violation_count++;
1656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1657 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1658 n->in_tracker.available_bytes_per_s__,
1659 n->quota_violation_count);
1660 /* Discount 32k per violation */
1661 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1665 if (n->quota_violation_count > 0)
1667 /* try to add 32k back */
1668 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1669 n->quota_violation_count--;
1672 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1674 GNUNET_STATISTICS_update (GST_stats,
1676 ("# bandwidth quota violations by other peers"),
1678 *do_forward = GNUNET_NO;
1679 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1681 *do_forward = GNUNET_YES;
1682 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1683 if (ret.rel_value_us > 0)
1685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1686 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1687 (unsigned long long) n->in_tracker.
1688 consumption_since_last_update__,
1689 (unsigned int) n->in_tracker.available_bytes_per_s__,
1690 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1691 GNUNET_STATISTICS_update (GST_stats,
1692 gettext_noop ("# ms throttling suggested"),
1693 (int64_t) ret.rel_value_us / 1000LL,
1701 * Transmit a message to the given target using the active connection.
1703 * @param target destination
1704 * @param msg message to send
1705 * @param msg_size number of bytes in msg
1706 * @param timeout when to fail with timeout
1707 * @param cont function to call when done
1708 * @param cont_cls closure for 'cont'
1711 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1712 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1713 GST_NeighbourSendContinuation cont, void *cont_cls)
1715 struct NeighbourMapEntry *n;
1716 struct MessageQueue *mq;
1718 /* All ove these cases should never happen; they are all API violations.
1719 But we check anyway, just to be sure. */
1720 if (NULL == (n = lookup_neighbour (target)))
1724 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1727 if (GNUNET_YES != test_connected (n))
1731 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1734 bytes_in_send_queue += msg_size;
1735 GNUNET_STATISTICS_set (GST_stats,
1737 ("# bytes in message queue for other peers"),
1738 bytes_in_send_queue, GNUNET_NO);
1739 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1741 mq->cont_cls = cont_cls;
1742 memcpy (&mq[1], msg, msg_size);
1743 mq->message_buf = (const char *) &mq[1];
1744 mq->message_buf_size = msg_size;
1745 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueueing %u bytes to send to peer %s\n",
1748 msg_size, GNUNET_i2s (target));
1750 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1751 if (NULL != n->task)
1752 GNUNET_SCHEDULER_cancel (n->task);
1753 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1758 send_session_connect_cont (void *cls,
1759 const struct GNUNET_PeerIdentity *target,
1761 size_t size_payload,
1762 size_t size_on_wire)
1764 struct NeighbourMapEntry *n;
1766 n = lookup_neighbour (target);
1769 /* SYN continuation was called after neighbor was freed,
1770 * for example due to a time out for the state or the session
1771 * used was already terminated: nothing to do here... */
1775 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1776 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1777 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1779 /* SYN continuation was called after neighbor changed state,
1780 * for example due to a time out for the state or the session
1781 * used was already terminated: nothing to do here... */
1784 if (GNUNET_OK == result)
1787 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1788 _("Failed to send SYN message to peer `%s' using address `%s' session %p\n"),
1789 GNUNET_i2s (target),
1790 GST_plugins_a2s (n->primary_address.address),
1791 n->primary_address.session);
1794 case GNUNET_TRANSPORT_PS_SYN_SENT:
1795 /* Remove address and request and additional one */
1796 unset_primary_address (n);
1797 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1798 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1800 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1801 /* Remove address and request and additional one */
1802 unset_primary_address (n);
1803 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1804 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1806 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1807 /* Remove address and request and go back to primary address */
1808 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1809 ("# Failed attempts to switch addresses (failed to send SYN CONT)"), 1, GNUNET_NO);
1810 unset_alternative_address (n);
1811 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1812 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1815 disconnect_neighbour (n);
1821 * Send a SYN message via the given address.
1823 * @param na address to use
1826 send_syn (struct NeighbourAddress *na)
1828 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1829 struct TransportSynMessage connect_msg;
1830 struct NeighbourMapEntry *n;
1832 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1833 "Sending SYN message to peer `%s'\n",
1834 GNUNET_i2s (&na->address->peer));
1836 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1841 if (NULL == na->session)
1842 na->session = papi->get_session (papi->cls, na->address);
1843 if (NULL == na->session)
1848 GST_ats_new_session (na->address,
1850 GNUNET_STATISTICS_update (GST_stats,
1852 ("# SYN messages sent"),
1854 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1855 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1856 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1857 connect_msg.reserved = htonl (0);
1858 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1860 papi->send (papi->cls,
1862 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
1864 SETUP_CONNECTION_TIMEOUT,
1865 send_session_connect_cont, NULL))
1867 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1868 _("Failed to transmit SYN message via plugin to %s\n"),
1869 GST_plugins_a2s (na->address));
1871 n = lookup_neighbour (&na->address->peer);
1879 case GNUNET_TRANSPORT_PS_SYN_SENT:
1880 /* Remove address and request and additional one */
1881 unset_primary_address (n);
1882 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1883 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1884 /* Hard failure to send the SYN message with this address:
1885 Destroy address and session */
1887 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1888 /* Remove address and request and additional one */
1889 unset_primary_address (n);
1890 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1891 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1893 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1894 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1895 ("# Failed attempts to switch addresses (failed to send SYN)"), 1, GNUNET_NO);
1896 /* Remove address and request and additional one */
1897 unset_alternative_address (n);
1898 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1899 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1902 disconnect_neighbour (n);
1906 GST_neighbours_notify_data_sent (na->address,
1908 sizeof (struct TransportSynMessage));
1913 send_session_connect_ack_cont (void *cls,
1914 const struct GNUNET_PeerIdentity *target,
1916 size_t size_payload,
1917 size_t size_on_wire)
1919 struct NeighbourMapEntry *n;
1921 n = lookup_neighbour (target);
1924 /* SYN_ACK continuation was called after neighbor was freed,
1925 * for example due to a time out for the state or the session
1926 * used was already terminated: nothing to do here... */
1930 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1932 /* SYN_ACK continuation was called after neighbor changed state,
1933 * for example due to a time out for the state or the session
1934 * used was already terminated: nothing to do here... */
1937 if (GNUNET_OK == result)
1940 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1941 _("Failed to send SYN_ACK message to peer `%s' using address `%s' session %p\n"),
1942 GNUNET_i2s (target),
1943 GST_plugins_a2s (n->primary_address.address),
1944 n->primary_address.session);
1946 /* Remove address and request and additional one */
1947 unset_primary_address (n);
1948 n->ack_state = ACK_SEND_SYN_ACK;
1949 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1950 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1956 * Send a SYN_ACK message via the given address.
1958 * @param address address to use
1959 * @param session session to use
1960 * @param timestamp timestamp to use for the ACK message
1961 * @return GNUNET_SYSERR if sending immediately failed, GNUNET_OK otherwise
1964 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1965 struct Session *session,
1966 struct GNUNET_TIME_Absolute timestamp)
1968 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1969 struct TransportSynMessage connect_msg;
1970 struct NeighbourMapEntry *n;
1972 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1973 "Sending SYN_ACK to peer `%s'\n",
1974 GNUNET_i2s (&address->peer));
1976 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1981 if (NULL == session)
1982 session = papi->get_session (papi->cls, address);
1983 if (NULL == session)
1988 GST_ats_new_session (address, session);
1989 GNUNET_STATISTICS_update (GST_stats,
1991 ("# SYN_ACK messages sent"),
1993 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1994 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
1995 connect_msg.reserved = htonl (0);
1996 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1998 if (GNUNET_SYSERR == papi->send (papi->cls,
2000 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
2002 GNUNET_TIME_UNIT_FOREVER_REL,
2003 send_session_connect_ack_cont, NULL))
2005 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
2006 _("Failed to transmit SYN_ACK message via plugin to %s\n"),
2007 GST_plugins_a2s (address));
2009 n = lookup_neighbour (&address->peer);
2015 /* Remove address and request and additional one */
2016 unset_primary_address (n);
2017 n->ack_state = ACK_SEND_SYN_ACK;
2018 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2019 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2027 inbound_bw_tracker_update (void *cls)
2029 struct NeighbourMapEntry *n = cls;
2030 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2031 struct GNUNET_TIME_Relative delay;
2034 if (NULL == n->primary_address.address)
2035 return; /* not active, ignore */
2036 papi = GST_plugins_find (n->primary_address.address->transport_name);
2037 GNUNET_assert (NULL != papi);
2038 if (NULL == papi->update_inbound_delay)
2040 delay = GST_neighbours_calculate_receive_delay (&n->id,
2043 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2044 "New inbound delay for peer `%s' is %llu ms\n",
2045 GNUNET_i2s (&n->id),
2046 delay.rel_value_us / 1000);
2047 papi->update_inbound_delay (papi->cls,
2049 n->primary_address.session,
2055 * Create a fresh entry in the neighbour map for the given peer
2057 * @param peer peer to create an entry for
2058 * @return new neighbour map entry
2060 static struct NeighbourMapEntry *
2061 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2063 struct NeighbourMapEntry *n;
2065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2066 "Creating new neighbour entry for `%s'\n",
2068 n = GNUNET_new (struct NeighbourMapEntry);
2070 n->ack_state = ACK_UNDEFINED;
2071 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2072 n->last_util_transmission = GNUNET_TIME_absolute_get();
2073 n->util_payload_bytes_recv = 0;
2074 n->util_payload_bytes_sent = 0;
2075 n->util_total_bytes_recv = 0;
2076 n->util_total_bytes_sent = 0;
2077 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
2078 &inbound_bw_tracker_update,
2080 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2081 MAX_BANDWIDTH_CARRY_S);
2082 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2083 set_state_and_timeout (n,
2084 GNUNET_TRANSPORT_PS_NOT_CONNECTED,
2085 GNUNET_TIME_UNIT_FOREVER_ABS);
2086 GNUNET_assert (GNUNET_OK ==
2087 GNUNET_CONTAINER_multipeermap_put (neighbours,
2089 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2097 struct BlacklistCheckSwitchContext
2102 struct BlacklistCheckSwitchContext *prev;
2107 struct BlacklistCheckSwitchContext *next;
2112 struct GST_BlacklistCheck *blc;
2117 struct GNUNET_HELLO_Address *address;
2122 struct Session *session;
2127 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2132 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2137 * Black list check result for try_connect call
2138 * If connection to the peer is allowed request adddress and
2140 * @param cls blc_ctx bl context
2141 * @param peer the peer
2142 * @param result the result
2145 try_connect_bl_check_cont (void *cls,
2146 const struct GNUNET_PeerIdentity *peer,
2149 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2150 struct NeighbourMapEntry *n;
2152 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2155 GNUNET_free (blc_ctx);
2156 if (GNUNET_OK != result)
2158 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2159 _("Blacklisting disapproved to connect to peer `%s'\n"),
2164 /* Setup a new neighbour */
2165 if (NULL != lookup_neighbour(peer))
2166 return; /* The neighbor was created in the meantime while waited for BL clients */
2168 n = setup_neighbour (peer);
2170 /* Request address suggestions for this peer */
2171 set_state_and_timeout (n,
2172 GNUNET_TRANSPORT_PS_INIT_ATS,
2173 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2174 GNUNET_ATS_reset_backoff (GST_ats, peer);
2175 GNUNET_assert (NULL == n->suggest_handle);
2176 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats,
2183 * Try to create a connection to the given target (eventually).
2185 * @param target peer to try to connect to
2188 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2190 struct NeighbourMapEntry *n;
2191 struct GST_BlacklistCheck *blc;
2192 struct BlacklistCheckSwitchContext *blc_ctx;
2194 if (NULL == neighbours)
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2197 "Asked to connect to peer `%s' during shutdown\n",
2198 GNUNET_i2s (target));
2199 return; /* during shutdown, do nothing */
2201 n = lookup_neighbour (target);
2202 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2203 "Asked to connect to peer `%s' (state: %s)\n",
2204 GNUNET_i2s (target),
2205 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2210 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2211 /* this should not be possible */
2213 free_neighbour (n, GNUNET_NO);
2215 case GNUNET_TRANSPORT_PS_INIT_ATS:
2216 case GNUNET_TRANSPORT_PS_SYN_SENT:
2217 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2218 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2219 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2220 "Ignoring request to try to connect to `%s', already trying!\n",
2221 GNUNET_i2s (target));
2222 return; /* already trying */
2223 case GNUNET_TRANSPORT_PS_CONNECTED:
2224 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2225 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2226 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2227 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2228 "Ignoring request to try to connect, already connected to `%s'!\n",
2229 GNUNET_i2s (target));
2230 return; /* already connected */
2231 case GNUNET_TRANSPORT_PS_DISCONNECT:
2232 /* get rid of remains, ready to re-try immediately */
2233 free_neighbour (n, GNUNET_NO);
2235 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2236 /* should not be possible */
2240 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2241 "Unhandled state `%s'\n",
2242 GNUNET_TRANSPORT_ps2s (n->state));
2244 free_neighbour (n, GNUNET_NO);
2249 /* Do blacklist check if connecting to this peer is allowed */
2250 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2251 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2253 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2254 &try_connect_bl_check_cont, blc_ctx)))
2262 * We received a 'SYN' message from the other peer.
2263 * Consider switching to it.
2265 * @param message possibly a 'struct TransportSynMessage' (check format)
2266 * @param peer identity of the peer to switch the address for
2267 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2270 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2271 const struct GNUNET_PeerIdentity *peer)
2273 const struct TransportSynMessage *scm;
2274 struct NeighbourMapEntry *n;
2275 struct GNUNET_TIME_Absolute ts;
2277 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2279 GNUNET_break_op (0);
2280 return GNUNET_SYSERR;
2282 GNUNET_STATISTICS_update (GST_stats,
2284 ("# SYN messages received"),
2286 if (NULL == neighbours)
2288 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2289 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2291 return GNUNET_OK; /* we're shutting down */
2293 scm = (const struct TransportSynMessage *) message;
2294 GNUNET_break_op (0 == ntohl (scm->reserved));
2295 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2296 n = lookup_neighbour (peer);
2299 /* This is a new neighbour and set to not connected */
2300 n = setup_neighbour (peer);
2303 /* Remember this SYN message in neighbour */
2304 n->ack_state = ACK_SEND_SYN_ACK;
2305 n->connect_ack_timestamp = ts;
2307 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2308 "Received SYN for peer `%s' in state %s/%s\n",
2310 GNUNET_TRANSPORT_ps2s (n->state),
2311 print_ack_state (n->ack_state));
2315 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2316 /* Request an address from ATS to send SYN_ACK to this peer */
2317 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2318 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2319 if (NULL == n->suggest_handle)
2320 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2322 case GNUNET_TRANSPORT_PS_INIT_ATS:
2323 /* SYN message takes priority over us asking ATS for address:
2324 * Wait for ATS to suggest an address and send SYN_ACK */
2325 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2326 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2328 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2329 /* We already wait for an address to send an SYN_ACK */
2331 case GNUNET_TRANSPORT_PS_SYN_SENT:
2332 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2333 /* Send ACK immediately */
2334 n->ack_state = ACK_SEND_ACK;
2335 send_connect_ack_message (n->primary_address.address,
2336 n->primary_address.session, ts);
2338 case GNUNET_TRANSPORT_PS_CONNECTED:
2339 /* we are already connected and can thus send the ACK immediately */
2340 GNUNET_assert (NULL != n->primary_address.address);
2341 GNUNET_assert (NULL != n->primary_address.session);
2342 n->ack_state = ACK_SEND_ACK;
2343 send_connect_ack_message (n->primary_address.address,
2344 n->primary_address.session, ts);
2346 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2347 /* We wait for ATS address suggestion */
2349 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2350 /* We received a SYN message while waiting for a SYN_ACK in fast
2351 * reconnect. Send SYN_ACK immediately */
2352 n->ack_state = ACK_SEND_ACK;
2353 send_connect_ack_message (n->primary_address.address,
2354 n->primary_address.session, n->connect_ack_timestamp);
2356 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2357 /* We are already connected and can thus send the ACK immediately;
2358 still, it can never hurt to have an alternative address, so also
2359 tell ATS about it */
2360 GNUNET_assert (NULL != n->primary_address.address);
2361 GNUNET_assert (NULL != n->primary_address.session);
2362 n->ack_state = ACK_SEND_ACK;
2363 send_connect_ack_message (n->primary_address.address,
2364 n->primary_address.session, ts);
2366 case GNUNET_TRANSPORT_PS_DISCONNECT:
2367 /* Get rid of remains without terminating sessions, ready to re-try */
2368 free_neighbour (n, GNUNET_YES);
2369 n = setup_neighbour (peer);
2370 /* Remember the SYN time stamp for ACK message */
2371 n->ack_state = ACK_SEND_SYN_ACK;
2372 n->connect_ack_timestamp = ts;
2373 /* Request an address for the peer */
2374 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2375 GNUNET_ATS_reset_backoff (GST_ats, peer);
2376 set_state (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS);
2378 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2379 /* should not be possible */
2383 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2384 "Unhandled state `%s'\n",
2385 GNUNET_TRANSPORT_ps2s (n->state));
2387 return GNUNET_SYSERR;
2394 * We've been asked to switch addresses, and just now
2395 * got the result from the blacklist check to see if this
2398 * @param cls the `struct BlacklistCheckSwitchContext` with
2399 * the information about the future address
2400 * @param peer the peer we may switch addresses on
2401 * @param result #GNUNET_NO if we are not allowed to use the new
2405 switch_address_bl_check_cont (void *cls,
2406 const struct GNUNET_PeerIdentity *peer,
2409 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2410 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2411 struct NeighbourMapEntry *n;
2413 papi = GST_plugins_find (blc_ctx->address->transport_name);
2415 if ( (NULL == (n = lookup_neighbour (peer))) ||
2416 (result == GNUNET_NO) ||
2421 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2422 "Peer %s is unknown, suggestion ignored\n",
2424 GNUNET_STATISTICS_update (GST_stats,
2425 "# ATS suggestions ignored (neighbour unknown)",
2429 if (result == GNUNET_NO)
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2432 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2433 GST_plugins_a2s (blc_ctx->address),
2435 GNUNET_i2s (&blc_ctx->address->peer));
2436 GNUNET_STATISTICS_update (GST_stats,
2437 "# ATS suggestions ignored (blacklist denied)",
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2445 blc_ctx->address->transport_name,
2446 GST_plugins_a2s (blc_ctx->address),
2448 GNUNET_i2s (&blc_ctx->address->peer));
2449 GNUNET_STATISTICS_update (GST_stats,
2450 "# ATS suggestions ignored (plugin unknown)",
2455 /* This address is blacklisted, delete session */
2456 /* FIXME: tell plugin to force killing session here and now! */
2458 /* Remove blacklist check and clean up */
2459 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2462 GNUNET_HELLO_address_free (blc_ctx->address);
2463 GNUNET_free (blc_ctx);
2467 if (NULL == blc_ctx->session)
2469 blc_ctx->session = papi->get_session (papi->cls,
2471 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2472 "Obtained new session for peer `%s' and address '%s': %p\n",
2473 GNUNET_i2s (&blc_ctx->address->peer),
2474 GST_plugins_a2s (blc_ctx->address),
2476 if (NULL != blc_ctx->session)
2477 GST_ats_new_session (blc_ctx->address,
2481 if (NULL == blc_ctx->session)
2483 GNUNET_STATISTICS_update (GST_stats,
2484 "# ATS suggestions ignored (failed to create session)",
2487 /* No session could be obtained, remove blacklist check and clean up */
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489 "Failed to obtain new session for peer `%s' and address '%s'\n",
2490 GNUNET_i2s (&blc_ctx->address->peer),
2491 GST_plugins_a2s (blc_ctx->address));
2492 /* FIXME: Delete address in ATS!? */
2493 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2496 GNUNET_HELLO_address_free (blc_ctx->address);
2497 GNUNET_free (blc_ctx);
2501 if ( (NULL != n->primary_address.address) &&
2502 (0 == GNUNET_HELLO_address_cmp (blc_ctx->address,
2503 n->primary_address.address)) )
2505 if (blc_ctx->session == n->primary_address.session)
2507 // FIXME: handle this before blacklist check!
2508 /* This address is already primary, update only quotas */
2509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2510 "Updating quota for peer `%s' address `%s' session %p\n",
2511 GNUNET_i2s (&blc_ctx->address->peer),
2512 GST_plugins_a2s (blc_ctx->address),
2514 set_primary_address (n,
2517 blc_ctx->bandwidth_in,
2518 blc_ctx->bandwidth_out,
2521 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2524 GNUNET_HELLO_address_free (blc_ctx->address);
2525 GNUNET_free (blc_ctx);
2528 // FIXME: is this really OK?
2529 GNUNET_STATISTICS_update (GST_stats,
2530 "# ATS suggestion oddity (address match, session missmatch)",
2535 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2536 "Peer `%s' switches to address `%s' session %p\n",
2537 GNUNET_i2s (&blc_ctx->address->peer),
2538 GST_plugins_a2s (blc_ctx->address),
2543 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2545 free_neighbour (n, GNUNET_NO);
2547 case GNUNET_TRANSPORT_PS_INIT_ATS:
2548 /* We requested an address and ATS suggests one:
2549 * set primary address and send SYN message*/
2550 set_primary_address (n,
2553 blc_ctx->bandwidth_in,
2554 blc_ctx->bandwidth_out,
2556 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2558 /* Send pending SYN_ACK message */
2559 n->ack_state = ACK_SEND_ACK;
2560 send_connect_ack_message (n->primary_address.address,
2561 n->primary_address.session,
2562 n->connect_ack_timestamp);
2564 set_state_and_timeout (n,
2565 GNUNET_TRANSPORT_PS_SYN_SENT,
2566 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2567 send_syn (&n->primary_address);
2569 case GNUNET_TRANSPORT_PS_SYN_SENT:
2570 /* ATS suggested a new address while waiting for an SYN_ACK:
2571 * Switch and send new SYN */
2572 /* ATS suggests a different address, switch again */
2573 set_primary_address (n,
2576 blc_ctx->bandwidth_in,
2577 blc_ctx->bandwidth_out,
2579 if (ACK_SEND_SYN_ACK == n->ack_state)
2581 /* Send pending SYN_ACK message */
2582 n->ack_state = ACK_SEND_ACK;
2583 send_connect_ack_message (n->primary_address.address,
2584 n->primary_address.session,
2585 n->connect_ack_timestamp);
2587 set_state_and_timeout (n,
2588 GNUNET_TRANSPORT_PS_SYN_SENT,
2589 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2590 send_syn (&n->primary_address);
2592 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2593 /* We requested an address and ATS suggests one:
2594 * set primary address and send SYN_ACK message*/
2595 set_primary_address (n,
2598 blc_ctx->bandwidth_in,
2599 blc_ctx->bandwidth_out,
2601 /* Send an ACK message as a response to the SYN msg */
2602 set_state_and_timeout (n,
2603 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2604 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2605 send_connect_ack_message (n->primary_address.address,
2606 n->primary_address.session,
2607 n->connect_ack_timestamp);
2608 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2609 (ACK_UNDEFINED == n->ack_state) )
2610 n->ack_state = ACK_SEND_ACK;
2612 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2613 /* ATS asks us to switch while we were trying to connect; switch to new
2614 address and check blacklist again */
2615 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2617 n->ack_state = ACK_SEND_ACK;
2618 send_connect_ack_message (n->primary_address.address,
2619 n->primary_address.session,
2620 n->connect_ack_timestamp);
2622 set_primary_address (n,
2625 blc_ctx->bandwidth_in,
2626 blc_ctx->bandwidth_out,
2628 set_state_and_timeout (n,
2629 GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2630 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2632 case GNUNET_TRANSPORT_PS_CONNECTED:
2633 GNUNET_assert (NULL != n->primary_address.address);
2634 GNUNET_assert (NULL != n->primary_address.session);
2635 if (n->primary_address.session == blc_ctx->session)
2637 /* not an address change, just a quota change */
2638 // FIXME: this case should have been caught above!
2639 set_primary_address (n,
2642 blc_ctx->bandwidth_in,
2643 blc_ctx->bandwidth_out,
2647 /* ATS asks us to switch a life connection; see if we can get
2648 a SYN_ACK on it before we actually do this! */
2649 set_alternative_address (n,
2652 blc_ctx->bandwidth_in,
2653 blc_ctx->bandwidth_out);
2654 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2655 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2656 GNUNET_STATISTICS_update (GST_stats,
2657 gettext_noop ("# Attempts to switch addresses"),
2660 send_syn (&n->alternative_address);
2662 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2663 set_primary_address (n,
2666 blc_ctx->bandwidth_in,
2667 blc_ctx->bandwidth_out,
2669 if (ACK_SEND_SYN_ACK == n->ack_state)
2671 /* Send pending SYN_ACK message */
2672 n->ack_state = ACK_SEND_ACK;
2673 send_connect_ack_message (n->primary_address.address,
2674 n->primary_address.session,
2675 n->connect_ack_timestamp);
2677 set_state_and_timeout (n,
2678 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2679 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2680 send_syn (&n->primary_address);
2682 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2683 /* ATS asks us to switch while we were trying to reconnect; switch to new
2684 address and send SYN again */
2685 set_primary_address (n,
2688 blc_ctx->bandwidth_in,
2689 blc_ctx->bandwidth_out,
2691 set_state_and_timeout (n,
2692 GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2693 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2694 send_syn (&n->primary_address);
2696 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2697 if ( (0 == GNUNET_HELLO_address_cmp(n->primary_address.address,
2698 blc_ctx->address)) &&
2699 (n->primary_address.session == blc_ctx->session) )
2701 /* ATS switches back to still-active session */
2702 free_address (&n->alternative_address);
2703 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2706 /* ATS asks us to switch a life connection, send */
2707 set_alternative_address (n,
2710 blc_ctx->bandwidth_in,
2711 blc_ctx->bandwidth_out);
2712 set_state_and_timeout (n,
2713 GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2714 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2715 send_syn (&n->alternative_address);
2717 case GNUNET_TRANSPORT_PS_DISCONNECT:
2718 /* not going to switch addresses while disconnecting */
2719 GNUNET_STATISTICS_update (GST_stats,
2720 "# ATS suggestion ignored (disconnecting)",
2724 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2728 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2729 "Unhandled state `%s'\n",
2730 GNUNET_TRANSPORT_ps2s (n->state));
2735 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2738 GNUNET_HELLO_address_free (blc_ctx->address);
2739 GNUNET_free (blc_ctx);
2744 * For the given peer, switch to this address.
2746 * Before accepting this addresses and actively using it, a blacklist check
2747 * is performed. If this blacklist check fails the address will be destroyed.
2749 * @param address address of the other peer,
2750 * @param session session to use or NULL if transport should initiate a session
2751 * @param bandwidth_in inbound quota to be used when connection is up,
2752 * 0 to disconnect from peer
2753 * @param bandwidth_out outbound quota to be used when connection is up,
2754 * 0 to disconnect from peer
2757 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2758 struct Session *session,
2759 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2760 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2762 struct NeighbourMapEntry *n;
2763 struct GST_BlacklistCheck *blc;
2764 struct BlacklistCheckSwitchContext *blc_ctx;
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2767 "ATS has decided on an address for peer %s\n",
2768 GNUNET_i2s (&address->peer));
2769 GNUNET_assert (NULL != address->transport_name);
2770 if (NULL == (n = lookup_neighbour (&address->peer)))
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2773 "Peer %s is unknown, suggestion ignored\n",
2774 GNUNET_i2s (&address->peer));
2775 GNUNET_STATISTICS_update (GST_stats,
2776 "# ATS suggestions ignored (neighbour unknown)",
2782 /* Check if plugin is available */
2783 if (NULL == (GST_plugins_find (address->transport_name)))
2785 /* we don't have the plugin for this address */
2789 if ((NULL == session) &&
2790 (GNUNET_HELLO_address_check_option (address,
2791 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2793 /* This is a inbound address and we do not have a session to use! */
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2799 "ATS suggests %s address '%s' session %p for "
2800 "peer `%s' in state %s/%s \n",
2801 GNUNET_HELLO_address_check_option (address,
2802 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2803 GST_plugins_a2s (address),
2805 GNUNET_i2s (&address->peer),
2806 GNUNET_TRANSPORT_ps2s (n->state),
2807 print_ack_state (n->ack_state));
2809 // FIXME: definitively do NOT do this if the
2810 // suggested address did not change!!!
2812 /* Perform blacklist check */
2813 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2814 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2815 blc_ctx->session = session;
2816 blc_ctx->bandwidth_in = bandwidth_in;
2817 blc_ctx->bandwidth_out = bandwidth_out;
2818 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2821 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2822 address->transport_name,
2823 &switch_address_bl_check_cont,
2832 * Function called to send network utilization data to ATS for
2833 * each active connection.
2836 * @param key peer we send utilization data for
2837 * @param value the `struct NeighbourMapEntry *` with data to send
2838 * @return #GNUNET_OK (continue to iterate)
2841 send_utilization_data (void *cls,
2842 const struct GNUNET_PeerIdentity *key,
2845 struct NeighbourMapEntry *n = value;
2846 struct GNUNET_ATS_Information atsi[4];
2848 uint32_t bps_pl_out;
2851 struct GNUNET_TIME_Relative delta;
2853 if (GNUNET_TRANSPORT_PS_CONNECTED != n->state)
2855 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2856 GNUNET_TIME_absolute_get ());
2860 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2861 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2863 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2864 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2866 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2871 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2872 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2874 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2875 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2879 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2883 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2884 atsi[0].value = htonl (bps_out);
2885 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2886 atsi[1].value = htonl (bps_in);
2888 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2889 atsi[2].value = htonl (bps_pl_out);
2890 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2891 atsi[3].value = htonl (bps_pl_in);
2893 GST_ats_update_metrics (n->primary_address.address,
2894 n->primary_address.session,
2896 n->util_payload_bytes_recv = 0;
2897 n->util_payload_bytes_sent = 0;
2898 n->util_total_bytes_recv = 0;
2899 n->util_total_bytes_sent = 0;
2900 n->last_util_transmission = GNUNET_TIME_absolute_get();
2906 * Task transmitting utilization in a regular interval
2908 * @param cls the 'struct NeighbourMapEntry' for which we are running
2909 * @param tc scheduler context (unused)
2912 utilization_transmission (void *cls,
2913 const struct GNUNET_SCHEDULER_TaskContext *tc)
2915 util_transmission_tk = NULL;
2917 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2918 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2920 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2921 utilization_transmission, NULL);
2927 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2928 struct Session *session,
2929 const struct GNUNET_MessageHeader *message)
2931 struct NeighbourMapEntry *n;
2933 n = lookup_neighbour (&address->peer);
2936 n->util_total_bytes_recv += ntohs(message->size);
2941 GST_neighbours_notify_payload_recv (const struct GNUNET_HELLO_Address *address,
2942 struct Session *session,
2943 const struct GNUNET_MessageHeader *message)
2945 struct NeighbourMapEntry *n;
2947 n = lookup_neighbour (&address->peer);
2950 n->util_payload_bytes_recv += ntohs(message->size);
2955 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2956 struct Session *session,
2959 struct NeighbourMapEntry *n;
2961 n = lookup_neighbour (&address->peer);
2964 if (n->primary_address.session != session)
2966 n->util_total_bytes_sent += size;
2971 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2974 struct NeighbourMapEntry *n;
2975 n = lookup_neighbour (peer);
2978 n->util_payload_bytes_sent += size;
2983 * Master task run for every neighbour. Performs all of the time-related
2984 * activities (keep alive, send next message, disconnect if idle, finish
2985 * clean up after disconnect).
2987 * @param cls the 'struct NeighbourMapEntry' for which we are running
2988 * @param tc scheduler context (unused)
2991 master_task (void *cls,
2992 const struct GNUNET_SCHEDULER_TaskContext *tc)
2994 struct NeighbourMapEntry *n = cls;
2995 struct GNUNET_TIME_Relative delay;
2998 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3000 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3001 GNUNET_i2s (&n->id),
3002 GNUNET_TRANSPORT_ps2s(n->state),
3003 GNUNET_STRINGS_relative_time_to_string (delay,
3007 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3008 /* invalid state for master task, clean up */
3010 free_neighbour (n, GNUNET_NO);
3012 case GNUNET_TRANSPORT_PS_INIT_ATS:
3013 if (0 == delay.rel_value_us)
3015 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3016 "Connection to `%s' timed out waiting for ATS to provide address\n",
3017 GNUNET_i2s (&n->id));
3018 free_neighbour (n, GNUNET_NO);
3022 case GNUNET_TRANSPORT_PS_SYN_SENT:
3023 if (0 == delay.rel_value_us)
3025 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3026 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
3027 GNUNET_i2s (&n->id));
3028 /* Remove address and request and additional one */
3029 unset_primary_address (n);
3030 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3031 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3035 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3036 if (0 == delay.rel_value_us)
3038 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3039 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
3040 GNUNET_i2s (&n->id));
3041 free_neighbour (n, GNUNET_NO);
3045 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3046 if (0 == delay.rel_value_us)
3048 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3049 "Connection to `%s' timed out waiting for other peer to send ACK\n",
3050 GNUNET_i2s (&n->id));
3051 disconnect_neighbour (n);
3055 case GNUNET_TRANSPORT_PS_CONNECTED:
3056 if (0 == delay.rel_value_us)
3058 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3059 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3060 GNUNET_i2s (&n->id));
3061 disconnect_neighbour (n);
3064 try_transmission_to_peer (n);
3067 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3068 if (0 == delay.rel_value_us)
3070 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3071 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3072 GNUNET_i2s (&n->id));
3073 disconnect_neighbour (n);
3077 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3078 if (0 == delay.rel_value_us)
3080 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3081 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
3082 GNUNET_i2s (&n->id));
3083 disconnect_neighbour (n);
3087 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3088 if (0 == delay.rel_value_us)
3090 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3091 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to SYN on alternative address)\n",
3092 GNUNET_i2s (&n->id));
3093 GNUNET_STATISTICS_update (GST_stats,
3094 gettext_noop ("# Failed attempts to switch addresses (no response)"),
3097 disconnect_neighbour (n);
3100 try_transmission_to_peer (n);
3103 case GNUNET_TRANSPORT_PS_DISCONNECT:
3104 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3105 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3106 GNUNET_i2s (&n->id));
3107 free_neighbour (n, GNUNET_NO);
3109 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3110 /* how did we get here!? */
3114 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3115 "Unhandled state `%s'\n",
3116 GNUNET_TRANSPORT_ps2s (n->state));
3120 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3121 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3123 /* if we are *now* in one of the two states, we're sending
3124 keep alive messages, so we need to consider the keepalive
3125 delay, not just the connection timeout */
3126 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3129 if (NULL == n->task)
3130 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3137 * Send a ACK message to the neighbour to confirm that we
3140 * @param n neighbour to send the ACK to
3143 send_session_ack_message (struct NeighbourMapEntry *n)
3145 struct GNUNET_MessageHeader msg;
3147 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sending ACK message to peer `%s'\n",
3148 GNUNET_i2s (&n->id));
3150 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3151 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3152 (void) send_with_session(n,
3153 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3154 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3160 * We received a 'SESSION_SYN_ACK' message from the other peer.
3161 * Consider switching to it.
3163 * @param message possibly a 'struct SessionConnectMessage' (check format)
3164 * @param peer identity of the peer to switch the address for
3165 * @param address address of the other peer, NULL if other peer
3167 * @param session session to use (or NULL)
3168 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3171 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3172 const struct GNUNET_HELLO_Address *address,
3173 struct Session *session)
3175 const struct TransportSynMessage *scm;
3176 struct GNUNET_TIME_Absolute ts;
3177 struct NeighbourMapEntry *n;
3179 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3180 "Received SYN_ACK message from peer `%s'\n",
3181 GNUNET_i2s (&address->peer));
3183 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3185 GNUNET_break_op (0);
3186 return GNUNET_SYSERR;
3188 GNUNET_STATISTICS_update (GST_stats,
3190 ("# SYN_ACK messages received"),
3192 scm = (const struct TransportSynMessage *) message;
3193 GNUNET_break_op (ntohl (scm->reserved) == 0);
3194 if (NULL == (n = lookup_neighbour (&address->peer)))
3196 GNUNET_STATISTICS_update (GST_stats,
3198 ("# unexpected SYN_ACK messages (no peer)"),
3200 return GNUNET_SYSERR;
3202 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3205 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3207 free_neighbour (n, GNUNET_NO);
3208 return GNUNET_SYSERR;
3209 case GNUNET_TRANSPORT_PS_INIT_ATS:
3210 GNUNET_STATISTICS_update (GST_stats,
3212 ("# unexpected SYN_ACK messages (not ready)"),
3215 case GNUNET_TRANSPORT_PS_SYN_SENT:
3216 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3218 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3219 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3222 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3223 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3224 GNUNET_STATISTICS_set (GST_stats,
3225 gettext_noop ("# peers connected"),
3226 ++neighbours_connected,
3228 neighbours_connect_notification (&n->id,
3229 n->primary_address.bandwidth_in,
3230 n->primary_address.bandwidth_out);
3231 /* Tell ATS that the outbound session we created to send SYN was successful */
3232 set_primary_address (n,
3233 n->primary_address.address,
3234 n->primary_address.session,
3235 n->primary_address.bandwidth_in,
3236 n->primary_address.bandwidth_out,
3238 send_session_ack_message (n);
3240 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3241 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3242 GNUNET_STATISTICS_update (GST_stats,
3244 ("# unexpected SYN_ACK messages (not ready)"),
3247 case GNUNET_TRANSPORT_PS_CONNECTED:
3248 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3249 send_session_ack_message (n);
3251 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3252 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3253 to give us a new address... */
3254 GNUNET_STATISTICS_update (GST_stats,
3256 ("# unexpected SYN_ACK messages (waiting on ATS)"),
3259 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3260 /* Reconnecting with new address address worked; go back to connected! */
3261 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3262 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3263 send_session_ack_message (n);
3265 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3266 /* new address worked; adopt it and go back to connected! */
3267 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3268 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3269 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3271 /* Set primary addresses */
3272 set_primary_address (n, n->alternative_address.address,
3273 n->alternative_address.session, n->alternative_address.bandwidth_in,
3274 n->alternative_address.bandwidth_out, GNUNET_YES);
3275 GNUNET_STATISTICS_update (GST_stats, gettext_noop
3276 ("# Successful attempts to switch addresses"), 1, GNUNET_NO);
3278 free_address (&n->alternative_address);
3279 send_session_ack_message (n);
3281 case GNUNET_TRANSPORT_PS_DISCONNECT:
3282 GNUNET_STATISTICS_update (GST_stats,
3284 ("# unexpected SYN_ACK messages (disconnecting)"),
3286 return GNUNET_SYSERR;
3287 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3291 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3292 "Unhandled state `%s'\n",
3293 GNUNET_TRANSPORT_ps2s (n->state));
3295 return GNUNET_SYSERR;
3302 * A session was terminated. Take note; if needed, try to get
3303 * an alternative address from ATS.
3305 * @param peer identity of the peer where the session died
3306 * @param session session that is gone
3307 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3308 * this session was not in use
3311 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3312 struct Session *session)
3314 struct NeighbourMapEntry *n;
3315 struct BlackListCheckContext *bcc;
3316 struct BlackListCheckContext *bcc_next;
3318 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3320 while (NULL != (bcc = bcc_next))
3322 bcc_next = bcc->next;
3323 if (bcc->na.session == session)
3325 if (NULL != bcc->bc)
3326 GST_blacklist_test_cancel (bcc->bc);
3327 GNUNET_HELLO_address_free (bcc->na.address);
3328 GNUNET_CONTAINER_DLL_remove (bc_head,
3334 if (NULL == (n = lookup_neighbour (peer)))
3335 return GNUNET_NO; /* can't affect us */
3336 if (session != n->primary_address.session)
3338 /* Free alternative address */
3339 if (session == n->alternative_address.session)
3341 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) )
3342 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3343 free_address (&n->alternative_address);
3345 return GNUNET_NO; /* doesn't affect us further */
3348 n->expect_latency_response = GNUNET_NO;
3349 /* The session for neighbour's primary address died */
3352 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3354 free_neighbour (n, GNUNET_NO);
3356 case GNUNET_TRANSPORT_PS_INIT_ATS:
3358 free_neighbour (n, GNUNET_NO);
3360 case GNUNET_TRANSPORT_PS_SYN_SENT:
3361 /* The session used to send the SYN terminated:
3362 * this implies a connect error*/
3363 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3364 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3366 GST_plugins_a2s (n->primary_address.address),
3367 n->primary_address.session,
3370 /* Destroy the address since it cannot be used */
3371 unset_primary_address (n);
3372 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3373 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3375 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3376 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3377 /* error on inbound session; free neighbour entirely */
3378 free_address (&n->primary_address);
3379 free_neighbour (n, GNUNET_NO);
3381 case GNUNET_TRANSPORT_PS_CONNECTED:
3382 /* Our primary connection died, try a fast reconnect */
3383 unset_primary_address (n);
3384 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3385 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3387 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3388 /* we don't have an address, how can it go down? */
3391 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3392 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3393 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3395 GST_plugins_a2s (n->primary_address.address),
3396 n->primary_address.session,
3398 /* Destroy the address since it cannot be used */
3399 unset_primary_address (n);
3400 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3401 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3403 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3404 /* primary went down while we were waiting for SYN_ACK on secondary;
3405 secondary as primary */
3407 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3408 "Connection `%s' %p to peer `%s' was terminated while switching, "
3409 "switching to alternative address `%s' %p\n",
3410 GST_plugins_a2s (n->primary_address.address),
3411 n->primary_address.session,
3413 GST_plugins_a2s (n->alternative_address.address),
3414 n->alternative_address.session);
3416 /* Destroy the inbound address since it cannot be used */
3417 free_address (&n->primary_address);
3418 n->primary_address = n->alternative_address;
3419 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3420 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3421 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3423 case GNUNET_TRANSPORT_PS_DISCONNECT:
3424 free_address (&n->primary_address);
3426 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3427 /* neighbour was freed and plugins told to terminate session */
3431 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3432 "Unhandled state `%s'\n",
3433 GNUNET_TRANSPORT_ps2s (n->state));
3437 if (NULL != n->task)
3438 GNUNET_SCHEDULER_cancel (n->task);
3439 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3445 * We received a 'ACK' message from the other peer.
3446 * If we sent a 'SYN_ACK' last, this means we are now
3447 * connected. Otherwise, do nothing.
3449 * @param message possibly a 'struct SessionConnectMessage' (check format)
3450 * @param address address of the other peer
3451 * @param session session to use (or NULL)
3452 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3455 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3456 const struct GNUNET_HELLO_Address *address,
3457 struct Session *session)
3459 struct NeighbourMapEntry *n;
3461 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3462 "Received ACK message from peer `%s'\n",
3463 GNUNET_i2s (&address->peer));
3464 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3466 GNUNET_break_op (0);
3467 return GNUNET_SYSERR;
3469 GNUNET_STATISTICS_update (GST_stats,
3471 ("# ACK messages received"),
3473 if (NULL == (n = lookup_neighbour (&address->peer)))
3475 GNUNET_break_op (0);
3476 return GNUNET_SYSERR;
3479 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3480 "Received %s for peer `%s' in state %s/%s\n",
3482 GNUNET_i2s (&address->peer),
3483 GNUNET_TRANSPORT_ps2s (n->state),
3484 print_ack_state (n->ack_state));
3486 /* Check if we are in a plausible state for having sent
3487 a SYN_ACK. If not, return, otherwise break.
3489 The remote peers sends a ACK as a response for a SYN_ACK
3493 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3494 now wait for the ACK to finally be connected
3495 - If we sent a SYN_ACK to this peer before */
3497 if ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3498 (ACK_SEND_ACK != n->ack_state))
3500 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3501 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3502 GNUNET_i2s (&address->peer),
3503 GNUNET_TRANSPORT_ps2s (n->state),
3504 print_ack_state (n->ack_state));
3506 GNUNET_STATISTICS_update (GST_stats,
3507 gettext_noop ("# unexpected ACK messages"), 1,
3512 /* We are connected */
3513 if (GNUNET_NO == GST_neighbours_test_connected(&n->id))
3515 /* Notify about connection */
3516 neighbours_connect_notification (&n->id,
3517 n->primary_address.bandwidth_in,
3518 n->primary_address.bandwidth_out);
3519 GNUNET_STATISTICS_set (GST_stats,
3520 gettext_noop ("# peers connected"),
3521 ++neighbours_connected,
3525 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3527 /* We tried to switch addresses while being connect. We explicitly wait
3528 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3529 * so we do not want to set the address as in use! */
3533 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3534 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3536 /* Set primary address to used */
3537 set_primary_address (n,
3538 n->primary_address.address,
3539 n->primary_address.session,
3540 n->primary_address.bandwidth_in,
3541 n->primary_address.bandwidth_out,
3548 * Test if we're connected to the given peer.
3550 * @param target peer to test
3551 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3554 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3556 return test_connected (lookup_neighbour (target));
3561 * Change the incoming quota for the given peer.
3563 * @param neighbour identity of peer to change qutoa for
3564 * @param quota new quota
3567 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3568 struct GNUNET_BANDWIDTH_Value32NBO quota)
3570 struct NeighbourMapEntry *n;
3572 if (NULL == (n = lookup_neighbour (neighbour)))
3574 GNUNET_STATISTICS_update (GST_stats,
3576 ("# SET QUOTA messages ignored (no such peer)"),
3580 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3581 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3582 ntohl (quota.value__), GNUNET_i2s (&n->id));
3583 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3584 if (0 != ntohl (quota.value__))
3586 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3587 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3588 GNUNET_i2s (&n->id));
3589 if (GNUNET_YES == test_connected (n))
3590 GNUNET_STATISTICS_update (GST_stats,
3591 gettext_noop ("# disconnects due to quota of 0"),
3593 disconnect_neighbour (n);
3598 delayed_disconnect (void *cls,
3599 const struct GNUNET_SCHEDULER_TaskContext* tc)
3601 struct NeighbourMapEntry *n = cls;
3603 n->delayed_disconnect_task = NULL;
3604 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3605 "Disconnecting by request from peer %s\n",
3606 GNUNET_i2s (&n->id));
3607 free_neighbour (n, GNUNET_NO);
3612 * We received a disconnect message from the given peer,
3613 * validate and process.
3615 * @param peer sender of the message
3616 * @param msg the disconnect message
3619 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3620 const struct GNUNET_MessageHeader *msg)
3622 struct NeighbourMapEntry *n;
3623 const struct SessionDisconnectMessage *sdm;
3625 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3626 "Received DISCONNECT message from peer `%s'\n",
3628 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3630 GNUNET_break_op (0);
3631 GNUNET_STATISTICS_update (GST_stats,
3633 ("# disconnect messages ignored (malformed)"), 1,
3637 GNUNET_STATISTICS_update (GST_stats,
3639 ("# DISCONNECT messages received"),
3641 sdm = (const struct SessionDisconnectMessage *) msg;
3642 if (NULL == (n = lookup_neighbour (peer)))
3643 return; /* gone already */
3644 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3646 GNUNET_STATISTICS_update (GST_stats,
3648 ("# disconnect messages ignored (timestamp)"), 1,
3652 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3654 GNUNET_break_op (0);
3657 if (ntohl (sdm->purpose.size) !=
3658 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3659 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3660 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3662 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3663 "%s message from peer `%s' has invalid size \n",
3666 GNUNET_break_op (0);
3670 GNUNET_CRYPTO_eddsa_verify
3671 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3672 &sdm->signature, &sdm->public_key))
3674 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3675 "%s message from peer `%s' cannot be verified \n",
3678 GNUNET_break_op (0);
3681 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect, n);
3686 * Closure for the #neighbours_iterate() function.
3688 struct IteratorContext
3691 * Function to call on each connected neighbour.
3693 GST_NeighbourIterator cb;
3696 * Closure for @e cb.
3703 * Call the callback from the closure for each neighbour.
3705 * @param cls the `struct IteratorContext`
3706 * @param key the hash of the public key of the neighbour
3707 * @param value the `struct NeighbourMapEntry`
3708 * @return #GNUNET_OK (continue to iterate)
3711 neighbours_iterate (void *cls,
3712 const struct GNUNET_PeerIdentity *key,
3715 struct IteratorContext *ic = cls;
3716 struct NeighbourMapEntry *n = value;
3717 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3718 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3721 if (NULL != n->primary_address.address)
3723 bandwidth_in = n->primary_address.bandwidth_in;
3724 bandwidth_out = n->primary_address.bandwidth_out;
3728 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3729 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3733 n->primary_address.address,
3736 bandwidth_in, bandwidth_out);
3742 * Iterate over all connected neighbours.
3744 * @param cb function to call
3745 * @param cb_cls closure for cb
3748 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3750 struct IteratorContext ic;
3752 if (NULL == neighbours)
3753 return; /* can happen during shutdown */
3756 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3761 * If we have an active connection to the given target, it must be shutdown.
3763 * @param target peer to disconnect from
3766 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3768 struct NeighbourMapEntry *n;
3770 if (NULL == (n = lookup_neighbour (target)))
3771 return; /* not active */
3772 if (GNUNET_YES == test_connected (n))
3773 GNUNET_STATISTICS_update (GST_stats,
3775 ("# disconnected from peer upon explicit request"), 1,
3777 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3778 "Forced disconnect from peer %s\n",
3779 GNUNET_i2s (target));
3780 disconnect_neighbour (n);
3785 * Obtain current latency information for the given neighbour.
3787 * @param peer to get the latency for
3788 * @return observed latency of the address, FOREVER if the
3789 * the connection is not up
3791 struct GNUNET_TIME_Relative
3792 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3794 struct NeighbourMapEntry *n;
3796 n = lookup_neighbour (peer);
3798 return GNUNET_TIME_UNIT_FOREVER_REL;
3801 case GNUNET_TRANSPORT_PS_CONNECTED:
3802 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3803 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3804 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3806 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3807 case GNUNET_TRANSPORT_PS_INIT_ATS:
3808 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3809 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3810 case GNUNET_TRANSPORT_PS_SYN_SENT:
3811 case GNUNET_TRANSPORT_PS_DISCONNECT:
3812 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3813 return GNUNET_TIME_UNIT_FOREVER_REL;
3815 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3816 "Unhandled state `%s'\n",
3817 GNUNET_TRANSPORT_ps2s (n->state));
3821 return GNUNET_TIME_UNIT_FOREVER_REL;
3826 * Obtain current address information for the given neighbour.
3829 * @return address currently used
3831 struct GNUNET_HELLO_Address *
3832 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3834 struct NeighbourMapEntry *n;
3836 n = lookup_neighbour (peer);
3839 return n->primary_address.address;
3844 * Initialize the neighbours subsystem.
3846 * @param max_fds maximum number of fds to use
3849 GST_neighbours_start (unsigned int max_fds)
3851 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3852 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3853 utilization_transmission, NULL);
3858 * Disconnect from the given neighbour.
3861 * @param key hash of neighbour's public key (not used)
3862 * @param value the 'struct NeighbourMapEntry' of the neighbour
3863 * @return #GNUNET_OK (continue to iterate)
3866 disconnect_all_neighbours (void *cls,
3867 const struct GNUNET_PeerIdentity *key,
3870 struct NeighbourMapEntry *n = value;
3872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3873 "Disconnecting peer `%4s', %s\n",
3874 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3875 free_neighbour (n, GNUNET_NO);
3881 * Cleanup the neighbours subsystem.
3884 GST_neighbours_stop ()
3886 struct BlacklistCheckSwitchContext *cur;
3887 struct BlacklistCheckSwitchContext *next;
3889 if (NULL == neighbours)
3891 if (NULL != util_transmission_tk)
3893 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3894 util_transmission_tk = NULL;
3896 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3897 &disconnect_all_neighbours,
3899 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3901 next = pending_bc_head;
3902 for (cur = next; NULL != cur; cur = next)
3905 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
3909 if (NULL != cur->blc)
3911 GST_blacklist_test_cancel (cur->blc);
3914 if (NULL != cur->address)
3915 GNUNET_HELLO_address_free (cur->address);
3922 /* end of file gnunet-service-transport_neighbours.c */