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 * - TEST, TEST, TEST...
30 #include "gnunet_ats_service.h"
31 #include "gnunet-service-transport_neighbours.h"
32 #include "gnunet-service-transport_plugins.h"
33 #include "gnunet-service-transport_validation.h"
34 #include "gnunet-service-transport_clients.h"
35 #include "gnunet-service-transport.h"
36 #include "gnunet_peerinfo_service.h"
37 #include "gnunet-service-transport_blacklist.h"
38 #include "gnunet_constants.h"
39 #include "transport.h"
44 * Size of the neighbour hash map.
46 #define NEIGHBOUR_TABLE_SIZE 256
49 * Time we give plugin to transmit DISCONNECT message before the
50 * neighbour entry self-destructs.
52 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
55 * How often must a peer violate bandwidth quotas before we start
56 * to simply drop its messages?
58 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
61 * How often do we send KEEPALIVE messages to each of our neighbours and measure
62 * the latency with this neighbour?
63 * (idle timeout is 5 minutes or 300 seconds, so with 100s interval we
64 * send 3 keepalives in each interval, so 3 messages would need to be
65 * lost in a row for a disconnect).
67 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
70 * How long are we willing to wait for a response from ATS before timing out?
72 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
75 * How long are we willing to wait for an ACK from the other peer before
76 * giving up on our connect operation?
78 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
81 * How long are we willing to wait for a successful reconnect if
82 * an existing connection went down? Much shorter than the
83 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
84 * higher layers about the disconnect during this period.
86 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
89 * How long are we willing to wait for a response from the blacklist
90 * subsystem before timing out?
92 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
95 * Interval to send utilization data
97 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
99 GNUNET_NETWORK_STRUCT_BEGIN
102 * Message a peer sends to another to indicate that it intends to
103 * setup a connection/session for data exchange. A 'SESSION_CONNECT'
104 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
105 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
106 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
107 * should be connected.
109 struct SessionConnectMessage
112 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT
113 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK
115 struct GNUNET_MessageHeader header;
120 uint32_t reserved GNUNET_PACKED;
123 * Absolute time at the sender. Only the most recent connect
124 * message implies which session is preferred by the sender.
126 struct GNUNET_TIME_AbsoluteNBO timestamp;
132 * Message we send to the other peer to notify him that we intentionally
133 * are disconnecting (to reduce timeouts). This is just a friendly
134 * notification, peers must not rely on always receiving disconnect
137 struct SessionDisconnectMessage
140 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
142 struct GNUNET_MessageHeader header;
147 uint32_t reserved GNUNET_PACKED;
150 * Purpose of the signature. Extends over the timestamp.
151 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
153 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
156 * Absolute time at the sender. Only the most recent connect
157 * message implies which session is preferred by the sender.
159 struct GNUNET_TIME_AbsoluteNBO timestamp;
162 * Public key of the sender.
164 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
167 * Signature of the peer that sends us the disconnect. Only
168 * valid if the timestamp is AFTER the timestamp from the
169 * corresponding 'CONNECT' message.
171 struct GNUNET_CRYPTO_EddsaSignature signature;
175 GNUNET_NETWORK_STRUCT_END
179 * For each neighbour we keep a list of messages
180 * that we still want to transmit to the neighbour.
186 * This is a doubly linked list.
188 struct MessageQueue *next;
191 * This is a doubly linked list.
193 struct MessageQueue *prev;
196 * Function to call once we're done.
198 GST_NeighbourSendContinuation cont;
201 * Closure for @e cont
206 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
207 * stuck together in memory. Allocated at the end of this struct.
209 const char *message_buf;
212 * Size of the message buf
214 size_t message_buf_size;
217 * At what time should we fail?
219 struct GNUNET_TIME_Absolute timeout;
225 * Possible state of a neighbour. Initially, we are #S_NOT_CONNECTED.
227 * Then, there are two main paths. If we receive a CONNECT message, we
228 * first run a check against the blacklist (#S_CONNECT_RECV_BLACKLIST_INBOUND).
229 * If this check is successful, we give the inbound address to ATS.
230 * After the check we ask ATS for a suggestion (S_CONNECT_RECV_ATS).
231 * If ATS makes a suggestion, we ALSO give that suggestion to the blacklist
232 * (#S_CONNECT_RECV_BLACKLIST). Once the blacklist approves the
233 * address we got from ATS, we send our CONNECT_ACK and go to
234 * #S_CONNECT_RECV_ACK. If we receive a SESSION_ACK, we go to
235 * #S_CONNECTED (and notify everyone about the new connection). If the
236 * operation times out, we go to #S_DISCONNECT.
238 * The other case is where we transmit a CONNECT message first. We
239 * start with #S_INIT_ATS. If we get an address, we enter
240 * #S_INIT_BLACKLIST and check the blacklist. If the blacklist is OK
241 * with the connection, we actually send the CONNECT message and go to
242 * state S_CONNECT_SENT. Once we receive a CONNECT_ACK, we go to
243 * #S_CONNECTED (and notify everyone about the new connection and send
244 * back a SESSION_ACK). If the operation times out, we go to
247 * If the session is in trouble (i.e. transport-level disconnect or
248 * timeout), we go to #S_RECONNECT_ATS where we ask ATS for a new
249 * address (we don't notify anyone about the disconnect yet). Once we
250 * have a new address, we go to #S_RECONNECT_BLACKLIST to check the new
251 * address against the blacklist. If the blacklist approves, we enter
252 * #S_RECONNECT_SENT and send a CONNECT message. If we receive a
253 * CONNECT_ACK, we go to #S_CONNECTED and nobody noticed that we had
254 * trouble; we also send a SESSION_ACK at this time just in case. If
255 * the operation times out, we go to S_DISCONNECT (and notify everyone
256 * about the lost connection).
258 * If ATS decides to switch addresses while we have a normal
259 * connection, we go to #S_CONNECTED_SWITCHING_BLACKLIST to check the
260 * new address against the blacklist. If the blacklist approves, we
261 * go to #S_CONNECTED_SWITCHING_CONNECT_SENT and send a
262 * SESSION_CONNECT. If we get a SESSION_ACK back, we switch the
263 * primary connection to the suggested alternative from ATS, go back
264 * to #S_CONNECTED and send a SESSION_ACK to the other peer just to be
265 * sure. If the operation times out (or the blacklist disapproves),
266 * we go to #S_CONNECTED (and notify ATS that the given alternative
267 * address is "invalid").
269 * Once a session is in #S_DISCONNECT, it is cleaned up and then goes
270 * to (#S_DISCONNECT_FINISHED). If we receive an explicit disconnect
271 * request, we can go from any state to #S_DISCONNECT, possibly after
272 * generating disconnect notifications.
274 * Note that it is quite possible that while we are in any of these
275 * states, we could receive a 'CONNECT' request from the other peer.
276 * We then enter a 'weird' state where we pursue our own primary state
277 * machine (as described above), but with the 'send_connect_ack' flag
278 * set to 1. If our state machine allows us to send a 'CONNECT_ACK'
279 * (because we have an acceptable address), we send the 'CONNECT_ACK'
280 * and set the 'send_connect_ack' to 2. If we then receive a
281 * 'SESSION_ACK', we go to #S_CONNECTED (and reset 'send_connect_ack'
288 * fresh peer or completely disconnected
293 * Asked to initiate connection, trying to get address from ATS
298 * Asked to initiate connection, trying to get address approved
304 * Sent CONNECT message to other peer, waiting for CONNECT_ACK
309 * Received a CONNECT, do a blacklist check for inbound address
311 S_CONNECT_RECV_BLACKLIST_INBOUND,
314 * Received a CONNECT, asking ATS about address suggestions.
319 * Received CONNECT from other peer, got an address, checking with blacklist.
321 S_CONNECT_RECV_BLACKLIST,
324 * CONNECT request from other peer was SESSION_ACK'ed, waiting for
330 * Got our CONNECT_ACK/SESSION_ACK, connection is up.
335 * Connection got into trouble, rest of the system still believes
336 * it to be up, but we're getting a new address from ATS.
341 * Connection got into trouble, rest of the system still believes
342 * it to be up; we are checking the new address against the blacklist.
344 S_RECONNECT_BLACKLIST,
347 * Sent CONNECT over new address (either by ATS telling us to switch
348 * addresses or from RECONNECT_ATS); if this fails, we need to tell
349 * the rest of the system about a disconnect.
354 * We have some primary connection, but ATS suggested we switch
355 * to some alternative; we're now checking the alternative against
358 S_CONNECTED_SWITCHING_BLACKLIST,
361 * We have some primary connection, but ATS suggested we switch
362 * to some alternative; we now sent a CONNECT message for the
363 * alternative session to the other peer and waiting for a
364 * CONNECT_ACK to make this our primary connection.
366 S_CONNECTED_SWITCHING_CONNECT_SENT,
369 * Disconnect in progress (we're sending the DISCONNECT message to the
370 * other peer; after that is finished, the state will be cleaned up).
375 * We're finished with the disconnect; and are cleaning up the state
376 * now! We put the struct into this state when we are really in the
377 * task that calls 'free' on it and are about to remove the record
378 * from the map. We should never find a 'struct NeighbourMapEntry'
379 * in this state in the map. Accessing a 'struct NeighbourMapEntry'
380 * in this state virtually always means using memory that has been
381 * freed (the exception being the cleanup code in #free_neighbour()).
383 S_DISCONNECT_FINISHED
388 * A possible address we could use to communicate with a neighbour.
390 struct NeighbourAddress
394 * Active session for this address.
396 struct Session *session;
399 * Network-level address information.
401 struct GNUNET_HELLO_Address *address;
404 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
405 * peer for this address. Use to check that the ACK is in response
406 * to our most recent 'CONNECT'.
408 struct GNUNET_TIME_Absolute connect_timestamp;
411 * Inbound bandwidth from ATS for this address.
413 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
416 * Outbound bandwidth from ATS for this address.
418 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
421 * Did we tell ATS that this is our 'active' address?
429 * Entry in neighbours.
431 struct NeighbourMapEntry
435 * Head of list of messages we would like to send to this peer;
436 * must contain at most one message per client.
438 struct MessageQueue *messages_head;
441 * Tail of list of messages we would like to send to this peer; must
442 * contain at most one message per client.
444 struct MessageQueue *messages_tail;
447 * Are we currently trying to send a message? If so, which one?
449 struct MessageQueue *is_active;
452 * Primary address we currently use to communicate with the neighbour.
454 struct NeighbourAddress primary_address;
457 * Alternative address currently under consideration for communicating
458 * with the neighbour.
460 struct NeighbourAddress alternative_address;
463 * Identity of this neighbour.
465 struct GNUNET_PeerIdentity id;
468 * Main task that drives this peer (timeouts, keepalives, etc.).
469 * Always runs the 'master_task'.
471 GNUNET_SCHEDULER_TaskIdentifier task;
474 * At what time should we sent the next keep-alive message?
476 struct GNUNET_TIME_Absolute keep_alive_time;
479 * At what time did we sent the last keep-alive message? Used
480 * to calculate round-trip time ("latency").
482 struct GNUNET_TIME_Absolute last_keep_alive_time;
485 * Timestamp we should include in our next CONNECT_ACK message.
486 * (only valid if 'send_connect_ack' is GNUNET_YES). Used to build
487 * our CONNECT_ACK message.
489 struct GNUNET_TIME_Absolute connect_ack_timestamp;
492 * ATS address suggest handle
494 struct GNUNET_ATS_SuggestHandle *suggest_handle;
497 * Time where we should cut the connection (timeout) if we don't
498 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
499 * if we are in S_CONNECTED).
501 struct GNUNET_TIME_Absolute timeout;
504 * Latest calculated latency value
506 struct GNUNET_TIME_Relative latency;
509 * Tracker for inbound bandwidth.
511 struct GNUNET_BANDWIDTH_Tracker in_tracker;
514 * How often has the other peer (recently) violated the inbound
515 * traffic limit? Incremented by 10 per violation, decremented by 1
516 * per non-violation (for each time interval).
518 unsigned int quota_violation_count;
521 * The current state of the peer.
526 * Did we sent an KEEP_ALIVE message and are we expecting a response?
528 int expect_latency_response;
531 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
532 * (once we have an address to use and the peer has been allowed by our
533 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
534 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
535 * a 'SESSION_ACK' (regardless of what our own state machine might say).
537 int send_connect_ack;
540 * Tracking utilization of outbound bandwidth
542 uint32_t util_payload_bytes_sent;
545 * Tracking utilization of inbound bandwidth
547 uint32_t util_payload_bytes_recv;
550 * Tracking utilization of outbound bandwidth
552 uint32_t util_total_bytes_sent;
555 * Tracking utilization of inbound bandwidth
557 uint32_t util_total_bytes_recv;
560 * Date of last utilization transmission
562 struct GNUNET_TIME_Absolute last_util_transmission;
567 * Context for blacklist checks and the 'handle_test_blacklist_cont'
568 * function. Stores information about ongoing blacklist checks.
570 struct BlackListCheckContext
574 * We keep blacklist checks in a DLL.
576 struct BlackListCheckContext *next;
579 * We keep blacklist checks in a DLL.
581 struct BlackListCheckContext *prev;
584 * Address that is being checked.
586 struct NeighbourAddress na;
589 * Handle to the ongoing blacklist check.
591 struct GST_BlacklistCheck *bc;
596 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
598 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
601 * We keep blacklist checks in a DLL so that we can find
602 * the 'sessions' in their 'struct NeighbourAddress' if
603 * a session goes down.
605 static struct BlackListCheckContext *bc_head;
608 * We keep blacklist checks in a DLL.
610 static struct BlackListCheckContext *bc_tail;
613 * Closure for #connect_notify_cb, #disconnect_notify_cb and #address_change_cb
615 static void *callback_cls;
618 * Function to call when we connected to a neighbour.
620 static NotifyConnect connect_notify_cb;
623 * Function to call when we disconnected from a neighbour.
625 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
628 * Function to call when we changed an active address of a neighbour.
630 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
633 * counter for connected neighbours
635 static unsigned int neighbours_connected;
638 * Number of bytes we have currently queued for transmission.
640 static unsigned long long bytes_in_send_queue;
643 * Task transmitting utilization data
645 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
649 * Lookup a neighbour entry in the neighbours hash map.
651 * @param pid identity of the peer to look up
652 * @return the entry, NULL if there is no existing record
654 static struct NeighbourMapEntry *
655 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
657 if (NULL == neighbours)
659 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
664 print_state (int state)
669 case S_NOT_CONNECTED:
670 return "S_NOT_CONNECTED";
673 case S_INIT_BLACKLIST:
674 return "S_INIT_BLACKLIST";
676 return "S_CONNECT_SENT";
677 case S_CONNECT_RECV_BLACKLIST_INBOUND:
678 return "S_CONNECT_RECV_BLACKLIST_INBOUND";
679 case S_CONNECT_RECV_ATS:
680 return "S_CONNECT_RECV_ATS";
681 case S_CONNECT_RECV_BLACKLIST:
682 return "S_CONNECT_RECV_BLACKLIST";
683 case S_CONNECT_RECV_ACK:
684 return "S_CONNECT_RECV_ACK";
686 return "S_CONNECTED";
687 case S_RECONNECT_ATS:
688 return "S_RECONNECT_ATS";
689 case S_RECONNECT_BLACKLIST:
690 return "S_RECONNECT_BLACKLIST";
691 case S_RECONNECT_SENT:
692 return "S_RECONNECT_SENT";
693 case S_CONNECTED_SWITCHING_BLACKLIST:
694 return "S_CONNECTED_SWITCHING_BLACKLIST";
695 case S_CONNECTED_SWITCHING_CONNECT_SENT:
696 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
698 return "S_DISCONNECT";
699 case S_DISCONNECT_FINISHED:
700 return "S_DISCONNECT_FINISHED";
708 * Test if we're connected to the given peer.
710 * @param n neighbour entry of peer to test
711 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
714 test_connected (struct NeighbourMapEntry *n)
720 case S_NOT_CONNECTED:
722 case S_INIT_BLACKLIST:
724 case S_CONNECT_RECV_BLACKLIST_INBOUND:
725 case S_CONNECT_RECV_ATS:
726 case S_CONNECT_RECV_BLACKLIST:
727 case S_CONNECT_RECV_ACK:
730 case S_RECONNECT_ATS:
731 case S_RECONNECT_BLACKLIST:
732 case S_RECONNECT_SENT:
733 case S_CONNECTED_SWITCHING_BLACKLIST:
734 case S_CONNECTED_SWITCHING_CONNECT_SENT:
737 case S_DISCONNECT_FINISHED:
740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
741 "Unhandled state `%s' \n",
742 print_state (n->state));
746 return GNUNET_SYSERR;
750 * Send information about a new outbound quota to our clients.
752 * @param target affected peer
753 * @param quota new quota
756 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
757 struct GNUNET_BANDWIDTH_Value32NBO quota)
759 struct QuotaSetMessage q_msg;
761 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
762 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
763 ntohl (quota.value__), GNUNET_i2s (target));
764 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
765 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
767 q_msg.peer = (*target);
768 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
773 * We don't need a given neighbour address any more.
774 * Release its resources and give appropriate notifications
775 * to ATS and other subsystems.
777 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
780 free_address (struct NeighbourAddress *na)
782 if (GNUNET_YES == na->ats_active)
784 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
785 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
786 address_change_cb (callback_cls, &na->address->peer, NULL);
789 na->ats_active = GNUNET_NO;
790 if (NULL != na->address)
792 GNUNET_HELLO_address_free (na->address);
800 * Initialize the 'struct NeighbourAddress'.
802 * @param na neighbour address to initialize
803 * @param address address of the other peer, NULL if other peer
805 * @param session session to use (or NULL, in which case an
806 * address must be setup)
807 * @param bandwidth_in inbound quota to be used when connection is up
808 * @param bandwidth_out outbound quota to be used when connection is up
809 * @param is_active #GNUNET_YES to mark this as the active address with ATS
812 set_address (struct NeighbourAddress *na,
813 const struct GNUNET_HELLO_Address *address,
814 struct Session *session,
815 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
816 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
819 struct GNUNET_TRANSPORT_PluginFunctions *papi;
821 if (NULL == (papi = GST_plugins_find (address->transport_name)))
826 if (session == na->session)
828 na->bandwidth_in = bandwidth_in;
829 na->bandwidth_out = bandwidth_out;
830 if (is_active != na->ats_active)
832 na->ats_active = is_active;
833 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
834 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
836 address_change_cb (callback_cls, &address->peer, address);
838 if (GNUNET_YES == is_active)
840 /* FIXME: is this the right place to set quotas? */
841 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
842 send_outbound_quota (&address->peer, bandwidth_out);
848 session = papi->get_session (papi->cls, address);
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Failed to obtain new session for peer `%s' and address '%s'\n",
853 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
854 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
857 na->address = GNUNET_HELLO_address_copy (address);
858 na->bandwidth_in = bandwidth_in;
859 na->bandwidth_out = bandwidth_out;
860 na->session = session;
861 na->ats_active = is_active;
862 if (GNUNET_YES == is_active)
864 /* Telling ATS about new session */
865 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
866 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
867 address_change_cb (callback_cls, &address->peer, address);
868 /* FIXME: is this the right place to set quotas? */
869 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
870 send_outbound_quota (&address->peer, bandwidth_out);
876 * Free a neighbour map entry.
878 * @param n entry to free
879 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
880 * #GNUNET_YES to keep all sessions
883 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
885 struct MessageQueue *mq;
886 struct GNUNET_TRANSPORT_PluginFunctions *papi;
887 struct GNUNET_HELLO_Address *backup_primary;
889 n->is_active = NULL; /* always free'd by its own continuation! */
891 /* fail messages currently in the queue */
892 while (NULL != (mq = n->messages_head))
894 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
895 if (NULL != mq->cont)
896 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
899 /* It is too late to send other peer disconnect notifications, but at
900 least internally we need to get clean... */
901 if (GNUNET_YES == test_connected (n))
903 GNUNET_STATISTICS_set (GST_stats,
904 gettext_noop ("# peers connected"),
905 --neighbours_connected,
907 disconnect_notify_cb (callback_cls, &n->id);
909 n->state = S_DISCONNECT_FINISHED;
911 if (NULL != n->primary_address.address)
913 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
916 backup_primary = NULL;
918 /* free addresses and mark as unused */
919 free_address (&n->primary_address);
920 free_address (&n->alternative_address);
922 /* FIXME: Note that if we are switching between two TCP sessions to
923 the same peer, we might want to selectively kill only one of
924 them! Killing all sessions like this seems to be very, very
927 /* cut transport-level connection */
928 if ((GNUNET_NO == keep_sessions) &&
929 (NULL != backup_primary) &&
930 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
931 papi->disconnect_peer (papi->cls, &n->id);
933 GNUNET_free_non_null (backup_primary);
935 GNUNET_assert (GNUNET_YES ==
936 GNUNET_CONTAINER_multipeermap_remove (neighbours,
939 // FIXME-ATS-API: we might want to be more specific about
940 // which states we do this from in the future (ATS should
941 // have given us a 'suggest_address' handle, and if we have
942 // such a handle, we should cancel the operation here!
943 if (NULL != n->suggest_handle)
945 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
946 n->suggest_handle = NULL;
949 if (GNUNET_SCHEDULER_NO_TASK != n->task)
951 GNUNET_SCHEDULER_cancel (n->task);
952 n->task = GNUNET_SCHEDULER_NO_TASK;
954 /* free rest of memory */
960 * Transmit a message using the current session of the given
963 * @param n entry for the recipient
964 * @param msgbuf buffer to transmit
965 * @param msgbuf_size number of bytes in buffer
966 * @param priority transmission priority
967 * @param timeout transmission timeout
968 * @param cont continuation to call when finished (can be NULL)
969 * @param cont_cls closure for cont
972 send_with_session (struct NeighbourMapEntry *n,
973 const char *msgbuf, size_t msgbuf_size,
975 struct GNUNET_TIME_Relative timeout,
976 GNUNET_TRANSPORT_TransmitContinuation cont,
979 struct GNUNET_TRANSPORT_PluginFunctions *papi;
981 GNUNET_assert (n->primary_address.session != NULL);
982 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
983 (-1 == papi->send (papi->cls,
984 n->primary_address.session,
988 cont, cont_cls)))) &&
990 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
991 GST_neighbours_notify_data_sent (&n->id,
992 n->primary_address.address, n->primary_address.session, msgbuf_size);
993 GNUNET_break (NULL != papi);
998 * Master task run for every neighbour. Performs all of the time-related
999 * activities (keep alive, send next message, disconnect if idle, finish
1000 * clean up after disconnect).
1002 * @param cls the 'struct NeighbourMapEntry' for which we are running
1003 * @param tc scheduler context (unused)
1006 master_task (void *cls,
1007 const struct GNUNET_SCHEDULER_TaskContext *tc);
1011 * Function called when the 'DISCONNECT' message has been sent by the
1012 * plugin. Frees the neighbour --- if the entry still exists.
1015 * @param target identity of the neighbour that was disconnected
1016 * @param result #GNUNET_OK if the disconnect got out successfully
1017 * @param payload bytes payload
1018 * @param physical bytes physical
1021 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1022 int result, size_t payload, size_t physical)
1024 struct NeighbourMapEntry *n;
1026 n = lookup_neighbour (target);
1028 return; /* already gone */
1029 if (S_DISCONNECT != n->state)
1030 return; /* have created a fresh entry since */
1031 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1032 GNUNET_SCHEDULER_cancel (n->task);
1033 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1038 * Transmit a DISCONNECT message to the other peer.
1040 * @param n neighbour to send DISCONNECT message.
1043 send_disconnect (struct NeighbourMapEntry *n)
1045 struct SessionDisconnectMessage disconnect_msg;
1047 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1048 "Sending DISCONNECT message to peer `%4s'\n",
1049 GNUNET_i2s (&n->id));
1050 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1051 disconnect_msg.header.type =
1052 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1053 disconnect_msg.reserved = htonl (0);
1054 disconnect_msg.purpose.size =
1055 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1056 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1057 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1058 disconnect_msg.purpose.purpose =
1059 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1060 disconnect_msg.timestamp =
1061 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1062 disconnect_msg.public_key = GST_my_identity.public_key;
1063 GNUNET_assert (GNUNET_OK ==
1064 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1065 &disconnect_msg.purpose,
1066 &disconnect_msg.signature));
1068 send_with_session (n,
1069 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1070 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1071 &send_disconnect_cont, NULL);
1072 GNUNET_STATISTICS_update (GST_stats,
1074 ("# DISCONNECT messages sent"), 1,
1080 * Disconnect from the given neighbour, clean up the record.
1082 * @param n neighbour to disconnect from
1085 disconnect_neighbour (struct NeighbourMapEntry *n)
1087 /* depending on state, notify neighbour and/or upper layers of this peer
1091 case S_NOT_CONNECTED:
1093 case S_INIT_BLACKLIST:
1094 /* other peer is completely unaware of us, no need to send DISCONNECT */
1095 n->state = S_DISCONNECT_FINISHED;
1096 free_neighbour (n, GNUNET_NO);
1098 case S_CONNECT_SENT:
1099 send_disconnect (n);
1100 n->state = S_DISCONNECT;
1102 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1103 case S_CONNECT_RECV_ATS:
1104 case S_CONNECT_RECV_BLACKLIST:
1105 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1106 n->state = S_DISCONNECT_FINISHED;
1107 free_neighbour (n, GNUNET_NO);
1109 case S_CONNECT_RECV_ACK:
1110 /* we DID ACK the other peer's request, must send DISCONNECT */
1111 send_disconnect (n);
1112 n->state = S_DISCONNECT;
1115 case S_RECONNECT_BLACKLIST:
1116 case S_RECONNECT_SENT:
1117 case S_CONNECTED_SWITCHING_BLACKLIST:
1118 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1119 /* we are currently connected, need to send disconnect and do
1120 internal notifications and update statistics */
1121 send_disconnect (n);
1122 GNUNET_STATISTICS_set (GST_stats,
1123 gettext_noop ("# peers connected"),
1124 --neighbours_connected,
1126 disconnect_notify_cb (callback_cls, &n->id);
1127 n->state = S_DISCONNECT;
1129 case S_RECONNECT_ATS:
1130 /* ATS address request timeout, disconnect without sending disconnect message */
1131 GNUNET_STATISTICS_set (GST_stats,
1132 gettext_noop ("# peers connected"),
1133 --neighbours_connected,
1135 disconnect_notify_cb (callback_cls, &n->id);
1136 n->state = S_DISCONNECT;
1139 /* already disconnected, ignore */
1141 case S_DISCONNECT_FINISHED:
1142 /* already cleaned up, how did we get here!? */
1146 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1147 "Unhandled state `%s'\n",
1148 print_state (n->state));
1152 /* schedule timeout to clean up */
1153 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1154 GNUNET_SCHEDULER_cancel (n->task);
1155 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1161 * We're done with our transmission attempt, continue processing.
1163 * @param cls the 'struct MessageQueue' of the message
1164 * @param receiver intended receiver
1165 * @param success whether it worked or not
1166 * @param size_payload bytes payload sent
1167 * @param physical bytes sent on wire
1170 transmit_send_continuation (void *cls,
1171 const struct GNUNET_PeerIdentity *receiver,
1172 int success, size_t size_payload, size_t physical)
1174 struct MessageQueue *mq = cls;
1175 struct NeighbourMapEntry *n;
1177 if (NULL == (n = lookup_neighbour (receiver)))
1180 return; /* disconnect or other error while transmitting, can happen */
1182 if (n->is_active == mq)
1184 /* this is still "our" neighbour, remove us from its queue
1185 and allow it to send the next message now */
1186 n->is_active = NULL;
1187 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1188 GNUNET_SCHEDULER_cancel (n->task);
1189 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1191 if (bytes_in_send_queue < mq->message_buf_size)
1193 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1194 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1195 bytes_in_send_queue, mq->message_buf_size,
1196 (GNUNET_OK == success) ? "OK" : "FAIL",
1197 size_payload, physical);
1202 GNUNET_break (size_payload == mq->message_buf_size);
1203 bytes_in_send_queue -= mq->message_buf_size;
1204 GNUNET_STATISTICS_set (GST_stats,
1206 ("# bytes in message queue for other peers"),
1207 bytes_in_send_queue, GNUNET_NO);
1208 if (GNUNET_OK == success)
1209 GNUNET_STATISTICS_update (GST_stats,
1211 ("# messages transmitted to other peers"),
1214 GNUNET_STATISTICS_update (GST_stats,
1216 ("# transmission failures for messages to other peers"),
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "Sending message to `%s' of type %u was a %s\n",
1220 GNUNET_i2s (receiver),
1221 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1222 (success == GNUNET_OK) ? "success" : "FAILURE");
1223 if (NULL != mq->cont)
1224 mq->cont (mq->cont_cls, success, size_payload, physical);
1230 * Check the message list for the given neighbour and if we can
1231 * send a message, do so. This function should only be called
1232 * if the connection is at least generally ready for transmission.
1233 * While we will only send one message at a time, no bandwidth
1234 * quota management is performed here. If a message was given to
1235 * the plugin, the continuation will automatically re-schedule
1236 * the 'master' task once the next message might be transmitted.
1238 * @param n target peer for which to transmit
1241 try_transmission_to_peer (struct NeighbourMapEntry *n)
1243 struct MessageQueue *mq;
1244 struct GNUNET_TIME_Relative timeout;
1246 if (NULL == n->primary_address.address)
1248 /* no address, why are we here? */
1252 if ((0 == n->primary_address.address->address_length) &&
1253 (NULL == n->primary_address.session))
1255 /* no address, why are we here? */
1259 if (NULL != n->is_active)
1261 /* transmission already pending */
1265 /* timeout messages from the queue that are past their due date */
1266 while (NULL != (mq = n->messages_head))
1268 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1269 if (timeout.rel_value_us > 0)
1271 GNUNET_STATISTICS_update (GST_stats,
1273 ("# messages timed out while in transport queue"),
1275 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1277 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0); /* timeout */
1280 return; /* no more messages */
1281 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1283 send_with_session (n,
1284 mq->message_buf, mq->message_buf_size,
1285 0 /* priority */, timeout,
1286 &transmit_send_continuation, mq);
1291 * Send keepalive message to the neighbour. Must only be called
1292 * if we are on 'connected' state or while trying to switch addresses.
1293 * Will internally determine if a keepalive is truly needed (so can
1294 * always be called).
1296 * @param n neighbour that went idle and needs a keepalive
1299 send_keepalive (struct NeighbourMapEntry *n)
1301 struct GNUNET_MessageHeader m;
1303 GNUNET_assert ((S_CONNECTED == n->state) ||
1304 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1305 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1306 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1307 return; /* no keepalive needed at this time */
1308 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1309 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1310 send_with_session (n,
1311 (const void *) &m, sizeof (m),
1312 UINT32_MAX /* priority */,
1313 KEEPALIVE_FREQUENCY,
1315 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1317 n->expect_latency_response = GNUNET_YES;
1318 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1319 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1324 * Keep the connection to the given neighbour alive longer,
1325 * we received a KEEPALIVE (or equivalent); send a response.
1327 * @param neighbour neighbour to keep alive (by sending keep alive response)
1330 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1332 struct NeighbourMapEntry *n;
1333 struct GNUNET_MessageHeader m;
1335 if (NULL == (n = lookup_neighbour (neighbour)))
1337 GNUNET_STATISTICS_update (GST_stats,
1339 ("# KEEPALIVE messages discarded (peer unknown)"),
1343 if (NULL == n->primary_address.session)
1345 GNUNET_STATISTICS_update (GST_stats,
1347 ("# KEEPALIVE messages discarded (no session)"),
1351 /* send reply to allow neighbour to measure latency */
1352 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1353 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1354 send_with_session(n,
1355 (const void *) &m, sizeof (m),
1356 UINT32_MAX /* priority */,
1357 KEEPALIVE_FREQUENCY,
1363 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1364 * latency to this peer. Pass the updated information (existing ats
1365 * plus calculated latency) to ATS.
1367 * @param neighbour neighbour to keep alive
1370 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
1372 struct NeighbourMapEntry *n;
1374 struct GNUNET_ATS_Information ats;
1376 if (NULL == (n = lookup_neighbour (neighbour)))
1378 GNUNET_STATISTICS_update (GST_stats,
1380 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1384 if ( (S_CONNECTED != n->state) ||
1385 (GNUNET_YES != n->expect_latency_response) )
1387 GNUNET_STATISTICS_update (GST_stats,
1389 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1393 n->expect_latency_response = GNUNET_NO;
1394 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1395 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Latency for peer `%s' is %s\n",
1398 GNUNET_i2s (&n->id),
1399 GNUNET_STRINGS_relative_time_to_string (n->latency,
1401 /* append latency */
1402 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1403 if (n->latency.rel_value_us > UINT32_MAX)
1404 latency = UINT32_MAX;
1406 latency = n->latency.rel_value_us;
1407 ats.value = htonl (latency);
1408 GST_ats_update_metrics (&n->id,
1409 n->primary_address.address,
1410 n->primary_address.session,
1416 * We have received a message from the given sender. How long should
1417 * we delay before receiving more? (Also used to keep the peer marked
1420 * @param sender sender of the message
1421 * @param size size of the message
1422 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1423 * GNUNET_NO if the neighbour is not connected or violates the quota,
1424 * GNUNET_SYSERR if the connection is not fully up yet
1425 * @return how long to wait before reading more from this sender
1427 struct GNUNET_TIME_Relative
1428 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1429 *sender, ssize_t size, int *do_forward)
1431 struct NeighbourMapEntry *n;
1432 struct GNUNET_TIME_Relative ret;
1434 if (NULL == neighbours)
1436 *do_forward = GNUNET_NO;
1437 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1439 if (NULL == (n = lookup_neighbour (sender)))
1441 GST_neighbours_try_connect (sender);
1442 if (NULL == (n = lookup_neighbour (sender)))
1444 GNUNET_STATISTICS_update (GST_stats,
1446 ("# messages discarded due to lack of neighbour record"),
1448 *do_forward = GNUNET_NO;
1449 return GNUNET_TIME_UNIT_ZERO;
1452 if (! test_connected (n))
1454 *do_forward = GNUNET_SYSERR;
1455 return GNUNET_TIME_UNIT_ZERO;
1457 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1459 n->quota_violation_count++;
1460 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1461 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1462 n->in_tracker.available_bytes_per_s__,
1463 n->quota_violation_count);
1464 /* Discount 32k per violation */
1465 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1469 if (n->quota_violation_count > 0)
1471 /* try to add 32k back */
1472 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1473 n->quota_violation_count--;
1476 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1478 GNUNET_STATISTICS_update (GST_stats,
1480 ("# bandwidth quota violations by other peers"),
1482 *do_forward = GNUNET_NO;
1483 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1485 *do_forward = GNUNET_YES;
1486 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1487 if (ret.rel_value_us > 0)
1489 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1490 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1491 (unsigned long long) n->in_tracker.
1492 consumption_since_last_update__,
1493 (unsigned int) n->in_tracker.available_bytes_per_s__,
1494 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1495 GNUNET_STATISTICS_update (GST_stats,
1496 gettext_noop ("# ms throttling suggested"),
1497 (int64_t) ret.rel_value_us / 1000LL,
1505 * Transmit a message to the given target using the active connection.
1507 * @param target destination
1508 * @param msg message to send
1509 * @param msg_size number of bytes in msg
1510 * @param timeout when to fail with timeout
1511 * @param cont function to call when done
1512 * @param cont_cls closure for 'cont'
1515 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1516 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1517 GST_NeighbourSendContinuation cont, void *cont_cls)
1519 struct NeighbourMapEntry *n;
1520 struct MessageQueue *mq;
1522 /* All ove these cases should never happen; they are all API violations.
1523 But we check anyway, just to be sure. */
1524 if (NULL == (n = lookup_neighbour (target)))
1528 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1531 if (GNUNET_YES != test_connected (n))
1535 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1538 bytes_in_send_queue += msg_size;
1539 GNUNET_STATISTICS_set (GST_stats,
1541 ("# bytes in message queue for other peers"),
1542 bytes_in_send_queue, GNUNET_NO);
1543 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1545 mq->cont_cls = cont_cls;
1546 memcpy (&mq[1], msg, msg_size);
1547 mq->message_buf = (const char *) &mq[1];
1548 mq->message_buf_size = msg_size;
1549 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1550 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1551 if ( (NULL != n->is_active) ||
1552 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1554 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1555 GNUNET_SCHEDULER_cancel (n->task);
1556 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1561 * Send a SESSION_CONNECT message via the given address.
1563 * @param na address to use
1566 send_session_connect (struct NeighbourAddress *na)
1568 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1569 struct SessionConnectMessage connect_msg;
1571 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1576 if (NULL == na->session)
1577 na->session = papi->get_session (papi->cls, na->address);
1578 if (NULL == na->session)
1583 GNUNET_STATISTICS_update (GST_stats,
1585 ("# SESSION_CONNECT messages sent"),
1587 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1588 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1589 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1590 connect_msg.reserved = htonl (0);
1591 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1593 papi->send (papi->cls,
1595 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1597 GNUNET_TIME_UNIT_FOREVER_REL,
1600 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1601 _("Failed to transmit CONNECT message via plugin to %s\n"),
1602 GST_plugins_a2s (na->address));
1604 GST_neighbours_notify_data_sent (&na->address->peer,
1607 sizeof (struct SessionConnectMessage));
1613 * Send a SESSION_CONNECT_ACK message via the given address.
1615 * @param address address to use
1616 * @param session session to use
1617 * @param timestamp timestamp to use for the ACK message
1620 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1621 struct Session *session,
1622 struct GNUNET_TIME_Absolute timestamp)
1624 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1625 struct SessionConnectMessage connect_msg;
1627 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1632 if (NULL == session)
1633 session = papi->get_session (papi->cls, address);
1634 if (NULL == session)
1639 GNUNET_STATISTICS_update (GST_stats,
1641 ("# CONNECT_ACK messages sent"),
1643 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1644 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1645 connect_msg.reserved = htonl (0);
1646 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1647 (void) papi->send (papi->cls,
1649 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1651 GNUNET_TIME_UNIT_FOREVER_REL,
1658 * Create a fresh entry in the neighbour map for the given peer
1660 * @param peer peer to create an entry for
1661 * @return new neighbour map entry
1663 static struct NeighbourMapEntry *
1664 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1666 struct NeighbourMapEntry *n;
1668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1669 "Creating new neighbour entry for `%s'\n",
1671 n = GNUNET_new (struct NeighbourMapEntry);
1673 n->state = S_NOT_CONNECTED;
1674 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1675 n->last_util_transmission = GNUNET_TIME_absolute_get();
1676 n->util_payload_bytes_recv = 0;
1677 n->util_payload_bytes_sent = 0;
1678 n->util_total_bytes_recv = 0;
1679 n->util_total_bytes_sent = 0;
1680 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1681 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1682 MAX_BANDWIDTH_CARRY_S);
1683 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1684 GNUNET_assert (GNUNET_OK ==
1685 GNUNET_CONTAINER_multipeermap_put (neighbours,
1687 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1693 * Check if the two given addresses are the same.
1694 * Actually only checks if the sessions are non-NULL
1695 * (which they should be) and then if they are identical;
1696 * the actual addresses don't matter if the session
1697 * pointers match anyway, and we must have session pointers
1700 * @param a1 first address to compare
1701 * @param a2 other address to compare
1702 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1705 address_matches (const struct NeighbourAddress *a1,
1706 const struct NeighbourAddress *a2)
1708 if ( (NULL == a1->session) ||
1709 (NULL == a2->session) )
1714 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1719 * Try to create a connection to the given target (eventually).
1721 * @param target peer to try to connect to
1724 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1726 struct NeighbourMapEntry *n;
1728 if (NULL == neighbours)
1730 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1731 "Asked to connect to peer `%s' during shutdown\n",
1732 GNUNET_i2s (target));
1733 return; /* during shutdown, do nothing */
1735 n = lookup_neighbour (target);
1736 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1737 "Asked to connect to peer `%s' (state: %s)\n",
1738 GNUNET_i2s (target),
1739 (NULL != n) ? print_state(n->state) : "UNKNOWN PEER");
1744 case S_NOT_CONNECTED:
1745 /* this should not be possible */
1747 free_neighbour (n, GNUNET_NO);
1750 case S_INIT_BLACKLIST:
1751 case S_CONNECT_SENT:
1752 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1753 case S_CONNECT_RECV_ATS:
1754 case S_CONNECT_RECV_BLACKLIST:
1755 case S_CONNECT_RECV_ACK:
1756 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1757 "Ignoring request to try to connect to `%s', already trying!\n",
1758 GNUNET_i2s (target));
1759 return; /* already trying */
1761 case S_RECONNECT_ATS:
1762 case S_RECONNECT_BLACKLIST:
1763 case S_RECONNECT_SENT:
1764 case S_CONNECTED_SWITCHING_BLACKLIST:
1765 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1766 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1767 "Ignoring request to try to connect, already connected to `%s'!\n",
1768 GNUNET_i2s (target));
1769 return; /* already connected */
1771 /* get rid of remains, ready to re-try immediately */
1772 free_neighbour (n, GNUNET_NO);
1774 case S_DISCONNECT_FINISHED:
1775 /* should not be possible */
1778 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1779 "Unhandled state `%s'\n",
1780 print_state (n->state));
1782 free_neighbour (n, GNUNET_NO);
1786 n = setup_neighbour (target);
1787 n->state = S_INIT_ATS;
1788 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1790 GNUNET_ATS_reset_backoff (GST_ats, target);
1791 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1796 * Function called with the result of a blacklist check.
1798 * @param cls closure with the 'struct BlackListCheckContext'
1799 * @param peer peer this check affects
1800 * @param result #GNUNET_OK if the address is allowed
1803 handle_test_blacklist_cont (void *cls,
1804 const struct GNUNET_PeerIdentity *peer,
1807 struct BlackListCheckContext *bcc = cls;
1808 struct NeighbourMapEntry *n;
1811 GNUNET_CONTAINER_DLL_remove (bc_head,
1814 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1815 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1817 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1818 if (GNUNET_OK == result)
1819 GST_ats_add_address (bcc->na.address, bcc->na.session);
1822 /* Blacklist disagreed on connecting to a peer with this address
1823 * Destroy address because we are not allowed to use it
1825 if (NULL != bcc->na.session)
1826 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1827 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1829 if (NULL == (n = lookup_neighbour (peer)))
1830 goto cleanup; /* nobody left to care about new address */
1833 case S_NOT_CONNECTED:
1834 /* this should not be possible */
1836 free_neighbour (n, GNUNET_NO);
1839 /* waiting on ATS suggestion; still, pass address to ATS as a
1842 case S_INIT_BLACKLIST:
1843 /* check if the address the blacklist was fine with matches
1844 ATS suggestion, if so, we can move on! */
1845 if ( (GNUNET_OK == result) &&
1846 (1 == n->send_connect_ack) )
1848 n->send_connect_ack = 2;
1849 send_session_connect_ack_message (bcc->na.address,
1851 n->connect_ack_timestamp);
1853 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1854 break; /* result for an address we currently don't care about */
1855 if (GNUNET_OK == result)
1857 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1858 n->state = S_CONNECT_SENT;
1859 send_session_connect (&n->primary_address);
1863 free_address (&n->primary_address);
1864 n->state = S_INIT_ATS;
1865 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1868 case S_CONNECT_SENT:
1869 /* waiting on CONNECT_ACK, send ACK if one is pending */
1870 if ( (GNUNET_OK == result) &&
1871 (1 == n->send_connect_ack) )
1873 n->send_connect_ack = 2;
1874 send_session_connect_ack_message (n->primary_address.address,
1875 n->primary_address.session,
1876 n->connect_ack_timestamp);
1879 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1880 n->state = S_CONNECT_RECV_ATS;
1881 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1882 GNUNET_ATS_reset_backoff (GST_ats, peer);
1883 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
1885 case S_CONNECT_RECV_ATS:
1886 /* waiting on ATS suggestion, don't care about blacklist */
1888 case S_CONNECT_RECV_BLACKLIST:
1889 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1891 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1892 "Blacklist result ignored, as it is not for our primary address\n");
1893 break; /* result for an address we currently don't care about */
1895 if (GNUNET_OK == result)
1897 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1898 n->state = S_CONNECT_RECV_ACK;
1899 send_session_connect_ack_message (bcc->na.address,
1901 n->connect_ack_timestamp);
1902 if (1 == n->send_connect_ack)
1903 n->send_connect_ack = 2;
1907 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
1909 plugin = GST_plugins_find (bcc->na.address->transport_name);
1910 if ( (NULL != plugin) &&
1911 (NULL != bcc->na.session) )
1913 plugin->disconnect_session (plugin->cls,
1917 GNUNET_break (NULL != plugin);
1918 free_address (&n->primary_address);
1919 n->state = S_INIT_ATS;
1920 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1921 GNUNET_ATS_reset_backoff (GST_ats, peer);
1924 case S_CONNECT_RECV_ACK:
1925 /* waiting on SESSION_ACK, send ACK if one is pending */
1926 if ( (GNUNET_OK == result) &&
1927 (1 == n->send_connect_ack) )
1929 n->send_connect_ack = 2;
1930 send_session_connect_ack_message (n->primary_address.address,
1931 n->primary_address.session,
1932 n->connect_ack_timestamp);
1936 /* already connected, don't care about blacklist */
1938 case S_RECONNECT_ATS:
1939 /* still waiting on ATS suggestion, don't care about blacklist */
1941 case S_RECONNECT_BLACKLIST:
1942 if ( (GNUNET_OK == result) &&
1943 (1 == n->send_connect_ack) )
1945 n->send_connect_ack = 2;
1946 send_session_connect_ack_message (bcc->na.address,
1948 n->connect_ack_timestamp);
1950 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1951 break; /* result for an address we currently don't care about */
1952 if (GNUNET_OK == result)
1954 send_session_connect (&n->primary_address);
1955 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1956 n->state = S_RECONNECT_SENT;
1960 n->state = S_RECONNECT_ATS;
1961 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1964 case S_RECONNECT_SENT:
1965 /* waiting on CONNECT_ACK, don't care about blacklist */
1966 if ( (GNUNET_OK == result) &&
1967 (1 == n->send_connect_ack) )
1969 n->send_connect_ack = 2;
1970 send_session_connect_ack_message (n->primary_address.address,
1971 n->primary_address.session,
1972 n->connect_ack_timestamp);
1975 case S_CONNECTED_SWITCHING_BLACKLIST:
1976 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1977 break; /* result for an address we currently don't care about */
1978 if (GNUNET_OK == result)
1980 send_session_connect (&n->alternative_address);
1981 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1985 free_address (&n->alternative_address);
1986 n->state = S_CONNECTED;
1989 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1990 /* waiting on CONNECT_ACK, don't care about blacklist */
1991 if ( (GNUNET_OK == result) &&
1992 (1 == n->send_connect_ack) )
1994 n->send_connect_ack = 2;
1995 send_session_connect_ack_message (n->primary_address.address,
1996 n->primary_address.session,
1997 n->connect_ack_timestamp);
2001 /* Nothing to do here, ATS will already do what can be done */
2003 case S_DISCONNECT_FINISHED:
2004 /* should not be possible */
2008 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2009 "Unhandled state `%s'\n",
2010 print_state (n->state));
2012 free_neighbour (n, GNUNET_NO);
2016 GNUNET_HELLO_address_free (bcc->na.address);
2022 * We want to know if connecting to a particular peer via
2023 * a particular address is allowed. Check it!
2025 * @param peer identity of the peer to switch the address for
2026 * @param ts time at which the check was initiated
2027 * @param address address of the other peer, NULL if other peer
2029 * @param session session to use (or NULL)
2032 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2033 struct GNUNET_TIME_Absolute ts,
2034 const struct GNUNET_HELLO_Address *address,
2035 struct Session *session)
2037 struct BlackListCheckContext *bcc;
2038 struct GST_BlacklistCheck *bc;
2040 bcc = GNUNET_new (struct BlackListCheckContext);
2041 bcc->na.address = GNUNET_HELLO_address_copy (address);
2042 bcc->na.session = session;
2043 bcc->na.connect_timestamp = ts;
2044 GNUNET_CONTAINER_DLL_insert (bc_head,
2047 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2048 address->transport_name,
2049 &handle_test_blacklist_cont, bcc)))
2051 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2052 we must only store 'bc' if 'bc' is non-NULL... */
2057 * We received a 'SESSION_CONNECT' message from the other peer.
2058 * Consider switching to it.
2060 * @param message possibly a 'struct SessionConnectMessage' (check format)
2061 * @param peer identity of the peer to switch the address for
2062 * @param address address of the other peer, NULL if other peer
2064 * @param session session to use (or NULL)
2065 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2068 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2069 const struct GNUNET_PeerIdentity *peer,
2070 const struct GNUNET_HELLO_Address *address,
2071 struct Session *session)
2073 const struct SessionConnectMessage *scm;
2074 struct NeighbourMapEntry *n;
2075 struct GNUNET_TIME_Absolute ts;
2077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2078 "Received CONNECT message from peer `%s'\n",
2080 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2082 GNUNET_break_op (0);
2083 return GNUNET_SYSERR;
2085 GNUNET_STATISTICS_update (GST_stats,
2087 ("# CONNECT messages received"),
2089 if (NULL == neighbours)
2091 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2092 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2094 return GNUNET_OK; /* we're shutting down */
2096 scm = (const struct SessionConnectMessage *) message;
2097 GNUNET_break_op (0 == ntohl (scm->reserved));
2098 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2099 n = lookup_neighbour (peer);
2101 n = setup_neighbour (peer);
2102 n->send_connect_ack = 1;
2103 n->connect_ack_timestamp = ts;
2107 case S_NOT_CONNECTED:
2108 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2109 /* Do a blacklist check for the new address */
2110 check_blacklist (peer, ts, address, session);
2113 /* CONNECT message takes priority over us asking ATS for address */
2114 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2116 case S_INIT_BLACKLIST:
2117 case S_CONNECT_SENT:
2118 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2119 case S_CONNECT_RECV_ATS:
2120 case S_CONNECT_RECV_BLACKLIST:
2121 case S_CONNECT_RECV_ACK:
2122 /* It can never hurt to have an alternative address in the above cases,
2123 see if it is allowed */
2124 check_blacklist (peer, ts, address, session);
2127 /* we are already connected and can thus send the ACK immediately;
2128 still, it can never hurt to have an alternative address, so also
2129 tell ATS about it */
2130 GNUNET_assert (NULL != n->primary_address.address);
2131 GNUNET_assert (NULL != n->primary_address.session);
2132 n->send_connect_ack = 0;
2133 send_session_connect_ack_message (n->primary_address.address,
2134 n->primary_address.session, ts);
2135 check_blacklist (peer, ts, address, session);
2137 case S_RECONNECT_ATS:
2138 case S_RECONNECT_BLACKLIST:
2139 case S_RECONNECT_SENT:
2140 /* It can never hurt to have an alternative address in the above cases,
2141 see if it is allowed */
2142 check_blacklist (peer, ts, address, session);
2144 case S_CONNECTED_SWITCHING_BLACKLIST:
2145 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2146 /* we are already connected and can thus send the ACK immediately;
2147 still, it can never hurt to have an alternative address, so also
2148 tell ATS about it */
2149 GNUNET_assert (NULL != n->primary_address.address);
2150 GNUNET_assert (NULL != n->primary_address.session);
2151 n->send_connect_ack = 0;
2152 send_session_connect_ack_message (n->primary_address.address,
2153 n->primary_address.session, ts);
2154 check_blacklist (peer, ts, address, session);
2157 /* get rid of remains without terminating sessions, ready to re-try */
2158 free_neighbour (n, GNUNET_YES);
2159 n = setup_neighbour (peer);
2160 n->state = S_CONNECT_RECV_ATS;
2161 GNUNET_ATS_reset_backoff (GST_ats, peer);
2162 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2164 case S_DISCONNECT_FINISHED:
2165 /* should not be possible */
2169 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2170 "Unhandled state `%s'\n",
2171 print_state (n->state));
2173 return GNUNET_SYSERR;
2180 * For an existing neighbour record, set the active connection to
2181 * use the given address.
2183 * @param peer identity of the peer to switch the address for
2184 * @param address address of the other peer, NULL if other peer
2186 * @param session session to use (or NULL)
2187 * @param ats performance data
2188 * @param ats_count number of entries in ats
2189 * @param bandwidth_in inbound quota to be used when connection is up,
2190 * 0 to disconnect from peer
2191 * @param bandwidth_out outbound quota to be used when connection is up,
2192 * 0 to disconnect from peer
2195 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2196 const struct GNUNET_HELLO_Address *address,
2197 struct Session *session,
2198 const struct GNUNET_ATS_Information *ats,
2200 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2201 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2203 struct NeighbourMapEntry *n;
2204 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2206 GNUNET_assert (address->transport_name != NULL);
2207 if (NULL == (n = lookup_neighbour (peer)))
2210 /* Obtain an session for this address from plugin */
2211 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2213 /* we don't have the plugin for this address */
2214 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2217 if ((NULL == session) && (0 == address->address_length))
2220 if (strlen (address->transport_name) > 0)
2221 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2225 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2226 "ATS tells us to switch to address '%s' session %p for "
2227 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2228 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2231 print_state (n->state),
2232 n->send_connect_ack,
2233 ntohl (bandwidth_in.value__),
2234 ntohl (bandwidth_out.value__));
2236 if (NULL == session)
2238 session = papi->get_session (papi->cls, address);
2239 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2240 "Obtained new session for peer `%s' and address '%s': %p\n",
2241 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2243 if (NULL == session)
2245 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2246 "Failed to obtain new session for peer `%s' and address '%s'\n",
2247 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2248 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2253 case S_NOT_CONNECTED:
2255 free_neighbour (n, GNUNET_NO);
2258 set_address (&n->primary_address,
2259 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2260 n->state = S_INIT_BLACKLIST;
2261 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2262 check_blacklist (&n->id,
2263 n->connect_ack_timestamp,
2266 case S_INIT_BLACKLIST:
2267 /* ATS suggests a different address, switch again */
2268 set_address (&n->primary_address,
2269 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2270 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2271 check_blacklist (&n->id,
2272 n->connect_ack_timestamp,
2275 case S_CONNECT_SENT:
2276 /* ATS suggests a different address, switch again */
2277 set_address (&n->primary_address,
2278 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2279 n->state = S_INIT_BLACKLIST;
2280 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2281 check_blacklist (&n->id,
2282 n->connect_ack_timestamp,
2285 case S_CONNECT_RECV_ATS:
2286 set_address (&n->primary_address,
2287 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2288 n->state = S_CONNECT_RECV_BLACKLIST;
2289 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2290 check_blacklist (&n->id,
2291 n->connect_ack_timestamp,
2294 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2295 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2296 check_blacklist (&n->id,
2297 n->connect_ack_timestamp,
2300 case S_CONNECT_RECV_BLACKLIST:
2301 case S_CONNECT_RECV_ACK:
2302 /* ATS asks us to switch while we were trying to connect; switch to new
2303 address and check blacklist again */
2304 set_address (&n->primary_address,
2305 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2306 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2307 check_blacklist (&n->id,
2308 n->connect_ack_timestamp,
2312 GNUNET_assert (NULL != n->primary_address.address);
2313 GNUNET_assert (NULL != n->primary_address.session);
2314 if (n->primary_address.session == session)
2316 /* not an address change, just a quota change */
2317 set_address (&n->primary_address,
2318 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2321 /* ATS asks us to switch a life connection; see if we can get
2322 a CONNECT_ACK on it before we actually do this! */
2323 set_address (&n->alternative_address,
2324 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2325 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2326 check_blacklist (&n->id,
2327 GNUNET_TIME_absolute_get (),
2330 case S_RECONNECT_ATS:
2331 set_address (&n->primary_address,
2332 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2333 n->state = S_RECONNECT_BLACKLIST;
2334 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2335 check_blacklist (&n->id,
2336 n->connect_ack_timestamp,
2339 case S_RECONNECT_BLACKLIST:
2340 /* ATS asks us to switch while we were trying to reconnect; switch to new
2341 address and check blacklist again */
2342 set_address (&n->primary_address,
2343 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2344 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2345 check_blacklist (&n->id,
2346 n->connect_ack_timestamp,
2349 case S_RECONNECT_SENT:
2350 /* ATS asks us to switch while we were trying to reconnect; switch to new
2351 address and check blacklist again */
2352 set_address (&n->primary_address,
2353 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2354 n->state = S_RECONNECT_BLACKLIST;
2355 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2356 check_blacklist (&n->id,
2357 n->connect_ack_timestamp,
2360 case S_CONNECTED_SWITCHING_BLACKLIST:
2361 if (n->primary_address.session == session)
2363 /* ATS switches back to still-active session */
2364 free_address (&n->alternative_address);
2365 n->state = S_CONNECTED;
2368 /* ATS asks us to switch a life connection, update blacklist check */
2369 set_address (&n->alternative_address,
2370 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2371 check_blacklist (&n->id,
2372 GNUNET_TIME_absolute_get (),
2375 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2376 if (n->primary_address.session == session)
2378 /* ATS switches back to still-active session */
2379 free_address (&n->alternative_address);
2380 n->state = S_CONNECTED;
2383 /* ATS asks us to switch a life connection, update blacklist check */
2384 set_address (&n->alternative_address,
2385 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2386 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2387 check_blacklist (&n->id,
2388 GNUNET_TIME_absolute_get (),
2392 /* not going to switch addresses while disconnecting */
2394 case S_DISCONNECT_FINISHED:
2398 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2399 "Unhandled state `%s'\n",
2400 print_state (n->state));
2408 send_utilization_data (void *cls,
2409 const struct GNUNET_PeerIdentity *key,
2412 struct NeighbourMapEntry *n = value;
2413 struct GNUNET_ATS_Information atsi[4];
2415 uint32_t bps_pl_out;
2418 struct GNUNET_TIME_Relative delta;
2420 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2421 GNUNET_TIME_absolute_get ());
2425 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2426 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2428 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2429 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2431 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2436 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2437 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2439 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2440 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2443 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2444 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2448 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2449 atsi[0].value = htonl (bps_out);
2450 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2451 atsi[1].value = htonl (bps_in);
2453 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2454 atsi[2].value = htonl (bps_pl_out);
2455 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2456 atsi[3].value = htonl (bps_pl_in);
2458 GST_ats_update_metrics (key, n->primary_address.address,
2459 n->primary_address.session, atsi, 4);
2460 n->util_payload_bytes_recv = 0;
2461 n->util_payload_bytes_sent = 0;
2462 n->util_total_bytes_recv = 0;
2463 n->util_total_bytes_sent = 0;
2464 n->last_util_transmission = GNUNET_TIME_absolute_get();
2470 * Task transmitting utilization in a regular interval
2472 * @param cls the 'struct NeighbourMapEntry' for which we are running
2473 * @param tc scheduler context (unused)
2476 utilization_transmission (void *cls,
2477 const struct GNUNET_SCHEDULER_TaskContext *tc)
2479 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2481 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2482 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2484 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2485 utilization_transmission, NULL);
2491 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2492 const struct GNUNET_HELLO_Address *address,
2493 struct Session *session,
2494 const struct GNUNET_MessageHeader *message)
2496 struct NeighbourMapEntry *n;
2498 n = lookup_neighbour (peer);
2501 n->util_total_bytes_recv += ntohs(message->size);
2506 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2507 const struct GNUNET_HELLO_Address *address,
2508 struct Session *session,
2509 const struct GNUNET_MessageHeader *message)
2511 struct NeighbourMapEntry *n;
2512 n = lookup_neighbour (peer);
2515 n->util_payload_bytes_recv += ntohs(message->size);
2520 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2521 const struct GNUNET_HELLO_Address *address,
2522 struct Session *session,
2525 struct NeighbourMapEntry *n;
2526 n = lookup_neighbour (peer);
2529 if (n->primary_address.session != session)
2531 n->util_total_bytes_sent += size;
2536 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2539 struct NeighbourMapEntry *n;
2540 n = lookup_neighbour (peer);
2543 n->util_payload_bytes_sent += size;
2548 * Master task run for every neighbour. Performs all of the time-related
2549 * activities (keep alive, send next message, disconnect if idle, finish
2550 * clean up after disconnect).
2552 * @param cls the 'struct NeighbourMapEntry' for which we are running
2553 * @param tc scheduler context (unused)
2556 master_task (void *cls,
2557 const struct GNUNET_SCHEDULER_TaskContext *tc)
2559 struct NeighbourMapEntry *n = cls;
2560 struct GNUNET_TIME_Relative delay;
2562 n->task = GNUNET_SCHEDULER_NO_TASK;
2563 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2565 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2566 GNUNET_i2s (&n->id),
2567 print_state(n->state),
2568 GNUNET_STRINGS_relative_time_to_string (delay,
2572 case S_NOT_CONNECTED:
2573 /* invalid state for master task, clean up */
2575 n->state = S_DISCONNECT_FINISHED;
2576 free_neighbour (n, GNUNET_NO);
2579 if (0 == delay.rel_value_us)
2581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2582 "Connection to `%s' timed out waiting for ATS to provide address\n",
2583 GNUNET_i2s (&n->id));
2584 n->state = S_DISCONNECT_FINISHED;
2585 free_neighbour (n, GNUNET_NO);
2589 case S_INIT_BLACKLIST:
2590 if (0 == delay.rel_value_us)
2592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2593 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2594 GNUNET_i2s (&n->id));
2595 n->state = S_DISCONNECT_FINISHED;
2596 free_neighbour (n, GNUNET_NO);
2600 case S_CONNECT_SENT:
2601 if (0 == delay.rel_value_us)
2603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2604 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2605 GNUNET_i2s (&n->id));
2606 disconnect_neighbour (n);
2610 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2611 if (0 == delay.rel_value_us)
2613 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2614 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2615 GNUNET_i2s (&n->id));
2616 n->state = S_DISCONNECT_FINISHED;
2617 free_neighbour (n, GNUNET_NO);
2621 case S_CONNECT_RECV_ATS:
2622 if (0 == delay.rel_value_us)
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2625 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2626 GNUNET_i2s (&n->id));
2627 n->state = S_DISCONNECT_FINISHED;
2628 free_neighbour (n, GNUNET_NO);
2632 case S_CONNECT_RECV_BLACKLIST:
2633 if (0 == delay.rel_value_us)
2635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2636 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2637 GNUNET_i2s (&n->id));
2638 n->state = S_DISCONNECT_FINISHED;
2639 free_neighbour (n, GNUNET_NO);
2643 case S_CONNECT_RECV_ACK:
2644 if (0 == delay.rel_value_us)
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2647 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2648 GNUNET_i2s (&n->id));
2649 disconnect_neighbour (n);
2654 if (0 == delay.rel_value_us)
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2658 GNUNET_i2s (&n->id));
2659 disconnect_neighbour (n);
2662 try_transmission_to_peer (n);
2665 case S_RECONNECT_ATS:
2666 if (0 == delay.rel_value_us)
2668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2669 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2670 GNUNET_i2s (&n->id));
2671 disconnect_neighbour (n);
2675 case S_RECONNECT_BLACKLIST:
2676 if (0 == delay.rel_value_us)
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2679 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2680 GNUNET_i2s (&n->id));
2681 disconnect_neighbour (n);
2685 case S_RECONNECT_SENT:
2686 if (0 == delay.rel_value_us)
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2689 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2690 GNUNET_i2s (&n->id));
2691 disconnect_neighbour (n);
2695 case S_CONNECTED_SWITCHING_BLACKLIST:
2696 if (0 == delay.rel_value_us)
2698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2699 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2700 GNUNET_i2s (&n->id));
2701 disconnect_neighbour (n);
2704 try_transmission_to_peer (n);
2707 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2708 if (0 == delay.rel_value_us)
2710 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2711 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2712 GNUNET_i2s (&n->id));
2713 disconnect_neighbour (n);
2716 try_transmission_to_peer (n);
2720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2721 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2722 GNUNET_i2s (&n->id));
2723 free_neighbour (n, GNUNET_NO);
2725 case S_DISCONNECT_FINISHED:
2726 /* how did we get here!? */
2730 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2731 "Unhandled state `%s'\n",
2732 print_state (n->state));
2736 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2737 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2738 (S_CONNECTED == n->state) )
2740 /* if we are *now* in one of these three states, we're sending
2741 keep alive messages, so we need to consider the keepalive
2742 delay, not just the connection timeout */
2743 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2746 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2747 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2754 * Send a SESSION_ACK message to the neighbour to confirm that we
2755 * got his CONNECT_ACK.
2757 * @param n neighbour to send the SESSION_ACK to
2760 send_session_ack_message (struct NeighbourMapEntry *n)
2762 struct GNUNET_MessageHeader msg;
2764 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2765 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2766 (void) send_with_session(n,
2767 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2768 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2774 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2775 * Consider switching to it.
2777 * @param message possibly a 'struct SessionConnectMessage' (check format)
2778 * @param peer identity of the peer to switch the address for
2779 * @param address address of the other peer, NULL if other peer
2781 * @param session session to use (or NULL)
2782 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2785 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2786 const struct GNUNET_PeerIdentity *peer,
2787 const struct GNUNET_HELLO_Address *address,
2788 struct Session *session)
2790 const struct SessionConnectMessage *scm;
2791 struct GNUNET_TIME_Absolute ts;
2792 struct NeighbourMapEntry *n;
2794 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2795 "Received CONNECT_ACK message from peer `%s'\n",
2798 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2800 GNUNET_break_op (0);
2801 return GNUNET_SYSERR;
2803 GNUNET_STATISTICS_update (GST_stats,
2805 ("# CONNECT_ACK messages received"),
2807 scm = (const struct SessionConnectMessage *) message;
2808 GNUNET_break_op (ntohl (scm->reserved) == 0);
2809 if (NULL == (n = lookup_neighbour (peer)))
2811 GNUNET_STATISTICS_update (GST_stats,
2813 ("# unexpected CONNECT_ACK messages (no peer)"),
2815 return GNUNET_SYSERR;
2817 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2820 case S_NOT_CONNECTED:
2822 free_neighbour (n, GNUNET_NO);
2823 return GNUNET_SYSERR;
2825 case S_INIT_BLACKLIST:
2826 GNUNET_STATISTICS_update (GST_stats,
2828 ("# unexpected CONNECT_ACK messages (not ready)"),
2831 case S_CONNECT_SENT:
2832 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
2834 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2835 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
2838 n->state = S_CONNECTED;
2839 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2840 GNUNET_STATISTICS_set (GST_stats,
2841 gettext_noop ("# peers connected"),
2842 ++neighbours_connected,
2844 connect_notify_cb (callback_cls, &n->id,
2845 n->primary_address.bandwidth_in,
2846 n->primary_address.bandwidth_out);
2847 /* Tell ATS that the outbound session we created to send CONNECT was successfull */
2848 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
2849 set_address (&n->primary_address,
2850 n->primary_address.address,
2851 n->primary_address.session,
2852 n->primary_address.bandwidth_in,
2853 n->primary_address.bandwidth_out,
2855 send_session_ack_message (n);
2857 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2858 case S_CONNECT_RECV_ATS:
2859 case S_CONNECT_RECV_BLACKLIST:
2860 case S_CONNECT_RECV_ACK:
2861 GNUNET_STATISTICS_update (GST_stats,
2863 ("# unexpected CONNECT_ACK messages (not ready)"),
2867 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2868 send_session_ack_message (n);
2870 case S_RECONNECT_ATS:
2871 case S_RECONNECT_BLACKLIST:
2872 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2873 to give us a new address... */
2874 GNUNET_STATISTICS_update (GST_stats,
2876 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2879 case S_RECONNECT_SENT:
2880 /* new address worked; go back to connected! */
2881 n->state = S_CONNECTED;
2882 send_session_ack_message (n);
2884 case S_CONNECTED_SWITCHING_BLACKLIST:
2885 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2886 send_session_ack_message (n);
2888 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2889 /* new address worked; adopt it and go back to connected! */
2890 n->state = S_CONNECTED;
2891 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2892 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2894 GST_ats_add_address (n->alternative_address.address, n->alternative_address.session);
2895 set_address (&n->primary_address,
2896 n->alternative_address.address,
2897 n->alternative_address.session,
2898 n->alternative_address.bandwidth_in,
2899 n->alternative_address.bandwidth_out,
2901 free_address (&n->alternative_address);
2902 send_session_ack_message (n);
2905 GNUNET_STATISTICS_update (GST_stats,
2907 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2909 return GNUNET_SYSERR;
2910 case S_DISCONNECT_FINISHED:
2914 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2915 "Unhandled state `%s'\n",
2916 print_state (n->state));
2918 return GNUNET_SYSERR;
2925 * A session was terminated. Take note; if needed, try to get
2926 * an alternative address from ATS.
2928 * @param peer identity of the peer where the session died
2929 * @param session session that is gone
2930 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
2931 * this session was not in use
2934 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2935 struct Session *session)
2937 struct NeighbourMapEntry *n;
2938 struct BlackListCheckContext *bcc;
2939 struct BlackListCheckContext *bcc_next;
2941 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2943 while (NULL != (bcc = bcc_next))
2945 bcc_next = bcc->next;
2946 if (bcc->na.session == session)
2948 if (NULL != bcc->bc)
2949 GST_blacklist_test_cancel (bcc->bc);
2950 GNUNET_HELLO_address_free (bcc->na.address);
2951 GNUNET_CONTAINER_DLL_remove (bc_head,
2957 if (NULL == (n = lookup_neighbour (peer)))
2958 return GNUNET_NO; /* can't affect us */
2959 if (session != n->primary_address.session)
2961 if (session == n->alternative_address.session)
2963 free_address (&n->alternative_address);
2964 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2965 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2966 n->state = S_CONNECTED;
2970 return GNUNET_NO; /* doesn't affect us further */
2973 n->expect_latency_response = GNUNET_NO;
2976 case S_NOT_CONNECTED:
2978 free_neighbour (n, GNUNET_NO);
2982 free_neighbour (n, GNUNET_NO);
2984 case S_INIT_BLACKLIST:
2985 case S_CONNECT_SENT:
2986 free_address (&n->primary_address);
2987 n->state = S_INIT_ATS;
2988 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2989 // FIXME: need to ask ATS for suggestions again?
2990 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2992 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2993 case S_CONNECT_RECV_ATS:
2994 case S_CONNECT_RECV_BLACKLIST:
2995 case S_CONNECT_RECV_ACK:
2996 /* error on inbound session; free neighbour entirely */
2997 free_address (&n->primary_address);
2998 free_neighbour (n, GNUNET_NO);
3001 free_address (&n->primary_address);
3002 n->state = S_RECONNECT_ATS;
3003 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3004 /* FIXME: is this ATS call needed? */
3005 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3007 case S_RECONNECT_ATS:
3008 /* we don't have an address, how can it go down? */
3011 case S_RECONNECT_BLACKLIST:
3012 case S_RECONNECT_SENT:
3013 n->state = S_RECONNECT_ATS;
3014 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3015 // FIXME: need to ask ATS for suggestions again?
3016 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3018 case S_CONNECTED_SWITCHING_BLACKLIST:
3019 /* primary went down while we were checking secondary against
3020 blacklist, adopt secondary as primary */
3021 free_address (&n->primary_address);
3022 n->primary_address = n->alternative_address;
3023 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3024 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3025 n->state = S_RECONNECT_BLACKLIST;
3027 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3028 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3029 secondary as primary */
3030 free_address (&n->primary_address);
3031 n->primary_address = n->alternative_address;
3032 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3033 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3034 n->state = S_RECONNECT_SENT;
3037 free_address (&n->primary_address);
3039 case S_DISCONNECT_FINISHED:
3040 /* neighbour was freed and plugins told to terminate session */
3044 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3045 "Unhandled state `%s'\n",
3046 print_state (n->state));
3050 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3051 GNUNET_SCHEDULER_cancel (n->task);
3052 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3058 * We received a 'SESSION_ACK' message from the other peer.
3059 * If we sent a 'CONNECT_ACK' last, this means we are now
3060 * connected. Otherwise, do nothing.
3062 * @param message possibly a 'struct SessionConnectMessage' (check format)
3063 * @param peer identity of the peer to switch the address for
3064 * @param address address of the other peer, NULL if other peer
3066 * @param session session to use (or NULL)
3067 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3070 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3071 const struct GNUNET_PeerIdentity *peer,
3072 const struct GNUNET_HELLO_Address *address,
3073 struct Session *session)
3075 struct NeighbourMapEntry *n;
3077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3078 "Received SESSION_ACK message from peer `%s'\n",
3080 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3082 GNUNET_break_op (0);
3083 return GNUNET_SYSERR;
3085 GNUNET_STATISTICS_update (GST_stats,
3087 ("# SESSION_ACK messages received"),
3089 if (NULL == (n = lookup_neighbour (peer)))
3090 return GNUNET_SYSERR;
3091 /* check if we are in a plausible state for having sent
3092 a CONNECT_ACK. If not, return, otherwise break */
3093 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3094 (S_CONNECT_SENT != n->state) ) ||
3095 (2 != n->send_connect_ack) )
3097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3098 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3100 print_state (n->state),
3101 n->send_connect_ack);
3102 GNUNET_STATISTICS_update (GST_stats,
3103 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3105 return GNUNET_SYSERR;
3107 n->state = S_CONNECTED;
3108 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3109 GNUNET_STATISTICS_set (GST_stats,
3110 gettext_noop ("# peers connected"),
3111 ++neighbours_connected,
3113 connect_notify_cb (callback_cls, &n->id,
3114 n->primary_address.bandwidth_in,
3115 n->primary_address.bandwidth_out);
3117 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
3118 set_address (&n->primary_address,
3119 n->primary_address.address,
3120 n->primary_address.session,
3121 n->primary_address.bandwidth_in,
3122 n->primary_address.bandwidth_out,
3129 * Test if we're connected to the given peer.
3131 * @param target peer to test
3132 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3135 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3137 return test_connected (lookup_neighbour (target));
3142 * Change the incoming quota for the given peer.
3144 * @param neighbour identity of peer to change qutoa for
3145 * @param quota new quota
3148 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3149 struct GNUNET_BANDWIDTH_Value32NBO quota)
3151 struct NeighbourMapEntry *n;
3153 if (NULL == (n = lookup_neighbour (neighbour)))
3155 GNUNET_STATISTICS_update (GST_stats,
3157 ("# SET QUOTA messages ignored (no such peer)"),
3161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3162 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3163 ntohl (quota.value__), GNUNET_i2s (&n->id));
3164 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3165 if (0 != ntohl (quota.value__))
3167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
3168 GNUNET_i2s (&n->id), "SET_QUOTA");
3169 if (GNUNET_YES == test_connected (n))
3170 GNUNET_STATISTICS_update (GST_stats,
3171 gettext_noop ("# disconnects due to quota of 0"),
3173 disconnect_neighbour (n);
3178 * We received a disconnect message from the given peer,
3179 * validate and process.
3181 * @param peer sender of the message
3182 * @param msg the disconnect message
3185 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
3187 const struct GNUNET_MessageHeader
3190 struct NeighbourMapEntry *n;
3191 const struct SessionDisconnectMessage *sdm;
3193 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3194 "Received DISCONNECT message from peer `%s'\n",
3196 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3198 // GNUNET_break_op (0);
3199 GNUNET_STATISTICS_update (GST_stats,
3201 ("# disconnect messages ignored (old format)"), 1,
3205 GNUNET_STATISTICS_update (GST_stats,
3207 ("# DISCONNECT messages received"),
3209 sdm = (const struct SessionDisconnectMessage *) msg;
3210 if (NULL == (n = lookup_neighbour (peer)))
3211 return; /* gone already */
3212 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3214 GNUNET_STATISTICS_update (GST_stats,
3216 ("# disconnect messages ignored (timestamp)"), 1,
3220 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3222 GNUNET_break_op (0);
3225 if (ntohl (sdm->purpose.size) !=
3226 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3227 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3228 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3230 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3231 "%s message from peer `%s' has invalid size \n",
3234 GNUNET_break_op (0);
3238 GNUNET_CRYPTO_eddsa_verify
3239 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3240 &sdm->signature, &sdm->public_key))
3242 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3243 "%s message from peer `%s' cannot be verified \n",
3246 GNUNET_break_op (0);
3249 if (GNUNET_YES == test_connected (n))
3250 GNUNET_STATISTICS_update (GST_stats,
3252 ("# other peer asked to disconnect from us"), 1,
3254 disconnect_neighbour (n);
3259 * Closure for the neighbours_iterate function.
3261 struct IteratorContext
3264 * Function to call on each connected neighbour.
3266 GST_NeighbourIterator cb;
3276 * Call the callback from the closure for each connected neighbour.
3278 * @param cls the `struct IteratorContext`
3279 * @param key the hash of the public key of the neighbour
3280 * @param value the `struct NeighbourMapEntry`
3281 * @return #GNUNET_OK (continue to iterate)
3284 neighbours_iterate (void *cls,
3285 const struct GNUNET_PeerIdentity *key,
3288 struct IteratorContext *ic = cls;
3289 struct NeighbourMapEntry *n = value;
3290 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3291 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3293 if (GNUNET_YES != test_connected (n))
3296 if (NULL != n->primary_address.address)
3298 bandwidth_in = n->primary_address.bandwidth_in;
3299 bandwidth_out = n->primary_address.bandwidth_out;
3303 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3304 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3306 ic->cb (ic->cb_cls, &n->id,
3307 n->primary_address.address,
3308 bandwidth_in, bandwidth_out);
3314 * Iterate over all connected neighbours.
3316 * @param cb function to call
3317 * @param cb_cls closure for cb
3320 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3322 struct IteratorContext ic;
3324 if (NULL == neighbours)
3325 return; /* can happen during shutdown */
3328 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3333 * If we have an active connection to the given target, it must be shutdown.
3335 * @param target peer to disconnect from
3338 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3340 struct NeighbourMapEntry *n;
3342 if (NULL == (n = lookup_neighbour (target)))
3343 return; /* not active */
3344 if (GNUNET_YES == test_connected (n))
3345 GNUNET_STATISTICS_update (GST_stats,
3347 ("# disconnected from peer upon explicit request"), 1,
3349 disconnect_neighbour (n);
3354 * Obtain current latency information for the given neighbour.
3356 * @param peer to get the latency for
3357 * @return observed latency of the address, FOREVER if the
3358 * the connection is not up
3360 struct GNUNET_TIME_Relative
3361 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3363 struct NeighbourMapEntry *n;
3365 n = lookup_neighbour (peer);
3367 return GNUNET_TIME_UNIT_FOREVER_REL;
3371 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3372 case S_CONNECTED_SWITCHING_BLACKLIST:
3373 case S_RECONNECT_SENT:
3374 case S_RECONNECT_ATS:
3375 case S_RECONNECT_BLACKLIST:
3377 case S_NOT_CONNECTED:
3378 case S_INIT_BLACKLIST:
3380 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3381 case S_CONNECT_RECV_ATS:
3382 case S_CONNECT_RECV_BLACKLIST:
3383 case S_CONNECT_RECV_ACK:
3384 case S_CONNECT_SENT:
3386 case S_DISCONNECT_FINISHED:
3387 return GNUNET_TIME_UNIT_FOREVER_REL;
3389 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3390 "Unhandled state `%s'\n",
3391 print_state (n->state));
3395 return GNUNET_TIME_UNIT_FOREVER_REL;
3400 * Obtain current address information for the given neighbour.
3403 * @return address currently used
3405 struct GNUNET_HELLO_Address *
3406 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3408 struct NeighbourMapEntry *n;
3410 n = lookup_neighbour (peer);
3413 return n->primary_address.address;
3418 * Initialize the neighbours subsystem.
3420 * @param cls closure for callbacks
3421 * @param connect_cb function to call if we connect to a peer
3422 * @param disconnect_cb function to call if we disconnect from a peer
3423 * @param peer_address_cb function to call if we change an active address
3425 * @param max_fds maximum number of fds to use
3428 GST_neighbours_start (void *cls,
3429 NotifyConnect connect_cb,
3430 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3431 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3432 unsigned int max_fds)
3435 connect_notify_cb = connect_cb;
3436 disconnect_notify_cb = disconnect_cb;
3437 address_change_cb = peer_address_cb;
3438 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3439 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3440 utilization_transmission, NULL);
3445 * Disconnect from the given neighbour.
3448 * @param key hash of neighbour's public key (not used)
3449 * @param value the 'struct NeighbourMapEntry' of the neighbour
3450 * @return #GNUNET_OK (continue to iterate)
3453 disconnect_all_neighbours (void *cls,
3454 const struct GNUNET_PeerIdentity *key,
3457 struct NeighbourMapEntry *n = value;
3459 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3460 "Disconnecting peer `%4s', %s\n",
3461 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3462 n->state = S_DISCONNECT_FINISHED;
3463 free_neighbour (n, GNUNET_NO);
3469 * Cleanup the neighbours subsystem.
3472 GST_neighbours_stop ()
3474 if (NULL == neighbours)
3476 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3478 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3479 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3482 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3483 &disconnect_all_neighbours,
3485 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3487 callback_cls = NULL;
3488 connect_notify_cb = NULL;
3489 disconnect_notify_cb = NULL;
3490 address_change_cb = NULL;
3494 /* end of file gnunet-service-transport_neighbours.c */