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;
73 * Message a peer sends to another to indicate its
74 * preference for communicating via a particular
75 * session (and the desire to establish a real
78 struct SessionConnectMessage
81 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
83 struct GNUNET_MessageHeader header;
88 uint32_t reserved GNUNET_PACKED;
91 * Absolute time at the sender. Only the most recent connect
92 * message implies which session is preferred by the sender.
94 struct GNUNET_TIME_AbsoluteNBO timestamp;
99 struct SessionDisconnectMessage
102 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
104 struct GNUNET_MessageHeader header;
109 uint32_t reserved GNUNET_PACKED;
112 * Purpose of the signature. Extends over the timestamp.
113 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
115 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
118 * Absolute time at the sender. Only the most recent connect
119 * message implies which session is preferred by the sender.
121 struct GNUNET_TIME_AbsoluteNBO timestamp;
124 * Public key of the sender.
126 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
129 * Signature of the peer that sends us the disconnect. Only
130 * valid if the timestamp is AFTER the timestamp from the
131 * corresponding 'CONNECT' message.
133 struct GNUNET_CRYPTO_RsaSignature signature;
139 * For each neighbour we keep a list of messages
140 * that we still want to transmit to the neighbour.
146 * This is a doubly linked list.
148 struct MessageQueue *next;
151 * This is a doubly linked list.
153 struct MessageQueue *prev;
156 * Once this message is actively being transmitted, which
157 * neighbour is it associated with?
159 struct NeighbourMapEntry *n;
162 * Function to call once we're done.
164 GST_NeighbourSendContinuation cont;
172 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
173 * stuck together in memory. Allocated at the end of this struct.
175 const char *message_buf;
178 * Size of the message buf
180 size_t message_buf_size;
183 * At what time should we fail?
185 struct GNUNET_TIME_Absolute timeout;
193 * fresh peer or completely disconnected
198 * sent CONNECT message to other peer, waiting for CONNECT_ACK
203 * received CONNECT message to other peer, sending CONNECT_ACK
208 * received ACK or payload
213 * connection ended, fast reconnect
218 * Disconnect in progress
231 * Entry in neighbours.
233 struct NeighbourMapEntry
237 * Head of list of messages we would like to send to this peer;
238 * must contain at most one message per client.
240 struct MessageQueue *messages_head;
243 * Tail of list of messages we would like to send to this peer; must
244 * contain at most one message per client.
246 struct MessageQueue *messages_tail;
249 * Performance data for the peer.
251 //struct GNUNET_ATS_Information *ats;
254 * Are we currently trying to send a message? If so, which one?
256 struct MessageQueue *is_active;
259 * Active session for communicating with the peer.
261 struct Session *session;
264 * Address we currently use.
266 struct GNUNET_HELLO_Address *address;
269 * Identity of this neighbour.
271 struct GNUNET_PeerIdentity id;
274 * ID of task scheduled to run when this peer is about to
275 * time out (will free resources associated with the peer).
277 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
280 * ID of task scheduled to send keepalives.
282 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
285 * ID of task scheduled to run when we should try transmitting
286 * the head of the message queue.
288 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
291 * Tracker for inbound bandwidth.
293 struct GNUNET_BANDWIDTH_Tracker in_tracker;
296 * Inbound bandwidth from ATS, activated when connection is up
298 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
301 * Inbound bandwidth from ATS, activated when connection is up
303 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
306 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
308 struct GNUNET_TIME_Absolute connect_ts;
311 * When did we sent the last keep-alive message?
313 struct GNUNET_TIME_Absolute keep_alive_sent;
316 * Latest calculated latency value
318 struct GNUNET_TIME_Relative latency;
322 * We asked ATS for a new address for this peer
324 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
327 * Task the resets the peer state after due to an pending
328 * unsuccessful connection setup
330 GNUNET_SCHEDULER_TaskIdentifier state_reset;
333 * How often has the other peer (recently) violated the inbound
334 * traffic limit? Incremented by 10 per violation, decremented by 1
335 * per non-violation (for each time interval).
337 unsigned int quota_violation_count;
341 * The current state of the peer
342 * Element of enum State
347 * Did we sent an KEEP_ALIVE message and are we expecting a response?
349 int expect_latency_response;
355 * All known neighbours and their HELLOs.
357 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
360 * Closure for connect_notify_cb and disconnect_notify_cb
362 static void *callback_cls;
365 * Function to call when we connected to a neighbour.
367 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
370 * Function to call when we disconnected from a neighbour.
372 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
375 * counter for connected neighbours
377 static int neighbours_connected;
380 * Lookup a neighbour entry in the neighbours hash map.
382 * @param pid identity of the peer to look up
383 * @return the entry, NULL if there is no existing record
385 static struct NeighbourMapEntry *
386 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
388 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
391 #define change_state(n, state, ...) change (n, state, __LINE__)
394 is_connecting (struct NeighbourMapEntry *n)
396 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
402 is_connected (struct NeighbourMapEntry *n)
404 if (n->state == S_CONNECTED)
410 is_disconnecting (struct NeighbourMapEntry *n)
412 if (n->state == S_DISCONNECT)
418 print_state (int state)
423 return "S_CONNECTED";
426 return "S_CONNECT_RECV";
429 return "S_CONNECT_SENT";
432 return "S_DISCONNECT";
434 case S_NOT_CONNECTED:
435 return "S_NOT_CONNECTED";
437 case S_FAST_RECONNECT:
438 return "S_FAST_RECONNECT";
448 change (struct NeighbourMapEntry *n, int state, int line);
451 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;
458 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
463 GNUNET_STATISTICS_update (GST_stats,
465 ("# failed connection attempts due to timeout"), 1,
468 /* resetting state */
469 n->state = S_NOT_CONNECTED;
471 /* destroying address */
472 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
474 /* request new address */
475 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
476 GNUNET_SCHEDULER_cancel (n->ats_suggest);
478 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
480 GNUNET_ATS_suggest_address (GST_ats, &n->id);
484 change (struct NeighbourMapEntry *n, int state, int line)
486 /* allowed transitions */
487 int allowed = GNUNET_NO;
491 case S_NOT_CONNECTED:
492 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
493 (state == S_DISCONNECT))
494 allowed = GNUNET_YES;
497 allowed = GNUNET_YES;
500 allowed = GNUNET_YES;
503 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
504 allowed = GNUNET_YES;
508 case S_FAST_RECONNECT:
509 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
510 allowed = GNUNET_YES;
516 if (allowed == GNUNET_NO)
518 char *old = GNUNET_strdup (print_state (n->state));
519 char *new = GNUNET_strdup (print_state (state));
520 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
521 "Illegal state transition from `%s' to `%s' in line %u \n", old,
526 return GNUNET_SYSERR;
531 char *old = GNUNET_strdup (print_state (n->state));
532 char *new = GNUNET_strdup (print_state (state));
533 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
534 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
535 GNUNET_i2s (&n->id), n, old, new, line);
544 case S_FAST_RECONNECT:
547 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
548 GNUNET_SCHEDULER_cancel (n->state_reset);
550 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task,
554 case S_NOT_CONNECTED:
556 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
559 char *old = GNUNET_strdup (print_state (n->state));
560 char *new = GNUNET_strdup (print_state (state));
561 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
562 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
563 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address),
568 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
569 GNUNET_SCHEDULER_cancel (n->state_reset);
570 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
584 send_with_plugin (const struct GNUNET_PeerIdentity *target, const char *msgbuf,
585 size_t msgbuf_size, uint32_t priority,
586 struct GNUNET_TIME_Relative timeout, struct Session *session,
587 const struct GNUNET_HELLO_Address *address,
588 int force_address, GNUNET_TRANSPORT_TransmitContinuation cont,
591 struct GNUNET_TRANSPORT_PluginFunctions *papi;
592 size_t ret = GNUNET_SYSERR;
594 /* FIXME : ats returns an address with all values 0 */
598 cont (cont_cls, target, GNUNET_SYSERR);
599 return GNUNET_SYSERR;
602 if ((session == NULL) && (address->address_length == 0))
605 cont (cont_cls, target, GNUNET_SYSERR);
606 return GNUNET_SYSERR;
609 papi = GST_plugins_find (address->transport_name);
613 cont (cont_cls, target, GNUNET_SYSERR);
614 return GNUNET_SYSERR;
618 papi->send (papi->cls, target, msgbuf, msgbuf_size, 0, timeout, session,
620 address->address_length, GNUNET_YES, cont, cont_cls);
625 cont (cont_cls, target, GNUNET_SYSERR);
631 * Task invoked to start a transmission to another peer.
633 * @param cls the 'struct NeighbourMapEntry'
634 * @param tc scheduler context
637 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
641 * We're done with our transmission attempt, continue processing.
643 * @param cls the 'struct MessageQueue' of the message
644 * @param receiver intended receiver
645 * @param success whether it worked or not
648 transmit_send_continuation (void *cls,
649 const struct GNUNET_PeerIdentity *receiver,
652 struct MessageQueue *mq;
653 struct NeighbourMapEntry *n;
659 GNUNET_assert (n->is_active == mq);
661 if (success == GNUNET_YES)
663 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
664 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
668 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
669 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
670 (success == GNUNET_OK) ? "successful" : "FAILED");
672 if (NULL != mq->cont)
673 mq->cont (mq->cont_cls, success);
679 * Check the ready list for the given neighbour and if a plugin is
680 * ready for transmission (and if we have a message), do so!
682 * @param n target peer for which to transmit
685 try_transmission_to_peer (struct NeighbourMapEntry *n)
687 struct MessageQueue *mq;
688 struct GNUNET_TIME_Relative timeout;
691 if (n->is_active != NULL)
694 return; /* transmission already pending */
696 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
699 return; /* currently waiting for bandwidth */
701 while (NULL != (mq = n->messages_head))
703 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
704 if (timeout.rel_value > 0)
706 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
709 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
712 return; /* no more messages */
714 if (n->address == NULL)
716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
717 "No address for peer `%s'\n",
718 GNUNET_i2s (&n->id));
719 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
720 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
721 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
725 if (GST_plugins_find (n->address->transport_name) == NULL)
730 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
734 if ((n->address->address_length == 0) && (n->session == NULL))
736 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
737 "No address for peer `%s'\n",
738 GNUNET_i2s (&n->id));
739 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
740 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
741 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
746 send_with_plugin (&n->id, mq->message_buf, mq->message_buf_size, 0,
747 timeout, n->session, n->address,
748 GNUNET_YES, &transmit_send_continuation,
752 /* failure, but 'send' would not call continuation in this case,
753 * so we need to do it here! */
754 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
761 * Task invoked to start a transmission to another peer.
763 * @param cls the 'struct NeighbourMapEntry'
764 * @param tc scheduler context
767 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
769 struct NeighbourMapEntry *n = cls;
771 GNUNET_assert (NULL != lookup_neighbour (&n->id));
772 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
773 try_transmission_to_peer (n);
778 * Initialize the neighbours subsystem.
780 * @param cls closure for callbacks
781 * @param connect_cb function to call if we connect to a peer
782 * @param disconnect_cb function to call if we disconnect from a peer
785 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
786 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
789 connect_notify_cb = connect_cb;
790 disconnect_notify_cb = disconnect_cb;
791 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
796 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
800 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
801 "Sending DISCONNECT message to peer `%4s': %i\n",
802 GNUNET_i2s (target), result);
808 send_disconnect (const struct GNUNET_PeerIdentity *target,
809 const struct GNUNET_HELLO_Address *address,
810 struct Session *session)
813 struct SessionDisconnectMessage disconnect_msg;
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817 "Sending DISCONNECT message to peer `%4s'\n",
818 GNUNET_i2s (target));
821 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
822 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
823 disconnect_msg.reserved = htonl (0);
824 disconnect_msg.purpose.size =
825 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
826 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
827 sizeof (struct GNUNET_TIME_AbsoluteNBO));
828 disconnect_msg.purpose.purpose =
829 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
830 disconnect_msg.timestamp =
831 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
832 disconnect_msg.public_key = GST_my_public_key;
833 GNUNET_assert (GNUNET_OK ==
834 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
835 &disconnect_msg.purpose,
836 &disconnect_msg.signature));
839 send_with_plugin (target, (const char *) &disconnect_msg,
840 sizeof (disconnect_msg), UINT32_MAX,
841 GNUNET_TIME_UNIT_FOREVER_REL, session, address,
843 &send_disconnect_cont, NULL);
845 if (ret == GNUNET_SYSERR)
846 return GNUNET_SYSERR;
848 GNUNET_STATISTICS_update (GST_stats,
850 ("# peers disconnected due to external request"), 1,
857 * Disconnect from the given neighbour, clean up the record.
859 * @param n neighbour to disconnect from
862 disconnect_neighbour (struct NeighbourMapEntry *n)
864 struct MessageQueue *mq;
867 previous_state = n->state;
869 if (is_disconnecting (n))
873 /* send DISCONNECT MESSAGE */
874 if ((previous_state == S_CONNECTED) || is_connecting (n))
877 send_disconnect (&n->id, n->address,
879 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
880 GNUNET_i2s (&n->id));
882 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
883 "Could not send DISCONNECT_MSG to `%s'\n",
884 GNUNET_i2s (&n->id));
887 change_state (n, S_DISCONNECT);
889 if (previous_state == S_CONNECTED)
891 GNUNET_assert (NULL != n->address);
892 if (n->address_state == USED)
894 GST_validation_set_address_use (&n->id,
899 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
900 n->address_state = UNUSED;
904 if (n->address != NULL)
906 struct GNUNET_TRANSPORT_PluginFunctions *papi;
907 papi = GST_plugins_find (n->address->transport_name);
909 papi->disconnect (papi->cls, &n->id);
911 while (NULL != (mq = n->messages_head))
913 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
914 if (NULL != mq->cont)
915 mq->cont (mq->cont_cls, GNUNET_SYSERR);
918 if (NULL != n->is_active)
920 n->is_active->n = NULL;
924 switch (previous_state) {
926 // GNUNET_assert (neighbours_connected > 0);
927 neighbours_connected--;
928 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
929 GNUNET_SCHEDULER_cancel (n->keepalive_task);
930 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
931 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
933 disconnect_notify_cb (callback_cls, &n->id);
935 case S_FAST_RECONNECT:
936 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
938 GNUNET_STATISTICS_update (GST_stats,
940 ("# fast reconnects failed"),
942 disconnect_notify_cb (callback_cls, &n->id);
947 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
949 GNUNET_assert (GNUNET_YES ==
950 GNUNET_CONTAINER_multihashmap_remove (neighbours,
951 &n->id.hashPubKey, n));
952 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
954 GNUNET_SCHEDULER_cancel (n->ats_suggest);
955 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
957 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
959 GNUNET_SCHEDULER_cancel (n->timeout_task);
960 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
962 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
964 GNUNET_SCHEDULER_cancel (n->transmission_task);
965 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
967 if (NULL != n->address)
969 GNUNET_HELLO_address_free (n->address);
973 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
974 GNUNET_i2s (&n->id), n);
980 * Peer has been idle for too long. Disconnect.
982 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
983 * @param tc scheduler context
986 neighbour_timeout_task (void *cls,
987 const struct GNUNET_SCHEDULER_TaskContext *tc)
989 struct NeighbourMapEntry *n = cls;
991 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
993 GNUNET_STATISTICS_update (GST_stats,
995 ("# peers disconnected due to timeout"), 1,
997 disconnect_neighbour (n);
1002 * Send another keepalive message.
1004 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1005 * @param tc scheduler context
1008 neighbour_keepalive_task (void *cls,
1009 const struct GNUNET_SCHEDULER_TaskContext *tc)
1011 struct NeighbourMapEntry *n = cls;
1012 struct GNUNET_MessageHeader m;
1016 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1017 &neighbour_keepalive_task, n);
1019 GNUNET_assert (S_CONNECTED == n->state);
1020 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1022 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1023 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1026 ret = send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1027 UINT32_MAX /* priority */ ,
1028 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
1029 GNUNET_YES, NULL, NULL);
1031 n->expect_latency_response = GNUNET_NO;
1032 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero();
1033 if (ret != GNUNET_SYSERR)
1035 n->expect_latency_response = GNUNET_YES;
1036 n->keep_alive_sent = GNUNET_TIME_absolute_get();
1043 * Disconnect from the given neighbour.
1046 * @param key hash of neighbour's public key (not used)
1047 * @param value the 'struct NeighbourMapEntry' of the neighbour
1050 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1052 struct NeighbourMapEntry *n = value;
1055 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1056 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1058 if (S_CONNECTED == n->state)
1059 GNUNET_STATISTICS_update (GST_stats,
1061 ("# peers disconnected due to global disconnect"),
1063 disconnect_neighbour (n);
1069 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1071 struct NeighbourMapEntry *n = cls;
1073 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1075 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1076 " ATS did not suggested address to connect to peer `%s'\n",
1077 GNUNET_i2s (&n->id));
1079 disconnect_neighbour (n);
1083 * Cleanup the neighbours subsystem.
1086 GST_neighbours_stop ()
1088 // This can happen during shutdown
1089 if (neighbours == NULL)
1094 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1096 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1097 // GNUNET_assert (neighbours_connected == 0);
1099 callback_cls = NULL;
1100 connect_notify_cb = NULL;
1101 disconnect_notify_cb = NULL;
1104 struct ContinutionContext
1106 struct GNUNET_HELLO_Address *address;
1108 struct Session *session;
1111 static void send_outbound_quota (const struct GNUNET_PeerIdentity *target, struct GNUNET_BANDWIDTH_Value32NBO quota)
1113 struct QuotaSetMessage q_msg;
1115 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1116 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1117 ntohl (quota.value__), GNUNET_i2s (target));
1119 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1120 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1121 q_msg.quota = quota;
1122 q_msg.peer = (*target);
1123 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1127 * We tried to send a SESSION_CONNECT message to another peer. If this
1128 * succeeded, we change the state. If it failed, we should tell
1129 * ATS to not use this address anymore (until it is re-validated).
1131 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1132 * @param success GNUNET_OK on success
1135 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1138 struct ContinutionContext * cc = cls;
1139 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1141 if (GNUNET_YES != success)
1142 GNUNET_ATS_address_destroyed (GST_ats, cc->address, NULL);
1143 //GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1144 if ( (NULL == neighbours) ||
1146 (n->state == S_DISCONNECT))
1148 GNUNET_HELLO_address_free (cc->address);
1153 if ((GNUNET_YES == success) &&
1154 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1156 change_state (n, S_CONNECT_SENT);
1157 GNUNET_HELLO_address_free (cc->address);
1162 if ((GNUNET_NO == success) &&
1163 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1166 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1167 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1168 GNUNET_i2s (&n->id),
1169 GST_plugins_a2s (n->address),
1172 change_state (n, S_NOT_CONNECTED);
1173 GNUNET_ATS_address_destroyed (GST_ats, cc->address, NULL);
1174 //GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1176 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1177 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1179 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1181 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1183 GNUNET_HELLO_address_free (cc->address);
1188 * We tried to switch addresses with an peer already connected. If it failed,
1189 * we should tell ATS to not use this address anymore (until it is re-validated).
1191 * @param cls the 'struct NeighbourMapEntry'
1192 * @param success GNUNET_OK on success
1195 send_switch_address_continuation (void *cls,
1196 const struct GNUNET_PeerIdentity *target,
1199 struct ContinutionContext * cc = cls;
1200 struct NeighbourMapEntry *n;
1202 if (neighbours == NULL)
1204 GNUNET_HELLO_address_free (cc->address);
1206 return; /* neighbour is going away */
1209 n = lookup_neighbour(&cc->address->peer);
1210 if ((n == NULL) || (is_disconnecting (n)))
1212 GNUNET_HELLO_address_free (cc->address);
1214 return; /* neighbour is going away */
1217 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1218 if (GNUNET_YES != success)
1221 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1222 "Failed to switch connected peer `%s' to address '%s' session %X, asking ATS for new address \n",
1223 GNUNET_i2s (&n->id),
1224 GST_plugins_a2s (n->address), n->session);
1226 GNUNET_ATS_address_destroyed (GST_ats, cc->address, NULL);
1227 //GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1229 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1230 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1232 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1234 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1235 GNUNET_HELLO_address_free (cc->address);
1239 /* Tell ATS that switching addresses was successful */
1242 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
1244 case S_FAST_RECONNECT:
1246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1247 "Successful fast reconnect to peer `%s'\n", GNUNET_i2s (&n->id));
1249 change_state (n, S_CONNECTED);
1250 neighbours_connected++;
1251 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1254 if (n->address_state == FRESH)
1256 GST_validation_set_address_use (&n->id,
1260 n->address_state = USED;
1262 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1264 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1265 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1267 /* Updating quotas */
1268 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1269 send_outbound_quota(target, n->bandwidth_out);
1274 GNUNET_HELLO_address_free (cc->address);
1279 * We tried to send a SESSION_CONNECT message to another peer. If this
1280 * succeeded, we change the state. If it failed, we should tell
1281 * ATS to not use this address anymore (until it is re-validated).
1283 * @param cls the 'struct NeighbourMapEntry'
1284 * @param success GNUNET_OK on success
1287 send_connect_ack_continuation (void *cls,
1288 const struct GNUNET_PeerIdentity *target,
1291 struct ContinutionContext * cc = cls;
1292 struct NeighbourMapEntry *n;
1294 if (neighbours == NULL)
1296 GNUNET_HELLO_address_free (cc->address);
1298 return; /* neighbour is going away */
1301 n = lookup_neighbour(&cc->address->peer);
1302 if ((n == NULL) || (is_disconnecting (n)))
1304 GNUNET_HELLO_address_free (cc->address);
1306 return; /* neighbour is going away */
1309 if (GNUNET_YES == success)
1311 GNUNET_HELLO_address_free (cc->address);
1313 return; /* sending successful */
1316 /* sending failed, ask for next address */
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1320 GNUNET_i2s (&n->id),
1321 GST_plugins_a2s (n->address),
1324 change_state (n, S_NOT_CONNECTED);
1326 GNUNET_ATS_address_destroyed (GST_ats, cc->address, NULL);
1327 //GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1329 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1330 GNUNET_SCHEDULER_cancel (n->ats_suggest);
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);
1341 * For an existing neighbour record, set the active connection to
1342 * the given address.
1344 * @param peer identity of the peer to switch the address for
1345 * @param address address of the other peer, NULL if other peer
1347 * @param session session to use (or NULL)
1348 * @param ats performance data
1349 * @param ats_count number of entries in ats
1350 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1351 * connection is not up (yet)
1354 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1355 const struct GNUNET_HELLO_Address *address,
1356 struct Session *session,
1357 const struct GNUNET_ATS_Information *ats,
1359 struct GNUNET_BANDWIDTH_Value32NBO
1361 struct GNUNET_BANDWIDTH_Value32NBO
1364 struct NeighbourMapEntry *n;
1365 struct SessionConnectMessage connect_msg;
1366 struct ContinutionContext * cc;
1370 if (neighbours == NULL)
1372 /* This can happen during shutdown */
1375 n = lookup_neighbour (peer);
1378 if (n->state == S_DISCONNECT)
1380 /* We are disconnecting, nothing to do here */
1383 GNUNET_assert (address->transport_name != NULL);
1384 if ( (session == NULL) && (0 == address->address_length) )
1386 GNUNET_break_op (0);
1387 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1388 GNUNET_ATS_suggest_address (GST_ats, peer);
1392 /* checks successful and neighbour != NULL */
1394 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1395 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1396 GST_plugins_a2s (address),
1399 print_state(n->state));
1401 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1403 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1404 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1406 /* do not switch addresses just update quotas */
1407 if ( (n->state == S_CONNECTED) &&
1408 (NULL != n->address) &&
1409 (0 == GNUNET_HELLO_address_cmp (address,
1411 (n->session == session) )
1413 n->bandwidth_in = bandwidth_in;
1414 n->bandwidth_out = bandwidth_out;
1415 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1416 send_outbound_quota(peer, n->bandwidth_out);
1419 if (n->state == S_CONNECTED)
1421 /* mark old address as no longer used */
1422 GNUNET_assert (NULL != n->address);
1423 if (n->address_state == USED)
1425 GST_validation_set_address_use (&n->id,
1429 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1430 n->address_state = UNUSED;
1435 /* set new address */
1436 if (NULL != n->address)
1437 GNUNET_HELLO_address_free (n->address);
1438 n->address = GNUNET_HELLO_address_copy (address);
1439 n->address_state = FRESH;
1440 n->session = session;
1441 n->bandwidth_in = bandwidth_in;
1442 n->bandwidth_out = bandwidth_out;
1443 GNUNET_SCHEDULER_cancel (n->timeout_task);
1445 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1446 &neighbour_timeout_task, n);
1449 case S_NOT_CONNECTED:
1450 case S_CONNECT_SENT:
1451 msg_len = sizeof (struct SessionConnectMessage);
1452 connect_msg.header.size = htons (msg_len);
1453 connect_msg.header.type =
1454 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1455 connect_msg.reserved = htonl (0);
1456 connect_msg.timestamp =
1457 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1459 cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1460 cc->session = session;
1461 cc->address = GNUNET_HELLO_address_copy (address);
1463 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1464 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1465 address, GNUNET_YES,
1466 &send_connect_continuation,
1469 case S_CONNECT_RECV:
1470 /* We received a CONNECT message and asked ATS for an address */
1471 msg_len = sizeof (struct SessionConnectMessage);
1472 connect_msg.header.size = htons (msg_len);
1473 connect_msg.header.type =
1474 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1475 connect_msg.reserved = htonl (0);
1476 connect_msg.timestamp =
1477 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1478 cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1479 cc->session = session;
1480 cc->address = GNUNET_HELLO_address_copy (address);
1482 send_with_plugin (&n->id, (const void *) &connect_msg, msg_len,
1483 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1484 address, GNUNET_YES,
1485 &send_connect_ack_continuation, cc);
1488 case S_FAST_RECONNECT:
1489 /* connected peer is switching addresses or tries fast reconnect*/
1490 msg_len = sizeof (struct SessionConnectMessage);
1491 connect_msg.header.size = htons (msg_len);
1492 connect_msg.header.type =
1493 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1494 connect_msg.reserved = htonl (0);
1495 connect_msg.timestamp =
1496 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1497 cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1498 cc->session = session;
1499 cc->address = GNUNET_HELLO_address_copy (address);
1501 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1502 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1503 address, GNUNET_YES,
1504 &send_switch_address_continuation, cc);
1505 if (ret == GNUNET_SYSERR)
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1508 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1510 GST_plugins_a2s (address), session);
1514 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1515 "Invalid connection state to switch addresses %u \n", n->state);
1516 GNUNET_break_op (0);
1523 * Obtain current latency information for the given neighbour.
1526 * @return observed latency of the address, FOREVER if the address was
1527 * never successfully validated
1529 struct GNUNET_TIME_Relative
1530 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1532 struct NeighbourMapEntry *n;
1534 n = lookup_neighbour (peer);
1536 ( (n->address == NULL) && (n->session == NULL) ) )
1537 return GNUNET_TIME_UNIT_FOREVER_REL;
1544 * Create an entry in the neighbour map for the given peer
1546 * @param peer peer to create an entry for
1547 * @return new neighbour map entry
1549 static struct NeighbourMapEntry *
1550 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1552 struct NeighbourMapEntry *n;
1555 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1556 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1558 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1560 n->state = S_NOT_CONNECTED;
1561 n->latency = GNUNET_TIME_relative_get_forever();
1562 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1563 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1564 MAX_BANDWIDTH_CARRY_S);
1566 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1567 &neighbour_timeout_task, n);
1568 GNUNET_assert (GNUNET_OK ==
1569 GNUNET_CONTAINER_multihashmap_put (neighbours,
1570 &n->id.hashPubKey, n,
1571 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1577 * Try to create a connection to the given target (eventually).
1579 * @param target peer to try to connect to
1582 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1584 struct NeighbourMapEntry *n;
1586 // This can happen during shutdown
1587 if (neighbours == NULL)
1592 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1593 GNUNET_i2s (target));
1596 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1601 n = lookup_neighbour (target);
1605 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1606 return; /* already connecting or connected */
1607 if (is_disconnecting (n))
1608 change_state (n, S_NOT_CONNECTED);
1613 n = setup_neighbour (target);
1615 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1616 "Asking ATS for suggested address to connect to peer `%s'\n",
1617 GNUNET_i2s (&n->id));
1620 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1624 * Test if we're connected to the given peer.
1626 * @param target peer to test
1627 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1630 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1632 struct NeighbourMapEntry *n;
1634 // This can happen during shutdown
1635 if (neighbours == NULL)
1640 n = lookup_neighbour (target);
1642 if ((NULL == n) || (S_CONNECTED != n->state))
1643 return GNUNET_NO; /* not connected */
1648 * A session was terminated. Take note.
1650 * @param peer identity of the peer where the session died
1651 * @param session session that is gone
1654 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1655 struct Session *session)
1657 struct NeighbourMapEntry *n;
1659 if (neighbours == NULL)
1661 /* This can happen during shutdown */
1666 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Session %X to peer `%s' ended \n",
1667 session, GNUNET_i2s (peer));
1670 n = lookup_neighbour (peer);
1673 if (session != n->session)
1674 return; /* doesn't affect us */
1675 if (n->state == S_CONNECTED)
1677 if (n->address_state == USED)
1679 GST_validation_set_address_use (&n->id,
1683 GNUNET_ATS_address_in_use (GST_ats,n->address, n->session, GNUNET_NO);
1684 n->address_state = UNUSED;
1689 //GNUNET_ATS_address_destroyed(GST_ats, n->address, n->session);
1691 if (NULL != n->address)
1693 GNUNET_HELLO_address_free (n->address);
1698 /* not connected anymore anyway, shouldn't matter */
1699 if ((S_CONNECTED != n->state) && (!is_connecting (n)))
1702 /* connected, try fast reconnect */
1704 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1705 "Trying fast reconnect to peer `%s'\n", GNUNET_i2s (peer));
1707 change_state (n, S_FAST_RECONNECT);
1708 GNUNET_assert (neighbours_connected > 0);
1709 neighbours_connected--;
1711 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1713 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1714 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1717 /* We are connected, so ask ATS to switch addresses */
1718 GNUNET_SCHEDULER_cancel (n->timeout_task);
1720 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1721 &neighbour_timeout_task, n);
1722 /* try QUICKLY to re-establish a connection, reduce timeout! */
1723 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1724 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1726 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1728 GNUNET_ATS_suggest_address (GST_ats, peer);
1733 * Transmit a message to the given target using the active connection.
1735 * @param target destination
1736 * @param msg message to send
1737 * @param msg_size number of bytes in msg
1738 * @param timeout when to fail with timeout
1739 * @param cont function to call when done
1740 * @param cont_cls closure for 'cont'
1743 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1744 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1745 GST_NeighbourSendContinuation cont, void *cont_cls)
1747 struct NeighbourMapEntry *n;
1748 struct MessageQueue *mq;
1750 // This can happen during shutdown
1751 if (neighbours == NULL)
1756 n = lookup_neighbour (target);
1757 if ((n == NULL) || (!is_connected (n)))
1759 GNUNET_STATISTICS_update (GST_stats,
1761 ("# messages not sent (no such peer or not connected)"),
1765 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1766 "Could not send message to peer `%s': unknown neighbour",
1767 GNUNET_i2s (target));
1768 else if (!is_connected (n))
1769 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1770 "Could not send message to peer `%s': not connected\n",
1771 GNUNET_i2s (target));
1774 cont (cont_cls, GNUNET_SYSERR);
1778 if ((n->session == NULL) && (n->address == NULL) )
1780 GNUNET_STATISTICS_update (GST_stats,
1782 ("# messages not sent (no such peer or not connected)"),
1785 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1786 "Could not send message to peer `%s': no address available\n",
1787 GNUNET_i2s (target));
1791 cont (cont_cls, GNUNET_SYSERR);
1795 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1796 GNUNET_STATISTICS_update (GST_stats,
1798 ("# bytes in message queue for other peers"),
1799 msg_size, GNUNET_NO);
1800 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1802 mq->cont_cls = cont_cls;
1803 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1804 memcpy (&mq[1], msg, msg_size);
1805 mq->message_buf = (const char *) &mq[1];
1806 mq->message_buf_size = msg_size;
1807 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1808 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1810 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1811 (NULL == n->is_active))
1812 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1817 * We have received a message from the given sender. How long should
1818 * we delay before receiving more? (Also used to keep the peer marked
1821 * @param sender sender of the message
1822 * @param size size of the message
1823 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1824 * GNUNET_NO if the neighbour is not connected or violates the quota,
1825 * GNUNET_SYSERR if the connection is not fully up yet
1826 * @return how long to wait before reading more from this sender
1828 struct GNUNET_TIME_Relative
1829 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1830 *sender, ssize_t size, int *do_forward)
1832 struct NeighbourMapEntry *n;
1833 struct GNUNET_TIME_Relative ret;
1835 // This can happen during shutdown
1836 if (neighbours == NULL)
1838 return GNUNET_TIME_UNIT_FOREVER_REL;
1841 n = lookup_neighbour (sender);
1844 GST_neighbours_try_connect (sender);
1845 n = lookup_neighbour (sender);
1848 GNUNET_STATISTICS_update (GST_stats,
1850 ("# messages discarded due to lack of neighbour record"),
1852 *do_forward = GNUNET_NO;
1853 return GNUNET_TIME_UNIT_ZERO;
1856 if (!is_connected (n))
1858 *do_forward = GNUNET_SYSERR;
1859 return GNUNET_TIME_UNIT_ZERO;
1861 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1863 n->quota_violation_count++;
1865 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1866 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1867 n->in_tracker.available_bytes_per_s__,
1868 n->quota_violation_count);
1870 /* Discount 32k per violation */
1871 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1875 if (n->quota_violation_count > 0)
1877 /* try to add 32k back */
1878 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1879 n->quota_violation_count--;
1882 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1884 GNUNET_STATISTICS_update (GST_stats,
1886 ("# bandwidth quota violations by other peers"),
1888 *do_forward = GNUNET_NO;
1889 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1891 *do_forward = GNUNET_YES;
1892 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1893 if (ret.rel_value > 0)
1896 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1897 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1898 (unsigned long long) n->in_tracker.
1899 consumption_since_last_update__,
1900 (unsigned int) n->in_tracker.available_bytes_per_s__,
1901 (unsigned long long) ret.rel_value);
1903 GNUNET_STATISTICS_update (GST_stats,
1904 gettext_noop ("# ms throttling suggested"),
1905 (int64_t) ret.rel_value, GNUNET_NO);
1912 * Keep the connection to the given neighbour alive longer,
1913 * we received a KEEPALIVE (or equivalent).
1915 * @param neighbour neighbour to keep alive
1918 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1920 struct NeighbourMapEntry *n;
1922 // This can happen during shutdown
1923 if (neighbours == NULL)
1928 n = lookup_neighbour (neighbour);
1931 GNUNET_STATISTICS_update (GST_stats,
1933 ("# KEEPALIVE messages discarded (not connected)"),
1937 GNUNET_SCHEDULER_cancel (n->timeout_task);
1939 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1940 &neighbour_timeout_task, n);
1942 /* send reply to measure latency */
1943 if (S_CONNECTED != n->state)
1946 struct GNUNET_MessageHeader m;
1947 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1948 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1950 send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1951 UINT32_MAX /* priority */ ,
1952 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
1953 GNUNET_YES, NULL, NULL);
1957 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
1960 * @param neighbour neighbour to keep alive
1963 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1964 const struct GNUNET_ATS_Information * ats,
1967 struct NeighbourMapEntry *n;
1968 struct GNUNET_ATS_Information * ats_new;
1971 if (neighbours == NULL)
1973 // This can happen during shutdown
1977 n = lookup_neighbour (neighbour);
1980 GNUNET_STATISTICS_update (GST_stats,
1982 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1986 if (n->expect_latency_response != GNUNET_YES)
1988 GNUNET_STATISTICS_update (GST_stats,
1990 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
1994 n->expect_latency_response = GNUNET_NO;
1996 GNUNET_assert (n->keep_alive_sent.abs_value != GNUNET_TIME_absolute_get_zero().abs_value);
1997 n->latency = GNUNET_TIME_absolute_get_difference(n->keep_alive_sent, GNUNET_TIME_absolute_get());
1999 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2000 "Latency for peer `%s' is %llu ms\n",
2001 GNUNET_i2s (&n->id), n->latency.rel_value);
2005 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever().rel_value)
2006 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2009 ats_new = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2010 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2013 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2014 if (n->latency.rel_value > UINT32_MAX)
2015 latency = UINT32_MAX;
2017 latency = n->latency.rel_value;
2018 ats_new[ats_count].value = htonl (latency);
2020 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new, ats_count + 1);
2021 GNUNET_free (ats_new);
2027 * Change the incoming quota for the given peer.
2029 * @param neighbour identity of peer to change qutoa for
2030 * @param quota new quota
2033 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2034 struct GNUNET_BANDWIDTH_Value32NBO quota)
2036 struct NeighbourMapEntry *n;
2038 // This can happen during shutdown
2039 if (neighbours == NULL)
2044 n = lookup_neighbour (neighbour);
2047 GNUNET_STATISTICS_update (GST_stats,
2049 ("# SET QUOTA messages ignored (no such peer)"),
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2055 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2056 ntohl (quota.value__), GNUNET_i2s (&n->id));
2058 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2059 if (0 != ntohl (quota.value__))
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2063 GNUNET_i2s (&n->id), "SET_QUOTA");
2065 if (is_connected (n))
2066 GNUNET_STATISTICS_update (GST_stats,
2067 gettext_noop ("# disconnects due to quota of 0"),
2069 disconnect_neighbour (n);
2074 * Closure for the neighbours_iterate function.
2076 struct IteratorContext
2079 * Function to call on each connected neighbour.
2081 GST_NeighbourIterator cb;
2091 * Call the callback from the closure for each connected neighbour.
2093 * @param cls the 'struct IteratorContext'
2094 * @param key the hash of the public key of the neighbour
2095 * @param value the 'struct NeighbourMapEntry'
2096 * @return GNUNET_OK (continue to iterate)
2099 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2101 struct IteratorContext *ic = cls;
2102 struct NeighbourMapEntry *n = value;
2104 if (!is_connected (n))
2107 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2113 * Iterate over all connected neighbours.
2115 * @param cb function to call
2116 * @param cb_cls closure for cb
2119 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2121 struct IteratorContext ic;
2123 // This can happen during shutdown
2124 if (neighbours == NULL)
2131 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2135 * If we have an active connection to the given target, it must be shutdown.
2137 * @param target peer to disconnect from
2140 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2142 struct NeighbourMapEntry *n;
2144 // This can happen during shutdown
2145 if (neighbours == NULL)
2150 n = lookup_neighbour (target);
2152 return; /* not active */
2153 disconnect_neighbour (n);
2158 * We received a disconnect message from the given peer,
2159 * validate and process.
2161 * @param peer sender of the message
2162 * @param msg the disconnect message
2165 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2167 const struct GNUNET_MessageHeader
2170 struct NeighbourMapEntry *n;
2171 const struct SessionDisconnectMessage *sdm;
2175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2176 "Received DISCONNECT message from peer `%s'\n",
2180 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2182 // GNUNET_break_op (0);
2183 GNUNET_STATISTICS_update (GST_stats,
2185 ("# disconnect messages ignored (old format)"), 1,
2189 sdm = (const struct SessionDisconnectMessage *) msg;
2190 n = lookup_neighbour (peer);
2192 return; /* gone already */
2193 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2194 n->connect_ts.abs_value)
2196 GNUNET_STATISTICS_update (GST_stats,
2198 ("# disconnect messages ignored (timestamp)"), 1,
2202 GNUNET_CRYPTO_hash (&sdm->public_key,
2203 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2205 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2207 GNUNET_break_op (0);
2210 if (ntohl (sdm->purpose.size) !=
2211 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2212 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2213 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2215 GNUNET_break_op (0);
2219 GNUNET_CRYPTO_rsa_verify
2220 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2221 &sdm->signature, &sdm->public_key))
2223 GNUNET_break_op (0);
2226 GST_neighbours_force_disconnect (peer);
2231 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2232 * Consider switching to it.
2234 * @param message possibly a 'struct SessionConnectMessage' (check format)
2235 * @param peer identity of the peer to switch the address for
2236 * @param address address of the other peer, NULL if other peer
2238 * @param session session to use (or NULL)
2239 * @param ats performance data
2240 * @param ats_count number of entries in ats
2243 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2244 const struct GNUNET_PeerIdentity *peer,
2245 const struct GNUNET_HELLO_Address *address,
2246 struct Session *session,
2247 const struct GNUNET_ATS_Information *ats,
2250 const struct SessionConnectMessage *scm;
2251 struct GNUNET_MessageHeader msg;
2252 struct NeighbourMapEntry *n;
2257 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2258 "Received CONNECT_ACK message from peer `%s'\n",
2262 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2264 GNUNET_break_op (0);
2267 scm = (const struct SessionConnectMessage *) message;
2268 GNUNET_break_op (ntohl (scm->reserved) == 0);
2269 n = lookup_neighbour (peer);
2272 /* we did not send 'CONNECT' (how could we? no record for this peer!) */
2273 GNUNET_break_op (0);
2279 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2281 * We also received an CONNECT message, switched from SENDT to RECV and
2282 * ATS already suggested us an address after a successful blacklist check
2284 if ((n->state != S_CONNECT_SENT) && ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2286 GNUNET_STATISTICS_update (GST_stats,
2287 gettext_noop ("# unexpected CONNECT_ACK messages"), 1,
2292 if (NULL != session)
2293 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2295 "Giving ATS session %p of plugin %s for peer %s\n",
2296 session, address->transport_name, GNUNET_i2s (peer));
2297 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2298 GNUNET_assert (NULL != n->address);
2300 change_state (n, S_CONNECTED);
2301 if (n->address_state == FRESH)
2303 GST_validation_set_address_use (&n->id,
2307 n->address_state = USED;
2309 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2311 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2313 /* send ACK (ACK) */
2314 msg_len = sizeof (msg);
2315 msg.size = htons (msg_len);
2316 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2319 send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2320 GNUNET_TIME_UNIT_FOREVER_REL, n->session,
2321 n->address, GNUNET_YES, NULL,
2324 if (ret == GNUNET_SYSERR)
2325 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2326 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2327 GNUNET_i2s (&n->id),
2328 GST_plugins_a2s (n->address), n->session);
2331 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2332 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2334 neighbours_connected++;
2335 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2338 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2339 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2340 GNUNET_i2s (&n->id),
2341 GST_plugins_a2s (n->address), n->session,
2344 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2345 send_outbound_quota(peer, n->bandwidth_out);
2351 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2352 const struct GNUNET_PeerIdentity *peer,
2353 const struct GNUNET_HELLO_Address *address,
2354 struct Session *session,
2355 const struct GNUNET_ATS_Information *ats,
2358 struct NeighbourMapEntry *n;
2361 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2362 "Received ACK message from peer `%s'\n",
2366 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2368 GNUNET_break_op (0);
2371 n = lookup_neighbour (peer);
2374 send_disconnect (peer, address,
2379 if (S_CONNECTED == n->state)
2381 if (!is_connecting(n))
2383 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# unexpected ACK messages"), 1,
2387 if (NULL != session)
2388 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2390 "Giving ATS session %p of plugin %s for peer %s\n",
2391 session, address->transport_name, GNUNET_i2s (peer));
2392 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2393 GNUNET_assert (n->address != NULL);
2394 change_state (n, S_CONNECTED);
2395 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2397 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2399 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2400 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2401 if (n->address_state == FRESH)
2403 GST_validation_set_address_use (&n->id,
2407 n->address_state = USED;
2409 neighbours_connected++;
2410 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2415 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2416 GNUNET_i2s (&n->id),
2417 GST_plugins_a2s (n->address), n->session,
2420 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2421 send_outbound_quota(peer, n->bandwidth_out);
2424 struct BlackListCheckContext
2426 struct GNUNET_ATS_Information *ats;
2430 struct Session *session;
2432 struct GNUNET_HELLO_Address *address;
2434 struct GNUNET_TIME_Absolute ts;
2439 handle_connect_blacklist_cont (void *cls,
2440 const struct GNUNET_PeerIdentity *peer,
2443 struct NeighbourMapEntry *n;
2444 struct BlackListCheckContext *bcc = cls;
2447 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2448 "Blacklist check due to CONNECT message: `%s'\n",
2450 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2454 if (GNUNET_OK != result)
2456 GNUNET_HELLO_address_free (bcc->address);
2461 n = lookup_neighbour (peer);
2463 n = setup_neighbour (peer);
2465 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2467 if (NULL != bcc->session)
2468 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2470 "Giving ATS session %p of address `%s' for peer %s\n",
2472 GST_plugins_a2s (bcc->address),
2474 GNUNET_ATS_address_update (GST_ats, bcc->address,
2475 bcc->session, bcc->ats, bcc->ats_count);
2476 n->connect_ts = bcc->ts;
2479 GNUNET_HELLO_address_free (bcc->address);
2482 if (n->state != S_CONNECT_RECV)
2483 change_state (n, S_CONNECT_RECV);
2485 /* Ask ATS for an address to connect via that address */
2486 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2487 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2489 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2491 GNUNET_ATS_suggest_address (GST_ats, peer);
2495 * We received a 'SESSION_CONNECT' message from the other peer.
2496 * Consider switching to it.
2498 * @param message possibly a 'struct SessionConnectMessage' (check format)
2499 * @param peer identity of the peer to switch the address for
2500 * @param address address of the other peer, NULL if other peer
2502 * @param session session to use (or NULL)
2503 * @param ats performance data
2504 * @param ats_count number of entries in ats (excluding 0-termination)
2507 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2508 const struct GNUNET_PeerIdentity *peer,
2509 const struct GNUNET_HELLO_Address *address,
2510 struct Session *session,
2511 const struct GNUNET_ATS_Information *ats,
2514 const struct SessionConnectMessage *scm;
2515 struct NeighbourMapEntry *n;
2516 struct BlackListCheckContext *bcc = NULL;
2519 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2520 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2523 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2525 GNUNET_break_op (0);
2529 scm = (const struct SessionConnectMessage *) message;
2530 GNUNET_break_op (ntohl (scm->reserved) == 0);
2532 n = lookup_neighbour (peer);
2534 (S_CONNECTED == n->state) )
2536 /* connected peer switches addresses */
2537 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2541 /* we are not connected to this peer */
2542 /* do blacklist check */
2544 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2545 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2546 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2547 bcc->ats_count = ats_count + 1;
2548 bcc->address = GNUNET_HELLO_address_copy (address);
2549 bcc->session = session;
2550 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2551 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2552 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2553 bcc->ats[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2554 GST_blacklist_test_allowed (peer, address->transport_name, handle_connect_blacklist_cont,
2559 /* end of file gnunet-service-transport_neighbours.c */