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 * Handle to the ongoing blacklist check.
735 struct GST_BlacklistCheck *bc;
740 * Hash map from peer identities to the respective 'struct NeighbourMapEntry'.
742 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
745 * We keep blacklist checks in a DLL so that we can find
746 * the 'sessions' in their 'struct NeighbourAddress' if
747 * a session goes down.
749 static struct BlackListCheckContext *bc_head;
752 * We keep blacklist checks in a DLL.
754 static struct BlackListCheckContext *bc_tail;
757 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
759 static void *callback_cls;
762 * Function to call when we connected to a neighbour.
764 static NotifyConnect connect_notify_cb;
767 * Function to call when we disconnected from a neighbour.
769 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
772 * Function to call when we changed an active address of a neighbour.
774 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
777 * counter for connected neighbours
779 static unsigned int neighbours_connected;
782 * Number of bytes we have currently queued for transmission.
784 static unsigned long long bytes_in_send_queue;
788 * Lookup a neighbour entry in the neighbours hash map.
790 * @param pid identity of the peer to look up
791 * @return the entry, NULL if there is no existing record
793 static struct NeighbourMapEntry *
794 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
796 if (NULL == neighbours)
798 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
802 print_state (int state)
807 case S_NOT_CONNECTED:
808 return "S_NOT_CONNECTED";
811 case S_INIT_BLACKLIST:
812 return "S_INIT_BLACKLIST";
814 return "S_CONNECT_SENT";
815 case S_CONNECT_RECV_BLACKLIST_INBOUND:
816 return "S_CONNECT_RECV_BLACKLIST_INBOUND";
817 case S_CONNECT_RECV_ATS:
818 return "S_CONNECT_RECV_ATS";
819 case S_CONNECT_RECV_BLACKLIST:
820 return "S_CONNECT_RECV_BLACKLIST";
821 case S_CONNECT_RECV_ACK:
822 return "S_CONNECT_RECV_ACK";
824 return "S_CONNECTED";
825 case S_RECONNECT_ATS:
826 return "S_RECONNECT_ATS";
827 case S_RECONNECT_BLACKLIST:
828 return "S_RECONNECT_BLACKLIST";
829 case S_RECONNECT_SENT:
830 return "S_RECONNECT_SENT";
831 case S_CONNECTED_SWITCHING_BLACKLIST:
832 return "S_CONNECTED_SWITCHING_BLACKLIST";
833 case S_CONNECTED_SWITCHING_CONNECT_SENT:
834 return "S_CONNECTED_SWITCHING_CONNECT_SENT";
836 return "S_DISCONNECT";
837 case S_DISCONNECT_FINISHED:
838 return "S_DISCONNECT_FINISHED";
846 * Test if we're connected to the given peer.
848 * @param n neighbour entry of peer to test
849 * @return GNUNET_YES if we are connected, GNUNET_NO if not
852 test_connected (struct NeighbourMapEntry *n)
858 case S_NOT_CONNECTED:
860 case S_INIT_BLACKLIST:
862 case S_CONNECT_RECV_BLACKLIST_INBOUND:
863 case S_CONNECT_RECV_ATS:
864 case S_CONNECT_RECV_BLACKLIST:
865 case S_CONNECT_RECV_ACK:
868 case S_RECONNECT_ATS:
869 case S_RECONNECT_BLACKLIST:
870 case S_RECONNECT_SENT:
871 case S_CONNECTED_SWITCHING_BLACKLIST:
872 case S_CONNECTED_SWITCHING_CONNECT_SENT:
875 case S_DISCONNECT_FINISHED:
878 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
882 return GNUNET_SYSERR;
886 * Send information about a new outbound quota to our clients.
888 * @param target affected peer
889 * @param quota new quota
892 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
893 struct GNUNET_BANDWIDTH_Value32NBO quota)
895 struct QuotaSetMessage q_msg;
897 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
898 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
899 ntohl (quota.value__), GNUNET_i2s (target));
900 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
901 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
903 q_msg.peer = (*target);
904 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
909 * We don't need a given neighbour address any more.
910 * Release its resources and give appropriate notifications
911 * to ATS and other subsystems.
913 * @param na address we are done with; 'na' itself must NOT be 'free'd, only the contents!
916 free_address (struct NeighbourAddress *na)
918 if (GNUNET_YES == na->ats_active)
920 GST_validation_set_address_use (na->address, na->session, GNUNET_NO, __LINE__);
921 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_NO);
922 address_change_cb (NULL, &na->address->peer, NULL);
925 na->ats_active = GNUNET_NO;
926 if (NULL != na->address)
928 MEMDEBUG_free (na->address, __LINE__);
929 //GNUNET_HELLO_address_free (na->address);
937 * Initialize the 'struct NeighbourAddress'.
939 * @param na neighbour address to initialize
940 * @param address address of the other peer, NULL if other peer
942 * @param session session to use (or NULL, in which case an
943 * address must be setup)
944 * @param bandwidth_in inbound quota to be used when connection is up
945 * @param bandwidth_out outbound quota to be used when connection is up
946 * @param is_active GNUNET_YES to mark this as the active address with ATS
949 set_address (struct NeighbourAddress *na,
950 const struct GNUNET_HELLO_Address *address,
951 struct Session *session,
952 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
953 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
956 struct GNUNET_TRANSPORT_PluginFunctions *papi;
957 if (NULL == (papi = GST_plugins_find (address->transport_name)))
962 if (session == na->session)
964 na->bandwidth_in = bandwidth_in;
965 na->bandwidth_out = bandwidth_out;
966 if (is_active != na->ats_active)
968 na->ats_active = is_active;
969 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
970 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
972 address_change_cb (NULL, &address->peer, address);
974 if (GNUNET_YES == is_active)
976 /* FIXME: is this the right place to set quotas? */
977 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
978 send_outbound_quota (&address->peer, bandwidth_out);
984 session = papi->get_session (papi->cls, address);
987 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
988 "Failed to obtain new session for peer `%s' and address '%s'\n",
989 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
990 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
993 na->address = GNUNET_HELLO_address_copy (address);
994 MEMDEBUG_add_alloc (na->address, GNUNET_HELLO_address_get_size (na->address), __LINE__);
995 na->bandwidth_in = bandwidth_in;
996 na->bandwidth_out = bandwidth_out;
997 na->session = session;
998 na->ats_active = is_active;
999 if (GNUNET_YES == is_active)
1001 /* Telling ATS about new session */
1002 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
1003 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
1004 address_change_cb (NULL, &address->peer, address);
1005 /* FIXME: is this the right place to set quotas? */
1006 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
1007 send_outbound_quota (&address->peer, bandwidth_out);
1013 * Free a neighbour map entry.
1015 * @param n entry to free
1016 * @param keep_sessions GNUNET_NO to tell plugin to terminate sessions,
1017 * GNUNET_YES to keep all sessions
1020 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
1022 struct MessageQueue *mq;
1023 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1024 struct GNUNET_HELLO_Address *backup_primary;
1026 n->is_active = NULL; /* always free'd by its own continuation! */
1028 /* fail messages currently in the queue */
1029 while (NULL != (mq = n->messages_head))
1031 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1032 if (NULL != mq->cont)
1033 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
1034 MEMDEBUG_free (mq, __LINE__);
1036 /* It is too late to send other peer disconnect notifications, but at
1037 least internally we need to get clean... */
1038 if (GNUNET_YES == test_connected (n))
1040 GNUNET_STATISTICS_set (GST_stats,
1041 gettext_noop ("# peers connected"),
1042 --neighbours_connected,
1044 disconnect_notify_cb (callback_cls, &n->id);
1047 n->state = S_DISCONNECT_FINISHED;
1049 if (NULL != n->primary_address.address)
1051 backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address);
1052 MEMDEBUG_add_alloc (backup_primary, GNUNET_HELLO_address_get_size(backup_primary), __LINE__);
1055 backup_primary = NULL;
1057 /* free addresses and mark as unused */
1058 free_address (&n->primary_address);
1059 free_address (&n->alternative_address);
1061 /* FIXME-PLUGIN-API: This does not seem to guarantee that all
1062 transport sessions eventually get killed due to inactivity; they
1063 MUST have their own timeout logic (but at least TCP doesn't have
1064 one yet). Are we sure that EVERY 'session' of a plugin is
1065 actually cleaned up this way!? Note that if we are switching
1066 between two TCP sessions to the same peer, the existing plugin
1067 API gives us not even the means to selectively kill only one of
1068 them! Killing all sessions like this seems to be very, very
1071 /* cut transport-level connection */
1072 if ((GNUNET_NO == keep_sessions) &&
1073 (NULL != backup_primary) &&
1074 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
1075 papi->disconnect (papi->cls, &n->id);
1077 MEMDEBUG_free_non_null (backup_primary, __LINE__);
1079 GNUNET_assert (GNUNET_YES ==
1080 GNUNET_CONTAINER_multihashmap_remove (neighbours,
1081 &n->id.hashPubKey, n));
1083 // FIXME-ATS-API: we might want to be more specific about
1084 // which states we do this from in the future (ATS should
1085 // have given us a 'suggest_address' handle, and if we have
1086 // such a handle, we should cancel the operation here!
1087 if (NULL != n->suggest_handle)
1089 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
1090 n->suggest_handle = NULL;
1093 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1095 GNUNET_SCHEDULER_cancel (n->task);
1096 n->task = GNUNET_SCHEDULER_NO_TASK;
1098 /* free rest of memory */
1099 MEMDEBUG_free (n, __LINE__);
1103 * Transmit a message using the current session of the given
1106 * @param n entry for the recipient
1107 * @param msgbuf buffer to transmit
1108 * @param msgbuf_size number of bytes in buffer
1109 * @param priority transmission priority
1110 * @param timeout transmission timeout
1111 * @param cont continuation to call when finished (can be NULL)
1112 * @param cont_cls closure for cont
1115 send_with_session (struct NeighbourMapEntry *n,
1116 const char *msgbuf, size_t msgbuf_size,
1118 struct GNUNET_TIME_Relative timeout,
1119 GNUNET_TRANSPORT_TransmitContinuation cont,
1122 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1124 GNUNET_assert (n->primary_address.session != NULL);
1125 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1126 (-1 == papi->send (papi->cls,
1127 n->primary_address.session,
1128 msgbuf, msgbuf_size,
1131 cont, cont_cls)))) &&
1133 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1134 GNUNET_break (NULL != papi);
1139 * Master task run for every neighbour. Performs all of the time-related
1140 * activities (keep alive, send next message, disconnect if idle, finish
1141 * clean up after disconnect).
1143 * @param cls the 'struct NeighbourMapEntry' for which we are running
1144 * @param tc scheduler context (unused)
1147 master_task (void *cls,
1148 const struct GNUNET_SCHEDULER_TaskContext *tc);
1152 * Function called when the 'DISCONNECT' message has been sent by the
1153 * plugin. Frees the neighbour --- if the entry still exists.
1156 * @param target identity of the neighbour that was disconnected
1157 * @param result GNUNET_OK if the disconnect got out successfully
1158 * @param payload bytes payload
1159 * @param physical bytes physical
1162 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1163 int result, size_t payload, size_t physical)
1165 struct NeighbourMapEntry *n;
1167 n = lookup_neighbour (target);
1169 return; /* already gone */
1170 if (S_DISCONNECT != n->state)
1171 return; /* have created a fresh entry since */
1172 n->state = S_DISCONNECT;
1173 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1174 GNUNET_SCHEDULER_cancel (n->task);
1175 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1180 * Transmit a DISCONNECT message to the other peer.
1182 * @param n neighbour to send DISCONNECT message.
1185 send_disconnect (struct NeighbourMapEntry *n)
1187 struct SessionDisconnectMessage disconnect_msg;
1189 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1190 "Sending DISCONNECT message to peer `%4s'\n",
1191 GNUNET_i2s (&n->id));
1192 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1193 disconnect_msg.header.type =
1194 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1195 disconnect_msg.reserved = htonl (0);
1196 disconnect_msg.purpose.size =
1197 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1198 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded) +
1199 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1200 disconnect_msg.purpose.purpose =
1201 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1202 disconnect_msg.timestamp =
1203 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1204 disconnect_msg.public_key = GST_my_public_key;
1205 GNUNET_assert (GNUNET_OK ==
1206 GNUNET_CRYPTO_ecc_sign (GST_my_private_key,
1207 &disconnect_msg.purpose,
1208 &disconnect_msg.signature));
1210 send_with_session (n,
1211 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1212 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1213 &send_disconnect_cont, NULL);
1214 GNUNET_STATISTICS_update (GST_stats,
1216 ("# DISCONNECT messages sent"), 1,
1222 * Disconnect from the given neighbour, clean up the record.
1224 * @param n neighbour to disconnect from
1227 disconnect_neighbour (struct NeighbourMapEntry *n)
1229 /* depending on state, notify neighbour and/or upper layers of this peer
1233 case S_NOT_CONNECTED:
1235 case S_INIT_BLACKLIST:
1236 /* other peer is completely unaware of us, no need to send DISCONNECT */
1237 n->state = S_DISCONNECT_FINISHED;
1238 free_neighbour (n, GNUNET_NO);
1240 case S_CONNECT_SENT:
1241 send_disconnect (n);
1242 n->state = S_DISCONNECT;
1244 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1245 case S_CONNECT_RECV_ATS:
1246 case S_CONNECT_RECV_BLACKLIST:
1247 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1248 n->state = S_DISCONNECT_FINISHED;
1249 free_neighbour (n, GNUNET_NO);
1251 case S_CONNECT_RECV_ACK:
1252 /* we DID ACK the other peer's request, must send DISCONNECT */
1253 send_disconnect (n);
1254 n->state = S_DISCONNECT;
1257 case S_RECONNECT_BLACKLIST:
1258 case S_RECONNECT_SENT:
1259 case S_CONNECTED_SWITCHING_BLACKLIST:
1260 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1261 /* we are currently connected, need to send disconnect and do
1262 internal notifications and update statistics */
1263 send_disconnect (n);
1264 GNUNET_STATISTICS_set (GST_stats,
1265 gettext_noop ("# peers connected"),
1266 --neighbours_connected,
1268 disconnect_notify_cb (callback_cls, &n->id);
1269 n->state = S_DISCONNECT;
1271 case S_RECONNECT_ATS:
1272 /* ATS address request timeout, disconnect without sending disconnect message */
1273 GNUNET_STATISTICS_set (GST_stats,
1274 gettext_noop ("# peers connected"),
1275 --neighbours_connected,
1277 disconnect_notify_cb (callback_cls, &n->id);
1278 n->state = S_DISCONNECT;
1281 /* already disconnected, ignore */
1283 case S_DISCONNECT_FINISHED:
1284 /* already cleaned up, how did we get here!? */
1288 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1292 /* schedule timeout to clean up */
1293 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1294 GNUNET_SCHEDULER_cancel (n->task);
1295 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1301 * We're done with our transmission attempt, continue processing.
1303 * @param cls the 'struct MessageQueue' of the message
1304 * @param receiver intended receiver
1305 * @param success whether it worked or not
1306 * @param size_payload bytes payload sent
1307 * @param physical bytes sent on wire
1310 transmit_send_continuation (void *cls,
1311 const struct GNUNET_PeerIdentity *receiver,
1312 int success, size_t size_payload, size_t physical)
1314 struct MessageQueue *mq = cls;
1315 struct NeighbourMapEntry *n;
1317 if (NULL == (n = lookup_neighbour (receiver)))
1319 MEMDEBUG_free (mq, __LINE__);
1320 return; /* disconnect or other error while transmitting, can happen */
1322 if (n->is_active == mq)
1324 /* this is still "our" neighbour, remove us from its queue
1325 and allow it to send the next message now */
1326 n->is_active = NULL;
1327 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1328 GNUNET_SCHEDULER_cancel (n->task);
1329 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1331 if (bytes_in_send_queue < mq->message_buf_size)
1333 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1334 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1335 bytes_in_send_queue, mq->message_buf_size,
1336 (GNUNET_OK == success) ? "OK" : "FAIL",
1337 size_payload, physical);
1342 GNUNET_break (size_payload == mq->message_buf_size);
1343 bytes_in_send_queue -= mq->message_buf_size;
1344 GNUNET_STATISTICS_set (GST_stats,
1346 ("# bytes in message queue for other peers"),
1347 bytes_in_send_queue, GNUNET_NO);
1348 if (GNUNET_OK == success)
1349 GNUNET_STATISTICS_update (GST_stats,
1351 ("# messages transmitted to other peers"),
1354 GNUNET_STATISTICS_update (GST_stats,
1356 ("# transmission failures for messages to other peers"),
1358 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1359 "Sending message to `%s' of type %u was a %s\n",
1360 GNUNET_i2s (receiver),
1361 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1362 (success == GNUNET_OK) ? "success" : "FAILURE");
1363 if (NULL != mq->cont)
1364 mq->cont (mq->cont_cls, success, size_payload, physical);
1365 MEMDEBUG_free (mq, __LINE__);
1370 * Check the message list for the given neighbour and if we can
1371 * send a message, do so. This function should only be called
1372 * if the connection is at least generally ready for transmission.
1373 * While we will only send one message at a time, no bandwidth
1374 * quota management is performed here. If a message was given to
1375 * the plugin, the continuation will automatically re-schedule
1376 * the 'master' task once the next message might be transmitted.
1378 * @param n target peer for which to transmit
1381 try_transmission_to_peer (struct NeighbourMapEntry *n)
1383 struct MessageQueue *mq;
1384 struct GNUNET_TIME_Relative timeout;
1386 if (NULL == n->primary_address.address)
1388 /* no address, why are we here? */
1392 if ((0 == n->primary_address.address->address_length) &&
1393 (NULL == n->primary_address.session))
1395 /* no address, why are we here? */
1399 if (NULL != n->is_active)
1401 /* transmission already pending */
1405 /* timeout messages from the queue that are past their due date */
1406 while (NULL != (mq = n->messages_head))
1408 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1409 if (timeout.rel_value > 0)
1411 GNUNET_STATISTICS_update (GST_stats,
1413 ("# messages timed out while in transport queue"),
1415 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1417 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0); /* timeout */
1420 return; /* no more messages */
1421 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1423 send_with_session (n,
1424 mq->message_buf, mq->message_buf_size,
1425 0 /* priority */, timeout,
1426 &transmit_send_continuation, mq);
1431 * Send keepalive message to the neighbour. Must only be called
1432 * if we are on 'connected' state or while trying to switch addresses.
1433 * Will internally determine if a keepalive is truly needed (so can
1434 * always be called).
1436 * @param n neighbour that went idle and needs a keepalive
1439 send_keepalive (struct NeighbourMapEntry *n)
1441 struct GNUNET_MessageHeader m;
1443 GNUNET_assert ((S_CONNECTED == n->state) ||
1444 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1445 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1446 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value > 0)
1447 return; /* no keepalive needed at this time */
1448 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1449 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1450 send_with_session (n,
1451 (const void *) &m, sizeof (m),
1452 UINT32_MAX /* priority */,
1453 KEEPALIVE_FREQUENCY,
1455 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1457 n->expect_latency_response = GNUNET_YES;
1458 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1459 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1464 * Keep the connection to the given neighbour alive longer,
1465 * we received a KEEPALIVE (or equivalent); send a response.
1467 * @param neighbour neighbour to keep alive (by sending keep alive response)
1470 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1472 struct NeighbourMapEntry *n;
1473 struct GNUNET_MessageHeader m;
1475 if (NULL == (n = lookup_neighbour (neighbour)))
1477 GNUNET_STATISTICS_update (GST_stats,
1479 ("# KEEPALIVE messages discarded (peer unknown)"),
1483 if (NULL == n->primary_address.session)
1485 GNUNET_STATISTICS_update (GST_stats,
1487 ("# KEEPALIVE messages discarded (no session)"),
1491 /* send reply to allow neighbour to measure latency */
1492 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1493 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1494 send_with_session(n,
1495 (const void *) &m, sizeof (m),
1496 UINT32_MAX /* priority */,
1497 KEEPALIVE_FREQUENCY,
1503 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1504 * latency to this peer. Pass the updated information (existing ats
1505 * plus calculated latency) to ATS.
1507 * @param neighbour neighbour to keep alive
1510 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
1512 struct NeighbourMapEntry *n;
1514 struct GNUNET_ATS_Information ats;
1516 if (NULL == (n = lookup_neighbour (neighbour)))
1518 GNUNET_STATISTICS_update (GST_stats,
1520 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1524 if ( (S_CONNECTED != n->state) ||
1525 (GNUNET_YES != n->expect_latency_response) )
1527 GNUNET_STATISTICS_update (GST_stats,
1529 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1533 n->expect_latency_response = GNUNET_NO;
1534 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1535 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1536 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1537 "Latency for peer `%s' is %llu ms\n",
1538 GNUNET_i2s (&n->id), n->latency.rel_value);
1539 /* append latency */
1540 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1541 if (n->latency.rel_value > UINT32_MAX)
1542 latency = UINT32_MAX;
1544 latency = n->latency.rel_value;
1545 ats.value = htonl (latency);
1546 GST_update_ats_metrics (&n->id,
1547 n->primary_address.address,
1548 n->primary_address.session,
1554 * We have received a message from the given sender. How long should
1555 * we delay before receiving more? (Also used to keep the peer marked
1558 * @param sender sender of the message
1559 * @param size size of the message
1560 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1561 * GNUNET_NO if the neighbour is not connected or violates the quota,
1562 * GNUNET_SYSERR if the connection is not fully up yet
1563 * @return how long to wait before reading more from this sender
1565 struct GNUNET_TIME_Relative
1566 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1567 *sender, ssize_t size, int *do_forward)
1569 struct NeighbourMapEntry *n;
1570 struct GNUNET_TIME_Relative ret;
1572 if (NULL == neighbours)
1574 *do_forward = GNUNET_NO;
1575 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1577 if (NULL == (n = lookup_neighbour (sender)))
1579 GST_neighbours_try_connect (sender);
1580 if (NULL == (n = lookup_neighbour (sender)))
1582 GNUNET_STATISTICS_update (GST_stats,
1584 ("# messages discarded due to lack of neighbour record"),
1586 *do_forward = GNUNET_NO;
1587 return GNUNET_TIME_UNIT_ZERO;
1590 if (! test_connected (n))
1592 *do_forward = GNUNET_SYSERR;
1593 return GNUNET_TIME_UNIT_ZERO;
1595 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1597 n->quota_violation_count++;
1598 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1599 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1600 n->in_tracker.available_bytes_per_s__,
1601 n->quota_violation_count);
1602 /* Discount 32k per violation */
1603 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1607 if (n->quota_violation_count > 0)
1609 /* try to add 32k back */
1610 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1611 n->quota_violation_count--;
1614 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1616 GNUNET_STATISTICS_update (GST_stats,
1618 ("# bandwidth quota violations by other peers"),
1620 *do_forward = GNUNET_NO;
1621 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1623 *do_forward = GNUNET_YES;
1624 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1625 if (ret.rel_value > 0)
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1629 (unsigned long long) n->in_tracker.
1630 consumption_since_last_update__,
1631 (unsigned int) n->in_tracker.available_bytes_per_s__,
1632 (unsigned long long) ret.rel_value);
1633 GNUNET_STATISTICS_update (GST_stats,
1634 gettext_noop ("# ms throttling suggested"),
1635 (int64_t) ret.rel_value, GNUNET_NO);
1642 * Transmit a message to the given target using the active connection.
1644 * @param target destination
1645 * @param msg message to send
1646 * @param msg_size number of bytes in msg
1647 * @param timeout when to fail with timeout
1648 * @param cont function to call when done
1649 * @param cont_cls closure for 'cont'
1652 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1653 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1654 GST_NeighbourSendContinuation cont, void *cont_cls)
1656 struct NeighbourMapEntry *n;
1657 struct MessageQueue *mq;
1659 /* All ove these cases should never happen; they are all API violations.
1660 But we check anyway, just to be sure. */
1661 if (NULL == (n = lookup_neighbour (target)))
1665 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1668 if (GNUNET_YES != test_connected (n))
1672 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1675 bytes_in_send_queue += msg_size;
1676 GNUNET_STATISTICS_set (GST_stats,
1678 ("# bytes in message queue for other peers"),
1679 bytes_in_send_queue, GNUNET_NO);
1680 mq = MEMDEBUG_malloc (sizeof (struct MessageQueue) + msg_size, __LINE__);
1682 mq->cont_cls = cont_cls;
1683 memcpy (&mq[1], msg, msg_size);
1684 mq->message_buf = (const char *) &mq[1];
1685 mq->message_buf_size = msg_size;
1686 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1687 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1688 if ( (NULL != n->is_active) ||
1689 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1691 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1692 GNUNET_SCHEDULER_cancel (n->task);
1693 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1698 * Send a SESSION_CONNECT message via the given address.
1700 * @param na address to use
1703 send_session_connect (struct NeighbourAddress *na)
1705 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1706 struct SessionConnectMessage connect_msg;
1708 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1713 if (NULL == na->session)
1714 na->session = papi->get_session (papi->cls, na->address);
1715 if (NULL == na->session)
1720 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1721 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1722 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1723 connect_msg.reserved = htonl (0);
1724 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1725 (void) papi->send (papi->cls,
1727 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1729 GNUNET_TIME_UNIT_FOREVER_REL,
1736 * Send a SESSION_CONNECT_ACK message via the given address.
1738 * @param address address to use
1739 * @param session session to use
1740 * @param timestamp timestamp to use for the ACK message
1743 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1744 struct Session *session,
1745 struct GNUNET_TIME_Absolute timestamp)
1747 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1748 struct SessionConnectMessage connect_msg;
1750 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1755 if (NULL == session)
1756 session = papi->get_session (papi->cls, address);
1757 if (NULL == session)
1762 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1763 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1764 connect_msg.reserved = htonl (0);
1765 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1766 (void) papi->send (papi->cls,
1768 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1770 GNUNET_TIME_UNIT_FOREVER_REL,
1777 * Create a fresh entry in the neighbour map for the given peer
1779 * @param peer peer to create an entry for
1780 * @return new neighbour map entry
1782 static struct NeighbourMapEntry *
1783 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1785 struct NeighbourMapEntry *n;
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1788 "Creating new neighbour entry for `%s'\n",
1790 n = MEMDEBUG_malloc (sizeof (struct NeighbourMapEntry), __LINE__);
1792 n->state = S_NOT_CONNECTED;
1793 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1794 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1795 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1796 MAX_BANDWIDTH_CARRY_S);
1797 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1798 GNUNET_assert (GNUNET_OK ==
1799 GNUNET_CONTAINER_multihashmap_put (neighbours,
1800 &n->id.hashPubKey, n,
1801 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1807 * Check if the two given addresses are the same.
1808 * Actually only checks if the sessions are non-NULL
1809 * (which they should be) and then if they are identical;
1810 * the actual addresses don't matter if the session
1811 * pointers match anyway, and we must have session pointers
1814 * @param a1 first address to compare
1815 * @param a2 other address to compare
1816 * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
1819 address_matches (const struct NeighbourAddress *a1,
1820 const struct NeighbourAddress *a2)
1822 if ( (NULL == a1->session) ||
1823 (NULL == a2->session) )
1828 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1833 * Try to create a connection to the given target (eventually).
1835 * @param target peer to try to connect to
1838 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1840 struct NeighbourMapEntry *n;
1842 if (NULL == neighbours)
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1845 "Asked to connect to peer `%s' during shutdown\n",
1846 GNUNET_i2s (target));
1847 return; /* during shutdown, do nothing */
1849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1850 "Asked to connect to peer `%s'\n",
1851 GNUNET_i2s (target));
1852 if (0 == memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1854 /* refuse to connect to myself */
1855 /* FIXME: can this happen? Is this not an API violation? */
1856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1857 "Refusing to try to connect to myself.\n");
1860 n = lookup_neighbour (target);
1865 case S_NOT_CONNECTED:
1866 /* this should not be possible */
1868 free_neighbour (n, GNUNET_NO);
1871 case S_INIT_BLACKLIST:
1872 case S_CONNECT_SENT:
1873 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1874 case S_CONNECT_RECV_ATS:
1875 case S_CONNECT_RECV_BLACKLIST:
1876 case S_CONNECT_RECV_ACK:
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1878 "Ignoring request to try to connect to `%s', already trying!\n",
1879 GNUNET_i2s (target));
1880 return; /* already trying */
1882 case S_RECONNECT_ATS:
1883 case S_RECONNECT_BLACKLIST:
1884 case S_RECONNECT_SENT:
1885 case S_CONNECTED_SWITCHING_BLACKLIST:
1886 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 "Ignoring request to try to connect, already connected to `%s'!\n",
1889 GNUNET_i2s (target));
1890 return; /* already connected */
1892 /* get rid of remains, ready to re-try immediately */
1893 free_neighbour (n, GNUNET_NO);
1895 case S_DISCONNECT_FINISHED:
1896 /* should not be possible */
1899 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1901 free_neighbour (n, GNUNET_NO);
1905 n = setup_neighbour (target);
1906 n->state = S_INIT_ATS;
1907 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1909 GNUNET_ATS_reset_backoff (GST_ats, target);
1910 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1915 * Function called with the result of a blacklist check.
1917 * @param cls closure with the 'struct BlackListCheckContext'
1918 * @param peer peer this check affects
1919 * @param result GNUNET_OK if the address is allowed
1922 handle_test_blacklist_cont (void *cls,
1923 const struct GNUNET_PeerIdentity *peer,
1926 struct BlackListCheckContext *bcc = cls;
1927 struct NeighbourMapEntry *n;
1930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1931 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1933 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1934 if (NULL == (n = lookup_neighbour (peer)))
1935 goto cleanup; /* nobody left to care about new address */
1938 case S_NOT_CONNECTED:
1939 /* this should not be possible */
1941 free_neighbour (n, GNUNET_NO);
1944 /* still waiting on ATS suggestion */
1946 case S_INIT_BLACKLIST:
1947 /* check if the address the blacklist was fine with matches
1948 ATS suggestion, if so, we can move on! */
1949 if ( (GNUNET_OK == result) &&
1950 (1 == n->send_connect_ack) )
1952 n->send_connect_ack = 2;
1953 send_session_connect_ack_message (bcc->na.address,
1955 n->connect_ack_timestamp);
1957 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1958 break; /* result for an address we currently don't care about */
1959 if (GNUNET_OK == result)
1961 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
1962 n->state = S_CONNECT_SENT;
1963 send_session_connect (&n->primary_address);
1967 // FIXME: should also possibly destroy session with plugin!?
1968 GNUNET_ATS_address_destroyed (GST_ats,
1971 free_address (&n->primary_address);
1972 n->state = S_INIT_ATS;
1973 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1974 // FIXME: do we need to ask ATS again for suggestions?
1975 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
1978 case S_CONNECT_SENT:
1979 /* waiting on CONNECT_ACK, send ACK if one is pending */
1980 if ( (GNUNET_OK == result) &&
1981 (1 == n->send_connect_ack) )
1983 n->send_connect_ack = 2;
1984 send_session_connect_ack_message (n->primary_address.address,
1985 n->primary_address.session,
1986 n->connect_ack_timestamp);
1989 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1990 if (GNUNET_OK == result)
1992 /* valid new address, let ATS know! */
1993 GNUNET_ATS_address_add (GST_ats,
1998 n->state = S_CONNECT_RECV_ATS;
1999 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2000 GNUNET_ATS_reset_backoff (GST_ats, peer);
2001 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2003 case S_CONNECT_RECV_ATS:
2004 /* still waiting on ATS suggestion, don't care about blacklist */
2006 case S_CONNECT_RECV_BLACKLIST:
2007 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2008 break; /* result for an address we currently don't care about */
2009 if (GNUNET_OK == result)
2011 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
2012 n->state = S_CONNECT_RECV_ACK;
2013 send_session_connect_ack_message (bcc->na.address,
2015 n->connect_ack_timestamp);
2016 if (1 == n->send_connect_ack)
2017 n->send_connect_ack = 2;
2021 // FIXME: should also possibly destroy session with plugin!?
2022 GNUNET_ATS_address_destroyed (GST_ats,
2025 free_address (&n->primary_address);
2026 n->state = S_INIT_ATS;
2027 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2028 // FIXME: do we need to ask ATS again for suggestions?
2029 GNUNET_ATS_reset_backoff (GST_ats, peer);
2030 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2033 case S_CONNECT_RECV_ACK:
2034 /* waiting on SESSION_ACK, send ACK if one is pending */
2035 if ( (GNUNET_OK == result) &&
2036 (1 == n->send_connect_ack) )
2038 n->send_connect_ack = 2;
2039 send_session_connect_ack_message (n->primary_address.address,
2040 n->primary_address.session,
2041 n->connect_ack_timestamp);
2045 /* already connected, don't care about blacklist */
2047 case S_RECONNECT_ATS:
2048 /* still waiting on ATS suggestion, don't care about blacklist */
2050 case S_RECONNECT_BLACKLIST:
2051 if ( (GNUNET_OK == result) &&
2052 (1 == n->send_connect_ack) )
2054 n->send_connect_ack = 2;
2055 send_session_connect_ack_message (bcc->na.address,
2057 n->connect_ack_timestamp);
2059 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2060 break; /* result for an address we currently don't care about */
2061 if (GNUNET_OK == result)
2063 send_session_connect (&n->primary_address);
2064 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2065 n->state = S_RECONNECT_SENT;
2069 GNUNET_ATS_address_destroyed (GST_ats,
2072 n->state = S_RECONNECT_ATS;
2073 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2074 // FIXME: do we need to ask ATS again for suggestions?
2075 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2078 case S_RECONNECT_SENT:
2079 /* waiting on CONNECT_ACK, don't care about blacklist */
2080 if ( (GNUNET_OK == result) &&
2081 (1 == n->send_connect_ack) )
2083 n->send_connect_ack = 2;
2084 send_session_connect_ack_message (n->primary_address.address,
2085 n->primary_address.session,
2086 n->connect_ack_timestamp);
2089 case S_CONNECTED_SWITCHING_BLACKLIST:
2090 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2091 break; /* result for an address we currently don't care about */
2092 if (GNUNET_OK == result)
2094 send_session_connect (&n->alternative_address);
2095 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
2099 GNUNET_ATS_address_destroyed (GST_ats,
2102 free_address (&n->alternative_address);
2103 n->state = S_CONNECTED;
2106 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2107 /* waiting on CONNECT_ACK, don't care about blacklist */
2108 if ( (GNUNET_OK == result) &&
2109 (1 == n->send_connect_ack) )
2111 n->send_connect_ack = 2;
2112 send_session_connect_ack_message (n->primary_address.address,
2113 n->primary_address.session,
2114 n->connect_ack_timestamp);
2118 /* Nothing to do here, ATS will already do what can be done */
2120 case S_DISCONNECT_FINISHED:
2121 /* should not be possible */
2125 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2127 free_neighbour (n, GNUNET_NO);
2131 MEMDEBUG_free (bcc->na.address, __LINE__);
2132 //GNUNET_HELLO_address_free (bcc->na.address);
2133 GNUNET_CONTAINER_DLL_remove (bc_head,
2136 MEMDEBUG_free (bcc, __LINE__);
2141 * We want to know if connecting to a particular peer via
2142 * a particular address is allowed. Check it!
2144 * @param peer identity of the peer to switch the address for
2145 * @param ts time at which the check was initiated
2146 * @param address address of the other peer, NULL if other peer
2148 * @param session session to use (or NULL)
2151 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2152 struct GNUNET_TIME_Absolute ts,
2153 const struct GNUNET_HELLO_Address *address,
2154 struct Session *session)
2156 struct BlackListCheckContext *bcc;
2157 struct GST_BlacklistCheck *bc;
2160 MEMDEBUG_malloc (sizeof (struct BlackListCheckContext), __LINE__);
2161 bcc->na.address = GNUNET_HELLO_address_copy (address);
2162 MEMDEBUG_add_alloc (bcc->na.address, GNUNET_HELLO_address_get_size (address), __LINE__);
2163 bcc->na.session = session;
2164 bcc->na.connect_timestamp = ts;
2165 GNUNET_CONTAINER_DLL_insert (bc_head,
2168 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2169 address->transport_name,
2170 &handle_test_blacklist_cont, bcc)))
2172 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2173 we must only store 'bc' if 'bc' is non-NULL... */
2178 * We received a 'SESSION_CONNECT' message from the other peer.
2179 * Consider switching to it.
2181 * @param message possibly a 'struct SessionConnectMessage' (check format)
2182 * @param peer identity of the peer to switch the address for
2183 * @param address address of the other peer, NULL if other peer
2185 * @param session session to use (or NULL)
2188 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2189 const struct GNUNET_PeerIdentity *peer,
2190 const struct GNUNET_HELLO_Address *address,
2191 struct Session *session)
2193 const struct SessionConnectMessage *scm;
2194 struct NeighbourMapEntry *n;
2195 struct GNUNET_TIME_Absolute ts;
2197 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2198 "Received CONNECT message from peer `%s'\n",
2201 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2203 GNUNET_break_op (0);
2206 if (NULL == neighbours)
2207 return; /* we're shutting down */
2208 scm = (const struct SessionConnectMessage *) message;
2209 GNUNET_break_op (0 == ntohl (scm->reserved));
2210 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2211 n = lookup_neighbour (peer);
2213 n = setup_neighbour (peer);
2214 n->send_connect_ack = 1;
2215 n->connect_ack_timestamp = ts;
2219 case S_NOT_CONNECTED:
2220 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2221 /* Do a blacklist check for the new address */
2222 check_blacklist (peer, ts, address, session);
2225 /* CONNECT message takes priority over us asking ATS for address */
2226 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2228 case S_INIT_BLACKLIST:
2229 case S_CONNECT_SENT:
2230 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2231 case S_CONNECT_RECV_ATS:
2232 case S_CONNECT_RECV_BLACKLIST:
2233 case S_CONNECT_RECV_ACK:
2234 /* It can never hurt to have an alternative address in the above cases,
2235 see if it is allowed */
2236 check_blacklist (peer, ts, address, session);
2239 /* we are already connected and can thus send the ACK immediately;
2240 still, it can never hurt to have an alternative address, so also
2241 tell ATS about it */
2242 GNUNET_assert (NULL != n->primary_address.address);
2243 GNUNET_assert (NULL != n->primary_address.session);
2244 n->send_connect_ack = 0;
2245 send_session_connect_ack_message (n->primary_address.address,
2246 n->primary_address.session, ts);
2247 check_blacklist (peer, ts, address, session);
2249 case S_RECONNECT_ATS:
2250 case S_RECONNECT_BLACKLIST:
2251 case S_RECONNECT_SENT:
2252 /* It can never hurt to have an alternative address in the above cases,
2253 see if it is allowed */
2254 check_blacklist (peer, ts, address, session);
2256 case S_CONNECTED_SWITCHING_BLACKLIST:
2257 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2258 /* we are already connected and can thus send the ACK immediately;
2259 still, it can never hurt to have an alternative address, so also
2260 tell ATS about it */
2261 GNUNET_assert (NULL != n->primary_address.address);
2262 GNUNET_assert (NULL != n->primary_address.session);
2263 n->send_connect_ack = 0;
2264 send_session_connect_ack_message (n->primary_address.address,
2265 n->primary_address.session, ts);
2266 check_blacklist (peer, ts, address, session);
2269 /* get rid of remains without terminating sessions, ready to re-try */
2270 free_neighbour (n, GNUNET_YES);
2271 n = setup_neighbour (peer);
2272 n->state = S_CONNECT_RECV_ATS;
2273 GNUNET_ATS_reset_backoff (GST_ats, peer);
2274 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2276 case S_DISCONNECT_FINISHED:
2277 /* should not be possible */
2281 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2283 free_neighbour (n, GNUNET_NO);
2290 * For an existing neighbour record, set the active connection to
2291 * use the given address.
2293 * @param peer identity of the peer to switch the address for
2294 * @param address address of the other peer, NULL if other peer
2296 * @param session session to use (or NULL)
2297 * @param ats performance data
2298 * @param ats_count number of entries in ats
2299 * @param bandwidth_in inbound quota to be used when connection is up
2300 * @param bandwidth_out outbound quota to be used when connection is up
2303 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2304 const struct GNUNET_HELLO_Address *address,
2305 struct Session *session,
2306 const struct GNUNET_ATS_Information *ats,
2308 struct GNUNET_BANDWIDTH_Value32NBO
2310 struct GNUNET_BANDWIDTH_Value32NBO
2313 struct NeighbourMapEntry *n;
2314 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2316 GNUNET_assert (address->transport_name != NULL);
2317 if (NULL == (n = lookup_neighbour (peer)))
2320 /* Obtain an session for this address from plugin */
2321 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2323 /* we don't have the plugin for this address */
2324 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2325 "2348 : `%s' \n", address->transport_name);
2326 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2329 if ((NULL == session) && (0 == address->address_length))
2332 if (strlen (address->transport_name) > 0)
2333 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2338 "ATS tells us to switch to address '%s' session %p for "
2339 "peer `%s' in state %s (quota in/out %u %u )\n",
2340 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2343 print_state (n->state),
2344 ntohl (bandwidth_in.value__),
2345 ntohl (bandwidth_out.value__));
2347 if (NULL == session)
2349 session = papi->get_session (papi->cls, address);
2350 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2351 "Obtained new session for peer `%s' and address '%s': %p\n",
2352 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2354 if (NULL == session)
2356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2357 "Failed to obtain new session for peer `%s' and address '%s'\n",
2358 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2359 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2364 case S_NOT_CONNECTED:
2366 free_neighbour (n, GNUNET_NO);
2369 set_address (&n->primary_address,
2370 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2371 n->state = S_INIT_BLACKLIST;
2372 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2373 check_blacklist (&n->id,
2374 n->connect_ack_timestamp,
2377 case S_INIT_BLACKLIST:
2378 /* ATS suggests a different address, switch again */
2379 set_address (&n->primary_address,
2380 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2381 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2382 check_blacklist (&n->id,
2383 n->connect_ack_timestamp,
2386 case S_CONNECT_SENT:
2387 /* ATS suggests a different address, switch again */
2388 set_address (&n->primary_address,
2389 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2390 n->state = S_INIT_BLACKLIST;
2391 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2392 check_blacklist (&n->id,
2393 n->connect_ack_timestamp,
2396 case S_CONNECT_RECV_ATS:
2397 set_address (&n->primary_address,
2398 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2399 n->state = S_CONNECT_RECV_BLACKLIST;
2400 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2401 check_blacklist (&n->id,
2402 n->connect_ack_timestamp,
2405 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2406 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2407 check_blacklist (&n->id,
2408 n->connect_ack_timestamp,
2411 case S_CONNECT_RECV_BLACKLIST:
2412 case S_CONNECT_RECV_ACK:
2413 /* ATS asks us to switch while we were trying to connect; switch to new
2414 address and check blacklist again */
2415 set_address (&n->primary_address,
2416 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2417 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2418 check_blacklist (&n->id,
2419 n->connect_ack_timestamp,
2423 GNUNET_assert (NULL != n->primary_address.address);
2424 GNUNET_assert (NULL != n->primary_address.session);
2425 if (n->primary_address.session == session)
2427 /* not an address change, just a quota change */
2428 set_address (&n->primary_address,
2429 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2432 /* ATS asks us to switch a life connection; see if we can get
2433 a CONNECT_ACK on it before we actually do this! */
2434 set_address (&n->alternative_address,
2435 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2436 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2437 check_blacklist (&n->id,
2438 GNUNET_TIME_absolute_get (),
2441 case S_RECONNECT_ATS:
2442 set_address (&n->primary_address,
2443 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2444 n->state = S_RECONNECT_BLACKLIST;
2445 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2446 check_blacklist (&n->id,
2447 n->connect_ack_timestamp,
2450 case S_RECONNECT_BLACKLIST:
2451 /* ATS asks us to switch while we were trying to reconnect; switch to new
2452 address and check blacklist again */
2453 set_address (&n->primary_address,
2454 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2455 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2456 check_blacklist (&n->id,
2457 n->connect_ack_timestamp,
2460 case S_RECONNECT_SENT:
2461 /* ATS asks us to switch while we were trying to reconnect; switch to new
2462 address and check blacklist again */
2463 set_address (&n->primary_address,
2464 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2465 n->state = S_RECONNECT_BLACKLIST;
2466 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2467 check_blacklist (&n->id,
2468 n->connect_ack_timestamp,
2471 case S_CONNECTED_SWITCHING_BLACKLIST:
2472 if (n->primary_address.session == session)
2474 /* ATS switches back to still-active session */
2475 free_address (&n->alternative_address);
2476 n->state = S_CONNECTED;
2479 /* ATS asks us to switch a life connection, update blacklist check */
2480 set_address (&n->alternative_address,
2481 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2482 check_blacklist (&n->id,
2483 GNUNET_TIME_absolute_get (),
2486 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2487 if (n->primary_address.session == session)
2489 /* ATS switches back to still-active session */
2490 free_address (&n->alternative_address);
2491 n->state = S_CONNECTED;
2494 /* ATS asks us to switch a life connection, update blacklist check */
2495 set_address (&n->alternative_address,
2496 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2497 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2498 check_blacklist (&n->id,
2499 GNUNET_TIME_absolute_get (),
2503 /* not going to switch addresses while disconnecting */
2505 case S_DISCONNECT_FINISHED:
2509 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2517 * Master task run for every neighbour. Performs all of the time-related
2518 * activities (keep alive, send next message, disconnect if idle, finish
2519 * clean up after disconnect).
2521 * @param cls the 'struct NeighbourMapEntry' for which we are running
2522 * @param tc scheduler context (unused)
2525 master_task (void *cls,
2526 const struct GNUNET_SCHEDULER_TaskContext *tc)
2528 struct NeighbourMapEntry *n = cls;
2529 struct GNUNET_TIME_Relative delay;
2531 n->task = GNUNET_SCHEDULER_NO_TASK;
2532 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2534 "Master task runs for neighbour `%s' in state %s with timeout in %llu ms\n",
2535 GNUNET_i2s (&n->id),
2536 print_state(n->state),
2537 (unsigned long long) delay.rel_value);
2540 case S_NOT_CONNECTED:
2541 /* invalid state for master task, clean up */
2543 n->state = S_DISCONNECT_FINISHED;
2544 free_neighbour (n, GNUNET_NO);
2547 if (0 == delay.rel_value)
2549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2550 "Connection to `%s' timed out waiting for ATS to provide address\n",
2551 GNUNET_i2s (&n->id));
2552 n->state = S_DISCONNECT_FINISHED;
2553 free_neighbour (n, GNUNET_NO);
2557 case S_INIT_BLACKLIST:
2558 if (0 == delay.rel_value)
2560 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2561 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2562 GNUNET_i2s (&n->id));
2563 n->state = S_DISCONNECT_FINISHED;
2564 free_neighbour (n, GNUNET_NO);
2568 case S_CONNECT_SENT:
2569 if (0 == delay.rel_value)
2571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2572 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2573 GNUNET_i2s (&n->id));
2574 disconnect_neighbour (n);
2578 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2579 if (0 == delay.rel_value)
2581 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2582 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2583 GNUNET_i2s (&n->id));
2584 n->state = S_DISCONNECT_FINISHED;
2585 free_neighbour (n, GNUNET_NO);
2589 case S_CONNECT_RECV_ATS:
2590 if (0 == delay.rel_value)
2592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2593 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2594 GNUNET_i2s (&n->id));
2595 n->state = S_DISCONNECT_FINISHED;
2596 free_neighbour (n, GNUNET_NO);
2600 case S_CONNECT_RECV_BLACKLIST:
2601 if (0 == delay.rel_value)
2603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2604 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2605 GNUNET_i2s (&n->id));
2606 n->state = S_DISCONNECT_FINISHED;
2607 free_neighbour (n, GNUNET_NO);
2611 case S_CONNECT_RECV_ACK:
2612 if (0 == delay.rel_value)
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2616 GNUNET_i2s (&n->id));
2617 disconnect_neighbour (n);
2622 if (0 == delay.rel_value)
2624 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2625 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2626 GNUNET_i2s (&n->id));
2627 disconnect_neighbour (n);
2630 try_transmission_to_peer (n);
2633 case S_RECONNECT_ATS:
2634 if (0 == delay.rel_value)
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2637 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2638 GNUNET_i2s (&n->id));
2639 disconnect_neighbour (n);
2643 case S_RECONNECT_BLACKLIST:
2644 if (0 == delay.rel_value)
2646 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2647 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2648 GNUNET_i2s (&n->id));
2649 disconnect_neighbour (n);
2653 case S_RECONNECT_SENT:
2654 if (0 == delay.rel_value)
2656 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2657 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2658 GNUNET_i2s (&n->id));
2659 disconnect_neighbour (n);
2663 case S_CONNECTED_SWITCHING_BLACKLIST:
2664 if (0 == delay.rel_value)
2666 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2667 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2668 GNUNET_i2s (&n->id));
2669 disconnect_neighbour (n);
2672 try_transmission_to_peer (n);
2675 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2676 if (0 == delay.rel_value)
2678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2679 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2680 GNUNET_i2s (&n->id));
2681 disconnect_neighbour (n);
2684 try_transmission_to_peer (n);
2688 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2689 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2690 GNUNET_i2s (&n->id));
2691 free_neighbour (n, GNUNET_NO);
2693 case S_DISCONNECT_FINISHED:
2694 /* how did we get here!? */
2698 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2702 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2703 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2704 (S_CONNECTED == n->state) )
2706 /* if we are *now* in one of these three states, we're sending
2707 keep alive messages, so we need to consider the keepalive
2708 delay, not just the connection timeout */
2709 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2712 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2713 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2720 * Send a SESSION_ACK message to the neighbour to confirm that we
2721 * got his CONNECT_ACK.
2723 * @param n neighbour to send the SESSION_ACK to
2726 send_session_ack_message (struct NeighbourMapEntry *n)
2728 struct GNUNET_MessageHeader msg;
2730 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2731 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2732 (void) send_with_session(n,
2733 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2734 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2740 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2741 * Consider switching to it.
2743 * @param message possibly a 'struct SessionConnectMessage' (check format)
2744 * @param peer identity of the peer to switch the address for
2745 * @param address address of the other peer, NULL if other peer
2747 * @param session session to use (or NULL)
2750 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2751 const struct GNUNET_PeerIdentity *peer,
2752 const struct GNUNET_HELLO_Address *address,
2753 struct Session *session)
2755 const struct SessionConnectMessage *scm;
2756 struct GNUNET_TIME_Absolute ts;
2757 struct NeighbourMapEntry *n;
2759 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2760 "Received CONNECT_ACK message from peer `%s'\n",
2763 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2765 GNUNET_break_op (0);
2768 scm = (const struct SessionConnectMessage *) message;
2769 GNUNET_break_op (ntohl (scm->reserved) == 0);
2770 if (NULL == (n = lookup_neighbour (peer)))
2772 GNUNET_STATISTICS_update (GST_stats,
2774 ("# unexpected CONNECT_ACK messages (no peer)"),
2778 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2781 case S_NOT_CONNECTED:
2783 free_neighbour (n, GNUNET_NO);
2786 case S_INIT_BLACKLIST:
2787 GNUNET_STATISTICS_update (GST_stats,
2789 ("# unexpected CONNECT_ACK messages (not ready)"),
2792 case S_CONNECT_SENT:
2793 if (ts.abs_value != n->primary_address.connect_timestamp.abs_value)
2794 break; /* ACK does not match our original CONNECT message */
2795 n->state = S_CONNECTED;
2796 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2797 GNUNET_STATISTICS_set (GST_stats,
2798 gettext_noop ("# peers connected"),
2799 ++neighbours_connected,
2801 connect_notify_cb (callback_cls, &n->id,
2802 n->primary_address.bandwidth_in,
2803 n->primary_address.bandwidth_out);
2804 /* Tell ATS that the outbound session we created to send CONNECT was successfull */
2805 GNUNET_ATS_address_add (GST_ats,
2806 n->primary_address.address,
2807 n->primary_address.session,
2809 set_address (&n->primary_address,
2810 n->primary_address.address,
2811 n->primary_address.session,
2812 n->primary_address.bandwidth_in,
2813 n->primary_address.bandwidth_out,
2815 send_session_ack_message (n);
2817 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2818 case S_CONNECT_RECV_ATS:
2819 case S_CONNECT_RECV_BLACKLIST:
2820 case S_CONNECT_RECV_ACK:
2821 GNUNET_STATISTICS_update (GST_stats,
2823 ("# unexpected CONNECT_ACK messages (not ready)"),
2827 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2828 send_session_ack_message (n);
2830 case S_RECONNECT_ATS:
2831 case S_RECONNECT_BLACKLIST:
2832 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2833 to give us a new address... */
2834 GNUNET_STATISTICS_update (GST_stats,
2836 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2839 case S_RECONNECT_SENT:
2840 /* new address worked; go back to connected! */
2841 n->state = S_CONNECTED;
2842 send_session_ack_message (n);
2844 case S_CONNECTED_SWITCHING_BLACKLIST:
2845 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2846 send_session_ack_message (n);
2848 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2849 /* new address worked; adopt it and go back to connected! */
2850 n->state = S_CONNECTED;
2851 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2852 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2853 GNUNET_ATS_address_add(GST_ats,
2854 n->alternative_address.address,
2855 n->alternative_address.session,
2857 set_address (&n->primary_address,
2858 n->alternative_address.address,
2859 n->alternative_address.session,
2860 n->alternative_address.bandwidth_in,
2861 n->alternative_address.bandwidth_out,
2863 free_address (&n->alternative_address);
2864 send_session_ack_message (n);
2867 GNUNET_STATISTICS_update (GST_stats,
2869 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2872 case S_DISCONNECT_FINISHED:
2876 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2884 * A session was terminated. Take note; if needed, try to get
2885 * an alternative address from ATS.
2887 * @param peer identity of the peer where the session died
2888 * @param session session that is gone
2889 * @return GNUNET_YES if this was a session used, GNUNET_NO if
2890 * this session was not in use
2893 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2894 struct Session *session)
2896 struct NeighbourMapEntry *n;
2897 struct BlackListCheckContext *bcc;
2898 struct BlackListCheckContext *bcc_next;
2900 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2902 while (NULL != (bcc = bcc_next))
2904 bcc_next = bcc->next;
2905 if (bcc->na.session == session)
2907 GST_blacklist_test_cancel (bcc->bc);
2908 MEMDEBUG_free (bcc->na.address, __LINE__);
2909 //GNUNET_HELLO_address_free (bcc->na.address);
2910 GNUNET_CONTAINER_DLL_remove (bc_head,
2913 MEMDEBUG_free (bcc, __LINE__);
2916 if (NULL == (n = lookup_neighbour (peer)))
2917 return GNUNET_NO; /* can't affect us */
2918 if (session != n->primary_address.session)
2920 if (session == n->alternative_address.session)
2922 free_address (&n->alternative_address);
2923 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2924 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2925 n->state = S_CONNECTED;
2929 return GNUNET_NO; /* doesn't affect us further */
2932 n->expect_latency_response = GNUNET_NO;
2935 case S_NOT_CONNECTED:
2937 free_neighbour (n, GNUNET_NO);
2941 free_neighbour (n, GNUNET_NO);
2943 case S_INIT_BLACKLIST:
2944 case S_CONNECT_SENT:
2945 free_address (&n->primary_address);
2946 n->state = S_INIT_ATS;
2947 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2948 // FIXME: need to ask ATS for suggestions again?
2949 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2951 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2952 case S_CONNECT_RECV_ATS:
2953 case S_CONNECT_RECV_BLACKLIST:
2954 case S_CONNECT_RECV_ACK:
2955 /* error on inbound session; free neighbour entirely */
2956 free_address (&n->primary_address);
2957 free_neighbour (n, GNUNET_NO);
2960 free_address (&n->primary_address);
2961 n->state = S_RECONNECT_ATS;
2962 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2963 /* FIXME: is this ATS call needed? */
2964 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2966 case S_RECONNECT_ATS:
2967 /* we don't have an address, how can it go down? */
2970 case S_RECONNECT_BLACKLIST:
2971 case S_RECONNECT_SENT:
2972 n->state = S_RECONNECT_ATS;
2973 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2974 // FIXME: need to ask ATS for suggestions again?
2975 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2977 case S_CONNECTED_SWITCHING_BLACKLIST:
2978 /* primary went down while we were checking secondary against
2979 blacklist, adopt secondary as primary */
2980 free_address (&n->primary_address);
2981 n->primary_address = n->alternative_address;
2982 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2983 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2984 n->state = S_RECONNECT_BLACKLIST;
2986 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2987 /* primary went down while we were waiting for CONNECT_ACK on secondary;
2988 secondary as primary */
2989 free_address (&n->primary_address);
2990 n->primary_address = n->alternative_address;
2991 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
2992 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2993 n->state = S_RECONNECT_SENT;
2996 free_address (&n->primary_address);
2998 case S_DISCONNECT_FINISHED:
2999 /* neighbour was freed and plugins told to terminate session */
3003 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3007 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3008 GNUNET_SCHEDULER_cancel (n->task);
3009 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3015 * We received a 'SESSION_ACK' message from the other peer.
3016 * If we sent a 'CONNECT_ACK' last, this means we are now
3017 * connected. Otherwise, do nothing.
3019 * @param message possibly a 'struct SessionConnectMessage' (check format)
3020 * @param peer identity of the peer to switch the address for
3021 * @param address address of the other peer, NULL if other peer
3023 * @param session session to use (or NULL)
3026 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3027 const struct GNUNET_PeerIdentity *peer,
3028 const struct GNUNET_HELLO_Address *address,
3029 struct Session *session)
3031 struct NeighbourMapEntry *n;
3033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3034 "Received SESSION_ACK message from peer `%s'\n",
3036 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3038 GNUNET_break_op (0);
3041 if (NULL == (n = lookup_neighbour (peer)))
3043 /* check if we are in a plausible state for having sent
3044 a CONNECT_ACK. If not, return, otherwise break */
3045 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3046 (S_CONNECT_SENT != n->state) ) ||
3047 (2 != n->send_connect_ack) )
3049 GNUNET_STATISTICS_update (GST_stats,
3050 gettext_noop ("# unexpected SESSION ACK messages"), 1,
3054 n->state = S_CONNECTED;
3055 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3056 GNUNET_STATISTICS_set (GST_stats,
3057 gettext_noop ("# peers connected"),
3058 ++neighbours_connected,
3060 connect_notify_cb (callback_cls, &n->id,
3061 n->primary_address.bandwidth_in,
3062 n->primary_address.bandwidth_out);
3063 GNUNET_ATS_address_add(GST_ats,
3064 n->primary_address.address,
3065 n->primary_address.session,
3067 set_address (&n->primary_address,
3068 n->primary_address.address,
3069 n->primary_address.session,
3070 n->primary_address.bandwidth_in,
3071 n->primary_address.bandwidth_out,
3077 * Test if we're connected to the given peer.
3079 * @param target peer to test
3080 * @return GNUNET_YES if we are connected, GNUNET_NO if not
3083 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3085 return test_connected (lookup_neighbour (target));
3090 * Change the incoming quota for the given peer.
3092 * @param neighbour identity of peer to change qutoa for
3093 * @param quota new quota
3096 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3097 struct GNUNET_BANDWIDTH_Value32NBO quota)
3099 struct NeighbourMapEntry *n;
3101 if (NULL == (n = lookup_neighbour (neighbour)))
3103 GNUNET_STATISTICS_update (GST_stats,
3105 ("# SET QUOTA messages ignored (no such peer)"),
3109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3110 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3111 ntohl (quota.value__), GNUNET_i2s (&n->id));
3112 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3113 if (0 != ntohl (quota.value__))
3115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
3116 GNUNET_i2s (&n->id), "SET_QUOTA");
3117 if (GNUNET_YES == test_connected (n))
3118 GNUNET_STATISTICS_update (GST_stats,
3119 gettext_noop ("# disconnects due to quota of 0"),
3121 disconnect_neighbour (n);
3126 * We received a disconnect message from the given peer,
3127 * validate and process.
3129 * @param peer sender of the message
3130 * @param msg the disconnect message
3133 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
3135 const struct GNUNET_MessageHeader
3138 struct NeighbourMapEntry *n;
3139 const struct SessionDisconnectMessage *sdm;
3140 struct GNUNET_HashCode hc;
3142 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3143 "Received DISCONNECT message from peer `%s'\n",
3145 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3147 // GNUNET_break_op (0);
3148 GNUNET_STATISTICS_update (GST_stats,
3150 ("# disconnect messages ignored (old format)"), 1,
3154 sdm = (const struct SessionDisconnectMessage *) msg;
3155 if (NULL == (n = lookup_neighbour (peer)))
3156 return; /* gone already */
3157 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <= n->connect_ack_timestamp.abs_value)
3159 GNUNET_STATISTICS_update (GST_stats,
3161 ("# disconnect messages ignored (timestamp)"), 1,
3165 GNUNET_CRYPTO_hash (&sdm->public_key,
3166 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
3168 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
3170 GNUNET_break_op (0);
3173 if (ntohl (sdm->purpose.size) !=
3174 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3175 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded) +
3176 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3178 GNUNET_break_op (0);
3182 GNUNET_CRYPTO_ecc_verify
3183 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3184 &sdm->signature, &sdm->public_key))
3186 GNUNET_break_op (0);
3189 if (GNUNET_YES == test_connected (n))
3190 GNUNET_STATISTICS_update (GST_stats,
3192 ("# other peer asked to disconnect from us"), 1,
3194 disconnect_neighbour (n);
3199 * Closure for the neighbours_iterate function.
3201 struct IteratorContext
3204 * Function to call on each connected neighbour.
3206 GST_NeighbourIterator cb;
3216 * Call the callback from the closure for each connected neighbour.
3218 * @param cls the 'struct IteratorContext'
3219 * @param key the hash of the public key of the neighbour
3220 * @param value the 'struct NeighbourMapEntry'
3221 * @return GNUNET_OK (continue to iterate)
3224 neighbours_iterate (void *cls, const struct GNUNET_HashCode * key, void *value)
3226 struct IteratorContext *ic = cls;
3227 struct NeighbourMapEntry *n = value;
3229 if (GNUNET_YES == test_connected (n))
3231 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3232 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3234 if (NULL != n->primary_address.address)
3236 bandwidth_in = n->primary_address.bandwidth_in;
3237 bandwidth_out = n->primary_address.bandwidth_out;
3241 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3242 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3245 ic->cb (ic->cb_cls, &n->id,
3246 n->primary_address.address,
3247 bandwidth_in, bandwidth_out);
3254 * Iterate over all connected neighbours.
3256 * @param cb function to call
3257 * @param cb_cls closure for cb
3260 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3262 struct IteratorContext ic;
3264 if (NULL == neighbours)
3265 return; /* can happen during shutdown */
3268 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
3273 * If we have an active connection to the given target, it must be shutdown.
3275 * @param target peer to disconnect from
3278 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3280 struct NeighbourMapEntry *n;
3282 if (NULL == (n = lookup_neighbour (target)))
3283 return; /* not active */
3284 if (GNUNET_YES == test_connected (n))
3285 GNUNET_STATISTICS_update (GST_stats,
3287 ("# disconnected from peer upon explicit request"), 1,
3289 disconnect_neighbour (n);
3294 * Obtain current latency information for the given neighbour.
3296 * @param peer to get the latency for
3297 * @return observed latency of the address, FOREVER if the
3298 * the connection is not up
3300 struct GNUNET_TIME_Relative
3301 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3303 struct NeighbourMapEntry *n;
3305 n = lookup_neighbour (peer);
3307 return GNUNET_TIME_UNIT_FOREVER_REL;
3311 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3312 case S_CONNECTED_SWITCHING_BLACKLIST:
3313 case S_RECONNECT_SENT:
3314 case S_RECONNECT_ATS:
3315 case S_RECONNECT_BLACKLIST:
3317 case S_NOT_CONNECTED:
3318 case S_INIT_BLACKLIST:
3320 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3321 case S_CONNECT_RECV_ATS:
3322 case S_CONNECT_RECV_BLACKLIST:
3323 case S_CONNECT_RECV_ACK:
3324 case S_CONNECT_SENT:
3326 case S_DISCONNECT_FINISHED:
3327 return GNUNET_TIME_UNIT_FOREVER_REL;
3329 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3333 return GNUNET_TIME_UNIT_FOREVER_REL;
3338 * Obtain current address information for the given neighbour.
3341 * @return address currently used
3343 struct GNUNET_HELLO_Address *
3344 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3346 struct NeighbourMapEntry *n;
3348 n = lookup_neighbour (peer);
3351 return n->primary_address.address;
3356 * Initialize the neighbours subsystem.
3358 * @param cls closure for callbacks
3359 * @param connect_cb function to call if we connect to a peer
3360 * @param disconnect_cb function to call if we disconnect from a peer
3361 * @param peer_address_cb function to call if we change an active address
3363 * @param max_fds maximum number of fds to use
3366 GST_neighbours_start (void *cls,
3367 NotifyConnect connect_cb,
3368 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3369 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3370 unsigned int max_fds)
3373 connect_notify_cb = connect_cb;
3374 disconnect_notify_cb = disconnect_cb;
3375 address_change_cb = peer_address_cb;
3376 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3381 * Disconnect from the given neighbour.
3384 * @param key hash of neighbour's public key (not used)
3385 * @param value the 'struct NeighbourMapEntry' of the neighbour
3386 * @return GNUNET_OK (continue to iterate)
3389 disconnect_all_neighbours (void *cls, const struct GNUNET_HashCode * key, void *value)
3391 struct NeighbourMapEntry *n = value;
3393 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3394 "Disconnecting peer `%4s', %s\n",
3395 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3396 n->state = S_DISCONNECT_FINISHED;
3397 free_neighbour (n, GNUNET_NO);
3403 * Cleanup the neighbours subsystem.
3406 GST_neighbours_stop ()
3408 if (NULL == neighbours)
3410 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
3411 &disconnect_all_neighbours,
3413 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
3415 callback_cls = NULL;
3416 connect_notify_cb = NULL;
3417 disconnect_notify_cb = NULL;
3418 address_change_cb = NULL;
3422 /* end of file gnunet-service-transport_neighbours.c */