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->bandwidth_in = GNUNET_BANDWIDTH_value_init (0);
582 na->bandwidth_out = GNUNET_BANDWIDTH_value_init (0);
583 na->ats_active = GNUNET_NO;
584 na->keep_alive_nonce = 0;
585 if (NULL != na->address)
587 GNUNET_HELLO_address_free (na->address);
595 * Set net state for this neighbour and notify monitoring
597 * @param n the respective neighbour
598 * @param s the new state
601 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
604 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
606 GNUNET_TRANSPORT_ps2s(s));
607 neighbour_change_cb (callback_cls,
609 n->primary_address.address,
610 n->state, n->timeout,
611 n->primary_address.bandwidth_in,
612 n->primary_address.bandwidth_out);
617 * Set net state and state timeout for this neighbour and notify monitoring
619 * @param n the respective neighbour
620 * @param s the new state
621 * @param timeout the new timeout
624 set_state_and_timeout (struct NeighbourMapEntry *n,
625 enum GNUNET_TRANSPORT_PeerState s,
626 struct GNUNET_TIME_Absolute timeout)
629 n->timeout = timeout;
630 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
632 GNUNET_TRANSPORT_ps2s(s),
633 GNUNET_STRINGS_absolute_time_to_string (timeout));
634 neighbour_change_cb (callback_cls,
636 n->primary_address.address,
637 n->state, n->timeout,
638 n->primary_address.bandwidth_in,
639 n->primary_address.bandwidth_out);
644 * Set new state timeout for this neighbour and notify monitoring
646 * @param n the respective neighbour
647 * @param timeout the new timeout
650 set_timeout (struct NeighbourMapEntry *n,
651 struct GNUNET_TIME_Absolute timeout)
653 n->timeout = timeout;
654 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed timeout %s\n",
656 GNUNET_STRINGS_absolute_time_to_string (timeout));
657 neighbour_change_cb (callback_cls,
659 n->primary_address.address,
660 n->state, n->timeout,
661 n->primary_address.bandwidth_in,
662 n->primary_address.bandwidth_out);
667 * Initialize the alternative address of a neighbour
669 * @param n the neighbour
670 * @param address address of the other peer, NULL if other peer
672 * @param session session to use (or NULL, in which case an
673 * address must be setup)
674 * @param bandwidth_in inbound quota to be used when connection is up
675 * @param bandwidth_out outbound quota to be used when connection is up
678 set_alternative_address (struct NeighbourMapEntry *n,
679 const struct GNUNET_HELLO_Address *address,
680 struct Session *session,
681 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
682 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
684 struct GNUNET_TRANSPORT_PluginFunctions *papi;
685 if (NULL == (papi = GST_plugins_find (address->transport_name)))
690 if (session == n->alternative_address.session)
692 n->alternative_address.bandwidth_in = bandwidth_in;
693 n->alternative_address.bandwidth_out = bandwidth_out;
696 free_address (&n->alternative_address);
698 session = papi->get_session (papi->cls, address);
701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
702 "Failed to obtain new session for peer `%s' and address '%s'\n",
703 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
704 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
708 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
710 GST_plugins_a2s(address));
712 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
713 n->alternative_address.bandwidth_in = bandwidth_in;
714 n->alternative_address.bandwidth_out = bandwidth_out;
715 n->alternative_address.session = session;
716 n->alternative_address.ats_active = GNUNET_NO;
717 n->alternative_address.keep_alive_nonce = 0;
722 * Initialize the primary address of a neighbour
724 * @param n the neighbour
725 * @param address address of the other peer, NULL if other peer
727 * @param session session to use (or NULL, in which case an
728 * address must be setup)
729 * @param bandwidth_in inbound quota to be used when connection is up
730 * @param bandwidth_out outbound quota to be used when connection is up
731 * @param is_active #GNUNET_YES to mark this as the active address with ATS
734 set_primary_address (struct NeighbourMapEntry *n,
735 const struct GNUNET_HELLO_Address *address,
736 struct Session *session,
737 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
738 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
741 struct GNUNET_TRANSPORT_PluginFunctions *papi;
743 if (NULL == (papi = GST_plugins_find (address->transport_name)))
748 if (session == n->primary_address.session)
750 n->primary_address.bandwidth_in = bandwidth_in;
751 n->primary_address.bandwidth_out = bandwidth_out;
752 if (is_active != n->primary_address.ats_active)
754 n->primary_address.ats_active = is_active;
755 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
756 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
758 if (GNUNET_YES == is_active)
760 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
761 send_outbound_quota (&address->peer, bandwidth_out);
765 free_address (&n->primary_address);
767 session = papi->get_session (papi->cls, address);
770 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
771 "Failed to obtain new session for peer `%s' and address '%s'\n",
772 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
773 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
777 n->primary_address.address = GNUNET_HELLO_address_copy (address);
778 n->primary_address.bandwidth_in = bandwidth_in;
779 n->primary_address.bandwidth_out = bandwidth_out;
780 n->primary_address.session = session;
781 n->primary_address.ats_active = is_active;
782 n->primary_address.keep_alive_nonce = 0;
783 if (GNUNET_YES == is_active)
785 /* Telling ATS about new session */
786 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
787 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
788 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
789 send_outbound_quota (&address->peer, bandwidth_out);
792 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
794 GST_plugins_a2s(address));
796 neighbour_change_cb (callback_cls,
798 n->primary_address.address,
799 n->state, n->timeout,
800 n->primary_address.bandwidth_in,
801 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)
813 /* Unset primary address */
814 free_address (&n->primary_address);
816 /* Notify monitoring about it */
817 neighbour_change_cb (callback_cls,
820 n->state, n->timeout,
821 n->primary_address.bandwidth_in,
822 n->primary_address.bandwidth_out);
828 * Free a neighbour map entry.
830 * @param n entry to free
831 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
832 * #GNUNET_YES to keep all sessions
835 free_neighbour (struct NeighbourMapEntry *n,
838 struct MessageQueue *mq;
839 struct GNUNET_TRANSPORT_PluginFunctions *papi;
840 struct GNUNET_HELLO_Address *backup_primary;
842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
843 "Freeing neighbour state of peer `%s'\n",
844 GNUNET_i2s (&n->id));
845 n->is_active = NULL; /* always free'd by its own continuation! */
847 /* fail messages currently in the queue */
848 while (NULL != (mq = n->messages_head))
850 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
851 if (NULL != mq->cont)
852 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
855 /* It is too late to send other peer disconnect notifications, but at
856 least internally we need to get clean... */
857 if (GNUNET_YES == test_connected (n))
859 GNUNET_STATISTICS_set (GST_stats,
860 gettext_noop ("# peers connected"),
861 --neighbours_connected,
863 disconnect_notify_cb (callback_cls, &n->id);
866 /* Mark peer as disconnected */
867 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
869 if (NULL != n->primary_address.address)
870 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
872 backup_primary = NULL;
874 /* free addresses and mark as unused */
875 unset_primary_address (n);
876 free_address (&n->alternative_address);
878 /* cut all transport-level connection for this peer */
879 if ((GNUNET_NO == keep_sessions) &&
880 (NULL != backup_primary) &&
881 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
882 papi->disconnect_peer (papi->cls, &n->id);
884 if (NULL != backup_primary)
885 GNUNET_HELLO_address_free (backup_primary);
886 GNUNET_assert (GNUNET_YES ==
887 GNUNET_CONTAINER_multipeermap_remove (neighbours,
890 /* Cancel address requests for this peer */
891 if (NULL != n->suggest_handle)
893 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
894 n->suggest_handle = NULL;
897 /* Cancel the master task */
898 if (GNUNET_SCHEDULER_NO_TASK != n->task)
900 GNUNET_SCHEDULER_cancel (n->task);
901 n->task = GNUNET_SCHEDULER_NO_TASK;
903 /* free rest of memory */
909 * Transmit a message using the current session of the given
912 * @param n entry for the recipient
913 * @param msgbuf buffer to transmit
914 * @param msgbuf_size number of bytes in @a msgbuf buffer
915 * @param priority transmission priority
916 * @param timeout transmission timeout
917 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
918 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
919 * @param cont continuation to call when finished (can be NULL)
920 * @param cont_cls closure for @a cont
921 * @return timeout (copy of @a timeout or a calculated one if
922 * @a use_keepalive_timeout is #GNUNET_YES.
924 static struct GNUNET_TIME_Relative
925 send_with_session (struct NeighbourMapEntry *n,
926 const char *msgbuf, size_t msgbuf_size,
928 struct GNUNET_TIME_Relative timeout,
929 unsigned int use_keepalive_timeout,
930 GNUNET_TRANSPORT_TransmitContinuation cont,
933 struct GNUNET_TRANSPORT_PluginFunctions *papi;
934 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
936 GNUNET_assert (n->primary_address.session != NULL);
937 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
938 (-1 == papi->send (papi->cls,
939 n->primary_address.session,
942 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
943 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
944 papi->query_keepalive_factor (papi->cls))),
945 cont, cont_cls)))) &&
947 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
948 GST_neighbours_notify_data_sent (&n->id,
949 n->primary_address.address, n->primary_address.session, msgbuf_size);
950 GNUNET_break (NULL != papi);
956 * Master task run for every neighbour. Performs all of the time-related
957 * activities (keep alive, send next message, disconnect if idle, finish
958 * clean up after disconnect).
960 * @param cls the `struct NeighbourMapEntry` for which we are running
961 * @param tc scheduler context (unused)
964 master_task (void *cls,
965 const struct GNUNET_SCHEDULER_TaskContext *tc);
969 * Function called when the 'DISCONNECT' message has been sent by the
970 * plugin. Frees the neighbour --- if the entry still exists.
973 * @param target identity of the neighbour that was disconnected
974 * @param result #GNUNET_OK if the disconnect got out successfully
975 * @param payload bytes payload
976 * @param physical bytes physical
979 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
980 int result, size_t payload, size_t physical)
982 struct NeighbourMapEntry *n;
984 n = lookup_neighbour (target);
986 return; /* already gone */
987 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
988 return; /* have created a fresh entry since */
989 if (GNUNET_SCHEDULER_NO_TASK != n->task)
990 GNUNET_SCHEDULER_cancel (n->task);
991 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
996 * Transmit a DISCONNECT message to the other peer.
998 * @param n neighbour to send DISCONNECT message.
1001 send_disconnect (struct NeighbourMapEntry *n)
1003 struct SessionDisconnectMessage disconnect_msg;
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Sending DISCONNECT message to peer `%4s'\n",
1007 GNUNET_i2s (&n->id));
1008 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1009 disconnect_msg.header.type =
1010 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1011 disconnect_msg.reserved = htonl (0);
1012 disconnect_msg.purpose.size =
1013 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1014 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1015 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1016 disconnect_msg.purpose.purpose =
1017 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1018 disconnect_msg.timestamp =
1019 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1020 disconnect_msg.public_key = GST_my_identity.public_key;
1021 GNUNET_assert (GNUNET_OK ==
1022 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1023 &disconnect_msg.purpose,
1024 &disconnect_msg.signature));
1026 (void) send_with_session (n,
1027 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1028 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1029 GNUNET_NO, &send_disconnect_cont, NULL);
1030 GNUNET_STATISTICS_update (GST_stats,
1032 ("# DISCONNECT messages sent"), 1,
1038 * Disconnect from the given neighbour, clean up the record.
1040 * @param n neighbour to disconnect from
1043 disconnect_neighbour (struct NeighbourMapEntry *n)
1045 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1046 "Disconnecting from peer %s in state %s\n",
1047 GNUNET_i2s (&n->id),
1048 GNUNET_TRANSPORT_ps2s (n->state));
1049 /* depending on state, notify neighbour and/or upper layers of this peer
1053 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1054 case GNUNET_TRANSPORT_PS_INIT_ATS:
1055 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1056 /* other peer is completely unaware of us, no need to send DISCONNECT */
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);
1596 send_session_connect_cont (void *cls,
1597 const struct GNUNET_PeerIdentity *target,
1599 size_t size_payload,
1600 size_t size_on_wire)
1602 struct NeighbourMapEntry *n;
1604 n = lookup_neighbour (target);
1607 /* CONNECT continuation was called after neighbor was freed,
1608 * for example due to a time out for the state or the session
1609 * used was already terminated: nothing to do here... */
1613 if (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state)
1615 /* CONNECT continuation was called after neighbor changed state,
1616 * for example due to a time out for the state or the session
1617 * used was already terminated: nothing to do here... */
1620 if (GNUNET_OK == result)
1623 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1624 _("Failed to send CONNECT message to peer `%s'\n"),
1625 (GNUNET_OK == result) ? "OK" : "ERROR");
1627 /* Failed to send CONNECT message with this address */
1628 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1629 n->primary_address.session);
1630 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address,
1633 /* Remove address and request and additional one */
1634 unset_primary_address (n);
1636 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1637 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1643 * Send a SESSION_CONNECT message via the given address.
1645 * @param na address to use
1648 send_session_connect (struct NeighbourAddress *na)
1650 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1651 struct SessionConnectMessage connect_msg;
1652 struct NeighbourMapEntry *n;
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1655 "Sending SESSION_CONNECT message to peer %s\n",
1656 GNUNET_i2s (&na->address->peer));
1657 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1662 if (NULL == na->session)
1663 na->session = papi->get_session (papi->cls, na->address);
1664 if (NULL == na->session)
1669 GNUNET_STATISTICS_update (GST_stats,
1671 ("# SESSION_CONNECT messages sent"),
1673 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1674 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1675 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1676 connect_msg.reserved = htonl (0);
1677 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1679 papi->send (papi->cls,
1681 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1683 SETUP_CONNECTION_TIMEOUT,
1684 send_session_connect_cont, NULL))
1686 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1687 _("Failed to transmit CONNECT message via plugin to %s\n"),
1688 GST_plugins_a2s (na->address));
1690 n = lookup_neighbour (&na->address->peer);
1696 /* Hard failure to send the CONNECT message with this address:
1697 Destroy address and session */
1698 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
1699 GNUNET_ATS_address_destroyed (GST_ats, na->address, NULL);
1701 /* Remove address and request and additional one */
1702 unset_primary_address (n);
1704 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
1705 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1709 GST_neighbours_notify_data_sent (&na->address->peer,
1712 sizeof (struct SessionConnectMessage));
1718 * Send a SESSION_CONNECT_ACK message via the given address.
1720 * @param address address to use
1721 * @param session session to use
1722 * @param timestamp timestamp to use for the ACK message
1725 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1726 struct Session *session,
1727 struct GNUNET_TIME_Absolute timestamp)
1729 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1730 struct SessionConnectMessage connect_msg;
1732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1733 "Sending CONNECT_ACK to peer `%s'\n",
1734 GNUNET_i2s (&address->peer));
1735 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1740 if (NULL == session)
1741 session = papi->get_session (papi->cls, address);
1742 if (NULL == session)
1747 GNUNET_STATISTICS_update (GST_stats,
1749 ("# CONNECT_ACK messages sent"),
1751 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1752 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1753 connect_msg.reserved = htonl (0);
1754 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1755 (void) papi->send (papi->cls,
1757 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1759 GNUNET_TIME_UNIT_FOREVER_REL,
1764 struct QuotaNotificationRequest
1766 struct GNUNET_PeerIdentity peer;
1767 struct Session *session;
1771 struct QNR_LookContext
1773 struct GNUNET_PeerIdentity peer;
1774 struct Session *session;
1777 struct QuotaNotificationRequest *res;
1781 find_notification_request (void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1783 struct QNR_LookContext *qnr_ctx = cls;
1784 struct QuotaNotificationRequest *qnr = value;
1786 if ((qnr->session == qnr_ctx->session) &&
1787 (0 == memcmp (&qnr->peer, &qnr_ctx->peer, sizeof (struct GNUNET_PeerIdentity))) &&
1788 (0 == strcmp(qnr_ctx->plugin, qnr->plugin)))
1790 qnr_ctx->res = value;
1797 GST_neighbours_register_quota_notification(void *cls,
1798 const struct GNUNET_PeerIdentity *peer, const char *plugin,
1799 struct Session *session)
1801 struct QuotaNotificationRequest *qnr;
1802 struct QNR_LookContext qnr_ctx;
1804 if (NULL == registered_quota_notifications)
1806 return; /* init or shutdown */
1809 qnr_ctx.peer = (*peer);
1810 qnr_ctx.plugin = plugin;
1811 qnr_ctx.session = session;
1814 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1815 peer, &find_notification_request, &qnr_ctx);
1816 if (NULL != qnr_ctx.res)
1822 qnr = GNUNET_new (struct QuotaNotificationRequest);
1823 qnr->peer = (*peer);
1824 qnr->plugin = GNUNET_strdup (plugin);
1825 qnr->session = session;
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 "Adding notification for peer `%s' plugin `%s' session %p \n",
1829 GNUNET_i2s (peer), plugin, session);
1831 GNUNET_CONTAINER_multipeermap_put (registered_quota_notifications, peer,
1832 qnr, GNUNET_CONTAINER_MULTIHASHMAPOPTION_MULTIPLE);
1837 GST_neighbours_unregister_quota_notification(void *cls,
1838 const struct GNUNET_PeerIdentity *peer, const char *plugin, struct Session *session)
1840 struct QNR_LookContext qnr_ctx;
1842 if (NULL == registered_quota_notifications)
1844 return; /* init or shutdown */
1847 qnr_ctx.peer = (*peer);
1848 qnr_ctx.plugin = plugin;
1849 qnr_ctx.session = session;
1852 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
1853 &find_notification_request, &qnr_ctx);
1854 if (NULL == qnr_ctx.res)
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Removing notification for peer `%s' plugin `%s' session %p \n",
1862 GNUNET_i2s (peer), plugin, session);
1864 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, peer,
1866 GNUNET_free (qnr_ctx.res->plugin);
1867 GNUNET_free (qnr_ctx.res);
1871 notification_cb(void *cls, const struct GNUNET_PeerIdentity *key, void *value)
1873 /* struct NeighbourMapEntry *n = cls; */
1874 struct QuotaNotificationRequest *qnr = value;
1875 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1876 struct GNUNET_TIME_Relative delay;
1879 papi = GST_plugins_find(qnr->plugin);
1886 delay = GST_neighbours_calculate_receive_delay (key, 0, &do_forward);
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 "New inbound delay for peer `%s' is %llu ms\n", GNUNET_i2s (key),
1889 delay.rel_value_us / 1000);
1891 if (NULL != papi->update_inbound_delay)
1892 papi->update_inbound_delay (papi->cls, key, qnr->session, delay);
1898 free_notification_cb(void *cls, const struct GNUNET_PeerIdentity *key,
1901 /* struct NeighbourMapEntry *n = cls; */
1902 struct QuotaNotificationRequest *qnr = value;
1904 GNUNET_CONTAINER_multipeermap_remove (registered_quota_notifications, key,
1906 GNUNET_free(qnr->plugin);
1913 inbound_bw_tracker_update(void *cls)
1915 struct NeighbourMapEntry *n = cls;
1917 /* Quota was updated, tell plugins to update the time to receive next */
1918 GNUNET_CONTAINER_multipeermap_get_multiple (registered_quota_notifications,
1919 &n->id, ¬ification_cb, n);
1924 * Create a fresh entry in the neighbour map for the given peer
1926 * @param peer peer to create an entry for
1927 * @return new neighbour map entry
1929 static struct NeighbourMapEntry *
1930 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1932 struct NeighbourMapEntry *n;
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1935 "Creating new neighbour entry for `%s'\n",
1937 n = GNUNET_new (struct NeighbourMapEntry);
1939 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1940 n->last_util_transmission = GNUNET_TIME_absolute_get();
1941 n->util_payload_bytes_recv = 0;
1942 n->util_payload_bytes_sent = 0;
1943 n->util_total_bytes_recv = 0;
1944 n->util_total_bytes_sent = 0;
1945 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker, &inbound_bw_tracker_update, n,
1946 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1947 MAX_BANDWIDTH_CARRY_S);
1948 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1949 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
1950 GNUNET_assert (GNUNET_OK ==
1951 GNUNET_CONTAINER_multipeermap_put (neighbours,
1953 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1959 * Check if the two given addresses are the same.
1960 * Actually only checks if the sessions are non-NULL
1961 * (which they should be) and then if they are identical;
1962 * the actual addresses don't matter if the session
1963 * pointers match anyway, and we must have session pointers
1966 * @param a1 first address to compare
1967 * @param a2 other address to compare
1968 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1971 address_matches (const struct NeighbourAddress *a1,
1972 const struct NeighbourAddress *a2)
1974 if ( (NULL == a1->session) ||
1975 (NULL == a2->session) )
1980 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1984 /* We received a address suggestion after requesting an address in
1985 * try_connect or after receiving a connect, switch to address
1988 address_suggest_cont (void *cls,
1989 const struct GNUNET_PeerIdentity *peer,
1990 const struct GNUNET_HELLO_Address *address, struct Session *session,
1991 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1992 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1993 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
1995 GST_neighbours_switch_to_address(peer, address, session, ats, ats_count,
1996 bandwidth_in, bandwidth_out);
2000 struct BlacklistCheckSwitchContext
2002 struct BlacklistCheckSwitchContext *prev;
2003 struct BlacklistCheckSwitchContext *next;
2006 struct GST_BlacklistCheck *blc;
2008 struct GNUNET_HELLO_Address *address;
2009 struct Session *session;
2010 struct GNUNET_ATS_Information *ats;
2013 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2014 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2018 * Black list check result for try_connect call
2019 * If connection to the peer is allowed request adddress and
2021 * @param cls blc_ctx bl context
2022 * @param peer the peer
2023 * @param result the result
2026 try_connect_bl_check_cont (void *cls,
2027 const struct GNUNET_PeerIdentity *peer, int result)
2029 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2030 struct NeighbourMapEntry *n;
2032 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2033 GNUNET_free (blc_ctx);
2035 if (GNUNET_OK != result)
2037 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2038 _("Blacklisting disapproved to connect to peer `%s'\n"),
2043 /* Setup a new neighbour */
2044 n = setup_neighbour (peer);
2046 /* Request address suggestions for this peer */
2047 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2048 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2049 GNUNET_ATS_reset_backoff (GST_ats, peer);
2050 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2051 &address_suggest_cont, n);
2057 * Try to create a connection to the given target (eventually).
2059 * @param target peer to try to connect to
2062 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
2064 struct NeighbourMapEntry *n;
2065 struct GST_BlacklistCheck *blc;
2066 struct BlacklistCheckSwitchContext *blc_ctx;
2068 if (NULL == neighbours)
2070 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2071 "Asked to connect to peer `%s' during shutdown\n",
2072 GNUNET_i2s (target));
2073 return; /* during shutdown, do nothing */
2075 n = lookup_neighbour (target);
2076 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2077 "Asked to connect to peer `%s' (state: %s)\n",
2078 GNUNET_i2s (target),
2079 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
2084 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2085 /* this should not be possible */
2087 free_neighbour (n, GNUNET_NO);
2089 case GNUNET_TRANSPORT_PS_INIT_ATS:
2090 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2091 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2092 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2093 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2094 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2095 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2096 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2097 "Ignoring request to try to connect to `%s', already trying!\n",
2098 GNUNET_i2s (target));
2099 return; /* already trying */
2100 case GNUNET_TRANSPORT_PS_CONNECTED:
2101 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2102 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2103 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2104 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2105 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2106 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2107 "Ignoring request to try to connect, already connected to `%s'!\n",
2108 GNUNET_i2s (target));
2109 return; /* already connected */
2110 case GNUNET_TRANSPORT_PS_DISCONNECT:
2111 /* get rid of remains, ready to re-try immediately */
2112 free_neighbour (n, GNUNET_NO);
2114 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2115 /* should not be possible */
2118 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2119 "Unhandled state `%s'\n",
2120 GNUNET_TRANSPORT_ps2s (n->state));
2122 free_neighbour (n, GNUNET_NO);
2127 /* Do blacklist check if connecting to this peer is allowed */
2128 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2129 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2131 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
2132 &try_connect_bl_check_cont, blc_ctx)))
2140 * Function called with the result of a blacklist check.
2142 * @param cls closure with the `struct BlackListCheckContext`
2143 * @param peer peer this check affects
2144 * @param result #GNUNET_OK if the address is allowed
2147 handle_test_blacklist_cont (void *cls,
2148 const struct GNUNET_PeerIdentity *peer,
2151 struct BlackListCheckContext *bcc = cls;
2152 struct NeighbourMapEntry *n;
2155 GNUNET_CONTAINER_DLL_remove (bc_head,
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
2161 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
2162 if (GNUNET_OK == result)
2164 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
2168 /* Blacklist disagreed on connecting to a peer with this address
2169 * Destroy address because we are not allowed to use it
2171 if (NULL != bcc->na.session)
2172 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
2173 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
2175 if (NULL == (n = lookup_neighbour (peer)))
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2178 "No neighbor entry for peer `%s', ignoring blacklist result\n",
2180 goto cleanup; /* nobody left to care about new address */
2182 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2183 "Received blacklist result for peer `%s' in state %s/%d\n",
2185 GNUNET_TRANSPORT_ps2s (n->state),
2186 n->send_connect_ack);
2189 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2190 /* this should not be possible */
2192 free_neighbour (n, GNUNET_NO);
2194 case GNUNET_TRANSPORT_PS_INIT_ATS:
2195 /* waiting on ATS suggestion; still, pass address to ATS as a
2198 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2199 /* check if the address the blacklist was fine with matches
2200 ATS suggestion, if so, we can move on! */
2201 if ( (GNUNET_OK == result) &&
2202 (1 == n->send_connect_ack) )
2204 n->send_connect_ack = 2;
2205 send_session_connect_ack_message (bcc->na.address,
2207 n->connect_ack_timestamp);
2209 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2211 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2212 "Blacklist result for peer %s is for non-primary address, ignored\n",
2214 break; /* result for an address we currently don't care about */
2216 if (GNUNET_OK == result)
2218 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2219 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2220 send_session_connect (&n->primary_address);
2224 free_address (&n->primary_address);
2226 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2227 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2230 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2231 /* waiting on CONNECT_ACK, send ACK if one is pending */
2232 if ( (GNUNET_OK == result) &&
2233 (1 == n->send_connect_ack) )
2235 n->send_connect_ack = 2;
2236 send_session_connect_ack_message (n->primary_address.address,
2237 n->primary_address.session,
2238 n->connect_ack_timestamp);
2241 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2242 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS,
2243 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2244 GNUNET_ATS_reset_backoff (GST_ats, peer);
2245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2246 "Suggesting address for peer %s to ATS\n",
2248 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
2249 &address_suggest_cont, n);
2251 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2252 /* waiting on ATS suggestion, don't care about blacklist */
2254 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2255 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2258 "Blacklist result ignored, as it is not for our primary address\n");
2259 break; /* result for an address we currently don't care about */
2261 if (GNUNET_OK == result)
2263 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK,
2264 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2265 send_session_connect_ack_message (bcc->na.address,
2267 n->connect_ack_timestamp);
2268 if (1 == n->send_connect_ack)
2269 n->send_connect_ack = 2;
2273 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2275 plugin = GST_plugins_find (bcc->na.address->transport_name);
2276 if ( (NULL != plugin) &&
2277 (NULL != bcc->na.session) )
2279 plugin->disconnect_session (plugin->cls,
2283 GNUNET_break (NULL != plugin);
2284 free_address (&n->primary_address);
2285 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
2286 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2287 GNUNET_ATS_reset_backoff (GST_ats, peer);
2290 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2291 /* waiting on SESSION_ACK, send ACK if one is pending */
2292 if ( (GNUNET_OK == result) &&
2293 (1 == n->send_connect_ack) )
2295 n->send_connect_ack = 2;
2296 send_session_connect_ack_message (n->primary_address.address,
2297 n->primary_address.session,
2298 n->connect_ack_timestamp);
2301 case GNUNET_TRANSPORT_PS_CONNECTED:
2302 /* already connected, don't care about blacklist */
2304 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2305 /* still waiting on ATS suggestion, don't care about blacklist */
2307 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2308 if ( (GNUNET_OK == result) &&
2309 (1 == n->send_connect_ack) )
2311 n->send_connect_ack = 2;
2312 send_session_connect_ack_message (bcc->na.address,
2314 n->connect_ack_timestamp);
2316 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319 "Blacklist result ignored, as it is not for our primary address\n");
2320 break; /* result for an address we currently don't care about */
2322 if (GNUNET_OK == result)
2324 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2325 send_session_connect (&n->primary_address);
2329 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2332 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2333 /* waiting on CONNECT_ACK, don't care about blacklist */
2334 if ( (GNUNET_OK == result) &&
2335 (1 == n->send_connect_ack) )
2337 n->send_connect_ack = 2;
2338 send_session_connect_ack_message (n->primary_address.address,
2339 n->primary_address.session,
2340 n->connect_ack_timestamp);
2343 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2344 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2346 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2347 "Blacklist result ignored, as it is not for our primary address\n");
2348 break; /* result for an address we currently don't care about */
2350 if (GNUNET_OK == result)
2352 send_session_connect (&n->alternative_address);
2353 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2357 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2358 free_address (&n->alternative_address);
2361 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2362 /* waiting on CONNECT_ACK, don't care about blacklist */
2363 if ( (GNUNET_OK == result) &&
2364 (1 == n->send_connect_ack) )
2366 n->send_connect_ack = 2;
2367 send_session_connect_ack_message (n->primary_address.address,
2368 n->primary_address.session,
2369 n->connect_ack_timestamp);
2372 case GNUNET_TRANSPORT_PS_DISCONNECT:
2373 /* Nothing to do here, ATS will already do what can be done */
2375 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2376 /* should not be possible */
2380 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2381 "Unhandled state `%s'\n",
2382 GNUNET_TRANSPORT_ps2s (n->state));
2384 free_neighbour (n, GNUNET_NO);
2388 GNUNET_HELLO_address_free (bcc->na.address);
2394 * We want to know if connecting to a particular peer via
2395 * a particular address is allowed. Check it!
2397 * @param peer identity of the peer to switch the address for
2398 * @param ts time at which the check was initiated
2399 * @param address address of the other peer, NULL if other peer
2401 * @param session session to use (or NULL)
2404 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2405 struct GNUNET_TIME_Absolute ts,
2406 const struct GNUNET_HELLO_Address *address,
2407 struct Session *session)
2409 struct BlackListCheckContext *bcc;
2410 struct GST_BlacklistCheck *bc;
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 "Checking peer `%s' against blacklist\n",
2415 bcc = GNUNET_new (struct BlackListCheckContext);
2416 bcc->na.address = GNUNET_HELLO_address_copy (address);
2417 bcc->na.session = session;
2418 bcc->na.connect_timestamp = ts;
2419 GNUNET_CONTAINER_DLL_insert (bc_head,
2422 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2423 address->transport_name,
2424 &handle_test_blacklist_cont, bcc)))
2426 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2427 we must only store 'bc' if 'bc' is non-NULL... */
2432 * We received a 'SESSION_CONNECT' message from the other peer.
2433 * Consider switching to it.
2435 * @param message possibly a 'struct SessionConnectMessage' (check format)
2436 * @param peer identity of the peer to switch the address for
2437 * @param address address of the other peer, NULL if other peer
2439 * @param session session to use (or NULL)
2440 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2443 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2444 const struct GNUNET_PeerIdentity *peer,
2445 const struct GNUNET_HELLO_Address *address,
2446 struct Session *session)
2448 const struct SessionConnectMessage *scm;
2449 struct NeighbourMapEntry *n;
2450 struct GNUNET_TIME_Absolute ts;
2452 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2453 "Received CONNECT message from peer `%s'\n",
2455 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2457 GNUNET_break_op (0);
2458 return GNUNET_SYSERR;
2460 GNUNET_STATISTICS_update (GST_stats,
2462 ("# CONNECT messages received"),
2464 if (NULL == neighbours)
2466 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2467 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2469 return GNUNET_OK; /* we're shutting down */
2471 scm = (const struct SessionConnectMessage *) message;
2472 GNUNET_break_op (0 == ntohl (scm->reserved));
2473 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2474 n = lookup_neighbour (peer);
2476 n = setup_neighbour (peer);
2477 n->send_connect_ack = 1;
2478 n->connect_ack_timestamp = ts;
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2481 "Received SESSION_CONNECT for peer `%s' in state %s/%d\n",
2483 GNUNET_TRANSPORT_ps2s (n->state),
2484 n->send_connect_ack);
2487 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2488 /* Do a blacklist check for the new address */
2489 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2490 check_blacklist (peer, ts, address, session);
2492 case GNUNET_TRANSPORT_PS_INIT_ATS:
2493 /* CONNECT message takes priority over us asking ATS for address */
2494 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2496 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2497 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2498 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2499 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2500 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2501 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2502 /* It can never hurt to have an alternative address in the above cases,
2503 see if it is allowed */
2504 check_blacklist (peer, ts, address, session);
2506 case GNUNET_TRANSPORT_PS_CONNECTED:
2507 /* we are already connected and can thus send the ACK immediately;
2508 still, it can never hurt to have an alternative address, so also
2509 tell ATS about it */
2510 GNUNET_assert (NULL != n->primary_address.address);
2511 GNUNET_assert (NULL != n->primary_address.session);
2512 n->send_connect_ack = 0;
2513 send_session_connect_ack_message (n->primary_address.address,
2514 n->primary_address.session, ts);
2515 check_blacklist (peer, ts, address, session);
2517 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2518 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2519 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2520 /* It can never hurt to have an alternative address in the above cases,
2521 see if it is allowed */
2522 check_blacklist (peer, ts, address, session);
2524 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2525 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2526 /* we are already connected and can thus send the ACK immediately;
2527 still, it can never hurt to have an alternative address, so also
2528 tell ATS about it */
2529 GNUNET_assert (NULL != n->primary_address.address);
2530 GNUNET_assert (NULL != n->primary_address.session);
2531 n->send_connect_ack = 0;
2532 send_session_connect_ack_message (n->primary_address.address,
2533 n->primary_address.session, ts);
2534 check_blacklist (peer, ts, address, session);
2536 case GNUNET_TRANSPORT_PS_DISCONNECT:
2537 /* get rid of remains without terminating sessions, ready to re-try */
2538 free_neighbour (n, GNUNET_YES);
2539 n = setup_neighbour (peer);
2540 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2541 GNUNET_ATS_reset_backoff (GST_ats, peer);
2543 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2544 /* should not be possible */
2548 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2549 "Unhandled state `%s'\n",
2550 GNUNET_TRANSPORT_ps2s (n->state));
2552 return GNUNET_SYSERR;
2558 switch_address_bl_check_cont (void *cls,
2559 const struct GNUNET_PeerIdentity *peer, int result)
2561 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2562 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2563 struct NeighbourMapEntry *n;
2565 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2566 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2571 "Peer %s is unknown, suggestion ignored\n",
2574 if (result == GNUNET_NO)
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2577 "Blacklist denied to switch to suggested address `%s' session %p for peer `%s'\n",
2578 GST_plugins_a2s (blc_ctx->address),
2580 GNUNET_i2s (&blc_ctx->address->peer));
2582 if (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name)))
2584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2585 "Plugin `%s' for suggested address `%s' session %p for peer `%s' is not available\n",
2586 blc_ctx->address->transport_name,
2587 GST_plugins_a2s (blc_ctx->address),
2589 GNUNET_i2s (&blc_ctx->address->peer));
2592 /* This address is blacklisted, delete address and session (if existing) in ATS */
2593 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2595 if ( (GNUNET_YES == (GNUNET_HELLO_address_check_option (blc_ctx->address,
2596 GNUNET_HELLO_ADDRESS_INFO_INBOUND))) && (NULL != blc_ctx->session))
2598 /* This is an inbound address, destroy full address */
2599 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL );
2602 /* Remove blacklist check and clean up */
2603 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2604 GNUNET_HELLO_address_free (blc_ctx->address);
2605 GNUNET_free_non_null (blc_ctx->ats);
2606 GNUNET_free (blc_ctx);
2610 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2611 "Blacklist accepted to switch to suggested address `%s' session %p for peer `%s'\n",
2612 GST_plugins_a2s (blc_ctx->address),
2614 GNUNET_i2s (&blc_ctx->address->peer));
2616 if (NULL == blc_ctx->session)
2618 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2620 "Obtained new session for peer `%s' and address '%s': %p\n",
2621 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2623 if (NULL == blc_ctx->session)
2625 /* No session could be obtained, remove blacklist check and clean up */
2626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2627 "Failed to obtain new session for peer `%s' and address '%s'\n",
2628 GNUNET_i2s (&blc_ctx->address->peer),
2629 GST_plugins_a2s (blc_ctx->address));
2630 /* Delete address in ATS */
2631 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2633 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2634 GNUNET_HELLO_address_free (blc_ctx->address);
2635 GNUNET_free_non_null (blc_ctx->ats);
2636 GNUNET_free (blc_ctx);
2642 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2644 free_neighbour (n, GNUNET_NO);
2646 case GNUNET_TRANSPORT_PS_INIT_ATS:
2647 /* We requested an address and ATS suggests one:
2648 * set primary address and send CONNECT message*/
2649 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2650 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2651 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2652 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2653 send_session_connect (&n->primary_address);
2655 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2656 /* ATS suggests a different address, switch again */
2657 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2658 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2659 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2660 check_blacklist (&n->id, n->connect_ack_timestamp,
2661 blc_ctx->address, blc_ctx->session);
2663 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2664 /* waiting on CONNECT_ACK, send ACK if one is pending */
2665 if (1 == n->send_connect_ack)
2667 n->send_connect_ack = 2;
2668 send_session_connect_ack_message (n->primary_address.address,
2669 n->primary_address.session,
2670 n->connect_ack_timestamp);
2672 /* ATS suggests a different address, switch again */
2673 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2674 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2675 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT,
2676 GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2677 send_session_connect (&n->primary_address);
2679 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2680 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2681 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2682 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2683 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2684 check_blacklist (&n->id, n->connect_ack_timestamp,
2685 blc_ctx->address, blc_ctx->session);
2687 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2688 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2689 check_blacklist (&n->id, n->connect_ack_timestamp,
2690 blc_ctx->address, blc_ctx->session);
2692 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2693 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2694 /* ATS asks us to switch while we were trying to connect; switch to new
2695 address and check blacklist again */
2696 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2697 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2698 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2699 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2700 check_blacklist (&n->id, n->connect_ack_timestamp,
2701 blc_ctx->address, blc_ctx->session);
2703 case GNUNET_TRANSPORT_PS_CONNECTED:
2704 GNUNET_assert (NULL != n->primary_address.address);
2705 GNUNET_assert (NULL != n->primary_address.session);
2706 if (n->primary_address.session == blc_ctx->session)
2708 /* not an address change, just a quota change */
2709 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2710 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2713 /* ATS asks us to switch a life connection; see if we can get
2714 a CONNECT_ACK on it before we actually do this! */
2715 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2716 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2717 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2718 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2719 blc_ctx->address, blc_ctx->session);
2721 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2722 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2723 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2724 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2725 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2726 check_blacklist (&n->id, n->connect_ack_timestamp,
2727 blc_ctx->address, blc_ctx->session);
2729 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2730 /* ATS asks us to switch while we were trying to reconnect; switch to new
2731 address and check blacklist again */
2732 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2733 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2734 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2735 check_blacklist (&n->id, n->connect_ack_timestamp,
2736 blc_ctx->address, blc_ctx->session);
2738 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2739 /* ATS asks us to switch while we were trying to reconnect; switch to new
2740 address and check blacklist again */
2741 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2742 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2743 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2744 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2745 check_blacklist (&n->id, n->connect_ack_timestamp,
2746 blc_ctx->address, blc_ctx->session);
2748 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2749 if (n->primary_address.session == blc_ctx->session)
2751 /* ATS switches back to still-active session */
2752 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2753 free_address (&n->alternative_address);
2756 /* ATS asks us to switch a life connection, update blacklist check */
2757 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2758 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2759 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2760 blc_ctx->address, blc_ctx->session);
2762 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2763 if (n->primary_address.session == blc_ctx->session)
2765 /* ATS switches back to still-active session */
2766 free_address (&n->alternative_address);
2767 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2770 /* ATS asks us to switch a life connection, update blacklist check */
2771 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2772 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2773 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2774 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2775 blc_ctx->address, blc_ctx->session);
2777 case GNUNET_TRANSPORT_PS_DISCONNECT:
2778 /* not going to switch addresses while disconnecting */
2780 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2784 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2785 "Unhandled state `%s'\n",
2786 GNUNET_TRANSPORT_ps2s (n->state));
2791 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2792 GNUNET_HELLO_address_free(blc_ctx->address);
2793 GNUNET_free_non_null (blc_ctx->ats);
2794 GNUNET_free (blc_ctx);
2800 * For the given peer, switch to this address.
2802 * Before accepting this addresses and actively using it, a blacklist check
2803 * is performed. If this blacklist check fails the address will be destroyed.
2805 * @param peer identity of the peer to switch the address for
2806 * @param address address of the other peer,
2807 * @param session session to use or NULL if transport should initiate a session
2808 * @param ats performance data
2809 * @param ats_count number of entries in ats
2810 * @param bandwidth_in inbound quota to be used when connection is up,
2811 * 0 to disconnect from peer
2812 * @param bandwidth_out outbound quota to be used when connection is up,
2813 * 0 to disconnect from peer
2816 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2817 const struct GNUNET_HELLO_Address *address,
2818 struct Session *session,
2819 const struct GNUNET_ATS_Information *ats,
2821 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2822 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2824 struct NeighbourMapEntry *n;
2825 struct GST_BlacklistCheck *blc;
2826 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2827 struct BlacklistCheckSwitchContext *blc_ctx;
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2831 "ATS has decided on an address for peer %s\n",
2833 GNUNET_assert (NULL != address->transport_name);
2834 if (NULL == (n = lookup_neighbour (peer)))
2836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2837 "Peer %s is unknown, suggestion ignored\n",
2842 /* Check if plugin is available */
2843 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2845 /* we don't have the plugin for this address */
2846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2847 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2848 address->transport_name,
2850 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2853 if ((NULL == session) &&
2854 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2856 /* This is a inbound address and we do not have a session to use! */
2857 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2858 "Inbound address without session `%s'! Destroying address...\n",
2859 GST_plugins_a2s (address));
2860 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2864 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2865 "ATS tells us to switch to %s address '%s' session %p for "
2866 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2867 GNUNET_HELLO_address_check_option (address,
2868 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "outbound",
2869 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2870 GNUNET_TRANSPORT_ps2s (n->state), n->send_connect_ack,
2871 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2873 /* Perform blacklist check */
2874 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2875 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2876 blc_ctx->session = session;
2877 blc_ctx->bandwidth_in = bandwidth_in;
2878 blc_ctx->bandwidth_out = bandwidth_out;
2879 blc_ctx->ats_count = ats_count;
2880 blc_ctx->ats = NULL;
2883 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2884 for (c = 0; c < ats_count; c++)
2886 blc_ctx->ats[c].type = ats[c].type;
2887 blc_ctx->ats[c].value = ats[c].value;
2891 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2892 if (NULL != (blc = GST_blacklist_test_allowed (peer, address->transport_name,
2893 &switch_address_bl_check_cont, blc_ctx)))
2901 send_utilization_data (void *cls,
2902 const struct GNUNET_PeerIdentity *key,
2905 struct NeighbourMapEntry *n = value;
2906 struct GNUNET_ATS_Information atsi[4];
2908 uint32_t bps_pl_out;
2911 struct GNUNET_TIME_Relative delta;
2913 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2914 GNUNET_TIME_absolute_get ());
2918 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2919 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2921 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2922 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2924 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2929 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2930 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2932 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2933 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2936 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2937 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2941 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2942 atsi[0].value = htonl (bps_out);
2943 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2944 atsi[1].value = htonl (bps_in);
2946 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2947 atsi[2].value = htonl (bps_pl_out);
2948 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2949 atsi[3].value = htonl (bps_pl_in);
2951 GST_ats_update_metrics (key, n->primary_address.address,
2952 n->primary_address.session, atsi, 4);
2953 n->util_payload_bytes_recv = 0;
2954 n->util_payload_bytes_sent = 0;
2955 n->util_total_bytes_recv = 0;
2956 n->util_total_bytes_sent = 0;
2957 n->last_util_transmission = GNUNET_TIME_absolute_get();
2963 * Task transmitting utilization in a regular interval
2965 * @param cls the 'struct NeighbourMapEntry' for which we are running
2966 * @param tc scheduler context (unused)
2969 utilization_transmission (void *cls,
2970 const struct GNUNET_SCHEDULER_TaskContext *tc)
2972 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2974 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2975 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2977 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2978 utilization_transmission, NULL);
2984 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2985 const struct GNUNET_HELLO_Address *address,
2986 struct Session *session,
2987 const struct GNUNET_MessageHeader *message)
2989 struct NeighbourMapEntry *n;
2991 n = lookup_neighbour (peer);
2994 n->util_total_bytes_recv += ntohs(message->size);
2999 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
3000 const struct GNUNET_HELLO_Address *address,
3001 struct Session *session,
3002 const struct GNUNET_MessageHeader *message)
3004 struct NeighbourMapEntry *n;
3005 n = lookup_neighbour (peer);
3008 n->util_payload_bytes_recv += ntohs(message->size);
3013 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
3014 const struct GNUNET_HELLO_Address *address,
3015 struct Session *session,
3018 struct NeighbourMapEntry *n;
3019 n = lookup_neighbour (peer);
3022 if (n->primary_address.session != session)
3024 n->util_total_bytes_sent += size;
3029 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
3032 struct NeighbourMapEntry *n;
3033 n = lookup_neighbour (peer);
3036 n->util_payload_bytes_sent += size;
3041 * Master task run for every neighbour. Performs all of the time-related
3042 * activities (keep alive, send next message, disconnect if idle, finish
3043 * clean up after disconnect).
3045 * @param cls the 'struct NeighbourMapEntry' for which we are running
3046 * @param tc scheduler context (unused)
3049 master_task (void *cls,
3050 const struct GNUNET_SCHEDULER_TaskContext *tc)
3052 struct NeighbourMapEntry *n = cls;
3053 struct GNUNET_TIME_Relative delay;
3055 n->task = GNUNET_SCHEDULER_NO_TASK;
3056 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
3057 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3058 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
3059 GNUNET_i2s (&n->id),
3060 GNUNET_TRANSPORT_ps2s(n->state),
3061 GNUNET_STRINGS_relative_time_to_string (delay,
3065 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3066 /* invalid state for master task, clean up */
3068 free_neighbour (n, GNUNET_NO);
3070 case GNUNET_TRANSPORT_PS_INIT_ATS:
3071 if (0 == delay.rel_value_us)
3073 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3074 "Connection to `%s' timed out waiting for ATS to provide address\n",
3075 GNUNET_i2s (&n->id));
3076 free_neighbour (n, GNUNET_NO);
3080 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3081 if (0 == delay.rel_value_us)
3083 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3084 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
3085 GNUNET_i2s (&n->id));
3086 free_neighbour (n, GNUNET_NO);
3090 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3091 if (0 == delay.rel_value_us)
3093 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3094 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
3095 GNUNET_i2s (&n->id));
3096 /* We could not send to this address, delete address and session */
3097 if (NULL != n->primary_address.session)
3098 GNUNET_ATS_address_destroyed (GST_ats,
3099 n->primary_address.address, n->primary_address.session);
3100 GNUNET_ATS_address_destroyed (GST_ats,
3101 n->primary_address.address, NULL);
3102 disconnect_neighbour (n);
3106 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3107 if (0 == delay.rel_value_us)
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3110 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
3111 GNUNET_i2s (&n->id));
3112 free_neighbour (n, GNUNET_NO);
3116 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3117 if (0 == delay.rel_value_us)
3119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3120 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
3121 GNUNET_i2s (&n->id));
3122 free_neighbour (n, GNUNET_NO);
3126 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3127 if (0 == delay.rel_value_us)
3129 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3130 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
3131 GNUNET_i2s (&n->id));
3132 free_neighbour (n, GNUNET_NO);
3136 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3137 if (0 == delay.rel_value_us)
3139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3140 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
3141 GNUNET_i2s (&n->id));
3142 disconnect_neighbour (n);
3146 case GNUNET_TRANSPORT_PS_CONNECTED:
3147 if (0 == delay.rel_value_us)
3149 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3150 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3151 GNUNET_i2s (&n->id));
3152 disconnect_neighbour (n);
3155 try_transmission_to_peer (n);
3158 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3159 if (0 == delay.rel_value_us)
3161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3162 "Connection to `%s' timed out, waiting for ATS replacement address\n",
3163 GNUNET_i2s (&n->id));
3164 disconnect_neighbour (n);
3168 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3169 if (0 == delay.rel_value_us)
3171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3172 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
3173 GNUNET_i2s (&n->id));
3174 disconnect_neighbour (n);
3178 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3179 if (0 == delay.rel_value_us)
3181 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3182 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
3183 GNUNET_i2s (&n->id));
3184 disconnect_neighbour (n);
3188 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3189 if (0 == delay.rel_value_us)
3191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3192 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
3193 GNUNET_i2s (&n->id));
3194 disconnect_neighbour (n);
3197 try_transmission_to_peer (n);
3200 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3201 if (0 == delay.rel_value_us)
3203 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3204 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
3205 GNUNET_i2s (&n->id));
3206 disconnect_neighbour (n);
3209 try_transmission_to_peer (n);
3212 case GNUNET_TRANSPORT_PS_DISCONNECT:
3213 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3214 "Cleaning up connection to `%s' after sending DISCONNECT\n",
3215 GNUNET_i2s (&n->id));
3216 free_neighbour (n, GNUNET_NO);
3218 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3219 /* how did we get here!? */
3223 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3224 "Unhandled state `%s'\n",
3225 GNUNET_TRANSPORT_ps2s (n->state));
3229 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
3230 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3231 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
3233 /* if we are *now* in one of these three states, we're sending
3234 keep alive messages, so we need to consider the keepalive
3235 delay, not just the connection timeout */
3236 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
3239 if (GNUNET_SCHEDULER_NO_TASK == n->task)
3240 n->task = GNUNET_SCHEDULER_add_delayed (delay,
3247 * Send a SESSION_ACK message to the neighbour to confirm that we
3248 * got his CONNECT_ACK.
3250 * @param n neighbour to send the SESSION_ACK to
3253 send_session_ack_message (struct NeighbourMapEntry *n)
3255 struct GNUNET_MessageHeader msg;
3257 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
3258 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
3259 (void) send_with_session(n,
3260 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
3261 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
3267 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
3268 * Consider switching to it.
3270 * @param message possibly a 'struct SessionConnectMessage' (check format)
3271 * @param peer identity of the peer to switch the address for
3272 * @param address address of the other peer, NULL if other peer
3274 * @param session session to use (or NULL)
3275 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3278 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
3279 const struct GNUNET_PeerIdentity *peer,
3280 const struct GNUNET_HELLO_Address *address,
3281 struct Session *session)
3283 const struct SessionConnectMessage *scm;
3284 struct GNUNET_TIME_Absolute ts;
3285 struct NeighbourMapEntry *n;
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 "Received CONNECT_ACK message from peer `%s'\n",
3291 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3293 GNUNET_break_op (0);
3294 return GNUNET_SYSERR;
3296 GNUNET_STATISTICS_update (GST_stats,
3298 ("# CONNECT_ACK messages received"),
3300 scm = (const struct SessionConnectMessage *) message;
3301 GNUNET_break_op (ntohl (scm->reserved) == 0);
3302 if (NULL == (n = lookup_neighbour (peer)))
3304 GNUNET_STATISTICS_update (GST_stats,
3306 ("# unexpected CONNECT_ACK messages (no peer)"),
3308 return GNUNET_SYSERR;
3310 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3313 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3315 free_neighbour (n, GNUNET_NO);
3316 return GNUNET_SYSERR;
3317 case GNUNET_TRANSPORT_PS_INIT_ATS:
3318 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3319 GNUNET_STATISTICS_update (GST_stats,
3321 ("# unexpected CONNECT_ACK messages (not ready)"),
3324 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3325 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3327 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3328 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3331 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3332 GNUNET_STATISTICS_set (GST_stats,
3333 gettext_noop ("# peers connected"),
3334 ++neighbours_connected,
3336 connect_notify_cb (callback_cls, &n->id,
3337 n->primary_address.bandwidth_in,
3338 n->primary_address.bandwidth_out);
3339 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3340 GST_ats_add_address (n->primary_address.address,
3341 n->primary_address.session,
3343 set_primary_address (n,
3344 n->primary_address.address,
3345 n->primary_address.session,
3346 n->primary_address.bandwidth_in,
3347 n->primary_address.bandwidth_out,
3349 send_session_ack_message (n);
3351 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3352 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3353 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3354 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3355 GNUNET_STATISTICS_update (GST_stats,
3357 ("# unexpected CONNECT_ACK messages (not ready)"),
3360 case GNUNET_TRANSPORT_PS_CONNECTED:
3361 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3362 send_session_ack_message (n);
3364 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3365 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3366 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3367 to give us a new address... */
3368 GNUNET_STATISTICS_update (GST_stats,
3370 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3373 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3374 /* new address worked; go back to connected! */
3375 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3376 send_session_ack_message (n);
3378 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3379 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3380 send_session_ack_message (n);
3382 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3383 /* new address worked; adopt it and go back to connected! */
3384 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3385 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3387 GST_ats_add_address (n->alternative_address.address,
3388 n->alternative_address.session,
3390 set_primary_address (n, n->alternative_address.address,
3391 n->alternative_address.session, n->alternative_address.bandwidth_in,
3392 n->alternative_address.bandwidth_out, GNUNET_YES);
3394 free_address (&n->alternative_address);
3395 send_session_ack_message (n);
3397 case GNUNET_TRANSPORT_PS_DISCONNECT:
3398 GNUNET_STATISTICS_update (GST_stats,
3400 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3402 return GNUNET_SYSERR;
3403 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3407 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3408 "Unhandled state `%s'\n",
3409 GNUNET_TRANSPORT_ps2s (n->state));
3411 return GNUNET_SYSERR;
3418 * A session was terminated. Take note; if needed, try to get
3419 * an alternative address from ATS.
3421 * @param peer identity of the peer where the session died
3422 * @param session session that is gone
3423 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3424 * this session was not in use
3427 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3428 struct Session *session)
3430 struct NeighbourMapEntry *n;
3431 struct BlackListCheckContext *bcc;
3432 struct BlackListCheckContext *bcc_next;
3434 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3436 while (NULL != (bcc = bcc_next))
3438 bcc_next = bcc->next;
3439 if (bcc->na.session == session)
3441 if (NULL != bcc->bc)
3442 GST_blacklist_test_cancel (bcc->bc);
3443 GNUNET_HELLO_address_free (bcc->na.address);
3444 GNUNET_CONTAINER_DLL_remove (bc_head,
3450 if (NULL == (n = lookup_neighbour (peer)))
3451 return GNUNET_NO; /* can't affect us */
3452 if (session != n->primary_address.session)
3454 if (session == n->alternative_address.session)
3456 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3457 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3458 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3461 free_address (&n->alternative_address);
3463 return GNUNET_NO; /* doesn't affect us further */
3466 n->expect_latency_response = GNUNET_NO;
3467 /* The session for neighbour's primary address died */
3470 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3472 free_neighbour (n, GNUNET_NO);
3474 case GNUNET_TRANSPORT_PS_INIT_ATS:
3476 free_neighbour (n, GNUNET_NO);
3478 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3479 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3480 /* The session used to send the CONNECT terminated:
3481 * this implies a connect error*/
3482 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3483 "Could not send CONNECT message with address `%s' session %p: session terminated, requesting new address\n",
3484 GST_plugins_a2s (n->primary_address.address), n->primary_address.session,
3486 GNUNET_ATS_address_destroyed (GST_ats, n->primary_address.address, NULL);
3487 unset_primary_address (n);
3488 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS,
3489 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3491 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3492 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3493 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3494 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3495 /* error on inbound session; free neighbour entirely */
3496 free_address (&n->primary_address);
3497 free_neighbour (n, GNUNET_NO);
3499 case GNUNET_TRANSPORT_PS_CONNECTED:
3500 unset_primary_address (n);
3501 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS,
3502 GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3504 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3505 /* we don't have an address, how can it go down? */
3508 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3509 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3510 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3512 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3513 /* primary went down while we were checking secondary against
3514 blacklist, adopt secondary as primary */
3515 free_address (&n->primary_address);
3516 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3517 n->primary_address = n->alternative_address;
3518 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3520 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3521 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3522 secondary as primary */
3523 free_address (&n->primary_address);
3524 n->primary_address = n->alternative_address;
3525 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3526 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3528 case GNUNET_TRANSPORT_PS_DISCONNECT:
3529 free_address (&n->primary_address);
3531 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3532 /* neighbour was freed and plugins told to terminate session */
3536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3537 "Unhandled state `%s'\n",
3538 GNUNET_TRANSPORT_ps2s (n->state));
3542 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3543 GNUNET_SCHEDULER_cancel (n->task);
3544 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3550 * We received a 'SESSION_ACK' message from the other peer.
3551 * If we sent a 'CONNECT_ACK' last, this means we are now
3552 * connected. Otherwise, do nothing.
3554 * @param message possibly a 'struct SessionConnectMessage' (check format)
3555 * @param peer identity of the peer to switch the address for
3556 * @param address address of the other peer, NULL if other peer
3558 * @param session session to use (or NULL)
3559 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3562 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3563 const struct GNUNET_PeerIdentity *peer,
3564 const struct GNUNET_HELLO_Address *address,
3565 struct Session *session)
3567 struct NeighbourMapEntry *n;
3569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3570 "Received SESSION_ACK message from peer `%s'\n",
3572 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3574 GNUNET_break_op (0);
3575 return GNUNET_SYSERR;
3577 GNUNET_STATISTICS_update (GST_stats,
3579 ("# SESSION_ACK messages received"),
3581 if (NULL == (n = lookup_neighbour (peer)))
3583 GNUNET_break_op (0);
3584 return GNUNET_SYSERR;
3586 /* check if we are in a plausible state for having sent
3587 a CONNECT_ACK. If not, return, otherwise break */
3588 if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3589 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3590 (2 != n->send_connect_ack) )
3592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3593 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3595 GNUNET_TRANSPORT_ps2s (n->state),
3596 n->send_connect_ack);
3597 GNUNET_STATISTICS_update (GST_stats,
3598 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3602 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3603 GNUNET_STATISTICS_set (GST_stats,
3604 gettext_noop ("# peers connected"),
3605 ++neighbours_connected,
3607 connect_notify_cb (callback_cls, &n->id,
3608 n->primary_address.bandwidth_in,
3609 n->primary_address.bandwidth_out);
3611 GST_ats_add_address (n->primary_address.address,
3612 n->primary_address.session,
3614 set_primary_address (n,
3615 n->primary_address.address,
3616 n->primary_address.session,
3617 n->primary_address.bandwidth_in,
3618 n->primary_address.bandwidth_out,
3625 * Test if we're connected to the given peer.
3627 * @param target peer to test
3628 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3631 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3633 return test_connected (lookup_neighbour (target));
3637 * Change the incoming quota for the given peer.
3639 * @param neighbour identity of peer to change qutoa for
3640 * @param quota new quota
3643 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3644 struct GNUNET_BANDWIDTH_Value32NBO quota)
3646 struct NeighbourMapEntry *n;
3648 if (NULL == (n = lookup_neighbour (neighbour)))
3650 GNUNET_STATISTICS_update (GST_stats,
3652 ("# SET QUOTA messages ignored (no such peer)"),
3656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3657 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3658 ntohl (quota.value__), GNUNET_i2s (&n->id));
3659 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3660 if (0 != ntohl (quota.value__))
3662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3663 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3664 GNUNET_i2s (&n->id));
3665 if (GNUNET_YES == test_connected (n))
3666 GNUNET_STATISTICS_update (GST_stats,
3667 gettext_noop ("# disconnects due to quota of 0"),
3669 disconnect_neighbour (n);
3674 * We received a disconnect message from the given peer,
3675 * validate and process.
3677 * @param peer sender of the message
3678 * @param msg the disconnect message
3681 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3682 const struct GNUNET_MessageHeader *msg)
3684 struct NeighbourMapEntry *n;
3685 const struct SessionDisconnectMessage *sdm;
3687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3688 "Received DISCONNECT message from peer `%s'\n",
3690 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3692 // GNUNET_break_op (0);
3693 GNUNET_STATISTICS_update (GST_stats,
3695 ("# disconnect messages ignored (old format)"), 1,
3699 GNUNET_STATISTICS_update (GST_stats,
3701 ("# DISCONNECT messages received"),
3703 sdm = (const struct SessionDisconnectMessage *) msg;
3704 if (NULL == (n = lookup_neighbour (peer)))
3705 return; /* gone already */
3706 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3708 GNUNET_STATISTICS_update (GST_stats,
3710 ("# disconnect messages ignored (timestamp)"), 1,
3714 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3716 GNUNET_break_op (0);
3719 if (ntohl (sdm->purpose.size) !=
3720 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3721 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3722 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3724 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3725 "%s message from peer `%s' has invalid size \n",
3728 GNUNET_break_op (0);
3732 GNUNET_CRYPTO_eddsa_verify
3733 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3734 &sdm->signature, &sdm->public_key))
3736 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3737 "%s message from peer `%s' cannot be verified \n",
3740 GNUNET_break_op (0);
3743 if (GNUNET_YES == test_connected (n))
3744 GNUNET_STATISTICS_update (GST_stats,
3746 ("# other peer asked to disconnect from us"), 1,
3748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3749 "Disconnecting by request from peer %s\n",
3751 disconnect_neighbour (n);
3756 * Closure for the neighbours_iterate function.
3758 struct IteratorContext
3761 * Function to call on each connected neighbour.
3763 GST_NeighbourIterator cb;
3773 * Call the callback from the closure for each neighbour.
3775 * @param cls the `struct IteratorContext`
3776 * @param key the hash of the public key of the neighbour
3777 * @param value the `struct NeighbourMapEntry`
3778 * @return #GNUNET_OK (continue to iterate)
3781 neighbours_iterate (void *cls,
3782 const struct GNUNET_PeerIdentity *key,
3785 struct IteratorContext *ic = cls;
3786 struct NeighbourMapEntry *n = value;
3787 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3788 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3791 if (NULL != n->primary_address.address)
3793 bandwidth_in = n->primary_address.bandwidth_in;
3794 bandwidth_out = n->primary_address.bandwidth_out;
3798 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3799 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3803 n->primary_address.address,
3806 bandwidth_in, bandwidth_out);
3812 * Iterate over all connected neighbours.
3814 * @param cb function to call
3815 * @param cb_cls closure for cb
3818 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3820 struct IteratorContext ic;
3822 if (NULL == neighbours)
3823 return; /* can happen during shutdown */
3826 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3831 * If we have an active connection to the given target, it must be shutdown.
3833 * @param target peer to disconnect from
3836 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3838 struct NeighbourMapEntry *n;
3840 if (NULL == (n = lookup_neighbour (target)))
3841 return; /* not active */
3842 if (GNUNET_YES == test_connected (n))
3843 GNUNET_STATISTICS_update (GST_stats,
3845 ("# disconnected from peer upon explicit request"), 1,
3847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3848 "Forced disconnect from peer %s\n",
3849 GNUNET_i2s (target));
3850 disconnect_neighbour (n);
3855 * Obtain current latency information for the given neighbour.
3857 * @param peer to get the latency for
3858 * @return observed latency of the address, FOREVER if the
3859 * the connection is not up
3861 struct GNUNET_TIME_Relative
3862 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3864 struct NeighbourMapEntry *n;
3866 n = lookup_neighbour (peer);
3868 return GNUNET_TIME_UNIT_FOREVER_REL;
3871 case GNUNET_TRANSPORT_PS_CONNECTED:
3872 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3873 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3874 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3875 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3876 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3878 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3879 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3880 case GNUNET_TRANSPORT_PS_INIT_ATS:
3881 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3882 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3883 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3884 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3885 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3886 case GNUNET_TRANSPORT_PS_DISCONNECT:
3887 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3888 return GNUNET_TIME_UNIT_FOREVER_REL;
3890 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3891 "Unhandled state `%s'\n",
3892 GNUNET_TRANSPORT_ps2s (n->state));
3896 return GNUNET_TIME_UNIT_FOREVER_REL;
3901 * Obtain current address information for the given neighbour.
3904 * @return address currently used
3906 struct GNUNET_HELLO_Address *
3907 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3909 struct NeighbourMapEntry *n;
3911 n = lookup_neighbour (peer);
3914 return n->primary_address.address;
3919 * Initialize the neighbours subsystem.
3921 * @param cls closure for callbacks
3922 * @param connect_cb function to call if we connect to a peer
3923 * @param disconnect_cb function to call if we disconnect from a peer
3924 * @param peer_address_cb function to call if we change an active address
3926 * @param max_fds maximum number of fds to use
3929 GST_neighbours_start (void *cls,
3930 NotifyConnect connect_cb,
3931 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3932 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3933 unsigned int max_fds)
3936 connect_notify_cb = connect_cb;
3937 disconnect_notify_cb = disconnect_cb;
3938 neighbour_change_cb = peer_address_cb;
3939 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3940 registered_quota_notifications = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3941 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3942 utilization_transmission, NULL);
3947 * Disconnect from the given neighbour.
3950 * @param key hash of neighbour's public key (not used)
3951 * @param value the 'struct NeighbourMapEntry' of the neighbour
3952 * @return #GNUNET_OK (continue to iterate)
3955 disconnect_all_neighbours (void *cls,
3956 const struct GNUNET_PeerIdentity *key,
3959 struct NeighbourMapEntry *n = value;
3961 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3962 "Disconnecting peer `%4s', %s\n",
3963 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3964 free_neighbour (n, GNUNET_NO);
3970 * Cleanup the neighbours subsystem.
3973 GST_neighbours_stop ()
3975 struct BlacklistCheckSwitchContext *cur;
3976 struct BlacklistCheckSwitchContext *next;
3978 if (NULL == neighbours)
3980 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3982 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3983 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3986 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3987 &disconnect_all_neighbours,
3989 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3991 next = pending_bc_head;
3992 for (cur = next; NULL != cur; cur = next )
3995 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3997 if (NULL != cur->blc)
3999 GST_blacklist_test_cancel (cur->blc);
4002 if (NULL != cur->address)
4003 GNUNET_HELLO_address_free (cur->address);
4004 GNUNET_free_non_null (cur->ats);
4008 GNUNET_CONTAINER_multipeermap_iterate (registered_quota_notifications,
4009 &free_notification_cb, NULL);
4010 GNUNET_CONTAINER_multipeermap_destroy (registered_quota_notifications);
4011 registered_quota_notifications = NULL;
4014 callback_cls = NULL;
4015 connect_notify_cb = NULL;
4016 disconnect_notify_cb = NULL;
4017 neighbour_change_cb = NULL;
4021 /* end of file gnunet-service-transport_neighbours.c */