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_NO
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, disconnect_notify_cb and address_change_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 * Function to call when we changed an active address of a neighbour.
377 static GNUNET_TRANSPORT_PeerIterateCallback address_change_cb;
380 * counter for connected neighbours
382 static int neighbours_connected;
385 * Lookup a neighbour entry in the neighbours hash map.
387 * @param pid identity of the peer to look up
388 * @return the entry, NULL if there is no existing record
390 static struct NeighbourMapEntry *
391 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
393 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
396 #define change_state(n, state, ...) change (n, state, __LINE__)
399 is_connecting (struct NeighbourMapEntry *n)
401 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
407 is_connected (struct NeighbourMapEntry *n)
409 if (n->state == S_CONNECTED)
415 is_disconnecting (struct NeighbourMapEntry *n)
417 if (n->state == S_DISCONNECT)
423 print_state (int state)
428 return "S_CONNECTED";
431 return "S_CONNECT_RECV";
434 return "S_CONNECT_SENT";
437 return "S_DISCONNECT";
439 case S_NOT_CONNECTED:
440 return "S_NOT_CONNECTED";
442 case S_FAST_RECONNECT:
443 return "S_FAST_RECONNECT";
453 change (struct NeighbourMapEntry *n, int state, int line);
456 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
460 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
462 struct NeighbourMapEntry *n = cls;
467 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
468 if (n->state == S_CONNECTED)
472 GNUNET_STATISTICS_update (GST_stats,
474 ("# failed connection attempts due to timeout"), 1,
478 /* resetting state */
479 n->state = S_NOT_CONNECTED;
481 /* destroying address */
482 if (n->address != NULL)
484 GNUNET_assert (strlen (n->address->transport_name) > 0);
485 GNUNET_ATS_address_destroyed (GST_ats, n->address, n->session);
488 /* request new address */
489 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
490 GNUNET_SCHEDULER_cancel (n->ats_suggest);
492 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
494 GNUNET_ATS_suggest_address (GST_ats, &n->id);
498 change (struct NeighbourMapEntry *n, int state, int line)
501 /* allowed transitions */
502 int allowed = GNUNET_NO;
504 previous_state = n->state;
508 case S_NOT_CONNECTED:
509 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
510 (state == S_DISCONNECT))
511 allowed = GNUNET_YES;
514 allowed = GNUNET_YES;
517 allowed = GNUNET_YES;
520 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
521 allowed = GNUNET_YES;
525 case S_FAST_RECONNECT:
526 if ((state == S_CONNECTED) || (state == S_DISCONNECT))
527 allowed = GNUNET_YES;
533 if (allowed == GNUNET_NO)
535 char *old = GNUNET_strdup (print_state (n->state));
536 char *new = GNUNET_strdup (print_state (state));
538 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
539 "Illegal state transition from `%s' to `%s' in line %u \n", old,
544 return GNUNET_SYSERR;
548 char *old = GNUNET_strdup (print_state (n->state));
549 char *new = GNUNET_strdup (print_state (state));
551 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
552 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
553 GNUNET_i2s (&n->id), n, old, new, line);
562 case S_FAST_RECONNECT:
565 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
566 GNUNET_SCHEDULER_cancel (n->state_reset);
568 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task, n);
571 case S_NOT_CONNECTED:
573 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
576 char *old = GNUNET_strdup (print_state (n->state));
577 char *new = GNUNET_strdup (print_state (state));
579 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
580 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
581 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), old, new);
585 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
586 GNUNET_SCHEDULER_cancel (n->state_reset);
587 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
595 if (NULL != address_change_cb)
597 if (n->state == S_CONNECTED)
598 address_change_cb (callback_cls, &n->id, n->address);
599 else if (previous_state == S_CONNECTED)
600 address_change_cb (callback_cls, &n->id, NULL);
607 send_with_session (struct NeighbourMapEntry *n,
608 const char *msgbuf, size_t msgbuf_size,
610 struct GNUNET_TIME_Relative timeout,
611 GNUNET_TRANSPORT_TransmitContinuation cont, void *cont_cls)
613 struct GNUNET_TRANSPORT_PluginFunctions *papi;
614 size_t ret = GNUNET_SYSERR;
616 GNUNET_assert (n != NULL);
617 GNUNET_assert (n->session != NULL);
619 papi = GST_plugins_find (n->address->transport_name);
623 cont (cont_cls, &n->id, GNUNET_SYSERR);
624 return GNUNET_SYSERR;
627 ret = papi->send (papi->cls,
634 if ((ret == -1) && (cont != NULL))
635 cont (cont_cls, &n->id, GNUNET_SYSERR);
640 * Task invoked to start a transmission to another peer.
642 * @param cls the 'struct NeighbourMapEntry'
643 * @param tc scheduler context
646 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
650 * We're done with our transmission attempt, continue processing.
652 * @param cls the 'struct MessageQueue' of the message
653 * @param receiver intended receiver
654 * @param success whether it worked or not
657 transmit_send_continuation (void *cls,
658 const struct GNUNET_PeerIdentity *receiver,
661 struct MessageQueue *mq = cls;
662 struct NeighbourMapEntry *n;
663 struct NeighbourMapEntry *tmp;
665 tmp = lookup_neighbour (receiver);
667 if ((NULL != n) && (tmp != NULL) && (tmp == n))
669 GNUNET_assert (n->is_active == mq);
671 if (success == GNUNET_YES)
673 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
674 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
678 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
679 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
680 (success == GNUNET_OK) ? "successful" : "FAILED");
682 if (NULL != mq->cont)
683 mq->cont (mq->cont_cls, success);
689 * Check the ready list for the given neighbour and if a plugin is
690 * ready for transmission (and if we have a message), do so!
692 * @param n target peer for which to transmit
695 try_transmission_to_peer (struct NeighbourMapEntry *n)
697 struct MessageQueue *mq;
698 struct GNUNET_TIME_Relative timeout;
701 if (n->is_active != NULL)
704 return; /* transmission already pending */
706 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
709 return; /* currently waiting for bandwidth */
711 while (NULL != (mq = n->messages_head))
713 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
714 if (timeout.rel_value > 0)
716 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
719 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
722 return; /* no more messages */
724 if (n->address == NULL)
727 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
728 GNUNET_i2s (&n->id));
730 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
731 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
732 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
733 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
737 if (GST_plugins_find (n->address->transport_name) == NULL)
742 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
746 if ((n->address->address_length == 0) && (n->session == NULL))
748 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "No address for peer `%s'\n",
749 GNUNET_i2s (&n->id));
750 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
751 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
752 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
756 ret = send_with_session(n,
757 mq->message_buf, mq->message_buf_size,
759 &transmit_send_continuation, mq);
763 /* failure, but 'send' would not call continuation in this case,
764 * so we need to do it here! */
765 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
772 * Task invoked to start a transmission to another peer.
774 * @param cls the 'struct NeighbourMapEntry'
775 * @param tc scheduler context
778 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
780 struct NeighbourMapEntry *n = cls;
782 GNUNET_assert (NULL != lookup_neighbour (&n->id));
783 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
784 try_transmission_to_peer (n);
789 * Initialize the neighbours subsystem.
791 * @param cls closure for callbacks
792 * @param connect_cb function to call if we connect to a peer
793 * @param disconnect_cb function to call if we disconnect from a peer
794 * @param peer_address_cb function to call if we change an active address
798 GST_neighbours_start (void *cls,
799 GNUNET_TRANSPORT_NotifyConnect connect_cb,
800 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb,
801 GNUNET_TRANSPORT_PeerIterateCallback peer_address_cb)
804 connect_notify_cb = connect_cb;
805 disconnect_notify_cb = disconnect_cb;
806 address_change_cb = peer_address_cb;
807 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
812 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
816 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
817 "Sending DISCONNECT message to peer `%4s': %i\n",
818 GNUNET_i2s (target), result);
824 send_disconnect (struct NeighbourMapEntry * n)
827 struct SessionDisconnectMessage disconnect_msg;
830 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
831 "Sending DISCONNECT message to peer `%4s'\n",
832 GNUNET_i2s (&n->id));
835 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
836 disconnect_msg.header.type =
837 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
838 disconnect_msg.reserved = htonl (0);
839 disconnect_msg.purpose.size =
840 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
841 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
842 sizeof (struct GNUNET_TIME_AbsoluteNBO));
843 disconnect_msg.purpose.purpose =
844 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
845 disconnect_msg.timestamp =
846 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
847 disconnect_msg.public_key = GST_my_public_key;
848 GNUNET_assert (GNUNET_OK ==
849 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
850 &disconnect_msg.purpose,
851 &disconnect_msg.signature));
853 ret = send_with_session (n,
854 (const char *) &disconnect_msg, sizeof (disconnect_msg),
855 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
856 &send_disconnect_cont, NULL);
858 if (ret == GNUNET_SYSERR)
859 return GNUNET_SYSERR;
861 GNUNET_STATISTICS_update (GST_stats,
863 ("# peers disconnected due to external request"), 1,
870 * Disconnect from the given neighbour, clean up the record.
872 * @param n neighbour to disconnect from
875 disconnect_neighbour (struct NeighbourMapEntry *n)
877 struct MessageQueue *mq;
880 previous_state = n->state;
882 if (is_disconnecting (n))
885 /* send DISCONNECT MESSAGE */
886 if (previous_state == S_CONNECTED)
888 if (GNUNET_OK == send_disconnect (n))
889 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
890 GNUNET_i2s (&n->id));
892 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
893 "Could not send DISCONNECT_MSG to `%s'\n",
894 GNUNET_i2s (&n->id));
897 change_state (n, S_DISCONNECT);
899 if (previous_state == S_CONNECTED)
901 GNUNET_assert (NULL != n->address);
902 if (n->address_state == USED)
904 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
905 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
906 n->address_state = UNUSED;
910 if (n->address != NULL)
912 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)
934 GNUNET_assert (neighbours_connected > 0);
935 neighbours_connected--;
936 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
937 GNUNET_SCHEDULER_cancel (n->keepalive_task);
938 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
939 n->expect_latency_response = GNUNET_NO;
940 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
942 disconnect_notify_cb (callback_cls, &n->id);
944 case S_FAST_RECONNECT:
945 GNUNET_STATISTICS_update (GST_stats,
946 gettext_noop ("# fast reconnects failed"), 1,
948 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;
1022 GNUNET_assert (S_CONNECTED == n->state);
1024 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
1025 &neighbour_keepalive_task, n);
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);
1032 ret = send_with_session (n,
1033 (const void *) &m, sizeof (m),
1034 UINT32_MAX /* priority */ ,
1035 GNUNET_TIME_UNIT_FOREVER_REL,
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;
1109 address_change_cb = NULL;
1112 struct ContinutionContext
1114 struct GNUNET_HELLO_Address *address;
1116 struct Session *session;
1120 send_outbound_quota (const struct GNUNET_PeerIdentity *target,
1121 struct GNUNET_BANDWIDTH_Value32NBO quota)
1123 struct QuotaSetMessage q_msg;
1126 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1127 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
1128 ntohl (quota.value__), GNUNET_i2s (target));
1130 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1131 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1132 q_msg.quota = quota;
1133 q_msg.peer = (*target);
1134 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1138 * We tried to send a SESSION_CONNECT message to another peer. If this
1139 * succeeded, we change the state. If it failed, we should tell
1140 * ATS to not use this address anymore (until it is re-validated).
1142 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1143 * @param target peer to send the message to
1144 * @param success GNUNET_OK on success
1147 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1150 struct ContinutionContext *cc = cls;
1151 struct NeighbourMapEntry *n = lookup_neighbour (&cc->address->peer);
1153 if (GNUNET_YES != success)
1155 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1156 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1158 if ((NULL == neighbours) || (NULL == n) || (n->state == S_DISCONNECT))
1160 GNUNET_HELLO_address_free (cc->address);
1165 if ((GNUNET_YES == success) &&
1166 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1168 change_state (n, S_CONNECT_SENT);
1169 GNUNET_HELLO_address_free (cc->address);
1174 if ((GNUNET_NO == success) &&
1175 ((n->state == S_NOT_CONNECTED) || (n->state == S_CONNECT_SENT)))
1178 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1179 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1180 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1182 change_state (n, S_NOT_CONNECTED);
1183 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1184 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1186 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1188 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1190 GNUNET_HELLO_address_free (cc->address);
1196 * We tried to switch addresses with an peer already connected. If it failed,
1197 * we should tell ATS to not use this address anymore (until it is re-validated).
1199 * @param cls the 'struct NeighbourMapEntry'
1200 * @param target peer to send the message to
1201 * @param success GNUNET_OK on success
1204 send_switch_address_continuation (void *cls,
1205 const struct GNUNET_PeerIdentity *target,
1208 struct ContinutionContext *cc = cls;
1209 struct NeighbourMapEntry *n;
1211 if (neighbours == NULL)
1213 GNUNET_HELLO_address_free (cc->address);
1215 return; /* neighbour is going away */
1218 n = lookup_neighbour (&cc->address->peer);
1219 if ((n == NULL) || (is_disconnecting (n)))
1221 GNUNET_HELLO_address_free (cc->address);
1223 return; /* neighbour is going away */
1226 GNUNET_assert ((n->state == S_CONNECTED) || (n->state == S_FAST_RECONNECT));
1227 if (GNUNET_YES != success)
1230 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1231 "Failed to switch connected peer `%s' to address '%s' session %X, asking ATS for new address \n",
1232 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1234 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1235 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1237 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1238 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1240 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1242 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1243 GNUNET_HELLO_address_free (cc->address);
1247 /* Tell ATS that switching addresses was successful */
1251 if (n->address_state == FRESH)
1253 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES);
1254 GNUNET_ATS_address_update (GST_ats, cc->address, cc->session, NULL, 0);
1255 if (cc->session != n->session)
1257 GNUNET_ATS_address_in_use (GST_ats, cc->address, cc->session, GNUNET_YES);
1258 n->address_state = USED;
1261 case S_FAST_RECONNECT:
1263 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1264 "Successful fast reconnect to peer `%s'\n",
1265 GNUNET_i2s (&n->id));
1267 change_state (n, S_CONNECTED);
1268 neighbours_connected++;
1269 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
1272 if (n->address_state == FRESH)
1274 GST_validation_set_address_use (cc->address, cc->session, GNUNET_YES);
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)
1282 GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
1284 /* Updating quotas */
1285 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1286 send_outbound_quota (target, n->bandwidth_out);
1291 GNUNET_HELLO_address_free (cc->address);
1297 * We tried to send a SESSION_CONNECT message to another peer. If this
1298 * succeeded, we change the state. If it failed, we should tell
1299 * ATS to not use this address anymore (until it is re-validated).
1301 * @param cls the 'struct NeighbourMapEntry'
1302 * @param target peer to send the message to
1303 * @param success GNUNET_OK on success
1306 send_connect_ack_continuation (void *cls,
1307 const struct GNUNET_PeerIdentity *target,
1310 struct ContinutionContext *cc = cls;
1311 struct NeighbourMapEntry *n;
1313 if (neighbours == NULL)
1315 GNUNET_HELLO_address_free (cc->address);
1317 return; /* neighbour is going away */
1320 n = lookup_neighbour (&cc->address->peer);
1321 if ((n == NULL) || (is_disconnecting (n)))
1323 GNUNET_HELLO_address_free (cc->address);
1325 return; /* neighbour is going away */
1328 if (GNUNET_YES == success)
1330 GNUNET_HELLO_address_free (cc->address);
1332 return; /* sending successful */
1335 /* sending failed, ask for next address */
1337 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1338 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1339 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
1341 change_state (n, S_NOT_CONNECTED);
1342 GNUNET_assert (strlen (cc->address->transport_name) > 0);
1343 GNUNET_ATS_address_destroyed (GST_ats, cc->address, cc->session);
1345 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1346 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1348 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1350 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1351 GNUNET_HELLO_address_free (cc->address);
1357 * For an existing neighbour record, set the active connection to
1358 * use the given address.
1360 * @param peer identity of the peer to switch the address for
1361 * @param address address of the other peer, NULL if other peer
1363 * @param session session to use (or NULL)
1364 * @param ats performance data
1365 * @param ats_count number of entries in ats
1366 * @param bandwidth_in inbound quota to be used when connection is up
1367 * @param bandwidth_out outbound quota to be used when connection is up
1368 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1369 * connection is not up (yet)
1372 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
1373 const struct GNUNET_HELLO_Address
1375 struct Session *session,
1376 const struct GNUNET_ATS_Information *ats,
1378 struct GNUNET_BANDWIDTH_Value32NBO
1380 struct GNUNET_BANDWIDTH_Value32NBO
1383 struct NeighbourMapEntry *n;
1384 struct SessionConnectMessage connect_msg;
1385 struct ContinutionContext *cc;
1389 if (neighbours == NULL)
1391 /* This can happen during shutdown */
1394 n = lookup_neighbour (peer);
1397 if (n->state == S_DISCONNECT)
1399 /* We are disconnecting, nothing to do here */
1402 GNUNET_assert (address->transport_name != NULL);
1403 if ((session == NULL) && (0 == address->address_length))
1405 GNUNET_break_op (0);
1406 /* FIXME: is this actually possible? When does this happen? */
1407 if (strlen (address->transport_name) > 0)
1408 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1409 GNUNET_ATS_suggest_address (GST_ats, peer);
1413 /* checks successful and neighbour != NULL */
1414 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1415 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1416 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>",
1419 print_state (n->state));
1421 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1423 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1424 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1426 /* do not switch addresses just update quotas */
1427 if ((n->state == S_CONNECTED) && (NULL != n->address) &&
1428 (0 == GNUNET_HELLO_address_cmp (address, n->address)) &&
1429 (n->session == session))
1431 n->bandwidth_in = bandwidth_in;
1432 n->bandwidth_out = bandwidth_out;
1433 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1434 send_outbound_quota (peer, n->bandwidth_out);
1437 if (n->state == S_CONNECTED)
1439 /* mark old address as no longer used */
1440 GNUNET_assert (NULL != n->address);
1441 if (n->address_state == USED)
1443 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
1444 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1445 n->address_state = UNUSED;
1449 /* set new address */
1450 if (NULL != n->address)
1451 GNUNET_HELLO_address_free (n->address);
1452 n->address = GNUNET_HELLO_address_copy (address);
1453 n->address_state = FRESH;
1454 n->bandwidth_in = bandwidth_in;
1455 n->bandwidth_out = bandwidth_out;
1456 GNUNET_SCHEDULER_cancel (n->timeout_task);
1458 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1459 &neighbour_timeout_task, n);
1461 if (NULL != address_change_cb && n->state == S_CONNECTED)
1462 address_change_cb (callback_cls, &n->id, n->address);
1464 /* Obtain an session for this address from plugin */
1465 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1466 papi = GST_plugins_find (address->transport_name);
1467 GNUNET_assert (papi != NULL);
1468 if (session == NULL)
1470 n->session = papi->get_session (papi->cls, address);
1471 /* Session could not be initiated */
1472 if (n->session == NULL)
1474 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1475 "Failed to obtain new session %p for peer `%s' and address '%s'\n",
1476 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1478 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1480 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1481 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1482 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1485 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1486 GNUNET_HELLO_address_free (n->address);
1492 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1493 "Obtained new session %p for peer `%s' and address '%s'\n",
1494 n->session, GNUNET_i2s (&n->id), GST_plugins_a2s (n->address));
1495 /* Telling ATS about new session */
1496 GNUNET_ATS_address_update (GST_ats, n->address, n->session, NULL, 0);
1500 n->session = session;
1501 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1502 "Using existing session %p for peer `%s' and address '%s'\n",
1504 GNUNET_i2s (&n->id),
1505 (address->address_length != 0) ? GST_plugins_a2s (address): "<inbound>");
1510 case S_NOT_CONNECTED:
1511 case S_CONNECT_SENT:
1512 msg_len = sizeof (struct SessionConnectMessage);
1513 connect_msg.header.size = htons (msg_len);
1514 connect_msg.header.type =
1515 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1516 connect_msg.reserved = htonl (0);
1517 connect_msg.timestamp =
1518 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1520 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1521 cc->session = n->session;
1522 cc->address = GNUNET_HELLO_address_copy (address);
1524 ret = send_with_session (n,
1525 (const char *) &connect_msg, msg_len,
1526 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1527 &send_connect_continuation, cc);
1530 case S_CONNECT_RECV:
1531 /* We received a CONNECT message and asked ATS for an address */
1532 msg_len = sizeof (struct SessionConnectMessage);
1533 connect_msg.header.size = htons (msg_len);
1534 connect_msg.header.type =
1535 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1536 connect_msg.reserved = htonl (0);
1537 connect_msg.timestamp =
1538 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1539 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1540 cc->session = n->session;
1541 cc->address = GNUNET_HELLO_address_copy (address);
1543 ret = send_with_session(n,
1544 (const void *) &connect_msg, msg_len,
1545 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1546 &send_connect_ack_continuation,
1550 case S_FAST_RECONNECT:
1551 /* connected peer is switching addresses or tries fast reconnect */
1552 msg_len = sizeof (struct SessionConnectMessage);
1553 connect_msg.header.size = htons (msg_len);
1554 connect_msg.header.type =
1555 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1556 connect_msg.reserved = htonl (0);
1557 connect_msg.timestamp =
1558 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1559 cc = GNUNET_malloc (sizeof (struct ContinutionContext));
1560 cc->session = n->session;
1561 cc->address = GNUNET_HELLO_address_copy (address);
1562 ret = send_with_session(n,
1563 (const void *) &connect_msg, msg_len,
1564 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
1565 &send_switch_address_continuation, cc);
1566 if (ret == GNUNET_SYSERR)
1568 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1569 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1570 GNUNET_i2s (peer), GST_plugins_a2s (address), session);
1574 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1575 "Invalid connection state to switch addresses %u \n", n->state);
1576 GNUNET_break_op (0);
1583 * Obtain current latency information for the given neighbour.
1586 * @return observed latency of the address, FOREVER if the address was
1587 * never successfully validated
1589 struct GNUNET_TIME_Relative
1590 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1592 struct NeighbourMapEntry *n;
1594 n = lookup_neighbour (peer);
1595 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1596 return GNUNET_TIME_UNIT_FOREVER_REL;
1602 * Obtain current address information for the given neighbour.
1605 * @return address currently used
1607 struct GNUNET_HELLO_Address *
1608 GST_neighbour_get_current_address (const struct GNUNET_PeerIdentity *peer)
1610 struct NeighbourMapEntry *n;
1612 n = lookup_neighbour (peer);
1613 if ((NULL == n) || ((n->address == NULL) && (n->session == NULL)))
1622 * Create an entry in the neighbour map for the given peer
1624 * @param peer peer to create an entry for
1625 * @return new neighbour map entry
1627 static struct NeighbourMapEntry *
1628 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1630 struct NeighbourMapEntry *n;
1633 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1634 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1636 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1638 n->state = S_NOT_CONNECTED;
1639 n->latency = GNUNET_TIME_relative_get_forever ();
1640 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1641 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1642 MAX_BANDWIDTH_CARRY_S);
1644 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1645 &neighbour_timeout_task, n);
1646 GNUNET_assert (GNUNET_OK ==
1647 GNUNET_CONTAINER_multihashmap_put (neighbours,
1648 &n->id.hashPubKey, n,
1649 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1655 * Try to create a connection to the given target (eventually).
1657 * @param target peer to try to connect to
1660 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1662 struct NeighbourMapEntry *n;
1664 // This can happen during shutdown
1665 if (neighbours == NULL)
1670 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1671 GNUNET_i2s (target));
1674 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1679 n = lookup_neighbour (target);
1683 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1684 return; /* already connecting or connected */
1685 if (is_disconnecting (n))
1686 change_state (n, S_NOT_CONNECTED);
1691 n = setup_neighbour (target);
1693 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1694 "Asking ATS for suggested address to connect to peer `%s'\n",
1695 GNUNET_i2s (&n->id));
1698 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1702 * Test if we're connected to the given peer.
1704 * @param target peer to test
1705 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1708 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1710 struct NeighbourMapEntry *n;
1712 // This can happen during shutdown
1713 if (neighbours == NULL)
1718 n = lookup_neighbour (target);
1720 if ((NULL == n) || (S_CONNECTED != n->state))
1721 return GNUNET_NO; /* not connected */
1726 * A session was terminated. Take note.
1728 * @param peer identity of the peer where the session died
1729 * @param session session that is gone
1732 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1733 struct Session *session)
1735 struct NeighbourMapEntry *n;
1737 if (neighbours == NULL)
1739 /* This can happen during shutdown */
1744 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1745 session, GNUNET_i2s (peer));
1748 n = lookup_neighbour (peer);
1751 if (session != n->session)
1752 return; /* doesn't affect us */
1753 if (n->state == S_CONNECTED)
1755 if (n->address_state == USED)
1757 GST_validation_set_address_use (n->address, n->session, GNUNET_NO);
1758 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1759 n->address_state = UNUSED;
1763 if (NULL != n->address)
1765 GNUNET_HELLO_address_free (n->address);
1770 /* not connected anymore anyway, shouldn't matter */
1771 if (S_CONNECTED != n->state)
1774 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1776 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1777 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1778 n->expect_latency_response = GNUNET_NO;
1781 /* connected, try fast reconnect */
1782 /* statistics "transport" : "# peers connected" -= 1
1783 * neighbours_connected -= 1
1784 * BUT: no disconnect_cb to notify clients about disconnect
1787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying fast reconnect to peer `%s'\n",
1790 GNUNET_assert (neighbours_connected > 0);
1791 change_state (n, S_FAST_RECONNECT);
1792 neighbours_connected--;
1793 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
1797 /* We are connected, so ask ATS to switch addresses */
1798 GNUNET_SCHEDULER_cancel (n->timeout_task);
1799 n->timeout_task = GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1800 &neighbour_timeout_task, n);
1801 /* try QUICKLY to re-establish a connection, reduce timeout! */
1802 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1803 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1804 n->ats_suggest = GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT,
1805 &ats_suggest_cancel,
1807 GNUNET_ATS_suggest_address (GST_ats, peer);
1812 * Transmit a message to the given target using the active connection.
1814 * @param target destination
1815 * @param msg message to send
1816 * @param msg_size number of bytes in msg
1817 * @param timeout when to fail with timeout
1818 * @param cont function to call when done
1819 * @param cont_cls closure for 'cont'
1822 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1823 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1824 GST_NeighbourSendContinuation cont, void *cont_cls)
1826 struct NeighbourMapEntry *n;
1827 struct MessageQueue *mq;
1829 // This can happen during shutdown
1830 if (neighbours == NULL)
1835 n = lookup_neighbour (target);
1836 if ((n == NULL) || (!is_connected (n)))
1838 GNUNET_STATISTICS_update (GST_stats,
1840 ("# messages not sent (no such peer or not connected)"),
1844 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1845 "Could not send message to peer `%s': unknown neighbour",
1846 GNUNET_i2s (target));
1847 else if (!is_connected (n))
1848 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1849 "Could not send message to peer `%s': not connected\n",
1850 GNUNET_i2s (target));
1853 cont (cont_cls, GNUNET_SYSERR);
1857 if ((n->session == NULL) && (n->address == NULL))
1859 GNUNET_STATISTICS_update (GST_stats,
1861 ("# messages not sent (no such peer or not connected)"),
1864 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1865 "Could not send message to peer `%s': no address available\n",
1866 GNUNET_i2s (target));
1870 cont (cont_cls, GNUNET_SYSERR);
1874 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1875 GNUNET_STATISTICS_update (GST_stats,
1877 ("# bytes in message queue for other peers"),
1878 msg_size, GNUNET_NO);
1879 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1881 mq->cont_cls = cont_cls;
1882 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1883 memcpy (&mq[1], msg, msg_size);
1884 mq->message_buf = (const char *) &mq[1];
1885 mq->message_buf_size = msg_size;
1886 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1887 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1889 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1890 (NULL == n->is_active))
1891 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1896 * We have received a message from the given sender. How long should
1897 * we delay before receiving more? (Also used to keep the peer marked
1900 * @param sender sender of the message
1901 * @param size size of the message
1902 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1903 * GNUNET_NO if the neighbour is not connected or violates the quota,
1904 * GNUNET_SYSERR if the connection is not fully up yet
1905 * @return how long to wait before reading more from this sender
1907 struct GNUNET_TIME_Relative
1908 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1909 *sender, ssize_t size, int *do_forward)
1911 struct NeighbourMapEntry *n;
1912 struct GNUNET_TIME_Relative ret;
1914 // This can happen during shutdown
1915 if (neighbours == NULL)
1917 return GNUNET_TIME_UNIT_FOREVER_REL;
1920 n = lookup_neighbour (sender);
1923 GST_neighbours_try_connect (sender);
1924 n = lookup_neighbour (sender);
1927 GNUNET_STATISTICS_update (GST_stats,
1929 ("# messages discarded due to lack of neighbour record"),
1931 *do_forward = GNUNET_NO;
1932 return GNUNET_TIME_UNIT_ZERO;
1935 if (!is_connected (n))
1937 *do_forward = GNUNET_SYSERR;
1938 return GNUNET_TIME_UNIT_ZERO;
1940 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1942 n->quota_violation_count++;
1944 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1945 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1946 n->in_tracker.available_bytes_per_s__,
1947 n->quota_violation_count);
1949 /* Discount 32k per violation */
1950 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1954 if (n->quota_violation_count > 0)
1956 /* try to add 32k back */
1957 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1958 n->quota_violation_count--;
1961 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1963 GNUNET_STATISTICS_update (GST_stats,
1965 ("# bandwidth quota violations by other peers"),
1967 *do_forward = GNUNET_NO;
1968 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1970 *do_forward = GNUNET_YES;
1971 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1972 if (ret.rel_value > 0)
1975 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1976 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1977 (unsigned long long) n->in_tracker.
1978 consumption_since_last_update__,
1979 (unsigned int) n->in_tracker.available_bytes_per_s__,
1980 (unsigned long long) ret.rel_value);
1982 GNUNET_STATISTICS_update (GST_stats,
1983 gettext_noop ("# ms throttling suggested"),
1984 (int64_t) ret.rel_value, GNUNET_NO);
1991 * Keep the connection to the given neighbour alive longer,
1992 * we received a KEEPALIVE (or equivalent).
1994 * @param neighbour neighbour to keep alive
1997 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1999 struct NeighbourMapEntry *n;
2001 // This can happen during shutdown
2002 if (neighbours == NULL)
2007 n = lookup_neighbour (neighbour);
2010 GNUNET_STATISTICS_update (GST_stats,
2012 ("# KEEPALIVE messages discarded (not connected)"),
2016 GNUNET_SCHEDULER_cancel (n->timeout_task);
2018 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
2019 &neighbour_timeout_task, n);
2021 /* send reply to measure latency */
2022 if (S_CONNECTED != n->state)
2025 struct GNUNET_MessageHeader m;
2027 m.size = htons (sizeof (struct GNUNET_MessageHeader));
2028 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE_RESPONSE);
2030 send_with_session(n,
2031 (const void *) &m, sizeof (m),
2033 GNUNET_TIME_UNIT_FOREVER_REL,
2038 * We received a KEEP_ALIVE_RESPONSE message and use this to calculate latency
2041 * @param neighbour neighbour to keep alive
2042 * @param ats performance data
2043 * @param ats_count number of entries in ats
2046 GST_neighbours_keepalive_response (const struct GNUNET_PeerIdentity *neighbour,
2047 const struct GNUNET_ATS_Information *ats,
2050 struct NeighbourMapEntry *n;
2051 struct GNUNET_ATS_Information *ats_new;
2054 if (neighbours == NULL)
2056 // This can happen during shutdown
2060 n = lookup_neighbour (neighbour);
2061 if ((NULL == n) || (n->state != S_CONNECTED))
2063 GNUNET_STATISTICS_update (GST_stats,
2065 ("# KEEPALIVE_RESPONSE messages discarded (not connected)"),
2069 if (n->expect_latency_response != GNUNET_YES)
2071 GNUNET_STATISTICS_update (GST_stats,
2073 ("# KEEPALIVE_RESPONSE messages discarded (not expected)"),
2077 n->expect_latency_response = GNUNET_NO;
2079 GNUNET_assert (n->keep_alive_sent.abs_value !=
2080 GNUNET_TIME_absolute_get_zero ().abs_value);
2082 GNUNET_TIME_absolute_get_difference (n->keep_alive_sent,
2083 GNUNET_TIME_absolute_get ());
2085 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Latency for peer `%s' is %llu ms\n",
2086 GNUNET_i2s (&n->id), n->latency.rel_value);
2090 if (n->latency.rel_value == GNUNET_TIME_relative_get_forever ().rel_value)
2092 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats, ats_count);
2097 GNUNET_malloc (sizeof (struct GNUNET_ATS_Information) *
2099 memcpy (ats_new, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2102 ats_new[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2103 if (n->latency.rel_value > UINT32_MAX)
2104 latency = UINT32_MAX;
2106 latency = n->latency.rel_value;
2107 ats_new[ats_count].value = htonl (latency);
2109 GNUNET_ATS_address_update (GST_ats, n->address, n->session, ats_new,
2111 GNUNET_free (ats_new);
2117 * Change the incoming quota for the given peer.
2119 * @param neighbour identity of peer to change qutoa for
2120 * @param quota new quota
2123 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
2124 struct GNUNET_BANDWIDTH_Value32NBO quota)
2126 struct NeighbourMapEntry *n;
2128 // This can happen during shutdown
2129 if (neighbours == NULL)
2134 n = lookup_neighbour (neighbour);
2137 GNUNET_STATISTICS_update (GST_stats,
2139 ("# SET QUOTA messages ignored (no such peer)"),
2144 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2145 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
2146 ntohl (quota.value__), GNUNET_i2s (&n->id));
2148 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
2149 if (0 != ntohl (quota.value__))
2152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
2153 GNUNET_i2s (&n->id), "SET_QUOTA");
2155 if (is_connected (n))
2156 GNUNET_STATISTICS_update (GST_stats,
2157 gettext_noop ("# disconnects due to quota of 0"),
2159 disconnect_neighbour (n);
2164 * Closure for the neighbours_iterate function.
2166 struct IteratorContext
2169 * Function to call on each connected neighbour.
2171 GST_NeighbourIterator cb;
2181 * Call the callback from the closure for each connected neighbour.
2183 * @param cls the 'struct IteratorContext'
2184 * @param key the hash of the public key of the neighbour
2185 * @param value the 'struct NeighbourMapEntry'
2186 * @return GNUNET_OK (continue to iterate)
2189 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
2191 struct IteratorContext *ic = cls;
2192 struct NeighbourMapEntry *n = value;
2194 if (!is_connected (n))
2197 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
2203 * Iterate over all connected neighbours.
2205 * @param cb function to call
2206 * @param cb_cls closure for cb
2209 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
2211 struct IteratorContext ic;
2213 // This can happen during shutdown
2214 if (neighbours == NULL)
2221 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
2225 * If we have an active connection to the given target, it must be shutdown.
2227 * @param target peer to disconnect from
2230 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
2232 struct NeighbourMapEntry *n;
2234 // This can happen during shutdown
2235 if (neighbours == NULL)
2240 n = lookup_neighbour (target);
2242 return; /* not active */
2243 disconnect_neighbour (n);
2248 * We received a disconnect message from the given peer,
2249 * validate and process.
2251 * @param peer sender of the message
2252 * @param msg the disconnect message
2255 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
2257 const struct GNUNET_MessageHeader
2260 struct NeighbourMapEntry *n;
2261 const struct SessionDisconnectMessage *sdm;
2265 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2266 "Received DISCONNECT message from peer `%s'\n",
2270 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
2272 // GNUNET_break_op (0);
2273 GNUNET_STATISTICS_update (GST_stats,
2275 ("# disconnect messages ignored (old format)"), 1,
2279 sdm = (const struct SessionDisconnectMessage *) msg;
2280 n = lookup_neighbour (peer);
2282 return; /* gone already */
2283 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
2284 n->connect_ts.abs_value)
2286 GNUNET_STATISTICS_update (GST_stats,
2288 ("# disconnect messages ignored (timestamp)"), 1,
2292 GNUNET_CRYPTO_hash (&sdm->public_key,
2293 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
2295 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
2297 GNUNET_break_op (0);
2300 if (ntohl (sdm->purpose.size) !=
2301 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
2302 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
2303 sizeof (struct GNUNET_TIME_AbsoluteNBO))
2305 GNUNET_break_op (0);
2309 GNUNET_CRYPTO_rsa_verify
2310 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
2311 &sdm->signature, &sdm->public_key))
2313 GNUNET_break_op (0);
2316 GST_neighbours_force_disconnect (peer);
2321 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
2322 * Consider switching to it.
2324 * @param message possibly a 'struct SessionConnectMessage' (check format)
2325 * @param peer identity of the peer to switch the address for
2326 * @param address address of the other peer, NULL if other peer
2328 * @param session session to use (or NULL)
2329 * @param ats performance data
2330 * @param ats_count number of entries in ats
2333 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2334 const struct GNUNET_PeerIdentity *peer,
2335 const struct GNUNET_HELLO_Address *address,
2336 struct Session *session,
2337 const struct GNUNET_ATS_Information *ats,
2340 const struct SessionConnectMessage *scm;
2341 struct GNUNET_MessageHeader msg;
2342 struct NeighbourMapEntry *n;
2347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2348 "Received CONNECT_ACK message from peer `%s'\n",
2352 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2354 GNUNET_break_op (0);
2357 scm = (const struct SessionConnectMessage *) message;
2358 GNUNET_break_op (ntohl (scm->reserved) == 0);
2359 n = lookup_neighbour (peer);
2362 /* we did not send 'CONNECT' -- at least not recently */
2363 GNUNET_STATISTICS_update (GST_stats,
2365 ("# unexpected CONNECT_ACK messages (no peer)"),
2372 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2374 * We also received an CONNECT message, switched from SENDT to RECV and
2375 * ATS already suggested us an address after a successful blacklist check
2377 if ((n->state != S_CONNECT_SENT) &&
2378 ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2380 GNUNET_STATISTICS_update (GST_stats,
2382 ("# unexpected CONNECT_ACK messages"), 1,
2387 change_state (n, S_CONNECTED);
2389 if (NULL != session)
2391 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2393 "Giving ATS session %p of plugin %s for peer %s\n",
2394 session, address->transport_name, GNUNET_i2s (peer));
2396 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2397 GNUNET_assert (NULL != n->address);
2399 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2401 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2402 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2403 n->address_state = USED;
2406 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2408 /* send ACK (ACK) */
2409 msg_len = sizeof (msg);
2410 msg.size = htons (msg_len);
2411 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2413 ret = send_with_session(n,
2414 (const char *) &msg, msg_len,
2415 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL,
2418 if (ret == GNUNET_SYSERR)
2419 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2420 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2421 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session);
2424 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2425 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2427 neighbours_connected++;
2428 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2431 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2432 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2433 GNUNET_i2s (&n->id), 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);
2443 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2444 const struct GNUNET_PeerIdentity *peer,
2445 const struct GNUNET_HELLO_Address *address,
2446 struct Session *session,
2447 const struct GNUNET_ATS_Information *ats,
2450 struct NeighbourMapEntry *n;
2453 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2457 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2459 GNUNET_break_op (0);
2462 n = lookup_neighbour (peer);
2468 if (S_CONNECTED == n->state)
2470 if (!is_connecting (n))
2472 GNUNET_STATISTICS_update (GST_stats,
2473 gettext_noop ("# unexpected ACK messages"), 1,
2477 change_state (n, S_CONNECTED);
2478 if (NULL != session)
2479 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2481 "Giving ATS session %p of plugin %s for peer %s\n",
2482 session, address->transport_name, GNUNET_i2s (peer));
2483 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2484 GNUNET_assert (n->address != NULL);
2486 if ((n->address_state == FRESH) && (0 == GNUNET_HELLO_address_cmp(address, n->address)))
2488 GST_validation_set_address_use (n->address, n->session, GNUNET_YES);
2489 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2490 n->address_state = USED;
2494 neighbours_connected++;
2495 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2498 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2499 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2500 n->keepalive_task = GNUNET_SCHEDULER_add_now (&neighbour_keepalive_task, n);
2502 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2503 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2504 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address), n->session,
2507 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2508 send_outbound_quota (peer, n->bandwidth_out);
2511 struct BlackListCheckContext
2513 struct GNUNET_ATS_Information *ats;
2517 struct Session *session;
2519 struct GNUNET_HELLO_Address *address;
2521 struct GNUNET_TIME_Absolute ts;
2526 handle_connect_blacklist_cont (void *cls,
2527 const struct GNUNET_PeerIdentity *peer,
2530 struct NeighbourMapEntry *n;
2531 struct BlackListCheckContext *bcc = cls;
2534 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2535 "Blacklist check due to CONNECT message: `%s'\n",
2537 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2541 if (GNUNET_OK != result)
2543 GNUNET_HELLO_address_free (bcc->address);
2548 n = lookup_neighbour (peer);
2550 n = setup_neighbour (peer);
2552 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2554 if (NULL != bcc->session)
2555 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2557 "Giving ATS session %p of address `%s' for peer %s\n",
2558 bcc->session, GST_plugins_a2s (bcc->address),
2560 /* Tell ATS about the session, so ATS can suggest it if it likes it. */
2562 GNUNET_ATS_address_update (GST_ats, bcc->address, bcc->session, bcc->ats,
2564 n->connect_ts = bcc->ts;
2567 GNUNET_HELLO_address_free (bcc->address);
2570 if (n->state != S_CONNECT_RECV)
2571 change_state (n, S_CONNECT_RECV);
2574 /* Ask ATS for an address to connect via that address */
2575 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2576 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2578 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2580 GNUNET_ATS_suggest_address (GST_ats, peer);
2584 * We received a 'SESSION_CONNECT' message from the other peer.
2585 * Consider switching to it.
2587 * @param message possibly a 'struct SessionConnectMessage' (check format)
2588 * @param peer identity of the peer to switch the address for
2589 * @param address address of the other peer, NULL if other peer
2591 * @param session session to use (or NULL)
2592 * @param ats performance data
2593 * @param ats_count number of entries in ats (excluding 0-termination)
2596 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2597 const struct GNUNET_PeerIdentity *peer,
2598 const struct GNUNET_HELLO_Address *address,
2599 struct Session *session,
2600 const struct GNUNET_ATS_Information *ats,
2603 const struct SessionConnectMessage *scm;
2604 struct BlackListCheckContext *bcc = NULL;
2605 struct NeighbourMapEntry *n;
2608 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2609 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2612 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2614 GNUNET_break_op (0);
2618 scm = (const struct SessionConnectMessage *) message;
2619 GNUNET_break_op (ntohl (scm->reserved) == 0);
2621 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2623 n = lookup_neighbour (peer);
2624 if ((n != NULL) && (S_CONNECTED == n->state))
2626 /* connected peer switches addresses */
2631 /* we are not connected to this peer */
2632 /* do blacklist check */
2634 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2635 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2636 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2637 bcc->ats_count = ats_count + 1;
2638 bcc->address = GNUNET_HELLO_address_copy (address);
2639 bcc->session = session;
2640 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2641 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2642 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2643 bcc->ats[ats_count].value =
2644 htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2645 GST_blacklist_test_allowed (peer, address->transport_name,
2646 &handle_connect_blacklist_cont, bcc);
2650 /* end of file gnunet-service-transport_neighbours.c */