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)
68 #define TEST_NEW_CODE GNUNET_NO
71 * Entry in neighbours.
73 struct NeighbourMapEntry;
75 GNUNET_NETWORK_STRUCT_BEGIN
78 * Message a peer sends to another to indicate its
79 * preference for communicating via a particular
80 * session (and the desire to establish a real
83 struct SessionConnectMessage
86 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
88 struct GNUNET_MessageHeader header;
93 uint32_t reserved GNUNET_PACKED;
96 * Absolute time at the sender. Only the most recent connect
97 * message implies which session is preferred by the sender.
99 struct GNUNET_TIME_AbsoluteNBO timestamp;
104 struct SessionDisconnectMessage
107 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
109 struct GNUNET_MessageHeader header;
114 uint32_t reserved GNUNET_PACKED;
117 * Purpose of the signature. Extends over the timestamp.
118 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
120 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
123 * Absolute time at the sender. Only the most recent connect
124 * message implies which session is preferred by the sender.
126 struct GNUNET_TIME_AbsoluteNBO timestamp;
129 * Public key of the sender.
131 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
134 * Signature of the peer that sends us the disconnect. Only
135 * valid if the timestamp is AFTER the timestamp from the
136 * corresponding 'CONNECT' message.
138 struct GNUNET_CRYPTO_RsaSignature signature;
141 GNUNET_NETWORK_STRUCT_END
144 * For each neighbour we keep a list of messages
145 * that we still want to transmit to the neighbour.
151 * This is a doubly linked list.
153 struct MessageQueue *next;
156 * This is a doubly linked list.
158 struct MessageQueue *prev;
161 * Once this message is actively being transmitted, which
162 * neighbour is it associated with?
164 struct NeighbourMapEntry *n;
167 * Function to call once we're done.
169 GST_NeighbourSendContinuation cont;
177 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
178 * stuck together in memory. Allocated at the end of this struct.
180 const char *message_buf;
183 * Size of the message buf
185 size_t message_buf_size;
188 * At what time should we fail?
190 struct GNUNET_TIME_Absolute timeout;
198 * fresh peer or completely disconnected
203 * sent CONNECT message to other peer, waiting for CONNECT_ACK
208 * received CONNECT message to other peer, sending CONNECT_ACK
213 * received ACK or payload
218 * connection ended, fast reconnect
223 * Disconnect in progress
237 * Entry in neighbours.
239 struct NeighbourMapEntry
243 * Head of list of messages we would like to send to this peer;
244 * must contain at most one message per client.
246 struct MessageQueue *messages_head;
249 * Tail of list of messages we would like to send to this peer; must
250 * contain at most one message per client.
252 struct MessageQueue *messages_tail;
255 * Are we currently trying to send a message? If so, which one?
257 struct MessageQueue *is_active;
260 * Active session for communicating with the peer.
262 struct Session *session;
265 * Address we currently use.
267 struct GNUNET_HELLO_Address *address;
270 * Address we currently use.
272 struct GNUNET_HELLO_Address *fast_reconnect_address;
276 * Identity of this neighbour.
278 struct GNUNET_PeerIdentity id;
281 * ID of task scheduled to run when this peer is about to
282 * time out (will free resources associated with the peer).
284 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
287 * ID of task scheduled to send keepalives.
289 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
292 * ID of task scheduled to run when we should try transmitting
293 * the head of the message queue.
295 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
298 * Tracker for inbound bandwidth.
300 struct GNUNET_BANDWIDTH_Tracker in_tracker;
303 * Inbound bandwidth from ATS, activated when connection is up
305 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
308 * Inbound bandwidth from ATS, activated when connection is up
310 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
313 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
315 struct GNUNET_TIME_Absolute connect_ts;
318 * When did we sent the last keep-alive message?
320 struct GNUNET_TIME_Absolute keep_alive_sent;
323 * Latest calculated latency value
325 struct GNUNET_TIME_Relative latency;
329 * We asked ATS for a new address for this peer
331 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
334 * Delay for ATS request
336 GNUNET_SCHEDULER_TaskIdentifier ats_request;
339 * Task the resets the peer state after due to an pending
340 * unsuccessful connection setup
342 GNUNET_SCHEDULER_TaskIdentifier state_reset;
346 * How often has the other peer (recently) violated the inbound
347 * traffic limit? Incremented by 10 per violation, decremented by 1
348 * per non-violation (for each time interval).
350 unsigned int quota_violation_count;
354 * The current state of the peer
355 * Element of enum State
360 * Did we sent an KEEP_ALIVE message and are we expecting a response?
362 int expect_latency_response;
365 * Was the address used successfully
370 * Fast reconnect attempts for identical address
372 unsigned int fast_reconnect_attempts;
377 * All known neighbours and their HELLOs.
379 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
382 * Closure for connect_notify_cb, disconnect_notify_cb and address_change_cb
384 static void *callback_cls;
387 * Function to call when we connected to a neighbour.
389 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
392 * Function to call when we disconnected from a neighbour.
394 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
397 * Function to call when we changed an active address of a neighbour.
399 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
402 * counter for connected neighbours
404 static int neighbours_connected;
407 * Lookup a neighbour entry in the neighbours hash map.
409 * @param pid identity of the peer to look up
410 * @return the entry, NULL if there is no existing record
412 static struct NeighbourMapEntry *
413 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
415 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
419 * Disconnect from the given neighbour, clean up the record.
421 * @param n neighbour to disconnect from
424 disconnect_neighbour (struct NeighbourMapEntry *n);
426 #define change_state(n, state, ...) change (n, state, __LINE__)
429 is_connecting (struct NeighbourMapEntry *n)
431 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
437 is_connected (struct NeighbourMapEntry *n)
439 if (n->state == S_CONNECTED)
445 is_disconnecting (struct NeighbourMapEntry *n)
447 if (n->state == S_DISCONNECT)
453 print_state (int state)
458 return "S_CONNECTED";
461 return "S_CONNECT_RECV";
464 return "S_CONNECT_SENT";
467 return "S_DISCONNECT";
469 case S_NOT_CONNECTED:
470 return "S_NOT_CONNECTED";
472 case S_FAST_RECONNECT:
473 return "S_FAST_RECONNECT";
483 change (struct NeighbourMapEntry *n, int state, int line);
486 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
490 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
492 struct NeighbourMapEntry *n = cls;
497 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
498 if (n->state == S_CONNECTED)
502 GNUNET_STATISTICS_update (GST_stats,
504 ("# failed connection attempts due to timeout"), 1,
508 /* resetting state */
510 if (n->state == S_FAST_RECONNECT)
512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
513 "Fast reconnect time out, disconnecting peer `%s'\n",
514 GNUNET_i2s (&n->id));
515 disconnect_neighbour(n);
519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
520 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
521 GNUNET_i2s (&n->id), n, print_state(n->state), "S_NOT_CONNECTED", __LINE__);
523 n->state = S_NOT_CONNECTED;
525 /* destroying address */
526 if (n->address != NULL)
528 GNUNET_assert (strlen (n->address->transport_name) > 0);
529 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
532 /* request new address */
533 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
534 GNUNET_SCHEDULER_cancel (n->ats_suggest);
536 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
538 GNUNET_ATS_suggest_address (GST_ats, &n->id);
542 change (struct NeighbourMapEntry *n, int state, int line)
545 /* allowed transitions */
546 int allowed = GNUNET_NO;
548 previous_state = n->state;
552 case S_NOT_CONNECTED:
553 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
554 (state == S_DISCONNECT))
555 allowed = GNUNET_YES;
558 allowed = GNUNET_YES;
561 allowed = GNUNET_YES;
564 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
565 allowed = GNUNET_YES;
569 case S_FAST_RECONNECT:
570 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
571 allowed = GNUNET_YES;
577 if (allowed == GNUNET_NO)
579 char *old = GNUNET_strdup (print_state (n->state));
580 char *new = GNUNET_strdup (print_state (state));
582 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
583 "Illegal state transition from `%s' to `%s' in line %u \n", old,
588 return GNUNET_SYSERR;
591 char *old = GNUNET_strdup (print_state (n->state));
592 char *new = GNUNET_strdup (print_state (state));
594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
595 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
596 GNUNET_i2s (&n->id), n, old, new, line);
604 case S_FAST_RECONNECT:
607 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
608 GNUNET_SCHEDULER_cancel (n->state_reset);
610 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
613 case S_NOT_CONNECTED:
615 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
618 char *old = GNUNET_strdup (print_state (n->state));
619 char *new = GNUNET_strdup (print_state (state));
621 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
622 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
623 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), old, new);
627 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
628 GNUNET_SCHEDULER_cancel (n->state_reset);
629 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
637 if (NULL != address_change_cb)
639 if (n->state == S_CONNECTED)
640 address_change_cb (callback_cls, &n->id, n->address);
641 else if (previous_state == S_CONNECTED)
642 address_change_cb (callback_cls, &n->id, NULL);
649 send_with_session (struct NeighbourMapEntry *n,
650 const char *msgbuf, size_t msgbuf_size,
652 struct GNUNET_TIME_Relative timeout,
653 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
655 struct GNUNET_TRANSPORT_PluginFunctions *papi;
656 size_t ret = GNUNET_SYSERR;
658 GNUNET_assert (n != NULL);
659 GNUNET_assert (n->session != NULL);
661 papi = GST_plugins_find (n->address->transport_name);
665 cont (cont_cls, &n->id, GNUNET_SYSERR);
666 return GNUNET_SYSERR;
669 ret = papi->send (papi->cls,
676 if ((ret == -1) && (cont != NULL))
677 cont (cont_cls, &n->id, GNUNET_SYSERR);
682 * Task invoked to start a transmission to another peer.
684 * @param cls the 'struct NeighbourMapEntry'
685 * @param tc scheduler context
688 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
692 * We're done with our transmission attempt, continue processing.
694 * @param cls the 'struct MessageQueue' of the message
695 * @param receiver intended receiver
696 * @param success whether it worked or not
699 transmit_send_continuation (void *cls,
700 const struct GNUNET_PeerIdentity *receiver,
703 struct MessageQueue *mq = cls;
704 struct NeighbourMapEntry *n;
705 struct NeighbourMapEntry *tmp;
707 tmp = lookup_neighbour (receiver);
709 if ((NULL != n) && (tmp != NULL) && (tmp == n))
711 GNUNET_assert (n->is_active == mq);
713 if (success == GNUNET_YES)
715 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
716 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
720 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
721 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
722 (success == GNUNET_OK) ? "successful" : "FAILED");
724 if (NULL != mq->cont)
725 mq->cont (mq->cont_cls, success);
731 * Check the ready list for the given neighbour and if a plugin is
732 * ready for transmission (and if we have a message), do so!
734 * @param n target peer for which to transmit
737 try_transmission_to_peer (struct NeighbourMapEntry *n)
739 struct MessageQueue *mq;
740 struct GNUNET_TIME_Relative timeout;
743 if (n->is_active != NULL)
746 return; /* transmission already pending */
748 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
751 return; /* currently waiting for bandwidth */
753 while (NULL != (mq = n->messages_head))
755 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
756 if (timeout.rel_value > 0)
758 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
761 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
764 return; /* no more messages */
766 if (n->address == NULL)
769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
770 GNUNET_i2s (&n->id));
772 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
773 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
774 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
775 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
779 if (GST_plugins_find (n->address->transport_name) == NULL)
784 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
788 if ((n->address->address_length == 0) && (n->session == NULL))
790 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
791 GNUNET_i2s (&n->id));
792 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
793 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
794 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
798 ret = send_with_session(n,
799 mq->message_buf, mq->message_buf_size,
801 &transmit_send_continuation, mq);
805 /* failure, but 'send' would not call continuation in this case,
806 * so we need to do it here! */
807 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
814 * Task invoked to start a transmission to another peer.
816 * @param cls the 'struct NeighbourMapEntry'
817 * @param tc scheduler context
820 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
822 struct NeighbourMapEntry *n = cls;
824 GNUNET_assert (NULL != lookup_neighbour (&n->id));
825 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
826 try_transmission_to_peer (n);
831 * Initialize the neighbours subsystem.
833 * @param cls closure for callbacks
834 * @param connect_cb function to call if we connect to a peer
835 * @param disconnect_cb function to call if we disconnect from a peer
836 * @param peer_address_cb function to call if we change an active address
840 GST_neighbours_start (void *cls,
841 GNUNET_TRANSPORT_NotifyConnect connect_cb,
842 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
843 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
846 connect_notify_cb = connect_cb;
847 disconnect_notify_cb = disconnect_cb;
848 address_change_cb = peer_address_cb;
849 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
854 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
858 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
859 "Sending DISCONNECT message to peer `%4s': %i\n",
860 GNUNET_i2s (target), result);
866 send_disconnect (struct NeighbourMapEntry * n)
869 struct SessionDisconnectMessage disconnect_msg;
872 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
873 "Sending DISCONNECT message to peer `%4s'\n",
874 GNUNET_i2s (&n->id));
877 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
878 disconnect_msg.header.type =
879 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
880 disconnect_msg.reserved = htonl (0);
881 disconnect_msg.purpose.size =
882 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
883 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
884 sizeof (struct GNUNET_TIME_AbsoluteNBO));
885 disconnect_msg.purpose.purpose =
886 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
887 disconnect_msg.timestamp =
888 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
889 disconnect_msg.public_key = GST_my_public_key;
890 GNUNET_assert (GNUNET_OK ==
891 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
892 &disconnect_msg.purpose,
893 &disconnect_msg.signature));
895 ret = send_with_session (n,
896 (const char *) &disconnect_msg, sizeof (disconnect_msg),
897 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
898 &send_disconnect_cont, NULL);
900 if (ret == GNUNET_SYSERR)
901 return GNUNET_SYSERR;
903 GNUNET_STATISTICS_update (GST_stats,
905 ("# peers disconnected due to external request"), 1,
912 * Disconnect from the given neighbour, clean up the record.
914 * @param n neighbour to disconnect from
917 disconnect_neighbour (struct NeighbourMapEntry *n)
919 struct MessageQueue *mq;
922 previous_state = n->state;
924 if (is_disconnecting (n))
927 /* send DISCONNECT MESSAGE */
928 if (previous_state == S_CONNECTED)
930 if (GNUNET_OK == send_disconnect (n))
931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
932 GNUNET_i2s (&n->id));
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
935 "Could not send DISCONNECT_MSG to `%s'\n",
936 GNUNET_i2s (&n->id));
939 change_state (n, S_DISCONNECT);
941 if (previous_state == S_CONNECTED)
943 GNUNET_assert (NULL != n->address);
944 if (n->address_state == USED)
946 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
947 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
948 n->address_state = UNUSED;
952 if (n->address != NULL)
954 struct GNUNET_TRANSPORT_PluginFunctions *papi;
956 papi = GST_plugins_find (n->address->transport_name);
958 papi->disconnect (papi->cls, &n->id);
960 while (NULL != (mq = n->messages_head))
962 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
963 if (NULL != mq->cont)
964 mq->cont (mq->cont_cls, GNUNET_SYSERR);
967 if (NULL != n->is_active)
969 n->is_active->n = NULL;
973 switch (previous_state)
976 GNUNET_assert (neighbours_connected > 0);
977 neighbours_connected--;
978 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
979 GNUNET_SCHEDULER_cancel (n->keepalive_task);
980 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
981 n->expect_latency_response = GNUNET_NO;
982 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
984 disconnect_notify_cb (callback_cls, &n->id);
986 case S_FAST_RECONNECT:
987 GNUNET_STATISTICS_update (GST_stats,
988 gettext_noop ("# fast reconnects failed"), 1,
990 disconnect_notify_cb (callback_cls, &n->id);
996 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
998 GNUNET_assert (GNUNET_YES ==
999 GNUNET_CONTAINER_multihashmap_remove (neighbours,
1000 &n->id.hashPubKey, n));
1001 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
1003 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1004 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1006 if (n->ats_request != GNUNET_SCHEDULER_NO_TASK)
1008 GNUNET_SCHEDULER_cancel (n->ats_request);
1009 n->ats_request = GNUNET_SCHEDULER_NO_TASK;
1012 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
1014 GNUNET_SCHEDULER_cancel (n->timeout_task);
1015 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1017 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
1019 GNUNET_SCHEDULER_cancel (n->transmission_task);
1020 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
1022 if (NULL != n->fast_reconnect_address)
1024 GNUNET_HELLO_address_free (n->fast_reconnect_address);
1025 n->fast_reconnect_address = NULL;
1027 if (NULL != n->address)
1029 GNUNET_HELLO_address_free (n->address);
1033 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
1034 GNUNET_i2s (&n->id), n);
1040 * Peer has been idle for too long. Disconnect.
1042 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1043 * @param tc scheduler context
1046 neighbour_timeout_task (void *cls,
1047 const struct GNUNET_SCHEDULER_TaskContext *tc)
1049 struct NeighbourMapEntry *n = cls;
1051 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1052 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Peer`%4s' disconnected due to timeout\n",
1053 GNUNET_i2s (&n->id));
1055 GNUNET_STATISTICS_update (GST_stats,
1057 ("# peers disconnected due to timeout"), 1,
1059 disconnect_neighbour (n);
1064 * Send another keepalive message.
1066 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1067 * @param tc scheduler context
1070 neighbour_keepalive_task (void *cls,
1071 const struct GNUNET_SCHEDULER_TaskContext *tc)
1073 struct NeighbourMapEntry *n = cls;
1074 struct GNUNET_MessageHeader m;
1077 GNUNET_assert (S_CONNECTED == n->state);
1079 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1080 &neighbour_keepalive_task, n);
1082 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1084 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1085 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1087 ret = send_with_session (n,
1088 (const void *) &m, sizeof (m),
1089 UINT32_MAX /* priority */ ,
1090 GNUNET_TIME_UNIT_FOREVER_REL,
1093 n->expect_latency_response = GNUNET_NO;
1094 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero ();
1095 if (ret != GNUNET_SYSERR)
1097 n->expect_latency_response = GNUNET_YES;
1098 n->keep_alive_sent = GNUNET_TIME_absolute_get ();
1105 * Disconnect from the given neighbour.
1108 * @param key hash of neighbour's public key (not used)
1109 * @param value the 'struct NeighbourMapEntry' of the neighbour
1112 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1114 struct NeighbourMapEntry *n = value;
1117 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1118 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1120 if (S_CONNECTED == n->state)
1121 GNUNET_STATISTICS_update (GST_stats,
1123 ("# peers disconnected due to global disconnect"),
1125 disconnect_neighbour (n);
1131 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1133 struct NeighbourMapEntry *n = cls;
1135 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1137 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1138 "ATS did not suggested address to connect to peer `%s'\n",
1139 GNUNET_i2s (&n->id));
1141 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# ATS address suggestions failed"), 1,
1145 disconnect_neighbour (n);
1149 * Cleanup the neighbours subsystem.
1152 GST_neighbours_stop ()
1154 // This can happen during shutdown
1155 if (neighbours == NULL)
1160 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1162 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1163 // GNUNET_assert (neighbours_connected == 0);
1165 callback_cls = NULL;
1166 connect_notify_cb = NULL;
1167 disconnect_notify_cb = NULL;
1168 address_change_cb = NULL;
1171 struct ContinutionContext
1173 struct GNUNET_HELLO_Address *address;
1175 struct Session *session;
1179 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
1180 struct GNUNET_BANDWIDTH_Value32NBO quota)
1182 struct QuotaSetMessage q_msg;
1185 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1186 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1187 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)))
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1239 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1241 change_state (n, S_NOT_CONNECTED);
1242 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1243 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1245 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1247 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1249 GNUNET_HELLO_address_free (cc->address);
1255 ats_request (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1257 struct NeighbourMapEntry *n = cls;
1258 n->ats_request = GNUNET_SCHEDULER_NO_TASK;
1261 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1267 * We tried to switch addresses with an peer already connected. If it failed,
1268 * we should tell ATS to not use this address anymore (until it is re-validated).
1270 * @param cls the 'struct NeighbourMapEntry'
1271 * @param target peer to send the message to
1272 * @param success GNUNET_OK on success
1275 send_switch_address_continuation (void *cls,
1276 const struct GNUNET_PeerIdentity *target,
1279 struct ContinutionContext *cc = cls;
1280 struct NeighbourMapEntry *n;
1282 if (neighbours == NULL)
1284 GNUNET_HELLO_address_free (cc->address);
1286 return; /* neighbour is going away */
1289 n = lookup_neighbour (&cc->address->peer);
1290 if ((n == NULL) || (is_disconnecting (n)))
1292 GNUNET_HELLO_address_free (cc->address);
1294 return; /* neighbour is going away */
1297 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1298 if (GNUNET_YES != success)
1301 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1302 "Failed to switch connected peer `%s' in state %s to address '%s' session %X, asking ATS for new address \n",
1303 GNUNET_i2s (&n->id), print_state(n->state), GST_plugins_a2s (cc->address), cc->session);
1305 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1306 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1308 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1310 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1311 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1314 if (n->state == S_FAST_RECONNECT)
1316 if (GNUNET_SCHEDULER_NO_TASK == n->ats_request)
1318 /* Throtteled ATS request for fast reconnect */
1319 struct GNUNET_TIME_Relative delay = GNUNET_TIME_relative_multiply(FAST_RECONNECT_RATE, n->fast_reconnect_attempts);
1320 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1321 "Fast reconnect attempt %u failed, delay ATS request for %llu ms\n", n->fast_reconnect_attempts, delay.rel_value);
1323 GNUNET_SCHEDULER_add_delayed (delay,
1330 /* Immediate ATS request for connected peers */
1332 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1334 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1336 GNUNET_HELLO_address_free (cc->address);
1340 /* Tell ATS that switching addresses was successful */
1344 if (n->address_state == FRESH)
1346 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES, __LINE__);
1347 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1348 if (cc->session != n->session)
1350 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1351 n->address_state = USED;
1354 case S_FAST_RECONNECT:
1356 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1357 "Successful fast reconnect to peer `%s'\n",
1358 GNUNET_i2s (&n->id));
1360 change_state (n, S_CONNECTED);
1361 neighbours_connected++;
1362 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1365 if (n->address_state == FRESH)
1367 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES, __LINE__);
1368 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1369 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1370 n->address_state = USED;
1373 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1375 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1377 /* Updating quotas */
1378 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1379 send_outbound_quota (target, n->bandwidth_out);
1384 GNUNET_HELLO_address_free (cc->address);
1390 * We tried to send a SESSION_CONNECT message to another peer. If this
1391 * succeeded, we change the state. If it failed, we should tell
1392 * ATS to not use this address anymore (until it is re-validated).
1394 * @param cls the 'struct NeighbourMapEntry'
1395 * @param target peer to send the message to
1396 * @param success GNUNET_OK on success
1399 send_connect_ack_continuation (void *cls,
1400 const struct GNUNET_PeerIdentity *target,
1403 struct ContinutionContext *cc = cls;
1404 struct NeighbourMapEntry *n;
1406 if (neighbours == NULL)
1408 GNUNET_HELLO_address_free (cc->address);
1410 return; /* neighbour is going away */
1413 n = lookup_neighbour (&cc->address->peer);
1414 if ((n == NULL) || (is_disconnecting (n)))
1416 GNUNET_HELLO_address_free (cc->address);
1418 return; /* neighbour is going away */
1421 if (GNUNET_YES == success)
1423 GNUNET_HELLO_address_free (cc->address);
1425 return; /* sending successful */
1428 /* sending failed, ask for next address */
1430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1431 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1432 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1434 change_state (n, S_NOT_CONNECTED);
1435 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1436 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1438 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1439 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1441 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1443 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1444 GNUNET_HELLO_address_free (cc->address);
1450 * For an existing neighbour record, set the active connection to
1451 * use the given address.
1453 * @param peer identity of the peer to switch the address for
1454 * @param address address of the other peer, NULL if other peer
1456 * @param session session to use (or NULL)
1457 * @param ats performance data
1458 * @param ats_count number of entries in ats
1459 * @param bandwidth_in inbound quota to be used when connection is up
1460 * @param bandwidth_out outbound quota to be used when connection is up
1461 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1462 * connection is not up (yet)
1465 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1466 const struct GNUNET_HELLO_Address
1468 struct Session *session,
1469 const struct GNUNET_ATS_Information *ats,
1471 struct GNUNET_BANDWIDTH_Value32NBO
1473 struct GNUNET_BANDWIDTH_Value32NBO
1476 struct NeighbourMapEntry *n;
1477 struct SessionConnectMessage connect_msg;
1478 struct ContinutionContext *cc;
1482 if (neighbours == NULL)
1484 /* This can happen during shutdown */
1487 n = lookup_neighbour (peer);
1490 if (n->state == S_DISCONNECT)
1492 /* We are disconnecting, nothing to do here */
1495 GNUNET_assert (address->transport_name != NULL);
1496 if ((session == NULL) && (0 == address->address_length))
1498 GNUNET_break_op (0);
1499 /* FIXME: is this actually possible? When does this happen? */
1500 if (strlen (address->transport_name) > 0)
1501 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1502 GNUNET_ATS_suggest_address (GST_ats, peer);
1506 /* checks successful and neighbour != NULL */
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1509 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
1512 print_state (n->state));
1514 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1516 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1517 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1521 if (n->state == S_FAST_RECONNECT)
1523 /* Throtteled fast reconnect */
1525 if (NULL == n->fast_reconnect_address)
1527 n->fast_reconnect_address = GNUNET_HELLO_address_copy (address);
1530 else if (0 == GNUNET_HELLO_address_cmp(address, n->fast_reconnect_address))
1532 n->fast_reconnect_attempts ++;
1534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1535 "FAST RECONNECT to peer `%s' and address '%s' with identical address attempt %u\n",
1536 GNUNET_i2s (&n->id), GST_plugins_a2s (address), n->fast_reconnect_attempts);
1541 n->fast_reconnect_attempts = 0;
1544 /* do not switch addresses just update quotas */
1545 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1546 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1547 (n->session == session))
1549 n->bandwidth_in = bandwidth_in;
1550 n->bandwidth_out = bandwidth_out;
1551 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1552 send_outbound_quota (peer, n->bandwidth_out);
1555 if (n->state == S_CONNECTED)
1557 /* mark old address as no longer used */
1558 GNUNET_assert (NULL != n->address);
1559 if (n->address_state == USED)
1561 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1562 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1563 n->address_state = UNUSED;
1567 /* set new address */
1568 if (NULL != n->address)
1569 GNUNET_HELLO_address_free (n->address);
1570 n->address = GNUNET_HELLO_address_copy (address);
1571 n->address_state = FRESH;
1572 n->bandwidth_in = bandwidth_in;
1573 n->bandwidth_out = bandwidth_out;
1574 GNUNET_SCHEDULER_cancel (n->timeout_task);
1576 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1577 &neighbour_timeout_task, n);
1579 if (NULL != address_change_cb && n->state == S_CONNECTED)
1580 address_change_cb (callback_cls, &n->id, n->address);
1582 /* Obtain an session for this address from plugin */
1583 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1584 papi = GST_plugins_find (address->transport_name);
1588 /* we don't have the plugin for this 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 if (session == NULL)
1605 n->session = papi->get_session (papi->cls, address);
1606 /* Session could not be initiated */
1607 if (n->session == NULL)
1609 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1610 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1611 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1613 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1615 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1616 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1617 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1620 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1621 GNUNET_HELLO_address_free (n->address);
1627 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1628 "Obtained new session %p for peer `%s' and address '%s'\n",
1629 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1630 /* Telling ATS about new session */
1631 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1635 n->session = session;
1636 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1637 "Using existing session %p for peer `%s' and address '%s'\n",
1639 GNUNET_i2s (&n->id),
1640 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>");
1645 case S_NOT_CONNECTED:
1646 case S_CONNECT_SENT:
1647 msg_len = sizeof (struct SessionConnectMessage);
1648 connect_msg.header.size = htons (msg_len);
1649 connect_msg.header.type =
1650 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1651 connect_msg.reserved = htonl (0);
1652 connect_msg.timestamp =
1653 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1655 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1656 cc->session = n->session;
1657 cc->address = GNUNET_HELLO_address_copy (address);
1659 ret = send_with_session (n,
1660 (const char *) &connect_msg, msg_len,
1661 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1662 &send_connect_continuation, cc);
1665 case S_CONNECT_RECV:
1666 /* We received a CONNECT message and asked ATS for an address */
1667 msg_len = sizeof (struct SessionConnectMessage);
1668 connect_msg.header.size = htons (msg_len);
1669 connect_msg.header.type =
1670 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1671 connect_msg.reserved = htonl (0);
1672 connect_msg.timestamp =
1673 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1674 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1675 cc->session = n->session;
1676 cc->address = GNUNET_HELLO_address_copy (address);
1678 ret = send_with_session(n,
1679 (const void *) &connect_msg, msg_len,
1680 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1681 &send_connect_ack_continuation,
1685 case S_FAST_RECONNECT:
1686 /* connected peer is switching addresses or tries fast reconnect */
1687 msg_len = sizeof (struct SessionConnectMessage);
1688 connect_msg.header.size = htons (msg_len);
1689 connect_msg.header.type =
1690 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1691 connect_msg.reserved = htonl (0);
1692 connect_msg.timestamp =
1693 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1694 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1695 cc->session = n->session;
1696 cc->address = GNUNET_HELLO_address_copy (address);
1697 ret = send_with_session(n,
1698 (const void *) &connect_msg, msg_len,
1699 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1700 &send_switch_address_continuation, cc);
1701 if (ret == GNUNET_SYSERR)
1703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1704 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1705 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1709 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1710 "Invalid connection state to switch addresses %u \n", n->state);
1711 GNUNET_break_op (0);
1718 * Obtain current latency information for the given neighbour.
1721 * @return observed latency of the address, FOREVER if the address was
1722 * never successfully validated
1724 struct GNUNET_TIME_Relative
1725 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1727 struct NeighbourMapEntry *n;
1729 n = lookup_neighbour (peer);
1730 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1731 return GNUNET_TIME_UNIT_FOREVER_REL;
1737 * Obtain current address information for the given neighbour.
1740 * @return address currently used
1742 struct GNUNET_HELLO_Address *
1743 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1745 struct NeighbourMapEntry *n;
1747 n = lookup_neighbour (peer);
1748 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1757 * Create an entry in the neighbour map for the given peer
1759 * @param peer peer to create an entry for
1760 * @return new neighbour map entry
1762 static struct NeighbourMapEntry *
1763 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1765 struct NeighbourMapEntry *n;
1768 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1769 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1771 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1773 n->state = S_NOT_CONNECTED;
1774 n->latency = GNUNET_TIME_relative_get_forever ();
1775 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1776 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1777 MAX_BANDWIDTH_CARRY_S);
1779 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1780 &neighbour_timeout_task, n);
1781 GNUNET_assert (GNUNET_OK ==
1782 GNUNET_CONTAINER_multihashmap_put (neighbours,
1783 &n->id.hashPubKey, n,
1784 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1790 * Try to create a connection to the given target (eventually).
1792 * @param target peer to try to connect to
1795 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1797 struct NeighbourMapEntry *n;
1799 // This can happen during shutdown
1800 if (neighbours == NULL)
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1806 GNUNET_i2s (target));
1809 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1814 n = lookup_neighbour (target);
1818 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1819 return; /* already connecting or connected */
1820 if (is_disconnecting (n))
1821 change_state (n, S_NOT_CONNECTED);
1826 n = setup_neighbour (target);
1828 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1829 "Asking ATS for suggested address to connect to peer `%s'\n",
1830 GNUNET_i2s (&n->id));
1833 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1837 * Test if we're connected to the given peer.
1839 * @param target peer to test
1840 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1843 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1845 struct NeighbourMapEntry *n;
1847 // This can happen during shutdown
1848 if (neighbours == NULL)
1853 n = lookup_neighbour (target);
1855 if ((NULL == n) || (S_CONNECTED != n->state))
1856 return GNUNET_NO; /* not connected */
1861 * A session was terminated. Take note.
1863 * @param peer identity of the peer where the session died
1864 * @param session session that is gone
1867 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1868 struct Session *session)
1870 struct NeighbourMapEntry *n;
1872 if (neighbours == NULL)
1874 /* This can happen during shutdown */
1879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1880 session, GNUNET_i2s (peer));
1883 n = lookup_neighbour (peer);
1886 if (session != n->session)
1887 return; /* doesn't affect us */
1888 if (n->state == S_CONNECTED)
1890 if (n->address_state == USED)
1892 GST_validation_set_address_use (n->address, n->session, GNUNET_NO, __LINE__);
1893 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1894 n->address_state = UNUSED;
1899 if (NULL != n->address)
1901 GNUNET_HELLO_address_free (n->address);
1906 /* not connected anymore anyway, shouldn't matter */
1907 if (S_CONNECTED != n->state)
1910 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1912 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1913 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1914 n->expect_latency_response = GNUNET_NO;
1917 /* connected, try fast reconnect */
1918 /* statistics "transport" : "# peers connected" -= 1
1919 * neighbours_connected -= 1
1920 * BUT: no disconnect_cb to notify clients about disconnect
1923 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1926 GNUNET_assert (neighbours_connected > 0);
1927 change_state (n, S_FAST_RECONNECT);
1928 neighbours_connected--;
1929 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
1933 /* We are connected, so ask ATS to switch addresses */
1934 GNUNET_SCHEDULER_cancel (n->timeout_task);
1935 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1936 &neighbour_timeout_task, n);
1937 /* try QUICKLY to re-establish a connection, reduce timeout! */
1938 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1939 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1940 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1941 &ats_suggest_cancel,
1943 GNUNET_ATS_suggest_address (GST_ats, peer);
1948 * Transmit a message to the given target using the active connection.
1950 * @param target destination
1951 * @param msg message to send
1952 * @param msg_size number of bytes in msg
1953 * @param timeout when to fail with timeout
1954 * @param cont function to call when done
1955 * @param cont_cls closure for 'cont'
1958 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1959 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1960 GST_NeighbourSendContinuation cont, void *cont_cls)
1962 struct NeighbourMapEntry *n;
1963 struct MessageQueue *mq;
1965 // This can happen during shutdown
1966 if (neighbours == NULL)
1971 n = lookup_neighbour (target);
1972 if ((n == NULL) || (!is_connected (n)))
1974 GNUNET_STATISTICS_update (GST_stats,
1976 ("# messages not sent (no such peer or not connected)"),
1980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1981 "Could not send message to peer `%s': unknown neighbour",
1982 GNUNET_i2s (target));
1983 else if (!is_connected (n))
1984 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1985 "Could not send message to peer `%s': not connected\n",
1986 GNUNET_i2s (target));
1989 cont (cont_cls, GNUNET_SYSERR);
1993 if ((n->session == NULL) && (n->address == NULL))
1995 GNUNET_STATISTICS_update (GST_stats,
1997 ("# messages not sent (no such peer or not connected)"),
2000 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2001 "Could not send message to peer `%s': no address available\n",
2002 GNUNET_i2s (target));
2006 cont (cont_cls, GNUNET_SYSERR);
2010 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
2011 GNUNET_STATISTICS_update (GST_stats,
2013 ("# bytes in message queue for other peers"),
2014 msg_size, GNUNET_NO);
2015 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
2017 mq->cont_cls = cont_cls;
2018 /* FIXME: this memcpy can be up to 7% of our total runtime! */
2019 memcpy (&mq[1], msg, msg_size);
2020 mq->message_buf = (const char *) &mq[1];
2021 mq->message_buf_size = msg_size;
2022 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
2023 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
2025 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
2026 (NULL == n->is_active))
2027 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
2032 * We have received a message from the given sender. How long should
2033 * we delay before receiving more? (Also used to keep the peer marked
2036 * @param sender sender of the message
2037 * @param size size of the message
2038 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
2039 * GNUNET_NO if the neighbour is not connected or violates the quota,
2040 * GNUNET_SYSERR if the connection is not fully up yet
2041 * @return how long to wait before reading more from this sender
2043 struct GNUNET_TIME_Relative
2044 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
2045 *sender, ssize_t size, int *do_forward)
2047 struct NeighbourMapEntry *n;
2048 struct GNUNET_TIME_Relative ret;
2050 // This can happen during shutdown
2051 if (neighbours == NULL)
2053 return GNUNET_TIME_UNIT_FOREVER_REL;
2056 n = lookup_neighbour (sender);
2059 GST_neighbours_try_connect (sender);
2060 n = lookup_neighbour (sender);
2063 GNUNET_STATISTICS_update (GST_stats,
2065 ("# messages discarded due to lack of neighbour record"),
2067 *do_forward = GNUNET_NO;
2068 return GNUNET_TIME_UNIT_ZERO;
2071 if (!is_connected (n))
2073 *do_forward = GNUNET_SYSERR;
2074 return GNUNET_TIME_UNIT_ZERO;
2076 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
2078 n->quota_violation_count++;
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2081 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
2082 n->in_tracker.available_bytes_per_s__,
2083 n->quota_violation_count);
2085 /* Discount 32k per violation */
2086 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
2090 if (n->quota_violation_count > 0)
2092 /* try to add 32k back */
2093 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
2094 n->quota_violation_count--;
2097 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2099 GNUNET_STATISTICS_update (GST_stats,
2101 ("# bandwidth quota violations by other peers"),
2103 *do_forward = GNUNET_NO;
2104 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
2106 *do_forward = GNUNET_YES;
2107 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
2108 if (ret.rel_value > 0)
2111 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2112 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
2113 (unsigned long long) n->in_tracker.
2114 consumption_since_last_update__,
2115 (unsigned int) n->in_tracker.available_bytes_per_s__,
2116 (unsigned long long) ret.rel_value);
2118 GNUNET_STATISTICS_update (GST_stats,
2119 gettext_noop ("# ms throttling suggested"),
2120 (int64_t) ret.rel_value, GNUNET_NO);
2127 * Keep the connection to the given neighbour alive longer,
2128 * we received a KEEPALIVE (or equivalent).
2130 * @param neighbour neighbour to keep alive
2133 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
2135 struct NeighbourMapEntry *n;
2137 // This can happen during shutdown
2138 if (neighbours == NULL)
2143 n = lookup_neighbour (neighbour);
2146 GNUNET_STATISTICS_update (GST_stats,
2148 ("# KEEPALIVE messages discarded (not connected)"),
2152 GNUNET_SCHEDULER_cancel (n->timeout_task);
2154 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2155 &neighbour_timeout_task, n);
2157 /* send reply to measure latency */
2158 if (S_CONNECTED != n->state)
2161 struct GNUNET_MessageHeader m;
2163 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2164 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2166 send_with_session(n,
2167 (const void *) &m, sizeof (m),
2169 GNUNET_TIME_UNIT_FOREVER_REL,
2174 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2177 * @param neighbour neighbour to keep alive
2178 * @param ats performance data
2179 * @param ats_count number of entries in ats
2182 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2183 const struct GNUNET_ATS_Information *ats,
2186 struct NeighbourMapEntry *n;
2187 struct GNUNET_ATS_Information *ats_new;
2190 if (neighbours == NULL)
2192 // This can happen during shutdown
2196 n = lookup_neighbour (neighbour);
2197 if ((NULL == n) || (n->state != S_CONNECTED))
2199 GNUNET_STATISTICS_update (GST_stats,
2201 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2205 if (n->expect_latency_response != GNUNET_YES)
2207 GNUNET_STATISTICS_update (GST_stats,
2209 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2213 n->expect_latency_response = GNUNET_NO;
2215 GNUNET_assert (n->keep_alive_sent.abs_value !=
2216 GNUNET_TIME_absolute_get_zero ().abs_value);
2218 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2219 GNUNET_TIME_absolute_get ());
2221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2222 GNUNET_i2s (&n->id), n->latency.rel_value);
2226 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2228 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2233 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2235 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2238 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2239 if (n->latency.rel_value > UINT32_MAX)
2240 latency = UINT32_MAX;
2242 latency = n->latency.rel_value;
2243 ats_new[ats_count].value = htonl (latency);
2245 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2247 GNUNET_free (ats_new);
2253 * Change the incoming quota for the given peer.
2255 * @param neighbour identity of peer to change qutoa for
2256 * @param quota new quota
2259 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2260 struct GNUNET_BANDWIDTH_Value32NBO quota)
2262 struct NeighbourMapEntry *n;
2264 // This can happen during shutdown
2265 if (neighbours == NULL)
2270 n = lookup_neighbour (neighbour);
2273 GNUNET_STATISTICS_update (GST_stats,
2275 ("# SET QUOTA messages ignored (no such peer)"),
2280 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2281 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2282 ntohl (quota.value__), GNUNET_i2s (&n->id));
2284 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2285 if (0 != ntohl (quota.value__))
2288 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2289 GNUNET_i2s (&n->id), "SET_QUOTA");
2291 if (is_connected (n))
2292 GNUNET_STATISTICS_update (GST_stats,
2293 gettext_noop ("# disconnects due to quota of 0"),
2295 disconnect_neighbour (n);
2300 * Closure for the neighbours_iterate function.
2302 struct IteratorContext
2305 * Function to call on each connected neighbour.
2307 GST_NeighbourIterator cb;
2317 * Call the callback from the closure for each connected neighbour.
2319 * @param cls the 'struct IteratorContext'
2320 * @param key the hash of the public key of the neighbour
2321 * @param value the 'struct NeighbourMapEntry'
2322 * @return GNUNET_OK (continue to iterate)
2325 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2327 struct IteratorContext *ic = cls;
2328 struct NeighbourMapEntry *n = value;
2330 if (!is_connected (n))
2333 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2339 * Iterate over all connected neighbours.
2341 * @param cb function to call
2342 * @param cb_cls closure for cb
2345 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2347 struct IteratorContext ic;
2349 // This can happen during shutdown
2350 if (neighbours == NULL)
2357 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2361 * If we have an active connection to the given target, it must be shutdown.
2363 * @param target peer to disconnect from
2366 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2368 struct NeighbourMapEntry *n;
2370 // This can happen during shutdown
2371 if (neighbours == NULL)
2376 n = lookup_neighbour (target);
2378 return; /* not active */
2379 disconnect_neighbour (n);
2384 * We received a disconnect message from the given peer,
2385 * validate and process.
2387 * @param peer sender of the message
2388 * @param msg the disconnect message
2391 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2393 const struct GNUNET_MessageHeader
2396 struct NeighbourMapEntry *n;
2397 const struct SessionDisconnectMessage *sdm;
2401 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2402 "Received DISCONNECT message from peer `%s'\n",
2406 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2408 // GNUNET_break_op (0);
2409 GNUNET_STATISTICS_update (GST_stats,
2411 ("# disconnect messages ignored (old format)"), 1,
2415 sdm = (const struct SessionDisconnectMessage *) msg;
2416 n = lookup_neighbour (peer);
2418 return; /* gone already */
2419 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2420 n->connect_ts.abs_value)
2422 GNUNET_STATISTICS_update (GST_stats,
2424 ("# disconnect messages ignored (timestamp)"), 1,
2428 GNUNET_CRYPTO_hash (&sdm->public_key,
2429 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2431 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2433 GNUNET_break_op (0);
2436 if (ntohl (sdm->purpose.size) !=
2437 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2438 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2439 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2441 GNUNET_break_op (0);
2445 GNUNET_CRYPTO_rsa_verify
2446 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2447 &sdm->signature, &sdm->public_key))
2449 GNUNET_break_op (0);
2452 GST_neighbours_force_disconnect (peer);
2457 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2458 * Consider switching to it.
2460 * @param message possibly a 'struct SessionConnectMessage' (check format)
2461 * @param peer identity of the peer to switch the address for
2462 * @param address address of the other peer, NULL if other peer
2464 * @param session session to use (or NULL)
2465 * @param ats performance data
2466 * @param ats_count number of entries in ats
2469 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2470 const struct GNUNET_PeerIdentity *peer,
2471 const struct GNUNET_HELLO_Address *address,
2472 struct Session *session,
2473 const struct GNUNET_ATS_Information *ats,
2476 const struct SessionConnectMessage *scm;
2477 struct GNUNET_MessageHeader msg;
2478 struct NeighbourMapEntry *n;
2482 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2483 "Received CONNECT_ACK message from peer `%s'\n",
2486 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2488 GNUNET_break_op (0);
2491 scm = (const struct SessionConnectMessage *) message;
2492 GNUNET_break_op (ntohl (scm->reserved) == 0);
2493 n = lookup_neighbour (peer);
2496 /* we did not send 'CONNECT' -- at least not recently */
2497 GNUNET_STATISTICS_update (GST_stats,
2499 ("# unexpected CONNECT_ACK messages (no peer)"),
2506 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2508 * We also received an CONNECT message, switched from SENDT to RECV and
2509 * ATS already suggested us an address after a successful blacklist check
2512 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2513 "Received CONNECT_ACK message from peer `%s' in state `%s'\n",
2515 print_state(n->state));
2517 if ((n->address != NULL) && (n->state == S_CONNECTED))
2519 /* After fast reconnect: send ACK (ACK) even when we are connected */
2520 msg_len = sizeof (msg);
2521 msg.size = htons (msg_len);
2522 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2524 ret = send_with_session(n,
2525 (const char *) &msg, msg_len,
2526 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2529 if (ret == GNUNET_SYSERR)
2530 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2531 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2532 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2536 if ((n->state != S_CONNECT_SENT) &&
2537 ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2539 GNUNET_STATISTICS_update (GST_stats,
2541 ("# unexpected CONNECT_ACK messages"), 1,
2545 if (n->state != S_CONNECTED)
2546 change_state (n, S_CONNECTED);
2548 if (NULL != session)
2550 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2552 "Giving ATS session %p of plugin %s for peer %s\n",
2553 session, address->transport_name, GNUNET_i2s (peer));
2555 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2556 GNUNET_assert (NULL != n->address);
2558 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2560 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2561 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2562 n->address_state = USED;
2565 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2567 /* send ACK (ACK) */
2568 msg_len = sizeof (msg);
2569 msg.size = htons (msg_len);
2570 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2572 ret = send_with_session(n,
2573 (const char *) &msg, msg_len,
2574 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2577 if (ret == GNUNET_SYSERR)
2578 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2579 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2580 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2583 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2584 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2586 neighbours_connected++;
2587 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2591 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2592 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2595 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2596 send_outbound_quota (peer, n->bandwidth_out);
2602 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2603 const struct GNUNET_PeerIdentity *peer,
2604 const struct GNUNET_HELLO_Address *address,
2605 struct Session *session,
2606 const struct GNUNET_ATS_Information *ats,
2609 struct NeighbourMapEntry *n;
2612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2616 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2618 GNUNET_break_op (0);
2621 n = lookup_neighbour (peer);
2627 if (S_CONNECTED == n->state)
2629 if (!is_connecting (n))
2631 GNUNET_STATISTICS_update (GST_stats,
2632 gettext_noop ("# unexpected ACK messages"), 1,
2636 change_state (n, S_CONNECTED);
2637 if (NULL != session)
2638 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2640 "Giving ATS session %p of plugin %s for peer %s\n",
2641 session, address->transport_name, GNUNET_i2s (peer));
2642 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2643 GNUNET_assert (n->address != NULL);
2645 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2647 GST_validation_set_address_use (n->address, n->session, GNUNET_YES, __LINE__);
2648 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2649 n->address_state = USED;
2653 neighbours_connected++;
2654 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2657 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2658 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2659 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2661 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2662 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2663 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2666 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2667 send_outbound_quota (peer, n->bandwidth_out);
2670 struct BlackListCheckContext
2672 struct GNUNET_ATS_Information *ats;
2676 struct Session *session;
2678 struct GNUNET_HELLO_Address *address;
2680 struct GNUNET_TIME_Absolute ts;
2685 handle_connect_blacklist_cont (void *cls,
2686 const struct GNUNET_PeerIdentity *peer,
2689 struct NeighbourMapEntry *n;
2690 struct BlackListCheckContext *bcc = cls;
2693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2694 "Blacklist check due to CONNECT message: `%s'\n",
2696 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2700 if (GNUNET_OK != result)
2702 GNUNET_HELLO_address_free (bcc->address);
2707 n = lookup_neighbour (peer);
2709 n = setup_neighbour (peer);
2711 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2713 if (NULL != bcc->session)
2714 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2716 "Giving ATS session %p of address `%s' for peer %s\n",
2717 bcc->session, GST_plugins_a2s (bcc->address),
2719 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2721 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2723 n->connect_ts = bcc->ts;
2726 GNUNET_HELLO_address_free (bcc->address);
2729 if (n->state != S_CONNECT_RECV)
2730 change_state (n, S_CONNECT_RECV);
2733 /* Ask ATS for an address to connect via that address */
2734 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2735 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2737 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2739 GNUNET_ATS_suggest_address (GST_ats, peer);
2743 * We received a 'SESSION_CONNECT' message from the other peer.
2744 * Consider switching to it.
2746 * @param message possibly a 'struct SessionConnectMessage' (check format)
2747 * @param peer identity of the peer to switch the address for
2748 * @param address address of the other peer, NULL if other peer
2750 * @param session session to use (or NULL)
2751 * @param ats performance data
2752 * @param ats_count number of entries in ats (excluding 0-termination)
2755 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2756 const struct GNUNET_PeerIdentity *peer,
2757 const struct GNUNET_HELLO_Address *address,
2758 struct Session *session,
2759 const struct GNUNET_ATS_Information *ats,
2762 const struct SessionConnectMessage *scm;
2763 struct BlackListCheckContext *bcc = NULL;
2764 struct NeighbourMapEntry *n;
2767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2768 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2771 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2773 GNUNET_break_op (0);
2777 scm = (const struct SessionConnectMessage *) message;
2778 GNUNET_break_op (ntohl (scm->reserved) == 0);
2780 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2782 n = lookup_neighbour (peer);
2783 if ((n != NULL) && ((S_CONNECTED == n->state) || (S_FAST_RECONNECT == n->state)))
2785 /* connected peer switches addresses or is trying to do a fast reconnect*/
2790 /* we are not connected to this peer */
2791 /* do blacklist check */
2793 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2794 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2795 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2796 bcc->ats_count = ats_count + 1;
2797 bcc->address = GNUNET_HELLO_address_copy (address);
2798 bcc->session = session;
2799 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2800 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2801 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2802 bcc->ats[ats_count].value =
2803 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2804 GST_blacklist_test_allowed (peer, address->transport_name,
2805 &handle_connect_blacklist_cont, bcc);
2809 /* end of file gnunet-service-transport_neighbours.c */