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 n->is_active = NULL; /* always free'd by its own continuation! */
913 /* fail messages currently in the queue */
914 while (NULL != (mq = n->messages_head))
916 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
917 if (NULL != mq->cont)
918 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
921 /* It is too late to send other peer disconnect notifications, but at
922 least internally we need to get clean... */
923 if (GNUNET_YES == test_connected (n))
925 GNUNET_STATISTICS_set (GST_stats,
926 gettext_noop ("# peers connected"),
927 --neighbours_connected,
929 disconnect_notify_cb (callback_cls, &n->id);
931 n->state = S_DISCONNECT_FINISHED;
933 if (NULL != n->primary_address.address)
935 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
938 backup_primary = NULL;
940 /* free addresses and mark as unused */
941 free_address (&n->primary_address);
942 free_address (&n->alternative_address);
944 /* FIXME: Note that if we are switching between two TCP sessions to
945 the same peer, we might want to selectively kill only one of
946 them! Killing all sessions like this seems to be very, very
949 /* cut transport-level connection */
950 if ((GNUNET_NO == keep_sessions) &&
951 (NULL != backup_primary) &&
952 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
953 papi->disconnect_peer (papi->cls, &n->id);
955 GNUNET_free_non_null (backup_primary);
957 GNUNET_assert (GNUNET_YES ==
958 GNUNET_CONTAINER_multipeermap_remove (neighbours,
961 // FIXME-ATS-API: we might want to be more specific about
962 // which states we do this from in the future (ATS should
963 // have given us a 'suggest_address' handle, and if we have
964 // such a handle, we should cancel the operation here!
965 if (NULL != n->suggest_handle)
967 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
968 n->suggest_handle = NULL;
971 if (GNUNET_SCHEDULER_NO_TASK != n->task)
973 GNUNET_SCHEDULER_cancel (n->task);
974 n->task = GNUNET_SCHEDULER_NO_TASK;
976 /* free rest of memory */
982 * Transmit a message using the current session of the given
985 * @param n entry for the recipient
986 * @param msgbuf buffer to transmit
987 * @param msgbuf_size number of bytes in @a msgbuf buffer
988 * @param priority transmission priority
989 * @param timeout transmission timeout
990 * @param use_keepalive_timeout #GNUNET_YES to use plugin-specific keep-alive
991 * timeout (@a timeout is ignored in that case), #GNUNET_NO otherwise
992 * @param cont continuation to call when finished (can be NULL)
993 * @param cont_cls closure for @a cont
994 * @return timeout (copy of @a timeout or a calculated one if
995 * @a use_keepalive_timeout is #GNUNET_YES.
997 static struct GNUNET_TIME_Relative
998 send_with_session (struct NeighbourMapEntry *n,
999 const char *msgbuf, size_t msgbuf_size,
1001 struct GNUNET_TIME_Relative timeout,
1002 unsigned int use_keepalive_timeout,
1003 GNUNET_TRANSPORT_TransmitContinuation cont,
1006 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1007 struct GNUNET_TIME_Relative result = GNUNET_TIME_UNIT_FOREVER_REL;
1009 GNUNET_assert (n->primary_address.session != NULL);
1010 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1011 (-1 == papi->send (papi->cls,
1012 n->primary_address.session,
1013 msgbuf, msgbuf_size,
1015 (result = (GNUNET_NO == use_keepalive_timeout) ? timeout :
1016 GNUNET_TIME_relative_divide (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1017 papi->query_keepalive_factor (papi->cls))),
1018 cont, cont_cls)))) &&
1020 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1021 GST_neighbours_notify_data_sent (&n->id,
1022 n->primary_address.address, n->primary_address.session, msgbuf_size);
1023 GNUNET_break (NULL != papi);
1029 * Master task run for every neighbour. Performs all of the time-related
1030 * activities (keep alive, send next message, disconnect if idle, finish
1031 * clean up after disconnect).
1033 * @param cls the `struct NeighbourMapEntry` for which we are running
1034 * @param tc scheduler context (unused)
1037 master_task (void *cls,
1038 const struct GNUNET_SCHEDULER_TaskContext *tc);
1042 * Function called when the 'DISCONNECT' message has been sent by the
1043 * plugin. Frees the neighbour --- if the entry still exists.
1046 * @param target identity of the neighbour that was disconnected
1047 * @param result #GNUNET_OK if the disconnect got out successfully
1048 * @param payload bytes payload
1049 * @param physical bytes physical
1052 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1053 int result, size_t payload, size_t physical)
1055 struct NeighbourMapEntry *n;
1057 n = lookup_neighbour (target);
1059 return; /* already gone */
1060 if (S_DISCONNECT != n->state)
1061 return; /* have created a fresh entry since */
1062 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1063 GNUNET_SCHEDULER_cancel (n->task);
1064 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1069 * Transmit a DISCONNECT message to the other peer.
1071 * @param n neighbour to send DISCONNECT message.
1074 send_disconnect (struct NeighbourMapEntry *n)
1076 struct SessionDisconnectMessage disconnect_msg;
1078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1079 "Sending DISCONNECT message to peer `%4s'\n",
1080 GNUNET_i2s (&n->id));
1081 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1082 disconnect_msg.header.type =
1083 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1084 disconnect_msg.reserved = htonl (0);
1085 disconnect_msg.purpose.size =
1086 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1087 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1088 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1089 disconnect_msg.purpose.purpose =
1090 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1091 disconnect_msg.timestamp =
1092 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1093 disconnect_msg.public_key = GST_my_identity.public_key;
1094 GNUNET_assert (GNUNET_OK ==
1095 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1096 &disconnect_msg.purpose,
1097 &disconnect_msg.signature));
1099 (void) send_with_session (n,
1100 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1101 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1102 GNUNET_NO, &send_disconnect_cont, NULL);
1103 GNUNET_STATISTICS_update (GST_stats,
1105 ("# DISCONNECT messages sent"), 1,
1111 * Disconnect from the given neighbour, clean up the record.
1113 * @param n neighbour to disconnect from
1116 disconnect_neighbour (struct NeighbourMapEntry *n)
1118 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1119 "Disconnecting from peer %s in state %s\n",
1120 GNUNET_i2s (&n->id),
1121 print_state (n->state));
1122 /* depending on state, notify neighbour and/or upper layers of this peer
1126 case S_NOT_CONNECTED:
1128 case S_INIT_BLACKLIST:
1129 /* other peer is completely unaware of us, no need to send DISCONNECT */
1130 n->state = S_DISCONNECT_FINISHED;
1131 free_neighbour (n, GNUNET_NO);
1133 case S_CONNECT_SENT:
1134 send_disconnect (n);
1135 n->state = S_DISCONNECT;
1137 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1138 case S_CONNECT_RECV_ATS:
1139 case S_CONNECT_RECV_BLACKLIST:
1140 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1141 n->state = S_DISCONNECT_FINISHED;
1142 free_neighbour (n, GNUNET_NO);
1144 case S_CONNECT_RECV_ACK:
1145 /* we DID ACK the other peer's request, must send DISCONNECT */
1146 send_disconnect (n);
1147 n->state = S_DISCONNECT;
1150 case S_RECONNECT_BLACKLIST:
1151 case S_RECONNECT_SENT:
1152 case S_CONNECTED_SWITCHING_BLACKLIST:
1153 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1154 /* we are currently connected, need to send disconnect and do
1155 internal notifications and update statistics */
1156 send_disconnect (n);
1157 GNUNET_STATISTICS_set (GST_stats,
1158 gettext_noop ("# peers connected"),
1159 --neighbours_connected,
1161 disconnect_notify_cb (callback_cls, &n->id);
1162 n->state = S_DISCONNECT;
1164 case S_RECONNECT_ATS:
1165 /* ATS address request timeout, disconnect without sending disconnect message */
1166 GNUNET_STATISTICS_set (GST_stats,
1167 gettext_noop ("# peers connected"),
1168 --neighbours_connected,
1170 disconnect_notify_cb (callback_cls, &n->id);
1171 n->state = S_DISCONNECT;
1174 /* already disconnected, ignore */
1176 case S_DISCONNECT_FINISHED:
1177 /* already cleaned up, how did we get here!? */
1181 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1182 "Unhandled state `%s'\n",
1183 print_state (n->state));
1187 /* schedule timeout to clean up */
1188 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1189 GNUNET_SCHEDULER_cancel (n->task);
1190 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1196 * We're done with our transmission attempt, continue processing.
1198 * @param cls the `struct MessageQueue` of the message
1199 * @param receiver intended receiver
1200 * @param success whether it worked or not
1201 * @param size_payload bytes payload sent
1202 * @param physical bytes sent on wire
1205 transmit_send_continuation (void *cls,
1206 const struct GNUNET_PeerIdentity *receiver,
1207 int success, size_t size_payload, size_t physical)
1209 struct MessageQueue *mq = cls;
1210 struct NeighbourMapEntry *n;
1212 if (NULL == (n = lookup_neighbour (receiver)))
1215 return; /* disconnect or other error while transmitting, can happen */
1217 if (n->is_active == mq)
1219 /* this is still "our" neighbour, remove us from its queue
1220 and allow it to send the next message now */
1221 n->is_active = NULL;
1222 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1223 GNUNET_SCHEDULER_cancel (n->task);
1224 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1226 if (bytes_in_send_queue < mq->message_buf_size)
1228 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1229 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1230 bytes_in_send_queue, mq->message_buf_size,
1231 (GNUNET_OK == success) ? "OK" : "FAIL",
1232 size_payload, physical);
1237 GNUNET_break (size_payload == mq->message_buf_size);
1238 bytes_in_send_queue -= mq->message_buf_size;
1239 GNUNET_STATISTICS_set (GST_stats,
1241 ("# bytes in message queue for other peers"),
1242 bytes_in_send_queue, GNUNET_NO);
1243 if (GNUNET_OK == success)
1244 GNUNET_STATISTICS_update (GST_stats,
1246 ("# messages transmitted to other peers"),
1249 GNUNET_STATISTICS_update (GST_stats,
1251 ("# transmission failures for messages to other peers"),
1253 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1254 "Sending message to `%s' of type %u was a %s\n",
1255 GNUNET_i2s (receiver),
1256 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1257 (success == GNUNET_OK) ? "success" : "FAILURE");
1258 if (NULL != mq->cont)
1259 mq->cont (mq->cont_cls, success, size_payload, physical);
1265 * Check the message list for the given neighbour and if we can
1266 * send a message, do so. This function should only be called
1267 * if the connection is at least generally ready for transmission.
1268 * While we will only send one message at a time, no bandwidth
1269 * quota management is performed here. If a message was given to
1270 * the plugin, the continuation will automatically re-schedule
1271 * the 'master' task once the next message might be transmitted.
1273 * @param n target peer for which to transmit
1276 try_transmission_to_peer (struct NeighbourMapEntry *n)
1278 struct MessageQueue *mq;
1279 struct GNUNET_TIME_Relative timeout;
1281 if (NULL == n->primary_address.address)
1283 /* no address, why are we here? */
1287 if ((0 == n->primary_address.address->address_length) &&
1288 (NULL == n->primary_address.session))
1290 /* no address, why are we here? */
1294 if (NULL != n->is_active)
1296 /* transmission already pending */
1300 /* timeout messages from the queue that are past their due date */
1301 while (NULL != (mq = n->messages_head))
1303 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1304 if (timeout.rel_value_us > 0)
1306 GNUNET_STATISTICS_update (GST_stats,
1308 ("# messages timed out while in transport queue"),
1310 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1312 transmit_send_continuation (mq, &n->id,
1314 mq->message_buf_size, 0); /* timeout */
1317 return; /* no more messages */
1318 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1320 (void) send_with_session (n,
1321 mq->message_buf, mq->message_buf_size,
1322 0 /* priority */, timeout, GNUNET_NO,
1323 &transmit_send_continuation, mq);
1328 * Send keepalive message to the neighbour. Must only be called
1329 * if we are on 'connected' state or while trying to switch addresses.
1330 * Will internally determine if a keepalive is truly needed (so can
1331 * always be called).
1333 * @param n neighbour that went idle and needs a keepalive
1336 send_keepalive (struct NeighbourMapEntry *n)
1338 struct SessionKeepAliveMessage m;
1339 struct GNUNET_TIME_Relative timeout;
1342 GNUNET_assert ((S_CONNECTED == n->state) ||
1343 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1344 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1345 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1346 return; /* no keepalive needed at this time */
1348 nonce = 0; /* 0 indicates 'not set' */
1350 nonce = GNUNET_CRYPTO_random_u32 (GNUNET_CRYPTO_QUALITY_NONCE, UINT32_MAX);
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Sending keep alive to peer `%s' with nonce %u\n",
1354 GNUNET_i2s (&n->id), nonce);
1356 m.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1357 m.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1358 m.nonce = htonl (nonce);
1360 timeout = send_with_session (n,
1361 (const void *) &m, sizeof (m),
1362 UINT32_MAX /* priority */,
1363 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1365 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1367 n->primary_address.keep_alive_nonce = nonce;
1368 n->expect_latency_response = GNUNET_YES;
1369 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1370 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (timeout);
1376 * Keep the connection to the given neighbour alive longer,
1377 * we received a KEEPALIVE (or equivalent); send a response.
1379 * @param neighbour neighbour to keep alive (by sending keep alive response)
1380 * @param m the keep alive message containing the nonce to respond to
1383 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour,
1384 const struct GNUNET_MessageHeader *m)
1386 struct NeighbourMapEntry *n;
1387 const struct SessionKeepAliveMessage *msg_in;
1388 struct SessionKeepAliveMessage msg;
1390 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1393 msg_in = (struct SessionKeepAliveMessage *) m;
1394 if (NULL == (n = lookup_neighbour (neighbour)))
1396 GNUNET_STATISTICS_update (GST_stats,
1398 ("# KEEPALIVE messages discarded (peer unknown)"),
1402 if (NULL == n->primary_address.session)
1404 GNUNET_STATISTICS_update (GST_stats,
1406 ("# KEEPALIVE messages discarded (no session)"),
1411 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1412 "Received keep alive request from peer `%s' with nonce %u\n",
1413 GNUNET_i2s (&n->id), ntohl (msg_in->nonce));
1415 /* send reply to allow neighbour to measure latency */
1416 msg.header.size = htons (sizeof (struct SessionKeepAliveMessage));
1417 msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1418 msg.nonce = msg_in->nonce;
1419 (void) send_with_session(n,
1420 (const void *) &msg, sizeof (struct SessionKeepAliveMessage),
1421 UINT32_MAX /* priority */,
1422 GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_YES,
1428 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1429 * latency to this peer. Pass the updated information (existing ats
1430 * plus calculated latency) to ATS.
1432 * @param neighbour neighbour to keep alive
1433 * @param m the message containing the keep alive response
1436 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1437 const struct GNUNET_MessageHeader *m)
1439 struct NeighbourMapEntry *n;
1440 const struct SessionKeepAliveMessage *msg;
1441 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1443 struct GNUNET_ATS_Information ats;
1445 if (sizeof (struct SessionKeepAliveMessage) != ntohs (m->size))
1448 msg = (const struct SessionKeepAliveMessage *) m;
1449 if (NULL == (n = lookup_neighbour (neighbour)))
1451 GNUNET_STATISTICS_update (GST_stats,
1453 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1457 if ( (S_CONNECTED != n->state) ||
1458 (GNUNET_YES != n->expect_latency_response) )
1460 GNUNET_STATISTICS_update (GST_stats,
1462 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1466 if (NULL == n->primary_address.address)
1468 GNUNET_STATISTICS_update (GST_stats,
1470 ("# KEEPALIVE_RESPONSE messages discarded (address changed)"),
1474 if (n->primary_address.keep_alive_nonce != ntohl (msg->nonce))
1476 GNUNET_STATISTICS_update (GST_stats,
1478 ("# KEEPALIVE_RESPONSE messages discarded (wrong nonce)"),
1484 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1485 "Received keep alive response from peer `%s' for session %p\n",
1486 GNUNET_i2s (&n->id), n->primary_address.session);
1490 /* Update session timeout here */
1491 if (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name)))
1493 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1494 "Updating session for peer `%s' for session %p\n",
1495 GNUNET_i2s (&n->id), n->primary_address.session);
1496 papi->update_session_timeout (papi->cls, &n->id, n->primary_address.session);
1503 n->primary_address.keep_alive_nonce = 0;
1504 n->expect_latency_response = GNUNET_NO;
1505 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1506 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Latency for peer `%s' is %s\n",
1509 GNUNET_i2s (&n->id),
1510 GNUNET_STRINGS_relative_time_to_string (n->latency,
1512 /* append latency */
1513 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1514 if (n->latency.rel_value_us > UINT32_MAX)
1515 latency = UINT32_MAX;
1517 latency = n->latency.rel_value_us;
1518 ats.value = htonl (latency);
1519 GST_ats_update_metrics (&n->id, n->primary_address.address,
1520 n->primary_address.session, &ats, 1);
1525 * We have received a message from the given sender. How long should
1526 * we delay before receiving more? (Also used to keep the peer marked
1529 * @param sender sender of the message
1530 * @param size size of the message
1531 * @param do_forward set to #GNUNET_YES if the message should be forwarded to clients
1532 * #GNUNET_NO if the neighbour is not connected or violates the quota,
1533 * #GNUNET_SYSERR if the connection is not fully up yet
1534 * @return how long to wait before reading more from this sender
1536 struct GNUNET_TIME_Relative
1537 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1538 *sender, ssize_t size, int *do_forward)
1540 struct NeighbourMapEntry *n;
1541 struct GNUNET_TIME_Relative ret;
1543 if (NULL == neighbours)
1545 *do_forward = GNUNET_NO;
1546 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1548 if (NULL == (n = lookup_neighbour (sender)))
1550 GST_neighbours_try_connect (sender);
1551 if (NULL == (n = lookup_neighbour (sender)))
1553 GNUNET_STATISTICS_update (GST_stats,
1555 ("# messages discarded due to lack of neighbour record"),
1557 *do_forward = GNUNET_NO;
1558 return GNUNET_TIME_UNIT_ZERO;
1561 if (! test_connected (n))
1563 *do_forward = GNUNET_SYSERR;
1564 return GNUNET_TIME_UNIT_ZERO;
1566 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1568 n->quota_violation_count++;
1569 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1570 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1571 n->in_tracker.available_bytes_per_s__,
1572 n->quota_violation_count);
1573 /* Discount 32k per violation */
1574 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1578 if (n->quota_violation_count > 0)
1580 /* try to add 32k back */
1581 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1582 n->quota_violation_count--;
1585 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1587 GNUNET_STATISTICS_update (GST_stats,
1589 ("# bandwidth quota violations by other peers"),
1591 *do_forward = GNUNET_NO;
1592 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1594 *do_forward = GNUNET_YES;
1595 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1596 if (ret.rel_value_us > 0)
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1600 (unsigned long long) n->in_tracker.
1601 consumption_since_last_update__,
1602 (unsigned int) n->in_tracker.available_bytes_per_s__,
1603 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1604 GNUNET_STATISTICS_update (GST_stats,
1605 gettext_noop ("# ms throttling suggested"),
1606 (int64_t) ret.rel_value_us / 1000LL,
1614 * Transmit a message to the given target using the active connection.
1616 * @param target destination
1617 * @param msg message to send
1618 * @param msg_size number of bytes in msg
1619 * @param timeout when to fail with timeout
1620 * @param cont function to call when done
1621 * @param cont_cls closure for 'cont'
1624 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1625 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1626 GST_NeighbourSendContinuation cont, void *cont_cls)
1628 struct NeighbourMapEntry *n;
1629 struct MessageQueue *mq;
1631 /* All ove these cases should never happen; they are all API violations.
1632 But we check anyway, just to be sure. */
1633 if (NULL == (n = lookup_neighbour (target)))
1637 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1640 if (GNUNET_YES != test_connected (n))
1644 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1647 bytes_in_send_queue += msg_size;
1648 GNUNET_STATISTICS_set (GST_stats,
1650 ("# bytes in message queue for other peers"),
1651 bytes_in_send_queue, GNUNET_NO);
1652 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1654 mq->cont_cls = cont_cls;
1655 memcpy (&mq[1], msg, msg_size);
1656 mq->message_buf = (const char *) &mq[1];
1657 mq->message_buf_size = msg_size;
1658 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1659 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1660 if ( (NULL != n->is_active) ||
1661 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1663 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1664 GNUNET_SCHEDULER_cancel (n->task);
1665 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1670 * Send a SESSION_CONNECT message via the given address.
1672 * @param na address to use
1675 send_session_connect (struct NeighbourAddress *na)
1677 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1678 struct SessionConnectMessage connect_msg;
1680 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1685 if (NULL == na->session)
1686 na->session = papi->get_session (papi->cls, na->address);
1687 if (NULL == na->session)
1692 GNUNET_STATISTICS_update (GST_stats,
1694 ("# SESSION_CONNECT messages sent"),
1696 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1697 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1698 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1699 connect_msg.reserved = htonl (0);
1700 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1702 papi->send (papi->cls,
1704 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1706 GNUNET_TIME_UNIT_FOREVER_REL,
1709 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1710 _("Failed to transmit CONNECT message via plugin to %s\n"),
1711 GST_plugins_a2s (na->address));
1713 GST_neighbours_notify_data_sent (&na->address->peer,
1716 sizeof (struct SessionConnectMessage));
1722 * Send a SESSION_CONNECT_ACK message via the given address.
1724 * @param address address to use
1725 * @param session session to use
1726 * @param timestamp timestamp to use for the ACK message
1729 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1730 struct Session *session,
1731 struct GNUNET_TIME_Absolute timestamp)
1733 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1734 struct SessionConnectMessage connect_msg;
1736 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1741 if (NULL == session)
1742 session = papi->get_session (papi->cls, address);
1743 if (NULL == session)
1748 GNUNET_STATISTICS_update (GST_stats,
1750 ("# CONNECT_ACK messages sent"),
1752 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1753 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1754 connect_msg.reserved = htonl (0);
1755 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1756 (void) papi->send (papi->cls,
1758 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1760 GNUNET_TIME_UNIT_FOREVER_REL,
1767 * Create a fresh entry in the neighbour map for the given peer
1769 * @param peer peer to create an entry for
1770 * @return new neighbour map entry
1772 static struct NeighbourMapEntry *
1773 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1775 struct NeighbourMapEntry *n;
1777 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1778 "Creating new neighbour entry for `%s'\n",
1780 n = GNUNET_new (struct NeighbourMapEntry);
1782 n->state = S_NOT_CONNECTED;
1783 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1784 n->last_util_transmission = GNUNET_TIME_absolute_get();
1785 n->util_payload_bytes_recv = 0;
1786 n->util_payload_bytes_sent = 0;
1787 n->util_total_bytes_recv = 0;
1788 n->util_total_bytes_sent = 0;
1789 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1790 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1791 MAX_BANDWIDTH_CARRY_S);
1792 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1793 GNUNET_assert (GNUNET_OK ==
1794 GNUNET_CONTAINER_multipeermap_put (neighbours,
1796 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1802 * Check if the two given addresses are the same.
1803 * Actually only checks if the sessions are non-NULL
1804 * (which they should be) and then if they are identical;
1805 * the actual addresses don't matter if the session
1806 * pointers match anyway, and we must have session pointers
1809 * @param a1 first address to compare
1810 * @param a2 other address to compare
1811 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1814 address_matches (const struct NeighbourAddress *a1,
1815 const struct NeighbourAddress *a2)
1817 if ( (NULL == a1->session) ||
1818 (NULL == a2->session) )
1823 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1828 * Try to create a connection to the given target (eventually).
1830 * @param target peer to try to connect to
1833 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1835 struct NeighbourMapEntry *n;
1837 if (NULL == neighbours)
1839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1840 "Asked to connect to peer `%s' during shutdown\n",
1841 GNUNET_i2s (target));
1842 return; /* during shutdown, do nothing */
1844 n = lookup_neighbour (target);
1845 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1846 "Asked to connect to peer `%s' (state: %s)\n",
1847 GNUNET_i2s (target),
1848 (NULL != n) ? print_state(n->state) : "NEW PEER");
1853 case S_NOT_CONNECTED:
1854 /* this should not be possible */
1856 free_neighbour (n, GNUNET_NO);
1859 case S_INIT_BLACKLIST:
1860 case S_CONNECT_SENT:
1861 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1862 case S_CONNECT_RECV_ATS:
1863 case S_CONNECT_RECV_BLACKLIST:
1864 case S_CONNECT_RECV_ACK:
1865 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1866 "Ignoring request to try to connect to `%s', already trying!\n",
1867 GNUNET_i2s (target));
1868 return; /* already trying */
1870 case S_RECONNECT_ATS:
1871 case S_RECONNECT_BLACKLIST:
1872 case S_RECONNECT_SENT:
1873 case S_CONNECTED_SWITCHING_BLACKLIST:
1874 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1875 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1876 "Ignoring request to try to connect, already connected to `%s'!\n",
1877 GNUNET_i2s (target));
1878 return; /* already connected */
1880 /* get rid of remains, ready to re-try immediately */
1881 free_neighbour (n, GNUNET_NO);
1883 case S_DISCONNECT_FINISHED:
1884 /* should not be possible */
1887 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1888 "Unhandled state `%s'\n",
1889 print_state (n->state));
1891 free_neighbour (n, GNUNET_NO);
1895 n = setup_neighbour (target);
1896 n->state = S_INIT_ATS;
1897 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1899 GNUNET_ATS_reset_backoff (GST_ats, target);
1900 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1905 * Function called with the result of a blacklist check.
1907 * @param cls closure with the 'struct BlackListCheckContext'
1908 * @param peer peer this check affects
1909 * @param result #GNUNET_OK if the address is allowed
1912 handle_test_blacklist_cont (void *cls,
1913 const struct GNUNET_PeerIdentity *peer,
1916 struct BlackListCheckContext *bcc = cls;
1917 struct NeighbourMapEntry *n;
1920 GNUNET_CONTAINER_DLL_remove (bc_head,
1923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1924 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1926 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1927 if (GNUNET_OK == result)
1928 GST_ats_add_address (bcc->na.address, bcc->na.session, NULL, 0);
1931 /* Blacklist disagreed on connecting to a peer with this address
1932 * Destroy address because we are not allowed to use it
1934 if (NULL != bcc->na.session)
1935 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1936 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1938 if (NULL == (n = lookup_neighbour (peer)))
1939 goto cleanup; /* nobody left to care about new address */
1942 case S_NOT_CONNECTED:
1943 /* this should not be possible */
1945 free_neighbour (n, GNUNET_NO);
1948 /* waiting on ATS suggestion; still, pass address to ATS as a
1951 case S_INIT_BLACKLIST:
1952 /* check if the address the blacklist was fine with matches
1953 ATS suggestion, if so, we can move on! */
1954 if ( (GNUNET_OK == result) &&
1955 (1 == n->send_connect_ack) )
1957 n->send_connect_ack = 2;
1958 send_session_connect_ack_message (bcc->na.address,
1960 n->connect_ack_timestamp);
1962 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1963 break; /* result for an address we currently don't care about */
1964 if (GNUNET_OK == result)
1966 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1967 n->state = S_CONNECT_SENT;
1968 send_session_connect (&n->primary_address);
1972 free_address (&n->primary_address);
1973 n->state = S_INIT_ATS;
1974 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1977 case S_CONNECT_SENT:
1978 /* waiting on CONNECT_ACK, send ACK if one is pending */
1979 if ( (GNUNET_OK == result) &&
1980 (1 == n->send_connect_ack) )
1982 n->send_connect_ack = 2;
1983 send_session_connect_ack_message (n->primary_address.address,
1984 n->primary_address.session,
1985 n->connect_ack_timestamp);
1988 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1989 n->state = S_CONNECT_RECV_ATS;
1990 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1991 GNUNET_ATS_reset_backoff (GST_ats, peer);
1992 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
1994 case S_CONNECT_RECV_ATS:
1995 /* waiting on ATS suggestion, don't care about blacklist */
1997 case S_CONNECT_RECV_BLACKLIST:
1998 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 "Blacklist result ignored, as it is not for our primary address\n");
2002 break; /* result for an address we currently don't care about */
2004 if (GNUNET_OK == result)
2006 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
2007 n->state = S_CONNECT_RECV_ACK;
2008 send_session_connect_ack_message (bcc->na.address,
2010 n->connect_ack_timestamp);
2011 if (1 == n->send_connect_ack)
2012 n->send_connect_ack = 2;
2016 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
2018 plugin = GST_plugins_find (bcc->na.address->transport_name);
2019 if ( (NULL != plugin) &&
2020 (NULL != bcc->na.session) )
2022 plugin->disconnect_session (plugin->cls,
2026 GNUNET_break (NULL != plugin);
2027 free_address (&n->primary_address);
2028 n->state = S_INIT_ATS;
2029 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2030 GNUNET_ATS_reset_backoff (GST_ats, peer);
2033 case S_CONNECT_RECV_ACK:
2034 /* waiting on SESSION_ACK, send ACK if one is pending */
2035 if ( (GNUNET_OK == result) &&
2036 (1 == n->send_connect_ack) )
2038 n->send_connect_ack = 2;
2039 send_session_connect_ack_message (n->primary_address.address,
2040 n->primary_address.session,
2041 n->connect_ack_timestamp);
2045 /* already connected, don't care about blacklist */
2047 case S_RECONNECT_ATS:
2048 /* still waiting on ATS suggestion, don't care about blacklist */
2050 case S_RECONNECT_BLACKLIST:
2051 if ( (GNUNET_OK == result) &&
2052 (1 == n->send_connect_ack) )
2054 n->send_connect_ack = 2;
2055 send_session_connect_ack_message (bcc->na.address,
2057 n->connect_ack_timestamp);
2059 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2060 break; /* result for an address we currently don't care about */
2061 if (GNUNET_OK == result)
2063 n->state = S_RECONNECT_SENT;
2064 send_session_connect (&n->primary_address);
2065 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2069 n->state = S_RECONNECT_ATS;
2070 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2073 case S_RECONNECT_SENT:
2074 /* waiting on CONNECT_ACK, don't care about blacklist */
2075 if ( (GNUNET_OK == result) &&
2076 (1 == n->send_connect_ack) )
2078 n->send_connect_ack = 2;
2079 send_session_connect_ack_message (n->primary_address.address,
2080 n->primary_address.session,
2081 n->connect_ack_timestamp);
2084 case S_CONNECTED_SWITCHING_BLACKLIST:
2085 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2086 break; /* result for an address we currently don't care about */
2087 if (GNUNET_OK == result)
2089 send_session_connect (&n->alternative_address);
2090 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
2094 n->state = S_CONNECTED;
2095 free_address (&n->alternative_address);
2098 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2099 /* waiting on CONNECT_ACK, don't care about blacklist */
2100 if ( (GNUNET_OK == result) &&
2101 (1 == n->send_connect_ack) )
2103 n->send_connect_ack = 2;
2104 send_session_connect_ack_message (n->primary_address.address,
2105 n->primary_address.session,
2106 n->connect_ack_timestamp);
2110 /* Nothing to do here, ATS will already do what can be done */
2112 case S_DISCONNECT_FINISHED:
2113 /* should not be possible */
2117 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2118 "Unhandled state `%s'\n",
2119 print_state (n->state));
2121 free_neighbour (n, GNUNET_NO);
2125 GNUNET_HELLO_address_free (bcc->na.address);
2131 * We want to know if connecting to a particular peer via
2132 * a particular address is allowed. Check it!
2134 * @param peer identity of the peer to switch the address for
2135 * @param ts time at which the check was initiated
2136 * @param address address of the other peer, NULL if other peer
2138 * @param session session to use (or NULL)
2141 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2142 struct GNUNET_TIME_Absolute ts,
2143 const struct GNUNET_HELLO_Address *address,
2144 struct Session *session)
2146 struct BlackListCheckContext *bcc;
2147 struct GST_BlacklistCheck *bc;
2149 bcc = GNUNET_new (struct BlackListCheckContext);
2150 bcc->na.address = GNUNET_HELLO_address_copy (address);
2151 bcc->na.session = session;
2152 bcc->na.connect_timestamp = ts;
2153 GNUNET_CONTAINER_DLL_insert (bc_head,
2156 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2157 address->transport_name,
2158 &handle_test_blacklist_cont, bcc)))
2160 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2161 we must only store 'bc' if 'bc' is non-NULL... */
2166 * We received a 'SESSION_CONNECT' message from the other peer.
2167 * Consider switching to it.
2169 * @param message possibly a 'struct SessionConnectMessage' (check format)
2170 * @param peer identity of the peer to switch the address for
2171 * @param address address of the other peer, NULL if other peer
2173 * @param session session to use (or NULL)
2174 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2177 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2178 const struct GNUNET_PeerIdentity *peer,
2179 const struct GNUNET_HELLO_Address *address,
2180 struct Session *session)
2182 const struct SessionConnectMessage *scm;
2183 struct NeighbourMapEntry *n;
2184 struct GNUNET_TIME_Absolute ts;
2186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2187 "Received CONNECT message from peer `%s'\n",
2189 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2191 GNUNET_break_op (0);
2192 return GNUNET_SYSERR;
2194 GNUNET_STATISTICS_update (GST_stats,
2196 ("# CONNECT messages received"),
2198 if (NULL == neighbours)
2200 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2201 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2203 return GNUNET_OK; /* we're shutting down */
2205 scm = (const struct SessionConnectMessage *) message;
2206 GNUNET_break_op (0 == ntohl (scm->reserved));
2207 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2208 n = lookup_neighbour (peer);
2210 n = setup_neighbour (peer);
2211 n->send_connect_ack = 1;
2212 n->connect_ack_timestamp = ts;
2216 case S_NOT_CONNECTED:
2217 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2218 /* Do a blacklist check for the new address */
2219 check_blacklist (peer, ts, address, session);
2222 /* CONNECT message takes priority over us asking ATS for address */
2223 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2225 case S_INIT_BLACKLIST:
2226 case S_CONNECT_SENT:
2227 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2228 case S_CONNECT_RECV_ATS:
2229 case S_CONNECT_RECV_BLACKLIST:
2230 case S_CONNECT_RECV_ACK:
2231 /* It can never hurt to have an alternative address in the above cases,
2232 see if it is allowed */
2233 check_blacklist (peer, ts, address, session);
2236 /* we are already connected and can thus send the ACK immediately;
2237 still, it can never hurt to have an alternative address, so also
2238 tell ATS about it */
2239 GNUNET_assert (NULL != n->primary_address.address);
2240 GNUNET_assert (NULL != n->primary_address.session);
2241 n->send_connect_ack = 0;
2242 send_session_connect_ack_message (n->primary_address.address,
2243 n->primary_address.session, ts);
2244 check_blacklist (peer, ts, address, session);
2246 case S_RECONNECT_ATS:
2247 case S_RECONNECT_BLACKLIST:
2248 case S_RECONNECT_SENT:
2249 /* It can never hurt to have an alternative address in the above cases,
2250 see if it is allowed */
2251 check_blacklist (peer, ts, address, session);
2253 case S_CONNECTED_SWITCHING_BLACKLIST:
2254 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2255 /* we are already connected and can thus send the ACK immediately;
2256 still, it can never hurt to have an alternative address, so also
2257 tell ATS about it */
2258 GNUNET_assert (NULL != n->primary_address.address);
2259 GNUNET_assert (NULL != n->primary_address.session);
2260 n->send_connect_ack = 0;
2261 send_session_connect_ack_message (n->primary_address.address,
2262 n->primary_address.session, ts);
2263 check_blacklist (peer, ts, address, session);
2266 /* get rid of remains without terminating sessions, ready to re-try */
2267 free_neighbour (n, GNUNET_YES);
2268 n = setup_neighbour (peer);
2269 n->state = S_CONNECT_RECV_ATS;
2270 GNUNET_ATS_reset_backoff (GST_ats, peer);
2271 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2273 case S_DISCONNECT_FINISHED:
2274 /* should not be possible */
2278 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2279 "Unhandled state `%s'\n",
2280 print_state (n->state));
2282 return GNUNET_SYSERR;
2289 * For an existing neighbour record, set the active connection to
2290 * use the given address.
2292 * @param peer identity of the peer to switch the address for
2293 * @param address address of the other peer, NULL if other peer
2295 * @param session session to use (or NULL)
2296 * @param ats performance data
2297 * @param ats_count number of entries in ats
2298 * @param bandwidth_in inbound quota to be used when connection is up,
2299 * 0 to disconnect from peer
2300 * @param bandwidth_out outbound quota to be used when connection is up,
2301 * 0 to disconnect from peer
2304 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2305 const struct GNUNET_HELLO_Address *address,
2306 struct Session *session,
2307 const struct GNUNET_ATS_Information *ats,
2309 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2310 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2312 struct NeighbourMapEntry *n;
2313 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2315 GNUNET_assert (address->transport_name != NULL);
2316 if (NULL == (n = lookup_neighbour (peer)))
2319 /* Obtain an session for this address from plugin */
2320 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2322 /* we don't have the plugin for this address */
2323 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2326 if ((NULL == session) && (0 == address->address_length))
2329 if (strlen (address->transport_name) > 0)
2330 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2335 "ATS tells us to switch to address '%s/%s' session %p for "
2336 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2337 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2338 address->transport_name,
2341 print_state (n->state),
2342 n->send_connect_ack,
2343 ntohl (bandwidth_in.value__),
2344 ntohl (bandwidth_out.value__));
2346 if (NULL == session)
2348 session = papi->get_session (papi->cls, address);
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "Obtained new session for peer `%s' and address '%s': %p\n",
2351 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2353 if (NULL == session)
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 "Failed to obtain new session for peer `%s' and address '%s'\n",
2357 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2358 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2363 case S_NOT_CONNECTED:
2365 free_neighbour (n, GNUNET_NO);
2368 set_address (&n->primary_address,
2369 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2370 n->state = S_INIT_BLACKLIST;
2371 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2372 check_blacklist (&n->id,
2373 n->connect_ack_timestamp,
2376 case S_INIT_BLACKLIST:
2377 /* ATS suggests a different address, switch again */
2378 set_address (&n->primary_address,
2379 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2380 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2381 check_blacklist (&n->id,
2382 n->connect_ack_timestamp,
2385 case S_CONNECT_SENT:
2386 /* ATS suggests a different address, switch again */
2387 set_address (&n->primary_address,
2388 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2389 n->state = S_INIT_BLACKLIST;
2390 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2391 check_blacklist (&n->id,
2392 n->connect_ack_timestamp,
2395 case S_CONNECT_RECV_ATS:
2396 set_address (&n->primary_address,
2397 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2398 n->state = S_CONNECT_RECV_BLACKLIST;
2399 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2400 check_blacklist (&n->id,
2401 n->connect_ack_timestamp,
2404 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2405 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2406 check_blacklist (&n->id,
2407 n->connect_ack_timestamp,
2410 case S_CONNECT_RECV_BLACKLIST:
2411 case S_CONNECT_RECV_ACK:
2412 /* ATS asks us to switch while we were trying to connect; switch to new
2413 address and check blacklist again */
2414 set_address (&n->primary_address,
2415 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2416 n->state = S_CONNECT_RECV_BLACKLIST;
2417 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2418 check_blacklist (&n->id,
2419 n->connect_ack_timestamp,
2423 GNUNET_assert (NULL != n->primary_address.address);
2424 GNUNET_assert (NULL != n->primary_address.session);
2425 if (n->primary_address.session == session)
2427 /* not an address change, just a quota change */
2428 set_address (&n->primary_address,
2429 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2432 /* ATS asks us to switch a life connection; see if we can get
2433 a CONNECT_ACK on it before we actually do this! */
2434 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2435 set_address (&n->alternative_address,
2436 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2437 check_blacklist (&n->id,
2438 GNUNET_TIME_absolute_get (),
2441 case S_RECONNECT_ATS:
2442 n->state = S_RECONNECT_BLACKLIST;
2443 set_address (&n->primary_address,
2444 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2445 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2446 check_blacklist (&n->id,
2447 n->connect_ack_timestamp,
2450 case S_RECONNECT_BLACKLIST:
2451 /* ATS asks us to switch while we were trying to reconnect; switch to new
2452 address and check blacklist again */
2453 set_address (&n->primary_address,
2454 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2455 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2456 check_blacklist (&n->id,
2457 n->connect_ack_timestamp,
2460 case S_RECONNECT_SENT:
2461 /* ATS asks us to switch while we were trying to reconnect; switch to new
2462 address and check blacklist again */
2463 n->state = S_RECONNECT_BLACKLIST;
2464 set_address (&n->primary_address,
2465 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2466 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2467 check_blacklist (&n->id,
2468 n->connect_ack_timestamp,
2471 case S_CONNECTED_SWITCHING_BLACKLIST:
2472 if (n->primary_address.session == session)
2474 /* ATS switches back to still-active session */
2475 n->state = S_CONNECTED;
2476 free_address (&n->alternative_address);
2479 /* ATS asks us to switch a life connection, update blacklist check */
2480 set_address (&n->alternative_address,
2481 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2482 check_blacklist (&n->id,
2483 GNUNET_TIME_absolute_get (),
2486 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2487 if (n->primary_address.session == session)
2489 /* ATS switches back to still-active session */
2490 free_address (&n->alternative_address);
2491 n->state = S_CONNECTED;
2494 /* ATS asks us to switch a life connection, update blacklist check */
2495 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2496 set_address (&n->alternative_address,
2497 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2498 check_blacklist (&n->id,
2499 GNUNET_TIME_absolute_get (),
2503 /* not going to switch addresses while disconnecting */
2505 case S_DISCONNECT_FINISHED:
2509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2510 "Unhandled state `%s'\n",
2511 print_state (n->state));
2519 send_utilization_data (void *cls,
2520 const struct GNUNET_PeerIdentity *key,
2523 struct NeighbourMapEntry *n = value;
2524 struct GNUNET_ATS_Information atsi[4];
2526 uint32_t bps_pl_out;
2529 struct GNUNET_TIME_Relative delta;
2531 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2532 GNUNET_TIME_absolute_get ());
2536 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2537 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2539 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2540 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2542 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2547 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2548 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2550 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2551 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2554 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2555 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2559 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2560 atsi[0].value = htonl (bps_out);
2561 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2562 atsi[1].value = htonl (bps_in);
2564 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2565 atsi[2].value = htonl (bps_pl_out);
2566 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2567 atsi[3].value = htonl (bps_pl_in);
2569 GST_ats_update_metrics (key, n->primary_address.address,
2570 n->primary_address.session, atsi, 4);
2571 n->util_payload_bytes_recv = 0;
2572 n->util_payload_bytes_sent = 0;
2573 n->util_total_bytes_recv = 0;
2574 n->util_total_bytes_sent = 0;
2575 n->last_util_transmission = GNUNET_TIME_absolute_get();
2581 * Task transmitting utilization in a regular interval
2583 * @param cls the 'struct NeighbourMapEntry' for which we are running
2584 * @param tc scheduler context (unused)
2587 utilization_transmission (void *cls,
2588 const struct GNUNET_SCHEDULER_TaskContext *tc)
2590 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2592 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2593 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2595 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2596 utilization_transmission, NULL);
2602 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2603 const struct GNUNET_HELLO_Address *address,
2604 struct Session *session,
2605 const struct GNUNET_MessageHeader *message)
2607 struct NeighbourMapEntry *n;
2609 n = lookup_neighbour (peer);
2612 n->util_total_bytes_recv += ntohs(message->size);
2617 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2618 const struct GNUNET_HELLO_Address *address,
2619 struct Session *session,
2620 const struct GNUNET_MessageHeader *message)
2622 struct NeighbourMapEntry *n;
2623 n = lookup_neighbour (peer);
2626 n->util_payload_bytes_recv += ntohs(message->size);
2631 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2632 const struct GNUNET_HELLO_Address *address,
2633 struct Session *session,
2636 struct NeighbourMapEntry *n;
2637 n = lookup_neighbour (peer);
2640 if (n->primary_address.session != session)
2642 n->util_total_bytes_sent += size;
2647 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2650 struct NeighbourMapEntry *n;
2651 n = lookup_neighbour (peer);
2654 n->util_payload_bytes_sent += size;
2659 * Master task run for every neighbour. Performs all of the time-related
2660 * activities (keep alive, send next message, disconnect if idle, finish
2661 * clean up after disconnect).
2663 * @param cls the 'struct NeighbourMapEntry' for which we are running
2664 * @param tc scheduler context (unused)
2667 master_task (void *cls,
2668 const struct GNUNET_SCHEDULER_TaskContext *tc)
2670 struct NeighbourMapEntry *n = cls;
2671 struct GNUNET_TIME_Relative delay;
2673 n->task = GNUNET_SCHEDULER_NO_TASK;
2674 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2676 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2677 GNUNET_i2s (&n->id),
2678 print_state(n->state),
2679 GNUNET_STRINGS_relative_time_to_string (delay,
2683 case S_NOT_CONNECTED:
2684 /* invalid state for master task, clean up */
2686 n->state = S_DISCONNECT_FINISHED;
2687 free_neighbour (n, GNUNET_NO);
2690 if (0 == delay.rel_value_us)
2692 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2693 "Connection to `%s' timed out waiting for ATS to provide address\n",
2694 GNUNET_i2s (&n->id));
2695 n->state = S_DISCONNECT_FINISHED;
2696 free_neighbour (n, GNUNET_NO);
2700 case S_INIT_BLACKLIST:
2701 if (0 == delay.rel_value_us)
2703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2704 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2705 GNUNET_i2s (&n->id));
2706 n->state = S_DISCONNECT_FINISHED;
2707 free_neighbour (n, GNUNET_NO);
2711 case S_CONNECT_SENT:
2712 if (0 == delay.rel_value_us)
2714 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2715 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2716 GNUNET_i2s (&n->id));
2717 /* We could not send to this address, delete address and session */
2718 if (NULL != n->primary_address.session)
2719 GNUNET_ATS_address_destroyed (GST_ats,
2720 n->primary_address.address, n->primary_address.session);
2721 GNUNET_ATS_address_destroyed (GST_ats,
2722 n->primary_address.address, NULL);
2723 disconnect_neighbour (n);
2727 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2728 if (0 == delay.rel_value_us)
2730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2731 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2732 GNUNET_i2s (&n->id));
2733 n->state = S_DISCONNECT_FINISHED;
2734 free_neighbour (n, GNUNET_NO);
2738 case S_CONNECT_RECV_ATS:
2739 if (0 == delay.rel_value_us)
2741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2742 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2743 GNUNET_i2s (&n->id));
2744 n->state = S_DISCONNECT_FINISHED;
2745 free_neighbour (n, GNUNET_NO);
2749 case S_CONNECT_RECV_BLACKLIST:
2750 if (0 == delay.rel_value_us)
2752 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2753 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2754 GNUNET_i2s (&n->id));
2755 n->state = S_DISCONNECT_FINISHED;
2756 free_neighbour (n, GNUNET_NO);
2760 case S_CONNECT_RECV_ACK:
2761 if (0 == delay.rel_value_us)
2763 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2764 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2765 GNUNET_i2s (&n->id));
2766 disconnect_neighbour (n);
2771 if (0 == delay.rel_value_us)
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2774 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2775 GNUNET_i2s (&n->id));
2776 disconnect_neighbour (n);
2779 try_transmission_to_peer (n);
2782 case S_RECONNECT_ATS:
2783 if (0 == delay.rel_value_us)
2785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2786 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2787 GNUNET_i2s (&n->id));
2788 disconnect_neighbour (n);
2792 case S_RECONNECT_BLACKLIST:
2793 if (0 == delay.rel_value_us)
2795 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2796 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2797 GNUNET_i2s (&n->id));
2798 disconnect_neighbour (n);
2802 case S_RECONNECT_SENT:
2803 if (0 == delay.rel_value_us)
2805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2806 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2807 GNUNET_i2s (&n->id));
2808 disconnect_neighbour (n);
2812 case S_CONNECTED_SWITCHING_BLACKLIST:
2813 if (0 == delay.rel_value_us)
2815 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2816 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2817 GNUNET_i2s (&n->id));
2818 disconnect_neighbour (n);
2821 try_transmission_to_peer (n);
2824 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2825 if (0 == delay.rel_value_us)
2827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2828 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2829 GNUNET_i2s (&n->id));
2830 disconnect_neighbour (n);
2833 try_transmission_to_peer (n);
2837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2838 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2839 GNUNET_i2s (&n->id));
2840 free_neighbour (n, GNUNET_NO);
2842 case S_DISCONNECT_FINISHED:
2843 /* how did we get here!? */
2847 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2848 "Unhandled state `%s'\n",
2849 print_state (n->state));
2853 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2854 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2855 (S_CONNECTED == n->state) )
2857 /* if we are *now* in one of these three states, we're sending
2858 keep alive messages, so we need to consider the keepalive
2859 delay, not just the connection timeout */
2860 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2863 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2864 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2871 * Send a SESSION_ACK message to the neighbour to confirm that we
2872 * got his CONNECT_ACK.
2874 * @param n neighbour to send the SESSION_ACK to
2877 send_session_ack_message (struct NeighbourMapEntry *n)
2879 struct GNUNET_MessageHeader msg;
2881 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2882 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2883 (void) send_with_session(n,
2884 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2885 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, GNUNET_NO,
2891 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2892 * Consider switching to it.
2894 * @param message possibly a 'struct SessionConnectMessage' (check format)
2895 * @param peer identity of the peer to switch the address for
2896 * @param address address of the other peer, NULL if other peer
2898 * @param session session to use (or NULL)
2899 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2902 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2903 const struct GNUNET_PeerIdentity *peer,
2904 const struct GNUNET_HELLO_Address *address,
2905 struct Session *session)
2907 const struct SessionConnectMessage *scm;
2908 struct GNUNET_TIME_Absolute ts;
2909 struct NeighbourMapEntry *n;
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2912 "Received CONNECT_ACK message from peer `%s'\n",
2915 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2917 GNUNET_break_op (0);
2918 return GNUNET_SYSERR;
2920 GNUNET_STATISTICS_update (GST_stats,
2922 ("# CONNECT_ACK messages received"),
2924 scm = (const struct SessionConnectMessage *) message;
2925 GNUNET_break_op (ntohl (scm->reserved) == 0);
2926 if (NULL == (n = lookup_neighbour (peer)))
2928 GNUNET_STATISTICS_update (GST_stats,
2930 ("# unexpected CONNECT_ACK messages (no peer)"),
2932 return GNUNET_SYSERR;
2934 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2937 case S_NOT_CONNECTED:
2939 free_neighbour (n, GNUNET_NO);
2940 return GNUNET_SYSERR;
2942 case S_INIT_BLACKLIST:
2943 GNUNET_STATISTICS_update (GST_stats,
2945 ("# unexpected CONNECT_ACK messages (not ready)"),
2948 case S_CONNECT_SENT:
2949 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
2951 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2952 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
2955 n->state = S_CONNECTED;
2956 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2957 GNUNET_STATISTICS_set (GST_stats,
2958 gettext_noop ("# peers connected"),
2959 ++neighbours_connected,
2961 connect_notify_cb (callback_cls, &n->id,
2962 n->primary_address.bandwidth_in,
2963 n->primary_address.bandwidth_out);
2964 /* Tell ATS that the outbound session we created to send CONNECT was successful */
2965 GST_ats_add_address (n->primary_address.address,
2966 n->primary_address.session,
2968 set_address (&n->primary_address,
2969 n->primary_address.address,
2970 n->primary_address.session,
2971 n->primary_address.bandwidth_in,
2972 n->primary_address.bandwidth_out,
2974 send_session_ack_message (n);
2976 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2977 case S_CONNECT_RECV_ATS:
2978 case S_CONNECT_RECV_BLACKLIST:
2979 case S_CONNECT_RECV_ACK:
2980 GNUNET_STATISTICS_update (GST_stats,
2982 ("# unexpected CONNECT_ACK messages (not ready)"),
2986 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2987 send_session_ack_message (n);
2989 case S_RECONNECT_ATS:
2990 case S_RECONNECT_BLACKLIST:
2991 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2992 to give us a new address... */
2993 GNUNET_STATISTICS_update (GST_stats,
2995 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2998 case S_RECONNECT_SENT:
2999 /* new address worked; go back to connected! */
3000 n->state = S_CONNECTED;
3001 send_session_ack_message (n);
3003 case S_CONNECTED_SWITCHING_BLACKLIST:
3004 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
3005 send_session_ack_message (n);
3007 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3008 /* new address worked; adopt it and go back to connected! */
3009 n->state = S_CONNECTED;
3010 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3011 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
3013 GST_ats_add_address (n->alternative_address.address,
3014 n->alternative_address.session,
3016 set_address (&n->primary_address,
3017 n->alternative_address.address,
3018 n->alternative_address.session,
3019 n->alternative_address.bandwidth_in,
3020 n->alternative_address.bandwidth_out,
3022 free_address (&n->alternative_address);
3023 send_session_ack_message (n);
3026 GNUNET_STATISTICS_update (GST_stats,
3028 ("# unexpected CONNECT_ACK messages (disconnecting)"),
3030 return GNUNET_SYSERR;
3031 case S_DISCONNECT_FINISHED:
3035 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3036 "Unhandled state `%s'\n",
3037 print_state (n->state));
3039 return GNUNET_SYSERR;
3046 * A session was terminated. Take note; if needed, try to get
3047 * an alternative address from ATS.
3049 * @param peer identity of the peer where the session died
3050 * @param session session that is gone
3051 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
3052 * this session was not in use
3055 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
3056 struct Session *session)
3058 struct NeighbourMapEntry *n;
3059 struct BlackListCheckContext *bcc;
3060 struct BlackListCheckContext *bcc_next;
3062 /* make sure to cancel all ongoing blacklist checks involving 'session' */
3064 while (NULL != (bcc = bcc_next))
3066 bcc_next = bcc->next;
3067 if (bcc->na.session == session)
3069 if (NULL != bcc->bc)
3070 GST_blacklist_test_cancel (bcc->bc);
3071 GNUNET_HELLO_address_free (bcc->na.address);
3072 GNUNET_CONTAINER_DLL_remove (bc_head,
3078 if (NULL == (n = lookup_neighbour (peer)))
3079 return GNUNET_NO; /* can't affect us */
3080 if (session != n->primary_address.session)
3082 if (session == n->alternative_address.session)
3084 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
3085 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
3086 n->state = S_CONNECTED;
3089 free_address (&n->alternative_address);
3091 return GNUNET_NO; /* doesn't affect us further */
3094 n->expect_latency_response = GNUNET_NO;
3097 case S_NOT_CONNECTED:
3099 free_neighbour (n, GNUNET_NO);
3103 free_neighbour (n, GNUNET_NO);
3105 case S_INIT_BLACKLIST:
3106 case S_CONNECT_SENT:
3107 free_address (&n->primary_address);
3108 n->state = S_INIT_ATS;
3109 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3110 // FIXME: need to ask ATS for suggestions again?
3111 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3113 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3114 case S_CONNECT_RECV_ATS:
3115 case S_CONNECT_RECV_BLACKLIST:
3116 case S_CONNECT_RECV_ACK:
3117 /* error on inbound session; free neighbour entirely */
3118 free_address (&n->primary_address);
3119 free_neighbour (n, GNUNET_NO);
3122 n->state = S_RECONNECT_ATS;
3123 free_address (&n->primary_address);
3124 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3125 /* FIXME: is this ATS call needed? */
3126 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3128 case S_RECONNECT_ATS:
3129 /* we don't have an address, how can it go down? */
3132 case S_RECONNECT_BLACKLIST:
3133 case S_RECONNECT_SENT:
3134 n->state = S_RECONNECT_ATS;
3135 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3136 // FIXME: need to ask ATS for suggestions again?
3137 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3139 case S_CONNECTED_SWITCHING_BLACKLIST:
3140 /* primary went down while we were checking secondary against
3141 blacklist, adopt secondary as primary */
3142 n->state = S_RECONNECT_BLACKLIST;
3143 free_address (&n->primary_address);
3144 n->primary_address = n->alternative_address;
3145 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3146 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3148 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3149 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3150 secondary as primary */
3151 n->state = S_RECONNECT_SENT;
3152 free_address (&n->primary_address);
3153 n->primary_address = n->alternative_address;
3154 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3155 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3158 free_address (&n->primary_address);
3160 case S_DISCONNECT_FINISHED:
3161 /* neighbour was freed and plugins told to terminate session */
3165 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3166 "Unhandled state `%s'\n",
3167 print_state (n->state));
3171 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3172 GNUNET_SCHEDULER_cancel (n->task);
3173 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3179 * We received a 'SESSION_ACK' message from the other peer.
3180 * If we sent a 'CONNECT_ACK' last, this means we are now
3181 * connected. Otherwise, do nothing.
3183 * @param message possibly a 'struct SessionConnectMessage' (check format)
3184 * @param peer identity of the peer to switch the address for
3185 * @param address address of the other peer, NULL if other peer
3187 * @param session session to use (or NULL)
3188 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3191 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3192 const struct GNUNET_PeerIdentity *peer,
3193 const struct GNUNET_HELLO_Address *address,
3194 struct Session *session)
3196 struct NeighbourMapEntry *n;
3198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3199 "Received SESSION_ACK message from peer `%s'\n",
3201 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3203 GNUNET_break_op (0);
3204 return GNUNET_SYSERR;
3206 GNUNET_STATISTICS_update (GST_stats,
3208 ("# SESSION_ACK messages received"),
3210 if (NULL == (n = lookup_neighbour (peer)))
3212 GNUNET_break_op (0);
3213 return GNUNET_SYSERR;
3215 /* check if we are in a plausible state for having sent
3216 a CONNECT_ACK. If not, return, otherwise break */
3217 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3218 (S_CONNECT_SENT != n->state) ) ||
3219 (2 != n->send_connect_ack) )
3221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3222 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3224 print_state (n->state),
3225 n->send_connect_ack);
3226 GNUNET_STATISTICS_update (GST_stats,
3227 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3231 n->state = S_CONNECTED;
3232 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3233 GNUNET_STATISTICS_set (GST_stats,
3234 gettext_noop ("# peers connected"),
3235 ++neighbours_connected,
3237 connect_notify_cb (callback_cls, &n->id,
3238 n->primary_address.bandwidth_in,
3239 n->primary_address.bandwidth_out);
3241 GST_ats_add_address (n->primary_address.address,
3242 n->primary_address.session,
3244 set_address (&n->primary_address,
3245 n->primary_address.address,
3246 n->primary_address.session,
3247 n->primary_address.bandwidth_in,
3248 n->primary_address.bandwidth_out,
3255 * Test if we're connected to the given peer.
3257 * @param target peer to test
3258 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3261 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3263 return test_connected (lookup_neighbour (target));
3268 * Change the incoming quota for the given peer.
3270 * @param neighbour identity of peer to change qutoa for
3271 * @param quota new quota
3274 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3275 struct GNUNET_BANDWIDTH_Value32NBO quota)
3277 struct NeighbourMapEntry *n;
3279 if (NULL == (n = lookup_neighbour (neighbour)))
3281 GNUNET_STATISTICS_update (GST_stats,
3283 ("# SET QUOTA messages ignored (no such peer)"),
3287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3288 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3289 ntohl (quota.value__), GNUNET_i2s (&n->id));
3290 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3291 if (0 != ntohl (quota.value__))
3293 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3294 "Disconnecting peer `%4s' due to SET_QUOTA\n",
3295 GNUNET_i2s (&n->id));
3296 if (GNUNET_YES == test_connected (n))
3297 GNUNET_STATISTICS_update (GST_stats,
3298 gettext_noop ("# disconnects due to quota of 0"),
3300 disconnect_neighbour (n);
3305 * We received a disconnect message from the given peer,
3306 * validate and process.
3308 * @param peer sender of the message
3309 * @param msg the disconnect message
3312 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity *peer,
3313 const struct GNUNET_MessageHeader *msg)
3315 struct NeighbourMapEntry *n;
3316 const struct SessionDisconnectMessage *sdm;
3318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3319 "Received DISCONNECT message from peer `%s'\n",
3321 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3323 // GNUNET_break_op (0);
3324 GNUNET_STATISTICS_update (GST_stats,
3326 ("# disconnect messages ignored (old format)"), 1,
3330 GNUNET_STATISTICS_update (GST_stats,
3332 ("# DISCONNECT messages received"),
3334 sdm = (const struct SessionDisconnectMessage *) msg;
3335 if (NULL == (n = lookup_neighbour (peer)))
3336 return; /* gone already */
3337 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3339 GNUNET_STATISTICS_update (GST_stats,
3341 ("# disconnect messages ignored (timestamp)"), 1,
3345 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3347 GNUNET_break_op (0);
3350 if (ntohl (sdm->purpose.size) !=
3351 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3352 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3353 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3355 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3356 "%s message from peer `%s' has invalid size \n",
3359 GNUNET_break_op (0);
3363 GNUNET_CRYPTO_eddsa_verify
3364 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3365 &sdm->signature, &sdm->public_key))
3367 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3368 "%s message from peer `%s' cannot be verified \n",
3371 GNUNET_break_op (0);
3374 if (GNUNET_YES == test_connected (n))
3375 GNUNET_STATISTICS_update (GST_stats,
3377 ("# other peer asked to disconnect from us"), 1,
3379 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3380 "Disconnecting by request from peer %s\n",
3382 disconnect_neighbour (n);
3387 * Closure for the neighbours_iterate function.
3389 struct IteratorContext
3392 * Function to call on each connected neighbour.
3394 GST_NeighbourIterator cb;
3404 * Call the callback from the closure for each connected neighbour.
3406 * @param cls the `struct IteratorContext`
3407 * @param key the hash of the public key of the neighbour
3408 * @param value the `struct NeighbourMapEntry`
3409 * @return #GNUNET_OK (continue to iterate)
3412 neighbours_iterate (void *cls,
3413 const struct GNUNET_PeerIdentity *key,
3416 struct IteratorContext *ic = cls;
3417 struct NeighbourMapEntry *n = value;
3418 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3419 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3421 if (GNUNET_YES != test_connected (n))
3424 if (NULL != n->primary_address.address)
3426 bandwidth_in = n->primary_address.bandwidth_in;
3427 bandwidth_out = n->primary_address.bandwidth_out;
3431 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3432 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3434 ic->cb (ic->cb_cls, &n->id,
3435 n->primary_address.address,
3436 bandwidth_in, bandwidth_out);
3442 * Iterate over all connected neighbours.
3444 * @param cb function to call
3445 * @param cb_cls closure for cb
3448 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3450 struct IteratorContext ic;
3452 if (NULL == neighbours)
3453 return; /* can happen during shutdown */
3456 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3461 * If we have an active connection to the given target, it must be shutdown.
3463 * @param target peer to disconnect from
3466 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3468 struct NeighbourMapEntry *n;
3470 if (NULL == (n = lookup_neighbour (target)))
3471 return; /* not active */
3472 if (GNUNET_YES == test_connected (n))
3473 GNUNET_STATISTICS_update (GST_stats,
3475 ("# disconnected from peer upon explicit request"), 1,
3477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3478 "Forced disconnect from peer %s\n",
3479 GNUNET_i2s (target));
3480 disconnect_neighbour (n);
3485 * Obtain current latency information for the given neighbour.
3487 * @param peer to get the latency for
3488 * @return observed latency of the address, FOREVER if the
3489 * the connection is not up
3491 struct GNUNET_TIME_Relative
3492 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3494 struct NeighbourMapEntry *n;
3496 n = lookup_neighbour (peer);
3498 return GNUNET_TIME_UNIT_FOREVER_REL;
3502 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3503 case S_CONNECTED_SWITCHING_BLACKLIST:
3504 case S_RECONNECT_SENT:
3505 case S_RECONNECT_ATS:
3506 case S_RECONNECT_BLACKLIST:
3508 case S_NOT_CONNECTED:
3509 case S_INIT_BLACKLIST:
3511 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3512 case S_CONNECT_RECV_ATS:
3513 case S_CONNECT_RECV_BLACKLIST:
3514 case S_CONNECT_RECV_ACK:
3515 case S_CONNECT_SENT:
3517 case S_DISCONNECT_FINISHED:
3518 return GNUNET_TIME_UNIT_FOREVER_REL;
3520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3521 "Unhandled state `%s'\n",
3522 print_state (n->state));
3526 return GNUNET_TIME_UNIT_FOREVER_REL;
3531 * Obtain current address information for the given neighbour.
3534 * @return address currently used
3536 struct GNUNET_HELLO_Address *
3537 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3539 struct NeighbourMapEntry *n;
3541 n = lookup_neighbour (peer);
3544 return n->primary_address.address;
3549 * Initialize the neighbours subsystem.
3551 * @param cls closure for callbacks
3552 * @param connect_cb function to call if we connect to a peer
3553 * @param disconnect_cb function to call if we disconnect from a peer
3554 * @param peer_address_cb function to call if we change an active address
3556 * @param max_fds maximum number of fds to use
3559 GST_neighbours_start (void *cls,
3560 NotifyConnect connect_cb,
3561 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3562 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3563 unsigned int max_fds)
3566 connect_notify_cb = connect_cb;
3567 disconnect_notify_cb = disconnect_cb;
3568 address_change_cb = peer_address_cb;
3569 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3570 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3571 utilization_transmission, NULL);
3576 * Disconnect from the given neighbour.
3579 * @param key hash of neighbour's public key (not used)
3580 * @param value the 'struct NeighbourMapEntry' of the neighbour
3581 * @return #GNUNET_OK (continue to iterate)
3584 disconnect_all_neighbours (void *cls,
3585 const struct GNUNET_PeerIdentity *key,
3588 struct NeighbourMapEntry *n = value;
3590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3591 "Disconnecting peer `%4s', %s\n",
3592 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3593 n->state = S_DISCONNECT_FINISHED;
3594 free_neighbour (n, GNUNET_NO);
3600 * Cleanup the neighbours subsystem.
3603 GST_neighbours_stop ()
3605 if (NULL == neighbours)
3607 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3609 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3610 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3613 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3614 &disconnect_all_neighbours,
3616 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3618 callback_cls = NULL;
3619 connect_notify_cb = NULL;
3620 disconnect_notify_cb = NULL;
3621 address_change_cb = NULL;
3625 /* end of file gnunet-service-transport_neighbours.c */