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,
883 struct MessageQueue *mq;
884 struct GNUNET_TRANSPORT_PluginFunctions *papi;
885 struct GNUNET_HELLO_Address *backup_primary;
887 n->is_active = NULL; /* always free'd by its own continuation! */
889 /* fail messages currently in the queue */
890 while (NULL != (mq = n->messages_head))
892 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
893 if (NULL != mq->cont)
894 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
897 /* It is too late to send other peer disconnect notifications, but at
898 least internally we need to get clean... */
899 if (GNUNET_YES == test_connected (n))
901 GNUNET_STATISTICS_set (GST_stats,
902 gettext_noop ("# peers connected"),
903 --neighbours_connected,
905 disconnect_notify_cb (callback_cls, &n->id);
907 n->state = S_DISCONNECT_FINISHED;
909 if (NULL != n->primary_address.address)
911 backup_primary = GNUNET_HELLO_address_copy (n->primary_address.address);
914 backup_primary = NULL;
916 /* free addresses and mark as unused */
917 free_address (&n->primary_address);
918 free_address (&n->alternative_address);
920 /* FIXME: Note that if we are switching between two TCP sessions to
921 the same peer, we might want to selectively kill only one of
922 them! Killing all sessions like this seems to be very, very
925 /* cut transport-level connection */
926 if ((GNUNET_NO == keep_sessions) &&
927 (NULL != backup_primary) &&
928 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
929 papi->disconnect_peer (papi->cls, &n->id);
931 GNUNET_free_non_null (backup_primary);
933 GNUNET_assert (GNUNET_YES ==
934 GNUNET_CONTAINER_multipeermap_remove (neighbours,
937 // FIXME-ATS-API: we might want to be more specific about
938 // which states we do this from in the future (ATS should
939 // have given us a 'suggest_address' handle, and if we have
940 // such a handle, we should cancel the operation here!
941 if (NULL != n->suggest_handle)
943 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
944 n->suggest_handle = NULL;
947 if (GNUNET_SCHEDULER_NO_TASK != n->task)
949 GNUNET_SCHEDULER_cancel (n->task);
950 n->task = GNUNET_SCHEDULER_NO_TASK;
952 /* free rest of memory */
958 * Transmit a message using the current session of the given
961 * @param n entry for the recipient
962 * @param msgbuf buffer to transmit
963 * @param msgbuf_size number of bytes in buffer
964 * @param priority transmission priority
965 * @param timeout transmission timeout
966 * @param cont continuation to call when finished (can be NULL)
967 * @param cont_cls closure for cont
970 send_with_session (struct NeighbourMapEntry *n,
971 const char *msgbuf, size_t msgbuf_size,
973 struct GNUNET_TIME_Relative timeout,
974 GNUNET_TRANSPORT_TransmitContinuation cont,
977 struct GNUNET_TRANSPORT_PluginFunctions *papi;
979 GNUNET_assert (n->primary_address.session != NULL);
980 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
981 (-1 == papi->send (papi->cls,
982 n->primary_address.session,
986 cont, cont_cls)))) &&
988 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
989 GST_neighbours_notify_data_sent (&n->id,
990 n->primary_address.address, n->primary_address.session, msgbuf_size);
991 GNUNET_break (NULL != papi);
996 * Master task run for every neighbour. Performs all of the time-related
997 * activities (keep alive, send next message, disconnect if idle, finish
998 * clean up after disconnect).
1000 * @param cls the 'struct NeighbourMapEntry' for which we are running
1001 * @param tc scheduler context (unused)
1004 master_task (void *cls,
1005 const struct GNUNET_SCHEDULER_TaskContext *tc);
1009 * Function called when the 'DISCONNECT' message has been sent by the
1010 * plugin. Frees the neighbour --- if the entry still exists.
1013 * @param target identity of the neighbour that was disconnected
1014 * @param result #GNUNET_OK if the disconnect got out successfully
1015 * @param payload bytes payload
1016 * @param physical bytes physical
1019 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1020 int result, size_t payload, size_t physical)
1022 struct NeighbourMapEntry *n;
1024 n = lookup_neighbour (target);
1026 return; /* already gone */
1027 if (S_DISCONNECT != n->state)
1028 return; /* have created a fresh entry since */
1029 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1030 GNUNET_SCHEDULER_cancel (n->task);
1031 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1036 * Transmit a DISCONNECT message to the other peer.
1038 * @param n neighbour to send DISCONNECT message.
1041 send_disconnect (struct NeighbourMapEntry *n)
1043 struct SessionDisconnectMessage disconnect_msg;
1045 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1046 "Sending DISCONNECT message to peer `%4s'\n",
1047 GNUNET_i2s (&n->id));
1048 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1049 disconnect_msg.header.type =
1050 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1051 disconnect_msg.reserved = htonl (0);
1052 disconnect_msg.purpose.size =
1053 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1054 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
1055 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1056 disconnect_msg.purpose.purpose =
1057 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1058 disconnect_msg.timestamp =
1059 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1060 disconnect_msg.public_key = GST_my_identity.public_key;
1061 GNUNET_assert (GNUNET_OK ==
1062 GNUNET_CRYPTO_eddsa_sign (GST_my_private_key,
1063 &disconnect_msg.purpose,
1064 &disconnect_msg.signature));
1066 send_with_session (n,
1067 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1068 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1069 &send_disconnect_cont, NULL);
1070 GNUNET_STATISTICS_update (GST_stats,
1072 ("# DISCONNECT messages sent"), 1,
1078 * Disconnect from the given neighbour, clean up the record.
1080 * @param n neighbour to disconnect from
1083 disconnect_neighbour (struct NeighbourMapEntry *n)
1085 /* depending on state, notify neighbour and/or upper layers of this peer
1089 case S_NOT_CONNECTED:
1091 case S_INIT_BLACKLIST:
1092 /* other peer is completely unaware of us, no need to send DISCONNECT */
1093 n->state = S_DISCONNECT_FINISHED;
1094 free_neighbour (n, GNUNET_NO);
1096 case S_CONNECT_SENT:
1097 send_disconnect (n);
1098 n->state = S_DISCONNECT;
1100 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1101 case S_CONNECT_RECV_ATS:
1102 case S_CONNECT_RECV_BLACKLIST:
1103 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1104 n->state = S_DISCONNECT_FINISHED;
1105 free_neighbour (n, GNUNET_NO);
1107 case S_CONNECT_RECV_ACK:
1108 /* we DID ACK the other peer's request, must send DISCONNECT */
1109 send_disconnect (n);
1110 n->state = S_DISCONNECT;
1113 case S_RECONNECT_BLACKLIST:
1114 case S_RECONNECT_SENT:
1115 case S_CONNECTED_SWITCHING_BLACKLIST:
1116 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1117 /* we are currently connected, need to send disconnect and do
1118 internal notifications and update statistics */
1119 send_disconnect (n);
1120 GNUNET_STATISTICS_set (GST_stats,
1121 gettext_noop ("# peers connected"),
1122 --neighbours_connected,
1124 disconnect_notify_cb (callback_cls, &n->id);
1125 n->state = S_DISCONNECT;
1127 case S_RECONNECT_ATS:
1128 /* ATS address request timeout, disconnect without sending disconnect message */
1129 GNUNET_STATISTICS_set (GST_stats,
1130 gettext_noop ("# peers connected"),
1131 --neighbours_connected,
1133 disconnect_notify_cb (callback_cls, &n->id);
1134 n->state = S_DISCONNECT;
1137 /* already disconnected, ignore */
1139 case S_DISCONNECT_FINISHED:
1140 /* already cleaned up, how did we get here!? */
1144 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1145 "Unhandled state `%s'\n",
1146 print_state (n->state));
1150 /* schedule timeout to clean up */
1151 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1152 GNUNET_SCHEDULER_cancel (n->task);
1153 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1159 * We're done with our transmission attempt, continue processing.
1161 * @param cls the 'struct MessageQueue' of the message
1162 * @param receiver intended receiver
1163 * @param success whether it worked or not
1164 * @param size_payload bytes payload sent
1165 * @param physical bytes sent on wire
1168 transmit_send_continuation (void *cls,
1169 const struct GNUNET_PeerIdentity *receiver,
1170 int success, size_t size_payload, size_t physical)
1172 struct MessageQueue *mq = cls;
1173 struct NeighbourMapEntry *n;
1175 if (NULL == (n = lookup_neighbour (receiver)))
1178 return; /* disconnect or other error while transmitting, can happen */
1180 if (n->is_active == mq)
1182 /* this is still "our" neighbour, remove us from its queue
1183 and allow it to send the next message now */
1184 n->is_active = NULL;
1185 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1186 GNUNET_SCHEDULER_cancel (n->task);
1187 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1189 if (bytes_in_send_queue < mq->message_buf_size)
1191 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1192 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1193 bytes_in_send_queue, mq->message_buf_size,
1194 (GNUNET_OK == success) ? "OK" : "FAIL",
1195 size_payload, physical);
1200 GNUNET_break (size_payload == mq->message_buf_size);
1201 bytes_in_send_queue -= mq->message_buf_size;
1202 GNUNET_STATISTICS_set (GST_stats,
1204 ("# bytes in message queue for other peers"),
1205 bytes_in_send_queue, GNUNET_NO);
1206 if (GNUNET_OK == success)
1207 GNUNET_STATISTICS_update (GST_stats,
1209 ("# messages transmitted to other peers"),
1212 GNUNET_STATISTICS_update (GST_stats,
1214 ("# transmission failures for messages to other peers"),
1216 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1217 "Sending message to `%s' of type %u was a %s\n",
1218 GNUNET_i2s (receiver),
1219 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1220 (success == GNUNET_OK) ? "success" : "FAILURE");
1221 if (NULL != mq->cont)
1222 mq->cont (mq->cont_cls, success, size_payload, physical);
1228 * Check the message list for the given neighbour and if we can
1229 * send a message, do so. This function should only be called
1230 * if the connection is at least generally ready for transmission.
1231 * While we will only send one message at a time, no bandwidth
1232 * quota management is performed here. If a message was given to
1233 * the plugin, the continuation will automatically re-schedule
1234 * the 'master' task once the next message might be transmitted.
1236 * @param n target peer for which to transmit
1239 try_transmission_to_peer (struct NeighbourMapEntry *n)
1241 struct MessageQueue *mq;
1242 struct GNUNET_TIME_Relative timeout;
1244 if (NULL == n->primary_address.address)
1246 /* no address, why are we here? */
1250 if ((0 == n->primary_address.address->address_length) &&
1251 (NULL == n->primary_address.session))
1253 /* no address, why are we here? */
1257 if (NULL != n->is_active)
1259 /* transmission already pending */
1263 /* timeout messages from the queue that are past their due date */
1264 while (NULL != (mq = n->messages_head))
1266 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1267 if (timeout.rel_value_us > 0)
1269 GNUNET_STATISTICS_update (GST_stats,
1271 ("# messages timed out while in transport queue"),
1273 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1275 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0); /* timeout */
1278 return; /* no more messages */
1279 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1281 send_with_session (n,
1282 mq->message_buf, mq->message_buf_size,
1283 0 /* priority */, timeout,
1284 &transmit_send_continuation, mq);
1289 * Send keepalive message to the neighbour. Must only be called
1290 * if we are on 'connected' state or while trying to switch addresses.
1291 * Will internally determine if a keepalive is truly needed (so can
1292 * always be called).
1294 * @param n neighbour that went idle and needs a keepalive
1297 send_keepalive (struct NeighbourMapEntry *n)
1299 struct GNUNET_MessageHeader m;
1301 GNUNET_assert ((S_CONNECTED == n->state) ||
1302 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1303 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1304 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value_us > 0)
1305 return; /* no keepalive needed at this time */
1306 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1307 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1308 send_with_session (n,
1309 (const void *) &m, sizeof (m),
1310 UINT32_MAX /* priority */,
1311 KEEPALIVE_FREQUENCY,
1313 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1315 n->expect_latency_response = GNUNET_YES;
1316 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1317 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1322 * Keep the connection to the given neighbour alive longer,
1323 * we received a KEEPALIVE (or equivalent); send a response.
1325 * @param neighbour neighbour to keep alive (by sending keep alive response)
1328 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1330 struct NeighbourMapEntry *n;
1331 struct GNUNET_MessageHeader m;
1333 if (NULL == (n = lookup_neighbour (neighbour)))
1335 GNUNET_STATISTICS_update (GST_stats,
1337 ("# KEEPALIVE messages discarded (peer unknown)"),
1341 if (NULL == n->primary_address.session)
1343 GNUNET_STATISTICS_update (GST_stats,
1345 ("# KEEPALIVE messages discarded (no session)"),
1349 /* send reply to allow neighbour to measure latency */
1350 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1351 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1352 send_with_session(n,
1353 (const void *) &m, sizeof (m),
1354 UINT32_MAX /* priority */,
1355 KEEPALIVE_FREQUENCY,
1361 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1362 * latency to this peer. Pass the updated information (existing ats
1363 * plus calculated latency) to ATS.
1365 * @param neighbour neighbour to keep alive
1368 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
1370 struct NeighbourMapEntry *n;
1372 struct GNUNET_ATS_Information ats;
1374 if (NULL == (n = lookup_neighbour (neighbour)))
1376 GNUNET_STATISTICS_update (GST_stats,
1378 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1382 if ( (S_CONNECTED != n->state) ||
1383 (GNUNET_YES != n->expect_latency_response) )
1385 GNUNET_STATISTICS_update (GST_stats,
1387 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1391 n->expect_latency_response = GNUNET_NO;
1392 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1393 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1395 "Latency for peer `%s' is %s\n",
1396 GNUNET_i2s (&n->id),
1397 GNUNET_STRINGS_relative_time_to_string (n->latency,
1399 /* append latency */
1400 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1401 if (n->latency.rel_value_us > UINT32_MAX)
1402 latency = UINT32_MAX;
1404 latency = n->latency.rel_value_us;
1405 ats.value = htonl (latency);
1406 GST_ats_update_metrics (&n->id,
1407 n->primary_address.address,
1408 n->primary_address.session,
1414 * We have received a message from the given sender. How long should
1415 * we delay before receiving more? (Also used to keep the peer marked
1418 * @param sender sender of the message
1419 * @param size size of the message
1420 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1421 * GNUNET_NO if the neighbour is not connected or violates the quota,
1422 * GNUNET_SYSERR if the connection is not fully up yet
1423 * @return how long to wait before reading more from this sender
1425 struct GNUNET_TIME_Relative
1426 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1427 *sender, ssize_t size, int *do_forward)
1429 struct NeighbourMapEntry *n;
1430 struct GNUNET_TIME_Relative ret;
1432 if (NULL == neighbours)
1434 *do_forward = GNUNET_NO;
1435 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1437 if (NULL == (n = lookup_neighbour (sender)))
1439 GST_neighbours_try_connect (sender);
1440 if (NULL == (n = lookup_neighbour (sender)))
1442 GNUNET_STATISTICS_update (GST_stats,
1444 ("# messages discarded due to lack of neighbour record"),
1446 *do_forward = GNUNET_NO;
1447 return GNUNET_TIME_UNIT_ZERO;
1450 if (! test_connected (n))
1452 *do_forward = GNUNET_SYSERR;
1453 return GNUNET_TIME_UNIT_ZERO;
1455 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1457 n->quota_violation_count++;
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1459 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1460 n->in_tracker.available_bytes_per_s__,
1461 n->quota_violation_count);
1462 /* Discount 32k per violation */
1463 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1467 if (n->quota_violation_count > 0)
1469 /* try to add 32k back */
1470 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1471 n->quota_violation_count--;
1474 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1476 GNUNET_STATISTICS_update (GST_stats,
1478 ("# bandwidth quota violations by other peers"),
1480 *do_forward = GNUNET_NO;
1481 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1483 *do_forward = GNUNET_YES;
1484 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1485 if (ret.rel_value_us > 0)
1487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1488 "Throttling read (%llu bytes excess at %u b/s), waiting %s before reading more.\n",
1489 (unsigned long long) n->in_tracker.
1490 consumption_since_last_update__,
1491 (unsigned int) n->in_tracker.available_bytes_per_s__,
1492 GNUNET_STRINGS_relative_time_to_string (ret, GNUNET_YES));
1493 GNUNET_STATISTICS_update (GST_stats,
1494 gettext_noop ("# ms throttling suggested"),
1495 (int64_t) ret.rel_value_us / 1000LL,
1503 * Transmit a message to the given target using the active connection.
1505 * @param target destination
1506 * @param msg message to send
1507 * @param msg_size number of bytes in msg
1508 * @param timeout when to fail with timeout
1509 * @param cont function to call when done
1510 * @param cont_cls closure for 'cont'
1513 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1514 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1515 GST_NeighbourSendContinuation cont, void *cont_cls)
1517 struct NeighbourMapEntry *n;
1518 struct MessageQueue *mq;
1520 /* All ove these cases should never happen; they are all API violations.
1521 But we check anyway, just to be sure. */
1522 if (NULL == (n = lookup_neighbour (target)))
1526 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1529 if (GNUNET_YES != test_connected (n))
1533 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1536 bytes_in_send_queue += msg_size;
1537 GNUNET_STATISTICS_set (GST_stats,
1539 ("# bytes in message queue for other peers"),
1540 bytes_in_send_queue, GNUNET_NO);
1541 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1543 mq->cont_cls = cont_cls;
1544 memcpy (&mq[1], msg, msg_size);
1545 mq->message_buf = (const char *) &mq[1];
1546 mq->message_buf_size = msg_size;
1547 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1548 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1549 if ( (NULL != n->is_active) ||
1550 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1552 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1553 GNUNET_SCHEDULER_cancel (n->task);
1554 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1559 * Send a SESSION_CONNECT message via the given address.
1561 * @param na address to use
1564 send_session_connect (struct NeighbourAddress *na)
1566 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1567 struct SessionConnectMessage connect_msg;
1569 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1574 if (NULL == na->session)
1575 na->session = papi->get_session (papi->cls, na->address);
1576 if (NULL == na->session)
1581 GNUNET_STATISTICS_update (GST_stats,
1583 ("# SESSION_CONNECT messages sent"),
1585 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1586 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1587 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1588 connect_msg.reserved = htonl (0);
1589 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1591 papi->send (papi->cls,
1593 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1595 GNUNET_TIME_UNIT_FOREVER_REL,
1598 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1599 _("Failed to transmit CONNECT message via plugin to %s\n"),
1600 GST_plugins_a2s (na->address));
1602 GST_neighbours_notify_data_sent (&na->address->peer,
1605 sizeof (struct SessionConnectMessage));
1611 * Send a SESSION_CONNECT_ACK message via the given address.
1613 * @param address address to use
1614 * @param session session to use
1615 * @param timestamp timestamp to use for the ACK message
1618 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1619 struct Session *session,
1620 struct GNUNET_TIME_Absolute timestamp)
1622 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1623 struct SessionConnectMessage connect_msg;
1625 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1630 if (NULL == session)
1631 session = papi->get_session (papi->cls, address);
1632 if (NULL == session)
1637 GNUNET_STATISTICS_update (GST_stats,
1639 ("# CONNECT_ACK messages sent"),
1641 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1642 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1643 connect_msg.reserved = htonl (0);
1644 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1645 (void) papi->send (papi->cls,
1647 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1649 GNUNET_TIME_UNIT_FOREVER_REL,
1656 * Create a fresh entry in the neighbour map for the given peer
1658 * @param peer peer to create an entry for
1659 * @return new neighbour map entry
1661 static struct NeighbourMapEntry *
1662 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1664 struct NeighbourMapEntry *n;
1666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1667 "Creating new neighbour entry for `%s'\n",
1669 n = GNUNET_new (struct NeighbourMapEntry);
1671 n->state = S_NOT_CONNECTED;
1672 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1673 n->last_util_transmission = GNUNET_TIME_absolute_get();
1674 n->util_payload_bytes_recv = 0;
1675 n->util_payload_bytes_sent = 0;
1676 n->util_total_bytes_recv = 0;
1677 n->util_total_bytes_sent = 0;
1678 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1679 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1680 MAX_BANDWIDTH_CARRY_S);
1681 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1682 GNUNET_assert (GNUNET_OK ==
1683 GNUNET_CONTAINER_multipeermap_put (neighbours,
1685 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1691 * Check if the two given addresses are the same.
1692 * Actually only checks if the sessions are non-NULL
1693 * (which they should be) and then if they are identical;
1694 * the actual addresses don't matter if the session
1695 * pointers match anyway, and we must have session pointers
1698 * @param a1 first address to compare
1699 * @param a2 other address to compare
1700 * @return #GNUNET_NO if the addresses do not match, #GNUNET_YES if they do match
1703 address_matches (const struct NeighbourAddress *a1,
1704 const struct NeighbourAddress *a2)
1706 if ( (NULL == a1->session) ||
1707 (NULL == a2->session) )
1712 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1717 * Try to create a connection to the given target (eventually).
1719 * @param target peer to try to connect to
1722 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1724 struct NeighbourMapEntry *n;
1726 if (NULL == neighbours)
1728 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1729 "Asked to connect to peer `%s' during shutdown\n",
1730 GNUNET_i2s (target));
1731 return; /* during shutdown, do nothing */
1733 n = lookup_neighbour (target);
1734 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1735 "Asked to connect to peer `%s' (state: %s)\n",
1736 GNUNET_i2s (target),
1737 (NULL != n) ? print_state(n->state) : "UNKNOWN PEER");
1742 case S_NOT_CONNECTED:
1743 /* this should not be possible */
1745 free_neighbour (n, GNUNET_NO);
1748 case S_INIT_BLACKLIST:
1749 case S_CONNECT_SENT:
1750 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1751 case S_CONNECT_RECV_ATS:
1752 case S_CONNECT_RECV_BLACKLIST:
1753 case S_CONNECT_RECV_ACK:
1754 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1755 "Ignoring request to try to connect to `%s', already trying!\n",
1756 GNUNET_i2s (target));
1757 return; /* already trying */
1759 case S_RECONNECT_ATS:
1760 case S_RECONNECT_BLACKLIST:
1761 case S_RECONNECT_SENT:
1762 case S_CONNECTED_SWITCHING_BLACKLIST:
1763 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1764 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
1765 "Ignoring request to try to connect, already connected to `%s'!\n",
1766 GNUNET_i2s (target));
1767 return; /* already connected */
1769 /* get rid of remains, ready to re-try immediately */
1770 free_neighbour (n, GNUNET_NO);
1772 case S_DISCONNECT_FINISHED:
1773 /* should not be possible */
1776 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1777 "Unhandled state `%s'\n",
1778 print_state (n->state));
1780 free_neighbour (n, GNUNET_NO);
1784 n = setup_neighbour (target);
1785 n->state = S_INIT_ATS;
1786 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1788 GNUNET_ATS_reset_backoff (GST_ats, target);
1789 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1794 * Function called with the result of a blacklist check.
1796 * @param cls closure with the 'struct BlackListCheckContext'
1797 * @param peer peer this check affects
1798 * @param result #GNUNET_OK if the address is allowed
1801 handle_test_blacklist_cont (void *cls,
1802 const struct GNUNET_PeerIdentity *peer,
1805 struct BlackListCheckContext *bcc = cls;
1806 struct NeighbourMapEntry *n;
1809 GNUNET_CONTAINER_DLL_remove (bc_head,
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1813 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1815 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1816 if (GNUNET_OK == result)
1817 GST_ats_add_address (bcc->na.address, bcc->na.session);
1820 /* Blacklist disagreed on connecting to a peer with this address
1821 * Destroy address because we are not allowed to use it
1823 if (NULL != bcc->na.session)
1824 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, bcc->na.session);
1825 GNUNET_ATS_address_destroyed (GST_ats, bcc->na.address, NULL);
1827 if (NULL == (n = lookup_neighbour (peer)))
1828 goto cleanup; /* nobody left to care about new address */
1831 case S_NOT_CONNECTED:
1832 /* this should not be possible */
1834 free_neighbour (n, GNUNET_NO);
1837 /* waiting on ATS suggestion; still, pass address to ATS as a
1840 case S_INIT_BLACKLIST:
1841 /* check if the address the blacklist was fine with matches
1842 ATS suggestion, if so, we can move on! */
1843 if ( (GNUNET_OK == result) &&
1844 (1 == n->send_connect_ack) )
1846 n->send_connect_ack = 2;
1847 send_session_connect_ack_message (bcc->na.address,
1849 n->connect_ack_timestamp);
1851 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1852 break; /* result for an address we currently don't care about */
1853 if (GNUNET_OK == result)
1855 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1856 n->state = S_CONNECT_SENT;
1857 send_session_connect (&n->primary_address);
1861 free_address (&n->primary_address);
1862 n->state = S_INIT_ATS;
1863 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1866 case S_CONNECT_SENT:
1867 /* waiting on CONNECT_ACK, send ACK if one is pending */
1868 if ( (GNUNET_OK == result) &&
1869 (1 == n->send_connect_ack) )
1871 n->send_connect_ack = 2;
1872 send_session_connect_ack_message (n->primary_address.address,
1873 n->primary_address.session,
1874 n->connect_ack_timestamp);
1877 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1878 n->state = S_CONNECT_RECV_ATS;
1879 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1880 GNUNET_ATS_reset_backoff (GST_ats, peer);
1881 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
1883 case S_CONNECT_RECV_ATS:
1884 /* waiting on ATS suggestion, don't care about blacklist */
1886 case S_CONNECT_RECV_BLACKLIST:
1887 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "Blacklist result ignored, as it is not for our primary address\n");
1891 break; /* result for an address we currently don't care about */
1893 if (GNUNET_OK == result)
1895 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1896 n->state = S_CONNECT_RECV_ACK;
1897 send_session_connect_ack_message (bcc->na.address,
1899 n->connect_ack_timestamp);
1900 if (1 == n->send_connect_ack)
1901 n->send_connect_ack = 2;
1905 struct GNUNET_TRANSPORT_PluginFunctions *plugin;
1907 plugin = GST_plugins_find (bcc->na.address->transport_name);
1908 if ( (NULL != plugin) &&
1909 (NULL != bcc->na.session) )
1911 plugin->disconnect_session (plugin->cls,
1915 GNUNET_break (NULL != plugin);
1916 free_address (&n->primary_address);
1917 n->state = S_INIT_ATS;
1918 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1919 GNUNET_ATS_reset_backoff (GST_ats, peer);
1922 case S_CONNECT_RECV_ACK:
1923 /* waiting on SESSION_ACK, send ACK if one is pending */
1924 if ( (GNUNET_OK == result) &&
1925 (1 == n->send_connect_ack) )
1927 n->send_connect_ack = 2;
1928 send_session_connect_ack_message (n->primary_address.address,
1929 n->primary_address.session,
1930 n->connect_ack_timestamp);
1934 /* already connected, don't care about blacklist */
1936 case S_RECONNECT_ATS:
1937 /* still waiting on ATS suggestion, don't care about blacklist */
1939 case S_RECONNECT_BLACKLIST:
1940 if ( (GNUNET_OK == result) &&
1941 (1 == n->send_connect_ack) )
1943 n->send_connect_ack = 2;
1944 send_session_connect_ack_message (bcc->na.address,
1946 n->connect_ack_timestamp);
1948 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1949 break; /* result for an address we currently don't care about */
1950 if (GNUNET_OK == result)
1952 send_session_connect (&n->primary_address);
1953 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1954 n->state = S_RECONNECT_SENT;
1958 n->state = S_RECONNECT_ATS;
1959 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1962 case S_RECONNECT_SENT:
1963 /* waiting on CONNECT_ACK, don't care about blacklist */
1964 if ( (GNUNET_OK == result) &&
1965 (1 == n->send_connect_ack) )
1967 n->send_connect_ack = 2;
1968 send_session_connect_ack_message (n->primary_address.address,
1969 n->primary_address.session,
1970 n->connect_ack_timestamp);
1973 case S_CONNECTED_SWITCHING_BLACKLIST:
1974 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1975 break; /* result for an address we currently don't care about */
1976 if (GNUNET_OK == result)
1978 send_session_connect (&n->alternative_address);
1979 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1983 free_address (&n->alternative_address);
1984 n->state = S_CONNECTED;
1987 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1988 /* waiting on CONNECT_ACK, don't care about blacklist */
1989 if ( (GNUNET_OK == result) &&
1990 (1 == n->send_connect_ack) )
1992 n->send_connect_ack = 2;
1993 send_session_connect_ack_message (n->primary_address.address,
1994 n->primary_address.session,
1995 n->connect_ack_timestamp);
1999 /* Nothing to do here, ATS will already do what can be done */
2001 case S_DISCONNECT_FINISHED:
2002 /* should not be possible */
2006 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2007 "Unhandled state `%s'\n",
2008 print_state (n->state));
2010 free_neighbour (n, GNUNET_NO);
2014 GNUNET_HELLO_address_free (bcc->na.address);
2020 * We want to know if connecting to a particular peer via
2021 * a particular address is allowed. Check it!
2023 * @param peer identity of the peer to switch the address for
2024 * @param ts time at which the check was initiated
2025 * @param address address of the other peer, NULL if other peer
2027 * @param session session to use (or NULL)
2030 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2031 struct GNUNET_TIME_Absolute ts,
2032 const struct GNUNET_HELLO_Address *address,
2033 struct Session *session)
2035 struct BlackListCheckContext *bcc;
2036 struct GST_BlacklistCheck *bc;
2038 bcc = GNUNET_new (struct BlackListCheckContext);
2039 bcc->na.address = GNUNET_HELLO_address_copy (address);
2040 bcc->na.session = session;
2041 bcc->na.connect_timestamp = ts;
2042 GNUNET_CONTAINER_DLL_insert (bc_head,
2045 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2046 address->transport_name,
2047 &handle_test_blacklist_cont, bcc)))
2049 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2050 we must only store 'bc' if 'bc' is non-NULL... */
2055 * We received a 'SESSION_CONNECT' message from the other peer.
2056 * Consider switching to it.
2058 * @param message possibly a 'struct SessionConnectMessage' (check format)
2059 * @param peer identity of the peer to switch the address for
2060 * @param address address of the other peer, NULL if other peer
2062 * @param session session to use (or NULL)
2063 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2066 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2067 const struct GNUNET_PeerIdentity *peer,
2068 const struct GNUNET_HELLO_Address *address,
2069 struct Session *session)
2071 const struct SessionConnectMessage *scm;
2072 struct NeighbourMapEntry *n;
2073 struct GNUNET_TIME_Absolute ts;
2075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2076 "Received CONNECT message from peer `%s'\n",
2078 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2080 GNUNET_break_op (0);
2081 return GNUNET_SYSERR;
2083 GNUNET_STATISTICS_update (GST_stats,
2085 ("# CONNECT messages received"),
2087 if (NULL == neighbours)
2089 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2090 _("CONNECT request from peer `%s' ignored due impending shutdown\n"),
2092 return GNUNET_OK; /* we're shutting down */
2094 scm = (const struct SessionConnectMessage *) message;
2095 GNUNET_break_op (0 == ntohl (scm->reserved));
2096 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2097 n = lookup_neighbour (peer);
2099 n = setup_neighbour (peer);
2100 n->send_connect_ack = 1;
2101 n->connect_ack_timestamp = ts;
2105 case S_NOT_CONNECTED:
2106 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2107 /* Do a blacklist check for the new address */
2108 check_blacklist (peer, ts, address, session);
2111 /* CONNECT message takes priority over us asking ATS for address */
2112 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2114 case S_INIT_BLACKLIST:
2115 case S_CONNECT_SENT:
2116 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2117 case S_CONNECT_RECV_ATS:
2118 case S_CONNECT_RECV_BLACKLIST:
2119 case S_CONNECT_RECV_ACK:
2120 /* It can never hurt to have an alternative address in the above cases,
2121 see if it is allowed */
2122 check_blacklist (peer, ts, address, session);
2125 /* we are already connected and can thus send the ACK immediately;
2126 still, it can never hurt to have an alternative address, so also
2127 tell ATS about it */
2128 GNUNET_assert (NULL != n->primary_address.address);
2129 GNUNET_assert (NULL != n->primary_address.session);
2130 n->send_connect_ack = 0;
2131 send_session_connect_ack_message (n->primary_address.address,
2132 n->primary_address.session, ts);
2133 check_blacklist (peer, ts, address, session);
2135 case S_RECONNECT_ATS:
2136 case S_RECONNECT_BLACKLIST:
2137 case S_RECONNECT_SENT:
2138 /* It can never hurt to have an alternative address in the above cases,
2139 see if it is allowed */
2140 check_blacklist (peer, ts, address, session);
2142 case S_CONNECTED_SWITCHING_BLACKLIST:
2143 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2144 /* we are already connected and can thus send the ACK immediately;
2145 still, it can never hurt to have an alternative address, so also
2146 tell ATS about it */
2147 GNUNET_assert (NULL != n->primary_address.address);
2148 GNUNET_assert (NULL != n->primary_address.session);
2149 n->send_connect_ack = 0;
2150 send_session_connect_ack_message (n->primary_address.address,
2151 n->primary_address.session, ts);
2152 check_blacklist (peer, ts, address, session);
2155 /* get rid of remains without terminating sessions, ready to re-try */
2156 free_neighbour (n, GNUNET_YES);
2157 n = setup_neighbour (peer);
2158 n->state = S_CONNECT_RECV_ATS;
2159 GNUNET_ATS_reset_backoff (GST_ats, peer);
2160 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2162 case S_DISCONNECT_FINISHED:
2163 /* should not be possible */
2167 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2168 "Unhandled state `%s'\n",
2169 print_state (n->state));
2171 return GNUNET_SYSERR;
2178 * For an existing neighbour record, set the active connection to
2179 * use the given address.
2181 * @param peer identity of the peer to switch the address for
2182 * @param address address of the other peer, NULL if other peer
2184 * @param session session to use (or NULL)
2185 * @param ats performance data
2186 * @param ats_count number of entries in ats
2187 * @param bandwidth_in inbound quota to be used when connection is up,
2188 * 0 to disconnect from peer
2189 * @param bandwidth_out outbound quota to be used when connection is up,
2190 * 0 to disconnect from peer
2193 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2194 const struct GNUNET_HELLO_Address *address,
2195 struct Session *session,
2196 const struct GNUNET_ATS_Information *ats,
2198 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
2199 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out)
2201 struct NeighbourMapEntry *n;
2202 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2204 GNUNET_assert (address->transport_name != NULL);
2205 if (NULL == (n = lookup_neighbour (peer)))
2208 /* Obtain an session for this address from plugin */
2209 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2211 /* we don't have the plugin for this address */
2212 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2215 if ((NULL == session) && (0 == address->address_length))
2218 if (strlen (address->transport_name) > 0)
2219 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2223 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2224 "ATS tells us to switch to address '%s' session %p for "
2225 "peer `%s' in state %s/%d (quota in/out %u %u )\n",
2226 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2229 print_state (n->state),
2230 n->send_connect_ack,
2231 ntohl (bandwidth_in.value__),
2232 ntohl (bandwidth_out.value__));
2234 if (NULL == session)
2236 session = papi->get_session (papi->cls, address);
2237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2238 "Obtained new session for peer `%s' and address '%s': %p\n",
2239 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2241 if (NULL == session)
2243 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2244 "Failed to obtain new session for peer `%s' and address '%s'\n",
2245 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2246 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2251 case S_NOT_CONNECTED:
2253 free_neighbour (n, GNUNET_NO);
2256 set_address (&n->primary_address,
2257 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2258 n->state = S_INIT_BLACKLIST;
2259 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2260 check_blacklist (&n->id,
2261 n->connect_ack_timestamp,
2264 case S_INIT_BLACKLIST:
2265 /* ATS suggests a different address, switch again */
2266 set_address (&n->primary_address,
2267 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2268 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2269 check_blacklist (&n->id,
2270 n->connect_ack_timestamp,
2273 case S_CONNECT_SENT:
2274 /* ATS suggests a different address, switch again */
2275 set_address (&n->primary_address,
2276 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2277 n->state = S_INIT_BLACKLIST;
2278 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2279 check_blacklist (&n->id,
2280 n->connect_ack_timestamp,
2283 case S_CONNECT_RECV_ATS:
2284 set_address (&n->primary_address,
2285 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2286 n->state = S_CONNECT_RECV_BLACKLIST;
2287 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2288 check_blacklist (&n->id,
2289 n->connect_ack_timestamp,
2292 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2293 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2294 check_blacklist (&n->id,
2295 n->connect_ack_timestamp,
2298 case S_CONNECT_RECV_BLACKLIST:
2299 case S_CONNECT_RECV_ACK:
2300 /* ATS asks us to switch while we were trying to connect; switch to new
2301 address and check blacklist again */
2302 set_address (&n->primary_address,
2303 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2304 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2305 check_blacklist (&n->id,
2306 n->connect_ack_timestamp,
2310 GNUNET_assert (NULL != n->primary_address.address);
2311 GNUNET_assert (NULL != n->primary_address.session);
2312 if (n->primary_address.session == session)
2314 /* not an address change, just a quota change */
2315 set_address (&n->primary_address,
2316 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2319 /* ATS asks us to switch a life connection; see if we can get
2320 a CONNECT_ACK on it before we actually do this! */
2321 set_address (&n->alternative_address,
2322 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2323 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2324 check_blacklist (&n->id,
2325 GNUNET_TIME_absolute_get (),
2328 case S_RECONNECT_ATS:
2329 set_address (&n->primary_address,
2330 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2331 n->state = S_RECONNECT_BLACKLIST;
2332 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2333 check_blacklist (&n->id,
2334 n->connect_ack_timestamp,
2337 case S_RECONNECT_BLACKLIST:
2338 /* ATS asks us to switch while we were trying to reconnect; switch to new
2339 address and check blacklist again */
2340 set_address (&n->primary_address,
2341 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2342 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2343 check_blacklist (&n->id,
2344 n->connect_ack_timestamp,
2347 case S_RECONNECT_SENT:
2348 /* ATS asks us to switch while we were trying to reconnect; switch to new
2349 address and check blacklist again */
2350 set_address (&n->primary_address,
2351 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2352 n->state = S_RECONNECT_BLACKLIST;
2353 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2354 check_blacklist (&n->id,
2355 n->connect_ack_timestamp,
2358 case S_CONNECTED_SWITCHING_BLACKLIST:
2359 if (n->primary_address.session == session)
2361 /* ATS switches back to still-active session */
2362 free_address (&n->alternative_address);
2363 n->state = S_CONNECTED;
2366 /* ATS asks us to switch a life connection, update blacklist check */
2367 set_address (&n->alternative_address,
2368 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2369 check_blacklist (&n->id,
2370 GNUNET_TIME_absolute_get (),
2373 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2374 if (n->primary_address.session == session)
2376 /* ATS switches back to still-active session */
2377 free_address (&n->alternative_address);
2378 n->state = S_CONNECTED;
2381 /* ATS asks us to switch a life connection, update blacklist check */
2382 set_address (&n->alternative_address,
2383 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2384 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2385 check_blacklist (&n->id,
2386 GNUNET_TIME_absolute_get (),
2390 /* not going to switch addresses while disconnecting */
2392 case S_DISCONNECT_FINISHED:
2396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2397 "Unhandled state `%s'\n",
2398 print_state (n->state));
2406 send_utilization_data (void *cls,
2407 const struct GNUNET_PeerIdentity *key,
2410 struct NeighbourMapEntry *n = value;
2411 struct GNUNET_ATS_Information atsi[4];
2413 uint32_t bps_pl_out;
2416 struct GNUNET_TIME_Relative delta;
2418 delta = GNUNET_TIME_absolute_get_difference (n->last_util_transmission,
2419 GNUNET_TIME_absolute_get ());
2423 if ((0 != n->util_payload_bytes_recv) && (0 != delta.rel_value_us))
2424 bps_pl_in = (1000LL * 1000LL * n->util_payload_bytes_recv) / (delta.rel_value_us);
2426 if ((0 != n->util_payload_bytes_sent) && (0 != delta.rel_value_us))
2427 bps_pl_out = (1000LL * 1000LL * n->util_payload_bytes_sent) / delta.rel_value_us;
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2429 "`%s' payload: received %u Bytes/s, sent %u Bytes/s\n",
2434 if ((0 != n->util_total_bytes_recv) && (0 != delta.rel_value_us))
2435 bps_in = (1000LL * 1000LL * n->util_total_bytes_recv) / (delta.rel_value_us);
2437 if ((0 != n->util_total_bytes_sent) && (0 != delta.rel_value_us))
2438 bps_out = (1000LL * 1000LL * n->util_total_bytes_sent) / delta.rel_value_us;
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2442 "`%s' total: received %u Bytes/s, sent %u Bytes/s\n",
2446 atsi[0].type = htonl (GNUNET_ATS_UTILIZATION_OUT);
2447 atsi[0].value = htonl (bps_out);
2448 atsi[1].type = htonl (GNUNET_ATS_UTILIZATION_IN);
2449 atsi[1].value = htonl (bps_in);
2451 atsi[2].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_OUT);
2452 atsi[2].value = htonl (bps_pl_out);
2453 atsi[3].type = htonl (GNUNET_ATS_UTILIZATION_PAYLOAD_IN);
2454 atsi[3].value = htonl (bps_pl_in);
2456 GST_ats_update_metrics (key, n->primary_address.address,
2457 n->primary_address.session, atsi, 4);
2458 n->util_payload_bytes_recv = 0;
2459 n->util_payload_bytes_sent = 0;
2460 n->util_total_bytes_recv = 0;
2461 n->util_total_bytes_sent = 0;
2462 n->last_util_transmission = GNUNET_TIME_absolute_get();
2468 * Task transmitting utilization in a regular interval
2470 * @param cls the 'struct NeighbourMapEntry' for which we are running
2471 * @param tc scheduler context (unused)
2474 utilization_transmission (void *cls,
2475 const struct GNUNET_SCHEDULER_TaskContext *tc)
2477 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
2479 if (0 < GNUNET_CONTAINER_multipeermap_size (neighbours))
2480 GNUNET_CONTAINER_multipeermap_iterate (neighbours, send_utilization_data, NULL);
2482 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
2483 utilization_transmission, NULL);
2489 GST_neighbours_notify_data_recv (const struct GNUNET_PeerIdentity *peer,
2490 const struct GNUNET_HELLO_Address *address,
2491 struct Session *session,
2492 const struct GNUNET_MessageHeader *message)
2494 struct NeighbourMapEntry *n;
2496 n = lookup_neighbour (peer);
2499 n->util_total_bytes_recv += ntohs(message->size);
2504 GST_neighbours_notify_payload_recv (const struct GNUNET_PeerIdentity *peer,
2505 const struct GNUNET_HELLO_Address *address,
2506 struct Session *session,
2507 const struct GNUNET_MessageHeader *message)
2509 struct NeighbourMapEntry *n;
2510 n = lookup_neighbour (peer);
2513 n->util_payload_bytes_recv += ntohs(message->size);
2518 GST_neighbours_notify_data_sent (const struct GNUNET_PeerIdentity *peer,
2519 const struct GNUNET_HELLO_Address *address,
2520 struct Session *session,
2523 struct NeighbourMapEntry *n;
2524 n = lookup_neighbour (peer);
2527 if (n->primary_address.session != session)
2529 n->util_total_bytes_sent += size;
2534 GST_neighbours_notify_payload_sent (const struct GNUNET_PeerIdentity *peer,
2537 struct NeighbourMapEntry *n;
2538 n = lookup_neighbour (peer);
2541 n->util_payload_bytes_sent += size;
2546 * Master task run for every neighbour. Performs all of the time-related
2547 * activities (keep alive, send next message, disconnect if idle, finish
2548 * clean up after disconnect).
2550 * @param cls the 'struct NeighbourMapEntry' for which we are running
2551 * @param tc scheduler context (unused)
2554 master_task (void *cls,
2555 const struct GNUNET_SCHEDULER_TaskContext *tc)
2557 struct NeighbourMapEntry *n = cls;
2558 struct GNUNET_TIME_Relative delay;
2560 n->task = GNUNET_SCHEDULER_NO_TASK;
2561 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2562 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2563 "Master task runs for neighbour `%s' in state %s with timeout in %s\n",
2564 GNUNET_i2s (&n->id),
2565 print_state(n->state),
2566 GNUNET_STRINGS_relative_time_to_string (delay,
2570 case S_NOT_CONNECTED:
2571 /* invalid state for master task, clean up */
2573 n->state = S_DISCONNECT_FINISHED;
2574 free_neighbour (n, GNUNET_NO);
2577 if (0 == delay.rel_value_us)
2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2580 "Connection to `%s' timed out waiting for ATS to provide address\n",
2581 GNUNET_i2s (&n->id));
2582 n->state = S_DISCONNECT_FINISHED;
2583 free_neighbour (n, GNUNET_NO);
2587 case S_INIT_BLACKLIST:
2588 if (0 == delay.rel_value_us)
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2591 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2592 GNUNET_i2s (&n->id));
2593 n->state = S_DISCONNECT_FINISHED;
2594 free_neighbour (n, GNUNET_NO);
2598 case S_CONNECT_SENT:
2599 if (0 == delay.rel_value_us)
2601 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2602 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2603 GNUNET_i2s (&n->id));
2604 /* We could not send to this address, delete address and session */
2605 if (NULL != n->primary_address.session)
2606 GNUNET_ATS_address_destroyed (GST_ats,
2607 n->primary_address.address, n->primary_address.session);
2608 GNUNET_ATS_address_destroyed (GST_ats,
2609 n->primary_address.address, NULL);
2610 disconnect_neighbour (n);
2614 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2615 if (0 == delay.rel_value_us)
2617 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2618 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2619 GNUNET_i2s (&n->id));
2620 n->state = S_DISCONNECT_FINISHED;
2621 free_neighbour (n, GNUNET_NO);
2625 case S_CONNECT_RECV_ATS:
2626 if (0 == delay.rel_value_us)
2628 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2629 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2630 GNUNET_i2s (&n->id));
2631 n->state = S_DISCONNECT_FINISHED;
2632 free_neighbour (n, GNUNET_NO);
2636 case S_CONNECT_RECV_BLACKLIST:
2637 if (0 == delay.rel_value_us)
2639 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2640 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2641 GNUNET_i2s (&n->id));
2642 n->state = S_DISCONNECT_FINISHED;
2643 free_neighbour (n, GNUNET_NO);
2647 case S_CONNECT_RECV_ACK:
2648 if (0 == delay.rel_value_us)
2650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2651 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2652 GNUNET_i2s (&n->id));
2653 disconnect_neighbour (n);
2658 if (0 == delay.rel_value_us)
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2661 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2662 GNUNET_i2s (&n->id));
2663 disconnect_neighbour (n);
2666 try_transmission_to_peer (n);
2669 case S_RECONNECT_ATS:
2670 if (0 == delay.rel_value_us)
2672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2673 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2674 GNUNET_i2s (&n->id));
2675 disconnect_neighbour (n);
2679 case S_RECONNECT_BLACKLIST:
2680 if (0 == delay.rel_value_us)
2682 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2683 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2684 GNUNET_i2s (&n->id));
2685 disconnect_neighbour (n);
2689 case S_RECONNECT_SENT:
2690 if (0 == delay.rel_value_us)
2692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2693 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2694 GNUNET_i2s (&n->id));
2695 disconnect_neighbour (n);
2699 case S_CONNECTED_SWITCHING_BLACKLIST:
2700 if (0 == delay.rel_value_us)
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2703 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2704 GNUNET_i2s (&n->id));
2705 disconnect_neighbour (n);
2708 try_transmission_to_peer (n);
2711 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2712 if (0 == delay.rel_value_us)
2714 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2715 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2716 GNUNET_i2s (&n->id));
2717 disconnect_neighbour (n);
2720 try_transmission_to_peer (n);
2724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2725 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2726 GNUNET_i2s (&n->id));
2727 free_neighbour (n, GNUNET_NO);
2729 case S_DISCONNECT_FINISHED:
2730 /* how did we get here!? */
2734 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2735 "Unhandled state `%s'\n",
2736 print_state (n->state));
2740 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2741 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2742 (S_CONNECTED == n->state) )
2744 /* if we are *now* in one of these three states, we're sending
2745 keep alive messages, so we need to consider the keepalive
2746 delay, not just the connection timeout */
2747 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2750 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2751 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2758 * Send a SESSION_ACK message to the neighbour to confirm that we
2759 * got his CONNECT_ACK.
2761 * @param n neighbour to send the SESSION_ACK to
2764 send_session_ack_message (struct NeighbourMapEntry *n)
2766 struct GNUNET_MessageHeader msg;
2768 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2769 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2770 (void) send_with_session(n,
2771 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2772 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2778 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2779 * Consider switching to it.
2781 * @param message possibly a 'struct SessionConnectMessage' (check format)
2782 * @param peer identity of the peer to switch the address for
2783 * @param address address of the other peer, NULL if other peer
2785 * @param session session to use (or NULL)
2786 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
2789 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2790 const struct GNUNET_PeerIdentity *peer,
2791 const struct GNUNET_HELLO_Address *address,
2792 struct Session *session)
2794 const struct SessionConnectMessage *scm;
2795 struct GNUNET_TIME_Absolute ts;
2796 struct NeighbourMapEntry *n;
2798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2799 "Received CONNECT_ACK message from peer `%s'\n",
2802 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2804 GNUNET_break_op (0);
2805 return GNUNET_SYSERR;
2807 GNUNET_STATISTICS_update (GST_stats,
2809 ("# CONNECT_ACK messages received"),
2811 scm = (const struct SessionConnectMessage *) message;
2812 GNUNET_break_op (ntohl (scm->reserved) == 0);
2813 if (NULL == (n = lookup_neighbour (peer)))
2815 GNUNET_STATISTICS_update (GST_stats,
2817 ("# unexpected CONNECT_ACK messages (no peer)"),
2819 return GNUNET_SYSERR;
2821 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2824 case S_NOT_CONNECTED:
2826 free_neighbour (n, GNUNET_NO);
2827 return GNUNET_SYSERR;
2829 case S_INIT_BLACKLIST:
2830 GNUNET_STATISTICS_update (GST_stats,
2832 ("# unexpected CONNECT_ACK messages (not ready)"),
2835 case S_CONNECT_SENT:
2836 if (ts.abs_value_us != n->primary_address.connect_timestamp.abs_value_us)
2838 GNUNET_log (GNUNET_ERROR_TYPE_INFO,
2839 "CONNECT_ACK ignored as the timestamp does not match our CONNECT request\n");
2842 n->state = S_CONNECTED;
2843 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2844 GNUNET_STATISTICS_set (GST_stats,
2845 gettext_noop ("# peers connected"),
2846 ++neighbours_connected,
2848 connect_notify_cb (callback_cls, &n->id,
2849 n->primary_address.bandwidth_in,
2850 n->primary_address.bandwidth_out);
2851 /* Tell ATS that the outbound session we created to send CONNECT was successful */
2852 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
2853 set_address (&n->primary_address,
2854 n->primary_address.address,
2855 n->primary_address.session,
2856 n->primary_address.bandwidth_in,
2857 n->primary_address.bandwidth_out,
2859 send_session_ack_message (n);
2861 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2862 case S_CONNECT_RECV_ATS:
2863 case S_CONNECT_RECV_BLACKLIST:
2864 case S_CONNECT_RECV_ACK:
2865 GNUNET_STATISTICS_update (GST_stats,
2867 ("# unexpected CONNECT_ACK messages (not ready)"),
2871 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2872 send_session_ack_message (n);
2874 case S_RECONNECT_ATS:
2875 case S_RECONNECT_BLACKLIST:
2876 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2877 to give us a new address... */
2878 GNUNET_STATISTICS_update (GST_stats,
2880 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2883 case S_RECONNECT_SENT:
2884 /* new address worked; go back to connected! */
2885 n->state = S_CONNECTED;
2886 send_session_ack_message (n);
2888 case S_CONNECTED_SWITCHING_BLACKLIST:
2889 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2890 send_session_ack_message (n);
2892 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2893 /* new address worked; adopt it and go back to connected! */
2894 n->state = S_CONNECTED;
2895 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2896 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2898 GST_ats_add_address (n->alternative_address.address, n->alternative_address.session);
2899 set_address (&n->primary_address,
2900 n->alternative_address.address,
2901 n->alternative_address.session,
2902 n->alternative_address.bandwidth_in,
2903 n->alternative_address.bandwidth_out,
2905 free_address (&n->alternative_address);
2906 send_session_ack_message (n);
2909 GNUNET_STATISTICS_update (GST_stats,
2911 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2913 return GNUNET_SYSERR;
2914 case S_DISCONNECT_FINISHED:
2918 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2919 "Unhandled state `%s'\n",
2920 print_state (n->state));
2922 return GNUNET_SYSERR;
2929 * A session was terminated. Take note; if needed, try to get
2930 * an alternative address from ATS.
2932 * @param peer identity of the peer where the session died
2933 * @param session session that is gone
2934 * @return #GNUNET_YES if this was a session used, #GNUNET_NO if
2935 * this session was not in use
2938 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2939 struct Session *session)
2941 struct NeighbourMapEntry *n;
2942 struct BlackListCheckContext *bcc;
2943 struct BlackListCheckContext *bcc_next;
2945 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2947 while (NULL != (bcc = bcc_next))
2949 bcc_next = bcc->next;
2950 if (bcc->na.session == session)
2952 if (NULL != bcc->bc)
2953 GST_blacklist_test_cancel (bcc->bc);
2954 GNUNET_HELLO_address_free (bcc->na.address);
2955 GNUNET_CONTAINER_DLL_remove (bc_head,
2961 if (NULL == (n = lookup_neighbour (peer)))
2962 return GNUNET_NO; /* can't affect us */
2963 if (session != n->primary_address.session)
2965 if (session == n->alternative_address.session)
2967 free_address (&n->alternative_address);
2968 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2969 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2970 n->state = S_CONNECTED;
2974 return GNUNET_NO; /* doesn't affect us further */
2977 n->expect_latency_response = GNUNET_NO;
2980 case S_NOT_CONNECTED:
2982 free_neighbour (n, GNUNET_NO);
2986 free_neighbour (n, GNUNET_NO);
2988 case S_INIT_BLACKLIST:
2989 case S_CONNECT_SENT:
2990 free_address (&n->primary_address);
2991 n->state = S_INIT_ATS;
2992 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2993 // FIXME: need to ask ATS for suggestions again?
2994 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2996 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2997 case S_CONNECT_RECV_ATS:
2998 case S_CONNECT_RECV_BLACKLIST:
2999 case S_CONNECT_RECV_ACK:
3000 /* error on inbound session; free neighbour entirely */
3001 free_address (&n->primary_address);
3002 free_neighbour (n, GNUNET_NO);
3005 free_address (&n->primary_address);
3006 n->state = S_RECONNECT_ATS;
3007 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3008 /* FIXME: is this ATS call needed? */
3009 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3011 case S_RECONNECT_ATS:
3012 /* we don't have an address, how can it go down? */
3015 case S_RECONNECT_BLACKLIST:
3016 case S_RECONNECT_SENT:
3017 n->state = S_RECONNECT_ATS;
3018 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3019 // FIXME: need to ask ATS for suggestions again?
3020 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3022 case S_CONNECTED_SWITCHING_BLACKLIST:
3023 /* primary went down while we were checking secondary against
3024 blacklist, adopt secondary as primary */
3025 free_address (&n->primary_address);
3026 n->primary_address = n->alternative_address;
3027 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3028 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3029 n->state = S_RECONNECT_BLACKLIST;
3031 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3032 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3033 secondary as primary */
3034 free_address (&n->primary_address);
3035 n->primary_address = n->alternative_address;
3036 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3037 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3038 n->state = S_RECONNECT_SENT;
3041 free_address (&n->primary_address);
3043 case S_DISCONNECT_FINISHED:
3044 /* neighbour was freed and plugins told to terminate session */
3048 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3049 "Unhandled state `%s'\n",
3050 print_state (n->state));
3054 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3055 GNUNET_SCHEDULER_cancel (n->task);
3056 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3062 * We received a 'SESSION_ACK' message from the other peer.
3063 * If we sent a 'CONNECT_ACK' last, this means we are now
3064 * connected. Otherwise, do nothing.
3066 * @param message possibly a 'struct SessionConnectMessage' (check format)
3067 * @param peer identity of the peer to switch the address for
3068 * @param address address of the other peer, NULL if other peer
3070 * @param session session to use (or NULL)
3071 * @return #GNUNET_OK if the message was fine, #GNUNET_SYSERR on serious error
3074 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3075 const struct GNUNET_PeerIdentity *peer,
3076 const struct GNUNET_HELLO_Address *address,
3077 struct Session *session)
3079 struct NeighbourMapEntry *n;
3081 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3082 "Received SESSION_ACK message from peer `%s'\n",
3084 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3086 GNUNET_break_op (0);
3087 return GNUNET_SYSERR;
3089 GNUNET_STATISTICS_update (GST_stats,
3091 ("# SESSION_ACK messages received"),
3093 if (NULL == (n = lookup_neighbour (peer)))
3095 GNUNET_break_op (0);
3096 return GNUNET_SYSERR;
3098 /* check if we are in a plausible state for having sent
3099 a CONNECT_ACK. If not, return, otherwise break */
3100 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3101 (S_CONNECT_SENT != n->state) ) ||
3102 (2 != n->send_connect_ack) )
3104 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3105 "Received SESSION_ACK message from peer `%s' in state %s/%d\n",
3107 print_state (n->state),
3108 n->send_connect_ack);
3109 GNUNET_STATISTICS_update (GST_stats,
3110 gettext_noop ("# unexpected SESSION_ACK messages"), 1,
3114 n->state = S_CONNECTED;
3115 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3116 GNUNET_STATISTICS_set (GST_stats,
3117 gettext_noop ("# peers connected"),
3118 ++neighbours_connected,
3120 connect_notify_cb (callback_cls, &n->id,
3121 n->primary_address.bandwidth_in,
3122 n->primary_address.bandwidth_out);
3124 GST_ats_add_address (n->primary_address.address, n->primary_address.session);
3125 set_address (&n->primary_address,
3126 n->primary_address.address,
3127 n->primary_address.session,
3128 n->primary_address.bandwidth_in,
3129 n->primary_address.bandwidth_out,
3136 * Test if we're connected to the given peer.
3138 * @param target peer to test
3139 * @return #GNUNET_YES if we are connected, #GNUNET_NO if not
3142 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3144 return test_connected (lookup_neighbour (target));
3149 * Change the incoming quota for the given peer.
3151 * @param neighbour identity of peer to change qutoa for
3152 * @param quota new quota
3155 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3156 struct GNUNET_BANDWIDTH_Value32NBO quota)
3158 struct NeighbourMapEntry *n;
3160 if (NULL == (n = lookup_neighbour (neighbour)))
3162 GNUNET_STATISTICS_update (GST_stats,
3164 ("# SET QUOTA messages ignored (no such peer)"),
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3170 ntohl (quota.value__), GNUNET_i2s (&n->id));
3171 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3172 if (0 != ntohl (quota.value__))
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
3175 GNUNET_i2s (&n->id), "SET_QUOTA");
3176 if (GNUNET_YES == test_connected (n))
3177 GNUNET_STATISTICS_update (GST_stats,
3178 gettext_noop ("# disconnects due to quota of 0"),
3180 disconnect_neighbour (n);
3185 * We received a disconnect message from the given peer,
3186 * validate and process.
3188 * @param peer sender of the message
3189 * @param msg the disconnect message
3192 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
3194 const struct GNUNET_MessageHeader
3197 struct NeighbourMapEntry *n;
3198 const struct SessionDisconnectMessage *sdm;
3200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3201 "Received DISCONNECT message from peer `%s'\n",
3203 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3205 // GNUNET_break_op (0);
3206 GNUNET_STATISTICS_update (GST_stats,
3208 ("# disconnect messages ignored (old format)"), 1,
3212 GNUNET_STATISTICS_update (GST_stats,
3214 ("# DISCONNECT messages received"),
3216 sdm = (const struct SessionDisconnectMessage *) msg;
3217 if (NULL == (n = lookup_neighbour (peer)))
3218 return; /* gone already */
3219 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value_us <= n->connect_ack_timestamp.abs_value_us)
3221 GNUNET_STATISTICS_update (GST_stats,
3223 ("# disconnect messages ignored (timestamp)"), 1,
3227 if (0 != memcmp (peer, &sdm->public_key, sizeof (struct GNUNET_PeerIdentity)))
3229 GNUNET_break_op (0);
3232 if (ntohl (sdm->purpose.size) !=
3233 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3234 sizeof (struct GNUNET_CRYPTO_EddsaPublicKey) +
3235 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3237 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3238 "%s message from peer `%s' has invalid size \n",
3241 GNUNET_break_op (0);
3245 GNUNET_CRYPTO_eddsa_verify
3246 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3247 &sdm->signature, &sdm->public_key))
3249 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
3250 "%s message from peer `%s' cannot be verified \n",
3253 GNUNET_break_op (0);
3256 if (GNUNET_YES == test_connected (n))
3257 GNUNET_STATISTICS_update (GST_stats,
3259 ("# other peer asked to disconnect from us"), 1,
3261 disconnect_neighbour (n);
3266 * Closure for the neighbours_iterate function.
3268 struct IteratorContext
3271 * Function to call on each connected neighbour.
3273 GST_NeighbourIterator cb;
3283 * Call the callback from the closure for each connected neighbour.
3285 * @param cls the `struct IteratorContext`
3286 * @param key the hash of the public key of the neighbour
3287 * @param value the `struct NeighbourMapEntry`
3288 * @return #GNUNET_OK (continue to iterate)
3291 neighbours_iterate (void *cls,
3292 const struct GNUNET_PeerIdentity *key,
3295 struct IteratorContext *ic = cls;
3296 struct NeighbourMapEntry *n = value;
3297 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3298 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3300 if (GNUNET_YES != test_connected (n))
3303 if (NULL != n->primary_address.address)
3305 bandwidth_in = n->primary_address.bandwidth_in;
3306 bandwidth_out = n->primary_address.bandwidth_out;
3310 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3311 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3313 ic->cb (ic->cb_cls, &n->id,
3314 n->primary_address.address,
3315 bandwidth_in, bandwidth_out);
3321 * Iterate over all connected neighbours.
3323 * @param cb function to call
3324 * @param cb_cls closure for cb
3327 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3329 struct IteratorContext ic;
3331 if (NULL == neighbours)
3332 return; /* can happen during shutdown */
3335 GNUNET_CONTAINER_multipeermap_iterate (neighbours, &neighbours_iterate, &ic);
3340 * If we have an active connection to the given target, it must be shutdown.
3342 * @param target peer to disconnect from
3345 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3347 struct NeighbourMapEntry *n;
3349 if (NULL == (n = lookup_neighbour (target)))
3350 return; /* not active */
3351 if (GNUNET_YES == test_connected (n))
3352 GNUNET_STATISTICS_update (GST_stats,
3354 ("# disconnected from peer upon explicit request"), 1,
3356 disconnect_neighbour (n);
3361 * Obtain current latency information for the given neighbour.
3363 * @param peer to get the latency for
3364 * @return observed latency of the address, FOREVER if the
3365 * the connection is not up
3367 struct GNUNET_TIME_Relative
3368 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3370 struct NeighbourMapEntry *n;
3372 n = lookup_neighbour (peer);
3374 return GNUNET_TIME_UNIT_FOREVER_REL;
3378 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3379 case S_CONNECTED_SWITCHING_BLACKLIST:
3380 case S_RECONNECT_SENT:
3381 case S_RECONNECT_ATS:
3382 case S_RECONNECT_BLACKLIST:
3384 case S_NOT_CONNECTED:
3385 case S_INIT_BLACKLIST:
3387 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3388 case S_CONNECT_RECV_ATS:
3389 case S_CONNECT_RECV_BLACKLIST:
3390 case S_CONNECT_RECV_ACK:
3391 case S_CONNECT_SENT:
3393 case S_DISCONNECT_FINISHED:
3394 return GNUNET_TIME_UNIT_FOREVER_REL;
3396 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
3397 "Unhandled state `%s'\n",
3398 print_state (n->state));
3402 return GNUNET_TIME_UNIT_FOREVER_REL;
3407 * Obtain current address information for the given neighbour.
3410 * @return address currently used
3412 struct GNUNET_HELLO_Address *
3413 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3415 struct NeighbourMapEntry *n;
3417 n = lookup_neighbour (peer);
3420 return n->primary_address.address;
3425 * Initialize the neighbours subsystem.
3427 * @param cls closure for callbacks
3428 * @param connect_cb function to call if we connect to a peer
3429 * @param disconnect_cb function to call if we disconnect from a peer
3430 * @param peer_address_cb function to call if we change an active address
3432 * @param max_fds maximum number of fds to use
3435 GST_neighbours_start (void *cls,
3436 NotifyConnect connect_cb,
3437 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3438 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3439 unsigned int max_fds)
3442 connect_notify_cb = connect_cb;
3443 disconnect_notify_cb = disconnect_cb;
3444 address_change_cb = peer_address_cb;
3445 neighbours = GNUNET_CONTAINER_multipeermap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3446 util_transmission_tk = GNUNET_SCHEDULER_add_delayed (UTIL_TRANSMISSION_INTERVAL,
3447 utilization_transmission, NULL);
3452 * Disconnect from the given neighbour.
3455 * @param key hash of neighbour's public key (not used)
3456 * @param value the 'struct NeighbourMapEntry' of the neighbour
3457 * @return #GNUNET_OK (continue to iterate)
3460 disconnect_all_neighbours (void *cls,
3461 const struct GNUNET_PeerIdentity *key,
3464 struct NeighbourMapEntry *n = value;
3466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3467 "Disconnecting peer `%4s', %s\n",
3468 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3469 n->state = S_DISCONNECT_FINISHED;
3470 free_neighbour (n, GNUNET_NO);
3476 * Cleanup the neighbours subsystem.
3479 GST_neighbours_stop ()
3481 if (NULL == neighbours)
3483 if (GNUNET_SCHEDULER_NO_TASK != util_transmission_tk)
3485 GNUNET_SCHEDULER_cancel (util_transmission_tk);
3486 util_transmission_tk = GNUNET_SCHEDULER_NO_TASK;
3489 GNUNET_CONTAINER_multipeermap_iterate (neighbours,
3490 &disconnect_all_neighbours,
3492 GNUNET_CONTAINER_multipeermap_destroy (neighbours);
3494 callback_cls = NULL;
3495 connect_notify_cb = NULL;
3496 disconnect_notify_cb = NULL;
3497 address_change_cb = NULL;
3501 /* end of file gnunet-service-transport_neighbours.c */