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, 3)
62 #define FAST_RECONNECT_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 1)
64 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
68 * Entry in neighbours.
70 struct NeighbourMapEntry;
72 GNUNET_NETWORK_STRUCT_BEGIN
75 * Message a peer sends to another to indicate its
76 * preference for communicating via a particular
77 * session (and the desire to establish a real
80 struct SessionConnectMessage
83 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
85 struct GNUNET_MessageHeader header;
90 uint32_t reserved GNUNET_PACKED;
93 * Absolute time at the sender. Only the most recent connect
94 * message implies which session is preferred by the sender.
96 struct GNUNET_TIME_AbsoluteNBO timestamp;
101 struct SessionDisconnectMessage
104 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
106 struct GNUNET_MessageHeader header;
111 uint32_t reserved GNUNET_PACKED;
114 * Purpose of the signature. Extends over the timestamp.
115 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
117 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
120 * Absolute time at the sender. Only the most recent connect
121 * message implies which session is preferred by the sender.
123 struct GNUNET_TIME_AbsoluteNBO timestamp;
126 * Public key of the sender.
128 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
131 * Signature of the peer that sends us the disconnect. Only
132 * valid if the timestamp is AFTER the timestamp from the
133 * corresponding 'CONNECT' message.
135 struct GNUNET_CRYPTO_RsaSignature signature;
138 GNUNET_NETWORK_STRUCT_END
141 * For each neighbour we keep a list of messages
142 * that we still want to transmit to the neighbour.
148 * This is a doubly linked list.
150 struct MessageQueue *next;
153 * This is a doubly linked list.
155 struct MessageQueue *prev;
158 * Once this message is actively being transmitted, which
159 * neighbour is it associated with?
161 struct NeighbourMapEntry *n;
164 * Function to call once we're done.
166 GST_NeighbourSendContinuation cont;
174 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
175 * stuck together in memory. Allocated at the end of this struct.
177 const char *message_buf;
180 * Size of the message buf
182 size_t message_buf_size;
185 * At what time should we fail?
187 struct GNUNET_TIME_Absolute timeout;
195 * fresh peer or completely disconnected
200 * sent CONNECT message to other peer, waiting for CONNECT_ACK
205 * received CONNECT message to other peer, sending CONNECT_ACK
210 * received ACK or payload
215 * connection ended, fast reconnect
220 * Disconnect in progress
234 * Entry in neighbours.
236 struct NeighbourMapEntry
240 * Head of list of messages we would like to send to this peer;
241 * must contain at most one message per client.
243 struct MessageQueue *messages_head;
246 * Tail of list of messages we would like to send to this peer; must
247 * contain at most one message per client.
249 struct MessageQueue *messages_tail;
252 * Are we currently trying to send a message? If so, which one?
254 struct MessageQueue *is_active;
257 * Active session for communicating with the peer.
259 struct Session *session;
262 * Address we currently use.
264 struct GNUNET_HELLO_Address *address;
267 * Identity of this neighbour.
269 struct GNUNET_PeerIdentity id;
272 * ID of task scheduled to run when this peer is about to
273 * time out (will free resources associated with the peer).
275 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
278 * ID of task scheduled to send keepalives.
280 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
283 * ID of task scheduled to run when we should try transmitting
284 * the head of the message queue.
286 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
289 * Tracker for inbound bandwidth.
291 struct GNUNET_BANDWIDTH_Tracker in_tracker;
294 * Inbound bandwidth from ATS, activated when connection is up
296 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
299 * Inbound bandwidth from ATS, activated when connection is up
301 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
304 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
306 struct GNUNET_TIME_Absolute connect_ts;
309 * When did we sent the last keep-alive message?
311 struct GNUNET_TIME_Absolute keep_alive_sent;
314 * Latest calculated latency value
316 struct GNUNET_TIME_Relative latency;
320 * We asked ATS for a new address for this peer
322 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
325 * Task the resets the peer state after due to an pending
326 * unsuccessful connection setup
328 GNUNET_SCHEDULER_TaskIdentifier state_reset;
332 * How often has the other peer (recently) violated the inbound
333 * traffic limit? Incremented by 10 per violation, decremented by 1
334 * per non-violation (for each time interval).
336 unsigned int quota_violation_count;
340 * The current state of the peer
341 * Element of enum State
346 * Did we sent an KEEP_ALIVE message and are we expecting a response?
348 int expect_latency_response;
354 * All known neighbours and their HELLOs.
356 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
359 * Closure for connect_notify_cb and disconnect_notify_cb
361 static void *callback_cls;
364 * Function to call when we connected to a neighbour.
366 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
369 * Function to call when we disconnected from a neighbour.
371 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
374 * counter for connected neighbours
376 static int neighbours_connected;
379 * Lookup a neighbour entry in the neighbours hash map.
381 * @param pid identity of the peer to look up
382 * @return the entry, NULL if there is no existing record
384 static struct NeighbourMapEntry *
385 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
387 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
390 #define change_state(n, state, ...) change (n, state, __LINE__)
393 is_connecting (struct NeighbourMapEntry *n)
395 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
401 is_connected (struct NeighbourMapEntry *n)
403 if (n->state == S_CONNECTED)
409 is_disconnecting (struct NeighbourMapEntry *n)
411 if (n->state == S_DISCONNECT)
417 print_state (int state)
422 return "S_CONNECTED";
425 return "S_CONNECT_RECV";
428 return "S_CONNECT_SENT";
431 return "S_DISCONNECT";
433 case S_NOT_CONNECTED:
434 return "S_NOT_CONNECTED";
436 case S_FAST_RECONNECT:
437 return "S_FAST_RECONNECT";
447 change (struct NeighbourMapEntry *n, int state, int line);
450 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
454 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
456 struct NeighbourMapEntry *n = cls;
461 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
462 if (n->state == S_CONNECTED)
466 GNUNET_STATISTICS_update (GST_stats,
468 ("# failed connection attempts due to timeout"), 1,
472 /* resetting state */
473 n->state = S_NOT_CONNECTED;
475 /* destroying address */
476 if (n->address != NULL)
478 GNUNET_assert (strlen (n->address->transport_name) > 0);
479 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
482 /* request new address */
483 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
484 GNUNET_SCHEDULER_cancel (n->ats_suggest);
486 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
488 GNUNET_ATS_suggest_address (GST_ats, &n->id);
492 change (struct NeighbourMapEntry *n, int state, int line)
494 /* allowed transitions */
495 int allowed = GNUNET_NO;
499 case S_NOT_CONNECTED:
500 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
501 (state == S_DISCONNECT))
502 allowed = GNUNET_YES;
505 allowed = GNUNET_YES;
508 allowed = GNUNET_YES;
511 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
512 allowed = GNUNET_YES;
516 case S_FAST_RECONNECT:
517 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
518 allowed = GNUNET_YES;
524 if (allowed == GNUNET_NO)
526 char *old = GNUNET_strdup (print_state (n->state));
527 char *new = GNUNET_strdup (print_state (state));
529 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
530 "Illegal state transition from `%s' to `%s' in line %u \n", old,
535 return GNUNET_SYSERR;
539 char *old = GNUNET_strdup (print_state (n->state));
540 char *new = GNUNET_strdup (print_state (state));
542 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
543 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
544 GNUNET_i2s (&n->id), n, old, new, line);
553 case S_FAST_RECONNECT:
556 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
557 GNUNET_SCHEDULER_cancel (n->state_reset);
559 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
562 case S_NOT_CONNECTED:
564 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
567 char *old = GNUNET_strdup (print_state (n->state));
568 char *new = GNUNET_strdup (print_state (state));
570 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
571 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
572 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), old, new);
576 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
577 GNUNET_SCHEDULER_cancel (n->state_reset);
578 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
592 send_with_plugin (const struct GNUNET_PeerIdentity *target, const char *msgbuf,
593 size_t msgbuf_size, uint32_t priority,
594 struct GNUNET_TIME_Relative timeout, struct Session *session,
595 const struct GNUNET_HELLO_Address *address, int force_address,
596 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
598 struct GNUNET_TRANSPORT_PluginFunctions *papi;
599 size_t ret = GNUNET_SYSERR;
601 /* FIXME : ats returns an address with all values 0 */
605 cont (cont_cls, target, GNUNET_SYSERR);
606 return GNUNET_SYSERR;
609 if ((session == NULL) && (address->address_length == 0))
612 cont (cont_cls, target, GNUNET_SYSERR);
613 return GNUNET_SYSERR;
616 papi = GST_plugins_find (address->transport_name);
620 cont (cont_cls, target, GNUNET_SYSERR);
621 return GNUNET_SYSERR;
625 papi->send (papi->cls, target, msgbuf, msgbuf_size, 0, timeout, session,
626 address->address, address->address_length, GNUNET_YES, cont,
632 cont (cont_cls, target, GNUNET_SYSERR);
639 * Task invoked to start a transmission to another peer.
641 * @param cls the 'struct NeighbourMapEntry'
642 * @param tc scheduler context
645 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
649 * We're done with our transmission attempt, continue processing.
651 * @param cls the 'struct MessageQueue' of the message
652 * @param receiver intended receiver
653 * @param success whether it worked or not
656 transmit_send_continuation (void *cls,
657 const struct GNUNET_PeerIdentity *receiver,
660 struct MessageQueue *mq = cls;
661 struct NeighbourMapEntry *n;
662 struct NeighbourMapEntry *tmp;
664 tmp = lookup_neighbour (receiver);
666 if ((NULL != n) && (tmp != NULL) && (tmp == n))
668 GNUNET_assert (n->is_active == mq);
670 if (success == GNUNET_YES)
672 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
673 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
677 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
678 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
679 (success == GNUNET_OK) ? "successful" : "FAILED");
681 if (NULL != mq->cont)
682 mq->cont (mq->cont_cls, success);
688 * Check the ready list for the given neighbour and if a plugin is
689 * ready for transmission (and if we have a message), do so!
691 * @param n target peer for which to transmit
694 try_transmission_to_peer (struct NeighbourMapEntry *n)
696 struct MessageQueue *mq;
697 struct GNUNET_TIME_Relative timeout;
700 if (n->is_active != NULL)
703 return; /* transmission already pending */
705 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
708 return; /* currently waiting for bandwidth */
710 while (NULL != (mq = n->messages_head))
712 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
713 if (timeout.rel_value > 0)
715 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
718 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
721 return; /* no more messages */
723 if (n->address == NULL)
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
727 GNUNET_i2s (&n->id));
729 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
730 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
731 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
732 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
736 if (GST_plugins_find (n->address->transport_name) == NULL)
741 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
745 if ((n->address->address_length == 0) && (n->session == NULL))
747 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
748 GNUNET_i2s (&n->id));
749 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
750 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
751 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
756 send_with_plugin (&n->id, mq->message_buf, mq->message_buf_size, 0,
757 timeout, n->session, n->address, GNUNET_YES,
758 &transmit_send_continuation, mq);
761 /* failure, but 'send' would not call continuation in this case,
762 * so we need to do it here! */
763 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
770 * Task invoked to start a transmission to another peer.
772 * @param cls the 'struct NeighbourMapEntry'
773 * @param tc scheduler context
776 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
778 struct NeighbourMapEntry *n = cls;
780 GNUNET_assert (NULL != lookup_neighbour (&n->id));
781 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
782 try_transmission_to_peer (n);
787 * Initialize the neighbours subsystem.
789 * @param cls closure for callbacks
790 * @param connect_cb function to call if we connect to a peer
791 * @param disconnect_cb function to call if we disconnect from a peer
794 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
795 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
798 connect_notify_cb = connect_cb;
799 disconnect_notify_cb = disconnect_cb;
800 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
805 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
809 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
810 "Sending DISCONNECT message to peer `%4s': %i\n",
811 GNUNET_i2s (target), result);
817 send_disconnect (const struct GNUNET_PeerIdentity *target,
818 const struct GNUNET_HELLO_Address *address,
819 struct Session *session)
822 struct SessionDisconnectMessage disconnect_msg;
825 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
826 "Sending DISCONNECT message to peer `%4s'\n",
827 GNUNET_i2s (target));
830 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
831 disconnect_msg.header.type =
832 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
833 disconnect_msg.reserved = htonl (0);
834 disconnect_msg.purpose.size =
835 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
836 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
837 sizeof (struct GNUNET_TIME_AbsoluteNBO));
838 disconnect_msg.purpose.purpose =
839 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
840 disconnect_msg.timestamp =
841 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
842 disconnect_msg.public_key = GST_my_public_key;
843 GNUNET_assert (GNUNET_OK ==
844 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
845 &disconnect_msg.purpose,
846 &disconnect_msg.signature));
849 send_with_plugin (target, (const char *) &disconnect_msg,
850 sizeof (disconnect_msg), UINT32_MAX,
851 GNUNET_TIME_UNIT_FOREVER_REL, session, address,
852 GNUNET_YES, &send_disconnect_cont, NULL);
854 if (ret == GNUNET_SYSERR)
855 return GNUNET_SYSERR;
857 GNUNET_STATISTICS_update (GST_stats,
859 ("# peers disconnected due to external request"), 1,
866 * Disconnect from the given neighbour, clean up the record.
868 * @param n neighbour to disconnect from
871 disconnect_neighbour (struct NeighbourMapEntry *n)
873 struct MessageQueue *mq;
876 previous_state = n->state;
878 if (is_disconnecting (n))
881 /* send DISCONNECT MESSAGE */
882 if (previous_state == S_CONNECTED)
884 if (GNUNET_OK == send_disconnect (&n->id, n->address, n->session))
885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
886 GNUNET_i2s (&n->id));
888 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
889 "Could not send DISCONNECT_MSG to `%s'\n",
890 GNUNET_i2s (&n->id));
893 change_state (n, S_DISCONNECT);
895 if (previous_state == S_CONNECTED)
897 GNUNET_assert (NULL != n->address);
898 if (n->address_state == USED)
900 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
901 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
902 n->address_state = UNUSED;
906 if (n->address != NULL)
908 struct GNUNET_TRANSPORT_PluginFunctions *papi;
910 papi = GST_plugins_find (n->address->transport_name);
912 papi->disconnect (papi->cls, &n->id);
914 while (NULL != (mq = n->messages_head))
916 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
917 if (NULL != mq->cont)
918 mq->cont (mq->cont_cls, GNUNET_SYSERR);
921 if (NULL != n->is_active)
923 n->is_active->n = NULL;
927 switch (previous_state)
930 GNUNET_assert (neighbours_connected > 0);
931 neighbours_connected--;
932 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
933 GNUNET_SCHEDULER_cancel (n->keepalive_task);
934 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
935 n->expect_latency_response = GNUNET_NO;
936 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
938 disconnect_notify_cb (callback_cls, &n->id);
940 case S_FAST_RECONNECT:
941 GNUNET_STATISTICS_update (GST_stats,
942 gettext_noop ("# fast reconnects failed"), 1,
944 disconnect_notify_cb (callback_cls, &n->id);
950 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
952 GNUNET_assert (GNUNET_YES ==
953 GNUNET_CONTAINER_multihashmap_remove (neighbours,
954 &n->id.hashPubKey, n));
955 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
957 GNUNET_SCHEDULER_cancel (n->ats_suggest);
958 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
960 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
962 GNUNET_SCHEDULER_cancel (n->timeout_task);
963 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
965 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
967 GNUNET_SCHEDULER_cancel (n->transmission_task);
968 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
970 if (NULL != n->address)
972 GNUNET_HELLO_address_free (n->address);
976 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
977 GNUNET_i2s (&n->id), n);
983 * Peer has been idle for too long. Disconnect.
985 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
986 * @param tc scheduler context
989 neighbour_timeout_task (void *cls,
990 const struct GNUNET_SCHEDULER_TaskContext *tc)
992 struct NeighbourMapEntry *n = cls;
994 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
996 GNUNET_STATISTICS_update (GST_stats,
998 ("# peers disconnected due to timeout"), 1,
1000 disconnect_neighbour (n);
1005 * Send another keepalive message.
1007 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1008 * @param tc scheduler context
1011 neighbour_keepalive_task (void *cls,
1012 const struct GNUNET_SCHEDULER_TaskContext *tc)
1014 struct NeighbourMapEntry *n = cls;
1015 struct GNUNET_MessageHeader m;
1018 GNUNET_assert (S_CONNECTED == n->state);
1020 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1021 &neighbour_keepalive_task, n);
1023 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1025 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1026 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1030 send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1031 UINT32_MAX /* priority */ ,
1032 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
1033 GNUNET_YES, NULL, NULL);
1035 n->expect_latency_response = GNUNET_NO;
1036 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero ();
1037 if (ret != GNUNET_SYSERR)
1039 n->expect_latency_response = GNUNET_YES;
1040 n->keep_alive_sent = GNUNET_TIME_absolute_get ();
1047 * Disconnect from the given neighbour.
1050 * @param key hash of neighbour's public key (not used)
1051 * @param value the 'struct NeighbourMapEntry' of the neighbour
1054 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1056 struct NeighbourMapEntry *n = value;
1059 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1060 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1062 if (S_CONNECTED == n->state)
1063 GNUNET_STATISTICS_update (GST_stats,
1065 ("# peers disconnected due to global disconnect"),
1067 disconnect_neighbour (n);
1073 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1075 struct NeighbourMapEntry *n = cls;
1077 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1079 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1080 "ATS did not suggested address to connect to peer `%s'\n",
1081 GNUNET_i2s (&n->id));
1083 disconnect_neighbour (n);
1087 * Cleanup the neighbours subsystem.
1090 GST_neighbours_stop ()
1092 // This can happen during shutdown
1093 if (neighbours == NULL)
1098 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1100 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1101 // GNUNET_assert (neighbours_connected == 0);
1103 callback_cls = NULL;
1104 connect_notify_cb = NULL;
1105 disconnect_notify_cb = NULL;
1108 struct ContinutionContext
1110 struct GNUNET_HELLO_Address *address;
1112 struct Session *session;
1116 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
1117 struct GNUNET_BANDWIDTH_Value32NBO quota)
1119 struct QuotaSetMessage q_msg;
1122 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1123 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1124 ntohl (quota.value__), GNUNET_i2s (target));
1126 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1127 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1128 q_msg.quota = quota;
1129 q_msg.peer = (*target);
1130 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1134 * We tried to send a SESSION_CONNECT message to another peer. If this
1135 * succeeded, we change the state. If it failed, we should tell
1136 * ATS to not use this address anymore (until it is re-validated).
1138 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1139 * @param target peer to send the message to
1140 * @param success GNUNET_OK on success
1143 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1146 struct ContinutionContext *cc = cls;
1147 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1149 if (GNUNET_YES != success)
1151 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1152 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1154 if ((NULL == neighbours) || (NULL == n) || (n->state == S_DISCONNECT))
1156 GNUNET_HELLO_address_free (cc->address);
1161 if ((GNUNET_YES == success) &&
1162 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1164 change_state (n, S_CONNECT_SENT);
1165 GNUNET_HELLO_address_free (cc->address);
1170 if ((GNUNET_NO == success) &&
1171 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1174 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1175 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1176 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1178 change_state (n, S_NOT_CONNECTED);
1179 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1180 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1182 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1184 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1186 GNUNET_HELLO_address_free (cc->address);
1192 * We tried to switch addresses with an peer already connected. If it failed,
1193 * we should tell ATS to not use this address anymore (until it is re-validated).
1195 * @param cls the 'struct NeighbourMapEntry'
1196 * @param target peer to send the message to
1197 * @param success GNUNET_OK on success
1200 send_switch_address_continuation (void *cls,
1201 const struct GNUNET_PeerIdentity *target,
1204 struct ContinutionContext *cc = cls;
1205 struct NeighbourMapEntry *n;
1207 if (neighbours == NULL)
1209 GNUNET_HELLO_address_free (cc->address);
1211 return; /* neighbour is going away */
1214 n = lookup_neighbour (&cc->address->peer);
1215 if ((n == NULL) || (is_disconnecting (n)))
1217 GNUNET_HELLO_address_free (cc->address);
1219 return; /* neighbour is going away */
1222 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1223 if (GNUNET_YES != success)
1226 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1227 "Failed to switch connected peer `%s' to address '%s' session %X, asking ATS for new address \n",
1228 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1230 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1231 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1233 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1234 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1236 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1238 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1239 GNUNET_HELLO_address_free (cc->address);
1243 /* Tell ATS that switching addresses was successful */
1247 if (n->address_state == FRESH)
1249 n->address_state = USED;
1250 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES);
1251 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1252 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1255 case S_FAST_RECONNECT:
1257 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1258 "Successful fast reconnect to peer `%s'\n",
1259 GNUNET_i2s (&n->id));
1261 change_state (n, S_CONNECTED);
1262 neighbours_connected++;
1263 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1266 if (n->address_state == FRESH)
1268 n->address_state = USED;
1269 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES);
1270 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1271 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1274 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1276 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1278 /* Updating quotas */
1279 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1280 send_outbound_quota (target, n->bandwidth_out);
1285 GNUNET_HELLO_address_free (cc->address);
1291 * We tried to send a SESSION_CONNECT message to another peer. If this
1292 * succeeded, we change the state. If it failed, we should tell
1293 * ATS to not use this address anymore (until it is re-validated).
1295 * @param cls the 'struct NeighbourMapEntry'
1296 * @param target peer to send the message to
1297 * @param success GNUNET_OK on success
1300 send_connect_ack_continuation (void *cls,
1301 const struct GNUNET_PeerIdentity *target,
1304 struct ContinutionContext *cc = cls;
1305 struct NeighbourMapEntry *n;
1307 if (neighbours == NULL)
1309 GNUNET_HELLO_address_free (cc->address);
1311 return; /* neighbour is going away */
1314 n = lookup_neighbour (&cc->address->peer);
1315 if ((n == NULL) || (is_disconnecting (n)))
1317 GNUNET_HELLO_address_free (cc->address);
1319 return; /* neighbour is going away */
1322 if (GNUNET_YES == success)
1324 GNUNET_HELLO_address_free (cc->address);
1326 return; /* sending successful */
1329 /* sending failed, ask for next address */
1331 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1332 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1333 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1335 change_state (n, S_NOT_CONNECTED);
1336 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1337 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1339 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1340 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1342 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1344 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1345 GNUNET_HELLO_address_free (cc->address);
1351 * For an existing neighbour record, set the active connection to
1352 * the given address.
1354 * @param peer identity of the peer to switch the address for
1355 * @param address address of the other peer, NULL if other peer
1357 * @param session session to use (or NULL)
1358 * @param ats performance data
1359 * @param ats_count number of entries in ats
1360 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1361 * connection is not up (yet)
1364 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1365 const struct GNUNET_HELLO_Address
1366 *address, struct Session *session,
1367 const struct GNUNET_ATS_Information *ats,
1369 struct GNUNET_BANDWIDTH_Value32NBO
1371 struct GNUNET_BANDWIDTH_Value32NBO
1374 struct NeighbourMapEntry *n;
1375 struct SessionConnectMessage connect_msg;
1376 struct ContinutionContext *cc;
1380 if (neighbours == NULL)
1382 /* This can happen during shutdown */
1385 n = lookup_neighbour (peer);
1388 if (n->state == S_DISCONNECT)
1390 /* We are disconnecting, nothing to do here */
1393 GNUNET_assert (address->transport_name != NULL);
1394 if ((session == NULL) && (0 == address->address_length))
1396 GNUNET_break_op (0);
1397 /* FIXME: is this actually possible? When does this happen? */
1398 if (strlen (address->transport_name) > 0)
1399 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1400 GNUNET_ATS_suggest_address (GST_ats, peer);
1404 /* checks successful and neighbour != NULL */
1406 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1407 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1408 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
1409 print_state (n->state));
1411 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1413 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1414 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1416 /* do not switch addresses just update quotas */
1417 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1418 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1419 (n->session == session))
1421 n->bandwidth_in = bandwidth_in;
1422 n->bandwidth_out = bandwidth_out;
1423 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1424 send_outbound_quota (peer, n->bandwidth_out);
1427 if (n->state == S_CONNECTED)
1429 /* mark old address as no longer used */
1430 GNUNET_assert (NULL != n->address);
1431 if (n->address_state == USED)
1433 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
1434 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1435 n->address_state = UNUSED;
1440 /* set new address */
1441 if (NULL != n->address)
1442 GNUNET_HELLO_address_free (n->address);
1443 n->address = GNUNET_HELLO_address_copy (address);
1444 n->address_state = FRESH;
1445 n->session = session;
1446 n->bandwidth_in = bandwidth_in;
1447 n->bandwidth_out = bandwidth_out;
1448 GNUNET_SCHEDULER_cancel (n->timeout_task);
1450 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1451 &neighbour_timeout_task, n);
1454 case S_NOT_CONNECTED:
1455 case S_CONNECT_SENT:
1456 msg_len = sizeof (struct SessionConnectMessage);
1457 connect_msg.header.size = htons (msg_len);
1458 connect_msg.header.type =
1459 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1460 connect_msg.reserved = htonl (0);
1461 connect_msg.timestamp =
1462 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1464 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1465 cc->session = session;
1466 cc->address = GNUNET_HELLO_address_copy (address);
1468 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1469 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1470 address, GNUNET_YES, &send_connect_continuation, cc);
1472 case S_CONNECT_RECV:
1473 /* We received a CONNECT message and asked ATS for an address */
1474 msg_len = sizeof (struct SessionConnectMessage);
1475 connect_msg.header.size = htons (msg_len);
1476 connect_msg.header.type =
1477 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1478 connect_msg.reserved = htonl (0);
1479 connect_msg.timestamp =
1480 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1481 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1482 cc->session = session;
1483 cc->address = GNUNET_HELLO_address_copy (address);
1485 send_with_plugin (&n->id, (const void *) &connect_msg, msg_len,
1486 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1487 address, GNUNET_YES, &send_connect_ack_continuation,
1491 case S_FAST_RECONNECT:
1492 /* connected peer is switching addresses or tries fast reconnect */
1493 msg_len = sizeof (struct SessionConnectMessage);
1494 connect_msg.header.size = htons (msg_len);
1495 connect_msg.header.type =
1496 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1497 connect_msg.reserved = htonl (0);
1498 connect_msg.timestamp =
1499 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1500 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1501 cc->session = session;
1502 cc->address = GNUNET_HELLO_address_copy (address);
1504 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1505 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1506 address, GNUNET_YES,
1507 &send_switch_address_continuation, cc);
1508 if (ret == GNUNET_SYSERR)
1510 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1511 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1512 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1517 "Invalid connection state to switch addresses %u \n", n->state);
1518 GNUNET_break_op (0);
1525 * Obtain current latency information for the given neighbour.
1528 * @return observed latency of the address, FOREVER if the address was
1529 * never successfully validated
1531 struct GNUNET_TIME_Relative
1532 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1534 struct NeighbourMapEntry *n;
1536 n = lookup_neighbour (peer);
1537 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1538 return GNUNET_TIME_UNIT_FOREVER_REL;
1544 * Obtain current address information for the given neighbour.
1547 * @return address currently used
1549 struct GNUNET_HELLO_Address *
1550 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1552 struct NeighbourMapEntry *n;
1554 n = lookup_neighbour (peer);
1555 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1564 * Create an entry in the neighbour map for the given peer
1566 * @param peer peer to create an entry for
1567 * @return new neighbour map entry
1569 static struct NeighbourMapEntry *
1570 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1572 struct NeighbourMapEntry *n;
1575 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1576 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1578 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1580 n->state = S_NOT_CONNECTED;
1581 n->latency = GNUNET_TIME_relative_get_forever ();
1582 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1583 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1584 MAX_BANDWIDTH_CARRY_S);
1586 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1587 &neighbour_timeout_task, n);
1588 GNUNET_assert (GNUNET_OK ==
1589 GNUNET_CONTAINER_multihashmap_put (neighbours,
1590 &n->id.hashPubKey, n,
1591 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1597 * Try to create a connection to the given target (eventually).
1599 * @param target peer to try to connect to
1602 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1604 struct NeighbourMapEntry *n;
1606 // This can happen during shutdown
1607 if (neighbours == NULL)
1612 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1613 GNUNET_i2s (target));
1616 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1621 n = lookup_neighbour (target);
1625 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1626 return; /* already connecting or connected */
1627 if (is_disconnecting (n))
1628 change_state (n, S_NOT_CONNECTED);
1633 n = setup_neighbour (target);
1635 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1636 "Asking ATS for suggested address to connect to peer `%s'\n",
1637 GNUNET_i2s (&n->id));
1640 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1644 * Test if we're connected to the given peer.
1646 * @param target peer to test
1647 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1650 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1652 struct NeighbourMapEntry *n;
1654 // This can happen during shutdown
1655 if (neighbours == NULL)
1660 n = lookup_neighbour (target);
1662 if ((NULL == n) || (S_CONNECTED != n->state))
1663 return GNUNET_NO; /* not connected */
1668 * A session was terminated. Take note.
1670 * @param peer identity of the peer where the session died
1671 * @param session session that is gone
1674 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1675 struct Session *session)
1677 struct NeighbourMapEntry *n;
1679 if (neighbours == NULL)
1681 /* This can happen during shutdown */
1686 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1687 session, GNUNET_i2s (peer));
1690 n = lookup_neighbour (peer);
1693 if (session != n->session)
1694 return; /* doesn't affect us */
1695 if (n->state == S_CONNECTED)
1697 if (n->address_state == USED)
1699 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
1700 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1701 n->address_state = UNUSED;
1705 if (NULL != n->address)
1707 GNUNET_HELLO_address_free (n->address);
1712 /* not connected anymore anyway, shouldn't matter */
1713 if (S_CONNECTED != n->state)
1716 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1718 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1719 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1720 n->expect_latency_response = GNUNET_NO;
1723 /* connected, try fast reconnect */
1724 /* statistics "transport" : "# peers connected" -= 1
1725 * neighbours_connected -= 1
1726 * BUT: no disconnect_cb to notify clients about disconnect
1729 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1732 GNUNET_assert (neighbours_connected > 0);
1733 change_state (n, S_FAST_RECONNECT);
1734 neighbours_connected--;
1735 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
1739 /* We are connected, so ask ATS to switch addresses */
1740 GNUNET_SCHEDULER_cancel (n->timeout_task);
1741 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1742 &neighbour_timeout_task, n);
1743 /* try QUICKLY to re-establish a connection, reduce timeout! */
1744 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1745 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1746 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1748 GNUNET_ATS_suggest_address (GST_ats, peer);
1753 * Transmit a message to the given target using the active connection.
1755 * @param target destination
1756 * @param msg message to send
1757 * @param msg_size number of bytes in msg
1758 * @param timeout when to fail with timeout
1759 * @param cont function to call when done
1760 * @param cont_cls closure for 'cont'
1763 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1764 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1765 GST_NeighbourSendContinuation cont, void *cont_cls)
1767 struct NeighbourMapEntry *n;
1768 struct MessageQueue *mq;
1770 // This can happen during shutdown
1771 if (neighbours == NULL)
1776 n = lookup_neighbour (target);
1777 if ((n == NULL) || (!is_connected (n)))
1779 GNUNET_STATISTICS_update (GST_stats,
1781 ("# messages not sent (no such peer or not connected)"),
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "Could not send message to peer `%s': unknown neighbour",
1787 GNUNET_i2s (target));
1788 else if (!is_connected (n))
1789 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1790 "Could not send message to peer `%s': not connected\n",
1791 GNUNET_i2s (target));
1794 cont (cont_cls, GNUNET_SYSERR);
1798 if ((n->session == NULL) && (n->address == NULL))
1800 GNUNET_STATISTICS_update (GST_stats,
1802 ("# messages not sent (no such peer or not connected)"),
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1806 "Could not send message to peer `%s': no address available\n",
1807 GNUNET_i2s (target));
1811 cont (cont_cls, GNUNET_SYSERR);
1815 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1816 GNUNET_STATISTICS_update (GST_stats,
1818 ("# bytes in message queue for other peers"),
1819 msg_size, GNUNET_NO);
1820 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1822 mq->cont_cls = cont_cls;
1823 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1824 memcpy (&mq[1], msg, msg_size);
1825 mq->message_buf = (const char *) &mq[1];
1826 mq->message_buf_size = msg_size;
1827 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1828 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1830 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1831 (NULL == n->is_active))
1832 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1837 * We have received a message from the given sender. How long should
1838 * we delay before receiving more? (Also used to keep the peer marked
1841 * @param sender sender of the message
1842 * @param size size of the message
1843 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1844 * GNUNET_NO if the neighbour is not connected or violates the quota,
1845 * GNUNET_SYSERR if the connection is not fully up yet
1846 * @return how long to wait before reading more from this sender
1848 struct GNUNET_TIME_Relative
1849 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1850 *sender, ssize_t size, int *do_forward)
1852 struct NeighbourMapEntry *n;
1853 struct GNUNET_TIME_Relative ret;
1855 // This can happen during shutdown
1856 if (neighbours == NULL)
1858 return GNUNET_TIME_UNIT_FOREVER_REL;
1861 n = lookup_neighbour (sender);
1864 GST_neighbours_try_connect (sender);
1865 n = lookup_neighbour (sender);
1868 GNUNET_STATISTICS_update (GST_stats,
1870 ("# messages discarded due to lack of neighbour record"),
1872 *do_forward = GNUNET_NO;
1873 return GNUNET_TIME_UNIT_ZERO;
1876 if (!is_connected (n))
1878 *do_forward = GNUNET_SYSERR;
1879 return GNUNET_TIME_UNIT_ZERO;
1881 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1883 n->quota_violation_count++;
1885 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1886 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1887 n->in_tracker.available_bytes_per_s__,
1888 n->quota_violation_count);
1890 /* Discount 32k per violation */
1891 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1895 if (n->quota_violation_count > 0)
1897 /* try to add 32k back */
1898 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1899 n->quota_violation_count--;
1902 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1904 GNUNET_STATISTICS_update (GST_stats,
1906 ("# bandwidth quota violations by other peers"),
1908 *do_forward = GNUNET_NO;
1909 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1911 *do_forward = GNUNET_YES;
1912 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1913 if (ret.rel_value > 0)
1916 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1917 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1918 (unsigned long long) n->in_tracker.
1919 consumption_since_last_update__,
1920 (unsigned int) n->in_tracker.available_bytes_per_s__,
1921 (unsigned long long) ret.rel_value);
1923 GNUNET_STATISTICS_update (GST_stats,
1924 gettext_noop ("# ms throttling suggested"),
1925 (int64_t) ret.rel_value, GNUNET_NO);
1932 * Keep the connection to the given neighbour alive longer,
1933 * we received a KEEPALIVE (or equivalent).
1935 * @param neighbour neighbour to keep alive
1938 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1940 struct NeighbourMapEntry *n;
1942 // This can happen during shutdown
1943 if (neighbours == NULL)
1948 n = lookup_neighbour (neighbour);
1951 GNUNET_STATISTICS_update (GST_stats,
1953 ("# KEEPALIVE messages discarded (not connected)"),
1957 GNUNET_SCHEDULER_cancel (n->timeout_task);
1959 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1960 &neighbour_timeout_task, n);
1962 /* send reply to measure latency */
1963 if (S_CONNECTED != n->state)
1966 struct GNUNET_MessageHeader m;
1968 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1969 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1971 send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1972 UINT32_MAX /* priority */ ,
1973 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
1974 GNUNET_YES, NULL, NULL);
1978 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
1981 * @param neighbour neighbour to keep alive
1982 * @param ats performance data
1983 * @param ats_count number of entries in ats
1986 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1987 const struct GNUNET_ATS_Information *ats,
1990 struct NeighbourMapEntry *n;
1991 struct GNUNET_ATS_Information *ats_new;
1994 if (neighbours == NULL)
1996 // This can happen during shutdown
2000 n = lookup_neighbour (neighbour);
2001 if ((NULL == n) || (n->state != S_CONNECTED))
2003 GNUNET_STATISTICS_update (GST_stats,
2005 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2009 if (n->expect_latency_response != GNUNET_YES)
2011 GNUNET_STATISTICS_update (GST_stats,
2013 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2017 n->expect_latency_response = GNUNET_NO;
2019 GNUNET_assert (n->keep_alive_sent.abs_value !=
2020 GNUNET_TIME_absolute_get_zero ().abs_value);
2022 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2023 GNUNET_TIME_absolute_get ());
2025 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2026 GNUNET_i2s (&n->id), n->latency.rel_value);
2030 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2032 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2037 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2039 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2042 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2043 if (n->latency.rel_value > UINT32_MAX)
2044 latency = UINT32_MAX;
2046 latency = n->latency.rel_value;
2047 ats_new[ats_count].value = htonl (latency);
2049 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2051 GNUNET_free (ats_new);
2057 * Change the incoming quota for the given peer.
2059 * @param neighbour identity of peer to change qutoa for
2060 * @param quota new quota
2063 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2064 struct GNUNET_BANDWIDTH_Value32NBO quota)
2066 struct NeighbourMapEntry *n;
2068 // This can happen during shutdown
2069 if (neighbours == NULL)
2074 n = lookup_neighbour (neighbour);
2077 GNUNET_STATISTICS_update (GST_stats,
2079 ("# SET QUOTA messages ignored (no such peer)"),
2084 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2085 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2086 ntohl (quota.value__), GNUNET_i2s (&n->id));
2088 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2089 if (0 != ntohl (quota.value__))
2092 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2093 GNUNET_i2s (&n->id), "SET_QUOTA");
2095 if (is_connected (n))
2096 GNUNET_STATISTICS_update (GST_stats,
2097 gettext_noop ("# disconnects due to quota of 0"),
2099 disconnect_neighbour (n);
2104 * Closure for the neighbours_iterate function.
2106 struct IteratorContext
2109 * Function to call on each connected neighbour.
2111 GST_NeighbourIterator cb;
2121 * Call the callback from the closure for each connected neighbour.
2123 * @param cls the 'struct IteratorContext'
2124 * @param key the hash of the public key of the neighbour
2125 * @param value the 'struct NeighbourMapEntry'
2126 * @return GNUNET_OK (continue to iterate)
2129 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2131 struct IteratorContext *ic = cls;
2132 struct NeighbourMapEntry *n = value;
2134 if (!is_connected (n))
2137 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2143 * Iterate over all connected neighbours.
2145 * @param cb function to call
2146 * @param cb_cls closure for cb
2149 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2151 struct IteratorContext ic;
2153 // This can happen during shutdown
2154 if (neighbours == NULL)
2161 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2165 * If we have an active connection to the given target, it must be shutdown.
2167 * @param target peer to disconnect from
2170 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2172 struct NeighbourMapEntry *n;
2174 // This can happen during shutdown
2175 if (neighbours == NULL)
2180 n = lookup_neighbour (target);
2182 return; /* not active */
2183 disconnect_neighbour (n);
2188 * We received a disconnect message from the given peer,
2189 * validate and process.
2191 * @param peer sender of the message
2192 * @param msg the disconnect message
2195 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2197 const struct GNUNET_MessageHeader
2200 struct NeighbourMapEntry *n;
2201 const struct SessionDisconnectMessage *sdm;
2205 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2206 "Received DISCONNECT message from peer `%s'\n",
2210 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2212 // GNUNET_break_op (0);
2213 GNUNET_STATISTICS_update (GST_stats,
2215 ("# disconnect messages ignored (old format)"), 1,
2219 sdm = (const struct SessionDisconnectMessage *) msg;
2220 n = lookup_neighbour (peer);
2222 return; /* gone already */
2223 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2224 n->connect_ts.abs_value)
2226 GNUNET_STATISTICS_update (GST_stats,
2228 ("# disconnect messages ignored (timestamp)"), 1,
2232 GNUNET_CRYPTO_hash (&sdm->public_key,
2233 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2235 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2237 GNUNET_break_op (0);
2240 if (ntohl (sdm->purpose.size) !=
2241 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2242 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2243 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2245 GNUNET_break_op (0);
2249 GNUNET_CRYPTO_rsa_verify
2250 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2251 &sdm->signature, &sdm->public_key))
2253 GNUNET_break_op (0);
2256 GST_neighbours_force_disconnect (peer);
2261 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2262 * Consider switching to it.
2264 * @param message possibly a 'struct SessionConnectMessage' (check format)
2265 * @param peer identity of the peer to switch the address for
2266 * @param address address of the other peer, NULL if other peer
2268 * @param session session to use (or NULL)
2269 * @param ats performance data
2270 * @param ats_count number of entries in ats
2273 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2274 const struct GNUNET_PeerIdentity *peer,
2275 const struct GNUNET_HELLO_Address *address,
2276 struct Session *session,
2277 const struct GNUNET_ATS_Information *ats,
2280 const struct SessionConnectMessage *scm;
2281 struct GNUNET_MessageHeader msg;
2282 struct NeighbourMapEntry *n;
2287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2288 "Received CONNECT_ACK message from peer `%s'\n",
2292 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2294 GNUNET_break_op (0);
2297 scm = (const struct SessionConnectMessage *) message;
2298 GNUNET_break_op (ntohl (scm->reserved) == 0);
2299 n = lookup_neighbour (peer);
2302 /* we did not send 'CONNECT' -- at least not recently */
2303 GNUNET_STATISTICS_update (GST_stats,
2305 ("# unexpected CONNECT_ACK messages (no peer)"),
2312 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2314 * We also received an CONNECT message, switched from SENDT to RECV and
2315 * ATS already suggested us an address after a successful blacklist check
2317 if ((n->state != S_CONNECT_SENT) &&
2318 ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2320 GNUNET_STATISTICS_update (GST_stats,
2322 ("# unexpected CONNECT_ACK messages"), 1,
2327 change_state (n, S_CONNECTED);
2329 if (NULL != session)
2331 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2333 "Giving ATS session %p of plugin %s for peer %s\n",
2334 session, address->transport_name, GNUNET_i2s (peer));
2336 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2337 GNUNET_assert (NULL != n->address);
2338 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2340 n->address_state = USED;
2341 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2342 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2343 if (n->session == NULL)
2344 n->session = session;
2347 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2349 /* send ACK (ACK) */
2350 msg_len = sizeof (msg);
2351 msg.size = htons (msg_len);
2352 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2355 send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2356 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
2357 GNUNET_YES, NULL, NULL);
2359 if (ret == GNUNET_SYSERR)
2360 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2361 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2362 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2365 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2366 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2368 neighbours_connected++;
2369 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2372 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2373 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2374 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2377 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2378 send_outbound_quota (peer, n->bandwidth_out);
2384 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2385 const struct GNUNET_PeerIdentity *peer,
2386 const struct GNUNET_HELLO_Address *address,
2387 struct Session *session,
2388 const struct GNUNET_ATS_Information *ats,
2391 struct NeighbourMapEntry *n;
2394 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2398 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2400 GNUNET_break_op (0);
2403 n = lookup_neighbour (peer);
2406 send_disconnect (peer, address, session);
2410 if (S_CONNECTED == n->state)
2412 if (!is_connecting (n))
2414 GNUNET_STATISTICS_update (GST_stats,
2415 gettext_noop ("# unexpected ACK messages"), 1,
2419 change_state (n, S_CONNECTED);
2420 if (NULL != session)
2421 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2423 "Giving ATS session %p of plugin %s for peer %s\n",
2424 session, address->transport_name, GNUNET_i2s (peer));
2425 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2426 GNUNET_assert (n->address != NULL);
2427 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2429 n->address_state = USED;
2430 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2431 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2432 if (n->session == NULL)
2433 n->session = session;
2436 neighbours_connected++;
2437 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2440 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2441 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2442 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2444 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2445 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2446 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2449 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2450 send_outbound_quota (peer, n->bandwidth_out);
2453 struct BlackListCheckContext
2455 struct GNUNET_ATS_Information *ats;
2459 struct Session *session;
2461 struct GNUNET_HELLO_Address *address;
2463 struct GNUNET_TIME_Absolute ts;
2468 handle_connect_blacklist_cont (void *cls,
2469 const struct GNUNET_PeerIdentity *peer,
2472 struct NeighbourMapEntry *n;
2473 struct BlackListCheckContext *bcc = cls;
2476 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2477 "Blacklist check due to CONNECT message: `%s'\n",
2479 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2483 if (GNUNET_OK != result)
2485 GNUNET_HELLO_address_free (bcc->address);
2490 n = lookup_neighbour (peer);
2492 n = setup_neighbour (peer);
2494 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2496 if (NULL != bcc->session)
2497 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2499 "Giving ATS session %p of address `%s' for peer %s\n",
2500 bcc->session, GST_plugins_a2s (bcc->address),
2502 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2504 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2506 n->connect_ts = bcc->ts;
2509 GNUNET_HELLO_address_free (bcc->address);
2512 if (n->state != S_CONNECT_RECV)
2513 change_state (n, S_CONNECT_RECV);
2516 /* Ask ATS for an address to connect via that address */
2517 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2518 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2520 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2522 GNUNET_ATS_suggest_address (GST_ats, peer);
2526 * We received a 'SESSION_CONNECT' message from the other peer.
2527 * Consider switching to it.
2529 * @param message possibly a 'struct SessionConnectMessage' (check format)
2530 * @param peer identity of the peer to switch the address for
2531 * @param address address of the other peer, NULL if other peer
2533 * @param session session to use (or NULL)
2534 * @param ats performance data
2535 * @param ats_count number of entries in ats (excluding 0-termination)
2538 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2539 const struct GNUNET_PeerIdentity *peer,
2540 const struct GNUNET_HELLO_Address *address,
2541 struct Session *session,
2542 const struct GNUNET_ATS_Information *ats,
2545 const struct SessionConnectMessage *scm;
2546 struct BlackListCheckContext *bcc = NULL;
2547 struct NeighbourMapEntry *n;
2550 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2551 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2554 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2556 GNUNET_break_op (0);
2560 scm = (const struct SessionConnectMessage *) message;
2561 GNUNET_break_op (ntohl (scm->reserved) == 0);
2563 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2565 n = lookup_neighbour (peer);
2566 if ((n != NULL) && (S_CONNECTED == n->state))
2568 /* connected peer switches addresses */
2573 /* we are not connected to this peer */
2574 /* do blacklist check */
2576 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2577 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2578 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2579 bcc->ats_count = ats_count + 1;
2580 bcc->address = GNUNET_HELLO_address_copy (address);
2581 bcc->session = session;
2582 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2583 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2584 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2585 bcc->ats[ats_count].value =
2586 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2587 GST_blacklist_test_allowed (peer, address->transport_name,
2588 handle_connect_blacklist_cont, bcc);
2592 /* end of file gnunet-service-transport_neighbours.c */