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;
235 * Possible state of a neighbour. Initially, we are #S_NOT_CONNECTED.
237 * Then, there are two main paths. If we receive a CONNECT message, we
238 * first run a check against the blacklist (#S_CONNECT_RECV_BLACKLIST_INBOUND).
239 * If this check is successful, we give the inbound address to ATS.
240 * After the check we ask ATS for a suggestion (S_CONNECT_RECV_ATS).
241 * If ATS makes a suggestion, we ALSO give that suggestion to the blacklist
242 * (#S_CONNECT_RECV_BLACKLIST). Once the blacklist approves the
243 * address we got from ATS, we send our CONNECT_ACK and go to
244 * #S_CONNECT_RECV_ACK. If we receive a SESSION_ACK, we go to
245 * #S_CONNECTED (and notify everyone about the new connection). If the
246 * operation times out, we go to #S_DISCONNECT.
248 * The other case is where we transmit a CONNECT message first. We
249 * start with #S_INIT_ATS. If we get an address, we enter
250 * #S_INIT_BLACKLIST and check the blacklist. If the blacklist is OK
251 * with the connection, we actually send the CONNECT message and go to
252 * state S_CONNECT_SENT. Once we receive a CONNECT_ACK, we go to
253 * #S_CONNECTED (and notify everyone about the new connection and send
254 * back a SESSION_ACK). If the operation times out, we go to
257 * If the session is in trouble (i.e. transport-level disconnect or
258 * timeout), we go to #S_RECONNECT_ATS where we ask ATS for a new
259 * address (we don't notify anyone about the disconnect yet). Once we
260 * have a new address, we go to #S_RECONNECT_BLACKLIST to check the new
261 * address against the blacklist. If the blacklist approves, we enter
262 * #S_RECONNECT_SENT and send a CONNECT message. If we receive a
263 * CONNECT_ACK, we go to #S_CONNECTED and nobody noticed that we had
264 * trouble; we also send a SESSION_ACK at this time just in case. If
265 * the operation times out, we go to S_DISCONNECT (and notify everyone
266 * about the lost connection).
268 * If ATS decides to switch addresses while we have a normal
269 * connection, we go to #S_CONNECTED_SWITCHING_BLACKLIST to check the
270 * new address against the blacklist. If the blacklist approves, we
271 * go to #S_CONNECTED_SWITCHING_CONNECT_SENT and send a
272 * SESSION_CONNECT. If we get a SESSION_ACK back, we switch the
273 * primary connection to the suggested alternative from ATS, go back
274 * to #S_CONNECTED and send a SESSION_ACK to the other peer just to be
275 * sure. If the operation times out (or the blacklist disapproves),
276 * we go to #S_CONNECTED (and notify ATS that the given alternative
277 * address is "invalid").
279 * Once a session is in #S_DISCONNECT, it is cleaned up and then goes
280 * to (#S_DISCONNECT_FINISHED). If we receive an explicit disconnect
281 * request, we can go from any state to #S_DISCONNECT, possibly after
282 * generating disconnect notifications.
284 * Note that it is quite possible that while we are in any of these
285 * states, we could receive a 'CONNECT' request from the other peer.
286 * We then enter a 'weird' state where we pursue our own primary state
287 * machine (as described above), but with the 'send_connect_ack' flag
288 * set to 1. If our state machine allows us to send a 'CONNECT_ACK'
289 * (because we have an acceptable address), we send the 'CONNECT_ACK'
290 * and set the 'send_connect_ack' to 2. If we then receive a
291 * 'SESSION_ACK', we go to #S_CONNECTED (and reset 'send_connect_ack'
298 * fresh peer or completely disconnected
303 * Asked to initiate connection, trying to get address from ATS
308 * Asked to initiate connection, trying to get address approved
314 * Sent CONNECT message to other peer, waiting for CONNECT_ACK
319 * Received a CONNECT, do a blacklist check for inbound address
321 S_CONNECT_RECV_BLACKLIST_INBOUND,
324 * Received a CONNECT, asking ATS about address suggestions.
329 * Received CONNECT from other peer, got an address, checking with blacklist.
331 S_CONNECT_RECV_BLACKLIST,
334 * CONNECT request from other peer was SESSION_ACK'ed, waiting for
340 * Got our CONNECT_ACK/SESSION_ACK, connection is up.
345 * Connection got into trouble, rest of the system still believes
346 * it to be up, but we're getting a new address from ATS.
351 * Connection got into trouble, rest of the system still believes
352 * it to be up; we are checking the new address against the blacklist.
354 S_RECONNECT_BLACKLIST,
357 * Sent CONNECT over new address (either by ATS telling us to switch
358 * addresses or from RECONNECT_ATS); if this fails, we need to tell
359 * the rest of the system about a disconnect.
364 * We have some primary connection, but ATS suggested we switch
365 * to some alternative; we're now checking the alternative against
368 S_CONNECTED_SWITCHING_BLACKLIST,
371 * We have some primary connection, but ATS suggested we switch
372 * to some alternative; we now sent a CONNECT message for the
373 * alternative session to the other peer and waiting for a
374 * CONNECT_ACK to make this our primary connection.
376 S_CONNECTED_SWITCHING_CONNECT_SENT,
379 * Disconnect in progress (we're sending the DISCONNECT message to the
380 * other peer; after that is finished, the state will be cleaned up).
385 * We're finished with the disconnect; and are cleaning up the state
386 * now! We put the struct into this state when we are really in the
387 * task that calls 'free' on it and are about to remove the record
388 * from the map. We should never find a 'struct NeighbourMapEntry'
389 * in this state in the map. Accessing a 'struct NeighbourMapEntry'
390 * in this state virtually always means using memory that has been
391 * freed (the exception being the cleanup code in #free_neighbour()).
393 S_DISCONNECT_FINISHED
398 * A possible address we could use to communicate with a neighbour.
400 struct NeighbourAddress
404 * Active session for this address.
406 struct Session *session;
409 * Network-level address information.
411 struct GNUNET_HELLO_Address *address;
414 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
415 * peer for this address. Use to check that the ACK is in response
416 * to our most recent 'CONNECT'.
418 struct GNUNET_TIME_Absolute connect_timestamp;
421 * Inbound bandwidth from ATS for this address.
423 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
426 * Outbound bandwidth from ATS for this address.
428 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
431 * Did we tell ATS that this is our 'active' address?
436 * The current nonce sent in the last keep alive messages
438 uint32_t keep_alive_nonce;
443 * Entry in neighbours.
445 struct NeighbourMapEntry
449 * Head of list of messages we would like to send to this peer;
450 * must contain at most one message per client.
452 struct MessageQueue *messages_head;
455 * Tail of list of messages we would like to send to this peer; must
456 * contain at most one message per client.
458 struct MessageQueue *messages_tail;
461 * Are we currently trying to send a message? If so, which one?
463 struct MessageQueue *is_active;
466 * Primary address we currently use to communicate with the neighbour.
468 struct NeighbourAddress primary_address;
471 * Alternative address currently under consideration for communicating
472 * with the neighbour.
474 struct NeighbourAddress alternative_address;
477 * Identity of this neighbour.
479 struct GNUNET_PeerIdentity id;
482 * Main task that drives this peer (timeouts, keepalives, etc.).
483 * Always runs the 'master_task'.
485 GNUNET_SCHEDULER_TaskIdentifier task;
488 * At what time should we sent the next keep-alive message?
490 struct GNUNET_TIME_Absolute keep_alive_time;
493 * At what time did we sent the last keep-alive message? Used
494 * to calculate round-trip time ("latency").
496 struct GNUNET_TIME_Absolute last_keep_alive_time;
499 * Timestamp we should include in our next CONNECT_ACK message.
500 * (only valid if 'send_connect_ack' is #GNUNET_YES). Used to build
501 * our CONNECT_ACK message.
503 struct GNUNET_TIME_Absolute connect_ack_timestamp;
506 * ATS address suggest handle
508 struct GNUNET_ATS_SuggestHandle *suggest_handle;
511 * Time where we should cut the connection (timeout) if we don't
512 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
513 * if we are in S_CONNECTED).
515 struct GNUNET_TIME_Absolute timeout;
518 * Latest calculated latency value
520 struct GNUNET_TIME_Relative latency;
523 * Tracker for inbound bandwidth.
525 struct GNUNET_BANDWIDTH_Tracker in_tracker;
528 * How often has the other peer (recently) violated the inbound
529 * traffic limit? Incremented by 10 per violation, decremented by 1
530 * per non-violation (for each time interval).
532 unsigned int quota_violation_count;
535 * The current state of the peer.
540 * Did we sent an KEEP_ALIVE message and are we expecting a response?
542 int expect_latency_response;
545 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
546 * (once we have an address to use and the peer has been allowed by our
547 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
548 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
549 * a 'SESSION_ACK' (regardless of what our own state machine might say).
551 int send_connect_ack;
554 * Tracking utilization of outbound bandwidth
556 uint32_t util_payload_bytes_sent;
559 * Tracking utilization of inbound bandwidth
561 uint32_t util_payload_bytes_recv;
564 * Tracking utilization of outbound bandwidth
566 uint32_t util_total_bytes_sent;
569 * Tracking utilization of inbound bandwidth
571 uint32_t util_total_bytes_recv;
574 * Date of last utilization transmission
576 struct GNUNET_TIME_Absolute last_util_transmission;
581 * Context for blacklist checks and the #handle_test_blacklist_cont()
582 * function. Stores information about ongoing blacklist checks.
584 struct BlackListCheckContext
588 * We keep blacklist checks in a DLL.
590 struct BlackListCheckContext *next;
593 * We keep blacklist checks in a DLL.
595 struct BlackListCheckContext *prev;
598 * Address that is being checked.
600 struct NeighbourAddress na;
603 * Handle to the ongoing blacklist check.
605 struct GST_BlacklistCheck *bc;
610 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
612 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
615 * We keep blacklist checks in a DLL so that we can find
616 * the 'sessions' in their 'struct NeighbourAddress' if
617 * a session goes down.
619 static struct BlackListCheckContext *bc_head;
622 * We keep blacklist checks in a DLL.
624 static struct BlackListCheckContext *bc_tail;
627 * Closure for #connect_notify_cb, #disconnect_notify_cb and #address_change_cb
629 static void *callback_cls;
632 * Function to call when we connected to a neighbour.
634 static NotifyConnect connect_notify_cb;
637 * Function to call when we disconnected from a neighbour.
639 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
642 * Function to call when we changed an active address of a neighbour.
644 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
647 * counter for connected neighbours
649 static unsigned int neighbours_connected;
652 * Number of bytes we have currently queued for transmission.
654 static unsigned long long bytes_in_send_queue;
657 * Task transmitting utilization data
659 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
663 * Lookup a neighbour entry in the neighbours hash map.
665 * @param pid identity of the peer to look up
666 * @return the entry, NULL if there is no existing record
668 static struct NeighbourMapEntry *
669 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
671 if (NULL == neighbours)
673 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
678 * Convert state to human-readable string.
680 * @param state the state value
681 * @return corresponding string
684 print_state (enum State state)
688 case S_NOT_CONNECTED:
689 return "S_NOT_CONNECTED";
692 case S_INIT_BLACKLIST:
693 return "S_INIT_BLACKLIST";
695 return "S_CONNECT_SENT";
696 case S_CONNECT_RECV_BLACKLIST_INBOUND:
697 return "S_CONNECT_RECV_BLACKLIST_INBOUND";
698 case S_CONNECT_RECV_ATS:
699 return "S_CONNECT_RECV_ATS";
700 case S_CONNECT_RECV_BLACKLIST:
701 return "S_CONNECT_RECV_BLACKLIST";
702 case S_CONNECT_RECV_ACK:
703 return "S_CONNECT_RECV_ACK";
705 return "S_CONNECTED";
706 case S_RECONNECT_ATS:
707 return "S_RECONNECT_ATS";
708 case S_RECONNECT_BLACKLIST:
709 return "S_RECONNECT_BLACKLIST";
710 case S_RECONNECT_SENT:
711 return "S_RECONNECT_SENT";
712 case S_CONNECTED_SWITCHING_BLACKLIST:
713 return "S_CONNECTED_SWITCHING_BLACKLIST";
714 case S_CONNECTED_SWITCHING_CONNECT_SENT:
715 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
717 return "S_DISCONNECT";
718 case S_DISCONNECT_FINISHED:
719 return "S_DISCONNECT_FINISHED";
727 * Test if we're connected to the given peer.
729 * @param n neighbour entry of peer to test
730 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
733 test_connected (struct NeighbourMapEntry *n)
739 case S_NOT_CONNECTED:
741 case S_INIT_BLACKLIST:
743 case S_CONNECT_RECV_BLACKLIST_INBOUND:
744 case S_CONNECT_RECV_ATS:
745 case S_CONNECT_RECV_BLACKLIST:
746 case S_CONNECT_RECV_ACK:
749 case S_RECONNECT_ATS:
750 case S_RECONNECT_BLACKLIST:
751 case S_RECONNECT_SENT:
752 case S_CONNECTED_SWITCHING_BLACKLIST:
753 case S_CONNECTED_SWITCHING_CONNECT_SENT:
756 case S_DISCONNECT_FINISHED:
759 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
760 "Unhandled state `%s' \n",
761 print_state (n->state));
765 return GNUNET_SYSERR;
769 * Send information about a new outbound quota to our clients.
771 * @param target affected peer
772 * @param quota new quota
775 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
776 struct GNUNET_BANDWIDTH_Value32NBO quota)
778 struct QuotaSetMessage q_msg;
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
781 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
782 ntohl (quota.value__), GNUNET_i2s (target));
783 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
784 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
786 q_msg.peer = (*target);
787 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
792 * We don't need a given neighbour address any more.
793 * Release its resources and give appropriate notifications
794 * to ATS and other subsystems.
796 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
799 free_address (struct NeighbourAddress *na)
801 if (GNUNET_YES == na->ats_active)
803 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
804 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
805 address_change_cb (callback_cls, &na->address->peer, NULL);
808 na->ats_active = GNUNET_NO;
809 na->keep_alive_nonce = 0;
810 if (NULL != na->address)
812 GNUNET_HELLO_address_free (na->address);
820 * Initialize the 'struct NeighbourAddress'.
822 * @param na neighbour address to initialize
823 * @param address address of the other peer, NULL if other peer
825 * @param session session to use (or NULL, in which case an
826 * address must be setup)
827 * @param bandwidth_in inbound quota to be used when connection is up
828 * @param bandwidth_out outbound quota to be used when connection is up
829 * @param is_active #GNUNET_YES to mark this as the active address with ATS
832 set_address (struct NeighbourAddress *na,
833 const struct GNUNET_HELLO_Address *address,
834 struct Session *session,
835 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
836 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
839 struct GNUNET_TRANSPORT_PluginFunctions *papi;
841 if (NULL == (papi = GST_plugins_find (address->transport_name)))
846 if (session == na->session)
848 na->bandwidth_in = bandwidth_in;
849 na->bandwidth_out = bandwidth_out;
850 if (is_active != na->ats_active)
852 na->ats_active = is_active;
853 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
854 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
856 address_change_cb (callback_cls, &address->peer, address);
858 if (GNUNET_YES == is_active)
860 /* FIXME: is this the right place to set quotas? */
861 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
862 send_outbound_quota (&address->peer, bandwidth_out);
868 session = papi->get_session (papi->cls, address);
871 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
872 "Failed to obtain new session for peer `%s' and address '%s'\n",
873 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
874 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
877 na->address = GNUNET_HELLO_address_copy (address);
878 na->bandwidth_in = bandwidth_in;
879 na->bandwidth_out = bandwidth_out;
880 na->session = session;
881 na->ats_active = is_active;
882 na->keep_alive_nonce = 0;
883 if (GNUNET_YES == is_active)
885 /* Telling ATS about new session */
886 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
887 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
888 address_change_cb (callback_cls, &address->peer, address);
889 /* FIXME: is this the right place to set quotas? */
890 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
891 send_outbound_quota (&address->peer, bandwidth_out);
897 * Free a neighbour map entry.
899 * @param n entry to free
900 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
901 * #GNUNET_YES to keep all sessions
904 free_neighbour (struct NeighbourMapEntry *n,
907 struct MessageQueue *mq;
908 struct GNUNET_TRANSPORT_PluginFunctions *papi;
909 struct GNUNET_HELLO_Address *backup_primary;
911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
912 "Freeing neighbur state of peer `%s'\n",
913 GNUNET_i2s (&n->id));
914 n->is_active = NULL; /* always free'd by its own continuation! */
916 /* fail messages currently in the queue */
917 while (NULL != (mq = n->messages_head))
919 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
920 if (NULL != mq->cont)
921 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
924 /* It is too late to send other peer disconnect notifications, but at
925 least internally we need to get clean... */
926 if (GNUNET_YES == test_connected (n))
928 GNUNET_STATISTICS_set (GST_stats,
929 gettext_noop ("# peers connected"),
930 --neighbours_connected,
932 disconnect_notify_cb (callback_cls, &n->id);
934 n->state = S_DISCONNECT_FINISHED;
936 if (NULL != n->primary_address.address)
938 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
941 backup_primary = NULL;
943 /* free addresses and mark as unused */
944 free_address (&n->primary_address);
945 free_address (&n->alternative_address);
947 /* FIXME: Note that if we are switching between two TCP sessions to
948 the same peer, we might want to selectively kill only one of
949 them! Killing all sessions like this seems to be very, very
952 /* cut transport-level connection */
953 if ((GNUNET_NO == keep_sessions) &&
954 (NULL != backup_primary) &&
955 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
956 papi->disconnect_peer (papi->cls, &n->id);
958 GNUNET_free_non_null (backup_primary);
960 GNUNET_assert (GNUNET_YES ==
961 GNUNET_CONTAINER_multipeermap_remove (neighbours,
964 // FIXME-ATS-API: we might want to be more specific about
965 // which states we do this from in the future (ATS should
966 // have given us a 'suggest_address' handle, and if we have
967 // such a handle, we should cancel the operation here!
968 if (NULL != n->suggest_handle)
970 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
971 n->suggest_handle = NULL;
974 if (GNUNET_SCHEDULER_NO_TASK != n->task)
976 GNUNET_SCHEDULER_cancel (n->task);
977 n->task = GNUNET_SCHEDULER_NO_TASK;
979 /* free rest of memory */
985 * Transmit a message using the current session of the given
988 * @param n entry for the recipient
989 * @param msgbuf buffer to transmit
990 * @param msgbuf_size number of bytes in @a msgbuf buffer
991 * @param priority transmission priority
992 * @param timeout transmission timeout
993 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
994 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
995 * @param cont continuation to call when finished (can be NULL)
996 * @param cont_cls closure for @a cont
997 * @return timeout (copy of @a timeout or a calculated one if
998 * @a use_keepalive_timeout is #GNUNET_YES.
1000 static struct GNUNET_TIME_Relative
1001 send_with_session (struct NeighbourMapEntry *n,
1002 const char *msgbuf, size_t msgbuf_size,
1004 struct GNUNET_TIME_Relative timeout,
1005 unsigned int use_keepalive_timeout,
1006 GNUNET_TRANSPORT_TransmitContinuation cont,
1009 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1010 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
1012 GNUNET_assert (n->primary_address.session != NULL);
1013 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1014 (-1 == papi->send (papi->cls,
1015 n->primary_address.session,
1016 msgbuf, msgbuf_size,
1018 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
1019 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1020 papi->query_keepalive_factor (papi->cls))),
1021 cont, cont_cls)))) &&
1023 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1024 GST_neighbours_notify_data_sent (&n->id,
1025 n->primary_address.address, n->primary_address.session, msgbuf_size);
1026 GNUNET_break (NULL != papi);
1032 * Master task run for every neighbour. Performs all of the time-related
1033 * activities (keep alive, send next message, disconnect if idle, finish
1034 * clean up after disconnect).
1036 * @param cls the `struct NeighbourMapEntry` for which we are running
1037 * @param tc scheduler context (unused)
1040 master_task (void *cls,
1041 const struct GNUNET_SCHEDULER_TaskContext *tc);
1045 * Function called when the 'DISCONNECT' message has been sent by the
1046 * plugin. Frees the neighbour --- if the entry still exists.
1049 * @param target identity of the neighbour that was disconnected
1050 * @param result #GNUNET_OK if the disconnect got out successfully
1051 * @param payload bytes payload
1052 * @param physical bytes physical
1055 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1056 int result, size_t payload, size_t physical)
1058 struct NeighbourMapEntry *n;
1060 n = lookup_neighbour (target);
1062 return; /* already gone */
1063 if (S_DISCONNECT != n->state)
1064 return; /* have created a fresh entry since */
1065 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1066 GNUNET_SCHEDULER_cancel (n->task);
1067 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1072 * Transmit a DISCONNECT message to the other peer.
1074 * @param n neighbour to send DISCONNECT message.
1077 send_disconnect (struct NeighbourMapEntry *n)
1079 struct SessionDisconnectMessage disconnect_msg;
1081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1082 "Sending DISCONNECT message to peer `%4s'\n",
1083 GNUNET_i2s (&n->id));
1084 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1085 disconnect_msg.header.type =
1086 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1087 disconnect_msg.reserved = htonl (0);
1088 disconnect_msg.purpose.size =
1089 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1090 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1091 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1092 disconnect_msg.purpose.purpose =
1093 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1094 disconnect_msg.timestamp =
1095 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1096 disconnect_msg.public_key = GST_my_identity.public_key;
1097 GNUNET_assert (GNUNET_OK ==
1098 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1099 &disconnect_msg.purpose,
1100 &disconnect_msg.signature));
1102 (void) send_with_session (n,
1103 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1104 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1105 GNUNET_NO, &send_disconnect_cont, NULL);
1106 GNUNET_STATISTICS_update (GST_stats,
1108 ("# DISCONNECT messages sent"), 1,
1114 * Disconnect from the given neighbour, clean up the record.
1116 * @param n neighbour to disconnect from
1119 disconnect_neighbour (struct NeighbourMapEntry *n)
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "Disconnecting from peer %s in state %s\n",
1123 GNUNET_i2s (&n->id),
1124 print_state (n->state));
1125 /* depending on state, notify neighbour and/or upper layers of this peer
1129 case S_NOT_CONNECTED:
1131 case S_INIT_BLACKLIST:
1132 /* other peer is completely unaware of us, no need to send DISCONNECT */
1133 n->state = S_DISCONNECT_FINISHED;
1134 free_neighbour (n, GNUNET_NO);
1136 case S_CONNECT_SENT:
1137 send_disconnect (n);
1138 n->state = S_DISCONNECT;
1140 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1141 case S_CONNECT_RECV_ATS:
1142 case S_CONNECT_RECV_BLACKLIST:
1143 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1144 n->state = S_DISCONNECT_FINISHED;
1145 free_neighbour (n, GNUNET_NO);
1147 case S_CONNECT_RECV_ACK:
1148 /* we DID ACK the other peer's request, must send DISCONNECT */
1149 send_disconnect (n);
1150 n->state = S_DISCONNECT;
1153 case S_RECONNECT_BLACKLIST:
1154 case S_RECONNECT_SENT:
1155 case S_CONNECTED_SWITCHING_BLACKLIST:
1156 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1157 /* we are currently connected, need to send disconnect and do
1158 internal notifications and update statistics */
1159 send_disconnect (n);
1160 GNUNET_STATISTICS_set (GST_stats,
1161 gettext_noop ("# peers connected"),
1162 --neighbours_connected,
1164 disconnect_notify_cb (callback_cls, &n->id);
1165 n->state = S_DISCONNECT;
1167 case S_RECONNECT_ATS:
1168 /* ATS address request timeout, disconnect without sending disconnect message */
1169 GNUNET_STATISTICS_set (GST_stats,
1170 gettext_noop ("# peers connected"),
1171 --neighbours_connected,
1173 disconnect_notify_cb (callback_cls, &n->id);
1174 n->state = S_DISCONNECT;
1177 /* already disconnected, ignore */
1179 case S_DISCONNECT_FINISHED:
1180 /* already cleaned up, how did we get here!? */
1184 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1185 "Unhandled state `%s'\n",
1186 print_state (n->state));
1190 /* schedule timeout to clean up */
1191 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1192 GNUNET_SCHEDULER_cancel (n->task);
1193 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1199 * We're done with our transmission attempt, continue processing.
1201 * @param cls the `struct MessageQueue` of the message
1202 * @param receiver intended receiver
1203 * @param success whether it worked or not
1204 * @param size_payload bytes payload sent
1205 * @param physical bytes sent on wire
1208 transmit_send_continuation (void *cls,
1209 const struct GNUNET_PeerIdentity *receiver,
1210 int success, size_t size_payload, size_t physical)
1212 struct MessageQueue *mq = cls;
1213 struct NeighbourMapEntry *n;
1215 if (NULL == (n = lookup_neighbour (receiver)))
1218 return; /* disconnect or other error while transmitting, can happen */
1220 if (n->is_active == mq)
1222 /* this is still "our" neighbour, remove us from its queue
1223 and allow it to send the next message now */
1224 n->is_active = NULL;
1225 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1226 GNUNET_SCHEDULER_cancel (n->task);
1227 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1229 if (bytes_in_send_queue < mq->message_buf_size)
1231 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1232 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1233 bytes_in_send_queue, mq->message_buf_size,
1234 (GNUNET_OK == success) ? "OK" : "FAIL",
1235 size_payload, physical);
1240 GNUNET_break (size_payload == mq->message_buf_size);
1241 bytes_in_send_queue -= mq->message_buf_size;
1242 GNUNET_STATISTICS_set (GST_stats,
1244 ("# bytes in message queue for other peers"),
1245 bytes_in_send_queue, GNUNET_NO);
1246 if (GNUNET_OK == success)
1247 GNUNET_STATISTICS_update (GST_stats,
1249 ("# messages transmitted to other peers"),
1252 GNUNET_STATISTICS_update (GST_stats,
1254 ("# transmission failures for messages to other peers"),
1256 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1257 "Sending message to `%s' of type %u was a %s\n",
1258 GNUNET_i2s (receiver),
1259 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1260 (success == GNUNET_OK) ? "success" : "FAILURE");
1261 if (NULL != mq->cont)
1262 mq->cont (mq->cont_cls, success, size_payload, physical);
1268 * Check the message list for the given neighbour and if we can
1269 * send a message, do so. This function should only be called
1270 * if the connection is at least generally ready for transmission.
1271 * While we will only send one message at a time, no bandwidth
1272 * quota management is performed here. If a message was given to
1273 * the plugin, the continuation will automatically re-schedule
1274 * the 'master' task once the next message might be transmitted.
1276 * @param n target peer for which to transmit
1279 try_transmission_to_peer (struct NeighbourMapEntry *n)
1281 struct MessageQueue *mq;
1282 struct GNUNET_TIME_Relative timeout;
1284 if (NULL == n->primary_address.address)
1286 /* no address, why are we here? */
1290 if ((0 == n->primary_address.address->address_length) &&
1291 (NULL == n->primary_address.session))
1293 /* no address, why are we here? */
1297 if (NULL != n->is_active)
1299 /* transmission already pending */
1303 /* timeout messages from the queue that are past their due date */
1304 while (NULL != (mq = n->messages_head))
1306 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1307 if (timeout.rel_value_us > 0)
1309 GNUNET_STATISTICS_update (GST_stats,
1311 ("# messages timed out while in transport queue"),
1313 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1315 transmit_send_continuation (mq, &n->id,
1317 mq->message_buf_size, 0); /* timeout */
1320 return; /* no more messages */
1321 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1323 (void) send_with_session (n,
1324 mq->message_buf, mq->message_buf_size,
1325 0 /* priority */, timeout, GNUNET_NO,
1326 &transmit_send_continuation, mq);
1331 * Send keepalive message to the neighbour. Must only be called
1332 * if we are on 'connected' state or while trying to switch addresses.
1333 * Will internally determine if a keepalive is truly needed (so can
1334 * always be called).
1336 * @param n neighbour that went idle and needs a keepalive
1339 send_keepalive (struct NeighbourMapEntry *n)
1341 struct SessionKeepAliveMessage m;
1342 struct GNUNET_TIME_Relative timeout;
1345 GNUNET_assert ((S_CONNECTED == n->state) ||
1346 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1347 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1348 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1349 return; /* no keepalive needed at this time */
1351 nonce = 0; /* 0 indicates 'not set' */
1353 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1356 "Sending keep alive to peer `%s' with nonce %u\n",
1357 GNUNET_i2s (&n->id), nonce);
1359 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1360 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1361 m.nonce = htonl (nonce);
1363 timeout = send_with_session (n,
1364 (const void *) &m, sizeof (m),
1365 UINT32_MAX /* priority */,
1366 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1368 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1370 n->primary_address.keep_alive_nonce = nonce;
1371 n->expect_latency_response = GNUNET_YES;
1372 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1373 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1379 * Keep the connection to the given neighbour alive longer,
1380 * we received a KEEPALIVE (or equivalent); send a response.
1382 * @param neighbour neighbour to keep alive (by sending keep alive response)
1383 * @param m the keep alive message containing the nonce to respond to
1386 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1387 const struct GNUNET_MessageHeader *m)
1389 struct NeighbourMapEntry *n;
1390 const struct SessionKeepAliveMessage *msg_in;
1391 struct SessionKeepAliveMessage msg;
1393 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1396 msg_in = (struct SessionKeepAliveMessage *) m;
1397 if (NULL == (n = lookup_neighbour (neighbour)))
1399 GNUNET_STATISTICS_update (GST_stats,
1401 ("# KEEPALIVE messages discarded (peer unknown)"),
1405 if (NULL == n->primary_address.session)
1407 GNUNET_STATISTICS_update (GST_stats,
1409 ("# KEEPALIVE messages discarded (no session)"),
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1415 "Received keep alive request from peer `%s' with nonce %u\n",
1416 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1418 /* send reply to allow neighbour to measure latency */
1419 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1420 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1421 msg.nonce = msg_in->nonce;
1422 (void) send_with_session(n,
1423 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1424 UINT32_MAX /* priority */,
1425 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1431 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1432 * latency to this peer. Pass the updated information (existing ats
1433 * plus calculated latency) to ATS.
1435 * @param neighbour neighbour to keep alive
1436 * @param m the message containing the keep alive response
1439 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1440 const struct GNUNET_MessageHeader *m)
1442 struct NeighbourMapEntry *n;
1443 const struct SessionKeepAliveMessage *msg;
1444 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1446 struct GNUNET_ATS_Information ats;
1448 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1451 msg = (const struct SessionKeepAliveMessage *) m;
1452 if (NULL == (n = lookup_neighbour (neighbour)))
1454 GNUNET_STATISTICS_update (GST_stats,
1456 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1460 if ( (S_CONNECTED != n->state) ||
1461 (GNUNET_YES != n->expect_latency_response) )
1463 GNUNET_STATISTICS_update (GST_stats,
1465 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1469 if (NULL == n->primary_address.address)
1471 GNUNET_STATISTICS_update (GST_stats,
1473 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1477 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1479 GNUNET_STATISTICS_update (GST_stats,
1481 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1488 "Received keep alive response from peer `%s' for session %p\n",
1489 GNUNET_i2s (&n->id), n->primary_address.session);
1493 /* Update session timeout here */
1494 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1496 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1497 "Updating session for peer `%s' for session %p\n",
1498 GNUNET_i2s (&n->id), n->primary_address.session);
1499 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1506 n->primary_address.keep_alive_nonce = 0;
1507 n->expect_latency_response = GNUNET_NO;
1508 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1509 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "Latency for peer `%s' is %s\n",
1512 GNUNET_i2s (&n->id),
1513 GNUNET_STRINGS_relative_time_to_string (n->latency,
1515 /* append latency */
1516 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1517 if (n->latency.rel_value_us > UINT32_MAX)
1518 latency = UINT32_MAX;
1520 latency = n->latency.rel_value_us;
1521 ats.value = htonl (latency);
1522 GST_ats_update_metrics (&n->id, n->primary_address.address,
1523 n->primary_address.session, &ats, 1);
1528 * We have received a message from the given sender. How long should
1529 * we delay before receiving more? (Also used to keep the peer marked
1532 * @param sender sender of the message
1533 * @param size size of the message
1534 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1535 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1536 * #GNUNET_SYSERR if the connection is not fully up yet
1537 * @return how long to wait before reading more from this sender
1539 struct GNUNET_TIME_Relative
1540 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1541 *sender, ssize_t size, int *do_forward)
1543 struct NeighbourMapEntry *n;
1544 struct GNUNET_TIME_Relative ret;
1546 if (NULL == neighbours)
1548 *do_forward = GNUNET_NO;
1549 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1551 if (NULL == (n = lookup_neighbour (sender)))
1553 GST_neighbours_try_connect (sender);
1554 if (NULL == (n = lookup_neighbour (sender)))
1556 GNUNET_STATISTICS_update (GST_stats,
1558 ("# messages discarded due to lack of neighbour record"),
1560 *do_forward = GNUNET_NO;
1561 return GNUNET_TIME_UNIT_ZERO;
1564 if (! test_connected (n))
1566 *do_forward = GNUNET_SYSERR;
1567 return GNUNET_TIME_UNIT_ZERO;
1569 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1571 n->quota_violation_count++;
1572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1573 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1574 n->in_tracker.available_bytes_per_s__,
1575 n->quota_violation_count);
1576 /* Discount 32k per violation */
1577 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1581 if (n->quota_violation_count > 0)
1583 /* try to add 32k back */
1584 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1585 n->quota_violation_count--;
1588 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1590 GNUNET_STATISTICS_update (GST_stats,
1592 ("# bandwidth quota violations by other peers"),
1594 *do_forward = GNUNET_NO;
1595 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1597 *do_forward = GNUNET_YES;
1598 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1599 if (ret.rel_value_us > 0)
1601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1602 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1603 (unsigned long long) n->in_tracker.
1604 consumption_since_last_update__,
1605 (unsigned int) n->in_tracker.available_bytes_per_s__,
1606 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1607 GNUNET_STATISTICS_update (GST_stats,
1608 gettext_noop ("# ms throttling suggested"),
1609 (int64_t) ret.rel_value_us / 1000LL,
1617 * Transmit a message to the given target using the active connection.
1619 * @param target destination
1620 * @param msg message to send
1621 * @param msg_size number of bytes in msg
1622 * @param timeout when to fail with timeout
1623 * @param cont function to call when done
1624 * @param cont_cls closure for 'cont'
1627 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1628 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1629 GST_NeighbourSendContinuation cont, void *cont_cls)
1631 struct NeighbourMapEntry *n;
1632 struct MessageQueue *mq;
1634 /* All ove these cases should never happen; they are all API violations.
1635 But we check anyway, just to be sure. */
1636 if (NULL == (n = lookup_neighbour (target)))
1640 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1643 if (GNUNET_YES != test_connected (n))
1647 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1650 bytes_in_send_queue += msg_size;
1651 GNUNET_STATISTICS_set (GST_stats,
1653 ("# bytes in message queue for other peers"),
1654 bytes_in_send_queue, GNUNET_NO);
1655 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1657 mq->cont_cls = cont_cls;
1658 memcpy (&mq[1], msg, msg_size);
1659 mq->message_buf = (const char *) &mq[1];
1660 mq->message_buf_size = msg_size;
1661 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1662 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1663 if ( (NULL != n->is_active) ||
1664 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1666 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1667 GNUNET_SCHEDULER_cancel (n->task);
1668 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1673 * Send a SESSION_CONNECT message via the given address.
1675 * @param na address to use
1678 send_session_connect (struct NeighbourAddress *na)
1680 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1681 struct SessionConnectMessage connect_msg;
1683 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1684 "Sending SESSION_CONNECT message to peer %s\n",
1685 GNUNET_i2s (&na->address->peer));
1686 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1691 if (NULL == na->session)
1692 na->session = papi->get_session (papi->cls, na->address);
1693 if (NULL == na->session)
1698 GNUNET_STATISTICS_update (GST_stats,
1700 ("# SESSION_CONNECT messages sent"),
1702 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1703 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1704 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1705 connect_msg.reserved = htonl (0);
1706 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1708 papi->send (papi->cls,
1710 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1712 GNUNET_TIME_UNIT_FOREVER_REL,
1715 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1716 _("Failed to transmit CONNECT message via plugin to %s\n"),
1717 GST_plugins_a2s (na->address));
1719 GST_neighbours_notify_data_sent (&na->address->peer,
1722 sizeof (struct SessionConnectMessage));
1728 * Send a SESSION_CONNECT_ACK message via the given address.
1730 * @param address address to use
1731 * @param session session to use
1732 * @param timestamp timestamp to use for the ACK message
1735 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1736 struct Session *session,
1737 struct GNUNET_TIME_Absolute timestamp)
1739 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1740 struct SessionConnectMessage connect_msg;
1742 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1743 "Sending CONNECT_ACK to peer `%s'\n",
1744 GNUNET_i2s (&address->peer));
1745 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1750 if (NULL == session)
1751 session = papi->get_session (papi->cls, address);
1752 if (NULL == session)
1757 GNUNET_STATISTICS_update (GST_stats,
1759 ("# CONNECT_ACK messages sent"),
1761 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1762 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1763 connect_msg.reserved = htonl (0);
1764 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1765 (void) papi->send (papi->cls,
1767 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1769 GNUNET_TIME_UNIT_FOREVER_REL,
1776 * Create a fresh entry in the neighbour map for the given peer
1778 * @param peer peer to create an entry for
1779 * @return new neighbour map entry
1781 static struct NeighbourMapEntry *
1782 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1784 struct NeighbourMapEntry *n;
1786 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1787 "Creating new neighbour entry for `%s'\n",
1789 n = GNUNET_new (struct NeighbourMapEntry);
1791 n->state = S_NOT_CONNECTED;
1792 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1793 n->last_util_transmission = GNUNET_TIME_absolute_get();
1794 n->util_payload_bytes_recv = 0;
1795 n->util_payload_bytes_sent = 0;
1796 n->util_total_bytes_recv = 0;
1797 n->util_total_bytes_sent = 0;
1798 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1799 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1800 MAX_BANDWIDTH_CARRY_S);
1801 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1802 GNUNET_assert (GNUNET_OK ==
1803 GNUNET_CONTAINER_multipeermap_put (neighbours,
1805 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1811 * Check if the two given addresses are the same.
1812 * Actually only checks if the sessions are non-NULL
1813 * (which they should be) and then if they are identical;
1814 * the actual addresses don't matter if the session
1815 * pointers match anyway, and we must have session pointers
1818 * @param a1 first address to compare
1819 * @param a2 other address to compare
1820 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1823 address_matches (const struct NeighbourAddress *a1,
1824 const struct NeighbourAddress *a2)
1826 if ( (NULL == a1->session) ||
1827 (NULL == a2->session) )
1832 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1837 * Try to create a connection to the given target (eventually).
1839 * @param target peer to try to connect to
1842 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1844 struct NeighbourMapEntry *n;
1846 if (NULL == neighbours)
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1849 "Asked to connect to peer `%s' during shutdown\n",
1850 GNUNET_i2s (target));
1851 return; /* during shutdown, do nothing */
1853 n = lookup_neighbour (target);
1854 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1855 "Asked to connect to peer `%s' (state: %s)\n",
1856 GNUNET_i2s (target),
1857 (NULL != n) ? print_state(n->state) : "NEW PEER");
1862 case S_NOT_CONNECTED:
1863 /* this should not be possible */
1865 free_neighbour (n, GNUNET_NO);
1868 case S_INIT_BLACKLIST:
1869 case S_CONNECT_SENT:
1870 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1871 case S_CONNECT_RECV_ATS:
1872 case S_CONNECT_RECV_BLACKLIST:
1873 case S_CONNECT_RECV_ACK:
1874 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1875 "Ignoring request to try to connect to `%s', already trying!\n",
1876 GNUNET_i2s (target));
1877 return; /* already trying */
1879 case S_RECONNECT_ATS:
1880 case S_RECONNECT_BLACKLIST:
1881 case S_RECONNECT_SENT:
1882 case S_CONNECTED_SWITCHING_BLACKLIST:
1883 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1884 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1885 "Ignoring request to try to connect, already connected to `%s'!\n",
1886 GNUNET_i2s (target));
1887 return; /* already connected */
1889 /* get rid of remains, ready to re-try immediately */
1890 free_neighbour (n, GNUNET_NO);
1892 case S_DISCONNECT_FINISHED:
1893 /* should not be possible */
1896 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1897 "Unhandled state `%s'\n",
1898 print_state (n->state));
1900 free_neighbour (n, GNUNET_NO);
1904 n = setup_neighbour (target);
1905 n->state = S_INIT_ATS;
1906 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1908 GNUNET_ATS_reset_backoff (GST_ats, target);
1909 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1914 * Function called with the result of a blacklist check.
1916 * @param cls closure with the `struct BlackListCheckContext`
1917 * @param peer peer this check affects
1918 * @param result #GNUNET_OK if the address is allowed
1921 handle_test_blacklist_cont (void *cls,
1922 const struct GNUNET_PeerIdentity *peer,
1925 struct BlackListCheckContext *bcc = cls;
1926 struct NeighbourMapEntry *n;
1929 GNUNET_CONTAINER_DLL_remove (bc_head,
1932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1933 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1935 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1936 if (GNUNET_OK == result)
1938 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
1942 /* Blacklist disagreed on connecting to a peer with this address
1943 * Destroy address because we are not allowed to use it
1945 if (NULL != bcc->na.session)
1946 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1947 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1949 if (NULL == (n = lookup_neighbour (peer)))
1951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1952 "No neighbor entry for peer `%s', ignoring blacklist result\n",
1954 goto cleanup; /* nobody left to care about new address */
1956 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1957 "Received blacklist result for peer `%s' in state %s/%d\n",
1959 print_state (n->state),
1960 n->send_connect_ack);
1963 case S_NOT_CONNECTED:
1964 /* this should not be possible */
1966 free_neighbour (n, GNUNET_NO);
1969 /* waiting on ATS suggestion; still, pass address to ATS as a
1972 case S_INIT_BLACKLIST:
1973 /* check if the address the blacklist was fine with matches
1974 ATS suggestion, if so, we can move on! */
1975 if ( (GNUNET_OK == result) &&
1976 (1 == n->send_connect_ack) )
1978 n->send_connect_ack = 2;
1979 send_session_connect_ack_message (bcc->na.address,
1981 n->connect_ack_timestamp);
1983 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1985 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1986 "Blacklist result for peer %s is for non-primary address, ignored\n",
1988 break; /* result for an address we currently don't care about */
1990 if (GNUNET_OK == result)
1992 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1993 n->state = S_CONNECT_SENT;
1994 send_session_connect (&n->primary_address);
1998 free_address (&n->primary_address);
1999 n->state = S_INIT_ATS;
2000 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2003 case S_CONNECT_SENT:
2004 /* waiting on CONNECT_ACK, send ACK if one is pending */
2005 if ( (GNUNET_OK == result) &&
2006 (1 == n->send_connect_ack) )
2008 n->send_connect_ack = 2;
2009 send_session_connect_ack_message (n->primary_address.address,
2010 n->primary_address.session,
2011 n->connect_ack_timestamp);
2014 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2015 n->state = S_CONNECT_RECV_ATS;
2016 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2017 GNUNET_ATS_reset_backoff (GST_ats, peer);
2018 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2019 "Suggesting address for peer %s to ATS\n",
2021 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2023 case S_CONNECT_RECV_ATS:
2024 /* waiting on ATS suggestion, don't care about blacklist */
2026 case S_CONNECT_RECV_BLACKLIST:
2027 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2029 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2030 "Blacklist result ignored, as it is not for our primary address\n");
2031 break; /* result for an address we currently don't care about */
2033 if (GNUNET_OK == result)
2035 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
2036 n->state = S_CONNECT_RECV_ACK;
2037 send_session_connect_ack_message (bcc->na.address,
2039 n->connect_ack_timestamp);
2040 if (1 == n->send_connect_ack)
2041 n->send_connect_ack = 2;
2045 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2047 plugin = GST_plugins_find (bcc->na.address->transport_name);
2048 if ( (NULL != plugin) &&
2049 (NULL != bcc->na.session) )
2051 plugin->disconnect_session (plugin->cls,
2055 GNUNET_break (NULL != plugin);
2056 free_address (&n->primary_address);
2057 n->state = S_INIT_ATS;
2058 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2059 GNUNET_ATS_reset_backoff (GST_ats, peer);
2062 case S_CONNECT_RECV_ACK:
2063 /* waiting on SESSION_ACK, send ACK if one is pending */
2064 if ( (GNUNET_OK == result) &&
2065 (1 == n->send_connect_ack) )
2067 n->send_connect_ack = 2;
2068 send_session_connect_ack_message (n->primary_address.address,
2069 n->primary_address.session,
2070 n->connect_ack_timestamp);
2074 /* already connected, don't care about blacklist */
2076 case S_RECONNECT_ATS:
2077 /* still waiting on ATS suggestion, don't care about blacklist */
2079 case S_RECONNECT_BLACKLIST:
2080 if ( (GNUNET_OK == result) &&
2081 (1 == n->send_connect_ack) )
2083 n->send_connect_ack = 2;
2084 send_session_connect_ack_message (bcc->na.address,
2086 n->connect_ack_timestamp);
2088 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2090 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2091 "Blacklist result ignored, as it is not for our primary address\n");
2092 break; /* result for an address we currently don't care about */
2094 if (GNUNET_OK == result)
2096 n->state = S_RECONNECT_SENT;
2097 send_session_connect (&n->primary_address);
2098 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2102 n->state = S_RECONNECT_ATS;
2103 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2106 case S_RECONNECT_SENT:
2107 /* waiting on CONNECT_ACK, don't care about blacklist */
2108 if ( (GNUNET_OK == result) &&
2109 (1 == n->send_connect_ack) )
2111 n->send_connect_ack = 2;
2112 send_session_connect_ack_message (n->primary_address.address,
2113 n->primary_address.session,
2114 n->connect_ack_timestamp);
2117 case S_CONNECTED_SWITCHING_BLACKLIST:
2118 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2121 "Blacklist result ignored, as it is not for our primary address\n");
2122 break; /* result for an address we currently don't care about */
2124 if (GNUNET_OK == result)
2126 send_session_connect (&n->alternative_address);
2127 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
2131 n->state = S_CONNECTED;
2132 free_address (&n->alternative_address);
2135 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2136 /* waiting on CONNECT_ACK, don't care about blacklist */
2137 if ( (GNUNET_OK == result) &&
2138 (1 == n->send_connect_ack) )
2140 n->send_connect_ack = 2;
2141 send_session_connect_ack_message (n->primary_address.address,
2142 n->primary_address.session,
2143 n->connect_ack_timestamp);
2147 /* Nothing to do here, ATS will already do what can be done */
2149 case S_DISCONNECT_FINISHED:
2150 /* should not be possible */
2154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2155 "Unhandled state `%s'\n",
2156 print_state (n->state));
2158 free_neighbour (n, GNUNET_NO);
2162 GNUNET_HELLO_address_free (bcc->na.address);
2168 * We want to know if connecting to a particular peer via
2169 * a particular address is allowed. Check it!
2171 * @param peer identity of the peer to switch the address for
2172 * @param ts time at which the check was initiated
2173 * @param address address of the other peer, NULL if other peer
2175 * @param session session to use (or NULL)
2178 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2179 struct GNUNET_TIME_Absolute ts,
2180 const struct GNUNET_HELLO_Address *address,
2181 struct Session *session)
2183 struct BlackListCheckContext *bcc;
2184 struct GST_BlacklistCheck *bc;
2186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2187 "Checking peer `%s' against blacklist\n",
2189 bcc = GNUNET_new (struct BlackListCheckContext);
2190 bcc->na.address = GNUNET_HELLO_address_copy (address);
2191 bcc->na.session = session;
2192 bcc->na.connect_timestamp = ts;
2193 GNUNET_CONTAINER_DLL_insert (bc_head,
2196 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2197 address->transport_name,
2198 &handle_test_blacklist_cont, bcc)))
2200 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2201 we must only store 'bc' if 'bc' is non-NULL... */
2206 * We received a 'SESSION_CONNECT' message from the other peer.
2207 * Consider switching to it.
2209 * @param message possibly a 'struct SessionConnectMessage' (check format)
2210 * @param peer identity of the peer to switch the address for
2211 * @param address address of the other peer, NULL if other peer
2213 * @param session session to use (or NULL)
2214 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2217 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2218 const struct GNUNET_PeerIdentity *peer,
2219 const struct GNUNET_HELLO_Address *address,
2220 struct Session *session)
2222 const struct SessionConnectMessage *scm;
2223 struct NeighbourMapEntry *n;
2224 struct GNUNET_TIME_Absolute ts;
2226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2227 "Received CONNECT message from peer `%s'\n",
2229 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2231 GNUNET_break_op (0);
2232 return GNUNET_SYSERR;
2234 GNUNET_STATISTICS_update (GST_stats,
2236 ("# CONNECT messages received"),
2238 if (NULL == neighbours)
2240 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2241 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2243 return GNUNET_OK; /* we're shutting down */
2245 scm = (const struct SessionConnectMessage *) message;
2246 GNUNET_break_op (0 == ntohl (scm->reserved));
2247 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2248 n = lookup_neighbour (peer);
2250 n = setup_neighbour (peer);
2251 n->send_connect_ack = 1;
2252 n->connect_ack_timestamp = ts;
2254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2255 "Received SESSION_CONNECT for peer `%s' in state %s/%d\n",
2257 print_state (n->state),
2258 n->send_connect_ack);
2261 case S_NOT_CONNECTED:
2262 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2263 /* Do a blacklist check for the new address */
2264 check_blacklist (peer, ts, address, session);
2267 /* CONNECT message takes priority over us asking ATS for address */
2268 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2270 case S_INIT_BLACKLIST:
2271 case S_CONNECT_SENT:
2272 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2273 case S_CONNECT_RECV_ATS:
2274 case S_CONNECT_RECV_BLACKLIST:
2275 case S_CONNECT_RECV_ACK:
2276 /* It can never hurt to have an alternative address in the above cases,
2277 see if it is allowed */
2278 check_blacklist (peer, ts, address, session);
2281 /* we are already connected and can thus send the ACK immediately;
2282 still, it can never hurt to have an alternative address, so also
2283 tell ATS about it */
2284 GNUNET_assert (NULL != n->primary_address.address);
2285 GNUNET_assert (NULL != n->primary_address.session);
2286 n->send_connect_ack = 0;
2287 send_session_connect_ack_message (n->primary_address.address,
2288 n->primary_address.session, ts);
2289 check_blacklist (peer, ts, address, session);
2291 case S_RECONNECT_ATS:
2292 case S_RECONNECT_BLACKLIST:
2293 case S_RECONNECT_SENT:
2294 /* It can never hurt to have an alternative address in the above cases,
2295 see if it is allowed */
2296 check_blacklist (peer, ts, address, session);
2298 case S_CONNECTED_SWITCHING_BLACKLIST:
2299 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2300 /* we are already connected and can thus send the ACK immediately;
2301 still, it can never hurt to have an alternative address, so also
2302 tell ATS about it */
2303 GNUNET_assert (NULL != n->primary_address.address);
2304 GNUNET_assert (NULL != n->primary_address.session);
2305 n->send_connect_ack = 0;
2306 send_session_connect_ack_message (n->primary_address.address,
2307 n->primary_address.session, ts);
2308 check_blacklist (peer, ts, address, session);
2311 /* get rid of remains without terminating sessions, ready to re-try */
2312 free_neighbour (n, GNUNET_YES);
2313 n = setup_neighbour (peer);
2314 n->state = S_CONNECT_RECV_ATS;
2315 GNUNET_ATS_reset_backoff (GST_ats, peer);
2316 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2318 case S_DISCONNECT_FINISHED:
2319 /* should not be possible */
2323 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2324 "Unhandled state `%s'\n",
2325 print_state (n->state));
2327 return GNUNET_SYSERR;
2334 * For an existing neighbour record, set the active connection to
2335 * use the given address.
2337 * @param peer identity of the peer to switch the address for
2338 * @param address address of the other peer, NULL if other peer
2340 * @param session session to use (or NULL)
2341 * @param ats performance data
2342 * @param ats_count number of entries in ats
2343 * @param bandwidth_in inbound quota to be used when connection is up,
2344 * 0 to disconnect from peer
2345 * @param bandwidth_out outbound quota to be used when connection is up,
2346 * 0 to disconnect from peer
2349 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2350 const struct GNUNET_HELLO_Address *address,
2351 struct Session *session,
2352 const struct GNUNET_ATS_Information *ats,
2354 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2355 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2357 struct NeighbourMapEntry *n;
2358 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "ATS has decided on an address for peer %s\n",
2363 GNUNET_assert (NULL != address->transport_name);
2364 if (NULL == (n = lookup_neighbour (peer)))
2366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2367 "Peer %s is unknown, suggestion ignored\n",
2372 /* Obtain an session for this address from plugin */
2373 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2375 /* we don't have the plugin for this address */
2376 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2377 "Plugin `%s' is unknown, suggestion for peer %s ignored\n",
2378 address->transport_name,
2380 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2383 if ((NULL == session) && (0 == address->address_length))
2386 if (strlen (address->transport_name) > 0)
2387 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2391 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2392 "ATS tells us to switch to address '%s/%s' session %p for "
2393 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2394 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2395 address->transport_name,
2398 print_state (n->state),
2399 n->send_connect_ack,
2400 ntohl (bandwidth_in.value__),
2401 ntohl (bandwidth_out.value__));
2403 if (NULL == session)
2405 session = papi->get_session (papi->cls, address);
2406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2407 "Obtained new session for peer `%s' and address '%s': %p\n",
2408 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2410 if (NULL == session)
2412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2413 "Failed to obtain new session for peer `%s' and address '%s'\n",
2414 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2415 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2420 case S_NOT_CONNECTED:
2422 free_neighbour (n, GNUNET_NO);
2425 set_address (&n->primary_address,
2426 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2427 n->state = S_INIT_BLACKLIST;
2428 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2429 check_blacklist (&n->id,
2430 n->connect_ack_timestamp,
2433 case S_INIT_BLACKLIST:
2434 /* ATS suggests a different address, switch again */
2435 set_address (&n->primary_address,
2436 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2437 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2438 check_blacklist (&n->id,
2439 n->connect_ack_timestamp,
2442 case S_CONNECT_SENT:
2443 /* ATS suggests a different address, switch again */
2444 set_address (&n->primary_address,
2445 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2446 n->state = S_INIT_BLACKLIST;
2447 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2448 check_blacklist (&n->id,
2449 n->connect_ack_timestamp,
2452 case S_CONNECT_RECV_ATS:
2453 set_address (&n->primary_address,
2454 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2455 n->state = S_CONNECT_RECV_BLACKLIST;
2456 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2457 check_blacklist (&n->id,
2458 n->connect_ack_timestamp,
2461 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2462 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2463 check_blacklist (&n->id,
2464 n->connect_ack_timestamp,
2467 case S_CONNECT_RECV_BLACKLIST:
2468 case S_CONNECT_RECV_ACK:
2469 /* ATS asks us to switch while we were trying to connect; switch to new
2470 address and check blacklist again */
2471 set_address (&n->primary_address,
2472 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2473 n->state = S_CONNECT_RECV_BLACKLIST;
2474 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2475 check_blacklist (&n->id,
2476 n->connect_ack_timestamp,
2480 GNUNET_assert (NULL != n->primary_address.address);
2481 GNUNET_assert (NULL != n->primary_address.session);
2482 if (n->primary_address.session == session)
2484 /* not an address change, just a quota change */
2485 set_address (&n->primary_address,
2486 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2489 /* ATS asks us to switch a life connection; see if we can get
2490 a CONNECT_ACK on it before we actually do this! */
2491 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2492 set_address (&n->alternative_address,
2493 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2494 check_blacklist (&n->id,
2495 GNUNET_TIME_absolute_get (),
2498 case S_RECONNECT_ATS:
2499 n->state = S_RECONNECT_BLACKLIST;
2500 set_address (&n->primary_address,
2501 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2502 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2503 check_blacklist (&n->id,
2504 n->connect_ack_timestamp,
2507 case S_RECONNECT_BLACKLIST:
2508 /* ATS asks us to switch while we were trying to reconnect; switch to new
2509 address and check blacklist again */
2510 set_address (&n->primary_address,
2511 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2512 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2513 check_blacklist (&n->id,
2514 n->connect_ack_timestamp,
2517 case S_RECONNECT_SENT:
2518 /* ATS asks us to switch while we were trying to reconnect; switch to new
2519 address and check blacklist again */
2520 n->state = S_RECONNECT_BLACKLIST;
2521 set_address (&n->primary_address,
2522 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2523 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2524 check_blacklist (&n->id,
2525 n->connect_ack_timestamp,
2528 case S_CONNECTED_SWITCHING_BLACKLIST:
2529 if (n->primary_address.session == session)
2531 /* ATS switches back to still-active session */
2532 n->state = S_CONNECTED;
2533 free_address (&n->alternative_address);
2536 /* ATS asks us to switch a life connection, update blacklist check */
2537 set_address (&n->alternative_address,
2538 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2539 check_blacklist (&n->id,
2540 GNUNET_TIME_absolute_get (),
2543 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2544 if (n->primary_address.session == session)
2546 /* ATS switches back to still-active session */
2547 free_address (&n->alternative_address);
2548 n->state = S_CONNECTED;
2551 /* ATS asks us to switch a life connection, update blacklist check */
2552 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2553 set_address (&n->alternative_address,
2554 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2555 check_blacklist (&n->id,
2556 GNUNET_TIME_absolute_get (),
2560 /* not going to switch addresses while disconnecting */
2562 case S_DISCONNECT_FINISHED:
2566 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2567 "Unhandled state `%s'\n",
2568 print_state (n->state));
2576 send_utilization_data (void *cls,
2577 const struct GNUNET_PeerIdentity *key,
2580 struct NeighbourMapEntry *n = value;
2581 struct GNUNET_ATS_Information atsi[4];
2583 uint32_t bps_pl_out;
2586 struct GNUNET_TIME_Relative delta;
2588 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2589 GNUNET_TIME_absolute_get ());
2593 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2594 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2596 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2597 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2599 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2604 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2605 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2607 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2608 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2612 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2616 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2617 atsi[0].value = htonl (bps_out);
2618 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2619 atsi[1].value = htonl (bps_in);
2621 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2622 atsi[2].value = htonl (bps_pl_out);
2623 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2624 atsi[3].value = htonl (bps_pl_in);
2626 GST_ats_update_metrics (key, n->primary_address.address,
2627 n->primary_address.session, atsi, 4);
2628 n->util_payload_bytes_recv = 0;
2629 n->util_payload_bytes_sent = 0;
2630 n->util_total_bytes_recv = 0;
2631 n->util_total_bytes_sent = 0;
2632 n->last_util_transmission = GNUNET_TIME_absolute_get();
2638 * Task transmitting utilization in a regular interval
2640 * @param cls the 'struct NeighbourMapEntry' for which we are running
2641 * @param tc scheduler context (unused)
2644 utilization_transmission (void *cls,
2645 const struct GNUNET_SCHEDULER_TaskContext *tc)
2647 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2649 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2650 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2652 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2653 utilization_transmission, NULL);
2659 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2660 const struct GNUNET_HELLO_Address *address,
2661 struct Session *session,
2662 const struct GNUNET_MessageHeader *message)
2664 struct NeighbourMapEntry *n;
2666 n = lookup_neighbour (peer);
2669 n->util_total_bytes_recv += ntohs(message->size);
2674 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2675 const struct GNUNET_HELLO_Address *address,
2676 struct Session *session,
2677 const struct GNUNET_MessageHeader *message)
2679 struct NeighbourMapEntry *n;
2680 n = lookup_neighbour (peer);
2683 n->util_payload_bytes_recv += ntohs(message->size);
2688 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2689 const struct GNUNET_HELLO_Address *address,
2690 struct Session *session,
2693 struct NeighbourMapEntry *n;
2694 n = lookup_neighbour (peer);
2697 if (n->primary_address.session != session)
2699 n->util_total_bytes_sent += size;
2704 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2707 struct NeighbourMapEntry *n;
2708 n = lookup_neighbour (peer);
2711 n->util_payload_bytes_sent += size;
2716 * Master task run for every neighbour. Performs all of the time-related
2717 * activities (keep alive, send next message, disconnect if idle, finish
2718 * clean up after disconnect).
2720 * @param cls the 'struct NeighbourMapEntry' for which we are running
2721 * @param tc scheduler context (unused)
2724 master_task (void *cls,
2725 const struct GNUNET_SCHEDULER_TaskContext *tc)
2727 struct NeighbourMapEntry *n = cls;
2728 struct GNUNET_TIME_Relative delay;
2730 n->task = GNUNET_SCHEDULER_NO_TASK;
2731 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2732 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2733 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2734 GNUNET_i2s (&n->id),
2735 print_state(n->state),
2736 GNUNET_STRINGS_relative_time_to_string (delay,
2740 case S_NOT_CONNECTED:
2741 /* invalid state for master task, clean up */
2743 n->state = S_DISCONNECT_FINISHED;
2744 free_neighbour (n, GNUNET_NO);
2747 if (0 == delay.rel_value_us)
2749 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2750 "Connection to `%s' timed out waiting for ATS to provide address\n",
2751 GNUNET_i2s (&n->id));
2752 n->state = S_DISCONNECT_FINISHED;
2753 free_neighbour (n, GNUNET_NO);
2757 case S_INIT_BLACKLIST:
2758 if (0 == delay.rel_value_us)
2760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2761 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2762 GNUNET_i2s (&n->id));
2763 n->state = S_DISCONNECT_FINISHED;
2764 free_neighbour (n, GNUNET_NO);
2768 case S_CONNECT_SENT:
2769 if (0 == delay.rel_value_us)
2771 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2772 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2773 GNUNET_i2s (&n->id));
2774 /* We could not send to this address, delete address and session */
2775 if (NULL != n->primary_address.session)
2776 GNUNET_ATS_address_destroyed (GST_ats,
2777 n->primary_address.address, n->primary_address.session);
2778 GNUNET_ATS_address_destroyed (GST_ats,
2779 n->primary_address.address, NULL);
2780 disconnect_neighbour (n);
2784 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2785 if (0 == delay.rel_value_us)
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2788 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2789 GNUNET_i2s (&n->id));
2790 n->state = S_DISCONNECT_FINISHED;
2791 free_neighbour (n, GNUNET_NO);
2795 case S_CONNECT_RECV_ATS:
2796 if (0 == delay.rel_value_us)
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2799 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2800 GNUNET_i2s (&n->id));
2801 n->state = S_DISCONNECT_FINISHED;
2802 free_neighbour (n, GNUNET_NO);
2806 case S_CONNECT_RECV_BLACKLIST:
2807 if (0 == delay.rel_value_us)
2809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2810 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2811 GNUNET_i2s (&n->id));
2812 n->state = S_DISCONNECT_FINISHED;
2813 free_neighbour (n, GNUNET_NO);
2817 case S_CONNECT_RECV_ACK:
2818 if (0 == delay.rel_value_us)
2820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2821 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2822 GNUNET_i2s (&n->id));
2823 disconnect_neighbour (n);
2828 if (0 == delay.rel_value_us)
2830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2831 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2832 GNUNET_i2s (&n->id));
2833 disconnect_neighbour (n);
2836 try_transmission_to_peer (n);
2839 case S_RECONNECT_ATS:
2840 if (0 == delay.rel_value_us)
2842 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2843 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2844 GNUNET_i2s (&n->id));
2845 disconnect_neighbour (n);
2849 case S_RECONNECT_BLACKLIST:
2850 if (0 == delay.rel_value_us)
2852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2853 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2854 GNUNET_i2s (&n->id));
2855 disconnect_neighbour (n);
2859 case S_RECONNECT_SENT:
2860 if (0 == delay.rel_value_us)
2862 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2863 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2864 GNUNET_i2s (&n->id));
2865 disconnect_neighbour (n);
2869 case S_CONNECTED_SWITCHING_BLACKLIST:
2870 if (0 == delay.rel_value_us)
2872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2873 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2874 GNUNET_i2s (&n->id));
2875 disconnect_neighbour (n);
2878 try_transmission_to_peer (n);
2881 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2882 if (0 == delay.rel_value_us)
2884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2885 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2886 GNUNET_i2s (&n->id));
2887 disconnect_neighbour (n);
2890 try_transmission_to_peer (n);
2894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2895 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2896 GNUNET_i2s (&n->id));
2897 free_neighbour (n, GNUNET_NO);
2899 case S_DISCONNECT_FINISHED:
2900 /* how did we get here!? */
2904 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2905 "Unhandled state `%s'\n",
2906 print_state (n->state));
2910 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2911 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2912 (S_CONNECTED == n->state) )
2914 /* if we are *now* in one of these three states, we're sending
2915 keep alive messages, so we need to consider the keepalive
2916 delay, not just the connection timeout */
2917 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2920 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2921 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2928 * Send a SESSION_ACK message to the neighbour to confirm that we
2929 * got his CONNECT_ACK.
2931 * @param n neighbour to send the SESSION_ACK to
2934 send_session_ack_message (struct NeighbourMapEntry *n)
2936 struct GNUNET_MessageHeader msg;
2938 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2939 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2940 (void) send_with_session(n,
2941 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2942 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
2948 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2949 * Consider switching to it.
2951 * @param message possibly a 'struct SessionConnectMessage' (check format)
2952 * @param peer identity of the peer to switch the address for
2953 * @param address address of the other peer, NULL if other peer
2955 * @param session session to use (or NULL)
2956 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2959 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2960 const struct GNUNET_PeerIdentity *peer,
2961 const struct GNUNET_HELLO_Address *address,
2962 struct Session *session)
2964 const struct SessionConnectMessage *scm;
2965 struct GNUNET_TIME_Absolute ts;
2966 struct NeighbourMapEntry *n;
2968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2969 "Received CONNECT_ACK message from peer `%s'\n",
2972 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2974 GNUNET_break_op (0);
2975 return GNUNET_SYSERR;
2977 GNUNET_STATISTICS_update (GST_stats,
2979 ("# CONNECT_ACK messages received"),
2981 scm = (const struct SessionConnectMessage *) message;
2982 GNUNET_break_op (ntohl (scm->reserved) == 0);
2983 if (NULL == (n = lookup_neighbour (peer)))
2985 GNUNET_STATISTICS_update (GST_stats,
2987 ("# unexpected CONNECT_ACK messages (no peer)"),
2989 return GNUNET_SYSERR;
2991 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2994 case S_NOT_CONNECTED:
2996 free_neighbour (n, GNUNET_NO);
2997 return GNUNET_SYSERR;
2999 case S_INIT_BLACKLIST:
3000 GNUNET_STATISTICS_update (GST_stats,
3002 ("# unexpected CONNECT_ACK messages (not ready)"),
3005 case S_CONNECT_SENT:
3006 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
3008 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
3009 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
3012 n->state = S_CONNECTED;
3013 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3014 GNUNET_STATISTICS_set (GST_stats,
3015 gettext_noop ("# peers connected"),
3016 ++neighbours_connected,
3018 connect_notify_cb (callback_cls, &n->id,
3019 n->primary_address.bandwidth_in,
3020 n->primary_address.bandwidth_out);
3021 /* Tell ATS that the outbound session we created to send CONNECT was successful */
3022 GST_ats_add_address (n->primary_address.address,
3023 n->primary_address.session,
3025 set_address (&n->primary_address,
3026 n->primary_address.address,
3027 n->primary_address.session,
3028 n->primary_address.bandwidth_in,
3029 n->primary_address.bandwidth_out,
3031 send_session_ack_message (n);
3033 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3034 case S_CONNECT_RECV_ATS:
3035 case S_CONNECT_RECV_BLACKLIST:
3036 case S_CONNECT_RECV_ACK:
3037 GNUNET_STATISTICS_update (GST_stats,
3039 ("# unexpected CONNECT_ACK messages (not ready)"),
3043 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3044 send_session_ack_message (n);
3046 case S_RECONNECT_ATS:
3047 case S_RECONNECT_BLACKLIST:
3048 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
3049 to give us a new address... */
3050 GNUNET_STATISTICS_update (GST_stats,
3052 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
3055 case S_RECONNECT_SENT:
3056 /* new address worked; go back to connected! */
3057 n->state = S_CONNECTED;
3058 send_session_ack_message (n);
3060 case S_CONNECTED_SWITCHING_BLACKLIST:
3061 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3062 send_session_ack_message (n);
3064 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3065 /* new address worked; adopt it and go back to connected! */
3066 n->state = S_CONNECTED;
3067 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3068 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3070 GST_ats_add_address (n->alternative_address.address,
3071 n->alternative_address.session,
3073 set_address (&n->primary_address,
3074 n->alternative_address.address,
3075 n->alternative_address.session,
3076 n->alternative_address.bandwidth_in,
3077 n->alternative_address.bandwidth_out,
3079 free_address (&n->alternative_address);
3080 send_session_ack_message (n);
3083 GNUNET_STATISTICS_update (GST_stats,
3085 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3087 return GNUNET_SYSERR;
3088 case S_DISCONNECT_FINISHED:
3092 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3093 "Unhandled state `%s'\n",
3094 print_state (n->state));
3096 return GNUNET_SYSERR;
3103 * A session was terminated. Take note; if needed, try to get
3104 * an alternative address from ATS.
3106 * @param peer identity of the peer where the session died
3107 * @param session session that is gone
3108 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3109 * this session was not in use
3112 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3113 struct Session *session)
3115 struct NeighbourMapEntry *n;
3116 struct BlackListCheckContext *bcc;
3117 struct BlackListCheckContext *bcc_next;
3119 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3121 while (NULL != (bcc = bcc_next))
3123 bcc_next = bcc->next;
3124 if (bcc->na.session == session)
3126 if (NULL != bcc->bc)
3127 GST_blacklist_test_cancel (bcc->bc);
3128 GNUNET_HELLO_address_free (bcc->na.address);
3129 GNUNET_CONTAINER_DLL_remove (bc_head,
3135 if (NULL == (n = lookup_neighbour (peer)))
3136 return GNUNET_NO; /* can't affect us */
3137 if (session != n->primary_address.session)
3139 if (session == n->alternative_address.session)
3141 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3142 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3143 n->state = S_CONNECTED;
3146 free_address (&n->alternative_address);
3148 return GNUNET_NO; /* doesn't affect us further */
3151 n->expect_latency_response = GNUNET_NO;
3154 case S_NOT_CONNECTED:
3156 free_neighbour (n, GNUNET_NO);
3160 free_neighbour (n, GNUNET_NO);
3162 case S_INIT_BLACKLIST:
3163 case S_CONNECT_SENT:
3164 free_address (&n->primary_address);
3165 n->state = S_INIT_ATS;
3166 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3167 // FIXME: need to ask ATS for suggestions again?
3168 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3170 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3171 case S_CONNECT_RECV_ATS:
3172 case S_CONNECT_RECV_BLACKLIST:
3173 case S_CONNECT_RECV_ACK:
3174 /* error on inbound session; free neighbour entirely */
3175 free_address (&n->primary_address);
3176 free_neighbour (n, GNUNET_NO);
3179 n->state = S_RECONNECT_ATS;
3180 free_address (&n->primary_address);
3181 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3182 /* FIXME: is this ATS call needed? */
3183 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3185 case S_RECONNECT_ATS:
3186 /* we don't have an address, how can it go down? */
3189 case S_RECONNECT_BLACKLIST:
3190 case S_RECONNECT_SENT:
3191 n->state = S_RECONNECT_ATS;
3192 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3193 // FIXME: need to ask ATS for suggestions again?
3194 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3196 case S_CONNECTED_SWITCHING_BLACKLIST:
3197 /* primary went down while we were checking secondary against
3198 blacklist, adopt secondary as primary */
3199 n->state = S_RECONNECT_BLACKLIST;
3200 free_address (&n->primary_address);
3201 n->primary_address = n->alternative_address;
3202 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3203 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3205 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3206 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3207 secondary as primary */
3208 n->state = S_RECONNECT_SENT;
3209 free_address (&n->primary_address);
3210 n->primary_address = n->alternative_address;
3211 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3212 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3215 free_address (&n->primary_address);
3217 case S_DISCONNECT_FINISHED:
3218 /* neighbour was freed and plugins told to terminate session */
3222 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3223 "Unhandled state `%s'\n",
3224 print_state (n->state));
3228 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3229 GNUNET_SCHEDULER_cancel (n->task);
3230 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3236 * We received a 'SESSION_ACK' message from the other peer.
3237 * If we sent a 'CONNECT_ACK' last, this means we are now
3238 * connected. Otherwise, do nothing.
3240 * @param message possibly a 'struct SessionConnectMessage' (check format)
3241 * @param peer identity of the peer to switch the address for
3242 * @param address address of the other peer, NULL if other peer
3244 * @param session session to use (or NULL)
3245 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3248 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3249 const struct GNUNET_PeerIdentity *peer,
3250 const struct GNUNET_HELLO_Address *address,
3251 struct Session *session)
3253 struct NeighbourMapEntry *n;
3255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3256 "Received SESSION_ACK message from peer `%s'\n",
3258 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3260 GNUNET_break_op (0);
3261 return GNUNET_SYSERR;
3263 GNUNET_STATISTICS_update (GST_stats,
3265 ("# SESSION_ACK messages received"),
3267 if (NULL == (n = lookup_neighbour (peer)))
3269 GNUNET_break_op (0);
3270 return GNUNET_SYSERR;
3272 /* check if we are in a plausible state for having sent
3273 a CONNECT_ACK. If not, return, otherwise break */
3274 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3275 (S_CONNECT_SENT != n->state) ) ||
3276 (2 != n->send_connect_ack) )
3278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3279 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3281 print_state (n->state),
3282 n->send_connect_ack);
3283 GNUNET_STATISTICS_update (GST_stats,
3284 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3288 n->state = S_CONNECTED;
3289 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3290 GNUNET_STATISTICS_set (GST_stats,
3291 gettext_noop ("# peers connected"),
3292 ++neighbours_connected,
3294 connect_notify_cb (callback_cls, &n->id,
3295 n->primary_address.bandwidth_in,
3296 n->primary_address.bandwidth_out);
3298 GST_ats_add_address (n->primary_address.address,
3299 n->primary_address.session,
3301 set_address (&n->primary_address,
3302 n->primary_address.address,
3303 n->primary_address.session,
3304 n->primary_address.bandwidth_in,
3305 n->primary_address.bandwidth_out,
3312 * Test if we're connected to the given peer.
3314 * @param target peer to test
3315 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3318 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3320 return test_connected (lookup_neighbour (target));
3325 * Change the incoming quota for the given peer.
3327 * @param neighbour identity of peer to change qutoa for
3328 * @param quota new quota
3331 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3332 struct GNUNET_BANDWIDTH_Value32NBO quota)
3334 struct NeighbourMapEntry *n;
3336 if (NULL == (n = lookup_neighbour (neighbour)))
3338 GNUNET_STATISTICS_update (GST_stats,
3340 ("# SET QUOTA messages ignored (no such peer)"),
3344 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3345 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3346 ntohl (quota.value__), GNUNET_i2s (&n->id));
3347 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3348 if (0 != ntohl (quota.value__))
3350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3351 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3352 GNUNET_i2s (&n->id));
3353 if (GNUNET_YES == test_connected (n))
3354 GNUNET_STATISTICS_update (GST_stats,
3355 gettext_noop ("# disconnects due to quota of 0"),
3357 disconnect_neighbour (n);
3362 * We received a disconnect message from the given peer,
3363 * validate and process.
3365 * @param peer sender of the message
3366 * @param msg the disconnect message
3369 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3370 const struct GNUNET_MessageHeader *msg)
3372 struct NeighbourMapEntry *n;
3373 const struct SessionDisconnectMessage *sdm;
3375 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3376 "Received DISCONNECT message from peer `%s'\n",
3378 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3380 // GNUNET_break_op (0);
3381 GNUNET_STATISTICS_update (GST_stats,
3383 ("# disconnect messages ignored (old format)"), 1,
3387 GNUNET_STATISTICS_update (GST_stats,
3389 ("# DISCONNECT messages received"),
3391 sdm = (const struct SessionDisconnectMessage *) msg;
3392 if (NULL == (n = lookup_neighbour (peer)))
3393 return; /* gone already */
3394 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3396 GNUNET_STATISTICS_update (GST_stats,
3398 ("# disconnect messages ignored (timestamp)"), 1,
3402 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3404 GNUNET_break_op (0);
3407 if (ntohl (sdm->purpose.size) !=
3408 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3409 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3410 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3412 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3413 "%s message from peer `%s' has invalid size \n",
3416 GNUNET_break_op (0);
3420 GNUNET_CRYPTO_eddsa_verify
3421 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3422 &sdm->signature, &sdm->public_key))
3424 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3425 "%s message from peer `%s' cannot be verified \n",
3428 GNUNET_break_op (0);
3431 if (GNUNET_YES == test_connected (n))
3432 GNUNET_STATISTICS_update (GST_stats,
3434 ("# other peer asked to disconnect from us"), 1,
3436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3437 "Disconnecting by request from peer %s\n",
3439 disconnect_neighbour (n);
3444 * Closure for the neighbours_iterate function.
3446 struct IteratorContext
3449 * Function to call on each connected neighbour.
3451 GST_NeighbourIterator cb;
3461 * Call the callback from the closure for each connected neighbour.
3463 * @param cls the `struct IteratorContext`
3464 * @param key the hash of the public key of the neighbour
3465 * @param value the `struct NeighbourMapEntry`
3466 * @return #GNUNET_OK (continue to iterate)
3469 neighbours_iterate (void *cls,
3470 const struct GNUNET_PeerIdentity *key,
3473 struct IteratorContext *ic = cls;
3474 struct NeighbourMapEntry *n = value;
3475 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3476 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3478 if (GNUNET_YES != test_connected (n))
3481 if (NULL != n->primary_address.address)
3483 bandwidth_in = n->primary_address.bandwidth_in;
3484 bandwidth_out = n->primary_address.bandwidth_out;
3488 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3489 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3491 ic->cb (ic->cb_cls, &n->id,
3492 n->primary_address.address,
3493 bandwidth_in, bandwidth_out);
3499 * Iterate over all connected neighbours.
3501 * @param cb function to call
3502 * @param cb_cls closure for cb
3505 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3507 struct IteratorContext ic;
3509 if (NULL == neighbours)
3510 return; /* can happen during shutdown */
3513 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3518 * If we have an active connection to the given target, it must be shutdown.
3520 * @param target peer to disconnect from
3523 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3525 struct NeighbourMapEntry *n;
3527 if (NULL == (n = lookup_neighbour (target)))
3528 return; /* not active */
3529 if (GNUNET_YES == test_connected (n))
3530 GNUNET_STATISTICS_update (GST_stats,
3532 ("# disconnected from peer upon explicit request"), 1,
3534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3535 "Forced disconnect from peer %s\n",
3536 GNUNET_i2s (target));
3537 disconnect_neighbour (n);
3542 * Obtain current latency information for the given neighbour.
3544 * @param peer to get the latency for
3545 * @return observed latency of the address, FOREVER if the
3546 * the connection is not up
3548 struct GNUNET_TIME_Relative
3549 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3551 struct NeighbourMapEntry *n;
3553 n = lookup_neighbour (peer);
3555 return GNUNET_TIME_UNIT_FOREVER_REL;
3559 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3560 case S_CONNECTED_SWITCHING_BLACKLIST:
3561 case S_RECONNECT_SENT:
3562 case S_RECONNECT_ATS:
3563 case S_RECONNECT_BLACKLIST:
3565 case S_NOT_CONNECTED:
3566 case S_INIT_BLACKLIST:
3568 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3569 case S_CONNECT_RECV_ATS:
3570 case S_CONNECT_RECV_BLACKLIST:
3571 case S_CONNECT_RECV_ACK:
3572 case S_CONNECT_SENT:
3574 case S_DISCONNECT_FINISHED:
3575 return GNUNET_TIME_UNIT_FOREVER_REL;
3577 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3578 "Unhandled state `%s'\n",
3579 print_state (n->state));
3583 return GNUNET_TIME_UNIT_FOREVER_REL;
3588 * Obtain current address information for the given neighbour.
3591 * @return address currently used
3593 struct GNUNET_HELLO_Address *
3594 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3596 struct NeighbourMapEntry *n;
3598 n = lookup_neighbour (peer);
3601 return n->primary_address.address;
3606 * Initialize the neighbours subsystem.
3608 * @param cls closure for callbacks
3609 * @param connect_cb function to call if we connect to a peer
3610 * @param disconnect_cb function to call if we disconnect from a peer
3611 * @param peer_address_cb function to call if we change an active address
3613 * @param max_fds maximum number of fds to use
3616 GST_neighbours_start (void *cls,
3617 NotifyConnect connect_cb,
3618 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3619 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3620 unsigned int max_fds)
3623 connect_notify_cb = connect_cb;
3624 disconnect_notify_cb = disconnect_cb;
3625 address_change_cb = peer_address_cb;
3626 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3627 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3628 utilization_transmission, NULL);
3633 * Disconnect from the given neighbour.
3636 * @param key hash of neighbour's public key (not used)
3637 * @param value the 'struct NeighbourMapEntry' of the neighbour
3638 * @return #GNUNET_OK (continue to iterate)
3641 disconnect_all_neighbours (void *cls,
3642 const struct GNUNET_PeerIdentity *key,
3645 struct NeighbourMapEntry *n = value;
3647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3648 "Disconnecting peer `%4s', %s\n",
3649 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3650 n->state = S_DISCONNECT_FINISHED;
3651 free_neighbour (n, GNUNET_NO);
3657 * Cleanup the neighbours subsystem.
3660 GST_neighbours_stop ()
3662 if (NULL == neighbours)
3664 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3666 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3667 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3670 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3671 &disconnect_all_neighbours,
3673 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3675 callback_cls = NULL;
3676 connect_notify_cb = NULL;
3677 disconnect_notify_cb = NULL;
3678 address_change_cb = NULL;
3682 /* end of file gnunet-service-transport_neighbours.c */