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_plugins.h"
33 #include "gnunet-service-transport_validation.h"
34 #include "gnunet-service-transport.h"
35 #include "gnunet_peerinfo_service.h"
36 #include "gnunet_constants.h"
37 #include "transport.h"
42 * Size of the neighbour hash map.
44 #define NEIGHBOUR_TABLE_SIZE 256
47 * Time we give plugin to transmit DISCONNECT message before the
48 * neighbour entry self-destructs.
50 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
53 * How often must a peer violate bandwidth quotas before we start
54 * to simply drop its messages?
56 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
59 * How long are we willing to wait for a response from ATS before timing out?
61 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
64 * How long are we willing to wait for an ACK from the other peer before
65 * giving up on our connect operation?
67 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
70 * How long are we willing to wait for a successful reconnect if
71 * an existing connection went down? Much shorter than the
72 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
73 * higher layers about the disconnect during this period.
75 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
78 * How long are we willing to wait for a response from the blacklist
79 * subsystem before timing out?
81 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
84 * Interval to send utilization data
86 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
89 * State describing which kind a reply this neighbour should send
94 * We did not receive a SYN message for this neighbour
99 * The neighbour received a SYN message and has to send a SYN_ACK
102 ACK_SEND_SYN_ACK = 1,
105 * The neighbour sent a SYN_ACK message and has to send a ACK
112 GNUNET_NETWORK_STRUCT_BEGIN
115 * Message a peer sends to another to indicate that it intends to
116 * setup a connection/session for data exchange. A 'SESSION_SYN'
117 * should be answered with a 'SESSION_SYN_ACK' with the same body
118 * to confirm. A 'SESSION_SYN_ACK' should then be followed with
119 * a 'ACK'. Once the 'ACK' is received, both peers
120 * should be connected.
122 struct TransportSynMessage
125 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN
126 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK
128 struct GNUNET_MessageHeader header;
133 uint32_t reserved GNUNET_PACKED;
136 * Absolute time at the sender. Only the most recent connect
137 * message implies which session is preferred by the sender.
139 struct GNUNET_TIME_AbsoluteNBO timestamp;
145 * Message a peer sends to another when connected to indicate that a
146 * session is in use and the peer is still alive or to respond to a keep alive.
147 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
148 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
149 * When the keep alive response with type is received, transport service
150 * will call the respective plugin to update the session timeout
152 struct SessionKeepAliveMessage
155 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
156 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
158 struct GNUNET_MessageHeader header;
161 * A nonce to identify the session the keep alive is used for
163 uint32_t nonce GNUNET_PACKED;
167 * Message we send to the other peer to notify him that we intentionally
168 * are disconnecting (to reduce timeouts). This is just a friendly
169 * notification, peers must not rely on always receiving disconnect
172 struct SessionDisconnectMessage
175 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
177 struct GNUNET_MessageHeader header;
182 uint32_t reserved GNUNET_PACKED;
185 * Purpose of the signature. Extends over the timestamp.
186 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
188 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
191 * Absolute time at the sender. Only the most recent connect
192 * message implies which session is preferred by the sender.
194 struct GNUNET_TIME_AbsoluteNBO timestamp;
197 * Public key of the sender.
199 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
202 * Signature of the peer that sends us the disconnect. Only
203 * valid if the timestamp is AFTER the timestamp from the
204 * corresponding 'SYN' message.
206 struct GNUNET_CRYPTO_EddsaSignature signature;
210 GNUNET_NETWORK_STRUCT_END
214 * For each neighbour we keep a list of messages
215 * that we still want to transmit to the neighbour.
221 * This is a doubly linked list.
223 struct MessageQueue *next;
226 * This is a doubly linked list.
228 struct MessageQueue *prev;
231 * Function to call once we're done.
233 GST_NeighbourSendContinuation cont;
236 * Closure for @e cont
241 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
242 * stuck together in memory. Allocated at the end of this struct.
244 const char *message_buf;
247 * Size of the message buf
249 size_t message_buf_size;
252 * At what time should we fail?
254 struct GNUNET_TIME_Absolute timeout;
260 * A possible address we could use to communicate with a neighbour.
262 struct NeighbourAddress
266 * Active session for this address.
268 struct Session *session;
271 * Network-level address information.
273 struct GNUNET_HELLO_Address *address;
276 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
277 * peer for this address. Use to check that the ACK is in response
278 * to our most recent 'SYN'.
280 struct GNUNET_TIME_Absolute connect_timestamp;
283 * Inbound bandwidth from ATS for this address.
285 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
288 * Outbound bandwidth from ATS for this address.
290 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
293 * Did we tell ATS that this is our 'active' address?
298 * The current nonce sent in the last keep alive messages
300 uint32_t keep_alive_nonce;
305 * Entry in neighbours.
307 struct NeighbourMapEntry
311 * Head of list of messages we would like to send to this peer;
312 * must contain at most one message per client.
314 struct MessageQueue *messages_head;
317 * Tail of list of messages we would like to send to this peer; must
318 * contain at most one message per client.
320 struct MessageQueue *messages_tail;
323 * Are we currently trying to send a message? If so, which one?
325 struct MessageQueue *is_active;
328 * Primary address we currently use to communicate with the neighbour.
330 struct NeighbourAddress primary_address;
333 * Alternative address currently under consideration for communicating
334 * with the neighbour.
336 struct NeighbourAddress alternative_address;
339 * Identity of this neighbour.
341 struct GNUNET_PeerIdentity id;
344 * Main task that drives this peer (timeouts, keepalives, etc.).
345 * Always runs the 'master_task'.
347 struct GNUNET_SCHEDULER_Task *task;
350 * Task to disconnect neighbour after we received a DISCONNECT message
352 struct GNUNET_SCHEDULER_Task *delayed_disconnect_task;
355 * At what time should we sent the next keep-alive message?
357 struct GNUNET_TIME_Absolute keep_alive_time;
360 * At what time did we sent the last keep-alive message? Used
361 * to calculate round-trip time ("latency").
363 struct GNUNET_TIME_Absolute last_keep_alive_time;
366 * Timestamp we should include in our next SYN_ACK message.
367 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
368 * our SYN_ACK message.
370 struct GNUNET_TIME_Absolute connect_ack_timestamp;
373 * ATS address suggest handle
375 struct GNUNET_ATS_SuggestHandle *suggest_handle;
378 * Time where we should cut the connection (timeout) if we don't
379 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
380 * if we are in #S_CONNECTED).
382 struct GNUNET_TIME_Absolute timeout;
385 * Latest calculated latency value
387 struct GNUNET_TIME_Relative latency;
390 * Tracker for inbound bandwidth.
392 struct GNUNET_BANDWIDTH_Tracker in_tracker;
395 * How often has the other peer (recently) violated the inbound
396 * traffic limit? Incremented by 10 per violation, decremented by 1
397 * per non-violation (for each time interval).
399 unsigned int quota_violation_count;
402 * The current state of the peer.
404 enum GNUNET_TRANSPORT_PeerState state;
407 * Did we sent an KEEP_ALIVE message and are we expecting a response?
409 int expect_latency_response;
412 * When a peer wants to connect we have to reply to the 1st SYN message
413 * with a SYN_ACK message. But sometime we cannot send this message
414 * immediately since we do not have an address and then we have to remember
415 * to send this message as soon as we have an address.
417 * Flag to set if we still need to send a SYN_ACK message to the other peer
418 * (once we have an address to use and the peer has been allowed by our
419 * blacklist). Initially set to #ACK_UNDEFINED. Set to #ACK_SEND_SYN_ACK
420 * if we need to send a SYN_ACK. Set to #ACK_SEND_ACK if we did
421 * send a SYN_ACK and should go to #S_CONNECTED upon receiving a
422 * 'ACK' (regardless of what our own state machine might say).
424 enum GST_ACK_State ack_state;
427 * Tracking utilization of outbound bandwidth
429 uint32_t util_payload_bytes_sent;
432 * Tracking utilization of inbound bandwidth
434 uint32_t util_payload_bytes_recv;
437 * Tracking utilization of outbound bandwidth
439 uint32_t util_total_bytes_sent;
442 * Tracking utilization of inbound bandwidth
444 uint32_t util_total_bytes_recv;
447 * Date of last utilization transmission
449 struct GNUNET_TIME_Absolute last_util_transmission;
454 * Context for blacklist checks and the #try_connect_bl_check_cont()
455 * function. Stores information about ongoing blacklist checks.
457 struct BlackListCheckContext
461 * We keep blacklist checks in a DLL.
463 struct BlackListCheckContext *next;
466 * We keep blacklist checks in a DLL.
468 struct BlackListCheckContext *prev;
471 * Address that is being checked.
473 struct NeighbourAddress na;
476 * Handle to the ongoing blacklist check.
478 struct GST_BlacklistCheck *bc;
483 * Hash map from peer identities to the respective `struct NeighbourMapEntry`.
485 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
488 * We keep blacklist checks in a DLL so that we can find
489 * the 'sessions' in their 'struct NeighbourAddress' if
490 * a session goes down.
492 static struct BlackListCheckContext *bc_head;
495 * We keep blacklist checks in a DLL.
497 static struct BlackListCheckContext *bc_tail;
500 * List of pending blacklist checks: head
502 static struct BlacklistCheckSwitchContext *pending_bc_head;
505 * List of pending blacklist checks: tail
507 static struct BlacklistCheckSwitchContext *pending_bc_tail;
510 * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb
512 static void *callback_cls;
515 * Function to call when we connected to a neighbour.
517 static NotifyConnect connect_notify_cb;
520 * Function to call when we disconnected from a neighbour.
522 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
525 * Function to call when a neighbour changed address, state or bandwidth.
527 static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb;
530 * counter for connected neighbours
532 static unsigned int neighbours_connected;
535 * Number of bytes we have currently queued for transmission.
537 static unsigned long long bytes_in_send_queue;
540 * Task transmitting utilization data
542 static struct GNUNET_SCHEDULER_Task * util_transmission_tk;
547 static struct GNUNET_CONTAINER_MultiPeerMap *registered_quota_notifications;
554 print_ack_state (enum GST_ACK_State s)
559 case ACK_SEND_SYN_ACK:
560 return "SEND_SYN_ACK";
570 * Lookup a neighbour entry in the neighbours hash map.
572 * @param pid identity of the peer to look up
573 * @return the entry, NULL if there is no existing record
575 static struct NeighbourMapEntry *
576 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
578 if (NULL == neighbours)
580 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
585 * Test if we're connected to the given peer.
587 * @param n neighbour entry of peer to test
588 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
591 test_connected (struct NeighbourMapEntry *n)
595 return GNUNET_TRANSPORT_is_connected (n->state);
600 * Send information about a new outbound quota to our clients.
602 * @param target affected peer
603 * @param quota new quota
606 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
607 struct GNUNET_BANDWIDTH_Value32NBO quota)
609 struct QuotaSetMessage q_msg;
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
613 ntohl (quota.value__), GNUNET_i2s (target));
614 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
615 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
617 q_msg.peer = (*target);
618 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
623 * We don't need a given neighbour address any more.
624 * Release its resources and give appropriate notifications
625 * to ATS and other subsystems.
627 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
630 free_address (struct NeighbourAddress *na)
632 if (GNUNET_YES == na->ats_active)
634 GST_validation_set_address_use (na->address,
637 GST_ats_set_in_use (na->address,
642 na->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
643 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
644 na->ats_active = GNUNET_NO;
645 na->keep_alive_nonce = 0;
646 if (NULL != na->address)
648 GNUNET_HELLO_address_free (na->address);
656 * Set net state for this neighbour and notify monitoring
658 * @param n the respective neighbour
659 * @param s the new state
662 set_state (struct NeighbourMapEntry *n,
663 enum GNUNET_TRANSPORT_PeerState s)
666 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
667 "Neighbour `%s' changed state to %s\n",
669 GNUNET_TRANSPORT_ps2s(s));
670 neighbour_change_cb (callback_cls,
672 n->primary_address.address,
675 n->primary_address.bandwidth_in,
676 n->primary_address.bandwidth_out);
681 * Set net state and state timeout for this neighbour and notify monitoring
683 * @param n the respective neighbour
684 * @param s the new state
685 * @param timeout the new timeout
688 set_state_and_timeout (struct NeighbourMapEntry *n,
689 enum GNUNET_TRANSPORT_PeerState s,
690 struct GNUNET_TIME_Absolute timeout)
693 n->timeout = timeout;
694 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
695 "Neighbour `%s' changed state to %s with timeout %s\n",
697 GNUNET_TRANSPORT_ps2s(s),
698 GNUNET_STRINGS_absolute_time_to_string (timeout));
699 neighbour_change_cb (callback_cls,
701 n->primary_address.address,
704 n->primary_address.bandwidth_in,
705 n->primary_address.bandwidth_out);
710 * Set new state timeout for this neighbour and notify monitoring
712 * @param n the respective neighbour
713 * @param timeout the new timeout
716 set_timeout (struct NeighbourMapEntry *n,
717 struct GNUNET_TIME_Absolute timeout)
719 n->timeout = timeout;
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
721 "Neighbour `%s' changed timeout %s\n",
723 GNUNET_STRINGS_absolute_time_to_string (timeout));
724 neighbour_change_cb (callback_cls,
726 n->primary_address.address,
727 n->state, n->timeout,
728 n->primary_address.bandwidth_in,
729 n->primary_address.bandwidth_out);
734 * Initialize the alternative address of a neighbour
736 * @param n the neighbour
737 * @param address address of the other peer, NULL if other peer
739 * @param session session to use (or NULL, in which case an
740 * address must be setup)
741 * @param bandwidth_in inbound quota to be used when connection is up
742 * @param bandwidth_out outbound quota to be used when connection is up
745 set_alternative_address (struct NeighbourMapEntry *n,
746 const struct GNUNET_HELLO_Address *address,
747 struct Session *session,
748 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
749 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
751 struct GNUNET_TRANSPORT_PluginFunctions *papi;
753 if (NULL == (papi = GST_plugins_find (address->transport_name)))
758 if (session == n->alternative_address.session)
760 n->alternative_address.bandwidth_in = bandwidth_in;
761 n->alternative_address.bandwidth_out = bandwidth_out;
764 free_address (&n->alternative_address);
766 session = papi->get_session (papi->cls, address);
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
770 "Failed to obtain new session for peer `%s' and address '%s'\n",
771 GNUNET_i2s (&address->peer),
772 GST_plugins_a2s (address));
773 GNUNET_STATISTICS_update (GST_stats,
774 gettext_noop ("# session creation failed"),
779 GST_ats_new_session (address, session);
780 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
781 "Neighbour `%s' configured alternative address %s\n",
783 GST_plugins_a2s(address));
785 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
786 n->alternative_address.bandwidth_in = bandwidth_in;
787 n->alternative_address.bandwidth_out = bandwidth_out;
788 n->alternative_address.session = session;
789 n->alternative_address.ats_active = GNUNET_NO;
790 n->alternative_address.keep_alive_nonce = 0;
795 * Initialize the primary address of a neighbour
797 * @param n the neighbour
798 * @param address address of the other peer, NULL if other peer
800 * @param session session to use (or NULL, in which case an
801 * address must be setup)
802 * @param bandwidth_in inbound quota to be used when connection is up
803 * @param bandwidth_out outbound quota to be used when connection is up
804 * @param is_active #GNUNET_YES to mark this as the active address with ATS
807 set_primary_address (struct NeighbourMapEntry *n,
808 const struct GNUNET_HELLO_Address *address,
809 struct Session *session,
810 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
811 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
814 struct GNUNET_TRANSPORT_PluginFunctions *papi;
816 if (NULL == (papi = GST_plugins_find (address->transport_name)))
821 if (session == n->primary_address.session)
823 n->primary_address.bandwidth_in = bandwidth_in;
824 n->primary_address.bandwidth_out = bandwidth_out;
825 if (is_active != n->primary_address.ats_active)
827 n->primary_address.ats_active = is_active;
828 GST_ats_set_in_use (n->primary_address.address,
829 n->primary_address.session,
831 GST_validation_set_address_use (n->primary_address.address,
832 n->primary_address.session,
835 if (GNUNET_YES == is_active)
837 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
838 send_outbound_quota (&address->peer, bandwidth_out);
842 free_address (&n->primary_address);
844 session = papi->get_session (papi->cls, address);
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Failed to obtain new session for peer `%s' and address '%s'\n",
849 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
850 GNUNET_STATISTICS_update (GST_stats,
851 gettext_noop ("# session creation failed"),
856 GST_ats_new_session (address, session);
857 n->primary_address.address = GNUNET_HELLO_address_copy (address);
858 n->primary_address.bandwidth_in = bandwidth_in;
859 n->primary_address.bandwidth_out = bandwidth_out;
860 n->primary_address.session = session;
861 n->primary_address.ats_active = is_active;
862 n->primary_address.keep_alive_nonce = 0;
863 if (GNUNET_YES == is_active)
865 /* Telling ATS about new session */
866 GST_ats_set_in_use (n->primary_address.address,
867 n->primary_address.session,
869 GST_validation_set_address_use (n->primary_address.address,
870 n->primary_address.session,
872 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
873 send_outbound_quota (&address->peer, bandwidth_out);
876 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
877 "Neighbour `%s' switched to address `%s'\n",
879 GST_plugins_a2s(address));
881 neighbour_change_cb (callback_cls,
883 n->primary_address.address,
886 n->primary_address.bandwidth_in,
887 n->primary_address.bandwidth_out);
892 * Clear the primary address of a neighbour since this address is not
893 * valid anymore and notify monitoring about it
895 * @param n the neighbour
898 unset_primary_address (struct NeighbourMapEntry *n)
900 /* Notify monitoring about change */
901 neighbour_change_cb (callback_cls,
903 n->primary_address.address,
906 GNUNET_BANDWIDTH_value_init (0),
907 GNUNET_BANDWIDTH_value_init (0));
908 /* Unset primary address */
909 free_address (&n->primary_address);
914 * Clear the alternative address of a neighbour since this address is not
917 * @param n the neighbour
920 unset_alternative_address (struct NeighbourMapEntry *n)
922 /* Unset primary address */
923 free_address (&n->alternative_address);
928 * Free a neighbour map entry.
930 * @param n entry to free
931 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
932 * #GNUNET_YES to keep all sessions
935 free_neighbour (struct NeighbourMapEntry *n,
938 struct MessageQueue *mq;
939 struct GNUNET_TRANSPORT_PluginFunctions *papi;
940 struct GNUNET_HELLO_Address *backup_primary;
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "Freeing neighbour state of peer `%s'\n",
944 GNUNET_i2s (&n->id));
945 n->is_active = NULL; /* always free'd by its own continuation! */
947 /* fail messages currently in the queue */
948 while (NULL != (mq = n->messages_head))
950 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
951 if (NULL != mq->cont)
952 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
955 /* It is too late to send other peer disconnect notifications, but at
956 least internally we need to get clean... */
957 if (GNUNET_YES == test_connected (n))
959 GNUNET_STATISTICS_set (GST_stats,
960 gettext_noop ("# peers connected"),
961 --neighbours_connected,
963 disconnect_notify_cb (callback_cls, &n->id);
966 /* Mark peer as disconnected */
967 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
969 if (NULL != n->primary_address.address)
970 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
972 backup_primary = NULL;
974 /* free addresses and mark as unused */
975 unset_primary_address (n);
976 free_address (&n->alternative_address);
978 /* cut all transport-level connection for this peer */
979 // FIXME: might want to revisit this; maybe just
980 // shorten session timeout on plugin level?
981 if ((GNUNET_NO == keep_sessions) &&
982 (NULL != backup_primary) &&
983 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
984 papi->disconnect_peer (papi->cls, &n->id);
986 if (NULL != backup_primary)
987 GNUNET_HELLO_address_free (backup_primary);
988 GNUNET_assert (GNUNET_YES ==
989 GNUNET_CONTAINER_multipeermap_remove (neighbours,
992 /* Cancel address requests for this peer */
993 if (NULL != n->suggest_handle)
995 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
996 n->suggest_handle = NULL;
999 /* Cancel the disconnect task */
1000 if (NULL != n->delayed_disconnect_task)
1002 GNUNET_SCHEDULER_cancel (n->delayed_disconnect_task);
1003 n->delayed_disconnect_task = NULL;
1006 /* Cancel the master task */
1007 if (NULL != n->task)
1009 GNUNET_SCHEDULER_cancel (n->task);
1012 /* free rest of memory */
1018 * Transmit a message using the current session of the given
1021 * @param n entry for the recipient
1022 * @param msgbuf buffer to transmit
1023 * @param msgbuf_size number of bytes in @a msgbuf buffer
1024 * @param priority transmission priority
1025 * @param timeout transmission timeout
1026 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
1027 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
1028 * @param cont continuation to call when finished (can be NULL)
1029 * @param cont_cls closure for @a cont
1030 * @return timeout (copy of @a timeout or a calculated one if
1031 * @a use_keepalive_timeout is #GNUNET_YES.
1033 static struct GNUNET_TIME_Relative
1034 send_with_session (struct NeighbourMapEntry *n,
1035 const char *msgbuf, size_t msgbuf_size,
1037 struct GNUNET_TIME_Relative timeout,
1038 unsigned int use_keepalive_timeout,
1039 GNUNET_TRANSPORT_TransmitContinuation cont,
1042 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1043 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
1045 GNUNET_assert (n->primary_address.session != NULL);
1046 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1047 (-1 == papi->send (papi->cls,
1048 n->primary_address.session,
1049 msgbuf, msgbuf_size,
1051 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
1052 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1053 papi->query_keepalive_factor (papi->cls))),
1054 cont, cont_cls)))) &&
1056 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1057 GST_neighbours_notify_data_sent (n->primary_address.address,
1058 n->primary_address.session,
1060 GNUNET_break (NULL != papi);
1066 * Master task run for every neighbour. Performs all of the time-related
1067 * activities (keep alive, send next message, disconnect if idle, finish
1068 * clean up after disconnect).
1070 * @param cls the `struct NeighbourMapEntry` for which we are running
1071 * @param tc scheduler context (unused)
1074 master_task (void *cls,
1075 const struct GNUNET_SCHEDULER_TaskContext *tc);
1079 * Function called when the 'DISCONNECT' message has been sent by the
1080 * plugin. Frees the neighbour --- if the entry still exists.
1083 * @param target identity of the neighbour that was disconnected
1084 * @param result #GNUNET_OK if the disconnect got out successfully
1085 * @param payload bytes payload
1086 * @param physical bytes physical
1089 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1090 int result, size_t payload, size_t physical)
1092 struct NeighbourMapEntry *n;
1094 n = lookup_neighbour (target);
1096 return; /* already gone */
1097 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
1098 return; /* have created a fresh entry since */
1099 if (NULL != n->task)
1100 GNUNET_SCHEDULER_cancel (n->task);
1101 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1106 * Transmit a DISCONNECT message to the other peer.
1108 * @param n neighbour to send DISCONNECT message.
1111 send_disconnect (struct NeighbourMapEntry *n)
1113 struct SessionDisconnectMessage disconnect_msg;
1115 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1116 "Sending DISCONNECT message to peer `%4s'\n",
1117 GNUNET_i2s (&n->id));
1118 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1119 disconnect_msg.header.type =
1120 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1121 disconnect_msg.reserved = htonl (0);
1122 disconnect_msg.purpose.size =
1123 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1124 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1125 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1126 disconnect_msg.purpose.purpose =
1127 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1128 disconnect_msg.timestamp =
1129 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1130 disconnect_msg.public_key = GST_my_identity.public_key;
1131 GNUNET_assert (GNUNET_OK ==
1132 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1133 &disconnect_msg.purpose,
1134 &disconnect_msg.signature));
1136 (void) send_with_session (n, (const char *) &disconnect_msg,
1137 sizeof (disconnect_msg), UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1138 GNUNET_NO, &send_disconnect_cont, NULL );
1139 GNUNET_STATISTICS_update (GST_stats,
1141 ("# DISCONNECT messages sent"), 1,
1147 * Disconnect from the given neighbour, clean up the record.
1149 * @param n neighbour to disconnect from
1152 disconnect_neighbour (struct NeighbourMapEntry *n)
1154 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1155 "Disconnecting from peer %s in state %s\n",
1156 GNUNET_i2s (&n->id),
1157 GNUNET_TRANSPORT_ps2s (n->state));
1158 /* depending on state, notify neighbour and/or upper layers of this peer
1162 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1163 case GNUNET_TRANSPORT_PS_INIT_ATS:
1164 /* other peer is completely unaware of us, no need to send DISCONNECT */
1165 free_neighbour (n, GNUNET_NO);
1167 case GNUNET_TRANSPORT_PS_SYN_SENT:
1168 send_disconnect (n);
1169 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1171 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
1172 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1173 free_neighbour (n, GNUNET_NO);
1175 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
1176 /* we DID ACK the other peer's request, must send DISCONNECT */
1177 send_disconnect (n);
1178 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1180 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1181 case GNUNET_TRANSPORT_PS_CONNECTED:
1182 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1183 /* we are currently connected, need to send disconnect and do
1184 internal notifications and update statistics */
1185 send_disconnect (n);
1186 GNUNET_STATISTICS_set (GST_stats,
1187 gettext_noop ("# peers connected"),
1188 --neighbours_connected,
1190 disconnect_notify_cb (callback_cls, &n->id);
1191 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1193 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1194 /* Disconnecting while waiting for an ATS address to reconnect,
1195 * cannot send DISCONNECT */
1196 free_neighbour (n, GNUNET_NO);
1198 case GNUNET_TRANSPORT_PS_DISCONNECT:
1199 /* already disconnected, ignore */
1201 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1202 /* already cleaned up, how did we get here!? */
1206 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1207 "Unhandled state `%s'\n",
1208 GNUNET_TRANSPORT_ps2s (n->state));
1212 /* schedule timeout to clean up */
1213 if (NULL != n->task)
1214 GNUNET_SCHEDULER_cancel (n->task);
1215 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1221 * We're done with our transmission attempt, continue processing.
1223 * @param cls the `struct MessageQueue` of the message
1224 * @param receiver intended receiver
1225 * @param success whether it worked or not
1226 * @param size_payload bytes payload sent
1227 * @param physical bytes sent on wire
1230 transmit_send_continuation (void *cls,
1231 const struct GNUNET_PeerIdentity *receiver,
1232 int success, size_t size_payload, size_t physical)
1234 struct MessageQueue *mq = cls;
1235 struct NeighbourMapEntry *n;
1237 if (NULL == (n = lookup_neighbour (receiver)))
1240 return; /* disconnect or other error while transmitting, can happen */
1242 if (n->is_active == mq)
1244 /* this is still "our" neighbour, remove us from its queue
1245 and allow it to send the next message now */
1246 n->is_active = NULL;
1247 if (NULL != n->task)
1248 GNUNET_SCHEDULER_cancel (n->task);
1249 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1251 if (bytes_in_send_queue < mq->message_buf_size)
1253 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1254 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1255 bytes_in_send_queue, mq->message_buf_size,
1256 (GNUNET_OK == success) ? "OK" : "FAIL",
1257 size_payload, physical);
1262 GNUNET_break (size_payload == mq->message_buf_size);
1263 bytes_in_send_queue -= mq->message_buf_size;
1264 GNUNET_STATISTICS_set (GST_stats,
1266 ("# bytes in message queue for other peers"),
1267 bytes_in_send_queue, GNUNET_NO);
1268 if (GNUNET_OK == success)
1269 GNUNET_STATISTICS_update (GST_stats,
1271 ("# messages transmitted to other peers"),
1274 GNUNET_STATISTICS_update (GST_stats,
1276 ("# transmission failures for messages to other peers"),
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Sending message to `%s' of type %u with %u bytes was a %s\n",
1280 GNUNET_i2s (receiver),
1281 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1282 mq->message_buf_size,
1283 (success == GNUNET_OK) ? "success" : "FAILURE");
1284 if (NULL != mq->cont)
1285 mq->cont (mq->cont_cls, success, size_payload, physical);
1291 * Check the message list for the given neighbour and if we can
1292 * send a message, do so. This function should only be called
1293 * if the connection is at least generally ready for transmission.
1294 * While we will only send one message at a time, no bandwidth
1295 * quota management is performed here. If a message was given to
1296 * the plugin, the continuation will automatically re-schedule
1297 * the 'master' task once the next message might be transmitted.
1299 * @param n target peer for which to transmit
1302 try_transmission_to_peer (struct NeighbourMapEntry *n)
1304 struct MessageQueue *mq;
1305 struct GNUNET_TIME_Relative timeout;
1307 if (NULL == n->primary_address.address)
1309 /* no address, why are we here? */
1313 if ((0 == n->primary_address.address->address_length) &&
1314 (NULL == n->primary_address.session))
1316 /* no address, why are we here? */
1320 if (NULL != n->is_active)
1322 /* transmission already pending */
1326 /* timeout messages from the queue that are past their due date */
1327 while (NULL != (mq = n->messages_head))
1329 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1330 if (timeout.rel_value_us > 0)
1332 GNUNET_STATISTICS_update (GST_stats,
1334 ("# messages timed out while in transport queue"),
1336 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1338 transmit_send_continuation (mq, &n->id,
1340 mq->message_buf_size, 0); /* timeout */
1343 return; /* no more messages */
1344 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 "Giving message with %u bytes to plugin session %p\n",
1349 mq->message_buf_size, n->primary_address.session);
1351 (void) send_with_session (n,
1352 mq->message_buf, mq->message_buf_size,
1353 0 /* priority */, timeout, GNUNET_NO,
1354 &transmit_send_continuation, mq);
1359 * Send keepalive message to the neighbour. Must only be called
1360 * if we are on 'connected' state or while trying to switch addresses.
1361 * Will internally determine if a keepalive is truly needed (so can
1362 * always be called).
1364 * @param n neighbour that went idle and needs a keepalive
1367 send_keepalive (struct NeighbourMapEntry *n)
1369 struct SessionKeepAliveMessage m;
1370 struct GNUNET_TIME_Relative timeout;
1373 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1374 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT));
1375 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1376 return; /* no keepalive needed at this time */
1378 nonce = 0; /* 0 indicates 'not set' */
1380 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1382 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1383 "Sending keep alive to peer `%s' with nonce %u\n",
1384 GNUNET_i2s (&n->id), nonce);
1386 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1387 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1388 m.nonce = htonl (nonce);
1390 timeout = send_with_session (n,
1391 (const void *) &m, sizeof (m),
1392 UINT32_MAX /* priority */,
1393 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1395 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1397 n->primary_address.keep_alive_nonce = nonce;
1398 n->expect_latency_response = GNUNET_YES;
1399 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1400 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1406 * Keep the connection to the given neighbour alive longer,
1407 * we received a KEEPALIVE (or equivalent); send a response.
1409 * @param neighbour neighbour to keep alive (by sending keep alive response)
1410 * @param m the keep alive message containing the nonce to respond to
1413 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1414 const struct GNUNET_MessageHeader *m)
1416 struct NeighbourMapEntry *n;
1417 const struct SessionKeepAliveMessage *msg_in;
1418 struct SessionKeepAliveMessage msg;
1420 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1423 msg_in = (struct SessionKeepAliveMessage *) m;
1424 if (NULL == (n = lookup_neighbour (neighbour)))
1426 GNUNET_STATISTICS_update (GST_stats,
1428 ("# KEEPALIVE messages discarded (peer unknown)"),
1432 if (NULL == n->primary_address.session)
1434 GNUNET_STATISTICS_update (GST_stats,
1436 ("# KEEPALIVE messages discarded (no session)"),
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Received keep alive request from peer `%s' with nonce %u\n",
1443 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1445 /* send reply to allow neighbour to measure latency */
1446 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1447 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1448 msg.nonce = msg_in->nonce;
1449 (void) send_with_session(n,
1450 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1451 UINT32_MAX /* priority */,
1452 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1458 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1459 * latency to this peer. Pass the updated information (existing ats
1460 * plus calculated latency) to ATS.
1462 * @param neighbour neighbour to keep alive
1463 * @param m the message containing the keep alive response
1466 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1467 const struct GNUNET_MessageHeader *m)
1469 struct NeighbourMapEntry *n;
1470 const struct SessionKeepAliveMessage *msg;
1471 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1473 struct GNUNET_ATS_Information ats;
1475 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1478 msg = (const struct SessionKeepAliveMessage *) m;
1479 if (NULL == (n = lookup_neighbour (neighbour)))
1481 GNUNET_STATISTICS_update (GST_stats,
1483 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1487 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1488 (GNUNET_YES != n->expect_latency_response) )
1490 GNUNET_STATISTICS_update (GST_stats,
1492 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1496 if (NULL == n->primary_address.address)
1498 GNUNET_STATISTICS_update (GST_stats,
1500 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1504 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1506 GNUNET_STATISTICS_update (GST_stats,
1508 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1514 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1515 "Received keep alive response from peer `%s' for session %p\n",
1516 GNUNET_i2s (&n->id), n->primary_address.session);
1520 /* Update session timeout here */
1521 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "Updating session for peer `%s' for session %p\n",
1525 GNUNET_i2s (&n->id), n->primary_address.session);
1526 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1533 n->primary_address.keep_alive_nonce = 0;
1534 n->expect_latency_response = GNUNET_NO;
1535 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1536 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1538 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1539 "Latency for peer `%s' is %s\n",
1540 GNUNET_i2s (&n->id),
1541 GNUNET_STRINGS_relative_time_to_string (n->latency,
1543 /* append latency */
1544 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1545 if (n->latency.rel_value_us > UINT32_MAX)
1546 latency = UINT32_MAX;
1548 latency = n->latency.rel_value_us;
1549 ats.value = htonl (latency);
1550 GST_ats_update_metrics (n->primary_address.address,
1551 n->primary_address.session,
1557 * We have received a message from the given sender. How long should
1558 * we delay before receiving more? (Also used to keep the peer marked
1561 * @param sender sender of the message
1562 * @param size size of the message
1563 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1564 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1565 * #GNUNET_SYSERR if the connection is not fully up yet
1566 * @return how long to wait before reading more from this sender
1568 struct GNUNET_TIME_Relative
1569 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1570 *sender, ssize_t size, int *do_forward)
1572 struct NeighbourMapEntry *n;
1573 struct GNUNET_TIME_Relative ret;
1575 if (NULL == neighbours)
1577 *do_forward = GNUNET_NO;
1578 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1580 if (NULL == (n = lookup_neighbour (sender)))
1582 GST_neighbours_try_connect (sender);
1583 if (NULL == (n = lookup_neighbour (sender)))
1585 GNUNET_STATISTICS_update (GST_stats,
1587 ("# messages discarded due to lack of neighbour record"),
1589 *do_forward = GNUNET_NO;
1590 return GNUNET_TIME_UNIT_ZERO;
1593 if (! test_connected (n))
1595 *do_forward = GNUNET_SYSERR;
1596 return GNUNET_TIME_UNIT_ZERO;
1598 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1600 n->quota_violation_count++;
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1603 n->in_tracker.available_bytes_per_s__,
1604 n->quota_violation_count);
1605 /* Discount 32k per violation */
1606 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1610 if (n->quota_violation_count > 0)
1612 /* try to add 32k back */
1613 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1614 n->quota_violation_count--;
1617 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1619 GNUNET_STATISTICS_update (GST_stats,
1621 ("# bandwidth quota violations by other peers"),
1623 *do_forward = GNUNET_NO;
1624 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1626 *do_forward = GNUNET_YES;
1627 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1628 if (ret.rel_value_us > 0)
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1632 (unsigned long long) n->in_tracker.
1633 consumption_since_last_update__,
1634 (unsigned int) n->in_tracker.available_bytes_per_s__,
1635 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1636 GNUNET_STATISTICS_update (GST_stats,
1637 gettext_noop ("# ms throttling suggested"),
1638 (int64_t) ret.rel_value_us / 1000LL,
1646 * Transmit a message to the given target using the active connection.
1648 * @param target destination
1649 * @param msg message to send
1650 * @param msg_size number of bytes in msg
1651 * @param timeout when to fail with timeout
1652 * @param cont function to call when done
1653 * @param cont_cls closure for 'cont'
1656 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1657 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1658 GST_NeighbourSendContinuation cont, void *cont_cls)
1660 struct NeighbourMapEntry *n;
1661 struct MessageQueue *mq;
1663 /* All ove these cases should never happen; they are all API violations.
1664 But we check anyway, just to be sure. */
1665 if (NULL == (n = lookup_neighbour (target)))
1669 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1672 if (GNUNET_YES != test_connected (n))
1676 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1679 bytes_in_send_queue += msg_size;
1680 GNUNET_STATISTICS_set (GST_stats,
1682 ("# bytes in message queue for other peers"),
1683 bytes_in_send_queue, GNUNET_NO);
1684 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1686 mq->cont_cls = cont_cls;
1687 memcpy (&mq[1], msg, msg_size);
1688 mq->message_buf = (const char *) &mq[1];
1689 mq->message_buf_size = msg_size;
1690 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Enqueueing %u bytes to send to peer %s\n",
1693 msg_size, GNUNET_i2s (target));
1695 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1696 if (NULL != n->task)
1697 GNUNET_SCHEDULER_cancel (n->task);
1698 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1703 send_session_connect_cont (void *cls,
1704 const struct GNUNET_PeerIdentity *target,
1706 size_t size_payload,
1707 size_t size_on_wire)
1709 struct NeighbourMapEntry *n;
1711 n = lookup_neighbour (target);
1714 /* SYN continuation was called after neighbor was freed,
1715 * for example due to a time out for the state or the session
1716 * used was already terminated: nothing to do here... */
1720 if ( (GNUNET_TRANSPORT_PS_SYN_SENT != n->state) &&
1721 (GNUNET_TRANSPORT_PS_RECONNECT_SENT != n->state) &&
1722 (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT != n->state))
1724 /* SYN continuation was called after neighbor changed state,
1725 * for example due to a time out for the state or the session
1726 * used was already terminated: nothing to do here... */
1729 if (GNUNET_OK == result)
1732 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1733 _("Failed to send SYN message to peer `%s' using address `%s' session %p\n"),
1734 GNUNET_i2s (target),
1735 GST_plugins_a2s (n->primary_address.address),
1736 n->primary_address.session);
1739 case GNUNET_TRANSPORT_PS_SYN_SENT:
1740 /* Remove address and request and additional one */
1741 unset_primary_address (n);
1742 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1743 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1745 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1746 /* Remove address and request and additional one */
1747 unset_primary_address (n);
1748 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1749 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1751 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1752 /* Remove address and request and go back to primary address */
1753 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1754 ("# Failed attempts to switch addresses (failed to send SYN CONT)"), 1, GNUNET_NO);
1755 unset_alternative_address (n);
1756 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1757 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1760 disconnect_neighbour (n);
1766 * Send a SYN message via the given address.
1768 * @param na address to use
1771 send_syn (struct NeighbourAddress *na)
1773 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1774 struct TransportSynMessage connect_msg;
1775 struct NeighbourMapEntry *n;
1777 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1778 "Sending SYN message to peer `%s'\n",
1779 GNUNET_i2s (&na->address->peer));
1781 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1786 if (NULL == na->session)
1787 na->session = papi->get_session (papi->cls, na->address);
1788 if (NULL == na->session)
1793 GST_ats_new_session (na->address,
1795 GNUNET_STATISTICS_update (GST_stats,
1797 ("# SYN messages sent"),
1799 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1800 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1801 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN);
1802 connect_msg.reserved = htonl (0);
1803 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1805 papi->send (papi->cls,
1807 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
1809 SETUP_CONNECTION_TIMEOUT,
1810 send_session_connect_cont, NULL))
1812 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1813 _("Failed to transmit SYN message via plugin to %s\n"),
1814 GST_plugins_a2s (na->address));
1816 n = lookup_neighbour (&na->address->peer);
1824 case GNUNET_TRANSPORT_PS_SYN_SENT:
1825 /* Remove address and request and additional one */
1826 unset_primary_address (n);
1827 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1828 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
1829 /* Hard failure to send the SYN message with this address:
1830 Destroy address and session */
1832 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1833 /* Remove address and request and additional one */
1834 unset_primary_address (n);
1835 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
1836 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1838 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
1839 GNUNET_STATISTICS_update (GST_stats, gettext_noop
1840 ("# Failed attempts to switch addresses (failed to send SYN)"), 1, GNUNET_NO);
1841 /* Remove address and request and additional one */
1842 unset_alternative_address (n);
1843 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
1844 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1847 disconnect_neighbour (n);
1851 GST_neighbours_notify_data_sent (na->address,
1853 sizeof (struct TransportSynMessage));
1858 send_session_connect_ack_cont (void *cls,
1859 const struct GNUNET_PeerIdentity *target,
1861 size_t size_payload,
1862 size_t size_on_wire)
1864 struct NeighbourMapEntry *n;
1866 n = lookup_neighbour (target);
1869 /* SYN_ACK continuation was called after neighbor was freed,
1870 * for example due to a time out for the state or the session
1871 * used was already terminated: nothing to do here... */
1875 if (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state)
1877 /* SYN_ACK continuation was called after neighbor changed state,
1878 * for example due to a time out for the state or the session
1879 * used was already terminated: nothing to do here... */
1882 if (GNUNET_OK == result)
1885 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1886 _("Failed to send SYN_ACK message to peer `%s' using address `%s' session %p\n"),
1887 GNUNET_i2s (target),
1888 GST_plugins_a2s (n->primary_address.address),
1889 n->primary_address.session);
1891 /* Remove address and request and additional one */
1892 unset_primary_address (n);
1893 n->ack_state = ACK_SEND_SYN_ACK;
1894 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1895 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1901 * Send a SYN_ACK message via the given address.
1903 * @param address address to use
1904 * @param session session to use
1905 * @param timestamp timestamp to use for the ACK message
1906 * @return GNUNET_SYSERR if sending immediately failed, GNUNET_OK otherwise
1909 send_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1910 struct Session *session,
1911 struct GNUNET_TIME_Absolute timestamp)
1913 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1914 struct TransportSynMessage connect_msg;
1915 struct NeighbourMapEntry *n;
1917 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1918 "Sending SYN_ACK to peer `%s'\n",
1919 GNUNET_i2s (&address->peer));
1921 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1926 if (NULL == session)
1927 session = papi->get_session (papi->cls, address);
1928 if (NULL == session)
1933 GST_ats_new_session (address, session);
1934 GNUNET_STATISTICS_update (GST_stats,
1936 ("# SYN_ACK messages sent"),
1938 connect_msg.header.size = htons (sizeof (struct TransportSynMessage));
1939 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_SYN_ACK);
1940 connect_msg.reserved = htonl (0);
1941 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1943 if (GNUNET_SYSERR == papi->send (papi->cls,
1945 (const char *) &connect_msg, sizeof (struct TransportSynMessage),
1947 GNUNET_TIME_UNIT_FOREVER_REL,
1948 send_session_connect_ack_cont, NULL))
1950 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1951 _("Failed to transmit SYN_ACK message via plugin to %s\n"),
1952 GST_plugins_a2s (address));
1954 n = lookup_neighbour (&address->peer);
1960 /* Remove address and request and additional one */
1961 unset_primary_address (n);
1962 n->ack_state = ACK_SEND_SYN_ACK;
1963 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
1964 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1970 struct QuotaNotificationRequest
1972 struct GNUNET_PeerIdentity peer;
1973 struct Session *session;
1977 struct QNR_LookContext
1979 struct GNUNET_PeerIdentity peer;
1980 struct Session *session;
1983 struct QuotaNotificationRequest *res;
1987 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1989 struct QNR_LookContext *qnr_ctx = cls;
1990 struct QuotaNotificationRequest *qnr = value;
1992 if ((qnr->session == qnr_ctx->session) &&
1993 (0 == memcmp (&qnr->peer,
1995 sizeof (struct GNUNET_PeerIdentity))) &&
1996 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1998 qnr_ctx->res = value;
2006 GST_neighbours_register_quota_notification (void *cls,
2007 const struct GNUNET_PeerIdentity *peer,
2009 struct Session *session)
2011 struct QuotaNotificationRequest *qnr;
2012 struct QNR_LookContext qnr_ctx;
2014 if (NULL == registered_quota_notifications)
2016 return; /* init or shutdown */
2019 qnr_ctx.peer = (*peer);
2020 qnr_ctx.plugin = plugin;
2021 qnr_ctx.session = session;
2024 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2025 peer, &find_notification_request, &qnr_ctx);
2026 if (NULL != qnr_ctx.res)
2032 qnr = GNUNET_new (struct QuotaNotificationRequest);
2033 qnr->peer = (*peer);
2034 qnr->plugin = GNUNET_strdup (plugin);
2035 qnr->session = session;
2037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2038 "Adding notification for peer `%s' plugin `%s' session %p \n",
2039 GNUNET_i2s (peer), plugin, session);
2041 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
2042 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
2047 GST_neighbours_unregister_quota_notification(void *cls,
2048 const struct GNUNET_PeerIdentity *peer,
2050 struct Session *session)
2052 struct QNR_LookContext qnr_ctx;
2054 if (NULL == registered_quota_notifications)
2056 return; /* init or shutdown */
2059 qnr_ctx.peer = (*peer);
2060 qnr_ctx.plugin = plugin;
2061 qnr_ctx.session = session;
2064 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
2065 &find_notification_request, &qnr_ctx);
2066 if (NULL == qnr_ctx.res)
2072 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2073 "Removing notification for peer `%s' plugin `%s' session %p \n",
2074 GNUNET_i2s (peer), plugin, session);
2076 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
2078 GNUNET_free (qnr_ctx.res->plugin);
2079 GNUNET_free (qnr_ctx.res);
2084 notification_cb (void *cls,
2085 const struct GNUNET_PeerIdentity *key,
2088 /* struct NeighbourMapEntry *n = cls; */
2089 struct QuotaNotificationRequest *qnr = value;
2090 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2091 struct GNUNET_TIME_Relative delay;
2094 papi = GST_plugins_find(qnr->plugin);
2101 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
2102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2103 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
2104 delay.rel_value_us / 1000);
2106 if (NULL != papi->update_inbound_delay)
2107 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
2113 free_notification_cb (void *cls,
2114 const struct GNUNET_PeerIdentity *key,
2117 /* struct NeighbourMapEntry *n = cls; */
2118 struct QuotaNotificationRequest *qnr = value;
2120 GNUNET_break (GNUNET_OK == GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
2122 GNUNET_free(qnr->plugin);
2130 inbound_bw_tracker_update (void *cls)
2132 struct NeighbourMapEntry *n = cls;
2134 /* Quota was updated, tell plugins to update the time to receive next */
2135 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
2136 &n->id, ¬ification_cb, n);
2141 * Create a fresh entry in the neighbour map for the given peer
2143 * @param peer peer to create an entry for
2144 * @return new neighbour map entry
2146 static struct NeighbourMapEntry *
2147 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
2149 struct NeighbourMapEntry *n;
2151 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2152 "Creating new neighbour entry for `%s'\n",
2154 n = GNUNET_new (struct NeighbourMapEntry);
2156 n->ack_state = ACK_UNDEFINED;
2157 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
2158 n->last_util_transmission = GNUNET_TIME_absolute_get();
2159 n->util_payload_bytes_recv = 0;
2160 n->util_payload_bytes_sent = 0;
2161 n->util_total_bytes_recv = 0;
2162 n->util_total_bytes_sent = 0;
2163 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
2164 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
2165 MAX_BANDWIDTH_CARRY_S);
2166 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2167 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
2168 GNUNET_assert (GNUNET_OK ==
2169 GNUNET_CONTAINER_multipeermap_put (neighbours,
2171 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
2179 struct BlacklistCheckSwitchContext
2184 struct BlacklistCheckSwitchContext *prev;
2189 struct BlacklistCheckSwitchContext *next;
2194 struct GST_BlacklistCheck *blc;
2199 struct GNUNET_HELLO_Address *address;
2204 struct Session *session;
2209 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2214 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2219 * Black list check result for try_connect call
2220 * If connection to the peer is allowed request adddress and
2222 * @param cls blc_ctx bl context
2223 * @param peer the peer
2224 * @param result the result
2227 try_connect_bl_check_cont (void *cls,
2228 const struct GNUNET_PeerIdentity *peer,
2231 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2232 struct NeighbourMapEntry *n;
2234 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2237 GNUNET_free (blc_ctx);
2238 if (GNUNET_OK != result)
2240 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2241 _("Blacklisting disapproved to connect to peer `%s'\n"),
2246 /* Setup a new neighbour */
2247 if (NULL != lookup_neighbour(peer))
2248 return; /* The neighbor was created in the meantime while waited for BL clients */
2250 n = setup_neighbour (peer);
2252 /* Request address suggestions for this peer */
2253 set_state_and_timeout (n,
2254 GNUNET_TRANSPORT_PS_INIT_ATS,
2255 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2256 GNUNET_ATS_reset_backoff (GST_ats, peer);
2257 GNUNET_assert (NULL == n->suggest_handle);
2258 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats,
2265 * Try to create a connection to the given target (eventually).
2267 * @param target peer to try to connect to
2270 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2272 struct NeighbourMapEntry *n;
2273 struct GST_BlacklistCheck *blc;
2274 struct BlacklistCheckSwitchContext *blc_ctx;
2276 if (NULL == neighbours)
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2279 "Asked to connect to peer `%s' during shutdown\n",
2280 GNUNET_i2s (target));
2281 return; /* during shutdown, do nothing */
2283 n = lookup_neighbour (target);
2284 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2285 "Asked to connect to peer `%s' (state: %s)\n",
2286 GNUNET_i2s (target),
2287 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2292 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2293 /* this should not be possible */
2295 free_neighbour (n, GNUNET_NO);
2297 case GNUNET_TRANSPORT_PS_INIT_ATS:
2298 case GNUNET_TRANSPORT_PS_SYN_SENT:
2299 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2300 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2301 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2302 "Ignoring request to try to connect to `%s', already trying!\n",
2303 GNUNET_i2s (target));
2304 return; /* already trying */
2305 case GNUNET_TRANSPORT_PS_CONNECTED:
2306 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2307 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2308 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2309 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2310 "Ignoring request to try to connect, already connected to `%s'!\n",
2311 GNUNET_i2s (target));
2312 return; /* already connected */
2313 case GNUNET_TRANSPORT_PS_DISCONNECT:
2314 /* get rid of remains, ready to re-try immediately */
2315 free_neighbour (n, GNUNET_NO);
2317 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2318 /* should not be possible */
2322 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2323 "Unhandled state `%s'\n",
2324 GNUNET_TRANSPORT_ps2s (n->state));
2326 free_neighbour (n, GNUNET_NO);
2331 /* Do blacklist check if connecting to this peer is allowed */
2332 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2333 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2335 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2336 &try_connect_bl_check_cont, blc_ctx)))
2344 * We received a 'SYN' message from the other peer.
2345 * Consider switching to it.
2347 * @param message possibly a 'struct TransportSynMessage' (check format)
2348 * @param peer identity of the peer to switch the address for
2349 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2352 GST_neighbours_handle_session_syn (const struct GNUNET_MessageHeader *message,
2353 const struct GNUNET_PeerIdentity *peer)
2355 const struct TransportSynMessage *scm;
2356 struct NeighbourMapEntry *n;
2357 struct GNUNET_TIME_Absolute ts;
2359 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
2361 GNUNET_break_op (0);
2362 return GNUNET_SYSERR;
2364 GNUNET_STATISTICS_update (GST_stats,
2366 ("# SYN messages received"),
2368 if (NULL == neighbours)
2370 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2371 _("SYN request from peer `%s' ignored due impending shutdown\n"),
2373 return GNUNET_OK; /* we're shutting down */
2375 scm = (const struct TransportSynMessage *) message;
2376 GNUNET_break_op (0 == ntohl (scm->reserved));
2377 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2378 n = lookup_neighbour (peer);
2381 /* This is a new neighbour and set to not connected */
2382 n = setup_neighbour (peer);
2385 /* Remember this SYN message in neighbour */
2386 n->ack_state = ACK_SEND_SYN_ACK;
2387 n->connect_ack_timestamp = ts;
2389 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2390 "Received SYN for peer `%s' in state %s/%s\n",
2392 GNUNET_TRANSPORT_ps2s (n->state),
2393 print_ack_state (n->ack_state));
2397 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2398 /* Request an address from ATS to send SYN_ACK to this peer */
2399 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2400 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2401 if (NULL == n->suggest_handle)
2402 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2404 case GNUNET_TRANSPORT_PS_INIT_ATS:
2405 /* SYN message takes priority over us asking ATS for address:
2406 * Wait for ATS to suggest an address and send SYN_ACK */
2407 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS,
2408 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2410 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2411 /* We already wait for an address to send an SYN_ACK */
2413 case GNUNET_TRANSPORT_PS_SYN_SENT:
2414 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2415 /* Send ACK immediately */
2416 n->ack_state = ACK_SEND_ACK;
2417 send_connect_ack_message (n->primary_address.address,
2418 n->primary_address.session, ts);
2420 case GNUNET_TRANSPORT_PS_CONNECTED:
2421 /* we are already connected and can thus send the ACK immediately */
2422 GNUNET_assert (NULL != n->primary_address.address);
2423 GNUNET_assert (NULL != n->primary_address.session);
2424 n->ack_state = ACK_SEND_ACK;
2425 send_connect_ack_message (n->primary_address.address,
2426 n->primary_address.session, ts);
2428 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2429 /* We wait for ATS address suggestion */
2431 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2432 /* We received a SYN message while waiting for a SYN_ACK in fast
2433 * reconnect. Send SYN_ACK immediately */
2434 n->ack_state = ACK_SEND_ACK;
2435 send_connect_ack_message (n->primary_address.address,
2436 n->primary_address.session, n->connect_ack_timestamp);
2438 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2439 /* We are already connected and can thus send the ACK immediately;
2440 still, it can never hurt to have an alternative address, so also
2441 tell ATS about it */
2442 GNUNET_assert (NULL != n->primary_address.address);
2443 GNUNET_assert (NULL != n->primary_address.session);
2444 n->ack_state = ACK_SEND_ACK;
2445 send_connect_ack_message (n->primary_address.address,
2446 n->primary_address.session, ts);
2448 case GNUNET_TRANSPORT_PS_DISCONNECT:
2449 /* Get rid of remains without terminating sessions, ready to re-try */
2450 free_neighbour (n, GNUNET_YES);
2451 n = setup_neighbour (peer);
2452 /* Remember the SYN time stamp for ACK message */
2453 n->ack_state = ACK_SEND_SYN_ACK;
2454 n->connect_ack_timestamp = ts;
2455 /* Request an address for the peer */
2456 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2457 GNUNET_ATS_reset_backoff (GST_ats, peer);
2458 set_state (n, GNUNET_TRANSPORT_PS_SYN_RECV_ATS);
2460 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2461 /* should not be possible */
2465 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2466 "Unhandled state `%s'\n",
2467 GNUNET_TRANSPORT_ps2s (n->state));
2469 return GNUNET_SYSERR;
2476 * We've been asked to switch addresses, and just now
2477 * got the result from the blacklist check to see if this
2480 * @param cls the `struct BlacklistCheckSwitchContext` with
2481 * the information about the future address
2482 * @param peer the peer we may switch addresses on
2483 * @param result #GNUNET_NO if we are not allowed to use the new
2487 switch_address_bl_check_cont (void *cls,
2488 const struct GNUNET_PeerIdentity *peer,
2491 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2492 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2493 struct NeighbourMapEntry *n;
2495 papi = GST_plugins_find (blc_ctx->address->transport_name);
2497 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 "Peer %s is unknown, suggestion ignored\n",
2506 if (result == GNUNET_NO)
2508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2509 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2510 GST_plugins_a2s (blc_ctx->address),
2512 GNUNET_i2s (&blc_ctx->address->peer));
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2518 blc_ctx->address->transport_name,
2519 GST_plugins_a2s (blc_ctx->address),
2521 GNUNET_i2s (&blc_ctx->address->peer));
2524 /* This address is blacklisted, delete session */
2525 /* FIXME: tell plugin to force killing session here and now! */
2527 /* Remove blacklist check and clean up */
2528 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2531 GNUNET_HELLO_address_free (blc_ctx->address);
2532 GNUNET_free (blc_ctx);
2536 if (NULL == blc_ctx->session)
2538 blc_ctx->session = papi->get_session (papi->cls,
2540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2541 "Obtained new session for peer `%s' and address '%s': %p\n",
2542 GNUNET_i2s (&blc_ctx->address->peer),
2543 GST_plugins_a2s (blc_ctx->address),
2545 if (NULL != blc_ctx->session)
2546 GST_ats_new_session (blc_ctx->address,
2550 if (NULL == blc_ctx->session)
2552 /* No session could be obtained, remove blacklist check and clean up */
2553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2554 "Failed to obtain new session for peer `%s' and address '%s'\n",
2555 GNUNET_i2s (&blc_ctx->address->peer),
2556 GST_plugins_a2s (blc_ctx->address));
2557 /* Delete address in ATS */
2558 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
2561 GNUNET_HELLO_address_free (blc_ctx->address);
2562 GNUNET_free (blc_ctx);
2566 if ( (NULL != n->primary_address.address) &&
2567 (0 == GNUNET_HELLO_address_cmp(blc_ctx->address, n->primary_address.address)) )
2569 if (blc_ctx->session == n->primary_address.session)
2571 /* This address is already primary, update only quotas */
2572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2573 "Updating quota for peer `%s' address `%s' session %p\n",
2574 GNUNET_i2s (&blc_ctx->address->peer),
2575 GST_plugins_a2s (blc_ctx->address),
2578 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2579 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2581 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2582 GNUNET_HELLO_address_free(blc_ctx->address);
2583 GNUNET_free (blc_ctx);
2588 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2589 "Peer `%s' switches to address `%s' session %p\n",
2590 GNUNET_i2s (&blc_ctx->address->peer),
2591 GST_plugins_a2s (blc_ctx->address),
2596 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2598 free_neighbour (n, GNUNET_NO);
2600 case GNUNET_TRANSPORT_PS_INIT_ATS:
2601 /* We requested an address and ATS suggests one:
2602 * set primary address and send SYN message*/
2603 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2604 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2605 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2607 /* Send pending SYN_ACK message */
2608 n->ack_state = ACK_SEND_ACK;
2609 send_connect_ack_message (n->primary_address.address,
2610 n->primary_address.session, n->connect_ack_timestamp);
2612 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_SENT,
2613 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2614 send_syn (&n->primary_address);
2616 case GNUNET_TRANSPORT_PS_SYN_SENT:
2617 /* ATS suggested a new address while waiting for an SYN_ACK:
2618 * Switch and send new SYN */
2619 /* ATS suggests a different address, switch again */
2620 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2621 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2622 if (ACK_SEND_SYN_ACK == n->ack_state)
2624 /* Send pending SYN_ACK message */
2625 n->ack_state = ACK_SEND_ACK;
2626 send_connect_ack_message (n->primary_address.address,
2627 n->primary_address.session, n->connect_ack_timestamp);
2629 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_SENT,
2630 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2631 send_syn (&n->primary_address);
2633 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
2634 /* We requested an address and ATS suggests one:
2635 * set primary address and send SYN_ACK message*/
2636 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2637 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2638 /* Send an ACK message as a response to the SYN msg */
2639 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2640 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2641 send_connect_ack_message (n->primary_address.address,
2642 n->primary_address.session,
2643 n->connect_ack_timestamp);
2644 if ( (ACK_SEND_SYN_ACK == n->ack_state) ||
2645 (ACK_UNDEFINED == n->ack_state) )
2646 n->ack_state = ACK_SEND_ACK;
2648 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
2649 /* ATS asks us to switch while we were trying to connect; switch to new
2650 address and check blacklist again */
2651 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2653 n->ack_state = ACK_SEND_ACK;
2654 send_connect_ack_message (n->primary_address.address,
2655 n->primary_address.session, n->connect_ack_timestamp);
2657 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2658 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2659 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SYN_RECV_ACK,
2660 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2662 case GNUNET_TRANSPORT_PS_CONNECTED:
2663 GNUNET_assert (NULL != n->primary_address.address);
2664 GNUNET_assert (NULL != n->primary_address.session);
2665 if (n->primary_address.session == blc_ctx->session)
2667 /* not an address change, just a quota change */
2668 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2669 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2672 /* ATS asks us to switch a life connection; see if we can get
2673 a SYN_ACK on it before we actually do this! */
2674 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2675 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2676 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2677 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2678 GNUNET_STATISTICS_update (GST_stats, gettext_noop
2679 ("# Attempts to switch addresses"), 1, GNUNET_NO);
2680 send_syn (&n->alternative_address);
2682 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2683 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2684 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2685 if ( (ACK_SEND_SYN_ACK == n->ack_state) )
2687 /* Send pending SYN_ACK message */
2688 n->ack_state = ACK_SEND_ACK;
2689 send_connect_ack_message (n->primary_address.address,
2690 n->primary_address.session, n->connect_ack_timestamp);
2692 set_state_and_timeout (n, 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_RECONNECT_SENT:
2697 /* ATS asks us to switch while we were trying to reconnect; switch to new
2698 address and send SYN again */
2699 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2700 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2701 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
2702 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2703 send_syn (&n->primary_address);
2705 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
2706 if ( (0 == GNUNET_HELLO_address_cmp(n->primary_address.address,
2707 blc_ctx->address) && n->primary_address.session == blc_ctx->session) )
2709 /* ATS switches back to still-active session */
2710 free_address (&n->alternative_address);
2711 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2714 /* ATS asks us to switch a life connection, send */
2715 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2716 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2717 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT,
2718 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2719 send_syn (&n->alternative_address);
2721 case GNUNET_TRANSPORT_PS_DISCONNECT:
2722 /* not going to switch addresses while 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, pending_bc_tail, blc_ctx);
2736 GNUNET_HELLO_address_free (blc_ctx->address);
2737 GNUNET_free (blc_ctx);
2742 * For the given peer, switch to this address.
2744 * Before accepting this addresses and actively using it, a blacklist check
2745 * is performed. If this blacklist check fails the address will be destroyed.
2747 * @param address address of the other peer,
2748 * @param session session to use or NULL if transport should initiate a session
2749 * @param bandwidth_in inbound quota to be used when connection is up,
2750 * 0 to disconnect from peer
2751 * @param bandwidth_out outbound quota to be used when connection is up,
2752 * 0 to disconnect from peer
2755 GST_neighbours_switch_to_address (const struct GNUNET_HELLO_Address *address,
2756 struct Session *session,
2757 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2758 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2760 struct NeighbourMapEntry *n;
2761 struct GST_BlacklistCheck *blc;
2762 struct BlacklistCheckSwitchContext *blc_ctx;
2764 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2765 "ATS has decided on an address for peer %s\n",
2766 GNUNET_i2s (&address->peer));
2767 GNUNET_assert (NULL != address->transport_name);
2768 if (NULL == (n = lookup_neighbour (&address->peer)))
2770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2771 "Peer %s is unknown, suggestion ignored\n",
2772 GNUNET_i2s (&address->peer));
2776 /* Check if plugin is available */
2777 if (NULL == (GST_plugins_find (address->transport_name)))
2779 /* we don't have the plugin for this address */
2783 if ((NULL == session) &&
2784 (GNUNET_HELLO_address_check_option (address,
2785 GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2787 /* This is a inbound address and we do not have a session to use! */
2792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2793 "ATS suggests %s address '%s' session %p for "
2794 "peer `%s' in state %s/%s \n",
2795 GNUNET_HELLO_address_check_option (address,
2796 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2797 GST_plugins_a2s (address),
2799 GNUNET_i2s (&address->peer),
2800 GNUNET_TRANSPORT_ps2s (n->state),
2801 print_ack_state (n->ack_state));
2803 /* Perform blacklist check */
2804 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2805 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2806 blc_ctx->session = session;
2807 blc_ctx->bandwidth_in = bandwidth_in;
2808 blc_ctx->bandwidth_out = bandwidth_out;
2809 GNUNET_CONTAINER_DLL_insert (pending_bc_head,
2812 if (NULL != (blc = GST_blacklist_test_allowed (&address->peer,
2813 address->transport_name,
2814 &switch_address_bl_check_cont,
2823 * Function called to send network utilization data to ATS for
2824 * each active connection.
2827 * @param key peer we send utilization data for
2828 * @param value the `struct NeighbourMapEntry *` with data to send
2829 * @return #GNUNET_OK (continue to iterate)
2832 send_utilization_data (void *cls,
2833 const struct GNUNET_PeerIdentity *key,
2836 struct NeighbourMapEntry *n = value;
2837 struct GNUNET_ATS_Information atsi[4];
2839 uint32_t bps_pl_out;
2842 struct GNUNET_TIME_Relative delta;
2844 if (GNUNET_TRANSPORT_PS_CONNECTED != n->state)
2846 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2847 GNUNET_TIME_absolute_get ());
2851 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2852 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2854 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2855 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2857 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2862 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2863 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2865 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2866 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2870 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2874 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2875 atsi[0].value = htonl (bps_out);
2876 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2877 atsi[1].value = htonl (bps_in);
2879 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2880 atsi[2].value = htonl (bps_pl_out);
2881 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2882 atsi[3].value = htonl (bps_pl_in);
2884 GST_ats_update_metrics (n->primary_address.address,
2885 n->primary_address.session,
2887 n->util_payload_bytes_recv = 0;
2888 n->util_payload_bytes_sent = 0;
2889 n->util_total_bytes_recv = 0;
2890 n->util_total_bytes_sent = 0;
2891 n->last_util_transmission = GNUNET_TIME_absolute_get();
2897 * Task transmitting utilization in a regular interval
2899 * @param cls the 'struct NeighbourMapEntry' for which we are running
2900 * @param tc scheduler context (unused)
2903 utilization_transmission (void *cls,
2904 const struct GNUNET_SCHEDULER_TaskContext *tc)
2906 util_transmission_tk = NULL;
2908 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2909 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2911 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2912 utilization_transmission, NULL);
2918 GST_neighbours_notify_data_recv (const struct GNUNET_HELLO_Address *address,
2919 struct Session *session,
2920 const struct GNUNET_MessageHeader *message)
2922 struct NeighbourMapEntry *n;
2924 n = lookup_neighbour (&address->peer);
2927 n->util_total_bytes_recv += ntohs(message->size);
2932 GST_neighbours_notify_payload_recv (const struct GNUNET_HELLO_Address *address,
2933 struct Session *session,
2934 const struct GNUNET_MessageHeader *message)
2936 struct NeighbourMapEntry *n;
2938 n = lookup_neighbour (&address->peer);
2941 n->util_payload_bytes_recv += ntohs(message->size);
2946 GST_neighbours_notify_data_sent (const struct GNUNET_HELLO_Address *address,
2947 struct Session *session,
2950 struct NeighbourMapEntry *n;
2952 n = lookup_neighbour (&address->peer);
2955 if (n->primary_address.session != session)
2957 n->util_total_bytes_sent += size;
2962 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2965 struct NeighbourMapEntry *n;
2966 n = lookup_neighbour (peer);
2969 n->util_payload_bytes_sent += size;
2974 * Master task run for every neighbour. Performs all of the time-related
2975 * activities (keep alive, send next message, disconnect if idle, finish
2976 * clean up after disconnect).
2978 * @param cls the 'struct NeighbourMapEntry' for which we are running
2979 * @param tc scheduler context (unused)
2982 master_task (void *cls,
2983 const struct GNUNET_SCHEDULER_TaskContext *tc)
2985 struct NeighbourMapEntry *n = cls;
2986 struct GNUNET_TIME_Relative delay;
2989 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2991 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2992 GNUNET_i2s (&n->id),
2993 GNUNET_TRANSPORT_ps2s(n->state),
2994 GNUNET_STRINGS_relative_time_to_string (delay,
2998 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2999 /* invalid state for master task, clean up */
3001 free_neighbour (n, GNUNET_NO);
3003 case GNUNET_TRANSPORT_PS_INIT_ATS:
3004 if (0 == delay.rel_value_us)
3006 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3007 "Connection to `%s' timed out waiting for ATS to provide address\n",
3008 GNUNET_i2s (&n->id));
3009 free_neighbour (n, GNUNET_NO);
3013 case GNUNET_TRANSPORT_PS_SYN_SENT:
3014 if (0 == delay.rel_value_us)
3016 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3017 "Connection to `%s' timed out waiting for other peer to send SYN_ACK\n",
3018 GNUNET_i2s (&n->id));
3019 /* Remove address and request and additional one */
3020 unset_primary_address (n);
3021 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3022 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3026 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3027 if (0 == delay.rel_value_us)
3029 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3030 "Connection to `%s' timed out waiting ATS to provide address to use for SYN_ACK\n",
3031 GNUNET_i2s (&n->id));
3032 free_neighbour (n, GNUNET_NO);
3036 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3037 if (0 == delay.rel_value_us)
3039 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3040 "Connection to `%s' timed out waiting for other peer to send ACK\n",
3041 GNUNET_i2s (&n->id));
3042 disconnect_neighbour (n);
3046 case GNUNET_TRANSPORT_PS_CONNECTED:
3047 if (0 == delay.rel_value_us)
3049 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3050 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3051 GNUNET_i2s (&n->id));
3052 disconnect_neighbour (n);
3055 try_transmission_to_peer (n);
3058 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3059 if (0 == delay.rel_value_us)
3061 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3062 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3063 GNUNET_i2s (&n->id));
3064 disconnect_neighbour (n);
3068 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3069 if (0 == delay.rel_value_us)
3071 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3072 "Connection to `%s' timed out, waiting for other peer to SYN_ACK replacement address\n",
3073 GNUNET_i2s (&n->id));
3074 disconnect_neighbour (n);
3078 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3079 if (0 == delay.rel_value_us)
3081 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3082 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to SYN on alternative address)\n",
3083 GNUNET_i2s (&n->id));
3084 GNUNET_STATISTICS_update (GST_stats,
3085 gettext_noop ("# Failed attempts to switch addresses (no response)"),
3088 disconnect_neighbour (n);
3091 try_transmission_to_peer (n);
3094 case GNUNET_TRANSPORT_PS_DISCONNECT:
3095 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3096 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3097 GNUNET_i2s (&n->id));
3098 free_neighbour (n, GNUNET_NO);
3100 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3101 /* how did we get here!? */
3105 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3106 "Unhandled state `%s'\n",
3107 GNUNET_TRANSPORT_ps2s (n->state));
3111 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) ||
3112 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3114 /* if we are *now* in one of the two states, we're sending
3115 keep alive messages, so we need to consider the keepalive
3116 delay, not just the connection timeout */
3117 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3120 if (NULL == n->task)
3121 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3128 * Send a ACK message to the neighbour to confirm that we
3131 * @param n neighbour to send the ACK to
3134 send_session_ack_message (struct NeighbourMapEntry *n)
3136 struct GNUNET_MessageHeader msg;
3138 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Sending ACK message to peer `%s'\n",
3139 GNUNET_i2s (&n->id));
3141 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3142 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3143 (void) send_with_session(n,
3144 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3145 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3151 * We received a 'SESSION_SYN_ACK' message from the other peer.
3152 * Consider switching to it.
3154 * @param message possibly a 'struct SessionConnectMessage' (check format)
3155 * @param peer identity of the peer to switch the address for
3156 * @param address address of the other peer, NULL if other peer
3158 * @param session session to use (or NULL)
3159 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3162 GST_neighbours_handle_session_syn_ack (const struct GNUNET_MessageHeader *message,
3163 const struct GNUNET_HELLO_Address *address,
3164 struct Session *session)
3166 const struct TransportSynMessage *scm;
3167 struct GNUNET_TIME_Absolute ts;
3168 struct NeighbourMapEntry *n;
3170 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3171 "Received SYN_ACK message from peer `%s'\n",
3172 GNUNET_i2s (&address->peer));
3174 if (ntohs (message->size) != sizeof (struct TransportSynMessage))
3176 GNUNET_break_op (0);
3177 return GNUNET_SYSERR;
3179 GNUNET_STATISTICS_update (GST_stats,
3181 ("# SYN_ACK messages received"),
3183 scm = (const struct TransportSynMessage *) message;
3184 GNUNET_break_op (ntohl (scm->reserved) == 0);
3185 if (NULL == (n = lookup_neighbour (&address->peer)))
3187 GNUNET_STATISTICS_update (GST_stats,
3189 ("# unexpected SYN_ACK messages (no peer)"),
3191 return GNUNET_SYSERR;
3193 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3196 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3198 free_neighbour (n, GNUNET_NO);
3199 return GNUNET_SYSERR;
3200 case GNUNET_TRANSPORT_PS_INIT_ATS:
3201 GNUNET_STATISTICS_update (GST_stats,
3203 ("# unexpected SYN_ACK messages (not ready)"),
3206 case GNUNET_TRANSPORT_PS_SYN_SENT:
3207 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3209 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3210 "SYN_ACK ignored as the timestamp does not match our SYN request\n");
3213 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3214 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3215 GNUNET_STATISTICS_set (GST_stats,
3216 gettext_noop ("# peers connected"),
3217 ++neighbours_connected,
3219 connect_notify_cb (callback_cls,
3221 n->primary_address.bandwidth_in,
3222 n->primary_address.bandwidth_out);
3223 /* Tell ATS that the outbound session we created to send SYN was successful */
3224 set_primary_address (n,
3225 n->primary_address.address,
3226 n->primary_address.session,
3227 n->primary_address.bandwidth_in,
3228 n->primary_address.bandwidth_out,
3230 send_session_ack_message (n);
3232 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3233 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3234 GNUNET_STATISTICS_update (GST_stats,
3236 ("# unexpected SYN_ACK messages (not ready)"),
3239 case GNUNET_TRANSPORT_PS_CONNECTED:
3240 /* duplicate SYN_ACK, let's answer by duplicate ACK just in case */
3241 send_session_ack_message (n);
3243 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3244 /* we didn't expect any SYN_ACK, as we are waiting for ATS
3245 to give us a new address... */
3246 GNUNET_STATISTICS_update (GST_stats,
3248 ("# unexpected SYN_ACK messages (waiting on ATS)"),
3251 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3252 /* Reconnecting with new address address worked; go back to connected! */
3253 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3254 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3255 send_session_ack_message (n);
3257 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3258 /* new address worked; adopt it and go back to connected! */
3259 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3260 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3261 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3263 /* Set primary addresses */
3264 set_primary_address (n, n->alternative_address.address,
3265 n->alternative_address.session, n->alternative_address.bandwidth_in,
3266 n->alternative_address.bandwidth_out, GNUNET_YES);
3267 GNUNET_STATISTICS_update (GST_stats, gettext_noop
3268 ("# Successful attempts to switch addresses"), 1, GNUNET_NO);
3270 free_address (&n->alternative_address);
3271 send_session_ack_message (n);
3273 case GNUNET_TRANSPORT_PS_DISCONNECT:
3274 GNUNET_STATISTICS_update (GST_stats,
3276 ("# unexpected SYN_ACK messages (disconnecting)"),
3278 return GNUNET_SYSERR;
3279 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3283 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3284 "Unhandled state `%s'\n",
3285 GNUNET_TRANSPORT_ps2s (n->state));
3287 return GNUNET_SYSERR;
3294 * A session was terminated. Take note; if needed, try to get
3295 * an alternative address from ATS.
3297 * @param peer identity of the peer where the session died
3298 * @param session session that is gone
3299 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3300 * this session was not in use
3303 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3304 struct Session *session)
3306 struct NeighbourMapEntry *n;
3307 struct BlackListCheckContext *bcc;
3308 struct BlackListCheckContext *bcc_next;
3310 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3312 while (NULL != (bcc = bcc_next))
3314 bcc_next = bcc->next;
3315 if (bcc->na.session == session)
3317 if (NULL != bcc->bc)
3318 GST_blacklist_test_cancel (bcc->bc);
3319 GNUNET_HELLO_address_free (bcc->na.address);
3320 GNUNET_CONTAINER_DLL_remove (bc_head,
3326 if (NULL == (n = lookup_neighbour (peer)))
3327 return GNUNET_NO; /* can't affect us */
3328 if (session != n->primary_address.session)
3330 /* Free alternative address */
3331 if (session == n->alternative_address.session)
3333 if ( (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state) )
3334 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3335 free_address (&n->alternative_address);
3337 return GNUNET_NO; /* doesn't affect us further */
3340 n->expect_latency_response = GNUNET_NO;
3341 /* The session for neighbour's primary address died */
3344 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3346 free_neighbour (n, GNUNET_NO);
3348 case GNUNET_TRANSPORT_PS_INIT_ATS:
3350 free_neighbour (n, GNUNET_NO);
3352 case GNUNET_TRANSPORT_PS_SYN_SENT:
3353 /* The session used to send the SYN terminated:
3354 * this implies a connect error*/
3355 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3356 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3358 GST_plugins_a2s (n->primary_address.address),
3359 n->primary_address.session,
3362 /* Destroy the address since it cannot be used */
3363 unset_primary_address (n);
3364 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3365 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3367 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3368 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3369 /* error on inbound session; free neighbour entirely */
3370 free_address (&n->primary_address);
3371 free_neighbour (n, GNUNET_NO);
3373 case GNUNET_TRANSPORT_PS_CONNECTED:
3374 /* Our primary connection died, try a fast reconnect */
3375 unset_primary_address (n);
3376 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3377 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3379 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3380 /* we don't have an address, how can it go down? */
3383 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3384 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3385 "Failed to send SYN in %s with `%s' %p: session terminated\n",
3387 GST_plugins_a2s (n->primary_address.address),
3388 n->primary_address.session,
3390 /* Destroy the address since it cannot be used */
3391 unset_primary_address (n);
3392 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3393 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3395 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3396 /* primary went down while we were waiting for SYN_ACK on secondary;
3397 secondary as primary */
3399 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3400 "Connection `%s' %p to peer `%s' was terminated while switching, "
3401 "switching to alternative address `%s' %p\n",
3402 GST_plugins_a2s (n->primary_address.address),
3403 n->primary_address.session,
3405 GST_plugins_a2s (n->alternative_address.address),
3406 n->alternative_address.session);
3408 /* Destroy the inbound address since it cannot be used */
3409 free_address (&n->primary_address);
3410 n->primary_address = n->alternative_address;
3411 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3412 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT,
3413 GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3415 case GNUNET_TRANSPORT_PS_DISCONNECT:
3416 free_address (&n->primary_address);
3418 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3419 /* neighbour was freed and plugins told to terminate session */
3423 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3424 "Unhandled state `%s'\n",
3425 GNUNET_TRANSPORT_ps2s (n->state));
3429 if (NULL != n->task)
3430 GNUNET_SCHEDULER_cancel (n->task);
3431 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3437 * We received a 'ACK' message from the other peer.
3438 * If we sent a 'SYN_ACK' last, this means we are now
3439 * connected. Otherwise, do nothing.
3441 * @param message possibly a 'struct SessionConnectMessage' (check format)
3442 * @param address address of the other peer
3443 * @param session session to use (or NULL)
3444 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3447 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3448 const struct GNUNET_HELLO_Address *address,
3449 struct Session *session)
3451 struct NeighbourMapEntry *n;
3453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3454 "Received ACK message from peer `%s'\n",
3455 GNUNET_i2s (&address->peer));
3456 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3458 GNUNET_break_op (0);
3459 return GNUNET_SYSERR;
3461 GNUNET_STATISTICS_update (GST_stats,
3463 ("# ACK messages received"),
3465 if (NULL == (n = lookup_neighbour (&address->peer)))
3467 GNUNET_break_op (0);
3468 return GNUNET_SYSERR;
3471 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3472 "Received %s for peer `%s' in state %s/%s\n",
3474 GNUNET_i2s (&address->peer),
3475 GNUNET_TRANSPORT_ps2s (n->state),
3476 print_ack_state (n->ack_state));
3478 /* Check if we are in a plausible state for having sent
3479 a SYN_ACK. If not, return, otherwise break.
3481 The remote peers sends a ACK as a response for a SYN_ACK
3485 - If a remote peer has sent a SYN, we responded with a SYN_ACK and
3486 now wait for the ACK to finally be connected
3487 - If we sent a SYN_ACK to this peer before */
3489 if ( (GNUNET_TRANSPORT_PS_SYN_RECV_ACK != n->state) &&
3490 (ACK_SEND_ACK != n->ack_state))
3492 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3493 "Received unexpected ACK message from peer `%s' in state %s/%s\n",
3494 GNUNET_i2s (&address->peer),
3495 GNUNET_TRANSPORT_ps2s (n->state),
3496 print_ack_state (n->ack_state));
3498 GNUNET_STATISTICS_update (GST_stats,
3499 gettext_noop ("# unexpected ACK messages"), 1,
3504 /* We are connected */
3505 if (GNUNET_NO == GST_neighbours_test_connected(&n->id))
3507 /* Notify about connection */
3508 connect_notify_cb (callback_cls,
3510 n->primary_address.bandwidth_in,
3511 n->primary_address.bandwidth_out);
3512 GNUNET_STATISTICS_set (GST_stats,
3513 gettext_noop ("# peers connected"),
3514 ++neighbours_connected,
3518 if (GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT == n->state)
3520 /* We tried to switch addresses while being connect. We explicitly wait
3521 * for a SYN_ACK before going to GNUNET_TRANSPORT_PS_CONNECTED,
3522 * so we do not want to set the address as in use! */
3526 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED,
3527 GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3529 /* Set primary address to used */
3530 set_primary_address (n,
3531 n->primary_address.address,
3532 n->primary_address.session,
3533 n->primary_address.bandwidth_in,
3534 n->primary_address.bandwidth_out,
3541 * Test if we're connected to the given peer.
3543 * @param target peer to test
3544 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3547 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3549 return test_connected (lookup_neighbour (target));
3554 * Change the incoming quota for the given peer.
3556 * @param neighbour identity of peer to change qutoa for
3557 * @param quota new quota
3560 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3561 struct GNUNET_BANDWIDTH_Value32NBO quota)
3563 struct NeighbourMapEntry *n;
3565 if (NULL == (n = lookup_neighbour (neighbour)))
3567 GNUNET_STATISTICS_update (GST_stats,
3569 ("# SET QUOTA messages ignored (no such peer)"),
3573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3574 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3575 ntohl (quota.value__), GNUNET_i2s (&n->id));
3576 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3577 if (0 != ntohl (quota.value__))
3579 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3580 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3581 GNUNET_i2s (&n->id));
3582 if (GNUNET_YES == test_connected (n))
3583 GNUNET_STATISTICS_update (GST_stats,
3584 gettext_noop ("# disconnects due to quota of 0"),
3586 disconnect_neighbour (n);
3591 delayed_disconnect (void *cls,
3592 const struct GNUNET_SCHEDULER_TaskContext* tc)
3594 struct NeighbourMapEntry *n = cls;
3596 n->delayed_disconnect_task = NULL;
3597 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3598 "Disconnecting by request from peer %s\n",
3599 GNUNET_i2s (&n->id));
3600 free_neighbour (n, GNUNET_NO);
3605 * We received a disconnect message from the given peer,
3606 * validate and process.
3608 * @param peer sender of the message
3609 * @param msg the disconnect message
3612 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3613 const struct GNUNET_MessageHeader *msg)
3615 struct NeighbourMapEntry *n;
3616 const struct SessionDisconnectMessage *sdm;
3618 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3619 "Received DISCONNECT message from peer `%s'\n",
3621 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3623 GNUNET_break_op (0);
3624 GNUNET_STATISTICS_update (GST_stats,
3626 ("# disconnect messages ignored (malformed)"), 1,
3630 GNUNET_STATISTICS_update (GST_stats,
3632 ("# DISCONNECT messages received"),
3634 sdm = (const struct SessionDisconnectMessage *) msg;
3635 if (NULL == (n = lookup_neighbour (peer)))
3636 return; /* gone already */
3637 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3639 GNUNET_STATISTICS_update (GST_stats,
3641 ("# disconnect messages ignored (timestamp)"), 1,
3645 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3647 GNUNET_break_op (0);
3650 if (ntohl (sdm->purpose.size) !=
3651 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3652 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3653 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3655 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3656 "%s message from peer `%s' has invalid size \n",
3659 GNUNET_break_op (0);
3663 GNUNET_CRYPTO_eddsa_verify
3664 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3665 &sdm->signature, &sdm->public_key))
3667 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3668 "%s message from peer `%s' cannot be verified \n",
3671 GNUNET_break_op (0);
3674 n->delayed_disconnect_task = GNUNET_SCHEDULER_add_now (&delayed_disconnect, n);
3679 * Closure for the #neighbours_iterate() function.
3681 struct IteratorContext
3684 * Function to call on each connected neighbour.
3686 GST_NeighbourIterator cb;
3689 * Closure for @e cb.
3696 * Call the callback from the closure for each neighbour.
3698 * @param cls the `struct IteratorContext`
3699 * @param key the hash of the public key of the neighbour
3700 * @param value the `struct NeighbourMapEntry`
3701 * @return #GNUNET_OK (continue to iterate)
3704 neighbours_iterate (void *cls,
3705 const struct GNUNET_PeerIdentity *key,
3708 struct IteratorContext *ic = cls;
3709 struct NeighbourMapEntry *n = value;
3710 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3711 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3714 if (NULL != n->primary_address.address)
3716 bandwidth_in = n->primary_address.bandwidth_in;
3717 bandwidth_out = n->primary_address.bandwidth_out;
3721 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3722 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3726 n->primary_address.address,
3729 bandwidth_in, bandwidth_out);
3735 * Iterate over all connected neighbours.
3737 * @param cb function to call
3738 * @param cb_cls closure for cb
3741 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3743 struct IteratorContext ic;
3745 if (NULL == neighbours)
3746 return; /* can happen during shutdown */
3749 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3754 * If we have an active connection to the given target, it must be shutdown.
3756 * @param target peer to disconnect from
3759 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3761 struct NeighbourMapEntry *n;
3763 if (NULL == (n = lookup_neighbour (target)))
3764 return; /* not active */
3765 if (GNUNET_YES == test_connected (n))
3766 GNUNET_STATISTICS_update (GST_stats,
3768 ("# disconnected from peer upon explicit request"), 1,
3770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3771 "Forced disconnect from peer %s\n",
3772 GNUNET_i2s (target));
3773 disconnect_neighbour (n);
3778 * Obtain current latency information for the given neighbour.
3780 * @param peer to get the latency for
3781 * @return observed latency of the address, FOREVER if the
3782 * the connection is not up
3784 struct GNUNET_TIME_Relative
3785 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3787 struct NeighbourMapEntry *n;
3789 n = lookup_neighbour (peer);
3791 return GNUNET_TIME_UNIT_FOREVER_REL;
3794 case GNUNET_TRANSPORT_PS_CONNECTED:
3795 case GNUNET_TRANSPORT_PS_SWITCH_SYN_SENT:
3796 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3797 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3799 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3800 case GNUNET_TRANSPORT_PS_INIT_ATS:
3801 case GNUNET_TRANSPORT_PS_SYN_RECV_ATS:
3802 case GNUNET_TRANSPORT_PS_SYN_RECV_ACK:
3803 case GNUNET_TRANSPORT_PS_SYN_SENT:
3804 case GNUNET_TRANSPORT_PS_DISCONNECT:
3805 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3806 return GNUNET_TIME_UNIT_FOREVER_REL;
3808 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3809 "Unhandled state `%s'\n",
3810 GNUNET_TRANSPORT_ps2s (n->state));
3814 return GNUNET_TIME_UNIT_FOREVER_REL;
3819 * Obtain current address information for the given neighbour.
3822 * @return address currently used
3824 struct GNUNET_HELLO_Address *
3825 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3827 struct NeighbourMapEntry *n;
3829 n = lookup_neighbour (peer);
3832 return n->primary_address.address;
3837 * Initialize the neighbours subsystem.
3839 * @param cls closure for callbacks
3840 * @param connect_cb function to call if we connect to a peer
3841 * @param disconnect_cb function to call if we disconnect from a peer
3842 * @param peer_address_cb function to call if we change an active address
3844 * @param max_fds maximum number of fds to use
3847 GST_neighbours_start (void *cls,
3848 NotifyConnect connect_cb,
3849 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3850 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3851 unsigned int max_fds)
3854 connect_notify_cb = connect_cb;
3855 disconnect_notify_cb = disconnect_cb;
3856 neighbour_change_cb = peer_address_cb;
3857 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3858 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3859 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3860 utilization_transmission, NULL);
3865 * Disconnect from the given neighbour.
3868 * @param key hash of neighbour's public key (not used)
3869 * @param value the 'struct NeighbourMapEntry' of the neighbour
3870 * @return #GNUNET_OK (continue to iterate)
3873 disconnect_all_neighbours (void *cls,
3874 const struct GNUNET_PeerIdentity *key,
3877 struct NeighbourMapEntry *n = value;
3879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3880 "Disconnecting peer `%4s', %s\n",
3881 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3882 free_neighbour (n, GNUNET_NO);
3888 * Cleanup the neighbours subsystem.
3891 GST_neighbours_stop ()
3893 struct BlacklistCheckSwitchContext *cur;
3894 struct BlacklistCheckSwitchContext *next;
3896 if (NULL == neighbours)
3898 if (NULL != util_transmission_tk)
3900 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3901 util_transmission_tk = NULL;
3903 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3904 &disconnect_all_neighbours,
3906 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3908 next = pending_bc_head;
3909 for (cur = next; NULL != cur; cur = next)
3912 GNUNET_CONTAINER_DLL_remove (pending_bc_head,
3916 if (NULL != cur->blc)
3918 GST_blacklist_test_cancel (cur->blc);
3921 if (NULL != cur->address)
3922 GNUNET_HELLO_address_free (cur->address);
3926 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
3927 &free_notification_cb, NULL);
3928 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
3929 registered_quota_notifications = NULL;
3932 callback_cls = NULL;
3933 connect_notify_cb = NULL;
3934 disconnect_notify_cb = NULL;
3935 neighbour_change_cb = NULL;
3939 /* end of file gnunet-service-transport_neighbours.c */