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);
771 na->ats_active = GNUNET_NO;
772 if (NULL != na->address)
774 GNUNET_HELLO_address_free (na->address);
782 * Initialize the 'struct NeighbourAddress'.
784 * @param na neighbour address to initialize
785 * @param address address of the other peer, NULL if other peer
787 * @param session session to use (or NULL, in which case an
788 * address must be setup)
789 * @param bandwidth_in inbound quota to be used when connection is up
790 * @param bandwidth_out outbound quota to be used when connection is up
791 * @param is_active GNUNET_YES to mark this as the active address with ATS
794 set_address (struct NeighbourAddress *na,
795 const struct GNUNET_HELLO_Address *address,
796 struct Session *session,
797 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
798 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
801 struct GNUNET_TRANSPORT_PluginFunctions *papi;
803 if (NULL == (papi = GST_plugins_find (address->transport_name)))
808 if (session == na->session)
810 na->bandwidth_in = bandwidth_in;
811 na->bandwidth_out = bandwidth_out;
812 if (is_active != na->ats_active)
814 na->ats_active = is_active;
815 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
816 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
818 if (GNUNET_YES == is_active)
820 /* FIXME: is this the right place to set quotas? */
821 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
822 send_outbound_quota (&address->peer, bandwidth_out);
828 session = papi->get_session (papi->cls, address);
831 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
832 "Failed to obtain new session for peer `%s' and address '%s'\n",
833 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
834 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
837 na->address = GNUNET_HELLO_address_copy (address);
838 na->bandwidth_in = bandwidth_in;
839 na->bandwidth_out = bandwidth_out;
840 na->session = session;
841 na->ats_active = is_active;
842 if (GNUNET_YES == is_active)
844 /* Telling ATS about new session */
845 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
846 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
848 /* FIXME: is this the right place to set quotas? */
849 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
850 send_outbound_quota (&address->peer, bandwidth_out);
856 * Free a neighbour map entry.
858 * @param n entry to free
859 * @param keep_sessions GNUNET_NO to tell plugin to terminate sessions,
860 * GNUNET_YES to keep all sessions
863 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
865 struct MessageQueue *mq;
866 struct GNUNET_TRANSPORT_PluginFunctions *papi;
868 n->is_active = NULL; /* always free'd by its own continuation! */
870 /* fail messages currently in the queue */
871 while (NULL != (mq = n->messages_head))
873 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
874 if (NULL != mq->cont)
875 mq->cont (mq->cont_cls, GNUNET_SYSERR);
878 /* It is too late to send other peer disconnect notifications, but at
879 least internally we need to get clean... */
880 if (GNUNET_YES == test_connected (n))
882 GNUNET_STATISTICS_set (GST_stats,
883 gettext_noop ("# peers connected"),
884 --neighbours_connected,
886 disconnect_notify_cb (callback_cls, &n->id);
889 /* FIXME-PLUGIN-API: This does not seem to guarantee that all
890 transport sessions eventually get killed due to inactivity; they
891 MUST have their own timeout logic (but at least TCP doesn't have
892 one yet). Are we sure that EVERY 'session' of a plugin is
893 actually cleaned up this way!? Note that if we are switching
894 between two TCP sessions to the same peer, the existing plugin
895 API gives us not even the means to selectively kill only one of
896 them! Killing all sessions like this seems to be very, very
898 if ((GNUNET_NO == keep_sessions) &&
899 (NULL != n->primary_address.address) &&
900 (NULL != (papi = GST_plugins_find (n->primary_address.address->transport_name))))
901 papi->disconnect (papi->cls, &n->id);
903 n->state = S_DISCONNECT_FINISHED;
905 GNUNET_assert (GNUNET_YES ==
906 GNUNET_CONTAINER_multihashmap_remove (neighbours,
907 &n->id.hashPubKey, n));
909 /* cut transport-level connection */
910 free_address (&n->primary_address);
911 free_address (&n->alternative_address);
913 // FIXME-ATS-API: we might want to be more specific about
914 // which states we do this from in the future (ATS should
915 // have given us a 'suggest_address' handle, and if we have
916 // such a handle, we should cancel the operation here!
917 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
919 if (GNUNET_SCHEDULER_NO_TASK != n->task)
921 GNUNET_SCHEDULER_cancel (n->task);
922 n->task = GNUNET_SCHEDULER_NO_TASK;
924 /* free rest of memory */
930 * Transmit a message using the current session of the given
933 * @param n entry for the recipient
934 * @param msgbuf buffer to transmit
935 * @param msgbuf_size number of bytes in buffer
936 * @param priority transmission priority
937 * @param timeout transmission timeout
938 * @param cont continuation to call when finished (can be NULL)
939 * @param cont_cls closure for cont
942 send_with_session (struct NeighbourMapEntry *n,
943 const char *msgbuf, size_t msgbuf_size,
945 struct GNUNET_TIME_Relative timeout,
946 GNUNET_TRANSPORT_TransmitContinuation cont,
949 struct GNUNET_TRANSPORT_PluginFunctions *papi;
951 GNUNET_assert (n->primary_address.session != NULL);
952 if ( ( (NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name))) ||
953 (-1 == papi->send (papi->cls,
954 n->primary_address.session,
960 cont (cont_cls, &n->id, GNUNET_SYSERR);
961 GNUNET_break (NULL != papi);
966 * Master task run for every neighbour. Performs all of the time-related
967 * activities (keep alive, send next message, disconnect if idle, finish
968 * clean up after disconnect).
970 * @param cls the 'struct NeighbourMapEntry' for which we are running
971 * @param tc scheduler context (unused)
974 master_task (void *cls,
975 const struct GNUNET_SCHEDULER_TaskContext *tc);
979 * Function called when the 'DISCONNECT' message has been sent by the
980 * plugin. Frees the neighbour --- if the entry still exists.
983 * @param target identity of the neighbour that was disconnected
984 * @param result GNUNET_OK if the disconnect got out successfully
987 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
990 struct NeighbourMapEntry *n;
992 n = lookup_neighbour (target);
994 return; /* already gone */
995 if (S_DISCONNECT != n->state)
996 return; /* have created a fresh entry since */
997 n->state = S_DISCONNECT;
998 if (GNUNET_SCHEDULER_NO_TASK != n->task)
999 GNUNET_SCHEDULER_cancel (n->task);
1000 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1005 * Transmit a DISCONNECT message to the other peer.
1007 * @param n neighbour to send DISCONNECT message.
1010 send_disconnect (struct NeighbourMapEntry *n)
1012 struct SessionDisconnectMessage disconnect_msg;
1014 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1015 "Sending DISCONNECT message to peer `%4s'\n",
1016 GNUNET_i2s (&n->id));
1017 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1018 disconnect_msg.header.type =
1019 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1020 disconnect_msg.reserved = htonl (0);
1021 disconnect_msg.purpose.size =
1022 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1023 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1024 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1025 disconnect_msg.purpose.purpose =
1026 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1027 disconnect_msg.timestamp =
1028 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1029 disconnect_msg.public_key = GST_my_public_key;
1030 GNUNET_assert (GNUNET_OK ==
1031 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1032 &disconnect_msg.purpose,
1033 &disconnect_msg.signature));
1035 send_with_session (n,
1036 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1037 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1038 &send_disconnect_cont, NULL);
1039 GNUNET_STATISTICS_update (GST_stats,
1041 ("# DISCONNECT messages sent"), 1,
1047 * Disconnect from the given neighbour, clean up the record.
1049 * @param n neighbour to disconnect from
1052 disconnect_neighbour (struct NeighbourMapEntry *n)
1054 /* depending on state, notify neighbour and/or upper layers of this peer
1058 case S_NOT_CONNECTED:
1060 case S_INIT_BLACKLIST:
1061 /* other peer is completely unaware of us, no need to send DISCONNECT */
1062 n->state = S_DISCONNECT_FINISHED;
1063 free_neighbour (n, GNUNET_NO);
1065 case S_CONNECT_SENT:
1066 send_disconnect (n);
1067 n->state = S_DISCONNECT;
1069 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1070 case S_CONNECT_RECV_ATS:
1071 case S_CONNECT_RECV_BLACKLIST:
1072 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1073 n->state = S_DISCONNECT_FINISHED;
1074 free_neighbour (n, GNUNET_NO);
1076 case S_CONNECT_RECV_ACK:
1077 /* we DID ACK the other peer's request, must send DISCONNECT */
1078 send_disconnect (n);
1079 n->state = S_DISCONNECT;
1082 case S_RECONNECT_BLACKLIST:
1083 case S_RECONNECT_SENT:
1084 case S_CONNECTED_SWITCHING_BLACKLIST:
1085 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1086 /* we are currently connected, need to send disconnect and do
1087 internal notifications and update statistics */
1088 send_disconnect (n);
1089 GNUNET_STATISTICS_set (GST_stats,
1090 gettext_noop ("# peers connected"),
1091 --neighbours_connected,
1093 disconnect_notify_cb (callback_cls, &n->id);
1094 n->state = S_DISCONNECT;
1096 case S_RECONNECT_ATS:
1097 /* ATS address request timeout, disconnect without sending disconnect message */
1098 GNUNET_STATISTICS_set (GST_stats,
1099 gettext_noop ("# peers connected"),
1100 --neighbours_connected,
1102 disconnect_notify_cb (callback_cls, &n->id);
1103 n->state = S_DISCONNECT;
1106 /* already disconnected, ignore */
1108 case S_DISCONNECT_FINISHED:
1109 /* already cleaned up, how did we get here!? */
1113 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1117 /* schedule timeout to clean up */
1118 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1119 GNUNET_SCHEDULER_cancel (n->task);
1120 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1126 * We're done with our transmission attempt, continue processing.
1128 * @param cls the 'struct MessageQueue' of the message
1129 * @param receiver intended receiver
1130 * @param success whether it worked or not
1133 transmit_send_continuation (void *cls,
1134 const struct GNUNET_PeerIdentity *receiver,
1137 struct MessageQueue *mq = cls;
1138 struct NeighbourMapEntry *n;
1140 if (NULL == (n = lookup_neighbour (receiver)))
1143 return; /* disconnect or other error while transmitting, can happen */
1145 if (n->is_active == mq)
1147 /* this is still "our" neighbour, remove us from its queue
1148 and allow it to send the next message now */
1149 n->is_active = NULL;
1150 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1151 GNUNET_SCHEDULER_cancel (n->task);
1152 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1154 GNUNET_assert (bytes_in_send_queue >= mq->message_buf_size);
1155 bytes_in_send_queue -= mq->message_buf_size;
1156 GNUNET_STATISTICS_set (GST_stats,
1158 ("# bytes in message queue for other peers"),
1159 bytes_in_send_queue, GNUNET_NO);
1160 if (GNUNET_OK == success)
1161 GNUNET_STATISTICS_update (GST_stats,
1163 ("# messages transmitted to other peers"),
1166 GNUNET_STATISTICS_update (GST_stats,
1168 ("# transmission failures for messages to other peers"),
1170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1171 "Sending message to `%s' of type %u was a %s\n",
1172 GNUNET_i2s (receiver),
1173 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1174 (success == GNUNET_OK) ? "success" : "FAILURE");
1175 if (NULL != mq->cont)
1176 mq->cont (mq->cont_cls, success);
1182 * Check the message list for the given neighbour and if we can
1183 * send a message, do so. This function should only be called
1184 * if the connection is at least generally ready for transmission.
1185 * While we will only send one message at a time, no bandwidth
1186 * quota management is performed here. If a message was given to
1187 * the plugin, the continuation will automatically re-schedule
1188 * the 'master' task once the next message might be transmitted.
1190 * @param n target peer for which to transmit
1193 try_transmission_to_peer (struct NeighbourMapEntry *n)
1195 struct MessageQueue *mq;
1196 struct GNUNET_TIME_Relative timeout;
1198 if (NULL == n->primary_address.address)
1200 /* no address, why are we here? */
1204 if ((0 == n->primary_address.address->address_length) &&
1205 (NULL == n->primary_address.session))
1207 /* no address, why are we here? */
1211 if (NULL != n->is_active)
1213 /* transmission already pending */
1217 /* timeout messages from the queue that are past their due date */
1218 while (NULL != (mq = n->messages_head))
1220 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1221 if (timeout.rel_value > 0)
1223 GNUNET_STATISTICS_update (GST_stats,
1225 ("# messages timed out while in transport queue"),
1227 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1229 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
1232 return; /* no more messages */
1233 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1235 send_with_session (n,
1236 mq->message_buf, mq->message_buf_size,
1237 0 /* priority */, timeout,
1238 &transmit_send_continuation, mq);
1243 * Send keepalive message to the neighbour. Must only be called
1244 * if we are on 'connected' state. Will internally determine
1245 * if a keepalive is truly needed (so can always be called).
1247 * @param n neighbour that went idle and needs a keepalive
1250 send_keepalive (struct NeighbourMapEntry *n)
1252 struct GNUNET_MessageHeader m;
1254 GNUNET_assert (S_CONNECTED == n->state);
1255 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value > 0)
1256 return; /* no keepalive needed at this time */
1257 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1258 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1259 send_with_session (n,
1260 (const void *) &m, sizeof (m),
1261 UINT32_MAX /* priority */,
1262 KEEPALIVE_FREQUENCY,
1264 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1266 n->expect_latency_response = GNUNET_YES;
1267 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1268 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1273 * Keep the connection to the given neighbour alive longer,
1274 * we received a KEEPALIVE (or equivalent); send a response.
1276 * @param neighbour neighbour to keep alive (by sending keep alive response)
1279 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1281 struct NeighbourMapEntry *n;
1282 struct GNUNET_MessageHeader m;
1284 if (NULL == (n = lookup_neighbour (neighbour)))
1286 GNUNET_STATISTICS_update (GST_stats,
1288 ("# KEEPALIVE messages discarded (peer unknown)"),
1292 if (NULL == n->primary_address.session)
1294 GNUNET_STATISTICS_update (GST_stats,
1296 ("# KEEPALIVE messages discarded (no session)"),
1300 /* send reply to allow neighbour to measure latency */
1301 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1302 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1303 send_with_session(n,
1304 (const void *) &m, sizeof (m),
1305 UINT32_MAX /* priority */,
1306 KEEPALIVE_FREQUENCY,
1312 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1313 * latency to this peer. Pass the updated information (existing ats
1314 * plus calculated latency) to ATS.
1316 * @param neighbour neighbour to keep alive
1317 * @param ats performance data
1318 * @param ats_count number of entries in ats
1321 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1322 const struct GNUNET_ATS_Information *ats,
1325 struct NeighbourMapEntry *n;
1327 struct GNUNET_ATS_Information ats_new[ats_count + 1];
1329 if (NULL == (n = lookup_neighbour (neighbour)))
1331 GNUNET_STATISTICS_update (GST_stats,
1333 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1337 if ( (S_CONNECTED != n->state) ||
1338 (GNUNET_YES != n->expect_latency_response) )
1340 GNUNET_STATISTICS_update (GST_stats,
1342 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1346 n->expect_latency_response = GNUNET_NO;
1347 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1348 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1350 "Latency for peer `%s' is %llu ms\n",
1351 GNUNET_i2s (&n->id), n->latency.rel_value);
1352 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1353 /* append latency */
1354 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1355 if (n->latency.rel_value > UINT32_MAX)
1356 latency = UINT32_MAX;
1358 latency = n->latency.rel_value;
1359 ats_new[ats_count].value = htonl (latency);
1360 GNUNET_ATS_address_update (GST_ats,
1361 n->primary_address.address,
1362 n->primary_address.session, ats_new,
1368 * We have received a message from the given sender. How long should
1369 * we delay before receiving more? (Also used to keep the peer marked
1372 * @param sender sender of the message
1373 * @param size size of the message
1374 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1375 * GNUNET_NO if the neighbour is not connected or violates the quota,
1376 * GNUNET_SYSERR if the connection is not fully up yet
1377 * @return how long to wait before reading more from this sender
1379 struct GNUNET_TIME_Relative
1380 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1381 *sender, ssize_t size, int *do_forward)
1383 struct NeighbourMapEntry *n;
1384 struct GNUNET_TIME_Relative ret;
1386 if (NULL == neighbours)
1388 *do_forward = GNUNET_NO;
1389 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1391 if (NULL == (n = lookup_neighbour (sender)))
1393 GST_neighbours_try_connect (sender);
1394 if (NULL == (n = lookup_neighbour (sender)))
1396 GNUNET_STATISTICS_update (GST_stats,
1398 ("# messages discarded due to lack of neighbour record"),
1400 *do_forward = GNUNET_NO;
1401 return GNUNET_TIME_UNIT_ZERO;
1404 if (! test_connected (n))
1406 *do_forward = GNUNET_SYSERR;
1407 return GNUNET_TIME_UNIT_ZERO;
1409 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1411 n->quota_violation_count++;
1412 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1413 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1414 n->in_tracker.available_bytes_per_s__,
1415 n->quota_violation_count);
1416 /* Discount 32k per violation */
1417 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1421 if (n->quota_violation_count > 0)
1423 /* try to add 32k back */
1424 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1425 n->quota_violation_count--;
1428 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1430 GNUNET_STATISTICS_update (GST_stats,
1432 ("# bandwidth quota violations by other peers"),
1434 *do_forward = GNUNET_NO;
1435 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1437 *do_forward = GNUNET_YES;
1438 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1439 if (ret.rel_value > 0)
1441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1442 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1443 (unsigned long long) n->in_tracker.
1444 consumption_since_last_update__,
1445 (unsigned int) n->in_tracker.available_bytes_per_s__,
1446 (unsigned long long) ret.rel_value);
1447 GNUNET_STATISTICS_update (GST_stats,
1448 gettext_noop ("# ms throttling suggested"),
1449 (int64_t) ret.rel_value, GNUNET_NO);
1456 * Transmit a message to the given target using the active connection.
1458 * @param target destination
1459 * @param msg message to send
1460 * @param msg_size number of bytes in msg
1461 * @param timeout when to fail with timeout
1462 * @param cont function to call when done
1463 * @param cont_cls closure for 'cont'
1466 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1467 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1468 GST_NeighbourSendContinuation cont, void *cont_cls)
1470 struct NeighbourMapEntry *n;
1471 struct MessageQueue *mq;
1473 /* All ove these cases should never happen; they are all API violations.
1474 But we check anyway, just to be sure. */
1475 if (NULL == (n = lookup_neighbour (target)))
1479 cont (cont_cls, GNUNET_SYSERR);
1482 if (GNUNET_YES != test_connected (n))
1486 cont (cont_cls, GNUNET_SYSERR);
1489 bytes_in_send_queue += msg_size;
1490 GNUNET_STATISTICS_set (GST_stats,
1492 ("# bytes in message queue for other peers"),
1493 bytes_in_send_queue, GNUNET_NO);
1494 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1496 mq->cont_cls = cont_cls;
1497 memcpy (&mq[1], msg, msg_size);
1498 mq->message_buf = (const char *) &mq[1];
1499 mq->message_buf_size = msg_size;
1500 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1501 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1502 if ( (NULL != n->is_active) ||
1503 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1505 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1506 GNUNET_SCHEDULER_cancel (n->task);
1507 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1512 * Send a SESSION_CONNECT message via the given address.
1514 * @param na address to use
1517 send_session_connect (struct NeighbourAddress *na)
1519 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1520 struct SessionConnectMessage connect_msg;
1522 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1527 if (NULL == na->session)
1528 na->session = papi->get_session (papi->cls, na->address);
1529 if (NULL == na->session)
1534 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1535 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1536 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1537 connect_msg.reserved = htonl (0);
1538 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1539 (void) papi->send (papi->cls,
1541 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1543 GNUNET_TIME_UNIT_FOREVER_REL,
1549 * Send a SESSION_CONNECT_ACK message via the given address.
1551 * @param address address to use
1552 * @param session session to use
1553 * @param timestamp timestamp to use for the ACK message
1556 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1557 struct Session *session,
1558 struct GNUNET_TIME_Absolute timestamp)
1560 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1561 struct SessionConnectMessage connect_msg;
1563 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1568 if (NULL == session)
1569 session = papi->get_session (papi->cls, address);
1570 if (NULL == session)
1575 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1576 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1577 connect_msg.reserved = htonl (0);
1578 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1579 (void) papi->send (papi->cls,
1581 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1583 GNUNET_TIME_UNIT_FOREVER_REL,
1589 * Create a fresh entry in the neighbour map for the given peer
1591 * @param peer peer to create an entry for
1592 * @return new neighbour map entry
1594 static struct NeighbourMapEntry *
1595 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1597 struct NeighbourMapEntry *n;
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "Creating new neighbour entry for `%s'\n",
1602 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1604 n->state = S_NOT_CONNECTED;
1605 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1606 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1607 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1608 MAX_BANDWIDTH_CARRY_S);
1609 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1610 GNUNET_assert (GNUNET_OK ==
1611 GNUNET_CONTAINER_multihashmap_put (neighbours,
1612 &n->id.hashPubKey, n,
1613 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1619 * Check if the two given addresses are the same.
1620 * Actually only checks if the sessions are non-NULL
1621 * (which they should be) and then if they are identical;
1622 * the actual addresses don't matter if the session
1623 * pointers match anyway, and we must have session pointers
1626 * @param a1 first address to compare
1627 * @param a2 other address to compare
1628 * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
1631 address_matches (const struct NeighbourAddress *a1,
1632 const struct NeighbourAddress *a2)
1634 if ( (NULL == a1->session) ||
1635 (NULL == a2->session) )
1640 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1645 * Try to create a connection to the given target (eventually).
1647 * @param target peer to try to connect to
1650 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1652 struct NeighbourMapEntry *n;
1654 if (NULL == neighbours)
1655 return; /* during shutdown, do nothing */
1656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1657 "Asked to connect to peer `%s'\n",
1658 GNUNET_i2s (target));
1660 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1662 /* refuse to connect to myself */
1663 /* FIXME: can this happen? Is this not an API violation? */
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Refusing to try to connect to myself.\n");
1668 n = lookup_neighbour (target);
1673 case S_NOT_CONNECTED:
1674 /* this should not be possible */
1676 free_neighbour (n, GNUNET_NO);
1679 case S_INIT_BLACKLIST:
1680 case S_CONNECT_SENT:
1681 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1682 case S_CONNECT_RECV_ATS:
1683 case S_CONNECT_RECV_BLACKLIST:
1684 case S_CONNECT_RECV_ACK:
1685 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1686 "Ignoring request to try to connect to `%s', already trying!\n",
1687 GNUNET_i2s (target));
1688 return; /* already trying */
1690 case S_RECONNECT_ATS:
1691 case S_RECONNECT_BLACKLIST:
1692 case S_RECONNECT_SENT:
1693 case S_CONNECTED_SWITCHING_BLACKLIST:
1694 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1695 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1696 "Ignoring request to try to connect, already connected to `%s'!\n",
1697 GNUNET_i2s (target));
1698 return; /* already connected */
1700 /* get rid of remains, ready to re-try immediately */
1701 free_neighbour (n, GNUNET_NO);
1703 case S_DISCONNECT_FINISHED:
1704 /* should not be possible */
1707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1709 free_neighbour (n, GNUNET_NO);
1713 n = setup_neighbour (target);
1714 n->state = S_INIT_ATS;
1715 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1717 GNUNET_ATS_reset_backoff (GST_ats, target);
1718 GNUNET_ATS_suggest_address (GST_ats, target);
1723 * Function called with the result of a blacklist check.
1725 * @param cls closure with the 'struct BlackListCheckContext'
1726 * @param peer peer this check affects
1727 * @param result GNUNET_OK if the address is allowed
1730 handle_test_blacklist_cont (void *cls,
1731 const struct GNUNET_PeerIdentity *peer,
1734 struct BlackListCheckContext *bcc = cls;
1735 struct NeighbourMapEntry *n;
1738 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1739 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1741 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1742 if (NULL == (n = lookup_neighbour (peer)))
1743 goto cleanup; /* nobody left to care about new address */
1746 case S_NOT_CONNECTED:
1747 /* this should not be possible */
1749 free_neighbour (n, GNUNET_NO);
1752 /* still waiting on ATS suggestion */
1754 case S_INIT_BLACKLIST:
1755 /* check if the address the blacklist was fine with matches
1756 ATS suggestion, if so, we can move on! */
1757 if ( (GNUNET_OK == result) &&
1758 (1 == n->send_connect_ack) )
1760 n->send_connect_ack = 2;
1761 send_session_connect_ack_message (bcc->na.address,
1763 n->connect_ack_timestamp);
1765 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1766 break; /* result for an address we currently don't care about */
1767 if (GNUNET_OK == result)
1769 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1770 n->state = S_CONNECT_SENT;
1771 send_session_connect (&n->primary_address);
1775 // FIXME: should also possibly destroy session with plugin!?
1776 GNUNET_ATS_address_destroyed (GST_ats,
1779 free_address (&n->primary_address);
1780 n->state = S_INIT_ATS;
1781 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1782 // FIXME: do we need to ask ATS again for suggestions?
1783 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1786 case S_CONNECT_SENT:
1787 /* waiting on CONNECT_ACK, send ACK if one is pending */
1788 if ( (GNUNET_OK == result) &&
1789 (1 == n->send_connect_ack) )
1791 n->send_connect_ack = 2;
1792 send_session_connect_ack_message (n->primary_address.address,
1793 n->primary_address.session,
1794 n->connect_ack_timestamp);
1797 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1798 if (GNUNET_OK == result)
1800 /* valid new address, let ATS know! */
1801 GNUNET_ATS_address_add (GST_ats,
1804 bcc->ats, bcc->ats_count);
1806 n->state = S_CONNECT_RECV_ATS;
1807 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1808 GNUNET_ATS_reset_backoff (GST_ats, peer);
1809 GNUNET_ATS_suggest_address (GST_ats, peer);
1811 case S_CONNECT_RECV_ATS:
1812 /* still waiting on ATS suggestion, don't care about blacklist */
1814 case S_CONNECT_RECV_BLACKLIST:
1815 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1816 break; /* result for an address we currently don't care about */
1817 if (GNUNET_OK == result)
1819 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1820 n->state = S_CONNECT_RECV_ACK;
1821 send_session_connect_ack_message (bcc->na.address,
1823 n->connect_ack_timestamp);
1824 if (1 == n->send_connect_ack)
1825 n->send_connect_ack = 2;
1829 // FIXME: should also possibly destroy session with plugin!?
1830 GNUNET_ATS_address_destroyed (GST_ats,
1833 free_address (&n->primary_address);
1834 n->state = S_INIT_ATS;
1835 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1836 // FIXME: do we need to ask ATS again for suggestions?
1837 GNUNET_ATS_reset_backoff (GST_ats, peer);
1838 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1841 case S_CONNECT_RECV_ACK:
1842 /* waiting on SESSION_ACK, send ACK if one is pending */
1843 if ( (GNUNET_OK == result) &&
1844 (1 == n->send_connect_ack) )
1846 n->send_connect_ack = 2;
1847 send_session_connect_ack_message (n->primary_address.address,
1848 n->primary_address.session,
1849 n->connect_ack_timestamp);
1853 /* already connected, don't care about blacklist */
1855 case S_RECONNECT_ATS:
1856 /* still waiting on ATS suggestion, don't care about blacklist */
1858 case S_RECONNECT_BLACKLIST:
1859 if ( (GNUNET_OK == result) &&
1860 (1 == n->send_connect_ack) )
1862 n->send_connect_ack = 2;
1863 send_session_connect_ack_message (bcc->na.address,
1865 n->connect_ack_timestamp);
1867 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1868 break; /* result for an address we currently don't care about */
1869 if (GNUNET_OK == result)
1871 send_session_connect (&n->primary_address);
1872 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1873 n->state = S_RECONNECT_SENT;
1877 GNUNET_ATS_address_destroyed (GST_ats,
1880 n->state = S_RECONNECT_ATS;
1881 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1882 // FIXME: do we need to ask ATS again for suggestions?
1883 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1886 case S_RECONNECT_SENT:
1887 /* waiting on CONNECT_ACK, don't care about blacklist */
1888 if ( (GNUNET_OK == result) &&
1889 (1 == n->send_connect_ack) )
1891 n->send_connect_ack = 2;
1892 send_session_connect_ack_message (n->primary_address.address,
1893 n->primary_address.session,
1894 n->connect_ack_timestamp);
1897 case S_CONNECTED_SWITCHING_BLACKLIST:
1898 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1899 break; /* result for an address we currently don't care about */
1900 if (GNUNET_OK == result)
1902 send_session_connect (&n->alternative_address);
1903 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1907 GNUNET_ATS_address_destroyed (GST_ats,
1910 free_address (&n->alternative_address);
1911 n->state = S_CONNECTED;
1914 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1915 /* waiting on CONNECT_ACK, don't care about blacklist */
1916 if ( (GNUNET_OK == result) &&
1917 (1 == n->send_connect_ack) )
1919 n->send_connect_ack = 2;
1920 send_session_connect_ack_message (n->primary_address.address,
1921 n->primary_address.session,
1922 n->connect_ack_timestamp);
1926 /* Nothing to do here, ATS will already do what can be done */
1928 case S_DISCONNECT_FINISHED:
1929 /* should not be possible */
1933 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1935 free_neighbour (n, GNUNET_NO);
1939 GNUNET_HELLO_address_free (bcc->na.address);
1940 GNUNET_CONTAINER_DLL_remove (bc_head,
1948 * We want to know if connecting to a particular peer via
1949 * a particular address is allowed. Check it!
1951 * @param peer identity of the peer to switch the address for
1952 * @param ts time at which the check was initiated
1953 * @param address address of the other peer, NULL if other peer
1955 * @param session session to use (or NULL)
1956 * @param ats performance data
1957 * @param ats_count number of entries in ats (excluding 0-termination)
1960 check_blacklist (const struct GNUNET_PeerIdentity *peer,
1961 struct GNUNET_TIME_Absolute ts,
1962 const struct GNUNET_HELLO_Address *address,
1963 struct Session *session,
1964 const struct GNUNET_ATS_Information *ats,
1967 struct BlackListCheckContext *bcc;
1968 struct GST_BlacklistCheck *bc;
1971 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
1972 sizeof (struct GNUNET_ATS_Information) * ats_count);
1973 bcc->ats_count = ats_count;
1974 bcc->na.address = GNUNET_HELLO_address_copy (address);
1975 bcc->na.session = session;
1976 bcc->na.connect_timestamp = ts;
1977 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
1978 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1979 GNUNET_CONTAINER_DLL_insert (bc_head,
1982 if (NULL != (bc = GST_blacklist_test_allowed (peer,
1983 address->transport_name,
1984 &handle_test_blacklist_cont, bcc)))
1986 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
1987 we must only store 'bc' if 'bc' is non-NULL... */
1992 * We received a 'SESSION_CONNECT' message from the other peer.
1993 * Consider switching to it.
1995 * @param message possibly a 'struct SessionConnectMessage' (check format)
1996 * @param peer identity of the peer to switch the address for
1997 * @param address address of the other peer, NULL if other peer
1999 * @param session session to use (or NULL)
2000 * @param ats performance data
2001 * @param ats_count number of entries in ats (excluding 0-termination)
2004 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2005 const struct GNUNET_PeerIdentity *peer,
2006 const struct GNUNET_HELLO_Address *address,
2007 struct Session *session,
2008 const struct GNUNET_ATS_Information *ats,
2011 const struct SessionConnectMessage *scm;
2012 struct NeighbourMapEntry *n;
2013 struct GNUNET_TIME_Absolute ts;
2015 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2016 "Received CONNECT message from peer `%s'\n",
2018 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2020 GNUNET_break_op (0);
2023 if (NULL == neighbours)
2024 return; /* we're shutting down */
2025 scm = (const struct SessionConnectMessage *) message;
2026 GNUNET_break_op (0 == ntohl (scm->reserved));
2027 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2028 n = lookup_neighbour (peer);
2030 n = setup_neighbour (peer);
2031 n->send_connect_ack = 1;
2032 n->connect_ack_timestamp = ts;
2035 case S_NOT_CONNECTED:
2036 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2037 /* Do a blacklist check for the new address */
2038 check_blacklist (peer, ts, address, session, ats, ats_count);
2041 case S_INIT_BLACKLIST:
2042 case S_CONNECT_SENT:
2043 case S_CONNECT_RECV_ATS:
2044 case S_CONNECT_RECV_BLACKLIST:
2045 case S_CONNECT_RECV_ACK:
2046 /* It can never hurt to have an alternative address in the above cases,
2047 see if it is allowed */
2048 check_blacklist (peer, ts, address, session, ats, ats_count);
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);
2061 case S_RECONNECT_ATS:
2062 case S_RECONNECT_BLACKLIST:
2063 case S_RECONNECT_SENT:
2064 /* It can never hurt to have an alternative address in the above cases,
2065 see if it is allowed */
2066 check_blacklist (peer, ts, address, session, ats, ats_count);
2068 case S_CONNECTED_SWITCHING_BLACKLIST:
2069 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2070 /* we are already connected and can thus send the ACK immediately;
2071 still, it can never hurt to have an alternative address, so also
2072 tell ATS about it */
2073 GNUNET_assert (NULL != n->primary_address.address);
2074 GNUNET_assert (NULL != n->primary_address.session);
2075 n->send_connect_ack = 0;
2076 send_session_connect_ack_message (n->primary_address.address,
2077 n->primary_address.session, ts);
2078 check_blacklist (peer, ts, address, session, ats, ats_count);
2081 /* get rid of remains without terminating sessions, ready to re-try */
2082 free_neighbour (n, GNUNET_YES);
2083 n = setup_neighbour (peer);
2084 n->state = S_CONNECT_RECV_ATS;
2085 GNUNET_ATS_reset_backoff (GST_ats, peer);
2086 GNUNET_ATS_suggest_address (GST_ats, peer);
2088 case S_DISCONNECT_FINISHED:
2089 /* should not be possible */
2093 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2095 free_neighbour (n, GNUNET_NO);
2102 * For an existing neighbour record, set the active connection to
2103 * use the given address.
2105 * @param peer identity of the peer to switch the address for
2106 * @param address address of the other peer, NULL if other peer
2108 * @param session session to use (or NULL)
2109 * @param ats performance data
2110 * @param ats_count number of entries in ats
2111 * @param bandwidth_in inbound quota to be used when connection is up
2112 * @param bandwidth_out outbound quota to be used when connection is up
2115 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2116 const struct GNUNET_HELLO_Address *address,
2117 struct Session *session,
2118 const struct GNUNET_ATS_Information *ats,
2120 struct GNUNET_BANDWIDTH_Value32NBO
2122 struct GNUNET_BANDWIDTH_Value32NBO
2125 struct NeighbourMapEntry *n;
2126 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2128 GNUNET_assert (address->transport_name != NULL);
2129 if (NULL == (n = lookup_neighbour (peer)))
2132 /* Obtain an session for this address from plugin */
2133 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2135 /* we don't have the plugin for this address */
2136 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2139 if ((NULL == session) && (0 == address->address_length))
2142 if (strlen (address->transport_name) > 0)
2143 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2146 if (NULL == session)
2147 session = papi->get_session (papi->cls, address);
2148 if (NULL == session)
2150 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2151 "Failed to obtain new session for peer `%s' and address '%s'\n",
2152 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2153 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2156 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2157 "ATS tells us to switch to address '%s' session %p for peer `%s'\n",
2158 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2164 case S_NOT_CONNECTED:
2166 free_neighbour (n, GNUNET_NO);
2169 set_address (&n->primary_address,
2170 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2171 n->state = S_INIT_BLACKLIST;
2172 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2173 check_blacklist (&n->id,
2174 n->connect_ack_timestamp,
2175 address, session, ats, ats_count);
2177 case S_INIT_BLACKLIST:
2178 /* ATS suggests a different address, switch again */
2179 set_address (&n->primary_address,
2180 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2181 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2182 check_blacklist (&n->id,
2183 n->connect_ack_timestamp,
2184 address, session, ats, ats_count);
2186 case S_CONNECT_SENT:
2187 /* ATS suggests a different address, switch again */
2188 set_address (&n->primary_address,
2189 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2190 n->state = S_INIT_BLACKLIST;
2191 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2192 check_blacklist (&n->id,
2193 n->connect_ack_timestamp,
2194 address, session, ats, ats_count);
2196 case S_CONNECT_RECV_ATS:
2197 set_address (&n->primary_address,
2198 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2199 n->state = S_CONNECT_RECV_BLACKLIST;
2200 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2201 check_blacklist (&n->id,
2202 n->connect_ack_timestamp,
2203 address, session, ats, ats_count);
2205 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2206 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2207 check_blacklist (&n->id,
2208 n->connect_ack_timestamp,
2209 address, session, ats, ats_count);
2211 case S_CONNECT_RECV_BLACKLIST:
2212 case S_CONNECT_RECV_ACK:
2213 /* ATS asks us to switch while we were trying to connect; switch to new
2214 address and check blacklist again */
2215 set_address (&n->primary_address,
2216 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
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);
2223 GNUNET_assert (NULL != n->primary_address.address);
2224 GNUNET_assert (NULL != n->primary_address.session);
2225 if (n->primary_address.session == session)
2227 /* not an address change, just a quota change */
2228 set_address (&n->primary_address,
2229 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2232 /* ATS asks us to switch a life connection; see if we can get
2233 a CONNECT_ACK on it before we actually do this! */
2234 set_address (&n->alternative_address,
2235 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2236 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2237 check_blacklist (&n->id,
2238 GNUNET_TIME_absolute_get (),
2239 address, session, ats, ats_count);
2241 case S_RECONNECT_ATS:
2242 set_address (&n->primary_address,
2243 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2244 n->state = S_RECONNECT_BLACKLIST;
2245 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2246 check_blacklist (&n->id,
2247 n->connect_ack_timestamp,
2248 address, session, ats, ats_count);
2250 case S_RECONNECT_BLACKLIST:
2251 /* ATS asks us to switch while we were trying to reconnect; switch to new
2252 address and check blacklist again */
2253 set_address (&n->primary_address,
2254 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2255 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2256 check_blacklist (&n->id,
2257 n->connect_ack_timestamp,
2258 address, session, ats, ats_count);
2260 case S_RECONNECT_SENT:
2261 /* ATS asks us to switch while we were trying to reconnect; switch to new
2262 address and check blacklist again */
2263 set_address (&n->primary_address,
2264 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2265 n->state = S_RECONNECT_BLACKLIST;
2266 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2267 check_blacklist (&n->id,
2268 n->connect_ack_timestamp,
2269 address, session, ats, ats_count);
2271 case S_CONNECTED_SWITCHING_BLACKLIST:
2272 if (n->primary_address.session == session)
2274 /* ATS switches back to still-active session */
2275 free_address (&n->alternative_address);
2276 n->state = S_CONNECTED;
2279 /* ATS asks us to switch a life connection, update blacklist check */
2280 set_address (&n->alternative_address,
2281 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2282 check_blacklist (&n->id,
2283 GNUNET_TIME_absolute_get (),
2284 address, session, ats, ats_count);
2286 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2287 if (n->primary_address.session == session)
2289 /* ATS switches back to still-active session */
2290 free_address (&n->alternative_address);
2291 n->state = S_CONNECTED;
2294 /* ATS asks us to switch a life connection, update blacklist check */
2295 set_address (&n->alternative_address,
2296 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2297 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2298 check_blacklist (&n->id,
2299 GNUNET_TIME_absolute_get (),
2300 address, session, ats, ats_count);
2303 /* not going to switch addresses while disconnecting */
2305 case S_DISCONNECT_FINISHED:
2309 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2317 * Master task run for every neighbour. Performs all of the time-related
2318 * activities (keep alive, send next message, disconnect if idle, finish
2319 * clean up after disconnect).
2321 * @param cls the 'struct NeighbourMapEntry' for which we are running
2322 * @param tc scheduler context (unused)
2325 master_task (void *cls,
2326 const struct GNUNET_SCHEDULER_TaskContext *tc)
2328 struct NeighbourMapEntry *n = cls;
2329 struct GNUNET_TIME_Relative delay;
2331 n->task = GNUNET_SCHEDULER_NO_TASK;
2332 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2333 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2334 "master task runs for neighbour `%s' in state %d with timeout in %llu ms\n",
2335 GNUNET_i2s (&n->id),
2337 (unsigned long long) delay.rel_value);
2340 case S_NOT_CONNECTED:
2341 /* invalid state for master task, clean up */
2343 n->state = S_DISCONNECT_FINISHED;
2344 free_neighbour (n, GNUNET_NO);
2347 if (0 == delay.rel_value)
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "Connection to `%s' timed out waiting for ATS to provide address\n",
2351 GNUNET_i2s (&n->id));
2352 n->state = S_DISCONNECT_FINISHED;
2353 free_neighbour (n, GNUNET_NO);
2357 case S_INIT_BLACKLIST:
2358 if (0 == delay.rel_value)
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2362 GNUNET_i2s (&n->id));
2363 n->state = S_DISCONNECT_FINISHED;
2364 free_neighbour (n, GNUNET_NO);
2368 case S_CONNECT_SENT:
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 CONNECT_ACK\n",
2373 GNUNET_i2s (&n->id));
2374 disconnect_neighbour (n);
2378 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2379 if (0 == delay.rel_value)
2381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2382 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2383 GNUNET_i2s (&n->id));
2384 n->state = S_DISCONNECT_FINISHED;
2385 free_neighbour (n, GNUNET_NO);
2389 case S_CONNECT_RECV_ATS:
2390 if (0 == delay.rel_value)
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2394 GNUNET_i2s (&n->id));
2395 n->state = S_DISCONNECT_FINISHED;
2396 free_neighbour (n, GNUNET_NO);
2400 case S_CONNECT_RECV_BLACKLIST:
2401 if (0 == delay.rel_value)
2403 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2404 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2405 GNUNET_i2s (&n->id));
2406 n->state = S_DISCONNECT_FINISHED;
2407 free_neighbour (n, GNUNET_NO);
2411 case S_CONNECT_RECV_ACK:
2412 if (0 == delay.rel_value)
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2415 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2416 GNUNET_i2s (&n->id));
2417 disconnect_neighbour (n);
2422 if (0 == delay.rel_value)
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2426 GNUNET_i2s (&n->id));
2427 disconnect_neighbour (n);
2430 try_transmission_to_peer (n);
2433 case S_RECONNECT_ATS:
2434 if (0 == delay.rel_value)
2436 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2437 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2438 GNUNET_i2s (&n->id));
2439 disconnect_neighbour (n);
2443 case S_RECONNECT_BLACKLIST:
2444 if (0 == delay.rel_value)
2446 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2447 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2448 GNUNET_i2s (&n->id));
2449 disconnect_neighbour (n);
2453 case S_RECONNECT_SENT:
2454 if (0 == delay.rel_value)
2456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2457 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2458 GNUNET_i2s (&n->id));
2459 disconnect_neighbour (n);
2463 case S_CONNECTED_SWITCHING_BLACKLIST:
2464 if (0 == delay.rel_value)
2466 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2467 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2468 GNUNET_i2s (&n->id));
2469 disconnect_neighbour (n);
2472 try_transmission_to_peer (n);
2475 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2476 if (0 == delay.rel_value)
2478 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2479 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2480 GNUNET_i2s (&n->id));
2481 disconnect_neighbour (n);
2484 try_transmission_to_peer (n);
2488 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2489 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2490 GNUNET_i2s (&n->id));
2491 n->state = S_DISCONNECT_FINISHED;
2492 free_neighbour (n, GNUNET_NO);
2494 case S_DISCONNECT_FINISHED:
2495 /* how did we get here!? */
2499 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2503 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2504 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2505 (S_CONNECTED == n->state) )
2507 /* if we are *now* in one of these three states, we're sending
2508 keep alive messages, so we need to consider the keepalive
2509 delay, not just the connection timeout */
2510 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2513 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2514 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2521 * Send a SESSION_ACK message to the neighbour to confirm that we
2522 * got his CONNECT_ACK.
2524 * @param n neighbour to send the SESSION_ACK to
2527 send_session_ack_message (struct NeighbourMapEntry *n)
2529 struct GNUNET_MessageHeader msg;
2531 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2532 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2533 (void) send_with_session(n,
2534 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2535 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2541 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2542 * Consider switching to it.
2544 * @param message possibly a 'struct SessionConnectMessage' (check format)
2545 * @param peer identity of the peer to switch the address for
2546 * @param address address of the other peer, NULL if other peer
2548 * @param session session to use (or NULL)
2549 * @param ats performance data
2550 * @param ats_count number of entries in ats
2553 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2554 const struct GNUNET_PeerIdentity *peer,
2555 const struct GNUNET_HELLO_Address *address,
2556 struct Session *session,
2557 const struct GNUNET_ATS_Information *ats,
2560 const struct SessionConnectMessage *scm;
2561 struct GNUNET_TIME_Absolute ts;
2562 struct NeighbourMapEntry *n;
2564 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2565 "Received CONNECT_ACK message from peer `%s'\n",
2567 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2569 GNUNET_break_op (0);
2572 scm = (const struct SessionConnectMessage *) message;
2573 GNUNET_break_op (ntohl (scm->reserved) == 0);
2574 if (NULL == (n = lookup_neighbour (peer)))
2576 GNUNET_STATISTICS_update (GST_stats,
2578 ("# unexpected CONNECT_ACK messages (no peer)"),
2582 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2585 case S_NOT_CONNECTED:
2587 free_neighbour (n, GNUNET_NO);
2590 case S_INIT_BLACKLIST:
2591 GNUNET_STATISTICS_update (GST_stats,
2593 ("# unexpected CONNECT_ACK messages (not ready)"),
2596 case S_CONNECT_SENT:
2597 if (ts.abs_value != n->primary_address.connect_timestamp.abs_value)
2598 break; /* ACK does not match our original CONNECT message */
2599 n->state = S_CONNECTED;
2600 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2601 GNUNET_STATISTICS_set (GST_stats,
2602 gettext_noop ("# peers connected"),
2603 ++neighbours_connected,
2605 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2606 /* Tell ATS that the outbound session we created to send CONNECT was successfull */
2607 GNUNET_ATS_address_add (GST_ats,
2608 n->primary_address.address,
2609 n->primary_address.session,
2611 set_address (&n->primary_address,
2612 n->primary_address.address,
2613 n->primary_address.session,
2614 n->primary_address.bandwidth_in,
2615 n->primary_address.bandwidth_out,
2617 send_session_ack_message (n);
2619 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2620 case S_CONNECT_RECV_ATS:
2621 case S_CONNECT_RECV_BLACKLIST:
2622 case S_CONNECT_RECV_ACK:
2623 GNUNET_STATISTICS_update (GST_stats,
2625 ("# unexpected CONNECT_ACK messages (not ready)"),
2629 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2630 send_session_ack_message (n);
2632 case S_RECONNECT_ATS:
2633 case S_RECONNECT_BLACKLIST:
2634 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2635 to give us a new address... */
2636 GNUNET_STATISTICS_update (GST_stats,
2638 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2641 case S_RECONNECT_SENT:
2642 /* new address worked; go back to connected! */
2643 n->state = S_CONNECTED;
2644 send_session_ack_message (n);
2646 case S_CONNECTED_SWITCHING_BLACKLIST:
2647 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2648 send_session_ack_message (n);
2650 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2651 /* new address worked; adopt it and go back to connected! */
2652 n->state = S_CONNECTED;
2653 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2654 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2655 GNUNET_ATS_address_add(GST_ats,
2656 n->alternative_address.address,
2657 n->alternative_address.session,
2659 set_address (&n->primary_address,
2660 n->alternative_address.address,
2661 n->alternative_address.session,
2662 n->alternative_address.bandwidth_in,
2663 n->alternative_address.bandwidth_out,
2665 free_address (&n->alternative_address);
2666 send_session_ack_message (n);
2669 GNUNET_STATISTICS_update (GST_stats,
2671 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2674 case S_DISCONNECT_FINISHED:
2678 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2686 * A session was terminated. Take note; if needed, try to get
2687 * an alternative address from ATS.
2689 * @param peer identity of the peer where the session died
2690 * @param session session that is gone
2691 * @param GNUNET_YES if this was a session used, GNUNET_NO if
2692 * this session was not in use
2695 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2696 struct Session *session)
2698 struct NeighbourMapEntry *n;
2699 struct BlackListCheckContext *bcc;
2700 struct BlackListCheckContext *bcc_next;
2702 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2704 while (NULL != (bcc = bcc_next))
2706 bcc_next = bcc->next;
2707 if (bcc->na.session == session)
2709 GST_blacklist_test_cancel (bcc->bc);
2710 GNUNET_HELLO_address_free (bcc->na.address);
2711 GNUNET_CONTAINER_DLL_remove (bc_head,
2717 if (NULL == (n = lookup_neighbour (peer)))
2718 return GNUNET_NO; /* can't affect us */
2719 if (session != n->primary_address.session)
2721 if (session == n->alternative_address.session)
2723 free_address (&n->alternative_address);
2724 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2725 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2726 n->state = S_CONNECTED;
2730 return GNUNET_NO; /* doesn't affect us further */
2733 n->expect_latency_response = GNUNET_NO;
2736 case S_NOT_CONNECTED:
2738 free_neighbour (n, GNUNET_NO);
2742 free_neighbour (n, GNUNET_NO);
2744 case S_INIT_BLACKLIST:
2745 case S_CONNECT_SENT:
2746 free_address (&n->primary_address);
2747 n->state = S_INIT_ATS;
2748 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2749 // FIXME: need to ask ATS for suggestions again?
2750 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2752 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2753 case S_CONNECT_RECV_ATS:
2754 case S_CONNECT_RECV_BLACKLIST:
2755 case S_CONNECT_RECV_ACK:
2756 /* error on inbound session; free neighbour entirely */
2757 free_address (&n->primary_address);
2758 free_neighbour (n, GNUNET_NO);
2761 free_address (&n->primary_address);
2762 n->state = S_RECONNECT_ATS;
2763 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2764 /* FIXME: is this ATS call needed? */
2765 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2767 case S_RECONNECT_ATS:
2768 /* we don't have an address, how can it go down? */
2771 case S_RECONNECT_BLACKLIST:
2772 case S_RECONNECT_SENT:
2773 n->state = S_RECONNECT_ATS;
2774 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2775 // FIXME: need to ask ATS for suggestions again?
2776 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2778 case S_CONNECTED_SWITCHING_BLACKLIST:
2779 /* primary went down while we were checking secondary against
2780 blacklist, adopt secondary as primary */
2781 free_address (&n->primary_address);
2782 n->primary_address = n->alternative_address;
2783 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2784 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2785 n->state = S_RECONNECT_BLACKLIST;
2787 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2788 /* primary went down while we were waiting for CONNECT_ACK on secondary;
2789 secondary as primary */
2790 free_address (&n->primary_address);
2791 n->primary_address = n->alternative_address;
2792 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2793 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2794 n->state = S_RECONNECT_SENT;
2797 free_address (&n->primary_address);
2799 case S_DISCONNECT_FINISHED:
2800 /* neighbour was freed and plugins told to terminate session */
2803 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2807 if (GNUNET_SCHEDULER_NO_TASK != n->task)
2808 GNUNET_SCHEDULER_cancel (n->task);
2809 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2815 * We received a 'SESSION_ACK' message from the other peer.
2816 * If we sent a 'CONNECT_ACK' last, this means we are now
2817 * connected. Otherwise, do nothing.
2819 * @param message possibly a 'struct SessionConnectMessage' (check format)
2820 * @param peer identity of the peer to switch the address for
2821 * @param address address of the other peer, NULL if other peer
2823 * @param session session to use (or NULL)
2824 * @param ats performance data
2825 * @param ats_count number of entries in ats
2828 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
2829 const struct GNUNET_PeerIdentity *peer,
2830 const struct GNUNET_HELLO_Address *address,
2831 struct Session *session,
2832 const struct GNUNET_ATS_Information *ats,
2835 struct NeighbourMapEntry *n;
2837 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2838 "Received SESSION_ACK message from peer `%s'\n",
2840 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2842 GNUNET_break_op (0);
2845 if (NULL == (n = lookup_neighbour (peer)))
2847 /* check if we are in a plausible state for having sent
2848 a CONNECT_ACK. If not, return, otherwise break */
2849 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
2850 (S_CONNECT_SENT != n->state) ) ||
2851 (2 != n->send_connect_ack) )
2853 GNUNET_STATISTICS_update (GST_stats,
2854 gettext_noop ("# unexpected SESSION ACK messages"), 1,
2858 n->state = S_CONNECTED;
2859 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2860 GNUNET_STATISTICS_set (GST_stats,
2861 gettext_noop ("# peers connected"),
2862 ++neighbours_connected,
2864 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2865 GNUNET_ATS_address_add(GST_ats,
2866 n->primary_address.address,
2867 n->primary_address.session,
2869 set_address (&n->primary_address,
2870 n->primary_address.address,
2871 n->primary_address.session,
2872 n->primary_address.bandwidth_in,
2873 n->primary_address.bandwidth_out,
2879 * Test if we're connected to the given peer.
2881 * @param target peer to test
2882 * @return GNUNET_YES if we are connected, GNUNET_NO if not
2885 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
2887 return test_connected (lookup_neighbour (target));
2892 * Change the incoming quota for the given peer.
2894 * @param neighbour identity of peer to change qutoa for
2895 * @param quota new quota
2898 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2899 struct GNUNET_BANDWIDTH_Value32NBO quota)
2901 struct NeighbourMapEntry *n;
2903 if (NULL == (n = lookup_neighbour (neighbour)))
2905 GNUNET_STATISTICS_update (GST_stats,
2907 ("# SET QUOTA messages ignored (no such peer)"),
2911 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2912 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2913 ntohl (quota.value__), GNUNET_i2s (&n->id));
2914 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2915 if (0 != ntohl (quota.value__))
2917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2918 GNUNET_i2s (&n->id), "SET_QUOTA");
2919 if (GNUNET_YES == test_connected (n))
2920 GNUNET_STATISTICS_update (GST_stats,
2921 gettext_noop ("# disconnects due to quota of 0"),
2923 disconnect_neighbour (n);
2928 * We received a disconnect message from the given peer,
2929 * validate and process.
2931 * @param peer sender of the message
2932 * @param msg the disconnect message
2935 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2937 const struct GNUNET_MessageHeader
2940 struct NeighbourMapEntry *n;
2941 const struct SessionDisconnectMessage *sdm;
2942 struct GNUNET_HashCode hc;
2944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2945 "Received DISCONNECT message from peer `%s'\n",
2947 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2949 // GNUNET_break_op (0);
2950 GNUNET_STATISTICS_update (GST_stats,
2952 ("# disconnect messages ignored (old format)"), 1,
2956 sdm = (const struct SessionDisconnectMessage *) msg;
2957 if (NULL == (n = lookup_neighbour (peer)))
2958 return; /* gone already */
2959 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <= n->connect_ack_timestamp.abs_value)
2961 GNUNET_STATISTICS_update (GST_stats,
2963 ("# disconnect messages ignored (timestamp)"), 1,
2967 GNUNET_CRYPTO_hash (&sdm->public_key,
2968 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2970 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2972 GNUNET_break_op (0);
2975 if (ntohl (sdm->purpose.size) !=
2976 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2977 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2978 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2980 GNUNET_break_op (0);
2984 GNUNET_CRYPTO_rsa_verify
2985 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2986 &sdm->signature, &sdm->public_key))
2988 GNUNET_break_op (0);
2991 if (GNUNET_YES == test_connected (n))
2992 GNUNET_STATISTICS_update (GST_stats,
2994 ("# other peer asked to disconnect from us"), 1,
2996 disconnect_neighbour (n);
3001 * Closure for the neighbours_iterate function.
3003 struct IteratorContext
3006 * Function to call on each connected neighbour.
3008 GST_NeighbourIterator cb;
3018 * Call the callback from the closure for each connected neighbour.
3020 * @param cls the 'struct IteratorContext'
3021 * @param key the hash of the public key of the neighbour
3022 * @param value the 'struct NeighbourMapEntry'
3023 * @return GNUNET_OK (continue to iterate)
3026 neighbours_iterate (void *cls, const struct GNUNET_HashCode * key, void *value)
3028 struct IteratorContext *ic = cls;
3029 struct NeighbourMapEntry *n = value;
3031 if (GNUNET_YES == test_connected (n))
3032 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->primary_address.address);
3038 * Iterate over all connected neighbours.
3040 * @param cb function to call
3041 * @param cb_cls closure for cb
3044 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3046 struct IteratorContext ic;
3048 if (NULL == neighbours)
3049 return; /* can happen during shutdown */
3052 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
3057 * If we have an active connection to the given target, it must be shutdown.
3059 * @param target peer to disconnect from
3062 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3064 struct NeighbourMapEntry *n;
3066 if (NULL == (n = lookup_neighbour (target)))
3067 return; /* not active */
3068 if (GNUNET_YES == test_connected (n))
3069 GNUNET_STATISTICS_update (GST_stats,
3071 ("# disconnected from peer upon explicit request"), 1,
3073 disconnect_neighbour (n);
3078 * Obtain current latency information for the given neighbour.
3080 * @param peer to get the latency for
3081 * @return observed latency of the address, FOREVER if the
3082 * the connection is not up
3084 struct GNUNET_TIME_Relative
3085 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3087 struct NeighbourMapEntry *n;
3089 n = lookup_neighbour (peer);
3091 return GNUNET_TIME_UNIT_FOREVER_REL;
3095 case S_RECONNECT_SENT:
3096 case S_RECONNECT_ATS:
3098 case S_NOT_CONNECTED:
3099 case S_INIT_BLACKLIST:
3101 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3102 case S_CONNECT_SENT:
3103 case S_CONNECT_RECV_BLACKLIST:
3105 case S_DISCONNECT_FINISHED:
3106 return GNUNET_TIME_UNIT_FOREVER_REL;
3108 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3112 return GNUNET_TIME_UNIT_FOREVER_REL;
3117 * Obtain current address information for the given neighbour.
3120 * @return address currently used
3122 struct GNUNET_HELLO_Address *
3123 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3125 struct NeighbourMapEntry *n;
3127 n = lookup_neighbour (peer);
3130 return n->primary_address.address;
3135 * Initialize the neighbours subsystem.
3137 * @param cls closure for callbacks
3138 * @param connect_cb function to call if we connect to a peer
3139 * @param disconnect_cb function to call if we disconnect from a peer
3140 * @param peer_address_cb function to call if we change an active address
3144 GST_neighbours_start (void *cls,
3145 GNUNET_TRANSPORT_NotifyConnect connect_cb,
3146 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3147 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
3150 connect_notify_cb = connect_cb;
3151 disconnect_notify_cb = disconnect_cb;
3152 address_change_cb = peer_address_cb;
3153 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
3158 * Disconnect from the given neighbour.
3161 * @param key hash of neighbour's public key (not used)
3162 * @param value the 'struct NeighbourMapEntry' of the neighbour
3163 * @return GNUNET_OK (continue to iterate)
3166 disconnect_all_neighbours (void *cls, const struct GNUNET_HashCode * key, void *value)
3168 struct NeighbourMapEntry *n = value;
3170 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3171 "Disconnecting peer `%4s', %s\n",
3172 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3173 n->state = S_DISCONNECT_FINISHED;
3174 free_neighbour (n, GNUNET_NO);
3180 * Cleanup the neighbours subsystem.
3183 GST_neighbours_stop ()
3185 if (NULL == neighbours)
3187 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
3188 &disconnect_all_neighbours,
3190 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
3192 callback_cls = NULL;
3193 connect_notify_cb = NULL;
3194 disconnect_notify_cb = NULL;
3195 address_change_cb = NULL;
3199 /* end of file gnunet-service-transport_neighbours.c */