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)
95 #define DEBUG_MALLOC GNUNET_NO
101 struct Allocator *prev;
102 struct Allocator *next;
104 unsigned int bytes_alloced;
105 unsigned int max_alloced;
109 struct GNUNET_TIME_Absolute max_alloced_when;
110 struct GNUNET_TIME_Absolute last_alloced_when;
114 struct Allocator *aehead;
115 struct Allocator *aetail;
119 struct Allocation *prev;
120 struct Allocation *next;
122 struct Allocator *alloc;
123 unsigned int bytes_alloced;
128 struct Allocation *ahead;
129 struct Allocation *atail;
131 static int bytes_alloced;
133 static struct Allocator *
134 find_allocator (int line)
136 struct Allocator *cur = aehead;
139 if (line == cur->line)
149 static int start = GNUNET_YES;
150 static struct GNUNET_TIME_Absolute next;
151 static struct GNUNET_TIME_Relative rem;
152 struct Allocator *cur = aehead;
155 next = GNUNET_TIME_UNIT_ZERO_ABS;
158 if (0 == (rem = GNUNET_TIME_absolute_get_remaining(next)).rel_value)
160 fprintf (stderr, "Allocated in `%s' total: %5u bytes\n", __FILE__, bytes_alloced);
163 char *last_alloc = GNUNET_strdup (GNUNET_STRINGS_absolute_time_to_string(cur->max_alloced_when));
164 fprintf (stderr, "Allocated from line %4u :%5u bytes (diff %5i bytes, max alloc: %5u @ %s, last alloc %s)\n",
165 cur->line, cur->bytes_alloced, cur->diff, cur->max_alloced,
167 GNUNET_STRINGS_absolute_time_to_string(cur->last_alloced_when));
168 GNUNET_free (last_alloc);
172 fprintf (stderr, "\n");
173 next = GNUNET_TIME_absolute_add(GNUNET_TIME_absolute_get(), GNUNET_TIME_UNIT_SECONDS);
180 MEMDEBUG_add_alloc (void *p, size_t size, int line)
183 struct Allocation *alloc = GNUNET_malloc (sizeof (struct Allocation));
184 struct Allocator *allocator = find_allocator(line);
185 if (NULL == allocator)
187 allocator = GNUNET_malloc (sizeof (struct Allocator));
188 allocator->line = line;
189 GNUNET_CONTAINER_DLL_insert (aehead, aetail, allocator);
191 alloc->alloc = allocator;
194 alloc->bytes_alloced = size;
195 allocator->bytes_alloced += size;
196 allocator->last_alloced_when = GNUNET_TIME_absolute_get();
197 if (allocator->bytes_alloced >= allocator->max_alloced)
199 allocator->max_alloced = allocator->bytes_alloced;
200 allocator->max_alloced_when = allocator->last_alloced_when;
202 allocator->diff += size;
203 GNUNET_CONTAINER_DLL_insert (ahead, atail, alloc);
205 bytes_alloced += size;
211 MEMDEBUG_malloc (size_t size, int line)
215 ret = GNUNET_malloc (size);
218 MEMDEBUG_add_alloc (ret, size, line);
225 MEMDEBUG_free (void * alloc, int line)
228 struct Allocation *cur;
229 struct Allocator *allocator;
239 fprintf (stderr, "Unmonitored free from line %4u\n", line);
243 allocator = cur->alloc;
244 if (NULL == allocator)
248 GNUNET_CONTAINER_DLL_remove (ahead, atail, cur);
249 allocator->bytes_alloced -= cur->bytes_alloced;
250 allocator->diff -= cur->bytes_alloced;
251 GNUNET_assert (allocator->bytes_alloced >= 0);
252 bytes_alloced -= cur->bytes_alloced;
253 GNUNET_assert (bytes_alloced >= 0);
260 MEMDEBUG_free_non_null (void * alloc, int line)
263 MEMDEBUG_free (alloc, line);
267 GNUNET_NETWORK_STRUCT_BEGIN
270 * Message a peer sends to another to indicate that it intends to
271 * setup a connection/session for data exchange. A 'SESSION_CONNECT'
272 * should be answered with a 'SESSION_CONNECT_ACK' with the same body
273 * to confirm. A 'SESSION_CONNECT_ACK' should then be followed with
274 * a 'SESSION_ACK'. Once the 'SESSION_ACK' is received, both peers
275 * should be connected.
277 struct SessionConnectMessage
280 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
281 * or 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK'
283 struct GNUNET_MessageHeader header;
288 uint32_t reserved GNUNET_PACKED;
291 * Absolute time at the sender. Only the most recent connect
292 * message implies which session is preferred by the sender.
294 struct GNUNET_TIME_AbsoluteNBO timestamp;
300 * Message we send to the other peer to notify him that we intentionally
301 * are disconnecting (to reduce timeouts). This is just a friendly
302 * notification, peers must not rely on always receiving disconnect
305 struct SessionDisconnectMessage
308 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
310 struct GNUNET_MessageHeader header;
315 uint32_t reserved GNUNET_PACKED;
318 * Purpose of the signature. Extends over the timestamp.
319 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
321 struct GNUNET_CRYPTO_EccSignaturePurpose purpose;
324 * Absolute time at the sender. Only the most recent connect
325 * message implies which session is preferred by the sender.
327 struct GNUNET_TIME_AbsoluteNBO timestamp;
330 * Public key of the sender.
332 struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded public_key;
335 * Signature of the peer that sends us the disconnect. Only
336 * valid if the timestamp is AFTER the timestamp from the
337 * corresponding 'CONNECT' message.
339 struct GNUNET_CRYPTO_EccSignature signature;
343 GNUNET_NETWORK_STRUCT_END
347 * For each neighbour we keep a list of messages
348 * that we still want to transmit to the neighbour.
354 * This is a doubly linked list.
356 struct MessageQueue *next;
359 * This is a doubly linked list.
361 struct MessageQueue *prev;
364 * Function to call once we're done.
366 GST_NeighbourSendContinuation cont;
374 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
375 * stuck together in memory. Allocated at the end of this struct.
377 const char *message_buf;
380 * Size of the message buf
382 size_t message_buf_size;
385 * At what time should we fail?
387 struct GNUNET_TIME_Absolute timeout;
393 * Possible state of a neighbour. Initially, we are S_NOT_CONNECTED.
395 * Then, there are two main paths. If we receive a CONNECT message, we
396 * first run a check against the blacklist (S_CONNECT_RECV_BLACKLIST_INBOUND).
397 * If this check is successful, we give the inbound address to ATS.
398 * After the check we ask ATS for a suggestion (S_CONNECT_RECV_ATS).
399 * If ATS makes a suggestion, we ALSO give that suggestion to the blacklist
400 * (S_CONNECT_RECV_BLACKLIST). Once the blacklist approves the
401 * address we got from ATS, we send our CONNECT_ACK and go to
402 * S_CONNECT_RECV_ACK. If we receive a SESSION_ACK, we go to
403 * S_CONNECTED (and notify everyone about the new connection). If the
404 * operation times out, we go to S_DISCONNECT.
406 * The other case is where we transmit a CONNECT message first. We
407 * start with S_INIT_ATS. If we get an address, we enter
408 * S_INIT_BLACKLIST and check the blacklist. If the blacklist is OK
409 * with the connection, we actually send the CONNECT message and go to
410 * state S_CONNECT_SENT. Once we receive a CONNECT_ACK, we go to
411 * S_CONNECTED (and notify everyone about the new connection and send
412 * back a SESSION_ACK). If the operation times out, we go to
415 * If the session is in trouble (i.e. transport-level disconnect or
416 * timeout), we go to S_RECONNECT_ATS where we ask ATS for a new
417 * address (we don't notify anyone about the disconnect yet). Once we
418 * have a new address, we go to S_RECONNECT_BLACKLIST to check the new
419 * address against the blacklist. If the blacklist approves, we enter
420 * S_RECONNECT_SENT and send a CONNECT message. If we receive a
421 * CONNECT_ACK, we go to S_CONNECTED and nobody noticed that we had
422 * trouble; we also send a SESSION_ACK at this time just in case. If
423 * the operation times out, we go to S_DISCONNECT (and notify everyone
424 * about the lost connection).
426 * If ATS decides to switch addresses while we have a normal
427 * connection, we go to S_CONNECTED_SWITCHING_BLACKLIST to check the
428 * new address against the blacklist. If the blacklist approves, we
429 * go to S_CONNECTED_SWITCHING_CONNECT_SENT and send a
430 * SESSION_CONNECT. If we get a SESSION_ACK back, we switch the
431 * primary connection to the suggested alternative from ATS, go back
432 * to S_CONNECTED and send a SESSION_ACK to the other peer just to be
433 * sure. If the operation times out (or the blacklist disapproves),
434 * we go to S_CONNECTED (and notify ATS that the given alternative
435 * address is "invalid").
437 * Once a session is in S_DISCONNECT, it is cleaned up and then goes
438 * to (S_DISCONNECT_FINISHED). If we receive an explicit disconnect
439 * request, we can go from any state to S_DISCONNECT, possibly after
440 * generating disconnect notifications.
442 * Note that it is quite possible that while we are in any of these
443 * states, we could receive a 'CONNECT' request from the other peer.
444 * We then enter a 'weird' state where we pursue our own primary state
445 * machine (as described above), but with the 'send_connect_ack' flag
446 * set to 1. If our state machine allows us to send a 'CONNECT_ACK'
447 * (because we have an acceptable address), we send the 'CONNECT_ACK'
448 * and set the 'send_connect_ack' to 2. If we then receive a
449 * 'SESSION_ACK', we go to 'S_CONNECTED' (and reset 'send_connect_ack'
456 * fresh peer or completely disconnected
461 * Asked to initiate connection, trying to get address from ATS
466 * Asked to initiate connection, trying to get address approved
472 * Sent CONNECT message to other peer, waiting for CONNECT_ACK
477 * Received a CONNECT, do a blacklist check for inbound address
479 S_CONNECT_RECV_BLACKLIST_INBOUND,
482 * Received a CONNECT, asking ATS about address suggestions.
487 * Received CONNECT from other peer, got an address, checking with blacklist.
489 S_CONNECT_RECV_BLACKLIST,
492 * CONNECT request from other peer was SESSION_ACK'ed, waiting for
498 * Got our CONNECT_ACK/SESSION_ACK, connection is up.
503 * Connection got into trouble, rest of the system still believes
504 * it to be up, but we're getting a new address from ATS.
509 * Connection got into trouble, rest of the system still believes
510 * it to be up; we are checking the new address against the blacklist.
512 S_RECONNECT_BLACKLIST,
515 * Sent CONNECT over new address (either by ATS telling us to switch
516 * addresses or from RECONNECT_ATS); if this fails, we need to tell
517 * the rest of the system about a disconnect.
522 * We have some primary connection, but ATS suggested we switch
523 * to some alternative; we're now checking the alternative against
526 S_CONNECTED_SWITCHING_BLACKLIST,
529 * We have some primary connection, but ATS suggested we switch
530 * to some alternative; we now sent a CONNECT message for the
531 * alternative session to the other peer and waiting for a
532 * CONNECT_ACK to make this our primary connection.
534 S_CONNECTED_SWITCHING_CONNECT_SENT,
537 * Disconnect in progress (we're sending the DISCONNECT message to the
538 * other peer; after that is finished, the state will be cleaned up).
543 * We're finished with the disconnect; and are cleaning up the state
544 * now! We put the struct into this state when we are really in the
545 * task that calls 'free' on it and are about to remove the record
546 * from the map. We should never find a 'struct NeighbourMapEntry'
547 * in this state in the map. Accessing a 'struct NeighbourMapEntry'
548 * in this state virtually always means using memory that has been
549 * freed (the exception being the cleanup code in 'free_neighbour').
551 S_DISCONNECT_FINISHED
556 * A possible address we could use to communicate with a neighbour.
558 struct NeighbourAddress
562 * Active session for this address.
564 struct Session *session;
567 * Network-level address information.
569 struct GNUNET_HELLO_Address *address;
572 * Timestamp of the 'SESSION_CONNECT' message we sent to the other
573 * peer for this address. Use to check that the ACK is in response
574 * to our most recent 'CONNECT'.
576 struct GNUNET_TIME_Absolute connect_timestamp;
579 * Inbound bandwidth from ATS for this address.
581 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
584 * Outbound bandwidth from ATS for this address.
586 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
589 * Did we tell ATS that this is our 'active' address?
597 * Entry in neighbours.
599 struct NeighbourMapEntry
603 * Head of list of messages we would like to send to this peer;
604 * must contain at most one message per client.
606 struct MessageQueue *messages_head;
609 * Tail of list of messages we would like to send to this peer; must
610 * contain at most one message per client.
612 struct MessageQueue *messages_tail;
615 * Are we currently trying to send a message? If so, which one?
617 struct MessageQueue *is_active;
620 * Primary address we currently use to communicate with the neighbour.
622 struct NeighbourAddress primary_address;
625 * Alternative address currently under consideration for communicating
626 * with the neighbour.
628 struct NeighbourAddress alternative_address;
631 * Identity of this neighbour.
633 struct GNUNET_PeerIdentity id;
636 * Main task that drives this peer (timeouts, keepalives, etc.).
637 * Always runs the 'master_task'.
639 GNUNET_SCHEDULER_TaskIdentifier task;
642 * At what time should we sent the next keep-alive message?
644 struct GNUNET_TIME_Absolute keep_alive_time;
647 * At what time did we sent the last keep-alive message? Used
648 * to calculate round-trip time ("latency").
650 struct GNUNET_TIME_Absolute last_keep_alive_time;
653 * Timestamp we should include in our next CONNECT_ACK message.
654 * (only valid if 'send_connect_ack' is GNUNET_YES). Used to build
655 * our CONNECT_ACK message.
657 struct GNUNET_TIME_Absolute connect_ack_timestamp;
660 * ATS address suggest handle
662 struct GNUNET_ATS_SuggestHandle *suggest_handle;
665 * Time where we should cut the connection (timeout) if we don't
666 * make progress in the state machine (or get a KEEPALIVE_RESPONSE
667 * if we are in S_CONNECTED).
669 struct GNUNET_TIME_Absolute timeout;
672 * Latest calculated latency value
674 struct GNUNET_TIME_Relative latency;
677 * Tracker for inbound bandwidth.
679 struct GNUNET_BANDWIDTH_Tracker in_tracker;
682 * How often has the other peer (recently) violated the inbound
683 * traffic limit? Incremented by 10 per violation, decremented by 1
684 * per non-violation (for each time interval).
686 unsigned int quota_violation_count;
689 * The current state of the peer.
694 * Did we sent an KEEP_ALIVE message and are we expecting a response?
696 int expect_latency_response;
699 * Flag to set if we still need to send a CONNECT_ACK message to the other peer
700 * (once we have an address to use and the peer has been allowed by our
701 * blacklist). Set to 1 if we need to send a CONNECT_ACK. Set to 2 if we
702 * did send a CONNECT_ACK and should go to 'S_CONNECTED' upon receiving
703 * a 'SESSION_ACK' (regardless of what our own state machine might say).
705 int send_connect_ack;
711 * Context for blacklist checks and the 'handle_test_blacklist_cont'
712 * function. Stores information about ongoing blacklist checks.
714 struct BlackListCheckContext
718 * We keep blacklist checks in a DLL.
720 struct BlackListCheckContext *next;
723 * We keep blacklist checks in a DLL.
725 struct BlackListCheckContext *prev;
728 * Address that is being checked.
730 struct NeighbourAddress na;
733 * ATS information about the address.
735 struct GNUNET_ATS_Information *ats;
738 * Handle to the ongoing blacklist check.
740 struct GST_BlacklistCheck *bc;
743 * Size of the 'ats' array.
751 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
753 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
756 * We keep blacklist checks in a DLL so that we can find
757 * the 'sessions' in their 'struct NeighbourAddress' if
758 * a session goes down.
760 static struct BlackListCheckContext *bc_head;
763 * We keep blacklist checks in a DLL.
765 static struct BlackListCheckContext *bc_tail;
768 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
770 static void *callback_cls;
773 * Function to call when we connected to a neighbour.
775 static NotifyConnect connect_notify_cb;
778 * Function to call when we disconnected from a neighbour.
780 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
783 * Function to call when we changed an active address of a neighbour.
785 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
788 * counter for connected neighbours
790 static unsigned int neighbours_connected;
793 * Number of bytes we have currently queued for transmission.
795 static unsigned long long bytes_in_send_queue;
799 * Lookup a neighbour entry in the neighbours hash map.
801 * @param pid identity of the peer to look up
802 * @return the entry, NULL if there is no existing record
804 static struct NeighbourMapEntry *
805 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
807 if (NULL == neighbours)
809 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
813 print_state (int state)
818 case S_NOT_CONNECTED:
819 return "S_NOT_CONNECTED";
822 case S_INIT_BLACKLIST:
823 return "S_INIT_BLACKLIST";
825 return "S_CONNECT_SENT";
826 case S_CONNECT_RECV_BLACKLIST_INBOUND:
827 return "S_CONNECT_RECV_BLACKLIST_INBOUND";
828 case S_CONNECT_RECV_ATS:
829 return "S_CONNECT_RECV_ATS";
830 case S_CONNECT_RECV_BLACKLIST:
831 return "S_CONNECT_RECV_BLACKLIST";
832 case S_CONNECT_RECV_ACK:
833 return "S_CONNECT_RECV_ACK";
835 return "S_CONNECTED";
836 case S_RECONNECT_ATS:
837 return "S_RECONNECT_ATS";
838 case S_RECONNECT_BLACKLIST:
839 return "S_RECONNECT_BLACKLIST";
840 case S_RECONNECT_SENT:
841 return "S_RECONNECT_SENT";
842 case S_CONNECTED_SWITCHING_BLACKLIST:
843 return "S_CONNECTED_SWITCHING_BLACKLIST";
844 case S_CONNECTED_SWITCHING_CONNECT_SENT:
845 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
847 return "S_DISCONNECT";
848 case S_DISCONNECT_FINISHED:
849 return "S_DISCONNECT_FINISHED";
857 * Test if we're connected to the given peer.
859 * @param n neighbour entry of peer to test
860 * @return GNUNET_YES if we are connected, GNUNET_NO if not
863 test_connected (struct NeighbourMapEntry *n)
869 case S_NOT_CONNECTED:
871 case S_INIT_BLACKLIST:
873 case S_CONNECT_RECV_BLACKLIST_INBOUND:
874 case S_CONNECT_RECV_ATS:
875 case S_CONNECT_RECV_BLACKLIST:
876 case S_CONNECT_RECV_ACK:
879 case S_RECONNECT_ATS:
880 case S_RECONNECT_BLACKLIST:
881 case S_RECONNECT_SENT:
882 case S_CONNECTED_SWITCHING_BLACKLIST:
883 case S_CONNECTED_SWITCHING_CONNECT_SENT:
886 case S_DISCONNECT_FINISHED:
889 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
893 return GNUNET_SYSERR;
897 * Send information about a new outbound quota to our clients.
899 * @param target affected peer
900 * @param quota new quota
903 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
904 struct GNUNET_BANDWIDTH_Value32NBO quota)
906 struct QuotaSetMessage q_msg;
908 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
909 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
910 ntohl (quota.value__), GNUNET_i2s (target));
911 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
912 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
914 q_msg.peer = (*target);
915 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
920 * We don't need a given neighbour address any more.
921 * Release its resources and give appropriate notifications
922 * to ATS and other subsystems.
924 * @param na address we are done with; 'na' itself must NOT be 'free'd, only the contents!
927 free_address (struct NeighbourAddress *na)
929 if (GNUNET_YES == na->ats_active)
931 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
932 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
933 address_change_cb (NULL, &na->address->peer, NULL);
936 na->ats_active = GNUNET_NO;
937 if (NULL != na->address)
939 MEMDEBUG_free (na->address, __LINE__);
940 //GNUNET_HELLO_address_free (na->address);
948 * Initialize the 'struct NeighbourAddress'.
950 * @param na neighbour address to initialize
951 * @param address address of the other peer, NULL if other peer
953 * @param session session to use (or NULL, in which case an
954 * address must be setup)
955 * @param bandwidth_in inbound quota to be used when connection is up
956 * @param bandwidth_out outbound quota to be used when connection is up
957 * @param is_active GNUNET_YES to mark this as the active address with ATS
960 set_address (struct NeighbourAddress *na,
961 const struct GNUNET_HELLO_Address *address,
962 struct Session *session,
963 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
964 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
967 struct GNUNET_TRANSPORT_PluginFunctions *papi;
968 if (NULL == (papi = GST_plugins_find (address->transport_name)))
973 if (session == na->session)
975 na->bandwidth_in = bandwidth_in;
976 na->bandwidth_out = bandwidth_out;
977 if (is_active != na->ats_active)
979 na->ats_active = is_active;
980 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
981 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
983 address_change_cb (NULL, &address->peer, address);
985 if (GNUNET_YES == is_active)
987 /* FIXME: is this the right place to set quotas? */
988 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
989 send_outbound_quota (&address->peer, bandwidth_out);
995 session = papi->get_session (papi->cls, address);
998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
999 "Failed to obtain new session for peer `%s' and address '%s'\n",
1000 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
1001 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1004 na->address = GNUNET_HELLO_address_copy (address);
1005 MEMDEBUG_add_alloc (na->address, GNUNET_HELLO_address_get_size (na->address), __LINE__);
1006 na->bandwidth_in = bandwidth_in;
1007 na->bandwidth_out = bandwidth_out;
1008 na->session = session;
1009 na->ats_active = is_active;
1010 if (GNUNET_YES == is_active)
1012 /* Telling ATS about new session */
1013 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
1014 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
1015 address_change_cb (NULL, &address->peer, address);
1016 /* FIXME: is this the right place to set quotas? */
1017 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
1018 send_outbound_quota (&address->peer, bandwidth_out);
1024 * Free a neighbour map entry.
1026 * @param n entry to free
1027 * @param keep_sessions GNUNET_NO to tell plugin to terminate sessions,
1028 * GNUNET_YES to keep all sessions
1031 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
1033 struct MessageQueue *mq;
1034 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1035 struct GNUNET_HELLO_Address *backup_primary;
1037 n->is_active = NULL; /* always free'd by its own continuation! */
1039 /* fail messages currently in the queue */
1040 while (NULL != (mq = n->messages_head))
1042 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1043 if (NULL != mq->cont)
1044 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
1045 MEMDEBUG_free (mq, __LINE__);
1047 /* It is too late to send other peer disconnect notifications, but at
1048 least internally we need to get clean... */
1049 if (GNUNET_YES == test_connected (n))
1051 GNUNET_STATISTICS_set (GST_stats,
1052 gettext_noop ("# peers connected"),
1053 --neighbours_connected,
1055 disconnect_notify_cb (callback_cls, &n->id);
1058 n->state = S_DISCONNECT_FINISHED;
1060 if (NULL != n->primary_address.address)
1062 backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address);
1063 MEMDEBUG_add_alloc (backup_primary, GNUNET_HELLO_address_get_size(backup_primary), __LINE__);
1066 backup_primary = NULL;
1068 /* free addresses and mark as unused */
1069 free_address (&n->primary_address);
1070 free_address (&n->alternative_address);
1072 /* FIXME-PLUGIN-API: This does not seem to guarantee that all
1073 transport sessions eventually get killed due to inactivity; they
1074 MUST have their own timeout logic (but at least TCP doesn't have
1075 one yet). Are we sure that EVERY 'session' of a plugin is
1076 actually cleaned up this way!? Note that if we are switching
1077 between two TCP sessions to the same peer, the existing plugin
1078 API gives us not even the means to selectively kill only one of
1079 them! Killing all sessions like this seems to be very, very
1082 /* cut transport-level connection */
1083 if ((GNUNET_NO == keep_sessions) &&
1084 (NULL != backup_primary) &&
1085 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
1086 papi->disconnect (papi->cls, &n->id);
1088 MEMDEBUG_free_non_null (backup_primary, __LINE__);
1090 GNUNET_assert (GNUNET_YES ==
1091 GNUNET_CONTAINER_multihashmap_remove (neighbours,
1092 &n->id.hashPubKey, n));
1094 // FIXME-ATS-API: we might want to be more specific about
1095 // which states we do this from in the future (ATS should
1096 // have given us a 'suggest_address' handle, and if we have
1097 // such a handle, we should cancel the operation here!
1098 if (NULL != n->suggest_handle)
1100 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
1101 n->suggest_handle = NULL;
1104 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1106 GNUNET_SCHEDULER_cancel (n->task);
1107 n->task = GNUNET_SCHEDULER_NO_TASK;
1109 /* free rest of memory */
1110 MEMDEBUG_free (n, __LINE__);
1114 * Transmit a message using the current session of the given
1117 * @param n entry for the recipient
1118 * @param msgbuf buffer to transmit
1119 * @param msgbuf_size number of bytes in buffer
1120 * @param priority transmission priority
1121 * @param timeout transmission timeout
1122 * @param cont continuation to call when finished (can be NULL)
1123 * @param cont_cls closure for cont
1126 send_with_session (struct NeighbourMapEntry *n,
1127 const char *msgbuf, size_t msgbuf_size,
1129 struct GNUNET_TIME_Relative timeout,
1130 GNUNET_TRANSPORT_TransmitContinuation cont,
1133 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1135 GNUNET_assert (n->primary_address.session != NULL);
1136 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1137 (-1 == papi->send (papi->cls,
1138 n->primary_address.session,
1139 msgbuf, msgbuf_size,
1142 cont, cont_cls)))) &&
1144 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1145 GNUNET_break (NULL != papi);
1150 * Master task run for every neighbour. Performs all of the time-related
1151 * activities (keep alive, send next message, disconnect if idle, finish
1152 * clean up after disconnect).
1154 * @param cls the 'struct NeighbourMapEntry' for which we are running
1155 * @param tc scheduler context (unused)
1158 master_task (void *cls,
1159 const struct GNUNET_SCHEDULER_TaskContext *tc);
1163 * Function called when the 'DISCONNECT' message has been sent by the
1164 * plugin. Frees the neighbour --- if the entry still exists.
1167 * @param target identity of the neighbour that was disconnected
1168 * @param result GNUNET_OK if the disconnect got out successfully
1169 * @param payload bytes payload
1170 * @param physical bytes physical
1173 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1174 int result, size_t payload, size_t physical)
1176 struct NeighbourMapEntry *n;
1178 n = lookup_neighbour (target);
1180 return; /* already gone */
1181 if (S_DISCONNECT != n->state)
1182 return; /* have created a fresh entry since */
1183 n->state = S_DISCONNECT;
1184 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1185 GNUNET_SCHEDULER_cancel (n->task);
1186 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1191 * Transmit a DISCONNECT message to the other peer.
1193 * @param n neighbour to send DISCONNECT message.
1196 send_disconnect (struct NeighbourMapEntry *n)
1198 struct SessionDisconnectMessage disconnect_msg;
1200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1201 "Sending DISCONNECT message to peer `%4s'\n",
1202 GNUNET_i2s (&n->id));
1203 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1204 disconnect_msg.header.type =
1205 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1206 disconnect_msg.reserved = htonl (0);
1207 disconnect_msg.purpose.size =
1208 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1209 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded) +
1210 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1211 disconnect_msg.purpose.purpose =
1212 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1213 disconnect_msg.timestamp =
1214 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1215 disconnect_msg.public_key = GST_my_public_key;
1216 GNUNET_assert (GNUNET_OK ==
1217 GNUNET_CRYPTO_ecc_sign (GST_my_private_key,
1218 &disconnect_msg.purpose,
1219 &disconnect_msg.signature));
1221 send_with_session (n,
1222 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1223 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1224 &send_disconnect_cont, NULL);
1225 GNUNET_STATISTICS_update (GST_stats,
1227 ("# DISCONNECT messages sent"), 1,
1233 * Disconnect from the given neighbour, clean up the record.
1235 * @param n neighbour to disconnect from
1238 disconnect_neighbour (struct NeighbourMapEntry *n)
1240 /* depending on state, notify neighbour and/or upper layers of this peer
1244 case S_NOT_CONNECTED:
1246 case S_INIT_BLACKLIST:
1247 /* other peer is completely unaware of us, no need to send DISCONNECT */
1248 n->state = S_DISCONNECT_FINISHED;
1249 free_neighbour (n, GNUNET_NO);
1251 case S_CONNECT_SENT:
1252 send_disconnect (n);
1253 n->state = S_DISCONNECT;
1255 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1256 case S_CONNECT_RECV_ATS:
1257 case S_CONNECT_RECV_BLACKLIST:
1258 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1259 n->state = S_DISCONNECT_FINISHED;
1260 free_neighbour (n, GNUNET_NO);
1262 case S_CONNECT_RECV_ACK:
1263 /* we DID ACK the other peer's request, must send DISCONNECT */
1264 send_disconnect (n);
1265 n->state = S_DISCONNECT;
1268 case S_RECONNECT_BLACKLIST:
1269 case S_RECONNECT_SENT:
1270 case S_CONNECTED_SWITCHING_BLACKLIST:
1271 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1272 /* we are currently connected, need to send disconnect and do
1273 internal notifications and update statistics */
1274 send_disconnect (n);
1275 GNUNET_STATISTICS_set (GST_stats,
1276 gettext_noop ("# peers connected"),
1277 --neighbours_connected,
1279 disconnect_notify_cb (callback_cls, &n->id);
1280 n->state = S_DISCONNECT;
1282 case S_RECONNECT_ATS:
1283 /* ATS address request timeout, disconnect without sending disconnect message */
1284 GNUNET_STATISTICS_set (GST_stats,
1285 gettext_noop ("# peers connected"),
1286 --neighbours_connected,
1288 disconnect_notify_cb (callback_cls, &n->id);
1289 n->state = S_DISCONNECT;
1292 /* already disconnected, ignore */
1294 case S_DISCONNECT_FINISHED:
1295 /* already cleaned up, how did we get here!? */
1299 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1303 /* schedule timeout to clean up */
1304 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1305 GNUNET_SCHEDULER_cancel (n->task);
1306 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1312 * We're done with our transmission attempt, continue processing.
1314 * @param cls the 'struct MessageQueue' of the message
1315 * @param receiver intended receiver
1316 * @param success whether it worked or not
1317 * @param size_payload bytes payload sent
1318 * @param physical bytes sent on wire
1321 transmit_send_continuation (void *cls,
1322 const struct GNUNET_PeerIdentity *receiver,
1323 int success, size_t size_payload, size_t physical)
1325 struct MessageQueue *mq = cls;
1326 struct NeighbourMapEntry *n;
1328 if (NULL == (n = lookup_neighbour (receiver)))
1330 MEMDEBUG_free (mq, __LINE__);
1331 return; /* disconnect or other error while transmitting, can happen */
1333 if (n->is_active == mq)
1335 /* this is still "our" neighbour, remove us from its queue
1336 and allow it to send the next message now */
1337 n->is_active = NULL;
1338 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1339 GNUNET_SCHEDULER_cancel (n->task);
1340 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1342 if (bytes_in_send_queue < mq->message_buf_size)
1344 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1345 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1346 bytes_in_send_queue, mq->message_buf_size,
1347 (GNUNET_OK == success) ? "OK" : "FAIL",
1348 size_payload, physical);
1353 GNUNET_break (size_payload == mq->message_buf_size);
1354 bytes_in_send_queue -= mq->message_buf_size;
1355 GNUNET_STATISTICS_set (GST_stats,
1357 ("# bytes in message queue for other peers"),
1358 bytes_in_send_queue, GNUNET_NO);
1359 if (GNUNET_OK == success)
1360 GNUNET_STATISTICS_update (GST_stats,
1362 ("# messages transmitted to other peers"),
1365 GNUNET_STATISTICS_update (GST_stats,
1367 ("# transmission failures for messages to other peers"),
1369 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1370 "Sending message to `%s' of type %u was a %s\n",
1371 GNUNET_i2s (receiver),
1372 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1373 (success == GNUNET_OK) ? "success" : "FAILURE");
1374 if (NULL != mq->cont)
1375 mq->cont (mq->cont_cls, success, size_payload, physical);
1376 MEMDEBUG_free (mq, __LINE__);
1381 * Check the message list for the given neighbour and if we can
1382 * send a message, do so. This function should only be called
1383 * if the connection is at least generally ready for transmission.
1384 * While we will only send one message at a time, no bandwidth
1385 * quota management is performed here. If a message was given to
1386 * the plugin, the continuation will automatically re-schedule
1387 * the 'master' task once the next message might be transmitted.
1389 * @param n target peer for which to transmit
1392 try_transmission_to_peer (struct NeighbourMapEntry *n)
1394 struct MessageQueue *mq;
1395 struct GNUNET_TIME_Relative timeout;
1397 if (NULL == n->primary_address.address)
1399 /* no address, why are we here? */
1403 if ((0 == n->primary_address.address->address_length) &&
1404 (NULL == n->primary_address.session))
1406 /* no address, why are we here? */
1410 if (NULL != n->is_active)
1412 /* transmission already pending */
1416 /* timeout messages from the queue that are past their due date */
1417 while (NULL != (mq = n->messages_head))
1419 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1420 if (timeout.rel_value > 0)
1422 GNUNET_STATISTICS_update (GST_stats,
1424 ("# messages timed out while in transport queue"),
1426 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1428 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0); /* timeout */
1431 return; /* no more messages */
1432 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1434 send_with_session (n,
1435 mq->message_buf, mq->message_buf_size,
1436 0 /* priority */, timeout,
1437 &transmit_send_continuation, mq);
1442 * Send keepalive message to the neighbour. Must only be called
1443 * if we are on 'connected' state or while trying to switch addresses.
1444 * Will internally determine if a keepalive is truly needed (so can
1445 * always be called).
1447 * @param n neighbour that went idle and needs a keepalive
1450 send_keepalive (struct NeighbourMapEntry *n)
1452 struct GNUNET_MessageHeader m;
1454 GNUNET_assert ((S_CONNECTED == n->state) ||
1455 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1456 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1457 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value > 0)
1458 return; /* no keepalive needed at this time */
1459 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1460 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1461 send_with_session (n,
1462 (const void *) &m, sizeof (m),
1463 UINT32_MAX /* priority */,
1464 KEEPALIVE_FREQUENCY,
1466 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1468 n->expect_latency_response = GNUNET_YES;
1469 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1470 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1475 * Keep the connection to the given neighbour alive longer,
1476 * we received a KEEPALIVE (or equivalent); send a response.
1478 * @param neighbour neighbour to keep alive (by sending keep alive response)
1481 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1483 struct NeighbourMapEntry *n;
1484 struct GNUNET_MessageHeader m;
1486 if (NULL == (n = lookup_neighbour (neighbour)))
1488 GNUNET_STATISTICS_update (GST_stats,
1490 ("# KEEPALIVE messages discarded (peer unknown)"),
1494 if (NULL == n->primary_address.session)
1496 GNUNET_STATISTICS_update (GST_stats,
1498 ("# KEEPALIVE messages discarded (no session)"),
1502 /* send reply to allow neighbour to measure latency */
1503 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1504 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1505 send_with_session(n,
1506 (const void *) &m, sizeof (m),
1507 UINT32_MAX /* priority */,
1508 KEEPALIVE_FREQUENCY,
1514 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1515 * latency to this peer. Pass the updated information (existing ats
1516 * plus calculated latency) to ATS.
1518 * @param neighbour neighbour to keep alive
1519 * @param ats performance data
1520 * @param ats_count number of entries in ats
1523 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1524 const struct GNUNET_ATS_Information *ats,
1527 struct NeighbourMapEntry *n;
1529 struct GNUNET_ATS_Information ats_new[ats_count + 1];
1531 if (NULL == (n = lookup_neighbour (neighbour)))
1533 GNUNET_STATISTICS_update (GST_stats,
1535 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1539 if ( (S_CONNECTED != n->state) ||
1540 (GNUNET_YES != n->expect_latency_response) )
1542 GNUNET_STATISTICS_update (GST_stats,
1544 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1548 n->expect_latency_response = GNUNET_NO;
1549 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1550 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1552 "Latency for peer `%s' is %llu ms\n",
1553 GNUNET_i2s (&n->id), n->latency.rel_value);
1554 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
1555 /* append latency */
1556 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1557 if (n->latency.rel_value > UINT32_MAX)
1558 latency = UINT32_MAX;
1560 latency = n->latency.rel_value;
1561 ats_new[ats_count].value = htonl (latency);
1562 GNUNET_ATS_address_update (GST_ats,
1563 n->primary_address.address,
1564 n->primary_address.session, ats_new,
1570 * We have received a message from the given sender. How long should
1571 * we delay before receiving more? (Also used to keep the peer marked
1574 * @param sender sender of the message
1575 * @param size size of the message
1576 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1577 * GNUNET_NO if the neighbour is not connected or violates the quota,
1578 * GNUNET_SYSERR if the connection is not fully up yet
1579 * @return how long to wait before reading more from this sender
1581 struct GNUNET_TIME_Relative
1582 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1583 *sender, ssize_t size, int *do_forward)
1585 struct NeighbourMapEntry *n;
1586 struct GNUNET_TIME_Relative ret;
1588 if (NULL == neighbours)
1590 *do_forward = GNUNET_NO;
1591 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1593 if (NULL == (n = lookup_neighbour (sender)))
1595 GST_neighbours_try_connect (sender);
1596 if (NULL == (n = lookup_neighbour (sender)))
1598 GNUNET_STATISTICS_update (GST_stats,
1600 ("# messages discarded due to lack of neighbour record"),
1602 *do_forward = GNUNET_NO;
1603 return GNUNET_TIME_UNIT_ZERO;
1606 if (! test_connected (n))
1608 *do_forward = GNUNET_SYSERR;
1609 return GNUNET_TIME_UNIT_ZERO;
1611 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1613 n->quota_violation_count++;
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1615 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1616 n->in_tracker.available_bytes_per_s__,
1617 n->quota_violation_count);
1618 /* Discount 32k per violation */
1619 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1623 if (n->quota_violation_count > 0)
1625 /* try to add 32k back */
1626 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1627 n->quota_violation_count--;
1630 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1632 GNUNET_STATISTICS_update (GST_stats,
1634 ("# bandwidth quota violations by other peers"),
1636 *do_forward = GNUNET_NO;
1637 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1639 *do_forward = GNUNET_YES;
1640 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1641 if (ret.rel_value > 0)
1643 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1644 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1645 (unsigned long long) n->in_tracker.
1646 consumption_since_last_update__,
1647 (unsigned int) n->in_tracker.available_bytes_per_s__,
1648 (unsigned long long) ret.rel_value);
1649 GNUNET_STATISTICS_update (GST_stats,
1650 gettext_noop ("# ms throttling suggested"),
1651 (int64_t) ret.rel_value, GNUNET_NO);
1658 * Transmit a message to the given target using the active connection.
1660 * @param target destination
1661 * @param msg message to send
1662 * @param msg_size number of bytes in msg
1663 * @param timeout when to fail with timeout
1664 * @param cont function to call when done
1665 * @param cont_cls closure for 'cont'
1668 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1669 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1670 GST_NeighbourSendContinuation cont, void *cont_cls)
1672 struct NeighbourMapEntry *n;
1673 struct MessageQueue *mq;
1675 /* All ove these cases should never happen; they are all API violations.
1676 But we check anyway, just to be sure. */
1677 if (NULL == (n = lookup_neighbour (target)))
1681 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1684 if (GNUNET_YES != test_connected (n))
1688 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1691 bytes_in_send_queue += msg_size;
1692 GNUNET_STATISTICS_set (GST_stats,
1694 ("# bytes in message queue for other peers"),
1695 bytes_in_send_queue, GNUNET_NO);
1696 mq = MEMDEBUG_malloc (sizeof (struct MessageQueue) + msg_size, __LINE__);
1698 mq->cont_cls = cont_cls;
1699 memcpy (&mq[1], msg, msg_size);
1700 mq->message_buf = (const char *) &mq[1];
1701 mq->message_buf_size = msg_size;
1702 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1703 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1704 if ( (NULL != n->is_active) ||
1705 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1707 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1708 GNUNET_SCHEDULER_cancel (n->task);
1709 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1714 * Send a SESSION_CONNECT message via the given address.
1716 * @param na address to use
1719 send_session_connect (struct NeighbourAddress *na)
1721 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1722 struct SessionConnectMessage connect_msg;
1724 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1729 if (NULL == na->session)
1730 na->session = papi->get_session (papi->cls, na->address);
1731 if (NULL == na->session)
1736 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1737 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1738 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1739 connect_msg.reserved = htonl (0);
1740 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1741 (void) papi->send (papi->cls,
1743 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1745 GNUNET_TIME_UNIT_FOREVER_REL,
1752 * Send a SESSION_CONNECT_ACK message via the given address.
1754 * @param address address to use
1755 * @param session session to use
1756 * @param timestamp timestamp to use for the ACK message
1759 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1760 struct Session *session,
1761 struct GNUNET_TIME_Absolute timestamp)
1763 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1764 struct SessionConnectMessage connect_msg;
1766 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1771 if (NULL == session)
1772 session = papi->get_session (papi->cls, address);
1773 if (NULL == session)
1778 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1779 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1780 connect_msg.reserved = htonl (0);
1781 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1782 (void) papi->send (papi->cls,
1784 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1786 GNUNET_TIME_UNIT_FOREVER_REL,
1793 * Create a fresh entry in the neighbour map for the given peer
1795 * @param peer peer to create an entry for
1796 * @return new neighbour map entry
1798 static struct NeighbourMapEntry *
1799 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1801 struct NeighbourMapEntry *n;
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1804 "Creating new neighbour entry for `%s'\n",
1806 n = MEMDEBUG_malloc (sizeof (struct NeighbourMapEntry), __LINE__);
1808 n->state = S_NOT_CONNECTED;
1809 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1810 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1811 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1812 MAX_BANDWIDTH_CARRY_S);
1813 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1814 GNUNET_assert (GNUNET_OK ==
1815 GNUNET_CONTAINER_multihashmap_put (neighbours,
1816 &n->id.hashPubKey, n,
1817 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1823 * Check if the two given addresses are the same.
1824 * Actually only checks if the sessions are non-NULL
1825 * (which they should be) and then if they are identical;
1826 * the actual addresses don't matter if the session
1827 * pointers match anyway, and we must have session pointers
1830 * @param a1 first address to compare
1831 * @param a2 other address to compare
1832 * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
1835 address_matches (const struct NeighbourAddress *a1,
1836 const struct NeighbourAddress *a2)
1838 if ( (NULL == a1->session) ||
1839 (NULL == a2->session) )
1844 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1849 * Try to create a connection to the given target (eventually).
1851 * @param target peer to try to connect to
1854 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1856 struct NeighbourMapEntry *n;
1858 if (NULL == neighbours)
1859 return; /* during shutdown, do nothing */
1860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1861 "Asked to connect to peer `%s'\n",
1862 GNUNET_i2s (target));
1864 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1866 /* refuse to connect to myself */
1867 /* FIXME: can this happen? Is this not an API violation? */
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1869 "Refusing to try to connect to myself.\n");
1872 n = lookup_neighbour (target);
1877 case S_NOT_CONNECTED:
1878 /* this should not be possible */
1880 free_neighbour (n, GNUNET_NO);
1883 case S_INIT_BLACKLIST:
1884 case S_CONNECT_SENT:
1885 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1886 case S_CONNECT_RECV_ATS:
1887 case S_CONNECT_RECV_BLACKLIST:
1888 case S_CONNECT_RECV_ACK:
1889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1890 "Ignoring request to try to connect to `%s', already trying!\n",
1891 GNUNET_i2s (target));
1892 return; /* already trying */
1894 case S_RECONNECT_ATS:
1895 case S_RECONNECT_BLACKLIST:
1896 case S_RECONNECT_SENT:
1897 case S_CONNECTED_SWITCHING_BLACKLIST:
1898 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1899 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1900 "Ignoring request to try to connect, already connected to `%s'!\n",
1901 GNUNET_i2s (target));
1902 return; /* already connected */
1904 /* get rid of remains, ready to re-try immediately */
1905 free_neighbour (n, GNUNET_NO);
1907 case S_DISCONNECT_FINISHED:
1908 /* should not be possible */
1911 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1913 free_neighbour (n, GNUNET_NO);
1917 n = setup_neighbour (target);
1918 n->state = S_INIT_ATS;
1919 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1921 GNUNET_ATS_reset_backoff (GST_ats, target);
1922 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1927 * Function called with the result of a blacklist check.
1929 * @param cls closure with the 'struct BlackListCheckContext'
1930 * @param peer peer this check affects
1931 * @param result GNUNET_OK if the address is allowed
1934 handle_test_blacklist_cont (void *cls,
1935 const struct GNUNET_PeerIdentity *peer,
1938 struct BlackListCheckContext *bcc = cls;
1939 struct NeighbourMapEntry *n;
1942 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1943 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1945 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1946 if (NULL == (n = lookup_neighbour (peer)))
1947 goto cleanup; /* nobody left to care about new address */
1950 case S_NOT_CONNECTED:
1951 /* this should not be possible */
1953 free_neighbour (n, GNUNET_NO);
1956 /* still waiting on ATS suggestion */
1958 case S_INIT_BLACKLIST:
1959 /* check if the address the blacklist was fine with matches
1960 ATS suggestion, if so, we can move on! */
1961 if ( (GNUNET_OK == result) &&
1962 (1 == n->send_connect_ack) )
1964 n->send_connect_ack = 2;
1965 send_session_connect_ack_message (bcc->na.address,
1967 n->connect_ack_timestamp);
1969 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1970 break; /* result for an address we currently don't care about */
1971 if (GNUNET_OK == result)
1973 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1974 n->state = S_CONNECT_SENT;
1975 send_session_connect (&n->primary_address);
1979 // FIXME: should also possibly destroy session with plugin!?
1980 GNUNET_ATS_address_destroyed (GST_ats,
1983 free_address (&n->primary_address);
1984 n->state = S_INIT_ATS;
1985 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1986 // FIXME: do we need to ask ATS again for suggestions?
1987 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
1990 case S_CONNECT_SENT:
1991 /* waiting on CONNECT_ACK, send ACK if one is pending */
1992 if ( (GNUNET_OK == result) &&
1993 (1 == n->send_connect_ack) )
1995 n->send_connect_ack = 2;
1996 send_session_connect_ack_message (n->primary_address.address,
1997 n->primary_address.session,
1998 n->connect_ack_timestamp);
2001 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2002 if (GNUNET_OK == result)
2004 /* valid new address, let ATS know! */
2005 GNUNET_ATS_address_add (GST_ats,
2008 bcc->ats, bcc->ats_count);
2010 n->state = S_CONNECT_RECV_ATS;
2011 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2012 GNUNET_ATS_reset_backoff (GST_ats, peer);
2013 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2015 case S_CONNECT_RECV_ATS:
2016 /* still waiting on ATS suggestion, don't care about blacklist */
2018 case S_CONNECT_RECV_BLACKLIST:
2019 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2020 break; /* result for an address we currently don't care about */
2021 if (GNUNET_OK == result)
2023 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
2024 n->state = S_CONNECT_RECV_ACK;
2025 send_session_connect_ack_message (bcc->na.address,
2027 n->connect_ack_timestamp);
2028 if (1 == n->send_connect_ack)
2029 n->send_connect_ack = 2;
2033 // FIXME: should also possibly destroy session with plugin!?
2034 GNUNET_ATS_address_destroyed (GST_ats,
2037 free_address (&n->primary_address);
2038 n->state = S_INIT_ATS;
2039 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2040 // FIXME: do we need to ask ATS again for suggestions?
2041 GNUNET_ATS_reset_backoff (GST_ats, peer);
2042 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2045 case S_CONNECT_RECV_ACK:
2046 /* waiting on SESSION_ACK, send ACK if one is pending */
2047 if ( (GNUNET_OK == result) &&
2048 (1 == n->send_connect_ack) )
2050 n->send_connect_ack = 2;
2051 send_session_connect_ack_message (n->primary_address.address,
2052 n->primary_address.session,
2053 n->connect_ack_timestamp);
2057 /* already connected, don't care about blacklist */
2059 case S_RECONNECT_ATS:
2060 /* still waiting on ATS suggestion, don't care about blacklist */
2062 case S_RECONNECT_BLACKLIST:
2063 if ( (GNUNET_OK == result) &&
2064 (1 == n->send_connect_ack) )
2066 n->send_connect_ack = 2;
2067 send_session_connect_ack_message (bcc->na.address,
2069 n->connect_ack_timestamp);
2071 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2072 break; /* result for an address we currently don't care about */
2073 if (GNUNET_OK == result)
2075 send_session_connect (&n->primary_address);
2076 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2077 n->state = S_RECONNECT_SENT;
2081 GNUNET_ATS_address_destroyed (GST_ats,
2084 n->state = S_RECONNECT_ATS;
2085 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2086 // FIXME: do we need to ask ATS again for suggestions?
2087 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2090 case S_RECONNECT_SENT:
2091 /* waiting on CONNECT_ACK, don't care about blacklist */
2092 if ( (GNUNET_OK == result) &&
2093 (1 == n->send_connect_ack) )
2095 n->send_connect_ack = 2;
2096 send_session_connect_ack_message (n->primary_address.address,
2097 n->primary_address.session,
2098 n->connect_ack_timestamp);
2101 case S_CONNECTED_SWITCHING_BLACKLIST:
2102 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2103 break; /* result for an address we currently don't care about */
2104 if (GNUNET_OK == result)
2106 send_session_connect (&n->alternative_address);
2107 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
2111 GNUNET_ATS_address_destroyed (GST_ats,
2114 free_address (&n->alternative_address);
2115 n->state = S_CONNECTED;
2118 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2119 /* waiting on CONNECT_ACK, don't care about blacklist */
2120 if ( (GNUNET_OK == result) &&
2121 (1 == n->send_connect_ack) )
2123 n->send_connect_ack = 2;
2124 send_session_connect_ack_message (n->primary_address.address,
2125 n->primary_address.session,
2126 n->connect_ack_timestamp);
2130 /* Nothing to do here, ATS will already do what can be done */
2132 case S_DISCONNECT_FINISHED:
2133 /* should not be possible */
2137 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2139 free_neighbour (n, GNUNET_NO);
2143 MEMDEBUG_free (bcc->na.address, __LINE__);
2144 //GNUNET_HELLO_address_free (bcc->na.address);
2145 GNUNET_CONTAINER_DLL_remove (bc_head,
2148 MEMDEBUG_free (bcc, __LINE__);
2153 * We want to know if connecting to a particular peer via
2154 * a particular address is allowed. Check it!
2156 * @param peer identity of the peer to switch the address for
2157 * @param ts time at which the check was initiated
2158 * @param address address of the other peer, NULL if other peer
2160 * @param session session to use (or NULL)
2161 * @param ats performance data
2162 * @param ats_count number of entries in ats (excluding 0-termination)
2165 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2166 struct GNUNET_TIME_Absolute ts,
2167 const struct GNUNET_HELLO_Address *address,
2168 struct Session *session,
2169 const struct GNUNET_ATS_Information *ats,
2172 struct BlackListCheckContext *bcc;
2173 struct GST_BlacklistCheck *bc;
2176 MEMDEBUG_malloc (sizeof (struct BlackListCheckContext) +
2177 sizeof (struct GNUNET_ATS_Information) * ats_count, __LINE__);
2178 bcc->ats_count = ats_count;
2179 bcc->na.address = GNUNET_HELLO_address_copy (address);
2180 MEMDEBUG_add_alloc (bcc->na.address, GNUNET_HELLO_address_get_size (address), __LINE__);
2181 bcc->na.session = session;
2182 bcc->na.connect_timestamp = ts;
2183 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2184 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2185 GNUNET_CONTAINER_DLL_insert (bc_head,
2188 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2189 address->transport_name,
2190 &handle_test_blacklist_cont, bcc)))
2192 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2193 we must only store 'bc' if 'bc' is non-NULL... */
2198 * We received a 'SESSION_CONNECT' message from the other peer.
2199 * Consider switching to it.
2201 * @param message possibly a 'struct SessionConnectMessage' (check format)
2202 * @param peer identity of the peer to switch the address for
2203 * @param address address of the other peer, NULL if other peer
2205 * @param session session to use (or NULL)
2206 * @param ats performance data
2207 * @param ats_count number of entries in ats (excluding 0-termination)
2210 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2211 const struct GNUNET_PeerIdentity *peer,
2212 const struct GNUNET_HELLO_Address *address,
2213 struct Session *session,
2214 const struct GNUNET_ATS_Information *ats,
2217 const struct SessionConnectMessage *scm;
2218 struct NeighbourMapEntry *n;
2219 struct GNUNET_TIME_Absolute ts;
2221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2222 "Received CONNECT message from peer `%s'\n",
2225 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2227 GNUNET_break_op (0);
2230 if (NULL == neighbours)
2231 return; /* we're shutting down */
2232 scm = (const struct SessionConnectMessage *) message;
2233 GNUNET_break_op (0 == ntohl (scm->reserved));
2234 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2235 n = lookup_neighbour (peer);
2237 n = setup_neighbour (peer);
2238 n->send_connect_ack = 1;
2239 n->connect_ack_timestamp = ts;
2243 case S_NOT_CONNECTED:
2244 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2245 /* Do a blacklist check for the new address */
2246 check_blacklist (peer, ts, address, session, ats, ats_count);
2249 /* CONNECT message takes priority over us asking ATS for address */
2250 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2252 case S_INIT_BLACKLIST:
2253 case S_CONNECT_SENT:
2254 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2255 case S_CONNECT_RECV_ATS:
2256 case S_CONNECT_RECV_BLACKLIST:
2257 case S_CONNECT_RECV_ACK:
2258 /* It can never hurt to have an alternative address in the above cases,
2259 see if it is allowed */
2260 check_blacklist (peer, ts, address, session, ats, ats_count);
2263 /* we are already connected and can thus send the ACK immediately;
2264 still, it can never hurt to have an alternative address, so also
2265 tell ATS about it */
2266 GNUNET_assert (NULL != n->primary_address.address);
2267 GNUNET_assert (NULL != n->primary_address.session);
2268 n->send_connect_ack = 0;
2269 send_session_connect_ack_message (n->primary_address.address,
2270 n->primary_address.session, ts);
2271 check_blacklist (peer, ts, address, session, ats, ats_count);
2273 case S_RECONNECT_ATS:
2274 case S_RECONNECT_BLACKLIST:
2275 case S_RECONNECT_SENT:
2276 /* It can never hurt to have an alternative address in the above cases,
2277 see if it is allowed */
2278 check_blacklist (peer, ts, address, session, ats, ats_count);
2280 case S_CONNECTED_SWITCHING_BLACKLIST:
2281 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2282 /* we are already connected and can thus send the ACK immediately;
2283 still, it can never hurt to have an alternative address, so also
2284 tell ATS about it */
2285 GNUNET_assert (NULL != n->primary_address.address);
2286 GNUNET_assert (NULL != n->primary_address.session);
2287 n->send_connect_ack = 0;
2288 send_session_connect_ack_message (n->primary_address.address,
2289 n->primary_address.session, ts);
2290 check_blacklist (peer, ts, address, session, ats, ats_count);
2293 /* get rid of remains without terminating sessions, ready to re-try */
2294 free_neighbour (n, GNUNET_YES);
2295 n = setup_neighbour (peer);
2296 n->state = S_CONNECT_RECV_ATS;
2297 GNUNET_ATS_reset_backoff (GST_ats, peer);
2298 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2300 case S_DISCONNECT_FINISHED:
2301 /* should not be possible */
2305 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2307 free_neighbour (n, GNUNET_NO);
2314 * For an existing neighbour record, set the active connection to
2315 * use the given address.
2317 * @param peer identity of the peer to switch the address for
2318 * @param address address of the other peer, NULL if other peer
2320 * @param session session to use (or NULL)
2321 * @param ats performance data
2322 * @param ats_count number of entries in ats
2323 * @param bandwidth_in inbound quota to be used when connection is up
2324 * @param bandwidth_out outbound quota to be used when connection is up
2327 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2328 const struct GNUNET_HELLO_Address *address,
2329 struct Session *session,
2330 const struct GNUNET_ATS_Information *ats,
2332 struct GNUNET_BANDWIDTH_Value32NBO
2334 struct GNUNET_BANDWIDTH_Value32NBO
2337 struct NeighbourMapEntry *n;
2338 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2340 GNUNET_assert (address->transport_name != NULL);
2341 if (NULL == (n = lookup_neighbour (peer)))
2344 /* Obtain an session for this address from plugin */
2345 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2347 /* we don't have the plugin for this address */
2348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2349 "2348 : `%s' \n", address->transport_name);
2350 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2353 if ((NULL == session) && (0 == address->address_length))
2356 if (strlen (address->transport_name) > 0)
2357 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "ATS tells us to switch to address '%s' session %p for "
2363 "peer `%s' in state %s (quota in/out %u %u )\n",
2364 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2367 print_state (n->state),
2368 ntohl (bandwidth_in.value__),
2369 ntohl (bandwidth_out.value__));
2371 if (NULL == session)
2373 session = papi->get_session (papi->cls, address);
2374 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2375 "Obtained new session for peer `%s' and address '%s': %p\n",
2376 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2378 if (NULL == session)
2380 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2381 "Failed to obtain new session for peer `%s' and address '%s'\n",
2382 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2383 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2388 case S_NOT_CONNECTED:
2390 free_neighbour (n, GNUNET_NO);
2393 set_address (&n->primary_address,
2394 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2395 n->state = S_INIT_BLACKLIST;
2396 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2397 check_blacklist (&n->id,
2398 n->connect_ack_timestamp,
2399 address, session, ats, ats_count);
2401 case S_INIT_BLACKLIST:
2402 /* ATS suggests a different address, switch again */
2403 set_address (&n->primary_address,
2404 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2405 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2406 check_blacklist (&n->id,
2407 n->connect_ack_timestamp,
2408 address, session, ats, ats_count);
2410 case S_CONNECT_SENT:
2411 /* ATS suggests a different address, switch again */
2412 set_address (&n->primary_address,
2413 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2414 n->state = S_INIT_BLACKLIST;
2415 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2416 check_blacklist (&n->id,
2417 n->connect_ack_timestamp,
2418 address, session, ats, ats_count);
2420 case S_CONNECT_RECV_ATS:
2421 set_address (&n->primary_address,
2422 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2423 n->state = S_CONNECT_RECV_BLACKLIST;
2424 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2425 check_blacklist (&n->id,
2426 n->connect_ack_timestamp,
2427 address, session, ats, ats_count);
2429 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2430 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2431 check_blacklist (&n->id,
2432 n->connect_ack_timestamp,
2433 address, session, ats, ats_count);
2435 case S_CONNECT_RECV_BLACKLIST:
2436 case S_CONNECT_RECV_ACK:
2437 /* ATS asks us to switch while we were trying to connect; switch to new
2438 address and check blacklist again */
2439 set_address (&n->primary_address,
2440 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2441 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2442 check_blacklist (&n->id,
2443 n->connect_ack_timestamp,
2444 address, session, ats, ats_count);
2447 GNUNET_assert (NULL != n->primary_address.address);
2448 GNUNET_assert (NULL != n->primary_address.session);
2449 if (n->primary_address.session == session)
2451 /* not an address change, just a quota change */
2452 set_address (&n->primary_address,
2453 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2456 /* ATS asks us to switch a life connection; see if we can get
2457 a CONNECT_ACK on it before we actually do this! */
2458 set_address (&n->alternative_address,
2459 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2460 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2461 check_blacklist (&n->id,
2462 GNUNET_TIME_absolute_get (),
2463 address, session, ats, ats_count);
2465 case S_RECONNECT_ATS:
2466 set_address (&n->primary_address,
2467 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2468 n->state = S_RECONNECT_BLACKLIST;
2469 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2470 check_blacklist (&n->id,
2471 n->connect_ack_timestamp,
2472 address, session, ats, ats_count);
2474 case S_RECONNECT_BLACKLIST:
2475 /* ATS asks us to switch while we were trying to reconnect; switch to new
2476 address and check blacklist again */
2477 set_address (&n->primary_address,
2478 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2479 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2480 check_blacklist (&n->id,
2481 n->connect_ack_timestamp,
2482 address, session, ats, ats_count);
2484 case S_RECONNECT_SENT:
2485 /* ATS asks us to switch while we were trying to reconnect; switch to new
2486 address and check blacklist again */
2487 set_address (&n->primary_address,
2488 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2489 n->state = S_RECONNECT_BLACKLIST;
2490 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2491 check_blacklist (&n->id,
2492 n->connect_ack_timestamp,
2493 address, session, ats, ats_count);
2495 case S_CONNECTED_SWITCHING_BLACKLIST:
2496 if (n->primary_address.session == session)
2498 /* ATS switches back to still-active session */
2499 free_address (&n->alternative_address);
2500 n->state = S_CONNECTED;
2503 /* ATS asks us to switch a life connection, update blacklist check */
2504 set_address (&n->alternative_address,
2505 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2506 check_blacklist (&n->id,
2507 GNUNET_TIME_absolute_get (),
2508 address, session, ats, ats_count);
2510 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2511 if (n->primary_address.session == session)
2513 /* ATS switches back to still-active session */
2514 free_address (&n->alternative_address);
2515 n->state = S_CONNECTED;
2518 /* ATS asks us to switch a life connection, update blacklist check */
2519 set_address (&n->alternative_address,
2520 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2521 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2522 check_blacklist (&n->id,
2523 GNUNET_TIME_absolute_get (),
2524 address, session, ats, ats_count);
2527 /* not going to switch addresses while disconnecting */
2529 case S_DISCONNECT_FINISHED:
2533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2541 * Master task run for every neighbour. Performs all of the time-related
2542 * activities (keep alive, send next message, disconnect if idle, finish
2543 * clean up after disconnect).
2545 * @param cls the 'struct NeighbourMapEntry' for which we are running
2546 * @param tc scheduler context (unused)
2549 master_task (void *cls,
2550 const struct GNUNET_SCHEDULER_TaskContext *tc)
2552 struct NeighbourMapEntry *n = cls;
2553 struct GNUNET_TIME_Relative delay;
2555 n->task = GNUNET_SCHEDULER_NO_TASK;
2556 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2557 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2558 "Master task runs for neighbour `%s' in state %s with timeout in %llu ms\n",
2559 GNUNET_i2s (&n->id),
2560 print_state(n->state),
2561 (unsigned long long) delay.rel_value);
2564 case S_NOT_CONNECTED:
2565 /* invalid state for master task, clean up */
2567 n->state = S_DISCONNECT_FINISHED;
2568 free_neighbour (n, GNUNET_NO);
2571 if (0 == delay.rel_value)
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2574 "Connection to `%s' timed out waiting for ATS to provide address\n",
2575 GNUNET_i2s (&n->id));
2576 n->state = S_DISCONNECT_FINISHED;
2577 free_neighbour (n, GNUNET_NO);
2581 case S_INIT_BLACKLIST:
2582 if (0 == delay.rel_value)
2584 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2585 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2586 GNUNET_i2s (&n->id));
2587 n->state = S_DISCONNECT_FINISHED;
2588 free_neighbour (n, GNUNET_NO);
2592 case S_CONNECT_SENT:
2593 if (0 == delay.rel_value)
2595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2596 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2597 GNUNET_i2s (&n->id));
2598 disconnect_neighbour (n);
2602 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2603 if (0 == delay.rel_value)
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2606 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2607 GNUNET_i2s (&n->id));
2608 n->state = S_DISCONNECT_FINISHED;
2609 free_neighbour (n, GNUNET_NO);
2613 case S_CONNECT_RECV_ATS:
2614 if (0 == delay.rel_value)
2616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2617 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2618 GNUNET_i2s (&n->id));
2619 n->state = S_DISCONNECT_FINISHED;
2620 free_neighbour (n, GNUNET_NO);
2624 case S_CONNECT_RECV_BLACKLIST:
2625 if (0 == delay.rel_value)
2627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2628 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2629 GNUNET_i2s (&n->id));
2630 n->state = S_DISCONNECT_FINISHED;
2631 free_neighbour (n, GNUNET_NO);
2635 case S_CONNECT_RECV_ACK:
2636 if (0 == delay.rel_value)
2638 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2639 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2640 GNUNET_i2s (&n->id));
2641 disconnect_neighbour (n);
2646 if (0 == delay.rel_value)
2648 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2649 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2650 GNUNET_i2s (&n->id));
2651 disconnect_neighbour (n);
2654 try_transmission_to_peer (n);
2657 case S_RECONNECT_ATS:
2658 if (0 == delay.rel_value)
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2661 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2662 GNUNET_i2s (&n->id));
2663 disconnect_neighbour (n);
2667 case S_RECONNECT_BLACKLIST:
2668 if (0 == delay.rel_value)
2670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2671 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2672 GNUNET_i2s (&n->id));
2673 disconnect_neighbour (n);
2677 case S_RECONNECT_SENT:
2678 if (0 == delay.rel_value)
2680 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2681 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2682 GNUNET_i2s (&n->id));
2683 disconnect_neighbour (n);
2687 case S_CONNECTED_SWITCHING_BLACKLIST:
2688 if (0 == delay.rel_value)
2690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2691 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2692 GNUNET_i2s (&n->id));
2693 disconnect_neighbour (n);
2696 try_transmission_to_peer (n);
2699 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2700 if (0 == delay.rel_value)
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2703 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2704 GNUNET_i2s (&n->id));
2705 disconnect_neighbour (n);
2708 try_transmission_to_peer (n);
2712 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2713 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2714 GNUNET_i2s (&n->id));
2715 free_neighbour (n, GNUNET_NO);
2717 case S_DISCONNECT_FINISHED:
2718 /* how did we get here!? */
2722 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2726 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2727 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2728 (S_CONNECTED == n->state) )
2730 /* if we are *now* in one of these three states, we're sending
2731 keep alive messages, so we need to consider the keepalive
2732 delay, not just the connection timeout */
2733 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2736 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2737 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2744 * Send a SESSION_ACK message to the neighbour to confirm that we
2745 * got his CONNECT_ACK.
2747 * @param n neighbour to send the SESSION_ACK to
2750 send_session_ack_message (struct NeighbourMapEntry *n)
2752 struct GNUNET_MessageHeader msg;
2754 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2755 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2756 (void) send_with_session(n,
2757 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2758 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2764 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2765 * Consider switching to it.
2767 * @param message possibly a 'struct SessionConnectMessage' (check format)
2768 * @param peer identity of the peer to switch the address for
2769 * @param address address of the other peer, NULL if other peer
2771 * @param session session to use (or NULL)
2772 * @param ats performance data
2773 * @param ats_count number of entries in ats
2776 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2777 const struct GNUNET_PeerIdentity *peer,
2778 const struct GNUNET_HELLO_Address *address,
2779 struct Session *session,
2780 const struct GNUNET_ATS_Information *ats,
2783 const struct SessionConnectMessage *scm;
2784 struct GNUNET_TIME_Absolute ts;
2785 struct NeighbourMapEntry *n;
2787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2788 "Received CONNECT_ACK message from peer `%s'\n",
2791 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2793 GNUNET_break_op (0);
2796 scm = (const struct SessionConnectMessage *) message;
2797 GNUNET_break_op (ntohl (scm->reserved) == 0);
2798 if (NULL == (n = lookup_neighbour (peer)))
2800 GNUNET_STATISTICS_update (GST_stats,
2802 ("# unexpected CONNECT_ACK messages (no peer)"),
2806 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2809 case S_NOT_CONNECTED:
2811 free_neighbour (n, GNUNET_NO);
2814 case S_INIT_BLACKLIST:
2815 GNUNET_STATISTICS_update (GST_stats,
2817 ("# unexpected CONNECT_ACK messages (not ready)"),
2820 case S_CONNECT_SENT:
2821 if (ts.abs_value != n->primary_address.connect_timestamp.abs_value)
2822 break; /* ACK does not match our original CONNECT message */
2823 n->state = S_CONNECTED;
2824 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2825 GNUNET_STATISTICS_set (GST_stats,
2826 gettext_noop ("# peers connected"),
2827 ++neighbours_connected,
2829 connect_notify_cb (callback_cls, &n->id, ats, ats_count,
2830 n->primary_address.bandwidth_in,
2831 n->primary_address.bandwidth_out);
2832 /* Tell ATS that the outbound session we created to send CONNECT was successfull */
2833 GNUNET_ATS_address_add (GST_ats,
2834 n->primary_address.address,
2835 n->primary_address.session,
2837 set_address (&n->primary_address,
2838 n->primary_address.address,
2839 n->primary_address.session,
2840 n->primary_address.bandwidth_in,
2841 n->primary_address.bandwidth_out,
2843 send_session_ack_message (n);
2845 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2846 case S_CONNECT_RECV_ATS:
2847 case S_CONNECT_RECV_BLACKLIST:
2848 case S_CONNECT_RECV_ACK:
2849 GNUNET_STATISTICS_update (GST_stats,
2851 ("# unexpected CONNECT_ACK messages (not ready)"),
2855 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2856 send_session_ack_message (n);
2858 case S_RECONNECT_ATS:
2859 case S_RECONNECT_BLACKLIST:
2860 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2861 to give us a new address... */
2862 GNUNET_STATISTICS_update (GST_stats,
2864 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2867 case S_RECONNECT_SENT:
2868 /* new address worked; go back to connected! */
2869 n->state = S_CONNECTED;
2870 send_session_ack_message (n);
2872 case S_CONNECTED_SWITCHING_BLACKLIST:
2873 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2874 send_session_ack_message (n);
2876 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2877 /* new address worked; adopt it and go back to connected! */
2878 n->state = S_CONNECTED;
2879 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2880 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2881 GNUNET_ATS_address_add(GST_ats,
2882 n->alternative_address.address,
2883 n->alternative_address.session,
2885 set_address (&n->primary_address,
2886 n->alternative_address.address,
2887 n->alternative_address.session,
2888 n->alternative_address.bandwidth_in,
2889 n->alternative_address.bandwidth_out,
2891 free_address (&n->alternative_address);
2892 send_session_ack_message (n);
2895 GNUNET_STATISTICS_update (GST_stats,
2897 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2900 case S_DISCONNECT_FINISHED:
2904 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2912 * A session was terminated. Take note; if needed, try to get
2913 * an alternative address from ATS.
2915 * @param peer identity of the peer where the session died
2916 * @param session session that is gone
2917 * @return GNUNET_YES if this was a session used, GNUNET_NO if
2918 * this session was not in use
2921 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2922 struct Session *session)
2924 struct NeighbourMapEntry *n;
2925 struct BlackListCheckContext *bcc;
2926 struct BlackListCheckContext *bcc_next;
2928 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2930 while (NULL != (bcc = bcc_next))
2932 bcc_next = bcc->next;
2933 if (bcc->na.session == session)
2935 GST_blacklist_test_cancel (bcc->bc);
2936 MEMDEBUG_free (bcc->na.address, __LINE__);
2937 //GNUNET_HELLO_address_free (bcc->na.address);
2938 GNUNET_CONTAINER_DLL_remove (bc_head,
2941 MEMDEBUG_free (bcc, __LINE__);
2944 if (NULL == (n = lookup_neighbour (peer)))
2945 return GNUNET_NO; /* can't affect us */
2946 if (session != n->primary_address.session)
2948 if (session == n->alternative_address.session)
2950 free_address (&n->alternative_address);
2951 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2952 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2953 n->state = S_CONNECTED;
2957 return GNUNET_NO; /* doesn't affect us further */
2960 n->expect_latency_response = GNUNET_NO;
2963 case S_NOT_CONNECTED:
2965 free_neighbour (n, GNUNET_NO);
2969 free_neighbour (n, GNUNET_NO);
2971 case S_INIT_BLACKLIST:
2972 case S_CONNECT_SENT:
2973 free_address (&n->primary_address);
2974 n->state = S_INIT_ATS;
2975 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2976 // FIXME: need to ask ATS for suggestions again?
2977 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2979 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2980 case S_CONNECT_RECV_ATS:
2981 case S_CONNECT_RECV_BLACKLIST:
2982 case S_CONNECT_RECV_ACK:
2983 /* error on inbound session; free neighbour entirely */
2984 free_address (&n->primary_address);
2985 free_neighbour (n, GNUNET_NO);
2988 free_address (&n->primary_address);
2989 n->state = S_RECONNECT_ATS;
2990 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2991 /* FIXME: is this ATS call needed? */
2992 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2994 case S_RECONNECT_ATS:
2995 /* we don't have an address, how can it go down? */
2998 case S_RECONNECT_BLACKLIST:
2999 case S_RECONNECT_SENT:
3000 n->state = S_RECONNECT_ATS;
3001 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3002 // FIXME: need to ask ATS for suggestions again?
3003 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3005 case S_CONNECTED_SWITCHING_BLACKLIST:
3006 /* primary went down while we were checking secondary against
3007 blacklist, adopt secondary as primary */
3008 free_address (&n->primary_address);
3009 n->primary_address = n->alternative_address;
3010 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3011 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3012 n->state = S_RECONNECT_BLACKLIST;
3014 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3015 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3016 secondary as primary */
3017 free_address (&n->primary_address);
3018 n->primary_address = n->alternative_address;
3019 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3020 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3021 n->state = S_RECONNECT_SENT;
3024 free_address (&n->primary_address);
3026 case S_DISCONNECT_FINISHED:
3027 /* neighbour was freed and plugins told to terminate session */
3031 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3035 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3036 GNUNET_SCHEDULER_cancel (n->task);
3037 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3043 * We received a 'SESSION_ACK' message from the other peer.
3044 * If we sent a 'CONNECT_ACK' last, this means we are now
3045 * connected. Otherwise, do nothing.
3047 * @param message possibly a 'struct SessionConnectMessage' (check format)
3048 * @param peer identity of the peer to switch the address for
3049 * @param address address of the other peer, NULL if other peer
3051 * @param session session to use (or NULL)
3052 * @param ats performance data
3053 * @param ats_count number of entries in ats
3056 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3057 const struct GNUNET_PeerIdentity *peer,
3058 const struct GNUNET_HELLO_Address *address,
3059 struct Session *session,
3060 const struct GNUNET_ATS_Information *ats,
3063 struct NeighbourMapEntry *n;
3065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3066 "Received SESSION_ACK message from peer `%s'\n",
3068 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3070 GNUNET_break_op (0);
3073 if (NULL == (n = lookup_neighbour (peer)))
3075 /* check if we are in a plausible state for having sent
3076 a CONNECT_ACK. If not, return, otherwise break */
3077 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3078 (S_CONNECT_SENT != n->state) ) ||
3079 (2 != n->send_connect_ack) )
3081 GNUNET_STATISTICS_update (GST_stats,
3082 gettext_noop ("# unexpected SESSION ACK messages"), 1,
3086 n->state = S_CONNECTED;
3087 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3088 GNUNET_STATISTICS_set (GST_stats,
3089 gettext_noop ("# peers connected"),
3090 ++neighbours_connected,
3092 connect_notify_cb (callback_cls, &n->id, ats, ats_count,
3093 n->primary_address.bandwidth_in,
3094 n->primary_address.bandwidth_out);
3095 GNUNET_ATS_address_add(GST_ats,
3096 n->primary_address.address,
3097 n->primary_address.session,
3099 set_address (&n->primary_address,
3100 n->primary_address.address,
3101 n->primary_address.session,
3102 n->primary_address.bandwidth_in,
3103 n->primary_address.bandwidth_out,
3109 * Test if we're connected to the given peer.
3111 * @param target peer to test
3112 * @return GNUNET_YES if we are connected, GNUNET_NO if not
3115 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3117 return test_connected (lookup_neighbour (target));
3122 * Change the incoming quota for the given peer.
3124 * @param neighbour identity of peer to change qutoa for
3125 * @param quota new quota
3128 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3129 struct GNUNET_BANDWIDTH_Value32NBO quota)
3131 struct NeighbourMapEntry *n;
3133 if (NULL == (n = lookup_neighbour (neighbour)))
3135 GNUNET_STATISTICS_update (GST_stats,
3137 ("# SET QUOTA messages ignored (no such peer)"),
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3142 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3143 ntohl (quota.value__), GNUNET_i2s (&n->id));
3144 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3145 if (0 != ntohl (quota.value__))
3147 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
3148 GNUNET_i2s (&n->id), "SET_QUOTA");
3149 if (GNUNET_YES == test_connected (n))
3150 GNUNET_STATISTICS_update (GST_stats,
3151 gettext_noop ("# disconnects due to quota of 0"),
3153 disconnect_neighbour (n);
3158 * We received a disconnect message from the given peer,
3159 * validate and process.
3161 * @param peer sender of the message
3162 * @param msg the disconnect message
3165 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
3167 const struct GNUNET_MessageHeader
3170 struct NeighbourMapEntry *n;
3171 const struct SessionDisconnectMessage *sdm;
3172 struct GNUNET_HashCode hc;
3174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3175 "Received DISCONNECT message from peer `%s'\n",
3177 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3179 // GNUNET_break_op (0);
3180 GNUNET_STATISTICS_update (GST_stats,
3182 ("# disconnect messages ignored (old format)"), 1,
3186 sdm = (const struct SessionDisconnectMessage *) msg;
3187 if (NULL == (n = lookup_neighbour (peer)))
3188 return; /* gone already */
3189 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <= n->connect_ack_timestamp.abs_value)
3191 GNUNET_STATISTICS_update (GST_stats,
3193 ("# disconnect messages ignored (timestamp)"), 1,
3197 GNUNET_CRYPTO_hash (&sdm->public_key,
3198 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
3200 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
3202 GNUNET_break_op (0);
3205 if (ntohl (sdm->purpose.size) !=
3206 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3207 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded) +
3208 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3210 GNUNET_break_op (0);
3214 GNUNET_CRYPTO_ecc_verify
3215 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3216 &sdm->signature, &sdm->public_key))
3218 GNUNET_break_op (0);
3221 if (GNUNET_YES == test_connected (n))
3222 GNUNET_STATISTICS_update (GST_stats,
3224 ("# other peer asked to disconnect from us"), 1,
3226 disconnect_neighbour (n);
3231 * Closure for the neighbours_iterate function.
3233 struct IteratorContext
3236 * Function to call on each connected neighbour.
3238 GST_NeighbourIterator cb;
3248 * Call the callback from the closure for each connected neighbour.
3250 * @param cls the 'struct IteratorContext'
3251 * @param key the hash of the public key of the neighbour
3252 * @param value the 'struct NeighbourMapEntry'
3253 * @return GNUNET_OK (continue to iterate)
3256 neighbours_iterate (void *cls, const struct GNUNET_HashCode * key, void *value)
3258 struct IteratorContext *ic = cls;
3259 struct NeighbourMapEntry *n = value;
3261 if (GNUNET_YES == test_connected (n))
3263 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3264 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3266 if (NULL != n->primary_address.address)
3268 bandwidth_in = n->primary_address.bandwidth_in;
3269 bandwidth_out = n->primary_address.bandwidth_out;
3273 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3274 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3277 ic->cb (ic->cb_cls, &n->id, NULL, 0,
3278 n->primary_address.address,
3279 bandwidth_in, bandwidth_out);
3286 * Iterate over all connected neighbours.
3288 * @param cb function to call
3289 * @param cb_cls closure for cb
3292 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3294 struct IteratorContext ic;
3296 if (NULL == neighbours)
3297 return; /* can happen during shutdown */
3300 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
3305 * If we have an active connection to the given target, it must be shutdown.
3307 * @param target peer to disconnect from
3310 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3312 struct NeighbourMapEntry *n;
3314 if (NULL == (n = lookup_neighbour (target)))
3315 return; /* not active */
3316 if (GNUNET_YES == test_connected (n))
3317 GNUNET_STATISTICS_update (GST_stats,
3319 ("# disconnected from peer upon explicit request"), 1,
3321 disconnect_neighbour (n);
3326 * Obtain current latency information for the given neighbour.
3328 * @param peer to get the latency for
3329 * @return observed latency of the address, FOREVER if the
3330 * the connection is not up
3332 struct GNUNET_TIME_Relative
3333 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3335 struct NeighbourMapEntry *n;
3337 n = lookup_neighbour (peer);
3339 return GNUNET_TIME_UNIT_FOREVER_REL;
3343 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3344 case S_CONNECTED_SWITCHING_BLACKLIST:
3345 case S_RECONNECT_SENT:
3346 case S_RECONNECT_ATS:
3347 case S_RECONNECT_BLACKLIST:
3349 case S_NOT_CONNECTED:
3350 case S_INIT_BLACKLIST:
3352 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3353 case S_CONNECT_RECV_ATS:
3354 case S_CONNECT_RECV_BLACKLIST:
3355 case S_CONNECT_RECV_ACK:
3356 case S_CONNECT_SENT:
3358 case S_DISCONNECT_FINISHED:
3359 return GNUNET_TIME_UNIT_FOREVER_REL;
3361 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3365 return GNUNET_TIME_UNIT_FOREVER_REL;
3370 * Obtain current address information for the given neighbour.
3373 * @return address currently used
3375 struct GNUNET_HELLO_Address *
3376 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3378 struct NeighbourMapEntry *n;
3380 n = lookup_neighbour (peer);
3383 return n->primary_address.address;
3388 * Initialize the neighbours subsystem.
3390 * @param cls closure for callbacks
3391 * @param connect_cb function to call if we connect to a peer
3392 * @param disconnect_cb function to call if we disconnect from a peer
3393 * @param peer_address_cb function to call if we change an active address
3395 * @param max_fds maximum number of fds to use
3398 GST_neighbours_start (void *cls,
3399 NotifyConnect connect_cb,
3400 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3401 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3402 unsigned int max_fds)
3405 connect_notify_cb = connect_cb;
3406 disconnect_notify_cb = disconnect_cb;
3407 address_change_cb = peer_address_cb;
3408 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3413 * Disconnect from the given neighbour.
3416 * @param key hash of neighbour's public key (not used)
3417 * @param value the 'struct NeighbourMapEntry' of the neighbour
3418 * @return GNUNET_OK (continue to iterate)
3421 disconnect_all_neighbours (void *cls, const struct GNUNET_HashCode * key, void *value)
3423 struct NeighbourMapEntry *n = value;
3425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3426 "Disconnecting peer `%4s', %s\n",
3427 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3428 n->state = S_DISCONNECT_FINISHED;
3429 free_neighbour (n, GNUNET_NO);
3435 * Cleanup the neighbours subsystem.
3438 GST_neighbours_stop ()
3440 if (NULL == neighbours)
3442 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
3443 &disconnect_all_neighbours,
3445 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
3447 callback_cls = NULL;
3448 connect_notify_cb = NULL;
3449 disconnect_notify_cb = NULL;
3450 address_change_cb = NULL;
3454 /* end of file gnunet-service-transport_neighbours.c */