2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport_clients.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
41 * Size of the neighbour hash map.
43 #define NEIGHBOUR_TABLE_SIZE 256
46 * Time we give plugin to transmit DISCONNECT message before the
47 * neighbour entry self-destructs.
49 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
52 * How often must a peer violate bandwidth quotas before we start
53 * to simply drop its messages?
55 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
58 * How long are we willing to wait for a response from ATS before timing out?
60 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
63 * How long are we willing to wait for an ACK from the other peer before
64 * giving up on our connect operation?
66 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
69 * How long are we willing to wait for a successful reconnect if
70 * an existing connection went down? Much shorter than the
71 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
72 * higher layers about the disconnect during this period.
74 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
77 * How long are we willing to wait for a response from the blacklist
78 * subsystem before timing out?
80 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
83 * Interval to send utilization data
85 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
87 GNUNET_NETWORK_STRUCT_BEGIN
90 * Message a peer sends to another to indicate that it intends to
91 * setup a connection/session for data exchange. A 'SESSION_CONNECT'
92 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
93 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
94 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
95 * should be connected.
97 struct SessionConnectMessage
100 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT
101 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK
103 struct GNUNET_MessageHeader header;
108 uint32_t reserved GNUNET_PACKED;
111 * Absolute time at the sender. Only the most recent connect
112 * message implies which session is preferred by the sender.
114 struct GNUNET_TIME_AbsoluteNBO timestamp;
120 * Message a peer sends to another when connected to indicate that a
121 * session is in use and the peer is still alive or to respond to a keep alive.
122 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
123 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
124 * When the keep alive response with type is received, transport service
125 * will call the respective plugin to update the session timeout
127 struct SessionKeepAliveMessage
130 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
131 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
133 struct GNUNET_MessageHeader header;
136 * A nonce to identify the session the keep alive is used for
138 uint32_t nonce GNUNET_PACKED;
142 * Message we send to the other peer to notify him that we intentionally
143 * are disconnecting (to reduce timeouts). This is just a friendly
144 * notification, peers must not rely on always receiving disconnect
147 struct SessionDisconnectMessage
150 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
152 struct GNUNET_MessageHeader header;
157 uint32_t reserved GNUNET_PACKED;
160 * Purpose of the signature. Extends over the timestamp.
161 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
163 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
166 * Absolute time at the sender. Only the most recent connect
167 * message implies which session is preferred by the sender.
169 struct GNUNET_TIME_AbsoluteNBO timestamp;
172 * Public key of the sender.
174 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
177 * Signature of the peer that sends us the disconnect. Only
178 * valid if the timestamp is AFTER the timestamp from the
179 * corresponding 'CONNECT' message.
181 struct GNUNET_CRYPTO_EddsaSignature signature;
185 GNUNET_NETWORK_STRUCT_END
189 * For each neighbour we keep a list of messages
190 * that we still want to transmit to the neighbour.
196 * This is a doubly linked list.
198 struct MessageQueue *next;
201 * This is a doubly linked list.
203 struct MessageQueue *prev;
206 * Function to call once we're done.
208 GST_NeighbourSendContinuation cont;
211 * Closure for @e cont
216 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
217 * stuck together in memory. Allocated at the end of this struct.
219 const char *message_buf;
222 * Size of the message buf
224 size_t message_buf_size;
227 * At what time should we fail?
229 struct GNUNET_TIME_Absolute timeout;
237 * A possible address we could use to communicate with a neighbour.
239 struct NeighbourAddress
243 * Active session for this address.
245 struct Session *session;
248 * Network-level address information.
250 struct GNUNET_HELLO_Address *address;
253 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
254 * peer for this address. Use to check that the ACK is in response
255 * to our most recent 'CONNECT'.
257 struct GNUNET_TIME_Absolute connect_timestamp;
260 * Inbound bandwidth from ATS for this address.
262 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
265 * Outbound bandwidth from ATS for this address.
267 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
270 * Did we tell ATS that this is our 'active' address?
275 * The current nonce sent in the last keep alive messages
277 uint32_t keep_alive_nonce;
282 * Entry in neighbours.
284 struct NeighbourMapEntry
288 * Head of list of messages we would like to send to this peer;
289 * must contain at most one message per client.
291 struct MessageQueue *messages_head;
294 * Tail of list of messages we would like to send to this peer; must
295 * contain at most one message per client.
297 struct MessageQueue *messages_tail;
300 * Are we currently trying to send a message? If so, which one?
302 struct MessageQueue *is_active;
305 * Primary address we currently use to communicate with the neighbour.
307 struct NeighbourAddress primary_address;
310 * Alternative address currently under consideration for communicating
311 * with the neighbour.
313 struct NeighbourAddress alternative_address;
316 * Identity of this neighbour.
318 struct GNUNET_PeerIdentity id;
321 * Main task that drives this peer (timeouts, keepalives, etc.).
322 * Always runs the 'master_task'.
324 GNUNET_SCHEDULER_TaskIdentifier task;
327 * At what time should we sent the next keep-alive message?
329 struct GNUNET_TIME_Absolute keep_alive_time;
332 * At what time did we sent the last keep-alive message? Used
333 * to calculate round-trip time ("latency").
335 struct GNUNET_TIME_Absolute last_keep_alive_time;
338 * Timestamp we should include in our next CONNECT_ACK message.
339 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
340 * our CONNECT_ACK message.
342 struct GNUNET_TIME_Absolute connect_ack_timestamp;
345 * ATS address suggest handle
347 struct GNUNET_ATS_SuggestHandle *suggest_handle;
350 * Time where we should cut the connection (timeout) if we don't
351 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
352 * if we are in S_CONNECTED).
354 struct GNUNET_TIME_Absolute timeout;
357 * Latest calculated latency value
359 struct GNUNET_TIME_Relative latency;
362 * Tracker for inbound bandwidth.
364 struct GNUNET_BANDWIDTH_Tracker in_tracker;
367 * How often has the other peer (recently) violated the inbound
368 * traffic limit? Incremented by 10 per violation, decremented by 1
369 * per non-violation (for each time interval).
371 unsigned int quota_violation_count;
374 * The current state of the peer.
376 enum GNUNET_TRANSPORT_PeerState state;
379 * Did we sent an KEEP_ALIVE message and are we expecting a response?
381 int expect_latency_response;
384 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
385 * (once we have an address to use and the peer has been allowed by our
386 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
387 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
388 * a 'SESSION_ACK' (regardless of what our own state machine might say).
390 int send_connect_ack;
393 * Tracking utilization of outbound bandwidth
395 uint32_t util_payload_bytes_sent;
398 * Tracking utilization of inbound bandwidth
400 uint32_t util_payload_bytes_recv;
403 * Tracking utilization of outbound bandwidth
405 uint32_t util_total_bytes_sent;
408 * Tracking utilization of inbound bandwidth
410 uint32_t util_total_bytes_recv;
413 * Date of last utilization transmission
415 struct GNUNET_TIME_Absolute last_util_transmission;
420 * Context for blacklist checks and the #handle_test_blacklist_cont()
421 * function. Stores information about ongoing blacklist checks.
423 struct BlackListCheckContext
427 * We keep blacklist checks in a DLL.
429 struct BlackListCheckContext *next;
432 * We keep blacklist checks in a DLL.
434 struct BlackListCheckContext *prev;
437 * Address that is being checked.
439 struct NeighbourAddress na;
442 * Handle to the ongoing blacklist check.
444 struct GST_BlacklistCheck *bc;
449 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
451 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
454 * We keep blacklist checks in a DLL so that we can find
455 * the 'sessions' in their 'struct NeighbourAddress' if
456 * a session goes down.
458 static struct BlackListCheckContext *bc_head;
461 * We keep blacklist checks in a DLL.
463 static struct BlackListCheckContext *bc_tail;
466 * List of pending blacklist checks: head
468 static struct BlacklistCheckSwitchContext *pending_bc_head;
471 * List of pending blacklist checks: tail
473 static struct BlacklistCheckSwitchContext *pending_bc_tail;
476 * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb
478 static void *callback_cls;
481 * Function to call when we connected to a neighbour.
483 static NotifyConnect connect_notify_cb;
486 * Function to call when we disconnected from a neighbour.
488 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
491 * Function to call when a neighbour changed address, state or bandwidth.
493 static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb;
496 * counter for connected neighbours
498 static unsigned int neighbours_connected;
501 * Number of bytes we have currently queued for transmission.
503 static unsigned long long bytes_in_send_queue;
506 * Task transmitting utilization data
508 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
511 static struct GNUNET_CONTAINER_MultiPeerMap *registered_quota_notifications;
514 * Lookup a neighbour entry in the neighbours hash map.
516 * @param pid identity of the peer to look up
517 * @return the entry, NULL if there is no existing record
519 static struct NeighbourMapEntry *
520 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
522 if (NULL == neighbours)
524 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
529 * Test if we're connected to the given peer.
531 * @param n neighbour entry of peer to test
532 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
535 test_connected (struct NeighbourMapEntry *n)
539 return GNUNET_TRANSPORT_is_connected (n->state);
543 * Send information about a new outbound quota to our clients.
545 * @param target affected peer
546 * @param quota new quota
549 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
550 struct GNUNET_BANDWIDTH_Value32NBO quota)
552 struct QuotaSetMessage q_msg;
554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
555 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
556 ntohl (quota.value__), GNUNET_i2s (target));
557 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
558 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
560 q_msg.peer = (*target);
561 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
566 * We don't need a given neighbour address any more.
567 * Release its resources and give appropriate notifications
568 * to ATS and other subsystems.
570 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
573 free_address (struct NeighbourAddress *na)
575 if (GNUNET_YES == na->ats_active)
577 GST_validation_set_address_use (na->address, na->session, GNUNET_NO);
578 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
581 na->ats_active = GNUNET_NO;
582 na->keep_alive_nonce = 0;
583 if (NULL != na->address)
585 GNUNET_HELLO_address_free (na->address);
593 * Set net state for this neighbour and notify monitoring
595 * @param n the respective neighbour
596 * @param s the new state
599 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
602 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
604 GNUNET_TRANSPORT_ps2s(s));
605 neighbour_change_cb (callback_cls,
607 n->primary_address.address,
608 n->state, n->timeout,
609 n->primary_address.bandwidth_in,
610 n->primary_address.bandwidth_out);
615 * Set net state and state timeout for this neighbour and notify monitoring
617 * @param n the respective neighbour
618 * @param s the new state
619 * @param timeout the new timeout
622 set_state_and_timeout (struct NeighbourMapEntry *n,
623 enum GNUNET_TRANSPORT_PeerState s,
624 struct GNUNET_TIME_Absolute timeout)
627 n->timeout = timeout;
628 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
630 GNUNET_TRANSPORT_ps2s(s),
631 GNUNET_STRINGS_absolute_time_to_string (timeout));
632 neighbour_change_cb (callback_cls,
634 n->primary_address.address,
635 n->state, n->timeout,
636 n->primary_address.bandwidth_in,
637 n->primary_address.bandwidth_out);
642 * Set new state timeout for this neighbour and notify monitoring
644 * @param n the respective neighbour
645 * @param timeout the new timeout
648 set_timeout (struct NeighbourMapEntry *n,
649 struct GNUNET_TIME_Absolute timeout)
651 n->timeout = timeout;
652 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed timeout %s\n",
654 GNUNET_STRINGS_absolute_time_to_string (timeout));
655 neighbour_change_cb (callback_cls,
657 n->primary_address.address,
658 n->state, n->timeout,
659 n->primary_address.bandwidth_in,
660 n->primary_address.bandwidth_out);
665 * Initialize the alternative address of a neighbour
667 * @param n the neighbour
668 * @param address address of the other peer, NULL if other peer
670 * @param session session to use (or NULL, in which case an
671 * address must be setup)
672 * @param bandwidth_in inbound quota to be used when connection is up
673 * @param bandwidth_out outbound quota to be used when connection is up
676 set_alternative_address (struct NeighbourMapEntry *n,
677 const struct GNUNET_HELLO_Address *address,
678 struct Session *session,
679 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
680 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
682 struct GNUNET_TRANSPORT_PluginFunctions *papi;
683 if (NULL == (papi = GST_plugins_find (address->transport_name)))
688 if (session == n->alternative_address.session)
690 n->alternative_address.bandwidth_in = bandwidth_in;
691 n->alternative_address.bandwidth_out = bandwidth_out;
694 free_address (&n->alternative_address);
696 session = papi->get_session (papi->cls, address);
699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
700 "Failed to obtain new session for peer `%s' and address '%s'\n",
701 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
702 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
706 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
708 GST_plugins_a2s(address));
710 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
711 n->alternative_address.bandwidth_in = bandwidth_in;
712 n->alternative_address.bandwidth_out = bandwidth_out;
713 n->alternative_address.session = session;
714 n->alternative_address.ats_active = GNUNET_NO;
715 n->alternative_address.keep_alive_nonce = 0;
720 * Initialize the primary address of a neighbour
722 * @param n the neighbour
723 * @param address address of the other peer, NULL if other peer
725 * @param session session to use (or NULL, in which case an
726 * address must be setup)
727 * @param bandwidth_in inbound quota to be used when connection is up
728 * @param bandwidth_out outbound quota to be used when connection is up
729 * @param is_active #GNUNET_YES to mark this as the active address with ATS
732 set_primary_address (struct NeighbourMapEntry *n,
733 const struct GNUNET_HELLO_Address *address,
734 struct Session *session,
735 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
736 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
739 struct GNUNET_TRANSPORT_PluginFunctions *papi;
741 if (NULL == (papi = GST_plugins_find (address->transport_name)))
746 if (session == n->primary_address.session)
748 n->primary_address.bandwidth_in = bandwidth_in;
749 n->primary_address.bandwidth_out = bandwidth_out;
750 if (is_active != n->primary_address.ats_active)
752 n->primary_address.ats_active = is_active;
753 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
754 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
756 if (GNUNET_YES == is_active)
758 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
759 send_outbound_quota (&address->peer, bandwidth_out);
763 free_address (&n->primary_address);
765 session = papi->get_session (papi->cls, address);
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
769 "Failed to obtain new session for peer `%s' and address '%s'\n",
770 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
771 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
775 n->primary_address.address = GNUNET_HELLO_address_copy (address);
776 n->primary_address.bandwidth_in = bandwidth_in;
777 n->primary_address.bandwidth_out = bandwidth_out;
778 n->primary_address.session = session;
779 n->primary_address.ats_active = is_active;
780 n->primary_address.keep_alive_nonce = 0;
781 if (GNUNET_YES == is_active)
783 /* Telling ATS about new session */
784 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
785 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
786 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
787 send_outbound_quota (&address->peer, bandwidth_out);
790 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
792 GST_plugins_a2s(address));
794 neighbour_change_cb (callback_cls,
796 n->primary_address.address,
797 n->state, n->timeout,
798 n->primary_address.bandwidth_in,
799 n->primary_address.bandwidth_out);
805 * Clear the primary address of a neighbour since this primary address is not
806 * valid anymore and notify monitoring about it
808 * @param n the neighbour
811 unset_primary_address (struct NeighbourMapEntry *n)
820 * Free a neighbour map entry.
822 * @param n entry to free
823 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
824 * #GNUNET_YES to keep all sessions
827 free_neighbour (struct NeighbourMapEntry *n,
830 struct MessageQueue *mq;
831 struct GNUNET_TRANSPORT_PluginFunctions *papi;
832 struct GNUNET_HELLO_Address *backup_primary;
834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
835 "Freeing neighbour state of peer `%s'\n",
836 GNUNET_i2s (&n->id));
837 n->is_active = NULL; /* always free'd by its own continuation! */
839 /* fail messages currently in the queue */
840 while (NULL != (mq = n->messages_head))
842 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
843 if (NULL != mq->cont)
844 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
847 /* It is too late to send other peer disconnect notifications, but at
848 least internally we need to get clean... */
849 if (GNUNET_YES == test_connected (n))
851 GNUNET_STATISTICS_set (GST_stats,
852 gettext_noop ("# peers connected"),
853 --neighbours_connected,
855 disconnect_notify_cb (callback_cls, &n->id);
857 /* Mark peer as disconnected */
858 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
860 if (NULL != n->primary_address.address)
861 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
863 backup_primary = NULL;
865 /* free addresses and mark as unused */
866 free_address (&n->primary_address);
867 free_address (&n->alternative_address);
869 /* cut all transport-level connection for this peer */
870 if ((GNUNET_NO == keep_sessions) &&
871 (NULL != backup_primary) &&
872 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
873 papi->disconnect_peer (papi->cls, &n->id);
875 if (NULL != backup_primary)
876 GNUNET_HELLO_address_free (backup_primary);
877 GNUNET_assert (GNUNET_YES ==
878 GNUNET_CONTAINER_multipeermap_remove (neighbours,
881 /* Cancel address requests for this peer */
882 if (NULL != n->suggest_handle)
884 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
885 n->suggest_handle = NULL;
888 /* Cancel the master task */
889 if (GNUNET_SCHEDULER_NO_TASK != n->task)
891 GNUNET_SCHEDULER_cancel (n->task);
892 n->task = GNUNET_SCHEDULER_NO_TASK;
894 /* free rest of memory */
900 * Transmit a message using the current session of the given
903 * @param n entry for the recipient
904 * @param msgbuf buffer to transmit
905 * @param msgbuf_size number of bytes in @a msgbuf buffer
906 * @param priority transmission priority
907 * @param timeout transmission timeout
908 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
909 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
910 * @param cont continuation to call when finished (can be NULL)
911 * @param cont_cls closure for @a cont
912 * @return timeout (copy of @a timeout or a calculated one if
913 * @a use_keepalive_timeout is #GNUNET_YES.
915 static struct GNUNET_TIME_Relative
916 send_with_session (struct NeighbourMapEntry *n,
917 const char *msgbuf, size_t msgbuf_size,
919 struct GNUNET_TIME_Relative timeout,
920 unsigned int use_keepalive_timeout,
921 GNUNET_TRANSPORT_TransmitContinuation cont,
924 struct GNUNET_TRANSPORT_PluginFunctions *papi;
925 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
927 GNUNET_assert (n->primary_address.session != NULL);
928 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
929 (-1 == papi->send (papi->cls,
930 n->primary_address.session,
933 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
934 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
935 papi->query_keepalive_factor (papi->cls))),
936 cont, cont_cls)))) &&
938 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
939 GST_neighbours_notify_data_sent (&n->id,
940 n->primary_address.address, n->primary_address.session, msgbuf_size);
941 GNUNET_break (NULL != papi);
947 * Master task run for every neighbour. Performs all of the time-related
948 * activities (keep alive, send next message, disconnect if idle, finish
949 * clean up after disconnect).
951 * @param cls the `struct NeighbourMapEntry` for which we are running
952 * @param tc scheduler context (unused)
955 master_task (void *cls,
956 const struct GNUNET_SCHEDULER_TaskContext *tc);
960 * Function called when the 'DISCONNECT' message has been sent by the
961 * plugin. Frees the neighbour --- if the entry still exists.
964 * @param target identity of the neighbour that was disconnected
965 * @param result #GNUNET_OK if the disconnect got out successfully
966 * @param payload bytes payload
967 * @param physical bytes physical
970 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
971 int result, size_t payload, size_t physical)
973 struct NeighbourMapEntry *n;
975 n = lookup_neighbour (target);
977 return; /* already gone */
978 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
979 return; /* have created a fresh entry since */
980 if (GNUNET_SCHEDULER_NO_TASK != n->task)
981 GNUNET_SCHEDULER_cancel (n->task);
982 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
987 * Transmit a DISCONNECT message to the other peer.
989 * @param n neighbour to send DISCONNECT message.
992 send_disconnect (struct NeighbourMapEntry *n)
994 struct SessionDisconnectMessage disconnect_msg;
996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
997 "Sending DISCONNECT message to peer `%4s'\n",
998 GNUNET_i2s (&n->id));
999 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1000 disconnect_msg.header.type =
1001 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1002 disconnect_msg.reserved = htonl (0);
1003 disconnect_msg.purpose.size =
1004 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1005 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1006 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1007 disconnect_msg.purpose.purpose =
1008 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1009 disconnect_msg.timestamp =
1010 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1011 disconnect_msg.public_key = GST_my_identity.public_key;
1012 GNUNET_assert (GNUNET_OK ==
1013 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1014 &disconnect_msg.purpose,
1015 &disconnect_msg.signature));
1017 (void) send_with_session (n,
1018 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1019 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1020 GNUNET_NO, &send_disconnect_cont, NULL);
1021 GNUNET_STATISTICS_update (GST_stats,
1023 ("# DISCONNECT messages sent"), 1,
1029 * Disconnect from the given neighbour, clean up the record.
1031 * @param n neighbour to disconnect from
1034 disconnect_neighbour (struct NeighbourMapEntry *n)
1036 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1037 "Disconnecting from peer %s in state %s\n",
1038 GNUNET_i2s (&n->id),
1039 GNUNET_TRANSPORT_ps2s (n->state));
1040 /* depending on state, notify neighbour and/or upper layers of this peer
1044 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1045 case GNUNET_TRANSPORT_PS_INIT_ATS:
1046 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1047 /* other peer is completely unaware of us, no need to send DISCONNECT */
1048 free_neighbour (n, GNUNET_NO);
1050 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1051 send_disconnect (n);
1052 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1054 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1055 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1056 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1057 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1058 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1059 free_neighbour (n, GNUNET_NO);
1061 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1062 /* we DID ACK the other peer's request, must send DISCONNECT */
1063 send_disconnect (n);
1064 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1066 case GNUNET_TRANSPORT_PS_CONNECTED:
1067 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1068 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1069 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1070 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1071 /* we are currently connected, need to send disconnect and do
1072 internal notifications and update statistics */
1073 send_disconnect (n);
1074 GNUNET_STATISTICS_set (GST_stats,
1075 gettext_noop ("# peers connected"),
1076 --neighbours_connected,
1078 disconnect_notify_cb (callback_cls, &n->id);
1079 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1081 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1082 /* ATS address request timeout, disconnect without sending disconnect message */
1083 GNUNET_STATISTICS_set (GST_stats,
1084 gettext_noop ("# peers connected"),
1085 --neighbours_connected,
1087 disconnect_notify_cb (callback_cls, &n->id);
1088 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1090 case GNUNET_TRANSPORT_PS_DISCONNECT:
1091 /* already disconnected, ignore */
1093 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1094 /* already cleaned up, how did we get here!? */
1098 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1099 "Unhandled state `%s'\n",
1100 GNUNET_TRANSPORT_ps2s (n->state));
1104 /* schedule timeout to clean up */
1105 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1106 GNUNET_SCHEDULER_cancel (n->task);
1107 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1113 * We're done with our transmission attempt, continue processing.
1115 * @param cls the `struct MessageQueue` of the message
1116 * @param receiver intended receiver
1117 * @param success whether it worked or not
1118 * @param size_payload bytes payload sent
1119 * @param physical bytes sent on wire
1122 transmit_send_continuation (void *cls,
1123 const struct GNUNET_PeerIdentity *receiver,
1124 int success, size_t size_payload, size_t physical)
1126 struct MessageQueue *mq = cls;
1127 struct NeighbourMapEntry *n;
1129 if (NULL == (n = lookup_neighbour (receiver)))
1132 return; /* disconnect or other error while transmitting, can happen */
1134 if (n->is_active == mq)
1136 /* this is still "our" neighbour, remove us from its queue
1137 and allow it to send the next message now */
1138 n->is_active = NULL;
1139 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1140 GNUNET_SCHEDULER_cancel (n->task);
1141 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1143 if (bytes_in_send_queue < mq->message_buf_size)
1145 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1146 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1147 bytes_in_send_queue, mq->message_buf_size,
1148 (GNUNET_OK == success) ? "OK" : "FAIL",
1149 size_payload, physical);
1154 GNUNET_break (size_payload == mq->message_buf_size);
1155 bytes_in_send_queue -= mq->message_buf_size;
1156 GNUNET_STATISTICS_set (GST_stats,
1158 ("# bytes in message queue for other peers"),
1159 bytes_in_send_queue, GNUNET_NO);
1160 if (GNUNET_OK == success)
1161 GNUNET_STATISTICS_update (GST_stats,
1163 ("# messages transmitted to other peers"),
1166 GNUNET_STATISTICS_update (GST_stats,
1168 ("# transmission failures for messages to other peers"),
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171 "Sending message to `%s' of type %u was a %s\n",
1172 GNUNET_i2s (receiver),
1173 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1174 (success == GNUNET_OK) ? "success" : "FAILURE");
1175 if (NULL != mq->cont)
1176 mq->cont (mq->cont_cls, success, size_payload, physical);
1182 * Check the message list for the given neighbour and if we can
1183 * send a message, do so. This function should only be called
1184 * if the connection is at least generally ready for transmission.
1185 * While we will only send one message at a time, no bandwidth
1186 * quota management is performed here. If a message was given to
1187 * the plugin, the continuation will automatically re-schedule
1188 * the 'master' task once the next message might be transmitted.
1190 * @param n target peer for which to transmit
1193 try_transmission_to_peer (struct NeighbourMapEntry *n)
1195 struct MessageQueue *mq;
1196 struct GNUNET_TIME_Relative timeout;
1198 if (NULL == n->primary_address.address)
1200 /* no address, why are we here? */
1204 if ((0 == n->primary_address.address->address_length) &&
1205 (NULL == n->primary_address.session))
1207 /* no address, why are we here? */
1211 if (NULL != n->is_active)
1213 /* transmission already pending */
1217 /* timeout messages from the queue that are past their due date */
1218 while (NULL != (mq = n->messages_head))
1220 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1221 if (timeout.rel_value_us > 0)
1223 GNUNET_STATISTICS_update (GST_stats,
1225 ("# messages timed out while in transport queue"),
1227 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1229 transmit_send_continuation (mq, &n->id,
1231 mq->message_buf_size, 0); /* timeout */
1234 return; /* no more messages */
1235 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1237 (void) send_with_session (n,
1238 mq->message_buf, mq->message_buf_size,
1239 0 /* priority */, timeout, GNUNET_NO,
1240 &transmit_send_continuation, mq);
1245 * Send keepalive message to the neighbour. Must only be called
1246 * if we are on 'connected' state or while trying to switch addresses.
1247 * Will internally determine if a keepalive is truly needed (so can
1248 * always be called).
1250 * @param n neighbour that went idle and needs a keepalive
1253 send_keepalive (struct NeighbourMapEntry *n)
1255 struct SessionKeepAliveMessage m;
1256 struct GNUNET_TIME_Relative timeout;
1259 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1260 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1261 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1262 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1263 return; /* no keepalive needed at this time */
1265 nonce = 0; /* 0 indicates 'not set' */
1267 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1269 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1270 "Sending keep alive to peer `%s' with nonce %u\n",
1271 GNUNET_i2s (&n->id), nonce);
1273 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1274 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1275 m.nonce = htonl (nonce);
1277 timeout = send_with_session (n,
1278 (const void *) &m, sizeof (m),
1279 UINT32_MAX /* priority */,
1280 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1282 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1284 n->primary_address.keep_alive_nonce = nonce;
1285 n->expect_latency_response = GNUNET_YES;
1286 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1287 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1293 * Keep the connection to the given neighbour alive longer,
1294 * we received a KEEPALIVE (or equivalent); send a response.
1296 * @param neighbour neighbour to keep alive (by sending keep alive response)
1297 * @param m the keep alive message containing the nonce to respond to
1300 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1301 const struct GNUNET_MessageHeader *m)
1303 struct NeighbourMapEntry *n;
1304 const struct SessionKeepAliveMessage *msg_in;
1305 struct SessionKeepAliveMessage msg;
1307 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1310 msg_in = (struct SessionKeepAliveMessage *) m;
1311 if (NULL == (n = lookup_neighbour (neighbour)))
1313 GNUNET_STATISTICS_update (GST_stats,
1315 ("# KEEPALIVE messages discarded (peer unknown)"),
1319 if (NULL == n->primary_address.session)
1321 GNUNET_STATISTICS_update (GST_stats,
1323 ("# KEEPALIVE messages discarded (no session)"),
1328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1329 "Received keep alive request from peer `%s' with nonce %u\n",
1330 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1332 /* send reply to allow neighbour to measure latency */
1333 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1334 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1335 msg.nonce = msg_in->nonce;
1336 (void) send_with_session(n,
1337 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1338 UINT32_MAX /* priority */,
1339 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1345 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1346 * latency to this peer. Pass the updated information (existing ats
1347 * plus calculated latency) to ATS.
1349 * @param neighbour neighbour to keep alive
1350 * @param m the message containing the keep alive response
1353 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1354 const struct GNUNET_MessageHeader *m)
1356 struct NeighbourMapEntry *n;
1357 const struct SessionKeepAliveMessage *msg;
1358 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1360 struct GNUNET_ATS_Information ats;
1362 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1365 msg = (const struct SessionKeepAliveMessage *) m;
1366 if (NULL == (n = lookup_neighbour (neighbour)))
1368 GNUNET_STATISTICS_update (GST_stats,
1370 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1374 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1375 (GNUNET_YES != n->expect_latency_response) )
1377 GNUNET_STATISTICS_update (GST_stats,
1379 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1383 if (NULL == n->primary_address.address)
1385 GNUNET_STATISTICS_update (GST_stats,
1387 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1391 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1393 GNUNET_STATISTICS_update (GST_stats,
1395 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1402 "Received keep alive response from peer `%s' for session %p\n",
1403 GNUNET_i2s (&n->id), n->primary_address.session);
1407 /* Update session timeout here */
1408 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 "Updating session for peer `%s' for session %p\n",
1412 GNUNET_i2s (&n->id), n->primary_address.session);
1413 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1420 n->primary_address.keep_alive_nonce = 0;
1421 n->expect_latency_response = GNUNET_NO;
1422 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1423 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1426 "Latency for peer `%s' is %s\n",
1427 GNUNET_i2s (&n->id),
1428 GNUNET_STRINGS_relative_time_to_string (n->latency,
1430 /* append latency */
1431 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1432 if (n->latency.rel_value_us > UINT32_MAX)
1433 latency = UINT32_MAX;
1435 latency = n->latency.rel_value_us;
1436 ats.value = htonl (latency);
1437 GST_ats_update_metrics (&n->id, n->primary_address.address,
1438 n->primary_address.session, &ats, 1);
1443 * We have received a message from the given sender. How long should
1444 * we delay before receiving more? (Also used to keep the peer marked
1447 * @param sender sender of the message
1448 * @param size size of the message
1449 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1450 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1451 * #GNUNET_SYSERR if the connection is not fully up yet
1452 * @return how long to wait before reading more from this sender
1454 struct GNUNET_TIME_Relative
1455 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1456 *sender, ssize_t size, int *do_forward)
1458 struct NeighbourMapEntry *n;
1459 struct GNUNET_TIME_Relative ret;
1461 if (NULL == neighbours)
1463 *do_forward = GNUNET_NO;
1464 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1466 if (NULL == (n = lookup_neighbour (sender)))
1468 GST_neighbours_try_connect (sender);
1469 if (NULL == (n = lookup_neighbour (sender)))
1471 GNUNET_STATISTICS_update (GST_stats,
1473 ("# messages discarded due to lack of neighbour record"),
1475 *do_forward = GNUNET_NO;
1476 return GNUNET_TIME_UNIT_ZERO;
1479 if (! test_connected (n))
1481 *do_forward = GNUNET_SYSERR;
1482 return GNUNET_TIME_UNIT_ZERO;
1484 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1486 n->quota_violation_count++;
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1488 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1489 n->in_tracker.available_bytes_per_s__,
1490 n->quota_violation_count);
1491 /* Discount 32k per violation */
1492 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1496 if (n->quota_violation_count > 0)
1498 /* try to add 32k back */
1499 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1500 n->quota_violation_count--;
1503 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1505 GNUNET_STATISTICS_update (GST_stats,
1507 ("# bandwidth quota violations by other peers"),
1509 *do_forward = GNUNET_NO;
1510 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1512 *do_forward = GNUNET_YES;
1513 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1514 if (ret.rel_value_us > 0)
1516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1517 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1518 (unsigned long long) n->in_tracker.
1519 consumption_since_last_update__,
1520 (unsigned int) n->in_tracker.available_bytes_per_s__,
1521 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1522 GNUNET_STATISTICS_update (GST_stats,
1523 gettext_noop ("# ms throttling suggested"),
1524 (int64_t) ret.rel_value_us / 1000LL,
1532 * Transmit a message to the given target using the active connection.
1534 * @param target destination
1535 * @param msg message to send
1536 * @param msg_size number of bytes in msg
1537 * @param timeout when to fail with timeout
1538 * @param cont function to call when done
1539 * @param cont_cls closure for 'cont'
1542 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1543 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1544 GST_NeighbourSendContinuation cont, void *cont_cls)
1546 struct NeighbourMapEntry *n;
1547 struct MessageQueue *mq;
1549 /* All ove these cases should never happen; they are all API violations.
1550 But we check anyway, just to be sure. */
1551 if (NULL == (n = lookup_neighbour (target)))
1555 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1558 if (GNUNET_YES != test_connected (n))
1562 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1565 bytes_in_send_queue += msg_size;
1566 GNUNET_STATISTICS_set (GST_stats,
1568 ("# bytes in message queue for other peers"),
1569 bytes_in_send_queue, GNUNET_NO);
1570 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1572 mq->cont_cls = cont_cls;
1573 memcpy (&mq[1], msg, msg_size);
1574 mq->message_buf = (const char *) &mq[1];
1575 mq->message_buf_size = msg_size;
1576 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1577 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1578 if ( (NULL != n->is_active) ||
1579 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1581 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1582 GNUNET_SCHEDULER_cancel (n->task);
1583 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1588 * Send a SESSION_CONNECT message via the given address.
1590 * @param na address to use
1593 send_session_connect (struct NeighbourAddress *na)
1595 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1596 struct SessionConnectMessage connect_msg;
1597 struct NeighbourMapEntry *n;
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "Sending SESSION_CONNECT message to peer %s\n",
1601 GNUNET_i2s (&na->address->peer));
1602 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1607 if (NULL == na->session)
1608 na->session = papi->get_session (papi->cls, na->address);
1609 if (NULL == na->session)
1614 GNUNET_STATISTICS_update (GST_stats,
1616 ("# SESSION_CONNECT messages sent"),
1618 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1619 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1620 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1621 connect_msg.reserved = htonl (0);
1622 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1624 papi->send (papi->cls,
1626 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1628 GNUNET_TIME_UNIT_FOREVER_REL,
1631 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1632 _("Failed to transmit CONNECT message via plugin to %s\n"),
1633 GST_plugins_a2s (na->address));
1635 n = lookup_neighbour (&na->address->peer);
1641 /* Hard failure to send the CONNECT message with this address:
1642 Destroy address and session */
1643 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1644 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1646 /* Remove address and request and additional one */
1652 GST_neighbours_notify_data_sent (&na->address->peer,
1655 sizeof (struct SessionConnectMessage));
1661 * Send a SESSION_CONNECT_ACK message via the given address.
1663 * @param address address to use
1664 * @param session session to use
1665 * @param timestamp timestamp to use for the ACK message
1668 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1669 struct Session *session,
1670 struct GNUNET_TIME_Absolute timestamp)
1672 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1673 struct SessionConnectMessage connect_msg;
1675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1676 "Sending CONNECT_ACK to peer `%s'\n",
1677 GNUNET_i2s (&address->peer));
1678 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1683 if (NULL == session)
1684 session = papi->get_session (papi->cls, address);
1685 if (NULL == session)
1690 GNUNET_STATISTICS_update (GST_stats,
1692 ("# CONNECT_ACK messages sent"),
1694 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1695 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1696 connect_msg.reserved = htonl (0);
1697 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1698 (void) papi->send (papi->cls,
1700 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1702 GNUNET_TIME_UNIT_FOREVER_REL,
1707 struct QuotaNotificationRequest
1709 struct GNUNET_PeerIdentity peer;
1710 struct Session *session;
1714 struct QNR_LookContext
1716 struct GNUNET_PeerIdentity peer;
1717 struct Session *session;
1720 struct QuotaNotificationRequest *res;
1724 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1726 struct QNR_LookContext *qnr_ctx = cls;
1727 struct QuotaNotificationRequest *qnr = value;
1729 if ((qnr->session == qnr_ctx->session) &&
1730 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1731 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1733 qnr_ctx->res = value;
1740 GST_neighbours_register_quota_notification(void *cls,
1741 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1742 struct Session *session)
1744 struct QuotaNotificationRequest *qnr;
1745 struct QNR_LookContext qnr_ctx;
1747 if (NULL == registered_quota_notifications)
1749 return; /* init or shutdown */
1752 qnr_ctx.peer = (*peer);
1753 qnr_ctx.plugin = plugin;
1754 qnr_ctx.session = session;
1757 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1758 peer, &find_notification_request, &qnr_ctx);
1759 if (NULL != qnr_ctx.res)
1765 qnr = GNUNET_new (struct QuotaNotificationRequest);
1766 qnr->peer = (*peer);
1767 qnr->plugin = GNUNET_strdup (plugin);
1768 qnr->session = session;
1770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1771 "Adding notification for peer `%s' plugin `%s' session %p \n",
1772 GNUNET_i2s (peer), plugin, session);
1774 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1775 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1780 GST_neighbours_unregister_quota_notification(void *cls,
1781 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1783 struct QNR_LookContext qnr_ctx;
1785 if (NULL == registered_quota_notifications)
1787 return; /* init or shutdown */
1790 qnr_ctx.peer = (*peer);
1791 qnr_ctx.plugin = plugin;
1792 qnr_ctx.session = session;
1795 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1796 &find_notification_request, &qnr_ctx);
1797 if (NULL == qnr_ctx.res)
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1804 "Removing notification for peer `%s' plugin `%s' session %p \n",
1805 GNUNET_i2s (peer), plugin, session);
1807 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
1809 GNUNET_free (qnr_ctx.res->plugin);
1810 GNUNET_free (qnr_ctx.res);
1814 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1816 /* struct NeighbourMapEntry *n = cls; */
1817 struct QuotaNotificationRequest *qnr = value;
1818 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1819 struct GNUNET_TIME_Relative delay;
1822 papi = GST_plugins_find(qnr->plugin);
1829 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
1830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1831 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
1832 delay.rel_value_us / 1000);
1834 if (NULL != papi->update_inbound_delay)
1835 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
1841 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
1844 /* struct NeighbourMapEntry *n = cls; */
1845 struct QuotaNotificationRequest *qnr = value;
1847 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
1849 GNUNET_free(qnr->plugin);
1856 inbound_bw_tracker_update(void *cls)
1858 struct NeighbourMapEntry *n = cls;
1860 /* Quota was updated, tell plugins to update the time to receive next */
1861 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1862 &n->id, ¬ification_cb, n);
1867 * Create a fresh entry in the neighbour map for the given peer
1869 * @param peer peer to create an entry for
1870 * @return new neighbour map entry
1872 static struct NeighbourMapEntry *
1873 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1875 struct NeighbourMapEntry *n;
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Creating new neighbour entry for `%s'\n",
1880 n = GNUNET_new (struct NeighbourMapEntry);
1882 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1883 n->last_util_transmission = GNUNET_TIME_absolute_get();
1884 n->util_payload_bytes_recv = 0;
1885 n->util_payload_bytes_sent = 0;
1886 n->util_total_bytes_recv = 0;
1887 n->util_total_bytes_sent = 0;
1888 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
1889 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1890 MAX_BANDWIDTH_CARRY_S);
1891 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1892 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
1893 GNUNET_assert (GNUNET_OK ==
1894 GNUNET_CONTAINER_multipeermap_put (neighbours,
1896 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1902 * Check if the two given addresses are the same.
1903 * Actually only checks if the sessions are non-NULL
1904 * (which they should be) and then if they are identical;
1905 * the actual addresses don't matter if the session
1906 * pointers match anyway, and we must have session pointers
1909 * @param a1 first address to compare
1910 * @param a2 other address to compare
1911 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1914 address_matches (const struct NeighbourAddress *a1,
1915 const struct NeighbourAddress *a2)
1917 if ( (NULL == a1->session) ||
1918 (NULL == a2->session) )
1923 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1927 /* We received a address suggestion after requesting an address in
1928 * try_connect or after receiving a connect, switch to address
1931 address_suggest_cont (void *cls,
1932 const struct GNUNET_PeerIdentity *peer,
1933 const struct GNUNET_HELLO_Address *address, struct Session *session,
1934 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1935 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1936 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
1938 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
1939 bandwidth_in, bandwidth_out);
1943 struct BlacklistCheckSwitchContext
1945 struct BlacklistCheckSwitchContext *prev;
1946 struct BlacklistCheckSwitchContext *next;
1949 struct GST_BlacklistCheck *blc;
1951 struct GNUNET_HELLO_Address *address;
1952 struct Session *session;
1953 struct GNUNET_ATS_Information *ats;
1956 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
1957 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
1961 * Black list check result for try_connect call
1962 * If connection to the peer is allowed request adddress and
1964 * @param cls blc_ctx bl context
1965 * @param peer the peer
1966 * @param result the result
1969 try_connect_bl_check_cont (void *cls,
1970 const struct GNUNET_PeerIdentity *peer, int result)
1972 struct BlacklistCheckSwitchContext *blc_ctx = cls;
1973 struct NeighbourMapEntry *n;
1975 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
1976 GNUNET_free (blc_ctx);
1978 if (GNUNET_OK != result)
1980 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1981 _("Blacklisting disapproved to connect to peer `%s'\n"),
1986 /* Setup a new neighbour */
1987 n = setup_neighbour (peer);
1989 /* Request address suggestions for this peer */
1990 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1991 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1992 GNUNET_ATS_reset_backoff (GST_ats, peer);
1993 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
1994 &address_suggest_cont, n);
2000 * Try to create a connection to the given target (eventually).
2002 * @param target peer to try to connect to
2005 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2007 struct NeighbourMapEntry *n;
2008 struct GST_BlacklistCheck *blc;
2009 struct BlacklistCheckSwitchContext *blc_ctx;
2011 if (NULL == neighbours)
2013 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2014 "Asked to connect to peer `%s' during shutdown\n",
2015 GNUNET_i2s (target));
2016 return; /* during shutdown, do nothing */
2018 n = lookup_neighbour (target);
2019 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2020 "Asked to connect to peer `%s' (state: %s)\n",
2021 GNUNET_i2s (target),
2022 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2027 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2028 /* this should not be possible */
2030 free_neighbour (n, GNUNET_NO);
2032 case GNUNET_TRANSPORT_PS_INIT_ATS:
2033 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2034 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2035 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2036 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2037 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2038 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2039 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2040 "Ignoring request to try to connect to `%s', already trying!\n",
2041 GNUNET_i2s (target));
2042 return; /* already trying */
2043 case GNUNET_TRANSPORT_PS_CONNECTED:
2044 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2045 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2046 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2047 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2048 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2049 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2050 "Ignoring request to try to connect, already connected to `%s'!\n",
2051 GNUNET_i2s (target));
2052 return; /* already connected */
2053 case GNUNET_TRANSPORT_PS_DISCONNECT:
2054 /* get rid of remains, ready to re-try immediately */
2055 free_neighbour (n, GNUNET_NO);
2057 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2058 /* should not be possible */
2061 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2062 "Unhandled state `%s'\n",
2063 GNUNET_TRANSPORT_ps2s (n->state));
2065 free_neighbour (n, GNUNET_NO);
2070 /* Do blacklist check if connecting to this peer is allowed */
2071 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2072 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2074 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2075 &try_connect_bl_check_cont, blc_ctx)))
2083 * Function called with the result of a blacklist check.
2085 * @param cls closure with the `struct BlackListCheckContext`
2086 * @param peer peer this check affects
2087 * @param result #GNUNET_OK if the address is allowed
2090 handle_test_blacklist_cont (void *cls,
2091 const struct GNUNET_PeerIdentity *peer,
2094 struct BlackListCheckContext *bcc = cls;
2095 struct NeighbourMapEntry *n;
2098 GNUNET_CONTAINER_DLL_remove (bc_head,
2101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2102 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2104 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2105 if (GNUNET_OK == result)
2107 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2111 /* Blacklist disagreed on connecting to a peer with this address
2112 * Destroy address because we are not allowed to use it
2114 if (NULL != bcc->na.session)
2115 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
2116 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
2118 if (NULL == (n = lookup_neighbour (peer)))
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2123 goto cleanup; /* nobody left to care about new address */
2125 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2126 "Received blacklist result for peer `%s' in state %s/%d\n",
2128 GNUNET_TRANSPORT_ps2s (n->state),
2129 n->send_connect_ack);
2132 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2133 /* this should not be possible */
2135 free_neighbour (n, GNUNET_NO);
2137 case GNUNET_TRANSPORT_PS_INIT_ATS:
2138 /* waiting on ATS suggestion; still, pass address to ATS as a
2141 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2142 /* check if the address the blacklist was fine with matches
2143 ATS suggestion, if so, we can move on! */
2144 if ( (GNUNET_OK == result) &&
2145 (1 == n->send_connect_ack) )
2147 n->send_connect_ack = 2;
2148 send_session_connect_ack_message (bcc->na.address,
2150 n->connect_ack_timestamp);
2152 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2155 "Blacklist result for peer %s is for non-primary address, ignored\n",
2157 break; /* result for an address we currently don't care about */
2159 if (GNUNET_OK == result)
2161 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2162 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2163 send_session_connect (&n->primary_address);
2167 free_address (&n->primary_address);
2169 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2170 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2173 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2174 /* waiting on CONNECT_ACK, send ACK if one is pending */
2175 if ( (GNUNET_OK == result) &&
2176 (1 == n->send_connect_ack) )
2178 n->send_connect_ack = 2;
2179 send_session_connect_ack_message (n->primary_address.address,
2180 n->primary_address.session,
2181 n->connect_ack_timestamp);
2184 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2185 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2186 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2187 GNUNET_ATS_reset_backoff (GST_ats, peer);
2188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2189 "Suggesting address for peer %s to ATS\n",
2191 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2192 &address_suggest_cont, n);
2194 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2195 /* waiting on ATS suggestion, don't care about blacklist */
2197 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2198 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2201 "Blacklist result ignored, as it is not for our primary address\n");
2202 break; /* result for an address we currently don't care about */
2204 if (GNUNET_OK == result)
2206 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2207 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2208 send_session_connect_ack_message (bcc->na.address,
2210 n->connect_ack_timestamp);
2211 if (1 == n->send_connect_ack)
2212 n->send_connect_ack = 2;
2216 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2218 plugin = GST_plugins_find (bcc->na.address->transport_name);
2219 if ( (NULL != plugin) &&
2220 (NULL != bcc->na.session) )
2222 plugin->disconnect_session (plugin->cls,
2226 GNUNET_break (NULL != plugin);
2227 free_address (&n->primary_address);
2228 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2229 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2230 GNUNET_ATS_reset_backoff (GST_ats, peer);
2233 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2234 /* waiting on SESSION_ACK, send ACK if one is pending */
2235 if ( (GNUNET_OK == result) &&
2236 (1 == n->send_connect_ack) )
2238 n->send_connect_ack = 2;
2239 send_session_connect_ack_message (n->primary_address.address,
2240 n->primary_address.session,
2241 n->connect_ack_timestamp);
2244 case GNUNET_TRANSPORT_PS_CONNECTED:
2245 /* already connected, don't care about blacklist */
2247 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2248 /* still waiting on ATS suggestion, don't care about blacklist */
2250 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2251 if ( (GNUNET_OK == result) &&
2252 (1 == n->send_connect_ack) )
2254 n->send_connect_ack = 2;
2255 send_session_connect_ack_message (bcc->na.address,
2257 n->connect_ack_timestamp);
2259 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2262 "Blacklist result ignored, as it is not for our primary address\n");
2263 break; /* result for an address we currently don't care about */
2265 if (GNUNET_OK == result)
2267 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2268 send_session_connect (&n->primary_address);
2272 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2275 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2276 /* waiting on CONNECT_ACK, don't care about blacklist */
2277 if ( (GNUNET_OK == result) &&
2278 (1 == n->send_connect_ack) )
2280 n->send_connect_ack = 2;
2281 send_session_connect_ack_message (n->primary_address.address,
2282 n->primary_address.session,
2283 n->connect_ack_timestamp);
2286 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2287 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2289 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2290 "Blacklist result ignored, as it is not for our primary address\n");
2291 break; /* result for an address we currently don't care about */
2293 if (GNUNET_OK == result)
2295 send_session_connect (&n->alternative_address);
2296 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2300 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2301 free_address (&n->alternative_address);
2304 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2305 /* waiting on CONNECT_ACK, don't care about blacklist */
2306 if ( (GNUNET_OK == result) &&
2307 (1 == n->send_connect_ack) )
2309 n->send_connect_ack = 2;
2310 send_session_connect_ack_message (n->primary_address.address,
2311 n->primary_address.session,
2312 n->connect_ack_timestamp);
2315 case GNUNET_TRANSPORT_PS_DISCONNECT:
2316 /* Nothing to do here, ATS will already do what can be done */
2318 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2319 /* should not be possible */
2323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2324 "Unhandled state `%s'\n",
2325 GNUNET_TRANSPORT_ps2s (n->state));
2327 free_neighbour (n, GNUNET_NO);
2331 GNUNET_HELLO_address_free (bcc->na.address);
2337 * We want to know if connecting to a particular peer via
2338 * a particular address is allowed. Check it!
2340 * @param peer identity of the peer to switch the address for
2341 * @param ts time at which the check was initiated
2342 * @param address address of the other peer, NULL if other peer
2344 * @param session session to use (or NULL)
2347 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2348 struct GNUNET_TIME_Absolute ts,
2349 const struct GNUNET_HELLO_Address *address,
2350 struct Session *session)
2352 struct BlackListCheckContext *bcc;
2353 struct GST_BlacklistCheck *bc;
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 "Checking peer `%s' against blacklist\n",
2358 bcc = GNUNET_new (struct BlackListCheckContext);
2359 bcc->na.address = GNUNET_HELLO_address_copy (address);
2360 bcc->na.session = session;
2361 bcc->na.connect_timestamp = ts;
2362 GNUNET_CONTAINER_DLL_insert (bc_head,
2365 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2366 address->transport_name,
2367 &handle_test_blacklist_cont, bcc)))
2369 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2370 we must only store 'bc' if 'bc' is non-NULL... */
2375 * We received a 'SESSION_CONNECT' message from the other peer.
2376 * Consider switching to it.
2378 * @param message possibly a 'struct SessionConnectMessage' (check format)
2379 * @param peer identity of the peer to switch the address for
2380 * @param address address of the other peer, NULL if other peer
2382 * @param session session to use (or NULL)
2383 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2386 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2387 const struct GNUNET_PeerIdentity *peer,
2388 const struct GNUNET_HELLO_Address *address,
2389 struct Session *session)
2391 const struct SessionConnectMessage *scm;
2392 struct NeighbourMapEntry *n;
2393 struct GNUNET_TIME_Absolute ts;
2395 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2396 "Received CONNECT message from peer `%s'\n",
2398 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2400 GNUNET_break_op (0);
2401 return GNUNET_SYSERR;
2403 GNUNET_STATISTICS_update (GST_stats,
2405 ("# CONNECT messages received"),
2407 if (NULL == neighbours)
2409 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2410 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2412 return GNUNET_OK; /* we're shutting down */
2414 scm = (const struct SessionConnectMessage *) message;
2415 GNUNET_break_op (0 == ntohl (scm->reserved));
2416 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2417 n = lookup_neighbour (peer);
2419 n = setup_neighbour (peer);
2420 n->send_connect_ack = 1;
2421 n->connect_ack_timestamp = ts;
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2424 "Received SESSION_CONNECT for peer `%s' in state %s/%d\n",
2426 GNUNET_TRANSPORT_ps2s (n->state),
2427 n->send_connect_ack);
2430 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2431 /* Do a blacklist check for the new address */
2432 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2433 check_blacklist (peer, ts, address, session);
2435 case GNUNET_TRANSPORT_PS_INIT_ATS:
2436 /* CONNECT message takes priority over us asking ATS for address */
2437 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2439 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2440 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2441 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2442 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2443 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2444 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2445 /* It can never hurt to have an alternative address in the above cases,
2446 see if it is allowed */
2447 check_blacklist (peer, ts, address, session);
2449 case GNUNET_TRANSPORT_PS_CONNECTED:
2450 /* we are already connected and can thus send the ACK immediately;
2451 still, it can never hurt to have an alternative address, so also
2452 tell ATS about it */
2453 GNUNET_assert (NULL != n->primary_address.address);
2454 GNUNET_assert (NULL != n->primary_address.session);
2455 n->send_connect_ack = 0;
2456 send_session_connect_ack_message (n->primary_address.address,
2457 n->primary_address.session, ts);
2458 check_blacklist (peer, ts, address, session);
2460 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2461 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2462 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2463 /* It can never hurt to have an alternative address in the above cases,
2464 see if it is allowed */
2465 check_blacklist (peer, ts, address, session);
2467 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2468 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2469 /* we are already connected and can thus send the ACK immediately;
2470 still, it can never hurt to have an alternative address, so also
2471 tell ATS about it */
2472 GNUNET_assert (NULL != n->primary_address.address);
2473 GNUNET_assert (NULL != n->primary_address.session);
2474 n->send_connect_ack = 0;
2475 send_session_connect_ack_message (n->primary_address.address,
2476 n->primary_address.session, ts);
2477 check_blacklist (peer, ts, address, session);
2479 case GNUNET_TRANSPORT_PS_DISCONNECT:
2480 /* get rid of remains without terminating sessions, ready to re-try */
2481 free_neighbour (n, GNUNET_YES);
2482 n = setup_neighbour (peer);
2483 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2484 GNUNET_ATS_reset_backoff (GST_ats, peer);
2486 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2487 /* should not be possible */
2491 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2492 "Unhandled state `%s'\n",
2493 GNUNET_TRANSPORT_ps2s (n->state));
2495 return GNUNET_SYSERR;
2501 switch_address_bl_check_cont (void *cls,
2502 const struct GNUNET_PeerIdentity *peer, int result)
2504 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2505 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2506 struct NeighbourMapEntry *n;
2508 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2509 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2514 "Peer %s is unknown, suggestion ignored\n",
2517 if (result == GNUNET_NO)
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2521 GST_plugins_a2s (blc_ctx->address),
2523 GNUNET_i2s (&blc_ctx->address->peer));
2525 if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2527 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2528 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2529 blc_ctx->address->transport_name,
2530 GST_plugins_a2s (blc_ctx->address),
2532 GNUNET_i2s (&blc_ctx->address->peer));
2535 /* This address is blacklisted, delete address and session (if existing) in ATS */
2536 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2538 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2539 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2541 /* This is an inbound address, destroy full address */
2542 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2545 /* Remove blacklist check and clean up */
2546 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2547 GNUNET_HELLO_address_free (blc_ctx->address);
2548 GNUNET_free_non_null (blc_ctx->ats);
2549 GNUNET_free (blc_ctx);
2553 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2554 "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2555 GST_plugins_a2s (blc_ctx->address),
2557 GNUNET_i2s (&blc_ctx->address->peer));
2559 if (NULL == blc_ctx->session)
2561 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2563 "Obtained new session for peer `%s' and address '%s': %p\n",
2564 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2566 if (NULL == blc_ctx->session)
2568 /* No session could be obtained, remove blacklist check and clean up */
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2570 "Failed to obtain new session for peer `%s' and address '%s'\n",
2571 GNUNET_i2s (&blc_ctx->address->peer),
2572 GST_plugins_a2s (blc_ctx->address));
2573 /* Delete address in ATS */
2574 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2576 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2577 GNUNET_HELLO_address_free (blc_ctx->address);
2578 GNUNET_free_non_null (blc_ctx->ats);
2579 GNUNET_free (blc_ctx);
2585 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2587 free_neighbour (n, GNUNET_NO);
2589 case GNUNET_TRANSPORT_PS_INIT_ATS:
2590 /* We requested an address and ATS suggests one:
2591 * set primary address and send CONNECT message*/
2592 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2593 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2594 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2595 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2596 send_session_connect (&n->primary_address);
2598 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2599 /* ATS suggests a different address, switch again */
2600 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2601 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2602 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2603 check_blacklist (&n->id, n->connect_ack_timestamp,
2604 blc_ctx->address, blc_ctx->session);
2606 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2607 /* ATS suggests a different address, switch again */
2608 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2609 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2610 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_BLACKLIST,
2611 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2612 check_blacklist (&n->id, n->connect_ack_timestamp,
2613 blc_ctx->address, blc_ctx->session);
2615 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2616 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2617 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2618 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2619 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2620 check_blacklist (&n->id, n->connect_ack_timestamp,
2621 blc_ctx->address, blc_ctx->session);
2623 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2624 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2625 check_blacklist (&n->id, n->connect_ack_timestamp,
2626 blc_ctx->address, blc_ctx->session);
2628 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2629 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2630 /* ATS asks us to switch while we were trying to connect; switch to new
2631 address and check blacklist again */
2632 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2633 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2634 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2635 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2636 check_blacklist (&n->id, n->connect_ack_timestamp,
2637 blc_ctx->address, blc_ctx->session);
2639 case GNUNET_TRANSPORT_PS_CONNECTED:
2640 GNUNET_assert (NULL != n->primary_address.address);
2641 GNUNET_assert (NULL != n->primary_address.session);
2642 if (n->primary_address.session == blc_ctx->session)
2644 /* not an address change, just a quota change */
2645 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2646 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2649 /* ATS asks us to switch a life connection; see if we can get
2650 a CONNECT_ACK on it before we actually do this! */
2651 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2652 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2653 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2654 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2655 blc_ctx->address, blc_ctx->session);
2657 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2658 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2659 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2660 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2661 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2662 check_blacklist (&n->id, n->connect_ack_timestamp,
2663 blc_ctx->address, blc_ctx->session);
2665 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2666 /* ATS asks us to switch while we were trying to reconnect; switch to new
2667 address and check blacklist again */
2668 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2669 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2670 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2671 check_blacklist (&n->id, n->connect_ack_timestamp,
2672 blc_ctx->address, blc_ctx->session);
2674 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2675 /* ATS asks us to switch while we were trying to reconnect; switch to new
2676 address and check blacklist again */
2677 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2678 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2679 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2680 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2681 check_blacklist (&n->id, n->connect_ack_timestamp,
2682 blc_ctx->address, blc_ctx->session);
2684 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2685 if (n->primary_address.session == blc_ctx->session)
2687 /* ATS switches back to still-active session */
2688 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2689 free_address (&n->alternative_address);
2692 /* ATS asks us to switch a life connection, update blacklist check */
2693 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2694 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2695 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2696 blc_ctx->address, blc_ctx->session);
2698 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2699 if (n->primary_address.session == blc_ctx->session)
2701 /* ATS switches back to still-active session */
2702 free_address (&n->alternative_address);
2703 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2706 /* ATS asks us to switch a life connection, update blacklist check */
2707 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2708 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2709 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2710 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2711 blc_ctx->address, blc_ctx->session);
2713 case GNUNET_TRANSPORT_PS_DISCONNECT:
2714 /* not going to switch addresses while disconnecting */
2716 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2720 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2721 "Unhandled state `%s'\n",
2722 GNUNET_TRANSPORT_ps2s (n->state));
2727 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2728 GNUNET_HELLO_address_free(blc_ctx->address);
2729 GNUNET_free_non_null (blc_ctx->ats);
2730 GNUNET_free (blc_ctx);
2736 * For the given peer, switch to this address.
2738 * Before accepting this addresses and actively using it, a blacklist check
2739 * is performed. If this blacklist check fails the address will be destroyed.
2741 * @param peer identity of the peer to switch the address for
2742 * @param address address of the other peer,
2743 * @param session session to use or NULL if transport should initiate a session
2744 * @param ats performance data
2745 * @param ats_count number of entries in ats
2746 * @param bandwidth_in inbound quota to be used when connection is up,
2747 * 0 to disconnect from peer
2748 * @param bandwidth_out outbound quota to be used when connection is up,
2749 * 0 to disconnect from peer
2752 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2753 const struct GNUNET_HELLO_Address *address,
2754 struct Session *session,
2755 const struct GNUNET_ATS_Information *ats,
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 GNUNET_TRANSPORT_PluginFunctions *papi;
2763 struct BlacklistCheckSwitchContext *blc_ctx;
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2767 "ATS has decided on an address for peer %s\n",
2769 GNUNET_assert (NULL != address->transport_name);
2770 if (NULL == (n = lookup_neighbour (peer)))
2772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2773 "Peer %s is unknown, suggestion ignored\n",
2778 /* Check if plugin is available */
2779 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2781 /* we don't have the plugin for this address */
2782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2783 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2784 address->transport_name,
2786 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2789 if ((NULL == session) &&
2790 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2792 /* This is a inbound address and we do not have a session to use! */
2793 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2794 "Inbound address without session `%s'! Destroying address...\n",
2795 GST_plugins_a2s (address));
2796 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2800 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2801 "ATS tells us to switch to %s address '%s' session %p for "
2802 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2803 GNUNET_HELLO_address_check_option (address,
2804 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "",
2805 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2806 GNUNET_TRANSPORT_ps2s (n->state), n->send_connect_ack,
2807 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2809 /* Perform blacklist check */
2810 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2811 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2812 blc_ctx->session = session;
2813 blc_ctx->bandwidth_in = bandwidth_in;
2814 blc_ctx->bandwidth_out = bandwidth_out;
2815 blc_ctx->ats_count = ats_count;
2816 blc_ctx->ats = NULL;
2819 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2820 for (c = 0; c < ats_count; c++)
2822 blc_ctx->ats[c].type = ats[c].type;
2823 blc_ctx->ats[c].value = ats[c].value;
2827 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2828 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2829 &switch_address_bl_check_cont, blc_ctx)))
2837 send_utilization_data (void *cls,
2838 const struct GNUNET_PeerIdentity *key,
2841 struct NeighbourMapEntry *n = value;
2842 struct GNUNET_ATS_Information atsi[4];
2844 uint32_t bps_pl_out;
2847 struct GNUNET_TIME_Relative delta;
2849 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2850 GNUNET_TIME_absolute_get ());
2854 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2855 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2857 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2858 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2860 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2865 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2866 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2868 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2869 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2873 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2877 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2878 atsi[0].value = htonl (bps_out);
2879 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2880 atsi[1].value = htonl (bps_in);
2882 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2883 atsi[2].value = htonl (bps_pl_out);
2884 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2885 atsi[3].value = htonl (bps_pl_in);
2887 GST_ats_update_metrics (key, n->primary_address.address,
2888 n->primary_address.session, atsi, 4);
2889 n->util_payload_bytes_recv = 0;
2890 n->util_payload_bytes_sent = 0;
2891 n->util_total_bytes_recv = 0;
2892 n->util_total_bytes_sent = 0;
2893 n->last_util_transmission = GNUNET_TIME_absolute_get();
2899 * Task transmitting utilization in a regular interval
2901 * @param cls the 'struct NeighbourMapEntry' for which we are running
2902 * @param tc scheduler context (unused)
2905 utilization_transmission (void *cls,
2906 const struct GNUNET_SCHEDULER_TaskContext *tc)
2908 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2910 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2911 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2913 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2914 utilization_transmission, NULL);
2920 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2921 const struct GNUNET_HELLO_Address *address,
2922 struct Session *session,
2923 const struct GNUNET_MessageHeader *message)
2925 struct NeighbourMapEntry *n;
2927 n = lookup_neighbour (peer);
2930 n->util_total_bytes_recv += ntohs(message->size);
2935 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2936 const struct GNUNET_HELLO_Address *address,
2937 struct Session *session,
2938 const struct GNUNET_MessageHeader *message)
2940 struct NeighbourMapEntry *n;
2941 n = lookup_neighbour (peer);
2944 n->util_payload_bytes_recv += ntohs(message->size);
2949 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2950 const struct GNUNET_HELLO_Address *address,
2951 struct Session *session,
2954 struct NeighbourMapEntry *n;
2955 n = lookup_neighbour (peer);
2958 if (n->primary_address.session != session)
2960 n->util_total_bytes_sent += size;
2965 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2968 struct NeighbourMapEntry *n;
2969 n = lookup_neighbour (peer);
2972 n->util_payload_bytes_sent += size;
2977 * Master task run for every neighbour. Performs all of the time-related
2978 * activities (keep alive, send next message, disconnect if idle, finish
2979 * clean up after disconnect).
2981 * @param cls the 'struct NeighbourMapEntry' for which we are running
2982 * @param tc scheduler context (unused)
2985 master_task (void *cls,
2986 const struct GNUNET_SCHEDULER_TaskContext *tc)
2988 struct NeighbourMapEntry *n = cls;
2989 struct GNUNET_TIME_Relative delay;
2991 n->task = GNUNET_SCHEDULER_NO_TASK;
2992 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2993 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2994 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2995 GNUNET_i2s (&n->id),
2996 GNUNET_TRANSPORT_ps2s(n->state),
2997 GNUNET_STRINGS_relative_time_to_string (delay,
3001 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3002 /* invalid state for master task, clean up */
3004 free_neighbour (n, GNUNET_NO);
3006 case GNUNET_TRANSPORT_PS_INIT_ATS:
3007 if (0 == delay.rel_value_us)
3009 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3010 "Connection to `%s' timed out waiting for ATS to provide address\n",
3011 GNUNET_i2s (&n->id));
3012 free_neighbour (n, GNUNET_NO);
3016 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3017 if (0 == delay.rel_value_us)
3019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3020 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
3021 GNUNET_i2s (&n->id));
3022 free_neighbour (n, GNUNET_NO);
3026 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3027 if (0 == delay.rel_value_us)
3029 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3030 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
3031 GNUNET_i2s (&n->id));
3032 /* We could not send to this address, delete address and session */
3033 if (NULL != n->primary_address.session)
3034 GNUNET_ATS_address_destroyed (GST_ats,
3035 n->primary_address.address, n->primary_address.session);
3036 GNUNET_ATS_address_destroyed (GST_ats,
3037 n->primary_address.address, NULL);
3038 disconnect_neighbour (n);
3042 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3043 if (0 == delay.rel_value_us)
3045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3046 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
3047 GNUNET_i2s (&n->id));
3048 free_neighbour (n, GNUNET_NO);
3052 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3053 if (0 == delay.rel_value_us)
3055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3056 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3057 GNUNET_i2s (&n->id));
3058 free_neighbour (n, GNUNET_NO);
3062 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3063 if (0 == delay.rel_value_us)
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3066 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
3067 GNUNET_i2s (&n->id));
3068 free_neighbour (n, GNUNET_NO);
3072 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3073 if (0 == delay.rel_value_us)
3075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3076 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3077 GNUNET_i2s (&n->id));
3078 disconnect_neighbour (n);
3082 case GNUNET_TRANSPORT_PS_CONNECTED:
3083 if (0 == delay.rel_value_us)
3085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3086 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3087 GNUNET_i2s (&n->id));
3088 disconnect_neighbour (n);
3091 try_transmission_to_peer (n);
3094 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3095 if (0 == delay.rel_value_us)
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3099 GNUNET_i2s (&n->id));
3100 disconnect_neighbour (n);
3104 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3105 if (0 == delay.rel_value_us)
3107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3108 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
3109 GNUNET_i2s (&n->id));
3110 disconnect_neighbour (n);
3114 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3115 if (0 == delay.rel_value_us)
3117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3118 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3119 GNUNET_i2s (&n->id));
3120 disconnect_neighbour (n);
3124 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3125 if (0 == delay.rel_value_us)
3127 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3128 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3129 GNUNET_i2s (&n->id));
3130 disconnect_neighbour (n);
3133 try_transmission_to_peer (n);
3136 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3137 if (0 == delay.rel_value_us)
3139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3140 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3141 GNUNET_i2s (&n->id));
3142 disconnect_neighbour (n);
3145 try_transmission_to_peer (n);
3148 case GNUNET_TRANSPORT_PS_DISCONNECT:
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3150 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3151 GNUNET_i2s (&n->id));
3152 free_neighbour (n, GNUNET_NO);
3154 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3155 /* how did we get here!? */
3159 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3160 "Unhandled state `%s'\n",
3161 GNUNET_TRANSPORT_ps2s (n->state));
3165 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3166 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3167 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3169 /* if we are *now* in one of these three states, we're sending
3170 keep alive messages, so we need to consider the keepalive
3171 delay, not just the connection timeout */
3172 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3175 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3176 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3183 * Send a SESSION_ACK message to the neighbour to confirm that we
3184 * got his CONNECT_ACK.
3186 * @param n neighbour to send the SESSION_ACK to
3189 send_session_ack_message (struct NeighbourMapEntry *n)
3191 struct GNUNET_MessageHeader msg;
3193 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3194 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3195 (void) send_with_session(n,
3196 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3197 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3203 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3204 * Consider switching to it.
3206 * @param message possibly a 'struct SessionConnectMessage' (check format)
3207 * @param peer identity of the peer to switch the address for
3208 * @param address address of the other peer, NULL if other peer
3210 * @param session session to use (or NULL)
3211 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3214 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3215 const struct GNUNET_PeerIdentity *peer,
3216 const struct GNUNET_HELLO_Address *address,
3217 struct Session *session)
3219 const struct SessionConnectMessage *scm;
3220 struct GNUNET_TIME_Absolute ts;
3221 struct NeighbourMapEntry *n;
3223 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3224 "Received CONNECT_ACK message from peer `%s'\n",
3227 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3229 GNUNET_break_op (0);
3230 return GNUNET_SYSERR;
3232 GNUNET_STATISTICS_update (GST_stats,
3234 ("# CONNECT_ACK messages received"),
3236 scm = (const struct SessionConnectMessage *) message;
3237 GNUNET_break_op (ntohl (scm->reserved) == 0);
3238 if (NULL == (n = lookup_neighbour (peer)))
3240 GNUNET_STATISTICS_update (GST_stats,
3242 ("# unexpected CONNECT_ACK messages (no peer)"),
3244 return GNUNET_SYSERR;
3246 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3249 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3251 free_neighbour (n, GNUNET_NO);
3252 return GNUNET_SYSERR;
3253 case GNUNET_TRANSPORT_PS_INIT_ATS:
3254 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3255 GNUNET_STATISTICS_update (GST_stats,
3257 ("# unexpected CONNECT_ACK messages (not ready)"),
3260 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3261 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3263 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3264 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3267 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3268 GNUNET_STATISTICS_set (GST_stats,
3269 gettext_noop ("# peers connected"),
3270 ++neighbours_connected,
3272 connect_notify_cb (callback_cls, &n->id,
3273 n->primary_address.bandwidth_in,
3274 n->primary_address.bandwidth_out);
3275 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3276 GST_ats_add_address (n->primary_address.address,
3277 n->primary_address.session,
3279 set_primary_address (n,
3280 n->primary_address.address,
3281 n->primary_address.session,
3282 n->primary_address.bandwidth_in,
3283 n->primary_address.bandwidth_out,
3285 send_session_ack_message (n);
3287 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3288 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3289 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3290 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3291 GNUNET_STATISTICS_update (GST_stats,
3293 ("# unexpected CONNECT_ACK messages (not ready)"),
3296 case GNUNET_TRANSPORT_PS_CONNECTED:
3297 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3298 send_session_ack_message (n);
3300 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3301 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3302 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3303 to give us a new address... */
3304 GNUNET_STATISTICS_update (GST_stats,
3306 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3309 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3310 /* new address worked; go back to connected! */
3311 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3312 send_session_ack_message (n);
3314 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3315 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3316 send_session_ack_message (n);
3318 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3319 /* new address worked; adopt it and go back to connected! */
3320 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3321 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3323 GST_ats_add_address (n->alternative_address.address,
3324 n->alternative_address.session,
3326 set_primary_address (n, n->alternative_address.address,
3327 n->alternative_address.session, n->alternative_address.bandwidth_in,
3328 n->alternative_address.bandwidth_out, GNUNET_YES);
3330 free_address (&n->alternative_address);
3331 send_session_ack_message (n);
3333 case GNUNET_TRANSPORT_PS_DISCONNECT:
3334 GNUNET_STATISTICS_update (GST_stats,
3336 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3338 return GNUNET_SYSERR;
3339 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3343 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3344 "Unhandled state `%s'\n",
3345 GNUNET_TRANSPORT_ps2s (n->state));
3347 return GNUNET_SYSERR;
3354 * A session was terminated. Take note; if needed, try to get
3355 * an alternative address from ATS.
3357 * @param peer identity of the peer where the session died
3358 * @param session session that is gone
3359 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3360 * this session was not in use
3363 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3364 struct Session *session)
3366 struct NeighbourMapEntry *n;
3367 struct BlackListCheckContext *bcc;
3368 struct BlackListCheckContext *bcc_next;
3370 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3372 while (NULL != (bcc = bcc_next))
3374 bcc_next = bcc->next;
3375 if (bcc->na.session == session)
3377 if (NULL != bcc->bc)
3378 GST_blacklist_test_cancel (bcc->bc);
3379 GNUNET_HELLO_address_free (bcc->na.address);
3380 GNUNET_CONTAINER_DLL_remove (bc_head,
3386 if (NULL == (n = lookup_neighbour (peer)))
3387 return GNUNET_NO; /* can't affect us */
3388 if (session != n->primary_address.session)
3390 if (session == n->alternative_address.session)
3392 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3393 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3394 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3397 free_address (&n->alternative_address);
3399 return GNUNET_NO; /* doesn't affect us further */
3402 n->expect_latency_response = GNUNET_NO;
3403 /* The session for neighbour's primary address died */
3406 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3408 free_neighbour (n, GNUNET_NO);
3410 case GNUNET_TRANSPORT_PS_INIT_ATS:
3412 free_neighbour (n, GNUNET_NO);
3414 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3415 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3416 /* The session used to send the CONNECT terminated:
3417 * this implies a connect error*/
3418 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3419 "Could not send CONNECT message with address `%s' session %p: session terminated, requesting new address\n",
3420 GST_plugins_a2s (n->primary_address.address), n->primary_address.session,
3422 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3423 free_address (&n->primary_address);
3424 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3425 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3427 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3428 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3429 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3430 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3431 /* error on inbound session; free neighbour entirely */
3432 free_address (&n->primary_address);
3433 free_neighbour (n, GNUNET_NO);
3435 case GNUNET_TRANSPORT_PS_CONNECTED:
3436 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3437 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3438 free_address (&n->primary_address);
3440 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3441 /* we don't have an address, how can it go down? */
3444 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3445 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3446 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3448 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3449 /* primary went down while we were checking secondary against
3450 blacklist, adopt secondary as primary */
3451 free_address (&n->primary_address);
3452 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3453 n->primary_address = n->alternative_address;
3454 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3456 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3457 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3458 secondary as primary */
3459 free_address (&n->primary_address);
3460 n->primary_address = n->alternative_address;
3461 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3462 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3464 case GNUNET_TRANSPORT_PS_DISCONNECT:
3465 free_address (&n->primary_address);
3467 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3468 /* neighbour was freed and plugins told to terminate session */
3472 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3473 "Unhandled state `%s'\n",
3474 GNUNET_TRANSPORT_ps2s (n->state));
3478 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3479 GNUNET_SCHEDULER_cancel (n->task);
3480 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3486 * We received a 'SESSION_ACK' message from the other peer.
3487 * If we sent a 'CONNECT_ACK' last, this means we are now
3488 * connected. Otherwise, do nothing.
3490 * @param message possibly a 'struct SessionConnectMessage' (check format)
3491 * @param peer identity of the peer to switch the address for
3492 * @param address address of the other peer, NULL if other peer
3494 * @param session session to use (or NULL)
3495 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3498 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3499 const struct GNUNET_PeerIdentity *peer,
3500 const struct GNUNET_HELLO_Address *address,
3501 struct Session *session)
3503 struct NeighbourMapEntry *n;
3505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3506 "Received SESSION_ACK message from peer `%s'\n",
3508 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3510 GNUNET_break_op (0);
3511 return GNUNET_SYSERR;
3513 GNUNET_STATISTICS_update (GST_stats,
3515 ("# SESSION_ACK messages received"),
3517 if (NULL == (n = lookup_neighbour (peer)))
3519 GNUNET_break_op (0);
3520 return GNUNET_SYSERR;
3522 /* check if we are in a plausible state for having sent
3523 a CONNECT_ACK. If not, return, otherwise break */
3524 if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3525 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3526 (2 != n->send_connect_ack) )
3528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3529 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3531 GNUNET_TRANSPORT_ps2s (n->state),
3532 n->send_connect_ack);
3533 GNUNET_STATISTICS_update (GST_stats,
3534 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3538 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3539 GNUNET_STATISTICS_set (GST_stats,
3540 gettext_noop ("# peers connected"),
3541 ++neighbours_connected,
3543 connect_notify_cb (callback_cls, &n->id,
3544 n->primary_address.bandwidth_in,
3545 n->primary_address.bandwidth_out);
3547 GST_ats_add_address (n->primary_address.address,
3548 n->primary_address.session,
3550 set_primary_address (n,
3551 n->primary_address.address,
3552 n->primary_address.session,
3553 n->primary_address.bandwidth_in,
3554 n->primary_address.bandwidth_out,
3561 * Test if we're connected to the given peer.
3563 * @param target peer to test
3564 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3567 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3569 return test_connected (lookup_neighbour (target));
3573 * Change the incoming quota for the given peer.
3575 * @param neighbour identity of peer to change qutoa for
3576 * @param quota new quota
3579 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3580 struct GNUNET_BANDWIDTH_Value32NBO quota)
3582 struct NeighbourMapEntry *n;
3584 if (NULL == (n = lookup_neighbour (neighbour)))
3586 GNUNET_STATISTICS_update (GST_stats,
3588 ("# SET QUOTA messages ignored (no such peer)"),
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3593 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3594 ntohl (quota.value__), GNUNET_i2s (&n->id));
3595 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3596 if (0 != ntohl (quota.value__))
3598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3599 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3600 GNUNET_i2s (&n->id));
3601 if (GNUNET_YES == test_connected (n))
3602 GNUNET_STATISTICS_update (GST_stats,
3603 gettext_noop ("# disconnects due to quota of 0"),
3605 disconnect_neighbour (n);
3610 * We received a disconnect message from the given peer,
3611 * validate and process.
3613 * @param peer sender of the message
3614 * @param msg the disconnect message
3617 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3618 const struct GNUNET_MessageHeader *msg)
3620 struct NeighbourMapEntry *n;
3621 const struct SessionDisconnectMessage *sdm;
3623 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3624 "Received DISCONNECT message from peer `%s'\n",
3626 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3628 // GNUNET_break_op (0);
3629 GNUNET_STATISTICS_update (GST_stats,
3631 ("# disconnect messages ignored (old format)"), 1,
3635 GNUNET_STATISTICS_update (GST_stats,
3637 ("# DISCONNECT messages received"),
3639 sdm = (const struct SessionDisconnectMessage *) msg;
3640 if (NULL == (n = lookup_neighbour (peer)))
3641 return; /* gone already */
3642 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3644 GNUNET_STATISTICS_update (GST_stats,
3646 ("# disconnect messages ignored (timestamp)"), 1,
3650 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3652 GNUNET_break_op (0);
3655 if (ntohl (sdm->purpose.size) !=
3656 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3657 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3658 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3660 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3661 "%s message from peer `%s' has invalid size \n",
3664 GNUNET_break_op (0);
3668 GNUNET_CRYPTO_eddsa_verify
3669 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3670 &sdm->signature, &sdm->public_key))
3672 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3673 "%s message from peer `%s' cannot be verified \n",
3676 GNUNET_break_op (0);
3679 if (GNUNET_YES == test_connected (n))
3680 GNUNET_STATISTICS_update (GST_stats,
3682 ("# other peer asked to disconnect from us"), 1,
3684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3685 "Disconnecting by request from peer %s\n",
3687 disconnect_neighbour (n);
3692 * Closure for the neighbours_iterate function.
3694 struct IteratorContext
3697 * Function to call on each connected neighbour.
3699 GST_NeighbourIterator cb;
3709 * Call the callback from the closure for each neighbour.
3711 * @param cls the `struct IteratorContext`
3712 * @param key the hash of the public key of the neighbour
3713 * @param value the `struct NeighbourMapEntry`
3714 * @return #GNUNET_OK (continue to iterate)
3717 neighbours_iterate (void *cls,
3718 const struct GNUNET_PeerIdentity *key,
3721 struct IteratorContext *ic = cls;
3722 struct NeighbourMapEntry *n = value;
3723 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3724 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3727 if (NULL != n->primary_address.address)
3729 bandwidth_in = n->primary_address.bandwidth_in;
3730 bandwidth_out = n->primary_address.bandwidth_out;
3734 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3735 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3739 n->primary_address.address,
3742 bandwidth_in, bandwidth_out);
3748 * Iterate over all connected neighbours.
3750 * @param cb function to call
3751 * @param cb_cls closure for cb
3754 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3756 struct IteratorContext ic;
3758 if (NULL == neighbours)
3759 return; /* can happen during shutdown */
3762 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3767 * If we have an active connection to the given target, it must be shutdown.
3769 * @param target peer to disconnect from
3772 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3774 struct NeighbourMapEntry *n;
3776 if (NULL == (n = lookup_neighbour (target)))
3777 return; /* not active */
3778 if (GNUNET_YES == test_connected (n))
3779 GNUNET_STATISTICS_update (GST_stats,
3781 ("# disconnected from peer upon explicit request"), 1,
3783 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3784 "Forced disconnect from peer %s\n",
3785 GNUNET_i2s (target));
3786 disconnect_neighbour (n);
3791 * Obtain current latency information for the given neighbour.
3793 * @param peer to get the latency for
3794 * @return observed latency of the address, FOREVER if the
3795 * the connection is not up
3797 struct GNUNET_TIME_Relative
3798 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3800 struct NeighbourMapEntry *n;
3802 n = lookup_neighbour (peer);
3804 return GNUNET_TIME_UNIT_FOREVER_REL;
3807 case GNUNET_TRANSPORT_PS_CONNECTED:
3808 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3809 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3810 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3811 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3812 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3814 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3815 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3816 case GNUNET_TRANSPORT_PS_INIT_ATS:
3817 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3818 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3819 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3820 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3821 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3822 case GNUNET_TRANSPORT_PS_DISCONNECT:
3823 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3824 return GNUNET_TIME_UNIT_FOREVER_REL;
3826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3827 "Unhandled state `%s'\n",
3828 GNUNET_TRANSPORT_ps2s (n->state));
3832 return GNUNET_TIME_UNIT_FOREVER_REL;
3837 * Obtain current address information for the given neighbour.
3840 * @return address currently used
3842 struct GNUNET_HELLO_Address *
3843 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3845 struct NeighbourMapEntry *n;
3847 n = lookup_neighbour (peer);
3850 return n->primary_address.address;
3855 * Initialize the neighbours subsystem.
3857 * @param cls closure for callbacks
3858 * @param connect_cb function to call if we connect to a peer
3859 * @param disconnect_cb function to call if we disconnect from a peer
3860 * @param peer_address_cb function to call if we change an active address
3862 * @param max_fds maximum number of fds to use
3865 GST_neighbours_start (void *cls,
3866 NotifyConnect connect_cb,
3867 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3868 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3869 unsigned int max_fds)
3872 connect_notify_cb = connect_cb;
3873 disconnect_notify_cb = disconnect_cb;
3874 neighbour_change_cb = peer_address_cb;
3875 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3876 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3877 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3878 utilization_transmission, NULL);
3883 * Disconnect from the given neighbour.
3886 * @param key hash of neighbour's public key (not used)
3887 * @param value the 'struct NeighbourMapEntry' of the neighbour
3888 * @return #GNUNET_OK (continue to iterate)
3891 disconnect_all_neighbours (void *cls,
3892 const struct GNUNET_PeerIdentity *key,
3895 struct NeighbourMapEntry *n = value;
3897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3898 "Disconnecting peer `%4s', %s\n",
3899 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3900 free_neighbour (n, GNUNET_NO);
3906 * Cleanup the neighbours subsystem.
3909 GST_neighbours_stop ()
3911 struct BlacklistCheckSwitchContext *cur;
3912 struct BlacklistCheckSwitchContext *next;
3914 if (NULL == neighbours)
3916 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3918 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3919 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3922 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3923 &disconnect_all_neighbours,
3925 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3927 next = pending_bc_head;
3928 for (cur = next; NULL != cur; cur = next )
3931 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3933 if (NULL != cur->blc)
3935 GST_blacklist_test_cancel (cur->blc);
3938 if (NULL != cur->address)
3939 GNUNET_HELLO_address_free (cur->address);
3940 GNUNET_free_non_null (cur->ats);
3944 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
3945 &free_notification_cb, NULL);
3946 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
3947 registered_quota_notifications = NULL;
3950 callback_cls = NULL;
3951 connect_notify_cb = NULL;
3952 disconnect_notify_cb = NULL;
3953 neighbour_change_cb = NULL;
3957 /* end of file gnunet-service-transport_neighbours.c */