2 This file is part of GNUnet.
3 (C) 2010-2013 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file transport/gnunet-service-transport_neighbours.c
23 * @brief neighbour management
24 * @author Christian Grothoff
27 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport_clients.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
41 * Size of the neighbour hash map.
43 #define NEIGHBOUR_TABLE_SIZE 256
46 * Time we give plugin to transmit DISCONNECT message before the
47 * neighbour entry self-destructs.
49 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
52 * How often must a peer violate bandwidth quotas before we start
53 * to simply drop its messages?
55 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
58 * How often do we send KEEPALIVE messages to each of our neighbours and measure
59 * the latency with this neighbour?
60 * (idle timeout is 5 minutes or 300 seconds, so with 100s interval we
61 * send 3 keepalives in each interval, so 3 messages would need to be
62 * lost in a row for a disconnect).
64 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
67 * How long are we willing to wait for a response from ATS before timing out?
69 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 5000)
72 * How long are we willing to wait for an ACK from the other peer before
73 * giving up on our connect operation?
75 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
78 * How long are we willing to wait for a successful reconnect if
79 * an existing connection went down? Much shorter than the
80 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
81 * higher layers about the disconnect during this period.
83 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
86 * How long are we willing to wait for a response from the blacklist
87 * subsystem before timing out?
89 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
92 * Interval to send utilization data
94 #define UTIL_TRANSMISSION_INTERVAL GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
96 GNUNET_NETWORK_STRUCT_BEGIN
99 * Message a peer sends to another to indicate that it intends to
100 * setup a connection/session for data exchange. A 'SESSION_CONNECT'
101 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
102 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
103 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
104 * should be connected.
106 struct SessionConnectMessage
109 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT
110 * or #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK
112 struct GNUNET_MessageHeader header;
117 uint32_t reserved GNUNET_PACKED;
120 * Absolute time at the sender. Only the most recent connect
121 * message implies which session is preferred by the sender.
123 struct GNUNET_TIME_AbsoluteNBO timestamp;
129 * Message we send to the other peer to notify him that we intentionally
130 * are disconnecting (to reduce timeouts). This is just a friendly
131 * notification, peers must not rely on always receiving disconnect
134 struct SessionDisconnectMessage
137 * Header of type #GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT
139 struct GNUNET_MessageHeader header;
144 uint32_t reserved GNUNET_PACKED;
147 * Purpose of the signature. Extends over the timestamp.
148 * Purpose should be #GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
150 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
153 * Absolute time at the sender. Only the most recent connect
154 * message implies which session is preferred by the sender.
156 struct GNUNET_TIME_AbsoluteNBO timestamp;
159 * Public key of the sender.
161 struct GNUNET_CRYPTO_EddsaPublicKey public_key;
164 * Signature of the peer that sends us the disconnect. Only
165 * valid if the timestamp is AFTER the timestamp from the
166 * corresponding 'CONNECT' message.
168 struct GNUNET_CRYPTO_EddsaSignature signature;
172 GNUNET_NETWORK_STRUCT_END
176 * For each neighbour we keep a list of messages
177 * that we still want to transmit to the neighbour.
183 * This is a doubly linked list.
185 struct MessageQueue *next;
188 * This is a doubly linked list.
190 struct MessageQueue *prev;
193 * Function to call once we're done.
195 GST_NeighbourSendContinuation cont;
198 * Closure for @e cont
203 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
204 * stuck together in memory. Allocated at the end of this struct.
206 const char *message_buf;
209 * Size of the message buf
211 size_t message_buf_size;
214 * At what time should we fail?
216 struct GNUNET_TIME_Absolute timeout;
222 * Possible state of a neighbour. Initially, we are #S_NOT_CONNECTED.
224 * Then, there are two main paths. If we receive a CONNECT message, we
225 * first run a check against the blacklist (#S_CONNECT_RECV_BLACKLIST_INBOUND).
226 * If this check is successful, we give the inbound address to ATS.
227 * After the check we ask ATS for a suggestion (S_CONNECT_RECV_ATS).
228 * If ATS makes a suggestion, we ALSO give that suggestion to the blacklist
229 * (#S_CONNECT_RECV_BLACKLIST). Once the blacklist approves the
230 * address we got from ATS, we send our CONNECT_ACK and go to
231 * #S_CONNECT_RECV_ACK. If we receive a SESSION_ACK, we go to
232 * #S_CONNECTED (and notify everyone about the new connection). If the
233 * operation times out, we go to #S_DISCONNECT.
235 * The other case is where we transmit a CONNECT message first. We
236 * start with #S_INIT_ATS. If we get an address, we enter
237 * #S_INIT_BLACKLIST and check the blacklist. If the blacklist is OK
238 * with the connection, we actually send the CONNECT message and go to
239 * state S_CONNECT_SENT. Once we receive a CONNECT_ACK, we go to
240 * #S_CONNECTED (and notify everyone about the new connection and send
241 * back a SESSION_ACK). If the operation times out, we go to
244 * If the session is in trouble (i.e. transport-level disconnect or
245 * timeout), we go to #S_RECONNECT_ATS where we ask ATS for a new
246 * address (we don't notify anyone about the disconnect yet). Once we
247 * have a new address, we go to #S_RECONNECT_BLACKLIST to check the new
248 * address against the blacklist. If the blacklist approves, we enter
249 * #S_RECONNECT_SENT and send a CONNECT message. If we receive a
250 * CONNECT_ACK, we go to #S_CONNECTED and nobody noticed that we had
251 * trouble; we also send a SESSION_ACK at this time just in case. If
252 * the operation times out, we go to S_DISCONNECT (and notify everyone
253 * about the lost connection).
255 * If ATS decides to switch addresses while we have a normal
256 * connection, we go to #S_CONNECTED_SWITCHING_BLACKLIST to check the
257 * new address against the blacklist. If the blacklist approves, we
258 * go to #S_CONNECTED_SWITCHING_CONNECT_SENT and send a
259 * SESSION_CONNECT. If we get a SESSION_ACK back, we switch the
260 * primary connection to the suggested alternative from ATS, go back
261 * to #S_CONNECTED and send a SESSION_ACK to the other peer just to be
262 * sure. If the operation times out (or the blacklist disapproves),
263 * we go to #S_CONNECTED (and notify ATS that the given alternative
264 * address is "invalid").
266 * Once a session is in #S_DISCONNECT, it is cleaned up and then goes
267 * to (#S_DISCONNECT_FINISHED). If we receive an explicit disconnect
268 * request, we can go from any state to #S_DISCONNECT, possibly after
269 * generating disconnect notifications.
271 * Note that it is quite possible that while we are in any of these
272 * states, we could receive a 'CONNECT' request from the other peer.
273 * We then enter a 'weird' state where we pursue our own primary state
274 * machine (as described above), but with the 'send_connect_ack' flag
275 * set to 1. If our state machine allows us to send a 'CONNECT_ACK'
276 * (because we have an acceptable address), we send the 'CONNECT_ACK'
277 * and set the 'send_connect_ack' to 2. If we then receive a
278 * 'SESSION_ACK', we go to #S_CONNECTED (and reset 'send_connect_ack'
285 * fresh peer or completely disconnected
290 * Asked to initiate connection, trying to get address from ATS
295 * Asked to initiate connection, trying to get address approved
301 * Sent CONNECT message to other peer, waiting for CONNECT_ACK
306 * Received a CONNECT, do a blacklist check for inbound address
308 S_CONNECT_RECV_BLACKLIST_INBOUND,
311 * Received a CONNECT, asking ATS about address suggestions.
316 * Received CONNECT from other peer, got an address, checking with blacklist.
318 S_CONNECT_RECV_BLACKLIST,
321 * CONNECT request from other peer was SESSION_ACK'ed, waiting for
327 * Got our CONNECT_ACK/SESSION_ACK, connection is up.
332 * Connection got into trouble, rest of the system still believes
333 * it to be up, but we're getting a new address from ATS.
338 * Connection got into trouble, rest of the system still believes
339 * it to be up; we are checking the new address against the blacklist.
341 S_RECONNECT_BLACKLIST,
344 * Sent CONNECT over new address (either by ATS telling us to switch
345 * addresses or from RECONNECT_ATS); if this fails, we need to tell
346 * the rest of the system about a disconnect.
351 * We have some primary connection, but ATS suggested we switch
352 * to some alternative; we're now checking the alternative against
355 S_CONNECTED_SWITCHING_BLACKLIST,
358 * We have some primary connection, but ATS suggested we switch
359 * to some alternative; we now sent a CONNECT message for the
360 * alternative session to the other peer and waiting for a
361 * CONNECT_ACK to make this our primary connection.
363 S_CONNECTED_SWITCHING_CONNECT_SENT,
366 * Disconnect in progress (we're sending the DISCONNECT message to the
367 * other peer; after that is finished, the state will be cleaned up).
372 * We're finished with the disconnect; and are cleaning up the state
373 * now! We put the struct into this state when we are really in the
374 * task that calls 'free' on it and are about to remove the record
375 * from the map. We should never find a 'struct NeighbourMapEntry'
376 * in this state in the map. Accessing a 'struct NeighbourMapEntry'
377 * in this state virtually always means using memory that has been
378 * freed (the exception being the cleanup code in #free_neighbour()).
380 S_DISCONNECT_FINISHED
385 * A possible address we could use to communicate with a neighbour.
387 struct NeighbourAddress
391 * Active session for this address.
393 struct Session *session;
396 * Network-level address information.
398 struct GNUNET_HELLO_Address *address;
401 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
402 * peer for this address. Use to check that the ACK is in response
403 * to our most recent 'CONNECT'.
405 struct GNUNET_TIME_Absolute connect_timestamp;
408 * Inbound bandwidth from ATS for this address.
410 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
413 * Outbound bandwidth from ATS for this address.
415 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
418 * Did we tell ATS that this is our 'active' address?
426 * Entry in neighbours.
428 struct NeighbourMapEntry
432 * Head of list of messages we would like to send to this peer;
433 * must contain at most one message per client.
435 struct MessageQueue *messages_head;
438 * Tail of list of messages we would like to send to this peer; must
439 * contain at most one message per client.
441 struct MessageQueue *messages_tail;
444 * Are we currently trying to send a message? If so, which one?
446 struct MessageQueue *is_active;
449 * Primary address we currently use to communicate with the neighbour.
451 struct NeighbourAddress primary_address;
454 * Alternative address currently under consideration for communicating
455 * with the neighbour.
457 struct NeighbourAddress alternative_address;
460 * Identity of this neighbour.
462 struct GNUNET_PeerIdentity id;
465 * Main task that drives this peer (timeouts, keepalives, etc.).
466 * Always runs the 'master_task'.
468 GNUNET_SCHEDULER_TaskIdentifier task;
471 * At what time should we sent the next keep-alive message?
473 struct GNUNET_TIME_Absolute keep_alive_time;
476 * At what time did we sent the last keep-alive message? Used
477 * to calculate round-trip time ("latency").
479 struct GNUNET_TIME_Absolute last_keep_alive_time;
482 * Timestamp we should include in our next CONNECT_ACK message.
483 * (only valid if 'send_connect_ack' is GNUNET_YES). Used to build
484 * our CONNECT_ACK message.
486 struct GNUNET_TIME_Absolute connect_ack_timestamp;
489 * ATS address suggest handle
491 struct GNUNET_ATS_SuggestHandle *suggest_handle;
494 * Time where we should cut the connection (timeout) if we don't
495 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
496 * if we are in S_CONNECTED).
498 struct GNUNET_TIME_Absolute timeout;
501 * Latest calculated latency value
503 struct GNUNET_TIME_Relative latency;
506 * Tracker for inbound bandwidth.
508 struct GNUNET_BANDWIDTH_Tracker in_tracker;
511 * How often has the other peer (recently) violated the inbound
512 * traffic limit? Incremented by 10 per violation, decremented by 1
513 * per non-violation (for each time interval).
515 unsigned int quota_violation_count;
518 * The current state of the peer.
523 * Did we sent an KEEP_ALIVE message and are we expecting a response?
525 int expect_latency_response;
528 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
529 * (once we have an address to use and the peer has been allowed by our
530 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
531 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
532 * a 'SESSION_ACK' (regardless of what our own state machine might say).
534 int send_connect_ack;
537 * Tracking utilization of outbound bandwidth
539 uint32_t util_payload_bytes_sent;
542 * Tracking utilization of inbound bandwidth
544 uint32_t util_payload_bytes_recv;
547 * Tracking utilization of outbound bandwidth
549 uint32_t util_total_bytes_sent;
552 * Tracking utilization of inbound bandwidth
554 uint32_t util_total_bytes_recv;
557 * Date of last utilization transmission
559 struct GNUNET_TIME_Absolute last_util_transmission;
564 * Context for blacklist checks and the 'handle_test_blacklist_cont'
565 * function. Stores information about ongoing blacklist checks.
567 struct BlackListCheckContext
571 * We keep blacklist checks in a DLL.
573 struct BlackListCheckContext *next;
576 * We keep blacklist checks in a DLL.
578 struct BlackListCheckContext *prev;
581 * Address that is being checked.
583 struct NeighbourAddress na;
586 * Handle to the ongoing blacklist check.
588 struct GST_BlacklistCheck *bc;
593 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
595 static struct GNUNET_CONTAINER_MultiPeerMap *neighbours;
598 * We keep blacklist checks in a DLL so that we can find
599 * the 'sessions' in their 'struct NeighbourAddress' if
600 * a session goes down.
602 static struct BlackListCheckContext *bc_head;
605 * We keep blacklist checks in a DLL.
607 static struct BlackListCheckContext *bc_tail;
610 * Closure for #connect_notify_cb, #disconnect_notify_cb and #address_change_cb
612 static void *callback_cls;
615 * Function to call when we connected to a neighbour.
617 static NotifyConnect connect_notify_cb;
620 * Function to call when we disconnected from a neighbour.
622 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
625 * Function to call when we changed an active address of a neighbour.
627 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
630 * counter for connected neighbours
632 static unsigned int neighbours_connected;
635 * Number of bytes we have currently queued for transmission.
637 static unsigned long long bytes_in_send_queue;
640 * Task transmitting utilization data
642 static GNUNET_SCHEDULER_TaskIdentifier util_transmission_tk;
646 * Lookup a neighbour entry in the neighbours hash map.
648 * @param pid identity of the peer to look up
649 * @return the entry, NULL if there is no existing record
651 static struct NeighbourMapEntry *
652 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
654 if (NULL == neighbours)
656 return GNUNET_CONTAINER_multipeermap_get (neighbours, pid);
661 print_state (int state)
666 case S_NOT_CONNECTED:
667 return "S_NOT_CONNECTED";
670 case S_INIT_BLACKLIST:
671 return "S_INIT_BLACKLIST";
673 return "S_CONNECT_SENT";
674 case S_CONNECT_RECV_BLACKLIST_INBOUND:
675 return "S_CONNECT_RECV_BLACKLIST_INBOUND";
676 case S_CONNECT_RECV_ATS:
677 return "S_CONNECT_RECV_ATS";
678 case S_CONNECT_RECV_BLACKLIST:
679 return "S_CONNECT_RECV_BLACKLIST";
680 case S_CONNECT_RECV_ACK:
681 return "S_CONNECT_RECV_ACK";
683 return "S_CONNECTED";
684 case S_RECONNECT_ATS:
685 return "S_RECONNECT_ATS";
686 case S_RECONNECT_BLACKLIST:
687 return "S_RECONNECT_BLACKLIST";
688 case S_RECONNECT_SENT:
689 return "S_RECONNECT_SENT";
690 case S_CONNECTED_SWITCHING_BLACKLIST:
691 return "S_CONNECTED_SWITCHING_BLACKLIST";
692 case S_CONNECTED_SWITCHING_CONNECT_SENT:
693 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
695 return "S_DISCONNECT";
696 case S_DISCONNECT_FINISHED:
697 return "S_DISCONNECT_FINISHED";
705 * Test if we're connected to the given peer.
707 * @param n neighbour entry of peer to test
708 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
711 test_connected (struct NeighbourMapEntry *n)
717 case S_NOT_CONNECTED:
719 case S_INIT_BLACKLIST:
721 case S_CONNECT_RECV_BLACKLIST_INBOUND:
722 case S_CONNECT_RECV_ATS:
723 case S_CONNECT_RECV_BLACKLIST:
724 case S_CONNECT_RECV_ACK:
727 case S_RECONNECT_ATS:
728 case S_RECONNECT_BLACKLIST:
729 case S_RECONNECT_SENT:
730 case S_CONNECTED_SWITCHING_BLACKLIST:
731 case S_CONNECTED_SWITCHING_CONNECT_SENT:
734 case S_DISCONNECT_FINISHED:
737 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
738 "Unhandled state `%s' \n",
739 print_state (n->state));
743 return GNUNET_SYSERR;
747 * Send information about a new outbound quota to our clients.
749 * @param target affected peer
750 * @param quota new quota
753 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
754 struct GNUNET_BANDWIDTH_Value32NBO quota)
756 struct QuotaSetMessage q_msg;
758 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
759 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
760 ntohl (quota.value__), GNUNET_i2s (target));
761 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
762 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
764 q_msg.peer = (*target);
765 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
770 * We don't need a given neighbour address any more.
771 * Release its resources and give appropriate notifications
772 * to ATS and other subsystems.
774 * @param na address we are done with; @a na itself must NOT be 'free'd, only the contents!
777 free_address (struct NeighbourAddress *na)
779 if (GNUNET_YES == na->ats_active)
781 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
782 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
783 address_change_cb (callback_cls, &na->address->peer, NULL);
786 na->ats_active = GNUNET_NO;
787 if (NULL != na->address)
789 GNUNET_HELLO_address_free (na->address);
797 * Initialize the 'struct NeighbourAddress'.
799 * @param na neighbour address to initialize
800 * @param address address of the other peer, NULL if other peer
802 * @param session session to use (or NULL, in which case an
803 * address must be setup)
804 * @param bandwidth_in inbound quota to be used when connection is up
805 * @param bandwidth_out outbound quota to be used when connection is up
806 * @param is_active #GNUNET_YES to mark this as the active address with ATS
809 set_address (struct NeighbourAddress *na,
810 const struct GNUNET_HELLO_Address *address,
811 struct Session *session,
812 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
813 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
816 struct GNUNET_TRANSPORT_PluginFunctions *papi;
818 if (NULL == (papi = GST_plugins_find (address->transport_name)))
823 if (session == na->session)
825 na->bandwidth_in = bandwidth_in;
826 na->bandwidth_out = bandwidth_out;
827 if (is_active != na->ats_active)
829 na->ats_active = is_active;
830 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
831 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
833 address_change_cb (callback_cls, &address->peer, address);
835 if (GNUNET_YES == is_active)
837 /* FIXME: is this the right place to set quotas? */
838 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
839 send_outbound_quota (&address->peer, bandwidth_out);
845 session = papi->get_session (papi->cls, address);
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Failed to obtain new session for peer `%s' and address '%s'\n",
850 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
851 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
854 na->address = GNUNET_HELLO_address_copy (address);
855 na->bandwidth_in = bandwidth_in;
856 na->bandwidth_out = bandwidth_out;
857 na->session = session;
858 na->ats_active = is_active;
859 if (GNUNET_YES == is_active)
861 /* Telling ATS about new session */
862 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
863 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
864 address_change_cb (callback_cls, &address->peer, address);
865 /* FIXME: is this the right place to set quotas? */
866 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
867 send_outbound_quota (&address->peer, bandwidth_out);
873 * Free a neighbour map entry.
875 * @param n entry to free
876 * @param keep_sessions #GNUNET_NO to tell plugin to terminate sessions,
877 * #GNUNET_YES to keep all sessions
880 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
882 struct MessageQueue *mq;
883 struct GNUNET_TRANSPORT_PluginFunctions *papi;
884 struct GNUNET_HELLO_Address *backup_primary;
886 n->is_active = NULL; /* always free'd by its own continuation! */
888 /* fail messages currently in the queue */
889 while (NULL != (mq = n->messages_head))
891 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
892 if (NULL != mq->cont)
893 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
896 /* It is too late to send other peer disconnect notifications, but at
897 least internally we need to get clean... */
898 if (GNUNET_YES == test_connected (n))
900 GNUNET_STATISTICS_set (GST_stats,
901 gettext_noop ("# peers connected"),
902 --neighbours_connected,
904 disconnect_notify_cb (callback_cls, &n->id);
906 n->state = S_DISCONNECT_FINISHED;
908 if (NULL != n->primary_address.address)
910 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
913 backup_primary = NULL;
915 /* free addresses and mark as unused */
916 free_address (&n->primary_address);
917 free_address (&n->alternative_address);
919 /* FIXME: Note that if we are switching between two TCP sessions to
920 the same peer, we might want to selectively kill only one of
921 them! Killing all sessions like this seems to be very, very
924 /* cut transport-level connection */
925 if ((GNUNET_NO == keep_sessions) &&
926 (NULL != backup_primary) &&
927 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
928 papi->disconnect_peer (papi->cls, &n->id);
930 GNUNET_free_non_null (backup_primary);
932 GNUNET_assert (GNUNET_YES ==
933 GNUNET_CONTAINER_multipeermap_remove (neighbours,
936 // FIXME-ATS-API: we might want to be more specific about
937 // which states we do this from in the future (ATS should
938 // have given us a 'suggest_address' handle, and if we have
939 // such a handle, we should cancel the operation here!
940 if (NULL != n->suggest_handle)
942 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
943 n->suggest_handle = NULL;
946 if (GNUNET_SCHEDULER_NO_TASK != n->task)
948 GNUNET_SCHEDULER_cancel (n->task);
949 n->task = GNUNET_SCHEDULER_NO_TASK;
951 /* free rest of memory */
957 * Transmit a message using the current session of the given
960 * @param n entry for the recipient
961 * @param msgbuf buffer to transmit
962 * @param msgbuf_size number of bytes in buffer
963 * @param priority transmission priority
964 * @param timeout transmission timeout
965 * @param cont continuation to call when finished (can be NULL)
966 * @param cont_cls closure for cont
969 send_with_session (struct NeighbourMapEntry *n,
970 const char *msgbuf, size_t msgbuf_size,
972 struct GNUNET_TIME_Relative timeout,
973 GNUNET_TRANSPORT_TransmitContinuation cont,
976 struct GNUNET_TRANSPORT_PluginFunctions *papi;
978 GNUNET_assert (n->primary_address.session != NULL);
979 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
980 (-1 == papi->send (papi->cls,
981 n->primary_address.session,
985 cont, cont_cls)))) &&
987 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
988 GST_neighbours_notify_data_sent (&n->id,
989 n->primary_address.address, n->primary_address.session, msgbuf_size);
990 GNUNET_break (NULL != papi);
995 * Master task run for every neighbour. Performs all of the time-related
996 * activities (keep alive, send next message, disconnect if idle, finish
997 * clean up after disconnect).
999 * @param cls the 'struct NeighbourMapEntry' for which we are running
1000 * @param tc scheduler context (unused)
1003 master_task (void *cls,
1004 const struct GNUNET_SCHEDULER_TaskContext *tc);
1008 * Function called when the 'DISCONNECT' message has been sent by the
1009 * plugin. Frees the neighbour --- if the entry still exists.
1012 * @param target identity of the neighbour that was disconnected
1013 * @param result #GNUNET_OK if the disconnect got out successfully
1014 * @param payload bytes payload
1015 * @param physical bytes physical
1018 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1019 int result, size_t payload, size_t physical)
1021 struct NeighbourMapEntry *n;
1023 n = lookup_neighbour (target);
1025 return; /* already gone */
1026 if (S_DISCONNECT != n->state)
1027 return; /* have created a fresh entry since */
1028 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1029 GNUNET_SCHEDULER_cancel (n->task);
1030 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1035 * Transmit a DISCONNECT message to the other peer.
1037 * @param n neighbour to send DISCONNECT message.
1040 send_disconnect (struct NeighbourMapEntry *n)
1042 struct SessionDisconnectMessage disconnect_msg;
1044 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1045 "Sending DISCONNECT message to peer `%4s'\n",
1046 GNUNET_i2s (&n->id));
1047 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1048 disconnect_msg.header.type =
1049 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1050 disconnect_msg.reserved = htonl (0);
1051 disconnect_msg.purpose.size =
1052 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1053 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1054 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1055 disconnect_msg.purpose.purpose =
1056 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1057 disconnect_msg.timestamp =
1058 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1059 disconnect_msg.public_key = GST_my_identity.public_key;
1060 GNUNET_assert (GNUNET_OK ==
1061 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1062 &disconnect_msg.purpose,
1063 &disconnect_msg.signature));
1065 send_with_session (n,
1066 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1067 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1068 &send_disconnect_cont, NULL);
1069 GNUNET_STATISTICS_update (GST_stats,
1071 ("# DISCONNECT messages sent"), 1,
1077 * Disconnect from the given neighbour, clean up the record.
1079 * @param n neighbour to disconnect from
1082 disconnect_neighbour (struct NeighbourMapEntry *n)
1084 /* depending on state, notify neighbour and/or upper layers of this peer
1088 case S_NOT_CONNECTED:
1090 case S_INIT_BLACKLIST:
1091 /* other peer is completely unaware of us, no need to send DISCONNECT */
1092 n->state = S_DISCONNECT_FINISHED;
1093 free_neighbour (n, GNUNET_NO);
1095 case S_CONNECT_SENT:
1096 send_disconnect (n);
1097 n->state = S_DISCONNECT;
1099 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1100 case S_CONNECT_RECV_ATS:
1101 case S_CONNECT_RECV_BLACKLIST:
1102 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1103 n->state = S_DISCONNECT_FINISHED;
1104 free_neighbour (n, GNUNET_NO);
1106 case S_CONNECT_RECV_ACK:
1107 /* we DID ACK the other peer's request, must send DISCONNECT */
1108 send_disconnect (n);
1109 n->state = S_DISCONNECT;
1112 case S_RECONNECT_BLACKLIST:
1113 case S_RECONNECT_SENT:
1114 case S_CONNECTED_SWITCHING_BLACKLIST:
1115 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1116 /* we are currently connected, need to send disconnect and do
1117 internal notifications and update statistics */
1118 send_disconnect (n);
1119 GNUNET_STATISTICS_set (GST_stats,
1120 gettext_noop ("# peers connected"),
1121 --neighbours_connected,
1123 disconnect_notify_cb (callback_cls, &n->id);
1124 n->state = S_DISCONNECT;
1126 case S_RECONNECT_ATS:
1127 /* ATS address request timeout, disconnect without sending disconnect message */
1128 GNUNET_STATISTICS_set (GST_stats,
1129 gettext_noop ("# peers connected"),
1130 --neighbours_connected,
1132 disconnect_notify_cb (callback_cls, &n->id);
1133 n->state = S_DISCONNECT;
1136 /* already disconnected, ignore */
1138 case S_DISCONNECT_FINISHED:
1139 /* already cleaned up, how did we get here!? */
1143 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1144 "Unhandled state `%s'\n",
1145 print_state (n->state));
1149 /* schedule timeout to clean up */
1150 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1151 GNUNET_SCHEDULER_cancel (n->task);
1152 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1158 * We're done with our transmission attempt, continue processing.
1160 * @param cls the 'struct MessageQueue' of the message
1161 * @param receiver intended receiver
1162 * @param success whether it worked or not
1163 * @param size_payload bytes payload sent
1164 * @param physical bytes sent on wire
1167 transmit_send_continuation (void *cls,
1168 const struct GNUNET_PeerIdentity *receiver,
1169 int success, size_t size_payload, size_t physical)
1171 struct MessageQueue *mq = cls;
1172 struct NeighbourMapEntry *n;
1174 if (NULL == (n = lookup_neighbour (receiver)))
1177 return; /* disconnect or other error while transmitting, can happen */
1179 if (n->is_active == mq)
1181 /* this is still "our" neighbour, remove us from its queue
1182 and allow it to send the next message now */
1183 n->is_active = NULL;
1184 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1185 GNUNET_SCHEDULER_cancel (n->task);
1186 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1188 if (bytes_in_send_queue < mq->message_buf_size)
1190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1191 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1192 bytes_in_send_queue, mq->message_buf_size,
1193 (GNUNET_OK == success) ? "OK" : "FAIL",
1194 size_payload, physical);
1199 GNUNET_break (size_payload == mq->message_buf_size);
1200 bytes_in_send_queue -= mq->message_buf_size;
1201 GNUNET_STATISTICS_set (GST_stats,
1203 ("# bytes in message queue for other peers"),
1204 bytes_in_send_queue, GNUNET_NO);
1205 if (GNUNET_OK == success)
1206 GNUNET_STATISTICS_update (GST_stats,
1208 ("# messages transmitted to other peers"),
1211 GNUNET_STATISTICS_update (GST_stats,
1213 ("# transmission failures for messages to other peers"),
1215 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1216 "Sending message to `%s' of type %u was a %s\n",
1217 GNUNET_i2s (receiver),
1218 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1219 (success == GNUNET_OK) ? "success" : "FAILURE");
1220 if (NULL != mq->cont)
1221 mq->cont (mq->cont_cls, success, size_payload, physical);
1227 * Check the message list for the given neighbour and if we can
1228 * send a message, do so. This function should only be called
1229 * if the connection is at least generally ready for transmission.
1230 * While we will only send one message at a time, no bandwidth
1231 * quota management is performed here. If a message was given to
1232 * the plugin, the continuation will automatically re-schedule
1233 * the 'master' task once the next message might be transmitted.
1235 * @param n target peer for which to transmit
1238 try_transmission_to_peer (struct NeighbourMapEntry *n)
1240 struct MessageQueue *mq;
1241 struct GNUNET_TIME_Relative timeout;
1243 if (NULL == n->primary_address.address)
1245 /* no address, why are we here? */
1249 if ((0 == n->primary_address.address->address_length) &&
1250 (NULL == n->primary_address.session))
1252 /* no address, why are we here? */
1256 if (NULL != n->is_active)
1258 /* transmission already pending */
1262 /* timeout messages from the queue that are past their due date */
1263 while (NULL != (mq = n->messages_head))
1265 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1266 if (timeout.rel_value_us > 0)
1268 GNUNET_STATISTICS_update (GST_stats,
1270 ("# messages timed out while in transport queue"),
1272 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1274 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0); /* timeout */
1277 return; /* no more messages */
1278 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1280 send_with_session (n,
1281 mq->message_buf, mq->message_buf_size,
1282 0 /* priority */, timeout,
1283 &transmit_send_continuation, mq);
1288 * Send keepalive message to the neighbour. Must only be called
1289 * if we are on 'connected' state or while trying to switch addresses.
1290 * Will internally determine if a keepalive is truly needed (so can
1291 * always be called).
1293 * @param n neighbour that went idle and needs a keepalive
1296 send_keepalive (struct NeighbourMapEntry *n)
1298 struct GNUNET_MessageHeader m;
1300 GNUNET_assert ((S_CONNECTED == n->state) ||
1301 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1302 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1303 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1304 return; /* no keepalive needed at this time */
1305 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1306 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1307 send_with_session (n,
1308 (const void *) &m, sizeof (m),
1309 UINT32_MAX /* priority */,
1310 KEEPALIVE_FREQUENCY,
1312 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1314 n->expect_latency_response = GNUNET_YES;
1315 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1316 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1321 * Keep the connection to the given neighbour alive longer,
1322 * we received a KEEPALIVE (or equivalent); send a response.
1324 * @param neighbour neighbour to keep alive (by sending keep alive response)
1327 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1329 struct NeighbourMapEntry *n;
1330 struct GNUNET_MessageHeader m;
1332 if (NULL == (n = lookup_neighbour (neighbour)))
1334 GNUNET_STATISTICS_update (GST_stats,
1336 ("# KEEPALIVE messages discarded (peer unknown)"),
1340 if (NULL == n->primary_address.session)
1342 GNUNET_STATISTICS_update (GST_stats,
1344 ("# KEEPALIVE messages discarded (no session)"),
1348 /* send reply to allow neighbour to measure latency */
1349 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1350 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1351 send_with_session(n,
1352 (const void *) &m, sizeof (m),
1353 UINT32_MAX /* priority */,
1354 KEEPALIVE_FREQUENCY,
1360 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1361 * latency to this peer. Pass the updated information (existing ats
1362 * plus calculated latency) to ATS.
1364 * @param neighbour neighbour to keep alive
1367 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
1369 struct NeighbourMapEntry *n;
1371 struct GNUNET_ATS_Information ats;
1373 if (NULL == (n = lookup_neighbour (neighbour)))
1375 GNUNET_STATISTICS_update (GST_stats,
1377 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1381 if ( (S_CONNECTED != n->state) ||
1382 (GNUNET_YES != n->expect_latency_response) )
1384 GNUNET_STATISTICS_update (GST_stats,
1386 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1390 n->expect_latency_response = GNUNET_NO;
1391 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1392 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1394 "Latency for peer `%s' is %s\n",
1395 GNUNET_i2s (&n->id),
1396 GNUNET_STRINGS_relative_time_to_string (n->latency,
1398 /* append latency */
1399 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1400 if (n->latency.rel_value_us > UINT32_MAX)
1401 latency = UINT32_MAX;
1403 latency = n->latency.rel_value_us;
1404 ats.value = htonl (latency);
1405 GST_ats_update_metrics (&n->id,
1406 n->primary_address.address,
1407 n->primary_address.session,
1413 * We have received a message from the given sender. How long should
1414 * we delay before receiving more? (Also used to keep the peer marked
1417 * @param sender sender of the message
1418 * @param size size of the message
1419 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1420 * GNUNET_NO if the neighbour is not connected or violates the quota,
1421 * GNUNET_SYSERR if the connection is not fully up yet
1422 * @return how long to wait before reading more from this sender
1424 struct GNUNET_TIME_Relative
1425 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1426 *sender, ssize_t size, int *do_forward)
1428 struct NeighbourMapEntry *n;
1429 struct GNUNET_TIME_Relative ret;
1431 if (NULL == neighbours)
1433 *do_forward = GNUNET_NO;
1434 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1436 if (NULL == (n = lookup_neighbour (sender)))
1438 GST_neighbours_try_connect (sender);
1439 if (NULL == (n = lookup_neighbour (sender)))
1441 GNUNET_STATISTICS_update (GST_stats,
1443 ("# messages discarded due to lack of neighbour record"),
1445 *do_forward = GNUNET_NO;
1446 return GNUNET_TIME_UNIT_ZERO;
1449 if (! test_connected (n))
1451 *do_forward = GNUNET_SYSERR;
1452 return GNUNET_TIME_UNIT_ZERO;
1454 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1456 n->quota_violation_count++;
1457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1458 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1459 n->in_tracker.available_bytes_per_s__,
1460 n->quota_violation_count);
1461 /* Discount 32k per violation */
1462 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1466 if (n->quota_violation_count > 0)
1468 /* try to add 32k back */
1469 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1470 n->quota_violation_count--;
1473 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1475 GNUNET_STATISTICS_update (GST_stats,
1477 ("# bandwidth quota violations by other peers"),
1479 *do_forward = GNUNET_NO;
1480 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1482 *do_forward = GNUNET_YES;
1483 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1484 if (ret.rel_value_us > 0)
1486 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1487 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1488 (unsigned long long) n->in_tracker.
1489 consumption_since_last_update__,
1490 (unsigned int) n->in_tracker.available_bytes_per_s__,
1491 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1492 GNUNET_STATISTICS_update (GST_stats,
1493 gettext_noop ("# ms throttling suggested"),
1494 (int64_t) ret.rel_value_us / 1000LL,
1502 * Transmit a message to the given target using the active connection.
1504 * @param target destination
1505 * @param msg message to send
1506 * @param msg_size number of bytes in msg
1507 * @param timeout when to fail with timeout
1508 * @param cont function to call when done
1509 * @param cont_cls closure for 'cont'
1512 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1513 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1514 GST_NeighbourSendContinuation cont, void *cont_cls)
1516 struct NeighbourMapEntry *n;
1517 struct MessageQueue *mq;
1519 /* All ove these cases should never happen; they are all API violations.
1520 But we check anyway, just to be sure. */
1521 if (NULL == (n = lookup_neighbour (target)))
1525 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1528 if (GNUNET_YES != test_connected (n))
1532 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1535 bytes_in_send_queue += msg_size;
1536 GNUNET_STATISTICS_set (GST_stats,
1538 ("# bytes in message queue for other peers"),
1539 bytes_in_send_queue, GNUNET_NO);
1540 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1542 mq->cont_cls = cont_cls;
1543 memcpy (&mq[1], msg, msg_size);
1544 mq->message_buf = (const char *) &mq[1];
1545 mq->message_buf_size = msg_size;
1546 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1547 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1548 if ( (NULL != n->is_active) ||
1549 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1551 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1552 GNUNET_SCHEDULER_cancel (n->task);
1553 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1558 * Send a SESSION_CONNECT message via the given address.
1560 * @param na address to use
1563 send_session_connect (struct NeighbourAddress *na)
1565 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1566 struct SessionConnectMessage connect_msg;
1568 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1573 if (NULL == na->session)
1574 na->session = papi->get_session (papi->cls, na->address);
1575 if (NULL == na->session)
1580 GNUNET_STATISTICS_update (GST_stats,
1582 ("# SESSION_CONNECT messages sent"),
1584 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1585 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1586 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1587 connect_msg.reserved = htonl (0);
1588 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1590 papi->send (papi->cls,
1592 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1594 GNUNET_TIME_UNIT_FOREVER_REL,
1597 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1598 _("Failed to transmit CONNECT message via plugin to %s\n"),
1599 GST_plugins_a2s (na->address));
1601 GST_neighbours_notify_data_sent (&na->address->peer,
1604 sizeof (struct SessionConnectMessage));
1610 * Send a SESSION_CONNECT_ACK message via the given address.
1612 * @param address address to use
1613 * @param session session to use
1614 * @param timestamp timestamp to use for the ACK message
1617 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1618 struct Session *session,
1619 struct GNUNET_TIME_Absolute timestamp)
1621 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1622 struct SessionConnectMessage connect_msg;
1624 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1629 if (NULL == session)
1630 session = papi->get_session (papi->cls, address);
1631 if (NULL == session)
1636 GNUNET_STATISTICS_update (GST_stats,
1638 ("# CONNECT_ACK messages sent"),
1640 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1641 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1642 connect_msg.reserved = htonl (0);
1643 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1644 (void) papi->send (papi->cls,
1646 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1648 GNUNET_TIME_UNIT_FOREVER_REL,
1655 * Create a fresh entry in the neighbour map for the given peer
1657 * @param peer peer to create an entry for
1658 * @return new neighbour map entry
1660 static struct NeighbourMapEntry *
1661 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1663 struct NeighbourMapEntry *n;
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1666 "Creating new neighbour entry for `%s'\n",
1668 n = GNUNET_new (struct NeighbourMapEntry);
1670 n->state = S_NOT_CONNECTED;
1671 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1672 n->last_util_transmission = GNUNET_TIME_absolute_get();
1673 n->util_payload_bytes_recv = 0;
1674 n->util_payload_bytes_sent = 0;
1675 n->util_total_bytes_recv = 0;
1676 n->util_total_bytes_sent = 0;
1677 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1678 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1679 MAX_BANDWIDTH_CARRY_S);
1680 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1681 GNUNET_assert (GNUNET_OK ==
1682 GNUNET_CONTAINER_multipeermap_put (neighbours,
1684 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1690 * Check if the two given addresses are the same.
1691 * Actually only checks if the sessions are non-NULL
1692 * (which they should be) and then if they are identical;
1693 * the actual addresses don't matter if the session
1694 * pointers match anyway, and we must have session pointers
1697 * @param a1 first address to compare
1698 * @param a2 other address to compare
1699 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1702 address_matches (const struct NeighbourAddress *a1,
1703 const struct NeighbourAddress *a2)
1705 if ( (NULL == a1->session) ||
1706 (NULL == a2->session) )
1711 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1716 * Try to create a connection to the given target (eventually).
1718 * @param target peer to try to connect to
1721 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1723 struct NeighbourMapEntry *n;
1725 if (NULL == neighbours)
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1728 "Asked to connect to peer `%s' during shutdown\n",
1729 GNUNET_i2s (target));
1730 return; /* during shutdown, do nothing */
1732 n = lookup_neighbour (target);
1733 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1734 "Asked to connect to peer `%s' (state: %s)\n",
1735 GNUNET_i2s (target),
1736 (NULL != n) ? print_state(n->state) : "UNKNOWN PEER");
1741 case S_NOT_CONNECTED:
1742 /* this should not be possible */
1744 free_neighbour (n, GNUNET_NO);
1747 case S_INIT_BLACKLIST:
1748 case S_CONNECT_SENT:
1749 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1750 case S_CONNECT_RECV_ATS:
1751 case S_CONNECT_RECV_BLACKLIST:
1752 case S_CONNECT_RECV_ACK:
1753 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1754 "Ignoring request to try to connect to `%s', already trying!\n",
1755 GNUNET_i2s (target));
1756 return; /* already trying */
1758 case S_RECONNECT_ATS:
1759 case S_RECONNECT_BLACKLIST:
1760 case S_RECONNECT_SENT:
1761 case S_CONNECTED_SWITCHING_BLACKLIST:
1762 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1763 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1764 "Ignoring request to try to connect, already connected to `%s'!\n",
1765 GNUNET_i2s (target));
1766 return; /* already connected */
1768 /* get rid of remains, ready to re-try immediately */
1769 free_neighbour (n, GNUNET_NO);
1771 case S_DISCONNECT_FINISHED:
1772 /* should not be possible */
1775 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1776 "Unhandled state `%s'\n",
1777 print_state (n->state));
1779 free_neighbour (n, GNUNET_NO);
1783 n = setup_neighbour (target);
1784 n->state = S_INIT_ATS;
1785 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1787 GNUNET_ATS_reset_backoff (GST_ats, target);
1788 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1793 * Function called with the result of a blacklist check.
1795 * @param cls closure with the 'struct BlackListCheckContext'
1796 * @param peer peer this check affects
1797 * @param result #GNUNET_OK if the address is allowed
1800 handle_test_blacklist_cont (void *cls,
1801 const struct GNUNET_PeerIdentity *peer,
1804 struct BlackListCheckContext *bcc = cls;
1805 struct NeighbourMapEntry *n;
1808 GNUNET_CONTAINER_DLL_remove (bc_head,
1811 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1812 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1814 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1815 if (GNUNET_OK == result)
1816 GST_ats_add_address (bcc->na.address, bcc->na.session);
1819 /* Blacklist disagreed on connecting to a peer with this address
1820 * Destroy address because we are not allowed to use it
1822 if (NULL != bcc->na.session)
1823 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1824 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1826 if (NULL == (n = lookup_neighbour (peer)))
1827 goto cleanup; /* nobody left to care about new address */
1830 case S_NOT_CONNECTED:
1831 /* this should not be possible */
1833 free_neighbour (n, GNUNET_NO);
1836 /* waiting on ATS suggestion; still, pass address to ATS as a
1839 case S_INIT_BLACKLIST:
1840 /* check if the address the blacklist was fine with matches
1841 ATS suggestion, if so, we can move on! */
1842 if ( (GNUNET_OK == result) &&
1843 (1 == n->send_connect_ack) )
1845 n->send_connect_ack = 2;
1846 send_session_connect_ack_message (bcc->na.address,
1848 n->connect_ack_timestamp);
1850 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1851 break; /* result for an address we currently don't care about */
1852 if (GNUNET_OK == result)
1854 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1855 n->state = S_CONNECT_SENT;
1856 send_session_connect (&n->primary_address);
1860 free_address (&n->primary_address);
1861 n->state = S_INIT_ATS;
1862 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1865 case S_CONNECT_SENT:
1866 /* waiting on CONNECT_ACK, send ACK if one is pending */
1867 if ( (GNUNET_OK == result) &&
1868 (1 == n->send_connect_ack) )
1870 n->send_connect_ack = 2;
1871 send_session_connect_ack_message (n->primary_address.address,
1872 n->primary_address.session,
1873 n->connect_ack_timestamp);
1876 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1877 n->state = S_CONNECT_RECV_ATS;
1878 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1879 GNUNET_ATS_reset_backoff (GST_ats, peer);
1880 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
1882 case S_CONNECT_RECV_ATS:
1883 /* waiting on ATS suggestion, don't care about blacklist */
1885 case S_CONNECT_RECV_BLACKLIST:
1886 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1889 "Blacklist result ignored, as it is not for our primary address\n");
1890 break; /* result for an address we currently don't care about */
1892 if (GNUNET_OK == result)
1894 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1895 n->state = S_CONNECT_RECV_ACK;
1896 send_session_connect_ack_message (bcc->na.address,
1898 n->connect_ack_timestamp);
1899 if (1 == n->send_connect_ack)
1900 n->send_connect_ack = 2;
1904 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
1906 plugin = GST_plugins_find (bcc->na.address->transport_name);
1907 if ( (NULL != plugin) &&
1908 (NULL != bcc->na.session) )
1910 plugin->disconnect_session (plugin->cls,
1914 GNUNET_break (NULL != plugin);
1915 free_address (&n->primary_address);
1916 n->state = S_INIT_ATS;
1917 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1918 GNUNET_ATS_reset_backoff (GST_ats, peer);
1921 case S_CONNECT_RECV_ACK:
1922 /* waiting on SESSION_ACK, send ACK if one is pending */
1923 if ( (GNUNET_OK == result) &&
1924 (1 == n->send_connect_ack) )
1926 n->send_connect_ack = 2;
1927 send_session_connect_ack_message (n->primary_address.address,
1928 n->primary_address.session,
1929 n->connect_ack_timestamp);
1933 /* already connected, don't care about blacklist */
1935 case S_RECONNECT_ATS:
1936 /* still waiting on ATS suggestion, don't care about blacklist */
1938 case S_RECONNECT_BLACKLIST:
1939 if ( (GNUNET_OK == result) &&
1940 (1 == n->send_connect_ack) )
1942 n->send_connect_ack = 2;
1943 send_session_connect_ack_message (bcc->na.address,
1945 n->connect_ack_timestamp);
1947 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1948 break; /* result for an address we currently don't care about */
1949 if (GNUNET_OK == result)
1951 send_session_connect (&n->primary_address);
1952 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1953 n->state = S_RECONNECT_SENT;
1957 n->state = S_RECONNECT_ATS;
1958 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1961 case S_RECONNECT_SENT:
1962 /* waiting on CONNECT_ACK, don't care about blacklist */
1963 if ( (GNUNET_OK == result) &&
1964 (1 == n->send_connect_ack) )
1966 n->send_connect_ack = 2;
1967 send_session_connect_ack_message (n->primary_address.address,
1968 n->primary_address.session,
1969 n->connect_ack_timestamp);
1972 case S_CONNECTED_SWITCHING_BLACKLIST:
1973 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1974 break; /* result for an address we currently don't care about */
1975 if (GNUNET_OK == result)
1977 send_session_connect (&n->alternative_address);
1978 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1982 free_address (&n->alternative_address);
1983 n->state = S_CONNECTED;
1986 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1987 /* waiting on CONNECT_ACK, don't care about blacklist */
1988 if ( (GNUNET_OK == result) &&
1989 (1 == n->send_connect_ack) )
1991 n->send_connect_ack = 2;
1992 send_session_connect_ack_message (n->primary_address.address,
1993 n->primary_address.session,
1994 n->connect_ack_timestamp);
1998 /* Nothing to do here, ATS will already do what can be done */
2000 case S_DISCONNECT_FINISHED:
2001 /* should not be possible */
2005 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2006 "Unhandled state `%s'\n",
2007 print_state (n->state));
2009 free_neighbour (n, GNUNET_NO);
2013 GNUNET_HELLO_address_free (bcc->na.address);
2019 * We want to know if connecting to a particular peer via
2020 * a particular address is allowed. Check it!
2022 * @param peer identity of the peer to switch the address for
2023 * @param ts time at which the check was initiated
2024 * @param address address of the other peer, NULL if other peer
2026 * @param session session to use (or NULL)
2029 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2030 struct GNUNET_TIME_Absolute ts,
2031 const struct GNUNET_HELLO_Address *address,
2032 struct Session *session)
2034 struct BlackListCheckContext *bcc;
2035 struct GST_BlacklistCheck *bc;
2037 bcc = GNUNET_new (struct BlackListCheckContext);
2038 bcc->na.address = GNUNET_HELLO_address_copy (address);
2039 bcc->na.session = session;
2040 bcc->na.connect_timestamp = ts;
2041 GNUNET_CONTAINER_DLL_insert (bc_head,
2044 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2045 address->transport_name,
2046 &handle_test_blacklist_cont, bcc)))
2048 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2049 we must only store 'bc' if 'bc' is non-NULL... */
2054 * We received a 'SESSION_CONNECT' message from the other peer.
2055 * Consider switching to it.
2057 * @param message possibly a 'struct SessionConnectMessage' (check format)
2058 * @param peer identity of the peer to switch the address for
2059 * @param address address of the other peer, NULL if other peer
2061 * @param session session to use (or NULL)
2062 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2065 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2066 const struct GNUNET_PeerIdentity *peer,
2067 const struct GNUNET_HELLO_Address *address,
2068 struct Session *session)
2070 const struct SessionConnectMessage *scm;
2071 struct NeighbourMapEntry *n;
2072 struct GNUNET_TIME_Absolute ts;
2074 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2075 "Received CONNECT message from peer `%s'\n",
2077 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2079 GNUNET_break_op (0);
2080 return GNUNET_SYSERR;
2082 GNUNET_STATISTICS_update (GST_stats,
2084 ("# CONNECT messages received"),
2086 if (NULL == neighbours)
2088 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2089 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2091 return GNUNET_OK; /* we're shutting down */
2093 scm = (const struct SessionConnectMessage *) message;
2094 GNUNET_break_op (0 == ntohl (scm->reserved));
2095 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2096 n = lookup_neighbour (peer);
2098 n = setup_neighbour (peer);
2099 n->send_connect_ack = 1;
2100 n->connect_ack_timestamp = ts;
2104 case S_NOT_CONNECTED:
2105 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2106 /* Do a blacklist check for the new address */
2107 check_blacklist (peer, ts, address, session);
2110 /* CONNECT message takes priority over us asking ATS for address */
2111 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2113 case S_INIT_BLACKLIST:
2114 case S_CONNECT_SENT:
2115 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2116 case S_CONNECT_RECV_ATS:
2117 case S_CONNECT_RECV_BLACKLIST:
2118 case S_CONNECT_RECV_ACK:
2119 /* It can never hurt to have an alternative address in the above cases,
2120 see if it is allowed */
2121 check_blacklist (peer, ts, address, session);
2124 /* we are already connected and can thus send the ACK immediately;
2125 still, it can never hurt to have an alternative address, so also
2126 tell ATS about it */
2127 GNUNET_assert (NULL != n->primary_address.address);
2128 GNUNET_assert (NULL != n->primary_address.session);
2129 n->send_connect_ack = 0;
2130 send_session_connect_ack_message (n->primary_address.address,
2131 n->primary_address.session, ts);
2132 check_blacklist (peer, ts, address, session);
2134 case S_RECONNECT_ATS:
2135 case S_RECONNECT_BLACKLIST:
2136 case S_RECONNECT_SENT:
2137 /* It can never hurt to have an alternative address in the above cases,
2138 see if it is allowed */
2139 check_blacklist (peer, ts, address, session);
2141 case S_CONNECTED_SWITCHING_BLACKLIST:
2142 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2143 /* we are already connected and can thus send the ACK immediately;
2144 still, it can never hurt to have an alternative address, so also
2145 tell ATS about it */
2146 GNUNET_assert (NULL != n->primary_address.address);
2147 GNUNET_assert (NULL != n->primary_address.session);
2148 n->send_connect_ack = 0;
2149 send_session_connect_ack_message (n->primary_address.address,
2150 n->primary_address.session, ts);
2151 check_blacklist (peer, ts, address, session);
2154 /* get rid of remains without terminating sessions, ready to re-try */
2155 free_neighbour (n, GNUNET_YES);
2156 n = setup_neighbour (peer);
2157 n->state = S_CONNECT_RECV_ATS;
2158 GNUNET_ATS_reset_backoff (GST_ats, peer);
2159 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2161 case S_DISCONNECT_FINISHED:
2162 /* should not be possible */
2166 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2167 "Unhandled state `%s'\n",
2168 print_state (n->state));
2170 return GNUNET_SYSERR;
2177 * For an existing neighbour record, set the active connection to
2178 * use the given address.
2180 * @param peer identity of the peer to switch the address for
2181 * @param address address of the other peer, NULL if other peer
2183 * @param session session to use (or NULL)
2184 * @param ats performance data
2185 * @param ats_count number of entries in ats
2186 * @param bandwidth_in inbound quota to be used when connection is up,
2187 * 0 to disconnect from peer
2188 * @param bandwidth_out outbound quota to be used when connection is up,
2189 * 0 to disconnect from peer
2192 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2193 const struct GNUNET_HELLO_Address *address,
2194 struct Session *session,
2195 const struct GNUNET_ATS_Information *ats,
2197 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2198 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2200 struct NeighbourMapEntry *n;
2201 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2203 GNUNET_assert (address->transport_name != NULL);
2204 if (NULL == (n = lookup_neighbour (peer)))
2207 /* Obtain an session for this address from plugin */
2208 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2210 /* we don't have the plugin for this address */
2211 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2214 if ((NULL == session) && (0 == address->address_length))
2217 if (strlen (address->transport_name) > 0)
2218 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2222 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2223 "ATS tells us to switch to address '%s' session %p for "
2224 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2225 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2228 print_state (n->state),
2229 n->send_connect_ack,
2230 ntohl (bandwidth_in.value__),
2231 ntohl (bandwidth_out.value__));
2233 if (NULL == session)
2235 session = papi->get_session (papi->cls, address);
2236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2237 "Obtained new session for peer `%s' and address '%s': %p\n",
2238 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2240 if (NULL == session)
2242 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2243 "Failed to obtain new session for peer `%s' and address '%s'\n",
2244 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2245 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2250 case S_NOT_CONNECTED:
2252 free_neighbour (n, GNUNET_NO);
2255 set_address (&n->primary_address,
2256 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2257 n->state = S_INIT_BLACKLIST;
2258 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2259 check_blacklist (&n->id,
2260 n->connect_ack_timestamp,
2263 case S_INIT_BLACKLIST:
2264 /* ATS suggests a different address, switch again */
2265 set_address (&n->primary_address,
2266 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2267 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2268 check_blacklist (&n->id,
2269 n->connect_ack_timestamp,
2272 case S_CONNECT_SENT:
2273 /* ATS suggests a different address, switch again */
2274 set_address (&n->primary_address,
2275 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2276 n->state = S_INIT_BLACKLIST;
2277 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2278 check_blacklist (&n->id,
2279 n->connect_ack_timestamp,
2282 case S_CONNECT_RECV_ATS:
2283 set_address (&n->primary_address,
2284 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2285 n->state = S_CONNECT_RECV_BLACKLIST;
2286 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2287 check_blacklist (&n->id,
2288 n->connect_ack_timestamp,
2291 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2292 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2293 check_blacklist (&n->id,
2294 n->connect_ack_timestamp,
2297 case S_CONNECT_RECV_BLACKLIST:
2298 case S_CONNECT_RECV_ACK:
2299 /* ATS asks us to switch while we were trying to connect; switch to new
2300 address and check blacklist again */
2301 set_address (&n->primary_address,
2302 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2303 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2304 check_blacklist (&n->id,
2305 n->connect_ack_timestamp,
2309 GNUNET_assert (NULL != n->primary_address.address);
2310 GNUNET_assert (NULL != n->primary_address.session);
2311 if (n->primary_address.session == session)
2313 /* not an address change, just a quota change */
2314 set_address (&n->primary_address,
2315 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2318 /* ATS asks us to switch a life connection; see if we can get
2319 a CONNECT_ACK on it before we actually do this! */
2320 set_address (&n->alternative_address,
2321 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2322 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2323 check_blacklist (&n->id,
2324 GNUNET_TIME_absolute_get (),
2327 case S_RECONNECT_ATS:
2328 set_address (&n->primary_address,
2329 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2330 n->state = S_RECONNECT_BLACKLIST;
2331 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2332 check_blacklist (&n->id,
2333 n->connect_ack_timestamp,
2336 case S_RECONNECT_BLACKLIST:
2337 /* ATS asks us to switch while we were trying to reconnect; switch to new
2338 address and check blacklist again */
2339 set_address (&n->primary_address,
2340 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2341 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2342 check_blacklist (&n->id,
2343 n->connect_ack_timestamp,
2346 case S_RECONNECT_SENT:
2347 /* ATS asks us to switch while we were trying to reconnect; switch to new
2348 address and check blacklist again */
2349 set_address (&n->primary_address,
2350 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2351 n->state = S_RECONNECT_BLACKLIST;
2352 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2353 check_blacklist (&n->id,
2354 n->connect_ack_timestamp,
2357 case S_CONNECTED_SWITCHING_BLACKLIST:
2358 if (n->primary_address.session == session)
2360 /* ATS switches back to still-active session */
2361 free_address (&n->alternative_address);
2362 n->state = S_CONNECTED;
2365 /* ATS asks us to switch a life connection, update blacklist check */
2366 set_address (&n->alternative_address,
2367 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2368 check_blacklist (&n->id,
2369 GNUNET_TIME_absolute_get (),
2372 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2373 if (n->primary_address.session == session)
2375 /* ATS switches back to still-active session */
2376 free_address (&n->alternative_address);
2377 n->state = S_CONNECTED;
2380 /* ATS asks us to switch a life connection, update blacklist check */
2381 set_address (&n->alternative_address,
2382 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2383 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2384 check_blacklist (&n->id,
2385 GNUNET_TIME_absolute_get (),
2389 /* not going to switch addresses while disconnecting */
2391 case S_DISCONNECT_FINISHED:
2395 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2396 "Unhandled state `%s'\n",
2397 print_state (n->state));
2405 send_utilization_data (void *cls,
2406 const struct GNUNET_PeerIdentity *key,
2409 struct NeighbourMapEntry *n = value;
2410 struct GNUNET_ATS_Information atsi[4];
2412 uint32_t bps_pl_out;
2415 struct GNUNET_TIME_Relative delta;
2417 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2418 GNUNET_TIME_absolute_get ());
2422 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2423 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2425 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2426 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2428 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2433 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2434 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2436 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2437 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2445 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2446 atsi[0].value = htonl (bps_out);
2447 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2448 atsi[1].value = htonl (bps_in);
2450 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2451 atsi[2].value = htonl (bps_pl_out);
2452 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2453 atsi[3].value = htonl (bps_pl_in);
2455 GST_ats_update_metrics (key, n->primary_address.address,
2456 n->primary_address.session, atsi, 4);
2457 n->util_payload_bytes_recv = 0;
2458 n->util_payload_bytes_sent = 0;
2459 n->util_total_bytes_recv = 0;
2460 n->util_total_bytes_sent = 0;
2461 n->last_util_transmission = GNUNET_TIME_absolute_get();
2467 * Task transmitting utilization in a regular interval
2469 * @param cls the 'struct NeighbourMapEntry' for which we are running
2470 * @param tc scheduler context (unused)
2473 utilization_transmission (void *cls,
2474 const struct GNUNET_SCHEDULER_TaskContext *tc)
2476 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2478 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2479 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2481 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2482 utilization_transmission, NULL);
2488 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2489 const struct GNUNET_HELLO_Address *address,
2490 struct Session *session,
2491 const struct GNUNET_MessageHeader *message)
2493 struct NeighbourMapEntry *n;
2495 n = lookup_neighbour (peer);
2498 n->util_total_bytes_recv += ntohs(message->size);
2503 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2504 const struct GNUNET_HELLO_Address *address,
2505 struct Session *session,
2506 const struct GNUNET_MessageHeader *message)
2508 struct NeighbourMapEntry *n;
2509 n = lookup_neighbour (peer);
2512 n->util_payload_bytes_recv += ntohs(message->size);
2517 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2518 const struct GNUNET_HELLO_Address *address,
2519 struct Session *session,
2522 struct NeighbourMapEntry *n;
2523 n = lookup_neighbour (peer);
2526 if (n->primary_address.session != session)
2528 n->util_total_bytes_sent += size;
2533 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2536 struct NeighbourMapEntry *n;
2537 n = lookup_neighbour (peer);
2540 n->util_payload_bytes_sent += size;
2545 * Master task run for every neighbour. Performs all of the time-related
2546 * activities (keep alive, send next message, disconnect if idle, finish
2547 * clean up after disconnect).
2549 * @param cls the 'struct NeighbourMapEntry' for which we are running
2550 * @param tc scheduler context (unused)
2553 master_task (void *cls,
2554 const struct GNUNET_SCHEDULER_TaskContext *tc)
2556 struct NeighbourMapEntry *n = cls;
2557 struct GNUNET_TIME_Relative delay;
2559 n->task = GNUNET_SCHEDULER_NO_TASK;
2560 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2562 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2563 GNUNET_i2s (&n->id),
2564 print_state(n->state),
2565 GNUNET_STRINGS_relative_time_to_string (delay,
2569 case S_NOT_CONNECTED:
2570 /* invalid state for master task, clean up */
2572 n->state = S_DISCONNECT_FINISHED;
2573 free_neighbour (n, GNUNET_NO);
2576 if (0 == delay.rel_value_us)
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 "Connection to `%s' timed out waiting for ATS to provide address\n",
2580 GNUNET_i2s (&n->id));
2581 n->state = S_DISCONNECT_FINISHED;
2582 free_neighbour (n, GNUNET_NO);
2586 case S_INIT_BLACKLIST:
2587 if (0 == delay.rel_value_us)
2589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2590 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2591 GNUNET_i2s (&n->id));
2592 n->state = S_DISCONNECT_FINISHED;
2593 free_neighbour (n, GNUNET_NO);
2597 case S_CONNECT_SENT:
2598 if (0 == delay.rel_value_us)
2600 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2601 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2602 GNUNET_i2s (&n->id));
2603 /* We could not send to this address, delete address and session */
2604 if (NULL != n->primary_address.session)
2605 GNUNET_ATS_address_destroyed (GST_ats,
2606 n->primary_address.address, n->primary_address.session);
2607 GNUNET_ATS_address_destroyed (GST_ats,
2608 n->primary_address.address, NULL);
2609 disconnect_neighbour (n);
2613 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2614 if (0 == delay.rel_value_us)
2616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2617 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2618 GNUNET_i2s (&n->id));
2619 n->state = S_DISCONNECT_FINISHED;
2620 free_neighbour (n, GNUNET_NO);
2624 case S_CONNECT_RECV_ATS:
2625 if (0 == delay.rel_value_us)
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2629 GNUNET_i2s (&n->id));
2630 n->state = S_DISCONNECT_FINISHED;
2631 free_neighbour (n, GNUNET_NO);
2635 case S_CONNECT_RECV_BLACKLIST:
2636 if (0 == delay.rel_value_us)
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2639 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2640 GNUNET_i2s (&n->id));
2641 n->state = S_DISCONNECT_FINISHED;
2642 free_neighbour (n, GNUNET_NO);
2646 case S_CONNECT_RECV_ACK:
2647 if (0 == delay.rel_value_us)
2649 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2650 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2651 GNUNET_i2s (&n->id));
2652 disconnect_neighbour (n);
2657 if (0 == delay.rel_value_us)
2659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2660 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2661 GNUNET_i2s (&n->id));
2662 disconnect_neighbour (n);
2665 try_transmission_to_peer (n);
2668 case S_RECONNECT_ATS:
2669 if (0 == delay.rel_value_us)
2671 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2672 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2673 GNUNET_i2s (&n->id));
2674 disconnect_neighbour (n);
2678 case S_RECONNECT_BLACKLIST:
2679 if (0 == delay.rel_value_us)
2681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2682 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2683 GNUNET_i2s (&n->id));
2684 disconnect_neighbour (n);
2688 case S_RECONNECT_SENT:
2689 if (0 == delay.rel_value_us)
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2692 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2693 GNUNET_i2s (&n->id));
2694 disconnect_neighbour (n);
2698 case S_CONNECTED_SWITCHING_BLACKLIST:
2699 if (0 == delay.rel_value_us)
2701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2702 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2703 GNUNET_i2s (&n->id));
2704 disconnect_neighbour (n);
2707 try_transmission_to_peer (n);
2710 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2711 if (0 == delay.rel_value_us)
2713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2714 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2715 GNUNET_i2s (&n->id));
2716 disconnect_neighbour (n);
2719 try_transmission_to_peer (n);
2723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2724 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2725 GNUNET_i2s (&n->id));
2726 free_neighbour (n, GNUNET_NO);
2728 case S_DISCONNECT_FINISHED:
2729 /* how did we get here!? */
2733 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2734 "Unhandled state `%s'\n",
2735 print_state (n->state));
2739 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2740 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2741 (S_CONNECTED == n->state) )
2743 /* if we are *now* in one of these three states, we're sending
2744 keep alive messages, so we need to consider the keepalive
2745 delay, not just the connection timeout */
2746 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2749 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2750 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2757 * Send a SESSION_ACK message to the neighbour to confirm that we
2758 * got his CONNECT_ACK.
2760 * @param n neighbour to send the SESSION_ACK to
2763 send_session_ack_message (struct NeighbourMapEntry *n)
2765 struct GNUNET_MessageHeader msg;
2767 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2768 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2769 (void) send_with_session(n,
2770 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2771 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2777 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2778 * Consider switching to it.
2780 * @param message possibly a 'struct SessionConnectMessage' (check format)
2781 * @param peer identity of the peer to switch the address for
2782 * @param address address of the other peer, NULL if other peer
2784 * @param session session to use (or NULL)
2785 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2788 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2789 const struct GNUNET_PeerIdentity *peer,
2790 const struct GNUNET_HELLO_Address *address,
2791 struct Session *session)
2793 const struct SessionConnectMessage *scm;
2794 struct GNUNET_TIME_Absolute ts;
2795 struct NeighbourMapEntry *n;
2797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2798 "Received CONNECT_ACK message from peer `%s'\n",
2801 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2803 GNUNET_break_op (0);
2804 return GNUNET_SYSERR;
2806 GNUNET_STATISTICS_update (GST_stats,
2808 ("# CONNECT_ACK messages received"),
2810 scm = (const struct SessionConnectMessage *) message;
2811 GNUNET_break_op (ntohl (scm->reserved) == 0);
2812 if (NULL == (n = lookup_neighbour (peer)))
2814 GNUNET_STATISTICS_update (GST_stats,
2816 ("# unexpected CONNECT_ACK messages (no peer)"),
2818 return GNUNET_SYSERR;
2820 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2823 case S_NOT_CONNECTED:
2825 free_neighbour (n, GNUNET_NO);
2826 return GNUNET_SYSERR;
2828 case S_INIT_BLACKLIST:
2829 GNUNET_STATISTICS_update (GST_stats,
2831 ("# unexpected CONNECT_ACK messages (not ready)"),
2834 case S_CONNECT_SENT:
2835 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
2837 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2838 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
2841 n->state = S_CONNECTED;
2842 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2843 GNUNET_STATISTICS_set (GST_stats,
2844 gettext_noop ("# peers connected"),
2845 ++neighbours_connected,
2847 connect_notify_cb (callback_cls, &n->id,
2848 n->primary_address.bandwidth_in,
2849 n->primary_address.bandwidth_out);
2850 /* Tell ATS that the outbound session we created to send CONNECT was successful */
2851 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
2852 set_address (&n->primary_address,
2853 n->primary_address.address,
2854 n->primary_address.session,
2855 n->primary_address.bandwidth_in,
2856 n->primary_address.bandwidth_out,
2858 send_session_ack_message (n);
2860 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2861 case S_CONNECT_RECV_ATS:
2862 case S_CONNECT_RECV_BLACKLIST:
2863 case S_CONNECT_RECV_ACK:
2864 GNUNET_STATISTICS_update (GST_stats,
2866 ("# unexpected CONNECT_ACK messages (not ready)"),
2870 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2871 send_session_ack_message (n);
2873 case S_RECONNECT_ATS:
2874 case S_RECONNECT_BLACKLIST:
2875 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2876 to give us a new address... */
2877 GNUNET_STATISTICS_update (GST_stats,
2879 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2882 case S_RECONNECT_SENT:
2883 /* new address worked; go back to connected! */
2884 n->state = S_CONNECTED;
2885 send_session_ack_message (n);
2887 case S_CONNECTED_SWITCHING_BLACKLIST:
2888 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2889 send_session_ack_message (n);
2891 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2892 /* new address worked; adopt it and go back to connected! */
2893 n->state = S_CONNECTED;
2894 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2895 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2897 GST_ats_add_address (n->alternative_address.address, n->alternative_address.session);
2898 set_address (&n->primary_address,
2899 n->alternative_address.address,
2900 n->alternative_address.session,
2901 n->alternative_address.bandwidth_in,
2902 n->alternative_address.bandwidth_out,
2904 free_address (&n->alternative_address);
2905 send_session_ack_message (n);
2908 GNUNET_STATISTICS_update (GST_stats,
2910 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2912 return GNUNET_SYSERR;
2913 case S_DISCONNECT_FINISHED:
2917 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2918 "Unhandled state `%s'\n",
2919 print_state (n->state));
2921 return GNUNET_SYSERR;
2928 * A session was terminated. Take note; if needed, try to get
2929 * an alternative address from ATS.
2931 * @param peer identity of the peer where the session died
2932 * @param session session that is gone
2933 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
2934 * this session was not in use
2937 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2938 struct Session *session)
2940 struct NeighbourMapEntry *n;
2941 struct BlackListCheckContext *bcc;
2942 struct BlackListCheckContext *bcc_next;
2944 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2946 while (NULL != (bcc = bcc_next))
2948 bcc_next = bcc->next;
2949 if (bcc->na.session == session)
2951 if (NULL != bcc->bc)
2952 GST_blacklist_test_cancel (bcc->bc);
2953 GNUNET_HELLO_address_free (bcc->na.address);
2954 GNUNET_CONTAINER_DLL_remove (bc_head,
2960 if (NULL == (n = lookup_neighbour (peer)))
2961 return GNUNET_NO; /* can't affect us */
2962 if (session != n->primary_address.session)
2964 if (session == n->alternative_address.session)
2966 free_address (&n->alternative_address);
2967 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2968 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2969 n->state = S_CONNECTED;
2973 return GNUNET_NO; /* doesn't affect us further */
2976 n->expect_latency_response = GNUNET_NO;
2979 case S_NOT_CONNECTED:
2981 free_neighbour (n, GNUNET_NO);
2985 free_neighbour (n, GNUNET_NO);
2987 case S_INIT_BLACKLIST:
2988 case S_CONNECT_SENT:
2989 free_address (&n->primary_address);
2990 n->state = S_INIT_ATS;
2991 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2992 // FIXME: need to ask ATS for suggestions again?
2993 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2995 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2996 case S_CONNECT_RECV_ATS:
2997 case S_CONNECT_RECV_BLACKLIST:
2998 case S_CONNECT_RECV_ACK:
2999 /* error on inbound session; free neighbour entirely */
3000 free_address (&n->primary_address);
3001 free_neighbour (n, GNUNET_NO);
3004 free_address (&n->primary_address);
3005 n->state = S_RECONNECT_ATS;
3006 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3007 /* FIXME: is this ATS call needed? */
3008 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3010 case S_RECONNECT_ATS:
3011 /* we don't have an address, how can it go down? */
3014 case S_RECONNECT_BLACKLIST:
3015 case S_RECONNECT_SENT:
3016 n->state = S_RECONNECT_ATS;
3017 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3018 // FIXME: need to ask ATS for suggestions again?
3019 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3021 case S_CONNECTED_SWITCHING_BLACKLIST:
3022 /* primary went down while we were checking secondary against
3023 blacklist, adopt secondary as primary */
3024 free_address (&n->primary_address);
3025 n->primary_address = n->alternative_address;
3026 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3027 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3028 n->state = S_RECONNECT_BLACKLIST;
3030 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3031 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3032 secondary as primary */
3033 free_address (&n->primary_address);
3034 n->primary_address = n->alternative_address;
3035 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3036 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3037 n->state = S_RECONNECT_SENT;
3040 free_address (&n->primary_address);
3042 case S_DISCONNECT_FINISHED:
3043 /* neighbour was freed and plugins told to terminate session */
3047 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3048 "Unhandled state `%s'\n",
3049 print_state (n->state));
3053 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3054 GNUNET_SCHEDULER_cancel (n->task);
3055 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3061 * We received a 'SESSION_ACK' message from the other peer.
3062 * If we sent a 'CONNECT_ACK' last, this means we are now
3063 * connected. Otherwise, do nothing.
3065 * @param message possibly a 'struct SessionConnectMessage' (check format)
3066 * @param peer identity of the peer to switch the address for
3067 * @param address address of the other peer, NULL if other peer
3069 * @param session session to use (or NULL)
3070 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3073 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3074 const struct GNUNET_PeerIdentity *peer,
3075 const struct GNUNET_HELLO_Address *address,
3076 struct Session *session)
3078 struct NeighbourMapEntry *n;
3080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3081 "Received SESSION_ACK message from peer `%s'\n",
3083 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3085 GNUNET_break_op (0);
3086 return GNUNET_SYSERR;
3088 GNUNET_STATISTICS_update (GST_stats,
3090 ("# SESSION_ACK messages received"),
3092 if (NULL == (n = lookup_neighbour (peer)))
3093 return GNUNET_SYSERR;
3094 /* check if we are in a plausible state for having sent
3095 a CONNECT_ACK. If not, return, otherwise break */
3096 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3097 (S_CONNECT_SENT != n->state) ) ||
3098 (2 != n->send_connect_ack) )
3100 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3101 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3103 print_state (n->state),
3104 n->send_connect_ack);
3105 GNUNET_STATISTICS_update (GST_stats,
3106 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3108 return GNUNET_SYSERR;
3110 n->state = S_CONNECTED;
3111 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3112 GNUNET_STATISTICS_set (GST_stats,
3113 gettext_noop ("# peers connected"),
3114 ++neighbours_connected,
3116 connect_notify_cb (callback_cls, &n->id,
3117 n->primary_address.bandwidth_in,
3118 n->primary_address.bandwidth_out);
3120 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
3121 set_address (&n->primary_address,
3122 n->primary_address.address,
3123 n->primary_address.session,
3124 n->primary_address.bandwidth_in,
3125 n->primary_address.bandwidth_out,
3132 * Test if we're connected to the given peer.
3134 * @param target peer to test
3135 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3138 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3140 return test_connected (lookup_neighbour (target));
3145 * Change the incoming quota for the given peer.
3147 * @param neighbour identity of peer to change qutoa for
3148 * @param quota new quota
3151 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3152 struct GNUNET_BANDWIDTH_Value32NBO quota)
3154 struct NeighbourMapEntry *n;
3156 if (NULL == (n = lookup_neighbour (neighbour)))
3158 GNUNET_STATISTICS_update (GST_stats,
3160 ("# SET QUOTA messages ignored (no such peer)"),
3164 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3165 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3166 ntohl (quota.value__), GNUNET_i2s (&n->id));
3167 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3168 if (0 != ntohl (quota.value__))
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
3171 GNUNET_i2s (&n->id), "SET_QUOTA");
3172 if (GNUNET_YES == test_connected (n))
3173 GNUNET_STATISTICS_update (GST_stats,
3174 gettext_noop ("# disconnects due to quota of 0"),
3176 disconnect_neighbour (n);
3181 * We received a disconnect message from the given peer,
3182 * validate and process.
3184 * @param peer sender of the message
3185 * @param msg the disconnect message
3188 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
3190 const struct GNUNET_MessageHeader
3193 struct NeighbourMapEntry *n;
3194 const struct SessionDisconnectMessage *sdm;
3196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3197 "Received DISCONNECT message from peer `%s'\n",
3199 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3201 // GNUNET_break_op (0);
3202 GNUNET_STATISTICS_update (GST_stats,
3204 ("# disconnect messages ignored (old format)"), 1,
3208 GNUNET_STATISTICS_update (GST_stats,
3210 ("# DISCONNECT messages received"),
3212 sdm = (const struct SessionDisconnectMessage *) msg;
3213 if (NULL == (n = lookup_neighbour (peer)))
3214 return; /* gone already */
3215 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3217 GNUNET_STATISTICS_update (GST_stats,
3219 ("# disconnect messages ignored (timestamp)"), 1,
3223 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3225 GNUNET_break_op (0);
3228 if (ntohl (sdm->purpose.size) !=
3229 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3230 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3231 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3233 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3234 "%s message from peer `%s' has invalid size \n",
3237 GNUNET_break_op (0);
3241 GNUNET_CRYPTO_eddsa_verify
3242 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3243 &sdm->signature, &sdm->public_key))
3245 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3246 "%s message from peer `%s' cannot be verified \n",
3249 GNUNET_break_op (0);
3252 if (GNUNET_YES == test_connected (n))
3253 GNUNET_STATISTICS_update (GST_stats,
3255 ("# other peer asked to disconnect from us"), 1,
3257 disconnect_neighbour (n);
3262 * Closure for the neighbours_iterate function.
3264 struct IteratorContext
3267 * Function to call on each connected neighbour.
3269 GST_NeighbourIterator cb;
3279 * Call the callback from the closure for each connected neighbour.
3281 * @param cls the `struct IteratorContext`
3282 * @param key the hash of the public key of the neighbour
3283 * @param value the `struct NeighbourMapEntry`
3284 * @return #GNUNET_OK (continue to iterate)
3287 neighbours_iterate (void *cls,
3288 const struct GNUNET_PeerIdentity *key,
3291 struct IteratorContext *ic = cls;
3292 struct NeighbourMapEntry *n = value;
3293 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3294 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3296 if (GNUNET_YES != test_connected (n))
3299 if (NULL != n->primary_address.address)
3301 bandwidth_in = n->primary_address.bandwidth_in;
3302 bandwidth_out = n->primary_address.bandwidth_out;
3306 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3307 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3309 ic->cb (ic->cb_cls, &n->id,
3310 n->primary_address.address,
3311 bandwidth_in, bandwidth_out);
3317 * Iterate over all connected neighbours.
3319 * @param cb function to call
3320 * @param cb_cls closure for cb
3323 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3325 struct IteratorContext ic;
3327 if (NULL == neighbours)
3328 return; /* can happen during shutdown */
3331 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3336 * If we have an active connection to the given target, it must be shutdown.
3338 * @param target peer to disconnect from
3341 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3343 struct NeighbourMapEntry *n;
3345 if (NULL == (n = lookup_neighbour (target)))
3346 return; /* not active */
3347 if (GNUNET_YES == test_connected (n))
3348 GNUNET_STATISTICS_update (GST_stats,
3350 ("# disconnected from peer upon explicit request"), 1,
3352 disconnect_neighbour (n);
3357 * Obtain current latency information for the given neighbour.
3359 * @param peer to get the latency for
3360 * @return observed latency of the address, FOREVER if the
3361 * the connection is not up
3363 struct GNUNET_TIME_Relative
3364 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3366 struct NeighbourMapEntry *n;
3368 n = lookup_neighbour (peer);
3370 return GNUNET_TIME_UNIT_FOREVER_REL;
3374 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3375 case S_CONNECTED_SWITCHING_BLACKLIST:
3376 case S_RECONNECT_SENT:
3377 case S_RECONNECT_ATS:
3378 case S_RECONNECT_BLACKLIST:
3380 case S_NOT_CONNECTED:
3381 case S_INIT_BLACKLIST:
3383 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3384 case S_CONNECT_RECV_ATS:
3385 case S_CONNECT_RECV_BLACKLIST:
3386 case S_CONNECT_RECV_ACK:
3387 case S_CONNECT_SENT:
3389 case S_DISCONNECT_FINISHED:
3390 return GNUNET_TIME_UNIT_FOREVER_REL;
3392 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3393 "Unhandled state `%s'\n",
3394 print_state (n->state));
3398 return GNUNET_TIME_UNIT_FOREVER_REL;
3403 * Obtain current address information for the given neighbour.
3406 * @return address currently used
3408 struct GNUNET_HELLO_Address *
3409 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3411 struct NeighbourMapEntry *n;
3413 n = lookup_neighbour (peer);
3416 return n->primary_address.address;
3421 * Initialize the neighbours subsystem.
3423 * @param cls closure for callbacks
3424 * @param connect_cb function to call if we connect to a peer
3425 * @param disconnect_cb function to call if we disconnect from a peer
3426 * @param peer_address_cb function to call if we change an active address
3428 * @param max_fds maximum number of fds to use
3431 GST_neighbours_start (void *cls,
3432 NotifyConnect connect_cb,
3433 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3434 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3435 unsigned int max_fds)
3438 connect_notify_cb = connect_cb;
3439 disconnect_notify_cb = disconnect_cb;
3440 address_change_cb = peer_address_cb;
3441 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3442 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3443 utilization_transmission, NULL);
3448 * Disconnect from the given neighbour.
3451 * @param key hash of neighbour's public key (not used)
3452 * @param value the 'struct NeighbourMapEntry' of the neighbour
3453 * @return #GNUNET_OK (continue to iterate)
3456 disconnect_all_neighbours (void *cls,
3457 const struct GNUNET_PeerIdentity *key,
3460 struct NeighbourMapEntry *n = value;
3462 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3463 "Disconnecting peer `%4s', %s\n",
3464 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3465 n->state = S_DISCONNECT_FINISHED;
3466 free_neighbour (n, GNUNET_NO);
3472 * Cleanup the neighbours subsystem.
3475 GST_neighbours_stop ()
3477 if (NULL == neighbours)
3479 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3481 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3482 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3485 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3486 &disconnect_all_neighbours,
3488 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3490 callback_cls = NULL;
3491 connect_notify_cb = NULL;
3492 disconnect_notify_cb = NULL;
3493 address_change_cb = NULL;
3497 /* end of file gnunet-service-transport_neighbours.c */