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 * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb
468 static void *callback_cls;
471 * Function to call when we connected to a neighbour.
473 static NotifyConnect connect_notify_cb;
476 * Function to call when we disconnected from a neighbour.
478 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
481 * Function to call when a neighbour changed address, state or bandwidth.
483 static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb;
486 * counter for connected neighbours
488 static unsigned int neighbours_connected;
491 * Number of bytes we have currently queued for transmission.
493 static unsigned long long bytes_in_send_queue;
496 * Task transmitting utilization data
498 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
502 * Lookup a neighbour entry in the neighbours hash map.
504 * @param pid identity of the peer to look up
505 * @return the entry, NULL if there is no existing record
507 static struct NeighbourMapEntry *
508 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
510 if (NULL == neighbours)
512 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
517 * Test if we're connected to the given peer.
519 * @param n neighbour entry of peer to test
520 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
523 test_connected (struct NeighbourMapEntry *n)
527 return GNUNET_TRANSPORT_is_connected (n->state);
531 * Send information about a new outbound quota to our clients.
533 * @param target affected peer
534 * @param quota new quota
537 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
538 struct GNUNET_BANDWIDTH_Value32NBO quota)
540 struct QuotaSetMessage q_msg;
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
544 ntohl (quota.value__), GNUNET_i2s (target));
545 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
546 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
548 q_msg.peer = (*target);
549 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
554 * We don't need a given neighbour address any more.
555 * Release its resources and give appropriate notifications
556 * to ATS and other subsystems.
558 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
561 free_address (struct NeighbourAddress *na)
563 if (GNUNET_YES == na->ats_active)
565 GST_validation_set_address_use (na->address, na->session, GNUNET_NO);
566 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
569 na->ats_active = GNUNET_NO;
570 na->keep_alive_nonce = 0;
571 if (NULL != na->address)
573 GNUNET_HELLO_address_free (na->address);
581 * Set net state for this neighbour and notify monitoring
583 * @param n the respective neighbour
584 * @param s the new state
587 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
590 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
592 GNUNET_TRANSPORT_ps2s(s));
593 neighbour_change_cb (callback_cls,
595 n->primary_address.address,
596 n->state, n->timeout,
597 n->primary_address.bandwidth_in,
598 n->primary_address.bandwidth_out);
603 * Set net state and state timeout for this neighbour and notify monitoring
605 * @param n the respective neighbour
606 * @param s the new state
607 * @param timeout the new timeout
610 set_state_and_timeout (struct NeighbourMapEntry *n,
611 enum GNUNET_TRANSPORT_PeerState s,
612 struct GNUNET_TIME_Absolute timeout)
615 n->timeout = timeout;
616 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
618 GNUNET_TRANSPORT_ps2s(s),
619 GNUNET_STRINGS_absolute_time_to_string (timeout));
620 neighbour_change_cb (callback_cls,
622 n->primary_address.address,
623 n->state, n->timeout,
624 n->primary_address.bandwidth_in,
625 n->primary_address.bandwidth_out);
630 * Set new state timeout for this neighbour and notify monitoring
632 * @param n the respective neighbour
633 * @param timeout the new timeout
636 set_timeout (struct NeighbourMapEntry *n,
637 struct GNUNET_TIME_Absolute timeout)
639 n->timeout = timeout;
640 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed timeout %s\n",
642 GNUNET_STRINGS_absolute_time_to_string (timeout));
643 neighbour_change_cb (callback_cls,
645 n->primary_address.address,
646 n->state, n->timeout,
647 n->primary_address.bandwidth_in,
648 n->primary_address.bandwidth_out);
653 * Initialize the alternative address of a neighbour
655 * @param n the neighbour
656 * @param address address of the other peer, NULL if other peer
658 * @param session session to use (or NULL, in which case an
659 * address must be setup)
660 * @param bandwidth_in inbound quota to be used when connection is up
661 * @param bandwidth_out outbound quota to be used when connection is up
664 set_alternative_address (struct NeighbourMapEntry *n,
665 const struct GNUNET_HELLO_Address *address,
666 struct Session *session,
667 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
668 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
670 struct GNUNET_TRANSPORT_PluginFunctions *papi;
671 if (NULL == (papi = GST_plugins_find (address->transport_name)))
676 if (session == n->alternative_address.session)
678 n->alternative_address.bandwidth_in = bandwidth_in;
679 n->alternative_address.bandwidth_out = bandwidth_out;
682 free_address (&n->alternative_address);
684 session = papi->get_session (papi->cls, address);
687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
688 "Failed to obtain new session for peer `%s' and address '%s'\n",
689 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
690 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
694 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
696 GST_plugins_a2s(address));
698 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
699 n->alternative_address.bandwidth_in = bandwidth_in;
700 n->alternative_address.bandwidth_out = bandwidth_out;
701 n->alternative_address.session = session;
702 n->alternative_address.ats_active = GNUNET_NO;
703 n->alternative_address.keep_alive_nonce = 0;
708 * Initialize the primary address of a neighbour
710 * @param n the neighbour
711 * @param address address of the other peer, NULL if other peer
713 * @param session session to use (or NULL, in which case an
714 * address must be setup)
715 * @param bandwidth_in inbound quota to be used when connection is up
716 * @param bandwidth_out outbound quota to be used when connection is up
717 * @param is_active #GNUNET_YES to mark this as the active address with ATS
720 set_primary_address (struct NeighbourMapEntry *n,
721 const struct GNUNET_HELLO_Address *address,
722 struct Session *session,
723 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
724 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
727 struct GNUNET_TRANSPORT_PluginFunctions *papi;
729 if (NULL == (papi = GST_plugins_find (address->transport_name)))
734 if (session == n->primary_address.session)
736 n->primary_address.bandwidth_in = bandwidth_in;
737 n->primary_address.bandwidth_out = bandwidth_out;
738 if (is_active != n->primary_address.ats_active)
740 n->primary_address.ats_active = is_active;
741 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
742 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
744 if (GNUNET_YES == is_active)
746 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
747 send_outbound_quota (&address->peer, bandwidth_out);
751 free_address (&n->primary_address);
753 session = papi->get_session (papi->cls, address);
756 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
757 "Failed to obtain new session for peer `%s' and address '%s'\n",
758 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
759 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
763 n->primary_address.address = GNUNET_HELLO_address_copy (address);
764 n->primary_address.bandwidth_in = bandwidth_in;
765 n->primary_address.bandwidth_out = bandwidth_out;
766 n->primary_address.session = session;
767 n->primary_address.ats_active = is_active;
768 n->primary_address.keep_alive_nonce = 0;
769 if (GNUNET_YES == is_active)
771 /* Telling ATS about new session */
772 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
773 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
774 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
775 send_outbound_quota (&address->peer, bandwidth_out);
778 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
780 GST_plugins_a2s(address));
782 neighbour_change_cb (callback_cls,
784 n->primary_address.address,
785 n->state, n->timeout,
786 n->primary_address.bandwidth_in,
787 n->primary_address.bandwidth_out);
791 * Clear the primary address of a neighbour since this primary address is not
792 * valid anymore and notify monitoring about it
794 * @param n the neighbour
797 unset_primary_address (struct NeighbourMapEntry *n)
804 * Free a neighbour map entry.
806 * @param n entry to free
807 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
808 * #GNUNET_YES to keep all sessions
811 free_neighbour (struct NeighbourMapEntry *n,
814 struct MessageQueue *mq;
815 struct GNUNET_TRANSPORT_PluginFunctions *papi;
816 struct GNUNET_HELLO_Address *backup_primary;
818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
819 "Freeing neighbur state of peer `%s'\n",
820 GNUNET_i2s (&n->id));
821 n->is_active = NULL; /* always free'd by its own continuation! */
823 /* fail messages currently in the queue */
824 while (NULL != (mq = n->messages_head))
826 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
827 if (NULL != mq->cont)
828 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
831 /* It is too late to send other peer disconnect notifications, but at
832 least internally we need to get clean... */
833 if (GNUNET_YES == test_connected (n))
835 GNUNET_STATISTICS_set (GST_stats,
836 gettext_noop ("# peers connected"),
837 --neighbours_connected,
839 disconnect_notify_cb (callback_cls, &n->id);
841 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
843 if (NULL != n->primary_address.address)
845 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
848 backup_primary = NULL;
850 /* free addresses and mark as unused */
851 free_address (&n->primary_address);
852 free_address (&n->alternative_address);
854 /* FIXME: Note that if we are switching between two TCP sessions to
855 the same peer, we might want to selectively kill only one of
856 them! Killing all sessions like this seems to be very, very
859 /* cut transport-level connection */
860 if ((GNUNET_NO == keep_sessions) &&
861 (NULL != backup_primary) &&
862 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
863 papi->disconnect_peer (papi->cls, &n->id);
865 GNUNET_free_non_null (backup_primary);
867 GNUNET_assert (GNUNET_YES ==
868 GNUNET_CONTAINER_multipeermap_remove (neighbours,
871 // FIXME-ATS-API: we might want to be more specific about
872 // which states we do this from in the future (ATS should
873 // have given us a 'suggest_address' handle, and if we have
874 // such a handle, we should cancel the operation here!
875 if (NULL != n->suggest_handle)
877 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
878 n->suggest_handle = NULL;
881 if (GNUNET_SCHEDULER_NO_TASK != n->task)
883 GNUNET_SCHEDULER_cancel (n->task);
884 n->task = GNUNET_SCHEDULER_NO_TASK;
886 /* free rest of memory */
892 * Transmit a message using the current session of the given
895 * @param n entry for the recipient
896 * @param msgbuf buffer to transmit
897 * @param msgbuf_size number of bytes in @a msgbuf buffer
898 * @param priority transmission priority
899 * @param timeout transmission timeout
900 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
901 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
902 * @param cont continuation to call when finished (can be NULL)
903 * @param cont_cls closure for @a cont
904 * @return timeout (copy of @a timeout or a calculated one if
905 * @a use_keepalive_timeout is #GNUNET_YES.
907 static struct GNUNET_TIME_Relative
908 send_with_session (struct NeighbourMapEntry *n,
909 const char *msgbuf, size_t msgbuf_size,
911 struct GNUNET_TIME_Relative timeout,
912 unsigned int use_keepalive_timeout,
913 GNUNET_TRANSPORT_TransmitContinuation cont,
916 struct GNUNET_TRANSPORT_PluginFunctions *papi;
917 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
919 GNUNET_assert (n->primary_address.session != NULL);
920 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
921 (-1 == papi->send (papi->cls,
922 n->primary_address.session,
925 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
926 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
927 papi->query_keepalive_factor (papi->cls))),
928 cont, cont_cls)))) &&
930 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
931 GST_neighbours_notify_data_sent (&n->id,
932 n->primary_address.address, n->primary_address.session, msgbuf_size);
933 GNUNET_break (NULL != papi);
939 * Master task run for every neighbour. Performs all of the time-related
940 * activities (keep alive, send next message, disconnect if idle, finish
941 * clean up after disconnect).
943 * @param cls the `struct NeighbourMapEntry` for which we are running
944 * @param tc scheduler context (unused)
947 master_task (void *cls,
948 const struct GNUNET_SCHEDULER_TaskContext *tc);
952 * Function called when the 'DISCONNECT' message has been sent by the
953 * plugin. Frees the neighbour --- if the entry still exists.
956 * @param target identity of the neighbour that was disconnected
957 * @param result #GNUNET_OK if the disconnect got out successfully
958 * @param payload bytes payload
959 * @param physical bytes physical
962 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
963 int result, size_t payload, size_t physical)
965 struct NeighbourMapEntry *n;
967 n = lookup_neighbour (target);
969 return; /* already gone */
970 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
971 return; /* have created a fresh entry since */
972 if (GNUNET_SCHEDULER_NO_TASK != n->task)
973 GNUNET_SCHEDULER_cancel (n->task);
974 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
979 * Transmit a DISCONNECT message to the other peer.
981 * @param n neighbour to send DISCONNECT message.
984 send_disconnect (struct NeighbourMapEntry *n)
986 struct SessionDisconnectMessage disconnect_msg;
988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
989 "Sending DISCONNECT message to peer `%4s'\n",
990 GNUNET_i2s (&n->id));
991 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
992 disconnect_msg.header.type =
993 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
994 disconnect_msg.reserved = htonl (0);
995 disconnect_msg.purpose.size =
996 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
997 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
998 sizeof (struct GNUNET_TIME_AbsoluteNBO));
999 disconnect_msg.purpose.purpose =
1000 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1001 disconnect_msg.timestamp =
1002 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1003 disconnect_msg.public_key = GST_my_identity.public_key;
1004 GNUNET_assert (GNUNET_OK ==
1005 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1006 &disconnect_msg.purpose,
1007 &disconnect_msg.signature));
1009 (void) send_with_session (n,
1010 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1011 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1012 GNUNET_NO, &send_disconnect_cont, NULL);
1013 GNUNET_STATISTICS_update (GST_stats,
1015 ("# DISCONNECT messages sent"), 1,
1021 * Disconnect from the given neighbour, clean up the record.
1023 * @param n neighbour to disconnect from
1026 disconnect_neighbour (struct NeighbourMapEntry *n)
1028 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1029 "Disconnecting from peer %s in state %s\n",
1030 GNUNET_i2s (&n->id),
1031 GNUNET_TRANSPORT_ps2s (n->state));
1032 /* depending on state, notify neighbour and/or upper layers of this peer
1036 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1037 case GNUNET_TRANSPORT_PS_INIT_ATS:
1038 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1039 /* other peer is completely unaware of us, no need to send DISCONNECT */
1040 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1041 free_neighbour (n, GNUNET_NO);
1043 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1044 send_disconnect (n);
1045 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1047 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1048 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1049 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1050 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1051 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1052 free_neighbour (n, GNUNET_NO);
1054 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1055 /* we DID ACK the other peer's request, must send DISCONNECT */
1056 send_disconnect (n);
1057 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1059 case GNUNET_TRANSPORT_PS_CONNECTED:
1060 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1061 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1062 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1063 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1064 /* we are currently connected, need to send disconnect and do
1065 internal notifications and update statistics */
1066 send_disconnect (n);
1067 GNUNET_STATISTICS_set (GST_stats,
1068 gettext_noop ("# peers connected"),
1069 --neighbours_connected,
1071 disconnect_notify_cb (callback_cls, &n->id);
1072 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1074 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1075 /* ATS address request timeout, disconnect without sending disconnect message */
1076 GNUNET_STATISTICS_set (GST_stats,
1077 gettext_noop ("# peers connected"),
1078 --neighbours_connected,
1080 disconnect_notify_cb (callback_cls, &n->id);
1081 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1083 case GNUNET_TRANSPORT_PS_DISCONNECT:
1084 /* already disconnected, ignore */
1086 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1087 /* already cleaned up, how did we get here!? */
1091 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1092 "Unhandled state `%s'\n",
1093 GNUNET_TRANSPORT_ps2s (n->state));
1097 /* schedule timeout to clean up */
1098 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1099 GNUNET_SCHEDULER_cancel (n->task);
1100 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1106 * We're done with our transmission attempt, continue processing.
1108 * @param cls the `struct MessageQueue` of the message
1109 * @param receiver intended receiver
1110 * @param success whether it worked or not
1111 * @param size_payload bytes payload sent
1112 * @param physical bytes sent on wire
1115 transmit_send_continuation (void *cls,
1116 const struct GNUNET_PeerIdentity *receiver,
1117 int success, size_t size_payload, size_t physical)
1119 struct MessageQueue *mq = cls;
1120 struct NeighbourMapEntry *n;
1122 if (NULL == (n = lookup_neighbour (receiver)))
1125 return; /* disconnect or other error while transmitting, can happen */
1127 if (n->is_active == mq)
1129 /* this is still "our" neighbour, remove us from its queue
1130 and allow it to send the next message now */
1131 n->is_active = NULL;
1132 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1133 GNUNET_SCHEDULER_cancel (n->task);
1134 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1136 if (bytes_in_send_queue < mq->message_buf_size)
1138 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1139 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1140 bytes_in_send_queue, mq->message_buf_size,
1141 (GNUNET_OK == success) ? "OK" : "FAIL",
1142 size_payload, physical);
1147 GNUNET_break (size_payload == mq->message_buf_size);
1148 bytes_in_send_queue -= mq->message_buf_size;
1149 GNUNET_STATISTICS_set (GST_stats,
1151 ("# bytes in message queue for other peers"),
1152 bytes_in_send_queue, GNUNET_NO);
1153 if (GNUNET_OK == success)
1154 GNUNET_STATISTICS_update (GST_stats,
1156 ("# messages transmitted to other peers"),
1159 GNUNET_STATISTICS_update (GST_stats,
1161 ("# transmission failures for messages to other peers"),
1163 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1164 "Sending message to `%s' of type %u was a %s\n",
1165 GNUNET_i2s (receiver),
1166 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1167 (success == GNUNET_OK) ? "success" : "FAILURE");
1168 if (NULL != mq->cont)
1169 mq->cont (mq->cont_cls, success, size_payload, physical);
1175 * Check the message list for the given neighbour and if we can
1176 * send a message, do so. This function should only be called
1177 * if the connection is at least generally ready for transmission.
1178 * While we will only send one message at a time, no bandwidth
1179 * quota management is performed here. If a message was given to
1180 * the plugin, the continuation will automatically re-schedule
1181 * the 'master' task once the next message might be transmitted.
1183 * @param n target peer for which to transmit
1186 try_transmission_to_peer (struct NeighbourMapEntry *n)
1188 struct MessageQueue *mq;
1189 struct GNUNET_TIME_Relative timeout;
1191 if (NULL == n->primary_address.address)
1193 /* no address, why are we here? */
1197 if ((0 == n->primary_address.address->address_length) &&
1198 (NULL == n->primary_address.session))
1200 /* no address, why are we here? */
1204 if (NULL != n->is_active)
1206 /* transmission already pending */
1210 /* timeout messages from the queue that are past their due date */
1211 while (NULL != (mq = n->messages_head))
1213 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1214 if (timeout.rel_value_us > 0)
1216 GNUNET_STATISTICS_update (GST_stats,
1218 ("# messages timed out while in transport queue"),
1220 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1222 transmit_send_continuation (mq, &n->id,
1224 mq->message_buf_size, 0); /* timeout */
1227 return; /* no more messages */
1228 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1230 (void) send_with_session (n,
1231 mq->message_buf, mq->message_buf_size,
1232 0 /* priority */, timeout, GNUNET_NO,
1233 &transmit_send_continuation, mq);
1238 * Send keepalive message to the neighbour. Must only be called
1239 * if we are on 'connected' state or while trying to switch addresses.
1240 * Will internally determine if a keepalive is truly needed (so can
1241 * always be called).
1243 * @param n neighbour that went idle and needs a keepalive
1246 send_keepalive (struct NeighbourMapEntry *n)
1248 struct SessionKeepAliveMessage m;
1249 struct GNUNET_TIME_Relative timeout;
1252 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1253 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1254 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1255 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1256 return; /* no keepalive needed at this time */
1258 nonce = 0; /* 0 indicates 'not set' */
1260 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1262 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1263 "Sending keep alive to peer `%s' with nonce %u\n",
1264 GNUNET_i2s (&n->id), nonce);
1266 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1267 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1268 m.nonce = htonl (nonce);
1270 timeout = send_with_session (n,
1271 (const void *) &m, sizeof (m),
1272 UINT32_MAX /* priority */,
1273 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1275 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1277 n->primary_address.keep_alive_nonce = nonce;
1278 n->expect_latency_response = GNUNET_YES;
1279 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1280 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1286 * Keep the connection to the given neighbour alive longer,
1287 * we received a KEEPALIVE (or equivalent); send a response.
1289 * @param neighbour neighbour to keep alive (by sending keep alive response)
1290 * @param m the keep alive message containing the nonce to respond to
1293 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1294 const struct GNUNET_MessageHeader *m)
1296 struct NeighbourMapEntry *n;
1297 const struct SessionKeepAliveMessage *msg_in;
1298 struct SessionKeepAliveMessage msg;
1300 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1303 msg_in = (struct SessionKeepAliveMessage *) m;
1304 if (NULL == (n = lookup_neighbour (neighbour)))
1306 GNUNET_STATISTICS_update (GST_stats,
1308 ("# KEEPALIVE messages discarded (peer unknown)"),
1312 if (NULL == n->primary_address.session)
1314 GNUNET_STATISTICS_update (GST_stats,
1316 ("# KEEPALIVE messages discarded (no session)"),
1321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1322 "Received keep alive request from peer `%s' with nonce %u\n",
1323 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1325 /* send reply to allow neighbour to measure latency */
1326 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1327 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1328 msg.nonce = msg_in->nonce;
1329 (void) send_with_session(n,
1330 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1331 UINT32_MAX /* priority */,
1332 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1338 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1339 * latency to this peer. Pass the updated information (existing ats
1340 * plus calculated latency) to ATS.
1342 * @param neighbour neighbour to keep alive
1343 * @param m the message containing the keep alive response
1346 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1347 const struct GNUNET_MessageHeader *m)
1349 struct NeighbourMapEntry *n;
1350 const struct SessionKeepAliveMessage *msg;
1351 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1353 struct GNUNET_ATS_Information ats;
1355 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1358 msg = (const struct SessionKeepAliveMessage *) m;
1359 if (NULL == (n = lookup_neighbour (neighbour)))
1361 GNUNET_STATISTICS_update (GST_stats,
1363 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1367 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1368 (GNUNET_YES != n->expect_latency_response) )
1370 GNUNET_STATISTICS_update (GST_stats,
1372 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1376 if (NULL == n->primary_address.address)
1378 GNUNET_STATISTICS_update (GST_stats,
1380 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1384 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1386 GNUNET_STATISTICS_update (GST_stats,
1388 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Received keep alive response from peer `%s' for session %p\n",
1396 GNUNET_i2s (&n->id), n->primary_address.session);
1400 /* Update session timeout here */
1401 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1404 "Updating session for peer `%s' for session %p\n",
1405 GNUNET_i2s (&n->id), n->primary_address.session);
1406 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1413 n->primary_address.keep_alive_nonce = 0;
1414 n->expect_latency_response = GNUNET_NO;
1415 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1416 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1418 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1419 "Latency for peer `%s' is %s\n",
1420 GNUNET_i2s (&n->id),
1421 GNUNET_STRINGS_relative_time_to_string (n->latency,
1423 /* append latency */
1424 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1425 if (n->latency.rel_value_us > UINT32_MAX)
1426 latency = UINT32_MAX;
1428 latency = n->latency.rel_value_us;
1429 ats.value = htonl (latency);
1430 GST_ats_update_metrics (&n->id, n->primary_address.address,
1431 n->primary_address.session, &ats, 1);
1436 * We have received a message from the given sender. How long should
1437 * we delay before receiving more? (Also used to keep the peer marked
1440 * @param sender sender of the message
1441 * @param size size of the message
1442 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1443 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1444 * #GNUNET_SYSERR if the connection is not fully up yet
1445 * @return how long to wait before reading more from this sender
1447 struct GNUNET_TIME_Relative
1448 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1449 *sender, ssize_t size, int *do_forward)
1451 struct NeighbourMapEntry *n;
1452 struct GNUNET_TIME_Relative ret;
1454 if (NULL == neighbours)
1456 *do_forward = GNUNET_NO;
1457 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1459 if (NULL == (n = lookup_neighbour (sender)))
1461 GST_neighbours_try_connect (sender);
1462 if (NULL == (n = lookup_neighbour (sender)))
1464 GNUNET_STATISTICS_update (GST_stats,
1466 ("# messages discarded due to lack of neighbour record"),
1468 *do_forward = GNUNET_NO;
1469 return GNUNET_TIME_UNIT_ZERO;
1472 if (! test_connected (n))
1474 *do_forward = GNUNET_SYSERR;
1475 return GNUNET_TIME_UNIT_ZERO;
1477 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1479 n->quota_violation_count++;
1480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1481 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1482 n->in_tracker.available_bytes_per_s__,
1483 n->quota_violation_count);
1484 /* Discount 32k per violation */
1485 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1489 if (n->quota_violation_count > 0)
1491 /* try to add 32k back */
1492 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1493 n->quota_violation_count--;
1496 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1498 GNUNET_STATISTICS_update (GST_stats,
1500 ("# bandwidth quota violations by other peers"),
1502 *do_forward = GNUNET_NO;
1503 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1505 *do_forward = GNUNET_YES;
1506 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1507 if (ret.rel_value_us > 0)
1509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1510 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1511 (unsigned long long) n->in_tracker.
1512 consumption_since_last_update__,
1513 (unsigned int) n->in_tracker.available_bytes_per_s__,
1514 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1515 GNUNET_STATISTICS_update (GST_stats,
1516 gettext_noop ("# ms throttling suggested"),
1517 (int64_t) ret.rel_value_us / 1000LL,
1525 * Transmit a message to the given target using the active connection.
1527 * @param target destination
1528 * @param msg message to send
1529 * @param msg_size number of bytes in msg
1530 * @param timeout when to fail with timeout
1531 * @param cont function to call when done
1532 * @param cont_cls closure for 'cont'
1535 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1536 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1537 GST_NeighbourSendContinuation cont, void *cont_cls)
1539 struct NeighbourMapEntry *n;
1540 struct MessageQueue *mq;
1542 /* All ove these cases should never happen; they are all API violations.
1543 But we check anyway, just to be sure. */
1544 if (NULL == (n = lookup_neighbour (target)))
1548 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1551 if (GNUNET_YES != test_connected (n))
1555 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1558 bytes_in_send_queue += msg_size;
1559 GNUNET_STATISTICS_set (GST_stats,
1561 ("# bytes in message queue for other peers"),
1562 bytes_in_send_queue, GNUNET_NO);
1563 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1565 mq->cont_cls = cont_cls;
1566 memcpy (&mq[1], msg, msg_size);
1567 mq->message_buf = (const char *) &mq[1];
1568 mq->message_buf_size = msg_size;
1569 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1570 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1571 if ( (NULL != n->is_active) ||
1572 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1574 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1575 GNUNET_SCHEDULER_cancel (n->task);
1576 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1581 * Send a SESSION_CONNECT message via the given address.
1583 * @param na address to use
1586 send_session_connect (struct NeighbourAddress *na)
1588 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1589 struct SessionConnectMessage connect_msg;
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1592 "Sending SESSION_CONNECT message to peer %s\n",
1593 GNUNET_i2s (&na->address->peer));
1594 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1599 if (NULL == na->session)
1600 na->session = papi->get_session (papi->cls, na->address);
1601 if (NULL == na->session)
1606 GNUNET_STATISTICS_update (GST_stats,
1608 ("# SESSION_CONNECT messages sent"),
1610 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1611 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1612 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1613 connect_msg.reserved = htonl (0);
1614 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1616 papi->send (papi->cls,
1618 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1620 GNUNET_TIME_UNIT_FOREVER_REL,
1623 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1624 _("Failed to transmit CONNECT message via plugin to %s\n"),
1625 GST_plugins_a2s (na->address));
1627 GST_neighbours_notify_data_sent (&na->address->peer,
1630 sizeof (struct SessionConnectMessage));
1636 * Send a SESSION_CONNECT_ACK message via the given address.
1638 * @param address address to use
1639 * @param session session to use
1640 * @param timestamp timestamp to use for the ACK message
1643 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1644 struct Session *session,
1645 struct GNUNET_TIME_Absolute timestamp)
1647 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1648 struct SessionConnectMessage connect_msg;
1650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1651 "Sending CONNECT_ACK to peer `%s'\n",
1652 GNUNET_i2s (&address->peer));
1653 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1658 if (NULL == session)
1659 session = papi->get_session (papi->cls, address);
1660 if (NULL == session)
1665 GNUNET_STATISTICS_update (GST_stats,
1667 ("# CONNECT_ACK messages sent"),
1669 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1670 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1671 connect_msg.reserved = htonl (0);
1672 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1673 (void) papi->send (papi->cls,
1675 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1677 GNUNET_TIME_UNIT_FOREVER_REL,
1684 * Create a fresh entry in the neighbour map for the given peer
1686 * @param peer peer to create an entry for
1687 * @return new neighbour map entry
1689 static struct NeighbourMapEntry *
1690 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1692 struct NeighbourMapEntry *n;
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Creating new neighbour entry for `%s'\n",
1697 n = GNUNET_new (struct NeighbourMapEntry);
1699 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1700 n->last_util_transmission = GNUNET_TIME_absolute_get();
1701 n->util_payload_bytes_recv = 0;
1702 n->util_payload_bytes_sent = 0;
1703 n->util_total_bytes_recv = 0;
1704 n->util_total_bytes_sent = 0;
1705 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1706 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1707 MAX_BANDWIDTH_CARRY_S);
1708 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1709 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
1710 GNUNET_assert (GNUNET_OK ==
1711 GNUNET_CONTAINER_multipeermap_put (neighbours,
1713 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1719 * Check if the two given addresses are the same.
1720 * Actually only checks if the sessions are non-NULL
1721 * (which they should be) and then if they are identical;
1722 * the actual addresses don't matter if the session
1723 * pointers match anyway, and we must have session pointers
1726 * @param a1 first address to compare
1727 * @param a2 other address to compare
1728 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1731 address_matches (const struct NeighbourAddress *a1,
1732 const struct NeighbourAddress *a2)
1734 if ( (NULL == a1->session) ||
1735 (NULL == a2->session) )
1740 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1745 address_suggest_cont (void *cls,
1746 const struct GNUNET_PeerIdentity *peer,
1747 const struct GNUNET_HELLO_Address *address, struct Session *session,
1748 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1749 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1750 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
1757 * Try to create a connection to the given target (eventually).
1759 * @param target peer to try to connect to
1762 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1764 struct NeighbourMapEntry *n;
1766 if (NULL == neighbours)
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 "Asked to connect to peer `%s' during shutdown\n",
1770 GNUNET_i2s (target));
1771 return; /* during shutdown, do nothing */
1773 n = lookup_neighbour (target);
1774 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1775 "Asked to connect to peer `%s' (state: %s)\n",
1776 GNUNET_i2s (target),
1777 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
1782 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1783 /* this should not be possible */
1785 free_neighbour (n, GNUNET_NO);
1787 case GNUNET_TRANSPORT_PS_INIT_ATS:
1788 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1789 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1790 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1791 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1792 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1793 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1794 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1795 "Ignoring request to try to connect to `%s', already trying!\n",
1796 GNUNET_i2s (target));
1797 return; /* already trying */
1798 case GNUNET_TRANSPORT_PS_CONNECTED:
1799 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1800 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1801 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1802 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1803 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1804 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1805 "Ignoring request to try to connect, already connected to `%s'!\n",
1806 GNUNET_i2s (target));
1807 return; /* already connected */
1808 case GNUNET_TRANSPORT_PS_DISCONNECT:
1809 /* get rid of remains, ready to re-try immediately */
1810 free_neighbour (n, GNUNET_NO);
1812 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1813 /* should not be possible */
1816 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1817 "Unhandled state `%s'\n",
1818 GNUNET_TRANSPORT_ps2s (n->state));
1820 free_neighbour (n, GNUNET_NO);
1824 n = setup_neighbour (target);
1825 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1827 GNUNET_ATS_reset_backoff (GST_ats, target);
1828 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target, &address_suggest_cont, n);
1833 * Function called with the result of a blacklist check.
1835 * @param cls closure with the `struct BlackListCheckContext`
1836 * @param peer peer this check affects
1837 * @param result #GNUNET_OK if the address is allowed
1840 handle_test_blacklist_cont (void *cls,
1841 const struct GNUNET_PeerIdentity *peer,
1844 struct BlackListCheckContext *bcc = cls;
1845 struct NeighbourMapEntry *n;
1848 GNUNET_CONTAINER_DLL_remove (bc_head,
1851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1852 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1854 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1855 if (GNUNET_OK == result)
1857 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
1861 /* Blacklist disagreed on connecting to a peer with this address
1862 * Destroy address because we are not allowed to use it
1864 if (NULL != bcc->na.session)
1865 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1866 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1868 if (NULL == (n = lookup_neighbour (peer)))
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1871 "No neighbor entry for peer `%s', ignoring blacklist result\n",
1873 goto cleanup; /* nobody left to care about new address */
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "Received blacklist result for peer `%s' in state %s/%d\n",
1878 GNUNET_TRANSPORT_ps2s (n->state),
1879 n->send_connect_ack);
1882 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1883 /* this should not be possible */
1885 free_neighbour (n, GNUNET_NO);
1887 case GNUNET_TRANSPORT_PS_INIT_ATS:
1888 /* waiting on ATS suggestion; still, pass address to ATS as a
1891 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1892 /* check if the address the blacklist was fine with matches
1893 ATS suggestion, if so, we can move on! */
1894 if ( (GNUNET_OK == result) &&
1895 (1 == n->send_connect_ack) )
1897 n->send_connect_ack = 2;
1898 send_session_connect_ack_message (bcc->na.address,
1900 n->connect_ack_timestamp);
1902 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1904 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1905 "Blacklist result for peer %s is for non-primary address, ignored\n",
1907 break; /* result for an address we currently don't care about */
1909 if (GNUNET_OK == result)
1911 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT, GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
1912 send_session_connect (&n->primary_address);
1916 free_address (&n->primary_address);
1917 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1920 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1921 /* waiting on CONNECT_ACK, send ACK if one is pending */
1922 if ( (GNUNET_OK == result) &&
1923 (1 == n->send_connect_ack) )
1925 n->send_connect_ack = 2;
1926 send_session_connect_ack_message (n->primary_address.address,
1927 n->primary_address.session,
1928 n->connect_ack_timestamp);
1931 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1932 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1933 GNUNET_ATS_reset_backoff (GST_ats, peer);
1934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1935 "Suggesting address for peer %s to ATS\n",
1937 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer, &address_suggest_cont, n);
1939 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1940 /* waiting on ATS suggestion, don't care about blacklist */
1942 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1943 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1945 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1946 "Blacklist result ignored, as it is not for our primary address\n");
1947 break; /* result for an address we currently don't care about */
1949 if (GNUNET_OK == result)
1951 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK, GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
1952 send_session_connect_ack_message (bcc->na.address,
1954 n->connect_ack_timestamp);
1955 if (1 == n->send_connect_ack)
1956 n->send_connect_ack = 2;
1960 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
1962 plugin = GST_plugins_find (bcc->na.address->transport_name);
1963 if ( (NULL != plugin) &&
1964 (NULL != bcc->na.session) )
1966 plugin->disconnect_session (plugin->cls,
1970 GNUNET_break (NULL != plugin);
1971 free_address (&n->primary_address);
1972 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1973 GNUNET_ATS_reset_backoff (GST_ats, peer);
1976 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1977 /* waiting on SESSION_ACK, send ACK if one is pending */
1978 if ( (GNUNET_OK == result) &&
1979 (1 == n->send_connect_ack) )
1981 n->send_connect_ack = 2;
1982 send_session_connect_ack_message (n->primary_address.address,
1983 n->primary_address.session,
1984 n->connect_ack_timestamp);
1987 case GNUNET_TRANSPORT_PS_CONNECTED:
1988 /* already connected, don't care about blacklist */
1990 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1991 /* still waiting on ATS suggestion, don't care about blacklist */
1993 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1994 if ( (GNUNET_OK == result) &&
1995 (1 == n->send_connect_ack) )
1997 n->send_connect_ack = 2;
1998 send_session_connect_ack_message (bcc->na.address,
2000 n->connect_ack_timestamp);
2002 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2005 "Blacklist result ignored, as it is not for our primary address\n");
2006 break; /* result for an address we currently don't care about */
2008 if (GNUNET_OK == result)
2010 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2011 send_session_connect (&n->primary_address);
2015 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2018 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2019 /* waiting on CONNECT_ACK, don't care about blacklist */
2020 if ( (GNUNET_OK == result) &&
2021 (1 == n->send_connect_ack) )
2023 n->send_connect_ack = 2;
2024 send_session_connect_ack_message (n->primary_address.address,
2025 n->primary_address.session,
2026 n->connect_ack_timestamp);
2029 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2030 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2032 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2033 "Blacklist result ignored, as it is not for our primary address\n");
2034 break; /* result for an address we currently don't care about */
2036 if (GNUNET_OK == result)
2038 send_session_connect (&n->alternative_address);
2039 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2043 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2044 free_address (&n->alternative_address);
2047 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2048 /* waiting on CONNECT_ACK, don't care about blacklist */
2049 if ( (GNUNET_OK == result) &&
2050 (1 == n->send_connect_ack) )
2052 n->send_connect_ack = 2;
2053 send_session_connect_ack_message (n->primary_address.address,
2054 n->primary_address.session,
2055 n->connect_ack_timestamp);
2058 case GNUNET_TRANSPORT_PS_DISCONNECT:
2059 /* Nothing to do here, ATS will already do what can be done */
2061 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2062 /* should not be possible */
2066 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2067 "Unhandled state `%s'\n",
2068 GNUNET_TRANSPORT_ps2s (n->state));
2070 free_neighbour (n, GNUNET_NO);
2074 GNUNET_HELLO_address_free (bcc->na.address);
2080 * We want to know if connecting to a particular peer via
2081 * a particular address is allowed. Check it!
2083 * @param peer identity of the peer to switch the address for
2084 * @param ts time at which the check was initiated
2085 * @param address address of the other peer, NULL if other peer
2087 * @param session session to use (or NULL)
2090 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2091 struct GNUNET_TIME_Absolute ts,
2092 const struct GNUNET_HELLO_Address *address,
2093 struct Session *session)
2095 struct BlackListCheckContext *bcc;
2096 struct GST_BlacklistCheck *bc;
2098 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2099 "Checking peer `%s' against blacklist\n",
2101 bcc = GNUNET_new (struct BlackListCheckContext);
2102 bcc->na.address = GNUNET_HELLO_address_copy (address);
2103 bcc->na.session = session;
2104 bcc->na.connect_timestamp = ts;
2105 GNUNET_CONTAINER_DLL_insert (bc_head,
2108 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2109 address->transport_name,
2110 &handle_test_blacklist_cont, bcc)))
2112 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2113 we must only store 'bc' if 'bc' is non-NULL... */
2118 * We received a 'SESSION_CONNECT' message from the other peer.
2119 * Consider switching to it.
2121 * @param message possibly a 'struct SessionConnectMessage' (check format)
2122 * @param peer identity of the peer to switch the address for
2123 * @param address address of the other peer, NULL if other peer
2125 * @param session session to use (or NULL)
2126 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2129 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2130 const struct GNUNET_PeerIdentity *peer,
2131 const struct GNUNET_HELLO_Address *address,
2132 struct Session *session)
2134 const struct SessionConnectMessage *scm;
2135 struct NeighbourMapEntry *n;
2136 struct GNUNET_TIME_Absolute ts;
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 "Received CONNECT message from peer `%s'\n",
2141 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2143 GNUNET_break_op (0);
2144 return GNUNET_SYSERR;
2146 GNUNET_STATISTICS_update (GST_stats,
2148 ("# CONNECT messages received"),
2150 if (NULL == neighbours)
2152 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2153 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2155 return GNUNET_OK; /* we're shutting down */
2157 scm = (const struct SessionConnectMessage *) message;
2158 GNUNET_break_op (0 == ntohl (scm->reserved));
2159 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2160 n = lookup_neighbour (peer);
2162 n = setup_neighbour (peer);
2163 n->send_connect_ack = 1;
2164 n->connect_ack_timestamp = ts;
2166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2167 "Received SESSION_CONNECT for peer `%s' in state %s/%d\n",
2169 GNUNET_TRANSPORT_ps2s (n->state),
2170 n->send_connect_ack);
2173 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2174 /* Do a blacklist check for the new address */
2175 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2176 check_blacklist (peer, ts, address, session);
2178 case GNUNET_TRANSPORT_PS_INIT_ATS:
2179 /* CONNECT message takes priority over us asking ATS for address */
2180 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2182 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2183 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2184 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2185 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2186 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2187 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2188 /* It can never hurt to have an alternative address in the above cases,
2189 see if it is allowed */
2190 check_blacklist (peer, ts, address, session);
2192 case GNUNET_TRANSPORT_PS_CONNECTED:
2193 /* we are already connected and can thus send the ACK immediately;
2194 still, it can never hurt to have an alternative address, so also
2195 tell ATS about it */
2196 GNUNET_assert (NULL != n->primary_address.address);
2197 GNUNET_assert (NULL != n->primary_address.session);
2198 n->send_connect_ack = 0;
2199 send_session_connect_ack_message (n->primary_address.address,
2200 n->primary_address.session, ts);
2201 check_blacklist (peer, ts, address, session);
2203 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2204 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2205 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2206 /* It can never hurt to have an alternative address in the above cases,
2207 see if it is allowed */
2208 check_blacklist (peer, ts, address, session);
2210 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2211 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2212 /* we are already connected and can thus send the ACK immediately;
2213 still, it can never hurt to have an alternative address, so also
2214 tell ATS about it */
2215 GNUNET_assert (NULL != n->primary_address.address);
2216 GNUNET_assert (NULL != n->primary_address.session);
2217 n->send_connect_ack = 0;
2218 send_session_connect_ack_message (n->primary_address.address,
2219 n->primary_address.session, ts);
2220 check_blacklist (peer, ts, address, session);
2222 case GNUNET_TRANSPORT_PS_DISCONNECT:
2223 /* get rid of remains without terminating sessions, ready to re-try */
2224 free_neighbour (n, GNUNET_YES);
2225 n = setup_neighbour (peer);
2226 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2227 GNUNET_ATS_reset_backoff (GST_ats, peer);
2229 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2230 /* should not be possible */
2234 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2235 "Unhandled state `%s'\n",
2236 GNUNET_TRANSPORT_ps2s (n->state));
2238 return GNUNET_SYSERR;
2245 * For an existing neighbour record, set the active connection to
2246 * use the given address.
2248 * @param peer identity of the peer to switch the address for
2249 * @param address address of the other peer, NULL if other peer
2251 * @param session session to use (or NULL)
2252 * @param ats performance data
2253 * @param ats_count number of entries in ats
2254 * @param bandwidth_in inbound quota to be used when connection is up,
2255 * 0 to disconnect from peer
2256 * @param bandwidth_out outbound quota to be used when connection is up,
2257 * 0 to disconnect from peer
2260 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2261 const struct GNUNET_HELLO_Address *address,
2262 struct Session *session,
2263 const struct GNUNET_ATS_Information *ats,
2265 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2266 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2268 struct NeighbourMapEntry *n;
2269 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2271 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2272 "ATS has decided on an address for peer %s\n",
2274 GNUNET_assert (NULL != address->transport_name);
2275 if (NULL == (n = lookup_neighbour (peer)))
2277 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2278 "Peer %s is unknown, suggestion ignored\n",
2283 /* Obtain an session for this address from plugin */
2284 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2286 /* we don't have the plugin for this address */
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2288 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2289 address->transport_name,
2291 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2294 if ((NULL == session) &&
2295 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2297 /* This is a inbound address and we do not have a session to use! */
2298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2299 "Inbound address without session `%s'! Destroying address...\n",
2300 GST_plugins_a2s (address));
2301 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2305 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2306 "ATS tells us to switch to %s address '%s' session %p for "
2307 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2308 GNUNET_HELLO_address_check_option (address,
2309 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "",
2310 GST_plugins_a2s (address),
2313 GNUNET_TRANSPORT_ps2s (n->state),
2314 n->send_connect_ack,
2315 ntohl (bandwidth_in.value__),
2316 ntohl (bandwidth_out.value__));
2318 if (NULL == session)
2320 session = papi->get_session (papi->cls, address);
2321 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2322 "Obtained new session for peer `%s' and address '%s': %p\n",
2323 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2325 if (NULL == session)
2327 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2328 "Failed to obtain new session for peer `%s' and address '%s'\n",
2329 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2330 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2335 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2337 free_neighbour (n, GNUNET_NO);
2339 case GNUNET_TRANSPORT_PS_INIT_ATS:
2340 set_primary_address (n, address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2341 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_BLACKLIST, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2342 check_blacklist (&n->id,
2343 n->connect_ack_timestamp,
2346 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2347 /* ATS suggests a different address, switch again */
2348 set_primary_address (n,
2349 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2350 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2351 check_blacklist (&n->id,
2352 n->connect_ack_timestamp,
2355 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2356 /* ATS suggests a different address, switch again */
2357 set_primary_address (n, address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2358 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_BLACKLIST, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2359 check_blacklist (&n->id,
2360 n->connect_ack_timestamp,
2363 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2364 set_primary_address (n,
2365 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2366 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2367 check_blacklist (&n->id,
2368 n->connect_ack_timestamp,
2371 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2372 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2373 check_blacklist (&n->id,
2374 n->connect_ack_timestamp,
2377 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2378 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2379 /* ATS asks us to switch while we were trying to connect; switch to new
2380 address and check blacklist again */
2381 set_primary_address (n,
2382 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2383 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2384 check_blacklist (&n->id,
2385 n->connect_ack_timestamp,
2388 case GNUNET_TRANSPORT_PS_CONNECTED:
2389 GNUNET_assert (NULL != n->primary_address.address);
2390 GNUNET_assert (NULL != n->primary_address.session);
2391 if (n->primary_address.session == session)
2393 /* not an address change, just a quota change */
2394 set_primary_address (n,
2395 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2398 /* ATS asks us to switch a life connection; see if we can get
2399 a CONNECT_ACK on it before we actually do this! */
2400 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2401 set_alternative_address (n, address, session, bandwidth_in, bandwidth_out);
2402 check_blacklist (&n->id,
2403 GNUNET_TIME_absolute_get (),
2406 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2407 set_primary_address (n,
2408 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2409 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2410 check_blacklist (&n->id,
2411 n->connect_ack_timestamp,
2414 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
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,
2418 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2419 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2420 check_blacklist (&n->id,
2421 n->connect_ack_timestamp,
2424 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2425 /* ATS asks us to switch while we were trying to reconnect; switch to new
2426 address and check blacklist again */
2427 set_primary_address (n,
2428 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2429 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2430 check_blacklist (&n->id,
2431 n->connect_ack_timestamp,
2434 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2435 if (n->primary_address.session == session)
2437 /* ATS switches back to still-active session */
2438 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2439 free_address (&n->alternative_address);
2442 /* ATS asks us to switch a life connection, update blacklist check */
2443 set_primary_address (n,
2444 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2445 check_blacklist (&n->id,
2446 GNUNET_TIME_absolute_get (),
2449 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2450 if (n->primary_address.session == session)
2452 /* ATS switches back to still-active session */
2453 free_address (&n->alternative_address);
2454 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2457 /* ATS asks us to switch a life connection, update blacklist check */
2458 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2459 set_alternative_address (n, address, session, bandwidth_in, bandwidth_out);
2460 check_blacklist (&n->id,
2461 GNUNET_TIME_absolute_get (),
2464 case GNUNET_TRANSPORT_PS_DISCONNECT:
2465 /* not going to switch addresses while disconnecting */
2467 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2471 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2472 "Unhandled state `%s'\n",
2473 GNUNET_TRANSPORT_ps2s (n->state));
2481 send_utilization_data (void *cls,
2482 const struct GNUNET_PeerIdentity *key,
2485 struct NeighbourMapEntry *n = value;
2486 struct GNUNET_ATS_Information atsi[4];
2488 uint32_t bps_pl_out;
2491 struct GNUNET_TIME_Relative delta;
2493 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2494 GNUNET_TIME_absolute_get ());
2498 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2499 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2501 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2502 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2503 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2504 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2509 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2510 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2512 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2513 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2517 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2521 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2522 atsi[0].value = htonl (bps_out);
2523 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2524 atsi[1].value = htonl (bps_in);
2526 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2527 atsi[2].value = htonl (bps_pl_out);
2528 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2529 atsi[3].value = htonl (bps_pl_in);
2531 GST_ats_update_metrics (key, n->primary_address.address,
2532 n->primary_address.session, atsi, 4);
2533 n->util_payload_bytes_recv = 0;
2534 n->util_payload_bytes_sent = 0;
2535 n->util_total_bytes_recv = 0;
2536 n->util_total_bytes_sent = 0;
2537 n->last_util_transmission = GNUNET_TIME_absolute_get();
2543 * Task transmitting utilization in a regular interval
2545 * @param cls the 'struct NeighbourMapEntry' for which we are running
2546 * @param tc scheduler context (unused)
2549 utilization_transmission (void *cls,
2550 const struct GNUNET_SCHEDULER_TaskContext *tc)
2552 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2554 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2555 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2557 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2558 utilization_transmission, NULL);
2564 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2565 const struct GNUNET_HELLO_Address *address,
2566 struct Session *session,
2567 const struct GNUNET_MessageHeader *message)
2569 struct NeighbourMapEntry *n;
2571 n = lookup_neighbour (peer);
2574 n->util_total_bytes_recv += ntohs(message->size);
2579 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2580 const struct GNUNET_HELLO_Address *address,
2581 struct Session *session,
2582 const struct GNUNET_MessageHeader *message)
2584 struct NeighbourMapEntry *n;
2585 n = lookup_neighbour (peer);
2588 n->util_payload_bytes_recv += ntohs(message->size);
2593 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2594 const struct GNUNET_HELLO_Address *address,
2595 struct Session *session,
2598 struct NeighbourMapEntry *n;
2599 n = lookup_neighbour (peer);
2602 if (n->primary_address.session != session)
2604 n->util_total_bytes_sent += size;
2609 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2612 struct NeighbourMapEntry *n;
2613 n = lookup_neighbour (peer);
2616 n->util_payload_bytes_sent += size;
2621 * Master task run for every neighbour. Performs all of the time-related
2622 * activities (keep alive, send next message, disconnect if idle, finish
2623 * clean up after disconnect).
2625 * @param cls the 'struct NeighbourMapEntry' for which we are running
2626 * @param tc scheduler context (unused)
2629 master_task (void *cls,
2630 const struct GNUNET_SCHEDULER_TaskContext *tc)
2632 struct NeighbourMapEntry *n = cls;
2633 struct GNUNET_TIME_Relative delay;
2635 n->task = GNUNET_SCHEDULER_NO_TASK;
2636 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2638 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2639 GNUNET_i2s (&n->id),
2640 GNUNET_TRANSPORT_ps2s(n->state),
2641 GNUNET_STRINGS_relative_time_to_string (delay,
2645 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2646 /* invalid state for master task, clean up */
2648 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2649 free_neighbour (n, GNUNET_NO);
2651 case GNUNET_TRANSPORT_PS_INIT_ATS:
2652 if (0 == delay.rel_value_us)
2654 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2655 "Connection to `%s' timed out waiting for ATS to provide address\n",
2656 GNUNET_i2s (&n->id));
2657 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2658 free_neighbour (n, GNUNET_NO);
2662 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2663 if (0 == delay.rel_value_us)
2665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2666 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2667 GNUNET_i2s (&n->id));
2668 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2669 free_neighbour (n, GNUNET_NO);
2673 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2674 if (0 == delay.rel_value_us)
2676 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2677 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2678 GNUNET_i2s (&n->id));
2679 /* We could not send to this address, delete address and session */
2680 if (NULL != n->primary_address.session)
2681 GNUNET_ATS_address_destroyed (GST_ats,
2682 n->primary_address.address, n->primary_address.session);
2683 GNUNET_ATS_address_destroyed (GST_ats,
2684 n->primary_address.address, NULL);
2685 disconnect_neighbour (n);
2689 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2690 if (0 == delay.rel_value_us)
2692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2693 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2694 GNUNET_i2s (&n->id));
2695 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2696 free_neighbour (n, GNUNET_NO);
2700 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2701 if (0 == delay.rel_value_us)
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2704 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2705 GNUNET_i2s (&n->id));
2706 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2707 free_neighbour (n, GNUNET_NO);
2711 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2712 if (0 == delay.rel_value_us)
2714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2715 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2716 GNUNET_i2s (&n->id));
2717 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2718 free_neighbour (n, GNUNET_NO);
2722 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2723 if (0 == delay.rel_value_us)
2725 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2726 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2727 GNUNET_i2s (&n->id));
2728 disconnect_neighbour (n);
2732 case GNUNET_TRANSPORT_PS_CONNECTED:
2733 if (0 == delay.rel_value_us)
2735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2736 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2737 GNUNET_i2s (&n->id));
2738 disconnect_neighbour (n);
2741 try_transmission_to_peer (n);
2744 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2745 if (0 == delay.rel_value_us)
2747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2748 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2749 GNUNET_i2s (&n->id));
2750 disconnect_neighbour (n);
2754 case GNUNET_TRANSPORT_PS_RECONNECT_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 replacement address\n",
2759 GNUNET_i2s (&n->id));
2760 disconnect_neighbour (n);
2764 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2765 if (0 == delay.rel_value_us)
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2768 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2769 GNUNET_i2s (&n->id));
2770 disconnect_neighbour (n);
2774 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2775 if (0 == delay.rel_value_us)
2777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2778 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2779 GNUNET_i2s (&n->id));
2780 disconnect_neighbour (n);
2783 try_transmission_to_peer (n);
2786 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2787 if (0 == delay.rel_value_us)
2789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2790 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2791 GNUNET_i2s (&n->id));
2792 disconnect_neighbour (n);
2795 try_transmission_to_peer (n);
2798 case GNUNET_TRANSPORT_PS_DISCONNECT:
2799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2800 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2801 GNUNET_i2s (&n->id));
2802 free_neighbour (n, GNUNET_NO);
2804 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2805 /* how did we get here!? */
2809 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2810 "Unhandled state `%s'\n",
2811 GNUNET_TRANSPORT_ps2s (n->state));
2815 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2816 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2817 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
2819 /* if we are *now* in one of these three states, we're sending
2820 keep alive messages, so we need to consider the keepalive
2821 delay, not just the connection timeout */
2822 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2825 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2826 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2833 * Send a SESSION_ACK message to the neighbour to confirm that we
2834 * got his CONNECT_ACK.
2836 * @param n neighbour to send the SESSION_ACK to
2839 send_session_ack_message (struct NeighbourMapEntry *n)
2841 struct GNUNET_MessageHeader msg;
2843 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2844 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2845 (void) send_with_session(n,
2846 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2847 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
2853 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2854 * Consider switching to it.
2856 * @param message possibly a 'struct SessionConnectMessage' (check format)
2857 * @param peer identity of the peer to switch the address for
2858 * @param address address of the other peer, NULL if other peer
2860 * @param session session to use (or NULL)
2861 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2864 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2865 const struct GNUNET_PeerIdentity *peer,
2866 const struct GNUNET_HELLO_Address *address,
2867 struct Session *session)
2869 const struct SessionConnectMessage *scm;
2870 struct GNUNET_TIME_Absolute ts;
2871 struct NeighbourMapEntry *n;
2873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2874 "Received CONNECT_ACK message from peer `%s'\n",
2877 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2879 GNUNET_break_op (0);
2880 return GNUNET_SYSERR;
2882 GNUNET_STATISTICS_update (GST_stats,
2884 ("# CONNECT_ACK messages received"),
2886 scm = (const struct SessionConnectMessage *) message;
2887 GNUNET_break_op (ntohl (scm->reserved) == 0);
2888 if (NULL == (n = lookup_neighbour (peer)))
2890 GNUNET_STATISTICS_update (GST_stats,
2892 ("# unexpected CONNECT_ACK messages (no peer)"),
2894 return GNUNET_SYSERR;
2896 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2899 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2901 free_neighbour (n, GNUNET_NO);
2902 return GNUNET_SYSERR;
2903 case GNUNET_TRANSPORT_PS_INIT_ATS:
2904 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2905 GNUNET_STATISTICS_update (GST_stats,
2907 ("# unexpected CONNECT_ACK messages (not ready)"),
2910 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2911 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
2913 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2914 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
2917 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
2918 GNUNET_STATISTICS_set (GST_stats,
2919 gettext_noop ("# peers connected"),
2920 ++neighbours_connected,
2922 connect_notify_cb (callback_cls, &n->id,
2923 n->primary_address.bandwidth_in,
2924 n->primary_address.bandwidth_out);
2925 /* Tell ATS that the outbound session we created to send CONNECT was successful */
2926 GST_ats_add_address (n->primary_address.address,
2927 n->primary_address.session,
2929 set_primary_address (n,
2930 n->primary_address.address,
2931 n->primary_address.session,
2932 n->primary_address.bandwidth_in,
2933 n->primary_address.bandwidth_out,
2935 send_session_ack_message (n);
2937 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2938 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2939 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2940 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2941 GNUNET_STATISTICS_update (GST_stats,
2943 ("# unexpected CONNECT_ACK messages (not ready)"),
2946 case GNUNET_TRANSPORT_PS_CONNECTED:
2947 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2948 send_session_ack_message (n);
2950 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2951 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2952 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2953 to give us a new address... */
2954 GNUNET_STATISTICS_update (GST_stats,
2956 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2959 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2960 /* new address worked; go back to connected! */
2961 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2962 send_session_ack_message (n);
2964 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2965 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2966 send_session_ack_message (n);
2968 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2969 /* new address worked; adopt it and go back to connected! */
2970 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
2971 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2973 GST_ats_add_address (n->alternative_address.address,
2974 n->alternative_address.session,
2976 set_primary_address (n, n->alternative_address.address,
2977 n->alternative_address.session, n->alternative_address.bandwidth_in,
2978 n->alternative_address.bandwidth_out, GNUNET_YES);
2980 free_address (&n->alternative_address);
2981 send_session_ack_message (n);
2983 case GNUNET_TRANSPORT_PS_DISCONNECT:
2984 GNUNET_STATISTICS_update (GST_stats,
2986 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2988 return GNUNET_SYSERR;
2989 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2994 "Unhandled state `%s'\n",
2995 GNUNET_TRANSPORT_ps2s (n->state));
2997 return GNUNET_SYSERR;
3004 * A session was terminated. Take note; if needed, try to get
3005 * an alternative address from ATS.
3007 * @param peer identity of the peer where the session died
3008 * @param session session that is gone
3009 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3010 * this session was not in use
3013 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3014 struct Session *session)
3016 struct NeighbourMapEntry *n;
3017 struct BlackListCheckContext *bcc;
3018 struct BlackListCheckContext *bcc_next;
3020 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3022 while (NULL != (bcc = bcc_next))
3024 bcc_next = bcc->next;
3025 if (bcc->na.session == session)
3027 if (NULL != bcc->bc)
3028 GST_blacklist_test_cancel (bcc->bc);
3029 GNUNET_HELLO_address_free (bcc->na.address);
3030 GNUNET_CONTAINER_DLL_remove (bc_head,
3036 if (NULL == (n = lookup_neighbour (peer)))
3037 return GNUNET_NO; /* can't affect us */
3038 if (session != n->primary_address.session)
3040 if (session == n->alternative_address.session)
3042 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3043 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3044 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3047 free_address (&n->alternative_address);
3049 return GNUNET_NO; /* doesn't affect us further */
3052 n->expect_latency_response = GNUNET_NO;
3055 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3057 free_neighbour (n, GNUNET_NO);
3059 case GNUNET_TRANSPORT_PS_INIT_ATS:
3061 free_neighbour (n, GNUNET_NO);
3063 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3064 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3065 free_address (&n->primary_address);
3066 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3068 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3069 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3070 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3071 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3072 /* error on inbound session; free neighbour entirely */
3073 free_address (&n->primary_address);
3074 free_neighbour (n, GNUNET_NO);
3076 case GNUNET_TRANSPORT_PS_CONNECTED:
3077 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3078 free_address (&n->primary_address);
3080 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3081 /* we don't have an address, how can it go down? */
3084 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3085 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3086 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3088 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3089 /* primary went down while we were checking secondary against
3090 blacklist, adopt secondary as primary */
3091 free_address (&n->primary_address);
3092 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3093 n->primary_address = n->alternative_address;
3094 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3096 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3097 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3098 secondary as primary */
3099 free_address (&n->primary_address);
3100 n->primary_address = n->alternative_address;
3101 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3102 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3104 case GNUNET_TRANSPORT_PS_DISCONNECT:
3105 free_address (&n->primary_address);
3107 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3108 /* neighbour was freed and plugins told to terminate session */
3112 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3113 "Unhandled state `%s'\n",
3114 GNUNET_TRANSPORT_ps2s (n->state));
3118 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3119 GNUNET_SCHEDULER_cancel (n->task);
3120 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3126 * We received a 'SESSION_ACK' message from the other peer.
3127 * If we sent a 'CONNECT_ACK' last, this means we are now
3128 * connected. Otherwise, do nothing.
3130 * @param message possibly a 'struct SessionConnectMessage' (check format)
3131 * @param peer identity of the peer to switch the address for
3132 * @param address address of the other peer, NULL if other peer
3134 * @param session session to use (or NULL)
3135 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3138 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3139 const struct GNUNET_PeerIdentity *peer,
3140 const struct GNUNET_HELLO_Address *address,
3141 struct Session *session)
3143 struct NeighbourMapEntry *n;
3145 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3146 "Received SESSION_ACK message from peer `%s'\n",
3148 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3150 GNUNET_break_op (0);
3151 return GNUNET_SYSERR;
3153 GNUNET_STATISTICS_update (GST_stats,
3155 ("# SESSION_ACK messages received"),
3157 if (NULL == (n = lookup_neighbour (peer)))
3159 GNUNET_break_op (0);
3160 return GNUNET_SYSERR;
3162 /* check if we are in a plausible state for having sent
3163 a CONNECT_ACK. If not, return, otherwise break */
3164 if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3165 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3166 (2 != n->send_connect_ack) )
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3171 GNUNET_TRANSPORT_ps2s (n->state),
3172 n->send_connect_ack);
3173 GNUNET_STATISTICS_update (GST_stats,
3174 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3178 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3179 GNUNET_STATISTICS_set (GST_stats,
3180 gettext_noop ("# peers connected"),
3181 ++neighbours_connected,
3183 connect_notify_cb (callback_cls, &n->id,
3184 n->primary_address.bandwidth_in,
3185 n->primary_address.bandwidth_out);
3187 GST_ats_add_address (n->primary_address.address,
3188 n->primary_address.session,
3190 set_primary_address (n,
3191 n->primary_address.address,
3192 n->primary_address.session,
3193 n->primary_address.bandwidth_in,
3194 n->primary_address.bandwidth_out,
3201 * Test if we're connected to the given peer.
3203 * @param target peer to test
3204 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3207 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3209 return test_connected (lookup_neighbour (target));
3214 * Change the incoming quota for the given peer.
3216 * @param neighbour identity of peer to change qutoa for
3217 * @param quota new quota
3220 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3221 struct GNUNET_BANDWIDTH_Value32NBO quota)
3223 struct NeighbourMapEntry *n;
3225 if (NULL == (n = lookup_neighbour (neighbour)))
3227 GNUNET_STATISTICS_update (GST_stats,
3229 ("# SET QUOTA messages ignored (no such peer)"),
3233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3234 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3235 ntohl (quota.value__), GNUNET_i2s (&n->id));
3236 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3237 if (0 != ntohl (quota.value__))
3239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3240 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3241 GNUNET_i2s (&n->id));
3242 if (GNUNET_YES == test_connected (n))
3243 GNUNET_STATISTICS_update (GST_stats,
3244 gettext_noop ("# disconnects due to quota of 0"),
3246 disconnect_neighbour (n);
3251 * We received a disconnect message from the given peer,
3252 * validate and process.
3254 * @param peer sender of the message
3255 * @param msg the disconnect message
3258 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3259 const struct GNUNET_MessageHeader *msg)
3261 struct NeighbourMapEntry *n;
3262 const struct SessionDisconnectMessage *sdm;
3264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3265 "Received DISCONNECT message from peer `%s'\n",
3267 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3269 // GNUNET_break_op (0);
3270 GNUNET_STATISTICS_update (GST_stats,
3272 ("# disconnect messages ignored (old format)"), 1,
3276 GNUNET_STATISTICS_update (GST_stats,
3278 ("# DISCONNECT messages received"),
3280 sdm = (const struct SessionDisconnectMessage *) msg;
3281 if (NULL == (n = lookup_neighbour (peer)))
3282 return; /* gone already */
3283 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3285 GNUNET_STATISTICS_update (GST_stats,
3287 ("# disconnect messages ignored (timestamp)"), 1,
3291 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3293 GNUNET_break_op (0);
3296 if (ntohl (sdm->purpose.size) !=
3297 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3298 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3299 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3301 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3302 "%s message from peer `%s' has invalid size \n",
3305 GNUNET_break_op (0);
3309 GNUNET_CRYPTO_eddsa_verify
3310 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3311 &sdm->signature, &sdm->public_key))
3313 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3314 "%s message from peer `%s' cannot be verified \n",
3317 GNUNET_break_op (0);
3320 if (GNUNET_YES == test_connected (n))
3321 GNUNET_STATISTICS_update (GST_stats,
3323 ("# other peer asked to disconnect from us"), 1,
3325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3326 "Disconnecting by request from peer %s\n",
3328 disconnect_neighbour (n);
3333 * Closure for the neighbours_iterate function.
3335 struct IteratorContext
3338 * Function to call on each connected neighbour.
3340 GST_NeighbourIterator cb;
3350 * Call the callback from the closure for each neighbour.
3352 * @param cls the `struct IteratorContext`
3353 * @param key the hash of the public key of the neighbour
3354 * @param value the `struct NeighbourMapEntry`
3355 * @return #GNUNET_OK (continue to iterate)
3358 neighbours_iterate (void *cls,
3359 const struct GNUNET_PeerIdentity *key,
3362 struct IteratorContext *ic = cls;
3363 struct NeighbourMapEntry *n = value;
3364 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3365 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3368 if (NULL != n->primary_address.address)
3370 bandwidth_in = n->primary_address.bandwidth_in;
3371 bandwidth_out = n->primary_address.bandwidth_out;
3375 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3376 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3380 n->primary_address.address,
3383 bandwidth_in, bandwidth_out);
3389 * Iterate over all connected neighbours.
3391 * @param cb function to call
3392 * @param cb_cls closure for cb
3395 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3397 struct IteratorContext ic;
3399 if (NULL == neighbours)
3400 return; /* can happen during shutdown */
3403 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3408 * If we have an active connection to the given target, it must be shutdown.
3410 * @param target peer to disconnect from
3413 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3415 struct NeighbourMapEntry *n;
3417 if (NULL == (n = lookup_neighbour (target)))
3418 return; /* not active */
3419 if (GNUNET_YES == test_connected (n))
3420 GNUNET_STATISTICS_update (GST_stats,
3422 ("# disconnected from peer upon explicit request"), 1,
3424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3425 "Forced disconnect from peer %s\n",
3426 GNUNET_i2s (target));
3427 disconnect_neighbour (n);
3432 * Obtain current latency information for the given neighbour.
3434 * @param peer to get the latency for
3435 * @return observed latency of the address, FOREVER if the
3436 * the connection is not up
3438 struct GNUNET_TIME_Relative
3439 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3441 struct NeighbourMapEntry *n;
3443 n = lookup_neighbour (peer);
3445 return GNUNET_TIME_UNIT_FOREVER_REL;
3448 case GNUNET_TRANSPORT_PS_CONNECTED:
3449 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3450 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3451 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3452 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3453 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3455 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3456 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3457 case GNUNET_TRANSPORT_PS_INIT_ATS:
3458 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3459 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3460 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3461 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3462 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3463 case GNUNET_TRANSPORT_PS_DISCONNECT:
3464 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3465 return GNUNET_TIME_UNIT_FOREVER_REL;
3467 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3468 "Unhandled state `%s'\n",
3469 GNUNET_TRANSPORT_ps2s (n->state));
3473 return GNUNET_TIME_UNIT_FOREVER_REL;
3478 * Obtain current address information for the given neighbour.
3481 * @return address currently used
3483 struct GNUNET_HELLO_Address *
3484 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3486 struct NeighbourMapEntry *n;
3488 n = lookup_neighbour (peer);
3491 return n->primary_address.address;
3496 * Initialize the neighbours subsystem.
3498 * @param cls closure for callbacks
3499 * @param connect_cb function to call if we connect to a peer
3500 * @param disconnect_cb function to call if we disconnect from a peer
3501 * @param peer_address_cb function to call if we change an active address
3503 * @param max_fds maximum number of fds to use
3506 GST_neighbours_start (void *cls,
3507 NotifyConnect connect_cb,
3508 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3509 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3510 unsigned int max_fds)
3513 connect_notify_cb = connect_cb;
3514 disconnect_notify_cb = disconnect_cb;
3515 neighbour_change_cb = peer_address_cb;
3516 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3517 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3518 utilization_transmission, NULL);
3523 * Disconnect from the given neighbour.
3526 * @param key hash of neighbour's public key (not used)
3527 * @param value the 'struct NeighbourMapEntry' of the neighbour
3528 * @return #GNUNET_OK (continue to iterate)
3531 disconnect_all_neighbours (void *cls,
3532 const struct GNUNET_PeerIdentity *key,
3535 struct NeighbourMapEntry *n = value;
3537 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3538 "Disconnecting peer `%4s', %s\n",
3539 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3540 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
3541 free_neighbour (n, GNUNET_NO);
3547 * Cleanup the neighbours subsystem.
3550 GST_neighbours_stop ()
3552 if (NULL == neighbours)
3554 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3556 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3557 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3560 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3561 &disconnect_all_neighbours,
3563 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3565 callback_cls = NULL;
3566 connect_notify_cb = NULL;
3567 disconnect_notify_cb = NULL;
3568 neighbour_change_cb = NULL;
3572 /* end of file gnunet-service-transport_neighbours.c */