2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport_clients.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
41 * Size of the neighbour hash map.
43 #define NEIGHBOUR_TABLE_SIZE 256
46 * Time we give plugin to transmit DISCONNECT message before the
47 * neighbour entry self-destructs.
49 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
52 * How often must a peer violate bandwidth quotas before we start
53 * to simply drop its messages?
55 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
58 * How long are we willing to wait for a response from ATS before timing out?
60 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
63 * How long are we willing to wait for an ACK from the other peer before
64 * giving up on our connect operation?
66 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
69 * How long are we willing to wait for a successful reconnect if
70 * an existing connection went down? Much shorter than the
71 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
72 * higher layers about the disconnect during this period.
74 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
77 * How long are we willing to wait for a response from the blacklist
78 * subsystem before timing out?
80 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
83 * Interval to send utilization data
85 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
87 GNUNET_NETWORK_STRUCT_BEGIN
90 * Message a peer sends to another to indicate that it intends to
91 * setup a connection/session for data exchange. A 'SESSION_CONNECT'
92 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
93 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
94 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
95 * should be connected.
97 struct SessionConnectMessage
100 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT
101 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK
103 struct GNUNET_MessageHeader header;
108 uint32_t reserved GNUNET_PACKED;
111 * Absolute time at the sender. Only the most recent connect
112 * message implies which session is preferred by the sender.
114 struct GNUNET_TIME_AbsoluteNBO timestamp;
120 * Message a peer sends to another when connected to indicate that a
121 * session is in use and the peer is still alive or to respond to a keep alive.
122 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
123 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
124 * When the keep alive response with type is received, transport service
125 * will call the respective plugin to update the session timeout
127 struct SessionKeepAliveMessage
130 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
131 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
133 struct GNUNET_MessageHeader header;
136 * A nonce to identify the session the keep alive is used for
138 uint32_t nonce GNUNET_PACKED;
142 * Message we send to the other peer to notify him that we intentionally
143 * are disconnecting (to reduce timeouts). This is just a friendly
144 * notification, peers must not rely on always receiving disconnect
147 struct SessionDisconnectMessage
150 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
152 struct GNUNET_MessageHeader header;
157 uint32_t reserved GNUNET_PACKED;
160 * Purpose of the signature. Extends over the timestamp.
161 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
163 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
166 * Absolute time at the sender. Only the most recent connect
167 * message implies which session is preferred by the sender.
169 struct GNUNET_TIME_AbsoluteNBO timestamp;
172 * Public key of the sender.
174 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
177 * Signature of the peer that sends us the disconnect. Only
178 * valid if the timestamp is AFTER the timestamp from the
179 * corresponding 'CONNECT' message.
181 struct GNUNET_CRYPTO_EddsaSignature signature;
185 GNUNET_NETWORK_STRUCT_END
189 * For each neighbour we keep a list of messages
190 * that we still want to transmit to the neighbour.
196 * This is a doubly linked list.
198 struct MessageQueue *next;
201 * This is a doubly linked list.
203 struct MessageQueue *prev;
206 * Function to call once we're done.
208 GST_NeighbourSendContinuation cont;
211 * Closure for @e cont
216 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
217 * stuck together in memory. Allocated at the end of this struct.
219 const char *message_buf;
222 * Size of the message buf
224 size_t message_buf_size;
227 * At what time should we fail?
229 struct GNUNET_TIME_Absolute timeout;
237 * A possible address we could use to communicate with a neighbour.
239 struct NeighbourAddress
243 * Active session for this address.
245 struct Session *session;
248 * Network-level address information.
250 struct GNUNET_HELLO_Address *address;
253 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
254 * peer for this address. Use to check that the ACK is in response
255 * to our most recent 'CONNECT'.
257 struct GNUNET_TIME_Absolute connect_timestamp;
260 * Inbound bandwidth from ATS for this address.
262 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
265 * Outbound bandwidth from ATS for this address.
267 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
270 * Did we tell ATS that this is our 'active' address?
275 * The current nonce sent in the last keep alive messages
277 uint32_t keep_alive_nonce;
282 * Entry in neighbours.
284 struct NeighbourMapEntry
288 * Head of list of messages we would like to send to this peer;
289 * must contain at most one message per client.
291 struct MessageQueue *messages_head;
294 * Tail of list of messages we would like to send to this peer; must
295 * contain at most one message per client.
297 struct MessageQueue *messages_tail;
300 * Are we currently trying to send a message? If so, which one?
302 struct MessageQueue *is_active;
305 * Primary address we currently use to communicate with the neighbour.
307 struct NeighbourAddress primary_address;
310 * Alternative address currently under consideration for communicating
311 * with the neighbour.
313 struct NeighbourAddress alternative_address;
316 * Identity of this neighbour.
318 struct GNUNET_PeerIdentity id;
321 * Main task that drives this peer (timeouts, keepalives, etc.).
322 * Always runs the 'master_task'.
324 GNUNET_SCHEDULER_TaskIdentifier task;
327 * At what time should we sent the next keep-alive message?
329 struct GNUNET_TIME_Absolute keep_alive_time;
332 * At what time did we sent the last keep-alive message? Used
333 * to calculate round-trip time ("latency").
335 struct GNUNET_TIME_Absolute last_keep_alive_time;
338 * Timestamp we should include in our next CONNECT_ACK message.
339 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
340 * our CONNECT_ACK message.
342 struct GNUNET_TIME_Absolute connect_ack_timestamp;
345 * ATS address suggest handle
347 struct GNUNET_ATS_SuggestHandle *suggest_handle;
350 * Time where we should cut the connection (timeout) if we don't
351 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
352 * if we are in S_CONNECTED).
354 struct GNUNET_TIME_Absolute timeout;
357 * Latest calculated latency value
359 struct GNUNET_TIME_Relative latency;
362 * Tracker for inbound bandwidth.
364 struct GNUNET_BANDWIDTH_Tracker in_tracker;
367 * How often has the other peer (recently) violated the inbound
368 * traffic limit? Incremented by 10 per violation, decremented by 1
369 * per non-violation (for each time interval).
371 unsigned int quota_violation_count;
374 * The current state of the peer.
376 enum GNUNET_TRANSPORT_PeerState state;
379 * Did we sent an KEEP_ALIVE message and are we expecting a response?
381 int expect_latency_response;
384 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
385 * (once we have an address to use and the peer has been allowed by our
386 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
387 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
388 * a 'SESSION_ACK' (regardless of what our own state machine might say).
390 int send_connect_ack;
393 * Tracking utilization of outbound bandwidth
395 uint32_t util_payload_bytes_sent;
398 * Tracking utilization of inbound bandwidth
400 uint32_t util_payload_bytes_recv;
403 * Tracking utilization of outbound bandwidth
405 uint32_t util_total_bytes_sent;
408 * Tracking utilization of inbound bandwidth
410 uint32_t util_total_bytes_recv;
413 * Date of last utilization transmission
415 struct GNUNET_TIME_Absolute last_util_transmission;
420 * Context for blacklist checks and the #handle_test_blacklist_cont()
421 * function. Stores information about ongoing blacklist checks.
423 struct BlackListCheckContext
427 * We keep blacklist checks in a DLL.
429 struct BlackListCheckContext *next;
432 * We keep blacklist checks in a DLL.
434 struct BlackListCheckContext *prev;
437 * Address that is being checked.
439 struct NeighbourAddress na;
442 * Handle to the ongoing blacklist check.
444 struct GST_BlacklistCheck *bc;
449 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
451 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
454 * We keep blacklist checks in a DLL so that we can find
455 * the 'sessions' in their 'struct NeighbourAddress' if
456 * a session goes down.
458 static struct BlackListCheckContext *bc_head;
461 * We keep blacklist checks in a DLL.
463 static struct BlackListCheckContext *bc_tail;
466 * List of pending blacklist checks: head
468 static struct BlacklistCheckSwitchContext *pending_bc_head;
471 * List of pending blacklist checks: tail
473 static struct BlacklistCheckSwitchContext *pending_bc_tail;
476 * Closure for #connect_notify_cb, #disconnect_notify_cb and #neighbour_change_cb
478 static void *callback_cls;
481 * Function to call when we connected to a neighbour.
483 static NotifyConnect connect_notify_cb;
486 * Function to call when we disconnected from a neighbour.
488 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
491 * Function to call when a neighbour changed address, state or bandwidth.
493 static GNUNET_TRANSPORT_NeighbourChangeCallback neighbour_change_cb;
496 * counter for connected neighbours
498 static unsigned int neighbours_connected;
501 * Number of bytes we have currently queued for transmission.
503 static unsigned long long bytes_in_send_queue;
506 * Task transmitting utilization data
508 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
512 * Lookup a neighbour entry in the neighbours hash map.
514 * @param pid identity of the peer to look up
515 * @return the entry, NULL if there is no existing record
517 static struct NeighbourMapEntry *
518 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
520 if (NULL == neighbours)
522 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
527 * Test if we're connected to the given peer.
529 * @param n neighbour entry of peer to test
530 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
533 test_connected (struct NeighbourMapEntry *n)
537 return GNUNET_TRANSPORT_is_connected (n->state);
541 * Send information about a new outbound quota to our clients.
543 * @param target affected peer
544 * @param quota new quota
547 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
548 struct GNUNET_BANDWIDTH_Value32NBO quota)
550 struct QuotaSetMessage q_msg;
552 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
553 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
554 ntohl (quota.value__), GNUNET_i2s (target));
555 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
556 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
558 q_msg.peer = (*target);
559 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
564 * We don't need a given neighbour address any more.
565 * Release its resources and give appropriate notifications
566 * to ATS and other subsystems.
568 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
571 free_address (struct NeighbourAddress *na)
573 if (GNUNET_YES == na->ats_active)
575 GST_validation_set_address_use (na->address, na->session, GNUNET_NO);
576 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
579 na->ats_active = GNUNET_NO;
580 na->keep_alive_nonce = 0;
581 if (NULL != na->address)
583 GNUNET_HELLO_address_free (na->address);
591 * Set net state for this neighbour and notify monitoring
593 * @param n the respective neighbour
594 * @param s the new state
597 set_state (struct NeighbourMapEntry *n, enum GNUNET_TRANSPORT_PeerState s)
600 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s\n",
602 GNUNET_TRANSPORT_ps2s(s));
603 neighbour_change_cb (callback_cls,
605 n->primary_address.address,
606 n->state, n->timeout,
607 n->primary_address.bandwidth_in,
608 n->primary_address.bandwidth_out);
613 * Set net state and state timeout for this neighbour and notify monitoring
615 * @param n the respective neighbour
616 * @param s the new state
617 * @param timeout the new timeout
620 set_state_and_timeout (struct NeighbourMapEntry *n,
621 enum GNUNET_TRANSPORT_PeerState s,
622 struct GNUNET_TIME_Absolute timeout)
625 n->timeout = timeout;
626 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed state to %s with timeout %s\n",
628 GNUNET_TRANSPORT_ps2s(s),
629 GNUNET_STRINGS_absolute_time_to_string (timeout));
630 neighbour_change_cb (callback_cls,
632 n->primary_address.address,
633 n->state, n->timeout,
634 n->primary_address.bandwidth_in,
635 n->primary_address.bandwidth_out);
640 * Set new state timeout for this neighbour and notify monitoring
642 * @param n the respective neighbour
643 * @param timeout the new timeout
646 set_timeout (struct NeighbourMapEntry *n,
647 struct GNUNET_TIME_Absolute timeout)
649 n->timeout = timeout;
650 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' changed timeout %s\n",
652 GNUNET_STRINGS_absolute_time_to_string (timeout));
653 neighbour_change_cb (callback_cls,
655 n->primary_address.address,
656 n->state, n->timeout,
657 n->primary_address.bandwidth_in,
658 n->primary_address.bandwidth_out);
663 * Initialize the alternative address of a neighbour
665 * @param n the neighbour
666 * @param address address of the other peer, NULL if other peer
668 * @param session session to use (or NULL, in which case an
669 * address must be setup)
670 * @param bandwidth_in inbound quota to be used when connection is up
671 * @param bandwidth_out outbound quota to be used when connection is up
674 set_alternative_address (struct NeighbourMapEntry *n,
675 const struct GNUNET_HELLO_Address *address,
676 struct Session *session,
677 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
678 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
680 struct GNUNET_TRANSPORT_PluginFunctions *papi;
681 if (NULL == (papi = GST_plugins_find (address->transport_name)))
686 if (session == n->alternative_address.session)
688 n->alternative_address.bandwidth_in = bandwidth_in;
689 n->alternative_address.bandwidth_out = bandwidth_out;
692 free_address (&n->alternative_address);
694 session = papi->get_session (papi->cls, address);
697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
698 "Failed to obtain new session for peer `%s' and address '%s'\n",
699 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
700 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
704 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' configured alternative address %s\n",
706 GST_plugins_a2s(address));
708 n->alternative_address.address = GNUNET_HELLO_address_copy (address);
709 n->alternative_address.bandwidth_in = bandwidth_in;
710 n->alternative_address.bandwidth_out = bandwidth_out;
711 n->alternative_address.session = session;
712 n->alternative_address.ats_active = GNUNET_NO;
713 n->alternative_address.keep_alive_nonce = 0;
718 * Initialize the primary address of a neighbour
720 * @param n the neighbour
721 * @param address address of the other peer, NULL if other peer
723 * @param session session to use (or NULL, in which case an
724 * address must be setup)
725 * @param bandwidth_in inbound quota to be used when connection is up
726 * @param bandwidth_out outbound quota to be used when connection is up
727 * @param is_active #GNUNET_YES to mark this as the active address with ATS
730 set_primary_address (struct NeighbourMapEntry *n,
731 const struct GNUNET_HELLO_Address *address,
732 struct Session *session,
733 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
734 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
737 struct GNUNET_TRANSPORT_PluginFunctions *papi;
739 if (NULL == (papi = GST_plugins_find (address->transport_name)))
744 if (session == n->primary_address.session)
746 n->primary_address.bandwidth_in = bandwidth_in;
747 n->primary_address.bandwidth_out = bandwidth_out;
748 if (is_active != n->primary_address.ats_active)
750 n->primary_address.ats_active = is_active;
751 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, is_active);
752 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, is_active);
754 if (GNUNET_YES == is_active)
756 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
757 send_outbound_quota (&address->peer, bandwidth_out);
761 free_address (&n->primary_address);
763 session = papi->get_session (papi->cls, address);
766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
767 "Failed to obtain new session for peer `%s' and address '%s'\n",
768 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
769 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
773 n->primary_address.address = GNUNET_HELLO_address_copy (address);
774 n->primary_address.bandwidth_in = bandwidth_in;
775 n->primary_address.bandwidth_out = bandwidth_out;
776 n->primary_address.session = session;
777 n->primary_address.ats_active = is_active;
778 n->primary_address.keep_alive_nonce = 0;
779 if (GNUNET_YES == is_active)
781 /* Telling ATS about new session */
782 GNUNET_ATS_address_in_use (GST_ats, n->primary_address.address, n->primary_address.session, GNUNET_YES);
783 GST_validation_set_address_use (n->primary_address.address, n->primary_address.session, GNUNET_YES);
784 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
785 send_outbound_quota (&address->peer, bandwidth_out);
788 GNUNET_log (GNUNET_ERROR_TYPE_INFO, "Neighbour `%s' switched to address `%s'\n",
790 GST_plugins_a2s(address));
792 neighbour_change_cb (callback_cls,
794 n->primary_address.address,
795 n->state, n->timeout,
796 n->primary_address.bandwidth_in,
797 n->primary_address.bandwidth_out);
801 * Clear the primary address of a neighbour since this primary address is not
802 * valid anymore and notify monitoring about it
804 * @param n the neighbour
807 unset_primary_address (struct NeighbourMapEntry *n)
814 * Free a neighbour map entry.
816 * @param n entry to free
817 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
818 * #GNUNET_YES to keep all sessions
821 free_neighbour (struct NeighbourMapEntry *n,
824 struct MessageQueue *mq;
825 struct GNUNET_TRANSPORT_PluginFunctions *papi;
826 struct GNUNET_HELLO_Address *backup_primary;
828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
829 "Freeing neighbur state of peer `%s'\n",
830 GNUNET_i2s (&n->id));
831 n->is_active = NULL; /* always free'd by its own continuation! */
833 /* fail messages currently in the queue */
834 while (NULL != (mq = n->messages_head))
836 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
837 if (NULL != mq->cont)
838 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
841 /* It is too late to send other peer disconnect notifications, but at
842 least internally we need to get clean... */
843 if (GNUNET_YES == test_connected (n))
845 GNUNET_STATISTICS_set (GST_stats,
846 gettext_noop ("# peers connected"),
847 --neighbours_connected,
849 disconnect_notify_cb (callback_cls, &n->id);
851 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
853 if (NULL != n->primary_address.address)
855 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
858 backup_primary = NULL;
860 /* free addresses and mark as unused */
861 free_address (&n->primary_address);
862 free_address (&n->alternative_address);
864 /* FIXME: Note that if we are switching between two TCP sessions to
865 the same peer, we might want to selectively kill only one of
866 them! Killing all sessions like this seems to be very, very
869 /* cut transport-level connection */
870 if ((GNUNET_NO == keep_sessions) &&
871 (NULL != backup_primary) &&
872 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
873 papi->disconnect_peer (papi->cls, &n->id);
875 GNUNET_free_non_null (backup_primary);
877 GNUNET_assert (GNUNET_YES ==
878 GNUNET_CONTAINER_multipeermap_remove (neighbours,
881 // FIXME-ATS-API: we might want to be more specific about
882 // which states we do this from in the future (ATS should
883 // have given us a 'suggest_address' handle, and if we have
884 // such a handle, we should cancel the operation here!
885 if (NULL != n->suggest_handle)
887 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
888 n->suggest_handle = NULL;
891 if (GNUNET_SCHEDULER_NO_TASK != n->task)
893 GNUNET_SCHEDULER_cancel (n->task);
894 n->task = GNUNET_SCHEDULER_NO_TASK;
896 /* free rest of memory */
902 * Transmit a message using the current session of the given
905 * @param n entry for the recipient
906 * @param msgbuf buffer to transmit
907 * @param msgbuf_size number of bytes in @a msgbuf buffer
908 * @param priority transmission priority
909 * @param timeout transmission timeout
910 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
911 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
912 * @param cont continuation to call when finished (can be NULL)
913 * @param cont_cls closure for @a cont
914 * @return timeout (copy of @a timeout or a calculated one if
915 * @a use_keepalive_timeout is #GNUNET_YES.
917 static struct GNUNET_TIME_Relative
918 send_with_session (struct NeighbourMapEntry *n,
919 const char *msgbuf, size_t msgbuf_size,
921 struct GNUNET_TIME_Relative timeout,
922 unsigned int use_keepalive_timeout,
923 GNUNET_TRANSPORT_TransmitContinuation cont,
926 struct GNUNET_TRANSPORT_PluginFunctions *papi;
927 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
929 GNUNET_assert (n->primary_address.session != NULL);
930 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
931 (-1 == papi->send (papi->cls,
932 n->primary_address.session,
935 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
936 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
937 papi->query_keepalive_factor (papi->cls))),
938 cont, cont_cls)))) &&
940 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
941 GST_neighbours_notify_data_sent (&n->id,
942 n->primary_address.address, n->primary_address.session, msgbuf_size);
943 GNUNET_break (NULL != papi);
949 * Master task run for every neighbour. Performs all of the time-related
950 * activities (keep alive, send next message, disconnect if idle, finish
951 * clean up after disconnect).
953 * @param cls the `struct NeighbourMapEntry` for which we are running
954 * @param tc scheduler context (unused)
957 master_task (void *cls,
958 const struct GNUNET_SCHEDULER_TaskContext *tc);
962 * Function called when the 'DISCONNECT' message has been sent by the
963 * plugin. Frees the neighbour --- if the entry still exists.
966 * @param target identity of the neighbour that was disconnected
967 * @param result #GNUNET_OK if the disconnect got out successfully
968 * @param payload bytes payload
969 * @param physical bytes physical
972 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
973 int result, size_t payload, size_t physical)
975 struct NeighbourMapEntry *n;
977 n = lookup_neighbour (target);
979 return; /* already gone */
980 if (GNUNET_TRANSPORT_PS_DISCONNECT != n->state)
981 return; /* have created a fresh entry since */
982 if (GNUNET_SCHEDULER_NO_TASK != n->task)
983 GNUNET_SCHEDULER_cancel (n->task);
984 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
989 * Transmit a DISCONNECT message to the other peer.
991 * @param n neighbour to send DISCONNECT message.
994 send_disconnect (struct NeighbourMapEntry *n)
996 struct SessionDisconnectMessage disconnect_msg;
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Sending DISCONNECT message to peer `%4s'\n",
1000 GNUNET_i2s (&n->id));
1001 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1002 disconnect_msg.header.type =
1003 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1004 disconnect_msg.reserved = htonl (0);
1005 disconnect_msg.purpose.size =
1006 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1007 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1008 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1009 disconnect_msg.purpose.purpose =
1010 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1011 disconnect_msg.timestamp =
1012 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1013 disconnect_msg.public_key = GST_my_identity.public_key;
1014 GNUNET_assert (GNUNET_OK ==
1015 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1016 &disconnect_msg.purpose,
1017 &disconnect_msg.signature));
1019 (void) send_with_session (n,
1020 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1021 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1022 GNUNET_NO, &send_disconnect_cont, NULL);
1023 GNUNET_STATISTICS_update (GST_stats,
1025 ("# DISCONNECT messages sent"), 1,
1031 * Disconnect from the given neighbour, clean up the record.
1033 * @param n neighbour to disconnect from
1036 disconnect_neighbour (struct NeighbourMapEntry *n)
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1039 "Disconnecting from peer %s in state %s\n",
1040 GNUNET_i2s (&n->id),
1041 GNUNET_TRANSPORT_ps2s (n->state));
1042 /* depending on state, notify neighbour and/or upper layers of this peer
1046 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1047 case GNUNET_TRANSPORT_PS_INIT_ATS:
1048 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1049 /* other peer is completely unaware of us, no need to send DISCONNECT */
1050 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1051 free_neighbour (n, GNUNET_NO);
1053 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1054 send_disconnect (n);
1055 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1057 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1058 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1059 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1060 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1061 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
1062 free_neighbour (n, GNUNET_NO);
1064 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1065 /* we DID ACK the other peer's request, must send DISCONNECT */
1066 send_disconnect (n);
1067 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1069 case GNUNET_TRANSPORT_PS_CONNECTED:
1070 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1071 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1072 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1073 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1074 /* we are currently connected, need to send disconnect and do
1075 internal notifications and update statistics */
1076 send_disconnect (n);
1077 GNUNET_STATISTICS_set (GST_stats,
1078 gettext_noop ("# peers connected"),
1079 --neighbours_connected,
1081 disconnect_notify_cb (callback_cls, &n->id);
1082 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1084 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1085 /* ATS address request timeout, disconnect without sending disconnect message */
1086 GNUNET_STATISTICS_set (GST_stats,
1087 gettext_noop ("# peers connected"),
1088 --neighbours_connected,
1090 disconnect_notify_cb (callback_cls, &n->id);
1091 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT);
1093 case GNUNET_TRANSPORT_PS_DISCONNECT:
1094 /* already disconnected, ignore */
1096 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1097 /* already cleaned up, how did we get here!? */
1101 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1102 "Unhandled state `%s'\n",
1103 GNUNET_TRANSPORT_ps2s (n->state));
1107 /* schedule timeout to clean up */
1108 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1109 GNUNET_SCHEDULER_cancel (n->task);
1110 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1116 * We're done with our transmission attempt, continue processing.
1118 * @param cls the `struct MessageQueue` of the message
1119 * @param receiver intended receiver
1120 * @param success whether it worked or not
1121 * @param size_payload bytes payload sent
1122 * @param physical bytes sent on wire
1125 transmit_send_continuation (void *cls,
1126 const struct GNUNET_PeerIdentity *receiver,
1127 int success, size_t size_payload, size_t physical)
1129 struct MessageQueue *mq = cls;
1130 struct NeighbourMapEntry *n;
1132 if (NULL == (n = lookup_neighbour (receiver)))
1135 return; /* disconnect or other error while transmitting, can happen */
1137 if (n->is_active == mq)
1139 /* this is still "our" neighbour, remove us from its queue
1140 and allow it to send the next message now */
1141 n->is_active = NULL;
1142 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1143 GNUNET_SCHEDULER_cancel (n->task);
1144 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1146 if (bytes_in_send_queue < mq->message_buf_size)
1148 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1149 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1150 bytes_in_send_queue, mq->message_buf_size,
1151 (GNUNET_OK == success) ? "OK" : "FAIL",
1152 size_payload, physical);
1157 GNUNET_break (size_payload == mq->message_buf_size);
1158 bytes_in_send_queue -= mq->message_buf_size;
1159 GNUNET_STATISTICS_set (GST_stats,
1161 ("# bytes in message queue for other peers"),
1162 bytes_in_send_queue, GNUNET_NO);
1163 if (GNUNET_OK == success)
1164 GNUNET_STATISTICS_update (GST_stats,
1166 ("# messages transmitted to other peers"),
1169 GNUNET_STATISTICS_update (GST_stats,
1171 ("# transmission failures for messages to other peers"),
1173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1174 "Sending message to `%s' of type %u was a %s\n",
1175 GNUNET_i2s (receiver),
1176 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1177 (success == GNUNET_OK) ? "success" : "FAILURE");
1178 if (NULL != mq->cont)
1179 mq->cont (mq->cont_cls, success, size_payload, physical);
1185 * Check the message list for the given neighbour and if we can
1186 * send a message, do so. This function should only be called
1187 * if the connection is at least generally ready for transmission.
1188 * While we will only send one message at a time, no bandwidth
1189 * quota management is performed here. If a message was given to
1190 * the plugin, the continuation will automatically re-schedule
1191 * the 'master' task once the next message might be transmitted.
1193 * @param n target peer for which to transmit
1196 try_transmission_to_peer (struct NeighbourMapEntry *n)
1198 struct MessageQueue *mq;
1199 struct GNUNET_TIME_Relative timeout;
1201 if (NULL == n->primary_address.address)
1203 /* no address, why are we here? */
1207 if ((0 == n->primary_address.address->address_length) &&
1208 (NULL == n->primary_address.session))
1210 /* no address, why are we here? */
1214 if (NULL != n->is_active)
1216 /* transmission already pending */
1220 /* timeout messages from the queue that are past their due date */
1221 while (NULL != (mq = n->messages_head))
1223 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1224 if (timeout.rel_value_us > 0)
1226 GNUNET_STATISTICS_update (GST_stats,
1228 ("# messages timed out while in transport queue"),
1230 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1232 transmit_send_continuation (mq, &n->id,
1234 mq->message_buf_size, 0); /* timeout */
1237 return; /* no more messages */
1238 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1240 (void) send_with_session (n,
1241 mq->message_buf, mq->message_buf_size,
1242 0 /* priority */, timeout, GNUNET_NO,
1243 &transmit_send_continuation, mq);
1248 * Send keepalive message to the neighbour. Must only be called
1249 * if we are on 'connected' state or while trying to switch addresses.
1250 * Will internally determine if a keepalive is truly needed (so can
1251 * always be called).
1253 * @param n neighbour that went idle and needs a keepalive
1256 send_keepalive (struct NeighbourMapEntry *n)
1258 struct SessionKeepAliveMessage m;
1259 struct GNUNET_TIME_Relative timeout;
1262 GNUNET_assert ((GNUNET_TRANSPORT_PS_CONNECTED == n->state) ||
1263 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1264 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT));
1265 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1266 return; /* no keepalive needed at this time */
1268 nonce = 0; /* 0 indicates 'not set' */
1270 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "Sending keep alive to peer `%s' with nonce %u\n",
1274 GNUNET_i2s (&n->id), nonce);
1276 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1277 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1278 m.nonce = htonl (nonce);
1280 timeout = send_with_session (n,
1281 (const void *) &m, sizeof (m),
1282 UINT32_MAX /* priority */,
1283 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1285 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1287 n->primary_address.keep_alive_nonce = nonce;
1288 n->expect_latency_response = GNUNET_YES;
1289 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1290 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1296 * Keep the connection to the given neighbour alive longer,
1297 * we received a KEEPALIVE (or equivalent); send a response.
1299 * @param neighbour neighbour to keep alive (by sending keep alive response)
1300 * @param m the keep alive message containing the nonce to respond to
1303 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1304 const struct GNUNET_MessageHeader *m)
1306 struct NeighbourMapEntry *n;
1307 const struct SessionKeepAliveMessage *msg_in;
1308 struct SessionKeepAliveMessage msg;
1310 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1313 msg_in = (struct SessionKeepAliveMessage *) m;
1314 if (NULL == (n = lookup_neighbour (neighbour)))
1316 GNUNET_STATISTICS_update (GST_stats,
1318 ("# KEEPALIVE messages discarded (peer unknown)"),
1322 if (NULL == n->primary_address.session)
1324 GNUNET_STATISTICS_update (GST_stats,
1326 ("# KEEPALIVE messages discarded (no session)"),
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "Received keep alive request from peer `%s' with nonce %u\n",
1333 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1335 /* send reply to allow neighbour to measure latency */
1336 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1337 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1338 msg.nonce = msg_in->nonce;
1339 (void) send_with_session(n,
1340 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1341 UINT32_MAX /* priority */,
1342 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1348 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1349 * latency to this peer. Pass the updated information (existing ats
1350 * plus calculated latency) to ATS.
1352 * @param neighbour neighbour to keep alive
1353 * @param m the message containing the keep alive response
1356 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1357 const struct GNUNET_MessageHeader *m)
1359 struct NeighbourMapEntry *n;
1360 const struct SessionKeepAliveMessage *msg;
1361 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1363 struct GNUNET_ATS_Information ats;
1365 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1368 msg = (const struct SessionKeepAliveMessage *) m;
1369 if (NULL == (n = lookup_neighbour (neighbour)))
1371 GNUNET_STATISTICS_update (GST_stats,
1373 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1377 if ( (GNUNET_TRANSPORT_PS_CONNECTED != n->state) ||
1378 (GNUNET_YES != n->expect_latency_response) )
1380 GNUNET_STATISTICS_update (GST_stats,
1382 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1386 if (NULL == n->primary_address.address)
1388 GNUNET_STATISTICS_update (GST_stats,
1390 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1394 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1396 GNUNET_STATISTICS_update (GST_stats,
1398 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1404 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1405 "Received keep alive response from peer `%s' for session %p\n",
1406 GNUNET_i2s (&n->id), n->primary_address.session);
1410 /* Update session timeout here */
1411 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1414 "Updating session for peer `%s' for session %p\n",
1415 GNUNET_i2s (&n->id), n->primary_address.session);
1416 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1423 n->primary_address.keep_alive_nonce = 0;
1424 n->expect_latency_response = GNUNET_NO;
1425 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1426 set_timeout (n, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Latency for peer `%s' is %s\n",
1430 GNUNET_i2s (&n->id),
1431 GNUNET_STRINGS_relative_time_to_string (n->latency,
1433 /* append latency */
1434 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1435 if (n->latency.rel_value_us > UINT32_MAX)
1436 latency = UINT32_MAX;
1438 latency = n->latency.rel_value_us;
1439 ats.value = htonl (latency);
1440 GST_ats_update_metrics (&n->id, n->primary_address.address,
1441 n->primary_address.session, &ats, 1);
1446 * We have received a message from the given sender. How long should
1447 * we delay before receiving more? (Also used to keep the peer marked
1450 * @param sender sender of the message
1451 * @param size size of the message
1452 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1453 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1454 * #GNUNET_SYSERR if the connection is not fully up yet
1455 * @return how long to wait before reading more from this sender
1457 struct GNUNET_TIME_Relative
1458 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1459 *sender, ssize_t size, int *do_forward)
1461 struct NeighbourMapEntry *n;
1462 struct GNUNET_TIME_Relative ret;
1464 if (NULL == neighbours)
1466 *do_forward = GNUNET_NO;
1467 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1469 if (NULL == (n = lookup_neighbour (sender)))
1471 GST_neighbours_try_connect (sender);
1472 if (NULL == (n = lookup_neighbour (sender)))
1474 GNUNET_STATISTICS_update (GST_stats,
1476 ("# messages discarded due to lack of neighbour record"),
1478 *do_forward = GNUNET_NO;
1479 return GNUNET_TIME_UNIT_ZERO;
1482 if (! test_connected (n))
1484 *do_forward = GNUNET_SYSERR;
1485 return GNUNET_TIME_UNIT_ZERO;
1487 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1489 n->quota_violation_count++;
1490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1491 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1492 n->in_tracker.available_bytes_per_s__,
1493 n->quota_violation_count);
1494 /* Discount 32k per violation */
1495 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1499 if (n->quota_violation_count > 0)
1501 /* try to add 32k back */
1502 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1503 n->quota_violation_count--;
1506 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1508 GNUNET_STATISTICS_update (GST_stats,
1510 ("# bandwidth quota violations by other peers"),
1512 *do_forward = GNUNET_NO;
1513 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1515 *do_forward = GNUNET_YES;
1516 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1517 if (ret.rel_value_us > 0)
1519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1520 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1521 (unsigned long long) n->in_tracker.
1522 consumption_since_last_update__,
1523 (unsigned int) n->in_tracker.available_bytes_per_s__,
1524 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1525 GNUNET_STATISTICS_update (GST_stats,
1526 gettext_noop ("# ms throttling suggested"),
1527 (int64_t) ret.rel_value_us / 1000LL,
1535 * Transmit a message to the given target using the active connection.
1537 * @param target destination
1538 * @param msg message to send
1539 * @param msg_size number of bytes in msg
1540 * @param timeout when to fail with timeout
1541 * @param cont function to call when done
1542 * @param cont_cls closure for 'cont'
1545 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1546 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1547 GST_NeighbourSendContinuation cont, void *cont_cls)
1549 struct NeighbourMapEntry *n;
1550 struct MessageQueue *mq;
1552 /* All ove these cases should never happen; they are all API violations.
1553 But we check anyway, just to be sure. */
1554 if (NULL == (n = lookup_neighbour (target)))
1558 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1561 if (GNUNET_YES != test_connected (n))
1565 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1568 bytes_in_send_queue += msg_size;
1569 GNUNET_STATISTICS_set (GST_stats,
1571 ("# bytes in message queue for other peers"),
1572 bytes_in_send_queue, GNUNET_NO);
1573 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1575 mq->cont_cls = cont_cls;
1576 memcpy (&mq[1], msg, msg_size);
1577 mq->message_buf = (const char *) &mq[1];
1578 mq->message_buf_size = msg_size;
1579 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1580 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1581 if ( (NULL != n->is_active) ||
1582 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1584 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1585 GNUNET_SCHEDULER_cancel (n->task);
1586 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1591 * Send a SESSION_CONNECT message via the given address.
1593 * @param na address to use
1596 send_session_connect (struct NeighbourAddress *na)
1598 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1599 struct SessionConnectMessage connect_msg;
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Sending SESSION_CONNECT message to peer %s\n",
1603 GNUNET_i2s (&na->address->peer));
1604 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1609 if (NULL == na->session)
1610 na->session = papi->get_session (papi->cls, na->address);
1611 if (NULL == na->session)
1616 GNUNET_STATISTICS_update (GST_stats,
1618 ("# SESSION_CONNECT messages sent"),
1620 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1621 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1622 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1623 connect_msg.reserved = htonl (0);
1624 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1626 papi->send (papi->cls,
1628 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1630 GNUNET_TIME_UNIT_FOREVER_REL,
1633 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1634 _("Failed to transmit CONNECT message via plugin to %s\n"),
1635 GST_plugins_a2s (na->address));
1637 GST_neighbours_notify_data_sent (&na->address->peer,
1640 sizeof (struct SessionConnectMessage));
1646 * Send a SESSION_CONNECT_ACK message via the given address.
1648 * @param address address to use
1649 * @param session session to use
1650 * @param timestamp timestamp to use for the ACK message
1653 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1654 struct Session *session,
1655 struct GNUNET_TIME_Absolute timestamp)
1657 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1658 struct SessionConnectMessage connect_msg;
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 "Sending CONNECT_ACK to peer `%s'\n",
1662 GNUNET_i2s (&address->peer));
1663 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1668 if (NULL == session)
1669 session = papi->get_session (papi->cls, address);
1670 if (NULL == session)
1675 GNUNET_STATISTICS_update (GST_stats,
1677 ("# CONNECT_ACK messages sent"),
1679 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1680 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1681 connect_msg.reserved = htonl (0);
1682 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1683 (void) papi->send (papi->cls,
1685 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1687 GNUNET_TIME_UNIT_FOREVER_REL,
1694 * Create a fresh entry in the neighbour map for the given peer
1696 * @param peer peer to create an entry for
1697 * @return new neighbour map entry
1699 static struct NeighbourMapEntry *
1700 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1702 struct NeighbourMapEntry *n;
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "Creating new neighbour entry for `%s'\n",
1707 n = GNUNET_new (struct NeighbourMapEntry);
1709 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1710 n->last_util_transmission = GNUNET_TIME_absolute_get();
1711 n->util_payload_bytes_recv = 0;
1712 n->util_payload_bytes_sent = 0;
1713 n->util_total_bytes_recv = 0;
1714 n->util_total_bytes_sent = 0;
1715 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1716 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1717 MAX_BANDWIDTH_CARRY_S);
1718 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1719 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_NOT_CONNECTED, GNUNET_TIME_UNIT_FOREVER_ABS);
1720 GNUNET_assert (GNUNET_OK ==
1721 GNUNET_CONTAINER_multipeermap_put (neighbours,
1723 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1729 * Check if the two given addresses are the same.
1730 * Actually only checks if the sessions are non-NULL
1731 * (which they should be) and then if they are identical;
1732 * the actual addresses don't matter if the session
1733 * pointers match anyway, and we must have session pointers
1736 * @param a1 first address to compare
1737 * @param a2 other address to compare
1738 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1741 address_matches (const struct NeighbourAddress *a1,
1742 const struct NeighbourAddress *a2)
1744 if ( (NULL == a1->session) ||
1745 (NULL == a2->session) )
1750 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1755 address_suggest_cont (void *cls,
1756 const struct GNUNET_PeerIdentity *peer,
1757 const struct GNUNET_HELLO_Address *address, struct Session *session,
1758 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
1759 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
1760 const struct GNUNET_ATS_Information *ats, uint32_t ats_count)
1766 struct BlacklistCheckSwitchContext
1768 struct BlacklistCheckSwitchContext *prev;
1769 struct BlacklistCheckSwitchContext *next;
1772 struct GST_BlacklistCheck *blc;
1774 struct GNUNET_HELLO_Address *address;
1775 struct Session *session;
1776 struct GNUNET_ATS_Information *ats;
1779 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
1780 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
1785 try_connect_bl_check_cont (void *cls,
1786 const struct GNUNET_PeerIdentity *peer, int result)
1788 struct BlacklistCheckSwitchContext *blc_ctx = cls;
1789 struct NeighbourMapEntry *n;
1791 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
1792 GNUNET_free (blc_ctx);
1794 if (GNUNET_OK != result)
1796 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1797 _("Blacklisting dissaproved to connect to peer `%s'\n"),
1801 n = setup_neighbour (peer);
1802 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1804 GNUNET_ATS_reset_backoff (GST_ats, peer);
1805 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer,
1806 &address_suggest_cont, n);
1812 * Try to create a connection to the given target (eventually).
1814 * @param target peer to try to connect to
1817 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1819 struct NeighbourMapEntry *n;
1820 struct GST_BlacklistCheck *blc;
1821 struct BlacklistCheckSwitchContext *blc_ctx;
1823 if (NULL == neighbours)
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "Asked to connect to peer `%s' during shutdown\n",
1827 GNUNET_i2s (target));
1828 return; /* during shutdown, do nothing */
1830 n = lookup_neighbour (target);
1831 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1832 "Asked to connect to peer `%s' (state: %s)\n",
1833 GNUNET_i2s (target),
1834 (NULL != n) ? GNUNET_TRANSPORT_ps2s(n->state) : "NEW PEER");
1839 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1840 /* this should not be possible */
1842 free_neighbour (n, GNUNET_NO);
1844 case GNUNET_TRANSPORT_PS_INIT_ATS:
1845 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1846 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1847 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1848 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
1849 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
1850 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
1851 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1852 "Ignoring request to try to connect to `%s', already trying!\n",
1853 GNUNET_i2s (target));
1854 return; /* already trying */
1855 case GNUNET_TRANSPORT_PS_CONNECTED:
1856 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
1857 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
1858 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
1859 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
1860 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
1861 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1862 "Ignoring request to try to connect, already connected to `%s'!\n",
1863 GNUNET_i2s (target));
1864 return; /* already connected */
1865 case GNUNET_TRANSPORT_PS_DISCONNECT:
1866 /* get rid of remains, ready to re-try immediately */
1867 free_neighbour (n, GNUNET_NO);
1869 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
1870 /* should not be possible */
1873 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1874 "Unhandled state `%s'\n",
1875 GNUNET_TRANSPORT_ps2s (n->state));
1877 free_neighbour (n, GNUNET_NO);
1882 /* Do blacklist check if connection is allowed */
1883 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
1884 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
1886 if (NULL != (blc = GST_blacklist_test_allowed (target, NULL,
1887 &try_connect_bl_check_cont, blc_ctx)))
1895 * Function called with the result of a blacklist check.
1897 * @param cls closure with the `struct BlackListCheckContext`
1898 * @param peer peer this check affects
1899 * @param result #GNUNET_OK if the address is allowed
1902 handle_test_blacklist_cont (void *cls,
1903 const struct GNUNET_PeerIdentity *peer,
1906 struct BlackListCheckContext *bcc = cls;
1907 struct NeighbourMapEntry *n;
1910 GNUNET_CONTAINER_DLL_remove (bc_head,
1913 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1914 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1916 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1917 if (GNUNET_OK == result)
1919 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
1923 /* Blacklist disagreed on connecting to a peer with this address
1924 * Destroy address because we are not allowed to use it
1926 if (NULL != bcc->na.session)
1927 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1928 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1930 if (NULL == (n = lookup_neighbour (peer)))
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1933 "No neighbor entry for peer `%s', ignoring blacklist result\n",
1935 goto cleanup; /* nobody left to care about new address */
1937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1938 "Received blacklist result for peer `%s' in state %s/%d\n",
1940 GNUNET_TRANSPORT_ps2s (n->state),
1941 n->send_connect_ack);
1944 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
1945 /* this should not be possible */
1947 free_neighbour (n, GNUNET_NO);
1949 case GNUNET_TRANSPORT_PS_INIT_ATS:
1950 /* waiting on ATS suggestion; still, pass address to ATS as a
1953 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
1954 /* check if the address the blacklist was fine with matches
1955 ATS suggestion, if so, we can move on! */
1956 if ( (GNUNET_OK == result) &&
1957 (1 == n->send_connect_ack) )
1959 n->send_connect_ack = 2;
1960 send_session_connect_ack_message (bcc->na.address,
1962 n->connect_ack_timestamp);
1964 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "Blacklist result for peer %s is for non-primary address, ignored\n",
1969 break; /* result for an address we currently don't care about */
1971 if (GNUNET_OK == result)
1973 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_SENT, GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
1974 send_session_connect (&n->primary_address);
1978 free_address (&n->primary_address);
1979 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1982 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
1983 /* waiting on CONNECT_ACK, send ACK if one is pending */
1984 if ( (GNUNET_OK == result) &&
1985 (1 == n->send_connect_ack) )
1987 n->send_connect_ack = 2;
1988 send_session_connect_ack_message (n->primary_address.address,
1989 n->primary_address.session,
1990 n->connect_ack_timestamp);
1993 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
1994 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
1995 GNUNET_ATS_reset_backoff (GST_ats, peer);
1996 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1997 "Suggesting address for peer %s to ATS\n",
1999 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer, &address_suggest_cont, n);
2001 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2002 /* waiting on ATS suggestion, don't care about blacklist */
2004 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2005 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2007 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2008 "Blacklist result ignored, as it is not for our primary address\n");
2009 break; /* result for an address we currently don't care about */
2011 if (GNUNET_OK == result)
2013 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK, GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT));
2014 send_session_connect_ack_message (bcc->na.address,
2016 n->connect_ack_timestamp);
2017 if (1 == n->send_connect_ack)
2018 n->send_connect_ack = 2;
2022 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2024 plugin = GST_plugins_find (bcc->na.address->transport_name);
2025 if ( (NULL != plugin) &&
2026 (NULL != bcc->na.session) )
2028 plugin->disconnect_session (plugin->cls,
2032 GNUNET_break (NULL != plugin);
2033 free_address (&n->primary_address);
2034 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2035 GNUNET_ATS_reset_backoff (GST_ats, peer);
2038 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2039 /* waiting on SESSION_ACK, send ACK if one is pending */
2040 if ( (GNUNET_OK == result) &&
2041 (1 == n->send_connect_ack) )
2043 n->send_connect_ack = 2;
2044 send_session_connect_ack_message (n->primary_address.address,
2045 n->primary_address.session,
2046 n->connect_ack_timestamp);
2049 case GNUNET_TRANSPORT_PS_CONNECTED:
2050 /* already connected, don't care about blacklist */
2052 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2053 /* still waiting on ATS suggestion, don't care about blacklist */
2055 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2056 if ( (GNUNET_OK == result) &&
2057 (1 == n->send_connect_ack) )
2059 n->send_connect_ack = 2;
2060 send_session_connect_ack_message (bcc->na.address,
2062 n->connect_ack_timestamp);
2064 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2067 "Blacklist result ignored, as it is not for our primary address\n");
2068 break; /* result for an address we currently don't care about */
2070 if (GNUNET_OK == result)
2072 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
2073 send_session_connect (&n->primary_address);
2077 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
2080 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2081 /* waiting on CONNECT_ACK, don't care about blacklist */
2082 if ( (GNUNET_OK == result) &&
2083 (1 == n->send_connect_ack) )
2085 n->send_connect_ack = 2;
2086 send_session_connect_ack_message (n->primary_address.address,
2087 n->primary_address.session,
2088 n->connect_ack_timestamp);
2091 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2092 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2094 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2095 "Blacklist result ignored, as it is not for our primary address\n");
2096 break; /* result for an address we currently don't care about */
2098 if (GNUNET_OK == result)
2100 send_session_connect (&n->alternative_address);
2101 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT);
2105 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2106 free_address (&n->alternative_address);
2109 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2110 /* waiting on CONNECT_ACK, don't care about blacklist */
2111 if ( (GNUNET_OK == result) &&
2112 (1 == n->send_connect_ack) )
2114 n->send_connect_ack = 2;
2115 send_session_connect_ack_message (n->primary_address.address,
2116 n->primary_address.session,
2117 n->connect_ack_timestamp);
2120 case GNUNET_TRANSPORT_PS_DISCONNECT:
2121 /* Nothing to do here, ATS will already do what can be done */
2123 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2124 /* should not be possible */
2128 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2129 "Unhandled state `%s'\n",
2130 GNUNET_TRANSPORT_ps2s (n->state));
2132 free_neighbour (n, GNUNET_NO);
2136 GNUNET_HELLO_address_free (bcc->na.address);
2142 * We want to know if connecting to a particular peer via
2143 * a particular address is allowed. Check it!
2145 * @param peer identity of the peer to switch the address for
2146 * @param ts time at which the check was initiated
2147 * @param address address of the other peer, NULL if other peer
2149 * @param session session to use (or NULL)
2152 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2153 struct GNUNET_TIME_Absolute ts,
2154 const struct GNUNET_HELLO_Address *address,
2155 struct Session *session)
2157 struct BlackListCheckContext *bcc;
2158 struct GST_BlacklistCheck *bc;
2160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2161 "Checking peer `%s' against blacklist\n",
2163 bcc = GNUNET_new (struct BlackListCheckContext);
2164 bcc->na.address = GNUNET_HELLO_address_copy (address);
2165 bcc->na.session = session;
2166 bcc->na.connect_timestamp = ts;
2167 GNUNET_CONTAINER_DLL_insert (bc_head,
2170 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2171 address->transport_name,
2172 &handle_test_blacklist_cont, bcc)))
2174 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2175 we must only store 'bc' if 'bc' is non-NULL... */
2180 * We received a 'SESSION_CONNECT' message from the other peer.
2181 * Consider switching to it.
2183 * @param message possibly a 'struct SessionConnectMessage' (check format)
2184 * @param peer identity of the peer to switch the address for
2185 * @param address address of the other peer, NULL if other peer
2187 * @param session session to use (or NULL)
2188 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2191 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2192 const struct GNUNET_PeerIdentity *peer,
2193 const struct GNUNET_HELLO_Address *address,
2194 struct Session *session)
2196 const struct SessionConnectMessage *scm;
2197 struct NeighbourMapEntry *n;
2198 struct GNUNET_TIME_Absolute ts;
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2201 "Received CONNECT message from peer `%s'\n",
2203 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2205 GNUNET_break_op (0);
2206 return GNUNET_SYSERR;
2208 GNUNET_STATISTICS_update (GST_stats,
2210 ("# CONNECT messages received"),
2212 if (NULL == neighbours)
2214 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2215 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2217 return GNUNET_OK; /* we're shutting down */
2219 scm = (const struct SessionConnectMessage *) message;
2220 GNUNET_break_op (0 == ntohl (scm->reserved));
2221 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2222 n = lookup_neighbour (peer);
2224 n = setup_neighbour (peer);
2225 n->send_connect_ack = 1;
2226 n->connect_ack_timestamp = ts;
2228 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2229 "Received SESSION_CONNECT for peer `%s' in state %s/%d\n",
2231 GNUNET_TRANSPORT_ps2s (n->state),
2232 n->send_connect_ack);
2235 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2236 /* Do a blacklist check for the new address */
2237 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2238 check_blacklist (peer, ts, address, session);
2240 case GNUNET_TRANSPORT_PS_INIT_ATS:
2241 /* CONNECT message takes priority over us asking ATS for address */
2242 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND);
2244 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2245 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2246 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2247 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2248 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2249 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2250 /* It can never hurt to have an alternative address in the above cases,
2251 see if it is allowed */
2252 check_blacklist (peer, ts, address, session);
2254 case GNUNET_TRANSPORT_PS_CONNECTED:
2255 /* we are already connected and can thus send the ACK immediately;
2256 still, it can never hurt to have an alternative address, so also
2257 tell ATS about it */
2258 GNUNET_assert (NULL != n->primary_address.address);
2259 GNUNET_assert (NULL != n->primary_address.session);
2260 n->send_connect_ack = 0;
2261 send_session_connect_ack_message (n->primary_address.address,
2262 n->primary_address.session, ts);
2263 check_blacklist (peer, ts, address, session);
2265 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2266 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2267 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2268 /* It can never hurt to have an alternative address in the above cases,
2269 see if it is allowed */
2270 check_blacklist (peer, ts, address, session);
2272 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2273 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2274 /* we are already connected and can thus send the ACK immediately;
2275 still, it can never hurt to have an alternative address, so also
2276 tell ATS about it */
2277 GNUNET_assert (NULL != n->primary_address.address);
2278 GNUNET_assert (NULL != n->primary_address.session);
2279 n->send_connect_ack = 0;
2280 send_session_connect_ack_message (n->primary_address.address,
2281 n->primary_address.session, ts);
2282 check_blacklist (peer, ts, address, session);
2284 case GNUNET_TRANSPORT_PS_DISCONNECT:
2285 /* get rid of remains without terminating sessions, ready to re-try */
2286 free_neighbour (n, GNUNET_YES);
2287 n = setup_neighbour (peer);
2288 set_state (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS);
2289 GNUNET_ATS_reset_backoff (GST_ats, peer);
2291 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2292 /* should not be possible */
2296 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2297 "Unhandled state `%s'\n",
2298 GNUNET_TRANSPORT_ps2s (n->state));
2300 return GNUNET_SYSERR;
2306 switch_address_bl_check_cont (void *cls,
2307 const struct GNUNET_PeerIdentity *peer, int result)
2309 struct BlacklistCheckSwitchContext *blc_ctx = cls;
2310 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2311 struct NeighbourMapEntry *n;
2313 if ( (NULL == (n = lookup_neighbour (peer))) || (result == GNUNET_NO) ||
2314 (NULL == (papi = GST_plugins_find (blc_ctx->address->transport_name))) )
2318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2319 "Peer %s is unknown, suggestion ignored\n",
2322 if (result == GNUNET_NO)
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "Blacklist denied to switch to suggested address `%s' sesion %p for peer `%s'\n",
2326 GST_plugins_a2s (blc_ctx->address),
2328 GNUNET_i2s (&blc_ctx->address->peer));
2330 /* Delete address (or session if existing) in ATS */
2331 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, blc_ctx->session);
2333 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2334 GNUNET_HELLO_address_free(blc_ctx->address);
2335 GNUNET_free_non_null (blc_ctx->ats);
2336 GNUNET_free (blc_ctx);
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 "Blacklist accepted to switch to suggested address `%s' for peer `%s'\n",
2342 GST_plugins_a2s (blc_ctx->address),
2344 GNUNET_i2s (&blc_ctx->address->peer));
2346 if (NULL == blc_ctx->session)
2348 blc_ctx->session = papi->get_session (papi->cls, blc_ctx->address);
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "Obtained new session for peer `%s' and address '%s': %p\n",
2351 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address), blc_ctx->session);
2353 if (NULL == blc_ctx->session)
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 "Failed to obtain new session for peer `%s' and address '%s'\n",
2357 GNUNET_i2s (&blc_ctx->address->peer), GST_plugins_a2s (blc_ctx->address));
2358 GNUNET_ATS_address_destroyed (GST_ats, blc_ctx->address, NULL);
2363 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2365 free_neighbour (n, GNUNET_NO);
2367 case GNUNET_TRANSPORT_PS_INIT_ATS:
2368 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2369 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2370 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_BLACKLIST,
2371 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2372 check_blacklist (&n->id, n->connect_ack_timestamp,
2373 blc_ctx->address, blc_ctx->session);
2375 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2376 /* ATS suggests a different address, switch again */
2377 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2378 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2379 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2380 check_blacklist (&n->id, n->connect_ack_timestamp,
2381 blc_ctx->address, blc_ctx->session);
2383 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2384 /* ATS suggests a different address, switch again */
2385 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2386 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2387 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_BLACKLIST,
2388 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2389 check_blacklist (&n->id, n->connect_ack_timestamp,
2390 blc_ctx->address, blc_ctx->session);
2392 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2393 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2394 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2395 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2396 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2397 check_blacklist (&n->id, n->connect_ack_timestamp,
2398 blc_ctx->address, blc_ctx->session);
2400 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2401 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2402 check_blacklist (&n->id, n->connect_ack_timestamp,
2403 blc_ctx->address, blc_ctx->session);
2405 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2406 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2407 /* ATS asks us to switch while we were trying to connect; switch to new
2408 address and check blacklist again */
2409 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2410 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2411 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST,
2412 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2413 check_blacklist (&n->id, n->connect_ack_timestamp,
2414 blc_ctx->address, blc_ctx->session);
2416 case GNUNET_TRANSPORT_PS_CONNECTED:
2417 GNUNET_assert (NULL != n->primary_address.address);
2418 GNUNET_assert (NULL != n->primary_address.session);
2419 if (n->primary_address.session == blc_ctx->session)
2421 /* not an address change, just a quota change */
2422 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2423 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_YES);
2426 /* ATS asks us to switch a life connection; see if we can get
2427 a CONNECT_ACK on it before we actually do this! */
2428 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2429 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2430 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2431 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2432 blc_ctx->address, blc_ctx->session);
2434 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2435 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2436 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2437 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2438 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2439 check_blacklist (&n->id, n->connect_ack_timestamp,
2440 blc_ctx->address, blc_ctx->session);
2442 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2443 /* ATS asks us to switch while we were trying to reconnect; switch to new
2444 address and check blacklist again */
2445 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2446 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2447 set_timeout (n, GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2448 check_blacklist (&n->id, n->connect_ack_timestamp,
2449 blc_ctx->address, blc_ctx->session);
2451 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2452 /* ATS asks us to switch while we were trying to reconnect; switch to new
2453 address and check blacklist again */
2454 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2455 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2456 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST,
2457 GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT));
2458 check_blacklist (&n->id, n->connect_ack_timestamp,
2459 blc_ctx->address, blc_ctx->session);
2461 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2462 if (n->primary_address.session == blc_ctx->session)
2464 /* ATS switches back to still-active session */
2465 set_state(n, GNUNET_TRANSPORT_PS_CONNECTED);
2466 free_address (&n->alternative_address);
2469 /* ATS asks us to switch a life connection, update blacklist check */
2470 set_primary_address (n, blc_ctx->address, blc_ctx->session,
2471 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out, GNUNET_NO);
2472 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2473 blc_ctx->address, blc_ctx->session);
2475 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2476 if (n->primary_address.session == blc_ctx->session)
2478 /* ATS switches back to still-active session */
2479 free_address (&n->alternative_address);
2480 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
2483 /* ATS asks us to switch a life connection, update blacklist check */
2484 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST);
2485 set_alternative_address (n, blc_ctx->address, blc_ctx->session,
2486 blc_ctx->bandwidth_in, blc_ctx->bandwidth_out);
2487 check_blacklist (&n->id, GNUNET_TIME_absolute_get (),
2488 blc_ctx->address, blc_ctx->session);
2490 case GNUNET_TRANSPORT_PS_DISCONNECT:
2491 /* not going to switch addresses while disconnecting */
2493 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2497 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2498 "Unhandled state `%s'\n",
2499 GNUNET_TRANSPORT_ps2s (n->state));
2504 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, blc_ctx);
2505 GNUNET_HELLO_address_free(blc_ctx->address);
2506 GNUNET_free_non_null (blc_ctx->ats);
2507 GNUNET_free (blc_ctx);
2513 * For the given peer, switch to this address.
2515 * Before accepting this addresses and actively using it, a blacklist check
2516 * is performed. If this blacklist check fails the address will be destroyed.
2518 * @param peer identity of the peer to switch the address for
2519 * @param address address of the other peer,
2520 * @param session session to use or NULL if transport should initiate a session
2521 * @param ats performance data
2522 * @param ats_count number of entries in ats
2523 * @param bandwidth_in inbound quota to be used when connection is up,
2524 * 0 to disconnect from peer
2525 * @param bandwidth_out outbound quota to be used when connection is up,
2526 * 0 to disconnect from peer
2529 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2530 const struct GNUNET_HELLO_Address *address,
2531 struct Session *session,
2532 const struct GNUNET_ATS_Information *ats,
2534 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2535 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2537 struct NeighbourMapEntry *n;
2538 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2539 struct BlacklistCheckSwitchContext *blc_ctx;
2542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2543 "ATS has decided on an address for peer %s\n",
2545 GNUNET_assert (NULL != address->transport_name);
2546 if (NULL == (n = lookup_neighbour (peer)))
2548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2549 "Peer %s is unknown, suggestion ignored\n",
2554 /* Obtain an session for this address from plugin */
2555 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2557 /* we don't have the plugin for this address */
2558 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2559 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2560 address->transport_name,
2562 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2565 if ((NULL == session) &&
2566 (GNUNET_HELLO_address_check_option (address, GNUNET_HELLO_ADDRESS_INFO_INBOUND)))
2568 /* This is a inbound address and we do not have a session to use! */
2569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2570 "Inbound address without session `%s'! Destroying address...\n",
2571 GST_plugins_a2s (address));
2572 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2576 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2577 "ATS tells us to switch to %s address '%s' session %p for "
2578 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2579 GNUNET_HELLO_address_check_option (address,
2580 GNUNET_HELLO_ADDRESS_INFO_INBOUND) ? "inbound" : "",
2581 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
2582 GNUNET_TRANSPORT_ps2s (n->state), n->send_connect_ack,
2583 ntohl (bandwidth_in.value__), ntohl (bandwidth_out.value__));
2585 /* Perform blacklist check */
2586 blc_ctx = GNUNET_new (struct BlacklistCheckSwitchContext);
2587 blc_ctx->address = GNUNET_HELLO_address_copy (address);
2588 blc_ctx->session = session;
2589 blc_ctx->bandwidth_in = bandwidth_in;
2590 blc_ctx->bandwidth_out = bandwidth_out;
2591 blc_ctx->ats_count = ats_count;
2592 blc_ctx->ats = NULL;
2595 blc_ctx->ats = GNUNET_malloc (ats_count * sizeof (struct GNUNET_ATS_Information));
2596 for (c = 0; c < ats_count; c++)
2598 blc_ctx->ats[c].type = ats[c].type;
2599 blc_ctx->ats[c].value = ats[c].value;
2603 GNUNET_CONTAINER_DLL_insert (pending_bc_head, pending_bc_tail, blc_ctx);
2604 blc_ctx->blc = GST_blacklist_test_allowed (peer, address->transport_name,
2605 &switch_address_bl_check_cont, blc_ctx);
2610 send_utilization_data (void *cls,
2611 const struct GNUNET_PeerIdentity *key,
2614 struct NeighbourMapEntry *n = value;
2615 struct GNUNET_ATS_Information atsi[4];
2617 uint32_t bps_pl_out;
2620 struct GNUNET_TIME_Relative delta;
2622 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2623 GNUNET_TIME_absolute_get ());
2627 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2628 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2630 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2631 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2633 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2638 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2639 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2641 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2642 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2645 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2646 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2650 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2651 atsi[0].value = htonl (bps_out);
2652 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2653 atsi[1].value = htonl (bps_in);
2655 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2656 atsi[2].value = htonl (bps_pl_out);
2657 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2658 atsi[3].value = htonl (bps_pl_in);
2660 GST_ats_update_metrics (key, n->primary_address.address,
2661 n->primary_address.session, atsi, 4);
2662 n->util_payload_bytes_recv = 0;
2663 n->util_payload_bytes_sent = 0;
2664 n->util_total_bytes_recv = 0;
2665 n->util_total_bytes_sent = 0;
2666 n->last_util_transmission = GNUNET_TIME_absolute_get();
2672 * Task transmitting utilization in a regular interval
2674 * @param cls the 'struct NeighbourMapEntry' for which we are running
2675 * @param tc scheduler context (unused)
2678 utilization_transmission (void *cls,
2679 const struct GNUNET_SCHEDULER_TaskContext *tc)
2681 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2683 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2684 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2686 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2687 utilization_transmission, NULL);
2693 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2694 const struct GNUNET_HELLO_Address *address,
2695 struct Session *session,
2696 const struct GNUNET_MessageHeader *message)
2698 struct NeighbourMapEntry *n;
2700 n = lookup_neighbour (peer);
2703 n->util_total_bytes_recv += ntohs(message->size);
2708 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2709 const struct GNUNET_HELLO_Address *address,
2710 struct Session *session,
2711 const struct GNUNET_MessageHeader *message)
2713 struct NeighbourMapEntry *n;
2714 n = lookup_neighbour (peer);
2717 n->util_payload_bytes_recv += ntohs(message->size);
2722 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2723 const struct GNUNET_HELLO_Address *address,
2724 struct Session *session,
2727 struct NeighbourMapEntry *n;
2728 n = lookup_neighbour (peer);
2731 if (n->primary_address.session != session)
2733 n->util_total_bytes_sent += size;
2738 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2741 struct NeighbourMapEntry *n;
2742 n = lookup_neighbour (peer);
2745 n->util_payload_bytes_sent += size;
2750 * Master task run for every neighbour. Performs all of the time-related
2751 * activities (keep alive, send next message, disconnect if idle, finish
2752 * clean up after disconnect).
2754 * @param cls the 'struct NeighbourMapEntry' for which we are running
2755 * @param tc scheduler context (unused)
2758 master_task (void *cls,
2759 const struct GNUNET_SCHEDULER_TaskContext *tc)
2761 struct NeighbourMapEntry *n = cls;
2762 struct GNUNET_TIME_Relative delay;
2764 n->task = GNUNET_SCHEDULER_NO_TASK;
2765 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2767 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2768 GNUNET_i2s (&n->id),
2769 GNUNET_TRANSPORT_ps2s(n->state),
2770 GNUNET_STRINGS_relative_time_to_string (delay,
2774 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
2775 /* invalid state for master task, clean up */
2777 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2778 free_neighbour (n, GNUNET_NO);
2780 case GNUNET_TRANSPORT_PS_INIT_ATS:
2781 if (0 == delay.rel_value_us)
2783 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2784 "Connection to `%s' timed out waiting for ATS to provide address\n",
2785 GNUNET_i2s (&n->id));
2786 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2787 free_neighbour (n, GNUNET_NO);
2791 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
2792 if (0 == delay.rel_value_us)
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2795 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2796 GNUNET_i2s (&n->id));
2797 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2798 free_neighbour (n, GNUNET_NO);
2802 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
2803 if (0 == delay.rel_value_us)
2805 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2806 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2807 GNUNET_i2s (&n->id));
2808 /* We could not send to this address, delete address and session */
2809 if (NULL != n->primary_address.session)
2810 GNUNET_ATS_address_destroyed (GST_ats,
2811 n->primary_address.address, n->primary_address.session);
2812 GNUNET_ATS_address_destroyed (GST_ats,
2813 n->primary_address.address, NULL);
2814 disconnect_neighbour (n);
2818 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
2819 if (0 == delay.rel_value_us)
2821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2822 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2823 GNUNET_i2s (&n->id));
2824 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2825 free_neighbour (n, GNUNET_NO);
2829 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
2830 if (0 == delay.rel_value_us)
2832 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2833 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2834 GNUNET_i2s (&n->id));
2835 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2836 free_neighbour (n, GNUNET_NO);
2840 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
2841 if (0 == delay.rel_value_us)
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2844 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2845 GNUNET_i2s (&n->id));
2846 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
2847 free_neighbour (n, GNUNET_NO);
2851 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
2852 if (0 == delay.rel_value_us)
2854 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2855 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2856 GNUNET_i2s (&n->id));
2857 disconnect_neighbour (n);
2861 case GNUNET_TRANSPORT_PS_CONNECTED:
2862 if (0 == delay.rel_value_us)
2864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2865 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2866 GNUNET_i2s (&n->id));
2867 disconnect_neighbour (n);
2870 try_transmission_to_peer (n);
2873 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
2874 if (0 == delay.rel_value_us)
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2878 GNUNET_i2s (&n->id));
2879 disconnect_neighbour (n);
2883 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
2884 if (0 == delay.rel_value_us)
2886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2887 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2888 GNUNET_i2s (&n->id));
2889 disconnect_neighbour (n);
2893 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
2894 if (0 == delay.rel_value_us)
2896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2897 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2898 GNUNET_i2s (&n->id));
2899 disconnect_neighbour (n);
2903 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
2904 if (0 == delay.rel_value_us)
2906 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2907 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2908 GNUNET_i2s (&n->id));
2909 disconnect_neighbour (n);
2912 try_transmission_to_peer (n);
2915 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
2916 if (0 == delay.rel_value_us)
2918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2919 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2920 GNUNET_i2s (&n->id));
2921 disconnect_neighbour (n);
2924 try_transmission_to_peer (n);
2927 case GNUNET_TRANSPORT_PS_DISCONNECT:
2928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2929 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2930 GNUNET_i2s (&n->id));
2931 free_neighbour (n, GNUNET_NO);
2933 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
2934 /* how did we get here!? */
2938 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2939 "Unhandled state `%s'\n",
2940 GNUNET_TRANSPORT_ps2s (n->state));
2944 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2945 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2946 (GNUNET_TRANSPORT_PS_CONNECTED == n->state) )
2948 /* if we are *now* in one of these three states, we're sending
2949 keep alive messages, so we need to consider the keepalive
2950 delay, not just the connection timeout */
2951 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2954 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2955 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2962 * Send a SESSION_ACK message to the neighbour to confirm that we
2963 * got his CONNECT_ACK.
2965 * @param n neighbour to send the SESSION_ACK to
2968 send_session_ack_message (struct NeighbourMapEntry *n)
2970 struct GNUNET_MessageHeader msg;
2972 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2973 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2974 (void) send_with_session(n,
2975 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2976 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
2982 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2983 * Consider switching to it.
2985 * @param message possibly a 'struct SessionConnectMessage' (check format)
2986 * @param peer identity of the peer to switch the address for
2987 * @param address address of the other peer, NULL if other peer
2989 * @param session session to use (or NULL)
2990 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2993 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2994 const struct GNUNET_PeerIdentity *peer,
2995 const struct GNUNET_HELLO_Address *address,
2996 struct Session *session)
2998 const struct SessionConnectMessage *scm;
2999 struct GNUNET_TIME_Absolute ts;
3000 struct NeighbourMapEntry *n;
3002 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3003 "Received CONNECT_ACK message from peer `%s'\n",
3006 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
3008 GNUNET_break_op (0);
3009 return GNUNET_SYSERR;
3011 GNUNET_STATISTICS_update (GST_stats,
3013 ("# CONNECT_ACK messages received"),
3015 scm = (const struct SessionConnectMessage *) message;
3016 GNUNET_break_op (ntohl (scm->reserved) == 0);
3017 if (NULL == (n = lookup_neighbour (peer)))
3019 GNUNET_STATISTICS_update (GST_stats,
3021 ("# unexpected CONNECT_ACK messages (no peer)"),
3023 return GNUNET_SYSERR;
3025 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
3028 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3030 free_neighbour (n, GNUNET_NO);
3031 return GNUNET_SYSERR;
3032 case GNUNET_TRANSPORT_PS_INIT_ATS:
3033 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3034 GNUNET_STATISTICS_update (GST_stats,
3036 ("# unexpected CONNECT_ACK messages (not ready)"),
3039 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3040 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3042 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3043 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3046 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3047 GNUNET_STATISTICS_set (GST_stats,
3048 gettext_noop ("# peers connected"),
3049 ++neighbours_connected,
3051 connect_notify_cb (callback_cls, &n->id,
3052 n->primary_address.bandwidth_in,
3053 n->primary_address.bandwidth_out);
3054 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3055 GST_ats_add_address (n->primary_address.address,
3056 n->primary_address.session,
3058 set_primary_address (n,
3059 n->primary_address.address,
3060 n->primary_address.session,
3061 n->primary_address.bandwidth_in,
3062 n->primary_address.bandwidth_out,
3064 send_session_ack_message (n);
3066 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3067 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3068 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3069 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3070 GNUNET_STATISTICS_update (GST_stats,
3072 ("# unexpected CONNECT_ACK messages (not ready)"),
3075 case GNUNET_TRANSPORT_PS_CONNECTED:
3076 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3077 send_session_ack_message (n);
3079 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3080 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3081 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3082 to give us a new address... */
3083 GNUNET_STATISTICS_update (GST_stats,
3085 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3088 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3089 /* new address worked; go back to connected! */
3090 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3091 send_session_ack_message (n);
3093 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3094 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3095 send_session_ack_message (n);
3097 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3098 /* new address worked; adopt it and go back to connected! */
3099 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3100 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3102 GST_ats_add_address (n->alternative_address.address,
3103 n->alternative_address.session,
3105 set_primary_address (n, n->alternative_address.address,
3106 n->alternative_address.session, n->alternative_address.bandwidth_in,
3107 n->alternative_address.bandwidth_out, GNUNET_YES);
3109 free_address (&n->alternative_address);
3110 send_session_ack_message (n);
3112 case GNUNET_TRANSPORT_PS_DISCONNECT:
3113 GNUNET_STATISTICS_update (GST_stats,
3115 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3117 return GNUNET_SYSERR;
3118 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3123 "Unhandled state `%s'\n",
3124 GNUNET_TRANSPORT_ps2s (n->state));
3126 return GNUNET_SYSERR;
3133 * A session was terminated. Take note; if needed, try to get
3134 * an alternative address from ATS.
3136 * @param peer identity of the peer where the session died
3137 * @param session session that is gone
3138 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3139 * this session was not in use
3142 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3143 struct Session *session)
3145 struct NeighbourMapEntry *n;
3146 struct BlackListCheckContext *bcc;
3147 struct BlackListCheckContext *bcc_next;
3149 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3151 while (NULL != (bcc = bcc_next))
3153 bcc_next = bcc->next;
3154 if (bcc->na.session == session)
3156 if (NULL != bcc->bc)
3157 GST_blacklist_test_cancel (bcc->bc);
3158 GNUNET_HELLO_address_free (bcc->na.address);
3159 GNUNET_CONTAINER_DLL_remove (bc_head,
3165 if (NULL == (n = lookup_neighbour (peer)))
3166 return GNUNET_NO; /* can't affect us */
3167 if (session != n->primary_address.session)
3169 if (session == n->alternative_address.session)
3171 if ( (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3172 (GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3173 set_state (n, GNUNET_TRANSPORT_PS_CONNECTED);
3176 free_address (&n->alternative_address);
3178 return GNUNET_NO; /* doesn't affect us further */
3181 n->expect_latency_response = GNUNET_NO;
3184 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3186 free_neighbour (n, GNUNET_NO);
3188 case GNUNET_TRANSPORT_PS_INIT_ATS:
3190 free_neighbour (n, GNUNET_NO);
3192 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3193 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3194 free_address (&n->primary_address);
3195 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3197 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3198 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3199 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3200 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3201 /* error on inbound session; free neighbour entirely */
3202 free_address (&n->primary_address);
3203 free_neighbour (n, GNUNET_NO);
3205 case GNUNET_TRANSPORT_PS_CONNECTED:
3206 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_INIT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3207 free_address (&n->primary_address);
3209 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3210 /* we don't have an address, how can it go down? */
3213 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3214 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3215 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_ATS, GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT));
3217 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3218 /* primary went down while we were checking secondary against
3219 blacklist, adopt secondary as primary */
3220 free_address (&n->primary_address);
3221 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3222 n->primary_address = n->alternative_address;
3223 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3225 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3226 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3227 secondary as primary */
3228 free_address (&n->primary_address);
3229 n->primary_address = n->alternative_address;
3230 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3231 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_RECONNECT_SENT, GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT));
3233 case GNUNET_TRANSPORT_PS_DISCONNECT:
3234 free_address (&n->primary_address);
3236 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3237 /* neighbour was freed and plugins told to terminate session */
3241 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3242 "Unhandled state `%s'\n",
3243 GNUNET_TRANSPORT_ps2s (n->state));
3247 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3248 GNUNET_SCHEDULER_cancel (n->task);
3249 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3255 * We received a 'SESSION_ACK' message from the other peer.
3256 * If we sent a 'CONNECT_ACK' last, this means we are now
3257 * connected. Otherwise, do nothing.
3259 * @param message possibly a 'struct SessionConnectMessage' (check format)
3260 * @param peer identity of the peer to switch the address for
3261 * @param address address of the other peer, NULL if other peer
3263 * @param session session to use (or NULL)
3264 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3267 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3268 const struct GNUNET_PeerIdentity *peer,
3269 const struct GNUNET_HELLO_Address *address,
3270 struct Session *session)
3272 struct NeighbourMapEntry *n;
3274 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3275 "Received SESSION_ACK message from peer `%s'\n",
3277 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3279 GNUNET_break_op (0);
3280 return GNUNET_SYSERR;
3282 GNUNET_STATISTICS_update (GST_stats,
3284 ("# SESSION_ACK messages received"),
3286 if (NULL == (n = lookup_neighbour (peer)))
3288 GNUNET_break_op (0);
3289 return GNUNET_SYSERR;
3291 /* check if we are in a plausible state for having sent
3292 a CONNECT_ACK. If not, return, otherwise break */
3293 if ( ( (GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK != n->state) &&
3294 (GNUNET_TRANSPORT_PS_CONNECT_SENT != n->state) ) ||
3295 (2 != n->send_connect_ack) )
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3298 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3300 GNUNET_TRANSPORT_ps2s (n->state),
3301 n->send_connect_ack);
3302 GNUNET_STATISTICS_update (GST_stats,
3303 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3307 set_state_and_timeout (n, GNUNET_TRANSPORT_PS_CONNECTED, GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT));
3308 GNUNET_STATISTICS_set (GST_stats,
3309 gettext_noop ("# peers connected"),
3310 ++neighbours_connected,
3312 connect_notify_cb (callback_cls, &n->id,
3313 n->primary_address.bandwidth_in,
3314 n->primary_address.bandwidth_out);
3316 GST_ats_add_address (n->primary_address.address,
3317 n->primary_address.session,
3319 set_primary_address (n,
3320 n->primary_address.address,
3321 n->primary_address.session,
3322 n->primary_address.bandwidth_in,
3323 n->primary_address.bandwidth_out,
3330 * Test if we're connected to the given peer.
3332 * @param target peer to test
3333 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3336 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3338 return test_connected (lookup_neighbour (target));
3343 * Change the incoming quota for the given peer.
3345 * @param neighbour identity of peer to change qutoa for
3346 * @param quota new quota
3349 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3350 struct GNUNET_BANDWIDTH_Value32NBO quota)
3352 struct NeighbourMapEntry *n;
3354 if (NULL == (n = lookup_neighbour (neighbour)))
3356 GNUNET_STATISTICS_update (GST_stats,
3358 ("# SET QUOTA messages ignored (no such peer)"),
3362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3363 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3364 ntohl (quota.value__), GNUNET_i2s (&n->id));
3365 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3366 if (0 != ntohl (quota.value__))
3368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3369 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3370 GNUNET_i2s (&n->id));
3371 if (GNUNET_YES == test_connected (n))
3372 GNUNET_STATISTICS_update (GST_stats,
3373 gettext_noop ("# disconnects due to quota of 0"),
3375 disconnect_neighbour (n);
3380 * We received a disconnect message from the given peer,
3381 * validate and process.
3383 * @param peer sender of the message
3384 * @param msg the disconnect message
3387 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3388 const struct GNUNET_MessageHeader *msg)
3390 struct NeighbourMapEntry *n;
3391 const struct SessionDisconnectMessage *sdm;
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394 "Received DISCONNECT message from peer `%s'\n",
3396 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3398 // GNUNET_break_op (0);
3399 GNUNET_STATISTICS_update (GST_stats,
3401 ("# disconnect messages ignored (old format)"), 1,
3405 GNUNET_STATISTICS_update (GST_stats,
3407 ("# DISCONNECT messages received"),
3409 sdm = (const struct SessionDisconnectMessage *) msg;
3410 if (NULL == (n = lookup_neighbour (peer)))
3411 return; /* gone already */
3412 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3414 GNUNET_STATISTICS_update (GST_stats,
3416 ("# disconnect messages ignored (timestamp)"), 1,
3420 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3422 GNUNET_break_op (0);
3425 if (ntohl (sdm->purpose.size) !=
3426 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3427 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3428 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3430 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3431 "%s message from peer `%s' has invalid size \n",
3434 GNUNET_break_op (0);
3438 GNUNET_CRYPTO_eddsa_verify
3439 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3440 &sdm->signature, &sdm->public_key))
3442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3443 "%s message from peer `%s' cannot be verified \n",
3446 GNUNET_break_op (0);
3449 if (GNUNET_YES == test_connected (n))
3450 GNUNET_STATISTICS_update (GST_stats,
3452 ("# other peer asked to disconnect from us"), 1,
3454 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3455 "Disconnecting by request from peer %s\n",
3457 disconnect_neighbour (n);
3462 * Closure for the neighbours_iterate function.
3464 struct IteratorContext
3467 * Function to call on each connected neighbour.
3469 GST_NeighbourIterator cb;
3479 * Call the callback from the closure for each neighbour.
3481 * @param cls the `struct IteratorContext`
3482 * @param key the hash of the public key of the neighbour
3483 * @param value the `struct NeighbourMapEntry`
3484 * @return #GNUNET_OK (continue to iterate)
3487 neighbours_iterate (void *cls,
3488 const struct GNUNET_PeerIdentity *key,
3491 struct IteratorContext *ic = cls;
3492 struct NeighbourMapEntry *n = value;
3493 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3494 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3497 if (NULL != n->primary_address.address)
3499 bandwidth_in = n->primary_address.bandwidth_in;
3500 bandwidth_out = n->primary_address.bandwidth_out;
3504 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3505 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3509 n->primary_address.address,
3512 bandwidth_in, bandwidth_out);
3518 * Iterate over all connected neighbours.
3520 * @param cb function to call
3521 * @param cb_cls closure for cb
3524 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3526 struct IteratorContext ic;
3528 if (NULL == neighbours)
3529 return; /* can happen during shutdown */
3532 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3537 * If we have an active connection to the given target, it must be shutdown.
3539 * @param target peer to disconnect from
3542 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3544 struct NeighbourMapEntry *n;
3546 if (NULL == (n = lookup_neighbour (target)))
3547 return; /* not active */
3548 if (GNUNET_YES == test_connected (n))
3549 GNUNET_STATISTICS_update (GST_stats,
3551 ("# disconnected from peer upon explicit request"), 1,
3553 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3554 "Forced disconnect from peer %s\n",
3555 GNUNET_i2s (target));
3556 disconnect_neighbour (n);
3561 * Obtain current latency information for the given neighbour.
3563 * @param peer to get the latency for
3564 * @return observed latency of the address, FOREVER if the
3565 * the connection is not up
3567 struct GNUNET_TIME_Relative
3568 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3570 struct NeighbourMapEntry *n;
3572 n = lookup_neighbour (peer);
3574 return GNUNET_TIME_UNIT_FOREVER_REL;
3577 case GNUNET_TRANSPORT_PS_CONNECTED:
3578 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_CONNECT_SENT:
3579 case GNUNET_TRANSPORT_PS_CONNECTED_SWITCHING_BLACKLIST:
3580 case GNUNET_TRANSPORT_PS_RECONNECT_SENT:
3581 case GNUNET_TRANSPORT_PS_RECONNECT_ATS:
3582 case GNUNET_TRANSPORT_PS_RECONNECT_BLACKLIST:
3584 case GNUNET_TRANSPORT_PS_NOT_CONNECTED:
3585 case GNUNET_TRANSPORT_PS_INIT_BLACKLIST:
3586 case GNUNET_TRANSPORT_PS_INIT_ATS:
3587 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST_INBOUND:
3588 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ATS:
3589 case GNUNET_TRANSPORT_PS_CONNECT_RECV_BLACKLIST:
3590 case GNUNET_TRANSPORT_PS_CONNECT_RECV_ACK:
3591 case GNUNET_TRANSPORT_PS_CONNECT_SENT:
3592 case GNUNET_TRANSPORT_PS_DISCONNECT:
3593 case GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED:
3594 return GNUNET_TIME_UNIT_FOREVER_REL;
3596 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3597 "Unhandled state `%s'\n",
3598 GNUNET_TRANSPORT_ps2s (n->state));
3602 return GNUNET_TIME_UNIT_FOREVER_REL;
3607 * Obtain current address information for the given neighbour.
3610 * @return address currently used
3612 struct GNUNET_HELLO_Address *
3613 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3615 struct NeighbourMapEntry *n;
3617 n = lookup_neighbour (peer);
3620 return n->primary_address.address;
3625 * Initialize the neighbours subsystem.
3627 * @param cls closure for callbacks
3628 * @param connect_cb function to call if we connect to a peer
3629 * @param disconnect_cb function to call if we disconnect from a peer
3630 * @param peer_address_cb function to call if we change an active address
3632 * @param max_fds maximum number of fds to use
3635 GST_neighbours_start (void *cls,
3636 NotifyConnect connect_cb,
3637 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3638 GNUNET_TRANSPORT_NeighbourChangeCallback peer_address_cb,
3639 unsigned int max_fds)
3642 connect_notify_cb = connect_cb;
3643 disconnect_notify_cb = disconnect_cb;
3644 neighbour_change_cb = peer_address_cb;
3645 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3646 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3647 utilization_transmission, NULL);
3652 * Disconnect from the given neighbour.
3655 * @param key hash of neighbour's public key (not used)
3656 * @param value the 'struct NeighbourMapEntry' of the neighbour
3657 * @return #GNUNET_OK (continue to iterate)
3660 disconnect_all_neighbours (void *cls,
3661 const struct GNUNET_PeerIdentity *key,
3664 struct NeighbourMapEntry *n = value;
3666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3667 "Disconnecting peer `%4s', %s\n",
3668 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3669 set_state (n, GNUNET_TRANSPORT_PS_DISCONNECT_FINISHED);
3670 free_neighbour (n, GNUNET_NO);
3676 * Cleanup the neighbours subsystem.
3679 GST_neighbours_stop ()
3681 struct BlacklistCheckSwitchContext *cur;
3682 struct BlacklistCheckSwitchContext *next;
3684 if (NULL == neighbours)
3686 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3688 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3689 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3692 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3693 &disconnect_all_neighbours,
3695 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3697 next = pending_bc_head;
3698 for (cur = next; NULL != cur; cur = next )
3701 GNUNET_CONTAINER_DLL_remove (pending_bc_head, pending_bc_tail, cur);
3703 if (NULL != cur->blc)
3705 GST_blacklist_test_cancel (cur->blc);
3708 if (NULL != cur->address)
3709 GNUNET_HELLO_address_free (cur->address);
3710 GNUNET_free_non_null (cur->ats);
3715 callback_cls = NULL;
3716 connect_notify_cb = NULL;
3717 disconnect_notify_cb = NULL;
3718 neighbour_change_cb = NULL;
3722 /* end of file gnunet-service-transport_neighbours.c */