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 if (0 == memcmp (target,
1736 &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1738 /* refuse to connect to myself */
1739 /* FIXME: can this happen? Is this not an API violation? */
1740 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1741 "Refusing to try to connect to myself.\n");
1744 n = lookup_neighbour (target);
1745 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1746 "Asked to connect to peer `%s' (state: %s)\n",
1747 GNUNET_i2s (target),
1748 (NULL != n) ? print_state(n->state) : "UNKNOWN PEER");
1753 case S_NOT_CONNECTED:
1754 /* this should not be possible */
1756 free_neighbour (n, GNUNET_NO);
1759 case S_INIT_BLACKLIST:
1760 case S_CONNECT_SENT:
1761 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1762 case S_CONNECT_RECV_ATS:
1763 case S_CONNECT_RECV_BLACKLIST:
1764 case S_CONNECT_RECV_ACK:
1765 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1766 "Ignoring request to try to connect to `%s', already trying!\n",
1767 GNUNET_i2s (target));
1768 return; /* already trying */
1770 case S_RECONNECT_ATS:
1771 case S_RECONNECT_BLACKLIST:
1772 case S_RECONNECT_SENT:
1773 case S_CONNECTED_SWITCHING_BLACKLIST:
1774 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1775 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1776 "Ignoring request to try to connect, already connected to `%s'!\n",
1777 GNUNET_i2s (target));
1778 return; /* already connected */
1780 /* get rid of remains, ready to re-try immediately */
1781 free_neighbour (n, GNUNET_NO);
1783 case S_DISCONNECT_FINISHED:
1784 /* should not be possible */
1787 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1788 "Unhandled state `%s'\n",
1789 print_state (n->state));
1791 free_neighbour (n, GNUNET_NO);
1795 n = setup_neighbour (target);
1796 n->state = S_INIT_ATS;
1797 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1799 GNUNET_ATS_reset_backoff (GST_ats, target);
1800 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1805 * Function called with the result of a blacklist check.
1807 * @param cls closure with the 'struct BlackListCheckContext'
1808 * @param peer peer this check affects
1809 * @param result #GNUNET_OK if the address is allowed
1812 handle_test_blacklist_cont (void *cls,
1813 const struct GNUNET_PeerIdentity *peer,
1816 struct BlackListCheckContext *bcc = cls;
1817 struct NeighbourMapEntry *n;
1820 GNUNET_CONTAINER_DLL_remove (bc_head,
1823 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1824 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1826 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1827 if (GNUNET_OK == result)
1828 GST_ats_add_address (bcc->na.address, bcc->na.session);
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 // FIXME: should also possibly destroy session with plugin!?
1864 GNUNET_ATS_address_destroyed (GST_ats,
1867 free_address (&n->primary_address);
1868 n->state = S_INIT_ATS;
1869 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1870 // FIXME: do we need to ask ATS again for suggestions?
1871 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
1874 case S_CONNECT_SENT:
1875 /* waiting on CONNECT_ACK, send ACK if one is pending */
1876 if ( (GNUNET_OK == result) &&
1877 (1 == n->send_connect_ack) )
1879 n->send_connect_ack = 2;
1880 send_session_connect_ack_message (n->primary_address.address,
1881 n->primary_address.session,
1882 n->connect_ack_timestamp);
1885 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1886 n->state = S_CONNECT_RECV_ATS;
1887 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1888 GNUNET_ATS_reset_backoff (GST_ats, peer);
1889 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
1891 case S_CONNECT_RECV_ATS:
1892 /* waiting on ATS suggestion, don't care about blacklist */
1894 case S_CONNECT_RECV_BLACKLIST:
1895 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1898 "Blacklist result ignored, as it is not for our primary address\n");
1899 break; /* result for an address we currently don't care about */
1901 if (GNUNET_OK == result)
1903 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1904 n->state = S_CONNECT_RECV_ACK;
1905 send_session_connect_ack_message (bcc->na.address,
1907 n->connect_ack_timestamp);
1908 if (1 == n->send_connect_ack)
1909 n->send_connect_ack = 2;
1913 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
1915 plugin = GST_plugins_find (bcc->na.address->transport_name);
1916 if ( (NULL != plugin) &&
1917 (NULL != bcc->na.session) )
1919 plugin->disconnect_session (plugin->cls,
1923 GNUNET_break (NULL != plugin);
1924 GNUNET_ATS_address_destroyed (GST_ats,
1927 free_address (&n->primary_address);
1928 n->state = S_INIT_ATS;
1929 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1930 // FIXME: do we need to ask ATS again for suggestions?
1931 GNUNET_ATS_reset_backoff (GST_ats, peer);
1932 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
1935 case S_CONNECT_RECV_ACK:
1936 /* waiting on SESSION_ACK, send ACK if one is pending */
1937 if ( (GNUNET_OK == result) &&
1938 (1 == n->send_connect_ack) )
1940 n->send_connect_ack = 2;
1941 send_session_connect_ack_message (n->primary_address.address,
1942 n->primary_address.session,
1943 n->connect_ack_timestamp);
1947 /* already connected, don't care about blacklist */
1949 case S_RECONNECT_ATS:
1950 /* still waiting on ATS suggestion, don't care about blacklist */
1952 case S_RECONNECT_BLACKLIST:
1953 if ( (GNUNET_OK == result) &&
1954 (1 == n->send_connect_ack) )
1956 n->send_connect_ack = 2;
1957 send_session_connect_ack_message (bcc->na.address,
1959 n->connect_ack_timestamp);
1961 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1962 break; /* result for an address we currently don't care about */
1963 if (GNUNET_OK == result)
1965 send_session_connect (&n->primary_address);
1966 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1967 n->state = S_RECONNECT_SENT;
1971 GNUNET_ATS_address_destroyed (GST_ats,
1974 n->state = S_RECONNECT_ATS;
1975 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1976 // FIXME: do we need to ask ATS again for suggestions?
1977 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
1980 case S_RECONNECT_SENT:
1981 /* waiting on CONNECT_ACK, don't care about blacklist */
1982 if ( (GNUNET_OK == result) &&
1983 (1 == n->send_connect_ack) )
1985 n->send_connect_ack = 2;
1986 send_session_connect_ack_message (n->primary_address.address,
1987 n->primary_address.session,
1988 n->connect_ack_timestamp);
1991 case S_CONNECTED_SWITCHING_BLACKLIST:
1992 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1993 break; /* result for an address we currently don't care about */
1994 if (GNUNET_OK == result)
1996 send_session_connect (&n->alternative_address);
1997 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
2001 GNUNET_ATS_address_destroyed (GST_ats,
2004 free_address (&n->alternative_address);
2005 n->state = S_CONNECTED;
2008 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2009 /* waiting on CONNECT_ACK, don't care about blacklist */
2010 if ( (GNUNET_OK == result) &&
2011 (1 == n->send_connect_ack) )
2013 n->send_connect_ack = 2;
2014 send_session_connect_ack_message (n->primary_address.address,
2015 n->primary_address.session,
2016 n->connect_ack_timestamp);
2020 /* Nothing to do here, ATS will already do what can be done */
2022 case S_DISCONNECT_FINISHED:
2023 /* should not be possible */
2027 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2028 "Unhandled state `%s'\n",
2029 print_state (n->state));
2031 free_neighbour (n, GNUNET_NO);
2035 GNUNET_HELLO_address_free (bcc->na.address);
2041 * We want to know if connecting to a particular peer via
2042 * a particular address is allowed. Check it!
2044 * @param peer identity of the peer to switch the address for
2045 * @param ts time at which the check was initiated
2046 * @param address address of the other peer, NULL if other peer
2048 * @param session session to use (or NULL)
2051 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2052 struct GNUNET_TIME_Absolute ts,
2053 const struct GNUNET_HELLO_Address *address,
2054 struct Session *session)
2056 struct BlackListCheckContext *bcc;
2057 struct GST_BlacklistCheck *bc;
2059 bcc = GNUNET_new (struct BlackListCheckContext);
2060 bcc->na.address = GNUNET_HELLO_address_copy (address);
2061 bcc->na.session = session;
2062 bcc->na.connect_timestamp = ts;
2063 GNUNET_CONTAINER_DLL_insert (bc_head,
2066 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2067 address->transport_name,
2068 &handle_test_blacklist_cont, bcc)))
2070 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2071 we must only store 'bc' if 'bc' is non-NULL... */
2076 * We received a 'SESSION_CONNECT' message from the other peer.
2077 * Consider switching to it.
2079 * @param message possibly a 'struct SessionConnectMessage' (check format)
2080 * @param peer identity of the peer to switch the address for
2081 * @param address address of the other peer, NULL if other peer
2083 * @param session session to use (or NULL)
2084 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2087 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2088 const struct GNUNET_PeerIdentity *peer,
2089 const struct GNUNET_HELLO_Address *address,
2090 struct Session *session)
2092 const struct SessionConnectMessage *scm;
2093 struct NeighbourMapEntry *n;
2094 struct GNUNET_TIME_Absolute ts;
2096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2097 "Received CONNECT message from peer `%s'\n",
2099 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2101 GNUNET_break_op (0);
2102 return GNUNET_SYSERR;
2104 GNUNET_STATISTICS_update (GST_stats,
2106 ("# CONNECT messages received"),
2108 if (NULL == neighbours)
2110 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2111 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2113 return GNUNET_OK; /* we're shutting down */
2115 scm = (const struct SessionConnectMessage *) message;
2116 GNUNET_break_op (0 == ntohl (scm->reserved));
2117 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2118 n = lookup_neighbour (peer);
2120 n = setup_neighbour (peer);
2121 n->send_connect_ack = 1;
2122 n->connect_ack_timestamp = ts;
2126 case S_NOT_CONNECTED:
2127 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2128 /* Do a blacklist check for the new address */
2129 check_blacklist (peer, ts, address, session);
2132 /* CONNECT message takes priority over us asking ATS for address */
2133 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2135 case S_INIT_BLACKLIST:
2136 case S_CONNECT_SENT:
2137 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2138 case S_CONNECT_RECV_ATS:
2139 case S_CONNECT_RECV_BLACKLIST:
2140 case S_CONNECT_RECV_ACK:
2141 /* It can never hurt to have an alternative address in the above cases,
2142 see if it is allowed */
2143 check_blacklist (peer, ts, address, session);
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);
2156 case S_RECONNECT_ATS:
2157 case S_RECONNECT_BLACKLIST:
2158 case S_RECONNECT_SENT:
2159 /* It can never hurt to have an alternative address in the above cases,
2160 see if it is allowed */
2161 check_blacklist (peer, ts, address, session);
2163 case S_CONNECTED_SWITCHING_BLACKLIST:
2164 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2165 /* we are already connected and can thus send the ACK immediately;
2166 still, it can never hurt to have an alternative address, so also
2167 tell ATS about it */
2168 GNUNET_assert (NULL != n->primary_address.address);
2169 GNUNET_assert (NULL != n->primary_address.session);
2170 n->send_connect_ack = 0;
2171 send_session_connect_ack_message (n->primary_address.address,
2172 n->primary_address.session, ts);
2173 check_blacklist (peer, ts, address, session);
2176 /* get rid of remains without terminating sessions, ready to re-try */
2177 free_neighbour (n, GNUNET_YES);
2178 n = setup_neighbour (peer);
2179 n->state = S_CONNECT_RECV_ATS;
2180 GNUNET_ATS_reset_backoff (GST_ats, peer);
2181 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2183 case S_DISCONNECT_FINISHED:
2184 /* should not be possible */
2188 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2189 "Unhandled state `%s'\n",
2190 print_state (n->state));
2192 return GNUNET_SYSERR;
2199 * For an existing neighbour record, set the active connection to
2200 * use the given address.
2202 * @param peer identity of the peer to switch the address for
2203 * @param address address of the other peer, NULL if other peer
2205 * @param session session to use (or NULL)
2206 * @param ats performance data
2207 * @param ats_count number of entries in ats
2208 * @param bandwidth_in inbound quota to be used when connection is up,
2209 * 0 to disconnect from peer
2210 * @param bandwidth_out outbound quota to be used when connection is up,
2211 * 0 to disconnect from peer
2214 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2215 const struct GNUNET_HELLO_Address *address,
2216 struct Session *session,
2217 const struct GNUNET_ATS_Information *ats,
2219 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2220 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2222 struct NeighbourMapEntry *n;
2223 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2225 GNUNET_assert (address->transport_name != NULL);
2226 if (NULL == (n = lookup_neighbour (peer)))
2229 /* Obtain an session for this address from plugin */
2230 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2232 /* we don't have the plugin for this address */
2233 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2236 if ((NULL == session) && (0 == address->address_length))
2239 if (strlen (address->transport_name) > 0)
2240 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2244 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2245 "ATS tells us to switch to address '%s' session %p for "
2246 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2247 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2250 print_state (n->state),
2251 n->send_connect_ack,
2252 ntohl (bandwidth_in.value__),
2253 ntohl (bandwidth_out.value__));
2255 if (NULL == session)
2257 session = papi->get_session (papi->cls, address);
2258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2259 "Obtained new session for peer `%s' and address '%s': %p\n",
2260 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2262 if (NULL == session)
2264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2265 "Failed to obtain new session for peer `%s' and address '%s'\n",
2266 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2267 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2272 case S_NOT_CONNECTED:
2274 free_neighbour (n, GNUNET_NO);
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_INIT_BLACKLIST:
2286 /* ATS suggests a different address, switch again */
2287 set_address (&n->primary_address,
2288 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
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_SENT:
2295 /* ATS suggests a different address, switch again */
2296 set_address (&n->primary_address,
2297 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2298 n->state = S_INIT_BLACKLIST;
2299 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2300 check_blacklist (&n->id,
2301 n->connect_ack_timestamp,
2304 case S_CONNECT_RECV_ATS:
2305 set_address (&n->primary_address,
2306 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2307 n->state = S_CONNECT_RECV_BLACKLIST;
2308 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2309 check_blacklist (&n->id,
2310 n->connect_ack_timestamp,
2313 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2314 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2315 check_blacklist (&n->id,
2316 n->connect_ack_timestamp,
2319 case S_CONNECT_RECV_BLACKLIST:
2320 case S_CONNECT_RECV_ACK:
2321 /* ATS asks us to switch while we were trying to connect; switch to new
2322 address and check blacklist again */
2323 set_address (&n->primary_address,
2324 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2325 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2326 check_blacklist (&n->id,
2327 n->connect_ack_timestamp,
2331 GNUNET_assert (NULL != n->primary_address.address);
2332 GNUNET_assert (NULL != n->primary_address.session);
2333 if (n->primary_address.session == session)
2335 /* not an address change, just a quota change */
2336 set_address (&n->primary_address,
2337 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2340 /* ATS asks us to switch a life connection; see if we can get
2341 a CONNECT_ACK on it before we actually do this! */
2342 set_address (&n->alternative_address,
2343 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2344 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2345 check_blacklist (&n->id,
2346 GNUNET_TIME_absolute_get (),
2349 case S_RECONNECT_ATS:
2350 set_address (&n->primary_address,
2351 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2352 n->state = S_RECONNECT_BLACKLIST;
2353 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2354 check_blacklist (&n->id,
2355 n->connect_ack_timestamp,
2358 case S_RECONNECT_BLACKLIST:
2359 /* ATS asks us to switch while we were trying to reconnect; switch to new
2360 address and check blacklist again */
2361 set_address (&n->primary_address,
2362 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2363 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2364 check_blacklist (&n->id,
2365 n->connect_ack_timestamp,
2368 case S_RECONNECT_SENT:
2369 /* ATS asks us to switch while we were trying to reconnect; switch to new
2370 address and check blacklist again */
2371 set_address (&n->primary_address,
2372 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2373 n->state = S_RECONNECT_BLACKLIST;
2374 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2375 check_blacklist (&n->id,
2376 n->connect_ack_timestamp,
2379 case S_CONNECTED_SWITCHING_BLACKLIST:
2380 if (n->primary_address.session == session)
2382 /* ATS switches back to still-active session */
2383 free_address (&n->alternative_address);
2384 n->state = S_CONNECTED;
2387 /* ATS asks us to switch a life connection, update blacklist check */
2388 set_address (&n->alternative_address,
2389 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2390 check_blacklist (&n->id,
2391 GNUNET_TIME_absolute_get (),
2394 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2395 if (n->primary_address.session == session)
2397 /* ATS switches back to still-active session */
2398 free_address (&n->alternative_address);
2399 n->state = S_CONNECTED;
2402 /* ATS asks us to switch a life connection, update blacklist check */
2403 set_address (&n->alternative_address,
2404 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2405 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2406 check_blacklist (&n->id,
2407 GNUNET_TIME_absolute_get (),
2411 /* not going to switch addresses while disconnecting */
2413 case S_DISCONNECT_FINISHED:
2417 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2418 "Unhandled state `%s'\n",
2419 print_state (n->state));
2427 send_utilization_data (void *cls,
2428 const struct GNUNET_PeerIdentity *key,
2431 struct NeighbourMapEntry *n = value;
2432 struct GNUNET_ATS_Information atsi[4];
2434 uint32_t bps_pl_out;
2437 struct GNUNET_TIME_Relative delta;
2439 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2440 GNUNET_TIME_absolute_get ());
2444 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2445 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2447 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2448 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2450 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2455 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2456 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2458 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2459 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2463 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2467 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2468 atsi[0].value = htonl (bps_out);
2469 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2470 atsi[1].value = htonl (bps_in);
2472 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2473 atsi[2].value = htonl (bps_pl_out);
2474 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2475 atsi[3].value = htonl (bps_pl_in);
2477 GST_ats_update_metrics (key, n->primary_address.address,
2478 n->primary_address.session, atsi, 4);
2479 n->util_payload_bytes_recv = 0;
2480 n->util_payload_bytes_sent = 0;
2481 n->util_total_bytes_recv = 0;
2482 n->util_total_bytes_sent = 0;
2483 n->last_util_transmission = GNUNET_TIME_absolute_get();
2489 * Task transmitting utilization in a regular interval
2491 * @param cls the 'struct NeighbourMapEntry' for which we are running
2492 * @param tc scheduler context (unused)
2495 utilization_transmission (void *cls,
2496 const struct GNUNET_SCHEDULER_TaskContext *tc)
2498 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2500 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2501 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2503 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2504 utilization_transmission, NULL);
2510 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2511 const struct GNUNET_HELLO_Address *address,
2512 struct Session *session,
2513 const struct GNUNET_MessageHeader *message)
2515 struct NeighbourMapEntry *n;
2517 n = lookup_neighbour (peer);
2520 n->util_total_bytes_recv += ntohs(message->size);
2525 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2526 const struct GNUNET_HELLO_Address *address,
2527 struct Session *session,
2528 const struct GNUNET_MessageHeader *message)
2530 struct NeighbourMapEntry *n;
2531 n = lookup_neighbour (peer);
2534 n->util_payload_bytes_recv += ntohs(message->size);
2539 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2540 const struct GNUNET_HELLO_Address *address,
2541 struct Session *session,
2544 struct NeighbourMapEntry *n;
2545 n = lookup_neighbour (peer);
2548 if (n->primary_address.session != session)
2550 n->util_total_bytes_sent += size;
2555 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2558 struct NeighbourMapEntry *n;
2559 n = lookup_neighbour (peer);
2562 n->util_payload_bytes_sent += size;
2567 * Master task run for every neighbour. Performs all of the time-related
2568 * activities (keep alive, send next message, disconnect if idle, finish
2569 * clean up after disconnect).
2571 * @param cls the 'struct NeighbourMapEntry' for which we are running
2572 * @param tc scheduler context (unused)
2575 master_task (void *cls,
2576 const struct GNUNET_SCHEDULER_TaskContext *tc)
2578 struct NeighbourMapEntry *n = cls;
2579 struct GNUNET_TIME_Relative delay;
2581 n->task = GNUNET_SCHEDULER_NO_TASK;
2582 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2583 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2584 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2585 GNUNET_i2s (&n->id),
2586 print_state(n->state),
2587 GNUNET_STRINGS_relative_time_to_string (delay,
2591 case S_NOT_CONNECTED:
2592 /* invalid state for master task, clean up */
2594 n->state = S_DISCONNECT_FINISHED;
2595 free_neighbour (n, GNUNET_NO);
2598 if (0 == delay.rel_value_us)
2600 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2601 "Connection to `%s' timed out waiting for ATS to provide address\n",
2602 GNUNET_i2s (&n->id));
2603 n->state = S_DISCONNECT_FINISHED;
2604 free_neighbour (n, GNUNET_NO);
2608 case S_INIT_BLACKLIST:
2609 if (0 == delay.rel_value_us)
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2612 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2613 GNUNET_i2s (&n->id));
2614 n->state = S_DISCONNECT_FINISHED;
2615 free_neighbour (n, GNUNET_NO);
2619 case S_CONNECT_SENT:
2620 if (0 == delay.rel_value_us)
2622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2623 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2624 GNUNET_i2s (&n->id));
2625 disconnect_neighbour (n);
2629 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2630 if (0 == delay.rel_value_us)
2632 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2633 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2634 GNUNET_i2s (&n->id));
2635 n->state = S_DISCONNECT_FINISHED;
2636 free_neighbour (n, GNUNET_NO);
2640 case S_CONNECT_RECV_ATS:
2641 if (0 == delay.rel_value_us)
2643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2644 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2645 GNUNET_i2s (&n->id));
2646 n->state = S_DISCONNECT_FINISHED;
2647 free_neighbour (n, GNUNET_NO);
2651 case S_CONNECT_RECV_BLACKLIST:
2652 if (0 == delay.rel_value_us)
2654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2655 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2656 GNUNET_i2s (&n->id));
2657 n->state = S_DISCONNECT_FINISHED;
2658 free_neighbour (n, GNUNET_NO);
2662 case S_CONNECT_RECV_ACK:
2663 if (0 == delay.rel_value_us)
2665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2666 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2667 GNUNET_i2s (&n->id));
2668 disconnect_neighbour (n);
2673 if (0 == delay.rel_value_us)
2675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2676 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2677 GNUNET_i2s (&n->id));
2678 disconnect_neighbour (n);
2681 try_transmission_to_peer (n);
2684 case S_RECONNECT_ATS:
2685 if (0 == delay.rel_value_us)
2687 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2688 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2689 GNUNET_i2s (&n->id));
2690 disconnect_neighbour (n);
2694 case S_RECONNECT_BLACKLIST:
2695 if (0 == delay.rel_value_us)
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2699 GNUNET_i2s (&n->id));
2700 disconnect_neighbour (n);
2704 case S_RECONNECT_SENT:
2705 if (0 == delay.rel_value_us)
2707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2708 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2709 GNUNET_i2s (&n->id));
2710 disconnect_neighbour (n);
2714 case S_CONNECTED_SWITCHING_BLACKLIST:
2715 if (0 == delay.rel_value_us)
2717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2718 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2719 GNUNET_i2s (&n->id));
2720 disconnect_neighbour (n);
2723 try_transmission_to_peer (n);
2726 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2727 if (0 == delay.rel_value_us)
2729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2730 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2731 GNUNET_i2s (&n->id));
2732 disconnect_neighbour (n);
2735 try_transmission_to_peer (n);
2739 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2740 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2741 GNUNET_i2s (&n->id));
2742 free_neighbour (n, GNUNET_NO);
2744 case S_DISCONNECT_FINISHED:
2745 /* how did we get here!? */
2749 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2750 "Unhandled state `%s'\n",
2751 print_state (n->state));
2755 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2756 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2757 (S_CONNECTED == n->state) )
2759 /* if we are *now* in one of these three states, we're sending
2760 keep alive messages, so we need to consider the keepalive
2761 delay, not just the connection timeout */
2762 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2765 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2766 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2773 * Send a SESSION_ACK message to the neighbour to confirm that we
2774 * got his CONNECT_ACK.
2776 * @param n neighbour to send the SESSION_ACK to
2779 send_session_ack_message (struct NeighbourMapEntry *n)
2781 struct GNUNET_MessageHeader msg;
2783 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2784 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2785 (void) send_with_session(n,
2786 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2787 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2793 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2794 * Consider switching to it.
2796 * @param message possibly a 'struct SessionConnectMessage' (check format)
2797 * @param peer identity of the peer to switch the address for
2798 * @param address address of the other peer, NULL if other peer
2800 * @param session session to use (or NULL)
2801 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2804 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2805 const struct GNUNET_PeerIdentity *peer,
2806 const struct GNUNET_HELLO_Address *address,
2807 struct Session *session)
2809 const struct SessionConnectMessage *scm;
2810 struct GNUNET_TIME_Absolute ts;
2811 struct NeighbourMapEntry *n;
2813 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2814 "Received CONNECT_ACK message from peer `%s'\n",
2817 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2819 GNUNET_break_op (0);
2820 return GNUNET_SYSERR;
2822 GNUNET_STATISTICS_update (GST_stats,
2824 ("# CONNECT_ACK messages received"),
2826 scm = (const struct SessionConnectMessage *) message;
2827 GNUNET_break_op (ntohl (scm->reserved) == 0);
2828 if (NULL == (n = lookup_neighbour (peer)))
2830 GNUNET_STATISTICS_update (GST_stats,
2832 ("# unexpected CONNECT_ACK messages (no peer)"),
2834 return GNUNET_SYSERR;
2836 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2839 case S_NOT_CONNECTED:
2841 free_neighbour (n, GNUNET_NO);
2842 return GNUNET_SYSERR;
2844 case S_INIT_BLACKLIST:
2845 GNUNET_STATISTICS_update (GST_stats,
2847 ("# unexpected CONNECT_ACK messages (not ready)"),
2850 case S_CONNECT_SENT:
2851 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
2853 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2854 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
2857 n->state = S_CONNECTED;
2858 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2859 GNUNET_STATISTICS_set (GST_stats,
2860 gettext_noop ("# peers connected"),
2861 ++neighbours_connected,
2863 connect_notify_cb (callback_cls, &n->id,
2864 n->primary_address.bandwidth_in,
2865 n->primary_address.bandwidth_out);
2866 /* Tell ATS that the outbound session we created to send CONNECT was successfull */
2867 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
2868 set_address (&n->primary_address,
2869 n->primary_address.address,
2870 n->primary_address.session,
2871 n->primary_address.bandwidth_in,
2872 n->primary_address.bandwidth_out,
2874 send_session_ack_message (n);
2876 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2877 case S_CONNECT_RECV_ATS:
2878 case S_CONNECT_RECV_BLACKLIST:
2879 case S_CONNECT_RECV_ACK:
2880 GNUNET_STATISTICS_update (GST_stats,
2882 ("# unexpected CONNECT_ACK messages (not ready)"),
2886 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2887 send_session_ack_message (n);
2889 case S_RECONNECT_ATS:
2890 case S_RECONNECT_BLACKLIST:
2891 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2892 to give us a new address... */
2893 GNUNET_STATISTICS_update (GST_stats,
2895 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2898 case S_RECONNECT_SENT:
2899 /* new address worked; go back to connected! */
2900 n->state = S_CONNECTED;
2901 send_session_ack_message (n);
2903 case S_CONNECTED_SWITCHING_BLACKLIST:
2904 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2905 send_session_ack_message (n);
2907 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2908 /* new address worked; adopt it and go back to connected! */
2909 n->state = S_CONNECTED;
2910 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2911 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2913 GST_ats_add_address (n->alternative_address.address, n->alternative_address.session);
2914 set_address (&n->primary_address,
2915 n->alternative_address.address,
2916 n->alternative_address.session,
2917 n->alternative_address.bandwidth_in,
2918 n->alternative_address.bandwidth_out,
2920 free_address (&n->alternative_address);
2921 send_session_ack_message (n);
2924 GNUNET_STATISTICS_update (GST_stats,
2926 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2928 return GNUNET_SYSERR;
2929 case S_DISCONNECT_FINISHED:
2933 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2934 "Unhandled state `%s'\n",
2935 print_state (n->state));
2937 return GNUNET_SYSERR;
2944 * A session was terminated. Take note; if needed, try to get
2945 * an alternative address from ATS.
2947 * @param peer identity of the peer where the session died
2948 * @param session session that is gone
2949 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
2950 * this session was not in use
2953 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2954 struct Session *session)
2956 struct NeighbourMapEntry *n;
2957 struct BlackListCheckContext *bcc;
2958 struct BlackListCheckContext *bcc_next;
2960 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2962 while (NULL != (bcc = bcc_next))
2964 bcc_next = bcc->next;
2965 if (bcc->na.session == session)
2967 if (NULL != bcc->bc)
2968 GST_blacklist_test_cancel (bcc->bc);
2969 GNUNET_HELLO_address_free (bcc->na.address);
2970 GNUNET_CONTAINER_DLL_remove (bc_head,
2976 if (NULL == (n = lookup_neighbour (peer)))
2977 return GNUNET_NO; /* can't affect us */
2978 if (session != n->primary_address.session)
2980 if (session == n->alternative_address.session)
2982 free_address (&n->alternative_address);
2983 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2984 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2985 n->state = S_CONNECTED;
2989 return GNUNET_NO; /* doesn't affect us further */
2992 n->expect_latency_response = GNUNET_NO;
2995 case S_NOT_CONNECTED:
2997 free_neighbour (n, GNUNET_NO);
3001 free_neighbour (n, GNUNET_NO);
3003 case S_INIT_BLACKLIST:
3004 case S_CONNECT_SENT:
3005 free_address (&n->primary_address);
3006 n->state = S_INIT_ATS;
3007 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3008 // FIXME: need to ask ATS for suggestions again?
3009 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3011 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3012 case S_CONNECT_RECV_ATS:
3013 case S_CONNECT_RECV_BLACKLIST:
3014 case S_CONNECT_RECV_ACK:
3015 /* error on inbound session; free neighbour entirely */
3016 free_address (&n->primary_address);
3017 free_neighbour (n, GNUNET_NO);
3020 free_address (&n->primary_address);
3021 n->state = S_RECONNECT_ATS;
3022 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3023 /* FIXME: is this ATS call needed? */
3024 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3026 case S_RECONNECT_ATS:
3027 /* we don't have an address, how can it go down? */
3030 case S_RECONNECT_BLACKLIST:
3031 case S_RECONNECT_SENT:
3032 n->state = S_RECONNECT_ATS;
3033 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3034 // FIXME: need to ask ATS for suggestions again?
3035 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3037 case S_CONNECTED_SWITCHING_BLACKLIST:
3038 /* primary went down while we were checking secondary against
3039 blacklist, adopt secondary as primary */
3040 free_address (&n->primary_address);
3041 n->primary_address = n->alternative_address;
3042 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3043 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3044 n->state = S_RECONNECT_BLACKLIST;
3046 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3047 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3048 secondary as primary */
3049 free_address (&n->primary_address);
3050 n->primary_address = n->alternative_address;
3051 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3052 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3053 n->state = S_RECONNECT_SENT;
3056 free_address (&n->primary_address);
3058 case S_DISCONNECT_FINISHED:
3059 /* neighbour was freed and plugins told to terminate session */
3063 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3064 "Unhandled state `%s'\n",
3065 print_state (n->state));
3069 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3070 GNUNET_SCHEDULER_cancel (n->task);
3071 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3077 * We received a 'SESSION_ACK' message from the other peer.
3078 * If we sent a 'CONNECT_ACK' last, this means we are now
3079 * connected. Otherwise, do nothing.
3081 * @param message possibly a 'struct SessionConnectMessage' (check format)
3082 * @param peer identity of the peer to switch the address for
3083 * @param address address of the other peer, NULL if other peer
3085 * @param session session to use (or NULL)
3086 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3089 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3090 const struct GNUNET_PeerIdentity *peer,
3091 const struct GNUNET_HELLO_Address *address,
3092 struct Session *session)
3094 struct NeighbourMapEntry *n;
3096 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3097 "Received SESSION_ACK message from peer `%s'\n",
3099 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3101 GNUNET_break_op (0);
3102 return GNUNET_SYSERR;
3104 GNUNET_STATISTICS_update (GST_stats,
3106 ("# SESSION_ACK messages received"),
3108 if (NULL == (n = lookup_neighbour (peer)))
3109 return GNUNET_SYSERR;
3110 /* check if we are in a plausible state for having sent
3111 a CONNECT_ACK. If not, return, otherwise break */
3112 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3113 (S_CONNECT_SENT != n->state) ) ||
3114 (2 != n->send_connect_ack) )
3116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3117 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3119 print_state (n->state),
3120 n->send_connect_ack);
3121 GNUNET_STATISTICS_update (GST_stats,
3122 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3124 return GNUNET_SYSERR;
3126 n->state = S_CONNECTED;
3127 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3128 GNUNET_STATISTICS_set (GST_stats,
3129 gettext_noop ("# peers connected"),
3130 ++neighbours_connected,
3132 connect_notify_cb (callback_cls, &n->id,
3133 n->primary_address.bandwidth_in,
3134 n->primary_address.bandwidth_out);
3136 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
3137 set_address (&n->primary_address,
3138 n->primary_address.address,
3139 n->primary_address.session,
3140 n->primary_address.bandwidth_in,
3141 n->primary_address.bandwidth_out,
3148 * Test if we're connected to the given peer.
3150 * @param target peer to test
3151 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3154 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3156 return test_connected (lookup_neighbour (target));
3161 * Change the incoming quota for the given peer.
3163 * @param neighbour identity of peer to change qutoa for
3164 * @param quota new quota
3167 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3168 struct GNUNET_BANDWIDTH_Value32NBO quota)
3170 struct NeighbourMapEntry *n;
3172 if (NULL == (n = lookup_neighbour (neighbour)))
3174 GNUNET_STATISTICS_update (GST_stats,
3176 ("# SET QUOTA messages ignored (no such peer)"),
3180 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3181 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3182 ntohl (quota.value__), GNUNET_i2s (&n->id));
3183 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3184 if (0 != ntohl (quota.value__))
3186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
3187 GNUNET_i2s (&n->id), "SET_QUOTA");
3188 if (GNUNET_YES == test_connected (n))
3189 GNUNET_STATISTICS_update (GST_stats,
3190 gettext_noop ("# disconnects due to quota of 0"),
3192 disconnect_neighbour (n);
3197 * We received a disconnect message from the given peer,
3198 * validate and process.
3200 * @param peer sender of the message
3201 * @param msg the disconnect message
3204 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
3206 const struct GNUNET_MessageHeader
3209 struct NeighbourMapEntry *n;
3210 const struct SessionDisconnectMessage *sdm;
3212 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3213 "Received DISCONNECT message from peer `%s'\n",
3215 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3217 // GNUNET_break_op (0);
3218 GNUNET_STATISTICS_update (GST_stats,
3220 ("# disconnect messages ignored (old format)"), 1,
3224 GNUNET_STATISTICS_update (GST_stats,
3226 ("# DISCONNECT messages received"),
3228 sdm = (const struct SessionDisconnectMessage *) msg;
3229 if (NULL == (n = lookup_neighbour (peer)))
3230 return; /* gone already */
3231 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3233 GNUNET_STATISTICS_update (GST_stats,
3235 ("# disconnect messages ignored (timestamp)"), 1,
3239 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3241 GNUNET_break_op (0);
3244 if (ntohl (sdm->purpose.size) !=
3245 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3246 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3247 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3250 "%s message from peer `%s' has invalid size \n",
3253 GNUNET_break_op (0);
3257 GNUNET_CRYPTO_eddsa_verify
3258 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3259 &sdm->signature, &sdm->public_key))
3261 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3262 "%s message from peer `%s' cannot be verified \n",
3265 GNUNET_break_op (0);
3268 if (GNUNET_YES == test_connected (n))
3269 GNUNET_STATISTICS_update (GST_stats,
3271 ("# other peer asked to disconnect from us"), 1,
3273 disconnect_neighbour (n);
3278 * Closure for the neighbours_iterate function.
3280 struct IteratorContext
3283 * Function to call on each connected neighbour.
3285 GST_NeighbourIterator cb;
3295 * Call the callback from the closure for each connected neighbour.
3297 * @param cls the `struct IteratorContext`
3298 * @param key the hash of the public key of the neighbour
3299 * @param value the `struct NeighbourMapEntry`
3300 * @return #GNUNET_OK (continue to iterate)
3303 neighbours_iterate (void *cls,
3304 const struct GNUNET_PeerIdentity *key,
3307 struct IteratorContext *ic = cls;
3308 struct NeighbourMapEntry *n = value;
3309 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3310 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3312 if (GNUNET_YES != test_connected (n))
3315 if (NULL != n->primary_address.address)
3317 bandwidth_in = n->primary_address.bandwidth_in;
3318 bandwidth_out = n->primary_address.bandwidth_out;
3322 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3323 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3325 ic->cb (ic->cb_cls, &n->id,
3326 n->primary_address.address,
3327 bandwidth_in, bandwidth_out);
3333 * Iterate over all connected neighbours.
3335 * @param cb function to call
3336 * @param cb_cls closure for cb
3339 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3341 struct IteratorContext ic;
3343 if (NULL == neighbours)
3344 return; /* can happen during shutdown */
3347 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3352 * If we have an active connection to the given target, it must be shutdown.
3354 * @param target peer to disconnect from
3357 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3359 struct NeighbourMapEntry *n;
3361 if (NULL == (n = lookup_neighbour (target)))
3362 return; /* not active */
3363 if (GNUNET_YES == test_connected (n))
3364 GNUNET_STATISTICS_update (GST_stats,
3366 ("# disconnected from peer upon explicit request"), 1,
3368 disconnect_neighbour (n);
3373 * Obtain current latency information for the given neighbour.
3375 * @param peer to get the latency for
3376 * @return observed latency of the address, FOREVER if the
3377 * the connection is not up
3379 struct GNUNET_TIME_Relative
3380 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3382 struct NeighbourMapEntry *n;
3384 n = lookup_neighbour (peer);
3386 return GNUNET_TIME_UNIT_FOREVER_REL;
3390 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3391 case S_CONNECTED_SWITCHING_BLACKLIST:
3392 case S_RECONNECT_SENT:
3393 case S_RECONNECT_ATS:
3394 case S_RECONNECT_BLACKLIST:
3396 case S_NOT_CONNECTED:
3397 case S_INIT_BLACKLIST:
3399 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3400 case S_CONNECT_RECV_ATS:
3401 case S_CONNECT_RECV_BLACKLIST:
3402 case S_CONNECT_RECV_ACK:
3403 case S_CONNECT_SENT:
3405 case S_DISCONNECT_FINISHED:
3406 return GNUNET_TIME_UNIT_FOREVER_REL;
3408 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3409 "Unhandled state `%s'\n",
3410 print_state (n->state));
3414 return GNUNET_TIME_UNIT_FOREVER_REL;
3419 * Obtain current address information for the given neighbour.
3422 * @return address currently used
3424 struct GNUNET_HELLO_Address *
3425 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3427 struct NeighbourMapEntry *n;
3429 n = lookup_neighbour (peer);
3432 return n->primary_address.address;
3437 * Initialize the neighbours subsystem.
3439 * @param cls closure for callbacks
3440 * @param connect_cb function to call if we connect to a peer
3441 * @param disconnect_cb function to call if we disconnect from a peer
3442 * @param peer_address_cb function to call if we change an active address
3444 * @param max_fds maximum number of fds to use
3447 GST_neighbours_start (void *cls,
3448 NotifyConnect connect_cb,
3449 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3450 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3451 unsigned int max_fds)
3454 connect_notify_cb = connect_cb;
3455 disconnect_notify_cb = disconnect_cb;
3456 address_change_cb = peer_address_cb;
3457 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3458 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3459 utilization_transmission, NULL);
3464 * Disconnect from the given neighbour.
3467 * @param key hash of neighbour's public key (not used)
3468 * @param value the 'struct NeighbourMapEntry' of the neighbour
3469 * @return #GNUNET_OK (continue to iterate)
3472 disconnect_all_neighbours (void *cls,
3473 const struct GNUNET_PeerIdentity *key,
3476 struct NeighbourMapEntry *n = value;
3478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3479 "Disconnecting peer `%4s', %s\n",
3480 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3481 n->state = S_DISCONNECT_FINISHED;
3482 free_neighbour (n, GNUNET_NO);
3488 * Cleanup the neighbours subsystem.
3491 GST_neighbours_stop ()
3493 if (NULL == neighbours)
3495 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3497 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3498 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3501 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3502 &disconnect_all_neighbours,
3504 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3506 callback_cls = NULL;
3507 connect_notify_cb = NULL;
3508 disconnect_notify_cb = NULL;
3509 address_change_cb = NULL;
3513 /* end of file gnunet-service-transport_neighbours.c */