2 This file is part of GNUnet.
3 (C) 2010,2011 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 #include "gnunet_ats_service.h"
28 #include "gnunet-service-transport_neighbours.h"
29 #include "gnunet-service-transport_plugins.h"
30 #include "gnunet-service-transport_validation.h"
31 #include "gnunet-service-transport_clients.h"
32 #include "gnunet-service-transport.h"
33 #include "gnunet_peerinfo_service.h"
34 #include "gnunet-service-transport_blacklist.h"
35 #include "gnunet_constants.h"
36 #include "transport.h"
40 * Size of the neighbour hash map.
42 #define NEIGHBOUR_TABLE_SIZE 256
45 * How often must a peer violate bandwidth quotas before we start
46 * to simply drop its messages?
48 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
51 * How often do we send KEEPALIVE messages to each of our neighbours and measure
52 * the latency with this neighbour?
53 * (idle timeout is 5 minutes or 300 seconds, so with 30s interval we
54 * send 10 keepalives in each interval, so 10 messages would need to be
55 * lost in a row for a disconnect).
57 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 30)
60 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
62 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
64 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
66 #define TEST_NEW_CODE GNUNET_NO
69 * Entry in neighbours.
71 struct NeighbourMapEntry;
73 GNUNET_NETWORK_STRUCT_BEGIN
76 * Message a peer sends to another to indicate its
77 * preference for communicating via a particular
78 * session (and the desire to establish a real
81 struct SessionConnectMessage
84 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
86 struct GNUNET_MessageHeader header;
91 uint32_t reserved GNUNET_PACKED;
94 * Absolute time at the sender. Only the most recent connect
95 * message implies which session is preferred by the sender.
97 struct GNUNET_TIME_AbsoluteNBO timestamp;
102 struct SessionDisconnectMessage
105 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
107 struct GNUNET_MessageHeader header;
112 uint32_t reserved GNUNET_PACKED;
115 * Purpose of the signature. Extends over the timestamp.
116 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
118 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
121 * Absolute time at the sender. Only the most recent connect
122 * message implies which session is preferred by the sender.
124 struct GNUNET_TIME_AbsoluteNBO timestamp;
127 * Public key of the sender.
129 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
132 * Signature of the peer that sends us the disconnect. Only
133 * valid if the timestamp is AFTER the timestamp from the
134 * corresponding 'CONNECT' message.
136 struct GNUNET_CRYPTO_RsaSignature signature;
139 GNUNET_NETWORK_STRUCT_END
142 * For each neighbour we keep a list of messages
143 * that we still want to transmit to the neighbour.
149 * This is a doubly linked list.
151 struct MessageQueue *next;
154 * This is a doubly linked list.
156 struct MessageQueue *prev;
159 * Once this message is actively being transmitted, which
160 * neighbour is it associated with?
162 struct NeighbourMapEntry *n;
165 * Function to call once we're done.
167 GST_NeighbourSendContinuation cont;
175 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
176 * stuck together in memory. Allocated at the end of this struct.
178 const char *message_buf;
181 * Size of the message buf
183 size_t message_buf_size;
186 * At what time should we fail?
188 struct GNUNET_TIME_Absolute timeout;
196 * fresh peer or completely disconnected
201 * sent CONNECT message to other peer, waiting for CONNECT_ACK
206 * received CONNECT message to other peer, sending CONNECT_ACK
211 * received ACK or payload
216 * connection ended, fast reconnect
221 * Disconnect in progress
235 * Entry in neighbours.
237 struct NeighbourMapEntry
241 * Head of list of messages we would like to send to this peer;
242 * must contain at most one message per client.
244 struct MessageQueue *messages_head;
247 * Tail of list of messages we would like to send to this peer; must
248 * contain at most one message per client.
250 struct MessageQueue *messages_tail;
253 * Are we currently trying to send a message? If so, which one?
255 struct MessageQueue *is_active;
258 * Active session for communicating with the peer.
260 struct Session *session;
263 * Address we currently use.
265 struct GNUNET_HELLO_Address *address;
268 * Identity of this neighbour.
270 struct GNUNET_PeerIdentity id;
273 * ID of task scheduled to run when this peer is about to
274 * time out (will free resources associated with the peer).
276 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
279 * ID of task scheduled to send keepalives.
281 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
284 * ID of task scheduled to run when we should try transmitting
285 * the head of the message queue.
287 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
290 * Tracker for inbound bandwidth.
292 struct GNUNET_BANDWIDTH_Tracker in_tracker;
295 * Inbound bandwidth from ATS, activated when connection is up
297 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
300 * Inbound bandwidth from ATS, activated when connection is up
302 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
305 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
307 struct GNUNET_TIME_Absolute connect_ts;
310 * When did we sent the last keep-alive message?
312 struct GNUNET_TIME_Absolute keep_alive_sent;
315 * Latest calculated latency value
317 struct GNUNET_TIME_Relative latency;
321 * We asked ATS for a new address for this peer
323 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
326 * Task the resets the peer state after due to an pending
327 * unsuccessful connection setup
329 GNUNET_SCHEDULER_TaskIdentifier state_reset;
333 * How often has the other peer (recently) violated the inbound
334 * traffic limit? Incremented by 10 per violation, decremented by 1
335 * per non-violation (for each time interval).
337 unsigned int quota_violation_count;
341 * The current state of the peer
342 * Element of enum State
347 * Did we sent an KEEP_ALIVE message and are we expecting a response?
349 int expect_latency_response;
355 * All known neighbours and their HELLOs.
357 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
360 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
362 static void *callback_cls;
365 * Function to call when we connected to a neighbour.
367 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
370 * Function to call when we disconnected from a neighbour.
372 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
375 * Function to call when we changed an active address of a neighbour.
377 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
380 * counter for connected neighbours
382 static int neighbours_connected;
385 * Lookup a neighbour entry in the neighbours hash map.
387 * @param pid identity of the peer to look up
388 * @return the entry, NULL if there is no existing record
390 static struct NeighbourMapEntry *
391 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
393 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
397 * Disconnect from the given neighbour, clean up the record.
399 * @param n neighbour to disconnect from
402 disconnect_neighbour (struct NeighbourMapEntry *n);
404 #define change_state(n, state, ...) change (n, state, __LINE__)
407 is_connecting (struct NeighbourMapEntry *n)
409 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
415 is_connected (struct NeighbourMapEntry *n)
417 if (n->state == S_CONNECTED)
423 is_disconnecting (struct NeighbourMapEntry *n)
425 if (n->state == S_DISCONNECT)
431 print_state (int state)
436 return "S_CONNECTED";
439 return "S_CONNECT_RECV";
442 return "S_CONNECT_SENT";
445 return "S_DISCONNECT";
447 case S_NOT_CONNECTED:
448 return "S_NOT_CONNECTED";
450 case S_FAST_RECONNECT:
451 return "S_FAST_RECONNECT";
461 change (struct NeighbourMapEntry *n, int state, int line);
464 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
468 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
470 struct NeighbourMapEntry *n = cls;
475 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
476 if (n->state == S_CONNECTED)
480 GNUNET_STATISTICS_update (GST_stats,
482 ("# failed connection attempts due to timeout"), 1,
486 /* resetting state */
488 if (n->state == S_FAST_RECONNECT)
490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
491 "Fast reconnect time out, disconnecting peer `%s'\n",
492 GNUNET_i2s (&n->id));
493 disconnect_neighbour(n);
497 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
498 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
499 GNUNET_i2s (&n->id), n, print_state(n->state), "S_NOT_CONNECTED", __LINE__);
501 n->state = S_NOT_CONNECTED;
503 /* destroying address */
504 if (n->address != NULL)
506 GNUNET_assert (strlen (n->address->transport_name) > 0);
507 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
510 /* request new address */
511 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
512 GNUNET_SCHEDULER_cancel (n->ats_suggest);
514 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
516 GNUNET_ATS_suggest_address (GST_ats, &n->id);
520 change (struct NeighbourMapEntry *n, int state, int line)
523 /* allowed transitions */
524 int allowed = GNUNET_NO;
526 previous_state = n->state;
530 case S_NOT_CONNECTED:
531 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
532 (state == S_DISCONNECT))
533 allowed = GNUNET_YES;
536 allowed = GNUNET_YES;
539 allowed = GNUNET_YES;
542 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
543 allowed = GNUNET_YES;
547 case S_FAST_RECONNECT:
548 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
549 allowed = GNUNET_YES;
555 if (allowed == GNUNET_NO)
557 char *old = GNUNET_strdup (print_state (n->state));
558 char *new = GNUNET_strdup (print_state (state));
560 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
561 "Illegal state transition from `%s' to `%s' in line %u \n", old,
566 return GNUNET_SYSERR;
569 char *old = GNUNET_strdup (print_state (n->state));
570 char *new = GNUNET_strdup (print_state (state));
572 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
573 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
574 GNUNET_i2s (&n->id), n, old, new, line);
582 case S_FAST_RECONNECT:
585 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
586 GNUNET_SCHEDULER_cancel (n->state_reset);
588 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
591 case S_NOT_CONNECTED:
593 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
596 char *old = GNUNET_strdup (print_state (n->state));
597 char *new = GNUNET_strdup (print_state (state));
599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
600 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
601 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), old, new);
605 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
606 GNUNET_SCHEDULER_cancel (n->state_reset);
607 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
615 if (NULL != address_change_cb)
617 if (n->state == S_CONNECTED)
618 address_change_cb (callback_cls, &n->id, n->address);
619 else if (previous_state == S_CONNECTED)
620 address_change_cb (callback_cls, &n->id, NULL);
627 send_with_session (struct NeighbourMapEntry *n,
628 const char *msgbuf, size_t msgbuf_size,
630 struct GNUNET_TIME_Relative timeout,
631 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
633 struct GNUNET_TRANSPORT_PluginFunctions *papi;
634 size_t ret = GNUNET_SYSERR;
636 GNUNET_assert (n != NULL);
637 GNUNET_assert (n->session != NULL);
639 papi = GST_plugins_find (n->address->transport_name);
643 cont (cont_cls, &n->id, GNUNET_SYSERR);
644 return GNUNET_SYSERR;
647 ret = papi->send (papi->cls,
654 if ((ret == -1) && (cont != NULL))
655 cont (cont_cls, &n->id, GNUNET_SYSERR);
660 * Task invoked to start a transmission to another peer.
662 * @param cls the 'struct NeighbourMapEntry'
663 * @param tc scheduler context
666 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
670 * We're done with our transmission attempt, continue processing.
672 * @param cls the 'struct MessageQueue' of the message
673 * @param receiver intended receiver
674 * @param success whether it worked or not
677 transmit_send_continuation (void *cls,
678 const struct GNUNET_PeerIdentity *receiver,
681 struct MessageQueue *mq = cls;
682 struct NeighbourMapEntry *n;
683 struct NeighbourMapEntry *tmp;
685 tmp = lookup_neighbour (receiver);
687 if ((NULL != n) && (tmp != NULL) && (tmp == n))
689 GNUNET_assert (n->is_active == mq);
691 if (success == GNUNET_YES)
693 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
694 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
699 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
700 (success == GNUNET_OK) ? "successful" : "FAILED");
702 if (NULL != mq->cont)
703 mq->cont (mq->cont_cls, success);
709 * Check the ready list for the given neighbour and if a plugin is
710 * ready for transmission (and if we have a message), do so!
712 * @param n target peer for which to transmit
715 try_transmission_to_peer (struct NeighbourMapEntry *n)
717 struct MessageQueue *mq;
718 struct GNUNET_TIME_Relative timeout;
721 if (n->is_active != NULL)
724 return; /* transmission already pending */
726 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
729 return; /* currently waiting for bandwidth */
731 while (NULL != (mq = n->messages_head))
733 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
734 if (timeout.rel_value > 0)
736 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
739 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
742 return; /* no more messages */
744 if (n->address == NULL)
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
748 GNUNET_i2s (&n->id));
750 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
751 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
752 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
753 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
757 if (GST_plugins_find (n->address->transport_name) == NULL)
762 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
766 if ((n->address->address_length == 0) && (n->session == NULL))
768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
769 GNUNET_i2s (&n->id));
770 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
771 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
772 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
776 ret = send_with_session(n,
777 mq->message_buf, mq->message_buf_size,
779 &transmit_send_continuation, mq);
783 /* failure, but 'send' would not call continuation in this case,
784 * so we need to do it here! */
785 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
792 * Task invoked to start a transmission to another peer.
794 * @param cls the 'struct NeighbourMapEntry'
795 * @param tc scheduler context
798 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
800 struct NeighbourMapEntry *n = cls;
802 GNUNET_assert (NULL != lookup_neighbour (&n->id));
803 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
804 try_transmission_to_peer (n);
809 * Initialize the neighbours subsystem.
811 * @param cls closure for callbacks
812 * @param connect_cb function to call if we connect to a peer
813 * @param disconnect_cb function to call if we disconnect from a peer
814 * @param peer_address_cb function to call if we change an active address
818 GST_neighbours_start (void *cls,
819 GNUNET_TRANSPORT_NotifyConnect connect_cb,
820 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
821 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
824 connect_notify_cb = connect_cb;
825 disconnect_notify_cb = disconnect_cb;
826 address_change_cb = peer_address_cb;
827 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
832 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
836 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
837 "Sending DISCONNECT message to peer `%4s': %i\n",
838 GNUNET_i2s (target), result);
844 send_disconnect (struct NeighbourMapEntry * n)
847 struct SessionDisconnectMessage disconnect_msg;
850 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
851 "Sending DISCONNECT message to peer `%4s'\n",
852 GNUNET_i2s (&n->id));
855 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
856 disconnect_msg.header.type =
857 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
858 disconnect_msg.reserved = htonl (0);
859 disconnect_msg.purpose.size =
860 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
861 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
862 sizeof (struct GNUNET_TIME_AbsoluteNBO));
863 disconnect_msg.purpose.purpose =
864 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
865 disconnect_msg.timestamp =
866 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
867 disconnect_msg.public_key = GST_my_public_key;
868 GNUNET_assert (GNUNET_OK ==
869 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
870 &disconnect_msg.purpose,
871 &disconnect_msg.signature));
873 ret = send_with_session (n,
874 (const char *) &disconnect_msg, sizeof (disconnect_msg),
875 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
876 &send_disconnect_cont, NULL);
878 if (ret == GNUNET_SYSERR)
879 return GNUNET_SYSERR;
881 GNUNET_STATISTICS_update (GST_stats,
883 ("# peers disconnected due to external request"), 1,
890 * Disconnect from the given neighbour, clean up the record.
892 * @param n neighbour to disconnect from
895 disconnect_neighbour (struct NeighbourMapEntry *n)
897 struct MessageQueue *mq;
900 previous_state = n->state;
902 if (is_disconnecting (n))
905 /* send DISCONNECT MESSAGE */
906 if (previous_state == S_CONNECTED)
908 if (GNUNET_OK == send_disconnect (n))
909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
910 GNUNET_i2s (&n->id));
912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
913 "Could not send DISCONNECT_MSG to `%s'\n",
914 GNUNET_i2s (&n->id));
917 change_state (n, S_DISCONNECT);
919 if (previous_state == S_CONNECTED)
921 GNUNET_assert (NULL != n->address);
922 if (n->address_state == USED)
924 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
925 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
926 n->address_state = UNUSED;
930 if (n->address != NULL)
932 struct GNUNET_TRANSPORT_PluginFunctions *papi;
934 papi = GST_plugins_find (n->address->transport_name);
936 papi->disconnect (papi->cls, &n->id);
938 while (NULL != (mq = n->messages_head))
940 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
941 if (NULL != mq->cont)
942 mq->cont (mq->cont_cls, GNUNET_SYSERR);
945 if (NULL != n->is_active)
947 n->is_active->n = NULL;
951 switch (previous_state)
954 GNUNET_assert (neighbours_connected > 0);
955 neighbours_connected--;
956 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
957 GNUNET_SCHEDULER_cancel (n->keepalive_task);
958 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
959 n->expect_latency_response = GNUNET_NO;
960 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
962 disconnect_notify_cb (callback_cls, &n->id);
964 case S_FAST_RECONNECT:
965 GNUNET_STATISTICS_update (GST_stats,
966 gettext_noop ("# fast reconnects failed"), 1,
968 disconnect_notify_cb (callback_cls, &n->id);
974 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
976 GNUNET_assert (GNUNET_YES ==
977 GNUNET_CONTAINER_multihashmap_remove (neighbours,
978 &n->id.hashPubKey, n));
979 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
981 GNUNET_SCHEDULER_cancel (n->ats_suggest);
982 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
984 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
986 GNUNET_SCHEDULER_cancel (n->timeout_task);
987 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
989 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
991 GNUNET_SCHEDULER_cancel (n->transmission_task);
992 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
994 if (NULL != n->address)
996 GNUNET_HELLO_address_free (n->address);
1000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
1001 GNUNET_i2s (&n->id), n);
1007 * Peer has been idle for too long. Disconnect.
1009 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1010 * @param tc scheduler context
1013 neighbour_timeout_task (void *cls,
1014 const struct GNUNET_SCHEDULER_TaskContext *tc)
1016 struct NeighbourMapEntry *n = cls;
1018 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1020 GNUNET_STATISTICS_update (GST_stats,
1022 ("# peers disconnected due to timeout"), 1,
1024 disconnect_neighbour (n);
1029 * Send another keepalive message.
1031 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1032 * @param tc scheduler context
1035 neighbour_keepalive_task (void *cls,
1036 const struct GNUNET_SCHEDULER_TaskContext *tc)
1038 struct NeighbourMapEntry *n = cls;
1039 struct GNUNET_MessageHeader m;
1042 GNUNET_assert (S_CONNECTED == n->state);
1044 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1045 &neighbour_keepalive_task, n);
1047 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1049 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1050 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1052 ret = send_with_session (n,
1053 (const void *) &m, sizeof (m),
1054 UINT32_MAX /* priority */ ,
1055 GNUNET_TIME_UNIT_FOREVER_REL,
1058 n->expect_latency_response = GNUNET_NO;
1059 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero ();
1060 if (ret != GNUNET_SYSERR)
1062 n->expect_latency_response = GNUNET_YES;
1063 n->keep_alive_sent = GNUNET_TIME_absolute_get ();
1070 * Disconnect from the given neighbour.
1073 * @param key hash of neighbour's public key (not used)
1074 * @param value the 'struct NeighbourMapEntry' of the neighbour
1077 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1079 struct NeighbourMapEntry *n = value;
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1083 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1085 if (S_CONNECTED == n->state)
1086 GNUNET_STATISTICS_update (GST_stats,
1088 ("# peers disconnected due to global disconnect"),
1090 disconnect_neighbour (n);
1096 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1098 struct NeighbourMapEntry *n = cls;
1100 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1102 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1103 "ATS did not suggested address to connect to peer `%s'\n",
1104 GNUNET_i2s (&n->id));
1106 disconnect_neighbour (n);
1110 * Cleanup the neighbours subsystem.
1113 GST_neighbours_stop ()
1115 // This can happen during shutdown
1116 if (neighbours == NULL)
1121 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1123 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1124 // GNUNET_assert (neighbours_connected == 0);
1126 callback_cls = NULL;
1127 connect_notify_cb = NULL;
1128 disconnect_notify_cb = NULL;
1129 address_change_cb = NULL;
1132 struct ContinutionContext
1134 struct GNUNET_HELLO_Address *address;
1136 struct Session *session;
1140 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
1141 struct GNUNET_BANDWIDTH_Value32NBO quota)
1143 struct QuotaSetMessage q_msg;
1146 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1147 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1148 ntohl (quota.value__), GNUNET_i2s (target));
1150 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1151 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1152 q_msg.quota = quota;
1153 q_msg.peer = (*target);
1154 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1158 * We tried to send a SESSION_CONNECT message to another peer. If this
1159 * succeeded, we change the state. If it failed, we should tell
1160 * ATS to not use this address anymore (until it is re-validated).
1162 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1163 * @param target peer to send the message to
1164 * @param success GNUNET_OK on success
1167 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1170 struct ContinutionContext *cc = cls;
1171 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1173 if (GNUNET_YES != success)
1175 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1176 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1178 if ((NULL == neighbours) || (NULL == n) || (n->state == S_DISCONNECT))
1180 GNUNET_HELLO_address_free (cc->address);
1185 if ((GNUNET_YES == success) &&
1186 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1188 change_state (n, S_CONNECT_SENT);
1189 GNUNET_HELLO_address_free (cc->address);
1194 if ((GNUNET_NO == success) &&
1195 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1198 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1199 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1200 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1202 change_state (n, S_NOT_CONNECTED);
1203 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1204 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1206 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1208 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1210 GNUNET_HELLO_address_free (cc->address);
1216 * We tried to switch addresses with an peer already connected. If it failed,
1217 * we should tell ATS to not use this address anymore (until it is re-validated).
1219 * @param cls the 'struct NeighbourMapEntry'
1220 * @param target peer to send the message to
1221 * @param success GNUNET_OK on success
1224 send_switch_address_continuation (void *cls,
1225 const struct GNUNET_PeerIdentity *target,
1228 struct ContinutionContext *cc = cls;
1229 struct NeighbourMapEntry *n;
1231 if (neighbours == NULL)
1233 GNUNET_HELLO_address_free (cc->address);
1235 return; /* neighbour is going away */
1238 n = lookup_neighbour (&cc->address->peer);
1239 if ((n == NULL) || (is_disconnecting (n)))
1241 GNUNET_HELLO_address_free (cc->address);
1243 return; /* neighbour is going away */
1246 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1247 if (GNUNET_YES != success)
1250 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1251 "Failed to switch connected peer `%s' to address '%s' session %X, asking ATS for new address \n",
1252 GNUNET_i2s (&n->id), GST_plugins_a2s (cc->address), cc->session);
1254 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1255 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1257 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1258 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1260 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1262 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1263 GNUNET_HELLO_address_free (cc->address);
1267 /* Tell ATS that switching addresses was successful */
1271 if (n->address_state == FRESH)
1273 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES);
1274 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1275 if (cc->session != n->session)
1277 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1278 n->address_state = USED;
1281 case S_FAST_RECONNECT:
1283 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1284 "Successful fast reconnect to peer `%s'\n",
1285 GNUNET_i2s (&n->id));
1287 change_state (n, S_CONNECTED);
1288 neighbours_connected++;
1289 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1292 if (n->address_state == FRESH)
1294 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES);
1295 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1296 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1297 n->address_state = USED;
1300 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1302 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1304 /* Updating quotas */
1305 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1306 send_outbound_quota (target, n->bandwidth_out);
1311 GNUNET_HELLO_address_free (cc->address);
1317 * We tried to send a SESSION_CONNECT message to another peer. If this
1318 * succeeded, we change the state. If it failed, we should tell
1319 * ATS to not use this address anymore (until it is re-validated).
1321 * @param cls the 'struct NeighbourMapEntry'
1322 * @param target peer to send the message to
1323 * @param success GNUNET_OK on success
1326 send_connect_ack_continuation (void *cls,
1327 const struct GNUNET_PeerIdentity *target,
1330 struct ContinutionContext *cc = cls;
1331 struct NeighbourMapEntry *n;
1333 if (neighbours == NULL)
1335 GNUNET_HELLO_address_free (cc->address);
1337 return; /* neighbour is going away */
1340 n = lookup_neighbour (&cc->address->peer);
1341 if ((n == NULL) || (is_disconnecting (n)))
1343 GNUNET_HELLO_address_free (cc->address);
1345 return; /* neighbour is going away */
1348 if (GNUNET_YES == success)
1350 GNUNET_HELLO_address_free (cc->address);
1352 return; /* sending successful */
1355 /* sending failed, ask for next address */
1357 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1358 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1359 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1361 change_state (n, S_NOT_CONNECTED);
1362 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1363 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1365 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1366 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1368 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1370 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1371 GNUNET_HELLO_address_free (cc->address);
1377 * For an existing neighbour record, set the active connection to
1378 * use the given address.
1380 * @param peer identity of the peer to switch the address for
1381 * @param address address of the other peer, NULL if other peer
1383 * @param session session to use (or NULL)
1384 * @param ats performance data
1385 * @param ats_count number of entries in ats
1386 * @param bandwidth_in inbound quota to be used when connection is up
1387 * @param bandwidth_out outbound quota to be used when connection is up
1388 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1389 * connection is not up (yet)
1392 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1393 const struct GNUNET_HELLO_Address
1395 struct Session *session,
1396 const struct GNUNET_ATS_Information *ats,
1398 struct GNUNET_BANDWIDTH_Value32NBO
1400 struct GNUNET_BANDWIDTH_Value32NBO
1403 struct NeighbourMapEntry *n;
1404 struct SessionConnectMessage connect_msg;
1405 struct ContinutionContext *cc;
1409 if (neighbours == NULL)
1411 /* This can happen during shutdown */
1414 n = lookup_neighbour (peer);
1417 if (n->state == S_DISCONNECT)
1419 /* We are disconnecting, nothing to do here */
1422 GNUNET_assert (address->transport_name != NULL);
1423 if ((session == NULL) && (0 == address->address_length))
1425 GNUNET_break_op (0);
1426 /* FIXME: is this actually possible? When does this happen? */
1427 if (strlen (address->transport_name) > 0)
1428 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1429 GNUNET_ATS_suggest_address (GST_ats, peer);
1433 /* checks successful and neighbour != NULL */
1434 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1435 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1436 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
1439 print_state (n->state));
1441 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1443 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1444 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1446 /* do not switch addresses just update quotas */
1448 if (n->state == S_FAST_RECONNECT)
1450 if (0 == GNUNET_HELLO_address_cmp(address, n->address))
1452 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1453 "FAST RECONNECT to peer `%s' and address '%s' with identical ADDRESS\n",
1454 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1458 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1459 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1460 (n->session == session))
1462 n->bandwidth_in = bandwidth_in;
1463 n->bandwidth_out = bandwidth_out;
1464 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1465 send_outbound_quota (peer, n->bandwidth_out);
1468 if (n->state == S_CONNECTED)
1470 /* mark old address as no longer used */
1471 GNUNET_assert (NULL != n->address);
1472 if (n->address_state == USED)
1474 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
1475 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1476 n->address_state = UNUSED;
1480 /* set new address */
1481 if (NULL != n->address)
1482 GNUNET_HELLO_address_free (n->address);
1483 n->address = GNUNET_HELLO_address_copy (address);
1484 n->address_state = FRESH;
1485 n->bandwidth_in = bandwidth_in;
1486 n->bandwidth_out = bandwidth_out;
1487 GNUNET_SCHEDULER_cancel (n->timeout_task);
1489 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1490 &neighbour_timeout_task, n);
1492 if (NULL != address_change_cb && n->state == S_CONNECTED)
1493 address_change_cb (callback_cls, &n->id, n->address);
1495 /* Obtain an session for this address from plugin */
1496 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1497 papi = GST_plugins_find (address->transport_name);
1501 /* we don't have the plugin for this address */
1502 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1504 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1505 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1506 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1509 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1510 GNUNET_HELLO_address_free (n->address);
1516 if (session == NULL)
1518 n->session = papi->get_session (papi->cls, address);
1519 /* Session could not be initiated */
1520 if (n->session == NULL)
1522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1523 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1524 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1526 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1528 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1529 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1530 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1533 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1534 GNUNET_HELLO_address_free (n->address);
1540 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1541 "Obtained new session %p for peer `%s' and address '%s'\n",
1542 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1543 /* Telling ATS about new session */
1544 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1548 n->session = session;
1549 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1550 "Using existing session %p for peer `%s' and address '%s'\n",
1552 GNUNET_i2s (&n->id),
1553 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>");
1558 case S_NOT_CONNECTED:
1559 case S_CONNECT_SENT:
1560 msg_len = sizeof (struct SessionConnectMessage);
1561 connect_msg.header.size = htons (msg_len);
1562 connect_msg.header.type =
1563 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1564 connect_msg.reserved = htonl (0);
1565 connect_msg.timestamp =
1566 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1568 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1569 cc->session = n->session;
1570 cc->address = GNUNET_HELLO_address_copy (address);
1572 ret = send_with_session (n,
1573 (const char *) &connect_msg, msg_len,
1574 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1575 &send_connect_continuation, cc);
1578 case S_CONNECT_RECV:
1579 /* We received a CONNECT message and asked ATS for an address */
1580 msg_len = sizeof (struct SessionConnectMessage);
1581 connect_msg.header.size = htons (msg_len);
1582 connect_msg.header.type =
1583 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1584 connect_msg.reserved = htonl (0);
1585 connect_msg.timestamp =
1586 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1587 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1588 cc->session = n->session;
1589 cc->address = GNUNET_HELLO_address_copy (address);
1591 ret = send_with_session(n,
1592 (const void *) &connect_msg, msg_len,
1593 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1594 &send_connect_ack_continuation,
1598 case S_FAST_RECONNECT:
1599 /* connected peer is switching addresses or tries fast reconnect */
1600 msg_len = sizeof (struct SessionConnectMessage);
1601 connect_msg.header.size = htons (msg_len);
1602 connect_msg.header.type =
1603 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1604 connect_msg.reserved = htonl (0);
1605 connect_msg.timestamp =
1606 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1607 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1608 cc->session = n->session;
1609 cc->address = GNUNET_HELLO_address_copy (address);
1610 ret = send_with_session(n,
1611 (const void *) &connect_msg, msg_len,
1612 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1613 &send_switch_address_continuation, cc);
1614 if (ret == GNUNET_SYSERR)
1616 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1617 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1618 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1622 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1623 "Invalid connection state to switch addresses %u \n", n->state);
1624 GNUNET_break_op (0);
1631 * Obtain current latency information for the given neighbour.
1634 * @return observed latency of the address, FOREVER if the address was
1635 * never successfully validated
1637 struct GNUNET_TIME_Relative
1638 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1640 struct NeighbourMapEntry *n;
1642 n = lookup_neighbour (peer);
1643 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1644 return GNUNET_TIME_UNIT_FOREVER_REL;
1650 * Obtain current address information for the given neighbour.
1653 * @return address currently used
1655 struct GNUNET_HELLO_Address *
1656 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1658 struct NeighbourMapEntry *n;
1660 n = lookup_neighbour (peer);
1661 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1670 * Create an entry in the neighbour map for the given peer
1672 * @param peer peer to create an entry for
1673 * @return new neighbour map entry
1675 static struct NeighbourMapEntry *
1676 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1678 struct NeighbourMapEntry *n;
1681 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1682 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1684 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1686 n->state = S_NOT_CONNECTED;
1687 n->latency = GNUNET_TIME_relative_get_forever ();
1688 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1689 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1690 MAX_BANDWIDTH_CARRY_S);
1692 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1693 &neighbour_timeout_task, n);
1694 GNUNET_assert (GNUNET_OK ==
1695 GNUNET_CONTAINER_multihashmap_put (neighbours,
1696 &n->id.hashPubKey, n,
1697 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1703 * Try to create a connection to the given target (eventually).
1705 * @param target peer to try to connect to
1708 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1710 struct NeighbourMapEntry *n;
1712 // This can happen during shutdown
1713 if (neighbours == NULL)
1718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1719 GNUNET_i2s (target));
1722 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1727 n = lookup_neighbour (target);
1731 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1732 return; /* already connecting or connected */
1733 if (is_disconnecting (n))
1734 change_state (n, S_NOT_CONNECTED);
1739 n = setup_neighbour (target);
1741 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1742 "Asking ATS for suggested address to connect to peer `%s'\n",
1743 GNUNET_i2s (&n->id));
1746 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1750 * Test if we're connected to the given peer.
1752 * @param target peer to test
1753 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1756 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1758 struct NeighbourMapEntry *n;
1760 // This can happen during shutdown
1761 if (neighbours == NULL)
1766 n = lookup_neighbour (target);
1768 if ((NULL == n) || (S_CONNECTED != n->state))
1769 return GNUNET_NO; /* not connected */
1774 * A session was terminated. Take note.
1776 * @param peer identity of the peer where the session died
1777 * @param session session that is gone
1780 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1781 struct Session *session)
1783 struct NeighbourMapEntry *n;
1785 if (neighbours == NULL)
1787 /* This can happen during shutdown */
1792 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1793 session, GNUNET_i2s (peer));
1796 n = lookup_neighbour (peer);
1799 if (session != n->session)
1800 return; /* doesn't affect us */
1801 if (n->state == S_CONNECTED)
1803 if (n->address_state == USED)
1805 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
1806 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1807 n->address_state = UNUSED;
1811 if (NULL != n->address)
1813 GNUNET_HELLO_address_free (n->address);
1818 /* not connected anymore anyway, shouldn't matter */
1819 if (S_CONNECTED != n->state)
1822 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1824 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1825 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1826 n->expect_latency_response = GNUNET_NO;
1829 /* connected, try fast reconnect */
1830 /* statistics "transport" : "# peers connected" -= 1
1831 * neighbours_connected -= 1
1832 * BUT: no disconnect_cb to notify clients about disconnect
1835 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1838 GNUNET_assert (neighbours_connected > 0);
1839 change_state (n, S_FAST_RECONNECT);
1840 neighbours_connected--;
1841 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
1845 /* We are connected, so ask ATS to switch addresses */
1846 GNUNET_SCHEDULER_cancel (n->timeout_task);
1847 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1848 &neighbour_timeout_task, n);
1849 /* try QUICKLY to re-establish a connection, reduce timeout! */
1850 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1851 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1852 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1853 &ats_suggest_cancel,
1855 GNUNET_ATS_suggest_address (GST_ats, peer);
1860 * Transmit a message to the given target using the active connection.
1862 * @param target destination
1863 * @param msg message to send
1864 * @param msg_size number of bytes in msg
1865 * @param timeout when to fail with timeout
1866 * @param cont function to call when done
1867 * @param cont_cls closure for 'cont'
1870 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1871 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1872 GST_NeighbourSendContinuation cont, void *cont_cls)
1874 struct NeighbourMapEntry *n;
1875 struct MessageQueue *mq;
1877 // This can happen during shutdown
1878 if (neighbours == NULL)
1883 n = lookup_neighbour (target);
1884 if ((n == NULL) || (!is_connected (n)))
1886 GNUNET_STATISTICS_update (GST_stats,
1888 ("# messages not sent (no such peer or not connected)"),
1892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1893 "Could not send message to peer `%s': unknown neighbour",
1894 GNUNET_i2s (target));
1895 else if (!is_connected (n))
1896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897 "Could not send message to peer `%s': not connected\n",
1898 GNUNET_i2s (target));
1901 cont (cont_cls, GNUNET_SYSERR);
1905 if ((n->session == NULL) && (n->address == NULL))
1907 GNUNET_STATISTICS_update (GST_stats,
1909 ("# messages not sent (no such peer or not connected)"),
1912 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1913 "Could not send message to peer `%s': no address available\n",
1914 GNUNET_i2s (target));
1918 cont (cont_cls, GNUNET_SYSERR);
1922 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1923 GNUNET_STATISTICS_update (GST_stats,
1925 ("# bytes in message queue for other peers"),
1926 msg_size, GNUNET_NO);
1927 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1929 mq->cont_cls = cont_cls;
1930 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1931 memcpy (&mq[1], msg, msg_size);
1932 mq->message_buf = (const char *) &mq[1];
1933 mq->message_buf_size = msg_size;
1934 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1935 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1937 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1938 (NULL == n->is_active))
1939 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1944 * We have received a message from the given sender. How long should
1945 * we delay before receiving more? (Also used to keep the peer marked
1948 * @param sender sender of the message
1949 * @param size size of the message
1950 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1951 * GNUNET_NO if the neighbour is not connected or violates the quota,
1952 * GNUNET_SYSERR if the connection is not fully up yet
1953 * @return how long to wait before reading more from this sender
1955 struct GNUNET_TIME_Relative
1956 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1957 *sender, ssize_t size, int *do_forward)
1959 struct NeighbourMapEntry *n;
1960 struct GNUNET_TIME_Relative ret;
1962 // This can happen during shutdown
1963 if (neighbours == NULL)
1965 return GNUNET_TIME_UNIT_FOREVER_REL;
1968 n = lookup_neighbour (sender);
1971 GST_neighbours_try_connect (sender);
1972 n = lookup_neighbour (sender);
1975 GNUNET_STATISTICS_update (GST_stats,
1977 ("# messages discarded due to lack of neighbour record"),
1979 *do_forward = GNUNET_NO;
1980 return GNUNET_TIME_UNIT_ZERO;
1983 if (!is_connected (n))
1985 *do_forward = GNUNET_SYSERR;
1986 return GNUNET_TIME_UNIT_ZERO;
1988 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1990 n->quota_violation_count++;
1992 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1993 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1994 n->in_tracker.available_bytes_per_s__,
1995 n->quota_violation_count);
1997 /* Discount 32k per violation */
1998 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
2002 if (n->quota_violation_count > 0)
2004 /* try to add 32k back */
2005 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
2006 n->quota_violation_count--;
2009 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2011 GNUNET_STATISTICS_update (GST_stats,
2013 ("# bandwidth quota violations by other peers"),
2015 *do_forward = GNUNET_NO;
2016 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
2018 *do_forward = GNUNET_YES;
2019 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
2020 if (ret.rel_value > 0)
2023 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2024 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
2025 (unsigned long long) n->in_tracker.
2026 consumption_since_last_update__,
2027 (unsigned int) n->in_tracker.available_bytes_per_s__,
2028 (unsigned long long) ret.rel_value);
2030 GNUNET_STATISTICS_update (GST_stats,
2031 gettext_noop ("# ms throttling suggested"),
2032 (int64_t) ret.rel_value, GNUNET_NO);
2039 * Keep the connection to the given neighbour alive longer,
2040 * we received a KEEPALIVE (or equivalent).
2042 * @param neighbour neighbour to keep alive
2045 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
2047 struct NeighbourMapEntry *n;
2049 // This can happen during shutdown
2050 if (neighbours == NULL)
2055 n = lookup_neighbour (neighbour);
2058 GNUNET_STATISTICS_update (GST_stats,
2060 ("# KEEPALIVE messages discarded (not connected)"),
2064 GNUNET_SCHEDULER_cancel (n->timeout_task);
2066 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2067 &neighbour_timeout_task, n);
2069 /* send reply to measure latency */
2070 if (S_CONNECTED != n->state)
2073 struct GNUNET_MessageHeader m;
2075 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2076 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2078 send_with_session(n,
2079 (const void *) &m, sizeof (m),
2081 GNUNET_TIME_UNIT_FOREVER_REL,
2086 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2089 * @param neighbour neighbour to keep alive
2090 * @param ats performance data
2091 * @param ats_count number of entries in ats
2094 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2095 const struct GNUNET_ATS_Information *ats,
2098 struct NeighbourMapEntry *n;
2099 struct GNUNET_ATS_Information *ats_new;
2102 if (neighbours == NULL)
2104 // This can happen during shutdown
2108 n = lookup_neighbour (neighbour);
2109 if ((NULL == n) || (n->state != S_CONNECTED))
2111 GNUNET_STATISTICS_update (GST_stats,
2113 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2117 if (n->expect_latency_response != GNUNET_YES)
2119 GNUNET_STATISTICS_update (GST_stats,
2121 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2125 n->expect_latency_response = GNUNET_NO;
2127 GNUNET_assert (n->keep_alive_sent.abs_value !=
2128 GNUNET_TIME_absolute_get_zero ().abs_value);
2130 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2131 GNUNET_TIME_absolute_get ());
2133 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2134 GNUNET_i2s (&n->id), n->latency.rel_value);
2138 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2140 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2145 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2147 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2150 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2151 if (n->latency.rel_value > UINT32_MAX)
2152 latency = UINT32_MAX;
2154 latency = n->latency.rel_value;
2155 ats_new[ats_count].value = htonl (latency);
2157 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2159 GNUNET_free (ats_new);
2165 * Change the incoming quota for the given peer.
2167 * @param neighbour identity of peer to change qutoa for
2168 * @param quota new quota
2171 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2172 struct GNUNET_BANDWIDTH_Value32NBO quota)
2174 struct NeighbourMapEntry *n;
2176 // This can happen during shutdown
2177 if (neighbours == NULL)
2182 n = lookup_neighbour (neighbour);
2185 GNUNET_STATISTICS_update (GST_stats,
2187 ("# SET QUOTA messages ignored (no such peer)"),
2192 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2193 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2194 ntohl (quota.value__), GNUNET_i2s (&n->id));
2196 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2197 if (0 != ntohl (quota.value__))
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2201 GNUNET_i2s (&n->id), "SET_QUOTA");
2203 if (is_connected (n))
2204 GNUNET_STATISTICS_update (GST_stats,
2205 gettext_noop ("# disconnects due to quota of 0"),
2207 disconnect_neighbour (n);
2212 * Closure for the neighbours_iterate function.
2214 struct IteratorContext
2217 * Function to call on each connected neighbour.
2219 GST_NeighbourIterator cb;
2229 * Call the callback from the closure for each connected neighbour.
2231 * @param cls the 'struct IteratorContext'
2232 * @param key the hash of the public key of the neighbour
2233 * @param value the 'struct NeighbourMapEntry'
2234 * @return GNUNET_OK (continue to iterate)
2237 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2239 struct IteratorContext *ic = cls;
2240 struct NeighbourMapEntry *n = value;
2242 if (!is_connected (n))
2245 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2251 * Iterate over all connected neighbours.
2253 * @param cb function to call
2254 * @param cb_cls closure for cb
2257 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2259 struct IteratorContext ic;
2261 // This can happen during shutdown
2262 if (neighbours == NULL)
2269 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2273 * If we have an active connection to the given target, it must be shutdown.
2275 * @param target peer to disconnect from
2278 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2280 struct NeighbourMapEntry *n;
2282 // This can happen during shutdown
2283 if (neighbours == NULL)
2288 n = lookup_neighbour (target);
2290 return; /* not active */
2291 disconnect_neighbour (n);
2296 * We received a disconnect message from the given peer,
2297 * validate and process.
2299 * @param peer sender of the message
2300 * @param msg the disconnect message
2303 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2305 const struct GNUNET_MessageHeader
2308 struct NeighbourMapEntry *n;
2309 const struct SessionDisconnectMessage *sdm;
2313 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2314 "Received DISCONNECT message from peer `%s'\n",
2318 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2320 // GNUNET_break_op (0);
2321 GNUNET_STATISTICS_update (GST_stats,
2323 ("# disconnect messages ignored (old format)"), 1,
2327 sdm = (const struct SessionDisconnectMessage *) msg;
2328 n = lookup_neighbour (peer);
2330 return; /* gone already */
2331 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2332 n->connect_ts.abs_value)
2334 GNUNET_STATISTICS_update (GST_stats,
2336 ("# disconnect messages ignored (timestamp)"), 1,
2340 GNUNET_CRYPTO_hash (&sdm->public_key,
2341 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2343 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2345 GNUNET_break_op (0);
2348 if (ntohl (sdm->purpose.size) !=
2349 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2350 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2351 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2353 GNUNET_break_op (0);
2357 GNUNET_CRYPTO_rsa_verify
2358 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2359 &sdm->signature, &sdm->public_key))
2361 GNUNET_break_op (0);
2364 GST_neighbours_force_disconnect (peer);
2369 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2370 * Consider switching to it.
2372 * @param message possibly a 'struct SessionConnectMessage' (check format)
2373 * @param peer identity of the peer to switch the address for
2374 * @param address address of the other peer, NULL if other peer
2376 * @param session session to use (or NULL)
2377 * @param ats performance data
2378 * @param ats_count number of entries in ats
2381 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2382 const struct GNUNET_PeerIdentity *peer,
2383 const struct GNUNET_HELLO_Address *address,
2384 struct Session *session,
2385 const struct GNUNET_ATS_Information *ats,
2388 const struct SessionConnectMessage *scm;
2389 struct GNUNET_MessageHeader msg;
2390 struct NeighbourMapEntry *n;
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2395 "Received CONNECT_ACK message from peer `%s'\n",
2398 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2400 GNUNET_break_op (0);
2403 scm = (const struct SessionConnectMessage *) message;
2404 GNUNET_break_op (ntohl (scm->reserved) == 0);
2405 n = lookup_neighbour (peer);
2408 /* we did not send 'CONNECT' -- at least not recently */
2409 GNUNET_STATISTICS_update (GST_stats,
2411 ("# unexpected CONNECT_ACK messages (no peer)"),
2418 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2420 * We also received an CONNECT message, switched from SENDT to RECV and
2421 * ATS already suggested us an address after a successful blacklist check
2424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2425 "Received CONNECT_ACK message from peer `%s' in state `%s'\n",
2427 print_state(n->state));
2429 if ((n->address != NULL) && (n->state == S_CONNECTED))
2431 /* After fast reconnect: send ACK (ACK) even when we are connected */
2432 msg_len = sizeof (msg);
2433 msg.size = htons (msg_len);
2434 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2436 ret = send_with_session(n,
2437 (const char *) &msg, msg_len,
2438 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2441 if (ret == GNUNET_SYSERR)
2442 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2443 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2444 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2448 if ((n->state != S_CONNECT_SENT) &&
2449 ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2451 GNUNET_STATISTICS_update (GST_stats,
2453 ("# unexpected CONNECT_ACK messages"), 1,
2457 if (n->state != S_CONNECTED)
2458 change_state (n, S_CONNECTED);
2460 if (NULL != session)
2462 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2464 "Giving ATS session %p of plugin %s for peer %s\n",
2465 session, address->transport_name, GNUNET_i2s (peer));
2467 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2468 GNUNET_assert (NULL != n->address);
2470 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2472 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2473 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2474 n->address_state = USED;
2477 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2479 /* send ACK (ACK) */
2480 msg_len = sizeof (msg);
2481 msg.size = htons (msg_len);
2482 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2484 ret = send_with_session(n,
2485 (const char *) &msg, msg_len,
2486 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2489 if (ret == GNUNET_SYSERR)
2490 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2491 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2492 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2495 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2496 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2498 neighbours_connected++;
2499 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2504 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2507 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2508 send_outbound_quota (peer, n->bandwidth_out);
2514 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2515 const struct GNUNET_PeerIdentity *peer,
2516 const struct GNUNET_HELLO_Address *address,
2517 struct Session *session,
2518 const struct GNUNET_ATS_Information *ats,
2521 struct NeighbourMapEntry *n;
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2528 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2530 GNUNET_break_op (0);
2533 n = lookup_neighbour (peer);
2539 if (S_CONNECTED == n->state)
2541 if (!is_connecting (n))
2543 GNUNET_STATISTICS_update (GST_stats,
2544 gettext_noop ("# unexpected ACK messages"), 1,
2548 change_state (n, S_CONNECTED);
2549 if (NULL != session)
2550 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2552 "Giving ATS session %p of plugin %s for peer %s\n",
2553 session, address->transport_name, GNUNET_i2s (peer));
2554 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2555 GNUNET_assert (n->address != NULL);
2557 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2559 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2560 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2561 n->address_state = USED;
2565 neighbours_connected++;
2566 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2569 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2570 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2571 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2573 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2574 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2575 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2578 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2579 send_outbound_quota (peer, n->bandwidth_out);
2582 struct BlackListCheckContext
2584 struct GNUNET_ATS_Information *ats;
2588 struct Session *session;
2590 struct GNUNET_HELLO_Address *address;
2592 struct GNUNET_TIME_Absolute ts;
2597 handle_connect_blacklist_cont (void *cls,
2598 const struct GNUNET_PeerIdentity *peer,
2601 struct NeighbourMapEntry *n;
2602 struct BlackListCheckContext *bcc = cls;
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2606 "Blacklist check due to CONNECT message: `%s'\n",
2608 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2612 if (GNUNET_OK != result)
2614 GNUNET_HELLO_address_free (bcc->address);
2619 n = lookup_neighbour (peer);
2621 n = setup_neighbour (peer);
2623 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2625 if (NULL != bcc->session)
2626 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2628 "Giving ATS session %p of address `%s' for peer %s\n",
2629 bcc->session, GST_plugins_a2s (bcc->address),
2631 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2633 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2635 n->connect_ts = bcc->ts;
2638 GNUNET_HELLO_address_free (bcc->address);
2641 if (n->state != S_CONNECT_RECV)
2642 change_state (n, S_CONNECT_RECV);
2645 /* Ask ATS for an address to connect via that address */
2646 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2647 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2649 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2651 GNUNET_ATS_suggest_address (GST_ats, peer);
2655 * We received a 'SESSION_CONNECT' message from the other peer.
2656 * Consider switching to it.
2658 * @param message possibly a 'struct SessionConnectMessage' (check format)
2659 * @param peer identity of the peer to switch the address for
2660 * @param address address of the other peer, NULL if other peer
2662 * @param session session to use (or NULL)
2663 * @param ats performance data
2664 * @param ats_count number of entries in ats (excluding 0-termination)
2667 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2668 const struct GNUNET_PeerIdentity *peer,
2669 const struct GNUNET_HELLO_Address *address,
2670 struct Session *session,
2671 const struct GNUNET_ATS_Information *ats,
2674 const struct SessionConnectMessage *scm;
2675 struct BlackListCheckContext *bcc = NULL;
2676 struct NeighbourMapEntry *n;
2679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2680 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2683 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2685 GNUNET_break_op (0);
2689 scm = (const struct SessionConnectMessage *) message;
2690 GNUNET_break_op (ntohl (scm->reserved) == 0);
2692 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2694 n = lookup_neighbour (peer);
2695 if ((n != NULL) && ((S_CONNECTED == n->state) || (S_FAST_RECONNECT == n->state)))
2697 /* connected peer switches addresses or is trying to do a fast reconnect*/
2702 /* we are not connected to this peer */
2703 /* do blacklist check */
2705 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2706 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2707 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2708 bcc->ats_count = ats_count + 1;
2709 bcc->address = GNUNET_HELLO_address_copy (address);
2710 bcc->session = session;
2711 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2712 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2713 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2714 bcc->ats[ats_count].value =
2715 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2716 GST_blacklist_test_allowed (peer, address->transport_name,
2717 &handle_connect_blacklist_cont, bcc);
2721 /* end of file gnunet-service-transport_neighbours.c */