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 100s interval we
64 * send 3 keepalives in each interval, so 3 messages would need to be
65 * lost in a row for a disconnect).
67 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
70 * How long are we willing to wait for a response from ATS before timing out?
72 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 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);
625 print_state (int state)
630 case S_NOT_CONNECTED:
631 return "S_NOT_CONNECTED";
636 case S_INIT_BLACKLIST:
637 return "S_INIT_BLACKLIST";
640 return "S_CONNECT_SENT";
642 case S_CONNECT_RECV_ATS:
643 return "S_CONNECT_RECV_ATS";
645 case S_CONNECT_RECV_BLACKLIST:
646 return "S_CONNECT_RECV_BLACKLIST";
648 case S_CONNECT_RECV_ACK:
649 return "S_CONNECT_RECV_ACK";
652 return "S_CONNECTED";
654 case S_RECONNECT_ATS:
655 return "S_RECONNECT_ATS";
657 case S_RECONNECT_BLACKLIST:
658 return "S_RECONNECT_BLACKLIST";
660 case S_RECONNECT_SENT:
661 return "S_RECONNECT_SENT";
663 case S_CONNECTED_SWITCHING_BLACKLIST:
664 return "S_CONNECTED_SWITCHING_BLACKLIST";
666 case S_CONNECTED_SWITCHING_CONNECT_SENT:
667 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
670 return "S_DISCONNECT";
672 case S_DISCONNECT_FINISHED:
673 return "S_DISCONNECT_FINISHED";
685 * Test if we're connected to the given peer.
687 * @param n neighbour entry of peer to test
688 * @return GNUNET_YES if we are connected, GNUNET_NO if not
691 test_connected (struct NeighbourMapEntry *n)
697 case S_NOT_CONNECTED:
699 case S_INIT_BLACKLIST:
701 case S_CONNECT_RECV_ATS:
702 case S_CONNECT_RECV_BLACKLIST:
703 case S_CONNECT_RECV_ACK:
706 case S_RECONNECT_ATS:
707 case S_RECONNECT_BLACKLIST:
708 case S_RECONNECT_SENT:
709 case S_CONNECTED_SWITCHING_BLACKLIST:
710 case S_CONNECTED_SWITCHING_CONNECT_SENT:
713 case S_DISCONNECT_FINISHED:
716 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
720 return GNUNET_SYSERR;
724 * Send information about a new outbound quota to our clients.
726 * @param target affected peer
727 * @param quota new quota
730 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
731 struct GNUNET_BANDWIDTH_Value32NBO quota)
733 struct QuotaSetMessage q_msg;
735 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
736 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
737 ntohl (quota.value__), GNUNET_i2s (target));
738 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
739 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
741 q_msg.peer = (*target);
742 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
747 * We don't need a given neighbour address any more.
748 * Release its resources and give appropriate notifications
749 * to ATS and other subsystems.
751 * @param na address we are done with; 'na' itself must NOT be 'free'd, only the contents!
754 free_address (struct NeighbourAddress *na)
756 if (GNUNET_YES == na->ats_active)
758 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
759 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
761 na->ats_active = GNUNET_NO;
762 if (NULL != na->address)
764 GNUNET_HELLO_address_free (na->address);
772 * Initialize the 'struct NeighbourAddress'.
774 * @param na neighbour address to initialize
775 * @param address address of the other peer, NULL if other peer
777 * @param session session to use (or NULL, in which case an
778 * address must be setup)
779 * @param bandwidth_in inbound quota to be used when connection is up
780 * @param bandwidth_out outbound quota to be used when connection is up
781 * @param is_active GNUNET_YES to mark this as the active address with ATS
784 set_address (struct NeighbourAddress *na,
785 const struct GNUNET_HELLO_Address *address,
786 struct Session *session,
787 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
788 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
791 struct GNUNET_TRANSPORT_PluginFunctions *papi;
793 if (NULL == (papi = GST_plugins_find (address->transport_name)))
798 if (session == na->session)
800 na->bandwidth_in = bandwidth_in;
801 na->bandwidth_out = bandwidth_out;
802 if (is_active != na->ats_active)
804 na->ats_active = is_active;
805 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
806 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
808 if (GNUNET_YES == is_active)
810 /* FIXME: is this the right place to set quotas? */
811 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
812 send_outbound_quota (&address->peer, bandwidth_out);
818 session = papi->get_session (papi->cls, address);
821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
822 "Failed to obtain new session for peer `%s' and address '%s'\n",
823 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
824 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
827 na->address = GNUNET_HELLO_address_copy (address);
828 na->bandwidth_in = bandwidth_in;
829 na->bandwidth_out = bandwidth_out;
830 na->session = session;
831 na->ats_active = is_active;
832 if (GNUNET_YES == is_active)
834 /* Telling ATS about new session */
835 GNUNET_ATS_address_update (GST_ats, na->address, na->session, NULL, 0);
836 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
837 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
839 /* FIXME: is this the right place to set quotas? */
840 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
841 send_outbound_quota (&address->peer, bandwidth_out);
847 * Free a neighbour map entry.
849 * @param n entry to free
850 * @param keep_sessions GNUNET_NO to tell plugin to terminate sessions,
851 * GNUNET_YES to keep all sessions
854 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
856 struct MessageQueue *mq;
857 struct GNUNET_TRANSPORT_PluginFunctions *papi;
859 n->is_active = NULL; /* always free'd by its own continuation! */
861 /* fail messages currently in the queue */
862 while (NULL != (mq = n->messages_head))
864 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
865 if (NULL != mq->cont)
866 mq->cont (mq->cont_cls, GNUNET_SYSERR);
869 /* It is too late to send other peer disconnect notifications, but at
870 least internally we need to get clean... */
871 if (GNUNET_YES == test_connected (n))
873 GNUNET_STATISTICS_set (GST_stats,
874 gettext_noop ("# peers connected"),
875 --neighbours_connected,
877 disconnect_notify_cb (callback_cls, &n->id);
880 /* FIXME-PLUGIN-API: This does not seem to guarantee that all
881 transport sessions eventually get killed due to inactivity; they
882 MUST have their own timeout logic (but at least TCP doesn't have
883 one yet). Are we sure that EVERY 'session' of a plugin is
884 actually cleaned up this way!? Note that if we are switching
885 between two TCP sessions to the same peer, the existing plugin
886 API gives us not even the means to selectively kill only one of
887 them! Killing all sessions like this seems to be very, very
889 if ((GNUNET_NO == keep_sessions) &&
890 (NULL != n->primary_address.address) &&
891 (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name))))
892 papi->disconnect (papi->cls, &n->id);
894 n->state = S_DISCONNECT_FINISHED;
896 GNUNET_assert (GNUNET_YES ==
897 GNUNET_CONTAINER_multihashmap_remove (neighbours,
898 &n->id.hashPubKey, n));
900 /* cut transport-level connection */
901 free_address (&n->primary_address);
902 free_address (&n->alternative_address);
904 // FIXME-ATS-API: we might want to be more specific about
905 // which states we do this from in the future (ATS should
906 // have given us a 'suggest_address' handle, and if we have
907 // such a handle, we should cancel the operation here!
908 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
910 if (GNUNET_SCHEDULER_NO_TASK != n->task)
912 GNUNET_SCHEDULER_cancel (n->task);
913 n->task = GNUNET_SCHEDULER_NO_TASK;
915 /* free rest of memory */
921 * Transmit a message using the current session of the given
924 * @param n entry for the recipient
925 * @param msgbuf buffer to transmit
926 * @param msgbuf_size number of bytes in buffer
927 * @param priority transmission priority
928 * @param timeout transmission timeout
929 * @param cont continuation to call when finished (can be NULL)
930 * @param cont_cls closure for cont
933 send_with_session (struct NeighbourMapEntry *n,
934 const char *msgbuf, size_t msgbuf_size,
936 struct GNUNET_TIME_Relative timeout,
937 GNUNET_TRANSPORT_TransmitContinuation cont,
940 struct GNUNET_TRANSPORT_PluginFunctions *papi;
942 GNUNET_assert (n->primary_address.session != NULL);
943 if ( ( (NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name))) ||
944 (-1 == papi->send (papi->cls,
945 n->primary_address.session,
951 cont (cont_cls, &n->id, GNUNET_SYSERR);
952 GNUNET_break (NULL != papi);
957 * Master task run for every neighbour. Performs all of the time-related
958 * activities (keep alive, send next message, disconnect if idle, finish
959 * clean up after disconnect).
961 * @param cls the 'struct NeighbourMapEntry' for which we are running
962 * @param tc scheduler context (unused)
965 master_task (void *cls,
966 const struct GNUNET_SCHEDULER_TaskContext *tc);
970 * Function called when the 'DISCONNECT' message has been sent by the
971 * plugin. Frees the neighbour --- if the entry still exists.
974 * @param target identity of the neighbour that was disconnected
975 * @param result GNUNET_OK if the disconnect got out successfully
978 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
981 struct NeighbourMapEntry *n;
983 n = lookup_neighbour (target);
985 return; /* already gone */
986 if (S_DISCONNECT != n->state)
987 return; /* have created a fresh entry since */
988 n->state = S_DISCONNECT;
989 if (GNUNET_SCHEDULER_NO_TASK != n->task)
990 GNUNET_SCHEDULER_cancel (n->task);
991 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
996 * Transmit a DISCONNECT message to the other peer.
998 * @param n neighbour to send DISCONNECT message.
1001 send_disconnect (struct NeighbourMapEntry *n)
1003 struct SessionDisconnectMessage disconnect_msg;
1005 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1006 "Sending DISCONNECT message to peer `%4s'\n",
1007 GNUNET_i2s (&n->id));
1008 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1009 disconnect_msg.header.type =
1010 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1011 disconnect_msg.reserved = htonl (0);
1012 disconnect_msg.purpose.size =
1013 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1014 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1015 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1016 disconnect_msg.purpose.purpose =
1017 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1018 disconnect_msg.timestamp =
1019 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1020 disconnect_msg.public_key = GST_my_public_key;
1021 GNUNET_assert (GNUNET_OK ==
1022 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1023 &disconnect_msg.purpose,
1024 &disconnect_msg.signature));
1026 send_with_session (n,
1027 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1028 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1029 &send_disconnect_cont, NULL);
1030 GNUNET_STATISTICS_update (GST_stats,
1032 ("# DISCONNECT messages sent"), 1,
1038 * Disconnect from the given neighbour, clean up the record.
1040 * @param n neighbour to disconnect from
1043 disconnect_neighbour (struct NeighbourMapEntry *n)
1045 /* depending on state, notify neighbour and/or upper layers of this peer
1049 case S_NOT_CONNECTED:
1051 case S_INIT_BLACKLIST:
1052 /* other peer is completely unaware of us, no need to send DISCONNECT */
1053 n->state = S_DISCONNECT_FINISHED;
1054 free_neighbour (n, GNUNET_NO);
1056 case S_CONNECT_SENT:
1057 send_disconnect (n);
1058 n->state = S_DISCONNECT;
1060 case S_CONNECT_RECV_ATS:
1061 case S_CONNECT_RECV_BLACKLIST:
1062 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1063 n->state = S_DISCONNECT_FINISHED;
1064 free_neighbour (n, GNUNET_NO);
1066 case S_CONNECT_RECV_ACK:
1067 /* we DID ACK the other peer's request, must send DISCONNECT */
1068 send_disconnect (n);
1069 n->state = S_DISCONNECT;
1072 case S_RECONNECT_BLACKLIST:
1073 case S_RECONNECT_SENT:
1074 case S_CONNECTED_SWITCHING_BLACKLIST:
1075 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1076 /* we are currently connected, need to send disconnect and do
1077 internal notifications and update statistics */
1078 send_disconnect (n);
1079 GNUNET_STATISTICS_set (GST_stats,
1080 gettext_noop ("# peers connected"),
1081 --neighbours_connected,
1083 disconnect_notify_cb (callback_cls, &n->id);
1084 n->state = S_DISCONNECT;
1086 case S_RECONNECT_ATS:
1087 /* ATS address request timeout, disconnect without sending disconnect message */
1088 GNUNET_STATISTICS_set (GST_stats,
1089 gettext_noop ("# peers connected"),
1090 --neighbours_connected,
1092 disconnect_notify_cb (callback_cls, &n->id);
1093 n->state = S_DISCONNECT;
1096 /* already disconnected, ignore */
1098 case S_DISCONNECT_FINISHED:
1099 /* already cleaned up, how did we get here!? */
1103 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1107 /* schedule timeout to clean up */
1108 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1109 GNUNET_SCHEDULER_cancel (n->task);
1110 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1116 * We're done with our transmission attempt, continue processing.
1118 * @param cls the 'struct MessageQueue' of the message
1119 * @param receiver intended receiver
1120 * @param success whether it worked or not
1123 transmit_send_continuation (void *cls,
1124 const struct GNUNET_PeerIdentity *receiver,
1127 struct MessageQueue *mq = cls;
1128 struct NeighbourMapEntry *n;
1130 if (NULL == (n = lookup_neighbour (receiver)))
1133 return; /* disconnect or other error while transmitting, can happen */
1135 if (n->is_active == mq)
1137 /* this is still "our" neighbour, remove us from its queue
1138 and allow it to send the next message now */
1139 n->is_active = NULL;
1140 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1141 GNUNET_SCHEDULER_cancel (n->task);
1142 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1144 GNUNET_assert (bytes_in_send_queue >= mq->message_buf_size);
1145 bytes_in_send_queue -= mq->message_buf_size;
1146 GNUNET_STATISTICS_set (GST_stats,
1148 ("# bytes in message queue for other peers"),
1149 bytes_in_send_queue, GNUNET_NO);
1150 if (GNUNET_OK == success)
1151 GNUNET_STATISTICS_update (GST_stats,
1153 ("# messages transmitted to other peers"),
1156 GNUNET_STATISTICS_update (GST_stats,
1158 ("# transmission failures for messages to other peers"),
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161 "Sending message to `%s' of type %u was a %s\n",
1162 GNUNET_i2s (receiver),
1163 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1164 (success == GNUNET_OK) ? "success" : "FAILURE");
1165 if (NULL != mq->cont)
1166 mq->cont (mq->cont_cls, success);
1172 * Check the message list for the given neighbour and if we can
1173 * send a message, do so. This function should only be called
1174 * if the connection is at least generally ready for transmission.
1175 * While we will only send one message at a time, no bandwidth
1176 * quota management is performed here. If a message was given to
1177 * the plugin, the continuation will automatically re-schedule
1178 * the 'master' task once the next message might be transmitted.
1180 * @param n target peer for which to transmit
1183 try_transmission_to_peer (struct NeighbourMapEntry *n)
1185 struct MessageQueue *mq;
1186 struct GNUNET_TIME_Relative timeout;
1188 if (NULL == n->primary_address.address)
1190 /* no address, why are we here? */
1194 if ((0 == n->primary_address.address->address_length) &&
1195 (NULL == n->primary_address.session))
1197 /* no address, why are we here? */
1201 if (NULL != n->is_active)
1203 /* transmission already pending */
1207 /* timeout messages from the queue that are past their due date */
1208 while (NULL != (mq = n->messages_head))
1210 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1211 if (timeout.rel_value > 0)
1213 GNUNET_STATISTICS_update (GST_stats,
1215 ("# messages timed out while in transport queue"),
1217 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1219 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
1222 return; /* no more messages */
1223 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1225 send_with_session (n,
1226 mq->message_buf, mq->message_buf_size,
1227 0 /* priority */, timeout,
1228 &transmit_send_continuation, mq);
1233 * Send keepalive message to the neighbour. Must only be called
1234 * if we are on 'connected' state. Will internally determine
1235 * if a keepalive is truly needed (so can always be called).
1237 * @param n neighbour that went idle and needs a keepalive
1240 send_keepalive (struct NeighbourMapEntry *n)
1242 struct GNUNET_MessageHeader m;
1244 GNUNET_assert (S_CONNECTED == n->state);
1245 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value > 0)
1246 return; /* no keepalive needed at this time */
1247 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1248 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1249 send_with_session (n,
1250 (const void *) &m, sizeof (m),
1251 UINT32_MAX /* priority */,
1252 KEEPALIVE_FREQUENCY,
1254 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1256 n->expect_latency_response = GNUNET_YES;
1257 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1258 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1263 * Keep the connection to the given neighbour alive longer,
1264 * we received a KEEPALIVE (or equivalent); send a response.
1266 * @param neighbour neighbour to keep alive (by sending keep alive response)
1269 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1271 struct NeighbourMapEntry *n;
1272 struct GNUNET_MessageHeader m;
1274 if (NULL == (n = lookup_neighbour (neighbour)))
1276 GNUNET_STATISTICS_update (GST_stats,
1278 ("# KEEPALIVE messages discarded (peer unknown)"),
1282 if (NULL == n->primary_address.session)
1284 GNUNET_STATISTICS_update (GST_stats,
1286 ("# KEEPALIVE messages discarded (no session)"),
1290 /* send reply to allow neighbour to measure latency */
1291 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1292 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1293 send_with_session(n,
1294 (const void *) &m, sizeof (m),
1295 UINT32_MAX /* priority */,
1296 KEEPALIVE_FREQUENCY,
1302 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1303 * latency to this peer. Pass the updated information (existing ats
1304 * plus calculated latency) to ATS.
1306 * @param neighbour neighbour to keep alive
1307 * @param ats performance data
1308 * @param ats_count number of entries in ats
1311 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1312 const struct GNUNET_ATS_Information *ats,
1315 struct NeighbourMapEntry *n;
1317 struct GNUNET_ATS_Information ats_new[ats_count + 1];
1319 if (NULL == (n = lookup_neighbour (neighbour)))
1321 GNUNET_STATISTICS_update (GST_stats,
1323 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1327 if ( (S_CONNECTED != n->state) ||
1328 (GNUNET_YES != n->expect_latency_response) )
1330 GNUNET_STATISTICS_update (GST_stats,
1332 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1336 n->expect_latency_response = GNUNET_NO;
1337 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1338 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1340 "Latency for peer `%s' is %llu ms\n",
1341 GNUNET_i2s (&n->id), n->latency.rel_value);
1342 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1343 /* append latency */
1344 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1345 if (n->latency.rel_value > UINT32_MAX)
1346 latency = UINT32_MAX;
1348 latency = n->latency.rel_value;
1349 ats_new[ats_count].value = htonl (latency);
1350 GNUNET_ATS_address_update (GST_ats,
1351 n->primary_address.address,
1352 n->primary_address.session, ats_new,
1358 * We have received a message from the given sender. How long should
1359 * we delay before receiving more? (Also used to keep the peer marked
1362 * @param sender sender of the message
1363 * @param size size of the message
1364 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1365 * GNUNET_NO if the neighbour is not connected or violates the quota,
1366 * GNUNET_SYSERR if the connection is not fully up yet
1367 * @return how long to wait before reading more from this sender
1369 struct GNUNET_TIME_Relative
1370 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1371 *sender, ssize_t size, int *do_forward)
1373 struct NeighbourMapEntry *n;
1374 struct GNUNET_TIME_Relative ret;
1376 if (NULL == neighbours)
1378 *do_forward = GNUNET_NO;
1379 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1381 if (NULL == (n = lookup_neighbour (sender)))
1383 GST_neighbours_try_connect (sender);
1384 if (NULL == (n = lookup_neighbour (sender)))
1386 GNUNET_STATISTICS_update (GST_stats,
1388 ("# messages discarded due to lack of neighbour record"),
1390 *do_forward = GNUNET_NO;
1391 return GNUNET_TIME_UNIT_ZERO;
1394 if (! test_connected (n))
1396 *do_forward = GNUNET_SYSERR;
1397 return GNUNET_TIME_UNIT_ZERO;
1399 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1401 n->quota_violation_count++;
1402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1403 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1404 n->in_tracker.available_bytes_per_s__,
1405 n->quota_violation_count);
1406 /* Discount 32k per violation */
1407 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1411 if (n->quota_violation_count > 0)
1413 /* try to add 32k back */
1414 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1415 n->quota_violation_count--;
1418 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1420 GNUNET_STATISTICS_update (GST_stats,
1422 ("# bandwidth quota violations by other peers"),
1424 *do_forward = GNUNET_NO;
1425 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1427 *do_forward = GNUNET_YES;
1428 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1429 if (ret.rel_value > 0)
1431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1432 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1433 (unsigned long long) n->in_tracker.
1434 consumption_since_last_update__,
1435 (unsigned int) n->in_tracker.available_bytes_per_s__,
1436 (unsigned long long) ret.rel_value);
1437 GNUNET_STATISTICS_update (GST_stats,
1438 gettext_noop ("# ms throttling suggested"),
1439 (int64_t) ret.rel_value, GNUNET_NO);
1446 * Transmit a message to the given target using the active connection.
1448 * @param target destination
1449 * @param msg message to send
1450 * @param msg_size number of bytes in msg
1451 * @param timeout when to fail with timeout
1452 * @param cont function to call when done
1453 * @param cont_cls closure for 'cont'
1456 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1457 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1458 GST_NeighbourSendContinuation cont, void *cont_cls)
1460 struct NeighbourMapEntry *n;
1461 struct MessageQueue *mq;
1463 /* All ove these cases should never happen; they are all API violations.
1464 But we check anyway, just to be sure. */
1465 if (NULL == (n = lookup_neighbour (target)))
1469 cont (cont_cls, GNUNET_SYSERR);
1472 if (GNUNET_YES != test_connected (n))
1476 cont (cont_cls, GNUNET_SYSERR);
1479 bytes_in_send_queue += msg_size;
1480 GNUNET_STATISTICS_set (GST_stats,
1482 ("# bytes in message queue for other peers"),
1483 bytes_in_send_queue, GNUNET_NO);
1484 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1486 mq->cont_cls = cont_cls;
1487 memcpy (&mq[1], msg, msg_size);
1488 mq->message_buf = (const char *) &mq[1];
1489 mq->message_buf_size = msg_size;
1490 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1491 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1492 if ( (NULL != n->is_active) ||
1493 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1495 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1496 GNUNET_SCHEDULER_cancel (n->task);
1497 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1502 * Send a SESSION_CONNECT message via the given address.
1504 * @param na address to use
1507 send_session_connect (struct NeighbourAddress *na)
1509 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1510 struct SessionConnectMessage connect_msg;
1512 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1517 if (NULL == na->session)
1518 na->session = papi->get_session (papi->cls, na->address);
1519 if (NULL == na->session)
1524 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1525 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1526 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1527 connect_msg.reserved = htonl (0);
1528 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1529 (void) papi->send (papi->cls,
1531 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1533 GNUNET_TIME_UNIT_FOREVER_REL,
1539 * Send a SESSION_CONNECT_ACK message via the given address.
1541 * @param address address to use
1542 * @param session session to use
1543 * @param timestamp timestamp to use for the ACK message
1546 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1547 struct Session *session,
1548 struct GNUNET_TIME_Absolute timestamp)
1550 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1551 struct SessionConnectMessage connect_msg;
1553 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1558 if (NULL == session)
1559 session = papi->get_session (papi->cls, address);
1560 if (NULL == session)
1565 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1566 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1567 connect_msg.reserved = htonl (0);
1568 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1569 (void) papi->send (papi->cls,
1571 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1573 GNUNET_TIME_UNIT_FOREVER_REL,
1579 * Create a fresh entry in the neighbour map for the given peer
1581 * @param peer peer to create an entry for
1582 * @return new neighbour map entry
1584 static struct NeighbourMapEntry *
1585 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1587 struct NeighbourMapEntry *n;
1589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1590 "Creating new neighbour entry for `%s'\n",
1592 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1594 n->state = S_NOT_CONNECTED;
1595 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1596 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1597 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1598 MAX_BANDWIDTH_CARRY_S);
1599 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1600 GNUNET_assert (GNUNET_OK ==
1601 GNUNET_CONTAINER_multihashmap_put (neighbours,
1602 &n->id.hashPubKey, n,
1603 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1609 * Check if the two given addresses are the same.
1610 * Actually only checks if the sessions are non-NULL
1611 * (which they should be) and then if they are identical;
1612 * the actual addresses don't matter if the session
1613 * pointers match anyway, and we must have session pointers
1616 * @param a1 first address to compare
1617 * @param a2 other address to compare
1618 * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
1621 address_matches (const struct NeighbourAddress *a1,
1622 const struct NeighbourAddress *a2)
1624 if ( (NULL == a1->session) ||
1625 (NULL == a2->session) )
1630 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1635 * Try to create a connection to the given target (eventually).
1637 * @param target peer to try to connect to
1640 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1642 struct NeighbourMapEntry *n;
1644 if (NULL == neighbours)
1645 return; /* during shutdown, do nothing */
1646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1647 "Asked to connect to peer `%s'\n",
1648 GNUNET_i2s (target));
1650 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1652 /* refuse to connect to myself */
1653 /* FIXME: can this happen? Is this not an API violation? */
1654 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1655 "Refusing to try to connect to myself.\n");
1658 n = lookup_neighbour (target);
1663 case S_NOT_CONNECTED:
1664 /* this should not be possible */
1666 free_neighbour (n, GNUNET_NO);
1669 case S_INIT_BLACKLIST:
1670 case S_CONNECT_SENT:
1671 case S_CONNECT_RECV_ATS:
1672 case S_CONNECT_RECV_BLACKLIST:
1673 case S_CONNECT_RECV_ACK:
1674 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1675 "Ignoring request to try to connect to `%s', already trying!\n",
1676 GNUNET_i2s (target));
1677 return; /* already trying */
1679 case S_RECONNECT_ATS:
1680 case S_RECONNECT_BLACKLIST:
1681 case S_RECONNECT_SENT:
1682 case S_CONNECTED_SWITCHING_BLACKLIST:
1683 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1684 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1685 "Ignoring request to try to connect, already connected to `%s'!\n",
1686 GNUNET_i2s (target));
1687 return; /* already connected */
1689 /* get rid of remains, ready to re-try immediately */
1690 free_neighbour (n, GNUNET_NO);
1692 case S_DISCONNECT_FINISHED:
1693 /* should not be possible */
1696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1698 free_neighbour (n, GNUNET_NO);
1702 n = setup_neighbour (target);
1703 n->state = S_INIT_ATS;
1704 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1706 GNUNET_ATS_reset_backoff (GST_ats, target);
1707 GNUNET_ATS_suggest_address (GST_ats, target);
1712 * Function called with the result of a blacklist check.
1714 * @param cls closure with the 'struct BlackListCheckContext'
1715 * @param peer peer this check affects
1716 * @param result GNUNET_OK if the address is allowed
1719 handle_test_blacklist_cont (void *cls,
1720 const struct GNUNET_PeerIdentity *peer,
1723 struct BlackListCheckContext *bcc = cls;
1724 struct NeighbourMapEntry *n;
1727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1728 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1730 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1731 if (GNUNET_OK == result)
1733 /* valid new address, let ATS know! */
1734 GNUNET_ATS_address_update (GST_ats,
1737 bcc->ats, bcc->ats_count);
1739 if (NULL == (n = lookup_neighbour (peer)))
1740 goto cleanup; /* nobody left to care about new address */
1743 case S_NOT_CONNECTED:
1744 /* this should not be possible */
1746 free_neighbour (n, GNUNET_NO);
1749 /* still waiting on ATS suggestion */
1751 case S_INIT_BLACKLIST:
1752 /* check if the address the blacklist was fine with matches
1753 ATS suggestion, if so, we can move on! */
1754 if ( (GNUNET_OK == result) &&
1755 (1 == n->send_connect_ack) )
1757 n->send_connect_ack = 2;
1758 send_session_connect_ack_message (bcc->na.address,
1760 n->connect_ack_timestamp);
1762 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1763 break; /* result for an address we currently don't care about */
1764 if (GNUNET_OK == result)
1766 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1767 n->state = S_CONNECT_SENT;
1768 send_session_connect (&n->primary_address);
1772 GNUNET_ATS_address_destroyed (GST_ats,
1775 n->state = S_INIT_ATS;
1776 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1777 // FIXME: do we need to ask ATS again for suggestions?
1778 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1781 case S_CONNECT_SENT:
1782 /* waiting on CONNECT_ACK, send ACK if one is pending */
1783 if ( (GNUNET_OK == result) &&
1784 (1 == n->send_connect_ack) )
1786 n->send_connect_ack = 2;
1787 send_session_connect_ack_message (n->primary_address.address,
1788 n->primary_address.session,
1789 n->connect_ack_timestamp);
1792 case S_CONNECT_RECV_ATS:
1793 /* still waiting on ATS suggestion, don't care about blacklist */
1795 case S_CONNECT_RECV_BLACKLIST:
1796 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1797 break; /* result for an address we currently don't care about */
1798 if (GNUNET_OK == result)
1800 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1801 n->state = S_CONNECT_RECV_ACK;
1802 send_session_connect_ack_message (bcc->na.address,
1804 n->connect_ack_timestamp);
1805 if (1 == n->send_connect_ack)
1806 n->send_connect_ack = 2;
1810 GNUNET_ATS_address_destroyed (GST_ats,
1813 n->state = S_INIT_ATS;
1814 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1815 // FIXME: do we need to ask ATS again for suggestions?
1816 GNUNET_ATS_reset_backoff (GST_ats, peer);
1817 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1820 case S_CONNECT_RECV_ACK:
1821 /* waiting on SESSION_ACK, send ACK if one is pending */
1822 if ( (GNUNET_OK == result) &&
1823 (1 == n->send_connect_ack) )
1825 n->send_connect_ack = 2;
1826 send_session_connect_ack_message (n->primary_address.address,
1827 n->primary_address.session,
1828 n->connect_ack_timestamp);
1832 /* already connected, don't care about blacklist */
1834 case S_RECONNECT_ATS:
1835 /* still waiting on ATS suggestion, don't care about blacklist */
1837 case S_RECONNECT_BLACKLIST:
1838 if ( (GNUNET_OK == result) &&
1839 (1 == n->send_connect_ack) )
1841 n->send_connect_ack = 2;
1842 send_session_connect_ack_message (bcc->na.address,
1844 n->connect_ack_timestamp);
1846 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1847 break; /* result for an address we currently don't care about */
1848 if (GNUNET_OK == result)
1850 send_session_connect (&n->primary_address);
1851 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1852 n->state = S_RECONNECT_SENT;
1856 GNUNET_ATS_address_destroyed (GST_ats,
1859 n->state = S_RECONNECT_ATS;
1860 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1861 // FIXME: do we need to ask ATS again for suggestions?
1862 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1865 case S_RECONNECT_SENT:
1866 /* waiting on CONNECT_ACK, don't care about blacklist */
1867 if ( (GNUNET_OK == result) &&
1868 (1 == n->send_connect_ack) )
1870 n->send_connect_ack = 2;
1871 send_session_connect_ack_message (n->primary_address.address,
1872 n->primary_address.session,
1873 n->connect_ack_timestamp);
1876 case S_CONNECTED_SWITCHING_BLACKLIST:
1877 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1878 break; /* result for an address we currently don't care about */
1879 if (GNUNET_OK == result)
1881 send_session_connect (&n->alternative_address);
1882 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1886 GNUNET_ATS_address_destroyed (GST_ats,
1889 free_address (&n->alternative_address);
1890 n->state = S_CONNECTED;
1893 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1894 /* waiting on CONNECT_ACK, don't care about blacklist */
1895 if ( (GNUNET_OK == result) &&
1896 (1 == n->send_connect_ack) )
1898 n->send_connect_ack = 2;
1899 send_session_connect_ack_message (n->primary_address.address,
1900 n->primary_address.session,
1901 n->connect_ack_timestamp);
1905 /* Nothing to do here, ATS will already do what can be done */
1907 case S_DISCONNECT_FINISHED:
1908 /* should not be possible */
1912 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1914 free_neighbour (n, GNUNET_NO);
1918 GNUNET_HELLO_address_free (bcc->na.address);
1919 GNUNET_CONTAINER_DLL_remove (bc_head,
1927 * We want to know if connecting to a particular peer via
1928 * a particular address is allowed. Check it!
1930 * @param peer identity of the peer to switch the address for
1931 * @param ts time at which the check was initiated
1932 * @param address address of the other peer, NULL if other peer
1934 * @param session session to use (or NULL)
1935 * @param ats performance data
1936 * @param ats_count number of entries in ats (excluding 0-termination)
1939 check_blacklist (const struct GNUNET_PeerIdentity *peer,
1940 struct GNUNET_TIME_Absolute ts,
1941 const struct GNUNET_HELLO_Address *address,
1942 struct Session *session,
1943 const struct GNUNET_ATS_Information *ats,
1946 struct BlackListCheckContext *bcc;
1947 struct GST_BlacklistCheck *bc;
1950 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
1951 sizeof (struct GNUNET_ATS_Information) * ats_count);
1952 bcc->ats_count = ats_count;
1953 bcc->na.address = GNUNET_HELLO_address_copy (address);
1954 bcc->na.session = session;
1955 bcc->na.connect_timestamp = ts;
1956 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
1957 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1958 GNUNET_CONTAINER_DLL_insert (bc_head,
1961 if (NULL != (bc = GST_blacklist_test_allowed (peer,
1962 address->transport_name,
1963 &handle_test_blacklist_cont, bcc)))
1965 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
1966 we must only store 'bc' if 'bc' is non-NULL... */
1971 * We received a 'SESSION_CONNECT' message from the other peer.
1972 * Consider switching to it.
1974 * @param message possibly a 'struct SessionConnectMessage' (check format)
1975 * @param peer identity of the peer to switch the address for
1976 * @param address address of the other peer, NULL if other peer
1978 * @param session session to use (or NULL)
1979 * @param ats performance data
1980 * @param ats_count number of entries in ats (excluding 0-termination)
1983 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
1984 const struct GNUNET_PeerIdentity *peer,
1985 const struct GNUNET_HELLO_Address *address,
1986 struct Session *session,
1987 const struct GNUNET_ATS_Information *ats,
1990 const struct SessionConnectMessage *scm;
1991 struct NeighbourMapEntry *n;
1992 struct GNUNET_TIME_Absolute ts;
1994 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1995 "Received CONNECT message from peer `%s'\n",
1997 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
1999 GNUNET_break_op (0);
2002 if (NULL == neighbours)
2003 return; /* we're shutting down */
2004 scm = (const struct SessionConnectMessage *) message;
2005 GNUNET_break_op (0 == ntohl (scm->reserved));
2006 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2007 n = lookup_neighbour (peer);
2009 n = setup_neighbour (peer);
2010 n->send_connect_ack = 1;
2011 n->connect_ack_timestamp = ts;
2014 case S_NOT_CONNECTED:
2015 n->state = S_CONNECT_RECV_ATS;
2016 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2017 GNUNET_ATS_reset_backoff (GST_ats, peer);
2018 GNUNET_ATS_suggest_address (GST_ats, peer);
2019 check_blacklist (peer, ts, address, session, ats, ats_count);
2022 case S_INIT_BLACKLIST:
2023 case S_CONNECT_SENT:
2024 case S_CONNECT_RECV_ATS:
2025 case S_CONNECT_RECV_BLACKLIST:
2026 case S_CONNECT_RECV_ACK:
2027 /* It can never hurt to have an alternative address in the above cases,
2028 see if it is allowed */
2029 check_blacklist (peer, ts, address, session, ats, ats_count);
2032 /* we are already connected and can thus send the ACK immediately;
2033 still, it can never hurt to have an alternative address, so also
2034 tell ATS about it */
2035 GNUNET_assert (NULL != n->primary_address.address);
2036 GNUNET_assert (NULL != n->primary_address.session);
2037 n->send_connect_ack = 0;
2038 send_session_connect_ack_message (n->primary_address.address,
2039 n->primary_address.session, ts);
2040 check_blacklist (peer, ts, address, session, ats, ats_count);
2042 case S_RECONNECT_ATS:
2043 case S_RECONNECT_BLACKLIST:
2044 case S_RECONNECT_SENT:
2045 /* It can never hurt to have an alternative address in the above cases,
2046 see if it is allowed */
2047 check_blacklist (peer, ts, address, session, ats, ats_count);
2049 case S_CONNECTED_SWITCHING_BLACKLIST:
2050 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2051 /* we are already connected and can thus send the ACK immediately;
2052 still, it can never hurt to have an alternative address, so also
2053 tell ATS about it */
2054 GNUNET_assert (NULL != n->primary_address.address);
2055 GNUNET_assert (NULL != n->primary_address.session);
2056 n->send_connect_ack = 0;
2057 send_session_connect_ack_message (n->primary_address.address,
2058 n->primary_address.session, ts);
2059 check_blacklist (peer, ts, address, session, ats, ats_count);
2062 /* get rid of remains without terminating sessions, ready to re-try */
2063 free_neighbour (n, GNUNET_YES);
2064 n = setup_neighbour (peer);
2065 n->state = S_CONNECT_RECV_ATS;
2066 GNUNET_ATS_reset_backoff (GST_ats, peer);
2067 GNUNET_ATS_suggest_address (GST_ats, peer);
2069 case S_DISCONNECT_FINISHED:
2070 /* should not be possible */
2074 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2076 free_neighbour (n, GNUNET_NO);
2083 * For an existing neighbour record, set the active connection to
2084 * use the given address.
2086 * @param peer identity of the peer to switch the address for
2087 * @param address address of the other peer, NULL if other peer
2089 * @param session session to use (or NULL)
2090 * @param ats performance data
2091 * @param ats_count number of entries in ats
2092 * @param bandwidth_in inbound quota to be used when connection is up
2093 * @param bandwidth_out outbound quota to be used when connection is up
2096 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2097 const struct GNUNET_HELLO_Address *address,
2098 struct Session *session,
2099 const struct GNUNET_ATS_Information *ats,
2101 struct GNUNET_BANDWIDTH_Value32NBO
2103 struct GNUNET_BANDWIDTH_Value32NBO
2106 struct NeighbourMapEntry *n;
2107 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2109 GNUNET_assert (address->transport_name != NULL);
2110 if (NULL == (n = lookup_neighbour (peer)))
2113 /* Obtain an session for this address from plugin */
2114 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2116 /* we don't have the plugin for this address */
2117 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2120 if ((NULL == session) && (0 == address->address_length))
2123 if (strlen (address->transport_name) > 0)
2124 GNUNET_ATS_address_destroyed (GST_ats, address, session);
2127 if (NULL == session)
2128 session = papi->get_session (papi->cls, address);
2129 if (NULL == session)
2131 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2132 "Failed to obtain new session for peer `%s' and address '%s'\n",
2133 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2134 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2138 "ATS tells us to switch to address '%s' for peer `%s'\n",
2139 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2143 case S_NOT_CONNECTED:
2145 free_neighbour (n, GNUNET_NO);
2148 set_address (&n->primary_address,
2149 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2150 n->state = S_INIT_BLACKLIST;
2151 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2152 check_blacklist (&n->id,
2153 n->connect_ack_timestamp,
2154 address, session, ats, ats_count);
2156 case S_INIT_BLACKLIST:
2157 /* ATS suggests a different address, switch again */
2158 set_address (&n->primary_address,
2159 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2160 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2161 check_blacklist (&n->id,
2162 n->connect_ack_timestamp,
2163 address, session, ats, ats_count);
2165 case S_CONNECT_SENT:
2166 /* ATS suggests a different address, switch again */
2167 set_address (&n->primary_address,
2168 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2169 n->state = S_INIT_BLACKLIST;
2170 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2171 check_blacklist (&n->id,
2172 n->connect_ack_timestamp,
2173 address, session, ats, ats_count);
2175 case S_CONNECT_RECV_ATS:
2176 set_address (&n->primary_address,
2177 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2178 n->state = S_CONNECT_RECV_BLACKLIST;
2179 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2180 check_blacklist (&n->id,
2181 n->connect_ack_timestamp,
2182 address, session, ats, ats_count);
2184 case S_CONNECT_RECV_BLACKLIST:
2185 case S_CONNECT_RECV_ACK:
2186 /* ATS asks us to switch while we were trying to connect; switch to new
2187 address and check blacklist again */
2188 set_address (&n->primary_address,
2189 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2190 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2191 check_blacklist (&n->id,
2192 n->connect_ack_timestamp,
2193 address, session, ats, ats_count);
2196 GNUNET_assert (NULL != n->primary_address.address);
2197 GNUNET_assert (NULL != n->primary_address.session);
2198 if (n->primary_address.session == session)
2200 /* not an address change, just a quota change */
2201 set_address (&n->primary_address,
2202 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2205 /* ATS asks us to switch a life connection; see if we can get
2206 a CONNECT_ACK on it before we actually do this! */
2207 set_address (&n->alternative_address,
2208 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2209 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2210 check_blacklist (&n->id,
2211 GNUNET_TIME_absolute_get (),
2212 address, session, ats, ats_count);
2214 case S_RECONNECT_ATS:
2215 set_address (&n->primary_address,
2216 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2217 n->state = S_RECONNECT_BLACKLIST;
2218 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2219 check_blacklist (&n->id,
2220 n->connect_ack_timestamp,
2221 address, session, ats, ats_count);
2223 case S_RECONNECT_BLACKLIST:
2224 /* ATS asks us to switch while we were trying to reconnect; switch to new
2225 address and check blacklist again */
2226 set_address (&n->primary_address,
2227 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2228 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2229 check_blacklist (&n->id,
2230 n->connect_ack_timestamp,
2231 address, session, ats, ats_count);
2233 case S_RECONNECT_SENT:
2234 /* ATS asks us to switch while we were trying to reconnect; switch to new
2235 address and check blacklist again */
2236 set_address (&n->primary_address,
2237 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2238 n->state = S_RECONNECT_BLACKLIST;
2239 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2240 check_blacklist (&n->id,
2241 n->connect_ack_timestamp,
2242 address, session, ats, ats_count);
2244 case S_CONNECTED_SWITCHING_BLACKLIST:
2245 if (n->primary_address.session == session)
2247 /* ATS switches back to still-active session */
2248 free_address (&n->alternative_address);
2249 n->state = S_CONNECTED;
2252 /* ATS asks us to switch a life connection, update blacklist check */
2253 set_address (&n->alternative_address,
2254 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2255 check_blacklist (&n->id,
2256 GNUNET_TIME_absolute_get (),
2257 address, session, ats, ats_count);
2259 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2260 if (n->primary_address.session == session)
2262 /* ATS switches back to still-active session */
2263 free_address (&n->alternative_address);
2264 n->state = S_CONNECTED;
2267 /* ATS asks us to switch a life connection, update blacklist check */
2268 set_address (&n->alternative_address,
2269 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2270 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2271 check_blacklist (&n->id,
2272 GNUNET_TIME_absolute_get (),
2273 address, session, ats, ats_count);
2276 /* not going to switch addresses while disconnecting */
2278 case S_DISCONNECT_FINISHED:
2282 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2290 * Master task run for every neighbour. Performs all of the time-related
2291 * activities (keep alive, send next message, disconnect if idle, finish
2292 * clean up after disconnect).
2294 * @param cls the 'struct NeighbourMapEntry' for which we are running
2295 * @param tc scheduler context (unused)
2298 master_task (void *cls,
2299 const struct GNUNET_SCHEDULER_TaskContext *tc)
2301 struct NeighbourMapEntry *n = cls;
2302 struct GNUNET_TIME_Relative delay;
2304 n->task = GNUNET_SCHEDULER_NO_TASK;
2305 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2308 case S_NOT_CONNECTED:
2309 /* invalid state for master task, clean up */
2311 n->state = S_DISCONNECT_FINISHED;
2312 free_neighbour (n, GNUNET_NO);
2315 if (0 == delay.rel_value)
2317 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2318 "Connection to `%s' timed out waiting for ATS to provide address\n",
2319 GNUNET_i2s (&n->id));
2320 n->state = S_DISCONNECT_FINISHED;
2321 free_neighbour (n, GNUNET_NO);
2325 case S_INIT_BLACKLIST:
2326 if (0 == delay.rel_value)
2328 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2329 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2330 GNUNET_i2s (&n->id));
2331 n->state = S_DISCONNECT_FINISHED;
2332 free_neighbour (n, GNUNET_NO);
2336 case S_CONNECT_SENT:
2337 if (0 == delay.rel_value)
2339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2340 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2341 GNUNET_i2s (&n->id));
2342 disconnect_neighbour (n);
2346 case S_CONNECT_RECV_ATS:
2347 if (0 == delay.rel_value)
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2351 GNUNET_i2s (&n->id));
2352 n->state = S_DISCONNECT_FINISHED;
2353 free_neighbour (n, GNUNET_NO);
2357 case S_CONNECT_RECV_BLACKLIST:
2358 if (0 == delay.rel_value)
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2362 GNUNET_i2s (&n->id));
2363 n->state = S_DISCONNECT_FINISHED;
2364 free_neighbour (n, GNUNET_NO);
2368 case S_CONNECT_RECV_ACK:
2369 if (0 == delay.rel_value)
2371 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2372 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2373 GNUNET_i2s (&n->id));
2374 disconnect_neighbour (n);
2379 if (0 == delay.rel_value)
2381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2382 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2383 GNUNET_i2s (&n->id));
2384 disconnect_neighbour (n);
2387 try_transmission_to_peer (n);
2390 case S_RECONNECT_ATS:
2391 if (0 == delay.rel_value)
2393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2394 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2395 GNUNET_i2s (&n->id));
2396 disconnect_neighbour (n);
2400 case S_RECONNECT_BLACKLIST:
2401 if (0 == delay.rel_value)
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2405 GNUNET_i2s (&n->id));
2406 disconnect_neighbour (n);
2410 case S_RECONNECT_SENT:
2411 if (0 == delay.rel_value)
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2414 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2415 GNUNET_i2s (&n->id));
2416 disconnect_neighbour (n);
2420 case S_CONNECTED_SWITCHING_BLACKLIST:
2421 if (0 == delay.rel_value)
2423 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2424 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2425 GNUNET_i2s (&n->id));
2426 disconnect_neighbour (n);
2429 try_transmission_to_peer (n);
2432 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2433 if (0 == delay.rel_value)
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2436 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2437 GNUNET_i2s (&n->id));
2438 disconnect_neighbour (n);
2441 try_transmission_to_peer (n);
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2447 GNUNET_i2s (&n->id));
2448 n->state = S_DISCONNECT_FINISHED;
2449 free_neighbour (n, GNUNET_NO);
2451 case S_DISCONNECT_FINISHED:
2452 /* how did we get here!? */
2456 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2460 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2462 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2463 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2470 * Send a SESSION_ACK message to the neighbour to confirm that we
2471 * got his CONNECT_ACK.
2473 * @param n neighbour to send the SESSION_ACK to
2476 send_session_ack_message (struct NeighbourMapEntry *n)
2478 struct GNUNET_MessageHeader msg;
2480 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2481 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2482 (void) send_with_session(n,
2483 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2484 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2490 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2491 * Consider switching to it.
2493 * @param message possibly a 'struct SessionConnectMessage' (check format)
2494 * @param peer identity of the peer to switch the address for
2495 * @param address address of the other peer, NULL if other peer
2497 * @param session session to use (or NULL)
2498 * @param ats performance data
2499 * @param ats_count number of entries in ats
2502 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2503 const struct GNUNET_PeerIdentity *peer,
2504 const struct GNUNET_HELLO_Address *address,
2505 struct Session *session,
2506 const struct GNUNET_ATS_Information *ats,
2509 const struct SessionConnectMessage *scm;
2510 struct GNUNET_TIME_Absolute ts;
2511 struct NeighbourMapEntry *n;
2513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2514 "Received CONNECT_ACK message from peer `%s'\n",
2516 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2518 GNUNET_break_op (0);
2521 scm = (const struct SessionConnectMessage *) message;
2522 GNUNET_break_op (ntohl (scm->reserved) == 0);
2523 if (NULL == (n = lookup_neighbour (peer)))
2525 GNUNET_STATISTICS_update (GST_stats,
2527 ("# unexpected CONNECT_ACK messages (no peer)"),
2531 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2534 case S_NOT_CONNECTED:
2536 free_neighbour (n, GNUNET_NO);
2539 case S_INIT_BLACKLIST:
2540 GNUNET_STATISTICS_update (GST_stats,
2542 ("# unexpected CONNECT_ACK messages (not ready)"),
2545 case S_CONNECT_SENT:
2546 if (ts.abs_value != n->primary_address.connect_timestamp.abs_value)
2547 break; /* ACK does not match our original CONNECT message */
2548 n->state = S_CONNECTED;
2549 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2550 GNUNET_STATISTICS_set (GST_stats,
2551 gettext_noop ("# peers connected"),
2552 ++neighbours_connected,
2554 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2555 set_address (&n->primary_address,
2556 n->primary_address.address,
2557 n->primary_address.session,
2558 n->primary_address.bandwidth_in,
2559 n->primary_address.bandwidth_out,
2561 send_session_ack_message (n);
2563 case S_CONNECT_RECV_ATS:
2564 case S_CONNECT_RECV_BLACKLIST:
2565 case S_CONNECT_RECV_ACK:
2566 GNUNET_STATISTICS_update (GST_stats,
2568 ("# unexpected CONNECT_ACK messages (not ready)"),
2572 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2573 send_session_ack_message (n);
2575 case S_RECONNECT_ATS:
2576 case S_RECONNECT_BLACKLIST:
2577 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2578 to give us a new address... */
2579 GNUNET_STATISTICS_update (GST_stats,
2581 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2584 case S_RECONNECT_SENT:
2585 /* new address worked; go back to connected! */
2586 n->state = S_CONNECTED;
2587 send_session_ack_message (n);
2589 case S_CONNECTED_SWITCHING_BLACKLIST:
2590 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2591 send_session_ack_message (n);
2593 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2594 /* new address worked; adopt it and go back to connected! */
2595 n->state = S_CONNECTED;
2596 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2597 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2598 set_address (&n->primary_address,
2599 n->alternative_address.address,
2600 n->alternative_address.session,
2601 n->alternative_address.bandwidth_in,
2602 n->alternative_address.bandwidth_out,
2604 free_address (&n->alternative_address);
2605 send_session_ack_message (n);
2608 GNUNET_STATISTICS_update (GST_stats,
2610 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2613 case S_DISCONNECT_FINISHED:
2617 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2625 * A session was terminated. Take note; if needed, try to get
2626 * an alternative address from ATS.
2628 * @param peer identity of the peer where the session died
2629 * @param session session that is gone
2632 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2633 struct Session *session)
2635 struct NeighbourMapEntry *n;
2636 struct BlackListCheckContext *bcc;
2637 struct BlackListCheckContext *bcc_next;
2639 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2641 while (NULL != (bcc = bcc_next))
2643 bcc_next = bcc->next;
2644 if (bcc->na.session == session)
2646 GST_blacklist_test_cancel (bcc->bc);
2647 GNUNET_HELLO_address_free (bcc->na.address);
2648 GNUNET_CONTAINER_DLL_remove (bc_head,
2654 if (NULL == (n = lookup_neighbour (peer)))
2655 return; /* can't affect us */
2656 if (session != n->primary_address.session)
2658 if (session == n->alternative_address.session)
2660 free_address (&n->alternative_address);
2661 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2662 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2663 n->state = S_CONNECTED;
2667 return; /* doesn't affect us further */
2670 n->expect_latency_response = GNUNET_NO;
2674 case S_NOT_CONNECTED:
2676 free_neighbour (n, GNUNET_NO);
2680 free_neighbour (n, GNUNET_NO);
2682 case S_INIT_BLACKLIST:
2683 case S_CONNECT_SENT:
2684 free_address (&n->primary_address);
2685 n->state = S_INIT_ATS;
2686 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2687 // FIXME: need to ask ATS for suggestions again?
2688 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2690 case S_CONNECT_RECV_ATS:
2691 case S_CONNECT_RECV_BLACKLIST:
2692 case S_CONNECT_RECV_ACK:
2693 /* error on inbound session; free neighbour entirely */
2694 free_address (&n->primary_address);
2695 free_neighbour (n, GNUNET_NO);
2698 free_address (&n->primary_address);
2699 n->state = S_RECONNECT_ATS;
2700 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2701 /* FIXME: is this ATS call needed? */
2702 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2704 case S_RECONNECT_ATS:
2705 /* we don't have an address, how can it go down? */
2708 case S_RECONNECT_BLACKLIST:
2709 case S_RECONNECT_SENT:
2710 n->state = S_RECONNECT_ATS;
2711 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2712 // FIXME: need to ask ATS for suggestions again?
2713 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2715 case S_CONNECTED_SWITCHING_BLACKLIST:
2716 /* primary went down while we were checking secondary against
2717 blacklist, adopt secondary as primary */
2718 free_address (&n->primary_address);
2719 n->primary_address = n->alternative_address;
2720 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2721 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2722 n->state = S_RECONNECT_BLACKLIST;
2724 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2725 /* primary went down while we were waiting for CONNECT_ACK on secondary;
2726 secondary as primary */
2727 free_address (&n->primary_address);
2728 n->primary_address = n->alternative_address;
2729 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2730 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2731 n->state = S_RECONNECT_SENT;
2734 free_address (&n->primary_address);
2736 case S_DISCONNECT_FINISHED:
2737 /* neighbour was freed and plugins told to terminate session */
2740 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2744 if (GNUNET_SCHEDULER_NO_TASK != n->task)
2745 GNUNET_SCHEDULER_cancel (n->task);
2746 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2751 * We received a 'SESSION_ACK' message from the other peer.
2752 * If we sent a 'CONNECT_ACK' last, this means we are now
2753 * connected. Otherwise, do nothing.
2755 * @param message possibly a 'struct SessionConnectMessage' (check format)
2756 * @param peer identity of the peer to switch the address for
2757 * @param address address of the other peer, NULL if other peer
2759 * @param session session to use (or NULL)
2760 * @param ats performance data
2761 * @param ats_count number of entries in ats
2764 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
2765 const struct GNUNET_PeerIdentity *peer,
2766 const struct GNUNET_HELLO_Address *address,
2767 struct Session *session,
2768 const struct GNUNET_ATS_Information *ats,
2771 struct NeighbourMapEntry *n;
2773 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2774 "Received SESSION_ACK message from peer `%s'\n",
2776 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2778 GNUNET_break_op (0);
2781 if (NULL == (n = lookup_neighbour (peer)))
2783 /* check if we are in a plausible state for having sent
2784 a CONNECT_ACK. If not, return, otherwise break */
2785 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
2786 (S_CONNECT_SENT != n->state) ) ||
2787 (2 != n->send_connect_ack) )
2789 GNUNET_STATISTICS_update (GST_stats,
2790 gettext_noop ("# unexpected SESSION ACK messages"), 1,
2794 n->state = S_CONNECTED;
2795 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2796 GNUNET_STATISTICS_set (GST_stats,
2797 gettext_noop ("# peers connected"),
2798 ++neighbours_connected,
2800 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2801 set_address (&n->primary_address,
2802 n->primary_address.address,
2803 n->primary_address.session,
2804 n->primary_address.bandwidth_in,
2805 n->primary_address.bandwidth_out,
2811 * Test if we're connected to the given peer.
2813 * @param target peer to test
2814 * @return GNUNET_YES if we are connected, GNUNET_NO if not
2817 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
2819 return test_connected (lookup_neighbour (target));
2824 * Change the incoming quota for the given peer.
2826 * @param neighbour identity of peer to change qutoa for
2827 * @param quota new quota
2830 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2831 struct GNUNET_BANDWIDTH_Value32NBO quota)
2833 struct NeighbourMapEntry *n;
2835 if (NULL == (n = lookup_neighbour (neighbour)))
2837 GNUNET_STATISTICS_update (GST_stats,
2839 ("# SET QUOTA messages ignored (no such peer)"),
2843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2844 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2845 ntohl (quota.value__), GNUNET_i2s (&n->id));
2846 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2847 if (0 != ntohl (quota.value__))
2849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2850 GNUNET_i2s (&n->id), "SET_QUOTA");
2851 if (GNUNET_YES == test_connected (n))
2852 GNUNET_STATISTICS_update (GST_stats,
2853 gettext_noop ("# disconnects due to quota of 0"),
2855 disconnect_neighbour (n);
2860 * We received a disconnect message from the given peer,
2861 * validate and process.
2863 * @param peer sender of the message
2864 * @param msg the disconnect message
2867 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2869 const struct GNUNET_MessageHeader
2872 struct NeighbourMapEntry *n;
2873 const struct SessionDisconnectMessage *sdm;
2876 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2877 "Received DISCONNECT message from peer `%s'\n",
2879 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2881 // GNUNET_break_op (0);
2882 GNUNET_STATISTICS_update (GST_stats,
2884 ("# disconnect messages ignored (old format)"), 1,
2888 sdm = (const struct SessionDisconnectMessage *) msg;
2889 if (NULL == (n = lookup_neighbour (peer)))
2890 return; /* gone already */
2891 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <= n->connect_ack_timestamp.abs_value)
2893 GNUNET_STATISTICS_update (GST_stats,
2895 ("# disconnect messages ignored (timestamp)"), 1,
2899 GNUNET_CRYPTO_hash (&sdm->public_key,
2900 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2902 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2904 GNUNET_break_op (0);
2907 if (ntohl (sdm->purpose.size) !=
2908 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2909 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2910 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2912 GNUNET_break_op (0);
2916 GNUNET_CRYPTO_rsa_verify
2917 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2918 &sdm->signature, &sdm->public_key))
2920 GNUNET_break_op (0);
2923 if (GNUNET_YES == test_connected (n))
2924 GNUNET_STATISTICS_update (GST_stats,
2926 ("# other peer asked to disconnect from us"), 1,
2928 disconnect_neighbour (n);
2933 * Closure for the neighbours_iterate function.
2935 struct IteratorContext
2938 * Function to call on each connected neighbour.
2940 GST_NeighbourIterator cb;
2950 * Call the callback from the closure for each connected neighbour.
2952 * @param cls the 'struct IteratorContext'
2953 * @param key the hash of the public key of the neighbour
2954 * @param value the 'struct NeighbourMapEntry'
2955 * @return GNUNET_OK (continue to iterate)
2958 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2960 struct IteratorContext *ic = cls;
2961 struct NeighbourMapEntry *n = value;
2963 if (GNUNET_YES == test_connected (n))
2964 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->primary_address.address);
2970 * Iterate over all connected neighbours.
2972 * @param cb function to call
2973 * @param cb_cls closure for cb
2976 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2978 struct IteratorContext ic;
2980 if (NULL == neighbours)
2981 return; /* can happen during shutdown */
2984 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2989 * If we have an active connection to the given target, it must be shutdown.
2991 * @param target peer to disconnect from
2994 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2996 struct NeighbourMapEntry *n;
2998 if (NULL == (n = lookup_neighbour (target)))
2999 return; /* not active */
3000 if (GNUNET_YES == test_connected (n))
3001 GNUNET_STATISTICS_update (GST_stats,
3003 ("# disconnected from peer upon explicit request"), 1,
3005 disconnect_neighbour (n);
3010 * Obtain current latency information for the given neighbour.
3012 * @param peer to get the latency for
3013 * @return observed latency of the address, FOREVER if the
3014 * the connection is not up
3016 struct GNUNET_TIME_Relative
3017 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3019 struct NeighbourMapEntry *n;
3021 n = lookup_neighbour (peer);
3023 return GNUNET_TIME_UNIT_FOREVER_REL;
3027 case S_RECONNECT_SENT:
3028 case S_RECONNECT_ATS:
3030 case S_NOT_CONNECTED:
3031 case S_INIT_BLACKLIST:
3033 case S_CONNECT_SENT:
3034 case S_CONNECT_RECV_BLACKLIST:
3036 case S_DISCONNECT_FINISHED:
3037 return GNUNET_TIME_UNIT_FOREVER_REL;
3039 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3043 return GNUNET_TIME_UNIT_FOREVER_REL;
3048 * Obtain current address information for the given neighbour.
3051 * @return address currently used
3053 struct GNUNET_HELLO_Address *
3054 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3056 struct NeighbourMapEntry *n;
3058 n = lookup_neighbour (peer);
3061 return n->primary_address.address;
3066 * Initialize the neighbours subsystem.
3068 * @param cls closure for callbacks
3069 * @param connect_cb function to call if we connect to a peer
3070 * @param disconnect_cb function to call if we disconnect from a peer
3071 * @param peer_address_cb function to call if we change an active address
3075 GST_neighbours_start (void *cls,
3076 GNUNET_TRANSPORT_NotifyConnect connect_cb,
3077 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3078 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
3081 connect_notify_cb = connect_cb;
3082 disconnect_notify_cb = disconnect_cb;
3083 address_change_cb = peer_address_cb;
3084 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
3089 * Disconnect from the given neighbour.
3092 * @param key hash of neighbour's public key (not used)
3093 * @param value the 'struct NeighbourMapEntry' of the neighbour
3094 * @return GNUNET_OK (continue to iterate)
3097 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
3099 struct NeighbourMapEntry *n = value;
3101 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3102 "Disconnecting peer `%4s', %s\n",
3103 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3104 n->state = S_DISCONNECT_FINISHED;
3105 free_neighbour (n, GNUNET_NO);
3111 * Cleanup the neighbours subsystem.
3114 GST_neighbours_stop ()
3116 if (NULL == neighbours)
3118 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
3119 &disconnect_all_neighbours,
3121 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
3123 callback_cls = NULL;
3124 connect_notify_cb = NULL;
3125 disconnect_notify_cb = NULL;
3126 address_change_cb = NULL;
3130 /* end of file gnunet-service-transport_neighbours.c */