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"
45 * Size of the neighbour hash map.
47 #define NEIGHBOUR_TABLE_SIZE 256
50 * Time we give plugin to transmit DISCONNECT message before the
51 * neighbour entry self-destructs.
53 #define DISCONNECT_SENT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
56 * How often must a peer violate bandwidth quotas before we start
57 * to simply drop its messages?
59 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
62 * How often do we send KEEPALIVE messages to each of our neighbours and measure
63 * the latency with this neighbour?
64 * (idle timeout is 5 minutes or 300 seconds, so with 100s interval we
65 * send 3 keepalives in each interval, so 3 messages would need to be
66 * lost in a row for a disconnect).
68 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 100)
71 * How long are we willing to wait for a response from ATS before timing out?
73 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
76 * How long are we willing to wait for an ACK from the other peer before
77 * giving up on our connect operation?
79 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
82 * How long are we willing to wait for a successful reconnect if
83 * an existing connection went down? Much shorter than the
84 * usual SETUP_CONNECTION_TIMEOUT as we do not inform the
85 * higher layers about the disconnect during this period.
87 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
90 * How long are we willing to wait for a response from the blacklist
91 * subsystem before timing out?
93 #define BLACKLIST_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 500)
96 GNUNET_NETWORK_STRUCT_BEGIN
99 * Message a peer sends to another to indicate that it intends to
100 * setup a connection/session for data exchange. A 'SESSION_CONNECT'
101 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
102 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
103 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
104 * should be connected.
106 struct SessionConnectMessage
109 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
110 * or 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK'
112 struct GNUNET_MessageHeader header;
117 uint32_t reserved GNUNET_PACKED;
120 * Absolute time at the sender. Only the most recent connect
121 * message implies which session is preferred by the sender.
123 struct GNUNET_TIME_AbsoluteNBO timestamp;
129 * Message we send to the other peer to notify him that we intentionally
130 * are disconnecting (to reduce timeouts). This is just a friendly
131 * notification, peers must not rely on always receiving disconnect
134 struct SessionDisconnectMessage
137 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
139 struct GNUNET_MessageHeader header;
144 uint32_t reserved GNUNET_PACKED;
147 * Purpose of the signature. Extends over the timestamp.
148 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
150 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
153 * Absolute time at the sender. Only the most recent connect
154 * message implies which session is preferred by the sender.
156 struct GNUNET_TIME_AbsoluteNBO timestamp;
159 * Public key of the sender.
161 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
164 * Signature of the peer that sends us the disconnect. Only
165 * valid if the timestamp is AFTER the timestamp from the
166 * corresponding 'CONNECT' message.
168 struct GNUNET_CRYPTO_RsaSignature signature;
172 GNUNET_NETWORK_STRUCT_END
176 * For each neighbour we keep a list of messages
177 * that we still want to transmit to the neighbour.
183 * This is a doubly linked list.
185 struct MessageQueue *next;
188 * This is a doubly linked list.
190 struct MessageQueue *prev;
193 * Function to call once we're done.
195 GST_NeighbourSendContinuation cont;
203 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
204 * stuck together in memory. Allocated at the end of this struct.
206 const char *message_buf;
209 * Size of the message buf
211 size_t message_buf_size;
214 * At what time should we fail?
216 struct GNUNET_TIME_Absolute timeout;
222 * Possible state of a neighbour. Initially, we are S_NOT_CONNECTED.
224 * Then, there are two main paths. If we receive a CONNECT message, we
225 * first run a check against the blacklist (S_CONNECT_RECV_BLACKLIST_INBOUND).
226 * If this check is successful, we give the inbound address to ATS.
227 * After the check we ask ATS for a suggestion (S_CONNECT_RECV_ATS).
228 * If ATS makes a suggestion, we ALSO give that suggestion to the blacklist
229 * (S_CONNECT_RECV_BLACKLIST). Once the blacklist approves the
230 * address we got from ATS, we send our CONNECT_ACK and go to
231 * S_CONNECT_RECV_ACK. If we receive a SESSION_ACK, we go to
232 * S_CONNECTED (and notify everyone about the new connection). If the
233 * operation times out, we go to S_DISCONNECT.
235 * The other case is where we transmit a CONNECT message first. We
236 * start with S_INIT_ATS. If we get an address, we enter
237 * S_INIT_BLACKLIST and check the blacklist. If the blacklist is OK
238 * with the connection, we actually send the CONNECT message and go to
239 * state S_CONNECT_SENT. Once we receive a CONNECT_ACK, we go to
240 * S_CONNECTED (and notify everyone about the new connection and send
241 * back a SESSION_ACK). If the operation times out, we go to
244 * If the session is in trouble (i.e. transport-level disconnect or
245 * timeout), we go to S_RECONNECT_ATS where we ask ATS for a new
246 * address (we don't notify anyone about the disconnect yet). Once we
247 * have a new address, we go to S_RECONNECT_BLACKLIST to check the new
248 * address against the blacklist. If the blacklist approves, we enter
249 * S_RECONNECT_SENT and send a CONNECT message. If we receive a
250 * CONNECT_ACK, we go to S_CONNECTED and nobody noticed that we had
251 * trouble; we also send a SESSION_ACK at this time just in case. If
252 * the operation times out, we go to S_DISCONNECT (and notify everyone
253 * about the lost connection).
255 * If ATS decides to switch addresses while we have a normal
256 * connection, we go to S_CONNECTED_SWITCHING_BLACKLIST to check the
257 * new address against the blacklist. If the blacklist approves, we
258 * go to S_CONNECTED_SWITCHING_CONNECT_SENT and send a
259 * SESSION_CONNECT. If we get a SESSION_ACK back, we switch the
260 * primary connection to the suggested alternative from ATS, go back
261 * to S_CONNECTED and send a SESSION_ACK to the other peer just to be
262 * sure. If the operation times out (or the blacklist disapproves),
263 * we go to S_CONNECTED (and notify ATS that the given alternative
264 * address is "invalid").
266 * Once a session is in S_DISCONNECT, it is cleaned up and then goes
267 * to (S_DISCONNECT_FINISHED). If we receive an explicit disconnect
268 * request, we can go from any state to S_DISCONNECT, possibly after
269 * generating disconnect notifications.
271 * Note that it is quite possible that while we are in any of these
272 * states, we could receive a 'CONNECT' request from the other peer.
273 * We then enter a 'weird' state where we pursue our own primary state
274 * machine (as described above), but with the 'send_connect_ack' flag
275 * set to 1. If our state machine allows us to send a 'CONNECT_ACK'
276 * (because we have an acceptable address), we send the 'CONNECT_ACK'
277 * and set the 'send_connect_ack' to 2. If we then receive a
278 * 'SESSION_ACK', we go to 'S_CONNECTED' (and reset 'send_connect_ack'
285 * fresh peer or completely disconnected
290 * Asked to initiate connection, trying to get address from ATS
295 * Asked to initiate connection, trying to get address approved
301 * Sent CONNECT message to other peer, waiting for CONNECT_ACK
306 * Received a CONNECT, do a blacklist check for inbound address
308 S_CONNECT_RECV_BLACKLIST_INBOUND,
311 * Received a CONNECT, asking ATS about address suggestions.
316 * Received CONNECT from other peer, got an address, checking with blacklist.
318 S_CONNECT_RECV_BLACKLIST,
321 * CONNECT request from other peer was SESSION_ACK'ed, waiting for
327 * Got our CONNECT_ACK/SESSION_ACK, connection is up.
332 * Connection got into trouble, rest of the system still believes
333 * it to be up, but we're getting a new address from ATS.
338 * Connection got into trouble, rest of the system still believes
339 * it to be up; we are checking the new address against the blacklist.
341 S_RECONNECT_BLACKLIST,
344 * Sent CONNECT over new address (either by ATS telling us to switch
345 * addresses or from RECONNECT_ATS); if this fails, we need to tell
346 * the rest of the system about a disconnect.
351 * We have some primary connection, but ATS suggested we switch
352 * to some alternative; we're now checking the alternative against
355 S_CONNECTED_SWITCHING_BLACKLIST,
358 * We have some primary connection, but ATS suggested we switch
359 * to some alternative; we now sent a CONNECT message for the
360 * alternative session to the other peer and waiting for a
361 * CONNECT_ACK to make this our primary connection.
363 S_CONNECTED_SWITCHING_CONNECT_SENT,
366 * Disconnect in progress (we're sending the DISCONNECT message to the
367 * other peer; after that is finished, the state will be cleaned up).
372 * We're finished with the disconnect; and are cleaning up the state
373 * now! We put the struct into this state when we are really in the
374 * task that calls 'free' on it and are about to remove the record
375 * from the map. We should never find a 'struct NeighbourMapEntry'
376 * in this state in the map. Accessing a 'struct NeighbourMapEntry'
377 * in this state virtually always means using memory that has been
378 * freed (the exception being the cleanup code in 'free_neighbour').
380 S_DISCONNECT_FINISHED
385 * A possible address we could use to communicate with a neighbour.
387 struct NeighbourAddress
391 * Active session for this address.
393 struct Session *session;
396 * Network-level address information.
398 struct GNUNET_HELLO_Address *address;
401 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
402 * peer for this address. Use to check that the ACK is in response
403 * to our most recent 'CONNECT'.
405 struct GNUNET_TIME_Absolute connect_timestamp;
408 * Inbound bandwidth from ATS for this address.
410 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
413 * Outbound bandwidth from ATS for this address.
415 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
418 * Did we tell ATS that this is our 'active' address?
426 * Entry in neighbours.
428 struct NeighbourMapEntry
432 * Head of list of messages we would like to send to this peer;
433 * must contain at most one message per client.
435 struct MessageQueue *messages_head;
438 * Tail of list of messages we would like to send to this peer; must
439 * contain at most one message per client.
441 struct MessageQueue *messages_tail;
444 * Are we currently trying to send a message? If so, which one?
446 struct MessageQueue *is_active;
449 * Primary address we currently use to communicate with the neighbour.
451 struct NeighbourAddress primary_address;
454 * Alternative address currently under consideration for communicating
455 * with the neighbour.
457 struct NeighbourAddress alternative_address;
460 * Identity of this neighbour.
462 struct GNUNET_PeerIdentity id;
465 * Main task that drives this peer (timeouts, keepalives, etc.).
466 * Always runs the 'master_task'.
468 GNUNET_SCHEDULER_TaskIdentifier task;
471 * At what time should we sent the next keep-alive message?
473 struct GNUNET_TIME_Absolute keep_alive_time;
476 * At what time did we sent the last keep-alive message? Used
477 * to calculate round-trip time ("latency").
479 struct GNUNET_TIME_Absolute last_keep_alive_time;
482 * Timestamp we should include in our next CONNECT_ACK message.
483 * (only valid if 'send_connect_ack' is GNUNET_YES). Used to build
484 * our CONNECT_ACK message.
486 struct GNUNET_TIME_Absolute connect_ack_timestamp;
489 * Time where we should cut the connection (timeout) if we don't
490 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
491 * if we are in S_CONNECTED).
493 struct GNUNET_TIME_Absolute timeout;
496 * Latest calculated latency value
498 struct GNUNET_TIME_Relative latency;
501 * Tracker for inbound bandwidth.
503 struct GNUNET_BANDWIDTH_Tracker in_tracker;
506 * How often has the other peer (recently) violated the inbound
507 * traffic limit? Incremented by 10 per violation, decremented by 1
508 * per non-violation (for each time interval).
510 unsigned int quota_violation_count;
513 * The current state of the peer.
518 * Did we sent an KEEP_ALIVE message and are we expecting a response?
520 int expect_latency_response;
523 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
524 * (once we have an address to use and the peer has been allowed by our
525 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
526 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
527 * a 'SESSION_ACK' (regardless of what our own state machine might say).
529 int send_connect_ack;
535 * Context for blacklist checks and the 'handle_test_blacklist_cont'
536 * function. Stores information about ongoing blacklist checks.
538 struct BlackListCheckContext
542 * We keep blacklist checks in a DLL.
544 struct BlackListCheckContext *next;
547 * We keep blacklist checks in a DLL.
549 struct BlackListCheckContext *prev;
552 * Address that is being checked.
554 struct NeighbourAddress na;
557 * ATS information about the address.
559 struct GNUNET_ATS_Information *ats;
562 * Handle to the ongoing blacklist check.
564 struct GST_BlacklistCheck *bc;
567 * Size of the 'ats' array.
575 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
577 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
580 * We keep blacklist checks in a DLL so that we can find
581 * the 'sessions' in their 'struct NeighbourAddress' if
582 * a session goes down.
584 static struct BlackListCheckContext *bc_head;
587 * We keep blacklist checks in a DLL.
589 static struct BlackListCheckContext *bc_tail;
592 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
594 static void *callback_cls;
597 * Function to call when we connected to a neighbour.
599 static NotifyConnect connect_notify_cb;
602 * Function to call when we disconnected from a neighbour.
604 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
607 * Function to call when we changed an active address of a neighbour.
609 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
612 * counter for connected neighbours
614 static unsigned int neighbours_connected;
617 * Number of bytes we have currently queued for transmission.
619 static unsigned long long bytes_in_send_queue;
623 * Lookup a neighbour entry in the neighbours hash map.
625 * @param pid identity of the peer to look up
626 * @return the entry, NULL if there is no existing record
628 static struct NeighbourMapEntry *
629 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
631 if (NULL == neighbours)
633 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
637 print_state (int state)
642 case S_NOT_CONNECTED:
643 return "S_NOT_CONNECTED";
646 case S_INIT_BLACKLIST:
647 return "S_INIT_BLACKLIST";
649 return "S_CONNECT_SENT";
650 case S_CONNECT_RECV_BLACKLIST_INBOUND:
651 return "S_CONNECT_RECV_BLACKLIST_INBOUND";
652 case S_CONNECT_RECV_ATS:
653 return "S_CONNECT_RECV_ATS";
654 case S_CONNECT_RECV_BLACKLIST:
655 return "S_CONNECT_RECV_BLACKLIST";
656 case S_CONNECT_RECV_ACK:
657 return "S_CONNECT_RECV_ACK";
659 return "S_CONNECTED";
660 case S_RECONNECT_ATS:
661 return "S_RECONNECT_ATS";
662 case S_RECONNECT_BLACKLIST:
663 return "S_RECONNECT_BLACKLIST";
664 case S_RECONNECT_SENT:
665 return "S_RECONNECT_SENT";
666 case S_CONNECTED_SWITCHING_BLACKLIST:
667 return "S_CONNECTED_SWITCHING_BLACKLIST";
668 case S_CONNECTED_SWITCHING_CONNECT_SENT:
669 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
671 return "S_DISCONNECT";
672 case S_DISCONNECT_FINISHED:
673 return "S_DISCONNECT_FINISHED";
682 * Test if we're connected to the given peer.
684 * @param n neighbour entry of peer to test
685 * @return GNUNET_YES if we are connected, GNUNET_NO if not
688 test_connected (struct NeighbourMapEntry *n)
694 case S_NOT_CONNECTED:
696 case S_INIT_BLACKLIST:
698 case S_CONNECT_RECV_BLACKLIST_INBOUND:
699 case S_CONNECT_RECV_ATS:
700 case S_CONNECT_RECV_BLACKLIST:
701 case S_CONNECT_RECV_ACK:
704 case S_RECONNECT_ATS:
705 case S_RECONNECT_BLACKLIST:
706 case S_RECONNECT_SENT:
707 case S_CONNECTED_SWITCHING_BLACKLIST:
708 case S_CONNECTED_SWITCHING_CONNECT_SENT:
711 case S_DISCONNECT_FINISHED:
714 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
718 return GNUNET_SYSERR;
722 * Send information about a new outbound quota to our clients.
724 * @param target affected peer
725 * @param quota new quota
728 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
729 struct GNUNET_BANDWIDTH_Value32NBO quota)
731 struct QuotaSetMessage q_msg;
733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
734 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
735 ntohl (quota.value__), GNUNET_i2s (target));
736 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
737 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
739 q_msg.peer = (*target);
740 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
745 * We don't need a given neighbour address any more.
746 * Release its resources and give appropriate notifications
747 * to ATS and other subsystems.
749 * @param na address we are done with; 'na' itself must NOT be 'free'd, only the contents!
752 free_address (struct NeighbourAddress *na)
754 if (GNUNET_YES == na->ats_active)
756 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
757 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
760 na->ats_active = GNUNET_NO;
761 if (NULL != na->address)
763 GNUNET_HELLO_address_free (na->address);
771 * Initialize the 'struct NeighbourAddress'.
773 * @param na neighbour address to initialize
774 * @param address address of the other peer, NULL if other peer
776 * @param session session to use (or NULL, in which case an
777 * address must be setup)
778 * @param bandwidth_in inbound quota to be used when connection is up
779 * @param bandwidth_out outbound quota to be used when connection is up
780 * @param is_active GNUNET_YES to mark this as the active address with ATS
783 set_address (struct NeighbourAddress *na,
784 const struct GNUNET_HELLO_Address *address,
785 struct Session *session,
786 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
787 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
790 struct GNUNET_TRANSPORT_PluginFunctions *papi;
792 if (NULL == (papi = GST_plugins_find (address->transport_name)))
797 if (session == na->session)
799 na->bandwidth_in = bandwidth_in;
800 na->bandwidth_out = bandwidth_out;
801 if (is_active != na->ats_active)
803 na->ats_active = is_active;
804 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
805 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
807 if (GNUNET_YES == is_active)
809 /* FIXME: is this the right place to set quotas? */
810 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
811 send_outbound_quota (&address->peer, bandwidth_out);
817 session = papi->get_session (papi->cls, address);
820 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
821 "Failed to obtain new session for peer `%s' and address '%s'\n",
822 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
823 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
826 na->address = GNUNET_HELLO_address_copy (address);
827 na->bandwidth_in = bandwidth_in;
828 na->bandwidth_out = bandwidth_out;
829 na->session = session;
830 na->ats_active = is_active;
831 if (GNUNET_YES == is_active)
833 /* Telling ATS about new session */
834 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
835 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
837 /* FIXME: is this the right place to set quotas? */
838 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
839 send_outbound_quota (&address->peer, bandwidth_out);
845 * Free a neighbour map entry.
847 * @param n entry to free
848 * @param keep_sessions GNUNET_NO to tell plugin to terminate sessions,
849 * GNUNET_YES to keep all sessions
852 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
854 struct MessageQueue *mq;
855 struct GNUNET_TRANSPORT_PluginFunctions *papi;
856 struct GNUNET_HELLO_Address *backup_primary;
858 n->is_active = NULL; /* always free'd by its own continuation! */
860 /* fail messages currently in the queue */
861 while (NULL != (mq = n->messages_head))
863 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
864 if (NULL != mq->cont)
865 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
868 /* It is too late to send other peer disconnect notifications, but at
869 least internally we need to get clean... */
870 if (GNUNET_YES == test_connected (n))
872 GNUNET_STATISTICS_set (GST_stats,
873 gettext_noop ("# peers connected"),
874 --neighbours_connected,
876 disconnect_notify_cb (callback_cls, &n->id);
879 n->state = S_DISCONNECT_FINISHED;
881 if (NULL != n->primary_address.address)
882 backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address);
884 backup_primary = NULL;
886 /* free addresses and mark as unused */
887 free_address (&n->primary_address);
888 free_address (&n->alternative_address);
890 /* FIXME-PLUGIN-API: This does not seem to guarantee that all
891 transport sessions eventually get killed due to inactivity; they
892 MUST have their own timeout logic (but at least TCP doesn't have
893 one yet). Are we sure that EVERY 'session' of a plugin is
894 actually cleaned up this way!? Note that if we are switching
895 between two TCP sessions to the same peer, the existing plugin
896 API gives us not even the means to selectively kill only one of
897 them! Killing all sessions like this seems to be very, very
900 /* cut transport-level connection */
901 if ((GNUNET_NO == keep_sessions) &&
902 (NULL != backup_primary) &&
903 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
904 papi->disconnect (papi->cls, &n->id);
906 GNUNET_free_non_null (backup_primary);
908 GNUNET_assert (GNUNET_YES ==
909 GNUNET_CONTAINER_multihashmap_remove (neighbours,
910 &n->id.hashPubKey, n));
912 // FIXME-ATS-API: we might want to be more specific about
913 // which states we do this from in the future (ATS should
914 // have given us a 'suggest_address' handle, and if we have
915 // such a handle, we should cancel the operation here!
916 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
918 if (GNUNET_SCHEDULER_NO_TASK != n->task)
920 GNUNET_SCHEDULER_cancel (n->task);
921 n->task = GNUNET_SCHEDULER_NO_TASK;
923 /* free rest of memory */
928 * Transmit a message using the current session of the given
931 * @param n entry for the recipient
932 * @param msgbuf buffer to transmit
933 * @param msgbuf_size number of bytes in buffer
934 * @param priority transmission priority
935 * @param timeout transmission timeout
936 * @param cont continuation to call when finished (can be NULL)
937 * @param cont_cls closure for cont
940 send_with_session (struct NeighbourMapEntry *n,
941 const char *msgbuf, size_t msgbuf_size,
943 struct GNUNET_TIME_Relative timeout,
944 GNUNET_TRANSPORT_TransmitContinuation cont,
947 struct GNUNET_TRANSPORT_PluginFunctions *papi;
949 GNUNET_assert (n->primary_address.session != NULL);
950 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
951 (-1 == papi->send (papi->cls,
952 n->primary_address.session,
956 cont, cont_cls)))) &&
958 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
959 GNUNET_break (NULL != papi);
964 * Master task run for every neighbour. Performs all of the time-related
965 * activities (keep alive, send next message, disconnect if idle, finish
966 * clean up after disconnect).
968 * @param cls the 'struct NeighbourMapEntry' for which we are running
969 * @param tc scheduler context (unused)
972 master_task (void *cls,
973 const struct GNUNET_SCHEDULER_TaskContext *tc);
977 * Function called when the 'DISCONNECT' message has been sent by the
978 * plugin. Frees the neighbour --- if the entry still exists.
981 * @param target identity of the neighbour that was disconnected
982 * @param result GNUNET_OK if the disconnect got out successfully
985 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
986 int result, size_t payload, size_t physical)
988 struct NeighbourMapEntry *n;
990 n = lookup_neighbour (target);
992 return; /* already gone */
993 if (S_DISCONNECT != n->state)
994 return; /* have created a fresh entry since */
995 n->state = S_DISCONNECT;
996 if (GNUNET_SCHEDULER_NO_TASK != n->task)
997 GNUNET_SCHEDULER_cancel (n->task);
998 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1003 * Transmit a DISCONNECT message to the other peer.
1005 * @param n neighbour to send DISCONNECT message.
1008 send_disconnect (struct NeighbourMapEntry *n)
1010 struct SessionDisconnectMessage disconnect_msg;
1012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1013 "Sending DISCONNECT message to peer `%4s'\n",
1014 GNUNET_i2s (&n->id));
1015 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1016 disconnect_msg.header.type =
1017 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1018 disconnect_msg.reserved = htonl (0);
1019 disconnect_msg.purpose.size =
1020 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1021 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1022 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1023 disconnect_msg.purpose.purpose =
1024 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1025 disconnect_msg.timestamp =
1026 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1027 disconnect_msg.public_key = GST_my_public_key;
1028 GNUNET_assert (GNUNET_OK ==
1029 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1030 &disconnect_msg.purpose,
1031 &disconnect_msg.signature));
1033 send_with_session (n,
1034 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1035 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1036 &send_disconnect_cont, NULL);
1037 GNUNET_STATISTICS_update (GST_stats,
1039 ("# DISCONNECT messages sent"), 1,
1045 * Disconnect from the given neighbour, clean up the record.
1047 * @param n neighbour to disconnect from
1050 disconnect_neighbour (struct NeighbourMapEntry *n)
1052 /* depending on state, notify neighbour and/or upper layers of this peer
1056 case S_NOT_CONNECTED:
1058 case S_INIT_BLACKLIST:
1059 /* other peer is completely unaware of us, no need to send DISCONNECT */
1060 n->state = S_DISCONNECT_FINISHED;
1061 free_neighbour (n, GNUNET_NO);
1063 case S_CONNECT_SENT:
1064 send_disconnect (n);
1065 n->state = S_DISCONNECT;
1067 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1068 case S_CONNECT_RECV_ATS:
1069 case S_CONNECT_RECV_BLACKLIST:
1070 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1071 n->state = S_DISCONNECT_FINISHED;
1072 free_neighbour (n, GNUNET_NO);
1074 case S_CONNECT_RECV_ACK:
1075 /* we DID ACK the other peer's request, must send DISCONNECT */
1076 send_disconnect (n);
1077 n->state = S_DISCONNECT;
1080 case S_RECONNECT_BLACKLIST:
1081 case S_RECONNECT_SENT:
1082 case S_CONNECTED_SWITCHING_BLACKLIST:
1083 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1084 /* we are currently connected, need to send disconnect and do
1085 internal notifications and update statistics */
1086 send_disconnect (n);
1087 GNUNET_STATISTICS_set (GST_stats,
1088 gettext_noop ("# peers connected"),
1089 --neighbours_connected,
1091 disconnect_notify_cb (callback_cls, &n->id);
1092 n->state = S_DISCONNECT;
1094 case S_RECONNECT_ATS:
1095 /* ATS address request timeout, disconnect without sending disconnect message */
1096 GNUNET_STATISTICS_set (GST_stats,
1097 gettext_noop ("# peers connected"),
1098 --neighbours_connected,
1100 disconnect_notify_cb (callback_cls, &n->id);
1101 n->state = S_DISCONNECT;
1104 /* already disconnected, ignore */
1106 case S_DISCONNECT_FINISHED:
1107 /* already cleaned up, how did we get here!? */
1111 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1115 /* schedule timeout to clean up */
1116 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1117 GNUNET_SCHEDULER_cancel (n->task);
1118 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1124 * We're done with our transmission attempt, continue processing.
1126 * @param cls the 'struct MessageQueue' of the message
1127 * @param receiver intended receiver
1128 * @param success whether it worked or not
1131 transmit_send_continuation (void *cls,
1132 const struct GNUNET_PeerIdentity *receiver,
1133 int success, size_t size_payload, size_t physical)
1135 struct MessageQueue *mq = cls;
1136 struct NeighbourMapEntry *n;
1138 if (NULL == (n = lookup_neighbour (receiver)))
1141 return; /* disconnect or other error while transmitting, can happen */
1143 if (n->is_active == mq)
1145 /* this is still "our" neighbour, remove us from its queue
1146 and allow it to send the next message now */
1147 n->is_active = NULL;
1148 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1149 GNUNET_SCHEDULER_cancel (n->task);
1150 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1152 GNUNET_assert (bytes_in_send_queue >= mq->message_buf_size);
1153 GNUNET_break (size_payload == mq->message_buf_size);
1154 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "%u %u res: %u\n",size_payload, mq->message_buf_size, success);
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, size_payload, physical);
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, mq->message_buf_size, 0); /* 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 or while trying to switch addresses.
1245 * Will internally determine if a keepalive is truly needed (so can
1246 * always be called).
1248 * @param n neighbour that went idle and needs a keepalive
1251 send_keepalive (struct NeighbourMapEntry *n)
1253 struct GNUNET_MessageHeader m;
1255 GNUNET_assert ((S_CONNECTED == n->state) ||
1256 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1257 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1258 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value > 0)
1259 return; /* no keepalive needed at this time */
1260 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1261 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1262 send_with_session (n,
1263 (const void *) &m, sizeof (m),
1264 UINT32_MAX /* priority */,
1265 KEEPALIVE_FREQUENCY,
1267 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1269 n->expect_latency_response = GNUNET_YES;
1270 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1271 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1276 * Keep the connection to the given neighbour alive longer,
1277 * we received a KEEPALIVE (or equivalent); send a response.
1279 * @param neighbour neighbour to keep alive (by sending keep alive response)
1282 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1284 struct NeighbourMapEntry *n;
1285 struct GNUNET_MessageHeader m;
1287 if (NULL == (n = lookup_neighbour (neighbour)))
1289 GNUNET_STATISTICS_update (GST_stats,
1291 ("# KEEPALIVE messages discarded (peer unknown)"),
1295 if (NULL == n->primary_address.session)
1297 GNUNET_STATISTICS_update (GST_stats,
1299 ("# KEEPALIVE messages discarded (no session)"),
1303 /* send reply to allow neighbour to measure latency */
1304 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1305 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1306 send_with_session(n,
1307 (const void *) &m, sizeof (m),
1308 UINT32_MAX /* priority */,
1309 KEEPALIVE_FREQUENCY,
1315 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1316 * latency to this peer. Pass the updated information (existing ats
1317 * plus calculated latency) to ATS.
1319 * @param neighbour neighbour to keep alive
1320 * @param ats performance data
1321 * @param ats_count number of entries in ats
1324 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1325 const struct GNUNET_ATS_Information *ats,
1328 struct NeighbourMapEntry *n;
1330 struct GNUNET_ATS_Information ats_new[ats_count + 1];
1332 if (NULL == (n = lookup_neighbour (neighbour)))
1334 GNUNET_STATISTICS_update (GST_stats,
1336 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1340 if ( (S_CONNECTED != n->state) ||
1341 (GNUNET_YES != n->expect_latency_response) )
1343 GNUNET_STATISTICS_update (GST_stats,
1345 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1349 n->expect_latency_response = GNUNET_NO;
1350 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1351 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Latency for peer `%s' is %llu ms\n",
1354 GNUNET_i2s (&n->id), n->latency.rel_value);
1355 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1356 /* append latency */
1357 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1358 if (n->latency.rel_value > UINT32_MAX)
1359 latency = UINT32_MAX;
1361 latency = n->latency.rel_value;
1362 ats_new[ats_count].value = htonl (latency);
1363 GNUNET_ATS_address_update (GST_ats,
1364 n->primary_address.address,
1365 n->primary_address.session, ats_new,
1371 * We have received a message from the given sender. How long should
1372 * we delay before receiving more? (Also used to keep the peer marked
1375 * @param sender sender of the message
1376 * @param size size of the message
1377 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1378 * GNUNET_NO if the neighbour is not connected or violates the quota,
1379 * GNUNET_SYSERR if the connection is not fully up yet
1380 * @return how long to wait before reading more from this sender
1382 struct GNUNET_TIME_Relative
1383 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1384 *sender, ssize_t size, int *do_forward)
1386 struct NeighbourMapEntry *n;
1387 struct GNUNET_TIME_Relative ret;
1389 if (NULL == neighbours)
1391 *do_forward = GNUNET_NO;
1392 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1394 if (NULL == (n = lookup_neighbour (sender)))
1396 GST_neighbours_try_connect (sender);
1397 if (NULL == (n = lookup_neighbour (sender)))
1399 GNUNET_STATISTICS_update (GST_stats,
1401 ("# messages discarded due to lack of neighbour record"),
1403 *do_forward = GNUNET_NO;
1404 return GNUNET_TIME_UNIT_ZERO;
1407 if (! test_connected (n))
1409 *do_forward = GNUNET_SYSERR;
1410 return GNUNET_TIME_UNIT_ZERO;
1412 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1414 n->quota_violation_count++;
1415 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1416 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1417 n->in_tracker.available_bytes_per_s__,
1418 n->quota_violation_count);
1419 /* Discount 32k per violation */
1420 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1424 if (n->quota_violation_count > 0)
1426 /* try to add 32k back */
1427 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1428 n->quota_violation_count--;
1431 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1433 GNUNET_STATISTICS_update (GST_stats,
1435 ("# bandwidth quota violations by other peers"),
1437 *do_forward = GNUNET_NO;
1438 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1440 *do_forward = GNUNET_YES;
1441 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1442 if (ret.rel_value > 0)
1444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1445 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1446 (unsigned long long) n->in_tracker.
1447 consumption_since_last_update__,
1448 (unsigned int) n->in_tracker.available_bytes_per_s__,
1449 (unsigned long long) ret.rel_value);
1450 GNUNET_STATISTICS_update (GST_stats,
1451 gettext_noop ("# ms throttling suggested"),
1452 (int64_t) ret.rel_value, GNUNET_NO);
1459 * Transmit a message to the given target using the active connection.
1461 * @param target destination
1462 * @param msg message to send
1463 * @param msg_size number of bytes in msg
1464 * @param timeout when to fail with timeout
1465 * @param cont function to call when done
1466 * @param cont_cls closure for 'cont'
1469 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1470 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1471 GST_NeighbourSendContinuation cont, void *cont_cls)
1473 struct NeighbourMapEntry *n;
1474 struct MessageQueue *mq;
1476 /* All ove these cases should never happen; they are all API violations.
1477 But we check anyway, just to be sure. */
1478 if (NULL == (n = lookup_neighbour (target)))
1482 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1485 if (GNUNET_YES != test_connected (n))
1489 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1492 bytes_in_send_queue += msg_size;
1493 GNUNET_STATISTICS_set (GST_stats,
1495 ("# bytes in message queue for other peers"),
1496 bytes_in_send_queue, GNUNET_NO);
1497 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1499 mq->cont_cls = cont_cls;
1500 memcpy (&mq[1], msg, msg_size);
1501 mq->message_buf = (const char *) &mq[1];
1502 mq->message_buf_size = msg_size;
1503 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1504 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1505 if ( (NULL != n->is_active) ||
1506 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1508 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1509 GNUNET_SCHEDULER_cancel (n->task);
1510 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1515 * Send a SESSION_CONNECT message via the given address.
1517 * @param na address to use
1520 send_session_connect (struct NeighbourAddress *na)
1522 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1523 struct SessionConnectMessage connect_msg;
1525 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1530 if (NULL == na->session)
1531 na->session = papi->get_session (papi->cls, na->address);
1532 if (NULL == na->session)
1537 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1538 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1539 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1540 connect_msg.reserved = htonl (0);
1541 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1542 (void) papi->send (papi->cls,
1544 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1546 GNUNET_TIME_UNIT_FOREVER_REL,
1553 * Send a SESSION_CONNECT_ACK message via the given address.
1555 * @param address address to use
1556 * @param session session to use
1557 * @param timestamp timestamp to use for the ACK message
1560 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1561 struct Session *session,
1562 struct GNUNET_TIME_Absolute timestamp)
1564 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1565 struct SessionConnectMessage connect_msg;
1567 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1572 if (NULL == session)
1573 session = papi->get_session (papi->cls, address);
1574 if (NULL == session)
1579 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1580 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1581 connect_msg.reserved = htonl (0);
1582 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1583 (void) papi->send (papi->cls,
1585 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1587 GNUNET_TIME_UNIT_FOREVER_REL,
1594 * Create a fresh entry in the neighbour map for the given peer
1596 * @param peer peer to create an entry for
1597 * @return new neighbour map entry
1599 static struct NeighbourMapEntry *
1600 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1602 struct NeighbourMapEntry *n;
1604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1605 "Creating new neighbour entry for `%s'\n",
1607 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1609 n->state = S_NOT_CONNECTED;
1610 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1611 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1612 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1613 MAX_BANDWIDTH_CARRY_S);
1614 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1615 GNUNET_assert (GNUNET_OK ==
1616 GNUNET_CONTAINER_multihashmap_put (neighbours,
1617 &n->id.hashPubKey, n,
1618 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1624 * Check if the two given addresses are the same.
1625 * Actually only checks if the sessions are non-NULL
1626 * (which they should be) and then if they are identical;
1627 * the actual addresses don't matter if the session
1628 * pointers match anyway, and we must have session pointers
1631 * @param a1 first address to compare
1632 * @param a2 other address to compare
1633 * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
1636 address_matches (const struct NeighbourAddress *a1,
1637 const struct NeighbourAddress *a2)
1639 if ( (NULL == a1->session) ||
1640 (NULL == a2->session) )
1645 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1650 * Try to create a connection to the given target (eventually).
1652 * @param target peer to try to connect to
1655 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1657 struct NeighbourMapEntry *n;
1659 if (NULL == neighbours)
1660 return; /* during shutdown, do nothing */
1661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1662 "Asked to connect to peer `%s'\n",
1663 GNUNET_i2s (target));
1665 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1667 /* refuse to connect to myself */
1668 /* FIXME: can this happen? Is this not an API violation? */
1669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1670 "Refusing to try to connect to myself.\n");
1673 n = lookup_neighbour (target);
1678 case S_NOT_CONNECTED:
1679 /* this should not be possible */
1681 free_neighbour (n, GNUNET_NO);
1684 case S_INIT_BLACKLIST:
1685 case S_CONNECT_SENT:
1686 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1687 case S_CONNECT_RECV_ATS:
1688 case S_CONNECT_RECV_BLACKLIST:
1689 case S_CONNECT_RECV_ACK:
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691 "Ignoring request to try to connect to `%s', already trying!\n",
1692 GNUNET_i2s (target));
1693 return; /* already trying */
1695 case S_RECONNECT_ATS:
1696 case S_RECONNECT_BLACKLIST:
1697 case S_RECONNECT_SENT:
1698 case S_CONNECTED_SWITCHING_BLACKLIST:
1699 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1700 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1701 "Ignoring request to try to connect, already connected to `%s'!\n",
1702 GNUNET_i2s (target));
1703 return; /* already connected */
1705 /* get rid of remains, ready to re-try immediately */
1706 free_neighbour (n, GNUNET_NO);
1708 case S_DISCONNECT_FINISHED:
1709 /* should not be possible */
1712 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1714 free_neighbour (n, GNUNET_NO);
1718 n = setup_neighbour (target);
1719 n->state = S_INIT_ATS;
1720 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1722 GNUNET_ATS_reset_backoff (GST_ats, target);
1723 GNUNET_ATS_suggest_address (GST_ats, target);
1728 * Function called with the result of a blacklist check.
1730 * @param cls closure with the 'struct BlackListCheckContext'
1731 * @param peer peer this check affects
1732 * @param result GNUNET_OK if the address is allowed
1735 handle_test_blacklist_cont (void *cls,
1736 const struct GNUNET_PeerIdentity *peer,
1739 struct BlackListCheckContext *bcc = cls;
1740 struct NeighbourMapEntry *n;
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1744 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1746 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1747 if (NULL == (n = lookup_neighbour (peer)))
1748 goto cleanup; /* nobody left to care about new address */
1751 case S_NOT_CONNECTED:
1752 /* this should not be possible */
1754 free_neighbour (n, GNUNET_NO);
1757 /* still waiting on ATS suggestion */
1759 case S_INIT_BLACKLIST:
1760 /* check if the address the blacklist was fine with matches
1761 ATS suggestion, if so, we can move on! */
1762 if ( (GNUNET_OK == result) &&
1763 (1 == n->send_connect_ack) )
1765 n->send_connect_ack = 2;
1766 send_session_connect_ack_message (bcc->na.address,
1768 n->connect_ack_timestamp);
1770 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1771 break; /* result for an address we currently don't care about */
1772 if (GNUNET_OK == result)
1774 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1775 n->state = S_CONNECT_SENT;
1776 send_session_connect (&n->primary_address);
1780 // FIXME: should also possibly destroy session with plugin!?
1781 GNUNET_ATS_address_destroyed (GST_ats,
1784 free_address (&n->primary_address);
1785 n->state = S_INIT_ATS;
1786 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1787 // FIXME: do we need to ask ATS again for suggestions?
1788 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1791 case S_CONNECT_SENT:
1792 /* waiting on CONNECT_ACK, send ACK if one is pending */
1793 if ( (GNUNET_OK == result) &&
1794 (1 == n->send_connect_ack) )
1796 n->send_connect_ack = 2;
1797 send_session_connect_ack_message (n->primary_address.address,
1798 n->primary_address.session,
1799 n->connect_ack_timestamp);
1802 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1803 if (GNUNET_OK == result)
1805 /* valid new address, let ATS know! */
1806 GNUNET_ATS_address_add (GST_ats,
1809 bcc->ats, bcc->ats_count);
1811 n->state = S_CONNECT_RECV_ATS;
1812 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1813 GNUNET_ATS_reset_backoff (GST_ats, peer);
1814 GNUNET_ATS_suggest_address (GST_ats, peer);
1816 case S_CONNECT_RECV_ATS:
1817 /* still waiting on ATS suggestion, don't care about blacklist */
1819 case S_CONNECT_RECV_BLACKLIST:
1820 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1821 break; /* result for an address we currently don't care about */
1822 if (GNUNET_OK == result)
1824 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1825 n->state = S_CONNECT_RECV_ACK;
1826 send_session_connect_ack_message (bcc->na.address,
1828 n->connect_ack_timestamp);
1829 if (1 == n->send_connect_ack)
1830 n->send_connect_ack = 2;
1834 // FIXME: should also possibly destroy session with plugin!?
1835 GNUNET_ATS_address_destroyed (GST_ats,
1838 free_address (&n->primary_address);
1839 n->state = S_INIT_ATS;
1840 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1841 // FIXME: do we need to ask ATS again for suggestions?
1842 GNUNET_ATS_reset_backoff (GST_ats, peer);
1843 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1846 case S_CONNECT_RECV_ACK:
1847 /* waiting on SESSION_ACK, send ACK if one is pending */
1848 if ( (GNUNET_OK == result) &&
1849 (1 == n->send_connect_ack) )
1851 n->send_connect_ack = 2;
1852 send_session_connect_ack_message (n->primary_address.address,
1853 n->primary_address.session,
1854 n->connect_ack_timestamp);
1858 /* already connected, don't care about blacklist */
1860 case S_RECONNECT_ATS:
1861 /* still waiting on ATS suggestion, don't care about blacklist */
1863 case S_RECONNECT_BLACKLIST:
1864 if ( (GNUNET_OK == result) &&
1865 (1 == n->send_connect_ack) )
1867 n->send_connect_ack = 2;
1868 send_session_connect_ack_message (bcc->na.address,
1870 n->connect_ack_timestamp);
1872 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1873 break; /* result for an address we currently don't care about */
1874 if (GNUNET_OK == result)
1876 send_session_connect (&n->primary_address);
1877 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
1878 n->state = S_RECONNECT_SENT;
1882 GNUNET_ATS_address_destroyed (GST_ats,
1885 n->state = S_RECONNECT_ATS;
1886 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1887 // FIXME: do we need to ask ATS again for suggestions?
1888 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1891 case S_RECONNECT_SENT:
1892 /* waiting on CONNECT_ACK, don't care about blacklist */
1893 if ( (GNUNET_OK == result) &&
1894 (1 == n->send_connect_ack) )
1896 n->send_connect_ack = 2;
1897 send_session_connect_ack_message (n->primary_address.address,
1898 n->primary_address.session,
1899 n->connect_ack_timestamp);
1902 case S_CONNECTED_SWITCHING_BLACKLIST:
1903 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
1904 break; /* result for an address we currently don't care about */
1905 if (GNUNET_OK == result)
1907 send_session_connect (&n->alternative_address);
1908 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
1912 GNUNET_ATS_address_destroyed (GST_ats,
1915 free_address (&n->alternative_address);
1916 n->state = S_CONNECTED;
1919 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1920 /* waiting on CONNECT_ACK, don't care about blacklist */
1921 if ( (GNUNET_OK == result) &&
1922 (1 == n->send_connect_ack) )
1924 n->send_connect_ack = 2;
1925 send_session_connect_ack_message (n->primary_address.address,
1926 n->primary_address.session,
1927 n->connect_ack_timestamp);
1931 /* Nothing to do here, ATS will already do what can be done */
1933 case S_DISCONNECT_FINISHED:
1934 /* should not be possible */
1938 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1940 free_neighbour (n, GNUNET_NO);
1944 GNUNET_HELLO_address_free (bcc->na.address);
1945 GNUNET_CONTAINER_DLL_remove (bc_head,
1953 * We want to know if connecting to a particular peer via
1954 * a particular address is allowed. Check it!
1956 * @param peer identity of the peer to switch the address for
1957 * @param ts time at which the check was initiated
1958 * @param address address of the other peer, NULL if other peer
1960 * @param session session to use (or NULL)
1961 * @param ats performance data
1962 * @param ats_count number of entries in ats (excluding 0-termination)
1965 check_blacklist (const struct GNUNET_PeerIdentity *peer,
1966 struct GNUNET_TIME_Absolute ts,
1967 const struct GNUNET_HELLO_Address *address,
1968 struct Session *session,
1969 const struct GNUNET_ATS_Information *ats,
1972 struct BlackListCheckContext *bcc;
1973 struct GST_BlacklistCheck *bc;
1976 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
1977 sizeof (struct GNUNET_ATS_Information) * ats_count);
1978 bcc->ats_count = ats_count;
1979 bcc->na.address = GNUNET_HELLO_address_copy (address);
1980 bcc->na.session = session;
1981 bcc->na.connect_timestamp = ts;
1982 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
1983 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1984 GNUNET_CONTAINER_DLL_insert (bc_head,
1987 if (NULL != (bc = GST_blacklist_test_allowed (peer,
1988 address->transport_name,
1989 &handle_test_blacklist_cont, bcc)))
1991 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
1992 we must only store 'bc' if 'bc' is non-NULL... */
1997 * We received a 'SESSION_CONNECT' message from the other peer.
1998 * Consider switching to it.
2000 * @param message possibly a 'struct SessionConnectMessage' (check format)
2001 * @param peer identity of the peer to switch the address for
2002 * @param address address of the other peer, NULL if other peer
2004 * @param session session to use (or NULL)
2005 * @param ats performance data
2006 * @param ats_count number of entries in ats (excluding 0-termination)
2009 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2010 const struct GNUNET_PeerIdentity *peer,
2011 const struct GNUNET_HELLO_Address *address,
2012 struct Session *session,
2013 const struct GNUNET_ATS_Information *ats,
2016 const struct SessionConnectMessage *scm;
2017 struct NeighbourMapEntry *n;
2018 struct GNUNET_TIME_Absolute ts;
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 "Received CONNECT message from peer `%s'\n",
2024 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2026 GNUNET_break_op (0);
2029 if (NULL == neighbours)
2030 return; /* we're shutting down */
2031 scm = (const struct SessionConnectMessage *) message;
2032 GNUNET_break_op (0 == ntohl (scm->reserved));
2033 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2034 n = lookup_neighbour (peer);
2036 n = setup_neighbour (peer);
2037 n->send_connect_ack = 1;
2038 n->connect_ack_timestamp = ts;
2042 case S_NOT_CONNECTED:
2043 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2044 /* Do a blacklist check for the new address */
2045 check_blacklist (peer, ts, address, session, ats, ats_count);
2048 /* CONNECT message takes priority over us asking ATS for address */
2049 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2051 case S_INIT_BLACKLIST:
2052 case S_CONNECT_SENT:
2053 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2054 case S_CONNECT_RECV_ATS:
2055 case S_CONNECT_RECV_BLACKLIST:
2056 case S_CONNECT_RECV_ACK:
2057 /* It can never hurt to have an alternative address in the above cases,
2058 see if it is allowed */
2059 check_blacklist (peer, ts, address, session, ats, ats_count);
2062 /* we are already connected and can thus send the ACK immediately;
2063 still, it can never hurt to have an alternative address, so also
2064 tell ATS about it */
2065 GNUNET_assert (NULL != n->primary_address.address);
2066 GNUNET_assert (NULL != n->primary_address.session);
2067 n->send_connect_ack = 0;
2068 send_session_connect_ack_message (n->primary_address.address,
2069 n->primary_address.session, ts);
2070 check_blacklist (peer, ts, address, session, ats, ats_count);
2072 case S_RECONNECT_ATS:
2073 case S_RECONNECT_BLACKLIST:
2074 case S_RECONNECT_SENT:
2075 /* It can never hurt to have an alternative address in the above cases,
2076 see if it is allowed */
2077 check_blacklist (peer, ts, address, session, ats, ats_count);
2079 case S_CONNECTED_SWITCHING_BLACKLIST:
2080 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2081 /* we are already connected and can thus send the ACK immediately;
2082 still, it can never hurt to have an alternative address, so also
2083 tell ATS about it */
2084 GNUNET_assert (NULL != n->primary_address.address);
2085 GNUNET_assert (NULL != n->primary_address.session);
2086 n->send_connect_ack = 0;
2087 send_session_connect_ack_message (n->primary_address.address,
2088 n->primary_address.session, ts);
2089 check_blacklist (peer, ts, address, session, ats, ats_count);
2092 /* get rid of remains without terminating sessions, ready to re-try */
2093 free_neighbour (n, GNUNET_YES);
2094 n = setup_neighbour (peer);
2095 n->state = S_CONNECT_RECV_ATS;
2096 GNUNET_ATS_reset_backoff (GST_ats, peer);
2097 GNUNET_ATS_suggest_address (GST_ats, peer);
2099 case S_DISCONNECT_FINISHED:
2100 /* should not be possible */
2104 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2106 free_neighbour (n, GNUNET_NO);
2113 * For an existing neighbour record, set the active connection to
2114 * use the given address.
2116 * @param peer identity of the peer to switch the address for
2117 * @param address address of the other peer, NULL if other peer
2119 * @param session session to use (or NULL)
2120 * @param ats performance data
2121 * @param ats_count number of entries in ats
2122 * @param bandwidth_in inbound quota to be used when connection is up
2123 * @param bandwidth_out outbound quota to be used when connection is up
2126 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2127 const struct GNUNET_HELLO_Address *address,
2128 struct Session *session,
2129 const struct GNUNET_ATS_Information *ats,
2131 struct GNUNET_BANDWIDTH_Value32NBO
2133 struct GNUNET_BANDWIDTH_Value32NBO
2136 struct NeighbourMapEntry *n;
2137 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2139 GNUNET_assert (address->transport_name != NULL);
2140 if (NULL == (n = lookup_neighbour (peer)))
2143 /* Obtain an session for this address from plugin */
2144 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2146 /* we don't have the plugin for this address */
2147 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2150 if ((NULL == session) && (0 == address->address_length))
2153 if (strlen (address->transport_name) > 0)
2154 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2158 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2159 "ATS tells us to switch to address '%s' session %p for "
2160 "peer `%s' in state %s (quota in/out %u %u )\n",
2161 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2164 print_state (n->state),
2165 ntohl (bandwidth_in.value__),
2166 ntohl (bandwidth_out.value__));
2168 if (NULL == session)
2170 session = papi->get_session (papi->cls, address);
2171 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2172 "Obtained new session for peer `%s' and address '%s': %p\n",
2173 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2175 if (NULL == session)
2177 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2178 "Failed to obtain new session for peer `%s' and address '%s'\n",
2179 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2180 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2185 case S_NOT_CONNECTED:
2187 free_neighbour (n, GNUNET_NO);
2190 set_address (&n->primary_address,
2191 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2192 n->state = S_INIT_BLACKLIST;
2193 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2194 check_blacklist (&n->id,
2195 n->connect_ack_timestamp,
2196 address, session, ats, ats_count);
2198 case S_INIT_BLACKLIST:
2199 /* ATS suggests a different address, switch again */
2200 set_address (&n->primary_address,
2201 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2202 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2203 check_blacklist (&n->id,
2204 n->connect_ack_timestamp,
2205 address, session, ats, ats_count);
2207 case S_CONNECT_SENT:
2208 /* ATS suggests a different address, switch again */
2209 set_address (&n->primary_address,
2210 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2211 n->state = S_INIT_BLACKLIST;
2212 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2213 check_blacklist (&n->id,
2214 n->connect_ack_timestamp,
2215 address, session, ats, ats_count);
2217 case S_CONNECT_RECV_ATS:
2218 set_address (&n->primary_address,
2219 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2220 n->state = S_CONNECT_RECV_BLACKLIST;
2221 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2222 check_blacklist (&n->id,
2223 n->connect_ack_timestamp,
2224 address, session, ats, ats_count);
2226 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2227 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2228 check_blacklist (&n->id,
2229 n->connect_ack_timestamp,
2230 address, session, ats, ats_count);
2232 case S_CONNECT_RECV_BLACKLIST:
2233 case S_CONNECT_RECV_ACK:
2234 /* ATS asks us to switch while we were trying to connect; switch to new
2235 address and check blacklist again */
2236 set_address (&n->primary_address,
2237 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2238 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2239 check_blacklist (&n->id,
2240 n->connect_ack_timestamp,
2241 address, session, ats, ats_count);
2244 GNUNET_assert (NULL != n->primary_address.address);
2245 GNUNET_assert (NULL != n->primary_address.session);
2246 if (n->primary_address.session == session)
2248 /* not an address change, just a quota change */
2249 set_address (&n->primary_address,
2250 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2253 /* ATS asks us to switch a life connection; see if we can get
2254 a CONNECT_ACK on it before we actually do this! */
2255 set_address (&n->alternative_address,
2256 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2257 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2258 check_blacklist (&n->id,
2259 GNUNET_TIME_absolute_get (),
2260 address, session, ats, ats_count);
2262 case S_RECONNECT_ATS:
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_RECONNECT_BLACKLIST:
2272 /* ATS asks us to switch while we were trying to reconnect; switch to new
2273 address and check blacklist again */
2274 set_address (&n->primary_address,
2275 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2276 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2277 check_blacklist (&n->id,
2278 n->connect_ack_timestamp,
2279 address, session, ats, ats_count);
2281 case S_RECONNECT_SENT:
2282 /* ATS asks us to switch while we were trying to reconnect; switch to new
2283 address and check blacklist again */
2284 set_address (&n->primary_address,
2285 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2286 n->state = S_RECONNECT_BLACKLIST;
2287 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2288 check_blacklist (&n->id,
2289 n->connect_ack_timestamp,
2290 address, session, ats, ats_count);
2292 case S_CONNECTED_SWITCHING_BLACKLIST:
2293 if (n->primary_address.session == session)
2295 /* ATS switches back to still-active session */
2296 free_address (&n->alternative_address);
2297 n->state = S_CONNECTED;
2300 /* ATS asks us to switch a life connection, update blacklist check */
2301 set_address (&n->alternative_address,
2302 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2303 check_blacklist (&n->id,
2304 GNUNET_TIME_absolute_get (),
2305 address, session, ats, ats_count);
2307 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2308 if (n->primary_address.session == session)
2310 /* ATS switches back to still-active session */
2311 free_address (&n->alternative_address);
2312 n->state = S_CONNECTED;
2315 /* ATS asks us to switch a life connection, update blacklist check */
2316 set_address (&n->alternative_address,
2317 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2318 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2319 check_blacklist (&n->id,
2320 GNUNET_TIME_absolute_get (),
2321 address, session, ats, ats_count);
2324 /* not going to switch addresses while disconnecting */
2326 case S_DISCONNECT_FINISHED:
2330 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2338 * Master task run for every neighbour. Performs all of the time-related
2339 * activities (keep alive, send next message, disconnect if idle, finish
2340 * clean up after disconnect).
2342 * @param cls the 'struct NeighbourMapEntry' for which we are running
2343 * @param tc scheduler context (unused)
2346 master_task (void *cls,
2347 const struct GNUNET_SCHEDULER_TaskContext *tc)
2349 struct NeighbourMapEntry *n = cls;
2350 struct GNUNET_TIME_Relative delay;
2352 n->task = GNUNET_SCHEDULER_NO_TASK;
2353 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2355 "Master task runs for neighbour `%s' in state %s with timeout in %llu ms\n",
2356 GNUNET_i2s (&n->id),
2357 print_state(n->state),
2358 (unsigned long long) delay.rel_value);
2361 case S_NOT_CONNECTED:
2362 /* invalid state for master task, clean up */
2364 n->state = S_DISCONNECT_FINISHED;
2365 free_neighbour (n, GNUNET_NO);
2368 if (0 == delay.rel_value)
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2371 "Connection to `%s' timed out waiting for ATS to provide address\n",
2372 GNUNET_i2s (&n->id));
2373 n->state = S_DISCONNECT_FINISHED;
2374 free_neighbour (n, GNUNET_NO);
2378 case S_INIT_BLACKLIST:
2379 if (0 == delay.rel_value)
2381 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2382 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2383 GNUNET_i2s (&n->id));
2384 n->state = S_DISCONNECT_FINISHED;
2385 free_neighbour (n, GNUNET_NO);
2389 case S_CONNECT_SENT:
2390 if (0 == delay.rel_value)
2392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2393 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2394 GNUNET_i2s (&n->id));
2395 disconnect_neighbour (n);
2399 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2400 if (0 == delay.rel_value)
2402 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2403 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2404 GNUNET_i2s (&n->id));
2405 n->state = S_DISCONNECT_FINISHED;
2406 free_neighbour (n, GNUNET_NO);
2410 case S_CONNECT_RECV_ATS:
2411 if (0 == delay.rel_value)
2413 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2414 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2415 GNUNET_i2s (&n->id));
2416 n->state = S_DISCONNECT_FINISHED;
2417 free_neighbour (n, GNUNET_NO);
2421 case S_CONNECT_RECV_BLACKLIST:
2422 if (0 == delay.rel_value)
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2426 GNUNET_i2s (&n->id));
2427 n->state = S_DISCONNECT_FINISHED;
2428 free_neighbour (n, GNUNET_NO);
2432 case S_CONNECT_RECV_ACK:
2433 if (0 == delay.rel_value)
2435 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2436 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2437 GNUNET_i2s (&n->id));
2438 disconnect_neighbour (n);
2443 if (0 == delay.rel_value)
2445 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2446 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2447 GNUNET_i2s (&n->id));
2448 disconnect_neighbour (n);
2451 try_transmission_to_peer (n);
2454 case S_RECONNECT_ATS:
2455 if (0 == delay.rel_value)
2457 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2458 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2459 GNUNET_i2s (&n->id));
2460 disconnect_neighbour (n);
2464 case S_RECONNECT_BLACKLIST:
2465 if (0 == delay.rel_value)
2467 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2468 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2469 GNUNET_i2s (&n->id));
2470 disconnect_neighbour (n);
2474 case S_RECONNECT_SENT:
2475 if (0 == delay.rel_value)
2477 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2478 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2479 GNUNET_i2s (&n->id));
2480 disconnect_neighbour (n);
2484 case S_CONNECTED_SWITCHING_BLACKLIST:
2485 if (0 == delay.rel_value)
2487 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2488 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2489 GNUNET_i2s (&n->id));
2490 disconnect_neighbour (n);
2493 try_transmission_to_peer (n);
2496 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2497 if (0 == delay.rel_value)
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2501 GNUNET_i2s (&n->id));
2502 disconnect_neighbour (n);
2505 try_transmission_to_peer (n);
2509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2510 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2511 GNUNET_i2s (&n->id));
2512 free_neighbour (n, GNUNET_NO);
2514 case S_DISCONNECT_FINISHED:
2515 /* how did we get here!? */
2519 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2523 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2524 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2525 (S_CONNECTED == n->state) )
2527 /* if we are *now* in one of these three states, we're sending
2528 keep alive messages, so we need to consider the keepalive
2529 delay, not just the connection timeout */
2530 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2533 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2534 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2541 * Send a SESSION_ACK message to the neighbour to confirm that we
2542 * got his CONNECT_ACK.
2544 * @param n neighbour to send the SESSION_ACK to
2547 send_session_ack_message (struct NeighbourMapEntry *n)
2549 struct GNUNET_MessageHeader msg;
2551 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2552 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2553 (void) send_with_session(n,
2554 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2555 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2561 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2562 * Consider switching to it.
2564 * @param message possibly a 'struct SessionConnectMessage' (check format)
2565 * @param peer identity of the peer to switch the address for
2566 * @param address address of the other peer, NULL if other peer
2568 * @param session session to use (or NULL)
2569 * @param ats performance data
2570 * @param ats_count number of entries in ats
2573 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2574 const struct GNUNET_PeerIdentity *peer,
2575 const struct GNUNET_HELLO_Address *address,
2576 struct Session *session,
2577 const struct GNUNET_ATS_Information *ats,
2580 const struct SessionConnectMessage *scm;
2581 struct GNUNET_TIME_Absolute ts;
2582 struct NeighbourMapEntry *n;
2584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2585 "Received CONNECT_ACK message from peer `%s'\n",
2588 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2590 GNUNET_break_op (0);
2593 scm = (const struct SessionConnectMessage *) message;
2594 GNUNET_break_op (ntohl (scm->reserved) == 0);
2595 if (NULL == (n = lookup_neighbour (peer)))
2597 GNUNET_STATISTICS_update (GST_stats,
2599 ("# unexpected CONNECT_ACK messages (no peer)"),
2603 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2606 case S_NOT_CONNECTED:
2608 free_neighbour (n, GNUNET_NO);
2611 case S_INIT_BLACKLIST:
2612 GNUNET_STATISTICS_update (GST_stats,
2614 ("# unexpected CONNECT_ACK messages (not ready)"),
2617 case S_CONNECT_SENT:
2618 if (ts.abs_value != n->primary_address.connect_timestamp.abs_value)
2619 break; /* ACK does not match our original CONNECT message */
2620 n->state = S_CONNECTED;
2621 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2622 GNUNET_STATISTICS_set (GST_stats,
2623 gettext_noop ("# peers connected"),
2624 ++neighbours_connected,
2626 connect_notify_cb (callback_cls, &n->id, ats, ats_count,
2627 n->primary_address.bandwidth_in,
2628 n->primary_address.bandwidth_out);
2629 /* Tell ATS that the outbound session we created to send CONNECT was successfull */
2630 GNUNET_ATS_address_add (GST_ats,
2631 n->primary_address.address,
2632 n->primary_address.session,
2634 set_address (&n->primary_address,
2635 n->primary_address.address,
2636 n->primary_address.session,
2637 n->primary_address.bandwidth_in,
2638 n->primary_address.bandwidth_out,
2640 send_session_ack_message (n);
2642 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2643 case S_CONNECT_RECV_ATS:
2644 case S_CONNECT_RECV_BLACKLIST:
2645 case S_CONNECT_RECV_ACK:
2646 GNUNET_STATISTICS_update (GST_stats,
2648 ("# unexpected CONNECT_ACK messages (not ready)"),
2652 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2653 send_session_ack_message (n);
2655 case S_RECONNECT_ATS:
2656 case S_RECONNECT_BLACKLIST:
2657 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2658 to give us a new address... */
2659 GNUNET_STATISTICS_update (GST_stats,
2661 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2664 case S_RECONNECT_SENT:
2665 /* new address worked; go back to connected! */
2666 n->state = S_CONNECTED;
2667 send_session_ack_message (n);
2669 case S_CONNECTED_SWITCHING_BLACKLIST:
2670 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2671 send_session_ack_message (n);
2673 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2674 /* new address worked; adopt it and go back to connected! */
2675 n->state = S_CONNECTED;
2676 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2677 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2678 GNUNET_ATS_address_add(GST_ats,
2679 n->alternative_address.address,
2680 n->alternative_address.session,
2682 set_address (&n->primary_address,
2683 n->alternative_address.address,
2684 n->alternative_address.session,
2685 n->alternative_address.bandwidth_in,
2686 n->alternative_address.bandwidth_out,
2688 free_address (&n->alternative_address);
2689 send_session_ack_message (n);
2692 GNUNET_STATISTICS_update (GST_stats,
2694 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2697 case S_DISCONNECT_FINISHED:
2701 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2709 * A session was terminated. Take note; if needed, try to get
2710 * an alternative address from ATS.
2712 * @param peer identity of the peer where the session died
2713 * @param session session that is gone
2714 * @return GNUNET_YES if this was a session used, GNUNET_NO if
2715 * this session was not in use
2718 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2719 struct Session *session)
2721 struct NeighbourMapEntry *n;
2722 struct BlackListCheckContext *bcc;
2723 struct BlackListCheckContext *bcc_next;
2725 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2727 while (NULL != (bcc = bcc_next))
2729 bcc_next = bcc->next;
2730 if (bcc->na.session == session)
2732 GST_blacklist_test_cancel (bcc->bc);
2733 GNUNET_HELLO_address_free (bcc->na.address);
2734 GNUNET_CONTAINER_DLL_remove (bc_head,
2740 if (NULL == (n = lookup_neighbour (peer)))
2741 return GNUNET_NO; /* can't affect us */
2742 if (session != n->primary_address.session)
2744 if (session == n->alternative_address.session)
2746 free_address (&n->alternative_address);
2747 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2748 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2749 n->state = S_CONNECTED;
2753 return GNUNET_NO; /* doesn't affect us further */
2756 n->expect_latency_response = GNUNET_NO;
2759 case S_NOT_CONNECTED:
2761 free_neighbour (n, GNUNET_NO);
2765 free_neighbour (n, GNUNET_NO);
2767 case S_INIT_BLACKLIST:
2768 case S_CONNECT_SENT:
2769 free_address (&n->primary_address);
2770 n->state = S_INIT_ATS;
2771 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2772 // FIXME: need to ask ATS for suggestions again?
2773 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2775 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2776 case S_CONNECT_RECV_ATS:
2777 case S_CONNECT_RECV_BLACKLIST:
2778 case S_CONNECT_RECV_ACK:
2779 /* error on inbound session; free neighbour entirely */
2780 free_address (&n->primary_address);
2781 free_neighbour (n, GNUNET_NO);
2784 free_address (&n->primary_address);
2785 n->state = S_RECONNECT_ATS;
2786 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2787 /* FIXME: is this ATS call needed? */
2788 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2790 case S_RECONNECT_ATS:
2791 /* we don't have an address, how can it go down? */
2794 case S_RECONNECT_BLACKLIST:
2795 case S_RECONNECT_SENT:
2796 n->state = S_RECONNECT_ATS;
2797 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2798 // FIXME: need to ask ATS for suggestions again?
2799 GNUNET_ATS_suggest_address (GST_ats, &n->id);
2801 case S_CONNECTED_SWITCHING_BLACKLIST:
2802 /* primary went down while we were checking secondary against
2803 blacklist, adopt secondary as primary */
2804 free_address (&n->primary_address);
2805 n->primary_address = n->alternative_address;
2806 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2807 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2808 n->state = S_RECONNECT_BLACKLIST;
2810 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2811 /* primary went down while we were waiting for CONNECT_ACK on secondary;
2812 secondary as primary */
2813 free_address (&n->primary_address);
2814 n->primary_address = n->alternative_address;
2815 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2816 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2817 n->state = S_RECONNECT_SENT;
2820 free_address (&n->primary_address);
2822 case S_DISCONNECT_FINISHED:
2823 /* neighbour was freed and plugins told to terminate session */
2827 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2831 if (GNUNET_SCHEDULER_NO_TASK != n->task)
2832 GNUNET_SCHEDULER_cancel (n->task);
2833 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
2839 * We received a 'SESSION_ACK' message from the other peer.
2840 * If we sent a 'CONNECT_ACK' last, this means we are now
2841 * connected. Otherwise, do nothing.
2843 * @param message possibly a 'struct SessionConnectMessage' (check format)
2844 * @param peer identity of the peer to switch the address for
2845 * @param address address of the other peer, NULL if other peer
2847 * @param session session to use (or NULL)
2848 * @param ats performance data
2849 * @param ats_count number of entries in ats
2852 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
2853 const struct GNUNET_PeerIdentity *peer,
2854 const struct GNUNET_HELLO_Address *address,
2855 struct Session *session,
2856 const struct GNUNET_ATS_Information *ats,
2859 struct NeighbourMapEntry *n;
2861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2862 "Received SESSION_ACK message from peer `%s'\n",
2864 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2866 GNUNET_break_op (0);
2869 if (NULL == (n = lookup_neighbour (peer)))
2871 /* check if we are in a plausible state for having sent
2872 a CONNECT_ACK. If not, return, otherwise break */
2873 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
2874 (S_CONNECT_SENT != n->state) ) ||
2875 (2 != n->send_connect_ack) )
2877 GNUNET_STATISTICS_update (GST_stats,
2878 gettext_noop ("# unexpected SESSION ACK messages"), 1,
2882 n->state = S_CONNECTED;
2883 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2884 GNUNET_STATISTICS_set (GST_stats,
2885 gettext_noop ("# peers connected"),
2886 ++neighbours_connected,
2888 connect_notify_cb (callback_cls, &n->id, ats, ats_count,
2889 n->primary_address.bandwidth_in,
2890 n->primary_address.bandwidth_out);
2891 GNUNET_ATS_address_add(GST_ats,
2892 n->primary_address.address,
2893 n->primary_address.session,
2895 set_address (&n->primary_address,
2896 n->primary_address.address,
2897 n->primary_address.session,
2898 n->primary_address.bandwidth_in,
2899 n->primary_address.bandwidth_out,
2905 * Test if we're connected to the given peer.
2907 * @param target peer to test
2908 * @return GNUNET_YES if we are connected, GNUNET_NO if not
2911 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
2913 return test_connected (lookup_neighbour (target));
2918 * Change the incoming quota for the given peer.
2920 * @param neighbour identity of peer to change qutoa for
2921 * @param quota new quota
2924 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2925 struct GNUNET_BANDWIDTH_Value32NBO quota)
2927 struct NeighbourMapEntry *n;
2929 if (NULL == (n = lookup_neighbour (neighbour)))
2931 GNUNET_STATISTICS_update (GST_stats,
2933 ("# SET QUOTA messages ignored (no such peer)"),
2937 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2938 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2939 ntohl (quota.value__), GNUNET_i2s (&n->id));
2940 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2941 if (0 != ntohl (quota.value__))
2943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2944 GNUNET_i2s (&n->id), "SET_QUOTA");
2945 if (GNUNET_YES == test_connected (n))
2946 GNUNET_STATISTICS_update (GST_stats,
2947 gettext_noop ("# disconnects due to quota of 0"),
2949 disconnect_neighbour (n);
2954 * We received a disconnect message from the given peer,
2955 * validate and process.
2957 * @param peer sender of the message
2958 * @param msg the disconnect message
2961 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2963 const struct GNUNET_MessageHeader
2966 struct NeighbourMapEntry *n;
2967 const struct SessionDisconnectMessage *sdm;
2968 struct GNUNET_HashCode hc;
2970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2971 "Received DISCONNECT message from peer `%s'\n",
2973 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2975 // GNUNET_break_op (0);
2976 GNUNET_STATISTICS_update (GST_stats,
2978 ("# disconnect messages ignored (old format)"), 1,
2982 sdm = (const struct SessionDisconnectMessage *) msg;
2983 if (NULL == (n = lookup_neighbour (peer)))
2984 return; /* gone already */
2985 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <= n->connect_ack_timestamp.abs_value)
2987 GNUNET_STATISTICS_update (GST_stats,
2989 ("# disconnect messages ignored (timestamp)"), 1,
2993 GNUNET_CRYPTO_hash (&sdm->public_key,
2994 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2996 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2998 GNUNET_break_op (0);
3001 if (ntohl (sdm->purpose.size) !=
3002 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
3003 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
3004 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3006 GNUNET_break_op (0);
3010 GNUNET_CRYPTO_rsa_verify
3011 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3012 &sdm->signature, &sdm->public_key))
3014 GNUNET_break_op (0);
3017 if (GNUNET_YES == test_connected (n))
3018 GNUNET_STATISTICS_update (GST_stats,
3020 ("# other peer asked to disconnect from us"), 1,
3022 disconnect_neighbour (n);
3027 * Closure for the neighbours_iterate function.
3029 struct IteratorContext
3032 * Function to call on each connected neighbour.
3034 GST_NeighbourIterator cb;
3044 * Call the callback from the closure for each connected neighbour.
3046 * @param cls the 'struct IteratorContext'
3047 * @param key the hash of the public key of the neighbour
3048 * @param value the 'struct NeighbourMapEntry'
3049 * @return GNUNET_OK (continue to iterate)
3052 neighbours_iterate (void *cls, const struct GNUNET_HashCode * key, void *value)
3054 struct IteratorContext *ic = cls;
3055 struct NeighbourMapEntry *n = value;
3057 if (GNUNET_YES == test_connected (n))
3059 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3060 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3062 if (NULL != n->primary_address.address)
3064 bandwidth_in = n->primary_address.bandwidth_in;
3065 bandwidth_out = n->primary_address.bandwidth_out;
3069 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3070 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3073 ic->cb (ic->cb_cls, &n->id, NULL, 0,
3074 n->primary_address.address,
3075 bandwidth_in, bandwidth_out);
3082 * Iterate over all connected neighbours.
3084 * @param cb function to call
3085 * @param cb_cls closure for cb
3088 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3090 struct IteratorContext ic;
3092 if (NULL == neighbours)
3093 return; /* can happen during shutdown */
3096 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
3101 * If we have an active connection to the given target, it must be shutdown.
3103 * @param target peer to disconnect from
3106 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3108 struct NeighbourMapEntry *n;
3110 if (NULL == (n = lookup_neighbour (target)))
3111 return; /* not active */
3112 if (GNUNET_YES == test_connected (n))
3113 GNUNET_STATISTICS_update (GST_stats,
3115 ("# disconnected from peer upon explicit request"), 1,
3117 disconnect_neighbour (n);
3122 * Obtain current latency information for the given neighbour.
3124 * @param peer to get the latency for
3125 * @return observed latency of the address, FOREVER if the
3126 * the connection is not up
3128 struct GNUNET_TIME_Relative
3129 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3131 struct NeighbourMapEntry *n;
3133 n = lookup_neighbour (peer);
3135 return GNUNET_TIME_UNIT_FOREVER_REL;
3139 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3140 case S_CONNECTED_SWITCHING_BLACKLIST:
3141 case S_RECONNECT_SENT:
3142 case S_RECONNECT_ATS:
3143 case S_RECONNECT_BLACKLIST:
3145 case S_NOT_CONNECTED:
3146 case S_INIT_BLACKLIST:
3148 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3149 case S_CONNECT_RECV_ATS:
3150 case S_CONNECT_RECV_BLACKLIST:
3151 case S_CONNECT_RECV_ACK:
3152 case S_CONNECT_SENT:
3154 case S_DISCONNECT_FINISHED:
3155 return GNUNET_TIME_UNIT_FOREVER_REL;
3157 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3161 return GNUNET_TIME_UNIT_FOREVER_REL;
3166 * Obtain current address information for the given neighbour.
3169 * @return address currently used
3171 struct GNUNET_HELLO_Address *
3172 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3174 struct NeighbourMapEntry *n;
3176 n = lookup_neighbour (peer);
3179 return n->primary_address.address;
3184 * Initialize the neighbours subsystem.
3186 * @param cls closure for callbacks
3187 * @param connect_cb function to call if we connect to a peer
3188 * @param disconnect_cb function to call if we disconnect from a peer
3189 * @param peer_address_cb function to call if we change an active address
3193 GST_neighbours_start (void *cls,
3194 NotifyConnect connect_cb,
3195 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3196 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
3199 connect_notify_cb = connect_cb;
3200 disconnect_notify_cb = disconnect_cb;
3201 address_change_cb = peer_address_cb;
3202 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3207 * Disconnect from the given neighbour.
3210 * @param key hash of neighbour's public key (not used)
3211 * @param value the 'struct NeighbourMapEntry' of the neighbour
3212 * @return GNUNET_OK (continue to iterate)
3215 disconnect_all_neighbours (void *cls, const struct GNUNET_HashCode * key, void *value)
3217 struct NeighbourMapEntry *n = value;
3219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3220 "Disconnecting peer `%4s', %s\n",
3221 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3222 n->state = S_DISCONNECT_FINISHED;
3223 free_neighbour (n, GNUNET_NO);
3229 * Cleanup the neighbours subsystem.
3232 GST_neighbours_stop ()
3234 if (NULL == neighbours)
3236 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
3237 &disconnect_all_neighbours,
3239 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
3241 callback_cls = NULL;
3242 connect_notify_cb = NULL;
3243 disconnect_notify_cb = NULL;
3244 address_change_cb = NULL;
3248 /* end of file gnunet-service-transport_neighbours.c */