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
232 * Entry in neighbours.
234 struct NeighbourMapEntry
238 * Head of list of messages we would like to send to this peer;
239 * must contain at most one message per client.
241 struct MessageQueue *messages_head;
244 * Tail of list of messages we would like to send to this peer; must
245 * contain at most one message per client.
247 struct MessageQueue *messages_tail;
250 * Are we currently trying to send a message? If so, which one?
252 struct MessageQueue *is_active;
255 * Active session for communicating with the peer.
257 struct Session *session;
260 * Address we currently use.
262 struct GNUNET_HELLO_Address *address;
265 * Identity of this neighbour.
267 struct GNUNET_PeerIdentity id;
270 * ID of task scheduled to run when this peer is about to
271 * time out (will free resources associated with the peer).
273 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
276 * ID of task scheduled to send keepalives.
278 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
281 * ID of task scheduled to run when we should try transmitting
282 * the head of the message queue.
284 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
287 * Tracker for inbound bandwidth.
289 struct GNUNET_BANDWIDTH_Tracker in_tracker;
292 * Inbound bandwidth from ATS, activated when connection is up
294 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
297 * Inbound bandwidth from ATS, activated when connection is up
299 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
302 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
304 struct GNUNET_TIME_Absolute connect_ts;
307 * When did we sent the last keep-alive message?
309 struct GNUNET_TIME_Absolute keep_alive_sent;
312 * Latest calculated latency value
314 struct GNUNET_TIME_Relative latency;
318 * We asked ATS for a new address for this peer
320 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
323 * Task the resets the peer state after due to an pending
324 * unsuccessful connection setup
326 GNUNET_SCHEDULER_TaskIdentifier state_reset;
330 * How often has the other peer (recently) violated the inbound
331 * traffic limit? Incremented by 10 per violation, decremented by 1
332 * per non-violation (for each time interval).
334 unsigned int quota_violation_count;
338 * The current state of the peer
339 * Element of enum State
344 * Did we sent an KEEP_ALIVE message and are we expecting a response?
346 int expect_latency_response;
352 * All known neighbours and their HELLOs.
354 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
357 * Closure for connect_notify_cb and disconnect_notify_cb
359 static void *callback_cls;
362 * Function to call when we connected to a neighbour.
364 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
367 * Function to call when we disconnected from a neighbour.
369 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
372 * counter for connected neighbours
374 static int neighbours_connected;
377 * Lookup a neighbour entry in the neighbours hash map.
379 * @param pid identity of the peer to look up
380 * @return the entry, NULL if there is no existing record
382 static struct NeighbourMapEntry *
383 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
385 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
388 #define change_state(n, state, ...) change (n, state, __LINE__)
391 is_connecting (struct NeighbourMapEntry *n)
393 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
399 is_connected (struct NeighbourMapEntry *n)
401 if (n->state == S_CONNECTED)
407 is_disconnecting (struct NeighbourMapEntry *n)
409 if (n->state == S_DISCONNECT)
415 print_state (int state)
420 return "S_CONNECTED";
423 return "S_CONNECT_RECV";
426 return "S_CONNECT_SENT";
429 return "S_DISCONNECT";
431 case S_NOT_CONNECTED:
432 return "S_NOT_CONNECTED";
434 case S_FAST_RECONNECT:
435 return "S_FAST_RECONNECT";
445 change (struct NeighbourMapEntry *n, int state, int line);
448 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
452 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
454 struct NeighbourMapEntry *n = cls;
458 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
459 if (n->state == S_CONNECTED)
463 GNUNET_STATISTICS_update (GST_stats,
465 ("# failed connection attempts due to timeout"), 1,
469 /* resetting state */
470 n->state = S_NOT_CONNECTED;
472 /* destroying address */
473 if (n->address != NULL)
475 GNUNET_assert (strlen(n->address->transport_name) > 0);
476 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
479 /* request new address */
480 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
481 GNUNET_SCHEDULER_cancel (n->ats_suggest);
483 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
485 GNUNET_ATS_suggest_address (GST_ats, &n->id);
489 change (struct NeighbourMapEntry *n, int state, int line)
491 /* allowed transitions */
492 int allowed = GNUNET_NO;
496 case S_NOT_CONNECTED:
497 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
498 (state == S_DISCONNECT))
499 allowed = GNUNET_YES;
502 allowed = GNUNET_YES;
505 allowed = GNUNET_YES;
508 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
509 allowed = GNUNET_YES;
513 case S_FAST_RECONNECT:
514 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
515 allowed = GNUNET_YES;
521 if (allowed == GNUNET_NO)
523 char *old = GNUNET_strdup (print_state (n->state));
524 char *new = GNUNET_strdup (print_state (state));
525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
526 "Illegal state transition from `%s' to `%s' in line %u \n", old,
531 return GNUNET_SYSERR;
535 char *old = GNUNET_strdup (print_state (n->state));
536 char *new = GNUNET_strdup (print_state (state));
537 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
538 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
539 GNUNET_i2s (&n->id), n, old, new, line);
548 case S_FAST_RECONNECT:
551 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
552 GNUNET_SCHEDULER_cancel (n->state_reset);
554 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task,
558 case S_NOT_CONNECTED:
560 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
563 char *old = GNUNET_strdup (print_state (n->state));
564 char *new = GNUNET_strdup (print_state (state));
565 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
566 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
567 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address),
572 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
573 GNUNET_SCHEDULER_cancel (n->state_reset);
574 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
588 send_with_plugin (const struct GNUNET_PeerIdentity *target, const char *msgbuf,
589 size_t msgbuf_size, uint32_t priority,
590 struct GNUNET_TIME_Relative timeout, struct Session *session,
591 const struct GNUNET_HELLO_Address *address,
592 int force_address, GNUNET_TRANSPORT_TransmitContinuation cont,
595 struct GNUNET_TRANSPORT_PluginFunctions *papi;
596 size_t ret = GNUNET_SYSERR;
598 /* FIXME : ats returns an address with all values 0 */
602 cont (cont_cls, target, GNUNET_SYSERR);
603 return GNUNET_SYSERR;
606 if ((session == NULL) && (address->address_length == 0))
609 cont (cont_cls, target, GNUNET_SYSERR);
610 return GNUNET_SYSERR;
613 papi = GST_plugins_find (address->transport_name);
617 cont (cont_cls, target, GNUNET_SYSERR);
618 return GNUNET_SYSERR;
622 papi->send (papi->cls, target, msgbuf, msgbuf_size, 0, timeout, session,
624 address->address_length, GNUNET_YES, cont, cont_cls);
629 cont (cont_cls, target, GNUNET_SYSERR);
635 * Task invoked to start a transmission to another peer.
637 * @param cls the 'struct NeighbourMapEntry'
638 * @param tc scheduler context
641 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
645 * We're done with our transmission attempt, continue processing.
647 * @param cls the 'struct MessageQueue' of the message
648 * @param receiver intended receiver
649 * @param success whether it worked or not
652 transmit_send_continuation (void *cls,
653 const struct GNUNET_PeerIdentity *receiver,
656 struct MessageQueue *mq;
657 struct NeighbourMapEntry *n;
658 struct NeighbourMapEntry *tmp;
660 tmp = lookup_neighbour(receiver);
664 if ((NULL != n) && (tmp != NULL) && (tmp == n))
666 GNUNET_assert (n->is_active == mq);
668 if (success == GNUNET_YES)
670 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
671 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
675 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
676 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
677 (success == GNUNET_OK) ? "successful" : "FAILED");
679 if (NULL != mq->cont)
680 mq->cont (mq->cont_cls, success);
686 * Check the ready list for the given neighbour and if a plugin is
687 * ready for transmission (and if we have a message), do so!
689 * @param n target peer for which to transmit
692 try_transmission_to_peer (struct NeighbourMapEntry *n)
694 struct MessageQueue *mq;
695 struct GNUNET_TIME_Relative timeout;
698 if (n->is_active != NULL)
701 return; /* transmission already pending */
703 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
706 return; /* currently waiting for bandwidth */
708 while (NULL != (mq = n->messages_head))
710 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
711 if (timeout.rel_value > 0)
713 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
716 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
719 return; /* no more messages */
721 if (n->address == NULL)
723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
724 "No address for peer `%s'\n",
725 GNUNET_i2s (&n->id));
726 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
727 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
728 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
732 if (GST_plugins_find (n->address->transport_name) == NULL)
737 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
741 if ((n->address->address_length == 0) && (n->session == NULL))
743 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
744 "No address for peer `%s'\n",
745 GNUNET_i2s (&n->id));
746 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
747 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
748 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
753 send_with_plugin (&n->id, mq->message_buf, mq->message_buf_size, 0,
754 timeout, n->session, n->address,
755 GNUNET_YES, &transmit_send_continuation,
759 /* failure, but 'send' would not call continuation in this case,
760 * so we need to do it here! */
761 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
768 * Task invoked to start a transmission to another peer.
770 * @param cls the 'struct NeighbourMapEntry'
771 * @param tc scheduler context
774 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
776 struct NeighbourMapEntry *n = cls;
778 GNUNET_assert (NULL != lookup_neighbour (&n->id));
779 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
780 try_transmission_to_peer (n);
785 * Initialize the neighbours subsystem.
787 * @param cls closure for callbacks
788 * @param connect_cb function to call if we connect to a peer
789 * @param disconnect_cb function to call if we disconnect from a peer
792 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
793 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
796 connect_notify_cb = connect_cb;
797 disconnect_notify_cb = disconnect_cb;
798 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
803 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
807 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
808 "Sending DISCONNECT message to peer `%4s': %i\n",
809 GNUNET_i2s (target), result);
815 send_disconnect (const struct GNUNET_PeerIdentity *target,
816 const struct GNUNET_HELLO_Address *address,
817 struct Session *session)
820 struct SessionDisconnectMessage disconnect_msg;
823 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
824 "Sending DISCONNECT message to peer `%4s'\n",
825 GNUNET_i2s (target));
828 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
829 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
830 disconnect_msg.reserved = htonl (0);
831 disconnect_msg.purpose.size =
832 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
833 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
834 sizeof (struct GNUNET_TIME_AbsoluteNBO));
835 disconnect_msg.purpose.purpose =
836 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
837 disconnect_msg.timestamp =
838 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
839 disconnect_msg.public_key = GST_my_public_key;
840 GNUNET_assert (GNUNET_OK ==
841 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
842 &disconnect_msg.purpose,
843 &disconnect_msg.signature));
846 send_with_plugin (target, (const char *) &disconnect_msg,
847 sizeof (disconnect_msg), UINT32_MAX,
848 GNUNET_TIME_UNIT_FOREVER_REL, session, address,
850 &send_disconnect_cont, NULL);
852 if (ret == GNUNET_SYSERR)
853 return GNUNET_SYSERR;
855 GNUNET_STATISTICS_update (GST_stats,
857 ("# peers disconnected due to external request"), 1,
864 * Disconnect from the given neighbour, clean up the record.
866 * @param n neighbour to disconnect from
869 disconnect_neighbour (struct NeighbourMapEntry *n)
871 struct MessageQueue *mq;
874 previous_state = n->state;
876 if (is_disconnecting (n))
880 /* send DISCONNECT MESSAGE */
881 if ((previous_state == S_CONNECTED) || is_connecting (n))
884 send_disconnect (&n->id, n->address,
886 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
887 GNUNET_i2s (&n->id));
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
890 "Could not send DISCONNECT_MSG to `%s'\n",
891 GNUNET_i2s (&n->id));
894 change_state (n, S_DISCONNECT);
896 if (previous_state == S_CONNECTED)
898 GNUNET_assert (NULL != n->address);
899 if (n->address_state == USED)
901 GST_validation_set_address_use (&n->id,
906 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
907 n->address_state = UNUSED;
911 if (n->address != NULL)
913 struct GNUNET_TRANSPORT_PluginFunctions *papi;
914 papi = GST_plugins_find (n->address->transport_name);
916 papi->disconnect (papi->cls, &n->id);
918 while (NULL != (mq = n->messages_head))
920 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
921 if (NULL != mq->cont)
922 mq->cont (mq->cont_cls, GNUNET_SYSERR);
925 if (NULL != n->is_active)
927 n->is_active->n = NULL;
931 switch (previous_state) {
933 // GNUNET_assert (neighbours_connected > 0);
934 neighbours_connected--;
935 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
936 GNUNET_SCHEDULER_cancel (n->keepalive_task);
937 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
938 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
940 disconnect_notify_cb (callback_cls, &n->id);
942 case S_FAST_RECONNECT:
943 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
945 GNUNET_STATISTICS_update (GST_stats,
947 ("# fast reconnects failed"),
949 disconnect_notify_cb (callback_cls, &n->id);
954 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
956 GNUNET_assert (GNUNET_YES ==
957 GNUNET_CONTAINER_multihashmap_remove (neighbours,
958 &n->id.hashPubKey, n));
959 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
961 GNUNET_SCHEDULER_cancel (n->ats_suggest);
962 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
964 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
966 GNUNET_SCHEDULER_cancel (n->timeout_task);
967 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
969 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
971 GNUNET_SCHEDULER_cancel (n->transmission_task);
972 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
974 if (NULL != n->address)
976 GNUNET_HELLO_address_free (n->address);
980 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
981 GNUNET_i2s (&n->id), n);
987 * Peer has been idle for too long. Disconnect.
989 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
990 * @param tc scheduler context
993 neighbour_timeout_task (void *cls,
994 const struct GNUNET_SCHEDULER_TaskContext *tc)
996 struct NeighbourMapEntry *n = cls;
998 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1000 GNUNET_STATISTICS_update (GST_stats,
1002 ("# peers disconnected due to timeout"), 1,
1004 disconnect_neighbour (n);
1009 * Send another keepalive message.
1011 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1012 * @param tc scheduler context
1015 neighbour_keepalive_task (void *cls,
1016 const struct GNUNET_SCHEDULER_TaskContext *tc)
1018 struct NeighbourMapEntry *n = cls;
1019 struct GNUNET_MessageHeader m;
1023 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1024 &neighbour_keepalive_task, n);
1026 GNUNET_assert (S_CONNECTED == n->state);
1027 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1029 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1030 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1033 ret = send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1034 UINT32_MAX /* priority */ ,
1035 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
1036 GNUNET_YES, NULL, NULL);
1038 n->expect_latency_response = GNUNET_NO;
1039 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero();
1040 if (ret != GNUNET_SYSERR)
1042 n->expect_latency_response = GNUNET_YES;
1043 n->keep_alive_sent = GNUNET_TIME_absolute_get();
1050 * Disconnect from the given neighbour.
1053 * @param key hash of neighbour's public key (not used)
1054 * @param value the 'struct NeighbourMapEntry' of the neighbour
1057 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1059 struct NeighbourMapEntry *n = value;
1062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1063 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1065 if (S_CONNECTED == n->state)
1066 GNUNET_STATISTICS_update (GST_stats,
1068 ("# peers disconnected due to global disconnect"),
1070 disconnect_neighbour (n);
1076 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1078 struct NeighbourMapEntry *n = cls;
1080 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1082 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1083 "ATS did not suggested address to connect to peer `%s'\n",
1084 GNUNET_i2s (&n->id));
1086 disconnect_neighbour (n);
1090 * Cleanup the neighbours subsystem.
1093 GST_neighbours_stop ()
1095 // This can happen during shutdown
1096 if (neighbours == NULL)
1101 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1103 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1104 // GNUNET_assert (neighbours_connected == 0);
1106 callback_cls = NULL;
1107 connect_notify_cb = NULL;
1108 disconnect_notify_cb = NULL;
1111 struct ContinutionContext
1113 struct GNUNET_HELLO_Address *address;
1115 struct Session *session;
1118 static void send_outbound_quota (const struct GNUNET_PeerIdentity *target, struct GNUNET_BANDWIDTH_Value32NBO quota)
1120 struct QuotaSetMessage q_msg;
1122 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
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) ||
1156 (n->state == S_DISCONNECT))
1158 GNUNET_HELLO_address_free (cc->address);
1163 if ((GNUNET_YES == success) &&
1164 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1166 change_state (n, S_CONNECT_SENT);
1167 GNUNET_HELLO_address_free (cc->address);
1172 if ((GNUNET_NO == success) &&
1173 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1176 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1177 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1178 GNUNET_i2s (&n->id),
1179 GST_plugins_a2s (n->address),
1182 change_state (n, S_NOT_CONNECTED);
1183 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1184 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1186 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1188 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1190 GNUNET_HELLO_address_free (cc->address);
1196 * We tried to switch addresses with an peer already connected. If it failed,
1197 * we should tell ATS to not use this address anymore (until it is re-validated).
1199 * @param cls the 'struct NeighbourMapEntry'
1200 * @param target peer to send the message to
1201 * @param success GNUNET_OK on success
1204 send_switch_address_continuation (void *cls,
1205 const struct GNUNET_PeerIdentity *target,
1208 struct ContinutionContext * cc = cls;
1209 struct NeighbourMapEntry *n;
1211 if (neighbours == NULL)
1213 GNUNET_HELLO_address_free (cc->address);
1215 return; /* neighbour is going away */
1218 n = lookup_neighbour(&cc->address->peer);
1219 if ((n == NULL) || (is_disconnecting (n)))
1221 GNUNET_HELLO_address_free (cc->address);
1223 return; /* neighbour is going away */
1226 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1227 if (GNUNET_YES != success)
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "Failed to switch connected peer `%s' to address '%s' session %X, asking ATS for new address \n",
1232 GNUNET_i2s (&n->id),
1233 GST_plugins_a2s (n->address), n->session);
1235 GNUNET_assert (strlen(cc->address->transport_name) > 0);
1236 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1238 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1239 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1241 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1243 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1244 GNUNET_HELLO_address_free (cc->address);
1248 /* Tell ATS that switching addresses was successful */
1251 if (n->address_state == FRESH)
1253 GST_validation_set_address_use (&n->id,
1257 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1258 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1259 n->address_state = USED;
1262 case S_FAST_RECONNECT:
1264 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1265 "Successful fast reconnect to peer `%s'\n", GNUNET_i2s (&n->id));
1267 change_state (n, S_CONNECTED);
1268 neighbours_connected++;
1269 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1272 if (n->address_state == FRESH)
1274 GST_validation_set_address_use (&n->id,
1278 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1279 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1280 n->address_state = USED;
1283 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1284 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1286 /* Updating quotas */
1287 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1288 send_outbound_quota(target, n->bandwidth_out);
1293 GNUNET_HELLO_address_free (cc->address);
1299 * We tried to send a SESSION_CONNECT message to another peer. If this
1300 * succeeded, we change the state. If it failed, we should tell
1301 * ATS to not use this address anymore (until it is re-validated).
1303 * @param cls the 'struct NeighbourMapEntry'
1304 * @param target peer to send the message to
1305 * @param success GNUNET_OK on success
1308 send_connect_ack_continuation (void *cls,
1309 const struct GNUNET_PeerIdentity *target,
1312 struct ContinutionContext * cc = cls;
1313 struct NeighbourMapEntry *n;
1315 if (neighbours == NULL)
1317 GNUNET_HELLO_address_free (cc->address);
1319 return; /* neighbour is going away */
1322 n = lookup_neighbour(&cc->address->peer);
1323 if ((n == NULL) || (is_disconnecting (n)))
1325 GNUNET_HELLO_address_free (cc->address);
1327 return; /* neighbour is going away */
1330 if (GNUNET_YES == success)
1332 GNUNET_HELLO_address_free (cc->address);
1334 return; /* sending successful */
1337 /* sending failed, ask for next address */
1339 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1340 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1341 GNUNET_i2s (&n->id),
1342 GST_plugins_a2s (n->address),
1345 change_state (n, S_NOT_CONNECTED);
1346 GNUNET_assert (strlen(cc->address->transport_name) > 0);
1347 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1349 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1350 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1352 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1354 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1355 GNUNET_HELLO_address_free (cc->address);
1361 * For an existing neighbour record, set the active connection to
1362 * the given address.
1364 * @param peer identity of the peer to switch the address for
1365 * @param address address of the other peer, NULL if other peer
1367 * @param session session to use (or NULL)
1368 * @param ats performance data
1369 * @param ats_count number of entries in ats
1370 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1371 * connection is not up (yet)
1374 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1375 const struct GNUNET_HELLO_Address *address,
1376 struct Session *session,
1377 const struct GNUNET_ATS_Information *ats,
1379 struct GNUNET_BANDWIDTH_Value32NBO
1381 struct GNUNET_BANDWIDTH_Value32NBO
1384 struct NeighbourMapEntry *n;
1385 struct SessionConnectMessage connect_msg;
1386 struct ContinutionContext * cc;
1390 if (neighbours == NULL)
1392 /* This can happen during shutdown */
1395 n = lookup_neighbour (peer);
1398 if (n->state == S_DISCONNECT)
1400 /* We are disconnecting, nothing to do here */
1403 GNUNET_assert (address->transport_name != NULL);
1404 if ( (session == NULL) && (0 == address->address_length) )
1406 GNUNET_break_op (0);
1407 /* FIXME: is this actually possible? When does this happen? */
1408 if (strlen(address->transport_name) > 0)
1409 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1410 GNUNET_ATS_suggest_address (GST_ats, peer);
1414 /* checks successful and neighbour != NULL */
1416 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1417 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1418 GST_plugins_a2s (address),
1421 print_state(n->state));
1423 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1425 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1426 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1428 /* do not switch addresses just update quotas */
1429 if ( (n->state == S_CONNECTED) &&
1430 (NULL != n->address) &&
1431 (0 == GNUNET_HELLO_address_cmp (address,
1433 (n->session == session) )
1435 n->bandwidth_in = bandwidth_in;
1436 n->bandwidth_out = bandwidth_out;
1437 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1438 send_outbound_quota(peer, n->bandwidth_out);
1441 if (n->state == S_CONNECTED)
1443 /* mark old address as no longer used */
1444 GNUNET_assert (NULL != n->address);
1445 if (n->address_state == USED)
1447 GST_validation_set_address_use (&n->id,
1451 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1452 n->address_state = UNUSED;
1457 /* set new address */
1458 if (NULL != n->address)
1459 GNUNET_HELLO_address_free (n->address);
1460 n->address = GNUNET_HELLO_address_copy (address);
1461 n->address_state = FRESH;
1462 n->session = session;
1463 n->bandwidth_in = bandwidth_in;
1464 n->bandwidth_out = bandwidth_out;
1465 GNUNET_SCHEDULER_cancel (n->timeout_task);
1467 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1468 &neighbour_timeout_task, n);
1471 case S_NOT_CONNECTED:
1472 case S_CONNECT_SENT:
1473 msg_len = sizeof (struct SessionConnectMessage);
1474 connect_msg.header.size = htons (msg_len);
1475 connect_msg.header.type =
1476 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1477 connect_msg.reserved = htonl (0);
1478 connect_msg.timestamp =
1479 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 (peer, (const char *) &connect_msg, msg_len,
1486 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1487 address, GNUNET_YES,
1488 &send_connect_continuation,
1491 case S_CONNECT_RECV:
1492 /* We received a CONNECT message and asked ATS for an address */
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_ACK);
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 (&n->id, (const void *) &connect_msg, msg_len,
1505 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1506 address, GNUNET_YES,
1507 &send_connect_ack_continuation, cc);
1510 case S_FAST_RECONNECT:
1511 /* connected peer is switching addresses or tries fast reconnect*/
1512 msg_len = sizeof (struct SessionConnectMessage);
1513 connect_msg.header.size = htons (msg_len);
1514 connect_msg.header.type =
1515 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1516 connect_msg.reserved = htonl (0);
1517 connect_msg.timestamp =
1518 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1519 cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1520 cc->session = session;
1521 cc->address = GNUNET_HELLO_address_copy (address);
1523 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1524 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1525 address, GNUNET_YES,
1526 &send_switch_address_continuation, cc);
1527 if (ret == GNUNET_SYSERR)
1529 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1530 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1532 GST_plugins_a2s (address), session);
1536 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1537 "Invalid connection state to switch addresses %u \n", n->state);
1538 GNUNET_break_op (0);
1545 * Obtain current latency information for the given neighbour.
1548 * @return observed latency of the address, FOREVER if the address was
1549 * never successfully validated
1551 struct GNUNET_TIME_Relative
1552 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1554 struct NeighbourMapEntry *n;
1556 n = lookup_neighbour (peer);
1558 ( (n->address == NULL) && (n->session == NULL) ) )
1559 return GNUNET_TIME_UNIT_FOREVER_REL;
1566 * Create an entry in the neighbour map for the given peer
1568 * @param peer peer to create an entry for
1569 * @return new neighbour map entry
1571 static struct NeighbourMapEntry *
1572 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1574 struct NeighbourMapEntry *n;
1577 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1578 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1580 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1582 n->state = S_NOT_CONNECTED;
1583 n->latency = GNUNET_TIME_relative_get_forever();
1584 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1585 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1586 MAX_BANDWIDTH_CARRY_S);
1588 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1589 &neighbour_timeout_task, n);
1590 GNUNET_assert (GNUNET_OK ==
1591 GNUNET_CONTAINER_multihashmap_put (neighbours,
1592 &n->id.hashPubKey, n,
1593 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1599 * Try to create a connection to the given target (eventually).
1601 * @param target peer to try to connect to
1604 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1606 struct NeighbourMapEntry *n;
1608 // This can happen during shutdown
1609 if (neighbours == NULL)
1614 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1615 GNUNET_i2s (target));
1618 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1623 n = lookup_neighbour (target);
1627 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1628 return; /* already connecting or connected */
1629 if (is_disconnecting (n))
1630 change_state (n, S_NOT_CONNECTED);
1635 n = setup_neighbour (target);
1637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1638 "Asking ATS for suggested address to connect to peer `%s'\n",
1639 GNUNET_i2s (&n->id));
1642 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1646 * Test if we're connected to the given peer.
1648 * @param target peer to test
1649 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1652 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1654 struct NeighbourMapEntry *n;
1656 // This can happen during shutdown
1657 if (neighbours == NULL)
1662 n = lookup_neighbour (target);
1664 if ((NULL == n) || (S_CONNECTED != n->state))
1665 return GNUNET_NO; /* not connected */
1670 * A session was terminated. Take note.
1672 * @param peer identity of the peer where the session died
1673 * @param session session that is gone
1676 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1677 struct Session *session)
1679 struct NeighbourMapEntry *n;
1681 if (neighbours == NULL)
1683 /* This can happen during shutdown */
1688 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Session %X to peer `%s' ended \n",
1689 session, GNUNET_i2s (peer));
1692 n = lookup_neighbour (peer);
1695 if (session != n->session)
1696 return; /* doesn't affect us */
1697 if (n->state == S_CONNECTED)
1699 if (n->address_state == USED)
1701 GST_validation_set_address_use (&n->id,
1705 GNUNET_ATS_address_in_use (GST_ats,n->address, n->session, GNUNET_NO);
1706 n->address_state = UNUSED;
1710 if (NULL != n->address)
1712 GNUNET_HELLO_address_free (n->address);
1717 /* not connected anymore anyway, shouldn't matter */
1718 if (S_CONNECTED != n->state)
1721 /* connected, try fast reconnect */
1723 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1724 "Trying fast reconnect to peer `%s'\n", GNUNET_i2s (peer));
1726 change_state (n, S_FAST_RECONNECT);
1727 GNUNET_assert (neighbours_connected > 0);
1728 neighbours_connected--;
1730 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1732 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1733 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1736 /* We are connected, so ask ATS to switch addresses */
1737 GNUNET_SCHEDULER_cancel (n->timeout_task);
1739 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1740 &neighbour_timeout_task, n);
1741 /* try QUICKLY to re-establish a connection, reduce timeout! */
1742 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1743 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1745 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1747 GNUNET_ATS_suggest_address (GST_ats, peer);
1752 * Transmit a message to the given target using the active connection.
1754 * @param target destination
1755 * @param msg message to send
1756 * @param msg_size number of bytes in msg
1757 * @param timeout when to fail with timeout
1758 * @param cont function to call when done
1759 * @param cont_cls closure for 'cont'
1762 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1763 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1764 GST_NeighbourSendContinuation cont, void *cont_cls)
1766 struct NeighbourMapEntry *n;
1767 struct MessageQueue *mq;
1769 // This can happen during shutdown
1770 if (neighbours == NULL)
1775 n = lookup_neighbour (target);
1776 if ((n == NULL) || (!is_connected (n)))
1778 GNUNET_STATISTICS_update (GST_stats,
1780 ("# messages not sent (no such peer or not connected)"),
1784 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1785 "Could not send message to peer `%s': unknown neighbour",
1786 GNUNET_i2s (target));
1787 else if (!is_connected (n))
1788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1789 "Could not send message to peer `%s': not connected\n",
1790 GNUNET_i2s (target));
1793 cont (cont_cls, GNUNET_SYSERR);
1797 if ((n->session == NULL) && (n->address == NULL) )
1799 GNUNET_STATISTICS_update (GST_stats,
1801 ("# messages not sent (no such peer or not connected)"),
1804 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1805 "Could not send message to peer `%s': no address available\n",
1806 GNUNET_i2s (target));
1810 cont (cont_cls, GNUNET_SYSERR);
1814 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1815 GNUNET_STATISTICS_update (GST_stats,
1817 ("# bytes in message queue for other peers"),
1818 msg_size, GNUNET_NO);
1819 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1821 mq->cont_cls = cont_cls;
1822 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1823 memcpy (&mq[1], msg, msg_size);
1824 mq->message_buf = (const char *) &mq[1];
1825 mq->message_buf_size = msg_size;
1826 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1827 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1829 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1830 (NULL == n->is_active))
1831 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1836 * We have received a message from the given sender. How long should
1837 * we delay before receiving more? (Also used to keep the peer marked
1840 * @param sender sender of the message
1841 * @param size size of the message
1842 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1843 * GNUNET_NO if the neighbour is not connected or violates the quota,
1844 * GNUNET_SYSERR if the connection is not fully up yet
1845 * @return how long to wait before reading more from this sender
1847 struct GNUNET_TIME_Relative
1848 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1849 *sender, ssize_t size, int *do_forward)
1851 struct NeighbourMapEntry *n;
1852 struct GNUNET_TIME_Relative ret;
1854 // This can happen during shutdown
1855 if (neighbours == NULL)
1857 return GNUNET_TIME_UNIT_FOREVER_REL;
1860 n = lookup_neighbour (sender);
1863 GST_neighbours_try_connect (sender);
1864 n = lookup_neighbour (sender);
1867 GNUNET_STATISTICS_update (GST_stats,
1869 ("# messages discarded due to lack of neighbour record"),
1871 *do_forward = GNUNET_NO;
1872 return GNUNET_TIME_UNIT_ZERO;
1875 if (!is_connected (n))
1877 *do_forward = GNUNET_SYSERR;
1878 return GNUNET_TIME_UNIT_ZERO;
1880 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1882 n->quota_violation_count++;
1884 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1885 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1886 n->in_tracker.available_bytes_per_s__,
1887 n->quota_violation_count);
1889 /* Discount 32k per violation */
1890 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1894 if (n->quota_violation_count > 0)
1896 /* try to add 32k back */
1897 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1898 n->quota_violation_count--;
1901 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1903 GNUNET_STATISTICS_update (GST_stats,
1905 ("# bandwidth quota violations by other peers"),
1907 *do_forward = GNUNET_NO;
1908 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1910 *do_forward = GNUNET_YES;
1911 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1912 if (ret.rel_value > 0)
1915 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1916 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1917 (unsigned long long) n->in_tracker.
1918 consumption_since_last_update__,
1919 (unsigned int) n->in_tracker.available_bytes_per_s__,
1920 (unsigned long long) ret.rel_value);
1922 GNUNET_STATISTICS_update (GST_stats,
1923 gettext_noop ("# ms throttling suggested"),
1924 (int64_t) ret.rel_value, GNUNET_NO);
1931 * Keep the connection to the given neighbour alive longer,
1932 * we received a KEEPALIVE (or equivalent).
1934 * @param neighbour neighbour to keep alive
1937 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1939 struct NeighbourMapEntry *n;
1941 // This can happen during shutdown
1942 if (neighbours == NULL)
1947 n = lookup_neighbour (neighbour);
1950 GNUNET_STATISTICS_update (GST_stats,
1952 ("# KEEPALIVE messages discarded (not connected)"),
1956 GNUNET_SCHEDULER_cancel (n->timeout_task);
1958 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1959 &neighbour_timeout_task, n);
1961 /* send reply to measure latency */
1962 if (S_CONNECTED != n->state)
1965 struct GNUNET_MessageHeader m;
1966 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1967 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1969 send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1970 UINT32_MAX /* priority */ ,
1971 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
1972 GNUNET_YES, NULL, NULL);
1976 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
1979 * @param neighbour neighbour to keep alive
1980 * @param ats performance data
1981 * @param ats_count number of entries in ats
1984 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1985 const struct GNUNET_ATS_Information * ats,
1988 struct NeighbourMapEntry *n;
1989 struct GNUNET_ATS_Information * ats_new;
1992 if (neighbours == NULL)
1994 // This can happen during shutdown
1998 n = lookup_neighbour (neighbour);
2001 GNUNET_STATISTICS_update (GST_stats,
2003 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2007 if (n->expect_latency_response != GNUNET_YES)
2009 GNUNET_STATISTICS_update (GST_stats,
2011 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2015 n->expect_latency_response = GNUNET_NO;
2017 GNUNET_assert (n->keep_alive_sent.abs_value != GNUNET_TIME_absolute_get_zero().abs_value);
2018 n->latency = GNUNET_TIME_absolute_get_difference(n->keep_alive_sent, GNUNET_TIME_absolute_get());
2020 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2021 "Latency for peer `%s' is %llu ms\n",
2022 GNUNET_i2s (&n->id), n->latency.rel_value);
2026 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever().rel_value)
2028 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2032 ats_new = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2033 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2036 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2037 if (n->latency.rel_value > UINT32_MAX)
2038 latency = UINT32_MAX;
2040 latency = n->latency.rel_value;
2041 ats_new[ats_count].value = htonl (latency);
2043 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new, ats_count + 1);
2044 GNUNET_free (ats_new);
2050 * Change the incoming quota for the given peer.
2052 * @param neighbour identity of peer to change qutoa for
2053 * @param quota new quota
2056 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2057 struct GNUNET_BANDWIDTH_Value32NBO quota)
2059 struct NeighbourMapEntry *n;
2061 // This can happen during shutdown
2062 if (neighbours == NULL)
2067 n = lookup_neighbour (neighbour);
2070 GNUNET_STATISTICS_update (GST_stats,
2072 ("# SET QUOTA messages ignored (no such peer)"),
2077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2078 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2079 ntohl (quota.value__), GNUNET_i2s (&n->id));
2081 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2082 if (0 != ntohl (quota.value__))
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2086 GNUNET_i2s (&n->id), "SET_QUOTA");
2088 if (is_connected (n))
2089 GNUNET_STATISTICS_update (GST_stats,
2090 gettext_noop ("# disconnects due to quota of 0"),
2092 disconnect_neighbour (n);
2097 * Closure for the neighbours_iterate function.
2099 struct IteratorContext
2102 * Function to call on each connected neighbour.
2104 GST_NeighbourIterator cb;
2114 * Call the callback from the closure for each connected neighbour.
2116 * @param cls the 'struct IteratorContext'
2117 * @param key the hash of the public key of the neighbour
2118 * @param value the 'struct NeighbourMapEntry'
2119 * @return GNUNET_OK (continue to iterate)
2122 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2124 struct IteratorContext *ic = cls;
2125 struct NeighbourMapEntry *n = value;
2127 if (!is_connected (n))
2130 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2136 * Iterate over all connected neighbours.
2138 * @param cb function to call
2139 * @param cb_cls closure for cb
2142 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2144 struct IteratorContext ic;
2146 // This can happen during shutdown
2147 if (neighbours == NULL)
2154 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2158 * If we have an active connection to the given target, it must be shutdown.
2160 * @param target peer to disconnect from
2163 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2165 struct NeighbourMapEntry *n;
2167 // This can happen during shutdown
2168 if (neighbours == NULL)
2173 n = lookup_neighbour (target);
2175 return; /* not active */
2176 disconnect_neighbour (n);
2181 * We received a disconnect message from the given peer,
2182 * validate and process.
2184 * @param peer sender of the message
2185 * @param msg the disconnect message
2188 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2190 const struct GNUNET_MessageHeader
2193 struct NeighbourMapEntry *n;
2194 const struct SessionDisconnectMessage *sdm;
2198 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2199 "Received DISCONNECT message from peer `%s'\n",
2203 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2205 // GNUNET_break_op (0);
2206 GNUNET_STATISTICS_update (GST_stats,
2208 ("# disconnect messages ignored (old format)"), 1,
2212 sdm = (const struct SessionDisconnectMessage *) msg;
2213 n = lookup_neighbour (peer);
2215 return; /* gone already */
2216 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2217 n->connect_ts.abs_value)
2219 GNUNET_STATISTICS_update (GST_stats,
2221 ("# disconnect messages ignored (timestamp)"), 1,
2225 GNUNET_CRYPTO_hash (&sdm->public_key,
2226 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2228 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2230 GNUNET_break_op (0);
2233 if (ntohl (sdm->purpose.size) !=
2234 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2235 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2236 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2238 GNUNET_break_op (0);
2242 GNUNET_CRYPTO_rsa_verify
2243 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2244 &sdm->signature, &sdm->public_key))
2246 GNUNET_break_op (0);
2249 GST_neighbours_force_disconnect (peer);
2254 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2255 * Consider switching to it.
2257 * @param message possibly a 'struct SessionConnectMessage' (check format)
2258 * @param peer identity of the peer to switch the address for
2259 * @param address address of the other peer, NULL if other peer
2261 * @param session session to use (or NULL)
2262 * @param ats performance data
2263 * @param ats_count number of entries in ats
2266 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2267 const struct GNUNET_PeerIdentity *peer,
2268 const struct GNUNET_HELLO_Address *address,
2269 struct Session *session,
2270 const struct GNUNET_ATS_Information *ats,
2273 const struct SessionConnectMessage *scm;
2274 struct GNUNET_MessageHeader msg;
2275 struct NeighbourMapEntry *n;
2280 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2281 "Received CONNECT_ACK message from peer `%s'\n",
2285 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2287 GNUNET_break_op (0);
2290 scm = (const struct SessionConnectMessage *) message;
2291 GNUNET_break_op (ntohl (scm->reserved) == 0);
2292 n = lookup_neighbour (peer);
2295 /* we did not send 'CONNECT' -- at least not recently */
2296 GNUNET_STATISTICS_update (GST_stats,
2297 gettext_noop ("# unexpected CONNECT_ACK messages (no peer)"), 1,
2304 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2306 * We also received an CONNECT message, switched from SENDT to RECV and
2307 * ATS already suggested us an address after a successful blacklist check
2309 if ((n->state != S_CONNECT_SENT) && ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2311 GNUNET_STATISTICS_update (GST_stats,
2312 gettext_noop ("# unexpected CONNECT_ACK messages"), 1,
2317 change_state (n, S_CONNECTED);
2319 if (NULL != session)
2321 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2323 "Giving ATS session %p of plugin %s for peer %s\n",
2324 session, address->transport_name, GNUNET_i2s (peer));
2326 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2327 GNUNET_assert (NULL != n->address);
2328 if (n->address_state == FRESH)
2330 GST_validation_set_address_use (&n->id,
2334 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2335 n->address_state = USED;
2338 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2340 /* send ACK (ACK) */
2341 msg_len = sizeof (msg);
2342 msg.size = htons (msg_len);
2343 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2346 send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2347 GNUNET_TIME_UNIT_FOREVER_REL, n->session,
2348 n->address, GNUNET_YES, NULL,
2351 if (ret == GNUNET_SYSERR)
2352 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2353 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2354 GNUNET_i2s (&n->id),
2355 GST_plugins_a2s (n->address), n->session);
2358 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2359 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2361 neighbours_connected++;
2362 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2365 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2366 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2367 GNUNET_i2s (&n->id),
2368 GST_plugins_a2s (n->address), n->session,
2371 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2372 send_outbound_quota(peer, n->bandwidth_out);
2378 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2379 const struct GNUNET_PeerIdentity *peer,
2380 const struct GNUNET_HELLO_Address *address,
2381 struct Session *session,
2382 const struct GNUNET_ATS_Information *ats,
2385 struct NeighbourMapEntry *n;
2388 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2389 "Received ACK message from peer `%s'\n",
2393 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2395 GNUNET_break_op (0);
2398 n = lookup_neighbour (peer);
2401 send_disconnect (peer, address,
2406 if (S_CONNECTED == n->state)
2408 if (!is_connecting(n))
2410 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# unexpected ACK messages"), 1,
2414 change_state (n, S_CONNECTED);
2415 if (NULL != session)
2416 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2418 "Giving ATS session %p of plugin %s for peer %s\n",
2419 session, address->transport_name, GNUNET_i2s (peer));
2420 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2421 GNUNET_assert (n->address != NULL);
2422 if (n->address_state == FRESH)
2424 GST_validation_set_address_use (&n->id,
2428 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2429 n->address_state = USED;
2432 neighbours_connected++;
2433 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2436 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2437 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2438 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2440 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2441 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2442 GNUNET_i2s (&n->id),
2443 GST_plugins_a2s (n->address), n->session,
2446 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2447 send_outbound_quota(peer, n->bandwidth_out);
2450 struct BlackListCheckContext
2452 struct GNUNET_ATS_Information *ats;
2456 struct Session *session;
2458 struct GNUNET_HELLO_Address *address;
2460 struct GNUNET_TIME_Absolute ts;
2465 handle_connect_blacklist_cont (void *cls,
2466 const struct GNUNET_PeerIdentity *peer,
2469 struct NeighbourMapEntry *n;
2470 struct BlackListCheckContext *bcc = cls;
2473 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2474 "Blacklist check due to CONNECT message: `%s'\n",
2476 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2480 if (GNUNET_OK != result)
2482 GNUNET_HELLO_address_free (bcc->address);
2487 n = lookup_neighbour (peer);
2489 n = setup_neighbour (peer);
2491 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2493 if (NULL != bcc->session)
2494 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2496 "Giving ATS session %p of address `%s' for peer %s\n",
2498 GST_plugins_a2s (bcc->address),
2500 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2502 GNUNET_ATS_address_update (GST_ats,
2507 n->connect_ts = bcc->ts;
2510 GNUNET_HELLO_address_free (bcc->address);
2513 if (n->state != S_CONNECT_RECV)
2514 change_state (n, S_CONNECT_RECV);
2517 /* Ask ATS for an address to connect via that address */
2518 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2519 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2521 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2523 GNUNET_ATS_suggest_address (GST_ats, peer);
2527 * We received a 'SESSION_CONNECT' message from the other peer.
2528 * Consider switching to it.
2530 * @param message possibly a 'struct SessionConnectMessage' (check format)
2531 * @param peer identity of the peer to switch the address for
2532 * @param address address of the other peer, NULL if other peer
2534 * @param session session to use (or NULL)
2535 * @param ats performance data
2536 * @param ats_count number of entries in ats (excluding 0-termination)
2539 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2540 const struct GNUNET_PeerIdentity *peer,
2541 const struct GNUNET_HELLO_Address *address,
2542 struct Session *session,
2543 const struct GNUNET_ATS_Information *ats,
2546 const struct SessionConnectMessage *scm;
2547 struct BlackListCheckContext *bcc = NULL;
2548 struct NeighbourMapEntry *n;
2551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2552 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2555 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2557 GNUNET_break_op (0);
2561 scm = (const struct SessionConnectMessage *) message;
2562 GNUNET_break_op (ntohl (scm->reserved) == 0);
2564 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2566 n = lookup_neighbour (peer);
2567 if ( (n != NULL) && (S_CONNECTED == n->state) )
2569 /* connected peer switches addresses */
2574 /* we are not connected to this peer */
2575 /* do blacklist check */
2577 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2578 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2579 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2580 bcc->ats_count = ats_count + 1;
2581 bcc->address = GNUNET_HELLO_address_copy (address);
2582 bcc->session = session;
2583 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2584 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2585 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2586 bcc->ats[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2587 GST_blacklist_test_allowed (peer, address->transport_name, handle_connect_blacklist_cont,
2592 /* end of file gnunet-service-transport_neighbours.c */