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)
66 #define TEST_NEW_CODE GNUNET_YES
69 * Entry in neighbours.
71 struct NeighbourMapEntry;
73 GNUNET_NETWORK_STRUCT_BEGIN
76 * Message a peer sends to another to indicate its
77 * preference for communicating via a particular
78 * session (and the desire to establish a real
81 struct SessionConnectMessage
84 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
86 struct GNUNET_MessageHeader header;
91 uint32_t reserved GNUNET_PACKED;
94 * Absolute time at the sender. Only the most recent connect
95 * message implies which session is preferred by the sender.
97 struct GNUNET_TIME_AbsoluteNBO timestamp;
102 struct SessionDisconnectMessage
105 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
107 struct GNUNET_MessageHeader header;
112 uint32_t reserved GNUNET_PACKED;
115 * Purpose of the signature. Extends over the timestamp.
116 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
118 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
121 * Absolute time at the sender. Only the most recent connect
122 * message implies which session is preferred by the sender.
124 struct GNUNET_TIME_AbsoluteNBO timestamp;
127 * Public key of the sender.
129 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
132 * Signature of the peer that sends us the disconnect. Only
133 * valid if the timestamp is AFTER the timestamp from the
134 * corresponding 'CONNECT' message.
136 struct GNUNET_CRYPTO_RsaSignature signature;
139 GNUNET_NETWORK_STRUCT_END
142 * For each neighbour we keep a list of messages
143 * that we still want to transmit to the neighbour.
149 * This is a doubly linked list.
151 struct MessageQueue *next;
154 * This is a doubly linked list.
156 struct MessageQueue *prev;
159 * Once this message is actively being transmitted, which
160 * neighbour is it associated with?
162 struct NeighbourMapEntry *n;
165 * Function to call once we're done.
167 GST_NeighbourSendContinuation cont;
175 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
176 * stuck together in memory. Allocated at the end of this struct.
178 const char *message_buf;
181 * Size of the message buf
183 size_t message_buf_size;
186 * At what time should we fail?
188 struct GNUNET_TIME_Absolute timeout;
196 * fresh peer or completely disconnected
201 * sent CONNECT message to other peer, waiting for CONNECT_ACK
206 * received CONNECT message to other peer, sending CONNECT_ACK
211 * received ACK or payload
216 * connection ended, fast reconnect
221 * Disconnect in progress
235 * Entry in neighbours.
237 struct NeighbourMapEntry
241 * Head of list of messages we would like to send to this peer;
242 * must contain at most one message per client.
244 struct MessageQueue *messages_head;
247 * Tail of list of messages we would like to send to this peer; must
248 * contain at most one message per client.
250 struct MessageQueue *messages_tail;
253 * Are we currently trying to send a message? If so, which one?
255 struct MessageQueue *is_active;
258 * Active session for communicating with the peer.
260 struct Session *session;
263 * Address we currently use.
265 struct GNUNET_HELLO_Address *address;
268 * Identity of this neighbour.
270 struct GNUNET_PeerIdentity id;
273 * ID of task scheduled to run when this peer is about to
274 * time out (will free resources associated with the peer).
276 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
279 * ID of task scheduled to send keepalives.
281 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
284 * ID of task scheduled to run when we should try transmitting
285 * the head of the message queue.
287 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
290 * Tracker for inbound bandwidth.
292 struct GNUNET_BANDWIDTH_Tracker in_tracker;
295 * Inbound bandwidth from ATS, activated when connection is up
297 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
300 * Inbound bandwidth from ATS, activated when connection is up
302 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
305 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
307 struct GNUNET_TIME_Absolute connect_ts;
310 * When did we sent the last keep-alive message?
312 struct GNUNET_TIME_Absolute keep_alive_sent;
315 * Latest calculated latency value
317 struct GNUNET_TIME_Relative latency;
321 * We asked ATS for a new address for this peer
323 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
326 * Task the resets the peer state after due to an pending
327 * unsuccessful connection setup
329 GNUNET_SCHEDULER_TaskIdentifier state_reset;
333 * How often has the other peer (recently) violated the inbound
334 * traffic limit? Incremented by 10 per violation, decremented by 1
335 * per non-violation (for each time interval).
337 unsigned int quota_violation_count;
341 * The current state of the peer
342 * Element of enum State
347 * Did we sent an KEEP_ALIVE message and are we expecting a response?
349 int expect_latency_response;
355 * All known neighbours and their HELLOs.
357 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
360 * Closure for connect_notify_cb and disconnect_notify_cb
362 static void *callback_cls;
365 * Function to call when we connected to a neighbour.
367 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
370 * Function to call when we disconnected from a neighbour.
372 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
375 * counter for connected neighbours
377 static int neighbours_connected;
380 * Lookup a neighbour entry in the neighbours hash map.
382 * @param pid identity of the peer to look up
383 * @return the entry, NULL if there is no existing record
385 static struct NeighbourMapEntry *
386 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
388 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
391 #define change_state(n, state, ...) change (n, state, __LINE__)
394 is_connecting (struct NeighbourMapEntry *n)
396 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
402 is_connected (struct NeighbourMapEntry *n)
404 if (n->state == S_CONNECTED)
410 is_disconnecting (struct NeighbourMapEntry *n)
412 if (n->state == S_DISCONNECT)
418 print_state (int state)
423 return "S_CONNECTED";
426 return "S_CONNECT_RECV";
429 return "S_CONNECT_SENT";
432 return "S_DISCONNECT";
434 case S_NOT_CONNECTED:
435 return "S_NOT_CONNECTED";
437 case S_FAST_RECONNECT:
438 return "S_FAST_RECONNECT";
448 change (struct NeighbourMapEntry *n, int state, int line);
451 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
455 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
457 struct NeighbourMapEntry *n = cls;
462 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
463 if (n->state == S_CONNECTED)
467 GNUNET_STATISTICS_update (GST_stats,
469 ("# failed connection attempts due to timeout"), 1,
473 /* resetting state */
474 n->state = S_NOT_CONNECTED;
476 /* destroying address */
477 if (n->address != NULL)
479 GNUNET_assert (strlen (n->address->transport_name) > 0);
480 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
483 /* request new address */
484 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
485 GNUNET_SCHEDULER_cancel (n->ats_suggest);
487 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
489 GNUNET_ATS_suggest_address (GST_ats, &n->id);
493 change (struct NeighbourMapEntry *n, int state, int line)
495 /* allowed transitions */
496 int allowed = GNUNET_NO;
500 case S_NOT_CONNECTED:
501 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
502 (state == S_DISCONNECT))
503 allowed = GNUNET_YES;
506 allowed = GNUNET_YES;
509 allowed = GNUNET_YES;
512 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
513 allowed = GNUNET_YES;
517 case S_FAST_RECONNECT:
518 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
519 allowed = GNUNET_YES;
525 if (allowed == GNUNET_NO)
527 char *old = GNUNET_strdup (print_state (n->state));
528 char *new = GNUNET_strdup (print_state (state));
530 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
531 "Illegal state transition from `%s' to `%s' in line %u \n", old,
536 return GNUNET_SYSERR;
540 char *old = GNUNET_strdup (print_state (n->state));
541 char *new = GNUNET_strdup (print_state (state));
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
544 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
545 GNUNET_i2s (&n->id), n, old, new, line);
554 case S_FAST_RECONNECT:
557 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
558 GNUNET_SCHEDULER_cancel (n->state_reset);
560 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
563 case S_NOT_CONNECTED:
565 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
568 char *old = GNUNET_strdup (print_state (n->state));
569 char *new = GNUNET_strdup (print_state (state));
571 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
572 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
573 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), old, new);
577 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
578 GNUNET_SCHEDULER_cancel (n->state_reset);
579 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
593 send_with_session (struct NeighbourMapEntry *n,
594 const char *msgbuf, size_t msgbuf_size,
596 struct GNUNET_TIME_Relative timeout,
597 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
599 struct GNUNET_TRANSPORT_PluginFunctions *papi;
600 size_t ret = GNUNET_SYSERR;
602 GNUNET_assert (n != NULL);
603 GNUNET_assert (n->session != NULL);
605 papi = GST_plugins_find (n->address->transport_name);
609 cont (cont_cls, &n->id, GNUNET_SYSERR);
610 return GNUNET_SYSERR;
613 ret = papi->send_with_session (papi->cls,
620 if ((ret == -1) && (cont != NULL))
621 cont (cont_cls, &n->id, GNUNET_SYSERR);
628 send_with_plugin (const struct GNUNET_PeerIdentity *target, const char *msgbuf,
629 size_t msgbuf_size, uint32_t priority,
630 struct GNUNET_TIME_Relative timeout, struct Session *session,
631 const struct GNUNET_HELLO_Address *address, int force_address,
632 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
635 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
636 "Call to depracted function send_with_plugin\n");
639 struct GNUNET_TRANSPORT_PluginFunctions *papi;
640 size_t ret = GNUNET_SYSERR;
642 /* FIXME : ats returns an address with all values 0 */
646 cont (cont_cls, target, GNUNET_SYSERR);
647 return GNUNET_SYSERR;
650 if ((session == NULL) && (address->address_length == 0))
653 cont (cont_cls, target, GNUNET_SYSERR);
654 return GNUNET_SYSERR;
657 papi = GST_plugins_find (address->transport_name);
661 cont (cont_cls, target, GNUNET_SYSERR);
662 return GNUNET_SYSERR;
666 papi->send (papi->cls, target, msgbuf, msgbuf_size, 0, timeout, session,
667 address->address, address->address_length, GNUNET_YES, cont,
673 cont (cont_cls, target, GNUNET_SYSERR);
680 * Task invoked to start a transmission to another peer.
682 * @param cls the 'struct NeighbourMapEntry'
683 * @param tc scheduler context
686 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
690 * We're done with our transmission attempt, continue processing.
692 * @param cls the 'struct MessageQueue' of the message
693 * @param receiver intended receiver
694 * @param success whether it worked or not
697 transmit_send_continuation (void *cls,
698 const struct GNUNET_PeerIdentity *receiver,
701 struct MessageQueue *mq = cls;
702 struct NeighbourMapEntry *n;
703 struct NeighbourMapEntry *tmp;
705 tmp = lookup_neighbour (receiver);
707 if ((NULL != n) && (tmp != NULL) && (tmp == n))
709 GNUNET_assert (n->is_active == mq);
711 if (success == GNUNET_YES)
713 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
714 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
718 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
719 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
720 (success == GNUNET_OK) ? "successful" : "FAILED");
722 if (NULL != mq->cont)
723 mq->cont (mq->cont_cls, success);
729 * Check the ready list for the given neighbour and if a plugin is
730 * ready for transmission (and if we have a message), do so!
732 * @param n target peer for which to transmit
735 try_transmission_to_peer (struct NeighbourMapEntry *n)
737 struct MessageQueue *mq;
738 struct GNUNET_TIME_Relative timeout;
741 if (n->is_active != NULL)
744 return; /* transmission already pending */
746 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
749 return; /* currently waiting for bandwidth */
751 while (NULL != (mq = n->messages_head))
753 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
754 if (timeout.rel_value > 0)
756 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
759 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
762 return; /* no more messages */
764 if (n->address == NULL)
767 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
768 GNUNET_i2s (&n->id));
770 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
771 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
772 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
773 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
777 if (GST_plugins_find (n->address->transport_name) == NULL)
782 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
786 if ((n->address->address_length == 0) && (n->session == NULL))
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
789 GNUNET_i2s (&n->id));
790 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
791 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
792 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
797 ret = send_with_session(n,
798 mq->message_buf, mq->message_buf_size,
800 &transmit_send_continuation, mq);
802 ret = send_with_plugin (&n->id,
803 mq->message_buf, mq->message_buf_size,
805 n->session, n->address, GNUNET_YES,
806 &transmit_send_continuation, mq);
811 /* failure, but 'send' would not call continuation in this case,
812 * so we need to do it here! */
813 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
820 * Task invoked to start a transmission to another peer.
822 * @param cls the 'struct NeighbourMapEntry'
823 * @param tc scheduler context
826 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
828 struct NeighbourMapEntry *n = cls;
830 GNUNET_assert (NULL != lookup_neighbour (&n->id));
831 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
832 try_transmission_to_peer (n);
837 * Initialize the neighbours subsystem.
839 * @param cls closure for callbacks
840 * @param connect_cb function to call if we connect to a peer
841 * @param disconnect_cb function to call if we disconnect from a peer
844 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
845 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
848 connect_notify_cb = connect_cb;
849 disconnect_notify_cb = disconnect_cb;
850 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
855 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Sending DISCONNECT message to peer `%4s': %i\n",
861 GNUNET_i2s (target), result);
867 send_disconnect (struct NeighbourMapEntry * n)
870 struct SessionDisconnectMessage disconnect_msg;
873 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
874 "Sending DISCONNECT message to peer `%4s'\n",
875 GNUNET_i2s (target));
878 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
879 disconnect_msg.header.type =
880 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
881 disconnect_msg.reserved = htonl (0);
882 disconnect_msg.purpose.size =
883 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
884 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
885 sizeof (struct GNUNET_TIME_AbsoluteNBO));
886 disconnect_msg.purpose.purpose =
887 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
888 disconnect_msg.timestamp =
889 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
890 disconnect_msg.public_key = GST_my_public_key;
891 GNUNET_assert (GNUNET_OK ==
892 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
893 &disconnect_msg.purpose,
894 &disconnect_msg.signature));
897 ret = send_with_session (n,
898 (const char *) &disconnect_msg, sizeof (disconnect_msg),
899 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
900 &send_disconnect_cont, NULL);
902 ret = send_with_plugin (&n->id,
903 (const char *) &disconnect_msg, sizeof (disconnect_msg),
904 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
905 n->session, n->address,
906 GNUNET_YES, &send_disconnect_cont, NULL);
909 if (ret == GNUNET_SYSERR)
910 return GNUNET_SYSERR;
912 GNUNET_STATISTICS_update (GST_stats,
914 ("# peers disconnected due to external request"), 1,
921 * Disconnect from the given neighbour, clean up the record.
923 * @param n neighbour to disconnect from
926 disconnect_neighbour (struct NeighbourMapEntry *n)
928 struct MessageQueue *mq;
931 previous_state = n->state;
933 if (is_disconnecting (n))
936 /* send DISCONNECT MESSAGE */
937 if (previous_state == S_CONNECTED)
939 if (GNUNET_OK == send_disconnect (n))
940 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
941 GNUNET_i2s (&n->id));
943 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
944 "Could not send DISCONNECT_MSG to `%s'\n",
945 GNUNET_i2s (&n->id));
948 change_state (n, S_DISCONNECT);
950 if (previous_state == S_CONNECTED)
952 GNUNET_assert (NULL != n->address);
953 if (n->address_state == USED)
955 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
956 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
957 n->address_state = UNUSED;
961 if (n->address != NULL)
963 struct GNUNET_TRANSPORT_PluginFunctions *papi;
965 papi = GST_plugins_find (n->address->transport_name);
967 papi->disconnect (papi->cls, &n->id);
969 while (NULL != (mq = n->messages_head))
971 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
972 if (NULL != mq->cont)
973 mq->cont (mq->cont_cls, GNUNET_SYSERR);
976 if (NULL != n->is_active)
978 n->is_active->n = NULL;
982 switch (previous_state)
985 GNUNET_assert (neighbours_connected > 0);
986 neighbours_connected--;
987 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
988 GNUNET_SCHEDULER_cancel (n->keepalive_task);
989 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
990 n->expect_latency_response = GNUNET_NO;
991 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
993 disconnect_notify_cb (callback_cls, &n->id);
995 case S_FAST_RECONNECT:
996 GNUNET_STATISTICS_update (GST_stats,
997 gettext_noop ("# fast reconnects failed"), 1,
999 disconnect_notify_cb (callback_cls, &n->id);
1005 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
1007 GNUNET_assert (GNUNET_YES ==
1008 GNUNET_CONTAINER_multihashmap_remove (neighbours,
1009 &n->id.hashPubKey, n));
1010 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
1012 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1013 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1015 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
1017 GNUNET_SCHEDULER_cancel (n->timeout_task);
1018 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1020 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
1022 GNUNET_SCHEDULER_cancel (n->transmission_task);
1023 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
1025 if (NULL != n->address)
1027 GNUNET_HELLO_address_free (n->address);
1031 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
1032 GNUNET_i2s (&n->id), n);
1038 * Peer has been idle for too long. Disconnect.
1040 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1041 * @param tc scheduler context
1044 neighbour_timeout_task (void *cls,
1045 const struct GNUNET_SCHEDULER_TaskContext *tc)
1047 struct NeighbourMapEntry *n = cls;
1049 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
1051 GNUNET_STATISTICS_update (GST_stats,
1053 ("# peers disconnected due to timeout"), 1,
1055 disconnect_neighbour (n);
1060 * Send another keepalive message.
1062 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
1063 * @param tc scheduler context
1066 neighbour_keepalive_task (void *cls,
1067 const struct GNUNET_SCHEDULER_TaskContext *tc)
1069 struct NeighbourMapEntry *n = cls;
1070 struct GNUNET_MessageHeader m;
1073 GNUNET_assert (S_CONNECTED == n->state);
1075 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1076 &neighbour_keepalive_task, n);
1078 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
1080 m.size = htons (sizeof (struct GNUNET_MessageHeader));
1081 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
1084 ret = send_with_session (n,
1085 (const void *) &m, sizeof (m),
1086 UINT32_MAX /* priority */ ,
1087 GNUNET_TIME_UNIT_FOREVER_REL,
1090 ret = send_with_plugin (&n->id, (const void *) &m, sizeof (m),
1091 UINT32_MAX /* priority */ ,
1092 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
1093 GNUNET_YES, NULL, NULL);
1096 n->expect_latency_response = GNUNET_NO;
1097 n->keep_alive_sent = GNUNET_TIME_absolute_get_zero ();
1098 if (ret != GNUNET_SYSERR)
1100 n->expect_latency_response = GNUNET_YES;
1101 n->keep_alive_sent = GNUNET_TIME_absolute_get ();
1108 * Disconnect from the given neighbour.
1111 * @param key hash of neighbour's public key (not used)
1112 * @param value the 'struct NeighbourMapEntry' of the neighbour
1115 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1117 struct NeighbourMapEntry *n = value;
1120 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1121 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1123 if (S_CONNECTED == n->state)
1124 GNUNET_STATISTICS_update (GST_stats,
1126 ("# peers disconnected due to global disconnect"),
1128 disconnect_neighbour (n);
1134 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1136 struct NeighbourMapEntry *n = cls;
1138 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1140 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1141 "ATS did not suggested address to connect to peer `%s'\n",
1142 GNUNET_i2s (&n->id));
1144 disconnect_neighbour (n);
1148 * Cleanup the neighbours subsystem.
1151 GST_neighbours_stop ()
1153 // This can happen during shutdown
1154 if (neighbours == NULL)
1159 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1161 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1162 // GNUNET_assert (neighbours_connected == 0);
1164 callback_cls = NULL;
1165 connect_notify_cb = NULL;
1166 disconnect_notify_cb = NULL;
1169 struct ContinutionContext
1171 struct GNUNET_HELLO_Address *address;
1173 struct Session *session;
1177 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
1178 struct GNUNET_BANDWIDTH_Value32NBO quota)
1180 struct QuotaSetMessage q_msg;
1183 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1184 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1185 ntohl (quota.value__), GNUNET_i2s (target));
1187 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1188 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1189 q_msg.quota = quota;
1190 q_msg.peer = (*target);
1191 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1195 * We tried to send a SESSION_CONNECT message to another peer. If this
1196 * succeeded, we change the state. If it failed, we should tell
1197 * ATS to not use this address anymore (until it is re-validated).
1199 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1200 * @param target peer to send the message to
1201 * @param success GNUNET_OK on success
1204 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1207 struct ContinutionContext *cc = cls;
1208 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1210 if (GNUNET_YES != success)
1212 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1213 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1215 if ((NULL == neighbours) || (NULL == n) || (n->state == S_DISCONNECT))
1217 GNUNET_HELLO_address_free (cc->address);
1222 if ((GNUNET_YES == success) &&
1223 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1225 change_state (n, S_CONNECT_SENT);
1226 GNUNET_HELLO_address_free (cc->address);
1231 if ((GNUNET_NO == success) &&
1232 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1235 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1236 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1237 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1239 change_state (n, S_NOT_CONNECTED);
1240 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1241 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1243 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1245 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1247 GNUNET_HELLO_address_free (cc->address);
1253 * We tried to switch addresses with an peer already connected. If it failed,
1254 * we should tell ATS to not use this address anymore (until it is re-validated).
1256 * @param cls the 'struct NeighbourMapEntry'
1257 * @param target peer to send the message to
1258 * @param success GNUNET_OK on success
1261 send_switch_address_continuation (void *cls,
1262 const struct GNUNET_PeerIdentity *target,
1265 struct ContinutionContext *cc = cls;
1266 struct NeighbourMapEntry *n;
1268 if (neighbours == NULL)
1270 GNUNET_HELLO_address_free (cc->address);
1272 return; /* neighbour is going away */
1275 n = lookup_neighbour (&cc->address->peer);
1276 if ((n == NULL) || (is_disconnecting (n)))
1278 GNUNET_HELLO_address_free (cc->address);
1280 return; /* neighbour is going away */
1283 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1284 if (GNUNET_YES != success)
1287 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1288 "Failed to switch connected peer `%s' to address '%s' session %X, asking ATS for new address \n",
1289 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1291 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1292 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1294 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1295 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1297 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1299 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1300 GNUNET_HELLO_address_free (cc->address);
1304 /* Tell ATS that switching addresses was successful */
1308 if (n->address_state == FRESH)
1310 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES);
1311 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1312 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1313 n->address_state = USED;
1316 case S_FAST_RECONNECT:
1318 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1319 "Successful fast reconnect to peer `%s'\n",
1320 GNUNET_i2s (&n->id));
1322 change_state (n, S_CONNECTED);
1323 neighbours_connected++;
1324 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1327 if (n->address_state == FRESH)
1329 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES);
1330 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1331 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1332 n->address_state = USED;
1335 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
1337 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1339 /* Updating quotas */
1340 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1341 send_outbound_quota (target, n->bandwidth_out);
1346 GNUNET_HELLO_address_free (cc->address);
1352 * We tried to send a SESSION_CONNECT message to another peer. If this
1353 * succeeded, we change the state. If it failed, we should tell
1354 * ATS to not use this address anymore (until it is re-validated).
1356 * @param cls the 'struct NeighbourMapEntry'
1357 * @param target peer to send the message to
1358 * @param success GNUNET_OK on success
1361 send_connect_ack_continuation (void *cls,
1362 const struct GNUNET_PeerIdentity *target,
1365 struct ContinutionContext *cc = cls;
1366 struct NeighbourMapEntry *n;
1368 if (neighbours == NULL)
1370 GNUNET_HELLO_address_free (cc->address);
1372 return; /* neighbour is going away */
1375 n = lookup_neighbour (&cc->address->peer);
1376 if ((n == NULL) || (is_disconnecting (n)))
1378 GNUNET_HELLO_address_free (cc->address);
1380 return; /* neighbour is going away */
1383 if (GNUNET_YES == success)
1385 GNUNET_HELLO_address_free (cc->address);
1387 return; /* sending successful */
1390 /* sending failed, ask for next address */
1392 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1393 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1394 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1396 change_state (n, S_NOT_CONNECTED);
1397 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1398 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1400 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1401 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1403 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1405 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1406 GNUNET_HELLO_address_free (cc->address);
1412 * For an existing neighbour record, set the active connection to
1413 * the given address.
1415 * @param peer identity of the peer to switch the address for
1416 * @param address address of the other peer, NULL if other peer
1418 * @param session session to use (or NULL)
1419 * @param ats performance data
1420 * @param ats_count number of entries in ats
1421 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1422 * connection is not up (yet)
1425 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1426 const struct GNUNET_HELLO_Address
1428 struct Session *session,
1429 const struct GNUNET_ATS_Information *ats,
1431 struct GNUNET_BANDWIDTH_Value32NBO
1433 struct GNUNET_BANDWIDTH_Value32NBO
1436 struct NeighbourMapEntry *n;
1437 struct SessionConnectMessage connect_msg;
1438 struct ContinutionContext *cc;
1442 if (neighbours == NULL)
1444 /* This can happen during shutdown */
1447 n = lookup_neighbour (peer);
1450 if (n->state == S_DISCONNECT)
1452 /* We are disconnecting, nothing to do here */
1455 GNUNET_assert (address->transport_name != NULL);
1456 if ((session == NULL) && (0 == address->address_length))
1458 GNUNET_break_op (0);
1459 /* FIXME: is this actually possible? When does this happen? */
1460 if (strlen (address->transport_name) > 0)
1461 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1462 GNUNET_ATS_suggest_address (GST_ats, peer);
1466 /* checks successful and neighbour != NULL */
1468 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1469 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1470 GST_plugins_a2s (address), session, GNUNET_i2s (peer),
1471 print_state (n->state));
1473 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1475 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1476 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1478 /* do not switch addresses just update quotas */
1479 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1480 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1481 (n->session == session))
1483 n->bandwidth_in = bandwidth_in;
1484 n->bandwidth_out = bandwidth_out;
1485 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1486 send_outbound_quota (peer, n->bandwidth_out);
1489 if (n->state == S_CONNECTED)
1491 /* mark old address as no longer used */
1492 GNUNET_assert (NULL != n->address);
1493 if (n->address_state == USED)
1495 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
1496 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1497 n->address_state = UNUSED;
1502 /* set new address */
1503 if (NULL != n->address)
1504 GNUNET_HELLO_address_free (n->address);
1505 n->address = GNUNET_HELLO_address_copy (address);
1506 n->address_state = FRESH;
1507 n->bandwidth_in = bandwidth_in;
1508 n->bandwidth_out = bandwidth_out;
1509 GNUNET_SCHEDULER_cancel (n->timeout_task);
1511 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1512 &neighbour_timeout_task, n);
1515 /* Obtain an session for this address from plugin */
1516 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1517 papi = GST_plugins_find (address->transport_name);
1518 GNUNET_assert (papi != NULL);
1519 if (session == NULL)
1521 n->session = papi->get_session (papi->cls, address);
1522 /* Session could not be initiated */
1523 if (n->session == NULL)
1525 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1526 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1527 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1529 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1531 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1532 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1533 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1536 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1537 GNUNET_HELLO_address_free (n->address);
1542 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1543 "Obtained new session %p for peer `%s' and address '%s'\n",
1544 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1545 /* Telling ATS about new session */
1546 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1550 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1551 "Using existing session %p for peer `%s' and address '%s'\n",
1552 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1553 n->session = session;
1555 /* remove this dummy */
1556 if (NULL != NULL) send_with_plugin (NULL, NULL, 0,
1557 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, NULL,
1558 NULL, GNUNET_YES, NULL, NULL);
1560 n->session = session;
1563 if (NULL != NULL) send_with_session( NULL, NULL, 0, 0, GNUNET_TIME_relative_get_zero(), NULL, NULL);
1568 case S_NOT_CONNECTED:
1569 case S_CONNECT_SENT:
1570 msg_len = sizeof (struct SessionConnectMessage);
1571 connect_msg.header.size = htons (msg_len);
1572 connect_msg.header.type =
1573 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1574 connect_msg.reserved = htonl (0);
1575 connect_msg.timestamp =
1576 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1578 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1579 cc->session = session;
1580 cc->address = GNUNET_HELLO_address_copy (address);
1583 ret = send_with_session (n,
1584 (const char *) &connect_msg, msg_len,
1585 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1586 &send_connect_continuation, cc);
1589 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1590 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1591 address, GNUNET_YES, &send_connect_continuation, cc);
1595 case S_CONNECT_RECV:
1596 /* We received a CONNECT message and asked ATS for an address */
1597 msg_len = sizeof (struct SessionConnectMessage);
1598 connect_msg.header.size = htons (msg_len);
1599 connect_msg.header.type =
1600 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1601 connect_msg.reserved = htonl (0);
1602 connect_msg.timestamp =
1603 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1604 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1605 cc->session = session;
1606 cc->address = GNUNET_HELLO_address_copy (address);
1608 ret = send_with_session(n,
1609 (const void *) &connect_msg, msg_len,
1610 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1611 &send_connect_ack_continuation,
1615 send_with_plugin (&n->id, (const void *) &connect_msg, msg_len,
1616 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1617 address, GNUNET_YES, &send_connect_ack_continuation,
1622 case S_FAST_RECONNECT:
1623 /* connected peer is switching addresses or tries fast reconnect */
1624 msg_len = sizeof (struct SessionConnectMessage);
1625 connect_msg.header.size = htons (msg_len);
1626 connect_msg.header.type =
1627 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1628 connect_msg.reserved = htonl (0);
1629 connect_msg.timestamp =
1630 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1631 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1632 cc->session = session;
1633 cc->address = GNUNET_HELLO_address_copy (address);
1636 ret = send_with_session(n,
1637 (const void *) &connect_msg, msg_len,
1638 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1639 &send_switch_address_continuation, cc);
1642 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1643 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1644 address, GNUNET_YES,
1645 &send_switch_address_continuation, cc);
1649 if (ret == GNUNET_SYSERR)
1651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1652 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1653 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1657 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1658 "Invalid connection state to switch addresses %u \n", n->state);
1659 GNUNET_break_op (0);
1666 * Obtain current latency information for the given neighbour.
1669 * @return observed latency of the address, FOREVER if the address was
1670 * never successfully validated
1672 struct GNUNET_TIME_Relative
1673 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1675 struct NeighbourMapEntry *n;
1677 n = lookup_neighbour (peer);
1678 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1679 return GNUNET_TIME_UNIT_FOREVER_REL;
1685 * Obtain current address information for the given neighbour.
1688 * @return address currently used
1690 struct GNUNET_HELLO_Address *
1691 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1693 struct NeighbourMapEntry *n;
1695 n = lookup_neighbour (peer);
1696 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1705 * Create an entry in the neighbour map for the given peer
1707 * @param peer peer to create an entry for
1708 * @return new neighbour map entry
1710 static struct NeighbourMapEntry *
1711 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1713 struct NeighbourMapEntry *n;
1716 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1717 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1719 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1721 n->state = S_NOT_CONNECTED;
1722 n->latency = GNUNET_TIME_relative_get_forever ();
1723 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1724 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1725 MAX_BANDWIDTH_CARRY_S);
1727 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1728 &neighbour_timeout_task, n);
1729 GNUNET_assert (GNUNET_OK ==
1730 GNUNET_CONTAINER_multihashmap_put (neighbours,
1731 &n->id.hashPubKey, n,
1732 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1738 * Try to create a connection to the given target (eventually).
1740 * @param target peer to try to connect to
1743 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1745 struct NeighbourMapEntry *n;
1747 // This can happen during shutdown
1748 if (neighbours == NULL)
1753 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1754 GNUNET_i2s (target));
1757 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1762 n = lookup_neighbour (target);
1766 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1767 return; /* already connecting or connected */
1768 if (is_disconnecting (n))
1769 change_state (n, S_NOT_CONNECTED);
1774 n = setup_neighbour (target);
1776 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1777 "Asking ATS for suggested address to connect to peer `%s'\n",
1778 GNUNET_i2s (&n->id));
1781 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1785 * Test if we're connected to the given peer.
1787 * @param target peer to test
1788 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1791 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1793 struct NeighbourMapEntry *n;
1795 // This can happen during shutdown
1796 if (neighbours == NULL)
1801 n = lookup_neighbour (target);
1803 if ((NULL == n) || (S_CONNECTED != n->state))
1804 return GNUNET_NO; /* not connected */
1809 * A session was terminated. Take note.
1811 * @param peer identity of the peer where the session died
1812 * @param session session that is gone
1815 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1816 struct Session *session)
1818 struct NeighbourMapEntry *n;
1820 if (neighbours == NULL)
1822 /* This can happen during shutdown */
1827 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1828 session, GNUNET_i2s (peer));
1831 n = lookup_neighbour (peer);
1834 if (session != n->session)
1835 return; /* doesn't affect us */
1836 if (n->state == S_CONNECTED)
1838 if (n->address_state == USED)
1840 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
1841 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1842 n->address_state = UNUSED;
1846 if (NULL != n->address)
1848 GNUNET_HELLO_address_free (n->address);
1853 /* not connected anymore anyway, shouldn't matter */
1854 if (S_CONNECTED != n->state)
1857 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1859 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1860 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1861 n->expect_latency_response = GNUNET_NO;
1864 /* connected, try fast reconnect */
1865 /* statistics "transport" : "# peers connected" -= 1
1866 * neighbours_connected -= 1
1867 * BUT: no disconnect_cb to notify clients about disconnect
1870 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1873 GNUNET_assert (neighbours_connected > 0);
1874 change_state (n, S_FAST_RECONNECT);
1875 neighbours_connected--;
1876 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
1880 /* We are connected, so ask ATS to switch addresses */
1881 GNUNET_SCHEDULER_cancel (n->timeout_task);
1882 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1883 &neighbour_timeout_task, n);
1884 /* try QUICKLY to re-establish a connection, reduce timeout! */
1885 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1886 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1887 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1888 &ats_suggest_cancel,
1890 GNUNET_ATS_suggest_address (GST_ats, peer);
1895 * Transmit a message to the given target using the active connection.
1897 * @param target destination
1898 * @param msg message to send
1899 * @param msg_size number of bytes in msg
1900 * @param timeout when to fail with timeout
1901 * @param cont function to call when done
1902 * @param cont_cls closure for 'cont'
1905 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1906 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1907 GST_NeighbourSendContinuation cont, void *cont_cls)
1909 struct NeighbourMapEntry *n;
1910 struct MessageQueue *mq;
1912 // This can happen during shutdown
1913 if (neighbours == NULL)
1918 n = lookup_neighbour (target);
1919 if ((n == NULL) || (!is_connected (n)))
1921 GNUNET_STATISTICS_update (GST_stats,
1923 ("# messages not sent (no such peer or not connected)"),
1927 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1928 "Could not send message to peer `%s': unknown neighbour",
1929 GNUNET_i2s (target));
1930 else if (!is_connected (n))
1931 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1932 "Could not send message to peer `%s': not connected\n",
1933 GNUNET_i2s (target));
1936 cont (cont_cls, GNUNET_SYSERR);
1940 if ((n->session == NULL) && (n->address == NULL))
1942 GNUNET_STATISTICS_update (GST_stats,
1944 ("# messages not sent (no such peer or not connected)"),
1947 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1948 "Could not send message to peer `%s': no address available\n",
1949 GNUNET_i2s (target));
1953 cont (cont_cls, GNUNET_SYSERR);
1957 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1958 GNUNET_STATISTICS_update (GST_stats,
1960 ("# bytes in message queue for other peers"),
1961 msg_size, GNUNET_NO);
1962 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1964 mq->cont_cls = cont_cls;
1965 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1966 memcpy (&mq[1], msg, msg_size);
1967 mq->message_buf = (const char *) &mq[1];
1968 mq->message_buf_size = msg_size;
1969 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1970 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1972 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1973 (NULL == n->is_active))
1974 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1979 * We have received a message from the given sender. How long should
1980 * we delay before receiving more? (Also used to keep the peer marked
1983 * @param sender sender of the message
1984 * @param size size of the message
1985 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1986 * GNUNET_NO if the neighbour is not connected or violates the quota,
1987 * GNUNET_SYSERR if the connection is not fully up yet
1988 * @return how long to wait before reading more from this sender
1990 struct GNUNET_TIME_Relative
1991 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1992 *sender, ssize_t size, int *do_forward)
1994 struct NeighbourMapEntry *n;
1995 struct GNUNET_TIME_Relative ret;
1997 // This can happen during shutdown
1998 if (neighbours == NULL)
2000 return GNUNET_TIME_UNIT_FOREVER_REL;
2003 n = lookup_neighbour (sender);
2006 GST_neighbours_try_connect (sender);
2007 n = lookup_neighbour (sender);
2010 GNUNET_STATISTICS_update (GST_stats,
2012 ("# messages discarded due to lack of neighbour record"),
2014 *do_forward = GNUNET_NO;
2015 return GNUNET_TIME_UNIT_ZERO;
2018 if (!is_connected (n))
2020 *do_forward = GNUNET_SYSERR;
2021 return GNUNET_TIME_UNIT_ZERO;
2023 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
2025 n->quota_violation_count++;
2027 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2028 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
2029 n->in_tracker.available_bytes_per_s__,
2030 n->quota_violation_count);
2032 /* Discount 32k per violation */
2033 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
2037 if (n->quota_violation_count > 0)
2039 /* try to add 32k back */
2040 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
2041 n->quota_violation_count--;
2044 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
2046 GNUNET_STATISTICS_update (GST_stats,
2048 ("# bandwidth quota violations by other peers"),
2050 *do_forward = GNUNET_NO;
2051 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
2053 *do_forward = GNUNET_YES;
2054 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
2055 if (ret.rel_value > 0)
2058 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2059 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
2060 (unsigned long long) n->in_tracker.
2061 consumption_since_last_update__,
2062 (unsigned int) n->in_tracker.available_bytes_per_s__,
2063 (unsigned long long) ret.rel_value);
2065 GNUNET_STATISTICS_update (GST_stats,
2066 gettext_noop ("# ms throttling suggested"),
2067 (int64_t) ret.rel_value, GNUNET_NO);
2074 * Keep the connection to the given neighbour alive longer,
2075 * we received a KEEPALIVE (or equivalent).
2077 * @param neighbour neighbour to keep alive
2080 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
2082 struct NeighbourMapEntry *n;
2084 // This can happen during shutdown
2085 if (neighbours == NULL)
2090 n = lookup_neighbour (neighbour);
2093 GNUNET_STATISTICS_update (GST_stats,
2095 ("# KEEPALIVE messages discarded (not connected)"),
2099 GNUNET_SCHEDULER_cancel (n->timeout_task);
2101 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2102 &neighbour_timeout_task, n);
2104 /* send reply to measure latency */
2105 if (S_CONNECTED != n->state)
2108 struct GNUNET_MessageHeader m;
2110 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2111 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2114 send_with_session(n,
2115 (const void *) &m, sizeof (m),
2117 GNUNET_TIME_UNIT_FOREVER_REL,
2120 send_with_plugin (&n->id,
2121 (const void *) &m, sizeof (m),
2122 UINT32_MAX /* priority */ ,
2123 GNUNET_TIME_UNIT_FOREVER_REL,
2124 n->session, n->address,
2125 GNUNET_YES, NULL, NULL);
2132 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2135 * @param neighbour neighbour to keep alive
2136 * @param ats performance data
2137 * @param ats_count number of entries in ats
2140 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2141 const struct GNUNET_ATS_Information *ats,
2144 struct NeighbourMapEntry *n;
2145 struct GNUNET_ATS_Information *ats_new;
2148 if (neighbours == NULL)
2150 // This can happen during shutdown
2154 n = lookup_neighbour (neighbour);
2155 if ((NULL == n) || (n->state != S_CONNECTED))
2157 GNUNET_STATISTICS_update (GST_stats,
2159 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2163 if (n->expect_latency_response != GNUNET_YES)
2165 GNUNET_STATISTICS_update (GST_stats,
2167 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2171 n->expect_latency_response = GNUNET_NO;
2173 GNUNET_assert (n->keep_alive_sent.abs_value !=
2174 GNUNET_TIME_absolute_get_zero ().abs_value);
2176 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2177 GNUNET_TIME_absolute_get ());
2179 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2180 GNUNET_i2s (&n->id), n->latency.rel_value);
2184 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2186 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2191 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2193 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2196 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2197 if (n->latency.rel_value > UINT32_MAX)
2198 latency = UINT32_MAX;
2200 latency = n->latency.rel_value;
2201 ats_new[ats_count].value = htonl (latency);
2203 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2205 GNUNET_free (ats_new);
2211 * Change the incoming quota for the given peer.
2213 * @param neighbour identity of peer to change qutoa for
2214 * @param quota new quota
2217 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2218 struct GNUNET_BANDWIDTH_Value32NBO quota)
2220 struct NeighbourMapEntry *n;
2222 // This can happen during shutdown
2223 if (neighbours == NULL)
2228 n = lookup_neighbour (neighbour);
2231 GNUNET_STATISTICS_update (GST_stats,
2233 ("# SET QUOTA messages ignored (no such peer)"),
2238 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2239 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2240 ntohl (quota.value__), GNUNET_i2s (&n->id));
2242 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2243 if (0 != ntohl (quota.value__))
2246 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2247 GNUNET_i2s (&n->id), "SET_QUOTA");
2249 if (is_connected (n))
2250 GNUNET_STATISTICS_update (GST_stats,
2251 gettext_noop ("# disconnects due to quota of 0"),
2253 disconnect_neighbour (n);
2258 * Closure for the neighbours_iterate function.
2260 struct IteratorContext
2263 * Function to call on each connected neighbour.
2265 GST_NeighbourIterator cb;
2275 * Call the callback from the closure for each connected neighbour.
2277 * @param cls the 'struct IteratorContext'
2278 * @param key the hash of the public key of the neighbour
2279 * @param value the 'struct NeighbourMapEntry'
2280 * @return GNUNET_OK (continue to iterate)
2283 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2285 struct IteratorContext *ic = cls;
2286 struct NeighbourMapEntry *n = value;
2288 if (!is_connected (n))
2291 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2297 * Iterate over all connected neighbours.
2299 * @param cb function to call
2300 * @param cb_cls closure for cb
2303 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2305 struct IteratorContext ic;
2307 // This can happen during shutdown
2308 if (neighbours == NULL)
2315 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2319 * If we have an active connection to the given target, it must be shutdown.
2321 * @param target peer to disconnect from
2324 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2326 struct NeighbourMapEntry *n;
2328 // This can happen during shutdown
2329 if (neighbours == NULL)
2334 n = lookup_neighbour (target);
2336 return; /* not active */
2337 disconnect_neighbour (n);
2342 * We received a disconnect message from the given peer,
2343 * validate and process.
2345 * @param peer sender of the message
2346 * @param msg the disconnect message
2349 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2351 const struct GNUNET_MessageHeader
2354 struct NeighbourMapEntry *n;
2355 const struct SessionDisconnectMessage *sdm;
2359 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2360 "Received DISCONNECT message from peer `%s'\n",
2364 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2366 // GNUNET_break_op (0);
2367 GNUNET_STATISTICS_update (GST_stats,
2369 ("# disconnect messages ignored (old format)"), 1,
2373 sdm = (const struct SessionDisconnectMessage *) msg;
2374 n = lookup_neighbour (peer);
2376 return; /* gone already */
2377 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2378 n->connect_ts.abs_value)
2380 GNUNET_STATISTICS_update (GST_stats,
2382 ("# disconnect messages ignored (timestamp)"), 1,
2386 GNUNET_CRYPTO_hash (&sdm->public_key,
2387 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2389 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2391 GNUNET_break_op (0);
2394 if (ntohl (sdm->purpose.size) !=
2395 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2396 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2397 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2399 GNUNET_break_op (0);
2403 GNUNET_CRYPTO_rsa_verify
2404 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2405 &sdm->signature, &sdm->public_key))
2407 GNUNET_break_op (0);
2410 GST_neighbours_force_disconnect (peer);
2415 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2416 * Consider switching to it.
2418 * @param message possibly a 'struct SessionConnectMessage' (check format)
2419 * @param peer identity of the peer to switch the address for
2420 * @param address address of the other peer, NULL if other peer
2422 * @param session session to use (or NULL)
2423 * @param ats performance data
2424 * @param ats_count number of entries in ats
2427 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2428 const struct GNUNET_PeerIdentity *peer,
2429 const struct GNUNET_HELLO_Address *address,
2430 struct Session *session,
2431 const struct GNUNET_ATS_Information *ats,
2434 const struct SessionConnectMessage *scm;
2435 struct GNUNET_MessageHeader msg;
2436 struct NeighbourMapEntry *n;
2441 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2442 "Received CONNECT_ACK message from peer `%s'\n",
2446 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2448 GNUNET_break_op (0);
2451 scm = (const struct SessionConnectMessage *) message;
2452 GNUNET_break_op (ntohl (scm->reserved) == 0);
2453 n = lookup_neighbour (peer);
2456 /* we did not send 'CONNECT' -- at least not recently */
2457 GNUNET_STATISTICS_update (GST_stats,
2459 ("# unexpected CONNECT_ACK messages (no peer)"),
2466 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2468 * We also received an CONNECT message, switched from SENDT to RECV and
2469 * ATS already suggested us an address after a successful blacklist check
2471 if ((n->state != S_CONNECT_SENT) &&
2472 ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2474 GNUNET_STATISTICS_update (GST_stats,
2476 ("# unexpected CONNECT_ACK messages"), 1,
2481 change_state (n, S_CONNECTED);
2483 if (NULL != session)
2485 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2487 "Giving ATS session %p of plugin %s for peer %s\n",
2488 session, address->transport_name, GNUNET_i2s (peer));
2490 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2491 GNUNET_assert (NULL != n->address);
2493 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2495 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2496 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2497 n->address_state = USED;
2500 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2502 /* send ACK (ACK) */
2503 msg_len = sizeof (msg);
2504 msg.size = htons (msg_len);
2505 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2508 ret = send_with_session(n,
2509 (const char *) &msg, msg_len,
2510 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2514 send_with_plugin (&n->id,
2515 (const char *) &msg, msg_len,
2516 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2517 n->session, n->address, GNUNET_YES,
2521 if (ret == GNUNET_SYSERR)
2522 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2523 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2524 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2527 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2528 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2530 neighbours_connected++;
2531 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2535 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2536 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2539 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2540 send_outbound_quota (peer, n->bandwidth_out);
2546 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2547 const struct GNUNET_PeerIdentity *peer,
2548 const struct GNUNET_HELLO_Address *address,
2549 struct Session *session,
2550 const struct GNUNET_ATS_Information *ats,
2553 struct NeighbourMapEntry *n;
2556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2560 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2562 GNUNET_break_op (0);
2565 n = lookup_neighbour (peer);
2571 if (S_CONNECTED == n->state)
2573 if (!is_connecting (n))
2575 GNUNET_STATISTICS_update (GST_stats,
2576 gettext_noop ("# unexpected ACK messages"), 1,
2580 change_state (n, S_CONNECTED);
2581 if (NULL != session)
2582 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2584 "Giving ATS session %p of plugin %s for peer %s\n",
2585 session, address->transport_name, GNUNET_i2s (peer));
2586 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2587 GNUNET_assert (n->address != NULL);
2589 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2591 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2592 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2593 n->address_state = USED;
2597 neighbours_connected++;
2598 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2601 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2602 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2603 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2605 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2606 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2607 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2610 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2611 send_outbound_quota (peer, n->bandwidth_out);
2614 struct BlackListCheckContext
2616 struct GNUNET_ATS_Information *ats;
2620 struct Session *session;
2622 struct GNUNET_HELLO_Address *address;
2624 struct GNUNET_TIME_Absolute ts;
2629 handle_connect_blacklist_cont (void *cls,
2630 const struct GNUNET_PeerIdentity *peer,
2633 struct NeighbourMapEntry *n;
2634 struct BlackListCheckContext *bcc = cls;
2637 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2638 "Blacklist check due to CONNECT message: `%s'\n",
2640 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2644 if (GNUNET_OK != result)
2646 GNUNET_HELLO_address_free (bcc->address);
2651 n = lookup_neighbour (peer);
2653 n = setup_neighbour (peer);
2655 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2657 if (NULL != bcc->session)
2658 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2660 "Giving ATS session %p of address `%s' for peer %s\n",
2661 bcc->session, GST_plugins_a2s (bcc->address),
2663 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2665 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2667 n->connect_ts = bcc->ts;
2670 GNUNET_HELLO_address_free (bcc->address);
2673 if (n->state != S_CONNECT_RECV)
2674 change_state (n, S_CONNECT_RECV);
2677 /* Ask ATS for an address to connect via that address */
2678 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2679 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2681 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2683 GNUNET_ATS_suggest_address (GST_ats, peer);
2687 * We received a 'SESSION_CONNECT' message from the other peer.
2688 * Consider switching to it.
2690 * @param message possibly a 'struct SessionConnectMessage' (check format)
2691 * @param peer identity of the peer to switch the address for
2692 * @param address address of the other peer, NULL if other peer
2694 * @param session session to use (or NULL)
2695 * @param ats performance data
2696 * @param ats_count number of entries in ats (excluding 0-termination)
2699 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2700 const struct GNUNET_PeerIdentity *peer,
2701 const struct GNUNET_HELLO_Address *address,
2702 struct Session *session,
2703 const struct GNUNET_ATS_Information *ats,
2706 const struct SessionConnectMessage *scm;
2707 struct BlackListCheckContext *bcc = NULL;
2708 struct NeighbourMapEntry *n;
2711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2712 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2715 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2717 GNUNET_break_op (0);
2721 scm = (const struct SessionConnectMessage *) message;
2722 GNUNET_break_op (ntohl (scm->reserved) == 0);
2724 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2726 n = lookup_neighbour (peer);
2727 if ((n != NULL) && (S_CONNECTED == n->state))
2729 /* connected peer switches addresses */
2734 /* we are not connected to this peer */
2735 /* do blacklist check */
2737 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2738 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2739 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2740 bcc->ats_count = ats_count + 1;
2741 bcc->address = GNUNET_HELLO_address_copy (address);
2742 bcc->session = session;
2743 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2744 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2745 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2746 bcc->ats[ats_count].value =
2747 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2748 GST_blacklist_test_allowed (peer, address->transport_name,
2749 handle_connect_blacklist_cont, bcc);
2753 /* end of file gnunet-service-transport_neighbours.c */