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);
1498 GNUNET_assert (papi != NULL);
1499 if (session == NULL)
1501 n->session = papi->get_session (papi->cls, address);
1502 /* Session could not be initiated */
1503 if (n->session == NULL)
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1506 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1507 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1509 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1511 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1512 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1513 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1516 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1517 GNUNET_HELLO_address_free (n->address);
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "Obtained new session %p for peer `%s' and address '%s'\n",
1525 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1526 /* Telling ATS about new session */
1527 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1531 n->session = session;
1532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1533 "Using existing session %p for peer `%s' and address '%s'\n",
1535 GNUNET_i2s (&n->id),
1536 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>");
1541 case S_NOT_CONNECTED:
1542 case S_CONNECT_SENT:
1543 msg_len = sizeof (struct SessionConnectMessage);
1544 connect_msg.header.size = htons (msg_len);
1545 connect_msg.header.type =
1546 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1547 connect_msg.reserved = htonl (0);
1548 connect_msg.timestamp =
1549 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1551 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1552 cc->session = n->session;
1553 cc->address = GNUNET_HELLO_address_copy (address);
1555 ret = send_with_session (n,
1556 (const char *) &connect_msg, msg_len,
1557 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1558 &send_connect_continuation, cc);
1561 case S_CONNECT_RECV:
1562 /* We received a CONNECT message and asked ATS for an address */
1563 msg_len = sizeof (struct SessionConnectMessage);
1564 connect_msg.header.size = htons (msg_len);
1565 connect_msg.header.type =
1566 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1567 connect_msg.reserved = htonl (0);
1568 connect_msg.timestamp =
1569 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1570 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1571 cc->session = n->session;
1572 cc->address = GNUNET_HELLO_address_copy (address);
1574 ret = send_with_session(n,
1575 (const void *) &connect_msg, msg_len,
1576 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1577 &send_connect_ack_continuation,
1581 case S_FAST_RECONNECT:
1582 /* connected peer is switching addresses or tries fast reconnect */
1583 msg_len = sizeof (struct SessionConnectMessage);
1584 connect_msg.header.size = htons (msg_len);
1585 connect_msg.header.type =
1586 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1587 connect_msg.reserved = htonl (0);
1588 connect_msg.timestamp =
1589 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1590 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1591 cc->session = n->session;
1592 cc->address = GNUNET_HELLO_address_copy (address);
1593 ret = send_with_session(n,
1594 (const void *) &connect_msg, msg_len,
1595 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1596 &send_switch_address_continuation, cc);
1597 if (ret == GNUNET_SYSERR)
1599 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1600 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1601 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1605 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1606 "Invalid connection state to switch addresses %u \n", n->state);
1607 GNUNET_break_op (0);
1614 * Obtain current latency information for the given neighbour.
1617 * @return observed latency of the address, FOREVER if the address was
1618 * never successfully validated
1620 struct GNUNET_TIME_Relative
1621 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1623 struct NeighbourMapEntry *n;
1625 n = lookup_neighbour (peer);
1626 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1627 return GNUNET_TIME_UNIT_FOREVER_REL;
1633 * Obtain current address information for the given neighbour.
1636 * @return address currently used
1638 struct GNUNET_HELLO_Address *
1639 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1641 struct NeighbourMapEntry *n;
1643 n = lookup_neighbour (peer);
1644 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1653 * Create an entry in the neighbour map for the given peer
1655 * @param peer peer to create an entry for
1656 * @return new neighbour map entry
1658 static struct NeighbourMapEntry *
1659 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1661 struct NeighbourMapEntry *n;
1664 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1665 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1667 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1669 n->state = S_NOT_CONNECTED;
1670 n->latency = GNUNET_TIME_relative_get_forever ();
1671 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1672 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1673 MAX_BANDWIDTH_CARRY_S);
1675 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1676 &neighbour_timeout_task, n);
1677 GNUNET_assert (GNUNET_OK ==
1678 GNUNET_CONTAINER_multihashmap_put (neighbours,
1679 &n->id.hashPubKey, n,
1680 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1686 * Try to create a connection to the given target (eventually).
1688 * @param target peer to try to connect to
1691 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1693 struct NeighbourMapEntry *n;
1695 // This can happen during shutdown
1696 if (neighbours == NULL)
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1702 GNUNET_i2s (target));
1705 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1710 n = lookup_neighbour (target);
1714 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1715 return; /* already connecting or connected */
1716 if (is_disconnecting (n))
1717 change_state (n, S_NOT_CONNECTED);
1722 n = setup_neighbour (target);
1724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1725 "Asking ATS for suggested address to connect to peer `%s'\n",
1726 GNUNET_i2s (&n->id));
1729 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1733 * Test if we're connected to the given peer.
1735 * @param target peer to test
1736 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1739 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1741 struct NeighbourMapEntry *n;
1743 // This can happen during shutdown
1744 if (neighbours == NULL)
1749 n = lookup_neighbour (target);
1751 if ((NULL == n) || (S_CONNECTED != n->state))
1752 return GNUNET_NO; /* not connected */
1757 * A session was terminated. Take note.
1759 * @param peer identity of the peer where the session died
1760 * @param session session that is gone
1763 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1764 struct Session *session)
1766 struct NeighbourMapEntry *n;
1768 if (neighbours == NULL)
1770 /* This can happen during shutdown */
1775 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1776 session, GNUNET_i2s (peer));
1779 n = lookup_neighbour (peer);
1782 if (session != n->session)
1783 return; /* doesn't affect us */
1784 if (n->state == S_CONNECTED)
1786 if (n->address_state == USED)
1788 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
1789 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1790 n->address_state = UNUSED;
1794 if (NULL != n->address)
1796 GNUNET_HELLO_address_free (n->address);
1801 /* not connected anymore anyway, shouldn't matter */
1802 if (S_CONNECTED != n->state)
1805 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1807 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1808 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1809 n->expect_latency_response = GNUNET_NO;
1812 /* connected, try fast reconnect */
1813 /* statistics "transport" : "# peers connected" -= 1
1814 * neighbours_connected -= 1
1815 * BUT: no disconnect_cb to notify clients about disconnect
1818 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1821 GNUNET_assert (neighbours_connected > 0);
1822 change_state (n, S_FAST_RECONNECT);
1823 neighbours_connected--;
1824 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
1828 /* We are connected, so ask ATS to switch addresses */
1829 GNUNET_SCHEDULER_cancel (n->timeout_task);
1830 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1831 &neighbour_timeout_task, n);
1832 /* try QUICKLY to re-establish a connection, reduce timeout! */
1833 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1834 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1835 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1836 &ats_suggest_cancel,
1838 GNUNET_ATS_suggest_address (GST_ats, peer);
1843 * Transmit a message to the given target using the active connection.
1845 * @param target destination
1846 * @param msg message to send
1847 * @param msg_size number of bytes in msg
1848 * @param timeout when to fail with timeout
1849 * @param cont function to call when done
1850 * @param cont_cls closure for 'cont'
1853 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1854 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1855 GST_NeighbourSendContinuation cont, void *cont_cls)
1857 struct NeighbourMapEntry *n;
1858 struct MessageQueue *mq;
1860 // This can happen during shutdown
1861 if (neighbours == NULL)
1866 n = lookup_neighbour (target);
1867 if ((n == NULL) || (!is_connected (n)))
1869 GNUNET_STATISTICS_update (GST_stats,
1871 ("# messages not sent (no such peer or not connected)"),
1875 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1876 "Could not send message to peer `%s': unknown neighbour",
1877 GNUNET_i2s (target));
1878 else if (!is_connected (n))
1879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1880 "Could not send message to peer `%s': not connected\n",
1881 GNUNET_i2s (target));
1884 cont (cont_cls, GNUNET_SYSERR);
1888 if ((n->session == NULL) && (n->address == NULL))
1890 GNUNET_STATISTICS_update (GST_stats,
1892 ("# messages not sent (no such peer or not connected)"),
1895 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1896 "Could not send message to peer `%s': no address available\n",
1897 GNUNET_i2s (target));
1901 cont (cont_cls, GNUNET_SYSERR);
1905 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1906 GNUNET_STATISTICS_update (GST_stats,
1908 ("# bytes in message queue for other peers"),
1909 msg_size, GNUNET_NO);
1910 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1912 mq->cont_cls = cont_cls;
1913 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1914 memcpy (&mq[1], msg, msg_size);
1915 mq->message_buf = (const char *) &mq[1];
1916 mq->message_buf_size = msg_size;
1917 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1918 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1920 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1921 (NULL == n->is_active))
1922 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1927 * We have received a message from the given sender. How long should
1928 * we delay before receiving more? (Also used to keep the peer marked
1931 * @param sender sender of the message
1932 * @param size size of the message
1933 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1934 * GNUNET_NO if the neighbour is not connected or violates the quota,
1935 * GNUNET_SYSERR if the connection is not fully up yet
1936 * @return how long to wait before reading more from this sender
1938 struct GNUNET_TIME_Relative
1939 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1940 *sender, ssize_t size, int *do_forward)
1942 struct NeighbourMapEntry *n;
1943 struct GNUNET_TIME_Relative ret;
1945 // This can happen during shutdown
1946 if (neighbours == NULL)
1948 return GNUNET_TIME_UNIT_FOREVER_REL;
1951 n = lookup_neighbour (sender);
1954 GST_neighbours_try_connect (sender);
1955 n = lookup_neighbour (sender);
1958 GNUNET_STATISTICS_update (GST_stats,
1960 ("# messages discarded due to lack of neighbour record"),
1962 *do_forward = GNUNET_NO;
1963 return GNUNET_TIME_UNIT_ZERO;
1966 if (!is_connected (n))
1968 *do_forward = GNUNET_SYSERR;
1969 return GNUNET_TIME_UNIT_ZERO;
1971 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1973 n->quota_violation_count++;
1975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1976 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1977 n->in_tracker.available_bytes_per_s__,
1978 n->quota_violation_count);
1980 /* Discount 32k per violation */
1981 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1985 if (n->quota_violation_count > 0)
1987 /* try to add 32k back */
1988 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1989 n->quota_violation_count--;
1992 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1994 GNUNET_STATISTICS_update (GST_stats,
1996 ("# bandwidth quota violations by other peers"),
1998 *do_forward = GNUNET_NO;
1999 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
2001 *do_forward = GNUNET_YES;
2002 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
2003 if (ret.rel_value > 0)
2006 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2007 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
2008 (unsigned long long) n->in_tracker.
2009 consumption_since_last_update__,
2010 (unsigned int) n->in_tracker.available_bytes_per_s__,
2011 (unsigned long long) ret.rel_value);
2013 GNUNET_STATISTICS_update (GST_stats,
2014 gettext_noop ("# ms throttling suggested"),
2015 (int64_t) ret.rel_value, GNUNET_NO);
2022 * Keep the connection to the given neighbour alive longer,
2023 * we received a KEEPALIVE (or equivalent).
2025 * @param neighbour neighbour to keep alive
2028 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
2030 struct NeighbourMapEntry *n;
2032 // This can happen during shutdown
2033 if (neighbours == NULL)
2038 n = lookup_neighbour (neighbour);
2041 GNUNET_STATISTICS_update (GST_stats,
2043 ("# KEEPALIVE messages discarded (not connected)"),
2047 GNUNET_SCHEDULER_cancel (n->timeout_task);
2049 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2050 &neighbour_timeout_task, n);
2052 /* send reply to measure latency */
2053 if (S_CONNECTED != n->state)
2056 struct GNUNET_MessageHeader m;
2058 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2059 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2061 send_with_session(n,
2062 (const void *) &m, sizeof (m),
2064 GNUNET_TIME_UNIT_FOREVER_REL,
2069 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2072 * @param neighbour neighbour to keep alive
2073 * @param ats performance data
2074 * @param ats_count number of entries in ats
2077 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2078 const struct GNUNET_ATS_Information *ats,
2081 struct NeighbourMapEntry *n;
2082 struct GNUNET_ATS_Information *ats_new;
2085 if (neighbours == NULL)
2087 // This can happen during shutdown
2091 n = lookup_neighbour (neighbour);
2092 if ((NULL == n) || (n->state != S_CONNECTED))
2094 GNUNET_STATISTICS_update (GST_stats,
2096 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2100 if (n->expect_latency_response != GNUNET_YES)
2102 GNUNET_STATISTICS_update (GST_stats,
2104 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2108 n->expect_latency_response = GNUNET_NO;
2110 GNUNET_assert (n->keep_alive_sent.abs_value !=
2111 GNUNET_TIME_absolute_get_zero ().abs_value);
2113 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2114 GNUNET_TIME_absolute_get ());
2116 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2117 GNUNET_i2s (&n->id), n->latency.rel_value);
2121 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2123 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2128 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2130 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2133 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2134 if (n->latency.rel_value > UINT32_MAX)
2135 latency = UINT32_MAX;
2137 latency = n->latency.rel_value;
2138 ats_new[ats_count].value = htonl (latency);
2140 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2142 GNUNET_free (ats_new);
2148 * Change the incoming quota for the given peer.
2150 * @param neighbour identity of peer to change qutoa for
2151 * @param quota new quota
2154 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2155 struct GNUNET_BANDWIDTH_Value32NBO quota)
2157 struct NeighbourMapEntry *n;
2159 // This can happen during shutdown
2160 if (neighbours == NULL)
2165 n = lookup_neighbour (neighbour);
2168 GNUNET_STATISTICS_update (GST_stats,
2170 ("# SET QUOTA messages ignored (no such peer)"),
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2177 ntohl (quota.value__), GNUNET_i2s (&n->id));
2179 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2180 if (0 != ntohl (quota.value__))
2183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2184 GNUNET_i2s (&n->id), "SET_QUOTA");
2186 if (is_connected (n))
2187 GNUNET_STATISTICS_update (GST_stats,
2188 gettext_noop ("# disconnects due to quota of 0"),
2190 disconnect_neighbour (n);
2195 * Closure for the neighbours_iterate function.
2197 struct IteratorContext
2200 * Function to call on each connected neighbour.
2202 GST_NeighbourIterator cb;
2212 * Call the callback from the closure for each connected neighbour.
2214 * @param cls the 'struct IteratorContext'
2215 * @param key the hash of the public key of the neighbour
2216 * @param value the 'struct NeighbourMapEntry'
2217 * @return GNUNET_OK (continue to iterate)
2220 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2222 struct IteratorContext *ic = cls;
2223 struct NeighbourMapEntry *n = value;
2225 if (!is_connected (n))
2228 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2234 * Iterate over all connected neighbours.
2236 * @param cb function to call
2237 * @param cb_cls closure for cb
2240 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2242 struct IteratorContext ic;
2244 // This can happen during shutdown
2245 if (neighbours == NULL)
2252 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2256 * If we have an active connection to the given target, it must be shutdown.
2258 * @param target peer to disconnect from
2261 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2263 struct NeighbourMapEntry *n;
2265 // This can happen during shutdown
2266 if (neighbours == NULL)
2271 n = lookup_neighbour (target);
2273 return; /* not active */
2274 disconnect_neighbour (n);
2279 * We received a disconnect message from the given peer,
2280 * validate and process.
2282 * @param peer sender of the message
2283 * @param msg the disconnect message
2286 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2288 const struct GNUNET_MessageHeader
2291 struct NeighbourMapEntry *n;
2292 const struct SessionDisconnectMessage *sdm;
2296 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2297 "Received DISCONNECT message from peer `%s'\n",
2301 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2303 // GNUNET_break_op (0);
2304 GNUNET_STATISTICS_update (GST_stats,
2306 ("# disconnect messages ignored (old format)"), 1,
2310 sdm = (const struct SessionDisconnectMessage *) msg;
2311 n = lookup_neighbour (peer);
2313 return; /* gone already */
2314 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2315 n->connect_ts.abs_value)
2317 GNUNET_STATISTICS_update (GST_stats,
2319 ("# disconnect messages ignored (timestamp)"), 1,
2323 GNUNET_CRYPTO_hash (&sdm->public_key,
2324 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2326 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2328 GNUNET_break_op (0);
2331 if (ntohl (sdm->purpose.size) !=
2332 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2333 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2334 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2336 GNUNET_break_op (0);
2340 GNUNET_CRYPTO_rsa_verify
2341 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2342 &sdm->signature, &sdm->public_key))
2344 GNUNET_break_op (0);
2347 GST_neighbours_force_disconnect (peer);
2352 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2353 * Consider switching to it.
2355 * @param message possibly a 'struct SessionConnectMessage' (check format)
2356 * @param peer identity of the peer to switch the address for
2357 * @param address address of the other peer, NULL if other peer
2359 * @param session session to use (or NULL)
2360 * @param ats performance data
2361 * @param ats_count number of entries in ats
2364 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2365 const struct GNUNET_PeerIdentity *peer,
2366 const struct GNUNET_HELLO_Address *address,
2367 struct Session *session,
2368 const struct GNUNET_ATS_Information *ats,
2371 const struct SessionConnectMessage *scm;
2372 struct GNUNET_MessageHeader msg;
2373 struct NeighbourMapEntry *n;
2377 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2378 "Received CONNECT_ACK message from peer `%s'\n",
2381 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2383 GNUNET_break_op (0);
2386 scm = (const struct SessionConnectMessage *) message;
2387 GNUNET_break_op (ntohl (scm->reserved) == 0);
2388 n = lookup_neighbour (peer);
2391 /* we did not send 'CONNECT' -- at least not recently */
2392 GNUNET_STATISTICS_update (GST_stats,
2394 ("# unexpected CONNECT_ACK messages (no peer)"),
2401 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2403 * We also received an CONNECT message, switched from SENDT to RECV and
2404 * ATS already suggested us an address after a successful blacklist check
2407 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2408 "Received CONNECT_ACK message from peer `%s' in state `%s'\n",
2410 print_state(n->state));
2412 if ((n->address != NULL) && (n->state == S_CONNECTED))
2414 /* After fast reconnect: send ACK (ACK) even when we are connected */
2415 msg_len = sizeof (msg);
2416 msg.size = htons (msg_len);
2417 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2419 ret = send_with_session(n,
2420 (const char *) &msg, msg_len,
2421 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2424 if (ret == GNUNET_SYSERR)
2425 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2426 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2427 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2431 if ((n->state != S_CONNECT_SENT) &&
2432 ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2434 GNUNET_STATISTICS_update (GST_stats,
2436 ("# unexpected CONNECT_ACK messages"), 1,
2440 if (n->state != S_CONNECTED)
2441 change_state (n, S_CONNECTED);
2443 if (NULL != session)
2445 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2447 "Giving ATS session %p of plugin %s for peer %s\n",
2448 session, address->transport_name, GNUNET_i2s (peer));
2450 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2451 GNUNET_assert (NULL != n->address);
2453 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2455 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2456 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2457 n->address_state = USED;
2460 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2462 /* send ACK (ACK) */
2463 msg_len = sizeof (msg);
2464 msg.size = htons (msg_len);
2465 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2467 ret = send_with_session(n,
2468 (const char *) &msg, msg_len,
2469 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2472 if (ret == GNUNET_SYSERR)
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2474 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2475 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2478 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2479 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2481 neighbours_connected++;
2482 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2486 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2487 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2490 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2491 send_outbound_quota (peer, n->bandwidth_out);
2497 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2498 const struct GNUNET_PeerIdentity *peer,
2499 const struct GNUNET_HELLO_Address *address,
2500 struct Session *session,
2501 const struct GNUNET_ATS_Information *ats,
2504 struct NeighbourMapEntry *n;
2507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2511 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2513 GNUNET_break_op (0);
2516 n = lookup_neighbour (peer);
2522 if (S_CONNECTED == n->state)
2524 if (!is_connecting (n))
2526 GNUNET_STATISTICS_update (GST_stats,
2527 gettext_noop ("# unexpected ACK messages"), 1,
2531 change_state (n, S_CONNECTED);
2532 if (NULL != session)
2533 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2535 "Giving ATS session %p of plugin %s for peer %s\n",
2536 session, address->transport_name, GNUNET_i2s (peer));
2537 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2538 GNUNET_assert (n->address != NULL);
2540 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2542 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2543 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2544 n->address_state = USED;
2548 neighbours_connected++;
2549 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2552 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2553 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2554 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2557 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2558 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2561 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2562 send_outbound_quota (peer, n->bandwidth_out);
2565 struct BlackListCheckContext
2567 struct GNUNET_ATS_Information *ats;
2571 struct Session *session;
2573 struct GNUNET_HELLO_Address *address;
2575 struct GNUNET_TIME_Absolute ts;
2580 handle_connect_blacklist_cont (void *cls,
2581 const struct GNUNET_PeerIdentity *peer,
2584 struct NeighbourMapEntry *n;
2585 struct BlackListCheckContext *bcc = cls;
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2589 "Blacklist check due to CONNECT message: `%s'\n",
2591 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2595 if (GNUNET_OK != result)
2597 GNUNET_HELLO_address_free (bcc->address);
2602 n = lookup_neighbour (peer);
2604 n = setup_neighbour (peer);
2606 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2608 if (NULL != bcc->session)
2609 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2611 "Giving ATS session %p of address `%s' for peer %s\n",
2612 bcc->session, GST_plugins_a2s (bcc->address),
2614 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2616 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2618 n->connect_ts = bcc->ts;
2621 GNUNET_HELLO_address_free (bcc->address);
2624 if (n->state != S_CONNECT_RECV)
2625 change_state (n, S_CONNECT_RECV);
2628 /* Ask ATS for an address to connect via that address */
2629 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2630 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2632 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2634 GNUNET_ATS_suggest_address (GST_ats, peer);
2638 * We received a 'SESSION_CONNECT' message from the other peer.
2639 * Consider switching to it.
2641 * @param message possibly a 'struct SessionConnectMessage' (check format)
2642 * @param peer identity of the peer to switch the address for
2643 * @param address address of the other peer, NULL if other peer
2645 * @param session session to use (or NULL)
2646 * @param ats performance data
2647 * @param ats_count number of entries in ats (excluding 0-termination)
2650 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2651 const struct GNUNET_PeerIdentity *peer,
2652 const struct GNUNET_HELLO_Address *address,
2653 struct Session *session,
2654 const struct GNUNET_ATS_Information *ats,
2657 const struct SessionConnectMessage *scm;
2658 struct BlackListCheckContext *bcc = NULL;
2659 struct NeighbourMapEntry *n;
2662 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2663 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2666 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2668 GNUNET_break_op (0);
2672 scm = (const struct SessionConnectMessage *) message;
2673 GNUNET_break_op (ntohl (scm->reserved) == 0);
2675 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2677 n = lookup_neighbour (peer);
2678 if ((n != NULL) && ((S_CONNECTED == n->state) || (S_FAST_RECONNECT == n->state)))
2680 /* connected peer switches addresses or is trying to do a fast reconnect*/
2685 /* we are not connected to this peer */
2686 /* do blacklist check */
2688 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2689 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2690 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2691 bcc->ats_count = ats_count + 1;
2692 bcc->address = GNUNET_HELLO_address_copy (address);
2693 bcc->session = session;
2694 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2695 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2696 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2697 bcc->ats[ats_count].value =
2698 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2699 GST_blacklist_test_allowed (peer, address->transport_name,
2700 &handle_connect_blacklist_cont, bcc);
2704 /* end of file gnunet-service-transport_neighbours.c */