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;
512 * Lookup a neighbour entry in the neighbours hash map.
514 * @param pid identity of the peer to look up
515 * @return the entry, NULL if there is no existing record
517 static struct NeighbourMapEntry *
518 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
520 if (NULL == neighbours)
522 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
527 * Test if we're connected to the given peer.
529 * @param n neighbour entry of peer to test
530 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
533 test_connected (struct NeighbourMapEntry *n)
537 return GNUNET_TRANSPORT_is_connected (n->state);
541 * Send information about a new outbound quota to our clients.
543 * @param target affected peer
544 * @param quota new quota
547 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
548 struct GNUNET_BANDWIDTH_Value32NBO quota)
550 struct QuotaSetMessage q_msg;
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
554 ntohl (quota.value__), GNUNET_i2s (target));
555 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
556 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
558 q_msg.peer = (*target);
559 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
564 * We don't need a given neighbour address any more.
565 * Release its resources and give appropriate notifications
566 * to ATS and other subsystems.
568 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
571 free_address (struct NeighbourAddress *na)
573 if (GNUNET_YES == na->ats_active)
575 GST_validation_set_address_use (na->address, na->session, GNUNET_NO);
576 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
579 na->ats_active = GNUNET_NO;
580 na->keep_alive_nonce = 0;
581 if (NULL != na->address)
583 GNUNET_HELLO_address_free (na->address);
591 * Set net state for this neighbour and notify monitoring
593 * @param n the respective neighbour
594 * @param s the new state
597 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
600 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
602 GNUNET_TRANSPORT_ps2s(s));
603 neighbour_change_cb (callback_cls,
605 n->primary_address.address,
606 n->state, n->timeout,
607 n->primary_address.bandwidth_in,
608 n->primary_address.bandwidth_out);
613 * Set net state and state timeout for this neighbour and notify monitoring
615 * @param n the respective neighbour
616 * @param s the new state
617 * @param timeout the new timeout
620 set_state_and_timeout (struct NeighbourMapEntry *n,
621 enum GNUNET_TRANSPORT_PeerState s,
622 struct GNUNET_TIME_Absolute timeout)
625 n->timeout = timeout;
626 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
628 GNUNET_TRANSPORT_ps2s(s),
629 GNUNET_STRINGS_absolute_time_to_string (timeout));
630 neighbour_change_cb (callback_cls,
632 n->primary_address.address,
633 n->state, n->timeout,
634 n->primary_address.bandwidth_in,
635 n->primary_address.bandwidth_out);
640 * Set new state timeout for this neighbour and notify monitoring
642 * @param n the respective neighbour
643 * @param timeout the new timeout
646 set_timeout (struct NeighbourMapEntry *n,
647 struct GNUNET_TIME_Absolute timeout)
649 n->timeout = timeout;
650 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed timeout %s\n",
652 GNUNET_STRINGS_absolute_time_to_string (timeout));
653 neighbour_change_cb (callback_cls,
655 n->primary_address.address,
656 n->state, n->timeout,
657 n->primary_address.bandwidth_in,
658 n->primary_address.bandwidth_out);
663 * Initialize the alternative address of a neighbour
665 * @param n the neighbour
666 * @param address address of the other peer, NULL if other peer
668 * @param session session to use (or NULL, in which case an
669 * address must be setup)
670 * @param bandwidth_in inbound quota to be used when connection is up
671 * @param bandwidth_out outbound quota to be used when connection is up
674 set_alternative_address (struct NeighbourMapEntry *n,
675 const struct GNUNET_HELLO_Address *address,
676 struct Session *session,
677 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
678 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
680 struct GNUNET_TRANSPORT_PluginFunctions *papi;
681 if (NULL == (papi = GST_plugins_find (address->transport_name)))
686 if (session == n->alternative_address.session)
688 n->alternative_address.bandwidth_in = bandwidth_in;
689 n->alternative_address.bandwidth_out = bandwidth_out;
692 free_address (&n->alternative_address);
694 session = papi->get_session (papi->cls, address);
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "Failed to obtain new session for peer `%s' and address '%s'\n",
699 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
700 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
704 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
706 GST_plugins_a2s(address));
708 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
709 n->alternative_address.bandwidth_in = bandwidth_in;
710 n->alternative_address.bandwidth_out = bandwidth_out;
711 n->alternative_address.session = session;
712 n->alternative_address.ats_active = GNUNET_NO;
713 n->alternative_address.keep_alive_nonce = 0;
718 * Initialize the primary address of a neighbour
720 * @param n the neighbour
721 * @param address address of the other peer, NULL if other peer
723 * @param session session to use (or NULL, in which case an
724 * address must be setup)
725 * @param bandwidth_in inbound quota to be used when connection is up
726 * @param bandwidth_out outbound quota to be used when connection is up
727 * @param is_active #GNUNET_YES to mark this as the active address with ATS
730 set_primary_address (struct NeighbourMapEntry *n,
731 const struct GNUNET_HELLO_Address *address,
732 struct Session *session,
733 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
734 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
737 struct GNUNET_TRANSPORT_PluginFunctions *papi;
739 if (NULL == (papi = GST_plugins_find (address->transport_name)))
744 if (session == n->primary_address.session)
746 n->primary_address.bandwidth_in = bandwidth_in;
747 n->primary_address.bandwidth_out = bandwidth_out;
748 if (is_active != n->primary_address.ats_active)
750 n->primary_address.ats_active = is_active;
751 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
752 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
754 if (GNUNET_YES == is_active)
756 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
757 send_outbound_quota (&address->peer, bandwidth_out);
761 free_address (&n->primary_address);
763 session = papi->get_session (papi->cls, address);
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Failed to obtain new session for peer `%s' and address '%s'\n",
768 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
769 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
773 n->primary_address.address = GNUNET_HELLO_address_copy (address);
774 n->primary_address.bandwidth_in = bandwidth_in;
775 n->primary_address.bandwidth_out = bandwidth_out;
776 n->primary_address.session = session;
777 n->primary_address.ats_active = is_active;
778 n->primary_address.keep_alive_nonce = 0;
779 if (GNUNET_YES == is_active)
781 /* Telling ATS about new session */
782 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
783 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
784 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
785 send_outbound_quota (&address->peer, bandwidth_out);
788 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
790 GST_plugins_a2s(address));
792 neighbour_change_cb (callback_cls,
794 n->primary_address.address,
795 n->state, n->timeout,
796 n->primary_address.bandwidth_in,
797 n->primary_address.bandwidth_out);
801 * Clear the primary address of a neighbour since this primary address is not
802 * valid anymore and notify monitoring about it
804 * @param n the neighbour
807 unset_primary_address (struct NeighbourMapEntry *n)
814 * Free a neighbour map entry.
816 * @param n entry to free
817 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
818 * #GNUNET_YES to keep all sessions
821 free_neighbour (struct NeighbourMapEntry *n,
824 struct MessageQueue *mq;
825 struct GNUNET_TRANSPORT_PluginFunctions *papi;
826 struct GNUNET_HELLO_Address *backup_primary;
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Freeing neighbur state of peer `%s'\n",
830 GNUNET_i2s (&n->id));
831 n->is_active = NULL; /* always free'd by its own continuation! */
833 /* fail messages currently in the queue */
834 while (NULL != (mq = n->messages_head))
836 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
837 if (NULL != mq->cont)
838 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
841 /* It is too late to send other peer disconnect notifications, but at
842 least internally we need to get clean... */
843 if (GNUNET_YES == test_connected (n))
845 GNUNET_STATISTICS_set (GST_stats,
846 gettext_noop ("# peers connected"),
847 --neighbours_connected,
849 disconnect_notify_cb (callback_cls, &n->id);
851 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
853 if (NULL != n->primary_address.address)
855 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
858 backup_primary = NULL;
860 /* free addresses and mark as unused */
861 free_address (&n->primary_address);
862 free_address (&n->alternative_address);
864 /* FIXME: Note that if we are switching between two TCP sessions to
865 the same peer, we might want to selectively kill only one of
866 them! Killing all sessions like this seems to be very, very
869 /* cut transport-level connection */
870 if ((GNUNET_NO == keep_sessions) &&
871 (NULL != backup_primary) &&
872 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
873 papi->disconnect_peer (papi->cls, &n->id);
875 GNUNET_free_non_null (backup_primary);
877 GNUNET_assert (GNUNET_YES ==
878 GNUNET_CONTAINER_multipeermap_remove (neighbours,
881 // FIXME-ATS-API: we might want to be more specific about
882 // which states we do this from in the future (ATS should
883 // have given us a 'suggest_address' handle, and if we have
884 // such a handle, we should cancel the operation here!
885 if (NULL != n->suggest_handle)
887 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
888 n->suggest_handle = NULL;
891 if (GNUNET_SCHEDULER_NO_TASK != n->task)
893 GNUNET_SCHEDULER_cancel (n->task);
894 n->task = GNUNET_SCHEDULER_NO_TASK;
896 /* free rest of memory */
902 * Transmit a message using the current session of the given
905 * @param n entry for the recipient
906 * @param msgbuf buffer to transmit
907 * @param msgbuf_size number of bytes in @a msgbuf buffer
908 * @param priority transmission priority
909 * @param timeout transmission timeout
910 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
911 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
912 * @param cont continuation to call when finished (can be NULL)
913 * @param cont_cls closure for @a cont
914 * @return timeout (copy of @a timeout or a calculated one if
915 * @a use_keepalive_timeout is #GNUNET_YES.
917 static struct GNUNET_TIME_Relative
918 send_with_session (struct NeighbourMapEntry *n,
919 const char *msgbuf, size_t msgbuf_size,
921 struct GNUNET_TIME_Relative timeout,
922 unsigned int use_keepalive_timeout,
923 GNUNET_TRANSPORT_TransmitContinuation cont,
926 struct GNUNET_TRANSPORT_PluginFunctions *papi;
927 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
929 GNUNET_assert (n->primary_address.session != NULL);
930 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
931 (-1 == papi->send (papi->cls,
932 n->primary_address.session,
935 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
936 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
937 papi->query_keepalive_factor (papi->cls))),
938 cont, cont_cls)))) &&
940 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
941 GST_neighbours_notify_data_sent (&n->id,
942 n->primary_address.address, n->primary_address.session, msgbuf_size);
943 GNUNET_break (NULL != papi);
949 * Master task run for every neighbour. Performs all of the time-related
950 * activities (keep alive, send next message, disconnect if idle, finish
951 * clean up after disconnect).
953 * @param cls the `struct NeighbourMapEntry` for which we are running
954 * @param tc scheduler context (unused)
957 master_task (void *cls,
958 const struct GNUNET_SCHEDULER_TaskContext *tc);
962 * Function called when the 'DISCONNECT' message has been sent by the
963 * plugin. Frees the neighbour --- if the entry still exists.
966 * @param target identity of the neighbour that was disconnected
967 * @param result #GNUNET_OK if the disconnect got out successfully
968 * @param payload bytes payload
969 * @param physical bytes physical
972 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
973 int result, size_t payload, size_t physical)
975 struct NeighbourMapEntry *n;
977 n = lookup_neighbour (target);
979 return; /* already gone */
980 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
981 return; /* have created a fresh entry since */
982 if (GNUNET_SCHEDULER_NO_TASK != n->task)
983 GNUNET_SCHEDULER_cancel (n->task);
984 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
989 * Transmit a DISCONNECT message to the other peer.
991 * @param n neighbour to send DISCONNECT message.
994 send_disconnect (struct NeighbourMapEntry *n)
996 struct SessionDisconnectMessage disconnect_msg;
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Sending DISCONNECT message to peer `%4s'\n",
1000 GNUNET_i2s (&n->id));
1001 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1002 disconnect_msg.header.type =
1003 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1004 disconnect_msg.reserved = htonl (0);
1005 disconnect_msg.purpose.size =
1006 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1007 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1008 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1009 disconnect_msg.purpose.purpose =
1010 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1011 disconnect_msg.timestamp =
1012 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1013 disconnect_msg.public_key = GST_my_identity.public_key;
1014 GNUNET_assert (GNUNET_OK ==
1015 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1016 &disconnect_msg.purpose,
1017 &disconnect_msg.signature));
1019 (void) send_with_session (n,
1020 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1021 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1022 GNUNET_NO, &send_disconnect_cont, NULL);
1023 GNUNET_STATISTICS_update (GST_stats,
1025 ("# DISCONNECT messages sent"), 1,
1031 * Disconnect from the given neighbour, clean up the record.
1033 * @param n neighbour to disconnect from
1036 disconnect_neighbour (struct NeighbourMapEntry *n)
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "Disconnecting from peer %s in state %s\n",
1040 GNUNET_i2s (&n->id),
1041 GNUNET_TRANSPORT_ps2s (n->state));
1042 /* depending on state, notify neighbour and/or upper layers of this peer
1046 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1047 case GNUNET_TRANSPORT_PS_INIT_ATS:
1048 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1049 /* other peer is completely unaware of us, no need to send DISCONNECT */
1050 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1051 free_neighbour (n, GNUNET_NO);
1053 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1054 send_disconnect (n);
1055 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1057 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1058 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1059 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1060 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1061 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1062 free_neighbour (n, GNUNET_NO);
1064 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1065 /* we DID ACK the other peer's request, must send DISCONNECT */
1066 send_disconnect (n);
1067 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1069 case GNUNET_TRANSPORT_PS_CONNECTED:
1070 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1071 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1072 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1073 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1074 /* we are currently connected, need to send disconnect and do
1075 internal notifications and update statistics */
1076 send_disconnect (n);
1077 GNUNET_STATISTICS_set (GST_stats,
1078 gettext_noop ("# peers connected"),
1079 --neighbours_connected,
1081 disconnect_notify_cb (callback_cls, &n->id);
1082 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1084 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1085 /* ATS address request timeout, disconnect without sending disconnect message */
1086 GNUNET_STATISTICS_set (GST_stats,
1087 gettext_noop ("# peers connected"),
1088 --neighbours_connected,
1090 disconnect_notify_cb (callback_cls, &n->id);
1091 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1093 case GNUNET_TRANSPORT_PS_DISCONNECT:
1094 /* already disconnected, ignore */
1096 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1097 /* already cleaned up, how did we get here!? */
1101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1102 "Unhandled state `%s'\n",
1103 GNUNET_TRANSPORT_ps2s (n->state));
1107 /* schedule timeout to clean up */
1108 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1109 GNUNET_SCHEDULER_cancel (n->task);
1110 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1116 * We're done with our transmission attempt, continue processing.
1118 * @param cls the `struct MessageQueue` of the message
1119 * @param receiver intended receiver
1120 * @param success whether it worked or not
1121 * @param size_payload bytes payload sent
1122 * @param physical bytes sent on wire
1125 transmit_send_continuation (void *cls,
1126 const struct GNUNET_PeerIdentity *receiver,
1127 int success, size_t size_payload, size_t physical)
1129 struct MessageQueue *mq = cls;
1130 struct NeighbourMapEntry *n;
1132 if (NULL == (n = lookup_neighbour (receiver)))
1135 return; /* disconnect or other error while transmitting, can happen */
1137 if (n->is_active == mq)
1139 /* this is still "our" neighbour, remove us from its queue
1140 and allow it to send the next message now */
1141 n->is_active = NULL;
1142 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1143 GNUNET_SCHEDULER_cancel (n->task);
1144 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1146 if (bytes_in_send_queue < mq->message_buf_size)
1148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1149 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1150 bytes_in_send_queue, mq->message_buf_size,
1151 (GNUNET_OK == success) ? "OK" : "FAIL",
1152 size_payload, physical);
1157 GNUNET_break (size_payload == mq->message_buf_size);
1158 bytes_in_send_queue -= mq->message_buf_size;
1159 GNUNET_STATISTICS_set (GST_stats,
1161 ("# bytes in message queue for other peers"),
1162 bytes_in_send_queue, GNUNET_NO);
1163 if (GNUNET_OK == success)
1164 GNUNET_STATISTICS_update (GST_stats,
1166 ("# messages transmitted to other peers"),
1169 GNUNET_STATISTICS_update (GST_stats,
1171 ("# transmission failures for messages to other peers"),
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "Sending message to `%s' of type %u was a %s\n",
1175 GNUNET_i2s (receiver),
1176 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1177 (success == GNUNET_OK) ? "success" : "FAILURE");
1178 if (NULL != mq->cont)
1179 mq->cont (mq->cont_cls, success, size_payload, physical);
1185 * Check the message list for the given neighbour and if we can
1186 * send a message, do so. This function should only be called
1187 * if the connection is at least generally ready for transmission.
1188 * While we will only send one message at a time, no bandwidth
1189 * quota management is performed here. If a message was given to
1190 * the plugin, the continuation will automatically re-schedule
1191 * the 'master' task once the next message might be transmitted.
1193 * @param n target peer for which to transmit
1196 try_transmission_to_peer (struct NeighbourMapEntry *n)
1198 struct MessageQueue *mq;
1199 struct GNUNET_TIME_Relative timeout;
1201 if (NULL == n->primary_address.address)
1203 /* no address, why are we here? */
1207 if ((0 == n->primary_address.address->address_length) &&
1208 (NULL == n->primary_address.session))
1210 /* no address, why are we here? */
1214 if (NULL != n->is_active)
1216 /* transmission already pending */
1220 /* timeout messages from the queue that are past their due date */
1221 while (NULL != (mq = n->messages_head))
1223 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1224 if (timeout.rel_value_us > 0)
1226 GNUNET_STATISTICS_update (GST_stats,
1228 ("# messages timed out while in transport queue"),
1230 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1232 transmit_send_continuation (mq, &n->id,
1234 mq->message_buf_size, 0); /* timeout */
1237 return; /* no more messages */
1238 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1240 (void) send_with_session (n,
1241 mq->message_buf, mq->message_buf_size,
1242 0 /* priority */, timeout, GNUNET_NO,
1243 &transmit_send_continuation, mq);
1248 * Send keepalive message to the neighbour. Must only be called
1249 * if we are on 'connected' state or while trying to switch addresses.
1250 * Will internally determine if a keepalive is truly needed (so can
1251 * always be called).
1253 * @param n neighbour that went idle and needs a keepalive
1256 send_keepalive (struct NeighbourMapEntry *n)
1258 struct SessionKeepAliveMessage m;
1259 struct GNUNET_TIME_Relative timeout;
1262 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1263 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1264 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1265 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1266 return; /* no keepalive needed at this time */
1268 nonce = 0; /* 0 indicates 'not set' */
1270 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "Sending keep alive to peer `%s' with nonce %u\n",
1274 GNUNET_i2s (&n->id), nonce);
1276 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1277 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1278 m.nonce = htonl (nonce);
1280 timeout = send_with_session (n,
1281 (const void *) &m, sizeof (m),
1282 UINT32_MAX /* priority */,
1283 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1285 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1287 n->primary_address.keep_alive_nonce = nonce;
1288 n->expect_latency_response = GNUNET_YES;
1289 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1290 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1296 * Keep the connection to the given neighbour alive longer,
1297 * we received a KEEPALIVE (or equivalent); send a response.
1299 * @param neighbour neighbour to keep alive (by sending keep alive response)
1300 * @param m the keep alive message containing the nonce to respond to
1303 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1304 const struct GNUNET_MessageHeader *m)
1306 struct NeighbourMapEntry *n;
1307 const struct SessionKeepAliveMessage *msg_in;
1308 struct SessionKeepAliveMessage msg;
1310 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1313 msg_in = (struct SessionKeepAliveMessage *) m;
1314 if (NULL == (n = lookup_neighbour (neighbour)))
1316 GNUNET_STATISTICS_update (GST_stats,
1318 ("# KEEPALIVE messages discarded (peer unknown)"),
1322 if (NULL == n->primary_address.session)
1324 GNUNET_STATISTICS_update (GST_stats,
1326 ("# KEEPALIVE messages discarded (no session)"),
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "Received keep alive request from peer `%s' with nonce %u\n",
1333 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1335 /* send reply to allow neighbour to measure latency */
1336 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1337 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1338 msg.nonce = msg_in->nonce;
1339 (void) send_with_session(n,
1340 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1341 UINT32_MAX /* priority */,
1342 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1348 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1349 * latency to this peer. Pass the updated information (existing ats
1350 * plus calculated latency) to ATS.
1352 * @param neighbour neighbour to keep alive
1353 * @param m the message containing the keep alive response
1356 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1357 const struct GNUNET_MessageHeader *m)
1359 struct NeighbourMapEntry *n;
1360 const struct SessionKeepAliveMessage *msg;
1361 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1363 struct GNUNET_ATS_Information ats;
1365 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1368 msg = (const struct SessionKeepAliveMessage *) m;
1369 if (NULL == (n = lookup_neighbour (neighbour)))
1371 GNUNET_STATISTICS_update (GST_stats,
1373 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1377 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1378 (GNUNET_YES != n->expect_latency_response) )
1380 GNUNET_STATISTICS_update (GST_stats,
1382 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1386 if (NULL == n->primary_address.address)
1388 GNUNET_STATISTICS_update (GST_stats,
1390 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1394 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1396 GNUNET_STATISTICS_update (GST_stats,
1398 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 "Received keep alive response from peer `%s' for session %p\n",
1406 GNUNET_i2s (&n->id), n->primary_address.session);
1410 /* Update session timeout here */
1411 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414 "Updating session for peer `%s' for session %p\n",
1415 GNUNET_i2s (&n->id), n->primary_address.session);
1416 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1423 n->primary_address.keep_alive_nonce = 0;
1424 n->expect_latency_response = GNUNET_NO;
1425 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1426 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Latency for peer `%s' is %s\n",
1430 GNUNET_i2s (&n->id),
1431 GNUNET_STRINGS_relative_time_to_string (n->latency,
1433 /* append latency */
1434 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1435 if (n->latency.rel_value_us > UINT32_MAX)
1436 latency = UINT32_MAX;
1438 latency = n->latency.rel_value_us;
1439 ats.value = htonl (latency);
1440 GST_ats_update_metrics (&n->id, n->primary_address.address,
1441 n->primary_address.session, &ats, 1);
1446 * We have received a message from the given sender. How long should
1447 * we delay before receiving more? (Also used to keep the peer marked
1450 * @param sender sender of the message
1451 * @param size size of the message
1452 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1453 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1454 * #GNUNET_SYSERR if the connection is not fully up yet
1455 * @return how long to wait before reading more from this sender
1457 struct GNUNET_TIME_Relative
1458 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1459 *sender, ssize_t size, int *do_forward)
1461 struct NeighbourMapEntry *n;
1462 struct GNUNET_TIME_Relative ret;
1464 if (NULL == neighbours)
1466 *do_forward = GNUNET_NO;
1467 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1469 if (NULL == (n = lookup_neighbour (sender)))
1471 GST_neighbours_try_connect (sender);
1472 if (NULL == (n = lookup_neighbour (sender)))
1474 GNUNET_STATISTICS_update (GST_stats,
1476 ("# messages discarded due to lack of neighbour record"),
1478 *do_forward = GNUNET_NO;
1479 return GNUNET_TIME_UNIT_ZERO;
1482 if (! test_connected (n))
1484 *do_forward = GNUNET_SYSERR;
1485 return GNUNET_TIME_UNIT_ZERO;
1487 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1489 n->quota_violation_count++;
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1492 n->in_tracker.available_bytes_per_s__,
1493 n->quota_violation_count);
1494 /* Discount 32k per violation */
1495 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1499 if (n->quota_violation_count > 0)
1501 /* try to add 32k back */
1502 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1503 n->quota_violation_count--;
1506 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1508 GNUNET_STATISTICS_update (GST_stats,
1510 ("# bandwidth quota violations by other peers"),
1512 *do_forward = GNUNET_NO;
1513 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1515 *do_forward = GNUNET_YES;
1516 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1517 if (ret.rel_value_us > 0)
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1520 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1521 (unsigned long long) n->in_tracker.
1522 consumption_since_last_update__,
1523 (unsigned int) n->in_tracker.available_bytes_per_s__,
1524 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1525 GNUNET_STATISTICS_update (GST_stats,
1526 gettext_noop ("# ms throttling suggested"),
1527 (int64_t) ret.rel_value_us / 1000LL,
1535 * Transmit a message to the given target using the active connection.
1537 * @param target destination
1538 * @param msg message to send
1539 * @param msg_size number of bytes in msg
1540 * @param timeout when to fail with timeout
1541 * @param cont function to call when done
1542 * @param cont_cls closure for 'cont'
1545 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1546 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1547 GST_NeighbourSendContinuation cont, void *cont_cls)
1549 struct NeighbourMapEntry *n;
1550 struct MessageQueue *mq;
1552 /* All ove these cases should never happen; they are all API violations.
1553 But we check anyway, just to be sure. */
1554 if (NULL == (n = lookup_neighbour (target)))
1558 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1561 if (GNUNET_YES != test_connected (n))
1565 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1568 bytes_in_send_queue += msg_size;
1569 GNUNET_STATISTICS_set (GST_stats,
1571 ("# bytes in message queue for other peers"),
1572 bytes_in_send_queue, GNUNET_NO);
1573 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1575 mq->cont_cls = cont_cls;
1576 memcpy (&mq[1], msg, msg_size);
1577 mq->message_buf = (const char *) &mq[1];
1578 mq->message_buf_size = msg_size;
1579 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1580 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1581 if ( (NULL != n->is_active) ||
1582 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1584 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1585 GNUNET_SCHEDULER_cancel (n->task);
1586 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1591 * Send a SESSION_CONNECT message via the given address.
1593 * @param na address to use
1596 send_session_connect (struct NeighbourAddress *na)
1598 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1599 struct SessionConnectMessage connect_msg;
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Sending SESSION_CONNECT message to peer %s\n",
1603 GNUNET_i2s (&na->address->peer));
1604 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1609 if (NULL == na->session)
1610 na->session = papi->get_session (papi->cls, na->address);
1611 if (NULL == na->session)
1616 GNUNET_STATISTICS_update (GST_stats,
1618 ("# SESSION_CONNECT messages sent"),
1620 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1621 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1622 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1623 connect_msg.reserved = htonl (0);
1624 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1626 papi->send (papi->cls,
1628 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1630 GNUNET_TIME_UNIT_FOREVER_REL,
1633 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1634 _("Failed to transmit CONNECT message via plugin to %s\n"),
1635 GST_plugins_a2s (na->address));
1637 GST_neighbours_notify_data_sent (&na->address->peer,
1640 sizeof (struct SessionConnectMessage));
1646 * Send a SESSION_CONNECT_ACK message via the given address.
1648 * @param address address to use
1649 * @param session session to use
1650 * @param timestamp timestamp to use for the ACK message
1653 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1654 struct Session *session,
1655 struct GNUNET_TIME_Absolute timestamp)
1657 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1658 struct SessionConnectMessage connect_msg;
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 "Sending CONNECT_ACK to peer `%s'\n",
1662 GNUNET_i2s (&address->peer));
1663 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1668 if (NULL == session)
1669 session = papi->get_session (papi->cls, address);
1670 if (NULL == session)
1675 GNUNET_STATISTICS_update (GST_stats,
1677 ("# CONNECT_ACK messages sent"),
1679 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1680 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1681 connect_msg.reserved = htonl (0);
1682 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1683 (void) papi->send (papi->cls,
1685 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1687 GNUNET_TIME_UNIT_FOREVER_REL,
1694 * Create a fresh entry in the neighbour map for the given peer
1696 * @param peer peer to create an entry for
1697 * @return new neighbour map entry
1699 static struct NeighbourMapEntry *
1700 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1702 struct NeighbourMapEntry *n;
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "Creating new neighbour entry for `%s'\n",
1707 n = GNUNET_new (struct NeighbourMapEntry);
1709 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1710 n->last_util_transmission = GNUNET_TIME_absolute_get();
1711 n->util_payload_bytes_recv = 0;
1712 n->util_payload_bytes_sent = 0;
1713 n->util_total_bytes_recv = 0;
1714 n->util_total_bytes_sent = 0;
1715 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1716 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1717 MAX_BANDWIDTH_CARRY_S);
1718 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1719 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
1720 GNUNET_assert (GNUNET_OK ==
1721 GNUNET_CONTAINER_multipeermap_put (neighbours,
1723 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1729 * Check if the two given addresses are the same.
1730 * Actually only checks if the sessions are non-NULL
1731 * (which they should be) and then if they are identical;
1732 * the actual addresses don't matter if the session
1733 * pointers match anyway, and we must have session pointers
1736 * @param a1 first address to compare
1737 * @param a2 other address to compare
1738 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1741 address_matches (const struct NeighbourAddress *a1,
1742 const struct NeighbourAddress *a2)
1744 if ( (NULL == a1->session) ||
1745 (NULL == a2->session) )
1750 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1755 address_suggest_cont (void *cls,
1756 const struct GNUNET_PeerIdentity *peer,
1757 const struct GNUNET_HELLO_Address *address, struct Session *session,
1758 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1759 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1760 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
1767 * Try to create a connection to the given target (eventually).
1769 * @param target peer to try to connect to
1772 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1774 struct NeighbourMapEntry *n;
1776 if (NULL == neighbours)
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1779 "Asked to connect to peer `%s' during shutdown\n",
1780 GNUNET_i2s (target));
1781 return; /* during shutdown, do nothing */
1783 n = lookup_neighbour (target);
1784 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1785 "Asked to connect to peer `%s' (state: %s)\n",
1786 GNUNET_i2s (target),
1787 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
1792 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1793 /* this should not be possible */
1795 free_neighbour (n, GNUNET_NO);
1797 case GNUNET_TRANSPORT_PS_INIT_ATS:
1798 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1799 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1800 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1801 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1802 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1803 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1804 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1805 "Ignoring request to try to connect to `%s', already trying!\n",
1806 GNUNET_i2s (target));
1807 return; /* already trying */
1808 case GNUNET_TRANSPORT_PS_CONNECTED:
1809 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1810 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1811 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1812 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1813 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1814 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1815 "Ignoring request to try to connect, already connected to `%s'!\n",
1816 GNUNET_i2s (target));
1817 return; /* already connected */
1818 case GNUNET_TRANSPORT_PS_DISCONNECT:
1819 /* get rid of remains, ready to re-try immediately */
1820 free_neighbour (n, GNUNET_NO);
1822 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1823 /* should not be possible */
1826 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1827 "Unhandled state `%s'\n",
1828 GNUNET_TRANSPORT_ps2s (n->state));
1830 free_neighbour (n, GNUNET_NO);
1834 n = setup_neighbour (target);
1835 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1837 GNUNET_ATS_reset_backoff (GST_ats, target);
1838 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target, &address_suggest_cont, n);
1843 * Function called with the result of a blacklist check.
1845 * @param cls closure with the `struct BlackListCheckContext`
1846 * @param peer peer this check affects
1847 * @param result #GNUNET_OK if the address is allowed
1850 handle_test_blacklist_cont (void *cls,
1851 const struct GNUNET_PeerIdentity *peer,
1854 struct BlackListCheckContext *bcc = cls;
1855 struct NeighbourMapEntry *n;
1858 GNUNET_CONTAINER_DLL_remove (bc_head,
1861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1862 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1864 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1865 if (GNUNET_OK == result)
1867 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
1871 /* Blacklist disagreed on connecting to a peer with this address
1872 * Destroy address because we are not allowed to use it
1874 if (NULL != bcc->na.session)
1875 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1876 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1878 if (NULL == (n = lookup_neighbour (peer)))
1880 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1881 "No neighbor entry for peer `%s', ignoring blacklist result\n",
1883 goto cleanup; /* nobody left to care about new address */
1885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1886 "Received blacklist result for peer `%s' in state %s/%d\n",
1888 GNUNET_TRANSPORT_ps2s (n->state),
1889 n->send_connect_ack);
1892 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1893 /* this should not be possible */
1895 free_neighbour (n, GNUNET_NO);
1897 case GNUNET_TRANSPORT_PS_INIT_ATS:
1898 /* waiting on ATS suggestion; still, pass address to ATS as a
1901 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1902 /* check if the address the blacklist was fine with matches
1903 ATS suggestion, if so, we can move on! */
1904 if ( (GNUNET_OK == result) &&
1905 (1 == n->send_connect_ack) )
1907 n->send_connect_ack = 2;
1908 send_session_connect_ack_message (bcc->na.address,
1910 n->connect_ack_timestamp);
1912 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1915 "Blacklist result for peer %s is for non-primary address, ignored\n",
1917 break; /* result for an address we currently don't care about */
1919 if (GNUNET_OK == result)
1921 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT, GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
1922 send_session_connect (&n->primary_address);
1926 free_address (&n->primary_address);
1927 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1930 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1931 /* waiting on CONNECT_ACK, send ACK if one is pending */
1932 if ( (GNUNET_OK == result) &&
1933 (1 == n->send_connect_ack) )
1935 n->send_connect_ack = 2;
1936 send_session_connect_ack_message (n->primary_address.address,
1937 n->primary_address.session,
1938 n->connect_ack_timestamp);
1941 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1942 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1943 GNUNET_ATS_reset_backoff (GST_ats, peer);
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1945 "Suggesting address for peer %s to ATS\n",
1947 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer, &address_suggest_cont, n);
1949 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1950 /* waiting on ATS suggestion, don't care about blacklist */
1952 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1953 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1955 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1956 "Blacklist result ignored, as it is not for our primary address\n");
1957 break; /* result for an address we currently don't care about */
1959 if (GNUNET_OK == result)
1961 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK, GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
1962 send_session_connect_ack_message (bcc->na.address,
1964 n->connect_ack_timestamp);
1965 if (1 == n->send_connect_ack)
1966 n->send_connect_ack = 2;
1970 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
1972 plugin = GST_plugins_find (bcc->na.address->transport_name);
1973 if ( (NULL != plugin) &&
1974 (NULL != bcc->na.session) )
1976 plugin->disconnect_session (plugin->cls,
1980 GNUNET_break (NULL != plugin);
1981 free_address (&n->primary_address);
1982 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1983 GNUNET_ATS_reset_backoff (GST_ats, peer);
1986 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1987 /* waiting on SESSION_ACK, send ACK if one is pending */
1988 if ( (GNUNET_OK == result) &&
1989 (1 == n->send_connect_ack) )
1991 n->send_connect_ack = 2;
1992 send_session_connect_ack_message (n->primary_address.address,
1993 n->primary_address.session,
1994 n->connect_ack_timestamp);
1997 case GNUNET_TRANSPORT_PS_CONNECTED:
1998 /* already connected, don't care about blacklist */
2000 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2001 /* still waiting on ATS suggestion, don't care about blacklist */
2003 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2004 if ( (GNUNET_OK == result) &&
2005 (1 == n->send_connect_ack) )
2007 n->send_connect_ack = 2;
2008 send_session_connect_ack_message (bcc->na.address,
2010 n->connect_ack_timestamp);
2012 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2015 "Blacklist result ignored, as it is not for our primary address\n");
2016 break; /* result for an address we currently don't care about */
2018 if (GNUNET_OK == result)
2020 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2021 send_session_connect (&n->primary_address);
2025 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2028 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2029 /* waiting on CONNECT_ACK, don't care about blacklist */
2030 if ( (GNUNET_OK == result) &&
2031 (1 == n->send_connect_ack) )
2033 n->send_connect_ack = 2;
2034 send_session_connect_ack_message (n->primary_address.address,
2035 n->primary_address.session,
2036 n->connect_ack_timestamp);
2039 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2040 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2042 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2043 "Blacklist result ignored, as it is not for our primary address\n");
2044 break; /* result for an address we currently don't care about */
2046 if (GNUNET_OK == result)
2048 send_session_connect (&n->alternative_address);
2049 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2053 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2054 free_address (&n->alternative_address);
2057 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2058 /* waiting on CONNECT_ACK, don't care about blacklist */
2059 if ( (GNUNET_OK == result) &&
2060 (1 == n->send_connect_ack) )
2062 n->send_connect_ack = 2;
2063 send_session_connect_ack_message (n->primary_address.address,
2064 n->primary_address.session,
2065 n->connect_ack_timestamp);
2068 case GNUNET_TRANSPORT_PS_DISCONNECT:
2069 /* Nothing to do here, ATS will already do what can be done */
2071 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2072 /* should not be possible */
2076 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2077 "Unhandled state `%s'\n",
2078 GNUNET_TRANSPORT_ps2s (n->state));
2080 free_neighbour (n, GNUNET_NO);
2084 GNUNET_HELLO_address_free (bcc->na.address);
2090 * We want to know if connecting to a particular peer via
2091 * a particular address is allowed. Check it!
2093 * @param peer identity of the peer to switch the address for
2094 * @param ts time at which the check was initiated
2095 * @param address address of the other peer, NULL if other peer
2097 * @param session session to use (or NULL)
2100 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2101 struct GNUNET_TIME_Absolute ts,
2102 const struct GNUNET_HELLO_Address *address,
2103 struct Session *session)
2105 struct BlackListCheckContext *bcc;
2106 struct GST_BlacklistCheck *bc;
2108 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2109 "Checking peer `%s' against blacklist\n",
2111 bcc = GNUNET_new (struct BlackListCheckContext);
2112 bcc->na.address = GNUNET_HELLO_address_copy (address);
2113 bcc->na.session = session;
2114 bcc->na.connect_timestamp = ts;
2115 GNUNET_CONTAINER_DLL_insert (bc_head,
2118 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2119 address->transport_name,
2120 &handle_test_blacklist_cont, bcc)))
2122 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2123 we must only store 'bc' if 'bc' is non-NULL... */
2128 * We received a 'SESSION_CONNECT' message from the other peer.
2129 * Consider switching to it.
2131 * @param message possibly a 'struct SessionConnectMessage' (check format)
2132 * @param peer identity of the peer to switch the address for
2133 * @param address address of the other peer, NULL if other peer
2135 * @param session session to use (or NULL)
2136 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2139 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2140 const struct GNUNET_PeerIdentity *peer,
2141 const struct GNUNET_HELLO_Address *address,
2142 struct Session *session)
2144 const struct SessionConnectMessage *scm;
2145 struct NeighbourMapEntry *n;
2146 struct GNUNET_TIME_Absolute ts;
2148 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2149 "Received CONNECT message from peer `%s'\n",
2151 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2153 GNUNET_break_op (0);
2154 return GNUNET_SYSERR;
2156 GNUNET_STATISTICS_update (GST_stats,
2158 ("# CONNECT messages received"),
2160 if (NULL == neighbours)
2162 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2163 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2165 return GNUNET_OK; /* we're shutting down */
2167 scm = (const struct SessionConnectMessage *) message;
2168 GNUNET_break_op (0 == ntohl (scm->reserved));
2169 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2170 n = lookup_neighbour (peer);
2172 n = setup_neighbour (peer);
2173 n->send_connect_ack = 1;
2174 n->connect_ack_timestamp = ts;
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2177 "Received SESSION_CONNECT for peer `%s' in state %s/%d\n",
2179 GNUNET_TRANSPORT_ps2s (n->state),
2180 n->send_connect_ack);
2183 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2184 /* Do a blacklist check for the new address */
2185 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2186 check_blacklist (peer, ts, address, session);
2188 case GNUNET_TRANSPORT_PS_INIT_ATS:
2189 /* CONNECT message takes priority over us asking ATS for address */
2190 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2192 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2193 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2194 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2195 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2196 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2197 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2198 /* It can never hurt to have an alternative address in the above cases,
2199 see if it is allowed */
2200 check_blacklist (peer, ts, address, session);
2202 case GNUNET_TRANSPORT_PS_CONNECTED:
2203 /* we are already connected and can thus send the ACK immediately;
2204 still, it can never hurt to have an alternative address, so also
2205 tell ATS about it */
2206 GNUNET_assert (NULL != n->primary_address.address);
2207 GNUNET_assert (NULL != n->primary_address.session);
2208 n->send_connect_ack = 0;
2209 send_session_connect_ack_message (n->primary_address.address,
2210 n->primary_address.session, ts);
2211 check_blacklist (peer, ts, address, session);
2213 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2214 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2215 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2216 /* It can never hurt to have an alternative address in the above cases,
2217 see if it is allowed */
2218 check_blacklist (peer, ts, address, session);
2220 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2221 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2222 /* we are already connected and can thus send the ACK immediately;
2223 still, it can never hurt to have an alternative address, so also
2224 tell ATS about it */
2225 GNUNET_assert (NULL != n->primary_address.address);
2226 GNUNET_assert (NULL != n->primary_address.session);
2227 n->send_connect_ack = 0;
2228 send_session_connect_ack_message (n->primary_address.address,
2229 n->primary_address.session, ts);
2230 check_blacklist (peer, ts, address, session);
2232 case GNUNET_TRANSPORT_PS_DISCONNECT:
2233 /* get rid of remains without terminating sessions, ready to re-try */
2234 free_neighbour (n, GNUNET_YES);
2235 n = setup_neighbour (peer);
2236 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2237 GNUNET_ATS_reset_backoff (GST_ats, peer);
2239 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2240 /* should not be possible */
2244 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2245 "Unhandled state `%s'\n",
2246 GNUNET_TRANSPORT_ps2s (n->state));
2248 return GNUNET_SYSERR;
2253 struct BlacklistCheckSwitchContext
2255 struct BlacklistCheckSwitchContext *prev;
2256 struct BlacklistCheckSwitchContext *next;
2258 struct GNUNET_HELLO_Address *address;
2259 struct Session *session;
2260 struct GNUNET_ATS_Information *ats;
2263 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
2264 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
2268 switch_address_bl_check_cont (void *cls,
2269 const struct GNUNET_PeerIdentity *peer, int result)
2271 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2272 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2273 struct NeighbourMapEntry *n;
2276 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2277 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2282 "Peer %s is unknown, suggestion ignored\n",
2285 if (result == GNUNET_NO)
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2288 "Blacklist denied to switch to suggested address `%s' sesion %p for peer `%s'\n",
2289 GST_plugins_a2s (blc_ctx->address),
2291 GNUNET_i2s (&blc_ctx->address->peer));
2293 /* Delete address (or session if existing) in ATS */
2294 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2296 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2297 GNUNET_HELLO_address_free(blc_ctx->address);
2298 GNUNET_free_non_null (blc_ctx->ats);
2299 GNUNET_free (blc_ctx);
2303 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2304 "Blacklist accepted to switch to suggested address `%s' for peer `%s'\n",
2305 GST_plugins_a2s (blc_ctx->address),
2307 GNUNET_i2s (&blc_ctx->address->peer));
2309 if (NULL == blc_ctx->session)
2311 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2312 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2313 "Obtained new session for peer `%s' and address '%s': %p\n",
2314 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2316 if (NULL == blc_ctx->session)
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319 "Failed to obtain new session for peer `%s' and address '%s'\n",
2320 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address));
2321 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2326 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2328 free_neighbour (n, GNUNET_NO);
2330 case GNUNET_TRANSPORT_PS_INIT_ATS:
2331 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2332 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2333 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_BLACKLIST,
2334 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2335 check_blacklist (&n->id, n->connect_ack_timestamp,
2336 blc_ctx->address, blc_ctx->session);
2338 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2339 /* ATS suggests a different address, switch again */
2340 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2341 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2342 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2343 check_blacklist (&n->id, n->connect_ack_timestamp,
2344 blc_ctx->address, blc_ctx->session);
2346 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2347 /* ATS suggests a different address, switch again */
2348 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2349 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2350 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_BLACKLIST,
2351 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2352 check_blacklist (&n->id, n->connect_ack_timestamp,
2353 blc_ctx->address, blc_ctx->session);
2355 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2356 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2357 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2358 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2359 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2360 check_blacklist (&n->id, n->connect_ack_timestamp,
2361 blc_ctx->address, blc_ctx->session);
2363 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2364 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2365 check_blacklist (&n->id, n->connect_ack_timestamp,
2366 blc_ctx->address, blc_ctx->session);
2368 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2369 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2370 /* ATS asks us to switch while we were trying to connect; switch to new
2371 address and check blacklist again */
2372 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2373 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2374 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2375 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2376 check_blacklist (&n->id, n->connect_ack_timestamp,
2377 blc_ctx->address, blc_ctx->session);
2379 case GNUNET_TRANSPORT_PS_CONNECTED:
2380 GNUNET_assert (NULL != n->primary_address.address);
2381 GNUNET_assert (NULL != n->primary_address.session);
2382 if (n->primary_address.session == blc_ctx->session)
2384 /* not an address change, just a quota change */
2385 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2386 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2389 /* ATS asks us to switch a life connection; see if we can get
2390 a CONNECT_ACK on it before we actually do this! */
2391 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2392 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2393 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2394 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2395 blc_ctx->address, blc_ctx->session);
2397 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2398 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2399 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2400 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2401 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2402 check_blacklist (&n->id, n->connect_ack_timestamp,
2403 blc_ctx->address, blc_ctx->session);
2405 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2406 /* ATS asks us to switch while we were trying to reconnect; switch to new
2407 address and check blacklist again */
2408 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2409 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2410 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2411 check_blacklist (&n->id, n->connect_ack_timestamp,
2412 blc_ctx->address, blc_ctx->session);
2414 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2415 /* ATS asks us to switch while we were trying to reconnect; switch to new
2416 address and check blacklist again */
2417 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2418 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2419 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2420 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2421 check_blacklist (&n->id, n->connect_ack_timestamp,
2422 blc_ctx->address, blc_ctx->session);
2424 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2425 if (n->primary_address.session == blc_ctx->session)
2427 /* ATS switches back to still-active session */
2428 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2429 free_address (&n->alternative_address);
2432 /* ATS asks us to switch a life connection, update blacklist check */
2433 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2434 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2435 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2436 blc_ctx->address, blc_ctx->session);
2438 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2439 if (n->primary_address.session == blc_ctx->session)
2441 /* ATS switches back to still-active session */
2442 free_address (&n->alternative_address);
2443 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2446 /* ATS asks us to switch a life connection, update blacklist check */
2447 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2448 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2449 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2450 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2451 blc_ctx->address, blc_ctx->session);
2453 case GNUNET_TRANSPORT_PS_DISCONNECT:
2454 /* not going to switch addresses while disconnecting */
2456 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2460 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2461 "Unhandled state `%s'\n",
2462 GNUNET_TRANSPORT_ps2s (n->state));
2467 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2468 GNUNET_HELLO_address_free(blc_ctx->address);
2469 GNUNET_free_non_null (blc_ctx->ats);
2470 GNUNET_free (blc_ctx);
2476 * For the given peer, switch to this address.
2478 * Before accepting this addresses and actively using it, a blacklist check
2479 * is performed. If this blacklist check fails the address will be destroyed.
2481 * @param peer identity of the peer to switch the address for
2482 * @param address address of the other peer,
2483 * @param session session to use or NULL if transport should initiate a session
2484 * @param ats performance data
2485 * @param ats_count number of entries in ats
2486 * @param bandwidth_in inbound quota to be used when connection is up,
2487 * 0 to disconnect from peer
2488 * @param bandwidth_out outbound quota to be used when connection is up,
2489 * 0 to disconnect from peer
2492 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2493 const struct GNUNET_HELLO_Address *address,
2494 struct Session *session,
2495 const struct GNUNET_ATS_Information *ats,
2497 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2498 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2500 struct NeighbourMapEntry *n;
2501 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2502 struct BlacklistCheckSwitchContext *blc_ctx;
2505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2506 "ATS has decided on an address for peer %s\n",
2508 GNUNET_assert (NULL != address->transport_name);
2509 if (NULL == (n = lookup_neighbour (peer)))
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2512 "Peer %s is unknown, suggestion ignored\n",
2517 /* Obtain an session for this address from plugin */
2518 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2520 /* we don't have the plugin for this address */
2521 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2522 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2523 address->transport_name,
2525 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2528 if ((NULL == session) &&
2529 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2531 /* This is a inbound address and we do not have a session to use! */
2532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2533 "Inbound address without session `%s'! Destroying address...\n",
2534 GST_plugins_a2s (address));
2535 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2539 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2540 "ATS tells us to switch to %s address '%s' session %p for "
2541 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2542 GNUNET_HELLO_address_check_option (address,
2543 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "",
2544 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2545 GNUNET_TRANSPORT_ps2s (n->state), n->send_connect_ack,
2546 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2548 /* Perform blacklist check */
2549 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2550 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2551 blc_ctx->session = session;
2552 blc_ctx->bandwidth_in = bandwidth_in;
2553 blc_ctx->bandwidth_out = bandwidth_out;
2554 blc_ctx->ats_count = ats_count;
2555 blc_ctx->ats = NULL;
2558 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2559 for (c = 0; c < ats_count; c++)
2561 blc_ctx->ats[c].type = ats[c].type;
2562 blc_ctx->ats[c].value = ats[c].value;
2566 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2567 GST_blacklist_test_allowed (peer, address->transport_name,
2568 &switch_address_bl_check_cont, blc_ctx);
2573 send_utilization_data (void *cls,
2574 const struct GNUNET_PeerIdentity *key,
2577 struct NeighbourMapEntry *n = value;
2578 struct GNUNET_ATS_Information atsi[4];
2580 uint32_t bps_pl_out;
2583 struct GNUNET_TIME_Relative delta;
2585 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2586 GNUNET_TIME_absolute_get ());
2590 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2591 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2593 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2594 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2596 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2601 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2602 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2604 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2605 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2613 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2614 atsi[0].value = htonl (bps_out);
2615 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2616 atsi[1].value = htonl (bps_in);
2618 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2619 atsi[2].value = htonl (bps_pl_out);
2620 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2621 atsi[3].value = htonl (bps_pl_in);
2623 GST_ats_update_metrics (key, n->primary_address.address,
2624 n->primary_address.session, atsi, 4);
2625 n->util_payload_bytes_recv = 0;
2626 n->util_payload_bytes_sent = 0;
2627 n->util_total_bytes_recv = 0;
2628 n->util_total_bytes_sent = 0;
2629 n->last_util_transmission = GNUNET_TIME_absolute_get();
2635 * Task transmitting utilization in a regular interval
2637 * @param cls the 'struct NeighbourMapEntry' for which we are running
2638 * @param tc scheduler context (unused)
2641 utilization_transmission (void *cls,
2642 const struct GNUNET_SCHEDULER_TaskContext *tc)
2644 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2646 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2647 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2649 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2650 utilization_transmission, NULL);
2656 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2657 const struct GNUNET_HELLO_Address *address,
2658 struct Session *session,
2659 const struct GNUNET_MessageHeader *message)
2661 struct NeighbourMapEntry *n;
2663 n = lookup_neighbour (peer);
2666 n->util_total_bytes_recv += ntohs(message->size);
2671 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2672 const struct GNUNET_HELLO_Address *address,
2673 struct Session *session,
2674 const struct GNUNET_MessageHeader *message)
2676 struct NeighbourMapEntry *n;
2677 n = lookup_neighbour (peer);
2680 n->util_payload_bytes_recv += ntohs(message->size);
2685 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2686 const struct GNUNET_HELLO_Address *address,
2687 struct Session *session,
2690 struct NeighbourMapEntry *n;
2691 n = lookup_neighbour (peer);
2694 if (n->primary_address.session != session)
2696 n->util_total_bytes_sent += size;
2701 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2704 struct NeighbourMapEntry *n;
2705 n = lookup_neighbour (peer);
2708 n->util_payload_bytes_sent += size;
2713 * Master task run for every neighbour. Performs all of the time-related
2714 * activities (keep alive, send next message, disconnect if idle, finish
2715 * clean up after disconnect).
2717 * @param cls the 'struct NeighbourMapEntry' for which we are running
2718 * @param tc scheduler context (unused)
2721 master_task (void *cls,
2722 const struct GNUNET_SCHEDULER_TaskContext *tc)
2724 struct NeighbourMapEntry *n = cls;
2725 struct GNUNET_TIME_Relative delay;
2727 n->task = GNUNET_SCHEDULER_NO_TASK;
2728 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2730 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2731 GNUNET_i2s (&n->id),
2732 GNUNET_TRANSPORT_ps2s(n->state),
2733 GNUNET_STRINGS_relative_time_to_string (delay,
2737 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2738 /* invalid state for master task, clean up */
2740 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2741 free_neighbour (n, GNUNET_NO);
2743 case GNUNET_TRANSPORT_PS_INIT_ATS:
2744 if (0 == delay.rel_value_us)
2746 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2747 "Connection to `%s' timed out waiting for ATS to provide address\n",
2748 GNUNET_i2s (&n->id));
2749 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2750 free_neighbour (n, GNUNET_NO);
2754 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2755 if (0 == delay.rel_value_us)
2757 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2758 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2759 GNUNET_i2s (&n->id));
2760 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2761 free_neighbour (n, GNUNET_NO);
2765 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2766 if (0 == delay.rel_value_us)
2768 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2769 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2770 GNUNET_i2s (&n->id));
2771 /* We could not send to this address, delete address and session */
2772 if (NULL != n->primary_address.session)
2773 GNUNET_ATS_address_destroyed (GST_ats,
2774 n->primary_address.address, n->primary_address.session);
2775 GNUNET_ATS_address_destroyed (GST_ats,
2776 n->primary_address.address, NULL);
2777 disconnect_neighbour (n);
2781 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2782 if (0 == delay.rel_value_us)
2784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2785 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2786 GNUNET_i2s (&n->id));
2787 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2788 free_neighbour (n, GNUNET_NO);
2792 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2793 if (0 == delay.rel_value_us)
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2796 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2797 GNUNET_i2s (&n->id));
2798 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2799 free_neighbour (n, GNUNET_NO);
2803 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2804 if (0 == delay.rel_value_us)
2806 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2807 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2808 GNUNET_i2s (&n->id));
2809 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2810 free_neighbour (n, GNUNET_NO);
2814 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2815 if (0 == delay.rel_value_us)
2817 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2818 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2819 GNUNET_i2s (&n->id));
2820 disconnect_neighbour (n);
2824 case GNUNET_TRANSPORT_PS_CONNECTED:
2825 if (0 == delay.rel_value_us)
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2828 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2829 GNUNET_i2s (&n->id));
2830 disconnect_neighbour (n);
2833 try_transmission_to_peer (n);
2836 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2837 if (0 == delay.rel_value_us)
2839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2840 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2841 GNUNET_i2s (&n->id));
2842 disconnect_neighbour (n);
2846 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2847 if (0 == delay.rel_value_us)
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2850 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2851 GNUNET_i2s (&n->id));
2852 disconnect_neighbour (n);
2856 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2857 if (0 == delay.rel_value_us)
2859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2860 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2861 GNUNET_i2s (&n->id));
2862 disconnect_neighbour (n);
2866 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2867 if (0 == delay.rel_value_us)
2869 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2870 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2871 GNUNET_i2s (&n->id));
2872 disconnect_neighbour (n);
2875 try_transmission_to_peer (n);
2878 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2879 if (0 == delay.rel_value_us)
2881 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2882 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2883 GNUNET_i2s (&n->id));
2884 disconnect_neighbour (n);
2887 try_transmission_to_peer (n);
2890 case GNUNET_TRANSPORT_PS_DISCONNECT:
2891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2892 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2893 GNUNET_i2s (&n->id));
2894 free_neighbour (n, GNUNET_NO);
2896 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2897 /* how did we get here!? */
2901 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2902 "Unhandled state `%s'\n",
2903 GNUNET_TRANSPORT_ps2s (n->state));
2907 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2908 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2909 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
2911 /* if we are *now* in one of these three states, we're sending
2912 keep alive messages, so we need to consider the keepalive
2913 delay, not just the connection timeout */
2914 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2917 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2918 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2925 * Send a SESSION_ACK message to the neighbour to confirm that we
2926 * got his CONNECT_ACK.
2928 * @param n neighbour to send the SESSION_ACK to
2931 send_session_ack_message (struct NeighbourMapEntry *n)
2933 struct GNUNET_MessageHeader msg;
2935 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2936 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2937 (void) send_with_session(n,
2938 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2939 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
2945 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2946 * Consider switching to it.
2948 * @param message possibly a 'struct SessionConnectMessage' (check format)
2949 * @param peer identity of the peer to switch the address for
2950 * @param address address of the other peer, NULL if other peer
2952 * @param session session to use (or NULL)
2953 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2956 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2957 const struct GNUNET_PeerIdentity *peer,
2958 const struct GNUNET_HELLO_Address *address,
2959 struct Session *session)
2961 const struct SessionConnectMessage *scm;
2962 struct GNUNET_TIME_Absolute ts;
2963 struct NeighbourMapEntry *n;
2965 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2966 "Received CONNECT_ACK message from peer `%s'\n",
2969 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2971 GNUNET_break_op (0);
2972 return GNUNET_SYSERR;
2974 GNUNET_STATISTICS_update (GST_stats,
2976 ("# CONNECT_ACK messages received"),
2978 scm = (const struct SessionConnectMessage *) message;
2979 GNUNET_break_op (ntohl (scm->reserved) == 0);
2980 if (NULL == (n = lookup_neighbour (peer)))
2982 GNUNET_STATISTICS_update (GST_stats,
2984 ("# unexpected CONNECT_ACK messages (no peer)"),
2986 return GNUNET_SYSERR;
2988 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2991 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2993 free_neighbour (n, GNUNET_NO);
2994 return GNUNET_SYSERR;
2995 case GNUNET_TRANSPORT_PS_INIT_ATS:
2996 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2997 GNUNET_STATISTICS_update (GST_stats,
2999 ("# unexpected CONNECT_ACK messages (not ready)"),
3002 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3003 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3005 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3006 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3009 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3010 GNUNET_STATISTICS_set (GST_stats,
3011 gettext_noop ("# peers connected"),
3012 ++neighbours_connected,
3014 connect_notify_cb (callback_cls, &n->id,
3015 n->primary_address.bandwidth_in,
3016 n->primary_address.bandwidth_out);
3017 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3018 GST_ats_add_address (n->primary_address.address,
3019 n->primary_address.session,
3021 set_primary_address (n,
3022 n->primary_address.address,
3023 n->primary_address.session,
3024 n->primary_address.bandwidth_in,
3025 n->primary_address.bandwidth_out,
3027 send_session_ack_message (n);
3029 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3030 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3031 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3032 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3033 GNUNET_STATISTICS_update (GST_stats,
3035 ("# unexpected CONNECT_ACK messages (not ready)"),
3038 case GNUNET_TRANSPORT_PS_CONNECTED:
3039 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3040 send_session_ack_message (n);
3042 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3043 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3044 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3045 to give us a new address... */
3046 GNUNET_STATISTICS_update (GST_stats,
3048 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3051 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3052 /* new address worked; go back to connected! */
3053 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3054 send_session_ack_message (n);
3056 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3057 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3058 send_session_ack_message (n);
3060 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3061 /* new address worked; adopt it and go back to connected! */
3062 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3063 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3065 GST_ats_add_address (n->alternative_address.address,
3066 n->alternative_address.session,
3068 set_primary_address (n, n->alternative_address.address,
3069 n->alternative_address.session, n->alternative_address.bandwidth_in,
3070 n->alternative_address.bandwidth_out, GNUNET_YES);
3072 free_address (&n->alternative_address);
3073 send_session_ack_message (n);
3075 case GNUNET_TRANSPORT_PS_DISCONNECT:
3076 GNUNET_STATISTICS_update (GST_stats,
3078 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3080 return GNUNET_SYSERR;
3081 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3085 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3086 "Unhandled state `%s'\n",
3087 GNUNET_TRANSPORT_ps2s (n->state));
3089 return GNUNET_SYSERR;
3096 * A session was terminated. Take note; if needed, try to get
3097 * an alternative address from ATS.
3099 * @param peer identity of the peer where the session died
3100 * @param session session that is gone
3101 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3102 * this session was not in use
3105 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3106 struct Session *session)
3108 struct NeighbourMapEntry *n;
3109 struct BlackListCheckContext *bcc;
3110 struct BlackListCheckContext *bcc_next;
3112 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3114 while (NULL != (bcc = bcc_next))
3116 bcc_next = bcc->next;
3117 if (bcc->na.session == session)
3119 if (NULL != bcc->bc)
3120 GST_blacklist_test_cancel (bcc->bc);
3121 GNUNET_HELLO_address_free (bcc->na.address);
3122 GNUNET_CONTAINER_DLL_remove (bc_head,
3128 if (NULL == (n = lookup_neighbour (peer)))
3129 return GNUNET_NO; /* can't affect us */
3130 if (session != n->primary_address.session)
3132 if (session == n->alternative_address.session)
3134 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3135 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3136 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3139 free_address (&n->alternative_address);
3141 return GNUNET_NO; /* doesn't affect us further */
3144 n->expect_latency_response = GNUNET_NO;
3147 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3149 free_neighbour (n, GNUNET_NO);
3151 case GNUNET_TRANSPORT_PS_INIT_ATS:
3153 free_neighbour (n, GNUNET_NO);
3155 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3156 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3157 free_address (&n->primary_address);
3158 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3160 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3161 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3162 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3163 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3164 /* error on inbound session; free neighbour entirely */
3165 free_address (&n->primary_address);
3166 free_neighbour (n, GNUNET_NO);
3168 case GNUNET_TRANSPORT_PS_CONNECTED:
3169 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3170 free_address (&n->primary_address);
3172 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3173 /* we don't have an address, how can it go down? */
3176 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3177 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3178 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3180 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3181 /* primary went down while we were checking secondary against
3182 blacklist, adopt secondary as primary */
3183 free_address (&n->primary_address);
3184 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3185 n->primary_address = n->alternative_address;
3186 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3188 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3189 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3190 secondary as primary */
3191 free_address (&n->primary_address);
3192 n->primary_address = n->alternative_address;
3193 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3194 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3196 case GNUNET_TRANSPORT_PS_DISCONNECT:
3197 free_address (&n->primary_address);
3199 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3200 /* neighbour was freed and plugins told to terminate session */
3204 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3205 "Unhandled state `%s'\n",
3206 GNUNET_TRANSPORT_ps2s (n->state));
3210 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3211 GNUNET_SCHEDULER_cancel (n->task);
3212 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3218 * We received a 'SESSION_ACK' message from the other peer.
3219 * If we sent a 'CONNECT_ACK' last, this means we are now
3220 * connected. Otherwise, do nothing.
3222 * @param message possibly a 'struct SessionConnectMessage' (check format)
3223 * @param peer identity of the peer to switch the address for
3224 * @param address address of the other peer, NULL if other peer
3226 * @param session session to use (or NULL)
3227 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3230 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3231 const struct GNUNET_PeerIdentity *peer,
3232 const struct GNUNET_HELLO_Address *address,
3233 struct Session *session)
3235 struct NeighbourMapEntry *n;
3237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3238 "Received SESSION_ACK message from peer `%s'\n",
3240 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3242 GNUNET_break_op (0);
3243 return GNUNET_SYSERR;
3245 GNUNET_STATISTICS_update (GST_stats,
3247 ("# SESSION_ACK messages received"),
3249 if (NULL == (n = lookup_neighbour (peer)))
3251 GNUNET_break_op (0);
3252 return GNUNET_SYSERR;
3254 /* check if we are in a plausible state for having sent
3255 a CONNECT_ACK. If not, return, otherwise break */
3256 if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3257 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3258 (2 != n->send_connect_ack) )
3260 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3261 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3263 GNUNET_TRANSPORT_ps2s (n->state),
3264 n->send_connect_ack);
3265 GNUNET_STATISTICS_update (GST_stats,
3266 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3270 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3271 GNUNET_STATISTICS_set (GST_stats,
3272 gettext_noop ("# peers connected"),
3273 ++neighbours_connected,
3275 connect_notify_cb (callback_cls, &n->id,
3276 n->primary_address.bandwidth_in,
3277 n->primary_address.bandwidth_out);
3279 GST_ats_add_address (n->primary_address.address,
3280 n->primary_address.session,
3282 set_primary_address (n,
3283 n->primary_address.address,
3284 n->primary_address.session,
3285 n->primary_address.bandwidth_in,
3286 n->primary_address.bandwidth_out,
3293 * Test if we're connected to the given peer.
3295 * @param target peer to test
3296 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3299 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3301 return test_connected (lookup_neighbour (target));
3306 * Change the incoming quota for the given peer.
3308 * @param neighbour identity of peer to change qutoa for
3309 * @param quota new quota
3312 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3313 struct GNUNET_BANDWIDTH_Value32NBO quota)
3315 struct NeighbourMapEntry *n;
3317 if (NULL == (n = lookup_neighbour (neighbour)))
3319 GNUNET_STATISTICS_update (GST_stats,
3321 ("# SET QUOTA messages ignored (no such peer)"),
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3326 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3327 ntohl (quota.value__), GNUNET_i2s (&n->id));
3328 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3329 if (0 != ntohl (quota.value__))
3331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3332 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3333 GNUNET_i2s (&n->id));
3334 if (GNUNET_YES == test_connected (n))
3335 GNUNET_STATISTICS_update (GST_stats,
3336 gettext_noop ("# disconnects due to quota of 0"),
3338 disconnect_neighbour (n);
3343 * We received a disconnect message from the given peer,
3344 * validate and process.
3346 * @param peer sender of the message
3347 * @param msg the disconnect message
3350 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3351 const struct GNUNET_MessageHeader *msg)
3353 struct NeighbourMapEntry *n;
3354 const struct SessionDisconnectMessage *sdm;
3356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3357 "Received DISCONNECT message from peer `%s'\n",
3359 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3361 // GNUNET_break_op (0);
3362 GNUNET_STATISTICS_update (GST_stats,
3364 ("# disconnect messages ignored (old format)"), 1,
3368 GNUNET_STATISTICS_update (GST_stats,
3370 ("# DISCONNECT messages received"),
3372 sdm = (const struct SessionDisconnectMessage *) msg;
3373 if (NULL == (n = lookup_neighbour (peer)))
3374 return; /* gone already */
3375 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3377 GNUNET_STATISTICS_update (GST_stats,
3379 ("# disconnect messages ignored (timestamp)"), 1,
3383 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3385 GNUNET_break_op (0);
3388 if (ntohl (sdm->purpose.size) !=
3389 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3390 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3391 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3393 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3394 "%s message from peer `%s' has invalid size \n",
3397 GNUNET_break_op (0);
3401 GNUNET_CRYPTO_eddsa_verify
3402 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3403 &sdm->signature, &sdm->public_key))
3405 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3406 "%s message from peer `%s' cannot be verified \n",
3409 GNUNET_break_op (0);
3412 if (GNUNET_YES == test_connected (n))
3413 GNUNET_STATISTICS_update (GST_stats,
3415 ("# other peer asked to disconnect from us"), 1,
3417 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3418 "Disconnecting by request from peer %s\n",
3420 disconnect_neighbour (n);
3425 * Closure for the neighbours_iterate function.
3427 struct IteratorContext
3430 * Function to call on each connected neighbour.
3432 GST_NeighbourIterator cb;
3442 * Call the callback from the closure for each neighbour.
3444 * @param cls the `struct IteratorContext`
3445 * @param key the hash of the public key of the neighbour
3446 * @param value the `struct NeighbourMapEntry`
3447 * @return #GNUNET_OK (continue to iterate)
3450 neighbours_iterate (void *cls,
3451 const struct GNUNET_PeerIdentity *key,
3454 struct IteratorContext *ic = cls;
3455 struct NeighbourMapEntry *n = value;
3456 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3457 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3460 if (NULL != n->primary_address.address)
3462 bandwidth_in = n->primary_address.bandwidth_in;
3463 bandwidth_out = n->primary_address.bandwidth_out;
3467 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3468 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3472 n->primary_address.address,
3475 bandwidth_in, bandwidth_out);
3481 * Iterate over all connected neighbours.
3483 * @param cb function to call
3484 * @param cb_cls closure for cb
3487 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3489 struct IteratorContext ic;
3491 if (NULL == neighbours)
3492 return; /* can happen during shutdown */
3495 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3500 * If we have an active connection to the given target, it must be shutdown.
3502 * @param target peer to disconnect from
3505 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3507 struct NeighbourMapEntry *n;
3509 if (NULL == (n = lookup_neighbour (target)))
3510 return; /* not active */
3511 if (GNUNET_YES == test_connected (n))
3512 GNUNET_STATISTICS_update (GST_stats,
3514 ("# disconnected from peer upon explicit request"), 1,
3516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3517 "Forced disconnect from peer %s\n",
3518 GNUNET_i2s (target));
3519 disconnect_neighbour (n);
3524 * Obtain current latency information for the given neighbour.
3526 * @param peer to get the latency for
3527 * @return observed latency of the address, FOREVER if the
3528 * the connection is not up
3530 struct GNUNET_TIME_Relative
3531 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3533 struct NeighbourMapEntry *n;
3535 n = lookup_neighbour (peer);
3537 return GNUNET_TIME_UNIT_FOREVER_REL;
3540 case GNUNET_TRANSPORT_PS_CONNECTED:
3541 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3542 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3543 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3544 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3545 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3547 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3548 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3549 case GNUNET_TRANSPORT_PS_INIT_ATS:
3550 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3551 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3552 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3553 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3554 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3555 case GNUNET_TRANSPORT_PS_DISCONNECT:
3556 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3557 return GNUNET_TIME_UNIT_FOREVER_REL;
3559 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3560 "Unhandled state `%s'\n",
3561 GNUNET_TRANSPORT_ps2s (n->state));
3565 return GNUNET_TIME_UNIT_FOREVER_REL;
3570 * Obtain current address information for the given neighbour.
3573 * @return address currently used
3575 struct GNUNET_HELLO_Address *
3576 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3578 struct NeighbourMapEntry *n;
3580 n = lookup_neighbour (peer);
3583 return n->primary_address.address;
3588 * Initialize the neighbours subsystem.
3590 * @param cls closure for callbacks
3591 * @param connect_cb function to call if we connect to a peer
3592 * @param disconnect_cb function to call if we disconnect from a peer
3593 * @param peer_address_cb function to call if we change an active address
3595 * @param max_fds maximum number of fds to use
3598 GST_neighbours_start (void *cls,
3599 NotifyConnect connect_cb,
3600 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3601 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3602 unsigned int max_fds)
3605 connect_notify_cb = connect_cb;
3606 disconnect_notify_cb = disconnect_cb;
3607 neighbour_change_cb = peer_address_cb;
3608 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3609 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3610 utilization_transmission, NULL);
3615 * Disconnect from the given neighbour.
3618 * @param key hash of neighbour's public key (not used)
3619 * @param value the 'struct NeighbourMapEntry' of the neighbour
3620 * @return #GNUNET_OK (continue to iterate)
3623 disconnect_all_neighbours (void *cls,
3624 const struct GNUNET_PeerIdentity *key,
3627 struct NeighbourMapEntry *n = value;
3629 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3630 "Disconnecting peer `%4s', %s\n",
3631 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3632 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
3633 free_neighbour (n, GNUNET_NO);
3639 * Cleanup the neighbours subsystem.
3642 GST_neighbours_stop ()
3644 struct BlacklistCheckSwitchContext *cur;
3645 struct BlacklistCheckSwitchContext *next;
3647 if (NULL == neighbours)
3649 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3651 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3652 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3655 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3656 &disconnect_all_neighbours,
3658 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3660 next = pending_bc_head;
3661 for (cur = next; NULL != cur; cur = next )
3665 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3666 GNUNET_HELLO_address_free (cur->address);
3667 GNUNET_free_non_null (cur->ats);
3672 callback_cls = NULL;
3673 connect_notify_cb = NULL;
3674 disconnect_notify_cb = NULL;
3675 neighbour_change_cb = NULL;
3679 /* end of file gnunet-service-transport_neighbours.c */