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 (S_CONNECT_RECV_BLACKLIST_INBOUND).
225 * If this check is successful, we give the inbound address to ATS.
226 * After the check we ask ATS for a suggestion (S_CONNECT_RECV_ATS).
227 * If ATS makes a suggestion, 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, do a blacklist check for inbound address
307 S_CONNECT_RECV_BLACKLIST_INBOUND,
310 * Received a CONNECT, asking ATS about address suggestions.
315 * Received CONNECT from other peer, got an address, checking with blacklist.
317 S_CONNECT_RECV_BLACKLIST,
320 * CONNECT request from other peer was SESSION_ACK'ed, waiting for
326 * Got our CONNECT_ACK/SESSION_ACK, connection is up.
331 * Connection got into trouble, rest of the system still believes
332 * it to be up, but we're getting a new address from ATS.
337 * Connection got into trouble, rest of the system still believes
338 * it to be up; we are checking the new address against the blacklist.
340 S_RECONNECT_BLACKLIST,
343 * Sent CONNECT over new address (either by ATS telling us to switch
344 * addresses or from RECONNECT_ATS); if this fails, we need to tell
345 * the rest of the system about a disconnect.
350 * We have some primary connection, but ATS suggested we switch
351 * to some alternative; we're now checking the alternative against
354 S_CONNECTED_SWITCHING_BLACKLIST,
357 * We have some primary connection, but ATS suggested we switch
358 * to some alternative; we now sent a CONNECT message for the
359 * alternative session to the other peer and waiting for a
360 * CONNECT_ACK to make this our primary connection.
362 S_CONNECTED_SWITCHING_CONNECT_SENT,
365 * Disconnect in progress (we're sending the DISCONNECT message to the
366 * other peer; after that is finished, the state will be cleaned up).
371 * We're finished with the disconnect; clean up state now!
373 S_DISCONNECT_FINISHED
378 * A possible address we could use to communicate with a neighbour.
380 struct NeighbourAddress
384 * Active session for this address.
386 struct Session *session;
389 * Network-level address information.
391 struct GNUNET_HELLO_Address *address;
394 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
395 * peer for this address. Use to check that the ACK is in response
396 * to our most recent 'CONNECT'.
398 struct GNUNET_TIME_Absolute connect_timestamp;
401 * Inbound bandwidth from ATS for this address.
403 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
406 * Outbound bandwidth from ATS for this address.
408 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
411 * Did we tell ATS that this is our 'active' address?
419 * Entry in neighbours.
421 struct NeighbourMapEntry
425 * Head of list of messages we would like to send to this peer;
426 * must contain at most one message per client.
428 struct MessageQueue *messages_head;
431 * Tail of list of messages we would like to send to this peer; must
432 * contain at most one message per client.
434 struct MessageQueue *messages_tail;
437 * Are we currently trying to send a message? If so, which one?
439 struct MessageQueue *is_active;
442 * Primary address we currently use to communicate with the neighbour.
444 struct NeighbourAddress primary_address;
447 * Alternative address currently under consideration for communicating
448 * with the neighbour.
450 struct NeighbourAddress alternative_address;
453 * Identity of this neighbour.
455 struct GNUNET_PeerIdentity id;
458 * Main task that drives this peer (timeouts, keepalives, etc.).
459 * Always runs the 'master_task'.
461 GNUNET_SCHEDULER_TaskIdentifier task;
464 * At what time should we sent the next keep-alive message?
466 struct GNUNET_TIME_Absolute keep_alive_time;
469 * At what time did we sent the last keep-alive message? Used
470 * to calculate round-trip time ("latency").
472 struct GNUNET_TIME_Absolute last_keep_alive_time;
475 * Timestamp we should include in our next CONNECT_ACK message.
476 * (only valid if 'send_connect_ack' is GNUNET_YES). Used to build
477 * our CONNECT_ACK message.
479 struct GNUNET_TIME_Absolute connect_ack_timestamp;
482 * Time where we should cut the connection (timeout) if we don't
483 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
484 * if we are in S_CONNECTED).
486 struct GNUNET_TIME_Absolute timeout;
489 * Latest calculated latency value
491 struct GNUNET_TIME_Relative latency;
494 * Tracker for inbound bandwidth.
496 struct GNUNET_BANDWIDTH_Tracker in_tracker;
499 * How often has the other peer (recently) violated the inbound
500 * traffic limit? Incremented by 10 per violation, decremented by 1
501 * per non-violation (for each time interval).
503 unsigned int quota_violation_count;
506 * The current state of the peer.
511 * Did we sent an KEEP_ALIVE message and are we expecting a response?
513 int expect_latency_response;
516 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
517 * (once we have an address to use and the peer has been allowed by our
518 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
519 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
520 * a 'SESSION_ACK' (regardless of what our own state machine might say).
522 int send_connect_ack;
528 * Context for blacklist checks and the 'handle_test_blacklist_cont'
529 * function. Stores information about ongoing blacklist checks.
531 struct BlackListCheckContext
535 * We keep blacklist checks in a DLL.
537 struct BlackListCheckContext *next;
540 * We keep blacklist checks in a DLL.
542 struct BlackListCheckContext *prev;
545 * Address that is being checked.
547 struct NeighbourAddress na;
550 * ATS information about the address.
552 struct GNUNET_ATS_Information *ats;
555 * Handle to the ongoing blacklist check.
557 struct GST_BlacklistCheck *bc;
560 * Size of the 'ats' array.
568 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
570 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
573 * We keep blacklist checks in a DLL so that we can find
574 * the 'sessions' in their 'struct NeighbourAddress' if
575 * a session goes down.
577 static struct BlackListCheckContext *bc_head;
580 * We keep blacklist checks in a DLL.
582 static struct BlackListCheckContext *bc_tail;
585 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
587 static void *callback_cls;
590 * Function to call when we connected to a neighbour.
592 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
595 * Function to call when we disconnected from a neighbour.
597 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
600 * Function to call when we changed an active address of a neighbour.
602 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
605 * counter for connected neighbours
607 static unsigned int neighbours_connected;
610 * Number of bytes we have currently queued for transmission.
612 static unsigned long long bytes_in_send_queue;
616 * Lookup a neighbour entry in the neighbours hash map.
618 * @param pid identity of the peer to look up
619 * @return the entry, NULL if there is no existing record
621 static struct NeighbourMapEntry *
622 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
624 if (NULL == neighbours)
626 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
630 print_state (int state)
635 case S_NOT_CONNECTED:
636 return "S_NOT_CONNECTED";
641 case S_INIT_BLACKLIST:
642 return "S_INIT_BLACKLIST";
645 return "S_CONNECT_SENT";
647 case S_CONNECT_RECV_BLACKLIST_INBOUND:
648 return "S_CONNECT_RECV_BLACKLIST_INBOUND";
650 case S_CONNECT_RECV_ATS:
651 return "S_CONNECT_RECV_ATS";
653 case S_CONNECT_RECV_BLACKLIST:
654 return "S_CONNECT_RECV_BLACKLIST";
656 case S_CONNECT_RECV_ACK:
657 return "S_CONNECT_RECV_ACK";
660 return "S_CONNECTED";
662 case S_RECONNECT_ATS:
663 return "S_RECONNECT_ATS";
665 case S_RECONNECT_BLACKLIST:
666 return "S_RECONNECT_BLACKLIST";
668 case S_RECONNECT_SENT:
669 return "S_RECONNECT_SENT";
671 case S_CONNECTED_SWITCHING_BLACKLIST:
672 return "S_CONNECTED_SWITCHING_BLACKLIST";
674 case S_CONNECTED_SWITCHING_CONNECT_SENT:
675 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
678 return "S_DISCONNECT";
680 case S_DISCONNECT_FINISHED:
681 return "S_DISCONNECT_FINISHED";
693 * Test if we're connected to the given peer.
695 * @param n neighbour entry of peer to test
696 * @return GNUNET_YES if we are connected, GNUNET_NO if not
699 test_connected (struct NeighbourMapEntry *n)
705 case S_NOT_CONNECTED:
707 case S_INIT_BLACKLIST:
709 case S_CONNECT_RECV_BLACKLIST_INBOUND:
710 case S_CONNECT_RECV_ATS:
711 case S_CONNECT_RECV_BLACKLIST:
712 case S_CONNECT_RECV_ACK:
715 case S_RECONNECT_ATS:
716 case S_RECONNECT_BLACKLIST:
717 case S_RECONNECT_SENT:
718 case S_CONNECTED_SWITCHING_BLACKLIST:
719 case S_CONNECTED_SWITCHING_CONNECT_SENT:
722 case S_DISCONNECT_FINISHED:
725 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
729 return GNUNET_SYSERR;
733 * Send information about a new outbound quota to our clients.
735 * @param target affected peer
736 * @param quota new quota
739 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
740 struct GNUNET_BANDWIDTH_Value32NBO quota)
742 struct QuotaSetMessage q_msg;
744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
745 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
746 ntohl (quota.value__), GNUNET_i2s (target));
747 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
748 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
750 q_msg.peer = (*target);
751 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
756 * We don't need a given neighbour address any more.
757 * Release its resources and give appropriate notifications
758 * to ATS and other subsystems.
760 * @param na address we are done with; 'na' itself must NOT be 'free'd, only the contents!
763 free_address (struct NeighbourAddress *na)
765 if (GNUNET_YES == na->ats_active)
767 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
768 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
769 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
773 if (NULL != na->address)
775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "HACK: destroy address\n");
776 GNUNET_ATS_address_destroyed (GST_ats, na->address, na->session);
779 na->ats_active = GNUNET_NO;
780 if (NULL != na->address)
782 GNUNET_HELLO_address_free (na->address);
790 * Initialize the 'struct NeighbourAddress'.
792 * @param na neighbour address to initialize
793 * @param address address of the other peer, NULL if other peer
795 * @param session session to use (or NULL, in which case an
796 * address must be setup)
797 * @param bandwidth_in inbound quota to be used when connection is up
798 * @param bandwidth_out outbound quota to be used when connection is up
799 * @param is_active GNUNET_YES to mark this as the active address with ATS
802 set_address (struct NeighbourAddress *na,
803 const struct GNUNET_HELLO_Address *address,
804 struct Session *session,
805 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
806 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
809 struct GNUNET_TRANSPORT_PluginFunctions *papi;
811 if (NULL == (papi = GST_plugins_find (address->transport_name)))
816 if (session == na->session)
818 na->bandwidth_in = bandwidth_in;
819 na->bandwidth_out = bandwidth_out;
820 if (is_active != na->ats_active)
822 na->ats_active = is_active;
823 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
824 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
826 if (GNUNET_YES == is_active)
828 /* FIXME: is this the right place to set quotas? */
829 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
830 send_outbound_quota (&address->peer, bandwidth_out);
836 session = papi->get_session (papi->cls, address);
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
840 "Failed to obtain new session for peer `%s' and address '%s'\n",
841 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
842 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
845 na->address = GNUNET_HELLO_address_copy (address);
846 na->bandwidth_in = bandwidth_in;
847 na->bandwidth_out = bandwidth_out;
848 na->session = session;
849 na->ats_active = is_active;
850 if (GNUNET_YES == is_active)
852 /* Telling ATS about new session */
853 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
854 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
856 /* FIXME: is this the right place to set quotas? */
857 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
858 send_outbound_quota (&address->peer, bandwidth_out);
864 * Free a neighbour map entry.
866 * @param n entry to free
867 * @param keep_sessions GNUNET_NO to tell plugin to terminate sessions,
868 * GNUNET_YES to keep all sessions
871 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
873 struct MessageQueue *mq;
874 struct GNUNET_TRANSPORT_PluginFunctions *papi;
876 n->is_active = NULL; /* always free'd by its own continuation! */
878 /* fail messages currently in the queue */
879 while (NULL != (mq = n->messages_head))
881 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
882 if (NULL != mq->cont)
883 mq->cont (mq->cont_cls, GNUNET_SYSERR);
886 /* It is too late to send other peer disconnect notifications, but at
887 least internally we need to get clean... */
888 if (GNUNET_YES == test_connected (n))
890 GNUNET_STATISTICS_set (GST_stats,
891 gettext_noop ("# peers connected"),
892 --neighbours_connected,
894 disconnect_notify_cb (callback_cls, &n->id);
897 /* FIXME-PLUGIN-API: This does not seem to guarantee that all
898 transport sessions eventually get killed due to inactivity; they
899 MUST have their own timeout logic (but at least TCP doesn't have
900 one yet). Are we sure that EVERY 'session' of a plugin is
901 actually cleaned up this way!? Note that if we are switching
902 between two TCP sessions to the same peer, the existing plugin
903 API gives us not even the means to selectively kill only one of
904 them! Killing all sessions like this seems to be very, very
906 if ((GNUNET_NO == keep_sessions) &&
907 (NULL != n->primary_address.address) &&
908 (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name))))
909 papi->disconnect (papi->cls, &n->id);
911 n->state = S_DISCONNECT_FINISHED;
913 GNUNET_assert (GNUNET_YES ==
914 GNUNET_CONTAINER_multihashmap_remove (neighbours,
915 &n->id.hashPubKey, n));
917 /* cut transport-level connection */
918 free_address (&n->primary_address);
919 free_address (&n->alternative_address);
921 // FIXME-ATS-API: we might want to be more specific about
922 // which states we do this from in the future (ATS should
923 // have given us a 'suggest_address' handle, and if we have
924 // such a handle, we should cancel the operation here!
925 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
927 if (GNUNET_SCHEDULER_NO_TASK != n->task)
929 GNUNET_SCHEDULER_cancel (n->task);
930 n->task = GNUNET_SCHEDULER_NO_TASK;
932 /* free rest of memory */
938 * Transmit a message using the current session of the given
941 * @param n entry for the recipient
942 * @param msgbuf buffer to transmit
943 * @param msgbuf_size number of bytes in buffer
944 * @param priority transmission priority
945 * @param timeout transmission timeout
946 * @param cont continuation to call when finished (can be NULL)
947 * @param cont_cls closure for cont
950 send_with_session (struct NeighbourMapEntry *n,
951 const char *msgbuf, size_t msgbuf_size,
953 struct GNUNET_TIME_Relative timeout,
954 GNUNET_TRANSPORT_TransmitContinuation cont,
957 struct GNUNET_TRANSPORT_PluginFunctions *papi;
959 GNUNET_assert (n->primary_address.session != NULL);
960 if ( ( (NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name))) ||
961 (-1 == papi->send (papi->cls,
962 n->primary_address.session,
968 cont (cont_cls, &n->id, GNUNET_SYSERR);
969 GNUNET_break (NULL != papi);
974 * Master task run for every neighbour. Performs all of the time-related
975 * activities (keep alive, send next message, disconnect if idle, finish
976 * clean up after disconnect).
978 * @param cls the 'struct NeighbourMapEntry' for which we are running
979 * @param tc scheduler context (unused)
982 master_task (void *cls,
983 const struct GNUNET_SCHEDULER_TaskContext *tc);
987 * Function called when the 'DISCONNECT' message has been sent by the
988 * plugin. Frees the neighbour --- if the entry still exists.
991 * @param target identity of the neighbour that was disconnected
992 * @param result GNUNET_OK if the disconnect got out successfully
995 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
998 struct NeighbourMapEntry *n;
1000 n = lookup_neighbour (target);
1002 return; /* already gone */
1003 if (S_DISCONNECT != n->state)
1004 return; /* have created a fresh entry since */
1005 n->state = S_DISCONNECT;
1006 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1007 GNUNET_SCHEDULER_cancel (n->task);
1008 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1013 * Transmit a DISCONNECT message to the other peer.
1015 * @param n neighbour to send DISCONNECT message.
1018 send_disconnect (struct NeighbourMapEntry *n)
1020 struct SessionDisconnectMessage disconnect_msg;
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1023 "Sending DISCONNECT message to peer `%4s'\n",
1024 GNUNET_i2s (&n->id));
1025 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1026 disconnect_msg.header.type =
1027 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1028 disconnect_msg.reserved = htonl (0);
1029 disconnect_msg.purpose.size =
1030 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1031 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1032 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1033 disconnect_msg.purpose.purpose =
1034 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1035 disconnect_msg.timestamp =
1036 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1037 disconnect_msg.public_key = GST_my_public_key;
1038 GNUNET_assert (GNUNET_OK ==
1039 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1040 &disconnect_msg.purpose,
1041 &disconnect_msg.signature));
1043 send_with_session (n,
1044 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1045 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1046 &send_disconnect_cont, NULL);
1047 GNUNET_STATISTICS_update (GST_stats,
1049 ("# DISCONNECT messages sent"), 1,
1055 * Disconnect from the given neighbour, clean up the record.
1057 * @param n neighbour to disconnect from
1060 disconnect_neighbour (struct NeighbourMapEntry *n)
1062 /* depending on state, notify neighbour and/or upper layers of this peer
1066 case S_NOT_CONNECTED:
1068 case S_INIT_BLACKLIST:
1069 /* other peer is completely unaware of us, no need to send DISCONNECT */
1070 n->state = S_DISCONNECT_FINISHED;
1071 free_neighbour (n, GNUNET_NO);
1073 case S_CONNECT_SENT:
1074 send_disconnect (n);
1075 n->state = S_DISCONNECT;
1077 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1078 case S_CONNECT_RECV_ATS:
1079 case S_CONNECT_RECV_BLACKLIST:
1080 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1081 n->state = S_DISCONNECT_FINISHED;
1082 free_neighbour (n, GNUNET_NO);
1084 case S_CONNECT_RECV_ACK:
1085 /* we DID ACK the other peer's request, must send DISCONNECT */
1086 send_disconnect (n);
1087 n->state = S_DISCONNECT;
1090 case S_RECONNECT_BLACKLIST:
1091 case S_RECONNECT_SENT:
1092 case S_CONNECTED_SWITCHING_BLACKLIST:
1093 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1094 /* we are currently connected, need to send disconnect and do
1095 internal notifications and update statistics */
1096 send_disconnect (n);
1097 GNUNET_STATISTICS_set (GST_stats,
1098 gettext_noop ("# peers connected"),
1099 --neighbours_connected,
1101 disconnect_notify_cb (callback_cls, &n->id);
1102 n->state = S_DISCONNECT;
1104 case S_RECONNECT_ATS:
1105 /* ATS address request timeout, disconnect without sending disconnect message */
1106 GNUNET_STATISTICS_set (GST_stats,
1107 gettext_noop ("# peers connected"),
1108 --neighbours_connected,
1110 disconnect_notify_cb (callback_cls, &n->id);
1111 n->state = S_DISCONNECT;
1114 /* already disconnected, ignore */
1116 case S_DISCONNECT_FINISHED:
1117 /* already cleaned up, how did we get here!? */
1121 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1125 /* schedule timeout to clean up */
1126 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1127 GNUNET_SCHEDULER_cancel (n->task);
1128 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1134 * We're done with our transmission attempt, continue processing.
1136 * @param cls the 'struct MessageQueue' of the message
1137 * @param receiver intended receiver
1138 * @param success whether it worked or not
1141 transmit_send_continuation (void *cls,
1142 const struct GNUNET_PeerIdentity *receiver,
1145 struct MessageQueue *mq = cls;
1146 struct NeighbourMapEntry *n;
1148 if (NULL == (n = lookup_neighbour (receiver)))
1151 return; /* disconnect or other error while transmitting, can happen */
1153 if (n->is_active == mq)
1155 /* this is still "our" neighbour, remove us from its queue
1156 and allow it to send the next message now */
1157 n->is_active = NULL;
1158 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1159 GNUNET_SCHEDULER_cancel (n->task);
1160 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1162 GNUNET_assert (bytes_in_send_queue >= mq->message_buf_size);
1163 bytes_in_send_queue -= mq->message_buf_size;
1164 GNUNET_STATISTICS_set (GST_stats,
1166 ("# bytes in message queue for other peers"),
1167 bytes_in_send_queue, GNUNET_NO);
1168 if (GNUNET_OK == success)
1169 GNUNET_STATISTICS_update (GST_stats,
1171 ("# messages transmitted to other peers"),
1174 GNUNET_STATISTICS_update (GST_stats,
1176 ("# transmission failures for messages to other peers"),
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 "Sending message to `%s' of type %u was a %s\n",
1180 GNUNET_i2s (receiver),
1181 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1182 (success == GNUNET_OK) ? "success" : "FAILURE");
1183 if (NULL != mq->cont)
1184 mq->cont (mq->cont_cls, success);
1190 * Check the message list for the given neighbour and if we can
1191 * send a message, do so. This function should only be called
1192 * if the connection is at least generally ready for transmission.
1193 * While we will only send one message at a time, no bandwidth
1194 * quota management is performed here. If a message was given to
1195 * the plugin, the continuation will automatically re-schedule
1196 * the 'master' task once the next message might be transmitted.
1198 * @param n target peer for which to transmit
1201 try_transmission_to_peer (struct NeighbourMapEntry *n)
1203 struct MessageQueue *mq;
1204 struct GNUNET_TIME_Relative timeout;
1206 if (NULL == n->primary_address.address)
1208 /* no address, why are we here? */
1212 if ((0 == n->primary_address.address->address_length) &&
1213 (NULL == n->primary_address.session))
1215 /* no address, why are we here? */
1219 if (NULL != n->is_active)
1221 /* transmission already pending */
1225 /* timeout messages from the queue that are past their due date */
1226 while (NULL != (mq = n->messages_head))
1228 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1229 if (timeout.rel_value > 0)
1231 GNUNET_STATISTICS_update (GST_stats,
1233 ("# messages timed out while in transport queue"),
1235 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1237 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
1240 return; /* no more messages */
1241 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1243 send_with_session (n,
1244 mq->message_buf, mq->message_buf_size,
1245 0 /* priority */, timeout,
1246 &transmit_send_continuation, mq);
1251 * Send keepalive message to the neighbour. Must only be called
1252 * if we are on 'connected' state. Will internally determine
1253 * if a keepalive is truly needed (so can always be called).
1255 * @param n neighbour that went idle and needs a keepalive
1258 send_keepalive (struct NeighbourMapEntry *n)
1260 struct GNUNET_MessageHeader m;
1262 GNUNET_assert (S_CONNECTED == n->state);
1263 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value > 0)
1264 return; /* no keepalive needed at this time */
1265 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1266 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1267 send_with_session (n,
1268 (const void *) &m, sizeof (m),
1269 UINT32_MAX /* priority */,
1270 KEEPALIVE_FREQUENCY,
1272 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1274 n->expect_latency_response = GNUNET_YES;
1275 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1276 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1281 * Keep the connection to the given neighbour alive longer,
1282 * we received a KEEPALIVE (or equivalent); send a response.
1284 * @param neighbour neighbour to keep alive (by sending keep alive response)
1287 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1289 struct NeighbourMapEntry *n;
1290 struct GNUNET_MessageHeader m;
1292 if (NULL == (n = lookup_neighbour (neighbour)))
1294 GNUNET_STATISTICS_update (GST_stats,
1296 ("# KEEPALIVE messages discarded (peer unknown)"),
1300 if (NULL == n->primary_address.session)
1302 GNUNET_STATISTICS_update (GST_stats,
1304 ("# KEEPALIVE messages discarded (no session)"),
1308 /* send reply to allow neighbour to measure latency */
1309 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1310 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1311 send_with_session(n,
1312 (const void *) &m, sizeof (m),
1313 UINT32_MAX /* priority */,
1314 KEEPALIVE_FREQUENCY,
1320 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1321 * latency to this peer. Pass the updated information (existing ats
1322 * plus calculated latency) to ATS.
1324 * @param neighbour neighbour to keep alive
1325 * @param ats performance data
1326 * @param ats_count number of entries in ats
1329 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1330 const struct GNUNET_ATS_Information *ats,
1333 struct NeighbourMapEntry *n;
1335 struct GNUNET_ATS_Information ats_new[ats_count + 1];
1337 if (NULL == (n = lookup_neighbour (neighbour)))
1339 GNUNET_STATISTICS_update (GST_stats,
1341 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1345 if ( (S_CONNECTED != n->state) ||
1346 (GNUNET_YES != n->expect_latency_response) )
1348 GNUNET_STATISTICS_update (GST_stats,
1350 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1354 n->expect_latency_response = GNUNET_NO;
1355 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1356 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1358 "Latency for peer `%s' is %llu ms\n",
1359 GNUNET_i2s (&n->id), n->latency.rel_value);
1360 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1361 /* append latency */
1362 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1363 if (n->latency.rel_value > UINT32_MAX)
1364 latency = UINT32_MAX;
1366 latency = n->latency.rel_value;
1367 ats_new[ats_count].value = htonl (latency);
1368 GNUNET_ATS_address_update (GST_ats,
1369 n->primary_address.address,
1370 n->primary_address.session, ats_new,
1376 * We have received a message from the given sender. How long should
1377 * we delay before receiving more? (Also used to keep the peer marked
1380 * @param sender sender of the message
1381 * @param size size of the message
1382 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1383 * GNUNET_NO if the neighbour is not connected or violates the quota,
1384 * GNUNET_SYSERR if the connection is not fully up yet
1385 * @return how long to wait before reading more from this sender
1387 struct GNUNET_TIME_Relative
1388 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1389 *sender, ssize_t size, int *do_forward)
1391 struct NeighbourMapEntry *n;
1392 struct GNUNET_TIME_Relative ret;
1394 if (NULL == neighbours)
1396 *do_forward = GNUNET_NO;
1397 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1399 if (NULL == (n = lookup_neighbour (sender)))
1401 GST_neighbours_try_connect (sender);
1402 if (NULL == (n = lookup_neighbour (sender)))
1404 GNUNET_STATISTICS_update (GST_stats,
1406 ("# messages discarded due to lack of neighbour record"),
1408 *do_forward = GNUNET_NO;
1409 return GNUNET_TIME_UNIT_ZERO;
1412 if (! test_connected (n))
1414 *do_forward = GNUNET_SYSERR;
1415 return GNUNET_TIME_UNIT_ZERO;
1417 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1419 n->quota_violation_count++;
1420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1421 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1422 n->in_tracker.available_bytes_per_s__,
1423 n->quota_violation_count);
1424 /* Discount 32k per violation */
1425 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1429 if (n->quota_violation_count > 0)
1431 /* try to add 32k back */
1432 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1433 n->quota_violation_count--;
1436 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1438 GNUNET_STATISTICS_update (GST_stats,
1440 ("# bandwidth quota violations by other peers"),
1442 *do_forward = GNUNET_NO;
1443 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1445 *do_forward = GNUNET_YES;
1446 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1447 if (ret.rel_value > 0)
1449 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1450 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1451 (unsigned long long) n->in_tracker.
1452 consumption_since_last_update__,
1453 (unsigned int) n->in_tracker.available_bytes_per_s__,
1454 (unsigned long long) ret.rel_value);
1455 GNUNET_STATISTICS_update (GST_stats,
1456 gettext_noop ("# ms throttling suggested"),
1457 (int64_t) ret.rel_value, GNUNET_NO);
1464 * Transmit a message to the given target using the active connection.
1466 * @param target destination
1467 * @param msg message to send
1468 * @param msg_size number of bytes in msg
1469 * @param timeout when to fail with timeout
1470 * @param cont function to call when done
1471 * @param cont_cls closure for 'cont'
1474 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1475 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1476 GST_NeighbourSendContinuation cont, void *cont_cls)
1478 struct NeighbourMapEntry *n;
1479 struct MessageQueue *mq;
1481 /* All ove these cases should never happen; they are all API violations.
1482 But we check anyway, just to be sure. */
1483 if (NULL == (n = lookup_neighbour (target)))
1487 cont (cont_cls, GNUNET_SYSERR);
1490 if (GNUNET_YES != test_connected (n))
1494 cont (cont_cls, GNUNET_SYSERR);
1497 bytes_in_send_queue += msg_size;
1498 GNUNET_STATISTICS_set (GST_stats,
1500 ("# bytes in message queue for other peers"),
1501 bytes_in_send_queue, GNUNET_NO);
1502 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1504 mq->cont_cls = cont_cls;
1505 memcpy (&mq[1], msg, msg_size);
1506 mq->message_buf = (const char *) &mq[1];
1507 mq->message_buf_size = msg_size;
1508 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1509 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1510 if ( (NULL != n->is_active) ||
1511 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1513 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1514 GNUNET_SCHEDULER_cancel (n->task);
1515 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1520 * Send a SESSION_CONNECT message via the given address.
1522 * @param na address to use
1525 send_session_connect (struct NeighbourAddress *na)
1527 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1528 struct SessionConnectMessage connect_msg;
1530 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1535 if (NULL == na->session)
1536 na->session = papi->get_session (papi->cls, na->address);
1537 if (NULL == na->session)
1542 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1543 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1544 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1545 connect_msg.reserved = htonl (0);
1546 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1547 (void) papi->send (papi->cls,
1549 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1551 GNUNET_TIME_UNIT_FOREVER_REL,
1557 * Send a SESSION_CONNECT_ACK message via the given address.
1559 * @param address address to use
1560 * @param session session to use
1561 * @param timestamp timestamp to use for the ACK message
1564 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1565 struct Session *session,
1566 struct GNUNET_TIME_Absolute timestamp)
1568 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1569 struct SessionConnectMessage connect_msg;
1571 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1576 if (NULL == session)
1577 session = papi->get_session (papi->cls, address);
1578 if (NULL == session)
1583 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1584 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1585 connect_msg.reserved = htonl (0);
1586 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1587 (void) papi->send (papi->cls,
1589 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1591 GNUNET_TIME_UNIT_FOREVER_REL,
1597 * Create a fresh entry in the neighbour map for the given peer
1599 * @param peer peer to create an entry for
1600 * @return new neighbour map entry
1602 static struct NeighbourMapEntry *
1603 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1605 struct NeighbourMapEntry *n;
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Creating new neighbour entry for `%s'\n",
1610 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1612 n->state = S_NOT_CONNECTED;
1613 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1614 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1615 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1616 MAX_BANDWIDTH_CARRY_S);
1617 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1618 GNUNET_assert (GNUNET_OK ==
1619 GNUNET_CONTAINER_multihashmap_put (neighbours,
1620 &n->id.hashPubKey, n,
1621 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1627 * Check if the two given addresses are the same.
1628 * Actually only checks if the sessions are non-NULL
1629 * (which they should be) and then if they are identical;
1630 * the actual addresses don't matter if the session
1631 * pointers match anyway, and we must have session pointers
1634 * @param a1 first address to compare
1635 * @param a2 other address to compare
1636 * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
1639 address_matches (const struct NeighbourAddress *a1,
1640 const struct NeighbourAddress *a2)
1642 if ( (NULL == a1->session) ||
1643 (NULL == a2->session) )
1648 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1653 * Try to create a connection to the given target (eventually).
1655 * @param target peer to try to connect to
1658 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1660 struct NeighbourMapEntry *n;
1662 if (NULL == neighbours)
1663 return; /* during shutdown, do nothing */
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Asked to connect to peer `%s'\n",
1666 GNUNET_i2s (target));
1668 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1670 /* refuse to connect to myself */
1671 /* FIXME: can this happen? Is this not an API violation? */
1672 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1673 "Refusing to try to connect to myself.\n");
1676 n = lookup_neighbour (target);
1681 case S_NOT_CONNECTED:
1682 /* this should not be possible */
1684 free_neighbour (n, GNUNET_NO);
1687 case S_INIT_BLACKLIST:
1688 case S_CONNECT_SENT:
1689 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1690 case S_CONNECT_RECV_ATS:
1691 case S_CONNECT_RECV_BLACKLIST:
1692 case S_CONNECT_RECV_ACK:
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1694 "Ignoring request to try to connect to `%s', already trying!\n",
1695 GNUNET_i2s (target));
1696 return; /* already trying */
1698 case S_RECONNECT_ATS:
1699 case S_RECONNECT_BLACKLIST:
1700 case S_RECONNECT_SENT:
1701 case S_CONNECTED_SWITCHING_BLACKLIST:
1702 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "Ignoring request to try to connect, already connected to `%s'!\n",
1705 GNUNET_i2s (target));
1706 return; /* already connected */
1708 /* get rid of remains, ready to re-try immediately */
1709 free_neighbour (n, GNUNET_NO);
1711 case S_DISCONNECT_FINISHED:
1712 /* should not be possible */
1715 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1717 free_neighbour (n, GNUNET_NO);
1721 n = setup_neighbour (target);
1722 n->state = S_INIT_ATS;
1723 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1725 GNUNET_ATS_reset_backoff (GST_ats, target);
1726 GNUNET_ATS_suggest_address (GST_ats, target);
1731 * Function called with the result of a blacklist check.
1733 * @param cls closure with the 'struct BlackListCheckContext'
1734 * @param peer peer this check affects
1735 * @param result GNUNET_OK if the address is allowed
1738 handle_test_blacklist_cont (void *cls,
1739 const struct GNUNET_PeerIdentity *peer,
1742 struct BlackListCheckContext *bcc = cls;
1743 struct NeighbourMapEntry *n;
1746 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1747 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1749 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1750 if (NULL == (n = lookup_neighbour (peer)))
1751 goto cleanup; /* nobody left to care about new address */
1754 case S_NOT_CONNECTED:
1755 /* this should not be possible */
1757 free_neighbour (n, GNUNET_NO);
1760 /* still waiting on ATS suggestion */
1762 case S_INIT_BLACKLIST:
1763 /* check if the address the blacklist was fine with matches
1764 ATS suggestion, if so, we can move on! */
1765 if ( (GNUNET_OK == result) &&
1766 (1 == n->send_connect_ack) )
1768 n->send_connect_ack = 2;
1769 send_session_connect_ack_message (bcc->na.address,
1771 n->connect_ack_timestamp);
1773 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1774 break; /* result for an address we currently don't care about */
1775 if (GNUNET_OK == result)
1777 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1778 n->state = S_CONNECT_SENT;
1779 send_session_connect (&n->primary_address);
1783 // FIXME: should also possibly destroy session with plugin!?
1784 GNUNET_ATS_address_destroyed (GST_ats,
1787 free_address (&n->primary_address);
1788 n->state = S_INIT_ATS;
1789 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1790 // FIXME: do we need to ask ATS again for suggestions?
1791 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1794 case S_CONNECT_SENT:
1795 /* waiting on CONNECT_ACK, send ACK if one is pending */
1796 if ( (GNUNET_OK == result) &&
1797 (1 == n->send_connect_ack) )
1799 n->send_connect_ack = 2;
1800 send_session_connect_ack_message (n->primary_address.address,
1801 n->primary_address.session,
1802 n->connect_ack_timestamp);
1805 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1806 if (GNUNET_OK == result)
1808 /* valid new address, let ATS know! */
1809 GNUNET_ATS_address_add (GST_ats,
1812 bcc->ats, bcc->ats_count);
1814 n->state = S_CONNECT_RECV_ATS;
1815 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1816 GNUNET_ATS_reset_backoff (GST_ats, peer);
1817 GNUNET_ATS_suggest_address (GST_ats, peer);
1819 case S_CONNECT_RECV_ATS:
1820 /* still waiting on ATS suggestion, don't care about blacklist */
1822 case S_CONNECT_RECV_BLACKLIST:
1823 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1824 break; /* result for an address we currently don't care about */
1825 if (GNUNET_OK == result)
1827 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1828 n->state = S_CONNECT_RECV_ACK;
1829 send_session_connect_ack_message (bcc->na.address,
1831 n->connect_ack_timestamp);
1832 if (1 == n->send_connect_ack)
1833 n->send_connect_ack = 2;
1837 // FIXME: should also possibly destroy session with plugin!?
1838 GNUNET_ATS_address_destroyed (GST_ats,
1841 free_address (&n->primary_address);
1842 n->state = S_INIT_ATS;
1843 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1844 // FIXME: do we need to ask ATS again for suggestions?
1845 GNUNET_ATS_reset_backoff (GST_ats, peer);
1846 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1849 case S_CONNECT_RECV_ACK:
1850 /* waiting on SESSION_ACK, send ACK if one is pending */
1851 if ( (GNUNET_OK == result) &&
1852 (1 == n->send_connect_ack) )
1854 n->send_connect_ack = 2;
1855 send_session_connect_ack_message (n->primary_address.address,
1856 n->primary_address.session,
1857 n->connect_ack_timestamp);
1861 /* already connected, don't care about blacklist */
1863 case S_RECONNECT_ATS:
1864 /* still waiting on ATS suggestion, don't care about blacklist */
1866 case S_RECONNECT_BLACKLIST:
1867 if ( (GNUNET_OK == result) &&
1868 (1 == n->send_connect_ack) )
1870 n->send_connect_ack = 2;
1871 send_session_connect_ack_message (bcc->na.address,
1873 n->connect_ack_timestamp);
1875 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1876 break; /* result for an address we currently don't care about */
1877 if (GNUNET_OK == result)
1879 send_session_connect (&n->primary_address);
1880 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1881 n->state = S_RECONNECT_SENT;
1885 GNUNET_ATS_address_destroyed (GST_ats,
1888 n->state = S_RECONNECT_ATS;
1889 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1890 // FIXME: do we need to ask ATS again for suggestions?
1891 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1894 case S_RECONNECT_SENT:
1895 /* waiting on CONNECT_ACK, don't care about blacklist */
1896 if ( (GNUNET_OK == result) &&
1897 (1 == n->send_connect_ack) )
1899 n->send_connect_ack = 2;
1900 send_session_connect_ack_message (n->primary_address.address,
1901 n->primary_address.session,
1902 n->connect_ack_timestamp);
1905 case S_CONNECTED_SWITCHING_BLACKLIST:
1906 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1907 break; /* result for an address we currently don't care about */
1908 if (GNUNET_OK == result)
1910 send_session_connect (&n->alternative_address);
1911 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1915 GNUNET_ATS_address_destroyed (GST_ats,
1918 free_address (&n->alternative_address);
1919 n->state = S_CONNECTED;
1922 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1923 /* waiting on CONNECT_ACK, don't care about blacklist */
1924 if ( (GNUNET_OK == result) &&
1925 (1 == n->send_connect_ack) )
1927 n->send_connect_ack = 2;
1928 send_session_connect_ack_message (n->primary_address.address,
1929 n->primary_address.session,
1930 n->connect_ack_timestamp);
1934 /* Nothing to do here, ATS will already do what can be done */
1936 case S_DISCONNECT_FINISHED:
1937 /* should not be possible */
1941 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1943 free_neighbour (n, GNUNET_NO);
1947 GNUNET_HELLO_address_free (bcc->na.address);
1948 GNUNET_CONTAINER_DLL_remove (bc_head,
1956 * We want to know if connecting to a particular peer via
1957 * a particular address is allowed. Check it!
1959 * @param peer identity of the peer to switch the address for
1960 * @param ts time at which the check was initiated
1961 * @param address address of the other peer, NULL if other peer
1963 * @param session session to use (or NULL)
1964 * @param ats performance data
1965 * @param ats_count number of entries in ats (excluding 0-termination)
1968 check_blacklist (const struct GNUNET_PeerIdentity *peer,
1969 struct GNUNET_TIME_Absolute ts,
1970 const struct GNUNET_HELLO_Address *address,
1971 struct Session *session,
1972 const struct GNUNET_ATS_Information *ats,
1975 struct BlackListCheckContext *bcc;
1976 struct GST_BlacklistCheck *bc;
1979 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
1980 sizeof (struct GNUNET_ATS_Information) * ats_count);
1981 bcc->ats_count = ats_count;
1982 bcc->na.address = GNUNET_HELLO_address_copy (address);
1983 bcc->na.session = session;
1984 bcc->na.connect_timestamp = ts;
1985 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
1986 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1987 GNUNET_CONTAINER_DLL_insert (bc_head,
1990 if (NULL != (bc = GST_blacklist_test_allowed (peer,
1991 address->transport_name,
1992 &handle_test_blacklist_cont, bcc)))
1994 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
1995 we must only store 'bc' if 'bc' is non-NULL... */
2000 * We received a 'SESSION_CONNECT' message from the other peer.
2001 * Consider switching to it.
2003 * @param message possibly a 'struct SessionConnectMessage' (check format)
2004 * @param peer identity of the peer to switch the address for
2005 * @param address address of the other peer, NULL if other peer
2007 * @param session session to use (or NULL)
2008 * @param ats performance data
2009 * @param ats_count number of entries in ats (excluding 0-termination)
2012 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2013 const struct GNUNET_PeerIdentity *peer,
2014 const struct GNUNET_HELLO_Address *address,
2015 struct Session *session,
2016 const struct GNUNET_ATS_Information *ats,
2019 const struct SessionConnectMessage *scm;
2020 struct NeighbourMapEntry *n;
2021 struct GNUNET_TIME_Absolute ts;
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2024 "Received CONNECT message from peer `%s'\n",
2027 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2029 GNUNET_break_op (0);
2032 if (NULL == neighbours)
2033 return; /* we're shutting down */
2034 scm = (const struct SessionConnectMessage *) message;
2035 GNUNET_break_op (0 == ntohl (scm->reserved));
2036 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2037 n = lookup_neighbour (peer);
2039 n = setup_neighbour (peer);
2040 n->send_connect_ack = 1;
2041 n->connect_ack_timestamp = ts;
2045 case S_NOT_CONNECTED:
2046 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2047 /* Do a blacklist check for the new address */
2048 check_blacklist (peer, ts, address, session, ats, ats_count);
2051 case S_INIT_BLACKLIST:
2052 case S_CONNECT_SENT:
2053 case S_CONNECT_RECV_ATS:
2054 case S_CONNECT_RECV_BLACKLIST:
2055 case S_CONNECT_RECV_ACK:
2056 /* It can never hurt to have an alternative address in the above cases,
2057 see if it is allowed */
2058 check_blacklist (peer, ts, address, session, ats, ats_count);
2061 /* we are already connected and can thus send the ACK immediately;
2062 still, it can never hurt to have an alternative address, so also
2063 tell ATS about it */
2064 GNUNET_assert (NULL != n->primary_address.address);
2065 GNUNET_assert (NULL != n->primary_address.session);
2066 n->send_connect_ack = 0;
2067 send_session_connect_ack_message (n->primary_address.address,
2068 n->primary_address.session, ts);
2069 check_blacklist (peer, ts, address, session, ats, ats_count);
2071 case S_RECONNECT_ATS:
2072 case S_RECONNECT_BLACKLIST:
2073 case S_RECONNECT_SENT:
2074 /* It can never hurt to have an alternative address in the above cases,
2075 see if it is allowed */
2076 check_blacklist (peer, ts, address, session, ats, ats_count);
2078 case S_CONNECTED_SWITCHING_BLACKLIST:
2079 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2080 /* we are already connected and can thus send the ACK immediately;
2081 still, it can never hurt to have an alternative address, so also
2082 tell ATS about it */
2083 GNUNET_assert (NULL != n->primary_address.address);
2084 GNUNET_assert (NULL != n->primary_address.session);
2085 n->send_connect_ack = 0;
2086 send_session_connect_ack_message (n->primary_address.address,
2087 n->primary_address.session, ts);
2088 check_blacklist (peer, ts, address, session, ats, ats_count);
2091 /* get rid of remains without terminating sessions, ready to re-try */
2092 free_neighbour (n, GNUNET_YES);
2093 n = setup_neighbour (peer);
2094 n->state = S_CONNECT_RECV_ATS;
2095 GNUNET_ATS_reset_backoff (GST_ats, peer);
2096 GNUNET_ATS_suggest_address (GST_ats, peer);
2098 case S_DISCONNECT_FINISHED:
2099 /* should not be possible */
2103 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2105 free_neighbour (n, GNUNET_NO);
2112 * For an existing neighbour record, set the active connection to
2113 * use the given address.
2115 * @param peer identity of the peer to switch the address for
2116 * @param address address of the other peer, NULL if other peer
2118 * @param session session to use (or NULL)
2119 * @param ats performance data
2120 * @param ats_count number of entries in ats
2121 * @param bandwidth_in inbound quota to be used when connection is up
2122 * @param bandwidth_out outbound quota to be used when connection is up
2125 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2126 const struct GNUNET_HELLO_Address *address,
2127 struct Session *session,
2128 const struct GNUNET_ATS_Information *ats,
2130 struct GNUNET_BANDWIDTH_Value32NBO
2132 struct GNUNET_BANDWIDTH_Value32NBO
2135 struct NeighbourMapEntry *n;
2136 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2138 GNUNET_assert (address->transport_name != NULL);
2139 if (NULL == (n = lookup_neighbour (peer)))
2142 /* Obtain an session for this address from plugin */
2143 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2145 /* we don't have the plugin for this address */
2146 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2149 if ((NULL == session) && (0 == address->address_length))
2152 if (strlen (address->transport_name) > 0)
2153 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2157 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2158 "ATS tells us to switch to address '%s' session %p for peer `%s' in state %s\n",
2159 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2162 print_state (n->state));
2164 if (NULL == session)
2166 session = papi->get_session (papi->cls, address);
2167 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2168 "Obtained new session for peer `%s' and address '%s': %p\n",
2169 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2171 if (NULL == session)
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2174 "Failed to obtain new session for peer `%s' and address '%s'\n",
2175 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2176 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2181 case S_NOT_CONNECTED:
2183 free_neighbour (n, GNUNET_NO);
2186 set_address (&n->primary_address,
2187 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2188 n->state = S_INIT_BLACKLIST;
2189 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2190 check_blacklist (&n->id,
2191 n->connect_ack_timestamp,
2192 address, session, ats, ats_count);
2194 case S_INIT_BLACKLIST:
2195 /* ATS suggests a different address, switch again */
2196 set_address (&n->primary_address,
2197 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2198 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2199 check_blacklist (&n->id,
2200 n->connect_ack_timestamp,
2201 address, session, ats, ats_count);
2203 case S_CONNECT_SENT:
2204 /* ATS suggests a different address, switch again */
2205 set_address (&n->primary_address,
2206 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2207 n->state = S_INIT_BLACKLIST;
2208 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2209 check_blacklist (&n->id,
2210 n->connect_ack_timestamp,
2211 address, session, ats, ats_count);
2213 case S_CONNECT_RECV_ATS:
2214 set_address (&n->primary_address,
2215 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2216 n->state = S_CONNECT_RECV_BLACKLIST;
2217 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2218 check_blacklist (&n->id,
2219 n->connect_ack_timestamp,
2220 address, session, ats, ats_count);
2222 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2223 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2224 check_blacklist (&n->id,
2225 n->connect_ack_timestamp,
2226 address, session, ats, ats_count);
2228 case S_CONNECT_RECV_BLACKLIST:
2229 case S_CONNECT_RECV_ACK:
2230 /* ATS asks us to switch while we were trying to connect; switch to new
2231 address and check blacklist again */
2232 set_address (&n->primary_address,
2233 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2234 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2235 check_blacklist (&n->id,
2236 n->connect_ack_timestamp,
2237 address, session, ats, ats_count);
2240 GNUNET_assert (NULL != n->primary_address.address);
2241 GNUNET_assert (NULL != n->primary_address.session);
2242 if (n->primary_address.session == session)
2244 /* not an address change, just a quota change */
2245 set_address (&n->primary_address,
2246 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2249 /* ATS asks us to switch a life connection; see if we can get
2250 a CONNECT_ACK on it before we actually do this! */
2251 set_address (&n->alternative_address,
2252 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2253 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2254 check_blacklist (&n->id,
2255 GNUNET_TIME_absolute_get (),
2256 address, session, ats, ats_count);
2258 case S_RECONNECT_ATS:
2259 set_address (&n->primary_address,
2260 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2261 n->state = S_RECONNECT_BLACKLIST;
2262 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2263 check_blacklist (&n->id,
2264 n->connect_ack_timestamp,
2265 address, session, ats, ats_count);
2267 case S_RECONNECT_BLACKLIST:
2268 /* ATS asks us to switch while we were trying to reconnect; switch to new
2269 address and check blacklist again */
2270 set_address (&n->primary_address,
2271 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2272 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2273 check_blacklist (&n->id,
2274 n->connect_ack_timestamp,
2275 address, session, ats, ats_count);
2277 case S_RECONNECT_SENT:
2278 /* ATS asks us to switch while we were trying to reconnect; switch to new
2279 address and check blacklist again */
2280 set_address (&n->primary_address,
2281 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2282 n->state = S_RECONNECT_BLACKLIST;
2283 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2284 check_blacklist (&n->id,
2285 n->connect_ack_timestamp,
2286 address, session, ats, ats_count);
2288 case S_CONNECTED_SWITCHING_BLACKLIST:
2289 if (n->primary_address.session == session)
2291 /* ATS switches back to still-active session */
2292 free_address (&n->alternative_address);
2293 n->state = S_CONNECTED;
2296 /* ATS asks us to switch a life connection, update blacklist check */
2297 set_address (&n->alternative_address,
2298 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2299 check_blacklist (&n->id,
2300 GNUNET_TIME_absolute_get (),
2301 address, session, ats, ats_count);
2303 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2304 if (n->primary_address.session == session)
2306 /* ATS switches back to still-active session */
2307 free_address (&n->alternative_address);
2308 n->state = S_CONNECTED;
2311 /* ATS asks us to switch a life connection, update blacklist check */
2312 set_address (&n->alternative_address,
2313 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2314 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2315 check_blacklist (&n->id,
2316 GNUNET_TIME_absolute_get (),
2317 address, session, ats, ats_count);
2320 /* not going to switch addresses while disconnecting */
2322 case S_DISCONNECT_FINISHED:
2326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2334 * Master task run for every neighbour. Performs all of the time-related
2335 * activities (keep alive, send next message, disconnect if idle, finish
2336 * clean up after disconnect).
2338 * @param cls the 'struct NeighbourMapEntry' for which we are running
2339 * @param tc scheduler context (unused)
2342 master_task (void *cls,
2343 const struct GNUNET_SCHEDULER_TaskContext *tc)
2345 struct NeighbourMapEntry *n = cls;
2346 struct GNUNET_TIME_Relative delay;
2348 n->task = GNUNET_SCHEDULER_NO_TASK;
2349 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2351 "Master task runs for neighbour `%s' in state %s with timeout in %llu ms\n",
2352 GNUNET_i2s (&n->id),
2353 print_state(n->state),
2354 (unsigned long long) delay.rel_value);
2357 case S_NOT_CONNECTED:
2358 /* invalid state for master task, clean up */
2360 n->state = S_DISCONNECT_FINISHED;
2361 free_neighbour (n, GNUNET_NO);
2364 if (0 == delay.rel_value)
2366 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2367 "Connection to `%s' timed out waiting for ATS to provide address\n",
2368 GNUNET_i2s (&n->id));
2369 n->state = S_DISCONNECT_FINISHED;
2370 free_neighbour (n, GNUNET_NO);
2374 case S_INIT_BLACKLIST:
2375 if (0 == delay.rel_value)
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2378 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2379 GNUNET_i2s (&n->id));
2380 n->state = S_DISCONNECT_FINISHED;
2381 free_neighbour (n, GNUNET_NO);
2385 case S_CONNECT_SENT:
2386 if (0 == delay.rel_value)
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2389 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2390 GNUNET_i2s (&n->id));
2391 disconnect_neighbour (n);
2395 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2396 if (0 == delay.rel_value)
2398 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2399 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2400 GNUNET_i2s (&n->id));
2401 n->state = S_DISCONNECT_FINISHED;
2402 free_neighbour (n, GNUNET_NO);
2406 case S_CONNECT_RECV_ATS:
2407 if (0 == delay.rel_value)
2409 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2410 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2411 GNUNET_i2s (&n->id));
2412 n->state = S_DISCONNECT_FINISHED;
2413 free_neighbour (n, GNUNET_NO);
2417 case S_CONNECT_RECV_BLACKLIST:
2418 if (0 == delay.rel_value)
2420 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2421 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2422 GNUNET_i2s (&n->id));
2423 n->state = S_DISCONNECT_FINISHED;
2424 free_neighbour (n, GNUNET_NO);
2428 case S_CONNECT_RECV_ACK:
2429 if (0 == delay.rel_value)
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2432 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2433 GNUNET_i2s (&n->id));
2434 disconnect_neighbour (n);
2439 if (0 == delay.rel_value)
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2442 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2443 GNUNET_i2s (&n->id));
2444 disconnect_neighbour (n);
2447 try_transmission_to_peer (n);
2450 case S_RECONNECT_ATS:
2451 if (0 == delay.rel_value)
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2454 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2455 GNUNET_i2s (&n->id));
2456 disconnect_neighbour (n);
2460 case S_RECONNECT_BLACKLIST:
2461 if (0 == delay.rel_value)
2463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2464 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2465 GNUNET_i2s (&n->id));
2466 disconnect_neighbour (n);
2470 case S_RECONNECT_SENT:
2471 if (0 == delay.rel_value)
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2474 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2475 GNUNET_i2s (&n->id));
2476 disconnect_neighbour (n);
2480 case S_CONNECTED_SWITCHING_BLACKLIST:
2481 if (0 == delay.rel_value)
2483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2484 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2485 GNUNET_i2s (&n->id));
2486 disconnect_neighbour (n);
2489 try_transmission_to_peer (n);
2492 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2493 if (0 == delay.rel_value)
2495 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2496 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2497 GNUNET_i2s (&n->id));
2498 disconnect_neighbour (n);
2501 try_transmission_to_peer (n);
2505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2506 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2507 GNUNET_i2s (&n->id));
2508 free_neighbour (n, GNUNET_NO);
2509 n->state = S_DISCONNECT_FINISHED;
2511 case S_DISCONNECT_FINISHED:
2512 /* how did we get here!? */
2516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2520 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2521 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2522 (S_CONNECTED == n->state) )
2524 /* if we are *now* in one of these three states, we're sending
2525 keep alive messages, so we need to consider the keepalive
2526 delay, not just the connection timeout */
2527 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2530 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2531 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2538 * Send a SESSION_ACK message to the neighbour to confirm that we
2539 * got his CONNECT_ACK.
2541 * @param n neighbour to send the SESSION_ACK to
2544 send_session_ack_message (struct NeighbourMapEntry *n)
2546 struct GNUNET_MessageHeader msg;
2548 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2549 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2550 (void) send_with_session(n,
2551 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2552 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2558 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2559 * Consider switching to it.
2561 * @param message possibly a 'struct SessionConnectMessage' (check format)
2562 * @param peer identity of the peer to switch the address for
2563 * @param address address of the other peer, NULL if other peer
2565 * @param session session to use (or NULL)
2566 * @param ats performance data
2567 * @param ats_count number of entries in ats
2570 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2571 const struct GNUNET_PeerIdentity *peer,
2572 const struct GNUNET_HELLO_Address *address,
2573 struct Session *session,
2574 const struct GNUNET_ATS_Information *ats,
2577 const struct SessionConnectMessage *scm;
2578 struct GNUNET_TIME_Absolute ts;
2579 struct NeighbourMapEntry *n;
2581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2582 "Received CONNECT_ACK message from peer `%s'\n",
2585 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2587 GNUNET_break_op (0);
2590 scm = (const struct SessionConnectMessage *) message;
2591 GNUNET_break_op (ntohl (scm->reserved) == 0);
2592 if (NULL == (n = lookup_neighbour (peer)))
2594 GNUNET_STATISTICS_update (GST_stats,
2596 ("# unexpected CONNECT_ACK messages (no peer)"),
2600 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2603 case S_NOT_CONNECTED:
2605 free_neighbour (n, GNUNET_NO);
2608 case S_INIT_BLACKLIST:
2609 GNUNET_STATISTICS_update (GST_stats,
2611 ("# unexpected CONNECT_ACK messages (not ready)"),
2614 case S_CONNECT_SENT:
2615 if (ts.abs_value != n->primary_address.connect_timestamp.abs_value)
2616 break; /* ACK does not match our original CONNECT message */
2617 n->state = S_CONNECTED;
2618 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2619 GNUNET_STATISTICS_set (GST_stats,
2620 gettext_noop ("# peers connected"),
2621 ++neighbours_connected,
2623 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2624 /* Tell ATS that the outbound session we created to send CONNECT was successfull */
2625 GNUNET_ATS_address_add (GST_ats,
2626 n->primary_address.address,
2627 n->primary_address.session,
2629 set_address (&n->primary_address,
2630 n->primary_address.address,
2631 n->primary_address.session,
2632 n->primary_address.bandwidth_in,
2633 n->primary_address.bandwidth_out,
2635 send_session_ack_message (n);
2637 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2638 case S_CONNECT_RECV_ATS:
2639 case S_CONNECT_RECV_BLACKLIST:
2640 case S_CONNECT_RECV_ACK:
2641 GNUNET_STATISTICS_update (GST_stats,
2643 ("# unexpected CONNECT_ACK messages (not ready)"),
2647 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2648 send_session_ack_message (n);
2650 case S_RECONNECT_ATS:
2651 case S_RECONNECT_BLACKLIST:
2652 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2653 to give us a new address... */
2654 GNUNET_STATISTICS_update (GST_stats,
2656 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2659 case S_RECONNECT_SENT:
2660 /* new address worked; go back to connected! */
2661 n->state = S_CONNECTED;
2662 send_session_ack_message (n);
2664 case S_CONNECTED_SWITCHING_BLACKLIST:
2665 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2666 send_session_ack_message (n);
2668 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2669 /* new address worked; adopt it and go back to connected! */
2670 n->state = S_CONNECTED;
2671 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2672 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2673 GNUNET_ATS_address_add(GST_ats,
2674 n->alternative_address.address,
2675 n->alternative_address.session,
2677 set_address (&n->primary_address,
2678 n->alternative_address.address,
2679 n->alternative_address.session,
2680 n->alternative_address.bandwidth_in,
2681 n->alternative_address.bandwidth_out,
2683 free_address (&n->alternative_address);
2684 send_session_ack_message (n);
2687 GNUNET_STATISTICS_update (GST_stats,
2689 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2692 case S_DISCONNECT_FINISHED:
2696 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2704 * A session was terminated. Take note; if needed, try to get
2705 * an alternative address from ATS.
2707 * @param peer identity of the peer where the session died
2708 * @param session session that is gone
2709 * @return GNUNET_YES if this was a session used, GNUNET_NO if
2710 * this session was not in use
2713 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2714 struct Session *session)
2716 struct NeighbourMapEntry *n;
2717 struct BlackListCheckContext *bcc;
2718 struct BlackListCheckContext *bcc_next;
2720 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2722 while (NULL != (bcc = bcc_next))
2724 bcc_next = bcc->next;
2725 if (bcc->na.session == session)
2727 GST_blacklist_test_cancel (bcc->bc);
2728 GNUNET_HELLO_address_free (bcc->na.address);
2729 GNUNET_CONTAINER_DLL_remove (bc_head,
2735 if (NULL == (n = lookup_neighbour (peer)))
2736 return GNUNET_NO; /* can't affect us */
2737 if (session != n->primary_address.session)
2739 if (session == n->alternative_address.session)
2741 free_address (&n->alternative_address);
2742 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2743 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2744 n->state = S_CONNECTED;
2748 return GNUNET_NO; /* doesn't affect us further */
2751 n->expect_latency_response = GNUNET_NO;
2754 case S_NOT_CONNECTED:
2756 free_neighbour (n, GNUNET_NO);
2760 free_neighbour (n, GNUNET_NO);
2762 case S_INIT_BLACKLIST:
2763 case S_CONNECT_SENT:
2764 free_address (&n->primary_address);
2765 n->state = S_INIT_ATS;
2766 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2767 // FIXME: need to ask ATS for suggestions again?
2768 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2770 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2771 case S_CONNECT_RECV_ATS:
2772 case S_CONNECT_RECV_BLACKLIST:
2773 case S_CONNECT_RECV_ACK:
2774 /* error on inbound session; free neighbour entirely */
2775 free_address (&n->primary_address);
2776 free_neighbour (n, GNUNET_NO);
2779 free_address (&n->primary_address);
2780 n->state = S_RECONNECT_ATS;
2781 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2782 /* FIXME: is this ATS call needed? */
2783 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2785 case S_RECONNECT_ATS:
2786 /* we don't have an address, how can it go down? */
2789 case S_RECONNECT_BLACKLIST:
2790 case S_RECONNECT_SENT:
2791 n->state = S_RECONNECT_ATS;
2792 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2793 // FIXME: need to ask ATS for suggestions again?
2794 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2796 case S_CONNECTED_SWITCHING_BLACKLIST:
2797 /* primary went down while we were checking secondary against
2798 blacklist, adopt secondary as primary */
2799 free_address (&n->primary_address);
2800 n->primary_address = n->alternative_address;
2801 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2802 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2803 n->state = S_RECONNECT_BLACKLIST;
2805 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2806 /* primary went down while we were waiting for CONNECT_ACK on secondary;
2807 secondary as primary */
2808 free_address (&n->primary_address);
2809 n->primary_address = n->alternative_address;
2810 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2811 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2812 n->state = S_RECONNECT_SENT;
2815 free_address (&n->primary_address);
2817 case S_DISCONNECT_FINISHED:
2818 /* neighbour was freed and plugins told to terminate session */
2821 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2825 if (GNUNET_SCHEDULER_NO_TASK != n->task)
2826 GNUNET_SCHEDULER_cancel (n->task);
2827 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2833 * We received a 'SESSION_ACK' message from the other peer.
2834 * If we sent a 'CONNECT_ACK' last, this means we are now
2835 * connected. Otherwise, do nothing.
2837 * @param message possibly a 'struct SessionConnectMessage' (check format)
2838 * @param peer identity of the peer to switch the address for
2839 * @param address address of the other peer, NULL if other peer
2841 * @param session session to use (or NULL)
2842 * @param ats performance data
2843 * @param ats_count number of entries in ats
2846 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
2847 const struct GNUNET_PeerIdentity *peer,
2848 const struct GNUNET_HELLO_Address *address,
2849 struct Session *session,
2850 const struct GNUNET_ATS_Information *ats,
2853 struct NeighbourMapEntry *n;
2855 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2856 "Received SESSION_ACK message from peer `%s'\n",
2858 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2860 GNUNET_break_op (0);
2863 if (NULL == (n = lookup_neighbour (peer)))
2865 /* check if we are in a plausible state for having sent
2866 a CONNECT_ACK. If not, return, otherwise break */
2867 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
2868 (S_CONNECT_SENT != n->state) ) ||
2869 (2 != n->send_connect_ack) )
2871 GNUNET_STATISTICS_update (GST_stats,
2872 gettext_noop ("# unexpected SESSION ACK messages"), 1,
2876 n->state = S_CONNECTED;
2877 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2878 GNUNET_STATISTICS_set (GST_stats,
2879 gettext_noop ("# peers connected"),
2880 ++neighbours_connected,
2882 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2883 GNUNET_ATS_address_add(GST_ats,
2884 n->primary_address.address,
2885 n->primary_address.session,
2887 set_address (&n->primary_address,
2888 n->primary_address.address,
2889 n->primary_address.session,
2890 n->primary_address.bandwidth_in,
2891 n->primary_address.bandwidth_out,
2897 * Test if we're connected to the given peer.
2899 * @param target peer to test
2900 * @return GNUNET_YES if we are connected, GNUNET_NO if not
2903 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
2905 return test_connected (lookup_neighbour (target));
2910 * Change the incoming quota for the given peer.
2912 * @param neighbour identity of peer to change qutoa for
2913 * @param quota new quota
2916 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2917 struct GNUNET_BANDWIDTH_Value32NBO quota)
2919 struct NeighbourMapEntry *n;
2921 if (NULL == (n = lookup_neighbour (neighbour)))
2923 GNUNET_STATISTICS_update (GST_stats,
2925 ("# SET QUOTA messages ignored (no such peer)"),
2929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2930 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2931 ntohl (quota.value__), GNUNET_i2s (&n->id));
2932 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2933 if (0 != ntohl (quota.value__))
2935 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2936 GNUNET_i2s (&n->id), "SET_QUOTA");
2937 if (GNUNET_YES == test_connected (n))
2938 GNUNET_STATISTICS_update (GST_stats,
2939 gettext_noop ("# disconnects due to quota of 0"),
2941 disconnect_neighbour (n);
2946 * We received a disconnect message from the given peer,
2947 * validate and process.
2949 * @param peer sender of the message
2950 * @param msg the disconnect message
2953 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2955 const struct GNUNET_MessageHeader
2958 struct NeighbourMapEntry *n;
2959 const struct SessionDisconnectMessage *sdm;
2960 struct GNUNET_HashCode hc;
2962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2963 "Received DISCONNECT message from peer `%s'\n",
2965 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2967 // GNUNET_break_op (0);
2968 GNUNET_STATISTICS_update (GST_stats,
2970 ("# disconnect messages ignored (old format)"), 1,
2974 sdm = (const struct SessionDisconnectMessage *) msg;
2975 if (NULL == (n = lookup_neighbour (peer)))
2976 return; /* gone already */
2977 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <= n->connect_ack_timestamp.abs_value)
2979 GNUNET_STATISTICS_update (GST_stats,
2981 ("# disconnect messages ignored (timestamp)"), 1,
2985 GNUNET_CRYPTO_hash (&sdm->public_key,
2986 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2988 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2990 GNUNET_break_op (0);
2993 if (ntohl (sdm->purpose.size) !=
2994 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2995 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2996 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2998 GNUNET_break_op (0);
3002 GNUNET_CRYPTO_rsa_verify
3003 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3004 &sdm->signature, &sdm->public_key))
3006 GNUNET_break_op (0);
3009 if (GNUNET_YES == test_connected (n))
3010 GNUNET_STATISTICS_update (GST_stats,
3012 ("# other peer asked to disconnect from us"), 1,
3014 disconnect_neighbour (n);
3019 * Closure for the neighbours_iterate function.
3021 struct IteratorContext
3024 * Function to call on each connected neighbour.
3026 GST_NeighbourIterator cb;
3036 * Call the callback from the closure for each connected neighbour.
3038 * @param cls the 'struct IteratorContext'
3039 * @param key the hash of the public key of the neighbour
3040 * @param value the 'struct NeighbourMapEntry'
3041 * @return GNUNET_OK (continue to iterate)
3044 neighbours_iterate (void *cls, const struct GNUNET_HashCode * key, void *value)
3046 struct IteratorContext *ic = cls;
3047 struct NeighbourMapEntry *n = value;
3049 if (GNUNET_YES == test_connected (n))
3050 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->primary_address.address);
3056 * Iterate over all connected neighbours.
3058 * @param cb function to call
3059 * @param cb_cls closure for cb
3062 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3064 struct IteratorContext ic;
3066 if (NULL == neighbours)
3067 return; /* can happen during shutdown */
3070 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
3075 * If we have an active connection to the given target, it must be shutdown.
3077 * @param target peer to disconnect from
3080 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3082 struct NeighbourMapEntry *n;
3084 if (NULL == (n = lookup_neighbour (target)))
3085 return; /* not active */
3086 if (GNUNET_YES == test_connected (n))
3087 GNUNET_STATISTICS_update (GST_stats,
3089 ("# disconnected from peer upon explicit request"), 1,
3091 disconnect_neighbour (n);
3096 * Obtain current latency information for the given neighbour.
3098 * @param peer to get the latency for
3099 * @return observed latency of the address, FOREVER if the
3100 * the connection is not up
3102 struct GNUNET_TIME_Relative
3103 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3105 struct NeighbourMapEntry *n;
3107 n = lookup_neighbour (peer);
3109 return GNUNET_TIME_UNIT_FOREVER_REL;
3113 case S_RECONNECT_SENT:
3114 case S_RECONNECT_ATS:
3116 case S_NOT_CONNECTED:
3117 case S_INIT_BLACKLIST:
3119 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3120 case S_CONNECT_SENT:
3121 case S_CONNECT_RECV_BLACKLIST:
3123 case S_DISCONNECT_FINISHED:
3124 return GNUNET_TIME_UNIT_FOREVER_REL;
3126 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3130 return GNUNET_TIME_UNIT_FOREVER_REL;
3135 * Obtain current address information for the given neighbour.
3138 * @return address currently used
3140 struct GNUNET_HELLO_Address *
3141 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3143 struct NeighbourMapEntry *n;
3145 n = lookup_neighbour (peer);
3148 return n->primary_address.address;
3153 * Initialize the neighbours subsystem.
3155 * @param cls closure for callbacks
3156 * @param connect_cb function to call if we connect to a peer
3157 * @param disconnect_cb function to call if we disconnect from a peer
3158 * @param peer_address_cb function to call if we change an active address
3162 GST_neighbours_start (void *cls,
3163 GNUNET_TRANSPORT_NotifyConnect connect_cb,
3164 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3165 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
3168 connect_notify_cb = connect_cb;
3169 disconnect_notify_cb = disconnect_cb;
3170 address_change_cb = peer_address_cb;
3171 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
3176 * Disconnect from the given neighbour.
3179 * @param key hash of neighbour's public key (not used)
3180 * @param value the 'struct NeighbourMapEntry' of the neighbour
3181 * @return GNUNET_OK (continue to iterate)
3184 disconnect_all_neighbours (void *cls, const struct GNUNET_HashCode * key, void *value)
3186 struct NeighbourMapEntry *n = value;
3188 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3189 "Disconnecting peer `%4s', %s\n",
3190 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3191 n->state = S_DISCONNECT_FINISHED;
3192 free_neighbour (n, GNUNET_NO);
3198 * Cleanup the neighbours subsystem.
3201 GST_neighbours_stop ()
3203 if (NULL == neighbours)
3205 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
3206 &disconnect_all_neighbours,
3208 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
3210 callback_cls = NULL;
3211 connect_notify_cb = NULL;
3212 disconnect_notify_cb = NULL;
3213 address_change_cb = NULL;
3217 /* end of file gnunet-service-transport_neighbours.c */