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 * Notify ATS about the new address including the network this address is
940 * @param address the address
941 * @param session the session*
944 add_address (struct GNUNET_HELLO_Address *address,
947 struct GNUNET_TRANSPORT_PluginFunctions *papi;
948 struct GNUNET_ATS_Information ats;
951 /* valid new address, let ATS know! */
952 if (NULL == address->transport_name)
957 if (NULL == (papi = GST_plugins_find (address->transport_name)))
959 /* we don't have the plugin for this address */
964 net = papi->get_network (NULL, session);
965 ats.type = htonl (GNUNET_ATS_NETWORK_TYPE);
966 ats.value = htonl(net);
968 // fprintf (stderr, "NET: %u\n", ntohl(net));
969 GNUNET_ATS_address_add (GST_ats,
970 address, session, &ats, 1);
975 * Initialize the 'struct NeighbourAddress'.
977 * @param na neighbour address to initialize
978 * @param address address of the other peer, NULL if other peer
980 * @param session session to use (or NULL, in which case an
981 * address must be setup)
982 * @param bandwidth_in inbound quota to be used when connection is up
983 * @param bandwidth_out outbound quota to be used when connection is up
984 * @param is_active GNUNET_YES to mark this as the active address with ATS
987 set_address (struct NeighbourAddress *na,
988 const struct GNUNET_HELLO_Address *address,
989 struct Session *session,
990 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in,
991 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out,
994 struct GNUNET_TRANSPORT_PluginFunctions *papi;
995 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1000 if (session == na->session)
1002 na->bandwidth_in = bandwidth_in;
1003 na->bandwidth_out = bandwidth_out;
1004 if (is_active != na->ats_active)
1006 na->ats_active = is_active;
1007 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, is_active);
1008 GST_validation_set_address_use (na->address, na->session, is_active, __LINE__);
1010 address_change_cb (NULL, &address->peer, address);
1012 if (GNUNET_YES == is_active)
1014 /* FIXME: is this the right place to set quotas? */
1015 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
1016 send_outbound_quota (&address->peer, bandwidth_out);
1021 if (NULL == session)
1022 session = papi->get_session (papi->cls, address);
1023 if (NULL == session)
1025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1026 "Failed to obtain new session for peer `%s' and address '%s'\n",
1027 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
1028 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1031 na->address = GNUNET_HELLO_address_copy (address);
1032 MEMDEBUG_add_alloc (na->address, GNUNET_HELLO_address_get_size (na->address), __LINE__);
1033 na->bandwidth_in = bandwidth_in;
1034 na->bandwidth_out = bandwidth_out;
1035 na->session = session;
1036 na->ats_active = is_active;
1037 if (GNUNET_YES == is_active)
1039 /* Telling ATS about new session */
1040 GNUNET_ATS_address_in_use (GST_ats, na->address, na->session, GNUNET_YES);
1041 GST_validation_set_address_use (na->address, na->session, GNUNET_YES, __LINE__);
1042 address_change_cb (NULL, &address->peer, address);
1043 /* FIXME: is this the right place to set quotas? */
1044 GST_neighbours_set_incoming_quota (&address->peer, bandwidth_in);
1045 send_outbound_quota (&address->peer, bandwidth_out);
1051 * Free a neighbour map entry.
1053 * @param n entry to free
1054 * @param keep_sessions GNUNET_NO to tell plugin to terminate sessions,
1055 * GNUNET_YES to keep all sessions
1058 free_neighbour (struct NeighbourMapEntry *n, int keep_sessions)
1060 struct MessageQueue *mq;
1061 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1062 struct GNUNET_HELLO_Address *backup_primary;
1064 n->is_active = NULL; /* always free'd by its own continuation! */
1066 /* fail messages currently in the queue */
1067 while (NULL != (mq = n->messages_head))
1069 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1070 if (NULL != mq->cont)
1071 mq->cont (mq->cont_cls, GNUNET_SYSERR, mq->message_buf_size, 0);
1072 MEMDEBUG_free (mq, __LINE__);
1074 /* It is too late to send other peer disconnect notifications, but at
1075 least internally we need to get clean... */
1076 if (GNUNET_YES == test_connected (n))
1078 GNUNET_STATISTICS_set (GST_stats,
1079 gettext_noop ("# peers connected"),
1080 --neighbours_connected,
1082 disconnect_notify_cb (callback_cls, &n->id);
1085 n->state = S_DISCONNECT_FINISHED;
1087 if (NULL != n->primary_address.address)
1089 backup_primary = GNUNET_HELLO_address_copy(n->primary_address.address);
1090 MEMDEBUG_add_alloc (backup_primary, GNUNET_HELLO_address_get_size(backup_primary), __LINE__);
1093 backup_primary = NULL;
1095 /* free addresses and mark as unused */
1096 free_address (&n->primary_address);
1097 free_address (&n->alternative_address);
1099 /* FIXME-PLUGIN-API: This does not seem to guarantee that all
1100 transport sessions eventually get killed due to inactivity; they
1101 MUST have their own timeout logic (but at least TCP doesn't have
1102 one yet). Are we sure that EVERY 'session' of a plugin is
1103 actually cleaned up this way!? Note that if we are switching
1104 between two TCP sessions to the same peer, the existing plugin
1105 API gives us not even the means to selectively kill only one of
1106 them! Killing all sessions like this seems to be very, very
1109 /* cut transport-level connection */
1110 if ((GNUNET_NO == keep_sessions) &&
1111 (NULL != backup_primary) &&
1112 (NULL != (papi = GST_plugins_find (backup_primary->transport_name))))
1113 papi->disconnect (papi->cls, &n->id);
1115 MEMDEBUG_free_non_null (backup_primary, __LINE__);
1117 GNUNET_assert (GNUNET_YES ==
1118 GNUNET_CONTAINER_multihashmap_remove (neighbours,
1119 &n->id.hashPubKey, n));
1121 // FIXME-ATS-API: we might want to be more specific about
1122 // which states we do this from in the future (ATS should
1123 // have given us a 'suggest_address' handle, and if we have
1124 // such a handle, we should cancel the operation here!
1125 if (NULL != n->suggest_handle)
1127 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
1128 n->suggest_handle = NULL;
1131 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1133 GNUNET_SCHEDULER_cancel (n->task);
1134 n->task = GNUNET_SCHEDULER_NO_TASK;
1136 /* free rest of memory */
1137 MEMDEBUG_free (n, __LINE__);
1141 * Transmit a message using the current session of the given
1144 * @param n entry for the recipient
1145 * @param msgbuf buffer to transmit
1146 * @param msgbuf_size number of bytes in buffer
1147 * @param priority transmission priority
1148 * @param timeout transmission timeout
1149 * @param cont continuation to call when finished (can be NULL)
1150 * @param cont_cls closure for cont
1153 send_with_session (struct NeighbourMapEntry *n,
1154 const char *msgbuf, size_t msgbuf_size,
1156 struct GNUNET_TIME_Relative timeout,
1157 GNUNET_TRANSPORT_TransmitContinuation cont,
1160 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1162 GNUNET_assert (n->primary_address.session != NULL);
1163 if ( ((NULL == (papi = GST_plugins_find (n->primary_address.address->transport_name)) ||
1164 (-1 == papi->send (papi->cls,
1165 n->primary_address.session,
1166 msgbuf, msgbuf_size,
1169 cont, cont_cls)))) &&
1171 cont (cont_cls, &n->id, GNUNET_SYSERR, msgbuf_size, 0);
1172 GNUNET_break (NULL != papi);
1177 * Master task run for every neighbour. Performs all of the time-related
1178 * activities (keep alive, send next message, disconnect if idle, finish
1179 * clean up after disconnect).
1181 * @param cls the 'struct NeighbourMapEntry' for which we are running
1182 * @param tc scheduler context (unused)
1185 master_task (void *cls,
1186 const struct GNUNET_SCHEDULER_TaskContext *tc);
1190 * Function called when the 'DISCONNECT' message has been sent by the
1191 * plugin. Frees the neighbour --- if the entry still exists.
1194 * @param target identity of the neighbour that was disconnected
1195 * @param result GNUNET_OK if the disconnect got out successfully
1196 * @param payload bytes payload
1197 * @param physical bytes physical
1200 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
1201 int result, size_t payload, size_t physical)
1203 struct NeighbourMapEntry *n;
1205 n = lookup_neighbour (target);
1207 return; /* already gone */
1208 if (S_DISCONNECT != n->state)
1209 return; /* have created a fresh entry since */
1210 n->state = S_DISCONNECT;
1211 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1212 GNUNET_SCHEDULER_cancel (n->task);
1213 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1218 * Transmit a DISCONNECT message to the other peer.
1220 * @param n neighbour to send DISCONNECT message.
1223 send_disconnect (struct NeighbourMapEntry *n)
1225 struct SessionDisconnectMessage disconnect_msg;
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Sending DISCONNECT message to peer `%4s'\n",
1229 GNUNET_i2s (&n->id));
1230 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1231 disconnect_msg.header.type =
1232 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1233 disconnect_msg.reserved = htonl (0);
1234 disconnect_msg.purpose.size =
1235 htonl (sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
1236 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded) +
1237 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1238 disconnect_msg.purpose.purpose =
1239 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1240 disconnect_msg.timestamp =
1241 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1242 disconnect_msg.public_key = GST_my_public_key;
1243 GNUNET_assert (GNUNET_OK ==
1244 GNUNET_CRYPTO_ecc_sign (GST_my_private_key,
1245 &disconnect_msg.purpose,
1246 &disconnect_msg.signature));
1248 send_with_session (n,
1249 (const char *) &disconnect_msg, sizeof (disconnect_msg),
1250 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1251 &send_disconnect_cont, NULL);
1252 GNUNET_STATISTICS_update (GST_stats,
1254 ("# DISCONNECT messages sent"), 1,
1260 * Disconnect from the given neighbour, clean up the record.
1262 * @param n neighbour to disconnect from
1265 disconnect_neighbour (struct NeighbourMapEntry *n)
1267 /* depending on state, notify neighbour and/or upper layers of this peer
1271 case S_NOT_CONNECTED:
1273 case S_INIT_BLACKLIST:
1274 /* other peer is completely unaware of us, no need to send DISCONNECT */
1275 n->state = S_DISCONNECT_FINISHED;
1276 free_neighbour (n, GNUNET_NO);
1278 case S_CONNECT_SENT:
1279 send_disconnect (n);
1280 n->state = S_DISCONNECT;
1282 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1283 case S_CONNECT_RECV_ATS:
1284 case S_CONNECT_RECV_BLACKLIST:
1285 /* we never ACK'ed the other peer's request, no need to send DISCONNECT */
1286 n->state = S_DISCONNECT_FINISHED;
1287 free_neighbour (n, GNUNET_NO);
1289 case S_CONNECT_RECV_ACK:
1290 /* we DID ACK the other peer's request, must send DISCONNECT */
1291 send_disconnect (n);
1292 n->state = S_DISCONNECT;
1295 case S_RECONNECT_BLACKLIST:
1296 case S_RECONNECT_SENT:
1297 case S_CONNECTED_SWITCHING_BLACKLIST:
1298 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1299 /* we are currently connected, need to send disconnect and do
1300 internal notifications and update statistics */
1301 send_disconnect (n);
1302 GNUNET_STATISTICS_set (GST_stats,
1303 gettext_noop ("# peers connected"),
1304 --neighbours_connected,
1306 disconnect_notify_cb (callback_cls, &n->id);
1307 n->state = S_DISCONNECT;
1309 case S_RECONNECT_ATS:
1310 /* ATS address request timeout, disconnect without sending disconnect message */
1311 GNUNET_STATISTICS_set (GST_stats,
1312 gettext_noop ("# peers connected"),
1313 --neighbours_connected,
1315 disconnect_notify_cb (callback_cls, &n->id);
1316 n->state = S_DISCONNECT;
1319 /* already disconnected, ignore */
1321 case S_DISCONNECT_FINISHED:
1322 /* already cleaned up, how did we get here!? */
1326 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1330 /* schedule timeout to clean up */
1331 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1332 GNUNET_SCHEDULER_cancel (n->task);
1333 n->task = GNUNET_SCHEDULER_add_delayed (DISCONNECT_SENT_TIMEOUT,
1339 * We're done with our transmission attempt, continue processing.
1341 * @param cls the 'struct MessageQueue' of the message
1342 * @param receiver intended receiver
1343 * @param success whether it worked or not
1344 * @param size_payload bytes payload sent
1345 * @param physical bytes sent on wire
1348 transmit_send_continuation (void *cls,
1349 const struct GNUNET_PeerIdentity *receiver,
1350 int success, size_t size_payload, size_t physical)
1352 struct MessageQueue *mq = cls;
1353 struct NeighbourMapEntry *n;
1355 if (NULL == (n = lookup_neighbour (receiver)))
1357 MEMDEBUG_free (mq, __LINE__);
1358 return; /* disconnect or other error while transmitting, can happen */
1360 if (n->is_active == mq)
1362 /* this is still "our" neighbour, remove us from its queue
1363 and allow it to send the next message now */
1364 n->is_active = NULL;
1365 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1366 GNUNET_SCHEDULER_cancel (n->task);
1367 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1369 if (bytes_in_send_queue < mq->message_buf_size)
1371 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1372 "Bytes_in_send_queue `%u', Message_size %u, result: %s, payload %u, on wire %u\n",
1373 bytes_in_send_queue, mq->message_buf_size,
1374 (GNUNET_OK == success) ? "OK" : "FAIL",
1375 size_payload, physical);
1380 GNUNET_break (size_payload == mq->message_buf_size);
1381 bytes_in_send_queue -= mq->message_buf_size;
1382 GNUNET_STATISTICS_set (GST_stats,
1384 ("# bytes in message queue for other peers"),
1385 bytes_in_send_queue, GNUNET_NO);
1386 if (GNUNET_OK == success)
1387 GNUNET_STATISTICS_update (GST_stats,
1389 ("# messages transmitted to other peers"),
1392 GNUNET_STATISTICS_update (GST_stats,
1394 ("# transmission failures for messages to other peers"),
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Sending message to `%s' of type %u was a %s\n",
1398 GNUNET_i2s (receiver),
1399 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
1400 (success == GNUNET_OK) ? "success" : "FAILURE");
1401 if (NULL != mq->cont)
1402 mq->cont (mq->cont_cls, success, size_payload, physical);
1403 MEMDEBUG_free (mq, __LINE__);
1408 * Check the message list for the given neighbour and if we can
1409 * send a message, do so. This function should only be called
1410 * if the connection is at least generally ready for transmission.
1411 * While we will only send one message at a time, no bandwidth
1412 * quota management is performed here. If a message was given to
1413 * the plugin, the continuation will automatically re-schedule
1414 * the 'master' task once the next message might be transmitted.
1416 * @param n target peer for which to transmit
1419 try_transmission_to_peer (struct NeighbourMapEntry *n)
1421 struct MessageQueue *mq;
1422 struct GNUNET_TIME_Relative timeout;
1424 if (NULL == n->primary_address.address)
1426 /* no address, why are we here? */
1430 if ((0 == n->primary_address.address->address_length) &&
1431 (NULL == n->primary_address.session))
1433 /* no address, why are we here? */
1437 if (NULL != n->is_active)
1439 /* transmission already pending */
1443 /* timeout messages from the queue that are past their due date */
1444 while (NULL != (mq = n->messages_head))
1446 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
1447 if (timeout.rel_value > 0)
1449 GNUNET_STATISTICS_update (GST_stats,
1451 ("# messages timed out while in transport queue"),
1453 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1455 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR, mq->message_buf_size, 0); /* timeout */
1458 return; /* no more messages */
1459 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
1461 send_with_session (n,
1462 mq->message_buf, mq->message_buf_size,
1463 0 /* priority */, timeout,
1464 &transmit_send_continuation, mq);
1469 * Send keepalive message to the neighbour. Must only be called
1470 * if we are on 'connected' state or while trying to switch addresses.
1471 * Will internally determine if a keepalive is truly needed (so can
1472 * always be called).
1474 * @param n neighbour that went idle and needs a keepalive
1477 send_keepalive (struct NeighbourMapEntry *n)
1479 struct GNUNET_MessageHeader m;
1481 GNUNET_assert ((S_CONNECTED == n->state) ||
1482 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
1483 (S_CONNECTED_SWITCHING_CONNECT_SENT));
1484 if (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time).rel_value > 0)
1485 return; /* no keepalive needed at this time */
1486 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1487 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1488 send_with_session (n,
1489 (const void *) &m, sizeof (m),
1490 UINT32_MAX /* priority */,
1491 KEEPALIVE_FREQUENCY,
1493 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1495 n->expect_latency_response = GNUNET_YES;
1496 n->last_keep_alive_time = GNUNET_TIME_absolute_get ();
1497 n->keep_alive_time = GNUNET_TIME_relative_to_absolute (KEEPALIVE_FREQUENCY);
1502 * Keep the connection to the given neighbour alive longer,
1503 * we received a KEEPALIVE (or equivalent); send a response.
1505 * @param neighbour neighbour to keep alive (by sending keep alive response)
1508 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1510 struct NeighbourMapEntry *n;
1511 struct GNUNET_MessageHeader m;
1513 if (NULL == (n = lookup_neighbour (neighbour)))
1515 GNUNET_STATISTICS_update (GST_stats,
1517 ("# KEEPALIVE messages discarded (peer unknown)"),
1521 if (NULL == n->primary_address.session)
1523 GNUNET_STATISTICS_update (GST_stats,
1525 ("# KEEPALIVE messages discarded (no session)"),
1529 /* send reply to allow neighbour to measure latency */
1530 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1531 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1532 send_with_session(n,
1533 (const void *) &m, sizeof (m),
1534 UINT32_MAX /* priority */,
1535 KEEPALIVE_FREQUENCY,
1541 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate
1542 * latency to this peer. Pass the updated information (existing ats
1543 * plus calculated latency) to ATS.
1545 * @param neighbour neighbour to keep alive
1548 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour)
1550 struct NeighbourMapEntry *n;
1552 struct GNUNET_ATS_Information ats;
1554 if (NULL == (n = lookup_neighbour (neighbour)))
1556 GNUNET_STATISTICS_update (GST_stats,
1558 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1562 if ( (S_CONNECTED != n->state) ||
1563 (GNUNET_YES != n->expect_latency_response) )
1565 GNUNET_STATISTICS_update (GST_stats,
1567 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1571 n->expect_latency_response = GNUNET_NO;
1572 n->latency = GNUNET_TIME_absolute_get_duration (n->last_keep_alive_time);
1573 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
1574 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1575 "Latency for peer `%s' is %llu ms\n",
1576 GNUNET_i2s (&n->id), n->latency.rel_value);
1577 /* append latency */
1578 ats.type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
1579 if (n->latency.rel_value > UINT32_MAX)
1580 latency = UINT32_MAX;
1582 latency = n->latency.rel_value;
1583 ats.value = htonl (latency);
1584 GST_update_ats_metrics (&n->id,
1585 n->primary_address.address,
1586 n->primary_address.session,
1592 * We have received a message from the given sender. How long should
1593 * we delay before receiving more? (Also used to keep the peer marked
1596 * @param sender sender of the message
1597 * @param size size of the message
1598 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1599 * GNUNET_NO if the neighbour is not connected or violates the quota,
1600 * GNUNET_SYSERR if the connection is not fully up yet
1601 * @return how long to wait before reading more from this sender
1603 struct GNUNET_TIME_Relative
1604 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1605 *sender, ssize_t size, int *do_forward)
1607 struct NeighbourMapEntry *n;
1608 struct GNUNET_TIME_Relative ret;
1610 if (NULL == neighbours)
1612 *do_forward = GNUNET_NO;
1613 return GNUNET_TIME_UNIT_FOREVER_REL; /* This can happen during shutdown */
1615 if (NULL == (n = lookup_neighbour (sender)))
1617 GST_neighbours_try_connect (sender);
1618 if (NULL == (n = lookup_neighbour (sender)))
1620 GNUNET_STATISTICS_update (GST_stats,
1622 ("# messages discarded due to lack of neighbour record"),
1624 *do_forward = GNUNET_NO;
1625 return GNUNET_TIME_UNIT_ZERO;
1628 if (! test_connected (n))
1630 *do_forward = GNUNET_SYSERR;
1631 return GNUNET_TIME_UNIT_ZERO;
1633 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1635 n->quota_violation_count++;
1636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1637 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1638 n->in_tracker.available_bytes_per_s__,
1639 n->quota_violation_count);
1640 /* Discount 32k per violation */
1641 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1645 if (n->quota_violation_count > 0)
1647 /* try to add 32k back */
1648 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1649 n->quota_violation_count--;
1652 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1654 GNUNET_STATISTICS_update (GST_stats,
1656 ("# bandwidth quota violations by other peers"),
1658 *do_forward = GNUNET_NO;
1659 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1661 *do_forward = GNUNET_YES;
1662 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1663 if (ret.rel_value > 0)
1665 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1666 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1667 (unsigned long long) n->in_tracker.
1668 consumption_since_last_update__,
1669 (unsigned int) n->in_tracker.available_bytes_per_s__,
1670 (unsigned long long) ret.rel_value);
1671 GNUNET_STATISTICS_update (GST_stats,
1672 gettext_noop ("# ms throttling suggested"),
1673 (int64_t) ret.rel_value, GNUNET_NO);
1680 * Transmit a message to the given target using the active connection.
1682 * @param target destination
1683 * @param msg message to send
1684 * @param msg_size number of bytes in msg
1685 * @param timeout when to fail with timeout
1686 * @param cont function to call when done
1687 * @param cont_cls closure for 'cont'
1690 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1691 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1692 GST_NeighbourSendContinuation cont, void *cont_cls)
1694 struct NeighbourMapEntry *n;
1695 struct MessageQueue *mq;
1697 /* All ove these cases should never happen; they are all API violations.
1698 But we check anyway, just to be sure. */
1699 if (NULL == (n = lookup_neighbour (target)))
1703 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1706 if (GNUNET_YES != test_connected (n))
1710 cont (cont_cls, GNUNET_SYSERR, msg_size, 0);
1713 bytes_in_send_queue += msg_size;
1714 GNUNET_STATISTICS_set (GST_stats,
1716 ("# bytes in message queue for other peers"),
1717 bytes_in_send_queue, GNUNET_NO);
1718 mq = MEMDEBUG_malloc (sizeof (struct MessageQueue) + msg_size, __LINE__);
1720 mq->cont_cls = cont_cls;
1721 memcpy (&mq[1], msg, msg_size);
1722 mq->message_buf = (const char *) &mq[1];
1723 mq->message_buf_size = msg_size;
1724 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1725 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1726 if ( (NULL != n->is_active) ||
1727 ( (NULL == n->primary_address.session) && (NULL == n->primary_address.address)) )
1729 if (GNUNET_SCHEDULER_NO_TASK != n->task)
1730 GNUNET_SCHEDULER_cancel (n->task);
1731 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1736 * Send a SESSION_CONNECT message via the given address.
1738 * @param na address to use
1741 send_session_connect (struct NeighbourAddress *na)
1743 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1744 struct SessionConnectMessage connect_msg;
1746 if (NULL == (papi = GST_plugins_find (na->address->transport_name)))
1751 if (NULL == na->session)
1752 na->session = papi->get_session (papi->cls, na->address);
1753 if (NULL == na->session)
1758 na->connect_timestamp = GNUNET_TIME_absolute_get ();
1759 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1760 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1761 connect_msg.reserved = htonl (0);
1762 connect_msg.timestamp = GNUNET_TIME_absolute_hton (na->connect_timestamp);
1763 (void) papi->send (papi->cls,
1765 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1767 GNUNET_TIME_UNIT_FOREVER_REL,
1774 * Send a SESSION_CONNECT_ACK message via the given address.
1776 * @param address address to use
1777 * @param session session to use
1778 * @param timestamp timestamp to use for the ACK message
1781 send_session_connect_ack_message (const struct GNUNET_HELLO_Address *address,
1782 struct Session *session,
1783 struct GNUNET_TIME_Absolute timestamp)
1785 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1786 struct SessionConnectMessage connect_msg;
1788 if (NULL == (papi = GST_plugins_find (address->transport_name)))
1793 if (NULL == session)
1794 session = papi->get_session (papi->cls, address);
1795 if (NULL == session)
1800 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
1801 connect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1802 connect_msg.reserved = htonl (0);
1803 connect_msg.timestamp = GNUNET_TIME_absolute_hton (timestamp);
1804 (void) papi->send (papi->cls,
1806 (const char *) &connect_msg, sizeof (struct SessionConnectMessage),
1808 GNUNET_TIME_UNIT_FOREVER_REL,
1815 * Create a fresh entry in the neighbour map for the given peer
1817 * @param peer peer to create an entry for
1818 * @return new neighbour map entry
1820 static struct NeighbourMapEntry *
1821 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1823 struct NeighbourMapEntry *n;
1825 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1826 "Creating new neighbour entry for `%s'\n",
1828 n = MEMDEBUG_malloc (sizeof (struct NeighbourMapEntry), __LINE__);
1830 n->state = S_NOT_CONNECTED;
1831 n->latency = GNUNET_TIME_UNIT_FOREVER_REL;
1832 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1833 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1834 MAX_BANDWIDTH_CARRY_S);
1835 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
1836 GNUNET_assert (GNUNET_OK ==
1837 GNUNET_CONTAINER_multihashmap_put (neighbours,
1838 &n->id.hashPubKey, n,
1839 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1845 * Check if the two given addresses are the same.
1846 * Actually only checks if the sessions are non-NULL
1847 * (which they should be) and then if they are identical;
1848 * the actual addresses don't matter if the session
1849 * pointers match anyway, and we must have session pointers
1852 * @param a1 first address to compare
1853 * @param a2 other address to compare
1854 * @return GNUNET_NO if the addresses do not match, GNUNET_YES if they do match
1857 address_matches (const struct NeighbourAddress *a1,
1858 const struct NeighbourAddress *a2)
1860 if ( (NULL == a1->session) ||
1861 (NULL == a2->session) )
1866 return (a1->session == a2->session) ? GNUNET_YES : GNUNET_NO;
1871 * Try to create a connection to the given target (eventually).
1873 * @param target peer to try to connect to
1876 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1878 struct NeighbourMapEntry *n;
1880 if (NULL == neighbours)
1882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1883 "Asked to connect to peer `%s' during shutdown\n",
1884 GNUNET_i2s (target));
1885 return; /* during shutdown, do nothing */
1887 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1888 "Asked to connect to peer `%s'\n",
1889 GNUNET_i2s (target));
1890 if (0 == memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1892 /* refuse to connect to myself */
1893 /* FIXME: can this happen? Is this not an API violation? */
1894 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1895 "Refusing to try to connect to myself.\n");
1898 n = lookup_neighbour (target);
1903 case S_NOT_CONNECTED:
1904 /* this should not be possible */
1906 free_neighbour (n, GNUNET_NO);
1909 case S_INIT_BLACKLIST:
1910 case S_CONNECT_SENT:
1911 case S_CONNECT_RECV_BLACKLIST_INBOUND:
1912 case S_CONNECT_RECV_ATS:
1913 case S_CONNECT_RECV_BLACKLIST:
1914 case S_CONNECT_RECV_ACK:
1915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1916 "Ignoring request to try to connect to `%s', already trying!\n",
1917 GNUNET_i2s (target));
1918 return; /* already trying */
1920 case S_RECONNECT_ATS:
1921 case S_RECONNECT_BLACKLIST:
1922 case S_RECONNECT_SENT:
1923 case S_CONNECTED_SWITCHING_BLACKLIST:
1924 case S_CONNECTED_SWITCHING_CONNECT_SENT:
1925 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1926 "Ignoring request to try to connect, already connected to `%s'!\n",
1927 GNUNET_i2s (target));
1928 return; /* already connected */
1930 /* get rid of remains, ready to re-try immediately */
1931 free_neighbour (n, GNUNET_NO);
1933 case S_DISCONNECT_FINISHED:
1934 /* should not be possible */
1937 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
1939 free_neighbour (n, GNUNET_NO);
1943 n = setup_neighbour (target);
1944 n->state = S_INIT_ATS;
1945 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
1947 GNUNET_ATS_reset_backoff (GST_ats, target);
1948 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, target);
1953 * Function called with the result of a blacklist check.
1955 * @param cls closure with the 'struct BlackListCheckContext'
1956 * @param peer peer this check affects
1957 * @param result GNUNET_OK if the address is allowed
1960 handle_test_blacklist_cont (void *cls,
1961 const struct GNUNET_PeerIdentity *peer,
1964 struct BlackListCheckContext *bcc = cls;
1965 struct NeighbourMapEntry *n;
1968 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1969 "Connection to new address of peer `%s' based on blacklist is `%s'\n",
1971 (GNUNET_OK == result) ? "allowed" : "FORBIDDEN");
1972 if (NULL == (n = lookup_neighbour (peer)))
1973 goto cleanup; /* nobody left to care about new address */
1976 case S_NOT_CONNECTED:
1977 /* this should not be possible */
1979 free_neighbour (n, GNUNET_NO);
1982 /* still waiting on ATS suggestion */
1984 case S_INIT_BLACKLIST:
1985 /* check if the address the blacklist was fine with matches
1986 ATS suggestion, if so, we can move on! */
1987 if ( (GNUNET_OK == result) &&
1988 (1 == n->send_connect_ack) )
1990 n->send_connect_ack = 2;
1991 send_session_connect_ack_message (bcc->na.address,
1993 n->connect_ack_timestamp);
1995 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
1996 break; /* result for an address we currently don't care about */
1997 if (GNUNET_OK == result)
1999 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
2000 n->state = S_CONNECT_SENT;
2001 send_session_connect (&n->primary_address);
2005 // FIXME: should also possibly destroy session with plugin!?
2006 GNUNET_ATS_address_destroyed (GST_ats,
2009 free_address (&n->primary_address);
2010 n->state = S_INIT_ATS;
2011 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2012 // FIXME: do we need to ask ATS again for suggestions?
2013 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2016 case S_CONNECT_SENT:
2017 /* waiting on CONNECT_ACK, send ACK if one is pending */
2018 if ( (GNUNET_OK == result) &&
2019 (1 == n->send_connect_ack) )
2021 n->send_connect_ack = 2;
2022 send_session_connect_ack_message (n->primary_address.address,
2023 n->primary_address.session,
2024 n->connect_ack_timestamp);
2027 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2028 if (GNUNET_OK == result)
2029 add_address (bcc->na.address, bcc->na.session);
2031 n->state = S_CONNECT_RECV_ATS;
2032 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2033 GNUNET_ATS_reset_backoff (GST_ats, peer);
2034 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2036 case S_CONNECT_RECV_ATS:
2037 /* still waiting on ATS suggestion, don't care about blacklist */
2039 case S_CONNECT_RECV_BLACKLIST:
2040 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2041 break; /* result for an address we currently don't care about */
2042 if (GNUNET_OK == result)
2044 n->timeout = GNUNET_TIME_relative_to_absolute (SETUP_CONNECTION_TIMEOUT);
2045 n->state = S_CONNECT_RECV_ACK;
2046 send_session_connect_ack_message (bcc->na.address,
2048 n->connect_ack_timestamp);
2049 if (1 == n->send_connect_ack)
2050 n->send_connect_ack = 2;
2054 // FIXME: should also possibly destroy session with plugin!?
2055 GNUNET_ATS_address_destroyed (GST_ats,
2058 free_address (&n->primary_address);
2059 n->state = S_INIT_ATS;
2060 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2061 // FIXME: do we need to ask ATS again for suggestions?
2062 GNUNET_ATS_reset_backoff (GST_ats, peer);
2063 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2066 case S_CONNECT_RECV_ACK:
2067 /* waiting on SESSION_ACK, send ACK if one is pending */
2068 if ( (GNUNET_OK == result) &&
2069 (1 == n->send_connect_ack) )
2071 n->send_connect_ack = 2;
2072 send_session_connect_ack_message (n->primary_address.address,
2073 n->primary_address.session,
2074 n->connect_ack_timestamp);
2078 /* already connected, don't care about blacklist */
2080 case S_RECONNECT_ATS:
2081 /* still waiting on ATS suggestion, don't care about blacklist */
2083 case S_RECONNECT_BLACKLIST:
2084 if ( (GNUNET_OK == result) &&
2085 (1 == n->send_connect_ack) )
2087 n->send_connect_ack = 2;
2088 send_session_connect_ack_message (bcc->na.address,
2090 n->connect_ack_timestamp);
2092 if (GNUNET_YES != address_matches (&bcc->na, &n->primary_address))
2093 break; /* result for an address we currently don't care about */
2094 if (GNUNET_OK == result)
2096 send_session_connect (&n->primary_address);
2097 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
2098 n->state = S_RECONNECT_SENT;
2102 GNUNET_ATS_address_destroyed (GST_ats,
2105 n->state = S_RECONNECT_ATS;
2106 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2107 // FIXME: do we need to ask ATS again for suggestions?
2108 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2111 case S_RECONNECT_SENT:
2112 /* waiting on CONNECT_ACK, don't care about blacklist */
2113 if ( (GNUNET_OK == result) &&
2114 (1 == n->send_connect_ack) )
2116 n->send_connect_ack = 2;
2117 send_session_connect_ack_message (n->primary_address.address,
2118 n->primary_address.session,
2119 n->connect_ack_timestamp);
2122 case S_CONNECTED_SWITCHING_BLACKLIST:
2123 if (GNUNET_YES != address_matches (&bcc->na, &n->alternative_address))
2124 break; /* result for an address we currently don't care about */
2125 if (GNUNET_OK == result)
2127 send_session_connect (&n->alternative_address);
2128 n->state = S_CONNECTED_SWITCHING_CONNECT_SENT;
2132 GNUNET_ATS_address_destroyed (GST_ats,
2135 free_address (&n->alternative_address);
2136 n->state = S_CONNECTED;
2139 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2140 /* waiting on CONNECT_ACK, don't care about blacklist */
2141 if ( (GNUNET_OK == result) &&
2142 (1 == n->send_connect_ack) )
2144 n->send_connect_ack = 2;
2145 send_session_connect_ack_message (n->primary_address.address,
2146 n->primary_address.session,
2147 n->connect_ack_timestamp);
2151 /* Nothing to do here, ATS will already do what can be done */
2153 case S_DISCONNECT_FINISHED:
2154 /* should not be possible */
2158 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2160 free_neighbour (n, GNUNET_NO);
2164 MEMDEBUG_free (bcc->na.address, __LINE__);
2165 //GNUNET_HELLO_address_free (bcc->na.address);
2166 GNUNET_CONTAINER_DLL_remove (bc_head,
2169 MEMDEBUG_free (bcc, __LINE__);
2174 * We want to know if connecting to a particular peer via
2175 * a particular address is allowed. Check it!
2177 * @param peer identity of the peer to switch the address for
2178 * @param ts time at which the check was initiated
2179 * @param address address of the other peer, NULL if other peer
2181 * @param session session to use (or NULL)
2184 check_blacklist (const struct GNUNET_PeerIdentity *peer,
2185 struct GNUNET_TIME_Absolute ts,
2186 const struct GNUNET_HELLO_Address *address,
2187 struct Session *session)
2189 struct BlackListCheckContext *bcc;
2190 struct GST_BlacklistCheck *bc;
2193 MEMDEBUG_malloc (sizeof (struct BlackListCheckContext), __LINE__);
2194 bcc->na.address = GNUNET_HELLO_address_copy (address);
2195 MEMDEBUG_add_alloc (bcc->na.address, GNUNET_HELLO_address_get_size (address), __LINE__);
2196 bcc->na.session = session;
2197 bcc->na.connect_timestamp = ts;
2198 GNUNET_CONTAINER_DLL_insert (bc_head,
2201 if (NULL != (bc = GST_blacklist_test_allowed (peer,
2202 address->transport_name,
2203 &handle_test_blacklist_cont, bcc)))
2205 /* if NULL == bc, 'cont' was already called and 'bcc' already free'd, so
2206 we must only store 'bc' if 'bc' is non-NULL... */
2211 * We received a 'SESSION_CONNECT' message from the other peer.
2212 * Consider switching to it.
2214 * @param message possibly a 'struct SessionConnectMessage' (check format)
2215 * @param peer identity of the peer to switch the address for
2216 * @param address address of the other peer, NULL if other peer
2218 * @param session session to use (or NULL)
2221 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2222 const struct GNUNET_PeerIdentity *peer,
2223 const struct GNUNET_HELLO_Address *address,
2224 struct Session *session)
2226 const struct SessionConnectMessage *scm;
2227 struct NeighbourMapEntry *n;
2228 struct GNUNET_TIME_Absolute ts;
2230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2231 "Received CONNECT message from peer `%s'\n",
2234 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2236 GNUNET_break_op (0);
2239 if (NULL == neighbours)
2240 return; /* we're shutting down */
2241 scm = (const struct SessionConnectMessage *) message;
2242 GNUNET_break_op (0 == ntohl (scm->reserved));
2243 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2244 n = lookup_neighbour (peer);
2246 n = setup_neighbour (peer);
2247 n->send_connect_ack = 1;
2248 n->connect_ack_timestamp = ts;
2252 case S_NOT_CONNECTED:
2253 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2254 /* Do a blacklist check for the new address */
2255 check_blacklist (peer, ts, address, session);
2258 /* CONNECT message takes priority over us asking ATS for address */
2259 n->state = S_CONNECT_RECV_BLACKLIST_INBOUND;
2261 case S_INIT_BLACKLIST:
2262 case S_CONNECT_SENT:
2263 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2264 case S_CONNECT_RECV_ATS:
2265 case S_CONNECT_RECV_BLACKLIST:
2266 case S_CONNECT_RECV_ACK:
2267 /* It can never hurt to have an alternative address in the above cases,
2268 see if it is allowed */
2269 check_blacklist (peer, ts, address, session);
2272 /* we are already connected and can thus send the ACK immediately;
2273 still, it can never hurt to have an alternative address, so also
2274 tell ATS about it */
2275 GNUNET_assert (NULL != n->primary_address.address);
2276 GNUNET_assert (NULL != n->primary_address.session);
2277 n->send_connect_ack = 0;
2278 send_session_connect_ack_message (n->primary_address.address,
2279 n->primary_address.session, ts);
2280 check_blacklist (peer, ts, address, session);
2282 case S_RECONNECT_ATS:
2283 case S_RECONNECT_BLACKLIST:
2284 case S_RECONNECT_SENT:
2285 /* It can never hurt to have an alternative address in the above cases,
2286 see if it is allowed */
2287 check_blacklist (peer, ts, address, session);
2289 case S_CONNECTED_SWITCHING_BLACKLIST:
2290 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2291 /* we are already connected and can thus send the ACK immediately;
2292 still, it can never hurt to have an alternative address, so also
2293 tell ATS about it */
2294 GNUNET_assert (NULL != n->primary_address.address);
2295 GNUNET_assert (NULL != n->primary_address.session);
2296 n->send_connect_ack = 0;
2297 send_session_connect_ack_message (n->primary_address.address,
2298 n->primary_address.session, ts);
2299 check_blacklist (peer, ts, address, session);
2302 /* get rid of remains without terminating sessions, ready to re-try */
2303 free_neighbour (n, GNUNET_YES);
2304 n = setup_neighbour (peer);
2305 n->state = S_CONNECT_RECV_ATS;
2306 GNUNET_ATS_reset_backoff (GST_ats, peer);
2307 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, peer);
2309 case S_DISCONNECT_FINISHED:
2310 /* should not be possible */
2314 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2316 free_neighbour (n, GNUNET_NO);
2323 * For an existing neighbour record, set the active connection to
2324 * use the given address.
2326 * @param peer identity of the peer to switch the address for
2327 * @param address address of the other peer, NULL if other peer
2329 * @param session session to use (or NULL)
2330 * @param ats performance data
2331 * @param ats_count number of entries in ats
2332 * @param bandwidth_in inbound quota to be used when connection is up
2333 * @param bandwidth_out outbound quota to be used when connection is up
2336 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
2337 const struct GNUNET_HELLO_Address *address,
2338 struct Session *session,
2339 const struct GNUNET_ATS_Information *ats,
2341 struct GNUNET_BANDWIDTH_Value32NBO
2343 struct GNUNET_BANDWIDTH_Value32NBO
2346 struct NeighbourMapEntry *n;
2347 struct GNUNET_TRANSPORT_PluginFunctions *papi;
2349 GNUNET_assert (address->transport_name != NULL);
2350 if (NULL == (n = lookup_neighbour (peer)))
2353 /* Obtain an session for this address from plugin */
2354 if (NULL == (papi = GST_plugins_find (address->transport_name)))
2356 /* we don't have the plugin for this address */
2357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2358 "2348 : `%s' \n", address->transport_name);
2359 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2362 if ((NULL == session) && (0 == address->address_length))
2365 if (strlen (address->transport_name) > 0)
2366 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2371 "ATS tells us to switch to address '%s' session %p for "
2372 "peer `%s' in state %s (quota in/out %u %u )\n",
2373 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
2376 print_state (n->state),
2377 ntohl (bandwidth_in.value__),
2378 ntohl (bandwidth_out.value__));
2380 if (NULL == session)
2382 session = papi->get_session (papi->cls, address);
2383 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2384 "Obtained new session for peer `%s' and address '%s': %p\n",
2385 GNUNET_i2s (&address->peer), GST_plugins_a2s (address), session);
2387 if (NULL == session)
2389 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2390 "Failed to obtain new session for peer `%s' and address '%s'\n",
2391 GNUNET_i2s (&address->peer), GST_plugins_a2s (address));
2392 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
2397 case S_NOT_CONNECTED:
2399 free_neighbour (n, GNUNET_NO);
2402 set_address (&n->primary_address,
2403 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2404 n->state = S_INIT_BLACKLIST;
2405 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2406 check_blacklist (&n->id,
2407 n->connect_ack_timestamp,
2410 case S_INIT_BLACKLIST:
2411 /* ATS suggests a different address, switch again */
2412 set_address (&n->primary_address,
2413 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2414 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2415 check_blacklist (&n->id,
2416 n->connect_ack_timestamp,
2419 case S_CONNECT_SENT:
2420 /* ATS suggests a different address, switch again */
2421 set_address (&n->primary_address,
2422 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2423 n->state = S_INIT_BLACKLIST;
2424 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2425 check_blacklist (&n->id,
2426 n->connect_ack_timestamp,
2429 case S_CONNECT_RECV_ATS:
2430 set_address (&n->primary_address,
2431 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2432 n->state = S_CONNECT_RECV_BLACKLIST;
2433 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2434 check_blacklist (&n->id,
2435 n->connect_ack_timestamp,
2438 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2439 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2440 check_blacklist (&n->id,
2441 n->connect_ack_timestamp,
2444 case S_CONNECT_RECV_BLACKLIST:
2445 case S_CONNECT_RECV_ACK:
2446 /* ATS asks us to switch while we were trying to connect; switch to new
2447 address and check blacklist again */
2448 set_address (&n->primary_address,
2449 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2450 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2451 check_blacklist (&n->id,
2452 n->connect_ack_timestamp,
2456 GNUNET_assert (NULL != n->primary_address.address);
2457 GNUNET_assert (NULL != n->primary_address.session);
2458 if (n->primary_address.session == session)
2460 /* not an address change, just a quota change */
2461 set_address (&n->primary_address,
2462 address, session, bandwidth_in, bandwidth_out, GNUNET_YES);
2465 /* ATS asks us to switch a life connection; see if we can get
2466 a CONNECT_ACK on it before we actually do this! */
2467 set_address (&n->alternative_address,
2468 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2469 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2470 check_blacklist (&n->id,
2471 GNUNET_TIME_absolute_get (),
2474 case S_RECONNECT_ATS:
2475 set_address (&n->primary_address,
2476 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2477 n->state = S_RECONNECT_BLACKLIST;
2478 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2479 check_blacklist (&n->id,
2480 n->connect_ack_timestamp,
2483 case S_RECONNECT_BLACKLIST:
2484 /* ATS asks us to switch while we were trying to reconnect; switch to new
2485 address and check blacklist again */
2486 set_address (&n->primary_address,
2487 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2488 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2489 check_blacklist (&n->id,
2490 n->connect_ack_timestamp,
2493 case S_RECONNECT_SENT:
2494 /* ATS asks us to switch while we were trying to reconnect; switch to new
2495 address and check blacklist again */
2496 set_address (&n->primary_address,
2497 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2498 n->state = S_RECONNECT_BLACKLIST;
2499 n->timeout = GNUNET_TIME_relative_to_absolute (BLACKLIST_RESPONSE_TIMEOUT);
2500 check_blacklist (&n->id,
2501 n->connect_ack_timestamp,
2504 case S_CONNECTED_SWITCHING_BLACKLIST:
2505 if (n->primary_address.session == session)
2507 /* ATS switches back to still-active session */
2508 free_address (&n->alternative_address);
2509 n->state = S_CONNECTED;
2512 /* ATS asks us to switch a life connection, update blacklist check */
2513 set_address (&n->alternative_address,
2514 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2515 check_blacklist (&n->id,
2516 GNUNET_TIME_absolute_get (),
2519 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2520 if (n->primary_address.session == session)
2522 /* ATS switches back to still-active session */
2523 free_address (&n->alternative_address);
2524 n->state = S_CONNECTED;
2527 /* ATS asks us to switch a life connection, update blacklist check */
2528 set_address (&n->alternative_address,
2529 address, session, bandwidth_in, bandwidth_out, GNUNET_NO);
2530 n->state = S_CONNECTED_SWITCHING_BLACKLIST;
2531 check_blacklist (&n->id,
2532 GNUNET_TIME_absolute_get (),
2536 /* not going to switch addresses while disconnecting */
2538 case S_DISCONNECT_FINISHED:
2542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2550 * Master task run for every neighbour. Performs all of the time-related
2551 * activities (keep alive, send next message, disconnect if idle, finish
2552 * clean up after disconnect).
2554 * @param cls the 'struct NeighbourMapEntry' for which we are running
2555 * @param tc scheduler context (unused)
2558 master_task (void *cls,
2559 const struct GNUNET_SCHEDULER_TaskContext *tc)
2561 struct NeighbourMapEntry *n = cls;
2562 struct GNUNET_TIME_Relative delay;
2564 n->task = GNUNET_SCHEDULER_NO_TASK;
2565 delay = GNUNET_TIME_absolute_get_remaining (n->timeout);
2566 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2567 "Master task runs for neighbour `%s' in state %s with timeout in %llu ms\n",
2568 GNUNET_i2s (&n->id),
2569 print_state(n->state),
2570 (unsigned long long) delay.rel_value);
2573 case S_NOT_CONNECTED:
2574 /* invalid state for master task, clean up */
2576 n->state = S_DISCONNECT_FINISHED;
2577 free_neighbour (n, GNUNET_NO);
2580 if (0 == delay.rel_value)
2582 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2583 "Connection to `%s' timed out waiting for ATS to provide address\n",
2584 GNUNET_i2s (&n->id));
2585 n->state = S_DISCONNECT_FINISHED;
2586 free_neighbour (n, GNUNET_NO);
2590 case S_INIT_BLACKLIST:
2591 if (0 == delay.rel_value)
2593 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2594 "Connection to `%s' timed out waiting for BLACKLIST to approve address\n",
2595 GNUNET_i2s (&n->id));
2596 n->state = S_DISCONNECT_FINISHED;
2597 free_neighbour (n, GNUNET_NO);
2601 case S_CONNECT_SENT:
2602 if (0 == delay.rel_value)
2604 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2605 "Connection to `%s' timed out waiting for other peer to send CONNECT_ACK\n",
2606 GNUNET_i2s (&n->id));
2607 disconnect_neighbour (n);
2611 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2612 if (0 == delay.rel_value)
2614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2615 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for received CONNECT\n",
2616 GNUNET_i2s (&n->id));
2617 n->state = S_DISCONNECT_FINISHED;
2618 free_neighbour (n, GNUNET_NO);
2622 case S_CONNECT_RECV_ATS:
2623 if (0 == delay.rel_value)
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 "Connection to `%s' timed out waiting ATS to provide address to use for CONNECT_ACK\n",
2627 GNUNET_i2s (&n->id));
2628 n->state = S_DISCONNECT_FINISHED;
2629 free_neighbour (n, GNUNET_NO);
2633 case S_CONNECT_RECV_BLACKLIST:
2634 if (0 == delay.rel_value)
2636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2637 "Connection to `%s' timed out waiting BLACKLIST to approve address to use for CONNECT_ACK\n",
2638 GNUNET_i2s (&n->id));
2639 n->state = S_DISCONNECT_FINISHED;
2640 free_neighbour (n, GNUNET_NO);
2644 case S_CONNECT_RECV_ACK:
2645 if (0 == delay.rel_value)
2647 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2648 "Connection to `%s' timed out waiting for other peer to send SESSION_ACK\n",
2649 GNUNET_i2s (&n->id));
2650 disconnect_neighbour (n);
2655 if (0 == delay.rel_value)
2657 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2658 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2659 GNUNET_i2s (&n->id));
2660 disconnect_neighbour (n);
2663 try_transmission_to_peer (n);
2666 case S_RECONNECT_ATS:
2667 if (0 == delay.rel_value)
2669 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2670 "Connection to `%s' timed out, waiting for ATS replacement address\n",
2671 GNUNET_i2s (&n->id));
2672 disconnect_neighbour (n);
2676 case S_RECONNECT_BLACKLIST:
2677 if (0 == delay.rel_value)
2679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2680 "Connection to `%s' timed out, waiting for BLACKLIST to approve replacement address\n",
2681 GNUNET_i2s (&n->id));
2682 disconnect_neighbour (n);
2686 case S_RECONNECT_SENT:
2687 if (0 == delay.rel_value)
2689 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2690 "Connection to `%s' timed out, waiting for other peer to CONNECT_ACK replacement address\n",
2691 GNUNET_i2s (&n->id));
2692 disconnect_neighbour (n);
2696 case S_CONNECTED_SWITCHING_BLACKLIST:
2697 if (0 == delay.rel_value)
2699 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2700 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs\n",
2701 GNUNET_i2s (&n->id));
2702 disconnect_neighbour (n);
2705 try_transmission_to_peer (n);
2708 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2709 if (0 == delay.rel_value)
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2712 "Connection to `%s' timed out, missing KEEPALIVE_RESPONSEs (after trying to CONNECT on alternative address)\n",
2713 GNUNET_i2s (&n->id));
2714 disconnect_neighbour (n);
2717 try_transmission_to_peer (n);
2721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2722 "Cleaning up connection to `%s' after sending DISCONNECT\n",
2723 GNUNET_i2s (&n->id));
2724 free_neighbour (n, GNUNET_NO);
2726 case S_DISCONNECT_FINISHED:
2727 /* how did we get here!? */
2731 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2735 if ( (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) ||
2736 (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2737 (S_CONNECTED == n->state) )
2739 /* if we are *now* in one of these three states, we're sending
2740 keep alive messages, so we need to consider the keepalive
2741 delay, not just the connection timeout */
2742 delay = GNUNET_TIME_relative_min (GNUNET_TIME_absolute_get_remaining (n->keep_alive_time),
2745 if (GNUNET_SCHEDULER_NO_TASK == n->task)
2746 n->task = GNUNET_SCHEDULER_add_delayed (delay,
2753 * Send a SESSION_ACK message to the neighbour to confirm that we
2754 * got his CONNECT_ACK.
2756 * @param n neighbour to send the SESSION_ACK to
2759 send_session_ack_message (struct NeighbourMapEntry *n)
2761 struct GNUNET_MessageHeader msg;
2763 msg.size = htons (sizeof (struct GNUNET_MessageHeader));
2764 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2765 (void) send_with_session(n,
2766 (const char *) &msg, sizeof (struct GNUNET_MessageHeader),
2767 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2773 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2774 * Consider switching to it.
2776 * @param message possibly a 'struct SessionConnectMessage' (check format)
2777 * @param peer identity of the peer to switch the address for
2778 * @param address address of the other peer, NULL if other peer
2780 * @param session session to use (or NULL)
2783 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2784 const struct GNUNET_PeerIdentity *peer,
2785 const struct GNUNET_HELLO_Address *address,
2786 struct Session *session)
2788 const struct SessionConnectMessage *scm;
2789 struct GNUNET_TIME_Absolute ts;
2790 struct NeighbourMapEntry *n;
2792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2793 "Received CONNECT_ACK message from peer `%s'\n",
2796 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2798 GNUNET_break_op (0);
2801 scm = (const struct SessionConnectMessage *) message;
2802 GNUNET_break_op (ntohl (scm->reserved) == 0);
2803 if (NULL == (n = lookup_neighbour (peer)))
2805 GNUNET_STATISTICS_update (GST_stats,
2807 ("# unexpected CONNECT_ACK messages (no peer)"),
2811 ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2814 case S_NOT_CONNECTED:
2816 free_neighbour (n, GNUNET_NO);
2819 case S_INIT_BLACKLIST:
2820 GNUNET_STATISTICS_update (GST_stats,
2822 ("# unexpected CONNECT_ACK messages (not ready)"),
2825 case S_CONNECT_SENT:
2826 if (ts.abs_value != n->primary_address.connect_timestamp.abs_value)
2827 break; /* ACK does not match our original CONNECT message */
2828 n->state = S_CONNECTED;
2829 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2830 GNUNET_STATISTICS_set (GST_stats,
2831 gettext_noop ("# peers connected"),
2832 ++neighbours_connected,
2834 connect_notify_cb (callback_cls, &n->id,
2835 n->primary_address.bandwidth_in,
2836 n->primary_address.bandwidth_out);
2837 /* Tell ATS that the outbound session we created to send CONNECT was successfull */
2838 add_address (n->primary_address.address, n->primary_address.session);
2839 set_address (&n->primary_address,
2840 n->primary_address.address,
2841 n->primary_address.session,
2842 n->primary_address.bandwidth_in,
2843 n->primary_address.bandwidth_out,
2845 send_session_ack_message (n);
2847 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2848 case S_CONNECT_RECV_ATS:
2849 case S_CONNECT_RECV_BLACKLIST:
2850 case S_CONNECT_RECV_ACK:
2851 GNUNET_STATISTICS_update (GST_stats,
2853 ("# unexpected CONNECT_ACK messages (not ready)"),
2857 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2858 send_session_ack_message (n);
2860 case S_RECONNECT_ATS:
2861 case S_RECONNECT_BLACKLIST:
2862 /* we didn't expect any CONNECT_ACK, as we are waiting for ATS
2863 to give us a new address... */
2864 GNUNET_STATISTICS_update (GST_stats,
2866 ("# unexpected CONNECT_ACK messages (waiting on ATS)"),
2869 case S_RECONNECT_SENT:
2870 /* new address worked; go back to connected! */
2871 n->state = S_CONNECTED;
2872 send_session_ack_message (n);
2874 case S_CONNECTED_SWITCHING_BLACKLIST:
2875 /* duplicate CONNECT_ACK, let's answer by duplciate SESSION_ACK just in case */
2876 send_session_ack_message (n);
2878 case S_CONNECTED_SWITCHING_CONNECT_SENT:
2879 /* new address worked; adopt it and go back to connected! */
2880 n->state = S_CONNECTED;
2881 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
2882 GNUNET_break (GNUNET_NO == n->alternative_address.ats_active);
2884 add_address (n->alternative_address.address, n->alternative_address.session);
2885 set_address (&n->primary_address,
2886 n->alternative_address.address,
2887 n->alternative_address.session,
2888 n->alternative_address.bandwidth_in,
2889 n->alternative_address.bandwidth_out,
2891 free_address (&n->alternative_address);
2892 send_session_ack_message (n);
2895 GNUNET_STATISTICS_update (GST_stats,
2897 ("# unexpected CONNECT_ACK messages (disconnecting)"),
2900 case S_DISCONNECT_FINISHED:
2904 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
2912 * A session was terminated. Take note; if needed, try to get
2913 * an alternative address from ATS.
2915 * @param peer identity of the peer where the session died
2916 * @param session session that is gone
2917 * @return GNUNET_YES if this was a session used, GNUNET_NO if
2918 * this session was not in use
2921 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
2922 struct Session *session)
2924 struct NeighbourMapEntry *n;
2925 struct BlackListCheckContext *bcc;
2926 struct BlackListCheckContext *bcc_next;
2928 /* make sure to cancel all ongoing blacklist checks involving 'session' */
2930 while (NULL != (bcc = bcc_next))
2932 bcc_next = bcc->next;
2933 if (bcc->na.session == session)
2935 GST_blacklist_test_cancel (bcc->bc);
2936 MEMDEBUG_free (bcc->na.address, __LINE__);
2937 //GNUNET_HELLO_address_free (bcc->na.address);
2938 GNUNET_CONTAINER_DLL_remove (bc_head,
2941 MEMDEBUG_free (bcc, __LINE__);
2944 if (NULL == (n = lookup_neighbour (peer)))
2945 return GNUNET_NO; /* can't affect us */
2946 if (session != n->primary_address.session)
2948 if (session == n->alternative_address.session)
2950 free_address (&n->alternative_address);
2951 if ( (S_CONNECTED_SWITCHING_BLACKLIST == n->state) ||
2952 (S_CONNECTED_SWITCHING_CONNECT_SENT == n->state) )
2953 n->state = S_CONNECTED;
2957 return GNUNET_NO; /* doesn't affect us further */
2960 n->expect_latency_response = GNUNET_NO;
2963 case S_NOT_CONNECTED:
2965 free_neighbour (n, GNUNET_NO);
2969 free_neighbour (n, GNUNET_NO);
2971 case S_INIT_BLACKLIST:
2972 case S_CONNECT_SENT:
2973 free_address (&n->primary_address);
2974 n->state = S_INIT_ATS;
2975 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2976 // FIXME: need to ask ATS for suggestions again?
2977 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2979 case S_CONNECT_RECV_BLACKLIST_INBOUND:
2980 case S_CONNECT_RECV_ATS:
2981 case S_CONNECT_RECV_BLACKLIST:
2982 case S_CONNECT_RECV_ACK:
2983 /* error on inbound session; free neighbour entirely */
2984 free_address (&n->primary_address);
2985 free_neighbour (n, GNUNET_NO);
2988 free_address (&n->primary_address);
2989 n->state = S_RECONNECT_ATS;
2990 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
2991 /* FIXME: is this ATS call needed? */
2992 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
2994 case S_RECONNECT_ATS:
2995 /* we don't have an address, how can it go down? */
2998 case S_RECONNECT_BLACKLIST:
2999 case S_RECONNECT_SENT:
3000 n->state = S_RECONNECT_ATS;
3001 n->timeout = GNUNET_TIME_relative_to_absolute (ATS_RESPONSE_TIMEOUT);
3002 // FIXME: need to ask ATS for suggestions again?
3003 n->suggest_handle = GNUNET_ATS_suggest_address (GST_ats, &n->id);
3005 case S_CONNECTED_SWITCHING_BLACKLIST:
3006 /* primary went down while we were checking secondary against
3007 blacklist, adopt secondary as primary */
3008 free_address (&n->primary_address);
3009 n->primary_address = n->alternative_address;
3010 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3011 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3012 n->state = S_RECONNECT_BLACKLIST;
3014 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3015 /* primary went down while we were waiting for CONNECT_ACK on secondary;
3016 secondary as primary */
3017 free_address (&n->primary_address);
3018 n->primary_address = n->alternative_address;
3019 memset (&n->alternative_address, 0, sizeof (struct NeighbourAddress));
3020 n->timeout = GNUNET_TIME_relative_to_absolute (FAST_RECONNECT_TIMEOUT);
3021 n->state = S_RECONNECT_SENT;
3024 free_address (&n->primary_address);
3026 case S_DISCONNECT_FINISHED:
3027 /* neighbour was freed and plugins told to terminate session */
3031 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3035 if (GNUNET_SCHEDULER_NO_TASK != n->task)
3036 GNUNET_SCHEDULER_cancel (n->task);
3037 n->task = GNUNET_SCHEDULER_add_now (&master_task, n);
3043 * We received a 'SESSION_ACK' message from the other peer.
3044 * If we sent a 'CONNECT_ACK' last, this means we are now
3045 * connected. Otherwise, do nothing.
3047 * @param message possibly a 'struct SessionConnectMessage' (check format)
3048 * @param peer identity of the peer to switch the address for
3049 * @param address address of the other peer, NULL if other peer
3051 * @param session session to use (or NULL)
3054 GST_neighbours_handle_session_ack (const struct GNUNET_MessageHeader *message,
3055 const struct GNUNET_PeerIdentity *peer,
3056 const struct GNUNET_HELLO_Address *address,
3057 struct Session *session)
3059 struct NeighbourMapEntry *n;
3061 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3062 "Received SESSION_ACK message from peer `%s'\n",
3064 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
3066 GNUNET_break_op (0);
3069 if (NULL == (n = lookup_neighbour (peer)))
3071 /* check if we are in a plausible state for having sent
3072 a CONNECT_ACK. If not, return, otherwise break */
3073 if ( ( (S_CONNECT_RECV_ACK != n->state) &&
3074 (S_CONNECT_SENT != n->state) ) ||
3075 (2 != n->send_connect_ack) )
3077 GNUNET_STATISTICS_update (GST_stats,
3078 gettext_noop ("# unexpected SESSION ACK messages"), 1,
3082 n->state = S_CONNECTED;
3083 n->timeout = GNUNET_TIME_relative_to_absolute (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT);
3084 GNUNET_STATISTICS_set (GST_stats,
3085 gettext_noop ("# peers connected"),
3086 ++neighbours_connected,
3088 connect_notify_cb (callback_cls, &n->id,
3089 n->primary_address.bandwidth_in,
3090 n->primary_address.bandwidth_out);
3092 add_address (n->primary_address.address, n->primary_address.session);
3093 set_address (&n->primary_address,
3094 n->primary_address.address,
3095 n->primary_address.session,
3096 n->primary_address.bandwidth_in,
3097 n->primary_address.bandwidth_out,
3103 * Test if we're connected to the given peer.
3105 * @param target peer to test
3106 * @return GNUNET_YES if we are connected, GNUNET_NO if not
3109 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
3111 return test_connected (lookup_neighbour (target));
3116 * Change the incoming quota for the given peer.
3118 * @param neighbour identity of peer to change qutoa for
3119 * @param quota new quota
3122 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
3123 struct GNUNET_BANDWIDTH_Value32NBO quota)
3125 struct NeighbourMapEntry *n;
3127 if (NULL == (n = lookup_neighbour (neighbour)))
3129 GNUNET_STATISTICS_update (GST_stats,
3131 ("# SET QUOTA messages ignored (no such peer)"),
3135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3136 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
3137 ntohl (quota.value__), GNUNET_i2s (&n->id));
3138 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
3139 if (0 != ntohl (quota.value__))
3141 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
3142 GNUNET_i2s (&n->id), "SET_QUOTA");
3143 if (GNUNET_YES == test_connected (n))
3144 GNUNET_STATISTICS_update (GST_stats,
3145 gettext_noop ("# disconnects due to quota of 0"),
3147 disconnect_neighbour (n);
3152 * We received a disconnect message from the given peer,
3153 * validate and process.
3155 * @param peer sender of the message
3156 * @param msg the disconnect message
3159 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
3161 const struct GNUNET_MessageHeader
3164 struct NeighbourMapEntry *n;
3165 const struct SessionDisconnectMessage *sdm;
3166 struct GNUNET_HashCode hc;
3168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3169 "Received DISCONNECT message from peer `%s'\n",
3171 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
3173 // GNUNET_break_op (0);
3174 GNUNET_STATISTICS_update (GST_stats,
3176 ("# disconnect messages ignored (old format)"), 1,
3180 sdm = (const struct SessionDisconnectMessage *) msg;
3181 if (NULL == (n = lookup_neighbour (peer)))
3182 return; /* gone already */
3183 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <= n->connect_ack_timestamp.abs_value)
3185 GNUNET_STATISTICS_update (GST_stats,
3187 ("# disconnect messages ignored (timestamp)"), 1,
3191 GNUNET_CRYPTO_hash (&sdm->public_key,
3192 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded),
3194 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
3196 GNUNET_break_op (0);
3199 if (ntohl (sdm->purpose.size) !=
3200 sizeof (struct GNUNET_CRYPTO_EccSignaturePurpose) +
3201 sizeof (struct GNUNET_CRYPTO_EccPublicKeyBinaryEncoded) +
3202 sizeof (struct GNUNET_TIME_AbsoluteNBO))
3204 GNUNET_break_op (0);
3208 GNUNET_CRYPTO_ecc_verify
3209 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
3210 &sdm->signature, &sdm->public_key))
3212 GNUNET_break_op (0);
3215 if (GNUNET_YES == test_connected (n))
3216 GNUNET_STATISTICS_update (GST_stats,
3218 ("# other peer asked to disconnect from us"), 1,
3220 disconnect_neighbour (n);
3225 * Closure for the neighbours_iterate function.
3227 struct IteratorContext
3230 * Function to call on each connected neighbour.
3232 GST_NeighbourIterator cb;
3242 * Call the callback from the closure for each connected neighbour.
3244 * @param cls the 'struct IteratorContext'
3245 * @param key the hash of the public key of the neighbour
3246 * @param value the 'struct NeighbourMapEntry'
3247 * @return GNUNET_OK (continue to iterate)
3250 neighbours_iterate (void *cls, const struct GNUNET_HashCode * key, void *value)
3252 struct IteratorContext *ic = cls;
3253 struct NeighbourMapEntry *n = value;
3255 if (GNUNET_YES == test_connected (n))
3257 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
3258 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
3260 if (NULL != n->primary_address.address)
3262 bandwidth_in = n->primary_address.bandwidth_in;
3263 bandwidth_out = n->primary_address.bandwidth_out;
3267 bandwidth_in = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3268 bandwidth_out = GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT;
3271 ic->cb (ic->cb_cls, &n->id,
3272 n->primary_address.address,
3273 bandwidth_in, bandwidth_out);
3280 * Iterate over all connected neighbours.
3282 * @param cb function to call
3283 * @param cb_cls closure for cb
3286 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
3288 struct IteratorContext ic;
3290 if (NULL == neighbours)
3291 return; /* can happen during shutdown */
3294 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
3299 * If we have an active connection to the given target, it must be shutdown.
3301 * @param target peer to disconnect from
3304 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
3306 struct NeighbourMapEntry *n;
3308 if (NULL == (n = lookup_neighbour (target)))
3309 return; /* not active */
3310 if (GNUNET_YES == test_connected (n))
3311 GNUNET_STATISTICS_update (GST_stats,
3313 ("# disconnected from peer upon explicit request"), 1,
3315 disconnect_neighbour (n);
3320 * Obtain current latency information for the given neighbour.
3322 * @param peer to get the latency for
3323 * @return observed latency of the address, FOREVER if the
3324 * the connection is not up
3326 struct GNUNET_TIME_Relative
3327 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
3329 struct NeighbourMapEntry *n;
3331 n = lookup_neighbour (peer);
3333 return GNUNET_TIME_UNIT_FOREVER_REL;
3337 case S_CONNECTED_SWITCHING_CONNECT_SENT:
3338 case S_CONNECTED_SWITCHING_BLACKLIST:
3339 case S_RECONNECT_SENT:
3340 case S_RECONNECT_ATS:
3341 case S_RECONNECT_BLACKLIST:
3343 case S_NOT_CONNECTED:
3344 case S_INIT_BLACKLIST:
3346 case S_CONNECT_RECV_BLACKLIST_INBOUND:
3347 case S_CONNECT_RECV_ATS:
3348 case S_CONNECT_RECV_BLACKLIST:
3349 case S_CONNECT_RECV_ACK:
3350 case S_CONNECT_SENT:
3352 case S_DISCONNECT_FINISHED:
3353 return GNUNET_TIME_UNIT_FOREVER_REL;
3355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Unhandled state `%s' \n",print_state (n->state));
3359 return GNUNET_TIME_UNIT_FOREVER_REL;
3364 * Obtain current address information for the given neighbour.
3367 * @return address currently used
3369 struct GNUNET_HELLO_Address *
3370 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
3372 struct NeighbourMapEntry *n;
3374 n = lookup_neighbour (peer);
3377 return n->primary_address.address;
3382 * Initialize the neighbours subsystem.
3384 * @param cls closure for callbacks
3385 * @param connect_cb function to call if we connect to a peer
3386 * @param disconnect_cb function to call if we disconnect from a peer
3387 * @param peer_address_cb function to call if we change an active address
3389 * @param max_fds maximum number of fds to use
3392 GST_neighbours_start (void *cls,
3393 NotifyConnect connect_cb,
3394 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
3395 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb,
3396 unsigned int max_fds)
3399 connect_notify_cb = connect_cb;
3400 disconnect_notify_cb = disconnect_cb;
3401 address_change_cb = peer_address_cb;
3402 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE, GNUNET_NO);
3407 * Disconnect from the given neighbour.
3410 * @param key hash of neighbour's public key (not used)
3411 * @param value the 'struct NeighbourMapEntry' of the neighbour
3412 * @return GNUNET_OK (continue to iterate)
3415 disconnect_all_neighbours (void *cls, const struct GNUNET_HashCode * key, void *value)
3417 struct NeighbourMapEntry *n = value;
3419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
3420 "Disconnecting peer `%4s', %s\n",
3421 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
3422 n->state = S_DISCONNECT_FINISHED;
3423 free_neighbour (n, GNUNET_NO);
3429 * Cleanup the neighbours subsystem.
3432 GST_neighbours_stop ()
3434 if (NULL == neighbours)
3436 GNUNET_CONTAINER_multihashmap_iterate (neighbours,
3437 &disconnect_all_neighbours,
3439 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
3441 callback_cls = NULL;
3442 connect_notify_cb = NULL;
3443 disconnect_notify_cb = NULL;
3444 address_change_cb = NULL;
3448 /* end of file gnunet-service-transport_neighbours.c */