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;
404 static unsigned int bytes_in_send_queue;
405 static unsigned int bytes_received;
408 * Lookup a neighbour entry in the neighbours hash map.
410 * @param pid identity of the peer to look up
411 * @return the entry, NULL if there is no existing record
413 static struct NeighbourMapEntry *
414 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
416 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
420 * Disconnect from the given neighbour, clean up the record.
422 * @param n neighbour to disconnect from
425 disconnect_neighbour (struct NeighbourMapEntry *n);
427 #define change_state(n, state, ...) change (n, state, __LINE__)
430 is_connecting (struct NeighbourMapEntry *n)
432 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
438 is_connected (struct NeighbourMapEntry *n)
440 if (n->state == S_CONNECTED)
446 is_disconnecting (struct NeighbourMapEntry *n)
448 if (n->state == S_DISCONNECT)
454 print_state (int state)
459 return "S_CONNECTED";
462 return "S_CONNECT_RECV";
465 return "S_CONNECT_SENT";
468 return "S_DISCONNECT";
470 case S_NOT_CONNECTED:
471 return "S_NOT_CONNECTED";
473 case S_FAST_RECONNECT:
474 return "S_FAST_RECONNECT";
484 change (struct NeighbourMapEntry *n, int state, int line);
487 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
491 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
493 struct NeighbourMapEntry *n = cls;
498 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
499 if (n->state == S_CONNECTED)
501 GNUNET_STATISTICS_update (GST_stats,
503 ("# failed connection attempts due to timeout"), 1,
505 /* resetting state */
507 if (n->state == S_FAST_RECONNECT)
509 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
510 "Fast reconnect time out, disconnecting peer `%s'\n",
511 GNUNET_i2s (&n->id));
512 disconnect_neighbour(n);
516 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
517 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
518 GNUNET_i2s (&n->id), n, print_state(n->state), "S_NOT_CONNECTED", __LINE__);
520 n->state = S_NOT_CONNECTED;
522 /* destroying address */
523 if (n->address != NULL)
525 GNUNET_assert (strlen (n->address->transport_name) > 0);
526 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
529 /* request new address */
530 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
531 GNUNET_SCHEDULER_cancel (n->ats_suggest);
533 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
535 GNUNET_ATS_suggest_address (GST_ats, &n->id);
539 change (struct NeighbourMapEntry *n, int state, int line)
542 /* allowed transitions */
543 int allowed = GNUNET_NO;
545 previous_state = n->state;
549 case S_NOT_CONNECTED:
550 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
551 (state == S_DISCONNECT))
552 allowed = GNUNET_YES;
555 allowed = GNUNET_YES;
558 allowed = GNUNET_YES;
561 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
562 allowed = GNUNET_YES;
566 case S_FAST_RECONNECT:
567 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
568 allowed = GNUNET_YES;
574 if (allowed == GNUNET_NO)
576 char *old = GNUNET_strdup (print_state (n->state));
577 char *new = GNUNET_strdup (print_state (state));
579 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
580 "Illegal state transition from `%s' to `%s' in line %u \n", old,
585 return GNUNET_SYSERR;
588 char *old = GNUNET_strdup (print_state (n->state));
589 char *new = GNUNET_strdup (print_state (state));
591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
592 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
593 GNUNET_i2s (&n->id), n, old, new, line);
601 case S_FAST_RECONNECT:
604 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
605 GNUNET_SCHEDULER_cancel (n->state_reset);
607 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
610 case S_NOT_CONNECTED:
612 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
615 char *old = GNUNET_strdup (print_state (n->state));
616 char *new = GNUNET_strdup (print_state (state));
618 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
619 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
620 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), old, new);
624 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
625 GNUNET_SCHEDULER_cancel (n->state_reset);
626 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
634 if (NULL != address_change_cb)
636 if (n->state == S_CONNECTED)
637 address_change_cb (callback_cls, &n->id, n->address);
638 else if (previous_state == S_CONNECTED)
639 address_change_cb (callback_cls, &n->id, NULL);
646 send_with_session (struct NeighbourMapEntry *n,
647 const char *msgbuf, size_t msgbuf_size,
649 struct GNUNET_TIME_Relative timeout,
650 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
652 struct GNUNET_TRANSPORT_PluginFunctions *papi;
653 size_t ret = GNUNET_SYSERR;
655 GNUNET_assert (n != NULL);
656 GNUNET_assert (n->session != NULL);
658 papi = GST_plugins_find (n->address->transport_name);
662 cont (cont_cls, &n->id, GNUNET_SYSERR);
663 return GNUNET_SYSERR;
666 ret = papi->send (papi->cls,
673 if ((ret == -1) && (cont != NULL))
674 cont (cont_cls, &n->id, GNUNET_SYSERR);
679 * Task invoked to start a transmission to another peer.
681 * @param cls the 'struct NeighbourMapEntry'
682 * @param tc scheduler context
685 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
689 * We're done with our transmission attempt, continue processing.
691 * @param cls the 'struct MessageQueue' of the message
692 * @param receiver intended receiver
693 * @param success whether it worked or not
696 transmit_send_continuation (void *cls,
697 const struct GNUNET_PeerIdentity *receiver,
700 struct MessageQueue *mq = cls;
701 struct NeighbourMapEntry *n;
702 struct NeighbourMapEntry *tmp;
704 tmp = lookup_neighbour (receiver);
706 if ((NULL != n) && (tmp != NULL) && (tmp == n))
708 GNUNET_assert (n->is_active == mq);
710 if (success == GNUNET_YES)
712 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
713 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
717 GNUNET_assert (bytes_in_send_queue >= mq->message_buf_size);
718 bytes_in_send_queue -= mq->message_buf_size;
719 GNUNET_STATISTICS_set (GST_stats,
721 ("# bytes in message queue for other peers"),
722 bytes_in_send_queue, GNUNET_NO);
724 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
725 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
726 (success == GNUNET_OK) ? "successful" : "FAILED");
727 if (NULL != mq->cont)
728 mq->cont (mq->cont_cls, success);
734 * Check the ready list for the given neighbour and if a plugin is
735 * ready for transmission (and if we have a message), do so!
737 * @param n target peer for which to transmit
740 try_transmission_to_peer (struct NeighbourMapEntry *n)
742 struct MessageQueue *mq;
743 struct GNUNET_TIME_Relative timeout;
746 if (n->is_active != NULL)
749 return; /* transmission already pending */
751 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
754 return; /* currently waiting for bandwidth */
756 while (NULL != (mq = n->messages_head))
758 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
759 if (timeout.rel_value > 0)
761 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
764 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
767 return; /* no more messages */
769 if (n->address == NULL)
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
772 GNUNET_i2s (&n->id));
773 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
774 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
775 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
776 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
780 if (GST_plugins_find (n->address->transport_name) == NULL)
785 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
789 if ((n->address->address_length == 0) && (n->session == NULL))
791 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
792 GNUNET_i2s (&n->id));
793 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
794 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
795 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
799 ret = send_with_session(n,
800 mq->message_buf, mq->message_buf_size,
802 &transmit_send_continuation, mq);
806 /* failure, but 'send' would not call continuation in this case,
807 * so we need to do it here! */
808 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
815 * Task invoked to start a transmission to another peer.
817 * @param cls the 'struct NeighbourMapEntry'
818 * @param tc scheduler context
821 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
823 struct NeighbourMapEntry *n = cls;
825 GNUNET_assert (NULL != lookup_neighbour (&n->id));
826 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
827 try_transmission_to_peer (n);
832 * Initialize the neighbours subsystem.
834 * @param cls closure for callbacks
835 * @param connect_cb function to call if we connect to a peer
836 * @param disconnect_cb function to call if we disconnect from a peer
837 * @param peer_address_cb function to call if we change an active address
841 GST_neighbours_start (void *cls,
842 GNUNET_TRANSPORT_NotifyConnect connect_cb,
843 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
844 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
847 connect_notify_cb = connect_cb;
848 disconnect_notify_cb = disconnect_cb;
849 address_change_cb = peer_address_cb;
850 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
851 neighbours_connected = 0;
852 bytes_in_send_queue = 0;
858 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
861 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
862 "Sending DISCONNECT message to peer `%4s': %i\n",
863 GNUNET_i2s (target), result);
868 send_disconnect (struct NeighbourMapEntry * n)
871 struct SessionDisconnectMessage disconnect_msg;
873 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
874 "Sending DISCONNECT message to peer `%4s'\n",
875 GNUNET_i2s (&n->id));
876 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
877 disconnect_msg.header.type =
878 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
879 disconnect_msg.reserved = htonl (0);
880 disconnect_msg.purpose.size =
881 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
882 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
883 sizeof (struct GNUNET_TIME_AbsoluteNBO));
884 disconnect_msg.purpose.purpose =
885 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
886 disconnect_msg.timestamp =
887 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
888 disconnect_msg.public_key = GST_my_public_key;
889 GNUNET_assert (GNUNET_OK ==
890 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
891 &disconnect_msg.purpose,
892 &disconnect_msg.signature));
894 ret = send_with_session (n,
895 (const char *) &disconnect_msg, sizeof (disconnect_msg),
896 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
897 &send_disconnect_cont, NULL);
899 if (ret == GNUNET_SYSERR)
900 return GNUNET_SYSERR;
902 GNUNET_STATISTICS_update (GST_stats,
904 ("# peers disconnected due to external request"), 1,
911 * Disconnect from the given neighbour, clean up the record.
913 * @param n neighbour to disconnect from
916 disconnect_neighbour (struct NeighbourMapEntry *n)
918 struct MessageQueue *mq;
921 previous_state = n->state;
923 if (is_disconnecting (n))
926 /* send DISCONNECT MESSAGE */
927 if (previous_state == S_CONNECTED)
929 if (GNUNET_OK == send_disconnect (n))
930 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
931 GNUNET_i2s (&n->id));
933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
934 "Could not send DISCONNECT_MSG to `%s'\n",
935 GNUNET_i2s (&n->id));
938 change_state (n, S_DISCONNECT);
940 if (previous_state == S_CONNECTED)
942 GNUNET_assert (NULL != n->address);
943 if (n->address_state == USED)
945 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
946 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
947 n->address_state = UNUSED;
951 if (n->address != NULL)
953 struct GNUNET_TRANSPORT_PluginFunctions *papi;
955 papi = GST_plugins_find (n->address->transport_name);
957 papi->disconnect (papi->cls, &n->id);
959 while (NULL != (mq = n->messages_head))
961 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
962 if (NULL != mq->cont)
963 mq->cont (mq->cont_cls, GNUNET_SYSERR);
966 if (NULL != n->is_active)
968 n->is_active->n = NULL;
969 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
970 "Failing transmission of active message due to disconnect\n");
971 if (NULL != n->is_active->cont)
972 n->is_active->cont (n->is_active->cont_cls, GNUNET_SYSERR);
973 GNUNET_free (n->is_active);
977 switch (previous_state)
980 GNUNET_assert (neighbours_connected > 0);
981 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
982 GNUNET_SCHEDULER_cancel (n->keepalive_task);
983 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
984 n->expect_latency_response = GNUNET_NO;
985 neighbours_connected--;
986 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
988 disconnect_notify_cb (callback_cls, &n->id);
990 case S_FAST_RECONNECT:
991 GNUNET_STATISTICS_update (GST_stats,
992 gettext_noop ("# fast reconnects failed"), 1,
994 disconnect_notify_cb (callback_cls, &n->id);
1000 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
1002 GNUNET_assert (GNUNET_YES ==
1003 GNUNET_CONTAINER_multihashmap_remove (neighbours,
1004 &n->id.hashPubKey, n));
1005 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
1007 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1008 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1010 if (n->ats_request != GNUNET_SCHEDULER_NO_TASK)
1012 GNUNET_SCHEDULER_cancel (n->ats_request);
1013 n->ats_request = GNUNET_SCHEDULER_NO_TASK;
1016 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
1018 GNUNET_SCHEDULER_cancel (n->timeout_task);
1019 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1021 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
1023 GNUNET_SCHEDULER_cancel (n->transmission_task);
1024 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
1026 if (NULL != n->fast_reconnect_address)
1028 GNUNET_HELLO_address_free (n->fast_reconnect_address);
1029 n->fast_reconnect_address = NULL;
1031 if (NULL != n->address)
1033 GNUNET_HELLO_address_free (n->address);
1037 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
1038 GNUNET_i2s (&n->id), n);
1044 * Peer has been idle for too long. Disconnect.
1046 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1047 * @param tc scheduler context
1050 neighbour_timeout_task (void *cls,
1051 const struct GNUNET_SCHEDULER_TaskContext *tc)
1053 struct NeighbourMapEntry *n = cls;
1055 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1056 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer`%4s' disconnected due to timeout\n",
1057 GNUNET_i2s (&n->id));
1059 GNUNET_STATISTICS_update (GST_stats,
1061 ("# peers disconnected due to timeout"), 1,
1063 disconnect_neighbour (n);
1068 * Send another keepalive message.
1070 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1071 * @param tc scheduler context
1074 neighbour_keepalive_task (void *cls,
1075 const struct GNUNET_SCHEDULER_TaskContext *tc)
1077 struct NeighbourMapEntry *n = cls;
1078 struct GNUNET_MessageHeader m;
1081 GNUNET_assert (S_CONNECTED == n->state);
1083 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1084 &neighbour_keepalive_task, n);
1086 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1088 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1089 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1091 ret = send_with_session (n,
1092 (const void *) &m, sizeof (m),
1093 UINT32_MAX /* priority */ ,
1094 GNUNET_TIME_UNIT_FOREVER_REL,
1097 n->expect_latency_response = GNUNET_NO;
1098 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero ();
1099 if (ret != GNUNET_SYSERR)
1101 n->expect_latency_response = GNUNET_YES;
1102 n->keep_alive_sent = GNUNET_TIME_absolute_get ();
1109 * Disconnect from the given neighbour.
1112 * @param key hash of neighbour's public key (not used)
1113 * @param value the 'struct NeighbourMapEntry' of the neighbour
1116 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1118 struct NeighbourMapEntry *n = value;
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1121 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1122 if (S_CONNECTED == n->state)
1123 GNUNET_STATISTICS_update (GST_stats,
1125 ("# peers disconnected due to global disconnect"),
1127 disconnect_neighbour (n);
1133 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1135 struct NeighbourMapEntry *n = cls;
1137 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1139 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1140 "ATS did not suggested address to connect to peer `%s'\n",
1141 GNUNET_i2s (&n->id));
1143 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# ATS address suggestions failed"), 1,
1147 disconnect_neighbour (n);
1151 * Cleanup the neighbours subsystem.
1154 GST_neighbours_stop ()
1156 // This can happen during shutdown
1157 if (neighbours == NULL)
1162 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1164 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1165 // GNUNET_assert (neighbours_connected == 0);
1167 callback_cls = NULL;
1168 connect_notify_cb = NULL;
1169 disconnect_notify_cb = NULL;
1170 address_change_cb = NULL;
1173 struct ContinutionContext
1175 struct GNUNET_HELLO_Address *address;
1177 struct Session *session;
1181 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
1182 struct GNUNET_BANDWIDTH_Value32NBO quota)
1184 struct QuotaSetMessage q_msg;
1186 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1187 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1188 ntohl (quota.value__), GNUNET_i2s (target));
1189 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1190 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1191 q_msg.quota = quota;
1192 q_msg.peer = (*target);
1193 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1197 * We tried to send a SESSION_CONNECT message to another peer. If this
1198 * succeeded, we change the state. If it failed, we should tell
1199 * ATS to not use this address anymore (until it is re-validated).
1201 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1202 * @param target peer to send the message to
1203 * @param success GNUNET_OK on success
1206 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1209 struct ContinutionContext *cc = cls;
1210 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1212 if (GNUNET_YES != success)
1214 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1215 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1217 if ((NULL == neighbours) || (NULL == n) || (n->state == S_DISCONNECT))
1219 GNUNET_HELLO_address_free (cc->address);
1224 if ((GNUNET_YES == success) &&
1225 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1227 change_state (n, S_CONNECT_SENT);
1228 GNUNET_HELLO_address_free (cc->address);
1233 if ((GNUNET_NO == success) &&
1234 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1236 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1237 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1238 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:
1352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1353 "Successful fast reconnect to peer `%s'\n",
1354 GNUNET_i2s (&n->id));
1355 change_state (n, S_CONNECTED);
1356 neighbours_connected++;
1357 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
1360 if (n->address_state == FRESH)
1362 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES, __LINE__);
1363 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1364 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1365 n->address_state = USED;
1368 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1370 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1372 /* Updating quotas */
1373 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1374 send_outbound_quota (target, n->bandwidth_out);
1379 GNUNET_HELLO_address_free (cc->address);
1385 * We tried to send a SESSION_CONNECT message to another peer. If this
1386 * succeeded, we change the state. If it failed, we should tell
1387 * ATS to not use this address anymore (until it is re-validated).
1389 * @param cls the 'struct NeighbourMapEntry'
1390 * @param target peer to send the message to
1391 * @param success GNUNET_OK on success
1394 send_connect_ack_continuation (void *cls,
1395 const struct GNUNET_PeerIdentity *target,
1398 struct ContinutionContext *cc = cls;
1399 struct NeighbourMapEntry *n;
1401 if (neighbours == NULL)
1403 GNUNET_HELLO_address_free (cc->address);
1405 return; /* neighbour is going away */
1408 n = lookup_neighbour (&cc->address->peer);
1409 if ((n == NULL) || (is_disconnecting (n)))
1411 GNUNET_HELLO_address_free (cc->address);
1413 return; /* neighbour is going away */
1416 if (GNUNET_YES == success)
1418 GNUNET_HELLO_address_free (cc->address);
1420 return; /* sending successful */
1423 /* sending failed, ask for next address */
1424 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1425 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1426 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1427 if (n->state != S_NOT_CONNECTED)
1428 change_state (n, S_NOT_CONNECTED);
1429 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1430 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1432 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1433 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1435 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1437 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1438 GNUNET_HELLO_address_free (cc->address);
1444 * For an existing neighbour record, set the active connection to
1445 * use the given address.
1447 * @param peer identity of the peer to switch the address for
1448 * @param address address of the other peer, NULL if other peer
1450 * @param session session to use (or NULL)
1451 * @param ats performance data
1452 * @param ats_count number of entries in ats
1453 * @param bandwidth_in inbound quota to be used when connection is up
1454 * @param bandwidth_out outbound quota to be used when connection is up
1455 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1456 * connection is not up (yet)
1459 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1460 const struct GNUNET_HELLO_Address
1462 struct Session *session,
1463 const struct GNUNET_ATS_Information *ats,
1465 struct GNUNET_BANDWIDTH_Value32NBO
1467 struct GNUNET_BANDWIDTH_Value32NBO
1470 struct NeighbourMapEntry *n;
1471 struct SessionConnectMessage connect_msg;
1472 struct ContinutionContext *cc;
1476 if (neighbours == NULL)
1478 /* This can happen during shutdown */
1481 n = lookup_neighbour (peer);
1484 if (n->state == S_DISCONNECT)
1486 /* We are disconnecting, nothing to do here */
1489 GNUNET_assert (address->transport_name != NULL);
1490 if ((session == NULL) && (0 == address->address_length))
1492 GNUNET_break_op (0);
1493 /* FIXME: is this actually possible? When does this happen? */
1494 if (strlen (address->transport_name) > 0)
1495 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1496 GNUNET_ATS_suggest_address (GST_ats, peer);
1500 /* checks successful and neighbour != NULL */
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
1502 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1503 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
1506 print_state (n->state));
1508 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1510 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1511 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1515 if (n->state == S_FAST_RECONNECT)
1517 /* Throtteled fast reconnect */
1519 if (NULL == n->fast_reconnect_address)
1521 n->fast_reconnect_address = GNUNET_HELLO_address_copy (address);
1524 else if (0 == GNUNET_HELLO_address_cmp(address, n->fast_reconnect_address))
1526 n->fast_reconnect_attempts ++;
1528 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1529 "FAST RECONNECT to peer `%s' and address '%s' with identical address attempt %u\n",
1530 GNUNET_i2s (&n->id), GST_plugins_a2s (address), n->fast_reconnect_attempts);
1535 n->fast_reconnect_attempts = 0;
1538 /* do not switch addresses just update quotas */
1539 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1540 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1541 (n->session == session))
1543 n->bandwidth_in = bandwidth_in;
1544 n->bandwidth_out = bandwidth_out;
1545 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1546 send_outbound_quota (peer, n->bandwidth_out);
1549 if (n->state == S_CONNECTED)
1551 /* mark old address as no longer used */
1552 GNUNET_assert (NULL != n->address);
1553 if (n->address_state == USED)
1555 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1556 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1557 n->address_state = UNUSED;
1561 /* set new address */
1562 if (NULL != n->address)
1563 GNUNET_HELLO_address_free (n->address);
1564 n->address = GNUNET_HELLO_address_copy (address);
1565 n->address_state = FRESH;
1566 n->bandwidth_in = bandwidth_in;
1567 n->bandwidth_out = bandwidth_out;
1568 GNUNET_SCHEDULER_cancel (n->timeout_task);
1570 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1571 &neighbour_timeout_task, n);
1573 if (NULL != address_change_cb && n->state == S_CONNECTED)
1574 address_change_cb (callback_cls, &n->id, n->address);
1576 /* Obtain an session for this address from plugin */
1577 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1578 papi = GST_plugins_find (address->transport_name);
1582 /* we don't have the plugin for this address */
1583 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1585 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1586 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1587 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1590 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1591 GNUNET_HELLO_address_free (n->address);
1597 if (session == NULL)
1599 n->session = papi->get_session (papi->cls, address);
1600 /* Session could not be initiated */
1601 if (n->session == NULL)
1603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1604 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1605 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1607 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1609 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1610 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1611 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1614 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1615 GNUNET_HELLO_address_free (n->address);
1621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1622 "Obtained new session %p for peer `%s' and address '%s'\n",
1623 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1624 /* Telling ATS about new session */
1625 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1629 n->session = session;
1630 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1631 "Using existing session %p for peer `%s' and address '%s'\n",
1633 GNUNET_i2s (&n->id),
1634 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>");
1639 case S_NOT_CONNECTED:
1640 case S_CONNECT_SENT:
1641 msg_len = sizeof (struct SessionConnectMessage);
1642 connect_msg.header.size = htons (msg_len);
1643 connect_msg.header.type =
1644 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1645 connect_msg.reserved = htonl (0);
1646 connect_msg.timestamp =
1647 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1649 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1650 cc->session = n->session;
1651 cc->address = GNUNET_HELLO_address_copy (address);
1652 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1653 "Sending CONNECT message to %s\n",
1654 GNUNET_i2s (&n->id));
1655 if (n->state != S_CONNECT_RECV)
1656 change_state (n, S_CONNECT_RECV);
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;
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1767 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1769 n->state = S_NOT_CONNECTED;
1770 n->latency = GNUNET_TIME_relative_get_forever ();
1771 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1772 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1773 MAX_BANDWIDTH_CARRY_S);
1775 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1776 &neighbour_timeout_task, n);
1777 GNUNET_assert (GNUNET_OK ==
1778 GNUNET_CONTAINER_multihashmap_put (neighbours,
1779 &n->id.hashPubKey, n,
1780 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1786 * Try to create a connection to the given target (eventually).
1788 * @param target peer to try to connect to
1791 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1793 struct NeighbourMapEntry *n;
1795 // This can happen during shutdown
1796 if (neighbours == NULL)
1800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1801 GNUNET_i2s (target));
1803 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1808 n = lookup_neighbour (target);
1812 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1813 return; /* already connecting or connected */
1814 if (is_disconnecting (n))
1815 change_state (n, S_NOT_CONNECTED);
1820 n = setup_neighbour (target);
1821 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1822 "Asking ATS for suggested address to connect to peer `%s'\n",
1823 GNUNET_i2s (&n->id));
1824 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1828 * Test if we're connected to the given peer.
1830 * @param target peer to test
1831 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1834 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1836 struct NeighbourMapEntry *n;
1838 // This can happen during shutdown
1839 if (neighbours == NULL)
1844 n = lookup_neighbour (target);
1846 if ((NULL == n) || (S_CONNECTED != n->state))
1847 return GNUNET_NO; /* not connected */
1852 * A session was terminated. Take note.
1854 * @param peer identity of the peer where the session died
1855 * @param session session that is gone
1858 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1859 struct Session *session)
1861 struct NeighbourMapEntry *n;
1863 if (neighbours == NULL)
1865 /* This can happen during shutdown */
1868 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1869 session, GNUNET_i2s (peer));
1870 n = lookup_neighbour (peer);
1873 if (session != n->session)
1874 return; /* doesn't affect us */
1875 if (n->state == S_CONNECTED)
1877 if (n->address_state == USED)
1879 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1880 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1881 n->address_state = UNUSED;
1886 if (NULL != n->address)
1888 GNUNET_HELLO_address_free (n->address);
1893 /* not connected anymore anyway, shouldn't matter */
1894 if (S_CONNECTED != n->state)
1897 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1899 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1900 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1901 n->expect_latency_response = GNUNET_NO;
1904 /* connected, try fast reconnect */
1905 /* statistics "transport" : "# peers connected" -= 1
1906 * neighbours_connected -= 1
1907 * BUT: no disconnect_cb to notify clients about disconnect
1910 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1913 GNUNET_assert (neighbours_connected > 0);
1914 change_state (n, S_FAST_RECONNECT);
1915 neighbours_connected--;
1916 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
1920 /* We are connected, so ask ATS to switch addresses */
1921 GNUNET_SCHEDULER_cancel (n->timeout_task);
1922 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1923 &neighbour_timeout_task, n);
1924 /* try QUICKLY to re-establish a connection, reduce timeout! */
1925 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1926 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1927 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1928 &ats_suggest_cancel,
1930 GNUNET_ATS_suggest_address (GST_ats, peer);
1935 * Transmit a message to the given target using the active connection.
1937 * @param target destination
1938 * @param msg message to send
1939 * @param msg_size number of bytes in msg
1940 * @param timeout when to fail with timeout
1941 * @param cont function to call when done
1942 * @param cont_cls closure for 'cont'
1945 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1946 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1947 GST_NeighbourSendContinuation cont, void *cont_cls)
1949 struct NeighbourMapEntry *n;
1950 struct MessageQueue *mq;
1952 // This can happen during shutdown
1953 if (neighbours == NULL)
1958 n = lookup_neighbour (target);
1959 if ((n == NULL) || (!is_connected (n)))
1961 GNUNET_STATISTICS_update (GST_stats,
1963 ("# messages not sent (no such peer or not connected)"),
1966 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1967 "Could not send message to peer `%s': unknown neighbour",
1968 GNUNET_i2s (target));
1969 else if (!is_connected (n))
1970 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1971 "Could not send message to peer `%s': not connected\n",
1972 GNUNET_i2s (target));
1974 cont (cont_cls, GNUNET_SYSERR);
1978 if ((n->session == NULL) && (n->address == NULL))
1980 GNUNET_STATISTICS_update (GST_stats,
1982 ("# messages not sent (no such peer or not connected)"),
1984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1985 "Could not send message to peer `%s': no address available\n",
1986 GNUNET_i2s (target));
1988 cont (cont_cls, GNUNET_SYSERR);
1992 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1993 bytes_in_send_queue += msg_size;
1994 GNUNET_STATISTICS_set (GST_stats,
1996 ("# bytes in message queue for other peers"),
1997 bytes_in_send_queue, GNUNET_NO);
1998 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
2000 mq->cont_cls = cont_cls;
2001 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2002 memcpy (&mq[1], msg, msg_size);
2003 mq->message_buf = (const char *) &mq[1];
2004 mq->message_buf_size = msg_size;
2005 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2006 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
2008 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
2009 (NULL == n->is_active))
2010 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
2015 * We have received a message from the given sender. How long should
2016 * we delay before receiving more? (Also used to keep the peer marked
2019 * @param sender sender of the message
2020 * @param size size of the message
2021 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
2022 * GNUNET_NO if the neighbour is not connected or violates the quota,
2023 * GNUNET_SYSERR if the connection is not fully up yet
2024 * @return how long to wait before reading more from this sender
2026 struct GNUNET_TIME_Relative
2027 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
2028 *sender, ssize_t size, int *do_forward)
2030 struct NeighbourMapEntry *n;
2031 struct GNUNET_TIME_Relative ret;
2033 // This can happen during shutdown
2034 if (neighbours == NULL)
2036 return GNUNET_TIME_UNIT_FOREVER_REL;
2039 n = lookup_neighbour (sender);
2042 GST_neighbours_try_connect (sender);
2043 n = lookup_neighbour (sender);
2046 GNUNET_STATISTICS_update (GST_stats,
2048 ("# messages discarded due to lack of neighbour record"),
2050 *do_forward = GNUNET_NO;
2051 return GNUNET_TIME_UNIT_ZERO;
2054 if (!is_connected (n))
2056 *do_forward = GNUNET_SYSERR;
2057 return GNUNET_TIME_UNIT_ZERO;
2059 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
2061 n->quota_violation_count++;
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2063 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
2064 n->in_tracker.available_bytes_per_s__,
2065 n->quota_violation_count);
2066 /* Discount 32k per violation */
2067 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
2071 if (n->quota_violation_count > 0)
2073 /* try to add 32k back */
2074 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
2075 n->quota_violation_count--;
2078 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2080 GNUNET_STATISTICS_update (GST_stats,
2082 ("# bandwidth quota violations by other peers"),
2084 *do_forward = GNUNET_NO;
2085 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
2087 *do_forward = GNUNET_YES;
2088 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
2089 if (ret.rel_value > 0)
2091 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2092 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
2093 (unsigned long long) n->in_tracker.
2094 consumption_since_last_update__,
2095 (unsigned int) n->in_tracker.available_bytes_per_s__,
2096 (unsigned long long) ret.rel_value);
2097 GNUNET_STATISTICS_update (GST_stats,
2098 gettext_noop ("# ms throttling suggested"),
2099 (int64_t) ret.rel_value, GNUNET_NO);
2106 * Keep the connection to the given neighbour alive longer,
2107 * we received a KEEPALIVE (or equivalent).
2109 * @param neighbour neighbour to keep alive
2112 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
2114 struct NeighbourMapEntry *n;
2116 // This can happen during shutdown
2117 if (neighbours == NULL)
2122 n = lookup_neighbour (neighbour);
2125 GNUNET_STATISTICS_update (GST_stats,
2127 ("# KEEPALIVE messages discarded (not connected)"),
2131 GNUNET_SCHEDULER_cancel (n->timeout_task);
2133 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2134 &neighbour_timeout_task, n);
2136 /* send reply to measure latency */
2137 if (S_CONNECTED != n->state)
2140 struct GNUNET_MessageHeader m;
2142 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2143 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2145 send_with_session(n,
2146 (const void *) &m, sizeof (m),
2148 GNUNET_TIME_UNIT_FOREVER_REL,
2153 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2156 * @param neighbour neighbour to keep alive
2157 * @param ats performance data
2158 * @param ats_count number of entries in ats
2161 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2162 const struct GNUNET_ATS_Information *ats,
2165 struct NeighbourMapEntry *n;
2166 struct GNUNET_ATS_Information *ats_new;
2169 if (neighbours == NULL)
2171 // This can happen during shutdown
2175 n = lookup_neighbour (neighbour);
2176 if ((NULL == n) || (n->state != S_CONNECTED))
2178 GNUNET_STATISTICS_update (GST_stats,
2180 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2184 if (n->expect_latency_response != GNUNET_YES)
2186 GNUNET_STATISTICS_update (GST_stats,
2188 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2192 n->expect_latency_response = GNUNET_NO;
2194 GNUNET_assert (n->keep_alive_sent.abs_value !=
2195 GNUNET_TIME_absolute_get_zero ().abs_value);
2197 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2198 GNUNET_TIME_absolute_get ());
2199 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2200 GNUNET_i2s (&n->id), n->latency.rel_value);
2202 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2204 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2209 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2211 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2214 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2215 if (n->latency.rel_value > UINT32_MAX)
2216 latency = UINT32_MAX;
2218 latency = n->latency.rel_value;
2219 ats_new[ats_count].value = htonl (latency);
2221 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2223 GNUNET_free (ats_new);
2229 * Change the incoming quota for the given peer.
2231 * @param neighbour identity of peer to change qutoa for
2232 * @param quota new quota
2235 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2236 struct GNUNET_BANDWIDTH_Value32NBO quota)
2238 struct NeighbourMapEntry *n;
2240 // This can happen during shutdown
2241 if (neighbours == NULL)
2246 n = lookup_neighbour (neighbour);
2249 GNUNET_STATISTICS_update (GST_stats,
2251 ("# SET QUOTA messages ignored (no such peer)"),
2255 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2256 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2257 ntohl (quota.value__), GNUNET_i2s (&n->id));
2258 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2259 if (0 != ntohl (quota.value__))
2261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2262 GNUNET_i2s (&n->id), "SET_QUOTA");
2263 if (is_connected (n))
2264 GNUNET_STATISTICS_update (GST_stats,
2265 gettext_noop ("# disconnects due to quota of 0"),
2267 disconnect_neighbour (n);
2272 * Closure for the neighbours_iterate function.
2274 struct IteratorContext
2277 * Function to call on each connected neighbour.
2279 GST_NeighbourIterator cb;
2289 * Call the callback from the closure for each connected neighbour.
2291 * @param cls the 'struct IteratorContext'
2292 * @param key the hash of the public key of the neighbour
2293 * @param value the 'struct NeighbourMapEntry'
2294 * @return GNUNET_OK (continue to iterate)
2297 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2299 struct IteratorContext *ic = cls;
2300 struct NeighbourMapEntry *n = value;
2302 if (!is_connected (n))
2305 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2311 * Iterate over all connected neighbours.
2313 * @param cb function to call
2314 * @param cb_cls closure for cb
2317 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2319 struct IteratorContext ic;
2321 // This can happen during shutdown
2322 if (neighbours == NULL)
2329 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2333 * If we have an active connection to the given target, it must be shutdown.
2335 * @param target peer to disconnect from
2338 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2340 struct NeighbourMapEntry *n;
2342 // This can happen during shutdown
2343 if (neighbours == NULL)
2348 n = lookup_neighbour (target);
2350 return; /* not active */
2351 disconnect_neighbour (n);
2356 * We received a disconnect message from the given peer,
2357 * validate and process.
2359 * @param peer sender of the message
2360 * @param msg the disconnect message
2363 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2365 const struct GNUNET_MessageHeader
2368 struct NeighbourMapEntry *n;
2369 const struct SessionDisconnectMessage *sdm;
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2373 "Received DISCONNECT message from peer `%s'\n",
2375 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2377 // GNUNET_break_op (0);
2378 GNUNET_STATISTICS_update (GST_stats,
2380 ("# disconnect messages ignored (old format)"), 1,
2384 sdm = (const struct SessionDisconnectMessage *) msg;
2385 n = lookup_neighbour (peer);
2387 return; /* gone already */
2388 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2389 n->connect_ts.abs_value)
2391 GNUNET_STATISTICS_update (GST_stats,
2393 ("# disconnect messages ignored (timestamp)"), 1,
2397 GNUNET_CRYPTO_hash (&sdm->public_key,
2398 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2400 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2402 GNUNET_break_op (0);
2405 if (ntohl (sdm->purpose.size) !=
2406 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2407 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2408 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2410 GNUNET_break_op (0);
2414 GNUNET_CRYPTO_rsa_verify
2415 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2416 &sdm->signature, &sdm->public_key))
2418 GNUNET_break_op (0);
2421 GST_neighbours_force_disconnect (peer);
2426 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2427 * Consider switching to it.
2429 * @param message possibly a 'struct SessionConnectMessage' (check format)
2430 * @param peer identity of the peer to switch the address for
2431 * @param address address of the other peer, NULL if other peer
2433 * @param session session to use (or NULL)
2434 * @param ats performance data
2435 * @param ats_count number of entries in ats
2438 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2439 const struct GNUNET_PeerIdentity *peer,
2440 const struct GNUNET_HELLO_Address *address,
2441 struct Session *session,
2442 const struct GNUNET_ATS_Information *ats,
2445 const struct SessionConnectMessage *scm;
2446 struct GNUNET_MessageHeader msg;
2447 struct NeighbourMapEntry *n;
2451 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2452 "Received CONNECT_ACK message from peer `%s'\n",
2455 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2457 GNUNET_break_op (0);
2460 scm = (const struct SessionConnectMessage *) message;
2461 GNUNET_break_op (ntohl (scm->reserved) == 0);
2462 n = lookup_neighbour (peer);
2465 /* we did not send 'CONNECT' -- at least not recently */
2466 GNUNET_STATISTICS_update (GST_stats,
2468 ("# unexpected CONNECT_ACK messages (no peer)"),
2475 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2477 * We also received an CONNECT message, switched from SENDT to RECV and
2478 * ATS already suggested us an address after a successful blacklist check
2481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2482 "Received CONNECT_ACK message from peer `%s' in state `%s'\n",
2484 print_state(n->state));
2486 if ((n->address != NULL) && (n->state == S_CONNECTED))
2488 /* After fast reconnect: send ACK (ACK) even when we are connected */
2489 msg_len = sizeof (msg);
2490 msg.size = htons (msg_len);
2491 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2493 ret = send_with_session(n,
2494 (const char *) &msg, msg_len,
2495 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2498 if (ret == GNUNET_SYSERR)
2499 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2500 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2501 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2505 if ((NULL == n->address) ||
2506 ((n->state != S_CONNECT_SENT) &&
2507 ((n->state != S_CONNECT_RECV) && (n->address != NULL))))
2509 GNUNET_STATISTICS_update (GST_stats,
2511 ("# unexpected CONNECT_ACK messages"), 1,
2515 if (n->state != S_CONNECTED)
2516 change_state (n, S_CONNECTED);
2518 if (NULL != session)
2520 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2522 "Giving ATS session %p of plugin %s for peer %s\n",
2523 session, address->transport_name, GNUNET_i2s (peer));
2525 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2526 GNUNET_assert (NULL != n->address);
2528 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2530 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2531 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2532 n->address_state = USED;
2535 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2537 /* send ACK (ACK) */
2538 msg_len = sizeof (msg);
2539 msg.size = htons (msg_len);
2540 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2542 ret = send_with_session(n,
2543 (const char *) &msg, msg_len,
2544 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2547 if (ret == GNUNET_SYSERR)
2548 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2549 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2550 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2553 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2554 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2556 neighbours_connected++;
2557 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
2559 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2560 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2561 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2563 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2564 send_outbound_quota (peer, n->bandwidth_out);
2570 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2571 const struct GNUNET_PeerIdentity *peer,
2572 const struct GNUNET_HELLO_Address *address,
2573 struct Session *session,
2574 const struct GNUNET_ATS_Information *ats,
2577 struct NeighbourMapEntry *n;
2579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2581 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2583 GNUNET_break_op (0);
2586 n = lookup_neighbour (peer);
2592 if (S_CONNECTED == n->state)
2594 if (!is_connecting (n))
2596 GNUNET_STATISTICS_update (GST_stats,
2597 gettext_noop ("# unexpected ACK messages"), 1,
2601 change_state (n, S_CONNECTED);
2602 if (NULL != session)
2603 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2605 "Giving ATS session %p of plugin %s for peer %s\n",
2606 session, address->transport_name, GNUNET_i2s (peer));
2607 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2608 GNUNET_assert (n->address != NULL);
2610 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2612 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2613 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2614 n->address_state = USED;
2618 neighbours_connected++;
2619 GNUNET_STATISTICS_set (GST_stats, gettext_noop ("# peers connected"), neighbours_connected,
2622 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2623 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2624 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2625 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2626 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2627 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2629 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2630 send_outbound_quota (peer, n->bandwidth_out);
2633 struct BlackListCheckContext
2635 struct GNUNET_ATS_Information *ats;
2639 struct Session *session;
2641 struct GNUNET_HELLO_Address *address;
2643 struct GNUNET_TIME_Absolute ts;
2648 handle_connect_blacklist_cont (void *cls,
2649 const struct GNUNET_PeerIdentity *peer,
2652 struct NeighbourMapEntry *n;
2653 struct BlackListCheckContext *bcc = cls;
2655 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2656 "Blacklist check due to CONNECT message: `%s'\n",
2658 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2660 if (GNUNET_OK != result)
2662 GNUNET_HELLO_address_free (bcc->address);
2667 n = lookup_neighbour (peer);
2669 n = setup_neighbour (peer);
2671 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2673 if (NULL != bcc->session)
2674 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2676 "Giving ATS session %p of address `%s' for peer %s\n",
2677 bcc->session, GST_plugins_a2s (bcc->address),
2679 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2681 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2683 n->connect_ts = bcc->ts;
2686 GNUNET_HELLO_address_free (bcc->address);
2689 if (n->state != S_CONNECT_RECV)
2690 change_state (n, S_CONNECT_RECV);
2693 /* Ask ATS for an address to connect via that address */
2694 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2695 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2697 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2699 GNUNET_ATS_suggest_address (GST_ats, peer);
2703 * We received a 'SESSION_CONNECT' message from the other peer.
2704 * Consider switching to it.
2706 * @param message possibly a 'struct SessionConnectMessage' (check format)
2707 * @param peer identity of the peer to switch the address for
2708 * @param address address of the other peer, NULL if other peer
2710 * @param session session to use (or NULL)
2711 * @param ats performance data
2712 * @param ats_count number of entries in ats (excluding 0-termination)
2715 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2716 const struct GNUNET_PeerIdentity *peer,
2717 const struct GNUNET_HELLO_Address *address,
2718 struct Session *session,
2719 const struct GNUNET_ATS_Information *ats,
2722 const struct SessionConnectMessage *scm;
2723 struct BlackListCheckContext *bcc = NULL;
2724 struct NeighbourMapEntry *n;
2726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2727 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2728 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2730 GNUNET_break_op (0);
2734 scm = (const struct SessionConnectMessage *) message;
2735 GNUNET_break_op (ntohl (scm->reserved) == 0);
2737 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2739 n = lookup_neighbour (peer);
2740 if ((n != NULL) && ((S_CONNECTED == n->state) || (S_FAST_RECONNECT == n->state)))
2742 /* connected peer switches addresses or is trying to do a fast reconnect*/
2747 /* we are not connected to this peer */
2748 /* do blacklist check */
2750 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2751 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2752 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2753 bcc->ats_count = ats_count + 1;
2754 bcc->address = GNUNET_HELLO_address_copy (address);
2755 bcc->session = session;
2756 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2757 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2758 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2759 bcc->ats[ats_count].value =
2760 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2761 GST_blacklist_test_allowed (peer, address->transport_name,
2762 &handle_connect_blacklist_cont, bcc);
2766 /* end of file gnunet-service-transport_neighbours.c */