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 GNUNET_break (cc->session == n->session);
1347 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1348 n->address_state = USED;
1351 case S_FAST_RECONNECT:
1353 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1354 "Successful fast reconnect to peer `%s'\n",
1355 GNUNET_i2s (&n->id));
1357 change_state (n, S_CONNECTED);
1358 neighbours_connected++;
1359 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1362 if (n->address_state == FRESH)
1364 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES, __LINE__);
1365 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1366 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1367 n->address_state = USED;
1370 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1372 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1374 /* Updating quotas */
1375 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1376 send_outbound_quota (target, n->bandwidth_out);
1381 GNUNET_HELLO_address_free (cc->address);
1387 * We tried to send a SESSION_CONNECT message to another peer. If this
1388 * succeeded, we change the state. If it failed, we should tell
1389 * ATS to not use this address anymore (until it is re-validated).
1391 * @param cls the 'struct NeighbourMapEntry'
1392 * @param target peer to send the message to
1393 * @param success GNUNET_OK on success
1396 send_connect_ack_continuation (void *cls,
1397 const struct GNUNET_PeerIdentity *target,
1400 struct ContinutionContext *cc = cls;
1401 struct NeighbourMapEntry *n;
1403 if (neighbours == NULL)
1405 GNUNET_HELLO_address_free (cc->address);
1407 return; /* neighbour is going away */
1410 n = lookup_neighbour (&cc->address->peer);
1411 if ((n == NULL) || (is_disconnecting (n)))
1413 GNUNET_HELLO_address_free (cc->address);
1415 return; /* neighbour is going away */
1418 if (GNUNET_YES == success)
1420 GNUNET_HELLO_address_free (cc->address);
1422 return; /* sending successful */
1425 /* sending failed, ask for next address */
1427 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1428 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1429 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1431 if (n->state != S_NOT_CONNECTED)
1432 change_state (n, S_NOT_CONNECTED);
1433 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1434 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1436 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1437 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1439 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1441 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1442 GNUNET_HELLO_address_free (cc->address);
1448 * For an existing neighbour record, set the active connection to
1449 * use the given address.
1451 * @param peer identity of the peer to switch the address for
1452 * @param address address of the other peer, NULL if other peer
1454 * @param session session to use (or NULL)
1455 * @param ats performance data
1456 * @param ats_count number of entries in ats
1457 * @param bandwidth_in inbound quota to be used when connection is up
1458 * @param bandwidth_out outbound quota to be used when connection is up
1459 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1460 * connection is not up (yet)
1463 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1464 const struct GNUNET_HELLO_Address
1466 struct Session *session,
1467 const struct GNUNET_ATS_Information *ats,
1469 struct GNUNET_BANDWIDTH_Value32NBO
1471 struct GNUNET_BANDWIDTH_Value32NBO
1474 struct NeighbourMapEntry *n;
1475 struct SessionConnectMessage connect_msg;
1476 struct ContinutionContext *cc;
1480 if (neighbours == NULL)
1482 /* This can happen during shutdown */
1485 n = lookup_neighbour (peer);
1488 if (n->state == S_DISCONNECT)
1490 /* We are disconnecting, nothing to do here */
1493 GNUNET_assert (address->transport_name != NULL);
1494 if ((session == NULL) && (0 == address->address_length))
1496 GNUNET_break_op (0);
1497 /* FIXME: is this actually possible? When does this happen? */
1498 if (strlen (address->transport_name) > 0)
1499 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1500 GNUNET_ATS_suggest_address (GST_ats, peer);
1504 /* checks successful and neighbour != NULL */
1505 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1506 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1507 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
1510 print_state (n->state));
1512 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1514 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1515 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1519 if (n->state == S_FAST_RECONNECT)
1521 /* Throtteled fast reconnect */
1523 if (NULL == n->fast_reconnect_address)
1525 n->fast_reconnect_address = GNUNET_HELLO_address_copy (address);
1528 else if (0 == GNUNET_HELLO_address_cmp(address, n->fast_reconnect_address))
1530 n->fast_reconnect_attempts ++;
1532 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1533 "FAST RECONNECT to peer `%s' and address '%s' with identical address attempt %u\n",
1534 GNUNET_i2s (&n->id), GST_plugins_a2s (address), n->fast_reconnect_attempts);
1539 n->fast_reconnect_attempts = 0;
1542 /* do not switch addresses just update quotas */
1543 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1544 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1545 (n->session == session))
1547 n->bandwidth_in = bandwidth_in;
1548 n->bandwidth_out = bandwidth_out;
1549 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1550 send_outbound_quota (peer, n->bandwidth_out);
1553 if (n->state == S_CONNECTED)
1555 /* mark old address as no longer used */
1556 GNUNET_assert (NULL != n->address);
1557 if (n->address_state == USED)
1559 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1560 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1561 n->address_state = UNUSED;
1565 /* set new address */
1566 if (NULL != n->address)
1567 GNUNET_HELLO_address_free (n->address);
1568 n->address = GNUNET_HELLO_address_copy (address);
1569 n->address_state = FRESH;
1570 n->bandwidth_in = bandwidth_in;
1571 n->bandwidth_out = bandwidth_out;
1572 GNUNET_SCHEDULER_cancel (n->timeout_task);
1574 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1575 &neighbour_timeout_task, n);
1577 if (NULL != address_change_cb && n->state == S_CONNECTED)
1578 address_change_cb (callback_cls, &n->id, n->address);
1580 /* Obtain an session for this address from plugin */
1581 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1582 papi = GST_plugins_find (address->transport_name);
1586 /* we don't have the plugin for this address */
1587 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1589 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1590 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1591 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1594 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1595 GNUNET_HELLO_address_free (n->address);
1601 if (session == NULL)
1603 n->session = papi->get_session (papi->cls, address);
1604 /* Session could not be initiated */
1605 if (n->session == NULL)
1607 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1608 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1609 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1611 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1613 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1614 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1615 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1618 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1619 GNUNET_HELLO_address_free (n->address);
1625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1626 "Obtained new session %p for peer `%s' and address '%s'\n",
1627 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1628 /* Telling ATS about new session */
1629 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1633 n->session = session;
1634 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1635 "Using existing session %p for peer `%s' and address '%s'\n",
1637 GNUNET_i2s (&n->id),
1638 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>");
1643 case S_NOT_CONNECTED:
1644 case S_CONNECT_SENT:
1645 msg_len = sizeof (struct SessionConnectMessage);
1646 connect_msg.header.size = htons (msg_len);
1647 connect_msg.header.type =
1648 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1649 connect_msg.reserved = htonl (0);
1650 connect_msg.timestamp =
1651 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1653 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1654 cc->session = n->session;
1655 cc->address = GNUNET_HELLO_address_copy (address);
1657 ret = send_with_session (n,
1658 (const char *) &connect_msg, msg_len,
1659 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1660 &send_connect_continuation, cc);
1663 case S_CONNECT_RECV:
1664 /* We received a CONNECT message and asked ATS for an address */
1665 msg_len = sizeof (struct SessionConnectMessage);
1666 connect_msg.header.size = htons (msg_len);
1667 connect_msg.header.type =
1668 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1669 connect_msg.reserved = htonl (0);
1670 connect_msg.timestamp =
1671 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1672 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1673 cc->session = n->session;
1674 cc->address = GNUNET_HELLO_address_copy (address);
1676 ret = send_with_session(n,
1677 (const void *) &connect_msg, msg_len,
1678 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1679 &send_connect_ack_continuation,
1683 case S_FAST_RECONNECT:
1684 /* connected peer is switching addresses or tries fast reconnect */
1685 msg_len = sizeof (struct SessionConnectMessage);
1686 connect_msg.header.size = htons (msg_len);
1687 connect_msg.header.type =
1688 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1689 connect_msg.reserved = htonl (0);
1690 connect_msg.timestamp =
1691 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1692 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1693 cc->session = n->session;
1694 cc->address = GNUNET_HELLO_address_copy (address);
1695 ret = send_with_session(n,
1696 (const void *) &connect_msg, msg_len,
1697 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1698 &send_switch_address_continuation, cc);
1699 if (ret == GNUNET_SYSERR)
1701 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1702 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1703 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1707 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1708 "Invalid connection state to switch addresses %u \n", n->state);
1709 GNUNET_break_op (0);
1716 * Obtain current latency information for the given neighbour.
1719 * @return observed latency of the address, FOREVER if the address was
1720 * never successfully validated
1722 struct GNUNET_TIME_Relative
1723 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1725 struct NeighbourMapEntry *n;
1727 n = lookup_neighbour (peer);
1728 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1729 return GNUNET_TIME_UNIT_FOREVER_REL;
1735 * Obtain current address information for the given neighbour.
1738 * @return address currently used
1740 struct GNUNET_HELLO_Address *
1741 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1743 struct NeighbourMapEntry *n;
1745 n = lookup_neighbour (peer);
1746 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1755 * Create an entry in the neighbour map for the given peer
1757 * @param peer peer to create an entry for
1758 * @return new neighbour map entry
1760 static struct NeighbourMapEntry *
1761 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1763 struct NeighbourMapEntry *n;
1766 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1767 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1769 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1771 n->state = S_NOT_CONNECTED;
1772 n->latency = GNUNET_TIME_relative_get_forever ();
1773 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1774 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1775 MAX_BANDWIDTH_CARRY_S);
1777 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1778 &neighbour_timeout_task, n);
1779 GNUNET_assert (GNUNET_OK ==
1780 GNUNET_CONTAINER_multihashmap_put (neighbours,
1781 &n->id.hashPubKey, n,
1782 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1788 * Try to create a connection to the given target (eventually).
1790 * @param target peer to try to connect to
1793 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1795 struct NeighbourMapEntry *n;
1797 // This can happen during shutdown
1798 if (neighbours == NULL)
1803 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1804 GNUNET_i2s (target));
1807 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1812 n = lookup_neighbour (target);
1816 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1817 return; /* already connecting or connected */
1818 if (is_disconnecting (n))
1819 change_state (n, S_NOT_CONNECTED);
1824 n = setup_neighbour (target);
1826 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1827 "Asking ATS for suggested address to connect to peer `%s'\n",
1828 GNUNET_i2s (&n->id));
1831 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1835 * Test if we're connected to the given peer.
1837 * @param target peer to test
1838 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1841 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1843 struct NeighbourMapEntry *n;
1845 // This can happen during shutdown
1846 if (neighbours == NULL)
1851 n = lookup_neighbour (target);
1853 if ((NULL == n) || (S_CONNECTED != n->state))
1854 return GNUNET_NO; /* not connected */
1859 * A session was terminated. Take note.
1861 * @param peer identity of the peer where the session died
1862 * @param session session that is gone
1865 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1866 struct Session *session)
1868 struct NeighbourMapEntry *n;
1870 if (neighbours == NULL)
1872 /* This can happen during shutdown */
1877 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1878 session, GNUNET_i2s (peer));
1881 n = lookup_neighbour (peer);
1884 if (session != n->session)
1885 return; /* doesn't affect us */
1886 if (n->state == S_CONNECTED)
1888 if (n->address_state == USED)
1890 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1891 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1892 n->address_state = UNUSED;
1897 if (NULL != n->address)
1899 GNUNET_HELLO_address_free (n->address);
1904 /* not connected anymore anyway, shouldn't matter */
1905 if (S_CONNECTED != n->state)
1908 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1910 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1911 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1912 n->expect_latency_response = GNUNET_NO;
1915 /* connected, try fast reconnect */
1916 /* statistics "transport" : "# peers connected" -= 1
1917 * neighbours_connected -= 1
1918 * BUT: no disconnect_cb to notify clients about disconnect
1921 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1924 GNUNET_assert (neighbours_connected > 0);
1925 change_state (n, S_FAST_RECONNECT);
1926 neighbours_connected--;
1927 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
1931 /* We are connected, so ask ATS to switch addresses */
1932 GNUNET_SCHEDULER_cancel (n->timeout_task);
1933 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1934 &neighbour_timeout_task, n);
1935 /* try QUICKLY to re-establish a connection, reduce timeout! */
1936 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1937 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1938 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1939 &ats_suggest_cancel,
1941 GNUNET_ATS_suggest_address (GST_ats, peer);
1946 * Transmit a message to the given target using the active connection.
1948 * @param target destination
1949 * @param msg message to send
1950 * @param msg_size number of bytes in msg
1951 * @param timeout when to fail with timeout
1952 * @param cont function to call when done
1953 * @param cont_cls closure for 'cont'
1956 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1957 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1958 GST_NeighbourSendContinuation cont, void *cont_cls)
1960 struct NeighbourMapEntry *n;
1961 struct MessageQueue *mq;
1963 // This can happen during shutdown
1964 if (neighbours == NULL)
1969 n = lookup_neighbour (target);
1970 if ((n == NULL) || (!is_connected (n)))
1972 GNUNET_STATISTICS_update (GST_stats,
1974 ("# messages not sent (no such peer or not connected)"),
1978 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1979 "Could not send message to peer `%s': unknown neighbour",
1980 GNUNET_i2s (target));
1981 else if (!is_connected (n))
1982 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1983 "Could not send message to peer `%s': not connected\n",
1984 GNUNET_i2s (target));
1987 cont (cont_cls, GNUNET_SYSERR);
1991 if ((n->session == NULL) && (n->address == NULL))
1993 GNUNET_STATISTICS_update (GST_stats,
1995 ("# messages not sent (no such peer or not connected)"),
1998 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1999 "Could not send message to peer `%s': no address available\n",
2000 GNUNET_i2s (target));
2004 cont (cont_cls, GNUNET_SYSERR);
2008 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
2009 GNUNET_STATISTICS_update (GST_stats,
2011 ("# bytes in message queue for other peers"),
2012 msg_size, GNUNET_NO);
2013 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
2015 mq->cont_cls = cont_cls;
2016 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2017 memcpy (&mq[1], msg, msg_size);
2018 mq->message_buf = (const char *) &mq[1];
2019 mq->message_buf_size = msg_size;
2020 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2021 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
2023 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
2024 (NULL == n->is_active))
2025 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
2030 * We have received a message from the given sender. How long should
2031 * we delay before receiving more? (Also used to keep the peer marked
2034 * @param sender sender of the message
2035 * @param size size of the message
2036 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
2037 * GNUNET_NO if the neighbour is not connected or violates the quota,
2038 * GNUNET_SYSERR if the connection is not fully up yet
2039 * @return how long to wait before reading more from this sender
2041 struct GNUNET_TIME_Relative
2042 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
2043 *sender, ssize_t size, int *do_forward)
2045 struct NeighbourMapEntry *n;
2046 struct GNUNET_TIME_Relative ret;
2048 // This can happen during shutdown
2049 if (neighbours == NULL)
2051 return GNUNET_TIME_UNIT_FOREVER_REL;
2054 n = lookup_neighbour (sender);
2057 GST_neighbours_try_connect (sender);
2058 n = lookup_neighbour (sender);
2061 GNUNET_STATISTICS_update (GST_stats,
2063 ("# messages discarded due to lack of neighbour record"),
2065 *do_forward = GNUNET_NO;
2066 return GNUNET_TIME_UNIT_ZERO;
2069 if (!is_connected (n))
2071 *do_forward = GNUNET_SYSERR;
2072 return GNUNET_TIME_UNIT_ZERO;
2074 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
2076 n->quota_violation_count++;
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
2080 n->in_tracker.available_bytes_per_s__,
2081 n->quota_violation_count);
2083 /* Discount 32k per violation */
2084 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
2088 if (n->quota_violation_count > 0)
2090 /* try to add 32k back */
2091 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
2092 n->quota_violation_count--;
2095 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2097 GNUNET_STATISTICS_update (GST_stats,
2099 ("# bandwidth quota violations by other peers"),
2101 *do_forward = GNUNET_NO;
2102 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
2104 *do_forward = GNUNET_YES;
2105 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
2106 if (ret.rel_value > 0)
2109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2110 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
2111 (unsigned long long) n->in_tracker.
2112 consumption_since_last_update__,
2113 (unsigned int) n->in_tracker.available_bytes_per_s__,
2114 (unsigned long long) ret.rel_value);
2116 GNUNET_STATISTICS_update (GST_stats,
2117 gettext_noop ("# ms throttling suggested"),
2118 (int64_t) ret.rel_value, GNUNET_NO);
2125 * Keep the connection to the given neighbour alive longer,
2126 * we received a KEEPALIVE (or equivalent).
2128 * @param neighbour neighbour to keep alive
2131 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
2133 struct NeighbourMapEntry *n;
2135 // This can happen during shutdown
2136 if (neighbours == NULL)
2141 n = lookup_neighbour (neighbour);
2144 GNUNET_STATISTICS_update (GST_stats,
2146 ("# KEEPALIVE messages discarded (not connected)"),
2150 GNUNET_SCHEDULER_cancel (n->timeout_task);
2152 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2153 &neighbour_timeout_task, n);
2155 /* send reply to measure latency */
2156 if (S_CONNECTED != n->state)
2159 struct GNUNET_MessageHeader m;
2161 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2162 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2164 send_with_session(n,
2165 (const void *) &m, sizeof (m),
2167 GNUNET_TIME_UNIT_FOREVER_REL,
2172 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2175 * @param neighbour neighbour to keep alive
2176 * @param ats performance data
2177 * @param ats_count number of entries in ats
2180 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2181 const struct GNUNET_ATS_Information *ats,
2184 struct NeighbourMapEntry *n;
2185 struct GNUNET_ATS_Information *ats_new;
2188 if (neighbours == NULL)
2190 // This can happen during shutdown
2194 n = lookup_neighbour (neighbour);
2195 if ((NULL == n) || (n->state != S_CONNECTED))
2197 GNUNET_STATISTICS_update (GST_stats,
2199 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2203 if (n->expect_latency_response != GNUNET_YES)
2205 GNUNET_STATISTICS_update (GST_stats,
2207 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2211 n->expect_latency_response = GNUNET_NO;
2213 GNUNET_assert (n->keep_alive_sent.abs_value !=
2214 GNUNET_TIME_absolute_get_zero ().abs_value);
2216 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2217 GNUNET_TIME_absolute_get ());
2219 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2220 GNUNET_i2s (&n->id), n->latency.rel_value);
2224 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2226 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2231 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2233 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2236 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2237 if (n->latency.rel_value > UINT32_MAX)
2238 latency = UINT32_MAX;
2240 latency = n->latency.rel_value;
2241 ats_new[ats_count].value = htonl (latency);
2243 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2245 GNUNET_free (ats_new);
2251 * Change the incoming quota for the given peer.
2253 * @param neighbour identity of peer to change qutoa for
2254 * @param quota new quota
2257 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2258 struct GNUNET_BANDWIDTH_Value32NBO quota)
2260 struct NeighbourMapEntry *n;
2262 // This can happen during shutdown
2263 if (neighbours == NULL)
2268 n = lookup_neighbour (neighbour);
2271 GNUNET_STATISTICS_update (GST_stats,
2273 ("# SET QUOTA messages ignored (no such peer)"),
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2279 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2280 ntohl (quota.value__), GNUNET_i2s (&n->id));
2282 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2283 if (0 != ntohl (quota.value__))
2286 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2287 GNUNET_i2s (&n->id), "SET_QUOTA");
2289 if (is_connected (n))
2290 GNUNET_STATISTICS_update (GST_stats,
2291 gettext_noop ("# disconnects due to quota of 0"),
2293 disconnect_neighbour (n);
2298 * Closure for the neighbours_iterate function.
2300 struct IteratorContext
2303 * Function to call on each connected neighbour.
2305 GST_NeighbourIterator cb;
2315 * Call the callback from the closure for each connected neighbour.
2317 * @param cls the 'struct IteratorContext'
2318 * @param key the hash of the public key of the neighbour
2319 * @param value the 'struct NeighbourMapEntry'
2320 * @return GNUNET_OK (continue to iterate)
2323 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2325 struct IteratorContext *ic = cls;
2326 struct NeighbourMapEntry *n = value;
2328 if (!is_connected (n))
2331 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2337 * Iterate over all connected neighbours.
2339 * @param cb function to call
2340 * @param cb_cls closure for cb
2343 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2345 struct IteratorContext ic;
2347 // This can happen during shutdown
2348 if (neighbours == NULL)
2355 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2359 * If we have an active connection to the given target, it must be shutdown.
2361 * @param target peer to disconnect from
2364 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2366 struct NeighbourMapEntry *n;
2368 // This can happen during shutdown
2369 if (neighbours == NULL)
2374 n = lookup_neighbour (target);
2376 return; /* not active */
2377 disconnect_neighbour (n);
2382 * We received a disconnect message from the given peer,
2383 * validate and process.
2385 * @param peer sender of the message
2386 * @param msg the disconnect message
2389 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2391 const struct GNUNET_MessageHeader
2394 struct NeighbourMapEntry *n;
2395 const struct SessionDisconnectMessage *sdm;
2399 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2400 "Received DISCONNECT message from peer `%s'\n",
2404 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2406 // GNUNET_break_op (0);
2407 GNUNET_STATISTICS_update (GST_stats,
2409 ("# disconnect messages ignored (old format)"), 1,
2413 sdm = (const struct SessionDisconnectMessage *) msg;
2414 n = lookup_neighbour (peer);
2416 return; /* gone already */
2417 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2418 n->connect_ts.abs_value)
2420 GNUNET_STATISTICS_update (GST_stats,
2422 ("# disconnect messages ignored (timestamp)"), 1,
2426 GNUNET_CRYPTO_hash (&sdm->public_key,
2427 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2429 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2431 GNUNET_break_op (0);
2434 if (ntohl (sdm->purpose.size) !=
2435 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2436 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2437 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2439 GNUNET_break_op (0);
2443 GNUNET_CRYPTO_rsa_verify
2444 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2445 &sdm->signature, &sdm->public_key))
2447 GNUNET_break_op (0);
2450 GST_neighbours_force_disconnect (peer);
2455 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2456 * Consider switching to it.
2458 * @param message possibly a 'struct SessionConnectMessage' (check format)
2459 * @param peer identity of the peer to switch the address for
2460 * @param address address of the other peer, NULL if other peer
2462 * @param session session to use (or NULL)
2463 * @param ats performance data
2464 * @param ats_count number of entries in ats
2467 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2468 const struct GNUNET_PeerIdentity *peer,
2469 const struct GNUNET_HELLO_Address *address,
2470 struct Session *session,
2471 const struct GNUNET_ATS_Information *ats,
2474 const struct SessionConnectMessage *scm;
2475 struct GNUNET_MessageHeader msg;
2476 struct NeighbourMapEntry *n;
2480 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2481 "Received CONNECT_ACK message from peer `%s'\n",
2484 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2486 GNUNET_break_op (0);
2489 scm = (const struct SessionConnectMessage *) message;
2490 GNUNET_break_op (ntohl (scm->reserved) == 0);
2491 n = lookup_neighbour (peer);
2494 /* we did not send 'CONNECT' -- at least not recently */
2495 GNUNET_STATISTICS_update (GST_stats,
2497 ("# unexpected CONNECT_ACK messages (no peer)"),
2504 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2506 * We also received an CONNECT message, switched from SENDT to RECV and
2507 * ATS already suggested us an address after a successful blacklist check
2510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2511 "Received CONNECT_ACK message from peer `%s' in state `%s'\n",
2513 print_state(n->state));
2515 if ((n->address != NULL) && (n->state == S_CONNECTED))
2517 /* After fast reconnect: send ACK (ACK) even when we are connected */
2518 msg_len = sizeof (msg);
2519 msg.size = htons (msg_len);
2520 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2522 ret = send_with_session(n,
2523 (const char *) &msg, msg_len,
2524 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2527 if (ret == GNUNET_SYSERR)
2528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2529 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2530 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2534 if ((n->state != S_CONNECT_SENT) &&
2535 ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2537 GNUNET_STATISTICS_update (GST_stats,
2539 ("# unexpected CONNECT_ACK messages"), 1,
2543 if (n->state != S_CONNECTED)
2544 change_state (n, S_CONNECTED);
2546 if (NULL != session)
2548 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2550 "Giving ATS session %p of plugin %s for peer %s\n",
2551 session, address->transport_name, GNUNET_i2s (peer));
2553 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2554 GNUNET_assert (NULL != n->address);
2556 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2558 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2559 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2560 n->address_state = USED;
2563 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2565 /* send ACK (ACK) */
2566 msg_len = sizeof (msg);
2567 msg.size = htons (msg_len);
2568 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2570 ret = send_with_session(n,
2571 (const char *) &msg, msg_len,
2572 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2575 if (ret == GNUNET_SYSERR)
2576 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2577 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2578 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2581 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2582 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2584 neighbours_connected++;
2585 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2589 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2590 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2593 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2594 send_outbound_quota (peer, n->bandwidth_out);
2600 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2601 const struct GNUNET_PeerIdentity *peer,
2602 const struct GNUNET_HELLO_Address *address,
2603 struct Session *session,
2604 const struct GNUNET_ATS_Information *ats,
2607 struct NeighbourMapEntry *n;
2610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2614 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2616 GNUNET_break_op (0);
2619 n = lookup_neighbour (peer);
2625 if (S_CONNECTED == n->state)
2627 if (!is_connecting (n))
2629 GNUNET_STATISTICS_update (GST_stats,
2630 gettext_noop ("# unexpected ACK messages"), 1,
2634 change_state (n, S_CONNECTED);
2635 if (NULL != session)
2636 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2638 "Giving ATS session %p of plugin %s for peer %s\n",
2639 session, address->transport_name, GNUNET_i2s (peer));
2640 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2641 GNUNET_assert (n->address != NULL);
2643 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2645 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2646 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2647 n->address_state = USED;
2651 neighbours_connected++;
2652 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2655 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2656 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2657 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2659 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2660 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2661 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2664 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2665 send_outbound_quota (peer, n->bandwidth_out);
2668 struct BlackListCheckContext
2670 struct GNUNET_ATS_Information *ats;
2674 struct Session *session;
2676 struct GNUNET_HELLO_Address *address;
2678 struct GNUNET_TIME_Absolute ts;
2683 handle_connect_blacklist_cont (void *cls,
2684 const struct GNUNET_PeerIdentity *peer,
2687 struct NeighbourMapEntry *n;
2688 struct BlackListCheckContext *bcc = cls;
2691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2692 "Blacklist check due to CONNECT message: `%s'\n",
2694 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2698 if (GNUNET_OK != result)
2700 GNUNET_HELLO_address_free (bcc->address);
2705 n = lookup_neighbour (peer);
2707 n = setup_neighbour (peer);
2709 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2711 if (NULL != bcc->session)
2712 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2714 "Giving ATS session %p of address `%s' for peer %s\n",
2715 bcc->session, GST_plugins_a2s (bcc->address),
2717 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2719 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2721 n->connect_ts = bcc->ts;
2724 GNUNET_HELLO_address_free (bcc->address);
2727 if (n->state != S_CONNECT_RECV)
2728 change_state (n, S_CONNECT_RECV);
2731 /* Ask ATS for an address to connect via that address */
2732 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2733 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2735 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2737 GNUNET_ATS_suggest_address (GST_ats, peer);
2741 * We received a 'SESSION_CONNECT' message from the other peer.
2742 * Consider switching to it.
2744 * @param message possibly a 'struct SessionConnectMessage' (check format)
2745 * @param peer identity of the peer to switch the address for
2746 * @param address address of the other peer, NULL if other peer
2748 * @param session session to use (or NULL)
2749 * @param ats performance data
2750 * @param ats_count number of entries in ats (excluding 0-termination)
2753 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2754 const struct GNUNET_PeerIdentity *peer,
2755 const struct GNUNET_HELLO_Address *address,
2756 struct Session *session,
2757 const struct GNUNET_ATS_Information *ats,
2760 const struct SessionConnectMessage *scm;
2761 struct BlackListCheckContext *bcc = NULL;
2762 struct NeighbourMapEntry *n;
2765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2766 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2769 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2771 GNUNET_break_op (0);
2775 scm = (const struct SessionConnectMessage *) message;
2776 GNUNET_break_op (ntohl (scm->reserved) == 0);
2778 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2780 n = lookup_neighbour (peer);
2781 if ((n != NULL) && ((S_CONNECTED == n->state) || (S_FAST_RECONNECT == n->state)))
2783 /* connected peer switches addresses or is trying to do a fast reconnect*/
2788 /* we are not connected to this peer */
2789 /* do blacklist check */
2791 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2792 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2793 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2794 bcc->ats_count = ats_count + 1;
2795 bcc->address = GNUNET_HELLO_address_copy (address);
2796 bcc->session = session;
2797 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2798 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2799 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2800 bcc->ats[ats_count].value =
2801 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2802 GST_blacklist_test_allowed (peer, address->transport_name,
2803 &handle_connect_blacklist_cont, bcc);
2807 /* end of file gnunet-service-transport_neighbours.c */