2 This file is part of GNUnet.
3 (C) 2010,2011,2012 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 * - "address_change_cb" is NEVER invoked; when should we call this one exactly?
28 * - TEST, TEST, TEST...
31 #include "gnunet_ats_service.h"
32 #include "gnunet-service-transport_neighbours.h"
33 #include "gnunet-service-transport_plugins.h"
34 #include "gnunet-service-transport_validation.h"
35 #include "gnunet-service-transport_clients.h"
36 #include "gnunet-service-transport.h"
37 #include "gnunet_peerinfo_service.h"
38 #include "gnunet-service-transport_blacklist.h"
39 #include "gnunet_constants.h"
40 #include "transport.h"
44 * Size of the neighbour hash map.
46 #define NEIGHBOUR_TABLE_SIZE 256
49 * Time we give plugin to transmit DISCONNECT message before the
50 * neighbour entry self-destructs.
52 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
55 * How often must a peer violate bandwidth quotas before we start
56 * to simply drop its messages?
58 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
61 * How often do we send KEEPALIVE messages to each of our neighbours and measure
62 * the latency with this neighbour?
63 * (idle timeout is 5 minutes or 300 seconds, so with 30s interval we
64 * send 10 keepalives in each interval, so 10 messages would need to be
65 * lost in a row for a disconnect).
67 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
70 * How long are we willing to wait for a response from ATS before timing out?
72 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
75 * How long are we willing to wait for an ACK from the other peer before
76 * giving up on our connect operation?
78 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
81 * How long are we willing to wait for a successful reconnect if
82 * an existing connection went down? Much shorter than the
83 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
84 * higher layers about the disconnect during this period.
86 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
89 * How long are we willing to wait for a response from the blacklist
90 * subsystem before timing out?
92 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
95 GNUNET_NETWORK_STRUCT_BEGIN
98 * Message a peer sends to another to indicate that it intends to
99 * setup a connection/session for data exchange. A 'SESSION_CONNECT'
100 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
101 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
102 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
103 * should be connected.
105 struct SessionConnectMessage
108 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
109 * or 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK'
111 struct GNUNET_MessageHeader header;
116 uint32_t reserved GNUNET_PACKED;
119 * Absolute time at the sender. Only the most recent connect
120 * message implies which session is preferred by the sender.
122 struct GNUNET_TIME_AbsoluteNBO timestamp;
128 * Message we send to the other peer to notify him that we intentionally
129 * are disconnecting (to reduce timeouts). This is just a friendly
130 * notification, peers must not rely on always receiving disconnect
133 struct SessionDisconnectMessage
136 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
138 struct GNUNET_MessageHeader header;
143 uint32_t reserved GNUNET_PACKED;
146 * Purpose of the signature. Extends over the timestamp.
147 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
149 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
152 * Absolute time at the sender. Only the most recent connect
153 * message implies which session is preferred by the sender.
155 struct GNUNET_TIME_AbsoluteNBO timestamp;
158 * Public key of the sender.
160 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
163 * Signature of the peer that sends us the disconnect. Only
164 * valid if the timestamp is AFTER the timestamp from the
165 * corresponding 'CONNECT' message.
167 struct GNUNET_CRYPTO_RsaSignature signature;
171 GNUNET_NETWORK_STRUCT_END
175 * For each neighbour we keep a list of messages
176 * that we still want to transmit to the neighbour.
182 * This is a doubly linked list.
184 struct MessageQueue *next;
187 * This is a doubly linked list.
189 struct MessageQueue *prev;
192 * Function to call once we're done.
194 GST_NeighbourSendContinuation cont;
202 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
203 * stuck together in memory. Allocated at the end of this struct.
205 const char *message_buf;
208 * Size of the message buf
210 size_t message_buf_size;
213 * At what time should we fail?
215 struct GNUNET_TIME_Absolute timeout;
221 * Possible state of a neighbour. Initially, we are S_NOT_CONNECTED.
223 * Then, there are two main paths. If we receive a CONNECT message, we
224 * first run a check against the blacklist and ask ATS for a
225 * suggestion. (S_CONNECT_RECV_ATS). If the blacklist comes back
226 * positive, we give the address to ATS. If ATS makes a suggestion,
227 * we ALSO give that suggestion to the blacklist
228 * (S_CONNECT_RECV_BLACKLIST). Once the blacklist approves the
229 * address we got from ATS, we send our CONNECT_ACK and go to
230 * S_CONNECT_RECV_ACK. If we receive a SESSION_ACK, we go to
231 * S_CONNECTED (and notify everyone about the new connection). If the
232 * operation times out, we go to S_DISCONNECT.
234 * The other case is where we transmit a CONNECT message first. We
235 * start with S_INIT_ATS. If we get an address, we enter
236 * S_INIT_BLACKLIST and check the blacklist. If the blacklist is OK
237 * with the connection, we actually send the CONNECT message and go to
238 * state S_CONNECT_SENT. Once we receive a CONNECT_ACK, we go to
239 * S_CONNECTED (and notify everyone about the new connection and send
240 * back a SESSION_ACK). If the operation times out, we go to
243 * If the session is in trouble (i.e. transport-level disconnect or
244 * timeout), we go to S_RECONNECT_ATS where we ask ATS for a new
245 * address (we don't notify anyone about the disconnect yet). Once we
246 * have a new address, we go to S_RECONNECT_BLACKLIST to check the new
247 * address against the blacklist. If the blacklist approves, we enter
248 * S_RECONNECT_SENT and send a CONNECT message. If we receive a
249 * CONNECT_ACK, we go to S_CONNECTED and nobody noticed that we had
250 * trouble; we also send a SESSION_ACK at this time just in case. If
251 * the operation times out, we go to S_DISCONNECT (and notify everyone
252 * about the lost connection).
254 * If ATS decides to switch addresses while we have a normal
255 * connection, we go to S_CONNECTED_SWITCHING_BLACKLIST to check the
256 * new address against the blacklist. If the blacklist approves, we
257 * go to S_CONNECTED_SWITCHING_CONNECT_SENT and send a
258 * SESSION_CONNECT. If we get a SESSION_ACK back, we switch the
259 * primary connection to the suggested alternative from ATS, go back
260 * to S_CONNECTED and send a SESSION_ACK to the other peer just to be
261 * sure. If the operation times out (or the blacklist disapproves),
262 * we go to S_CONNECTED (and notify ATS that the given alternative
263 * address is "invalid").
265 * Once a session is in S_DISCONNECT, it is cleaned up and then goes
266 * to (S_DISCONNECT_FINISHED). If we receive an explicit disconnect
267 * request, we can go from any state to S_DISCONNECT, possibly after
268 * generating disconnect notifications.
270 * Note that it is quite possible that while we are in any of these
271 * states, we could receive a 'CONNECT' request from the other peer.
272 * We then enter a 'weird' state where we pursue our own primary state
273 * machine (as described above), but with the 'send_connect_ack' flag
274 * set to 1. If our state machine allows us to send a 'CONNECT_ACK'
275 * (because we have an acceptable address), we send the 'CONNECT_ACK'
276 * and set the 'send_connect_ack' to 2. If we then receive a
277 * 'SESSION_ACK', we go to 'S_CONNECTED' (and reset 'send_connect_ack'
284 * fresh peer or completely disconnected
289 * Asked to initiate connection, trying to get address from ATS
294 * Asked to initiate connection, trying to get address approved
300 * Sent CONNECT message to other peer, waiting for CONNECT_ACK
305 * Received a CONNECT, asking ATS about address suggestions.
310 * Received CONNECT from other peer, got an address, checking with blacklist.
312 S_CONNECT_RECV_BLACKLIST,
315 * CONNECT request from other peer was SESSION_ACK'ed, waiting for
321 * Got our CONNECT_ACK/SESSION_ACK, connection is up.
326 * Connection got into trouble, rest of the system still believes
327 * it to be up, but we're getting a new address from ATS.
332 * Connection got into trouble, rest of the system still believes
333 * it to be up; we are checking the new address against the blacklist.
335 S_RECONNECT_BLACKLIST,
338 * Sent CONNECT over new address (either by ATS telling us to switch
339 * addresses or from RECONNECT_ATS); if this fails, we need to tell
340 * the rest of the system about a disconnect.
345 * We have some primary connection, but ATS suggested we switch
346 * to some alternative; we're now checking the alternative against
349 S_CONNECTED_SWITCHING_BLACKLIST,
352 * We have some primary connection, but ATS suggested we switch
353 * to some alternative; we now sent a CONNECT message for the
354 * alternative session to the other peer and waiting for a
355 * CONNECT_ACK to make this our primary connection.
357 S_CONNECTED_SWITCHING_CONNECT_SENT,
360 * Disconnect in progress (we're sending the DISCONNECT message to the
361 * other peer; after that is finished, the state will be cleaned up).
366 * We're finished with the disconnect; clean up state now!
368 S_DISCONNECT_FINISHED
373 * A possible address we could use to communicate with a neighbour.
375 struct NeighbourAddress
379 * Active session for this address.
381 struct Session *session;
384 * Network-level address information.
386 struct GNUNET_HELLO_Address *address;
389 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
390 * peer for this address. Use to check that the ACK is in response
391 * to our most recent 'CONNECT'.
393 struct GNUNET_TIME_Absolute connect_timestamp;
396 * Inbound bandwidth from ATS for this address.
398 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
401 * Outbound bandwidth from ATS for this address.
403 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
406 * Did we tell ATS that this is our 'active' address?
414 * Entry in neighbours.
416 struct NeighbourMapEntry
420 * Head of list of messages we would like to send to this peer;
421 * must contain at most one message per client.
423 struct MessageQueue *messages_head;
426 * Tail of list of messages we would like to send to this peer; must
427 * contain at most one message per client.
429 struct MessageQueue *messages_tail;
432 * Are we currently trying to send a message? If so, which one?
434 struct MessageQueue *is_active;
437 * Primary address we currently use to communicate with the neighbour.
439 struct NeighbourAddress primary_address;
442 * Alternative address currently under consideration for communicating
443 * with the neighbour.
445 struct NeighbourAddress alternative_address;
448 * Identity of this neighbour.
450 struct GNUNET_PeerIdentity id;
453 * Main task that drives this peer (timeouts, keepalives, etc.).
454 * Always runs the 'master_task'.
456 GNUNET_SCHEDULER_TaskIdentifier task;
459 * At what time should we sent the next keep-alive message?
461 struct GNUNET_TIME_Absolute keep_alive_time;
464 * At what time did we sent the last keep-alive message? Used
465 * to calculate round-trip time ("latency").
467 struct GNUNET_TIME_Absolute last_keep_alive_time;
470 * Timestamp we should include in our next CONNECT_ACK message.
471 * (only valid if 'send_connect_ack' is GNUNET_YES). Used to build
472 * our CONNECT_ACK message.
474 struct GNUNET_TIME_Absolute connect_ack_timestamp;
477 * Time where we should cut the connection (timeout) if we don't
478 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
479 * if we are in S_CONNECTED).
481 struct GNUNET_TIME_Absolute timeout;
484 * Latest calculated latency value
486 struct GNUNET_TIME_Relative latency;
489 * Tracker for inbound bandwidth.
491 struct GNUNET_BANDWIDTH_Tracker in_tracker;
494 * How often has the other peer (recently) violated the inbound
495 * traffic limit? Incremented by 10 per violation, decremented by 1
496 * per non-violation (for each time interval).
498 unsigned int quota_violation_count;
501 * The current state of the peer.
506 * Did we sent an KEEP_ALIVE message and are we expecting a response?
508 int expect_latency_response;
511 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
512 * (once we have an address to use and the peer has been allowed by our
513 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
514 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
515 * a 'SESSION_ACK' (regardless of what our own state machine might say).
517 int send_connect_ack;
523 * Context for blacklist checks and the 'handle_test_blacklist_cont'
524 * function. Stores information about ongoing blacklist checks.
526 struct BlackListCheckContext
530 * We keep blacklist checks in a DLL.
532 struct BlackListCheckContext *next;
535 * We keep blacklist checks in a DLL.
537 struct BlackListCheckContext *prev;
540 * Address that is being checked.
542 struct NeighbourAddress na;
545 * ATS information about the address.
547 struct GNUNET_ATS_Information *ats;
550 * Handle to the ongoing blacklist check.
552 struct GST_BlacklistCheck *bc;
555 * Size of the 'ats' array.
563 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
565 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
568 * We keep blacklist checks in a DLL so that we can find
569 * the 'sessions' in their 'struct NeighbourAddress' if
570 * a session goes down.
572 static struct BlackListCheckContext *bc_head;
575 * We keep blacklist checks in a DLL.
577 static struct BlackListCheckContext *bc_tail;
580 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
582 static void *callback_cls;
585 * Function to call when we connected to a neighbour.
587 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
590 * Function to call when we disconnected from a neighbour.
592 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
595 * Function to call when we changed an active address of a neighbour.
597 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
600 * counter for connected neighbours
602 static unsigned int neighbours_connected;
605 * Number of bytes we have currently queued for transmission.
607 static unsigned long long bytes_in_send_queue;
611 * Lookup a neighbour entry in the neighbours hash map.
613 * @param pid identity of the peer to look up
614 * @return the entry, NULL if there is no existing record
616 static struct NeighbourMapEntry *
617 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
619 if (NULL == neighbours)
621 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
626 * Test if we're connected to the given peer.
628 * @param n neighbour entry of peer to test
629 * @return GNUNET_YES if we are connected, GNUNET_NO if not
632 test_connected (struct NeighbourMapEntry *n)
638 case S_NOT_CONNECTED:
640 case S_INIT_BLACKLIST:
642 case S_CONNECT_RECV_ATS:
643 case S_CONNECT_RECV_BLACKLIST:
644 case S_CONNECT_RECV_ACK:
647 case S_RECONNECT_ATS:
648 case S_RECONNECT_BLACKLIST:
649 case S_RECONNECT_SENT:
650 case S_CONNECTED_SWITCHING_BLACKLIST:
651 case S_CONNECTED_SWITCHING_CONNECT_SENT:
654 case S_DISCONNECT_FINISHED:
660 return GNUNET_SYSERR;
665 * Send information about a new outbound quota to our clients.
667 * @param target affected peer
668 * @param quota new quota
671 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
672 struct GNUNET_BANDWIDTH_Value32NBO quota)
674 struct QuotaSetMessage q_msg;
676 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
677 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
678 ntohl (quota.value__), GNUNET_i2s (target));
679 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
680 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
682 q_msg.peer = (*target);
683 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
688 * We don't need a given neighbour address any more.
689 * Release its resources and give appropriate notifications
690 * to ATS and other subsystems.
692 * @param na address we are done with; 'na' itself must NOT be 'free'd, only the contents!
695 free_address (struct NeighbourAddress *na)
697 if (GNUNET_YES == na->ats_active)
699 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
700 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
702 na->ats_active = GNUNET_NO;
703 if (NULL != na->address)
705 GNUNET_HELLO_address_free (na->address);
713 * Initialize the 'struct NeighbourAddress'.
715 * @param na neighbour address to initialize
716 * @param address address of the other peer, NULL if other peer
718 * @param session session to use (or NULL, in which case an
719 * address must be setup)
720 * @param bandwidth_in inbound quota to be used when connection is up
721 * @param bandwidth_out outbound quota to be used when connection is up
722 * @param is_active GNUNET_YES to mark this as the active address with ATS
725 set_address (struct NeighbourAddress *na,
726 const struct GNUNET_HELLO_Address *address,
727 struct Session *session,
728 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
729 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
732 struct GNUNET_TRANSPORT_PluginFunctions *papi;
734 if (NULL == (papi = GST_plugins_find (address->transport_name)))
739 if (session == na->session)
741 na->bandwidth_in = bandwidth_in;
742 na->bandwidth_out = bandwidth_out;
743 if (is_active != na->ats_active)
745 na->ats_active = is_active;
746 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
747 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
749 if (GNUNET_YES == is_active)
751 /* FIXME: is this the right place to set quotas? */
752 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
753 send_outbound_quota (&address->peer, bandwidth_out);
759 session = papi->get_session (papi->cls, address);
762 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
763 "Failed to obtain new session for peer `%s' and address '%s'\n",
764 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
765 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
768 na->address = GNUNET_HELLO_address_copy (address);
769 na->bandwidth_in = bandwidth_in;
770 na->bandwidth_out = bandwidth_out;
771 na->session = session;
772 na->ats_active = is_active;
773 if (GNUNET_YES == is_active)
775 /* Telling ATS about new session */
776 GNUNET_ATS_address_update (GST_ats, na->address, na->session, NULL, 0);
777 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
778 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
780 /* FIXME: is this the right place to set quotas? */
781 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
782 send_outbound_quota (&address->peer, bandwidth_out);
788 * Free a neighbour map entry.
790 * @param n entry to free
793 free_neighbour (struct NeighbourMapEntry *n)
795 struct MessageQueue *mq;
796 struct GNUNET_TRANSPORT_PluginFunctions *papi;
798 n->is_active = NULL; /* always free'd by its own continuation! */
800 /* fail messages currently in the queue */
801 while (NULL != (mq = n->messages_head))
803 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
804 if (NULL != mq->cont)
805 mq->cont (mq->cont_cls, GNUNET_SYSERR);
808 /* It is too late to send other peer disconnect notifications, but at
809 least internally we need to get clean... */
810 if (GNUNET_YES == test_connected (n))
812 GNUNET_STATISTICS_set (GST_stats,
813 gettext_noop ("# peers connected"),
814 --neighbours_connected,
816 disconnect_notify_cb (callback_cls, &n->id);
819 /* FIXME-PLUGIN-API: This does not seem to guarantee that all
820 transport sessions eventually get killed due to inactivity; they
821 MUST have their own timeout logic (but at least TCP doesn't have
822 one yet). Are we sure that EVERY 'session' of a plugin is
823 actually cleaned up this way!? Note that if we are switching
824 between two TCP sessions to the same peer, the existing plugin
825 API gives us not even the means to selectively kill only one of
826 them! Killing all sessions like this seems to be very, very
828 if ( (NULL != n->primary_address.address) &&
829 (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name))) )
830 papi->disconnect (papi->cls, &n->id);
832 n->state = S_DISCONNECT_FINISHED;
834 GNUNET_assert (GNUNET_YES ==
835 GNUNET_CONTAINER_multihashmap_remove (neighbours,
836 &n->id.hashPubKey, n));
838 /* cut transport-level connection */
839 free_address (&n->primary_address);
840 free_address (&n->alternative_address);
842 // FIXME-ATS-API: we might want to be more specific about
843 // which states we do this from in the future (ATS should
844 // have given us a 'suggest_address' handle, and if we have
845 // such a handle, we should cancel the operation here!
846 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
848 if (GNUNET_SCHEDULER_NO_TASK != n->task)
850 GNUNET_SCHEDULER_cancel (n->task);
851 n->task = GNUNET_SCHEDULER_NO_TASK;
853 /* free rest of memory */
859 * Transmit a message using the current session of the given
862 * @param n entry for the recipient
863 * @param msgbuf buffer to transmit
864 * @param msgbuf_size number of bytes in buffer
865 * @param priority transmission priority
866 * @param timeout transmission timeout
867 * @param cont continuation to call when finished (can be NULL)
868 * @param cont_cls closure for cont
871 send_with_session (struct NeighbourMapEntry *n,
872 const char *msgbuf, size_t msgbuf_size,
874 struct GNUNET_TIME_Relative timeout,
875 GNUNET_TRANSPORT_TransmitContinuation cont,
878 struct GNUNET_TRANSPORT_PluginFunctions *papi;
880 GNUNET_assert (n->primary_address.session != NULL);
881 if ( ( (NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name))) ||
882 (-1 == papi->send (papi->cls,
883 n->primary_address.session,
889 cont (cont_cls, &n->id, GNUNET_SYSERR);
890 GNUNET_break (NULL != papi);
895 * Master task run for every neighbour. Performs all of the time-related
896 * activities (keep alive, send next message, disconnect if idle, finish
897 * clean up after disconnect).
899 * @param cls the 'struct NeighbourMapEntry' for which we are running
900 * @param tc scheduler context (unused)
903 master_task (void *cls,
904 const struct GNUNET_SCHEDULER_TaskContext *tc);
908 * Function called when the 'DISCONNECT' message has been sent by the
909 * plugin. Frees the neighbour --- if the entry still exists.
912 * @param target identity of the neighbour that was disconnected
913 * @param result GNUNET_OK if the disconnect got out successfully
916 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
919 struct NeighbourMapEntry *n;
921 n = lookup_neighbour (target);
923 return; /* already gone */
924 if (S_DISCONNECT != n->state)
925 return; /* have created a fresh entry since */
926 n->state = S_DISCONNECT;
927 GNUNET_SCHEDULER_cancel (n->task);
928 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
933 * Transmit a DISCONNECT message to the other peer.
935 * @param n neighbour to send DISCONNECT message.
938 send_disconnect (struct NeighbourMapEntry *n)
940 struct SessionDisconnectMessage disconnect_msg;
942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
943 "Sending DISCONNECT message to peer `%4s'\n",
944 GNUNET_i2s (&n->id));
945 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
946 disconnect_msg.header.type =
947 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
948 disconnect_msg.reserved = htonl (0);
949 disconnect_msg.purpose.size =
950 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
951 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
952 sizeof (struct GNUNET_TIME_AbsoluteNBO));
953 disconnect_msg.purpose.purpose =
954 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
955 disconnect_msg.timestamp =
956 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
957 disconnect_msg.public_key = GST_my_public_key;
958 GNUNET_assert (GNUNET_OK ==
959 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
960 &disconnect_msg.purpose,
961 &disconnect_msg.signature));
963 send_with_session (n,
964 (const char *) &disconnect_msg, sizeof (disconnect_msg),
965 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
966 &send_disconnect_cont, NULL);
967 GNUNET_STATISTICS_update (GST_stats,
969 ("# DISCONNECT messages sent"), 1,
975 * Disconnect from the given neighbour, clean up the record.
977 * @param n neighbour to disconnect from
980 disconnect_neighbour (struct NeighbourMapEntry *n)
982 /* depending on state, notify neighbour and/or upper layers of this peer
986 case S_NOT_CONNECTED:
988 case S_INIT_BLACKLIST:
989 /* other peer is completely unaware of us, no need to send DISCONNECT */
990 n->state = S_DISCONNECT_FINISHED;
995 n->state = S_DISCONNECT;
997 case S_CONNECT_RECV_ATS:
998 case S_CONNECT_RECV_BLACKLIST:
999 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1000 n->state = S_DISCONNECT_FINISHED;
1003 case S_CONNECT_RECV_ACK:
1004 /* we DID ACK the other peer's request, must send DISCONNECT */
1005 send_disconnect (n);
1006 n->state = S_DISCONNECT;
1009 case S_RECONNECT_BLACKLIST:
1010 case S_RECONNECT_SENT:
1011 case S_CONNECTED_SWITCHING_BLACKLIST:
1012 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1013 /* we are currently connected, need to send disconnect and do
1014 internal notifications and update statistics */
1015 send_disconnect (n);
1016 GNUNET_STATISTICS_set (GST_stats,
1017 gettext_noop ("# peers connected"),
1018 --neighbours_connected,
1020 disconnect_notify_cb (callback_cls, &n->id);
1021 n->state = S_DISCONNECT;
1023 case S_RECONNECT_ATS:
1024 /* ATS address request timeout, disconnect without sending disconnect message */
1025 GNUNET_STATISTICS_set (GST_stats,
1026 gettext_noop ("# peers connected"),
1027 --neighbours_connected,
1029 disconnect_notify_cb (callback_cls, &n->id);
1030 n->state = S_DISCONNECT;
1033 /* already disconnected, ignore */
1035 case S_DISCONNECT_FINISHED:
1036 /* already cleaned up, how did we get here!? */
1043 /* schedule timeout to clean up */
1044 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1045 GNUNET_SCHEDULER_cancel (n->task);
1046 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1052 * We're done with our transmission attempt, continue processing.
1054 * @param cls the 'struct MessageQueue' of the message
1055 * @param receiver intended receiver
1056 * @param success whether it worked or not
1059 transmit_send_continuation (void *cls,
1060 const struct GNUNET_PeerIdentity *receiver,
1063 struct MessageQueue *mq = cls;
1064 struct NeighbourMapEntry *n;
1066 n = lookup_neighbour (receiver);
1073 if (n->is_active == mq)
1075 /* this is still "our" neighbour, remove us from its queue
1076 and allow it to send the next message now */
1077 n->is_active = NULL;
1078 GNUNET_SCHEDULER_cancel (n->task);
1079 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1081 GNUNET_assert (bytes_in_send_queue >= mq->message_buf_size);
1082 bytes_in_send_queue -= mq->message_buf_size;
1083 GNUNET_STATISTICS_set (GST_stats,
1085 ("# bytes in message queue for other peers"),
1086 bytes_in_send_queue, GNUNET_NO);
1087 if (GNUNET_OK == success)
1088 GNUNET_STATISTICS_update (GST_stats,
1090 ("# messages transmitted to other peers"),
1093 GNUNET_STATISTICS_update (GST_stats,
1095 ("# transmission failures for messages to other peers"),
1097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1098 "Sending message to `%s' of type %u was a %s\n",
1099 GNUNET_i2s (receiver),
1100 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1101 (success == GNUNET_OK) ? "success" : "FAILURE");
1102 if (NULL != mq->cont)
1103 mq->cont (mq->cont_cls, success);
1109 * Check the message list for the given neighbour and if we can
1110 * send a message, do so. This function should only be called
1111 * if the connection is at least generally ready for transmission.
1112 * While we will only send one message at a time, no bandwidth
1113 * quota management is performed here. If a message was given to
1114 * the plugin, the continuation will automatically re-schedule
1115 * the 'master' task once the next message might be transmitted.
1117 * @param n target peer for which to transmit
1120 try_transmission_to_peer (struct NeighbourMapEntry *n)
1122 struct MessageQueue *mq;
1123 struct GNUNET_TIME_Relative timeout;
1125 if (NULL == n->primary_address.address)
1127 /* no address, why are we here? */
1131 if ((0 == n->primary_address.address->address_length) &&
1132 (NULL == n->primary_address.session))
1134 /* no address, why are we here? */
1138 if (NULL != n->is_active)
1140 /* transmission already pending */
1144 /* timeout messages from the queue that are past their due date */
1145 while (NULL != (mq = n->messages_head))
1147 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1148 if (timeout.rel_value > 0)
1150 GNUNET_STATISTICS_update (GST_stats,
1152 ("# messages timed out while in transport queue"),
1154 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1156 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
1159 return; /* no more messages */
1160 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1162 send_with_session (n,
1163 mq->message_buf, mq->message_buf_size,
1164 0 /* priority */, timeout,
1165 &transmit_send_continuation, mq);
1170 * Send keepalive message to the neighbour. Must only be called
1171 * if we are on 'connected' state. Will internally determine
1172 * if a keepalive is truly needed (so can always be called).
1174 * @param n neighbour that went idle and needs a keepalive
1177 send_keepalive (struct NeighbourMapEntry *n)
1179 struct GNUNET_MessageHeader m;
1181 GNUNET_assert (S_CONNECTED == n->state);
1182 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value > 0)
1183 return; /* no keepalive needed at this time */
1184 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1185 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1186 send_with_session (n,
1187 (const void *) &m, sizeof (m),
1188 UINT32_MAX /* priority */,
1189 KEEPALIVE_FREQUENCY,
1191 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1193 n->expect_latency_response = GNUNET_YES;
1194 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1195 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1200 * Keep the connection to the given neighbour alive longer,
1201 * we received a KEEPALIVE (or equivalent); send a response.
1203 * @param neighbour neighbour to keep alive (by sending keep alive response)
1206 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1208 struct NeighbourMapEntry *n;
1209 struct GNUNET_MessageHeader m;
1211 if (NULL == (n = lookup_neighbour (neighbour)))
1213 GNUNET_STATISTICS_update (GST_stats,
1215 ("# KEEPALIVE messages discarded (peer unknown)"),
1219 if (NULL == n->primary_address.session)
1221 GNUNET_STATISTICS_update (GST_stats,
1223 ("# KEEPALIVE messages discarded (no session)"),
1227 /* send reply to allow neighbour to measure latency */
1228 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1229 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1230 send_with_session(n,
1231 (const void *) &m, sizeof (m),
1232 UINT32_MAX /* priority */,
1233 KEEPALIVE_FREQUENCY,
1239 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1240 * latency to this peer. Pass the updated information (existing ats
1241 * plus calculated latency) to ATS.
1243 * @param neighbour neighbour to keep alive
1244 * @param ats performance data
1245 * @param ats_count number of entries in ats
1248 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1249 const struct GNUNET_ATS_Information *ats,
1252 struct NeighbourMapEntry *n;
1254 struct GNUNET_ATS_Information ats_new[ats_count + 1];
1256 if (NULL == (n = lookup_neighbour (neighbour)))
1258 GNUNET_STATISTICS_update (GST_stats,
1260 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1264 if ( (S_CONNECTED != n->state) ||
1265 (GNUNET_YES != n->expect_latency_response) )
1267 GNUNET_STATISTICS_update (GST_stats,
1269 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1273 n->expect_latency_response = GNUNET_NO;
1274 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1275 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1277 "Latency for peer `%s' is %llu ms\n",
1278 GNUNET_i2s (&n->id), n->latency.rel_value);
1279 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1280 /* append latency */
1281 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1282 if (n->latency.rel_value > UINT32_MAX)
1283 latency = UINT32_MAX;
1285 latency = n->latency.rel_value;
1286 ats_new[ats_count].value = htonl (latency);
1287 GNUNET_ATS_address_update (GST_ats,
1288 n->primary_address.address,
1289 n->primary_address.session, ats_new,
1295 * We have received a message from the given sender. How long should
1296 * we delay before receiving more? (Also used to keep the peer marked
1299 * @param sender sender of the message
1300 * @param size size of the message
1301 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1302 * GNUNET_NO if the neighbour is not connected or violates the quota,
1303 * GNUNET_SYSERR if the connection is not fully up yet
1304 * @return how long to wait before reading more from this sender
1306 struct GNUNET_TIME_Relative
1307 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1308 *sender, ssize_t size, int *do_forward)
1310 struct NeighbourMapEntry *n;
1311 struct GNUNET_TIME_Relative ret;
1313 if (NULL == neighbours)
1315 *do_forward = GNUNET_NO;
1316 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1318 if (NULL == (n = lookup_neighbour (sender)))
1320 GST_neighbours_try_connect (sender);
1321 if (NULL == (n = lookup_neighbour (sender)))
1323 GNUNET_STATISTICS_update (GST_stats,
1325 ("# messages discarded due to lack of neighbour record"),
1327 *do_forward = GNUNET_NO;
1328 return GNUNET_TIME_UNIT_ZERO;
1331 if (! test_connected (n))
1333 *do_forward = GNUNET_SYSERR;
1334 return GNUNET_TIME_UNIT_ZERO;
1336 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1338 n->quota_violation_count++;
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1340 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1341 n->in_tracker.available_bytes_per_s__,
1342 n->quota_violation_count);
1343 /* Discount 32k per violation */
1344 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1348 if (n->quota_violation_count > 0)
1350 /* try to add 32k back */
1351 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1352 n->quota_violation_count--;
1355 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1357 GNUNET_STATISTICS_update (GST_stats,
1359 ("# bandwidth quota violations by other peers"),
1361 *do_forward = GNUNET_NO;
1362 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1364 *do_forward = GNUNET_YES;
1365 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1366 if (ret.rel_value > 0)
1368 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1369 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1370 (unsigned long long) n->in_tracker.
1371 consumption_since_last_update__,
1372 (unsigned int) n->in_tracker.available_bytes_per_s__,
1373 (unsigned long long) ret.rel_value);
1374 GNUNET_STATISTICS_update (GST_stats,
1375 gettext_noop ("# ms throttling suggested"),
1376 (int64_t) ret.rel_value, GNUNET_NO);
1383 * Transmit a message to the given target using the active connection.
1385 * @param target destination
1386 * @param msg message to send
1387 * @param msg_size number of bytes in msg
1388 * @param timeout when to fail with timeout
1389 * @param cont function to call when done
1390 * @param cont_cls closure for 'cont'
1393 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1394 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1395 GST_NeighbourSendContinuation cont, void *cont_cls)
1397 struct NeighbourMapEntry *n;
1398 struct MessageQueue *mq;
1400 /* All ove these cases should never happen; they are all API violations.
1401 But we check anyway, just to be sure. */
1402 if (NULL == (n = lookup_neighbour (target)))
1406 cont (cont_cls, GNUNET_SYSERR);
1409 if (GNUNET_YES != test_connected (n))
1413 cont (cont_cls, GNUNET_SYSERR);
1416 bytes_in_send_queue += msg_size;
1417 GNUNET_STATISTICS_set (GST_stats,
1419 ("# bytes in message queue for other peers"),
1420 bytes_in_send_queue, GNUNET_NO);
1421 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1423 mq->cont_cls = cont_cls;
1424 memcpy (&mq[1], msg, msg_size);
1425 mq->message_buf = (const char *) &mq[1];
1426 mq->message_buf_size = msg_size;
1427 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1428 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1429 if ( (NULL != n->is_active) ||
1430 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1432 GNUNET_SCHEDULER_cancel (n->task);
1433 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1438 * Send a SESSION_CONNECT message via the given address.
1440 * @param na address to use
1443 send_session_connect (struct NeighbourAddress *na)
1445 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1446 struct SessionConnectMessage connect_msg;
1448 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1453 if (NULL == na->session)
1454 na->session = papi->get_session (papi->cls, na->address);
1455 if (NULL == na->session)
1460 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1461 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1462 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1463 connect_msg.reserved = htonl (0);
1464 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1465 (void) papi->send (papi->cls,
1467 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1469 GNUNET_TIME_UNIT_FOREVER_REL,
1475 * Send a SESSION_CONNECT_ACK message via the given address.
1477 * @param address address to use
1478 * @param session session to use
1479 * @param timestamp timestamp to use for the ACK message
1482 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1483 struct Session *session,
1484 struct GNUNET_TIME_Absolute timestamp)
1486 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1487 struct SessionConnectMessage connect_msg;
1489 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1494 if (NULL == session)
1495 session = papi->get_session (papi->cls, address);
1496 if (NULL == session)
1501 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1502 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1503 connect_msg.reserved = htonl (0);
1504 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1505 (void) papi->send (papi->cls,
1507 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1509 GNUNET_TIME_UNIT_FOREVER_REL,
1515 * Create a fresh entry in the neighbour map for the given peer
1517 * @param peer peer to create an entry for
1518 * @return new neighbour map entry
1520 static struct NeighbourMapEntry *
1521 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1523 struct NeighbourMapEntry *n;
1525 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1526 "Creating new neighbour entry for `%s'\n",
1528 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1530 n->state = S_NOT_CONNECTED;
1531 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1532 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1533 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1534 MAX_BANDWIDTH_CARRY_S);
1535 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1536 GNUNET_assert (GNUNET_OK ==
1537 GNUNET_CONTAINER_multihashmap_put (neighbours,
1538 &n->id.hashPubKey, n,
1539 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1545 * Check if the two given addresses are the same.
1546 * Actually only checks if the sessions are non-NULL
1547 * (which they should be) and then if they are identical;
1548 * the actual addresses don't matter if the session
1549 * pointers match anyway, and we must have session pointers
1552 * @param a1 first address to compare
1553 * @param a2 other address to compare
1554 * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
1557 address_matches (const struct NeighbourAddress *a1,
1558 const struct NeighbourAddress *a2)
1560 if ( (NULL == a1->session) ||
1561 (NULL == a2->session) )
1566 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1571 * Try to create a connection to the given target (eventually).
1573 * @param target peer to try to connect to
1576 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1578 struct NeighbourMapEntry *n;
1580 if (NULL == neighbours)
1581 return; /* during shutdown, do nothing */
1582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1583 "Asked to connect to peer `%s'\n",
1584 GNUNET_i2s (target));
1586 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1588 /* refuse to connect to myself */
1589 /* FIXME: can this happen? Is this not an API violation? */
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "Refusing to try to connect to myself.\n");
1594 n = lookup_neighbour (target);
1599 case S_NOT_CONNECTED:
1600 /* this should not be possible */
1605 case S_INIT_BLACKLIST:
1606 case S_CONNECT_SENT:
1607 case S_CONNECT_RECV_ATS:
1608 case S_CONNECT_RECV_BLACKLIST:
1609 case S_CONNECT_RECV_ACK:
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611 "Ignoring request to try to connect to `%s', already trying!\n",
1612 GNUNET_i2s (target));
1613 return; /* already trying */
1615 case S_RECONNECT_ATS:
1616 case S_RECONNECT_BLACKLIST:
1617 case S_RECONNECT_SENT:
1618 case S_CONNECTED_SWITCHING_BLACKLIST:
1619 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1620 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1621 "Ignoring request to try to connect, already connected to `%s'!\n",
1622 GNUNET_i2s (target));
1623 return; /* already connected */
1625 /* get rid of remains, ready to re-try immediately */
1628 case S_DISCONNECT_FINISHED:
1629 /* should not be possible */
1637 n = setup_neighbour (target);
1638 n->state = S_INIT_ATS;
1639 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1640 GNUNET_ATS_suggest_address (GST_ats, target);
1645 * Function called with the result of a blacklist check.
1647 * @param cls closure with the 'struct BlackListCheckContext'
1648 * @param peer peer this check affects
1649 * @param result GNUNET_OK if the address is allowed
1652 handle_test_blacklist_cont (void *cls,
1653 const struct GNUNET_PeerIdentity *peer,
1656 struct BlackListCheckContext *bcc = cls;
1657 struct NeighbourMapEntry *n;
1660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1661 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1663 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1664 if (GNUNET_OK == result)
1666 /* valid new address, let ATS know! */
1667 GNUNET_ATS_address_update (GST_ats,
1670 bcc->ats, bcc->ats_count);
1672 if (NULL == (n = lookup_neighbour (peer)))
1673 goto cleanup; /* nobody left to care about new address */
1676 case S_NOT_CONNECTED:
1677 /* this should not be possible */
1682 /* still waiting on ATS suggestion */
1684 case S_INIT_BLACKLIST:
1685 /* check if the address the blacklist was fine with matches
1686 ATS suggestion, if so, we can move on! */
1687 if ( (GNUNET_OK == result) &&
1688 (1 == n->send_connect_ack) )
1690 n->send_connect_ack = 2;
1691 send_session_connect_ack_message (bcc->na.address,
1693 n->connect_ack_timestamp);
1695 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1696 break; /* result for an address we currently don't care about */
1697 if (GNUNET_OK == result)
1699 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1700 n->state = S_CONNECT_SENT;
1701 send_session_connect (&n->primary_address);
1705 GNUNET_ATS_address_destroyed (GST_ats,
1708 n->state = S_INIT_ATS;
1709 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1710 // FIXME: do we need to ask ATS again for suggestions?
1711 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1714 case S_CONNECT_SENT:
1715 /* waiting on CONNECT_ACK, send ACK if one is pending */
1716 if ( (GNUNET_OK == result) &&
1717 (1 == n->send_connect_ack) )
1719 n->send_connect_ack = 2;
1720 send_session_connect_ack_message (n->primary_address.address,
1721 n->primary_address.session,
1722 n->connect_ack_timestamp);
1725 case S_CONNECT_RECV_ATS:
1726 /* still waiting on ATS suggestion, don't care about blacklist */
1728 case S_CONNECT_RECV_BLACKLIST:
1729 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1730 break; /* result for an address we currently don't care about */
1731 if (GNUNET_OK == result)
1733 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1734 n->state = S_CONNECT_RECV_ACK;
1735 send_session_connect_ack_message (bcc->na.address,
1737 n->connect_ack_timestamp);
1738 if (1 == n->send_connect_ack)
1739 n->send_connect_ack = 2;
1743 GNUNET_ATS_address_destroyed (GST_ats,
1746 n->state = S_INIT_ATS;
1747 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1748 // FIXME: do we need to ask ATS again for suggestions?
1749 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1752 case S_CONNECT_RECV_ACK:
1753 /* waiting on SESSION_ACK, send ACK if one is pending */
1754 if ( (GNUNET_OK == result) &&
1755 (1 == n->send_connect_ack) )
1757 n->send_connect_ack = 2;
1758 send_session_connect_ack_message (n->primary_address.address,
1759 n->primary_address.session,
1760 n->connect_ack_timestamp);
1764 /* already connected, don't care about blacklist */
1766 case S_RECONNECT_ATS:
1767 /* still waiting on ATS suggestion, don't care about blacklist */
1769 case S_RECONNECT_BLACKLIST:
1770 if ( (GNUNET_OK == result) &&
1771 (1 == n->send_connect_ack) )
1773 n->send_connect_ack = 2;
1774 send_session_connect_ack_message (bcc->na.address,
1776 n->connect_ack_timestamp);
1778 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1779 break; /* result for an address we currently don't care about */
1780 if (GNUNET_OK == result)
1782 send_session_connect (&n->primary_address);
1783 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1784 n->state = S_RECONNECT_SENT;
1788 GNUNET_ATS_address_destroyed (GST_ats,
1791 n->state = S_RECONNECT_ATS;
1792 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1793 // FIXME: do we need to ask ATS again for suggestions?
1794 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1797 case S_RECONNECT_SENT:
1798 /* waiting on CONNECT_ACK, don't care about blacklist */
1799 if ( (GNUNET_OK == result) &&
1800 (1 == n->send_connect_ack) )
1802 n->send_connect_ack = 2;
1803 send_session_connect_ack_message (n->primary_address.address,
1804 n->primary_address.session,
1805 n->connect_ack_timestamp);
1808 case S_CONNECTED_SWITCHING_BLACKLIST:
1809 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1810 break; /* result for an address we currently don't care about */
1811 if (GNUNET_OK == result)
1813 send_session_connect (&n->alternative_address);
1814 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1818 GNUNET_ATS_address_destroyed (GST_ats,
1821 free_address (&n->alternative_address);
1822 n->state = S_CONNECTED;
1825 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1826 /* waiting on CONNECT_ACK, don't care about blacklist */
1827 if ( (GNUNET_OK == result) &&
1828 (1 == n->send_connect_ack) )
1830 n->send_connect_ack = 2;
1831 send_session_connect_ack_message (n->primary_address.address,
1832 n->primary_address.session,
1833 n->connect_ack_timestamp);
1837 /* Nothing to do here, ATS will already do what can be done */
1839 case S_DISCONNECT_FINISHED:
1840 /* should not be possible */
1849 GNUNET_HELLO_address_free (bcc->na.address);
1850 GNUNET_CONTAINER_DLL_remove (bc_head,
1858 * We want to know if connecting to a particular peer via
1859 * a particular address is allowed. Check it!
1861 * @param peer identity of the peer to switch the address for
1862 * @param ts time at which the check was initiated
1863 * @param address address of the other peer, NULL if other peer
1865 * @param session session to use (or NULL)
1866 * @param ats performance data
1867 * @param ats_count number of entries in ats (excluding 0-termination)
1870 check_blacklist (const struct GNUNET_PeerIdentity *peer,
1871 struct GNUNET_TIME_Absolute ts,
1872 const struct GNUNET_HELLO_Address *address,
1873 struct Session *session,
1874 const struct GNUNET_ATS_Information *ats,
1877 struct BlackListCheckContext *bcc;
1878 struct GST_BlacklistCheck *bc;
1881 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
1882 sizeof (struct GNUNET_ATS_Information) * ats_count);
1883 bcc->ats_count = ats_count;
1884 bcc->na.address = GNUNET_HELLO_address_copy (address);
1885 bcc->na.session = session;
1886 bcc->na.connect_timestamp = ts;
1887 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
1888 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1889 GNUNET_CONTAINER_DLL_insert (bc_head,
1892 if (NULL != (bc = GST_blacklist_test_allowed (peer,
1893 address->transport_name,
1894 &handle_test_blacklist_cont, bcc)))
1896 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
1897 we must only store 'bc' if 'bc' is non-NULL... */
1902 * We received a 'SESSION_CONNECT' message from the other peer.
1903 * Consider switching to it.
1905 * @param message possibly a 'struct SessionConnectMessage' (check format)
1906 * @param peer identity of the peer to switch the address for
1907 * @param address address of the other peer, NULL if other peer
1909 * @param session session to use (or NULL)
1910 * @param ats performance data
1911 * @param ats_count number of entries in ats (excluding 0-termination)
1914 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
1915 const struct GNUNET_PeerIdentity *peer,
1916 const struct GNUNET_HELLO_Address *address,
1917 struct Session *session,
1918 const struct GNUNET_ATS_Information *ats,
1921 const struct SessionConnectMessage *scm;
1922 struct NeighbourMapEntry *n;
1923 struct GNUNET_TIME_Absolute ts;
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 "Received CONNECT message from peer `%s'\n",
1928 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1930 GNUNET_break_op (0);
1933 if (NULL == neighbours)
1934 return; /* we're shutting down */
1935 scm = (const struct SessionConnectMessage *) message;
1936 GNUNET_break_op (0 == ntohl (scm->reserved));
1937 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
1938 n = lookup_neighbour (peer);
1940 n = setup_neighbour (peer);
1941 n->send_connect_ack = 1;
1942 n->connect_ack_timestamp = ts;
1945 case S_NOT_CONNECTED:
1946 n->state = S_CONNECT_RECV_ATS;
1947 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1948 GNUNET_ATS_suggest_address (GST_ats, peer);
1949 check_blacklist (peer, ts, address, session, ats, ats_count);
1952 case S_INIT_BLACKLIST:
1953 case S_CONNECT_SENT:
1954 case S_CONNECT_RECV_ATS:
1955 case S_CONNECT_RECV_BLACKLIST:
1956 case S_CONNECT_RECV_ACK:
1957 /* It can never hurt to have an alternative address in the above cases,
1958 see if it is allowed */
1959 check_blacklist (peer, ts, address, session, ats, ats_count);
1962 /* we are already connected and can thus send the ACK immediately;
1963 still, it can never hurt to have an alternative address, so also
1964 tell ATS about it */
1965 GNUNET_assert (NULL != n->primary_address.address);
1966 GNUNET_assert (NULL != n->primary_address.session);
1967 n->send_connect_ack = 0;
1968 send_session_connect_ack_message (n->primary_address.address,
1969 n->primary_address.session, ts);
1970 check_blacklist (peer, ts, address, session, ats, ats_count);
1972 case S_RECONNECT_ATS:
1973 case S_RECONNECT_BLACKLIST:
1974 case S_RECONNECT_SENT:
1975 /* It can never hurt to have an alternative address in the above cases,
1976 see if it is allowed */
1977 check_blacklist (peer, ts, address, session, ats, ats_count);
1979 case S_CONNECTED_SWITCHING_BLACKLIST:
1980 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1981 /* we are already connected and can thus send the ACK immediately;
1982 still, it can never hurt to have an alternative address, so also
1983 tell ATS about it */
1984 GNUNET_assert (NULL != n->primary_address.address);
1985 GNUNET_assert (NULL != n->primary_address.session);
1986 n->send_connect_ack = 0;
1987 send_session_connect_ack_message (n->primary_address.address,
1988 n->primary_address.session, ts);
1989 check_blacklist (peer, ts, address, session, ats, ats_count);
1992 /* get rid of remains, ready to re-try */
1994 n = setup_neighbour (peer);
1995 n->state = S_CONNECT_RECV_ATS;
1996 GNUNET_ATS_suggest_address (GST_ats, peer);
1997 check_blacklist (peer, ts, address, session, ats, ats_count);
1999 case S_DISCONNECT_FINISHED:
2000 /* should not be possible */
2012 * For an existing neighbour record, set the active connection to
2013 * use the given address.
2015 * @param peer identity of the peer to switch the address for
2016 * @param address address of the other peer, NULL if other peer
2018 * @param session session to use (or NULL)
2019 * @param ats performance data
2020 * @param ats_count number of entries in ats
2021 * @param bandwidth_in inbound quota to be used when connection is up
2022 * @param bandwidth_out outbound quota to be used when connection is up
2025 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2026 const struct GNUNET_HELLO_Address *address,
2027 struct Session *session,
2028 const struct GNUNET_ATS_Information *ats,
2030 struct GNUNET_BANDWIDTH_Value32NBO
2032 struct GNUNET_BANDWIDTH_Value32NBO
2035 struct NeighbourMapEntry *n;
2036 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2038 GNUNET_assert (address->transport_name != NULL);
2039 if (NULL == (n = lookup_neighbour (peer)))
2042 /* Obtain an session for this address from plugin */
2043 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2045 /* we don't have the plugin for this address */
2046 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2049 if ((NULL == session) && (0 == address->address_length))
2052 if (strlen (address->transport_name) > 0)
2053 GNUNET_ATS_address_destroyed (GST_ats, address, session);
2056 if (NULL == session)
2057 session = papi->get_session (papi->cls, address);
2058 if (NULL == session)
2060 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2061 "Failed to obtain new session for peer `%s' and address '%s'\n",
2062 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2063 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2066 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2067 "ATS tells us to switch to address '%s' for peer `%s'\n",
2068 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2072 case S_NOT_CONNECTED:
2077 set_address (&n->primary_address,
2078 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2079 n->state = S_INIT_BLACKLIST;
2080 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2081 check_blacklist (&n->id,
2082 n->connect_ack_timestamp,
2083 address, session, ats, ats_count);
2085 case S_INIT_BLACKLIST:
2086 /* ATS suggests a different address, switch again */
2087 set_address (&n->primary_address,
2088 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2089 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2090 check_blacklist (&n->id,
2091 n->connect_ack_timestamp,
2092 address, session, ats, ats_count);
2094 case S_CONNECT_SENT:
2095 /* ATS suggests a different address, switch again */
2096 set_address (&n->primary_address,
2097 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2098 n->state = S_INIT_BLACKLIST;
2099 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2100 check_blacklist (&n->id,
2101 n->connect_ack_timestamp,
2102 address, session, ats, ats_count);
2104 case S_CONNECT_RECV_ATS:
2105 set_address (&n->primary_address,
2106 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2107 n->state = S_CONNECT_RECV_BLACKLIST;
2108 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2109 check_blacklist (&n->id,
2110 n->connect_ack_timestamp,
2111 address, session, ats, ats_count);
2113 case S_CONNECT_RECV_BLACKLIST:
2114 /* ATS asks us to switch while we were trying to connect; switch to new
2115 address and check blacklist again */
2116 set_address (&n->primary_address,
2117 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2118 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2119 check_blacklist (&n->id,
2120 n->connect_ack_timestamp,
2121 address, session, ats, ats_count);
2124 GNUNET_assert (NULL != n->primary_address.address);
2125 GNUNET_assert (NULL != n->primary_address.session);
2126 if (n->primary_address.session == session)
2128 /* not an address change, just a quota change */
2129 set_address (&n->primary_address,
2130 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2133 /* ATS asks us to switch a life connection; see if we can get
2134 a CONNECT_ACK on it before we actually do this! */
2135 set_address (&n->alternative_address,
2136 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2137 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2138 check_blacklist (&n->id,
2139 GNUNET_TIME_absolute_get (),
2140 address, session, ats, ats_count);
2142 case S_RECONNECT_ATS:
2143 set_address (&n->primary_address,
2144 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2145 n->state = S_RECONNECT_BLACKLIST;
2146 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2147 check_blacklist (&n->id,
2148 n->connect_ack_timestamp,
2149 address, session, ats, ats_count);
2151 case S_RECONNECT_BLACKLIST:
2152 /* ATS asks us to switch while we were trying to reconnect; switch to new
2153 address and check blacklist again */
2154 set_address (&n->primary_address,
2155 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2156 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2157 check_blacklist (&n->id,
2158 n->connect_ack_timestamp,
2159 address, session, ats, ats_count);
2161 case S_RECONNECT_SENT:
2162 /* ATS asks us to switch while we were trying to reconnect; switch to new
2163 address and check blacklist again */
2164 set_address (&n->primary_address,
2165 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2166 n->state = S_RECONNECT_BLACKLIST;
2167 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2168 check_blacklist (&n->id,
2169 n->connect_ack_timestamp,
2170 address, session, ats, ats_count);
2172 case S_CONNECTED_SWITCHING_BLACKLIST:
2173 if (n->primary_address.session == session)
2175 /* ATS switches back to still-active session */
2176 free_address (&n->alternative_address);
2177 n->state = S_CONNECTED;
2180 /* ATS asks us to switch a life connection, update blacklist check */
2181 set_address (&n->alternative_address,
2182 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2183 check_blacklist (&n->id,
2184 GNUNET_TIME_absolute_get (),
2185 address, session, ats, ats_count);
2187 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2188 if (n->primary_address.session == session)
2190 /* ATS switches back to still-active session */
2191 free_address (&n->alternative_address);
2192 n->state = S_CONNECTED;
2195 /* ATS asks us to switch a life connection, update blacklist check */
2196 set_address (&n->alternative_address,
2197 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2198 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2199 check_blacklist (&n->id,
2200 GNUNET_TIME_absolute_get (),
2201 address, session, ats, ats_count);
2204 /* not going to switch addresses while disconnecting */
2206 case S_DISCONNECT_FINISHED:
2217 * Master task run for every neighbour. Performs all of the time-related
2218 * activities (keep alive, send next message, disconnect if idle, finish
2219 * clean up after disconnect).
2221 * @param cls the 'struct NeighbourMapEntry' for which we are running
2222 * @param tc scheduler context (unused)
2225 master_task (void *cls,
2226 const struct GNUNET_SCHEDULER_TaskContext *tc)
2228 struct NeighbourMapEntry *n = cls;
2229 struct GNUNET_TIME_Relative delay;
2231 n->task = GNUNET_SCHEDULER_NO_TASK;
2232 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2235 case S_NOT_CONNECTED:
2236 /* invalid state for master task, clean up */
2238 n->state = S_DISCONNECT_FINISHED;
2242 if (0 == delay.rel_value)
2244 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2245 "Connection to `%s' timed out waiting for ATS to provide address\n",
2246 GNUNET_i2s (&n->id));
2247 n->state = S_DISCONNECT_FINISHED;
2252 case S_INIT_BLACKLIST:
2253 if (0 == delay.rel_value)
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2257 GNUNET_i2s (&n->id));
2258 n->state = S_DISCONNECT_FINISHED;
2263 case S_CONNECT_SENT:
2264 if (0 == delay.rel_value)
2266 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2267 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2268 GNUNET_i2s (&n->id));
2269 disconnect_neighbour (n);
2273 case S_CONNECT_RECV_ATS:
2274 if (0 == delay.rel_value)
2276 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2277 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2278 GNUNET_i2s (&n->id));
2279 n->state = S_DISCONNECT_FINISHED;
2284 case S_CONNECT_RECV_BLACKLIST:
2285 if (0 == delay.rel_value)
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2288 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2289 GNUNET_i2s (&n->id));
2290 n->state = S_DISCONNECT_FINISHED;
2295 case S_CONNECT_RECV_ACK:
2296 if (0 == delay.rel_value)
2298 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2299 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2300 GNUNET_i2s (&n->id));
2301 disconnect_neighbour (n);
2306 if (0 == delay.rel_value)
2308 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2309 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2310 GNUNET_i2s (&n->id));
2311 disconnect_neighbour (n);
2314 try_transmission_to_peer (n);
2317 case S_RECONNECT_ATS:
2318 if (0 == delay.rel_value)
2320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2321 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2322 GNUNET_i2s (&n->id));
2323 disconnect_neighbour (n);
2327 case S_RECONNECT_BLACKLIST:
2328 if (0 == delay.rel_value)
2330 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2331 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2332 GNUNET_i2s (&n->id));
2333 disconnect_neighbour (n);
2337 case S_RECONNECT_SENT:
2338 if (0 == delay.rel_value)
2340 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2341 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2342 GNUNET_i2s (&n->id));
2343 disconnect_neighbour (n);
2347 case S_CONNECTED_SWITCHING_BLACKLIST:
2348 if (0 == delay.rel_value)
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2351 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2352 GNUNET_i2s (&n->id));
2353 disconnect_neighbour (n);
2356 try_transmission_to_peer (n);
2359 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2360 if (0 == delay.rel_value)
2362 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2363 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2364 GNUNET_i2s (&n->id));
2365 disconnect_neighbour (n);
2368 try_transmission_to_peer (n);
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2373 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2374 GNUNET_i2s (&n->id));
2375 n->state = S_DISCONNECT_FINISHED;
2378 case S_DISCONNECT_FINISHED:
2379 /* how did we get here!? */
2386 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2388 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == n->task);
2389 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2396 * Send a SESSION_ACK message to the neighbour to confirm that we
2397 * got his CONNECT_ACK.
2399 * @param n neighbour to send the SESSION_ACK to
2402 send_session_ack_message (struct NeighbourMapEntry *n)
2404 struct GNUNET_MessageHeader msg;
2406 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2407 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2408 (void) send_with_session(n,
2409 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2410 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2416 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2417 * Consider switching to it.
2419 * @param message possibly a 'struct SessionConnectMessage' (check format)
2420 * @param peer identity of the peer to switch the address for
2421 * @param address address of the other peer, NULL if other peer
2423 * @param session session to use (or NULL)
2424 * @param ats performance data
2425 * @param ats_count number of entries in ats
2428 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2429 const struct GNUNET_PeerIdentity *peer,
2430 const struct GNUNET_HELLO_Address *address,
2431 struct Session *session,
2432 const struct GNUNET_ATS_Information *ats,
2435 const struct SessionConnectMessage *scm;
2436 struct GNUNET_TIME_Absolute ts;
2437 struct NeighbourMapEntry *n;
2439 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2440 "Received CONNECT_ACK message from peer `%s'\n",
2442 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2444 GNUNET_break_op (0);
2447 scm = (const struct SessionConnectMessage *) message;
2448 GNUNET_break_op (ntohl (scm->reserved) == 0);
2449 if (NULL == (n = lookup_neighbour (peer)))
2451 GNUNET_STATISTICS_update (GST_stats,
2453 ("# unexpected CONNECT_ACK messages (no peer)"),
2457 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2460 case S_NOT_CONNECTED:
2465 case S_INIT_BLACKLIST:
2466 GNUNET_STATISTICS_update (GST_stats,
2468 ("# unexpected CONNECT_ACK messages (not ready)"),
2471 case S_CONNECT_SENT:
2472 if (ts.abs_value != n->primary_address.connect_timestamp.abs_value)
2473 break; /* ACK does not match our original CONNECT message */
2474 n->state = S_CONNECTED;
2475 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2476 GNUNET_STATISTICS_set (GST_stats,
2477 gettext_noop ("# peers connected"),
2478 ++neighbours_connected,
2480 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2481 set_address (&n->primary_address,
2482 n->primary_address.address,
2483 n->primary_address.session,
2484 n->primary_address.bandwidth_in,
2485 n->primary_address.bandwidth_out,
2487 send_session_ack_message (n);
2489 case S_CONNECT_RECV_ATS:
2490 case S_CONNECT_RECV_BLACKLIST:
2491 case S_CONNECT_RECV_ACK:
2492 GNUNET_STATISTICS_update (GST_stats,
2494 ("# unexpected CONNECT_ACK messages (not ready)"),
2498 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2499 send_session_ack_message (n);
2501 case S_RECONNECT_ATS:
2502 case S_RECONNECT_BLACKLIST:
2503 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2504 to give us a new address... */
2505 GNUNET_STATISTICS_update (GST_stats,
2507 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2510 case S_RECONNECT_SENT:
2511 /* new address worked; go back to connected! */
2512 n->state = S_CONNECTED;
2513 send_session_ack_message (n);
2515 case S_CONNECTED_SWITCHING_BLACKLIST:
2516 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2517 send_session_ack_message (n);
2519 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2520 /* new address worked; adopt it and go back to connected! */
2521 n->state = S_CONNECTED;
2522 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2523 GNUNET_assert (GNUNET_NO == n->alternative_address.ats_active);
2524 set_address (&n->primary_address,
2525 n->alternative_address.address,
2526 n->alternative_address.session,
2527 n->alternative_address.bandwidth_in,
2528 n->alternative_address.bandwidth_out,
2530 free_address (&n->alternative_address);
2531 send_session_ack_message (n);
2534 GNUNET_STATISTICS_update (GST_stats,
2536 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2539 case S_DISCONNECT_FINISHED:
2550 * A session was terminated. Take note; if needed, try to get
2551 * an alternative address from ATS.
2553 * @param peer identity of the peer where the session died
2554 * @param session session that is gone
2557 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2558 struct Session *session)
2560 struct NeighbourMapEntry *n;
2561 struct BlackListCheckContext *bcc;
2562 struct BlackListCheckContext *bcc_next;
2564 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2566 while (NULL != (bcc = bcc_next))
2568 bcc_next = bcc->next;
2569 if (bcc->na.session == session)
2571 GST_blacklist_test_cancel (bcc->bc);
2572 GNUNET_HELLO_address_free (bcc->na.address);
2573 GNUNET_CONTAINER_DLL_remove (bc_head,
2579 if (NULL == (n = lookup_neighbour (peer)))
2580 return; /* can't affect us */
2581 if (session != n->primary_address.session)
2583 if (session == n->alternative_address.session)
2585 free_address (&n->alternative_address);
2586 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2587 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2588 n->state = S_CONNECTED;
2592 return; /* doesn't affect us further */
2595 n->expect_latency_response = GNUNET_NO;
2599 case S_NOT_CONNECTED:
2607 case S_INIT_BLACKLIST:
2608 case S_CONNECT_SENT:
2609 free_address (&n->primary_address);
2610 n->state = S_INIT_ATS;
2611 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2612 // FIXME: need to ask ATS for suggestions again?
2613 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2615 case S_CONNECT_RECV_ATS:
2616 case S_CONNECT_RECV_BLACKLIST:
2617 case S_CONNECT_RECV_ACK:
2618 /* error on inbound session; free neighbour entirely */
2619 free_address (&n->primary_address);
2623 free_address (&n->primary_address);
2624 n->state = S_RECONNECT_ATS;
2625 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2626 /* FIXME: is this ATS call needed? */
2627 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2629 case S_RECONNECT_ATS:
2630 /* we don't have an address, how can it go down? */
2633 case S_RECONNECT_BLACKLIST:
2634 case S_RECONNECT_SENT:
2635 n->state = S_RECONNECT_ATS;
2636 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2637 // FIXME: need to ask ATS for suggestions again?
2638 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2640 case S_CONNECTED_SWITCHING_BLACKLIST:
2641 /* primary went down while we were checking secondary against
2642 blacklist, adopt secondary as primary */
2643 free_address (&n->primary_address);
2644 n->primary_address = n->alternative_address;
2645 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2646 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2647 n->state = S_RECONNECT_BLACKLIST;
2649 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2650 /* primary went down while we were waiting for CONNECT_ACK on secondary;
2651 secondary as primary */
2652 free_address (&n->primary_address);
2653 n->primary_address = n->alternative_address;
2654 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2655 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2656 n->state = S_RECONNECT_SENT;
2659 free_address (&n->primary_address);
2661 case S_DISCONNECT_FINISHED:
2662 /* neighbour was freed and plugins told to terminate session */
2668 if (GNUNET_SCHEDULER_NO_TASK != n->task)
2669 GNUNET_SCHEDULER_cancel (n->task);
2670 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2675 * We received a 'SESSION_ACK' message from the other peer.
2676 * If we sent a 'CONNECT_ACK' last, this means we are now
2677 * connected. Otherwise, do nothing.
2679 * @param message possibly a 'struct SessionConnectMessage' (check format)
2680 * @param peer identity of the peer to switch the address for
2681 * @param address address of the other peer, NULL if other peer
2683 * @param session session to use (or NULL)
2684 * @param ats performance data
2685 * @param ats_count number of entries in ats
2688 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
2689 const struct GNUNET_PeerIdentity *peer,
2690 const struct GNUNET_HELLO_Address *address,
2691 struct Session *session,
2692 const struct GNUNET_ATS_Information *ats,
2695 struct NeighbourMapEntry *n;
2697 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2698 "Received SESSION_ACK message from peer `%s'\n",
2700 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2702 GNUNET_break_op (0);
2705 if (NULL == (n = lookup_neighbour (peer)))
2707 /* check if we are in a plausible state for having sent
2708 a CONNECT_ACK. If not, return, otherwise break */
2709 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
2710 (S_CONNECT_SENT != n->state) ) ||
2711 (2 != n->send_connect_ack) )
2713 GNUNET_STATISTICS_update (GST_stats,
2714 gettext_noop ("# unexpected SESSION ACK messages"), 1,
2718 n->state = S_CONNECTED;
2719 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2720 GNUNET_STATISTICS_set (GST_stats,
2721 gettext_noop ("# peers connected"),
2722 ++neighbours_connected,
2724 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2725 set_address (&n->primary_address,
2726 n->primary_address.address,
2727 n->primary_address.session,
2728 n->primary_address.bandwidth_in,
2729 n->primary_address.bandwidth_out,
2735 * Test if we're connected to the given peer.
2737 * @param target peer to test
2738 * @return GNUNET_YES if we are connected, GNUNET_NO if not
2741 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
2743 return test_connected (lookup_neighbour (target));
2748 * Change the incoming quota for the given peer.
2750 * @param neighbour identity of peer to change qutoa for
2751 * @param quota new quota
2754 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2755 struct GNUNET_BANDWIDTH_Value32NBO quota)
2757 struct NeighbourMapEntry *n;
2759 if (NULL == (n = lookup_neighbour (neighbour)))
2761 GNUNET_STATISTICS_update (GST_stats,
2763 ("# SET QUOTA messages ignored (no such peer)"),
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2768 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2769 ntohl (quota.value__), GNUNET_i2s (&n->id));
2770 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2771 if (0 != ntohl (quota.value__))
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2774 GNUNET_i2s (&n->id), "SET_QUOTA");
2775 if (GNUNET_YES == test_connected (n))
2776 GNUNET_STATISTICS_update (GST_stats,
2777 gettext_noop ("# disconnects due to quota of 0"),
2779 disconnect_neighbour (n);
2784 * We received a disconnect message from the given peer,
2785 * validate and process.
2787 * @param peer sender of the message
2788 * @param msg the disconnect message
2791 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2793 const struct GNUNET_MessageHeader
2796 struct NeighbourMapEntry *n;
2797 const struct SessionDisconnectMessage *sdm;
2800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2801 "Received DISCONNECT message from peer `%s'\n",
2803 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2805 // GNUNET_break_op (0);
2806 GNUNET_STATISTICS_update (GST_stats,
2808 ("# disconnect messages ignored (old format)"), 1,
2812 sdm = (const struct SessionDisconnectMessage *) msg;
2813 if (NULL == (n = lookup_neighbour (peer)))
2814 return; /* gone already */
2815 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <= n->connect_ack_timestamp.abs_value)
2817 GNUNET_STATISTICS_update (GST_stats,
2819 ("# disconnect messages ignored (timestamp)"), 1,
2823 GNUNET_CRYPTO_hash (&sdm->public_key,
2824 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2826 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2828 GNUNET_break_op (0);
2831 if (ntohl (sdm->purpose.size) !=
2832 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2833 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2834 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2836 GNUNET_break_op (0);
2840 GNUNET_CRYPTO_rsa_verify
2841 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2842 &sdm->signature, &sdm->public_key))
2844 GNUNET_break_op (0);
2847 if (GNUNET_YES == test_connected (n))
2848 GNUNET_STATISTICS_update (GST_stats,
2850 ("# other peer asked to disconnect from us"), 1,
2852 disconnect_neighbour (n);
2857 * Closure for the neighbours_iterate function.
2859 struct IteratorContext
2862 * Function to call on each connected neighbour.
2864 GST_NeighbourIterator cb;
2874 * Call the callback from the closure for each connected neighbour.
2876 * @param cls the 'struct IteratorContext'
2877 * @param key the hash of the public key of the neighbour
2878 * @param value the 'struct NeighbourMapEntry'
2879 * @return GNUNET_OK (continue to iterate)
2882 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2884 struct IteratorContext *ic = cls;
2885 struct NeighbourMapEntry *n = value;
2887 if (GNUNET_YES == test_connected (n))
2888 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->primary_address.address);
2894 * Iterate over all connected neighbours.
2896 * @param cb function to call
2897 * @param cb_cls closure for cb
2900 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2902 struct IteratorContext ic;
2904 if (NULL == neighbours)
2905 return; /* can happen during shutdown */
2908 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2913 * If we have an active connection to the given target, it must be shutdown.
2915 * @param target peer to disconnect from
2918 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2920 struct NeighbourMapEntry *n;
2922 if (NULL == (n = lookup_neighbour (target)))
2923 return; /* not active */
2924 if (GNUNET_YES == test_connected (n))
2925 GNUNET_STATISTICS_update (GST_stats,
2927 ("# disconnected from peer upon explicit request"), 1,
2929 disconnect_neighbour (n);
2934 * Obtain current latency information for the given neighbour.
2936 * @param peer to get the latency for
2937 * @return observed latency of the address, FOREVER if the
2938 * the connection is not up
2940 struct GNUNET_TIME_Relative
2941 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
2943 struct NeighbourMapEntry *n;
2945 n = lookup_neighbour (peer);
2947 return GNUNET_TIME_UNIT_FOREVER_REL;
2951 case S_RECONNECT_SENT:
2952 case S_RECONNECT_ATS:
2954 case S_NOT_CONNECTED:
2955 case S_INIT_BLACKLIST:
2957 case S_CONNECT_SENT:
2958 case S_CONNECT_RECV_BLACKLIST:
2960 case S_DISCONNECT_FINISHED:
2961 return GNUNET_TIME_UNIT_FOREVER_REL;
2966 return GNUNET_TIME_UNIT_FOREVER_REL;
2971 * Obtain current address information for the given neighbour.
2974 * @return address currently used
2976 struct GNUNET_HELLO_Address *
2977 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
2979 struct NeighbourMapEntry *n;
2981 n = lookup_neighbour (peer);
2984 return n->primary_address.address;
2989 * Initialize the neighbours subsystem.
2991 * @param cls closure for callbacks
2992 * @param connect_cb function to call if we connect to a peer
2993 * @param disconnect_cb function to call if we disconnect from a peer
2994 * @param peer_address_cb function to call if we change an active address
2998 GST_neighbours_start (void *cls,
2999 GNUNET_TRANSPORT_NotifyConnect connect_cb,
3000 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3001 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
3004 connect_notify_cb = connect_cb;
3005 disconnect_notify_cb = disconnect_cb;
3006 address_change_cb = peer_address_cb;
3007 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
3012 * Disconnect from the given neighbour.
3015 * @param key hash of neighbour's public key (not used)
3016 * @param value the 'struct NeighbourMapEntry' of the neighbour
3017 * @return GNUNET_OK (continue to iterate)
3020 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
3022 struct NeighbourMapEntry *n = value;
3024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3025 "Disconnecting peer `%4s', %s\n",
3026 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3033 * Cleanup the neighbours subsystem.
3036 GST_neighbours_stop ()
3038 if (NULL == neighbours)
3040 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
3041 &disconnect_all_neighbours,
3043 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
3045 callback_cls = NULL;
3046 connect_notify_cb = NULL;
3047 disconnect_notify_cb = NULL;
3048 address_change_cb = NULL;
3052 /* end of file gnunet-service-transport_neighbours.c */