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 success GNUNET_OK on success
1142 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1145 struct ContinutionContext * cc = cls;
1146 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1148 if (GNUNET_YES != success)
1150 GNUNET_assert (strlen(cc->address->transport_name) > 0);
1151 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1153 if ( (NULL == neighbours) ||
1155 (n->state == S_DISCONNECT))
1157 GNUNET_HELLO_address_free (cc->address);
1162 if ((GNUNET_YES == success) &&
1163 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1165 change_state (n, S_CONNECT_SENT);
1166 GNUNET_HELLO_address_free (cc->address);
1171 if ((GNUNET_NO == success) &&
1172 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1175 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1176 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1177 GNUNET_i2s (&n->id),
1178 GST_plugins_a2s (n->address),
1181 change_state (n, S_NOT_CONNECTED);
1182 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1183 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1185 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1187 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1189 GNUNET_HELLO_address_free (cc->address);
1194 * We tried to switch addresses with an peer already connected. If it failed,
1195 * we should tell ATS to not use this address anymore (until it is re-validated).
1197 * @param cls the 'struct NeighbourMapEntry'
1198 * @param success GNUNET_OK on success
1201 send_switch_address_continuation (void *cls,
1202 const struct GNUNET_PeerIdentity *target,
1205 struct ContinutionContext * cc = cls;
1206 struct NeighbourMapEntry *n;
1208 if (neighbours == NULL)
1210 GNUNET_HELLO_address_free (cc->address);
1212 return; /* neighbour is going away */
1215 n = lookup_neighbour(&cc->address->peer);
1216 if ((n == NULL) || (is_disconnecting (n)))
1218 GNUNET_HELLO_address_free (cc->address);
1220 return; /* neighbour is going away */
1223 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1224 if (GNUNET_YES != success)
1227 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1228 "Failed to switch connected peer `%s' to address '%s' session %X, asking ATS for new address \n",
1229 GNUNET_i2s (&n->id),
1230 GST_plugins_a2s (n->address), n->session);
1232 GNUNET_assert (strlen(cc->address->transport_name) > 0);
1233 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1235 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1236 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1238 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1240 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1241 GNUNET_HELLO_address_free (cc->address);
1245 /* Tell ATS that switching addresses was successful */
1248 if (n->address_state == FRESH)
1250 GST_validation_set_address_use (&n->id,
1254 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1255 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1256 n->address_state = USED;
1259 case S_FAST_RECONNECT:
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "Successful fast reconnect to peer `%s'\n", GNUNET_i2s (&n->id));
1264 change_state (n, S_CONNECTED);
1265 neighbours_connected++;
1266 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1269 if (n->address_state == FRESH)
1271 GST_validation_set_address_use (&n->id,
1275 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1276 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1277 n->address_state = USED;
1280 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1281 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1283 /* Updating quotas */
1284 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1285 send_outbound_quota(target, n->bandwidth_out);
1290 GNUNET_HELLO_address_free (cc->address);
1295 * We tried to send a SESSION_CONNECT message to another peer. If this
1296 * succeeded, we change the state. If it failed, we should tell
1297 * ATS to not use this address anymore (until it is re-validated).
1299 * @param cls the 'struct NeighbourMapEntry'
1300 * @param success GNUNET_OK on success
1303 send_connect_ack_continuation (void *cls,
1304 const struct GNUNET_PeerIdentity *target,
1307 struct ContinutionContext * cc = cls;
1308 struct NeighbourMapEntry *n;
1310 if (neighbours == NULL)
1312 GNUNET_HELLO_address_free (cc->address);
1314 return; /* neighbour is going away */
1317 n = lookup_neighbour(&cc->address->peer);
1318 if ((n == NULL) || (is_disconnecting (n)))
1320 GNUNET_HELLO_address_free (cc->address);
1322 return; /* neighbour is going away */
1325 if (GNUNET_YES == success)
1327 GNUNET_HELLO_address_free (cc->address);
1329 return; /* sending successful */
1332 /* sending failed, ask for next address */
1334 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1335 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1336 GNUNET_i2s (&n->id),
1337 GST_plugins_a2s (n->address),
1340 change_state (n, S_NOT_CONNECTED);
1341 GNUNET_assert (strlen(cc->address->transport_name) > 0);
1342 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1344 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1345 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1347 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1349 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1350 GNUNET_HELLO_address_free (cc->address);
1355 * For an existing neighbour record, set the active connection to
1356 * the given address.
1358 * @param peer identity of the peer to switch the address for
1359 * @param address address of the other peer, NULL if other peer
1361 * @param session session to use (or NULL)
1362 * @param ats performance data
1363 * @param ats_count number of entries in ats
1364 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1365 * connection is not up (yet)
1368 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1369 const struct GNUNET_HELLO_Address *address,
1370 struct Session *session,
1371 const struct GNUNET_ATS_Information *ats,
1373 struct GNUNET_BANDWIDTH_Value32NBO
1375 struct GNUNET_BANDWIDTH_Value32NBO
1378 struct NeighbourMapEntry *n;
1379 struct SessionConnectMessage connect_msg;
1380 struct ContinutionContext * cc;
1384 if (neighbours == NULL)
1386 /* This can happen during shutdown */
1389 n = lookup_neighbour (peer);
1392 if (n->state == S_DISCONNECT)
1394 /* We are disconnecting, nothing to do here */
1397 GNUNET_assert (address->transport_name != NULL);
1398 if ( (session == NULL) && (0 == address->address_length) )
1400 GNUNET_break_op (0);
1401 /* FIXME: is this actually possible? When does this happen? */
1402 if (strlen(address->transport_name) > 0)
1403 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1404 GNUNET_ATS_suggest_address (GST_ats, peer);
1408 /* checks successful and neighbour != NULL */
1410 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1411 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1412 GST_plugins_a2s (address),
1415 print_state(n->state));
1417 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1419 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1420 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1422 /* do not switch addresses just update quotas */
1423 if ( (n->state == S_CONNECTED) &&
1424 (NULL != n->address) &&
1425 (0 == GNUNET_HELLO_address_cmp (address,
1427 (n->session == session) )
1429 n->bandwidth_in = bandwidth_in;
1430 n->bandwidth_out = bandwidth_out;
1431 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1432 send_outbound_quota(peer, n->bandwidth_out);
1435 if (n->state == S_CONNECTED)
1437 /* mark old address as no longer used */
1438 GNUNET_assert (NULL != n->address);
1439 if (n->address_state == USED)
1441 GST_validation_set_address_use (&n->id,
1445 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1446 n->address_state = UNUSED;
1451 /* set new address */
1452 if (NULL != n->address)
1453 GNUNET_HELLO_address_free (n->address);
1454 n->address = GNUNET_HELLO_address_copy (address);
1455 n->address_state = FRESH;
1456 n->session = session;
1457 n->bandwidth_in = bandwidth_in;
1458 n->bandwidth_out = bandwidth_out;
1459 GNUNET_SCHEDULER_cancel (n->timeout_task);
1461 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1462 &neighbour_timeout_task, n);
1465 case S_NOT_CONNECTED:
1466 case S_CONNECT_SENT:
1467 msg_len = sizeof (struct SessionConnectMessage);
1468 connect_msg.header.size = htons (msg_len);
1469 connect_msg.header.type =
1470 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1471 connect_msg.reserved = htonl (0);
1472 connect_msg.timestamp =
1473 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1475 cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1476 cc->session = session;
1477 cc->address = GNUNET_HELLO_address_copy (address);
1479 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1480 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1481 address, GNUNET_YES,
1482 &send_connect_continuation,
1485 case S_CONNECT_RECV:
1486 /* We received a CONNECT message and asked ATS for an address */
1487 msg_len = sizeof (struct SessionConnectMessage);
1488 connect_msg.header.size = htons (msg_len);
1489 connect_msg.header.type =
1490 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1491 connect_msg.reserved = htonl (0);
1492 connect_msg.timestamp =
1493 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1494 cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1495 cc->session = session;
1496 cc->address = GNUNET_HELLO_address_copy (address);
1498 send_with_plugin (&n->id, (const void *) &connect_msg, msg_len,
1499 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1500 address, GNUNET_YES,
1501 &send_connect_ack_continuation, cc);
1504 case S_FAST_RECONNECT:
1505 /* connected peer is switching addresses or tries fast reconnect*/
1506 msg_len = sizeof (struct SessionConnectMessage);
1507 connect_msg.header.size = htons (msg_len);
1508 connect_msg.header.type =
1509 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1510 connect_msg.reserved = htonl (0);
1511 connect_msg.timestamp =
1512 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1513 cc = GNUNET_malloc(sizeof (struct ContinutionContext));
1514 cc->session = session;
1515 cc->address = GNUNET_HELLO_address_copy (address);
1517 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1518 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1519 address, GNUNET_YES,
1520 &send_switch_address_continuation, cc);
1521 if (ret == GNUNET_SYSERR)
1523 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1524 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1526 GST_plugins_a2s (address), session);
1530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1531 "Invalid connection state to switch addresses %u \n", n->state);
1532 GNUNET_break_op (0);
1539 * Obtain current latency information for the given neighbour.
1542 * @return observed latency of the address, FOREVER if the address was
1543 * never successfully validated
1545 struct GNUNET_TIME_Relative
1546 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1548 struct NeighbourMapEntry *n;
1550 n = lookup_neighbour (peer);
1552 ( (n->address == NULL) && (n->session == NULL) ) )
1553 return GNUNET_TIME_UNIT_FOREVER_REL;
1560 * Create an entry in the neighbour map for the given peer
1562 * @param peer peer to create an entry for
1563 * @return new neighbour map entry
1565 static struct NeighbourMapEntry *
1566 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1568 struct NeighbourMapEntry *n;
1571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1572 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1574 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1576 n->state = S_NOT_CONNECTED;
1577 n->latency = GNUNET_TIME_relative_get_forever();
1578 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1579 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1580 MAX_BANDWIDTH_CARRY_S);
1582 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1583 &neighbour_timeout_task, n);
1584 GNUNET_assert (GNUNET_OK ==
1585 GNUNET_CONTAINER_multihashmap_put (neighbours,
1586 &n->id.hashPubKey, n,
1587 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1593 * Try to create a connection to the given target (eventually).
1595 * @param target peer to try to connect to
1598 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1600 struct NeighbourMapEntry *n;
1602 // This can happen during shutdown
1603 if (neighbours == NULL)
1608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1609 GNUNET_i2s (target));
1612 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1617 n = lookup_neighbour (target);
1621 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1622 return; /* already connecting or connected */
1623 if (is_disconnecting (n))
1624 change_state (n, S_NOT_CONNECTED);
1629 n = setup_neighbour (target);
1631 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1632 "Asking ATS for suggested address to connect to peer `%s'\n",
1633 GNUNET_i2s (&n->id));
1636 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1640 * Test if we're connected to the given peer.
1642 * @param target peer to test
1643 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1646 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1648 struct NeighbourMapEntry *n;
1650 // This can happen during shutdown
1651 if (neighbours == NULL)
1656 n = lookup_neighbour (target);
1658 if ((NULL == n) || (S_CONNECTED != n->state))
1659 return GNUNET_NO; /* not connected */
1664 * A session was terminated. Take note.
1666 * @param peer identity of the peer where the session died
1667 * @param session session that is gone
1670 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1671 struct Session *session)
1673 struct NeighbourMapEntry *n;
1675 if (neighbours == NULL)
1677 /* This can happen during shutdown */
1682 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "Session %X to peer `%s' ended \n",
1683 session, GNUNET_i2s (peer));
1686 n = lookup_neighbour (peer);
1689 if (session != n->session)
1690 return; /* doesn't affect us */
1691 if (n->state == S_CONNECTED)
1693 if (n->address_state == USED)
1695 GST_validation_set_address_use (&n->id,
1699 GNUNET_ATS_address_in_use (GST_ats,n->address, n->session, GNUNET_NO);
1700 n->address_state = UNUSED;
1704 if (NULL != n->address)
1706 GNUNET_HELLO_address_free (n->address);
1711 /* not connected anymore anyway, shouldn't matter */
1712 if (S_CONNECTED != n->state)
1715 /* connected, try fast reconnect */
1717 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1718 "Trying fast reconnect to peer `%s'\n", GNUNET_i2s (peer));
1720 change_state (n, S_FAST_RECONNECT);
1721 GNUNET_assert (neighbours_connected > 0);
1722 neighbours_connected--;
1724 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1726 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1727 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1730 /* We are connected, so ask ATS to switch addresses */
1731 GNUNET_SCHEDULER_cancel (n->timeout_task);
1733 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1734 &neighbour_timeout_task, n);
1735 /* try QUICKLY to re-establish a connection, reduce timeout! */
1736 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1737 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1739 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1741 GNUNET_ATS_suggest_address (GST_ats, peer);
1746 * Transmit a message to the given target using the active connection.
1748 * @param target destination
1749 * @param msg message to send
1750 * @param msg_size number of bytes in msg
1751 * @param timeout when to fail with timeout
1752 * @param cont function to call when done
1753 * @param cont_cls closure for 'cont'
1756 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1757 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1758 GST_NeighbourSendContinuation cont, void *cont_cls)
1760 struct NeighbourMapEntry *n;
1761 struct MessageQueue *mq;
1763 // This can happen during shutdown
1764 if (neighbours == NULL)
1769 n = lookup_neighbour (target);
1770 if ((n == NULL) || (!is_connected (n)))
1772 GNUNET_STATISTICS_update (GST_stats,
1774 ("# messages not sent (no such peer or not connected)"),
1778 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1779 "Could not send message to peer `%s': unknown neighbour",
1780 GNUNET_i2s (target));
1781 else if (!is_connected (n))
1782 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1783 "Could not send message to peer `%s': not connected\n",
1784 GNUNET_i2s (target));
1787 cont (cont_cls, GNUNET_SYSERR);
1791 if ((n->session == NULL) && (n->address == NULL) )
1793 GNUNET_STATISTICS_update (GST_stats,
1795 ("# messages not sent (no such peer or not connected)"),
1798 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1799 "Could not send message to peer `%s': no address available\n",
1800 GNUNET_i2s (target));
1804 cont (cont_cls, GNUNET_SYSERR);
1808 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1809 GNUNET_STATISTICS_update (GST_stats,
1811 ("# bytes in message queue for other peers"),
1812 msg_size, GNUNET_NO);
1813 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1815 mq->cont_cls = cont_cls;
1816 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1817 memcpy (&mq[1], msg, msg_size);
1818 mq->message_buf = (const char *) &mq[1];
1819 mq->message_buf_size = msg_size;
1820 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1821 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1823 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1824 (NULL == n->is_active))
1825 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1830 * We have received a message from the given sender. How long should
1831 * we delay before receiving more? (Also used to keep the peer marked
1834 * @param sender sender of the message
1835 * @param size size of the message
1836 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1837 * GNUNET_NO if the neighbour is not connected or violates the quota,
1838 * GNUNET_SYSERR if the connection is not fully up yet
1839 * @return how long to wait before reading more from this sender
1841 struct GNUNET_TIME_Relative
1842 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1843 *sender, ssize_t size, int *do_forward)
1845 struct NeighbourMapEntry *n;
1846 struct GNUNET_TIME_Relative ret;
1848 // This can happen during shutdown
1849 if (neighbours == NULL)
1851 return GNUNET_TIME_UNIT_FOREVER_REL;
1854 n = lookup_neighbour (sender);
1857 GST_neighbours_try_connect (sender);
1858 n = lookup_neighbour (sender);
1861 GNUNET_STATISTICS_update (GST_stats,
1863 ("# messages discarded due to lack of neighbour record"),
1865 *do_forward = GNUNET_NO;
1866 return GNUNET_TIME_UNIT_ZERO;
1869 if (!is_connected (n))
1871 *do_forward = GNUNET_SYSERR;
1872 return GNUNET_TIME_UNIT_ZERO;
1874 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1876 n->quota_violation_count++;
1878 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1879 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1880 n->in_tracker.available_bytes_per_s__,
1881 n->quota_violation_count);
1883 /* Discount 32k per violation */
1884 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1888 if (n->quota_violation_count > 0)
1890 /* try to add 32k back */
1891 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1892 n->quota_violation_count--;
1895 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1897 GNUNET_STATISTICS_update (GST_stats,
1899 ("# bandwidth quota violations by other peers"),
1901 *do_forward = GNUNET_NO;
1902 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1904 *do_forward = GNUNET_YES;
1905 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1906 if (ret.rel_value > 0)
1909 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1910 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1911 (unsigned long long) n->in_tracker.
1912 consumption_since_last_update__,
1913 (unsigned int) n->in_tracker.available_bytes_per_s__,
1914 (unsigned long long) ret.rel_value);
1916 GNUNET_STATISTICS_update (GST_stats,
1917 gettext_noop ("# ms throttling suggested"),
1918 (int64_t) ret.rel_value, GNUNET_NO);
1925 * Keep the connection to the given neighbour alive longer,
1926 * we received a KEEPALIVE (or equivalent).
1928 * @param neighbour neighbour to keep alive
1931 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1933 struct NeighbourMapEntry *n;
1935 // This can happen during shutdown
1936 if (neighbours == NULL)
1941 n = lookup_neighbour (neighbour);
1944 GNUNET_STATISTICS_update (GST_stats,
1946 ("# KEEPALIVE messages discarded (not connected)"),
1950 GNUNET_SCHEDULER_cancel (n->timeout_task);
1952 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1953 &neighbour_timeout_task, n);
1955 /* send reply to measure latency */
1956 if (S_CONNECTED != n->state)
1959 struct GNUNET_MessageHeader m;
1960 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1961 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
1963 send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1964 UINT32_MAX /* priority */ ,
1965 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
1966 GNUNET_YES, NULL, NULL);
1970 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
1973 * @param neighbour neighbour to keep alive
1976 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
1977 const struct GNUNET_ATS_Information * ats,
1980 struct NeighbourMapEntry *n;
1981 struct GNUNET_ATS_Information * ats_new;
1984 if (neighbours == NULL)
1986 // This can happen during shutdown
1990 n = lookup_neighbour (neighbour);
1993 GNUNET_STATISTICS_update (GST_stats,
1995 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
1999 if (n->expect_latency_response != GNUNET_YES)
2001 GNUNET_STATISTICS_update (GST_stats,
2003 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2007 n->expect_latency_response = GNUNET_NO;
2009 GNUNET_assert (n->keep_alive_sent.abs_value != GNUNET_TIME_absolute_get_zero().abs_value);
2010 n->latency = GNUNET_TIME_absolute_get_difference(n->keep_alive_sent, GNUNET_TIME_absolute_get());
2012 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2013 "Latency for peer `%s' is %llu ms\n",
2014 GNUNET_i2s (&n->id), n->latency.rel_value);
2018 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever().rel_value)
2020 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2024 ats_new = GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2025 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2028 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2029 if (n->latency.rel_value > UINT32_MAX)
2030 latency = UINT32_MAX;
2032 latency = n->latency.rel_value;
2033 ats_new[ats_count].value = htonl (latency);
2035 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new, ats_count + 1);
2036 GNUNET_free (ats_new);
2042 * Change the incoming quota for the given peer.
2044 * @param neighbour identity of peer to change qutoa for
2045 * @param quota new quota
2048 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2049 struct GNUNET_BANDWIDTH_Value32NBO quota)
2051 struct NeighbourMapEntry *n;
2053 // This can happen during shutdown
2054 if (neighbours == NULL)
2059 n = lookup_neighbour (neighbour);
2062 GNUNET_STATISTICS_update (GST_stats,
2064 ("# SET QUOTA messages ignored (no such peer)"),
2069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2070 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2071 ntohl (quota.value__), GNUNET_i2s (&n->id));
2073 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2074 if (0 != ntohl (quota.value__))
2077 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2078 GNUNET_i2s (&n->id), "SET_QUOTA");
2080 if (is_connected (n))
2081 GNUNET_STATISTICS_update (GST_stats,
2082 gettext_noop ("# disconnects due to quota of 0"),
2084 disconnect_neighbour (n);
2089 * Closure for the neighbours_iterate function.
2091 struct IteratorContext
2094 * Function to call on each connected neighbour.
2096 GST_NeighbourIterator cb;
2106 * Call the callback from the closure for each connected neighbour.
2108 * @param cls the 'struct IteratorContext'
2109 * @param key the hash of the public key of the neighbour
2110 * @param value the 'struct NeighbourMapEntry'
2111 * @return GNUNET_OK (continue to iterate)
2114 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2116 struct IteratorContext *ic = cls;
2117 struct NeighbourMapEntry *n = value;
2119 if (!is_connected (n))
2122 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2128 * Iterate over all connected neighbours.
2130 * @param cb function to call
2131 * @param cb_cls closure for cb
2134 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2136 struct IteratorContext ic;
2138 // This can happen during shutdown
2139 if (neighbours == NULL)
2146 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2150 * If we have an active connection to the given target, it must be shutdown.
2152 * @param target peer to disconnect from
2155 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2157 struct NeighbourMapEntry *n;
2159 // This can happen during shutdown
2160 if (neighbours == NULL)
2165 n = lookup_neighbour (target);
2167 return; /* not active */
2168 disconnect_neighbour (n);
2173 * We received a disconnect message from the given peer,
2174 * validate and process.
2176 * @param peer sender of the message
2177 * @param msg the disconnect message
2180 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2182 const struct GNUNET_MessageHeader
2185 struct NeighbourMapEntry *n;
2186 const struct SessionDisconnectMessage *sdm;
2190 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2191 "Received DISCONNECT message from peer `%s'\n",
2195 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2197 // GNUNET_break_op (0);
2198 GNUNET_STATISTICS_update (GST_stats,
2200 ("# disconnect messages ignored (old format)"), 1,
2204 sdm = (const struct SessionDisconnectMessage *) msg;
2205 n = lookup_neighbour (peer);
2207 return; /* gone already */
2208 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2209 n->connect_ts.abs_value)
2211 GNUNET_STATISTICS_update (GST_stats,
2213 ("# disconnect messages ignored (timestamp)"), 1,
2217 GNUNET_CRYPTO_hash (&sdm->public_key,
2218 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2220 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2222 GNUNET_break_op (0);
2225 if (ntohl (sdm->purpose.size) !=
2226 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2227 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2228 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2230 GNUNET_break_op (0);
2234 GNUNET_CRYPTO_rsa_verify
2235 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2236 &sdm->signature, &sdm->public_key))
2238 GNUNET_break_op (0);
2241 GST_neighbours_force_disconnect (peer);
2246 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2247 * Consider switching to it.
2249 * @param message possibly a 'struct SessionConnectMessage' (check format)
2250 * @param peer identity of the peer to switch the address for
2251 * @param address address of the other peer, NULL if other peer
2253 * @param session session to use (or NULL)
2254 * @param ats performance data
2255 * @param ats_count number of entries in ats
2258 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2259 const struct GNUNET_PeerIdentity *peer,
2260 const struct GNUNET_HELLO_Address *address,
2261 struct Session *session,
2262 const struct GNUNET_ATS_Information *ats,
2265 const struct SessionConnectMessage *scm;
2266 struct GNUNET_MessageHeader msg;
2267 struct NeighbourMapEntry *n;
2272 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2273 "Received CONNECT_ACK message from peer `%s'\n",
2277 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2279 GNUNET_break_op (0);
2282 scm = (const struct SessionConnectMessage *) message;
2283 GNUNET_break_op (ntohl (scm->reserved) == 0);
2284 n = lookup_neighbour (peer);
2287 /* we did not send 'CONNECT' (how could we? no record for this peer!) */
2288 GNUNET_break_op (0);
2294 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2296 * We also received an CONNECT message, switched from SENDT to RECV and
2297 * ATS already suggested us an address after a successful blacklist check
2299 if ((n->state != S_CONNECT_SENT) && ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2301 GNUNET_STATISTICS_update (GST_stats,
2302 gettext_noop ("# unexpected CONNECT_ACK messages"), 1,
2307 change_state (n, S_CONNECTED);
2309 if (NULL != session)
2311 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2313 "Giving ATS session %p of plugin %s for peer %s\n",
2314 session, address->transport_name, GNUNET_i2s (peer));
2316 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2317 GNUNET_assert (NULL != n->address);
2318 if (n->address_state == FRESH)
2320 GST_validation_set_address_use (&n->id,
2324 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2325 n->address_state = USED;
2328 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2330 /* send ACK (ACK) */
2331 msg_len = sizeof (msg);
2332 msg.size = htons (msg_len);
2333 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2336 send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2337 GNUNET_TIME_UNIT_FOREVER_REL, n->session,
2338 n->address, GNUNET_YES, NULL,
2341 if (ret == GNUNET_SYSERR)
2342 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2343 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2344 GNUNET_i2s (&n->id),
2345 GST_plugins_a2s (n->address), n->session);
2348 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2349 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2351 neighbours_connected++;
2352 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2355 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2356 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2357 GNUNET_i2s (&n->id),
2358 GST_plugins_a2s (n->address), n->session,
2361 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2362 send_outbound_quota(peer, n->bandwidth_out);
2368 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2369 const struct GNUNET_PeerIdentity *peer,
2370 const struct GNUNET_HELLO_Address *address,
2371 struct Session *session,
2372 const struct GNUNET_ATS_Information *ats,
2375 struct NeighbourMapEntry *n;
2378 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2379 "Received ACK message from peer `%s'\n",
2383 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2385 GNUNET_break_op (0);
2388 n = lookup_neighbour (peer);
2391 send_disconnect (peer, address,
2396 if (S_CONNECTED == n->state)
2398 if (!is_connecting(n))
2400 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# unexpected ACK messages"), 1,
2404 change_state (n, S_CONNECTED);
2405 if (NULL != session)
2406 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2408 "Giving ATS session %p of plugin %s for peer %s\n",
2409 session, address->transport_name, GNUNET_i2s (peer));
2410 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2411 GNUNET_assert (n->address != NULL);
2412 if (n->address_state == FRESH)
2414 GST_validation_set_address_use (&n->id,
2418 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2419 n->address_state = USED;
2422 neighbours_connected++;
2423 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2426 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2427 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2428 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2430 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2431 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2432 GNUNET_i2s (&n->id),
2433 GST_plugins_a2s (n->address), n->session,
2436 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2437 send_outbound_quota(peer, n->bandwidth_out);
2440 struct BlackListCheckContext
2442 struct GNUNET_ATS_Information *ats;
2446 struct Session *session;
2448 struct GNUNET_HELLO_Address *address;
2450 struct GNUNET_TIME_Absolute ts;
2455 handle_connect_blacklist_cont (void *cls,
2456 const struct GNUNET_PeerIdentity *peer,
2459 struct NeighbourMapEntry *n;
2460 struct BlackListCheckContext *bcc = cls;
2463 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2464 "Blacklist check due to CONNECT message: `%s'\n",
2466 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2470 if (GNUNET_OK != result)
2472 GNUNET_HELLO_address_free (bcc->address);
2477 n = lookup_neighbour (peer);
2479 n = setup_neighbour (peer);
2481 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2483 if (NULL != bcc->session)
2484 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2486 "Giving ATS session %p of address `%s' for peer %s\n",
2488 GST_plugins_a2s (bcc->address),
2490 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2492 GNUNET_ATS_address_update (GST_ats,
2497 n->connect_ts = bcc->ts;
2500 GNUNET_HELLO_address_free (bcc->address);
2503 if (n->state != S_CONNECT_RECV)
2504 change_state (n, S_CONNECT_RECV);
2507 /* Ask ATS for an address to connect via that address */
2508 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2509 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2511 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2513 GNUNET_ATS_suggest_address (GST_ats, peer);
2517 * We received a 'SESSION_CONNECT' message from the other peer.
2518 * Consider switching to it.
2520 * @param message possibly a 'struct SessionConnectMessage' (check format)
2521 * @param peer identity of the peer to switch the address for
2522 * @param address address of the other peer, NULL if other peer
2524 * @param session session to use (or NULL)
2525 * @param ats performance data
2526 * @param ats_count number of entries in ats (excluding 0-termination)
2529 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2530 const struct GNUNET_PeerIdentity *peer,
2531 const struct GNUNET_HELLO_Address *address,
2532 struct Session *session,
2533 const struct GNUNET_ATS_Information *ats,
2536 const struct SessionConnectMessage *scm;
2537 struct BlackListCheckContext *bcc = NULL;
2538 struct NeighbourMapEntry *n;
2541 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2542 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2545 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2547 GNUNET_break_op (0);
2551 scm = (const struct SessionConnectMessage *) message;
2552 GNUNET_break_op (ntohl (scm->reserved) == 0);
2554 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2556 n = lookup_neighbour (peer);
2557 if ( (n != NULL) && (S_CONNECTED == n->state) )
2559 /* connected peer switches addresses */
2564 /* we are not connected to this peer */
2565 /* do blacklist check */
2567 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2568 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2569 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2570 bcc->ats_count = ats_count + 1;
2571 bcc->address = GNUNET_HELLO_address_copy (address);
2572 bcc->session = session;
2573 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2574 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2575 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2576 bcc->ats[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2577 GST_blacklist_test_allowed (peer, address->transport_name, handle_connect_blacklist_cont,
2582 /* end of file gnunet-service-transport_neighbours.c */