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)
498 GNUNET_STATISTICS_update (GST_stats,
500 ("# failed connection attempts due to timeout"), 1,
502 /* resetting state */
504 if (n->state == S_FAST_RECONNECT)
506 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
507 "Fast reconnect time out, disconnecting peer `%s'\n",
508 GNUNET_i2s (&n->id));
509 disconnect_neighbour(n);
513 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
514 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
515 GNUNET_i2s (&n->id), n, print_state(n->state), "S_NOT_CONNECTED", __LINE__);
517 n->state = S_NOT_CONNECTED;
519 /* destroying address */
520 if (n->address != NULL)
522 GNUNET_assert (strlen (n->address->transport_name) > 0);
523 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
526 /* request new address */
527 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
528 GNUNET_SCHEDULER_cancel (n->ats_suggest);
530 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
532 GNUNET_ATS_suggest_address (GST_ats, &n->id);
536 change (struct NeighbourMapEntry *n, int state, int line)
539 /* allowed transitions */
540 int allowed = GNUNET_NO;
542 previous_state = n->state;
546 case S_NOT_CONNECTED:
547 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
548 (state == S_DISCONNECT))
549 allowed = GNUNET_YES;
552 allowed = GNUNET_YES;
555 allowed = GNUNET_YES;
558 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
559 allowed = GNUNET_YES;
563 case S_FAST_RECONNECT:
564 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
565 allowed = GNUNET_YES;
571 if (allowed == GNUNET_NO)
573 char *old = GNUNET_strdup (print_state (n->state));
574 char *new = GNUNET_strdup (print_state (state));
576 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
577 "Illegal state transition from `%s' to `%s' in line %u \n", old,
582 return GNUNET_SYSERR;
585 char *old = GNUNET_strdup (print_state (n->state));
586 char *new = GNUNET_strdup (print_state (state));
588 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
589 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
590 GNUNET_i2s (&n->id), n, old, new, line);
598 case S_FAST_RECONNECT:
601 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
602 GNUNET_SCHEDULER_cancel (n->state_reset);
604 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
607 case S_NOT_CONNECTED:
609 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
612 char *old = GNUNET_strdup (print_state (n->state));
613 char *new = GNUNET_strdup (print_state (state));
615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
616 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
617 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), old, new);
621 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
622 GNUNET_SCHEDULER_cancel (n->state_reset);
623 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
631 if (NULL != address_change_cb)
633 if (n->state == S_CONNECTED)
634 address_change_cb (callback_cls, &n->id, n->address);
635 else if (previous_state == S_CONNECTED)
636 address_change_cb (callback_cls, &n->id, NULL);
643 send_with_session (struct NeighbourMapEntry *n,
644 const char *msgbuf, size_t msgbuf_size,
646 struct GNUNET_TIME_Relative timeout,
647 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
649 struct GNUNET_TRANSPORT_PluginFunctions *papi;
650 size_t ret = GNUNET_SYSERR;
652 GNUNET_assert (n != NULL);
653 GNUNET_assert (n->session != NULL);
655 papi = GST_plugins_find (n->address->transport_name);
659 cont (cont_cls, &n->id, GNUNET_SYSERR);
660 return GNUNET_SYSERR;
663 ret = papi->send (papi->cls,
670 if ((ret == -1) && (cont != NULL))
671 cont (cont_cls, &n->id, GNUNET_SYSERR);
676 * Task invoked to start a transmission to another peer.
678 * @param cls the 'struct NeighbourMapEntry'
679 * @param tc scheduler context
682 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
686 * We're done with our transmission attempt, continue processing.
688 * @param cls the 'struct MessageQueue' of the message
689 * @param receiver intended receiver
690 * @param success whether it worked or not
693 transmit_send_continuation (void *cls,
694 const struct GNUNET_PeerIdentity *receiver,
697 struct MessageQueue *mq = cls;
698 struct NeighbourMapEntry *n;
699 struct NeighbourMapEntry *tmp;
701 tmp = lookup_neighbour (receiver);
703 if ((NULL != n) && (tmp != NULL) && (tmp == n))
705 GNUNET_assert (n->is_active == mq);
707 if (success == GNUNET_YES)
709 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
710 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
713 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
714 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
715 (success == GNUNET_OK) ? "successful" : "FAILED");
716 if (NULL != mq->cont)
717 mq->cont (mq->cont_cls, success);
723 * Check the ready list for the given neighbour and if a plugin is
724 * ready for transmission (and if we have a message), do so!
726 * @param n target peer for which to transmit
729 try_transmission_to_peer (struct NeighbourMapEntry *n)
731 struct MessageQueue *mq;
732 struct GNUNET_TIME_Relative timeout;
735 if (n->is_active != NULL)
738 return; /* transmission already pending */
740 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
743 return; /* currently waiting for bandwidth */
745 while (NULL != (mq = n->messages_head))
747 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
748 if (timeout.rel_value > 0)
750 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
753 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
756 return; /* no more messages */
758 if (n->address == NULL)
760 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
761 GNUNET_i2s (&n->id));
762 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
763 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
764 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
765 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
769 if (GST_plugins_find (n->address->transport_name) == NULL)
774 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
778 if ((n->address->address_length == 0) && (n->session == NULL))
780 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
781 GNUNET_i2s (&n->id));
782 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
783 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
784 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
788 ret = send_with_session(n,
789 mq->message_buf, mq->message_buf_size,
791 &transmit_send_continuation, mq);
795 /* failure, but 'send' would not call continuation in this case,
796 * so we need to do it here! */
797 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
804 * Task invoked to start a transmission to another peer.
806 * @param cls the 'struct NeighbourMapEntry'
807 * @param tc scheduler context
810 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
812 struct NeighbourMapEntry *n = cls;
814 GNUNET_assert (NULL != lookup_neighbour (&n->id));
815 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
816 try_transmission_to_peer (n);
821 * Initialize the neighbours subsystem.
823 * @param cls closure for callbacks
824 * @param connect_cb function to call if we connect to a peer
825 * @param disconnect_cb function to call if we disconnect from a peer
826 * @param peer_address_cb function to call if we change an active address
830 GST_neighbours_start (void *cls,
831 GNUNET_TRANSPORT_NotifyConnect connect_cb,
832 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
833 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
836 connect_notify_cb = connect_cb;
837 disconnect_notify_cb = disconnect_cb;
838 address_change_cb = peer_address_cb;
839 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
840 neighbours_connected = 0;
845 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
849 "Sending DISCONNECT message to peer `%4s': %i\n",
850 GNUNET_i2s (target), result);
855 send_disconnect (struct NeighbourMapEntry * n)
858 struct SessionDisconnectMessage disconnect_msg;
860 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
861 "Sending DISCONNECT message to peer `%4s'\n",
862 GNUNET_i2s (&n->id));
863 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
864 disconnect_msg.header.type =
865 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
866 disconnect_msg.reserved = htonl (0);
867 disconnect_msg.purpose.size =
868 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
869 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
870 sizeof (struct GNUNET_TIME_AbsoluteNBO));
871 disconnect_msg.purpose.purpose =
872 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
873 disconnect_msg.timestamp =
874 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
875 disconnect_msg.public_key = GST_my_public_key;
876 GNUNET_assert (GNUNET_OK ==
877 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
878 &disconnect_msg.purpose,
879 &disconnect_msg.signature));
881 ret = send_with_session (n,
882 (const char *) &disconnect_msg, sizeof (disconnect_msg),
883 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
884 &send_disconnect_cont, NULL);
886 if (ret == GNUNET_SYSERR)
887 return GNUNET_SYSERR;
889 GNUNET_STATISTICS_update (GST_stats,
891 ("# peers disconnected due to external request"), 1,
898 * Disconnect from the given neighbour, clean up the record.
900 * @param n neighbour to disconnect from
903 disconnect_neighbour (struct NeighbourMapEntry *n)
905 struct MessageQueue *mq;
908 previous_state = n->state;
910 if (is_disconnecting (n))
913 /* send DISCONNECT MESSAGE */
914 if (previous_state == S_CONNECTED)
916 if (GNUNET_OK == send_disconnect (n))
917 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
918 GNUNET_i2s (&n->id));
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Could not send DISCONNECT_MSG to `%s'\n",
922 GNUNET_i2s (&n->id));
925 change_state (n, S_DISCONNECT);
927 if (previous_state == S_CONNECTED)
929 GNUNET_assert (NULL != n->address);
930 if (n->address_state == USED)
932 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
933 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
934 n->address_state = UNUSED;
938 if (n->address != NULL)
940 struct GNUNET_TRANSPORT_PluginFunctions *papi;
942 papi = GST_plugins_find (n->address->transport_name);
944 papi->disconnect (papi->cls, &n->id);
946 while (NULL != (mq = n->messages_head))
948 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
949 if (NULL != mq->cont)
950 mq->cont (mq->cont_cls, GNUNET_SYSERR);
953 if (NULL != n->is_active)
955 n->is_active->n = NULL;
959 switch (previous_state)
962 GNUNET_assert (neighbours_connected > 0);
963 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
964 GNUNET_SCHEDULER_cancel (n->keepalive_task);
965 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
966 n->expect_latency_response = GNUNET_NO;
967 neighbours_connected--;
968 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
970 disconnect_notify_cb (callback_cls, &n->id);
972 case S_FAST_RECONNECT:
973 GNUNET_STATISTICS_update (GST_stats,
974 gettext_noop ("# fast reconnects failed"), 1,
976 disconnect_notify_cb (callback_cls, &n->id);
982 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
984 GNUNET_assert (GNUNET_YES ==
985 GNUNET_CONTAINER_multihashmap_remove (neighbours,
986 &n->id.hashPubKey, n));
987 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
989 GNUNET_SCHEDULER_cancel (n->ats_suggest);
990 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
992 if (n->ats_request != GNUNET_SCHEDULER_NO_TASK)
994 GNUNET_SCHEDULER_cancel (n->ats_request);
995 n->ats_request = GNUNET_SCHEDULER_NO_TASK;
998 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
1000 GNUNET_SCHEDULER_cancel (n->timeout_task);
1001 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1003 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
1005 GNUNET_SCHEDULER_cancel (n->transmission_task);
1006 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
1008 if (NULL != n->fast_reconnect_address)
1010 GNUNET_HELLO_address_free (n->fast_reconnect_address);
1011 n->fast_reconnect_address = NULL;
1013 if (NULL != n->address)
1015 GNUNET_HELLO_address_free (n->address);
1019 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
1020 GNUNET_i2s (&n->id), n);
1026 * Peer has been idle for too long. Disconnect.
1028 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1029 * @param tc scheduler context
1032 neighbour_timeout_task (void *cls,
1033 const struct GNUNET_SCHEDULER_TaskContext *tc)
1035 struct NeighbourMapEntry *n = cls;
1037 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1038 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer`%4s' disconnected due to timeout\n",
1039 GNUNET_i2s (&n->id));
1041 GNUNET_STATISTICS_update (GST_stats,
1043 ("# peers disconnected due to timeout"), 1,
1045 disconnect_neighbour (n);
1050 * Send another keepalive message.
1052 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1053 * @param tc scheduler context
1056 neighbour_keepalive_task (void *cls,
1057 const struct GNUNET_SCHEDULER_TaskContext *tc)
1059 struct NeighbourMapEntry *n = cls;
1060 struct GNUNET_MessageHeader m;
1063 GNUNET_assert (S_CONNECTED == n->state);
1065 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1066 &neighbour_keepalive_task, n);
1068 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1070 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1071 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1073 ret = send_with_session (n,
1074 (const void *) &m, sizeof (m),
1075 UINT32_MAX /* priority */ ,
1076 GNUNET_TIME_UNIT_FOREVER_REL,
1079 n->expect_latency_response = GNUNET_NO;
1080 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero ();
1081 if (ret != GNUNET_SYSERR)
1083 n->expect_latency_response = GNUNET_YES;
1084 n->keep_alive_sent = GNUNET_TIME_absolute_get ();
1091 * Disconnect from the given neighbour.
1094 * @param key hash of neighbour's public key (not used)
1095 * @param value the 'struct NeighbourMapEntry' of the neighbour
1098 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1100 struct NeighbourMapEntry *n = value;
1102 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1103 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1104 if (S_CONNECTED == n->state)
1105 GNUNET_STATISTICS_update (GST_stats,
1107 ("# peers disconnected due to global disconnect"),
1109 disconnect_neighbour (n);
1115 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1117 struct NeighbourMapEntry *n = cls;
1119 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1121 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1122 "ATS did not suggested address to connect to peer `%s'\n",
1123 GNUNET_i2s (&n->id));
1125 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# ATS address suggestions failed"), 1,
1129 disconnect_neighbour (n);
1133 * Cleanup the neighbours subsystem.
1136 GST_neighbours_stop ()
1138 // This can happen during shutdown
1139 if (neighbours == NULL)
1144 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1146 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1147 // GNUNET_assert (neighbours_connected == 0);
1149 callback_cls = NULL;
1150 connect_notify_cb = NULL;
1151 disconnect_notify_cb = NULL;
1152 address_change_cb = NULL;
1155 struct ContinutionContext
1157 struct GNUNET_HELLO_Address *address;
1159 struct Session *session;
1163 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
1164 struct GNUNET_BANDWIDTH_Value32NBO quota)
1166 struct QuotaSetMessage q_msg;
1168 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1169 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1170 ntohl (quota.value__), GNUNET_i2s (target));
1171 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1172 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1173 q_msg.quota = quota;
1174 q_msg.peer = (*target);
1175 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1179 * We tried to send a SESSION_CONNECT message to another peer. If this
1180 * succeeded, we change the state. If it failed, we should tell
1181 * ATS to not use this address anymore (until it is re-validated).
1183 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1184 * @param target peer to send the message to
1185 * @param success GNUNET_OK on success
1188 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1191 struct ContinutionContext *cc = cls;
1192 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1194 if (GNUNET_YES != success)
1196 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1197 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1199 if ((NULL == neighbours) || (NULL == n) || (n->state == S_DISCONNECT))
1201 GNUNET_HELLO_address_free (cc->address);
1206 if ((GNUNET_YES == success) &&
1207 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1209 change_state (n, S_CONNECT_SENT);
1210 GNUNET_HELLO_address_free (cc->address);
1215 if ((GNUNET_NO == success) &&
1216 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1218 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1219 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1220 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1221 change_state (n, S_NOT_CONNECTED);
1222 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1223 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1225 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1227 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1229 GNUNET_HELLO_address_free (cc->address);
1235 ats_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1237 struct NeighbourMapEntry *n = cls;
1238 n->ats_request = GNUNET_SCHEDULER_NO_TASK;
1241 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1247 * We tried to switch addresses with an peer already connected. If it failed,
1248 * we should tell ATS to not use this address anymore (until it is re-validated).
1250 * @param cls the 'struct NeighbourMapEntry'
1251 * @param target peer to send the message to
1252 * @param success GNUNET_OK on success
1255 send_switch_address_continuation (void *cls,
1256 const struct GNUNET_PeerIdentity *target,
1259 struct ContinutionContext *cc = cls;
1260 struct NeighbourMapEntry *n;
1262 if (neighbours == NULL)
1264 GNUNET_HELLO_address_free (cc->address);
1266 return; /* neighbour is going away */
1269 n = lookup_neighbour (&cc->address->peer);
1270 if ((n == NULL) || (is_disconnecting (n)))
1272 GNUNET_HELLO_address_free (cc->address);
1274 return; /* neighbour is going away */
1277 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1278 if (GNUNET_YES != success)
1281 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1282 "Failed to switch connected peer `%s' in state %s to address '%s' session %X, asking ATS for new address \n",
1283 GNUNET_i2s (&n->id), print_state(n->state), GST_plugins_a2s (cc->address), cc->session);
1285 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1286 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1288 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1290 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1291 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1294 if (n->state == S_FAST_RECONNECT)
1296 if (GNUNET_SCHEDULER_NO_TASK == n->ats_request)
1298 /* Throtteled ATS request for fast reconnect */
1299 struct GNUNET_TIME_Relative delay = GNUNET_TIME_relative_multiply(FAST_RECONNECT_RATE, n->fast_reconnect_attempts);
1300 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1301 "Fast reconnect attempt %u failed, delay ATS request for %llu ms\n", n->fast_reconnect_attempts, delay.rel_value);
1303 GNUNET_SCHEDULER_add_delayed (delay,
1310 /* Immediate ATS request for connected peers */
1312 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1314 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1316 GNUNET_HELLO_address_free (cc->address);
1320 /* Tell ATS that switching addresses was successful */
1324 if (n->address_state == FRESH)
1326 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES, __LINE__);
1327 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1328 GNUNET_break (cc->session == n->session);
1329 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1330 n->address_state = USED;
1333 case S_FAST_RECONNECT:
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "Successful fast reconnect to peer `%s'\n",
1336 GNUNET_i2s (&n->id));
1337 change_state (n, S_CONNECTED);
1338 neighbours_connected++;
1339 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
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_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1347 n->address_state = USED;
1350 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1352 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1354 /* Updating quotas */
1355 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1356 send_outbound_quota (target, n->bandwidth_out);
1361 GNUNET_HELLO_address_free (cc->address);
1367 * We tried to send a SESSION_CONNECT message to another peer. If this
1368 * succeeded, we change the state. If it failed, we should tell
1369 * ATS to not use this address anymore (until it is re-validated).
1371 * @param cls the 'struct NeighbourMapEntry'
1372 * @param target peer to send the message to
1373 * @param success GNUNET_OK on success
1376 send_connect_ack_continuation (void *cls,
1377 const struct GNUNET_PeerIdentity *target,
1380 struct ContinutionContext *cc = cls;
1381 struct NeighbourMapEntry *n;
1383 if (neighbours == NULL)
1385 GNUNET_HELLO_address_free (cc->address);
1387 return; /* neighbour is going away */
1390 n = lookup_neighbour (&cc->address->peer);
1391 if ((n == NULL) || (is_disconnecting (n)))
1393 GNUNET_HELLO_address_free (cc->address);
1395 return; /* neighbour is going away */
1398 if (GNUNET_YES == success)
1400 GNUNET_HELLO_address_free (cc->address);
1402 return; /* sending successful */
1405 /* sending failed, ask for next address */
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1408 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1409 if (n->state != S_NOT_CONNECTED)
1410 change_state (n, S_NOT_CONNECTED);
1411 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1412 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1414 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1415 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1417 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1419 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1420 GNUNET_HELLO_address_free (cc->address);
1426 * For an existing neighbour record, set the active connection to
1427 * use the given address.
1429 * @param peer identity of the peer to switch the address for
1430 * @param address address of the other peer, NULL if other peer
1432 * @param session session to use (or NULL)
1433 * @param ats performance data
1434 * @param ats_count number of entries in ats
1435 * @param bandwidth_in inbound quota to be used when connection is up
1436 * @param bandwidth_out outbound quota to be used when connection is up
1437 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1438 * connection is not up (yet)
1441 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1442 const struct GNUNET_HELLO_Address
1444 struct Session *session,
1445 const struct GNUNET_ATS_Information *ats,
1447 struct GNUNET_BANDWIDTH_Value32NBO
1449 struct GNUNET_BANDWIDTH_Value32NBO
1452 struct NeighbourMapEntry *n;
1453 struct SessionConnectMessage connect_msg;
1454 struct ContinutionContext *cc;
1458 if (neighbours == NULL)
1460 /* This can happen during shutdown */
1463 n = lookup_neighbour (peer);
1466 if (n->state == S_DISCONNECT)
1468 /* We are disconnecting, nothing to do here */
1471 GNUNET_assert (address->transport_name != NULL);
1472 if ((session == NULL) && (0 == address->address_length))
1474 GNUNET_break_op (0);
1475 /* FIXME: is this actually possible? When does this happen? */
1476 if (strlen (address->transport_name) > 0)
1477 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1478 GNUNET_ATS_suggest_address (GST_ats, peer);
1482 /* checks successful and neighbour != NULL */
1483 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1484 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1485 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
1488 print_state (n->state));
1490 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1492 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1493 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1497 if (n->state == S_FAST_RECONNECT)
1499 /* Throtteled fast reconnect */
1501 if (NULL == n->fast_reconnect_address)
1503 n->fast_reconnect_address = GNUNET_HELLO_address_copy (address);
1506 else if (0 == GNUNET_HELLO_address_cmp(address, n->fast_reconnect_address))
1508 n->fast_reconnect_attempts ++;
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "FAST RECONNECT to peer `%s' and address '%s' with identical address attempt %u\n",
1512 GNUNET_i2s (&n->id), GST_plugins_a2s (address), n->fast_reconnect_attempts);
1517 n->fast_reconnect_attempts = 0;
1520 /* do not switch addresses just update quotas */
1521 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1522 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1523 (n->session == session))
1525 n->bandwidth_in = bandwidth_in;
1526 n->bandwidth_out = bandwidth_out;
1527 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1528 send_outbound_quota (peer, n->bandwidth_out);
1531 if (n->state == S_CONNECTED)
1533 /* mark old address as no longer used */
1534 GNUNET_assert (NULL != n->address);
1535 if (n->address_state == USED)
1537 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1538 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1539 n->address_state = UNUSED;
1543 /* set new address */
1544 if (NULL != n->address)
1545 GNUNET_HELLO_address_free (n->address);
1546 n->address = GNUNET_HELLO_address_copy (address);
1547 n->address_state = FRESH;
1548 n->bandwidth_in = bandwidth_in;
1549 n->bandwidth_out = bandwidth_out;
1550 GNUNET_SCHEDULER_cancel (n->timeout_task);
1552 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1553 &neighbour_timeout_task, n);
1555 if (NULL != address_change_cb && n->state == S_CONNECTED)
1556 address_change_cb (callback_cls, &n->id, n->address);
1558 /* Obtain an session for this address from plugin */
1559 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1560 papi = GST_plugins_find (address->transport_name);
1564 /* we don't have the plugin for this address */
1565 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1567 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1568 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1569 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1572 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1573 GNUNET_HELLO_address_free (n->address);
1579 if (session == NULL)
1581 n->session = papi->get_session (papi->cls, address);
1582 /* Session could not be initiated */
1583 if (n->session == NULL)
1585 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1586 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1587 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1589 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1591 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1592 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1593 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1596 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1597 GNUNET_HELLO_address_free (n->address);
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Obtained new session %p for peer `%s' and address '%s'\n",
1605 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1606 /* Telling ATS about new session */
1607 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1611 n->session = session;
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1613 "Using existing session %p for peer `%s' and address '%s'\n",
1615 GNUNET_i2s (&n->id),
1616 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>");
1621 case S_NOT_CONNECTED:
1622 case S_CONNECT_SENT:
1623 msg_len = sizeof (struct SessionConnectMessage);
1624 connect_msg.header.size = htons (msg_len);
1625 connect_msg.header.type =
1626 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1627 connect_msg.reserved = htonl (0);
1628 connect_msg.timestamp =
1629 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1631 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1632 cc->session = n->session;
1633 cc->address = GNUNET_HELLO_address_copy (address);
1635 ret = send_with_session (n,
1636 (const char *) &connect_msg, msg_len,
1637 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1638 &send_connect_continuation, cc);
1641 case S_CONNECT_RECV:
1642 /* We received a CONNECT message and asked ATS for an address */
1643 msg_len = sizeof (struct SessionConnectMessage);
1644 connect_msg.header.size = htons (msg_len);
1645 connect_msg.header.type =
1646 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1647 connect_msg.reserved = htonl (0);
1648 connect_msg.timestamp =
1649 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1650 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1651 cc->session = n->session;
1652 cc->address = GNUNET_HELLO_address_copy (address);
1654 ret = send_with_session(n,
1655 (const void *) &connect_msg, msg_len,
1656 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1657 &send_connect_ack_continuation,
1661 case S_FAST_RECONNECT:
1662 /* connected peer is switching addresses or tries fast reconnect */
1663 msg_len = sizeof (struct SessionConnectMessage);
1664 connect_msg.header.size = htons (msg_len);
1665 connect_msg.header.type =
1666 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1667 connect_msg.reserved = htonl (0);
1668 connect_msg.timestamp =
1669 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1670 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1671 cc->session = n->session;
1672 cc->address = GNUNET_HELLO_address_copy (address);
1673 ret = send_with_session(n,
1674 (const void *) &connect_msg, msg_len,
1675 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1676 &send_switch_address_continuation, cc);
1677 if (ret == GNUNET_SYSERR)
1679 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1680 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1681 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1685 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1686 "Invalid connection state to switch addresses %u \n", n->state);
1687 GNUNET_break_op (0);
1694 * Obtain current latency information for the given neighbour.
1697 * @return observed latency of the address, FOREVER if the address was
1698 * never successfully validated
1700 struct GNUNET_TIME_Relative
1701 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1703 struct NeighbourMapEntry *n;
1705 n = lookup_neighbour (peer);
1706 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1707 return GNUNET_TIME_UNIT_FOREVER_REL;
1713 * Obtain current address information for the given neighbour.
1716 * @return address currently used
1718 struct GNUNET_HELLO_Address *
1719 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1721 struct NeighbourMapEntry *n;
1723 n = lookup_neighbour (peer);
1724 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1733 * Create an entry in the neighbour map for the given peer
1735 * @param peer peer to create an entry for
1736 * @return new neighbour map entry
1738 static struct NeighbourMapEntry *
1739 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1741 struct NeighbourMapEntry *n;
1743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1744 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1745 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1747 n->state = S_NOT_CONNECTED;
1748 n->latency = GNUNET_TIME_relative_get_forever ();
1749 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1750 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1751 MAX_BANDWIDTH_CARRY_S);
1753 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1754 &neighbour_timeout_task, n);
1755 GNUNET_assert (GNUNET_OK ==
1756 GNUNET_CONTAINER_multihashmap_put (neighbours,
1757 &n->id.hashPubKey, n,
1758 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1764 * Try to create a connection to the given target (eventually).
1766 * @param target peer to try to connect to
1769 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1771 struct NeighbourMapEntry *n;
1773 // This can happen during shutdown
1774 if (neighbours == NULL)
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1779 GNUNET_i2s (target));
1781 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1786 n = lookup_neighbour (target);
1790 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1791 return; /* already connecting or connected */
1792 if (is_disconnecting (n))
1793 change_state (n, S_NOT_CONNECTED);
1798 n = setup_neighbour (target);
1799 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1800 "Asking ATS for suggested address to connect to peer `%s'\n",
1801 GNUNET_i2s (&n->id));
1802 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1806 * Test if we're connected to the given peer.
1808 * @param target peer to test
1809 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1812 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1814 struct NeighbourMapEntry *n;
1816 // This can happen during shutdown
1817 if (neighbours == NULL)
1822 n = lookup_neighbour (target);
1824 if ((NULL == n) || (S_CONNECTED != n->state))
1825 return GNUNET_NO; /* not connected */
1830 * A session was terminated. Take note.
1832 * @param peer identity of the peer where the session died
1833 * @param session session that is gone
1836 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1837 struct Session *session)
1839 struct NeighbourMapEntry *n;
1841 if (neighbours == NULL)
1843 /* This can happen during shutdown */
1846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1847 session, GNUNET_i2s (peer));
1848 n = lookup_neighbour (peer);
1851 if (session != n->session)
1852 return; /* doesn't affect us */
1853 if (n->state == S_CONNECTED)
1855 if (n->address_state == USED)
1857 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1858 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1859 n->address_state = UNUSED;
1864 if (NULL != n->address)
1866 GNUNET_HELLO_address_free (n->address);
1871 /* not connected anymore anyway, shouldn't matter */
1872 if (S_CONNECTED != n->state)
1875 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1877 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1878 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1879 n->expect_latency_response = GNUNET_NO;
1882 /* connected, try fast reconnect */
1883 /* statistics "transport" : "# peers connected" -= 1
1884 * neighbours_connected -= 1
1885 * BUT: no disconnect_cb to notify clients about disconnect
1888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1891 GNUNET_assert (neighbours_connected > 0);
1892 change_state (n, S_FAST_RECONNECT);
1893 neighbours_connected--;
1894 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
1898 /* We are connected, so ask ATS to switch addresses */
1899 GNUNET_SCHEDULER_cancel (n->timeout_task);
1900 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1901 &neighbour_timeout_task, n);
1902 /* try QUICKLY to re-establish a connection, reduce timeout! */
1903 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1904 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1905 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1906 &ats_suggest_cancel,
1908 GNUNET_ATS_suggest_address (GST_ats, peer);
1913 * Transmit a message to the given target using the active connection.
1915 * @param target destination
1916 * @param msg message to send
1917 * @param msg_size number of bytes in msg
1918 * @param timeout when to fail with timeout
1919 * @param cont function to call when done
1920 * @param cont_cls closure for 'cont'
1923 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1924 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1925 GST_NeighbourSendContinuation cont, void *cont_cls)
1927 struct NeighbourMapEntry *n;
1928 struct MessageQueue *mq;
1930 // This can happen during shutdown
1931 if (neighbours == NULL)
1936 n = lookup_neighbour (target);
1937 if ((n == NULL) || (!is_connected (n)))
1939 GNUNET_STATISTICS_update (GST_stats,
1941 ("# messages not sent (no such peer or not connected)"),
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1945 "Could not send message to peer `%s': unknown neighbour",
1946 GNUNET_i2s (target));
1947 else if (!is_connected (n))
1948 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1949 "Could not send message to peer `%s': not connected\n",
1950 GNUNET_i2s (target));
1952 cont (cont_cls, GNUNET_SYSERR);
1956 if ((n->session == NULL) && (n->address == NULL))
1958 GNUNET_STATISTICS_update (GST_stats,
1960 ("# messages not sent (no such peer or not connected)"),
1962 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1963 "Could not send message to peer `%s': no address available\n",
1964 GNUNET_i2s (target));
1966 cont (cont_cls, GNUNET_SYSERR);
1970 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1971 GNUNET_STATISTICS_update (GST_stats,
1973 ("# bytes in message queue for other peers"),
1974 msg_size, GNUNET_NO);
1975 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1977 mq->cont_cls = cont_cls;
1978 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1979 memcpy (&mq[1], msg, msg_size);
1980 mq->message_buf = (const char *) &mq[1];
1981 mq->message_buf_size = msg_size;
1982 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1983 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1985 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1986 (NULL == n->is_active))
1987 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1992 * We have received a message from the given sender. How long should
1993 * we delay before receiving more? (Also used to keep the peer marked
1996 * @param sender sender of the message
1997 * @param size size of the message
1998 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1999 * GNUNET_NO if the neighbour is not connected or violates the quota,
2000 * GNUNET_SYSERR if the connection is not fully up yet
2001 * @return how long to wait before reading more from this sender
2003 struct GNUNET_TIME_Relative
2004 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
2005 *sender, ssize_t size, int *do_forward)
2007 struct NeighbourMapEntry *n;
2008 struct GNUNET_TIME_Relative ret;
2010 // This can happen during shutdown
2011 if (neighbours == NULL)
2013 return GNUNET_TIME_UNIT_FOREVER_REL;
2016 n = lookup_neighbour (sender);
2019 GST_neighbours_try_connect (sender);
2020 n = lookup_neighbour (sender);
2023 GNUNET_STATISTICS_update (GST_stats,
2025 ("# messages discarded due to lack of neighbour record"),
2027 *do_forward = GNUNET_NO;
2028 return GNUNET_TIME_UNIT_ZERO;
2031 if (!is_connected (n))
2033 *do_forward = GNUNET_SYSERR;
2034 return GNUNET_TIME_UNIT_ZERO;
2036 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
2038 n->quota_violation_count++;
2039 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2040 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
2041 n->in_tracker.available_bytes_per_s__,
2042 n->quota_violation_count);
2043 /* Discount 32k per violation */
2044 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
2048 if (n->quota_violation_count > 0)
2050 /* try to add 32k back */
2051 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
2052 n->quota_violation_count--;
2055 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2057 GNUNET_STATISTICS_update (GST_stats,
2059 ("# bandwidth quota violations by other peers"),
2061 *do_forward = GNUNET_NO;
2062 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
2064 *do_forward = GNUNET_YES;
2065 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
2066 if (ret.rel_value > 0)
2068 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2069 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
2070 (unsigned long long) n->in_tracker.
2071 consumption_since_last_update__,
2072 (unsigned int) n->in_tracker.available_bytes_per_s__,
2073 (unsigned long long) ret.rel_value);
2074 GNUNET_STATISTICS_update (GST_stats,
2075 gettext_noop ("# ms throttling suggested"),
2076 (int64_t) ret.rel_value, GNUNET_NO);
2083 * Keep the connection to the given neighbour alive longer,
2084 * we received a KEEPALIVE (or equivalent).
2086 * @param neighbour neighbour to keep alive
2089 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
2091 struct NeighbourMapEntry *n;
2093 // This can happen during shutdown
2094 if (neighbours == NULL)
2099 n = lookup_neighbour (neighbour);
2102 GNUNET_STATISTICS_update (GST_stats,
2104 ("# KEEPALIVE messages discarded (not connected)"),
2108 GNUNET_SCHEDULER_cancel (n->timeout_task);
2110 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2111 &neighbour_timeout_task, n);
2113 /* send reply to measure latency */
2114 if (S_CONNECTED != n->state)
2117 struct GNUNET_MessageHeader m;
2119 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2120 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2122 send_with_session(n,
2123 (const void *) &m, sizeof (m),
2125 GNUNET_TIME_UNIT_FOREVER_REL,
2130 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2133 * @param neighbour neighbour to keep alive
2134 * @param ats performance data
2135 * @param ats_count number of entries in ats
2138 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2139 const struct GNUNET_ATS_Information *ats,
2142 struct NeighbourMapEntry *n;
2143 struct GNUNET_ATS_Information *ats_new;
2146 if (neighbours == NULL)
2148 // This can happen during shutdown
2152 n = lookup_neighbour (neighbour);
2153 if ((NULL == n) || (n->state != S_CONNECTED))
2155 GNUNET_STATISTICS_update (GST_stats,
2157 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2161 if (n->expect_latency_response != GNUNET_YES)
2163 GNUNET_STATISTICS_update (GST_stats,
2165 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2169 n->expect_latency_response = GNUNET_NO;
2171 GNUNET_assert (n->keep_alive_sent.abs_value !=
2172 GNUNET_TIME_absolute_get_zero ().abs_value);
2174 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2175 GNUNET_TIME_absolute_get ());
2176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2177 GNUNET_i2s (&n->id), n->latency.rel_value);
2179 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2181 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2186 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2188 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2191 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2192 if (n->latency.rel_value > UINT32_MAX)
2193 latency = UINT32_MAX;
2195 latency = n->latency.rel_value;
2196 ats_new[ats_count].value = htonl (latency);
2198 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2200 GNUNET_free (ats_new);
2206 * Change the incoming quota for the given peer.
2208 * @param neighbour identity of peer to change qutoa for
2209 * @param quota new quota
2212 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2213 struct GNUNET_BANDWIDTH_Value32NBO quota)
2215 struct NeighbourMapEntry *n;
2217 // This can happen during shutdown
2218 if (neighbours == NULL)
2223 n = lookup_neighbour (neighbour);
2226 GNUNET_STATISTICS_update (GST_stats,
2228 ("# SET QUOTA messages ignored (no such peer)"),
2232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2233 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2234 ntohl (quota.value__), GNUNET_i2s (&n->id));
2235 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2236 if (0 != ntohl (quota.value__))
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2239 GNUNET_i2s (&n->id), "SET_QUOTA");
2240 if (is_connected (n))
2241 GNUNET_STATISTICS_update (GST_stats,
2242 gettext_noop ("# disconnects due to quota of 0"),
2244 disconnect_neighbour (n);
2249 * Closure for the neighbours_iterate function.
2251 struct IteratorContext
2254 * Function to call on each connected neighbour.
2256 GST_NeighbourIterator cb;
2266 * Call the callback from the closure for each connected neighbour.
2268 * @param cls the 'struct IteratorContext'
2269 * @param key the hash of the public key of the neighbour
2270 * @param value the 'struct NeighbourMapEntry'
2271 * @return GNUNET_OK (continue to iterate)
2274 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2276 struct IteratorContext *ic = cls;
2277 struct NeighbourMapEntry *n = value;
2279 if (!is_connected (n))
2282 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2288 * Iterate over all connected neighbours.
2290 * @param cb function to call
2291 * @param cb_cls closure for cb
2294 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2296 struct IteratorContext ic;
2298 // This can happen during shutdown
2299 if (neighbours == NULL)
2306 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2310 * If we have an active connection to the given target, it must be shutdown.
2312 * @param target peer to disconnect from
2315 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2317 struct NeighbourMapEntry *n;
2319 // This can happen during shutdown
2320 if (neighbours == NULL)
2325 n = lookup_neighbour (target);
2327 return; /* not active */
2328 disconnect_neighbour (n);
2333 * We received a disconnect message from the given peer,
2334 * validate and process.
2336 * @param peer sender of the message
2337 * @param msg the disconnect message
2340 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2342 const struct GNUNET_MessageHeader
2345 struct NeighbourMapEntry *n;
2346 const struct SessionDisconnectMessage *sdm;
2349 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2350 "Received DISCONNECT message from peer `%s'\n",
2352 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2354 // GNUNET_break_op (0);
2355 GNUNET_STATISTICS_update (GST_stats,
2357 ("# disconnect messages ignored (old format)"), 1,
2361 sdm = (const struct SessionDisconnectMessage *) msg;
2362 n = lookup_neighbour (peer);
2364 return; /* gone already */
2365 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2366 n->connect_ts.abs_value)
2368 GNUNET_STATISTICS_update (GST_stats,
2370 ("# disconnect messages ignored (timestamp)"), 1,
2374 GNUNET_CRYPTO_hash (&sdm->public_key,
2375 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2377 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2379 GNUNET_break_op (0);
2382 if (ntohl (sdm->purpose.size) !=
2383 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2384 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2385 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2387 GNUNET_break_op (0);
2391 GNUNET_CRYPTO_rsa_verify
2392 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2393 &sdm->signature, &sdm->public_key))
2395 GNUNET_break_op (0);
2398 GST_neighbours_force_disconnect (peer);
2403 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2404 * Consider switching to it.
2406 * @param message possibly a 'struct SessionConnectMessage' (check format)
2407 * @param peer identity of the peer to switch the address for
2408 * @param address address of the other peer, NULL if other peer
2410 * @param session session to use (or NULL)
2411 * @param ats performance data
2412 * @param ats_count number of entries in ats
2415 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2416 const struct GNUNET_PeerIdentity *peer,
2417 const struct GNUNET_HELLO_Address *address,
2418 struct Session *session,
2419 const struct GNUNET_ATS_Information *ats,
2422 const struct SessionConnectMessage *scm;
2423 struct GNUNET_MessageHeader msg;
2424 struct NeighbourMapEntry *n;
2428 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2429 "Received CONNECT_ACK message from peer `%s'\n",
2432 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2434 GNUNET_break_op (0);
2437 scm = (const struct SessionConnectMessage *) message;
2438 GNUNET_break_op (ntohl (scm->reserved) == 0);
2439 n = lookup_neighbour (peer);
2442 /* we did not send 'CONNECT' -- at least not recently */
2443 GNUNET_STATISTICS_update (GST_stats,
2445 ("# unexpected CONNECT_ACK messages (no peer)"),
2452 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2454 * We also received an CONNECT message, switched from SENDT to RECV and
2455 * ATS already suggested us an address after a successful blacklist check
2458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2459 "Received CONNECT_ACK message from peer `%s' in state `%s'\n",
2461 print_state(n->state));
2463 if ((n->address != NULL) && (n->state == S_CONNECTED))
2465 /* After fast reconnect: send ACK (ACK) even when we are connected */
2466 msg_len = sizeof (msg);
2467 msg.size = htons (msg_len);
2468 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2470 ret = send_with_session(n,
2471 (const char *) &msg, msg_len,
2472 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2475 if (ret == GNUNET_SYSERR)
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2477 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2478 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2482 if ((n->state != S_CONNECT_SENT) &&
2483 ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2485 GNUNET_STATISTICS_update (GST_stats,
2487 ("# unexpected CONNECT_ACK messages"), 1,
2491 if (n->state != S_CONNECTED)
2492 change_state (n, S_CONNECTED);
2494 if (NULL != session)
2496 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2498 "Giving ATS session %p of plugin %s for peer %s\n",
2499 session, address->transport_name, GNUNET_i2s (peer));
2501 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2502 GNUNET_assert (NULL != n->address);
2504 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2506 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2507 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2508 n->address_state = USED;
2511 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2513 /* send ACK (ACK) */
2514 msg_len = sizeof (msg);
2515 msg.size = htons (msg_len);
2516 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2518 ret = send_with_session(n,
2519 (const char *) &msg, msg_len,
2520 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2523 if (ret == GNUNET_SYSERR)
2524 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2525 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2526 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2529 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2530 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2532 neighbours_connected++;
2533 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
2535 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2536 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2537 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2539 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2540 send_outbound_quota (peer, n->bandwidth_out);
2546 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2547 const struct GNUNET_PeerIdentity *peer,
2548 const struct GNUNET_HELLO_Address *address,
2549 struct Session *session,
2550 const struct GNUNET_ATS_Information *ats,
2553 struct NeighbourMapEntry *n;
2555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2557 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2559 GNUNET_break_op (0);
2562 n = lookup_neighbour (peer);
2568 if (S_CONNECTED == n->state)
2570 if (!is_connecting (n))
2572 GNUNET_STATISTICS_update (GST_stats,
2573 gettext_noop ("# unexpected ACK messages"), 1,
2577 change_state (n, S_CONNECTED);
2578 if (NULL != session)
2579 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2581 "Giving ATS session %p of plugin %s for peer %s\n",
2582 session, address->transport_name, GNUNET_i2s (peer));
2583 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2584 GNUNET_assert (n->address != NULL);
2586 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2588 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2589 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2590 n->address_state = USED;
2594 neighbours_connected++;
2595 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
2598 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2599 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2600 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2601 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2602 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2603 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2605 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2606 send_outbound_quota (peer, n->bandwidth_out);
2609 struct BlackListCheckContext
2611 struct GNUNET_ATS_Information *ats;
2615 struct Session *session;
2617 struct GNUNET_HELLO_Address *address;
2619 struct GNUNET_TIME_Absolute ts;
2624 handle_connect_blacklist_cont (void *cls,
2625 const struct GNUNET_PeerIdentity *peer,
2628 struct NeighbourMapEntry *n;
2629 struct BlackListCheckContext *bcc = cls;
2631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2632 "Blacklist check due to CONNECT message: `%s'\n",
2634 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2636 if (GNUNET_OK != result)
2638 GNUNET_HELLO_address_free (bcc->address);
2643 n = lookup_neighbour (peer);
2645 n = setup_neighbour (peer);
2647 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2649 if (NULL != bcc->session)
2650 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2652 "Giving ATS session %p of address `%s' for peer %s\n",
2653 bcc->session, GST_plugins_a2s (bcc->address),
2655 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2657 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2659 n->connect_ts = bcc->ts;
2662 GNUNET_HELLO_address_free (bcc->address);
2665 if (n->state != S_CONNECT_RECV)
2666 change_state (n, S_CONNECT_RECV);
2669 /* Ask ATS for an address to connect via that address */
2670 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2671 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2673 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2675 GNUNET_ATS_suggest_address (GST_ats, peer);
2679 * We received a 'SESSION_CONNECT' message from the other peer.
2680 * Consider switching to it.
2682 * @param message possibly a 'struct SessionConnectMessage' (check format)
2683 * @param peer identity of the peer to switch the address for
2684 * @param address address of the other peer, NULL if other peer
2686 * @param session session to use (or NULL)
2687 * @param ats performance data
2688 * @param ats_count number of entries in ats (excluding 0-termination)
2691 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2692 const struct GNUNET_PeerIdentity *peer,
2693 const struct GNUNET_HELLO_Address *address,
2694 struct Session *session,
2695 const struct GNUNET_ATS_Information *ats,
2698 const struct SessionConnectMessage *scm;
2699 struct BlackListCheckContext *bcc = NULL;
2700 struct NeighbourMapEntry *n;
2702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2703 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2704 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2706 GNUNET_break_op (0);
2710 scm = (const struct SessionConnectMessage *) message;
2711 GNUNET_break_op (ntohl (scm->reserved) == 0);
2713 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2715 n = lookup_neighbour (peer);
2716 if ((n != NULL) && ((S_CONNECTED == n->state) || (S_FAST_RECONNECT == n->state)))
2718 /* connected peer switches addresses or is trying to do a fast reconnect*/
2723 /* we are not connected to this peer */
2724 /* do blacklist check */
2726 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2727 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2728 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2729 bcc->ats_count = ats_count + 1;
2730 bcc->address = GNUNET_HELLO_address_copy (address);
2731 bcc->session = session;
2732 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2733 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2734 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2735 bcc->ats[ats_count].value =
2736 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2737 GST_blacklist_test_allowed (peer, address->transport_name,
2738 &handle_connect_blacklist_cont, bcc);
2742 /* end of file gnunet-service-transport_neighbours.c */