2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport_clients.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
41 * Size of the neighbour hash map.
43 #define NEIGHBOUR_TABLE_SIZE 256
46 * Time we give plugin to transmit DISCONNECT message before the
47 * neighbour entry self-destructs.
49 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
52 * How often must a peer violate bandwidth quotas before we start
53 * to simply drop its messages?
55 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
58 * How long are we willing to wait for a response from ATS before timing out?
60 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
63 * How long are we willing to wait for an ACK from the other peer before
64 * giving up on our connect operation?
66 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
69 * How long are we willing to wait for a successful reconnect if
70 * an existing connection went down? Much shorter than the
71 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
72 * higher layers about the disconnect during this period.
74 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
77 * How long are we willing to wait for a response from the blacklist
78 * subsystem before timing out?
80 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
83 * Interval to send utilization data
85 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
87 GNUNET_NETWORK_STRUCT_BEGIN
90 * Message a peer sends to another to indicate that it intends to
91 * setup a connection/session for data exchange. A 'SESSION_CONNECT'
92 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
93 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
94 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
95 * should be connected.
97 struct SessionConnectMessage
100 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT
101 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK
103 struct GNUNET_MessageHeader header;
108 uint32_t reserved GNUNET_PACKED;
111 * Absolute time at the sender. Only the most recent connect
112 * message implies which session is preferred by the sender.
114 struct GNUNET_TIME_AbsoluteNBO timestamp;
120 * Message a peer sends to another when connected to indicate that a
121 * session is in use and the peer is still alive or to respond to a keep alive.
122 * A peer sends a message with type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE
123 * to request a message with #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
124 * When the keep alive response with type is received, transport service
125 * will call the respective plugin to update the session timeout
127 struct SessionKeepAliveMessage
130 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE or
131 * #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE.
133 struct GNUNET_MessageHeader header;
136 * A nonce to identify the session the keep alive is used for
138 uint32_t nonce GNUNET_PACKED;
142 * Message we send to the other peer to notify him that we intentionally
143 * are disconnecting (to reduce timeouts). This is just a friendly
144 * notification, peers must not rely on always receiving disconnect
147 struct SessionDisconnectMessage
150 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
152 struct GNUNET_MessageHeader header;
157 uint32_t reserved GNUNET_PACKED;
160 * Purpose of the signature. Extends over the timestamp.
161 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
163 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
166 * Absolute time at the sender. Only the most recent connect
167 * message implies which session is preferred by the sender.
169 struct GNUNET_TIME_AbsoluteNBO timestamp;
172 * Public key of the sender.
174 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
177 * Signature of the peer that sends us the disconnect. Only
178 * valid if the timestamp is AFTER the timestamp from the
179 * corresponding 'CONNECT' message.
181 struct GNUNET_CRYPTO_EddsaSignature signature;
185 GNUNET_NETWORK_STRUCT_END
189 * For each neighbour we keep a list of messages
190 * that we still want to transmit to the neighbour.
196 * This is a doubly linked list.
198 struct MessageQueue *next;
201 * This is a doubly linked list.
203 struct MessageQueue *prev;
206 * Function to call once we're done.
208 GST_NeighbourSendContinuation cont;
211 * Closure for @e cont
216 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
217 * stuck together in memory. Allocated at the end of this struct.
219 const char *message_buf;
222 * Size of the message buf
224 size_t message_buf_size;
227 * At what time should we fail?
229 struct GNUNET_TIME_Absolute timeout;
237 * A possible address we could use to communicate with a neighbour.
239 struct NeighbourAddress
243 * Active session for this address.
245 struct Session *session;
248 * Network-level address information.
250 struct GNUNET_HELLO_Address *address;
253 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
254 * peer for this address. Use to check that the ACK is in response
255 * to our most recent 'CONNECT'.
257 struct GNUNET_TIME_Absolute connect_timestamp;
260 * Inbound bandwidth from ATS for this address.
262 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
265 * Outbound bandwidth from ATS for this address.
267 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
270 * Did we tell ATS that this is our 'active' address?
275 * The current nonce sent in the last keep alive messages
277 uint32_t keep_alive_nonce;
282 * Entry in neighbours.
284 struct NeighbourMapEntry
288 * Head of list of messages we would like to send to this peer;
289 * must contain at most one message per client.
291 struct MessageQueue *messages_head;
294 * Tail of list of messages we would like to send to this peer; must
295 * contain at most one message per client.
297 struct MessageQueue *messages_tail;
300 * Are we currently trying to send a message? If so, which one?
302 struct MessageQueue *is_active;
305 * Primary address we currently use to communicate with the neighbour.
307 struct NeighbourAddress primary_address;
310 * Alternative address currently under consideration for communicating
311 * with the neighbour.
313 struct NeighbourAddress alternative_address;
316 * Identity of this neighbour.
318 struct GNUNET_PeerIdentity id;
321 * Main task that drives this peer (timeouts, keepalives, etc.).
322 * Always runs the 'master_task'.
324 GNUNET_SCHEDULER_TaskIdentifier task;
327 * At what time should we sent the next keep-alive message?
329 struct GNUNET_TIME_Absolute keep_alive_time;
332 * At what time did we sent the last keep-alive message? Used
333 * to calculate round-trip time ("latency").
335 struct GNUNET_TIME_Absolute last_keep_alive_time;
338 * Timestamp we should include in our next CONNECT_ACK message.
339 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
340 * our CONNECT_ACK message.
342 struct GNUNET_TIME_Absolute connect_ack_timestamp;
345 * ATS address suggest handle
347 struct GNUNET_ATS_SuggestHandle *suggest_handle;
350 * Time where we should cut the connection (timeout) if we don't
351 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
352 * if we are in S_CONNECTED).
354 struct GNUNET_TIME_Absolute timeout;
357 * Latest calculated latency value
359 struct GNUNET_TIME_Relative latency;
362 * Tracker for inbound bandwidth.
364 struct GNUNET_BANDWIDTH_Tracker in_tracker;
367 * How often has the other peer (recently) violated the inbound
368 * traffic limit? Incremented by 10 per violation, decremented by 1
369 * per non-violation (for each time interval).
371 unsigned int quota_violation_count;
374 * The current state of the peer.
376 enum GNUNET_TRANSPORT_PeerState state;
379 * Did we sent an KEEP_ALIVE message and are we expecting a response?
381 int expect_latency_response;
384 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
385 * (once we have an address to use and the peer has been allowed by our
386 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
387 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
388 * a 'SESSION_ACK' (regardless of what our own state machine might say).
390 int send_connect_ack;
393 * Tracking utilization of outbound bandwidth
395 uint32_t util_payload_bytes_sent;
398 * Tracking utilization of inbound bandwidth
400 uint32_t util_payload_bytes_recv;
403 * Tracking utilization of outbound bandwidth
405 uint32_t util_total_bytes_sent;
408 * Tracking utilization of inbound bandwidth
410 uint32_t util_total_bytes_recv;
413 * Date of last utilization transmission
415 struct GNUNET_TIME_Absolute last_util_transmission;
420 * Context for blacklist checks and the #handle_test_blacklist_cont()
421 * function. Stores information about ongoing blacklist checks.
423 struct BlackListCheckContext
427 * We keep blacklist checks in a DLL.
429 struct BlackListCheckContext *next;
432 * We keep blacklist checks in a DLL.
434 struct BlackListCheckContext *prev;
437 * Address that is being checked.
439 struct NeighbourAddress na;
442 * Handle to the ongoing blacklist check.
444 struct GST_BlacklistCheck *bc;
449 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
451 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
454 * We keep blacklist checks in a DLL so that we can find
455 * the 'sessions' in their 'struct NeighbourAddress' if
456 * a session goes down.
458 static struct BlackListCheckContext *bc_head;
461 * We keep blacklist checks in a DLL.
463 static struct BlackListCheckContext *bc_tail;
466 * Closure for #connect_notify_cb, #disconnect_notify_cb and #address_change_cb
468 static void *callback_cls;
471 * Function to call when we connected to a neighbour.
473 static NotifyConnect connect_notify_cb;
476 * Function to call when we disconnected from a neighbour.
478 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
481 * Function to call when we changed an active address of a neighbour.
483 static GNUNET_TRANSPORT_AddressChangeCallback address_change_cb;
486 * counter for connected neighbours
488 static unsigned int neighbours_connected;
491 * Number of bytes we have currently queued for transmission.
493 static unsigned long long bytes_in_send_queue;
496 * Task transmitting utilization data
498 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
502 * Lookup a neighbour entry in the neighbours hash map.
504 * @param pid identity of the peer to look up
505 * @return the entry, NULL if there is no existing record
507 static struct NeighbourMapEntry *
508 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
510 if (NULL == neighbours)
512 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
517 * Test if we're connected to the given peer.
519 * @param n neighbour entry of peer to test
520 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
523 test_connected (struct NeighbourMapEntry *n)
527 return GNUNET_TRANSPORT_is_connected (n->state);
531 * Send information about a new outbound quota to our clients.
533 * @param target affected peer
534 * @param quota new quota
537 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
538 struct GNUNET_BANDWIDTH_Value32NBO quota)
540 struct QuotaSetMessage q_msg;
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
544 ntohl (quota.value__), GNUNET_i2s (target));
545 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
546 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
548 q_msg.peer = (*target);
549 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
554 * We don't need a given neighbour address any more.
555 * Release its resources and give appropriate notifications
556 * to ATS and other subsystems.
558 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
561 free_address (struct NeighbourAddress *na)
563 if (GNUNET_YES == na->ats_active)
565 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
566 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
567 address_change_cb (callback_cls, &na->address->peer, NULL);
570 na->ats_active = GNUNET_NO;
571 na->keep_alive_nonce = 0;
572 if (NULL != na->address)
574 GNUNET_HELLO_address_free (na->address);
582 * Initialize the 'struct NeighbourAddress'.
584 * @param na neighbour address to initialize
585 * @param address address of the other peer, NULL if other peer
587 * @param session session to use (or NULL, in which case an
588 * address must be setup)
589 * @param bandwidth_in inbound quota to be used when connection is up
590 * @param bandwidth_out outbound quota to be used when connection is up
591 * @param is_active #GNUNET_YES to mark this as the active address with ATS
594 set_address (struct NeighbourAddress *na,
595 const struct GNUNET_HELLO_Address *address,
596 struct Session *session,
597 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
598 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
601 struct GNUNET_TRANSPORT_PluginFunctions *papi;
603 if (NULL == (papi = GST_plugins_find (address->transport_name)))
608 if (session == na->session)
610 na->bandwidth_in = bandwidth_in;
611 na->bandwidth_out = bandwidth_out;
612 if (is_active != na->ats_active)
614 na->ats_active = is_active;
615 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
616 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
618 address_change_cb (callback_cls, &address->peer, address);
620 if (GNUNET_YES == is_active)
622 /* FIXME: is this the right place to set quotas? */
623 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
624 send_outbound_quota (&address->peer, bandwidth_out);
630 session = papi->get_session (papi->cls, address);
633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
634 "Failed to obtain new session for peer `%s' and address '%s'\n",
635 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
636 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
639 na->address = GNUNET_HELLO_address_copy (address);
640 na->bandwidth_in = bandwidth_in;
641 na->bandwidth_out = bandwidth_out;
642 na->session = session;
643 na->ats_active = is_active;
644 na->keep_alive_nonce = 0;
645 if (GNUNET_YES == is_active)
647 /* Telling ATS about new session */
648 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
649 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
650 address_change_cb (callback_cls, &address->peer, address);
651 /* FIXME: is this the right place to set quotas? */
652 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
653 send_outbound_quota (&address->peer, bandwidth_out);
659 * Free a neighbour map entry.
661 * @param n entry to free
662 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
663 * #GNUNET_YES to keep all sessions
666 free_neighbour (struct NeighbourMapEntry *n,
669 struct MessageQueue *mq;
670 struct GNUNET_TRANSPORT_PluginFunctions *papi;
671 struct GNUNET_HELLO_Address *backup_primary;
673 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
674 "Freeing neighbur state of peer `%s'\n",
675 GNUNET_i2s (&n->id));
676 n->is_active = NULL; /* always free'd by its own continuation! */
678 /* fail messages currently in the queue */
679 while (NULL != (mq = n->messages_head))
681 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
682 if (NULL != mq->cont)
683 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
686 /* It is too late to send other peer disconnect notifications, but at
687 least internally we need to get clean... */
688 if (GNUNET_YES == test_connected (n))
690 GNUNET_STATISTICS_set (GST_stats,
691 gettext_noop ("# peers connected"),
692 --neighbours_connected,
694 disconnect_notify_cb (callback_cls, &n->id);
696 n->state = S_DISCONNECT_FINISHED;
698 if (NULL != n->primary_address.address)
700 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
703 backup_primary = NULL;
705 /* free addresses and mark as unused */
706 free_address (&n->primary_address);
707 free_address (&n->alternative_address);
709 /* FIXME: Note that if we are switching between two TCP sessions to
710 the same peer, we might want to selectively kill only one of
711 them! Killing all sessions like this seems to be very, very
714 /* cut transport-level connection */
715 if ((GNUNET_NO == keep_sessions) &&
716 (NULL != backup_primary) &&
717 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
718 papi->disconnect_peer (papi->cls, &n->id);
720 GNUNET_free_non_null (backup_primary);
722 GNUNET_assert (GNUNET_YES ==
723 GNUNET_CONTAINER_multipeermap_remove (neighbours,
726 // FIXME-ATS-API: we might want to be more specific about
727 // which states we do this from in the future (ATS should
728 // have given us a 'suggest_address' handle, and if we have
729 // such a handle, we should cancel the operation here!
730 if (NULL != n->suggest_handle)
732 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
733 n->suggest_handle = NULL;
736 if (GNUNET_SCHEDULER_NO_TASK != n->task)
738 GNUNET_SCHEDULER_cancel (n->task);
739 n->task = GNUNET_SCHEDULER_NO_TASK;
741 /* free rest of memory */
747 * Transmit a message using the current session of the given
750 * @param n entry for the recipient
751 * @param msgbuf buffer to transmit
752 * @param msgbuf_size number of bytes in @a msgbuf buffer
753 * @param priority transmission priority
754 * @param timeout transmission timeout
755 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
756 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
757 * @param cont continuation to call when finished (can be NULL)
758 * @param cont_cls closure for @a cont
759 * @return timeout (copy of @a timeout or a calculated one if
760 * @a use_keepalive_timeout is #GNUNET_YES.
762 static struct GNUNET_TIME_Relative
763 send_with_session (struct NeighbourMapEntry *n,
764 const char *msgbuf, size_t msgbuf_size,
766 struct GNUNET_TIME_Relative timeout,
767 unsigned int use_keepalive_timeout,
768 GNUNET_TRANSPORT_TransmitContinuation cont,
771 struct GNUNET_TRANSPORT_PluginFunctions *papi;
772 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
774 GNUNET_assert (n->primary_address.session != NULL);
775 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
776 (-1 == papi->send (papi->cls,
777 n->primary_address.session,
780 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
781 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
782 papi->query_keepalive_factor (papi->cls))),
783 cont, cont_cls)))) &&
785 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
786 GST_neighbours_notify_data_sent (&n->id,
787 n->primary_address.address, n->primary_address.session, msgbuf_size);
788 GNUNET_break (NULL != papi);
794 * Master task run for every neighbour. Performs all of the time-related
795 * activities (keep alive, send next message, disconnect if idle, finish
796 * clean up after disconnect).
798 * @param cls the `struct NeighbourMapEntry` for which we are running
799 * @param tc scheduler context (unused)
802 master_task (void *cls,
803 const struct GNUNET_SCHEDULER_TaskContext *tc);
807 * Function called when the 'DISCONNECT' message has been sent by the
808 * plugin. Frees the neighbour --- if the entry still exists.
811 * @param target identity of the neighbour that was disconnected
812 * @param result #GNUNET_OK if the disconnect got out successfully
813 * @param payload bytes payload
814 * @param physical bytes physical
817 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
818 int result, size_t payload, size_t physical)
820 struct NeighbourMapEntry *n;
822 n = lookup_neighbour (target);
824 return; /* already gone */
825 if (S_DISCONNECT != n->state)
826 return; /* have created a fresh entry since */
827 if (GNUNET_SCHEDULER_NO_TASK != n->task)
828 GNUNET_SCHEDULER_cancel (n->task);
829 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
834 * Transmit a DISCONNECT message to the other peer.
836 * @param n neighbour to send DISCONNECT message.
839 send_disconnect (struct NeighbourMapEntry *n)
841 struct SessionDisconnectMessage disconnect_msg;
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Sending DISCONNECT message to peer `%4s'\n",
845 GNUNET_i2s (&n->id));
846 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
847 disconnect_msg.header.type =
848 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
849 disconnect_msg.reserved = htonl (0);
850 disconnect_msg.purpose.size =
851 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
852 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
853 sizeof (struct GNUNET_TIME_AbsoluteNBO));
854 disconnect_msg.purpose.purpose =
855 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
856 disconnect_msg.timestamp =
857 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
858 disconnect_msg.public_key = GST_my_identity.public_key;
859 GNUNET_assert (GNUNET_OK ==
860 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
861 &disconnect_msg.purpose,
862 &disconnect_msg.signature));
864 (void) send_with_session (n,
865 (const char *) &disconnect_msg, sizeof (disconnect_msg),
866 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
867 GNUNET_NO, &send_disconnect_cont, NULL);
868 GNUNET_STATISTICS_update (GST_stats,
870 ("# DISCONNECT messages sent"), 1,
876 * Disconnect from the given neighbour, clean up the record.
878 * @param n neighbour to disconnect from
881 disconnect_neighbour (struct NeighbourMapEntry *n)
883 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
884 "Disconnecting from peer %s in state %s\n",
886 GNUNET_TRANSPORT_p2s (n->state));
887 /* depending on state, notify neighbour and/or upper layers of this peer
891 case S_NOT_CONNECTED:
893 case S_INIT_BLACKLIST:
894 /* other peer is completely unaware of us, no need to send DISCONNECT */
895 n->state = S_DISCONNECT_FINISHED;
896 free_neighbour (n, GNUNET_NO);
900 n->state = S_DISCONNECT;
902 case S_CONNECT_RECV_BLACKLIST_INBOUND:
903 case S_CONNECT_RECV_ATS:
904 case S_CONNECT_RECV_BLACKLIST:
905 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
906 n->state = S_DISCONNECT_FINISHED;
907 free_neighbour (n, GNUNET_NO);
909 case S_CONNECT_RECV_ACK:
910 /* we DID ACK the other peer's request, must send DISCONNECT */
912 n->state = S_DISCONNECT;
915 case S_RECONNECT_BLACKLIST:
916 case S_RECONNECT_SENT:
917 case S_CONNECTED_SWITCHING_BLACKLIST:
918 case S_CONNECTED_SWITCHING_CONNECT_SENT:
919 /* we are currently connected, need to send disconnect and do
920 internal notifications and update statistics */
922 GNUNET_STATISTICS_set (GST_stats,
923 gettext_noop ("# peers connected"),
924 --neighbours_connected,
926 disconnect_notify_cb (callback_cls, &n->id);
927 n->state = S_DISCONNECT;
929 case S_RECONNECT_ATS:
930 /* ATS address request timeout, disconnect without sending disconnect message */
931 GNUNET_STATISTICS_set (GST_stats,
932 gettext_noop ("# peers connected"),
933 --neighbours_connected,
935 disconnect_notify_cb (callback_cls, &n->id);
936 n->state = S_DISCONNECT;
939 /* already disconnected, ignore */
941 case S_DISCONNECT_FINISHED:
942 /* already cleaned up, how did we get here!? */
946 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
947 "Unhandled state `%s'\n",
948 GNUNET_TRANSPORT_p2s (n->state));
952 /* schedule timeout to clean up */
953 if (GNUNET_SCHEDULER_NO_TASK != n->task)
954 GNUNET_SCHEDULER_cancel (n->task);
955 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
961 * We're done with our transmission attempt, continue processing.
963 * @param cls the `struct MessageQueue` of the message
964 * @param receiver intended receiver
965 * @param success whether it worked or not
966 * @param size_payload bytes payload sent
967 * @param physical bytes sent on wire
970 transmit_send_continuation (void *cls,
971 const struct GNUNET_PeerIdentity *receiver,
972 int success, size_t size_payload, size_t physical)
974 struct MessageQueue *mq = cls;
975 struct NeighbourMapEntry *n;
977 if (NULL == (n = lookup_neighbour (receiver)))
980 return; /* disconnect or other error while transmitting, can happen */
982 if (n->is_active == mq)
984 /* this is still "our" neighbour, remove us from its queue
985 and allow it to send the next message now */
987 if (GNUNET_SCHEDULER_NO_TASK != n->task)
988 GNUNET_SCHEDULER_cancel (n->task);
989 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
991 if (bytes_in_send_queue < mq->message_buf_size)
993 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
994 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
995 bytes_in_send_queue, mq->message_buf_size,
996 (GNUNET_OK == success) ? "OK" : "FAIL",
997 size_payload, physical);
1002 GNUNET_break (size_payload == mq->message_buf_size);
1003 bytes_in_send_queue -= mq->message_buf_size;
1004 GNUNET_STATISTICS_set (GST_stats,
1006 ("# bytes in message queue for other peers"),
1007 bytes_in_send_queue, GNUNET_NO);
1008 if (GNUNET_OK == success)
1009 GNUNET_STATISTICS_update (GST_stats,
1011 ("# messages transmitted to other peers"),
1014 GNUNET_STATISTICS_update (GST_stats,
1016 ("# transmission failures for messages to other peers"),
1018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1019 "Sending message to `%s' of type %u was a %s\n",
1020 GNUNET_i2s (receiver),
1021 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1022 (success == GNUNET_OK) ? "success" : "FAILURE");
1023 if (NULL != mq->cont)
1024 mq->cont (mq->cont_cls, success, size_payload, physical);
1030 * Check the message list for the given neighbour and if we can
1031 * send a message, do so. This function should only be called
1032 * if the connection is at least generally ready for transmission.
1033 * While we will only send one message at a time, no bandwidth
1034 * quota management is performed here. If a message was given to
1035 * the plugin, the continuation will automatically re-schedule
1036 * the 'master' task once the next message might be transmitted.
1038 * @param n target peer for which to transmit
1041 try_transmission_to_peer (struct NeighbourMapEntry *n)
1043 struct MessageQueue *mq;
1044 struct GNUNET_TIME_Relative timeout;
1046 if (NULL == n->primary_address.address)
1048 /* no address, why are we here? */
1052 if ((0 == n->primary_address.address->address_length) &&
1053 (NULL == n->primary_address.session))
1055 /* no address, why are we here? */
1059 if (NULL != n->is_active)
1061 /* transmission already pending */
1065 /* timeout messages from the queue that are past their due date */
1066 while (NULL != (mq = n->messages_head))
1068 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1069 if (timeout.rel_value_us > 0)
1071 GNUNET_STATISTICS_update (GST_stats,
1073 ("# messages timed out while in transport queue"),
1075 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1077 transmit_send_continuation (mq, &n->id,
1079 mq->message_buf_size, 0); /* timeout */
1082 return; /* no more messages */
1083 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1085 (void) send_with_session (n,
1086 mq->message_buf, mq->message_buf_size,
1087 0 /* priority */, timeout, GNUNET_NO,
1088 &transmit_send_continuation, mq);
1093 * Send keepalive message to the neighbour. Must only be called
1094 * if we are on 'connected' state or while trying to switch addresses.
1095 * Will internally determine if a keepalive is truly needed (so can
1096 * always be called).
1098 * @param n neighbour that went idle and needs a keepalive
1101 send_keepalive (struct NeighbourMapEntry *n)
1103 struct SessionKeepAliveMessage m;
1104 struct GNUNET_TIME_Relative timeout;
1107 GNUNET_assert ((S_CONNECTED == n->state) ||
1108 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1109 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1110 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1111 return; /* no keepalive needed at this time */
1113 nonce = 0; /* 0 indicates 'not set' */
1115 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1118 "Sending keep alive to peer `%s' with nonce %u\n",
1119 GNUNET_i2s (&n->id), nonce);
1121 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1122 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1123 m.nonce = htonl (nonce);
1125 timeout = send_with_session (n,
1126 (const void *) &m, sizeof (m),
1127 UINT32_MAX /* priority */,
1128 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1130 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1132 n->primary_address.keep_alive_nonce = nonce;
1133 n->expect_latency_response = GNUNET_YES;
1134 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1135 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1141 * Keep the connection to the given neighbour alive longer,
1142 * we received a KEEPALIVE (or equivalent); send a response.
1144 * @param neighbour neighbour to keep alive (by sending keep alive response)
1145 * @param m the keep alive message containing the nonce to respond to
1148 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1149 const struct GNUNET_MessageHeader *m)
1151 struct NeighbourMapEntry *n;
1152 const struct SessionKeepAliveMessage *msg_in;
1153 struct SessionKeepAliveMessage msg;
1155 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1158 msg_in = (struct SessionKeepAliveMessage *) m;
1159 if (NULL == (n = lookup_neighbour (neighbour)))
1161 GNUNET_STATISTICS_update (GST_stats,
1163 ("# KEEPALIVE messages discarded (peer unknown)"),
1167 if (NULL == n->primary_address.session)
1169 GNUNET_STATISTICS_update (GST_stats,
1171 ("# KEEPALIVE messages discarded (no session)"),
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "Received keep alive request from peer `%s' with nonce %u\n",
1178 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1180 /* send reply to allow neighbour to measure latency */
1181 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1182 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1183 msg.nonce = msg_in->nonce;
1184 (void) send_with_session(n,
1185 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1186 UINT32_MAX /* priority */,
1187 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1193 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1194 * latency to this peer. Pass the updated information (existing ats
1195 * plus calculated latency) to ATS.
1197 * @param neighbour neighbour to keep alive
1198 * @param m the message containing the keep alive response
1201 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1202 const struct GNUNET_MessageHeader *m)
1204 struct NeighbourMapEntry *n;
1205 const struct SessionKeepAliveMessage *msg;
1206 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1208 struct GNUNET_ATS_Information ats;
1210 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1213 msg = (const struct SessionKeepAliveMessage *) m;
1214 if (NULL == (n = lookup_neighbour (neighbour)))
1216 GNUNET_STATISTICS_update (GST_stats,
1218 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1222 if ( (S_CONNECTED != n->state) ||
1223 (GNUNET_YES != n->expect_latency_response) )
1225 GNUNET_STATISTICS_update (GST_stats,
1227 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1231 if (NULL == n->primary_address.address)
1233 GNUNET_STATISTICS_update (GST_stats,
1235 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1239 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1241 GNUNET_STATISTICS_update (GST_stats,
1243 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1249 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1250 "Received keep alive response from peer `%s' for session %p\n",
1251 GNUNET_i2s (&n->id), n->primary_address.session);
1255 /* Update session timeout here */
1256 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 "Updating session for peer `%s' for session %p\n",
1260 GNUNET_i2s (&n->id), n->primary_address.session);
1261 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1268 n->primary_address.keep_alive_nonce = 0;
1269 n->expect_latency_response = GNUNET_NO;
1270 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1271 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1272 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1273 "Latency for peer `%s' is %s\n",
1274 GNUNET_i2s (&n->id),
1275 GNUNET_STRINGS_relative_time_to_string (n->latency,
1277 /* append latency */
1278 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1279 if (n->latency.rel_value_us > UINT32_MAX)
1280 latency = UINT32_MAX;
1282 latency = n->latency.rel_value_us;
1283 ats.value = htonl (latency);
1284 GST_ats_update_metrics (&n->id, n->primary_address.address,
1285 n->primary_address.session, &ats, 1);
1290 * We have received a message from the given sender. How long should
1291 * we delay before receiving more? (Also used to keep the peer marked
1294 * @param sender sender of the message
1295 * @param size size of the message
1296 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1297 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1298 * #GNUNET_SYSERR if the connection is not fully up yet
1299 * @return how long to wait before reading more from this sender
1301 struct GNUNET_TIME_Relative
1302 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1303 *sender, ssize_t size, int *do_forward)
1305 struct NeighbourMapEntry *n;
1306 struct GNUNET_TIME_Relative ret;
1308 if (NULL == neighbours)
1310 *do_forward = GNUNET_NO;
1311 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1313 if (NULL == (n = lookup_neighbour (sender)))
1315 GST_neighbours_try_connect (sender);
1316 if (NULL == (n = lookup_neighbour (sender)))
1318 GNUNET_STATISTICS_update (GST_stats,
1320 ("# messages discarded due to lack of neighbour record"),
1322 *do_forward = GNUNET_NO;
1323 return GNUNET_TIME_UNIT_ZERO;
1326 if (! test_connected (n))
1328 *do_forward = GNUNET_SYSERR;
1329 return GNUNET_TIME_UNIT_ZERO;
1331 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1333 n->quota_violation_count++;
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1336 n->in_tracker.available_bytes_per_s__,
1337 n->quota_violation_count);
1338 /* Discount 32k per violation */
1339 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1343 if (n->quota_violation_count > 0)
1345 /* try to add 32k back */
1346 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1347 n->quota_violation_count--;
1350 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1352 GNUNET_STATISTICS_update (GST_stats,
1354 ("# bandwidth quota violations by other peers"),
1356 *do_forward = GNUNET_NO;
1357 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1359 *do_forward = GNUNET_YES;
1360 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1361 if (ret.rel_value_us > 0)
1363 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1364 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1365 (unsigned long long) n->in_tracker.
1366 consumption_since_last_update__,
1367 (unsigned int) n->in_tracker.available_bytes_per_s__,
1368 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1369 GNUNET_STATISTICS_update (GST_stats,
1370 gettext_noop ("# ms throttling suggested"),
1371 (int64_t) ret.rel_value_us / 1000LL,
1379 * Transmit a message to the given target using the active connection.
1381 * @param target destination
1382 * @param msg message to send
1383 * @param msg_size number of bytes in msg
1384 * @param timeout when to fail with timeout
1385 * @param cont function to call when done
1386 * @param cont_cls closure for 'cont'
1389 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1390 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1391 GST_NeighbourSendContinuation cont, void *cont_cls)
1393 struct NeighbourMapEntry *n;
1394 struct MessageQueue *mq;
1396 /* All ove these cases should never happen; they are all API violations.
1397 But we check anyway, just to be sure. */
1398 if (NULL == (n = lookup_neighbour (target)))
1402 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1405 if (GNUNET_YES != test_connected (n))
1409 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1412 bytes_in_send_queue += msg_size;
1413 GNUNET_STATISTICS_set (GST_stats,
1415 ("# bytes in message queue for other peers"),
1416 bytes_in_send_queue, GNUNET_NO);
1417 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1419 mq->cont_cls = cont_cls;
1420 memcpy (&mq[1], msg, msg_size);
1421 mq->message_buf = (const char *) &mq[1];
1422 mq->message_buf_size = msg_size;
1423 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1424 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1425 if ( (NULL != n->is_active) ||
1426 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1428 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1429 GNUNET_SCHEDULER_cancel (n->task);
1430 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1435 * Send a SESSION_CONNECT message via the given address.
1437 * @param na address to use
1440 send_session_connect (struct NeighbourAddress *na)
1442 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1443 struct SessionConnectMessage connect_msg;
1445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1446 "Sending SESSION_CONNECT message to peer %s\n",
1447 GNUNET_i2s (&na->address->peer));
1448 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1453 if (NULL == na->session)
1454 na->session = papi->get_session (papi->cls, na->address);
1455 if (NULL == na->session)
1460 GNUNET_STATISTICS_update (GST_stats,
1462 ("# SESSION_CONNECT messages sent"),
1464 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1465 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1466 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1467 connect_msg.reserved = htonl (0);
1468 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1470 papi->send (papi->cls,
1472 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1474 GNUNET_TIME_UNIT_FOREVER_REL,
1477 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1478 _("Failed to transmit CONNECT message via plugin to %s\n"),
1479 GST_plugins_a2s (na->address));
1481 GST_neighbours_notify_data_sent (&na->address->peer,
1484 sizeof (struct SessionConnectMessage));
1490 * Send a SESSION_CONNECT_ACK message via the given address.
1492 * @param address address to use
1493 * @param session session to use
1494 * @param timestamp timestamp to use for the ACK message
1497 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1498 struct Session *session,
1499 struct GNUNET_TIME_Absolute timestamp)
1501 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1502 struct SessionConnectMessage connect_msg;
1504 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1505 "Sending CONNECT_ACK to peer `%s'\n",
1506 GNUNET_i2s (&address->peer));
1507 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1512 if (NULL == session)
1513 session = papi->get_session (papi->cls, address);
1514 if (NULL == session)
1519 GNUNET_STATISTICS_update (GST_stats,
1521 ("# CONNECT_ACK messages sent"),
1523 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1524 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1525 connect_msg.reserved = htonl (0);
1526 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1527 (void) papi->send (papi->cls,
1529 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1531 GNUNET_TIME_UNIT_FOREVER_REL,
1538 * Create a fresh entry in the neighbour map for the given peer
1540 * @param peer peer to create an entry for
1541 * @return new neighbour map entry
1543 static struct NeighbourMapEntry *
1544 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1546 struct NeighbourMapEntry *n;
1548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1549 "Creating new neighbour entry for `%s'\n",
1551 n = GNUNET_new (struct NeighbourMapEntry);
1553 n->state = S_NOT_CONNECTED;
1554 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1555 n->last_util_transmission = GNUNET_TIME_absolute_get();
1556 n->util_payload_bytes_recv = 0;
1557 n->util_payload_bytes_sent = 0;
1558 n->util_total_bytes_recv = 0;
1559 n->util_total_bytes_sent = 0;
1560 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1561 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1562 MAX_BANDWIDTH_CARRY_S);
1563 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1564 GNUNET_assert (GNUNET_OK ==
1565 GNUNET_CONTAINER_multipeermap_put (neighbours,
1567 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1573 * Check if the two given addresses are the same.
1574 * Actually only checks if the sessions are non-NULL
1575 * (which they should be) and then if they are identical;
1576 * the actual addresses don't matter if the session
1577 * pointers match anyway, and we must have session pointers
1580 * @param a1 first address to compare
1581 * @param a2 other address to compare
1582 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1585 address_matches (const struct NeighbourAddress *a1,
1586 const struct NeighbourAddress *a2)
1588 if ( (NULL == a1->session) ||
1589 (NULL == a2->session) )
1594 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1599 * Try to create a connection to the given target (eventually).
1601 * @param target peer to try to connect to
1604 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1606 struct NeighbourMapEntry *n;
1608 if (NULL == neighbours)
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611 "Asked to connect to peer `%s' during shutdown\n",
1612 GNUNET_i2s (target));
1613 return; /* during shutdown, do nothing */
1615 n = lookup_neighbour (target);
1616 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1617 "Asked to connect to peer `%s' (state: %s)\n",
1618 GNUNET_i2s (target),
1619 (NULL != n) ? GNUNET_TRANSPORT_p2s(n->state) : "NEW PEER");
1624 case S_NOT_CONNECTED:
1625 /* this should not be possible */
1627 free_neighbour (n, GNUNET_NO);
1630 case S_INIT_BLACKLIST:
1631 case S_CONNECT_SENT:
1632 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1633 case S_CONNECT_RECV_ATS:
1634 case S_CONNECT_RECV_BLACKLIST:
1635 case S_CONNECT_RECV_ACK:
1636 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1637 "Ignoring request to try to connect to `%s', already trying!\n",
1638 GNUNET_i2s (target));
1639 return; /* already trying */
1641 case S_RECONNECT_ATS:
1642 case S_RECONNECT_BLACKLIST:
1643 case S_RECONNECT_SENT:
1644 case S_CONNECTED_SWITCHING_BLACKLIST:
1645 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1646 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1647 "Ignoring request to try to connect, already connected to `%s'!\n",
1648 GNUNET_i2s (target));
1649 return; /* already connected */
1651 /* get rid of remains, ready to re-try immediately */
1652 free_neighbour (n, GNUNET_NO);
1654 case S_DISCONNECT_FINISHED:
1655 /* should not be possible */
1658 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1659 "Unhandled state `%s'\n",
1660 GNUNET_TRANSPORT_p2s (n->state));
1662 free_neighbour (n, GNUNET_NO);
1666 n = setup_neighbour (target);
1667 n->state = S_INIT_ATS;
1668 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1670 GNUNET_ATS_reset_backoff (GST_ats, target);
1671 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1676 * Function called with the result of a blacklist check.
1678 * @param cls closure with the `struct BlackListCheckContext`
1679 * @param peer peer this check affects
1680 * @param result #GNUNET_OK if the address is allowed
1683 handle_test_blacklist_cont (void *cls,
1684 const struct GNUNET_PeerIdentity *peer,
1687 struct BlackListCheckContext *bcc = cls;
1688 struct NeighbourMapEntry *n;
1691 GNUNET_CONTAINER_DLL_remove (bc_head,
1694 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1695 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1697 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1698 if (GNUNET_OK == result)
1700 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
1704 /* Blacklist disagreed on connecting to a peer with this address
1705 * Destroy address because we are not allowed to use it
1707 if (NULL != bcc->na.session)
1708 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1709 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1711 if (NULL == (n = lookup_neighbour (peer)))
1713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1714 "No neighbor entry for peer `%s', ignoring blacklist result\n",
1716 goto cleanup; /* nobody left to care about new address */
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1719 "Received blacklist result for peer `%s' in state %s/%d\n",
1721 GNUNET_TRANSPORT_p2s (n->state),
1722 n->send_connect_ack);
1725 case S_NOT_CONNECTED:
1726 /* this should not be possible */
1728 free_neighbour (n, GNUNET_NO);
1731 /* waiting on ATS suggestion; still, pass address to ATS as a
1734 case S_INIT_BLACKLIST:
1735 /* check if the address the blacklist was fine with matches
1736 ATS suggestion, if so, we can move on! */
1737 if ( (GNUNET_OK == result) &&
1738 (1 == n->send_connect_ack) )
1740 n->send_connect_ack = 2;
1741 send_session_connect_ack_message (bcc->na.address,
1743 n->connect_ack_timestamp);
1745 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1748 "Blacklist result for peer %s is for non-primary address, ignored\n",
1750 break; /* result for an address we currently don't care about */
1752 if (GNUNET_OK == result)
1754 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1755 n->state = S_CONNECT_SENT;
1756 send_session_connect (&n->primary_address);
1760 free_address (&n->primary_address);
1761 n->state = S_INIT_ATS;
1762 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1765 case S_CONNECT_SENT:
1766 /* waiting on CONNECT_ACK, send ACK if one is pending */
1767 if ( (GNUNET_OK == result) &&
1768 (1 == n->send_connect_ack) )
1770 n->send_connect_ack = 2;
1771 send_session_connect_ack_message (n->primary_address.address,
1772 n->primary_address.session,
1773 n->connect_ack_timestamp);
1776 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1777 n->state = S_CONNECT_RECV_ATS;
1778 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1779 GNUNET_ATS_reset_backoff (GST_ats, peer);
1780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1781 "Suggesting address for peer %s to ATS\n",
1783 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
1785 case S_CONNECT_RECV_ATS:
1786 /* waiting on ATS suggestion, don't care about blacklist */
1788 case S_CONNECT_RECV_BLACKLIST:
1789 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1792 "Blacklist result ignored, as it is not for our primary address\n");
1793 break; /* result for an address we currently don't care about */
1795 if (GNUNET_OK == result)
1797 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1798 n->state = S_CONNECT_RECV_ACK;
1799 send_session_connect_ack_message (bcc->na.address,
1801 n->connect_ack_timestamp);
1802 if (1 == n->send_connect_ack)
1803 n->send_connect_ack = 2;
1807 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
1809 plugin = GST_plugins_find (bcc->na.address->transport_name);
1810 if ( (NULL != plugin) &&
1811 (NULL != bcc->na.session) )
1813 plugin->disconnect_session (plugin->cls,
1817 GNUNET_break (NULL != plugin);
1818 free_address (&n->primary_address);
1819 n->state = S_INIT_ATS;
1820 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1821 GNUNET_ATS_reset_backoff (GST_ats, peer);
1824 case S_CONNECT_RECV_ACK:
1825 /* waiting on SESSION_ACK, send ACK if one is pending */
1826 if ( (GNUNET_OK == result) &&
1827 (1 == n->send_connect_ack) )
1829 n->send_connect_ack = 2;
1830 send_session_connect_ack_message (n->primary_address.address,
1831 n->primary_address.session,
1832 n->connect_ack_timestamp);
1836 /* already connected, don't care about blacklist */
1838 case S_RECONNECT_ATS:
1839 /* still waiting on ATS suggestion, don't care about blacklist */
1841 case S_RECONNECT_BLACKLIST:
1842 if ( (GNUNET_OK == result) &&
1843 (1 == n->send_connect_ack) )
1845 n->send_connect_ack = 2;
1846 send_session_connect_ack_message (bcc->na.address,
1848 n->connect_ack_timestamp);
1850 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1853 "Blacklist result ignored, as it is not for our primary address\n");
1854 break; /* result for an address we currently don't care about */
1856 if (GNUNET_OK == result)
1858 n->state = S_RECONNECT_SENT;
1859 send_session_connect (&n->primary_address);
1860 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1864 n->state = S_RECONNECT_ATS;
1865 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1868 case S_RECONNECT_SENT:
1869 /* waiting on CONNECT_ACK, don't care about blacklist */
1870 if ( (GNUNET_OK == result) &&
1871 (1 == n->send_connect_ack) )
1873 n->send_connect_ack = 2;
1874 send_session_connect_ack_message (n->primary_address.address,
1875 n->primary_address.session,
1876 n->connect_ack_timestamp);
1879 case S_CONNECTED_SWITCHING_BLACKLIST:
1880 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883 "Blacklist result ignored, as it is not for our primary address\n");
1884 break; /* result for an address we currently don't care about */
1886 if (GNUNET_OK == result)
1888 send_session_connect (&n->alternative_address);
1889 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1893 n->state = S_CONNECTED;
1894 free_address (&n->alternative_address);
1897 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1898 /* waiting on CONNECT_ACK, don't care about blacklist */
1899 if ( (GNUNET_OK == result) &&
1900 (1 == n->send_connect_ack) )
1902 n->send_connect_ack = 2;
1903 send_session_connect_ack_message (n->primary_address.address,
1904 n->primary_address.session,
1905 n->connect_ack_timestamp);
1909 /* Nothing to do here, ATS will already do what can be done */
1911 case S_DISCONNECT_FINISHED:
1912 /* should not be possible */
1916 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1917 "Unhandled state `%s'\n",
1918 GNUNET_TRANSPORT_p2s (n->state));
1920 free_neighbour (n, GNUNET_NO);
1924 GNUNET_HELLO_address_free (bcc->na.address);
1930 * We want to know if connecting to a particular peer via
1931 * a particular address is allowed. Check it!
1933 * @param peer identity of the peer to switch the address for
1934 * @param ts time at which the check was initiated
1935 * @param address address of the other peer, NULL if other peer
1937 * @param session session to use (or NULL)
1940 check_blacklist (const struct GNUNET_PeerIdentity *peer,
1941 struct GNUNET_TIME_Absolute ts,
1942 const struct GNUNET_HELLO_Address *address,
1943 struct Session *session)
1945 struct BlackListCheckContext *bcc;
1946 struct GST_BlacklistCheck *bc;
1948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1949 "Checking peer `%s' against blacklist\n",
1951 bcc = GNUNET_new (struct BlackListCheckContext);
1952 bcc->na.address = GNUNET_HELLO_address_copy (address);
1953 bcc->na.session = session;
1954 bcc->na.connect_timestamp = ts;
1955 GNUNET_CONTAINER_DLL_insert (bc_head,
1958 if (NULL != (bc = GST_blacklist_test_allowed (peer,
1959 address->transport_name,
1960 &handle_test_blacklist_cont, bcc)))
1962 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
1963 we must only store 'bc' if 'bc' is non-NULL... */
1968 * We received a 'SESSION_CONNECT' message from the other peer.
1969 * Consider switching to it.
1971 * @param message possibly a 'struct SessionConnectMessage' (check format)
1972 * @param peer identity of the peer to switch the address for
1973 * @param address address of the other peer, NULL if other peer
1975 * @param session session to use (or NULL)
1976 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
1979 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
1980 const struct GNUNET_PeerIdentity *peer,
1981 const struct GNUNET_HELLO_Address *address,
1982 struct Session *session)
1984 const struct SessionConnectMessage *scm;
1985 struct NeighbourMapEntry *n;
1986 struct GNUNET_TIME_Absolute ts;
1988 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1989 "Received CONNECT message from peer `%s'\n",
1991 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1993 GNUNET_break_op (0);
1994 return GNUNET_SYSERR;
1996 GNUNET_STATISTICS_update (GST_stats,
1998 ("# CONNECT messages received"),
2000 if (NULL == neighbours)
2002 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2003 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2005 return GNUNET_OK; /* we're shutting down */
2007 scm = (const struct SessionConnectMessage *) message;
2008 GNUNET_break_op (0 == ntohl (scm->reserved));
2009 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2010 n = lookup_neighbour (peer);
2012 n = setup_neighbour (peer);
2013 n->send_connect_ack = 1;
2014 n->connect_ack_timestamp = ts;
2016 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2017 "Received SESSION_CONNECT for peer `%s' in state %s/%d\n",
2019 GNUNET_TRANSPORT_p2s (n->state),
2020 n->send_connect_ack);
2023 case S_NOT_CONNECTED:
2024 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2025 /* Do a blacklist check for the new address */
2026 check_blacklist (peer, ts, address, session);
2029 /* CONNECT message takes priority over us asking ATS for address */
2030 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2032 case S_INIT_BLACKLIST:
2033 case S_CONNECT_SENT:
2034 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2035 case S_CONNECT_RECV_ATS:
2036 case S_CONNECT_RECV_BLACKLIST:
2037 case S_CONNECT_RECV_ACK:
2038 /* It can never hurt to have an alternative address in the above cases,
2039 see if it is allowed */
2040 check_blacklist (peer, ts, address, session);
2043 /* we are already connected and can thus send the ACK immediately;
2044 still, it can never hurt to have an alternative address, so also
2045 tell ATS about it */
2046 GNUNET_assert (NULL != n->primary_address.address);
2047 GNUNET_assert (NULL != n->primary_address.session);
2048 n->send_connect_ack = 0;
2049 send_session_connect_ack_message (n->primary_address.address,
2050 n->primary_address.session, ts);
2051 check_blacklist (peer, ts, address, session);
2053 case S_RECONNECT_ATS:
2054 case S_RECONNECT_BLACKLIST:
2055 case S_RECONNECT_SENT:
2056 /* It can never hurt to have an alternative address in the above cases,
2057 see if it is allowed */
2058 check_blacklist (peer, ts, address, session);
2060 case S_CONNECTED_SWITCHING_BLACKLIST:
2061 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2062 /* we are already connected and can thus send the ACK immediately;
2063 still, it can never hurt to have an alternative address, so also
2064 tell ATS about it */
2065 GNUNET_assert (NULL != n->primary_address.address);
2066 GNUNET_assert (NULL != n->primary_address.session);
2067 n->send_connect_ack = 0;
2068 send_session_connect_ack_message (n->primary_address.address,
2069 n->primary_address.session, ts);
2070 check_blacklist (peer, ts, address, session);
2073 /* get rid of remains without terminating sessions, ready to re-try */
2074 free_neighbour (n, GNUNET_YES);
2075 n = setup_neighbour (peer);
2076 n->state = S_CONNECT_RECV_ATS;
2077 GNUNET_ATS_reset_backoff (GST_ats, peer);
2078 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2080 case S_DISCONNECT_FINISHED:
2081 /* should not be possible */
2085 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2086 "Unhandled state `%s'\n",
2087 GNUNET_TRANSPORT_p2s (n->state));
2089 return GNUNET_SYSERR;
2096 * For an existing neighbour record, set the active connection to
2097 * use the given address.
2099 * @param peer identity of the peer to switch the address for
2100 * @param address address of the other peer, NULL if other peer
2102 * @param session session to use (or NULL)
2103 * @param ats performance data
2104 * @param ats_count number of entries in ats
2105 * @param bandwidth_in inbound quota to be used when connection is up,
2106 * 0 to disconnect from peer
2107 * @param bandwidth_out outbound quota to be used when connection is up,
2108 * 0 to disconnect from peer
2111 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2112 const struct GNUNET_HELLO_Address *address,
2113 struct Session *session,
2114 const struct GNUNET_ATS_Information *ats,
2116 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2117 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2119 struct NeighbourMapEntry *n;
2120 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2123 "ATS has decided on an address for peer %s\n",
2125 GNUNET_assert (NULL != address->transport_name);
2126 if (NULL == (n = lookup_neighbour (peer)))
2128 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2129 "Peer %s is unknown, suggestion ignored\n",
2134 /* Obtain an session for this address from plugin */
2135 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2137 /* we don't have the plugin for this address */
2138 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2139 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2140 address->transport_name,
2142 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2145 if ((NULL == session) && (0 == address->address_length))
2148 if (strlen (address->transport_name) > 0)
2149 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2153 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2154 "ATS tells us to switch to address '%s/%s' session %p for "
2155 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2156 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2157 address->transport_name,
2160 GNUNET_TRANSPORT_p2s (n->state),
2161 n->send_connect_ack,
2162 ntohl (bandwidth_in.value__),
2163 ntohl (bandwidth_out.value__));
2165 if (NULL == session)
2167 session = papi->get_session (papi->cls, address);
2168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2169 "Obtained new session for peer `%s' and address '%s': %p\n",
2170 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2172 if (NULL == session)
2174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2175 "Failed to obtain new session for peer `%s' and address '%s'\n",
2176 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2177 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2182 case S_NOT_CONNECTED:
2184 free_neighbour (n, GNUNET_NO);
2187 set_address (&n->primary_address,
2188 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2189 n->state = S_INIT_BLACKLIST;
2190 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2191 check_blacklist (&n->id,
2192 n->connect_ack_timestamp,
2195 case S_INIT_BLACKLIST:
2196 /* ATS suggests a different address, switch again */
2197 set_address (&n->primary_address,
2198 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2199 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2200 check_blacklist (&n->id,
2201 n->connect_ack_timestamp,
2204 case S_CONNECT_SENT:
2205 /* ATS suggests a different address, switch again */
2206 set_address (&n->primary_address,
2207 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2208 n->state = S_INIT_BLACKLIST;
2209 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2210 check_blacklist (&n->id,
2211 n->connect_ack_timestamp,
2214 case S_CONNECT_RECV_ATS:
2215 set_address (&n->primary_address,
2216 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2217 n->state = S_CONNECT_RECV_BLACKLIST;
2218 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2219 check_blacklist (&n->id,
2220 n->connect_ack_timestamp,
2223 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2224 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2225 check_blacklist (&n->id,
2226 n->connect_ack_timestamp,
2229 case S_CONNECT_RECV_BLACKLIST:
2230 case S_CONNECT_RECV_ACK:
2231 /* ATS asks us to switch while we were trying to connect; switch to new
2232 address and check blacklist again */
2233 set_address (&n->primary_address,
2234 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2235 n->state = S_CONNECT_RECV_BLACKLIST;
2236 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2237 check_blacklist (&n->id,
2238 n->connect_ack_timestamp,
2242 GNUNET_assert (NULL != n->primary_address.address);
2243 GNUNET_assert (NULL != n->primary_address.session);
2244 if (n->primary_address.session == session)
2246 /* not an address change, just a quota change */
2247 set_address (&n->primary_address,
2248 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2251 /* ATS asks us to switch a life connection; see if we can get
2252 a CONNECT_ACK on it before we actually do this! */
2253 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2254 set_address (&n->alternative_address,
2255 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2256 check_blacklist (&n->id,
2257 GNUNET_TIME_absolute_get (),
2260 case S_RECONNECT_ATS:
2261 n->state = S_RECONNECT_BLACKLIST;
2262 set_address (&n->primary_address,
2263 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2264 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2265 check_blacklist (&n->id,
2266 n->connect_ack_timestamp,
2269 case S_RECONNECT_BLACKLIST:
2270 /* ATS asks us to switch while we were trying to reconnect; switch to new
2271 address and check blacklist again */
2272 set_address (&n->primary_address,
2273 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2274 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2275 check_blacklist (&n->id,
2276 n->connect_ack_timestamp,
2279 case S_RECONNECT_SENT:
2280 /* ATS asks us to switch while we were trying to reconnect; switch to new
2281 address and check blacklist again */
2282 n->state = S_RECONNECT_BLACKLIST;
2283 set_address (&n->primary_address,
2284 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2285 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2286 check_blacklist (&n->id,
2287 n->connect_ack_timestamp,
2290 case S_CONNECTED_SWITCHING_BLACKLIST:
2291 if (n->primary_address.session == session)
2293 /* ATS switches back to still-active session */
2294 n->state = S_CONNECTED;
2295 free_address (&n->alternative_address);
2298 /* ATS asks us to switch a life connection, update blacklist check */
2299 set_address (&n->alternative_address,
2300 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2301 check_blacklist (&n->id,
2302 GNUNET_TIME_absolute_get (),
2305 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2306 if (n->primary_address.session == session)
2308 /* ATS switches back to still-active session */
2309 free_address (&n->alternative_address);
2310 n->state = S_CONNECTED;
2313 /* ATS asks us to switch a life connection, update blacklist check */
2314 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2315 set_address (&n->alternative_address,
2316 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2317 check_blacklist (&n->id,
2318 GNUNET_TIME_absolute_get (),
2322 /* not going to switch addresses while disconnecting */
2324 case S_DISCONNECT_FINISHED:
2328 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2329 "Unhandled state `%s'\n",
2330 GNUNET_TRANSPORT_p2s (n->state));
2338 send_utilization_data (void *cls,
2339 const struct GNUNET_PeerIdentity *key,
2342 struct NeighbourMapEntry *n = value;
2343 struct GNUNET_ATS_Information atsi[4];
2345 uint32_t bps_pl_out;
2348 struct GNUNET_TIME_Relative delta;
2350 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2351 GNUNET_TIME_absolute_get ());
2355 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2356 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2358 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2359 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2366 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2367 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2369 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2370 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2373 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2374 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2378 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2379 atsi[0].value = htonl (bps_out);
2380 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2381 atsi[1].value = htonl (bps_in);
2383 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2384 atsi[2].value = htonl (bps_pl_out);
2385 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2386 atsi[3].value = htonl (bps_pl_in);
2388 GST_ats_update_metrics (key, n->primary_address.address,
2389 n->primary_address.session, atsi, 4);
2390 n->util_payload_bytes_recv = 0;
2391 n->util_payload_bytes_sent = 0;
2392 n->util_total_bytes_recv = 0;
2393 n->util_total_bytes_sent = 0;
2394 n->last_util_transmission = GNUNET_TIME_absolute_get();
2400 * Task transmitting utilization in a regular interval
2402 * @param cls the 'struct NeighbourMapEntry' for which we are running
2403 * @param tc scheduler context (unused)
2406 utilization_transmission (void *cls,
2407 const struct GNUNET_SCHEDULER_TaskContext *tc)
2409 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2411 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2412 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2414 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2415 utilization_transmission, NULL);
2421 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2422 const struct GNUNET_HELLO_Address *address,
2423 struct Session *session,
2424 const struct GNUNET_MessageHeader *message)
2426 struct NeighbourMapEntry *n;
2428 n = lookup_neighbour (peer);
2431 n->util_total_bytes_recv += ntohs(message->size);
2436 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2437 const struct GNUNET_HELLO_Address *address,
2438 struct Session *session,
2439 const struct GNUNET_MessageHeader *message)
2441 struct NeighbourMapEntry *n;
2442 n = lookup_neighbour (peer);
2445 n->util_payload_bytes_recv += ntohs(message->size);
2450 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2451 const struct GNUNET_HELLO_Address *address,
2452 struct Session *session,
2455 struct NeighbourMapEntry *n;
2456 n = lookup_neighbour (peer);
2459 if (n->primary_address.session != session)
2461 n->util_total_bytes_sent += size;
2466 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2469 struct NeighbourMapEntry *n;
2470 n = lookup_neighbour (peer);
2473 n->util_payload_bytes_sent += size;
2478 * Master task run for every neighbour. Performs all of the time-related
2479 * activities (keep alive, send next message, disconnect if idle, finish
2480 * clean up after disconnect).
2482 * @param cls the 'struct NeighbourMapEntry' for which we are running
2483 * @param tc scheduler context (unused)
2486 master_task (void *cls,
2487 const struct GNUNET_SCHEDULER_TaskContext *tc)
2489 struct NeighbourMapEntry *n = cls;
2490 struct GNUNET_TIME_Relative delay;
2492 n->task = GNUNET_SCHEDULER_NO_TASK;
2493 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2494 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2495 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2496 GNUNET_i2s (&n->id),
2497 GNUNET_TRANSPORT_p2s(n->state),
2498 GNUNET_STRINGS_relative_time_to_string (delay,
2502 case S_NOT_CONNECTED:
2503 /* invalid state for master task, clean up */
2505 n->state = S_DISCONNECT_FINISHED;
2506 free_neighbour (n, GNUNET_NO);
2509 if (0 == delay.rel_value_us)
2511 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2512 "Connection to `%s' timed out waiting for ATS to provide address\n",
2513 GNUNET_i2s (&n->id));
2514 n->state = S_DISCONNECT_FINISHED;
2515 free_neighbour (n, GNUNET_NO);
2519 case S_INIT_BLACKLIST:
2520 if (0 == delay.rel_value_us)
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2524 GNUNET_i2s (&n->id));
2525 n->state = S_DISCONNECT_FINISHED;
2526 free_neighbour (n, GNUNET_NO);
2530 case S_CONNECT_SENT:
2531 if (0 == delay.rel_value_us)
2533 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2534 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2535 GNUNET_i2s (&n->id));
2536 /* We could not send to this address, delete address and session */
2537 if (NULL != n->primary_address.session)
2538 GNUNET_ATS_address_destroyed (GST_ats,
2539 n->primary_address.address, n->primary_address.session);
2540 GNUNET_ATS_address_destroyed (GST_ats,
2541 n->primary_address.address, NULL);
2542 disconnect_neighbour (n);
2546 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2547 if (0 == delay.rel_value_us)
2549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2550 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2551 GNUNET_i2s (&n->id));
2552 n->state = S_DISCONNECT_FINISHED;
2553 free_neighbour (n, GNUNET_NO);
2557 case S_CONNECT_RECV_ATS:
2558 if (0 == delay.rel_value_us)
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2561 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2562 GNUNET_i2s (&n->id));
2563 n->state = S_DISCONNECT_FINISHED;
2564 free_neighbour (n, GNUNET_NO);
2568 case S_CONNECT_RECV_BLACKLIST:
2569 if (0 == delay.rel_value_us)
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2573 GNUNET_i2s (&n->id));
2574 n->state = S_DISCONNECT_FINISHED;
2575 free_neighbour (n, GNUNET_NO);
2579 case S_CONNECT_RECV_ACK:
2580 if (0 == delay.rel_value_us)
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2584 GNUNET_i2s (&n->id));
2585 disconnect_neighbour (n);
2590 if (0 == delay.rel_value_us)
2592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2593 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2594 GNUNET_i2s (&n->id));
2595 disconnect_neighbour (n);
2598 try_transmission_to_peer (n);
2601 case S_RECONNECT_ATS:
2602 if (0 == delay.rel_value_us)
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2606 GNUNET_i2s (&n->id));
2607 disconnect_neighbour (n);
2611 case S_RECONNECT_BLACKLIST:
2612 if (0 == delay.rel_value_us)
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2616 GNUNET_i2s (&n->id));
2617 disconnect_neighbour (n);
2621 case S_RECONNECT_SENT:
2622 if (0 == delay.rel_value_us)
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2625 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2626 GNUNET_i2s (&n->id));
2627 disconnect_neighbour (n);
2631 case S_CONNECTED_SWITCHING_BLACKLIST:
2632 if (0 == delay.rel_value_us)
2634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2635 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2636 GNUNET_i2s (&n->id));
2637 disconnect_neighbour (n);
2640 try_transmission_to_peer (n);
2643 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2644 if (0 == delay.rel_value_us)
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2647 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2648 GNUNET_i2s (&n->id));
2649 disconnect_neighbour (n);
2652 try_transmission_to_peer (n);
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2658 GNUNET_i2s (&n->id));
2659 free_neighbour (n, GNUNET_NO);
2661 case S_DISCONNECT_FINISHED:
2662 /* how did we get here!? */
2666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2667 "Unhandled state `%s'\n",
2668 GNUNET_TRANSPORT_p2s (n->state));
2672 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2673 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2674 (S_CONNECTED == n->state) )
2676 /* if we are *now* in one of these three states, we're sending
2677 keep alive messages, so we need to consider the keepalive
2678 delay, not just the connection timeout */
2679 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2682 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2683 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2690 * Send a SESSION_ACK message to the neighbour to confirm that we
2691 * got his CONNECT_ACK.
2693 * @param n neighbour to send the SESSION_ACK to
2696 send_session_ack_message (struct NeighbourMapEntry *n)
2698 struct GNUNET_MessageHeader msg;
2700 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2701 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2702 (void) send_with_session(n,
2703 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2704 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
2710 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2711 * Consider switching to it.
2713 * @param message possibly a 'struct SessionConnectMessage' (check format)
2714 * @param peer identity of the peer to switch the address for
2715 * @param address address of the other peer, NULL if other peer
2717 * @param session session to use (or NULL)
2718 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2721 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2722 const struct GNUNET_PeerIdentity *peer,
2723 const struct GNUNET_HELLO_Address *address,
2724 struct Session *session)
2726 const struct SessionConnectMessage *scm;
2727 struct GNUNET_TIME_Absolute ts;
2728 struct NeighbourMapEntry *n;
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2731 "Received CONNECT_ACK message from peer `%s'\n",
2734 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2736 GNUNET_break_op (0);
2737 return GNUNET_SYSERR;
2739 GNUNET_STATISTICS_update (GST_stats,
2741 ("# CONNECT_ACK messages received"),
2743 scm = (const struct SessionConnectMessage *) message;
2744 GNUNET_break_op (ntohl (scm->reserved) == 0);
2745 if (NULL == (n = lookup_neighbour (peer)))
2747 GNUNET_STATISTICS_update (GST_stats,
2749 ("# unexpected CONNECT_ACK messages (no peer)"),
2751 return GNUNET_SYSERR;
2753 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2756 case S_NOT_CONNECTED:
2758 free_neighbour (n, GNUNET_NO);
2759 return GNUNET_SYSERR;
2761 case S_INIT_BLACKLIST:
2762 GNUNET_STATISTICS_update (GST_stats,
2764 ("# unexpected CONNECT_ACK messages (not ready)"),
2767 case S_CONNECT_SENT:
2768 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
2770 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2771 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
2774 n->state = S_CONNECTED;
2775 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2776 GNUNET_STATISTICS_set (GST_stats,
2777 gettext_noop ("# peers connected"),
2778 ++neighbours_connected,
2780 connect_notify_cb (callback_cls, &n->id,
2781 n->primary_address.bandwidth_in,
2782 n->primary_address.bandwidth_out);
2783 /* Tell ATS that the outbound session we created to send CONNECT was successful */
2784 GST_ats_add_address (n->primary_address.address,
2785 n->primary_address.session,
2787 set_address (&n->primary_address,
2788 n->primary_address.address,
2789 n->primary_address.session,
2790 n->primary_address.bandwidth_in,
2791 n->primary_address.bandwidth_out,
2793 send_session_ack_message (n);
2795 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2796 case S_CONNECT_RECV_ATS:
2797 case S_CONNECT_RECV_BLACKLIST:
2798 case S_CONNECT_RECV_ACK:
2799 GNUNET_STATISTICS_update (GST_stats,
2801 ("# unexpected CONNECT_ACK messages (not ready)"),
2805 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2806 send_session_ack_message (n);
2808 case S_RECONNECT_ATS:
2809 case S_RECONNECT_BLACKLIST:
2810 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2811 to give us a new address... */
2812 GNUNET_STATISTICS_update (GST_stats,
2814 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2817 case S_RECONNECT_SENT:
2818 /* new address worked; go back to connected! */
2819 n->state = S_CONNECTED;
2820 send_session_ack_message (n);
2822 case S_CONNECTED_SWITCHING_BLACKLIST:
2823 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2824 send_session_ack_message (n);
2826 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2827 /* new address worked; adopt it and go back to connected! */
2828 n->state = S_CONNECTED;
2829 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2830 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2832 GST_ats_add_address (n->alternative_address.address,
2833 n->alternative_address.session,
2835 set_address (&n->primary_address,
2836 n->alternative_address.address,
2837 n->alternative_address.session,
2838 n->alternative_address.bandwidth_in,
2839 n->alternative_address.bandwidth_out,
2841 free_address (&n->alternative_address);
2842 send_session_ack_message (n);
2845 GNUNET_STATISTICS_update (GST_stats,
2847 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2849 return GNUNET_SYSERR;
2850 case S_DISCONNECT_FINISHED:
2854 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2855 "Unhandled state `%s'\n",
2856 GNUNET_TRANSPORT_p2s (n->state));
2858 return GNUNET_SYSERR;
2865 * A session was terminated. Take note; if needed, try to get
2866 * an alternative address from ATS.
2868 * @param peer identity of the peer where the session died
2869 * @param session session that is gone
2870 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
2871 * this session was not in use
2874 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2875 struct Session *session)
2877 struct NeighbourMapEntry *n;
2878 struct BlackListCheckContext *bcc;
2879 struct BlackListCheckContext *bcc_next;
2881 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2883 while (NULL != (bcc = bcc_next))
2885 bcc_next = bcc->next;
2886 if (bcc->na.session == session)
2888 if (NULL != bcc->bc)
2889 GST_blacklist_test_cancel (bcc->bc);
2890 GNUNET_HELLO_address_free (bcc->na.address);
2891 GNUNET_CONTAINER_DLL_remove (bc_head,
2897 if (NULL == (n = lookup_neighbour (peer)))
2898 return GNUNET_NO; /* can't affect us */
2899 if (session != n->primary_address.session)
2901 if (session == n->alternative_address.session)
2903 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2904 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2905 n->state = S_CONNECTED;
2908 free_address (&n->alternative_address);
2910 return GNUNET_NO; /* doesn't affect us further */
2913 n->expect_latency_response = GNUNET_NO;
2916 case S_NOT_CONNECTED:
2918 free_neighbour (n, GNUNET_NO);
2922 free_neighbour (n, GNUNET_NO);
2924 case S_INIT_BLACKLIST:
2925 case S_CONNECT_SENT:
2926 free_address (&n->primary_address);
2927 n->state = S_INIT_ATS;
2928 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2929 // FIXME: need to ask ATS for suggestions again?
2930 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2932 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2933 case S_CONNECT_RECV_ATS:
2934 case S_CONNECT_RECV_BLACKLIST:
2935 case S_CONNECT_RECV_ACK:
2936 /* error on inbound session; free neighbour entirely */
2937 free_address (&n->primary_address);
2938 free_neighbour (n, GNUNET_NO);
2941 n->state = S_RECONNECT_ATS;
2942 free_address (&n->primary_address);
2943 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2944 /* FIXME: is this ATS call needed? */
2945 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2947 case S_RECONNECT_ATS:
2948 /* we don't have an address, how can it go down? */
2951 case S_RECONNECT_BLACKLIST:
2952 case S_RECONNECT_SENT:
2953 n->state = S_RECONNECT_ATS;
2954 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2955 // FIXME: need to ask ATS for suggestions again?
2956 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2958 case S_CONNECTED_SWITCHING_BLACKLIST:
2959 /* primary went down while we were checking secondary against
2960 blacklist, adopt secondary as primary */
2961 n->state = S_RECONNECT_BLACKLIST;
2962 free_address (&n->primary_address);
2963 n->primary_address = n->alternative_address;
2964 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2965 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2967 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2968 /* primary went down while we were waiting for CONNECT_ACK on secondary;
2969 secondary as primary */
2970 n->state = S_RECONNECT_SENT;
2971 free_address (&n->primary_address);
2972 n->primary_address = n->alternative_address;
2973 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2974 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2977 free_address (&n->primary_address);
2979 case S_DISCONNECT_FINISHED:
2980 /* neighbour was freed and plugins told to terminate session */
2984 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2985 "Unhandled state `%s'\n",
2986 GNUNET_TRANSPORT_p2s (n->state));
2990 if (GNUNET_SCHEDULER_NO_TASK != n->task)
2991 GNUNET_SCHEDULER_cancel (n->task);
2992 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2998 * We received a 'SESSION_ACK' message from the other peer.
2999 * If we sent a 'CONNECT_ACK' last, this means we are now
3000 * connected. Otherwise, do nothing.
3002 * @param message possibly a 'struct SessionConnectMessage' (check format)
3003 * @param peer identity of the peer to switch the address for
3004 * @param address address of the other peer, NULL if other peer
3006 * @param session session to use (or NULL)
3007 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3010 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3011 const struct GNUNET_PeerIdentity *peer,
3012 const struct GNUNET_HELLO_Address *address,
3013 struct Session *session)
3015 struct NeighbourMapEntry *n;
3017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3018 "Received SESSION_ACK message from peer `%s'\n",
3020 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3022 GNUNET_break_op (0);
3023 return GNUNET_SYSERR;
3025 GNUNET_STATISTICS_update (GST_stats,
3027 ("# SESSION_ACK messages received"),
3029 if (NULL == (n = lookup_neighbour (peer)))
3031 GNUNET_break_op (0);
3032 return GNUNET_SYSERR;
3034 /* check if we are in a plausible state for having sent
3035 a CONNECT_ACK. If not, return, otherwise break */
3036 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3037 (S_CONNECT_SENT != n->state) ) ||
3038 (2 != n->send_connect_ack) )
3040 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3041 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3043 GNUNET_TRANSPORT_p2s (n->state),
3044 n->send_connect_ack);
3045 GNUNET_STATISTICS_update (GST_stats,
3046 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3050 n->state = S_CONNECTED;
3051 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3052 GNUNET_STATISTICS_set (GST_stats,
3053 gettext_noop ("# peers connected"),
3054 ++neighbours_connected,
3056 connect_notify_cb (callback_cls, &n->id,
3057 n->primary_address.bandwidth_in,
3058 n->primary_address.bandwidth_out);
3060 GST_ats_add_address (n->primary_address.address,
3061 n->primary_address.session,
3063 set_address (&n->primary_address,
3064 n->primary_address.address,
3065 n->primary_address.session,
3066 n->primary_address.bandwidth_in,
3067 n->primary_address.bandwidth_out,
3074 * Test if we're connected to the given peer.
3076 * @param target peer to test
3077 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3080 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3082 return test_connected (lookup_neighbour (target));
3087 * Change the incoming quota for the given peer.
3089 * @param neighbour identity of peer to change qutoa for
3090 * @param quota new quota
3093 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3094 struct GNUNET_BANDWIDTH_Value32NBO quota)
3096 struct NeighbourMapEntry *n;
3098 if (NULL == (n = lookup_neighbour (neighbour)))
3100 GNUNET_STATISTICS_update (GST_stats,
3102 ("# SET QUOTA messages ignored (no such peer)"),
3106 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3107 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3108 ntohl (quota.value__), GNUNET_i2s (&n->id));
3109 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3110 if (0 != ntohl (quota.value__))
3112 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3113 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3114 GNUNET_i2s (&n->id));
3115 if (GNUNET_YES == test_connected (n))
3116 GNUNET_STATISTICS_update (GST_stats,
3117 gettext_noop ("# disconnects due to quota of 0"),
3119 disconnect_neighbour (n);
3124 * We received a disconnect message from the given peer,
3125 * validate and process.
3127 * @param peer sender of the message
3128 * @param msg the disconnect message
3131 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3132 const struct GNUNET_MessageHeader *msg)
3134 struct NeighbourMapEntry *n;
3135 const struct SessionDisconnectMessage *sdm;
3137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3138 "Received DISCONNECT message from peer `%s'\n",
3140 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3142 // GNUNET_break_op (0);
3143 GNUNET_STATISTICS_update (GST_stats,
3145 ("# disconnect messages ignored (old format)"), 1,
3149 GNUNET_STATISTICS_update (GST_stats,
3151 ("# DISCONNECT messages received"),
3153 sdm = (const struct SessionDisconnectMessage *) msg;
3154 if (NULL == (n = lookup_neighbour (peer)))
3155 return; /* gone already */
3156 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3158 GNUNET_STATISTICS_update (GST_stats,
3160 ("# disconnect messages ignored (timestamp)"), 1,
3164 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3166 GNUNET_break_op (0);
3169 if (ntohl (sdm->purpose.size) !=
3170 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3171 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3172 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3174 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3175 "%s message from peer `%s' has invalid size \n",
3178 GNUNET_break_op (0);
3182 GNUNET_CRYPTO_eddsa_verify
3183 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3184 &sdm->signature, &sdm->public_key))
3186 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3187 "%s message from peer `%s' cannot be verified \n",
3190 GNUNET_break_op (0);
3193 if (GNUNET_YES == test_connected (n))
3194 GNUNET_STATISTICS_update (GST_stats,
3196 ("# other peer asked to disconnect from us"), 1,
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3199 "Disconnecting by request from peer %s\n",
3201 disconnect_neighbour (n);
3206 * Closure for the neighbours_iterate function.
3208 struct IteratorContext
3211 * Function to call on each connected neighbour.
3213 GST_NeighbourIterator cb;
3223 * Call the callback from the closure for each neighbour.
3225 * @param cls the `struct IteratorContext`
3226 * @param key the hash of the public key of the neighbour
3227 * @param value the `struct NeighbourMapEntry`
3228 * @return #GNUNET_OK (continue to iterate)
3231 neighbours_iterate (void *cls,
3232 const struct GNUNET_PeerIdentity *key,
3235 struct IteratorContext *ic = cls;
3236 struct NeighbourMapEntry *n = value;
3237 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3238 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3241 if (NULL != n->primary_address.address)
3243 bandwidth_in = n->primary_address.bandwidth_in;
3244 bandwidth_out = n->primary_address.bandwidth_out;
3248 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3249 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3253 n->primary_address.address,
3256 bandwidth_in, bandwidth_out);
3262 * Iterate over all connected neighbours.
3264 * @param cb function to call
3265 * @param cb_cls closure for cb
3268 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3270 struct IteratorContext ic;
3272 if (NULL == neighbours)
3273 return; /* can happen during shutdown */
3276 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3281 * If we have an active connection to the given target, it must be shutdown.
3283 * @param target peer to disconnect from
3286 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3288 struct NeighbourMapEntry *n;
3290 if (NULL == (n = lookup_neighbour (target)))
3291 return; /* not active */
3292 if (GNUNET_YES == test_connected (n))
3293 GNUNET_STATISTICS_update (GST_stats,
3295 ("# disconnected from peer upon explicit request"), 1,
3297 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3298 "Forced disconnect from peer %s\n",
3299 GNUNET_i2s (target));
3300 disconnect_neighbour (n);
3305 * Obtain current latency information for the given neighbour.
3307 * @param peer to get the latency for
3308 * @return observed latency of the address, FOREVER if the
3309 * the connection is not up
3311 struct GNUNET_TIME_Relative
3312 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3314 struct NeighbourMapEntry *n;
3316 n = lookup_neighbour (peer);
3318 return GNUNET_TIME_UNIT_FOREVER_REL;
3322 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3323 case S_CONNECTED_SWITCHING_BLACKLIST:
3324 case S_RECONNECT_SENT:
3325 case S_RECONNECT_ATS:
3326 case S_RECONNECT_BLACKLIST:
3328 case S_NOT_CONNECTED:
3329 case S_INIT_BLACKLIST:
3331 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3332 case S_CONNECT_RECV_ATS:
3333 case S_CONNECT_RECV_BLACKLIST:
3334 case S_CONNECT_RECV_ACK:
3335 case S_CONNECT_SENT:
3337 case S_DISCONNECT_FINISHED:
3338 return GNUNET_TIME_UNIT_FOREVER_REL;
3340 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3341 "Unhandled state `%s'\n",
3342 GNUNET_TRANSPORT_p2s (n->state));
3346 return GNUNET_TIME_UNIT_FOREVER_REL;
3351 * Obtain current address information for the given neighbour.
3354 * @return address currently used
3356 struct GNUNET_HELLO_Address *
3357 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3359 struct NeighbourMapEntry *n;
3361 n = lookup_neighbour (peer);
3364 return n->primary_address.address;
3369 * Initialize the neighbours subsystem.
3371 * @param cls closure for callbacks
3372 * @param connect_cb function to call if we connect to a peer
3373 * @param disconnect_cb function to call if we disconnect from a peer
3374 * @param peer_address_cb function to call if we change an active address
3376 * @param max_fds maximum number of fds to use
3379 GST_neighbours_start (void *cls,
3380 NotifyConnect connect_cb,
3381 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3382 GNUNET_TRANSPORT_AddressChangeCallback peer_address_cb,
3383 unsigned int max_fds)
3386 connect_notify_cb = connect_cb;
3387 disconnect_notify_cb = disconnect_cb;
3388 address_change_cb = peer_address_cb;
3389 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3390 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3391 utilization_transmission, NULL);
3396 * Disconnect from the given neighbour.
3399 * @param key hash of neighbour's public key (not used)
3400 * @param value the 'struct NeighbourMapEntry' of the neighbour
3401 * @return #GNUNET_OK (continue to iterate)
3404 disconnect_all_neighbours (void *cls,
3405 const struct GNUNET_PeerIdentity *key,
3408 struct NeighbourMapEntry *n = value;
3410 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3411 "Disconnecting peer `%4s', %s\n",
3412 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3413 n->state = S_DISCONNECT_FINISHED;
3414 free_neighbour (n, GNUNET_NO);
3420 * Cleanup the neighbours subsystem.
3423 GST_neighbours_stop ()
3425 if (NULL == neighbours)
3427 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3429 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3430 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3433 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3434 &disconnect_all_neighbours,
3436 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3438 callback_cls = NULL;
3439 connect_notify_cb = NULL;
3440 disconnect_notify_cb = NULL;
3441 address_change_cb = NULL;
3445 /* end of file gnunet-service-transport_neighbours.c */