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 neighbur 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 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
859 if (NULL != n->primary_address.address)
861 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
864 backup_primary = NULL;
866 /* free addresses and mark as unused */
867 free_address (&n->primary_address);
868 free_address (&n->alternative_address);
870 /* FIXME: Note that if we are switching between two TCP sessions to
871 the same peer, we might want to selectively kill only one of
872 them! Killing all sessions like this seems to be very, very
875 /* cut transport-level connection */
876 if ((GNUNET_NO == keep_sessions) &&
877 (NULL != backup_primary) &&
878 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
879 papi->disconnect_peer (papi->cls, &n->id);
881 GNUNET_free_non_null (backup_primary);
883 GNUNET_assert (GNUNET_YES ==
884 GNUNET_CONTAINER_multipeermap_remove (neighbours,
887 // FIXME-ATS-API: we might want to be more specific about
888 // which states we do this from in the future (ATS should
889 // have given us a 'suggest_address' handle, and if we have
890 // such a handle, we should cancel the operation here!
891 if (NULL != n->suggest_handle)
893 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
894 n->suggest_handle = NULL;
897 if (GNUNET_SCHEDULER_NO_TASK != n->task)
899 GNUNET_SCHEDULER_cancel (n->task);
900 n->task = GNUNET_SCHEDULER_NO_TASK;
902 /* free rest of memory */
908 * Transmit a message using the current session of the given
911 * @param n entry for the recipient
912 * @param msgbuf buffer to transmit
913 * @param msgbuf_size number of bytes in @a msgbuf buffer
914 * @param priority transmission priority
915 * @param timeout transmission timeout
916 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
917 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
918 * @param cont continuation to call when finished (can be NULL)
919 * @param cont_cls closure for @a cont
920 * @return timeout (copy of @a timeout or a calculated one if
921 * @a use_keepalive_timeout is #GNUNET_YES.
923 static struct GNUNET_TIME_Relative
924 send_with_session (struct NeighbourMapEntry *n,
925 const char *msgbuf, size_t msgbuf_size,
927 struct GNUNET_TIME_Relative timeout,
928 unsigned int use_keepalive_timeout,
929 GNUNET_TRANSPORT_TransmitContinuation cont,
932 struct GNUNET_TRANSPORT_PluginFunctions *papi;
933 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
935 GNUNET_assert (n->primary_address.session != NULL);
936 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
937 (-1 == papi->send (papi->cls,
938 n->primary_address.session,
941 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
942 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
943 papi->query_keepalive_factor (papi->cls))),
944 cont, cont_cls)))) &&
946 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
947 GST_neighbours_notify_data_sent (&n->id,
948 n->primary_address.address, n->primary_address.session, msgbuf_size);
949 GNUNET_break (NULL != papi);
955 * Master task run for every neighbour. Performs all of the time-related
956 * activities (keep alive, send next message, disconnect if idle, finish
957 * clean up after disconnect).
959 * @param cls the `struct NeighbourMapEntry` for which we are running
960 * @param tc scheduler context (unused)
963 master_task (void *cls,
964 const struct GNUNET_SCHEDULER_TaskContext *tc);
968 * Function called when the 'DISCONNECT' message has been sent by the
969 * plugin. Frees the neighbour --- if the entry still exists.
972 * @param target identity of the neighbour that was disconnected
973 * @param result #GNUNET_OK if the disconnect got out successfully
974 * @param payload bytes payload
975 * @param physical bytes physical
978 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
979 int result, size_t payload, size_t physical)
981 struct NeighbourMapEntry *n;
983 n = lookup_neighbour (target);
985 return; /* already gone */
986 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
987 return; /* have created a fresh entry since */
988 if (GNUNET_SCHEDULER_NO_TASK != n->task)
989 GNUNET_SCHEDULER_cancel (n->task);
990 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
995 * Transmit a DISCONNECT message to the other peer.
997 * @param n neighbour to send DISCONNECT message.
1000 send_disconnect (struct NeighbourMapEntry *n)
1002 struct SessionDisconnectMessage disconnect_msg;
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1005 "Sending DISCONNECT message to peer `%4s'\n",
1006 GNUNET_i2s (&n->id));
1007 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1008 disconnect_msg.header.type =
1009 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1010 disconnect_msg.reserved = htonl (0);
1011 disconnect_msg.purpose.size =
1012 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1013 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1014 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1015 disconnect_msg.purpose.purpose =
1016 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1017 disconnect_msg.timestamp =
1018 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1019 disconnect_msg.public_key = GST_my_identity.public_key;
1020 GNUNET_assert (GNUNET_OK ==
1021 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1022 &disconnect_msg.purpose,
1023 &disconnect_msg.signature));
1025 (void) send_with_session (n,
1026 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1027 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1028 GNUNET_NO, &send_disconnect_cont, NULL);
1029 GNUNET_STATISTICS_update (GST_stats,
1031 ("# DISCONNECT messages sent"), 1,
1037 * Disconnect from the given neighbour, clean up the record.
1039 * @param n neighbour to disconnect from
1042 disconnect_neighbour (struct NeighbourMapEntry *n)
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045 "Disconnecting from peer %s in state %s\n",
1046 GNUNET_i2s (&n->id),
1047 GNUNET_TRANSPORT_ps2s (n->state));
1048 /* depending on state, notify neighbour and/or upper layers of this peer
1052 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1053 case GNUNET_TRANSPORT_PS_INIT_ATS:
1054 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1055 /* other peer is completely unaware of us, no need to send DISCONNECT */
1056 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1057 free_neighbour (n, GNUNET_NO);
1059 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1060 send_disconnect (n);
1061 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1063 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1064 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1065 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1066 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1067 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1068 free_neighbour (n, GNUNET_NO);
1070 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1071 /* we DID ACK the other peer's request, must send DISCONNECT */
1072 send_disconnect (n);
1073 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1075 case GNUNET_TRANSPORT_PS_CONNECTED:
1076 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1077 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1078 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1079 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1080 /* we are currently connected, need to send disconnect and do
1081 internal notifications and update statistics */
1082 send_disconnect (n);
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_RECONNECT_ATS:
1091 /* ATS address request timeout, disconnect without sending disconnect message */
1092 GNUNET_STATISTICS_set (GST_stats,
1093 gettext_noop ("# peers connected"),
1094 --neighbours_connected,
1096 disconnect_notify_cb (callback_cls, &n->id);
1097 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1099 case GNUNET_TRANSPORT_PS_DISCONNECT:
1100 /* already disconnected, ignore */
1102 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1103 /* already cleaned up, how did we get here!? */
1107 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1108 "Unhandled state `%s'\n",
1109 GNUNET_TRANSPORT_ps2s (n->state));
1113 /* schedule timeout to clean up */
1114 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1115 GNUNET_SCHEDULER_cancel (n->task);
1116 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1122 * We're done with our transmission attempt, continue processing.
1124 * @param cls the `struct MessageQueue` of the message
1125 * @param receiver intended receiver
1126 * @param success whether it worked or not
1127 * @param size_payload bytes payload sent
1128 * @param physical bytes sent on wire
1131 transmit_send_continuation (void *cls,
1132 const struct GNUNET_PeerIdentity *receiver,
1133 int success, size_t size_payload, size_t physical)
1135 struct MessageQueue *mq = cls;
1136 struct NeighbourMapEntry *n;
1138 if (NULL == (n = lookup_neighbour (receiver)))
1141 return; /* disconnect or other error while transmitting, can happen */
1143 if (n->is_active == mq)
1145 /* this is still "our" neighbour, remove us from its queue
1146 and allow it to send the next message now */
1147 n->is_active = NULL;
1148 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1149 GNUNET_SCHEDULER_cancel (n->task);
1150 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1152 if (bytes_in_send_queue < mq->message_buf_size)
1154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1155 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1156 bytes_in_send_queue, mq->message_buf_size,
1157 (GNUNET_OK == success) ? "OK" : "FAIL",
1158 size_payload, physical);
1163 GNUNET_break (size_payload == mq->message_buf_size);
1164 bytes_in_send_queue -= mq->message_buf_size;
1165 GNUNET_STATISTICS_set (GST_stats,
1167 ("# bytes in message queue for other peers"),
1168 bytes_in_send_queue, GNUNET_NO);
1169 if (GNUNET_OK == success)
1170 GNUNET_STATISTICS_update (GST_stats,
1172 ("# messages transmitted to other peers"),
1175 GNUNET_STATISTICS_update (GST_stats,
1177 ("# transmission failures for messages to other peers"),
1179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1180 "Sending message to `%s' of type %u was a %s\n",
1181 GNUNET_i2s (receiver),
1182 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1183 (success == GNUNET_OK) ? "success" : "FAILURE");
1184 if (NULL != mq->cont)
1185 mq->cont (mq->cont_cls, success, size_payload, physical);
1191 * Check the message list for the given neighbour and if we can
1192 * send a message, do so. This function should only be called
1193 * if the connection is at least generally ready for transmission.
1194 * While we will only send one message at a time, no bandwidth
1195 * quota management is performed here. If a message was given to
1196 * the plugin, the continuation will automatically re-schedule
1197 * the 'master' task once the next message might be transmitted.
1199 * @param n target peer for which to transmit
1202 try_transmission_to_peer (struct NeighbourMapEntry *n)
1204 struct MessageQueue *mq;
1205 struct GNUNET_TIME_Relative timeout;
1207 if (NULL == n->primary_address.address)
1209 /* no address, why are we here? */
1213 if ((0 == n->primary_address.address->address_length) &&
1214 (NULL == n->primary_address.session))
1216 /* no address, why are we here? */
1220 if (NULL != n->is_active)
1222 /* transmission already pending */
1226 /* timeout messages from the queue that are past their due date */
1227 while (NULL != (mq = n->messages_head))
1229 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1230 if (timeout.rel_value_us > 0)
1232 GNUNET_STATISTICS_update (GST_stats,
1234 ("# messages timed out while in transport queue"),
1236 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1238 transmit_send_continuation (mq, &n->id,
1240 mq->message_buf_size, 0); /* timeout */
1243 return; /* no more messages */
1244 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1246 (void) send_with_session (n,
1247 mq->message_buf, mq->message_buf_size,
1248 0 /* priority */, timeout, GNUNET_NO,
1249 &transmit_send_continuation, mq);
1254 * Send keepalive message to the neighbour. Must only be called
1255 * if we are on 'connected' state or while trying to switch addresses.
1256 * Will internally determine if a keepalive is truly needed (so can
1257 * always be called).
1259 * @param n neighbour that went idle and needs a keepalive
1262 send_keepalive (struct NeighbourMapEntry *n)
1264 struct SessionKeepAliveMessage m;
1265 struct GNUNET_TIME_Relative timeout;
1268 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1269 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1270 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1271 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1272 return; /* no keepalive needed at this time */
1274 nonce = 0; /* 0 indicates 'not set' */
1276 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1279 "Sending keep alive to peer `%s' with nonce %u\n",
1280 GNUNET_i2s (&n->id), nonce);
1282 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1283 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1284 m.nonce = htonl (nonce);
1286 timeout = send_with_session (n,
1287 (const void *) &m, sizeof (m),
1288 UINT32_MAX /* priority */,
1289 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1291 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1293 n->primary_address.keep_alive_nonce = nonce;
1294 n->expect_latency_response = GNUNET_YES;
1295 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1296 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1302 * Keep the connection to the given neighbour alive longer,
1303 * we received a KEEPALIVE (or equivalent); send a response.
1305 * @param neighbour neighbour to keep alive (by sending keep alive response)
1306 * @param m the keep alive message containing the nonce to respond to
1309 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1310 const struct GNUNET_MessageHeader *m)
1312 struct NeighbourMapEntry *n;
1313 const struct SessionKeepAliveMessage *msg_in;
1314 struct SessionKeepAliveMessage msg;
1316 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1319 msg_in = (struct SessionKeepAliveMessage *) m;
1320 if (NULL == (n = lookup_neighbour (neighbour)))
1322 GNUNET_STATISTICS_update (GST_stats,
1324 ("# KEEPALIVE messages discarded (peer unknown)"),
1328 if (NULL == n->primary_address.session)
1330 GNUNET_STATISTICS_update (GST_stats,
1332 ("# KEEPALIVE messages discarded (no session)"),
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Received keep alive request from peer `%s' with nonce %u\n",
1339 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1341 /* send reply to allow neighbour to measure latency */
1342 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1343 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1344 msg.nonce = msg_in->nonce;
1345 (void) send_with_session(n,
1346 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1347 UINT32_MAX /* priority */,
1348 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1354 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1355 * latency to this peer. Pass the updated information (existing ats
1356 * plus calculated latency) to ATS.
1358 * @param neighbour neighbour to keep alive
1359 * @param m the message containing the keep alive response
1362 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1363 const struct GNUNET_MessageHeader *m)
1365 struct NeighbourMapEntry *n;
1366 const struct SessionKeepAliveMessage *msg;
1367 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1369 struct GNUNET_ATS_Information ats;
1371 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1374 msg = (const struct SessionKeepAliveMessage *) m;
1375 if (NULL == (n = lookup_neighbour (neighbour)))
1377 GNUNET_STATISTICS_update (GST_stats,
1379 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1383 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1384 (GNUNET_YES != n->expect_latency_response) )
1386 GNUNET_STATISTICS_update (GST_stats,
1388 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1392 if (NULL == n->primary_address.address)
1394 GNUNET_STATISTICS_update (GST_stats,
1396 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1400 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1402 GNUNET_STATISTICS_update (GST_stats,
1404 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1411 "Received keep alive response from peer `%s' for session %p\n",
1412 GNUNET_i2s (&n->id), n->primary_address.session);
1416 /* Update session timeout here */
1417 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1420 "Updating session for peer `%s' for session %p\n",
1421 GNUNET_i2s (&n->id), n->primary_address.session);
1422 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1429 n->primary_address.keep_alive_nonce = 0;
1430 n->expect_latency_response = GNUNET_NO;
1431 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1432 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1435 "Latency for peer `%s' is %s\n",
1436 GNUNET_i2s (&n->id),
1437 GNUNET_STRINGS_relative_time_to_string (n->latency,
1439 /* append latency */
1440 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1441 if (n->latency.rel_value_us > UINT32_MAX)
1442 latency = UINT32_MAX;
1444 latency = n->latency.rel_value_us;
1445 ats.value = htonl (latency);
1446 GST_ats_update_metrics (&n->id, n->primary_address.address,
1447 n->primary_address.session, &ats, 1);
1452 * We have received a message from the given sender. How long should
1453 * we delay before receiving more? (Also used to keep the peer marked
1456 * @param sender sender of the message
1457 * @param size size of the message
1458 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1459 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1460 * #GNUNET_SYSERR if the connection is not fully up yet
1461 * @return how long to wait before reading more from this sender
1463 struct GNUNET_TIME_Relative
1464 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1465 *sender, ssize_t size, int *do_forward)
1467 struct NeighbourMapEntry *n;
1468 struct GNUNET_TIME_Relative ret;
1470 if (NULL == neighbours)
1472 *do_forward = GNUNET_NO;
1473 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1475 if (NULL == (n = lookup_neighbour (sender)))
1477 GST_neighbours_try_connect (sender);
1478 if (NULL == (n = lookup_neighbour (sender)))
1480 GNUNET_STATISTICS_update (GST_stats,
1482 ("# messages discarded due to lack of neighbour record"),
1484 *do_forward = GNUNET_NO;
1485 return GNUNET_TIME_UNIT_ZERO;
1488 if (! test_connected (n))
1490 *do_forward = GNUNET_SYSERR;
1491 return GNUNET_TIME_UNIT_ZERO;
1493 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1495 n->quota_violation_count++;
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1498 n->in_tracker.available_bytes_per_s__,
1499 n->quota_violation_count);
1500 /* Discount 32k per violation */
1501 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1505 if (n->quota_violation_count > 0)
1507 /* try to add 32k back */
1508 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1509 n->quota_violation_count--;
1512 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1514 GNUNET_STATISTICS_update (GST_stats,
1516 ("# bandwidth quota violations by other peers"),
1518 *do_forward = GNUNET_NO;
1519 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1521 *do_forward = GNUNET_YES;
1522 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1523 if (ret.rel_value_us > 0)
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1527 (unsigned long long) n->in_tracker.
1528 consumption_since_last_update__,
1529 (unsigned int) n->in_tracker.available_bytes_per_s__,
1530 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1531 GNUNET_STATISTICS_update (GST_stats,
1532 gettext_noop ("# ms throttling suggested"),
1533 (int64_t) ret.rel_value_us / 1000LL,
1541 * Transmit a message to the given target using the active connection.
1543 * @param target destination
1544 * @param msg message to send
1545 * @param msg_size number of bytes in msg
1546 * @param timeout when to fail with timeout
1547 * @param cont function to call when done
1548 * @param cont_cls closure for 'cont'
1551 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1552 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1553 GST_NeighbourSendContinuation cont, void *cont_cls)
1555 struct NeighbourMapEntry *n;
1556 struct MessageQueue *mq;
1558 /* All ove these cases should never happen; they are all API violations.
1559 But we check anyway, just to be sure. */
1560 if (NULL == (n = lookup_neighbour (target)))
1564 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1567 if (GNUNET_YES != test_connected (n))
1571 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1574 bytes_in_send_queue += msg_size;
1575 GNUNET_STATISTICS_set (GST_stats,
1577 ("# bytes in message queue for other peers"),
1578 bytes_in_send_queue, GNUNET_NO);
1579 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1581 mq->cont_cls = cont_cls;
1582 memcpy (&mq[1], msg, msg_size);
1583 mq->message_buf = (const char *) &mq[1];
1584 mq->message_buf_size = msg_size;
1585 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1586 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1587 if ( (NULL != n->is_active) ||
1588 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1590 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1591 GNUNET_SCHEDULER_cancel (n->task);
1592 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1597 * Send a SESSION_CONNECT message via the given address.
1599 * @param na address to use
1602 send_session_connect (struct NeighbourAddress *na)
1604 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1605 struct SessionConnectMessage connect_msg;
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Sending SESSION_CONNECT message to peer %s\n",
1609 GNUNET_i2s (&na->address->peer));
1610 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1615 if (NULL == na->session)
1616 na->session = papi->get_session (papi->cls, na->address);
1617 if (NULL == na->session)
1622 GNUNET_STATISTICS_update (GST_stats,
1624 ("# SESSION_CONNECT messages sent"),
1626 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1627 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1628 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1629 connect_msg.reserved = htonl (0);
1630 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1632 papi->send (papi->cls,
1634 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1636 GNUNET_TIME_UNIT_FOREVER_REL,
1639 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1640 _("Failed to transmit CONNECT message via plugin to %s\n"),
1641 GST_plugins_a2s (na->address));
1643 GST_neighbours_notify_data_sent (&na->address->peer,
1646 sizeof (struct SessionConnectMessage));
1652 * Send a SESSION_CONNECT_ACK message via the given address.
1654 * @param address address to use
1655 * @param session session to use
1656 * @param timestamp timestamp to use for the ACK message
1659 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1660 struct Session *session,
1661 struct GNUNET_TIME_Absolute timestamp)
1663 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1664 struct SessionConnectMessage connect_msg;
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1667 "Sending CONNECT_ACK to peer `%s'\n",
1668 GNUNET_i2s (&address->peer));
1669 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1674 if (NULL == session)
1675 session = papi->get_session (papi->cls, address);
1676 if (NULL == session)
1681 GNUNET_STATISTICS_update (GST_stats,
1683 ("# CONNECT_ACK messages sent"),
1685 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1686 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1687 connect_msg.reserved = htonl (0);
1688 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1689 (void) papi->send (papi->cls,
1691 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1693 GNUNET_TIME_UNIT_FOREVER_REL,
1698 struct QuotaNotificationRequest
1700 struct GNUNET_PeerIdentity peer;
1701 struct Session *session;
1705 struct QNR_LookContext
1707 struct GNUNET_PeerIdentity peer;
1708 struct Session *session;
1711 struct QuotaNotificationRequest *res;
1715 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1717 struct QNR_LookContext *qnr_ctx = cls;
1718 struct QuotaNotificationRequest *qnr = value;
1720 if ((qnr->session == qnr_ctx->session) &&
1721 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1722 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1724 qnr_ctx->res = value;
1731 GST_neighbours_register_quota_notification(void *cls,
1732 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1733 struct Session *session)
1735 struct QuotaNotificationRequest *qnr;
1736 struct QNR_LookContext qnr_ctx;
1738 if (NULL == registered_quota_notifications)
1740 return; /* init or shutdown */
1743 qnr_ctx.peer = (*peer);
1744 qnr_ctx.plugin = plugin;
1745 qnr_ctx.session = session;
1748 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1749 peer, &find_notification_request, &qnr_ctx);
1750 if (NULL != qnr_ctx.res)
1756 qnr = GNUNET_new (struct QuotaNotificationRequest);
1757 qnr->peer = (*peer);
1758 qnr->plugin = GNUNET_strdup (plugin);
1759 qnr->session = session;
1761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1762 "Adding notification for peer `%s' plugin `%s' session %p \n",
1763 GNUNET_i2s (peer), plugin, session);
1765 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1766 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1771 GST_neighbours_unregister_quota_notification(void *cls,
1772 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1774 struct QNR_LookContext qnr_ctx;
1776 if (NULL == registered_quota_notifications)
1778 return; /* init or shutdown */
1781 qnr_ctx.peer = (*peer);
1782 qnr_ctx.plugin = plugin;
1783 qnr_ctx.session = session;
1786 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1787 &find_notification_request, &qnr_ctx);
1788 if (NULL == qnr_ctx.res)
1794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1795 "Removing notification for peer `%s' plugin `%s' session %p \n",
1796 GNUNET_i2s (peer), plugin, session);
1798 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
1800 GNUNET_free (qnr_ctx.res->plugin);
1801 GNUNET_free (qnr_ctx.res);
1805 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1807 /* struct NeighbourMapEntry *n = cls; */
1808 struct QuotaNotificationRequest *qnr = value;
1809 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1810 struct GNUNET_TIME_Relative delay;
1813 papi = GST_plugins_find(qnr->plugin);
1820 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
1823 delay.rel_value_us / 1000);
1825 if (NULL != papi->update_inbound_delay)
1826 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
1832 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
1835 /* struct NeighbourMapEntry *n = cls; */
1836 struct QuotaNotificationRequest *qnr = value;
1838 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
1840 GNUNET_free(qnr->plugin);
1847 inbound_bw_tracker_update(void *cls)
1849 struct NeighbourMapEntry *n = cls;
1851 /* Quota was updated, tell plugins to update the time to receive next */
1852 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1853 &n->id, ¬ification_cb, n);
1858 * Create a fresh entry in the neighbour map for the given peer
1860 * @param peer peer to create an entry for
1861 * @return new neighbour map entry
1863 static struct NeighbourMapEntry *
1864 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1866 struct NeighbourMapEntry *n;
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "Creating new neighbour entry for `%s'\n",
1871 n = GNUNET_new (struct NeighbourMapEntry);
1873 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1874 n->last_util_transmission = GNUNET_TIME_absolute_get();
1875 n->util_payload_bytes_recv = 0;
1876 n->util_payload_bytes_sent = 0;
1877 n->util_total_bytes_recv = 0;
1878 n->util_total_bytes_sent = 0;
1879 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
1880 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1881 MAX_BANDWIDTH_CARRY_S);
1882 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1883 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
1884 GNUNET_assert (GNUNET_OK ==
1885 GNUNET_CONTAINER_multipeermap_put (neighbours,
1887 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1893 * Check if the two given addresses are the same.
1894 * Actually only checks if the sessions are non-NULL
1895 * (which they should be) and then if they are identical;
1896 * the actual addresses don't matter if the session
1897 * pointers match anyway, and we must have session pointers
1900 * @param a1 first address to compare
1901 * @param a2 other address to compare
1902 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1905 address_matches (const struct NeighbourAddress *a1,
1906 const struct NeighbourAddress *a2)
1908 if ( (NULL == a1->session) ||
1909 (NULL == a2->session) )
1914 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1919 address_suggest_cont (void *cls,
1920 const struct GNUNET_PeerIdentity *peer,
1921 const struct GNUNET_HELLO_Address *address, struct Session *session,
1922 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1923 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1924 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
1930 struct BlacklistCheckSwitchContext
1932 struct BlacklistCheckSwitchContext *prev;
1933 struct BlacklistCheckSwitchContext *next;
1936 struct GST_BlacklistCheck *blc;
1938 struct GNUNET_HELLO_Address *address;
1939 struct Session *session;
1940 struct GNUNET_ATS_Information *ats;
1943 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
1944 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
1949 try_connect_bl_check_cont (void *cls,
1950 const struct GNUNET_PeerIdentity *peer, int result)
1952 struct BlacklistCheckSwitchContext *blc_ctx = cls;
1953 struct NeighbourMapEntry *n;
1955 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
1956 GNUNET_free (blc_ctx);
1958 if (GNUNET_OK != result)
1960 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1961 _("Blacklisting dissaproved to connect to peer `%s'\n"),
1965 n = setup_neighbour (peer);
1966 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1968 GNUNET_ATS_reset_backoff (GST_ats, peer);
1969 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
1970 &address_suggest_cont, n);
1976 * Try to create a connection to the given target (eventually).
1978 * @param target peer to try to connect to
1981 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1983 struct NeighbourMapEntry *n;
1984 struct GST_BlacklistCheck *blc;
1985 struct BlacklistCheckSwitchContext *blc_ctx;
1987 if (NULL == neighbours)
1989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1990 "Asked to connect to peer `%s' during shutdown\n",
1991 GNUNET_i2s (target));
1992 return; /* during shutdown, do nothing */
1994 n = lookup_neighbour (target);
1995 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1996 "Asked to connect to peer `%s' (state: %s)\n",
1997 GNUNET_i2s (target),
1998 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2003 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2004 /* this should not be possible */
2006 free_neighbour (n, GNUNET_NO);
2008 case GNUNET_TRANSPORT_PS_INIT_ATS:
2009 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2010 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2011 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2012 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2013 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2014 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2015 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2016 "Ignoring request to try to connect to `%s', already trying!\n",
2017 GNUNET_i2s (target));
2018 return; /* already trying */
2019 case GNUNET_TRANSPORT_PS_CONNECTED:
2020 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2021 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2022 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2023 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2024 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2025 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2026 "Ignoring request to try to connect, already connected to `%s'!\n",
2027 GNUNET_i2s (target));
2028 return; /* already connected */
2029 case GNUNET_TRANSPORT_PS_DISCONNECT:
2030 /* get rid of remains, ready to re-try immediately */
2031 free_neighbour (n, GNUNET_NO);
2033 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2034 /* should not be possible */
2037 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2038 "Unhandled state `%s'\n",
2039 GNUNET_TRANSPORT_ps2s (n->state));
2041 free_neighbour (n, GNUNET_NO);
2046 /* Do blacklist check if connection is allowed */
2047 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2048 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2050 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2051 &try_connect_bl_check_cont, blc_ctx)))
2059 * Function called with the result of a blacklist check.
2061 * @param cls closure with the `struct BlackListCheckContext`
2062 * @param peer peer this check affects
2063 * @param result #GNUNET_OK if the address is allowed
2066 handle_test_blacklist_cont (void *cls,
2067 const struct GNUNET_PeerIdentity *peer,
2070 struct BlackListCheckContext *bcc = cls;
2071 struct NeighbourMapEntry *n;
2074 GNUNET_CONTAINER_DLL_remove (bc_head,
2077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2078 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2080 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2081 if (GNUNET_OK == result)
2083 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2087 /* Blacklist disagreed on connecting to a peer with this address
2088 * Destroy address because we are not allowed to use it
2090 if (NULL != bcc->na.session)
2091 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
2092 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
2094 if (NULL == (n = lookup_neighbour (peer)))
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2097 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2099 goto cleanup; /* nobody left to care about new address */
2101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2102 "Received blacklist result for peer `%s' in state %s/%d\n",
2104 GNUNET_TRANSPORT_ps2s (n->state),
2105 n->send_connect_ack);
2108 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2109 /* this should not be possible */
2111 free_neighbour (n, GNUNET_NO);
2113 case GNUNET_TRANSPORT_PS_INIT_ATS:
2114 /* waiting on ATS suggestion; still, pass address to ATS as a
2117 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2118 /* check if the address the blacklist was fine with matches
2119 ATS suggestion, if so, we can move on! */
2120 if ( (GNUNET_OK == result) &&
2121 (1 == n->send_connect_ack) )
2123 n->send_connect_ack = 2;
2124 send_session_connect_ack_message (bcc->na.address,
2126 n->connect_ack_timestamp);
2128 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2130 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2131 "Blacklist result for peer %s is for non-primary address, ignored\n",
2133 break; /* result for an address we currently don't care about */
2135 if (GNUNET_OK == result)
2137 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT, GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2138 send_session_connect (&n->primary_address);
2142 free_address (&n->primary_address);
2143 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2146 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2147 /* waiting on CONNECT_ACK, send ACK if one is pending */
2148 if ( (GNUNET_OK == result) &&
2149 (1 == n->send_connect_ack) )
2151 n->send_connect_ack = 2;
2152 send_session_connect_ack_message (n->primary_address.address,
2153 n->primary_address.session,
2154 n->connect_ack_timestamp);
2157 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2158 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2159 GNUNET_ATS_reset_backoff (GST_ats, peer);
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2161 "Suggesting address for peer %s to ATS\n",
2163 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer, &address_suggest_cont, n);
2165 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2166 /* waiting on ATS suggestion, don't care about blacklist */
2168 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2169 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 "Blacklist result ignored, as it is not for our primary address\n");
2173 break; /* result for an address we currently don't care about */
2175 if (GNUNET_OK == result)
2177 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK, GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2178 send_session_connect_ack_message (bcc->na.address,
2180 n->connect_ack_timestamp);
2181 if (1 == n->send_connect_ack)
2182 n->send_connect_ack = 2;
2186 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2188 plugin = GST_plugins_find (bcc->na.address->transport_name);
2189 if ( (NULL != plugin) &&
2190 (NULL != bcc->na.session) )
2192 plugin->disconnect_session (plugin->cls,
2196 GNUNET_break (NULL != plugin);
2197 free_address (&n->primary_address);
2198 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2199 GNUNET_ATS_reset_backoff (GST_ats, peer);
2202 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2203 /* waiting on SESSION_ACK, send ACK if one is pending */
2204 if ( (GNUNET_OK == result) &&
2205 (1 == n->send_connect_ack) )
2207 n->send_connect_ack = 2;
2208 send_session_connect_ack_message (n->primary_address.address,
2209 n->primary_address.session,
2210 n->connect_ack_timestamp);
2213 case GNUNET_TRANSPORT_PS_CONNECTED:
2214 /* already connected, don't care about blacklist */
2216 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2217 /* still waiting on ATS suggestion, don't care about blacklist */
2219 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2220 if ( (GNUNET_OK == result) &&
2221 (1 == n->send_connect_ack) )
2223 n->send_connect_ack = 2;
2224 send_session_connect_ack_message (bcc->na.address,
2226 n->connect_ack_timestamp);
2228 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 "Blacklist result ignored, as it is not for our primary address\n");
2232 break; /* result for an address we currently don't care about */
2234 if (GNUNET_OK == result)
2236 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2237 send_session_connect (&n->primary_address);
2241 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2244 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2245 /* waiting on CONNECT_ACK, don't care about blacklist */
2246 if ( (GNUNET_OK == result) &&
2247 (1 == n->send_connect_ack) )
2249 n->send_connect_ack = 2;
2250 send_session_connect_ack_message (n->primary_address.address,
2251 n->primary_address.session,
2252 n->connect_ack_timestamp);
2255 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2256 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2259 "Blacklist result ignored, as it is not for our primary address\n");
2260 break; /* result for an address we currently don't care about */
2262 if (GNUNET_OK == result)
2264 send_session_connect (&n->alternative_address);
2265 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2269 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2270 free_address (&n->alternative_address);
2273 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2274 /* waiting on CONNECT_ACK, don't care about blacklist */
2275 if ( (GNUNET_OK == result) &&
2276 (1 == n->send_connect_ack) )
2278 n->send_connect_ack = 2;
2279 send_session_connect_ack_message (n->primary_address.address,
2280 n->primary_address.session,
2281 n->connect_ack_timestamp);
2284 case GNUNET_TRANSPORT_PS_DISCONNECT:
2285 /* Nothing to do here, ATS will already do what can be done */
2287 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2288 /* should not be possible */
2292 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2293 "Unhandled state `%s'\n",
2294 GNUNET_TRANSPORT_ps2s (n->state));
2296 free_neighbour (n, GNUNET_NO);
2300 GNUNET_HELLO_address_free (bcc->na.address);
2306 * We want to know if connecting to a particular peer via
2307 * a particular address is allowed. Check it!
2309 * @param peer identity of the peer to switch the address for
2310 * @param ts time at which the check was initiated
2311 * @param address address of the other peer, NULL if other peer
2313 * @param session session to use (or NULL)
2316 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2317 struct GNUNET_TIME_Absolute ts,
2318 const struct GNUNET_HELLO_Address *address,
2319 struct Session *session)
2321 struct BlackListCheckContext *bcc;
2322 struct GST_BlacklistCheck *bc;
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "Checking peer `%s' against blacklist\n",
2327 bcc = GNUNET_new (struct BlackListCheckContext);
2328 bcc->na.address = GNUNET_HELLO_address_copy (address);
2329 bcc->na.session = session;
2330 bcc->na.connect_timestamp = ts;
2331 GNUNET_CONTAINER_DLL_insert (bc_head,
2334 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2335 address->transport_name,
2336 &handle_test_blacklist_cont, bcc)))
2338 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2339 we must only store 'bc' if 'bc' is non-NULL... */
2344 * We received a 'SESSION_CONNECT' message from the other peer.
2345 * Consider switching to it.
2347 * @param message possibly a 'struct SessionConnectMessage' (check format)
2348 * @param peer identity of the peer to switch the address for
2349 * @param address address of the other peer, NULL if other peer
2351 * @param session session to use (or NULL)
2352 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2355 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2356 const struct GNUNET_PeerIdentity *peer,
2357 const struct GNUNET_HELLO_Address *address,
2358 struct Session *session)
2360 const struct SessionConnectMessage *scm;
2361 struct NeighbourMapEntry *n;
2362 struct GNUNET_TIME_Absolute ts;
2364 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2365 "Received CONNECT message from peer `%s'\n",
2367 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2369 GNUNET_break_op (0);
2370 return GNUNET_SYSERR;
2372 GNUNET_STATISTICS_update (GST_stats,
2374 ("# CONNECT messages received"),
2376 if (NULL == neighbours)
2378 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2379 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2381 return GNUNET_OK; /* we're shutting down */
2383 scm = (const struct SessionConnectMessage *) message;
2384 GNUNET_break_op (0 == ntohl (scm->reserved));
2385 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2386 n = lookup_neighbour (peer);
2388 n = setup_neighbour (peer);
2389 n->send_connect_ack = 1;
2390 n->connect_ack_timestamp = ts;
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "Received SESSION_CONNECT for peer `%s' in state %s/%d\n",
2395 GNUNET_TRANSPORT_ps2s (n->state),
2396 n->send_connect_ack);
2399 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2400 /* Do a blacklist check for the new address */
2401 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2402 check_blacklist (peer, ts, address, session);
2404 case GNUNET_TRANSPORT_PS_INIT_ATS:
2405 /* CONNECT message takes priority over us asking ATS for address */
2406 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2408 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2409 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2410 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2411 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2412 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2413 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2414 /* It can never hurt to have an alternative address in the above cases,
2415 see if it is allowed */
2416 check_blacklist (peer, ts, address, session);
2418 case GNUNET_TRANSPORT_PS_CONNECTED:
2419 /* we are already connected and can thus send the ACK immediately;
2420 still, it can never hurt to have an alternative address, so also
2421 tell ATS about it */
2422 GNUNET_assert (NULL != n->primary_address.address);
2423 GNUNET_assert (NULL != n->primary_address.session);
2424 n->send_connect_ack = 0;
2425 send_session_connect_ack_message (n->primary_address.address,
2426 n->primary_address.session, ts);
2427 check_blacklist (peer, ts, address, session);
2429 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2430 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2431 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2432 /* It can never hurt to have an alternative address in the above cases,
2433 see if it is allowed */
2434 check_blacklist (peer, ts, address, session);
2436 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2437 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2438 /* we are already connected and can thus send the ACK immediately;
2439 still, it can never hurt to have an alternative address, so also
2440 tell ATS about it */
2441 GNUNET_assert (NULL != n->primary_address.address);
2442 GNUNET_assert (NULL != n->primary_address.session);
2443 n->send_connect_ack = 0;
2444 send_session_connect_ack_message (n->primary_address.address,
2445 n->primary_address.session, ts);
2446 check_blacklist (peer, ts, address, session);
2448 case GNUNET_TRANSPORT_PS_DISCONNECT:
2449 /* get rid of remains without terminating sessions, ready to re-try */
2450 free_neighbour (n, GNUNET_YES);
2451 n = setup_neighbour (peer);
2452 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2453 GNUNET_ATS_reset_backoff (GST_ats, peer);
2455 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2456 /* should not be possible */
2460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2461 "Unhandled state `%s'\n",
2462 GNUNET_TRANSPORT_ps2s (n->state));
2464 return GNUNET_SYSERR;
2470 switch_address_bl_check_cont (void *cls,
2471 const struct GNUNET_PeerIdentity *peer, int result)
2473 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2474 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2475 struct NeighbourMapEntry *n;
2477 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2478 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483 "Peer %s is unknown, suggestion ignored\n",
2486 if (result == GNUNET_NO)
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489 "Blacklist denied to switch to suggested address `%s' sesion %p for peer `%s'\n",
2490 GST_plugins_a2s (blc_ctx->address),
2492 GNUNET_i2s (&blc_ctx->address->peer));
2494 /* Delete address (or session if existing) in ATS */
2495 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2497 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2498 GNUNET_HELLO_address_free(blc_ctx->address);
2499 GNUNET_free_non_null (blc_ctx->ats);
2500 GNUNET_free (blc_ctx);
2504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2505 "Blacklist accepted to switch to suggested address `%s' for peer `%s'\n",
2506 GST_plugins_a2s (blc_ctx->address),
2508 GNUNET_i2s (&blc_ctx->address->peer));
2510 if (NULL == blc_ctx->session)
2512 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2514 "Obtained new session for peer `%s' and address '%s': %p\n",
2515 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2517 if (NULL == blc_ctx->session)
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520 "Failed to obtain new session for peer `%s' and address '%s'\n",
2521 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address));
2522 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2527 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2529 free_neighbour (n, GNUNET_NO);
2531 case GNUNET_TRANSPORT_PS_INIT_ATS:
2532 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2533 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2534 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_BLACKLIST,
2535 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2536 check_blacklist (&n->id, n->connect_ack_timestamp,
2537 blc_ctx->address, blc_ctx->session);
2539 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2540 /* ATS suggests a different address, switch again */
2541 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2542 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2543 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2544 check_blacklist (&n->id, n->connect_ack_timestamp,
2545 blc_ctx->address, blc_ctx->session);
2547 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2548 /* ATS suggests a different address, switch again */
2549 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2550 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2551 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_BLACKLIST,
2552 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2553 check_blacklist (&n->id, n->connect_ack_timestamp,
2554 blc_ctx->address, blc_ctx->session);
2556 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2557 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2558 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2559 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2560 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2561 check_blacklist (&n->id, n->connect_ack_timestamp,
2562 blc_ctx->address, blc_ctx->session);
2564 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2565 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2566 check_blacklist (&n->id, n->connect_ack_timestamp,
2567 blc_ctx->address, blc_ctx->session);
2569 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2570 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2571 /* ATS asks us to switch while we were trying to connect; switch to new
2572 address and check blacklist again */
2573 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2574 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2575 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2576 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2577 check_blacklist (&n->id, n->connect_ack_timestamp,
2578 blc_ctx->address, blc_ctx->session);
2580 case GNUNET_TRANSPORT_PS_CONNECTED:
2581 GNUNET_assert (NULL != n->primary_address.address);
2582 GNUNET_assert (NULL != n->primary_address.session);
2583 if (n->primary_address.session == blc_ctx->session)
2585 /* not an address change, just a quota change */
2586 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2587 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2590 /* ATS asks us to switch a life connection; see if we can get
2591 a CONNECT_ACK on it before we actually do this! */
2592 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2593 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2594 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2595 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2596 blc_ctx->address, blc_ctx->session);
2598 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2599 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2600 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2601 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2602 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_RECONNECT_BLACKLIST:
2607 /* ATS asks us to switch while we were trying to reconnect; switch to new
2608 address and check blacklist again */
2609 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2610 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2611 set_timeout (n, 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_RECONNECT_SENT:
2616 /* ATS asks us to switch while we were trying to reconnect; switch to new
2617 address and check blacklist again */
2618 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2619 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2620 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2621 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2622 check_blacklist (&n->id, n->connect_ack_timestamp,
2623 blc_ctx->address, blc_ctx->session);
2625 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2626 if (n->primary_address.session == blc_ctx->session)
2628 /* ATS switches back to still-active session */
2629 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2630 free_address (&n->alternative_address);
2633 /* ATS asks us to switch a life connection, update blacklist check */
2634 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2635 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2636 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2637 blc_ctx->address, blc_ctx->session);
2639 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2640 if (n->primary_address.session == blc_ctx->session)
2642 /* ATS switches back to still-active session */
2643 free_address (&n->alternative_address);
2644 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2647 /* ATS asks us to switch a life connection, update blacklist check */
2648 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2649 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2650 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2651 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2652 blc_ctx->address, blc_ctx->session);
2654 case GNUNET_TRANSPORT_PS_DISCONNECT:
2655 /* not going to switch addresses while disconnecting */
2657 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2661 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2662 "Unhandled state `%s'\n",
2663 GNUNET_TRANSPORT_ps2s (n->state));
2668 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2669 GNUNET_HELLO_address_free(blc_ctx->address);
2670 GNUNET_free_non_null (blc_ctx->ats);
2671 GNUNET_free (blc_ctx);
2677 * For the given peer, switch to this address.
2679 * Before accepting this addresses and actively using it, a blacklist check
2680 * is performed. If this blacklist check fails the address will be destroyed.
2682 * @param peer identity of the peer to switch the address for
2683 * @param address address of the other peer,
2684 * @param session session to use or NULL if transport should initiate a session
2685 * @param ats performance data
2686 * @param ats_count number of entries in ats
2687 * @param bandwidth_in inbound quota to be used when connection is up,
2688 * 0 to disconnect from peer
2689 * @param bandwidth_out outbound quota to be used when connection is up,
2690 * 0 to disconnect from peer
2693 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2694 const struct GNUNET_HELLO_Address *address,
2695 struct Session *session,
2696 const struct GNUNET_ATS_Information *ats,
2698 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2699 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2701 struct NeighbourMapEntry *n;
2702 struct GST_BlacklistCheck *blc;
2703 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2704 struct BlacklistCheckSwitchContext *blc_ctx;
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2708 "ATS has decided on an address for peer %s\n",
2710 GNUNET_assert (NULL != address->transport_name);
2711 if (NULL == (n = lookup_neighbour (peer)))
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2714 "Peer %s is unknown, suggestion ignored\n",
2719 /* Obtain an session for this address from plugin */
2720 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2722 /* we don't have the plugin for this address */
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2724 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2725 address->transport_name,
2727 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2730 if ((NULL == session) &&
2731 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2733 /* This is a inbound address and we do not have a session to use! */
2734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2735 "Inbound address without session `%s'! Destroying address...\n",
2736 GST_plugins_a2s (address));
2737 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2741 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2742 "ATS tells us to switch to %s address '%s' session %p for "
2743 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2744 GNUNET_HELLO_address_check_option (address,
2745 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "",
2746 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2747 GNUNET_TRANSPORT_ps2s (n->state), n->send_connect_ack,
2748 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2750 /* Perform blacklist check */
2751 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2752 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2753 blc_ctx->session = session;
2754 blc_ctx->bandwidth_in = bandwidth_in;
2755 blc_ctx->bandwidth_out = bandwidth_out;
2756 blc_ctx->ats_count = ats_count;
2757 blc_ctx->ats = NULL;
2760 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2761 for (c = 0; c < ats_count; c++)
2763 blc_ctx->ats[c].type = ats[c].type;
2764 blc_ctx->ats[c].value = ats[c].value;
2768 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2769 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2770 &switch_address_bl_check_cont, blc_ctx)))
2778 send_utilization_data (void *cls,
2779 const struct GNUNET_PeerIdentity *key,
2782 struct NeighbourMapEntry *n = value;
2783 struct GNUNET_ATS_Information atsi[4];
2785 uint32_t bps_pl_out;
2788 struct GNUNET_TIME_Relative delta;
2790 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2791 GNUNET_TIME_absolute_get ());
2795 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2796 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2798 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2799 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2801 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2806 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2807 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2809 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2810 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2814 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2818 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2819 atsi[0].value = htonl (bps_out);
2820 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2821 atsi[1].value = htonl (bps_in);
2823 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2824 atsi[2].value = htonl (bps_pl_out);
2825 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2826 atsi[3].value = htonl (bps_pl_in);
2828 GST_ats_update_metrics (key, n->primary_address.address,
2829 n->primary_address.session, atsi, 4);
2830 n->util_payload_bytes_recv = 0;
2831 n->util_payload_bytes_sent = 0;
2832 n->util_total_bytes_recv = 0;
2833 n->util_total_bytes_sent = 0;
2834 n->last_util_transmission = GNUNET_TIME_absolute_get();
2840 * Task transmitting utilization in a regular interval
2842 * @param cls the 'struct NeighbourMapEntry' for which we are running
2843 * @param tc scheduler context (unused)
2846 utilization_transmission (void *cls,
2847 const struct GNUNET_SCHEDULER_TaskContext *tc)
2849 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2851 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2852 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2854 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2855 utilization_transmission, NULL);
2861 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2862 const struct GNUNET_HELLO_Address *address,
2863 struct Session *session,
2864 const struct GNUNET_MessageHeader *message)
2866 struct NeighbourMapEntry *n;
2868 n = lookup_neighbour (peer);
2871 n->util_total_bytes_recv += ntohs(message->size);
2876 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2877 const struct GNUNET_HELLO_Address *address,
2878 struct Session *session,
2879 const struct GNUNET_MessageHeader *message)
2881 struct NeighbourMapEntry *n;
2882 n = lookup_neighbour (peer);
2885 n->util_payload_bytes_recv += ntohs(message->size);
2890 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2891 const struct GNUNET_HELLO_Address *address,
2892 struct Session *session,
2895 struct NeighbourMapEntry *n;
2896 n = lookup_neighbour (peer);
2899 if (n->primary_address.session != session)
2901 n->util_total_bytes_sent += size;
2906 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2909 struct NeighbourMapEntry *n;
2910 n = lookup_neighbour (peer);
2913 n->util_payload_bytes_sent += size;
2918 * Master task run for every neighbour. Performs all of the time-related
2919 * activities (keep alive, send next message, disconnect if idle, finish
2920 * clean up after disconnect).
2922 * @param cls the 'struct NeighbourMapEntry' for which we are running
2923 * @param tc scheduler context (unused)
2926 master_task (void *cls,
2927 const struct GNUNET_SCHEDULER_TaskContext *tc)
2929 struct NeighbourMapEntry *n = cls;
2930 struct GNUNET_TIME_Relative delay;
2932 n->task = GNUNET_SCHEDULER_NO_TASK;
2933 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2935 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2936 GNUNET_i2s (&n->id),
2937 GNUNET_TRANSPORT_ps2s(n->state),
2938 GNUNET_STRINGS_relative_time_to_string (delay,
2942 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2943 /* invalid state for master task, clean up */
2945 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2946 free_neighbour (n, GNUNET_NO);
2948 case GNUNET_TRANSPORT_PS_INIT_ATS:
2949 if (0 == delay.rel_value_us)
2951 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2952 "Connection to `%s' timed out waiting for ATS to provide address\n",
2953 GNUNET_i2s (&n->id));
2954 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2955 free_neighbour (n, GNUNET_NO);
2959 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2960 if (0 == delay.rel_value_us)
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2963 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2964 GNUNET_i2s (&n->id));
2965 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2966 free_neighbour (n, GNUNET_NO);
2970 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2971 if (0 == delay.rel_value_us)
2973 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2974 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2975 GNUNET_i2s (&n->id));
2976 /* We could not send to this address, delete address and session */
2977 if (NULL != n->primary_address.session)
2978 GNUNET_ATS_address_destroyed (GST_ats,
2979 n->primary_address.address, n->primary_address.session);
2980 GNUNET_ATS_address_destroyed (GST_ats,
2981 n->primary_address.address, NULL);
2982 disconnect_neighbour (n);
2986 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2987 if (0 == delay.rel_value_us)
2989 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2990 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2991 GNUNET_i2s (&n->id));
2992 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2993 free_neighbour (n, GNUNET_NO);
2997 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2998 if (0 == delay.rel_value_us)
3000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3001 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3002 GNUNET_i2s (&n->id));
3003 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
3004 free_neighbour (n, GNUNET_NO);
3008 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3009 if (0 == delay.rel_value_us)
3011 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3012 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
3013 GNUNET_i2s (&n->id));
3014 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
3015 free_neighbour (n, GNUNET_NO);
3019 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3020 if (0 == delay.rel_value_us)
3022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3023 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3024 GNUNET_i2s (&n->id));
3025 disconnect_neighbour (n);
3029 case GNUNET_TRANSPORT_PS_CONNECTED:
3030 if (0 == delay.rel_value_us)
3032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3033 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3034 GNUNET_i2s (&n->id));
3035 disconnect_neighbour (n);
3038 try_transmission_to_peer (n);
3041 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3042 if (0 == delay.rel_value_us)
3044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3045 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3046 GNUNET_i2s (&n->id));
3047 disconnect_neighbour (n);
3051 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3052 if (0 == delay.rel_value_us)
3054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3055 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
3056 GNUNET_i2s (&n->id));
3057 disconnect_neighbour (n);
3061 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3062 if (0 == delay.rel_value_us)
3064 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3065 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3066 GNUNET_i2s (&n->id));
3067 disconnect_neighbour (n);
3071 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3072 if (0 == delay.rel_value_us)
3074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3075 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3076 GNUNET_i2s (&n->id));
3077 disconnect_neighbour (n);
3080 try_transmission_to_peer (n);
3083 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3084 if (0 == delay.rel_value_us)
3086 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3087 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3088 GNUNET_i2s (&n->id));
3089 disconnect_neighbour (n);
3092 try_transmission_to_peer (n);
3095 case GNUNET_TRANSPORT_PS_DISCONNECT:
3096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3097 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3098 GNUNET_i2s (&n->id));
3099 free_neighbour (n, GNUNET_NO);
3101 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3102 /* how did we get here!? */
3106 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3107 "Unhandled state `%s'\n",
3108 GNUNET_TRANSPORT_ps2s (n->state));
3112 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3113 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3114 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3116 /* if we are *now* in one of these three states, we're sending
3117 keep alive messages, so we need to consider the keepalive
3118 delay, not just the connection timeout */
3119 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3122 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3123 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3130 * Send a SESSION_ACK message to the neighbour to confirm that we
3131 * got his CONNECT_ACK.
3133 * @param n neighbour to send the SESSION_ACK to
3136 send_session_ack_message (struct NeighbourMapEntry *n)
3138 struct GNUNET_MessageHeader msg;
3140 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3141 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3142 (void) send_with_session(n,
3143 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3144 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3150 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3151 * Consider switching to it.
3153 * @param message possibly a 'struct SessionConnectMessage' (check format)
3154 * @param peer identity of the peer to switch the address for
3155 * @param address address of the other peer, NULL if other peer
3157 * @param session session to use (or NULL)
3158 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3161 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3162 const struct GNUNET_PeerIdentity *peer,
3163 const struct GNUNET_HELLO_Address *address,
3164 struct Session *session)
3166 const struct SessionConnectMessage *scm;
3167 struct GNUNET_TIME_Absolute ts;
3168 struct NeighbourMapEntry *n;
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3171 "Received CONNECT_ACK message from peer `%s'\n",
3174 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3176 GNUNET_break_op (0);
3177 return GNUNET_SYSERR;
3179 GNUNET_STATISTICS_update (GST_stats,
3181 ("# CONNECT_ACK messages received"),
3183 scm = (const struct SessionConnectMessage *) message;
3184 GNUNET_break_op (ntohl (scm->reserved) == 0);
3185 if (NULL == (n = lookup_neighbour (peer)))
3187 GNUNET_STATISTICS_update (GST_stats,
3189 ("# unexpected CONNECT_ACK messages (no peer)"),
3191 return GNUNET_SYSERR;
3193 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3196 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3198 free_neighbour (n, GNUNET_NO);
3199 return GNUNET_SYSERR;
3200 case GNUNET_TRANSPORT_PS_INIT_ATS:
3201 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3202 GNUNET_STATISTICS_update (GST_stats,
3204 ("# unexpected CONNECT_ACK messages (not ready)"),
3207 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3208 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3210 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3211 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3214 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3215 GNUNET_STATISTICS_set (GST_stats,
3216 gettext_noop ("# peers connected"),
3217 ++neighbours_connected,
3219 connect_notify_cb (callback_cls, &n->id,
3220 n->primary_address.bandwidth_in,
3221 n->primary_address.bandwidth_out);
3222 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3223 GST_ats_add_address (n->primary_address.address,
3224 n->primary_address.session,
3226 set_primary_address (n,
3227 n->primary_address.address,
3228 n->primary_address.session,
3229 n->primary_address.bandwidth_in,
3230 n->primary_address.bandwidth_out,
3232 send_session_ack_message (n);
3234 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3235 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3236 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3237 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3238 GNUNET_STATISTICS_update (GST_stats,
3240 ("# unexpected CONNECT_ACK messages (not ready)"),
3243 case GNUNET_TRANSPORT_PS_CONNECTED:
3244 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3245 send_session_ack_message (n);
3247 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3248 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3249 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3250 to give us a new address... */
3251 GNUNET_STATISTICS_update (GST_stats,
3253 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3256 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3257 /* new address worked; go back to connected! */
3258 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3259 send_session_ack_message (n);
3261 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3262 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3263 send_session_ack_message (n);
3265 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3266 /* new address worked; adopt it and go back to connected! */
3267 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3268 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3270 GST_ats_add_address (n->alternative_address.address,
3271 n->alternative_address.session,
3273 set_primary_address (n, n->alternative_address.address,
3274 n->alternative_address.session, n->alternative_address.bandwidth_in,
3275 n->alternative_address.bandwidth_out, GNUNET_YES);
3277 free_address (&n->alternative_address);
3278 send_session_ack_message (n);
3280 case GNUNET_TRANSPORT_PS_DISCONNECT:
3281 GNUNET_STATISTICS_update (GST_stats,
3283 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3285 return GNUNET_SYSERR;
3286 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3290 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3291 "Unhandled state `%s'\n",
3292 GNUNET_TRANSPORT_ps2s (n->state));
3294 return GNUNET_SYSERR;
3301 * A session was terminated. Take note; if needed, try to get
3302 * an alternative address from ATS.
3304 * @param peer identity of the peer where the session died
3305 * @param session session that is gone
3306 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3307 * this session was not in use
3310 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3311 struct Session *session)
3313 struct NeighbourMapEntry *n;
3314 struct BlackListCheckContext *bcc;
3315 struct BlackListCheckContext *bcc_next;
3317 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3319 while (NULL != (bcc = bcc_next))
3321 bcc_next = bcc->next;
3322 if (bcc->na.session == session)
3324 if (NULL != bcc->bc)
3325 GST_blacklist_test_cancel (bcc->bc);
3326 GNUNET_HELLO_address_free (bcc->na.address);
3327 GNUNET_CONTAINER_DLL_remove (bc_head,
3333 if (NULL == (n = lookup_neighbour (peer)))
3334 return GNUNET_NO; /* can't affect us */
3335 if (session != n->primary_address.session)
3337 if (session == n->alternative_address.session)
3339 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3340 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3341 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3344 free_address (&n->alternative_address);
3346 return GNUNET_NO; /* doesn't affect us further */
3349 n->expect_latency_response = GNUNET_NO;
3352 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3354 free_neighbour (n, GNUNET_NO);
3356 case GNUNET_TRANSPORT_PS_INIT_ATS:
3358 free_neighbour (n, GNUNET_NO);
3360 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3361 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3362 free_address (&n->primary_address);
3363 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3365 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3366 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3367 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3368 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3369 /* error on inbound session; free neighbour entirely */
3370 free_address (&n->primary_address);
3371 free_neighbour (n, GNUNET_NO);
3373 case GNUNET_TRANSPORT_PS_CONNECTED:
3374 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3375 free_address (&n->primary_address);
3377 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3378 /* we don't have an address, how can it go down? */
3381 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3382 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3383 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3385 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3386 /* primary went down while we were checking secondary against
3387 blacklist, adopt secondary as primary */
3388 free_address (&n->primary_address);
3389 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3390 n->primary_address = n->alternative_address;
3391 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3393 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3394 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3395 secondary as primary */
3396 free_address (&n->primary_address);
3397 n->primary_address = n->alternative_address;
3398 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3399 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3401 case GNUNET_TRANSPORT_PS_DISCONNECT:
3402 free_address (&n->primary_address);
3404 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3405 /* neighbour was freed and plugins told to terminate session */
3409 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3410 "Unhandled state `%s'\n",
3411 GNUNET_TRANSPORT_ps2s (n->state));
3415 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3416 GNUNET_SCHEDULER_cancel (n->task);
3417 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3423 * We received a 'SESSION_ACK' message from the other peer.
3424 * If we sent a 'CONNECT_ACK' last, this means we are now
3425 * connected. Otherwise, do nothing.
3427 * @param message possibly a 'struct SessionConnectMessage' (check format)
3428 * @param peer identity of the peer to switch the address for
3429 * @param address address of the other peer, NULL if other peer
3431 * @param session session to use (or NULL)
3432 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3435 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3436 const struct GNUNET_PeerIdentity *peer,
3437 const struct GNUNET_HELLO_Address *address,
3438 struct Session *session)
3440 struct NeighbourMapEntry *n;
3442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3443 "Received SESSION_ACK message from peer `%s'\n",
3445 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3447 GNUNET_break_op (0);
3448 return GNUNET_SYSERR;
3450 GNUNET_STATISTICS_update (GST_stats,
3452 ("# SESSION_ACK messages received"),
3454 if (NULL == (n = lookup_neighbour (peer)))
3456 GNUNET_break_op (0);
3457 return GNUNET_SYSERR;
3459 /* check if we are in a plausible state for having sent
3460 a CONNECT_ACK. If not, return, otherwise break */
3461 if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3462 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3463 (2 != n->send_connect_ack) )
3465 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3466 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3468 GNUNET_TRANSPORT_ps2s (n->state),
3469 n->send_connect_ack);
3470 GNUNET_STATISTICS_update (GST_stats,
3471 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3475 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3476 GNUNET_STATISTICS_set (GST_stats,
3477 gettext_noop ("# peers connected"),
3478 ++neighbours_connected,
3480 connect_notify_cb (callback_cls, &n->id,
3481 n->primary_address.bandwidth_in,
3482 n->primary_address.bandwidth_out);
3484 GST_ats_add_address (n->primary_address.address,
3485 n->primary_address.session,
3487 set_primary_address (n,
3488 n->primary_address.address,
3489 n->primary_address.session,
3490 n->primary_address.bandwidth_in,
3491 n->primary_address.bandwidth_out,
3498 * Test if we're connected to the given peer.
3500 * @param target peer to test
3501 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3504 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3506 return test_connected (lookup_neighbour (target));
3510 * Change the incoming quota for the given peer.
3512 * @param neighbour identity of peer to change qutoa for
3513 * @param quota new quota
3516 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3517 struct GNUNET_BANDWIDTH_Value32NBO quota)
3519 struct NeighbourMapEntry *n;
3521 if (NULL == (n = lookup_neighbour (neighbour)))
3523 GNUNET_STATISTICS_update (GST_stats,
3525 ("# SET QUOTA messages ignored (no such peer)"),
3529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3530 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3531 ntohl (quota.value__), GNUNET_i2s (&n->id));
3532 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3533 if (0 != ntohl (quota.value__))
3535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3536 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3537 GNUNET_i2s (&n->id));
3538 if (GNUNET_YES == test_connected (n))
3539 GNUNET_STATISTICS_update (GST_stats,
3540 gettext_noop ("# disconnects due to quota of 0"),
3542 disconnect_neighbour (n);
3547 * We received a disconnect message from the given peer,
3548 * validate and process.
3550 * @param peer sender of the message
3551 * @param msg the disconnect message
3554 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3555 const struct GNUNET_MessageHeader *msg)
3557 struct NeighbourMapEntry *n;
3558 const struct SessionDisconnectMessage *sdm;
3560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3561 "Received DISCONNECT message from peer `%s'\n",
3563 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3565 // GNUNET_break_op (0);
3566 GNUNET_STATISTICS_update (GST_stats,
3568 ("# disconnect messages ignored (old format)"), 1,
3572 GNUNET_STATISTICS_update (GST_stats,
3574 ("# DISCONNECT messages received"),
3576 sdm = (const struct SessionDisconnectMessage *) msg;
3577 if (NULL == (n = lookup_neighbour (peer)))
3578 return; /* gone already */
3579 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3581 GNUNET_STATISTICS_update (GST_stats,
3583 ("# disconnect messages ignored (timestamp)"), 1,
3587 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3589 GNUNET_break_op (0);
3592 if (ntohl (sdm->purpose.size) !=
3593 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3594 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3595 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3598 "%s message from peer `%s' has invalid size \n",
3601 GNUNET_break_op (0);
3605 GNUNET_CRYPTO_eddsa_verify
3606 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3607 &sdm->signature, &sdm->public_key))
3609 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3610 "%s message from peer `%s' cannot be verified \n",
3613 GNUNET_break_op (0);
3616 if (GNUNET_YES == test_connected (n))
3617 GNUNET_STATISTICS_update (GST_stats,
3619 ("# other peer asked to disconnect from us"), 1,
3621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3622 "Disconnecting by request from peer %s\n",
3624 disconnect_neighbour (n);
3629 * Closure for the neighbours_iterate function.
3631 struct IteratorContext
3634 * Function to call on each connected neighbour.
3636 GST_NeighbourIterator cb;
3646 * Call the callback from the closure for each neighbour.
3648 * @param cls the `struct IteratorContext`
3649 * @param key the hash of the public key of the neighbour
3650 * @param value the `struct NeighbourMapEntry`
3651 * @return #GNUNET_OK (continue to iterate)
3654 neighbours_iterate (void *cls,
3655 const struct GNUNET_PeerIdentity *key,
3658 struct IteratorContext *ic = cls;
3659 struct NeighbourMapEntry *n = value;
3660 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3661 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3664 if (NULL != n->primary_address.address)
3666 bandwidth_in = n->primary_address.bandwidth_in;
3667 bandwidth_out = n->primary_address.bandwidth_out;
3671 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3672 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3676 n->primary_address.address,
3679 bandwidth_in, bandwidth_out);
3685 * Iterate over all connected neighbours.
3687 * @param cb function to call
3688 * @param cb_cls closure for cb
3691 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3693 struct IteratorContext ic;
3695 if (NULL == neighbours)
3696 return; /* can happen during shutdown */
3699 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3704 * If we have an active connection to the given target, it must be shutdown.
3706 * @param target peer to disconnect from
3709 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3711 struct NeighbourMapEntry *n;
3713 if (NULL == (n = lookup_neighbour (target)))
3714 return; /* not active */
3715 if (GNUNET_YES == test_connected (n))
3716 GNUNET_STATISTICS_update (GST_stats,
3718 ("# disconnected from peer upon explicit request"), 1,
3720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3721 "Forced disconnect from peer %s\n",
3722 GNUNET_i2s (target));
3723 disconnect_neighbour (n);
3728 * Obtain current latency information for the given neighbour.
3730 * @param peer to get the latency for
3731 * @return observed latency of the address, FOREVER if the
3732 * the connection is not up
3734 struct GNUNET_TIME_Relative
3735 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3737 struct NeighbourMapEntry *n;
3739 n = lookup_neighbour (peer);
3741 return GNUNET_TIME_UNIT_FOREVER_REL;
3744 case GNUNET_TRANSPORT_PS_CONNECTED:
3745 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3746 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3747 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3748 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3749 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3751 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3752 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3753 case GNUNET_TRANSPORT_PS_INIT_ATS:
3754 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3755 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3756 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3757 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3758 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3759 case GNUNET_TRANSPORT_PS_DISCONNECT:
3760 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3761 return GNUNET_TIME_UNIT_FOREVER_REL;
3763 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3764 "Unhandled state `%s'\n",
3765 GNUNET_TRANSPORT_ps2s (n->state));
3769 return GNUNET_TIME_UNIT_FOREVER_REL;
3774 * Obtain current address information for the given neighbour.
3777 * @return address currently used
3779 struct GNUNET_HELLO_Address *
3780 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3782 struct NeighbourMapEntry *n;
3784 n = lookup_neighbour (peer);
3787 return n->primary_address.address;
3792 * Initialize the neighbours subsystem.
3794 * @param cls closure for callbacks
3795 * @param connect_cb function to call if we connect to a peer
3796 * @param disconnect_cb function to call if we disconnect from a peer
3797 * @param peer_address_cb function to call if we change an active address
3799 * @param max_fds maximum number of fds to use
3802 GST_neighbours_start (void *cls,
3803 NotifyConnect connect_cb,
3804 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3805 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3806 unsigned int max_fds)
3809 connect_notify_cb = connect_cb;
3810 disconnect_notify_cb = disconnect_cb;
3811 neighbour_change_cb = peer_address_cb;
3812 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3813 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3814 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3815 utilization_transmission, NULL);
3820 * Disconnect from the given neighbour.
3823 * @param key hash of neighbour's public key (not used)
3824 * @param value the 'struct NeighbourMapEntry' of the neighbour
3825 * @return #GNUNET_OK (continue to iterate)
3828 disconnect_all_neighbours (void *cls,
3829 const struct GNUNET_PeerIdentity *key,
3832 struct NeighbourMapEntry *n = value;
3834 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3835 "Disconnecting peer `%4s', %s\n",
3836 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3837 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
3838 free_neighbour (n, GNUNET_NO);
3844 * Cleanup the neighbours subsystem.
3847 GST_neighbours_stop ()
3849 struct BlacklistCheckSwitchContext *cur;
3850 struct BlacklistCheckSwitchContext *next;
3852 if (NULL == neighbours)
3854 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3856 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3857 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3860 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3861 &disconnect_all_neighbours,
3863 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3865 next = pending_bc_head;
3866 for (cur = next; NULL != cur; cur = next )
3869 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3871 if (NULL != cur->blc)
3873 GST_blacklist_test_cancel (cur->blc);
3876 if (NULL != cur->address)
3877 GNUNET_HELLO_address_free (cur->address);
3878 GNUNET_free_non_null (cur->ats);
3882 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
3883 &free_notification_cb, NULL);
3884 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
3885 registered_quota_notifications = NULL;
3888 callback_cls = NULL;
3889 connect_notify_cb = NULL;
3890 disconnect_notify_cb = NULL;
3891 neighbour_change_cb = NULL;
3895 /* end of file gnunet-service-transport_neighbours.c */