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_RATE GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_MILLISECONDS, 100)
64 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
66 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
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 * Address we currently use.
270 struct GNUNET_HELLO_Address *fast_reconnect_address;
274 * Identity of this neighbour.
276 struct GNUNET_PeerIdentity id;
279 * ID of task scheduled to run when this peer is about to
280 * time out (will free resources associated with the peer).
282 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
285 * ID of task scheduled to send keepalives.
287 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
290 * ID of task scheduled to run when we should try transmitting
291 * the head of the message queue.
293 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
296 * Tracker for inbound bandwidth.
298 struct GNUNET_BANDWIDTH_Tracker in_tracker;
301 * Inbound bandwidth from ATS, activated when connection is up
303 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
306 * Inbound bandwidth from ATS, activated when connection is up
308 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
311 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
313 struct GNUNET_TIME_Absolute connect_ts;
316 * When did we sent the last keep-alive message?
318 struct GNUNET_TIME_Absolute keep_alive_sent;
321 * Latest calculated latency value
323 struct GNUNET_TIME_Relative latency;
327 * We asked ATS for a new address for this peer
329 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
332 * Delay for ATS request
334 GNUNET_SCHEDULER_TaskIdentifier ats_request;
337 * Task the resets the peer state after due to an pending
338 * unsuccessful connection setup
340 GNUNET_SCHEDULER_TaskIdentifier state_reset;
344 * How often has the other peer (recently) violated the inbound
345 * traffic limit? Incremented by 10 per violation, decremented by 1
346 * per non-violation (for each time interval).
348 unsigned int quota_violation_count;
352 * The current state of the peer
353 * Element of enum State
358 * Did we sent an KEEP_ALIVE message and are we expecting a response?
360 int expect_latency_response;
363 * Was the address used successfully
368 * Fast reconnect attempts for identical address
370 unsigned int fast_reconnect_attempts;
375 * All known neighbours and their HELLOs.
377 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
380 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
382 static void *callback_cls;
385 * Function to call when we connected to a neighbour.
387 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
390 * Function to call when we disconnected from a neighbour.
392 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
395 * Function to call when we changed an active address of a neighbour.
397 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
400 * counter for connected neighbours
402 static int neighbours_connected;
405 * Lookup a neighbour entry in the neighbours hash map.
407 * @param pid identity of the peer to look up
408 * @return the entry, NULL if there is no existing record
410 static struct NeighbourMapEntry *
411 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
413 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
417 * Disconnect from the given neighbour, clean up the record.
419 * @param n neighbour to disconnect from
422 disconnect_neighbour (struct NeighbourMapEntry *n);
424 #define change_state(n, state, ...) change (n, state, __LINE__)
427 is_connecting (struct NeighbourMapEntry *n)
429 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
435 is_connected (struct NeighbourMapEntry *n)
437 if (n->state == S_CONNECTED)
443 is_disconnecting (struct NeighbourMapEntry *n)
445 if (n->state == S_DISCONNECT)
451 print_state (int state)
456 return "S_CONNECTED";
459 return "S_CONNECT_RECV";
462 return "S_CONNECT_SENT";
465 return "S_DISCONNECT";
467 case S_NOT_CONNECTED:
468 return "S_NOT_CONNECTED";
470 case S_FAST_RECONNECT:
471 return "S_FAST_RECONNECT";
481 change (struct NeighbourMapEntry *n, int state, int line);
484 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
488 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
490 struct NeighbourMapEntry *n = cls;
495 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
496 if (n->state == S_CONNECTED)
500 GNUNET_STATISTICS_update (GST_stats,
502 ("# failed connection attempts due to timeout"), 1,
506 /* resetting state */
508 if (n->state == S_FAST_RECONNECT)
510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
511 "Fast reconnect time out, disconnecting peer `%s'\n",
512 GNUNET_i2s (&n->id));
513 disconnect_neighbour(n);
517 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
518 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
519 GNUNET_i2s (&n->id), n, print_state(n->state), "S_NOT_CONNECTED", __LINE__);
521 n->state = S_NOT_CONNECTED;
523 /* destroying address */
524 if (n->address != NULL)
526 GNUNET_assert (strlen (n->address->transport_name) > 0);
527 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
530 /* request new address */
531 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
532 GNUNET_SCHEDULER_cancel (n->ats_suggest);
534 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
536 GNUNET_ATS_suggest_address (GST_ats, &n->id);
540 change (struct NeighbourMapEntry *n, int state, int line)
543 /* allowed transitions */
544 int allowed = GNUNET_NO;
546 previous_state = n->state;
550 case S_NOT_CONNECTED:
551 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
552 (state == S_DISCONNECT))
553 allowed = GNUNET_YES;
556 allowed = GNUNET_YES;
559 allowed = GNUNET_YES;
562 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
563 allowed = GNUNET_YES;
567 case S_FAST_RECONNECT:
568 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
569 allowed = GNUNET_YES;
575 if (allowed == GNUNET_NO)
577 char *old = GNUNET_strdup (print_state (n->state));
578 char *new = GNUNET_strdup (print_state (state));
580 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
581 "Illegal state transition from `%s' to `%s' in line %u \n", old,
586 return GNUNET_SYSERR;
589 char *old = GNUNET_strdup (print_state (n->state));
590 char *new = GNUNET_strdup (print_state (state));
592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
593 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
594 GNUNET_i2s (&n->id), n, old, new, line);
602 case S_FAST_RECONNECT:
605 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
606 GNUNET_SCHEDULER_cancel (n->state_reset);
608 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
611 case S_NOT_CONNECTED:
613 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
616 char *old = GNUNET_strdup (print_state (n->state));
617 char *new = GNUNET_strdup (print_state (state));
619 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
620 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
621 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), old, new);
625 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
626 GNUNET_SCHEDULER_cancel (n->state_reset);
627 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
635 if (NULL != address_change_cb)
637 if (n->state == S_CONNECTED)
638 address_change_cb (callback_cls, &n->id, n->address);
639 else if (previous_state == S_CONNECTED)
640 address_change_cb (callback_cls, &n->id, NULL);
647 send_with_session (struct NeighbourMapEntry *n,
648 const char *msgbuf, size_t msgbuf_size,
650 struct GNUNET_TIME_Relative timeout,
651 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
653 struct GNUNET_TRANSPORT_PluginFunctions *papi;
654 size_t ret = GNUNET_SYSERR;
656 GNUNET_assert (n != NULL);
657 GNUNET_assert (n->session != NULL);
659 papi = GST_plugins_find (n->address->transport_name);
663 cont (cont_cls, &n->id, GNUNET_SYSERR);
664 return GNUNET_SYSERR;
667 ret = papi->send (papi->cls,
674 if ((ret == -1) && (cont != NULL))
675 cont (cont_cls, &n->id, GNUNET_SYSERR);
680 * Task invoked to start a transmission to another peer.
682 * @param cls the 'struct NeighbourMapEntry'
683 * @param tc scheduler context
686 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
690 * We're done with our transmission attempt, continue processing.
692 * @param cls the 'struct MessageQueue' of the message
693 * @param receiver intended receiver
694 * @param success whether it worked or not
697 transmit_send_continuation (void *cls,
698 const struct GNUNET_PeerIdentity *receiver,
701 struct MessageQueue *mq = cls;
702 struct NeighbourMapEntry *n;
703 struct NeighbourMapEntry *tmp;
705 tmp = lookup_neighbour (receiver);
707 if ((NULL != n) && (tmp != NULL) && (tmp == n))
709 GNUNET_assert (n->is_active == mq);
711 if (success == GNUNET_YES)
713 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
714 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
719 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
720 (success == GNUNET_OK) ? "successful" : "FAILED");
722 if (NULL != mq->cont)
723 mq->cont (mq->cont_cls, success);
729 * Check the ready list for the given neighbour and if a plugin is
730 * ready for transmission (and if we have a message), do so!
732 * @param n target peer for which to transmit
735 try_transmission_to_peer (struct NeighbourMapEntry *n)
737 struct MessageQueue *mq;
738 struct GNUNET_TIME_Relative timeout;
741 if (n->is_active != NULL)
744 return; /* transmission already pending */
746 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
749 return; /* currently waiting for bandwidth */
751 while (NULL != (mq = n->messages_head))
753 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
754 if (timeout.rel_value > 0)
756 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
759 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
762 return; /* no more messages */
764 if (n->address == NULL)
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
768 GNUNET_i2s (&n->id));
770 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
771 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
772 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
773 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
777 if (GST_plugins_find (n->address->transport_name) == NULL)
782 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
786 if ((n->address->address_length == 0) && (n->session == NULL))
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
789 GNUNET_i2s (&n->id));
790 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
791 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
792 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
796 ret = send_with_session(n,
797 mq->message_buf, mq->message_buf_size,
799 &transmit_send_continuation, mq);
803 /* failure, but 'send' would not call continuation in this case,
804 * so we need to do it here! */
805 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
812 * Task invoked to start a transmission to another peer.
814 * @param cls the 'struct NeighbourMapEntry'
815 * @param tc scheduler context
818 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
820 struct NeighbourMapEntry *n = cls;
822 GNUNET_assert (NULL != lookup_neighbour (&n->id));
823 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
824 try_transmission_to_peer (n);
829 * Initialize the neighbours subsystem.
831 * @param cls closure for callbacks
832 * @param connect_cb function to call if we connect to a peer
833 * @param disconnect_cb function to call if we disconnect from a peer
834 * @param peer_address_cb function to call if we change an active address
838 GST_neighbours_start (void *cls,
839 GNUNET_TRANSPORT_NotifyConnect connect_cb,
840 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
841 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
844 connect_notify_cb = connect_cb;
845 disconnect_notify_cb = disconnect_cb;
846 address_change_cb = peer_address_cb;
847 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
852 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
856 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
857 "Sending DISCONNECT message to peer `%4s': %i\n",
858 GNUNET_i2s (target), result);
864 send_disconnect (struct NeighbourMapEntry * n)
867 struct SessionDisconnectMessage disconnect_msg;
870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
871 "Sending DISCONNECT message to peer `%4s'\n",
872 GNUNET_i2s (&n->id));
875 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
876 disconnect_msg.header.type =
877 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
878 disconnect_msg.reserved = htonl (0);
879 disconnect_msg.purpose.size =
880 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
881 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
882 sizeof (struct GNUNET_TIME_AbsoluteNBO));
883 disconnect_msg.purpose.purpose =
884 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
885 disconnect_msg.timestamp =
886 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
887 disconnect_msg.public_key = GST_my_public_key;
888 GNUNET_assert (GNUNET_OK ==
889 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
890 &disconnect_msg.purpose,
891 &disconnect_msg.signature));
893 ret = send_with_session (n,
894 (const char *) &disconnect_msg, sizeof (disconnect_msg),
895 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
896 &send_disconnect_cont, NULL);
898 if (ret == GNUNET_SYSERR)
899 return GNUNET_SYSERR;
901 GNUNET_STATISTICS_update (GST_stats,
903 ("# peers disconnected due to external request"), 1,
910 * Disconnect from the given neighbour, clean up the record.
912 * @param n neighbour to disconnect from
915 disconnect_neighbour (struct NeighbourMapEntry *n)
917 struct MessageQueue *mq;
920 previous_state = n->state;
922 if (is_disconnecting (n))
925 /* send DISCONNECT MESSAGE */
926 if (previous_state == S_CONNECTED)
928 if (GNUNET_OK == send_disconnect (n))
929 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
930 GNUNET_i2s (&n->id));
932 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
933 "Could not send DISCONNECT_MSG to `%s'\n",
934 GNUNET_i2s (&n->id));
937 change_state (n, S_DISCONNECT);
939 if (previous_state == S_CONNECTED)
941 GNUNET_assert (NULL != n->address);
942 if (n->address_state == USED)
944 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
945 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
946 n->address_state = UNUSED;
950 if (n->address != NULL)
952 struct GNUNET_TRANSPORT_PluginFunctions *papi;
954 papi = GST_plugins_find (n->address->transport_name);
956 papi->disconnect (papi->cls, &n->id);
958 while (NULL != (mq = n->messages_head))
960 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
961 if (NULL != mq->cont)
962 mq->cont (mq->cont_cls, GNUNET_SYSERR);
965 if (NULL != n->is_active)
967 n->is_active->n = NULL;
971 switch (previous_state)
974 GNUNET_assert (neighbours_connected > 0);
975 neighbours_connected--;
976 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
977 GNUNET_SCHEDULER_cancel (n->keepalive_task);
978 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
979 n->expect_latency_response = GNUNET_NO;
980 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
982 disconnect_notify_cb (callback_cls, &n->id);
984 case S_FAST_RECONNECT:
985 GNUNET_STATISTICS_update (GST_stats,
986 gettext_noop ("# fast reconnects failed"), 1,
988 disconnect_notify_cb (callback_cls, &n->id);
994 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
996 GNUNET_assert (GNUNET_YES ==
997 GNUNET_CONTAINER_multihashmap_remove (neighbours,
998 &n->id.hashPubKey, n));
999 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
1001 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1002 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1004 if (n->ats_request != GNUNET_SCHEDULER_NO_TASK)
1006 GNUNET_SCHEDULER_cancel (n->ats_request);
1007 n->ats_request = GNUNET_SCHEDULER_NO_TASK;
1010 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
1012 GNUNET_SCHEDULER_cancel (n->timeout_task);
1013 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1015 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
1017 GNUNET_SCHEDULER_cancel (n->transmission_task);
1018 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
1020 if (NULL != n->fast_reconnect_address)
1022 GNUNET_HELLO_address_free (n->fast_reconnect_address);
1023 n->fast_reconnect_address = NULL;
1025 if (NULL != n->address)
1027 GNUNET_HELLO_address_free (n->address);
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
1032 GNUNET_i2s (&n->id), n);
1038 * Peer has been idle for too long. Disconnect.
1040 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1041 * @param tc scheduler context
1044 neighbour_timeout_task (void *cls,
1045 const struct GNUNET_SCHEDULER_TaskContext *tc)
1047 struct NeighbourMapEntry *n = cls;
1049 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1050 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer`%4s' disconnected due to timeout\n",
1051 GNUNET_i2s (&n->id));
1053 GNUNET_STATISTICS_update (GST_stats,
1055 ("# peers disconnected due to timeout"), 1,
1057 disconnect_neighbour (n);
1062 * Send another keepalive message.
1064 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1065 * @param tc scheduler context
1068 neighbour_keepalive_task (void *cls,
1069 const struct GNUNET_SCHEDULER_TaskContext *tc)
1071 struct NeighbourMapEntry *n = cls;
1072 struct GNUNET_MessageHeader m;
1075 GNUNET_assert (S_CONNECTED == n->state);
1077 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1078 &neighbour_keepalive_task, n);
1080 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1082 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1083 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1085 ret = send_with_session (n,
1086 (const void *) &m, sizeof (m),
1087 UINT32_MAX /* priority */ ,
1088 GNUNET_TIME_UNIT_FOREVER_REL,
1091 n->expect_latency_response = GNUNET_NO;
1092 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero ();
1093 if (ret != GNUNET_SYSERR)
1095 n->expect_latency_response = GNUNET_YES;
1096 n->keep_alive_sent = GNUNET_TIME_absolute_get ();
1103 * Disconnect from the given neighbour.
1106 * @param key hash of neighbour's public key (not used)
1107 * @param value the 'struct NeighbourMapEntry' of the neighbour
1110 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1112 struct NeighbourMapEntry *n = value;
1115 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1116 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1118 if (S_CONNECTED == n->state)
1119 GNUNET_STATISTICS_update (GST_stats,
1121 ("# peers disconnected due to global disconnect"),
1123 disconnect_neighbour (n);
1129 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1131 struct NeighbourMapEntry *n = cls;
1133 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1135 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1136 "ATS did not suggested address to connect to peer `%s'\n",
1137 GNUNET_i2s (&n->id));
1139 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# ATS address suggestions failed"), 1,
1143 disconnect_neighbour (n);
1147 * Cleanup the neighbours subsystem.
1150 GST_neighbours_stop ()
1152 // This can happen during shutdown
1153 if (neighbours == NULL)
1158 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1160 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1161 // GNUNET_assert (neighbours_connected == 0);
1163 callback_cls = NULL;
1164 connect_notify_cb = NULL;
1165 disconnect_notify_cb = NULL;
1166 address_change_cb = NULL;
1169 struct ContinutionContext
1171 struct GNUNET_HELLO_Address *address;
1173 struct Session *session;
1177 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
1178 struct GNUNET_BANDWIDTH_Value32NBO quota)
1180 struct QuotaSetMessage q_msg;
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1185 ntohl (quota.value__), GNUNET_i2s (target));
1187 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1188 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1189 q_msg.quota = quota;
1190 q_msg.peer = (*target);
1191 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1195 * We tried to send a SESSION_CONNECT message to another peer. If this
1196 * succeeded, we change the state. If it failed, we should tell
1197 * ATS to not use this address anymore (until it is re-validated).
1199 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1200 * @param target peer to send the message to
1201 * @param success GNUNET_OK on success
1204 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1207 struct ContinutionContext *cc = cls;
1208 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1210 if (GNUNET_YES != success)
1212 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1213 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1215 if ((NULL == neighbours) || (NULL == n) || (n->state == S_DISCONNECT))
1217 GNUNET_HELLO_address_free (cc->address);
1222 if ((GNUNET_YES == success) &&
1223 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1225 change_state (n, S_CONNECT_SENT);
1226 GNUNET_HELLO_address_free (cc->address);
1231 if ((GNUNET_NO == success) &&
1232 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1236 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1237 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1239 change_state (n, S_NOT_CONNECTED);
1240 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1241 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1243 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1245 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1247 GNUNET_HELLO_address_free (cc->address);
1253 ats_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1255 struct NeighbourMapEntry *n = cls;
1256 n->ats_request = GNUNET_SCHEDULER_NO_TASK;
1259 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1265 * We tried to switch addresses with an peer already connected. If it failed,
1266 * we should tell ATS to not use this address anymore (until it is re-validated).
1268 * @param cls the 'struct NeighbourMapEntry'
1269 * @param target peer to send the message to
1270 * @param success GNUNET_OK on success
1273 send_switch_address_continuation (void *cls,
1274 const struct GNUNET_PeerIdentity *target,
1277 struct ContinutionContext *cc = cls;
1278 struct NeighbourMapEntry *n;
1280 if (neighbours == NULL)
1282 GNUNET_HELLO_address_free (cc->address);
1284 return; /* neighbour is going away */
1287 n = lookup_neighbour (&cc->address->peer);
1288 if ((n == NULL) || (is_disconnecting (n)))
1290 GNUNET_HELLO_address_free (cc->address);
1292 return; /* neighbour is going away */
1295 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1296 if (GNUNET_YES != success)
1299 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1300 "Failed to switch connected peer `%s' in state %s to address '%s' session %X, asking ATS for new address \n",
1301 GNUNET_i2s (&n->id), print_state(n->state), GST_plugins_a2s (cc->address), cc->session);
1303 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1304 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1306 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1308 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1309 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1312 if (n->state == S_FAST_RECONNECT)
1314 if (GNUNET_SCHEDULER_NO_TASK == n->ats_request)
1316 /* Throtteled ATS request for fast reconnect */
1317 struct GNUNET_TIME_Relative delay = GNUNET_TIME_relative_multiply(FAST_RECONNECT_RATE, n->fast_reconnect_attempts);
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "Fast reconnect attempt %u failed, delay ATS request for %llu ms\n", n->fast_reconnect_attempts, delay.rel_value);
1321 GNUNET_SCHEDULER_add_delayed (delay,
1328 /* Immediate ATS request for connected peers */
1330 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1332 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1334 GNUNET_HELLO_address_free (cc->address);
1338 /* Tell ATS that switching addresses was successful */
1342 if (n->address_state == FRESH)
1344 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES, __LINE__);
1345 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1346 if (cc->session != n->session)
1348 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1349 n->address_state = USED;
1352 case S_FAST_RECONNECT:
1354 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1355 "Successful fast reconnect to peer `%s'\n",
1356 GNUNET_i2s (&n->id));
1358 change_state (n, S_CONNECTED);
1359 neighbours_connected++;
1360 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1363 if (n->address_state == FRESH)
1365 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES, __LINE__);
1366 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1367 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1368 n->address_state = USED;
1371 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1373 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1375 /* Updating quotas */
1376 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1377 send_outbound_quota (target, n->bandwidth_out);
1382 GNUNET_HELLO_address_free (cc->address);
1388 * We tried to send a SESSION_CONNECT message to another peer. If this
1389 * succeeded, we change the state. If it failed, we should tell
1390 * ATS to not use this address anymore (until it is re-validated).
1392 * @param cls the 'struct NeighbourMapEntry'
1393 * @param target peer to send the message to
1394 * @param success GNUNET_OK on success
1397 send_connect_ack_continuation (void *cls,
1398 const struct GNUNET_PeerIdentity *target,
1401 struct ContinutionContext *cc = cls;
1402 struct NeighbourMapEntry *n;
1404 if (neighbours == NULL)
1406 GNUNET_HELLO_address_free (cc->address);
1408 return; /* neighbour is going away */
1411 n = lookup_neighbour (&cc->address->peer);
1412 if ((n == NULL) || (is_disconnecting (n)))
1414 GNUNET_HELLO_address_free (cc->address);
1416 return; /* neighbour is going away */
1419 if (GNUNET_YES == success)
1421 GNUNET_HELLO_address_free (cc->address);
1423 return; /* sending successful */
1426 /* sending failed, ask for next address */
1428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1429 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1430 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1432 if (n->state != S_NOT_CONNECTED)
1433 change_state (n, S_NOT_CONNECTED);
1434 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1435 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1437 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1438 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1440 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1442 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1443 GNUNET_HELLO_address_free (cc->address);
1449 * For an existing neighbour record, set the active connection to
1450 * use the given address.
1452 * @param peer identity of the peer to switch the address for
1453 * @param address address of the other peer, NULL if other peer
1455 * @param session session to use (or NULL)
1456 * @param ats performance data
1457 * @param ats_count number of entries in ats
1458 * @param bandwidth_in inbound quota to be used when connection is up
1459 * @param bandwidth_out outbound quota to be used when connection is up
1460 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1461 * connection is not up (yet)
1464 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1465 const struct GNUNET_HELLO_Address
1467 struct Session *session,
1468 const struct GNUNET_ATS_Information *ats,
1470 struct GNUNET_BANDWIDTH_Value32NBO
1472 struct GNUNET_BANDWIDTH_Value32NBO
1475 struct NeighbourMapEntry *n;
1476 struct SessionConnectMessage connect_msg;
1477 struct ContinutionContext *cc;
1481 if (neighbours == NULL)
1483 /* This can happen during shutdown */
1486 n = lookup_neighbour (peer);
1489 if (n->state == S_DISCONNECT)
1491 /* We are disconnecting, nothing to do here */
1494 GNUNET_assert (address->transport_name != NULL);
1495 if ((session == NULL) && (0 == address->address_length))
1497 GNUNET_break_op (0);
1498 /* FIXME: is this actually possible? When does this happen? */
1499 if (strlen (address->transport_name) > 0)
1500 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1501 GNUNET_ATS_suggest_address (GST_ats, peer);
1505 /* checks successful and neighbour != NULL */
1506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1507 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1508 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
1511 print_state (n->state));
1513 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1515 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1516 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1520 if (n->state == S_FAST_RECONNECT)
1522 /* Throtteled fast reconnect */
1524 if (NULL == n->fast_reconnect_address)
1526 n->fast_reconnect_address = GNUNET_HELLO_address_copy (address);
1529 else if (0 == GNUNET_HELLO_address_cmp(address, n->fast_reconnect_address))
1531 n->fast_reconnect_attempts ++;
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1534 "FAST RECONNECT to peer `%s' and address '%s' with identical address attempt %u\n",
1535 GNUNET_i2s (&n->id), GST_plugins_a2s (address), n->fast_reconnect_attempts);
1540 n->fast_reconnect_attempts = 0;
1543 /* do not switch addresses just update quotas */
1544 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1545 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1546 (n->session == session))
1548 n->bandwidth_in = bandwidth_in;
1549 n->bandwidth_out = bandwidth_out;
1550 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1551 send_outbound_quota (peer, n->bandwidth_out);
1554 if (n->state == S_CONNECTED)
1556 /* mark old address as no longer used */
1557 GNUNET_assert (NULL != n->address);
1558 if (n->address_state == USED)
1560 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1561 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1562 n->address_state = UNUSED;
1566 /* set new address */
1567 if (NULL != n->address)
1568 GNUNET_HELLO_address_free (n->address);
1569 n->address = GNUNET_HELLO_address_copy (address);
1570 n->address_state = FRESH;
1571 n->bandwidth_in = bandwidth_in;
1572 n->bandwidth_out = bandwidth_out;
1573 GNUNET_SCHEDULER_cancel (n->timeout_task);
1575 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1576 &neighbour_timeout_task, n);
1578 if (NULL != address_change_cb && n->state == S_CONNECTED)
1579 address_change_cb (callback_cls, &n->id, n->address);
1581 /* Obtain an session for this address from plugin */
1582 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1583 papi = GST_plugins_find (address->transport_name);
1587 /* we don't have the plugin for this address */
1588 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1590 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1591 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1592 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1595 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1596 GNUNET_HELLO_address_free (n->address);
1602 if (session == NULL)
1604 n->session = papi->get_session (papi->cls, address);
1605 /* Session could not be initiated */
1606 if (n->session == NULL)
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1609 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1610 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1612 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1614 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1615 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1616 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1619 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1620 GNUNET_HELLO_address_free (n->address);
1626 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1627 "Obtained new session %p for peer `%s' and address '%s'\n",
1628 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1629 /* Telling ATS about new session */
1630 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1634 n->session = session;
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "Using existing session %p for peer `%s' and address '%s'\n",
1638 GNUNET_i2s (&n->id),
1639 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>");
1644 case S_NOT_CONNECTED:
1645 case S_CONNECT_SENT:
1646 msg_len = sizeof (struct SessionConnectMessage);
1647 connect_msg.header.size = htons (msg_len);
1648 connect_msg.header.type =
1649 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1650 connect_msg.reserved = htonl (0);
1651 connect_msg.timestamp =
1652 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1654 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1655 cc->session = n->session;
1656 cc->address = GNUNET_HELLO_address_copy (address);
1658 ret = send_with_session (n,
1659 (const char *) &connect_msg, msg_len,
1660 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1661 &send_connect_continuation, cc);
1664 case S_CONNECT_RECV:
1665 /* We received a CONNECT message and asked ATS for an address */
1666 msg_len = sizeof (struct SessionConnectMessage);
1667 connect_msg.header.size = htons (msg_len);
1668 connect_msg.header.type =
1669 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1670 connect_msg.reserved = htonl (0);
1671 connect_msg.timestamp =
1672 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1673 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1674 cc->session = n->session;
1675 cc->address = GNUNET_HELLO_address_copy (address);
1677 ret = send_with_session(n,
1678 (const void *) &connect_msg, msg_len,
1679 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1680 &send_connect_ack_continuation,
1684 case S_FAST_RECONNECT:
1685 /* connected peer is switching addresses or tries fast reconnect */
1686 msg_len = sizeof (struct SessionConnectMessage);
1687 connect_msg.header.size = htons (msg_len);
1688 connect_msg.header.type =
1689 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1690 connect_msg.reserved = htonl (0);
1691 connect_msg.timestamp =
1692 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1693 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1694 cc->session = n->session;
1695 cc->address = GNUNET_HELLO_address_copy (address);
1696 ret = send_with_session(n,
1697 (const void *) &connect_msg, msg_len,
1698 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1699 &send_switch_address_continuation, cc);
1700 if (ret == GNUNET_SYSERR)
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1704 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1708 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1709 "Invalid connection state to switch addresses %u \n", n->state);
1710 GNUNET_break_op (0);
1717 * Obtain current latency information for the given neighbour.
1720 * @return observed latency of the address, FOREVER if the address was
1721 * never successfully validated
1723 struct GNUNET_TIME_Relative
1724 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1726 struct NeighbourMapEntry *n;
1728 n = lookup_neighbour (peer);
1729 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1730 return GNUNET_TIME_UNIT_FOREVER_REL;
1736 * Obtain current address information for the given neighbour.
1739 * @return address currently used
1741 struct GNUNET_HELLO_Address *
1742 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1744 struct NeighbourMapEntry *n;
1746 n = lookup_neighbour (peer);
1747 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1756 * Create an entry in the neighbour map for the given peer
1758 * @param peer peer to create an entry for
1759 * @return new neighbour map entry
1761 static struct NeighbourMapEntry *
1762 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1764 struct NeighbourMapEntry *n;
1767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1768 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1770 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1772 n->state = S_NOT_CONNECTED;
1773 n->latency = GNUNET_TIME_relative_get_forever ();
1774 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1775 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1776 MAX_BANDWIDTH_CARRY_S);
1778 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1779 &neighbour_timeout_task, n);
1780 GNUNET_assert (GNUNET_OK ==
1781 GNUNET_CONTAINER_multihashmap_put (neighbours,
1782 &n->id.hashPubKey, n,
1783 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1789 * Try to create a connection to the given target (eventually).
1791 * @param target peer to try to connect to
1794 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1796 struct NeighbourMapEntry *n;
1798 // This can happen during shutdown
1799 if (neighbours == NULL)
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1805 GNUNET_i2s (target));
1808 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1813 n = lookup_neighbour (target);
1817 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1818 return; /* already connecting or connected */
1819 if (is_disconnecting (n))
1820 change_state (n, S_NOT_CONNECTED);
1825 n = setup_neighbour (target);
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1828 "Asking ATS for suggested address to connect to peer `%s'\n",
1829 GNUNET_i2s (&n->id));
1832 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1836 * Test if we're connected to the given peer.
1838 * @param target peer to test
1839 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1842 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1844 struct NeighbourMapEntry *n;
1846 // This can happen during shutdown
1847 if (neighbours == NULL)
1852 n = lookup_neighbour (target);
1854 if ((NULL == n) || (S_CONNECTED != n->state))
1855 return GNUNET_NO; /* not connected */
1860 * A session was terminated. Take note.
1862 * @param peer identity of the peer where the session died
1863 * @param session session that is gone
1866 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1867 struct Session *session)
1869 struct NeighbourMapEntry *n;
1871 if (neighbours == NULL)
1873 /* This can happen during shutdown */
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1879 session, GNUNET_i2s (peer));
1882 n = lookup_neighbour (peer);
1885 if (session != n->session)
1886 return; /* doesn't affect us */
1887 if (n->state == S_CONNECTED)
1889 if (n->address_state == USED)
1891 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1892 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1893 n->address_state = UNUSED;
1898 if (NULL != n->address)
1900 GNUNET_HELLO_address_free (n->address);
1905 /* not connected anymore anyway, shouldn't matter */
1906 if (S_CONNECTED != n->state)
1909 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1911 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1912 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1913 n->expect_latency_response = GNUNET_NO;
1916 /* connected, try fast reconnect */
1917 /* statistics "transport" : "# peers connected" -= 1
1918 * neighbours_connected -= 1
1919 * BUT: no disconnect_cb to notify clients about disconnect
1922 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1925 GNUNET_assert (neighbours_connected > 0);
1926 change_state (n, S_FAST_RECONNECT);
1927 neighbours_connected--;
1928 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
1932 /* We are connected, so ask ATS to switch addresses */
1933 GNUNET_SCHEDULER_cancel (n->timeout_task);
1934 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1935 &neighbour_timeout_task, n);
1936 /* try QUICKLY to re-establish a connection, reduce timeout! */
1937 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1938 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1939 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1940 &ats_suggest_cancel,
1942 GNUNET_ATS_suggest_address (GST_ats, peer);
1947 * Transmit a message to the given target using the active connection.
1949 * @param target destination
1950 * @param msg message to send
1951 * @param msg_size number of bytes in msg
1952 * @param timeout when to fail with timeout
1953 * @param cont function to call when done
1954 * @param cont_cls closure for 'cont'
1957 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1958 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1959 GST_NeighbourSendContinuation cont, void *cont_cls)
1961 struct NeighbourMapEntry *n;
1962 struct MessageQueue *mq;
1964 // This can happen during shutdown
1965 if (neighbours == NULL)
1970 n = lookup_neighbour (target);
1971 if ((n == NULL) || (!is_connected (n)))
1973 GNUNET_STATISTICS_update (GST_stats,
1975 ("# messages not sent (no such peer or not connected)"),
1979 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1980 "Could not send message to peer `%s': unknown neighbour",
1981 GNUNET_i2s (target));
1982 else if (!is_connected (n))
1983 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1984 "Could not send message to peer `%s': not connected\n",
1985 GNUNET_i2s (target));
1988 cont (cont_cls, GNUNET_SYSERR);
1992 if ((n->session == NULL) && (n->address == NULL))
1994 GNUNET_STATISTICS_update (GST_stats,
1996 ("# messages not sent (no such peer or not connected)"),
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000 "Could not send message to peer `%s': no address available\n",
2001 GNUNET_i2s (target));
2005 cont (cont_cls, GNUNET_SYSERR);
2009 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
2010 GNUNET_STATISTICS_update (GST_stats,
2012 ("# bytes in message queue for other peers"),
2013 msg_size, GNUNET_NO);
2014 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
2016 mq->cont_cls = cont_cls;
2017 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2018 memcpy (&mq[1], msg, msg_size);
2019 mq->message_buf = (const char *) &mq[1];
2020 mq->message_buf_size = msg_size;
2021 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2022 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
2024 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
2025 (NULL == n->is_active))
2026 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
2031 * We have received a message from the given sender. How long should
2032 * we delay before receiving more? (Also used to keep the peer marked
2035 * @param sender sender of the message
2036 * @param size size of the message
2037 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
2038 * GNUNET_NO if the neighbour is not connected or violates the quota,
2039 * GNUNET_SYSERR if the connection is not fully up yet
2040 * @return how long to wait before reading more from this sender
2042 struct GNUNET_TIME_Relative
2043 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
2044 *sender, ssize_t size, int *do_forward)
2046 struct NeighbourMapEntry *n;
2047 struct GNUNET_TIME_Relative ret;
2049 // This can happen during shutdown
2050 if (neighbours == NULL)
2052 return GNUNET_TIME_UNIT_FOREVER_REL;
2055 n = lookup_neighbour (sender);
2058 GST_neighbours_try_connect (sender);
2059 n = lookup_neighbour (sender);
2062 GNUNET_STATISTICS_update (GST_stats,
2064 ("# messages discarded due to lack of neighbour record"),
2066 *do_forward = GNUNET_NO;
2067 return GNUNET_TIME_UNIT_ZERO;
2070 if (!is_connected (n))
2072 *do_forward = GNUNET_SYSERR;
2073 return GNUNET_TIME_UNIT_ZERO;
2075 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
2077 n->quota_violation_count++;
2079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2080 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
2081 n->in_tracker.available_bytes_per_s__,
2082 n->quota_violation_count);
2084 /* Discount 32k per violation */
2085 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
2089 if (n->quota_violation_count > 0)
2091 /* try to add 32k back */
2092 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
2093 n->quota_violation_count--;
2096 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2098 GNUNET_STATISTICS_update (GST_stats,
2100 ("# bandwidth quota violations by other peers"),
2102 *do_forward = GNUNET_NO;
2103 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
2105 *do_forward = GNUNET_YES;
2106 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
2107 if (ret.rel_value > 0)
2110 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2111 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
2112 (unsigned long long) n->in_tracker.
2113 consumption_since_last_update__,
2114 (unsigned int) n->in_tracker.available_bytes_per_s__,
2115 (unsigned long long) ret.rel_value);
2117 GNUNET_STATISTICS_update (GST_stats,
2118 gettext_noop ("# ms throttling suggested"),
2119 (int64_t) ret.rel_value, GNUNET_NO);
2126 * Keep the connection to the given neighbour alive longer,
2127 * we received a KEEPALIVE (or equivalent).
2129 * @param neighbour neighbour to keep alive
2132 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
2134 struct NeighbourMapEntry *n;
2136 // This can happen during shutdown
2137 if (neighbours == NULL)
2142 n = lookup_neighbour (neighbour);
2145 GNUNET_STATISTICS_update (GST_stats,
2147 ("# KEEPALIVE messages discarded (not connected)"),
2151 GNUNET_SCHEDULER_cancel (n->timeout_task);
2153 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2154 &neighbour_timeout_task, n);
2156 /* send reply to measure latency */
2157 if (S_CONNECTED != n->state)
2160 struct GNUNET_MessageHeader m;
2162 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2163 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2165 send_with_session(n,
2166 (const void *) &m, sizeof (m),
2168 GNUNET_TIME_UNIT_FOREVER_REL,
2173 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2176 * @param neighbour neighbour to keep alive
2177 * @param ats performance data
2178 * @param ats_count number of entries in ats
2181 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2182 const struct GNUNET_ATS_Information *ats,
2185 struct NeighbourMapEntry *n;
2186 struct GNUNET_ATS_Information *ats_new;
2189 if (neighbours == NULL)
2191 // This can happen during shutdown
2195 n = lookup_neighbour (neighbour);
2196 if ((NULL == n) || (n->state != S_CONNECTED))
2198 GNUNET_STATISTICS_update (GST_stats,
2200 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2204 if (n->expect_latency_response != GNUNET_YES)
2206 GNUNET_STATISTICS_update (GST_stats,
2208 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2212 n->expect_latency_response = GNUNET_NO;
2214 GNUNET_assert (n->keep_alive_sent.abs_value !=
2215 GNUNET_TIME_absolute_get_zero ().abs_value);
2217 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2218 GNUNET_TIME_absolute_get ());
2220 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2221 GNUNET_i2s (&n->id), n->latency.rel_value);
2225 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2227 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2232 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2234 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2237 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2238 if (n->latency.rel_value > UINT32_MAX)
2239 latency = UINT32_MAX;
2241 latency = n->latency.rel_value;
2242 ats_new[ats_count].value = htonl (latency);
2244 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2246 GNUNET_free (ats_new);
2252 * Change the incoming quota for the given peer.
2254 * @param neighbour identity of peer to change qutoa for
2255 * @param quota new quota
2258 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2259 struct GNUNET_BANDWIDTH_Value32NBO quota)
2261 struct NeighbourMapEntry *n;
2263 // This can happen during shutdown
2264 if (neighbours == NULL)
2269 n = lookup_neighbour (neighbour);
2272 GNUNET_STATISTICS_update (GST_stats,
2274 ("# SET QUOTA messages ignored (no such peer)"),
2279 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2280 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2281 ntohl (quota.value__), GNUNET_i2s (&n->id));
2283 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2284 if (0 != ntohl (quota.value__))
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2288 GNUNET_i2s (&n->id), "SET_QUOTA");
2290 if (is_connected (n))
2291 GNUNET_STATISTICS_update (GST_stats,
2292 gettext_noop ("# disconnects due to quota of 0"),
2294 disconnect_neighbour (n);
2299 * Closure for the neighbours_iterate function.
2301 struct IteratorContext
2304 * Function to call on each connected neighbour.
2306 GST_NeighbourIterator cb;
2316 * Call the callback from the closure for each connected neighbour.
2318 * @param cls the 'struct IteratorContext'
2319 * @param key the hash of the public key of the neighbour
2320 * @param value the 'struct NeighbourMapEntry'
2321 * @return GNUNET_OK (continue to iterate)
2324 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2326 struct IteratorContext *ic = cls;
2327 struct NeighbourMapEntry *n = value;
2329 if (!is_connected (n))
2332 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2338 * Iterate over all connected neighbours.
2340 * @param cb function to call
2341 * @param cb_cls closure for cb
2344 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2346 struct IteratorContext ic;
2348 // This can happen during shutdown
2349 if (neighbours == NULL)
2356 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2360 * If we have an active connection to the given target, it must be shutdown.
2362 * @param target peer to disconnect from
2365 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2367 struct NeighbourMapEntry *n;
2369 // This can happen during shutdown
2370 if (neighbours == NULL)
2375 n = lookup_neighbour (target);
2377 return; /* not active */
2378 disconnect_neighbour (n);
2383 * We received a disconnect message from the given peer,
2384 * validate and process.
2386 * @param peer sender of the message
2387 * @param msg the disconnect message
2390 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2392 const struct GNUNET_MessageHeader
2395 struct NeighbourMapEntry *n;
2396 const struct SessionDisconnectMessage *sdm;
2400 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2401 "Received DISCONNECT message from peer `%s'\n",
2405 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2407 // GNUNET_break_op (0);
2408 GNUNET_STATISTICS_update (GST_stats,
2410 ("# disconnect messages ignored (old format)"), 1,
2414 sdm = (const struct SessionDisconnectMessage *) msg;
2415 n = lookup_neighbour (peer);
2417 return; /* gone already */
2418 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2419 n->connect_ts.abs_value)
2421 GNUNET_STATISTICS_update (GST_stats,
2423 ("# disconnect messages ignored (timestamp)"), 1,
2427 GNUNET_CRYPTO_hash (&sdm->public_key,
2428 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2430 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2432 GNUNET_break_op (0);
2435 if (ntohl (sdm->purpose.size) !=
2436 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2437 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2438 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2440 GNUNET_break_op (0);
2444 GNUNET_CRYPTO_rsa_verify
2445 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2446 &sdm->signature, &sdm->public_key))
2448 GNUNET_break_op (0);
2451 GST_neighbours_force_disconnect (peer);
2456 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2457 * Consider switching to it.
2459 * @param message possibly a 'struct SessionConnectMessage' (check format)
2460 * @param peer identity of the peer to switch the address for
2461 * @param address address of the other peer, NULL if other peer
2463 * @param session session to use (or NULL)
2464 * @param ats performance data
2465 * @param ats_count number of entries in ats
2468 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2469 const struct GNUNET_PeerIdentity *peer,
2470 const struct GNUNET_HELLO_Address *address,
2471 struct Session *session,
2472 const struct GNUNET_ATS_Information *ats,
2475 const struct SessionConnectMessage *scm;
2476 struct GNUNET_MessageHeader msg;
2477 struct NeighbourMapEntry *n;
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2482 "Received CONNECT_ACK message from peer `%s'\n",
2485 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2487 GNUNET_break_op (0);
2490 scm = (const struct SessionConnectMessage *) message;
2491 GNUNET_break_op (ntohl (scm->reserved) == 0);
2492 n = lookup_neighbour (peer);
2495 /* we did not send 'CONNECT' -- at least not recently */
2496 GNUNET_STATISTICS_update (GST_stats,
2498 ("# unexpected CONNECT_ACK messages (no peer)"),
2505 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2507 * We also received an CONNECT message, switched from SENDT to RECV and
2508 * ATS already suggested us an address after a successful blacklist check
2511 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2512 "Received CONNECT_ACK message from peer `%s' in state `%s'\n",
2514 print_state(n->state));
2516 if ((n->address != NULL) && (n->state == S_CONNECTED))
2518 /* After fast reconnect: send ACK (ACK) even when we are connected */
2519 msg_len = sizeof (msg);
2520 msg.size = htons (msg_len);
2521 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2523 ret = send_with_session(n,
2524 (const char *) &msg, msg_len,
2525 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2528 if (ret == GNUNET_SYSERR)
2529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2530 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2531 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2535 if ((n->state != S_CONNECT_SENT) &&
2536 ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2538 GNUNET_STATISTICS_update (GST_stats,
2540 ("# unexpected CONNECT_ACK messages"), 1,
2544 if (n->state != S_CONNECTED)
2545 change_state (n, S_CONNECTED);
2547 if (NULL != session)
2549 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2551 "Giving ATS session %p of plugin %s for peer %s\n",
2552 session, address->transport_name, GNUNET_i2s (peer));
2554 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2555 GNUNET_assert (NULL != n->address);
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, __LINE__);
2560 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2561 n->address_state = USED;
2564 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2566 /* send ACK (ACK) */
2567 msg_len = sizeof (msg);
2568 msg.size = htons (msg_len);
2569 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2571 ret = send_with_session(n,
2572 (const char *) &msg, msg_len,
2573 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2576 if (ret == GNUNET_SYSERR)
2577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2578 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2579 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2582 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2583 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2585 neighbours_connected++;
2586 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2589 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2590 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2591 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2594 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2595 send_outbound_quota (peer, n->bandwidth_out);
2601 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2602 const struct GNUNET_PeerIdentity *peer,
2603 const struct GNUNET_HELLO_Address *address,
2604 struct Session *session,
2605 const struct GNUNET_ATS_Information *ats,
2608 struct NeighbourMapEntry *n;
2611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2615 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2617 GNUNET_break_op (0);
2620 n = lookup_neighbour (peer);
2626 if (S_CONNECTED == n->state)
2628 if (!is_connecting (n))
2630 GNUNET_STATISTICS_update (GST_stats,
2631 gettext_noop ("# unexpected ACK messages"), 1,
2635 change_state (n, S_CONNECTED);
2636 if (NULL != session)
2637 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2639 "Giving ATS session %p of plugin %s for peer %s\n",
2640 session, address->transport_name, GNUNET_i2s (peer));
2641 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2642 GNUNET_assert (n->address != NULL);
2644 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2646 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2647 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2648 n->address_state = USED;
2652 neighbours_connected++;
2653 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2656 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2657 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2658 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2660 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2661 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2662 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2665 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2666 send_outbound_quota (peer, n->bandwidth_out);
2669 struct BlackListCheckContext
2671 struct GNUNET_ATS_Information *ats;
2675 struct Session *session;
2677 struct GNUNET_HELLO_Address *address;
2679 struct GNUNET_TIME_Absolute ts;
2684 handle_connect_blacklist_cont (void *cls,
2685 const struct GNUNET_PeerIdentity *peer,
2688 struct NeighbourMapEntry *n;
2689 struct BlackListCheckContext *bcc = cls;
2692 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2693 "Blacklist check due to CONNECT message: `%s'\n",
2695 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2699 if (GNUNET_OK != result)
2701 GNUNET_HELLO_address_free (bcc->address);
2706 n = lookup_neighbour (peer);
2708 n = setup_neighbour (peer);
2710 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2712 if (NULL != bcc->session)
2713 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2715 "Giving ATS session %p of address `%s' for peer %s\n",
2716 bcc->session, GST_plugins_a2s (bcc->address),
2718 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2720 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2722 n->connect_ts = bcc->ts;
2725 GNUNET_HELLO_address_free (bcc->address);
2728 if (n->state != S_CONNECT_RECV)
2729 change_state (n, S_CONNECT_RECV);
2732 /* Ask ATS for an address to connect via that address */
2733 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2734 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2736 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2738 GNUNET_ATS_suggest_address (GST_ats, peer);
2742 * We received a 'SESSION_CONNECT' message from the other peer.
2743 * Consider switching to it.
2745 * @param message possibly a 'struct SessionConnectMessage' (check format)
2746 * @param peer identity of the peer to switch the address for
2747 * @param address address of the other peer, NULL if other peer
2749 * @param session session to use (or NULL)
2750 * @param ats performance data
2751 * @param ats_count number of entries in ats (excluding 0-termination)
2754 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2755 const struct GNUNET_PeerIdentity *peer,
2756 const struct GNUNET_HELLO_Address *address,
2757 struct Session *session,
2758 const struct GNUNET_ATS_Information *ats,
2761 const struct SessionConnectMessage *scm;
2762 struct BlackListCheckContext *bcc = NULL;
2763 struct NeighbourMapEntry *n;
2766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2767 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2770 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2772 GNUNET_break_op (0);
2776 scm = (const struct SessionConnectMessage *) message;
2777 GNUNET_break_op (ntohl (scm->reserved) == 0);
2779 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2781 n = lookup_neighbour (peer);
2782 if ((n != NULL) && ((S_CONNECTED == n->state) || (S_FAST_RECONNECT == n->state)))
2784 /* connected peer switches addresses or is trying to do a fast reconnect*/
2789 /* we are not connected to this peer */
2790 /* do blacklist check */
2792 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2793 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2794 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2795 bcc->ats_count = ats_count + 1;
2796 bcc->address = GNUNET_HELLO_address_copy (address);
2797 bcc->session = session;
2798 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2799 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2800 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2801 bcc->ats[ats_count].value =
2802 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2803 GST_blacklist_test_allowed (peer, address->transport_name,
2804 &handle_connect_blacklist_cont, bcc);
2808 /* end of file gnunet-service-transport_neighbours.c */