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?
52 * (idle timeout is 5 minutes or 300 seconds, so with 90s interval we
53 * send 3 keepalives in each interval, so 3 messages would need to be
54 * lost in a row for a disconnect).
56 #define KEEPALIVE_FREQUENCY GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 90)
59 #define ATS_RESPONSE_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 3)
62 #define SETUP_CONNECTION_TIMEOUT GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 15)
66 * Entry in neighbours.
68 struct NeighbourMapEntry;
71 * Message a peer sends to another to indicate its
72 * preference for communicating via a particular
73 * session (and the desire to establish a real
76 struct SessionConnectMessage
79 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
81 struct GNUNET_MessageHeader header;
86 uint32_t reserved GNUNET_PACKED;
89 * Absolute time at the sender. Only the most recent connect
90 * message implies which session is preferred by the sender.
92 struct GNUNET_TIME_AbsoluteNBO timestamp;
97 struct SessionDisconnectMessage
100 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
102 struct GNUNET_MessageHeader header;
107 uint32_t reserved GNUNET_PACKED;
110 * Purpose of the signature. Extends over the timestamp.
111 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
113 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
116 * Absolute time at the sender. Only the most recent connect
117 * message implies which session is preferred by the sender.
119 struct GNUNET_TIME_AbsoluteNBO timestamp;
122 * Public key of the sender.
124 struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded public_key;
127 * Signature of the peer that sends us the disconnect. Only
128 * valid if the timestamp is AFTER the timestamp from the
129 * corresponding 'CONNECT' message.
131 struct GNUNET_CRYPTO_RsaSignature signature;
137 * For each neighbour we keep a list of messages
138 * that we still want to transmit to the neighbour.
144 * This is a doubly linked list.
146 struct MessageQueue *next;
149 * This is a doubly linked list.
151 struct MessageQueue *prev;
154 * Once this message is actively being transmitted, which
155 * neighbour is it associated with?
157 struct NeighbourMapEntry *n;
160 * Function to call once we're done.
162 GST_NeighbourSendContinuation cont;
170 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
171 * stuck together in memory. Allocated at the end of this struct.
173 const char *message_buf;
176 * Size of the message buf
178 size_t message_buf_size;
181 * At what time should we fail?
183 struct GNUNET_TIME_Absolute timeout;
191 * fresh peer or completely disconnected
196 * sent CONNECT message to other peer, waiting for CONNECT_ACK
201 * received CONNECT message to other peer, sending CONNECT_ACK
206 * received ACK or payload
211 * connection ended, fast reconnect
216 * Disconnect in progress
223 * Entry in neighbours.
225 struct NeighbourMapEntry
229 * Head of list of messages we would like to send to this peer;
230 * must contain at most one message per client.
232 struct MessageQueue *messages_head;
235 * Tail of list of messages we would like to send to this peer; must
236 * contain at most one message per client.
238 struct MessageQueue *messages_tail;
241 * Performance data for the peer.
243 //struct GNUNET_ATS_Information *ats;
246 * Are we currently trying to send a message? If so, which one?
248 struct MessageQueue *is_active;
251 * Active session for communicating with the peer.
253 struct Session *session;
256 * Address we currently use.
258 struct GNUNET_HELLO_Address *address;
261 * Identity of this neighbour.
263 struct GNUNET_PeerIdentity id;
266 * ID of task scheduled to run when this peer is about to
267 * time out (will free resources associated with the peer).
269 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
272 * ID of task scheduled to send keepalives.
274 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
277 * ID of task scheduled to run when we should try transmitting
278 * the head of the message queue.
280 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
283 * Tracker for inbound bandwidth.
285 struct GNUNET_BANDWIDTH_Tracker in_tracker;
288 * Inbound bandwidth from ATS, activated when connection is up
290 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
293 * Inbound bandwidth from ATS, activated when connection is up
295 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
298 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
300 struct GNUNET_TIME_Absolute connect_ts;
304 * We asked ATS for a new address for this peer
306 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
309 * Task the resets the peer state after due to an pending
310 * unsuccessful connection setup
312 GNUNET_SCHEDULER_TaskIdentifier state_reset;
315 * How often has the other peer (recently) violated the inbound
316 * traffic limit? Incremented by 10 per violation, decremented by 1
317 * per non-violation (for each time interval).
319 unsigned int quota_violation_count;
323 * The current state of the peer
324 * Element of enum State
332 * All known neighbours and their HELLOs.
334 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
337 * Closure for connect_notify_cb and disconnect_notify_cb
339 static void *callback_cls;
342 * Function to call when we connected to a neighbour.
344 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
347 * Function to call when we disconnected from a neighbour.
349 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
352 * counter for connected neighbours
354 static int neighbours_connected;
357 * Lookup a neighbour entry in the neighbours hash map.
359 * @param pid identity of the peer to look up
360 * @return the entry, NULL if there is no existing record
362 static struct NeighbourMapEntry *
363 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
365 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
368 #define change_state(n, state, ...) change (n, state, __LINE__)
371 is_connecting (struct NeighbourMapEntry *n)
373 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
379 is_connected (struct NeighbourMapEntry *n)
381 if (n->state == S_CONNECTED)
387 is_disconnecting (struct NeighbourMapEntry *n)
389 if (n->state == S_DISCONNECT)
395 print_state (int state)
400 return "S_CONNECTED";
403 return "S_CONNECT_RECV";
406 return "S_CONNECT_SENT";
409 return "S_DISCONNECT";
411 case S_NOT_CONNECTED:
412 return "S_NOT_CONNECTED";
414 case S_FAST_RECONNECT:
415 return "S_FAST_RECONNECT";
425 change (struct NeighbourMapEntry *n, int state, int line);
428 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
431 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
433 struct NeighbourMapEntry *n = cls;
435 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
439 /* This jut a temporary debug message to check if a the value
440 * SETUP_CONNECTION_TIMEOUT was choosen to small for slow machines
442 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
443 "Information for developers: Connection to peer `%s' %s failed in state `%s', resetting connection attempt \n",
444 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address),
445 print_state (n->state));
447 GNUNET_STATISTICS_update (GST_stats,
449 ("# failed connection attempts due to timeout"), 1,
452 /* resetting state */
453 n->state = S_NOT_CONNECTED;
455 /* destroying address */
456 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
458 /* request new address */
459 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
460 GNUNET_SCHEDULER_cancel (n->ats_suggest);
462 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
464 GNUNET_ATS_suggest_address (GST_ats, &n->id);
468 change (struct NeighbourMapEntry *n, int state, int line)
470 /* allowed transitions */
471 int allowed = GNUNET_NO;
475 case S_NOT_CONNECTED:
476 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
477 (state == S_DISCONNECT))
478 allowed = GNUNET_YES;
481 allowed = GNUNET_YES;
484 allowed = GNUNET_YES;
487 if ((state == S_DISCONNECT) || (state == S_FAST_RECONNECT))
488 allowed = GNUNET_YES;
492 case S_FAST_RECONNECT:
493 if ((state == S_CONNECT_SENT) || (state == S_DISCONNECT))
494 allowed = GNUNET_YES;
500 if (allowed == GNUNET_NO)
502 char *old = GNUNET_strdup (print_state (n->state));
503 char *new = GNUNET_strdup (print_state (state));
504 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
505 "Illegal state transition from `%s' to `%s' in line %u \n", old,
510 return GNUNET_SYSERR;
514 char *old = GNUNET_strdup (print_state (n->state));
515 char *new = GNUNET_strdup (print_state (state));
516 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
517 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
518 GNUNET_i2s (&n->id), n, old, new, line);
527 case S_FAST_RECONNECT:
530 if (n->state_reset != GNUNET_SCHEDULER_NO_TASK)
531 GNUNET_SCHEDULER_cancel (n->state_reset);
533 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task,
537 case S_NOT_CONNECTED:
539 if (GNUNET_SCHEDULER_NO_TASK != n->state_reset)
542 char *old = GNUNET_strdup (print_state (n->state));
543 char *new = GNUNET_strdup (print_state (state));
544 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
545 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
546 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address),
551 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
552 GNUNET_SCHEDULER_cancel (n->state_reset);
553 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
567 send_with_plugin (const struct GNUNET_PeerIdentity *target, const char *msgbuf,
568 size_t msgbuf_size, uint32_t priority,
569 struct GNUNET_TIME_Relative timeout, struct Session *session,
570 const struct GNUNET_HELLO_Address *address,
571 int force_address, GNUNET_TRANSPORT_TransmitContinuation cont,
574 struct GNUNET_TRANSPORT_PluginFunctions *papi;
575 size_t ret = GNUNET_SYSERR;
577 /* FIXME : ats returns an address with all values 0 */
581 cont (cont_cls, target, GNUNET_SYSERR);
582 return GNUNET_SYSERR;
585 if ((session == NULL) && (address == NULL))
588 cont (cont_cls, target, GNUNET_SYSERR);
589 return GNUNET_SYSERR;
592 papi = GST_plugins_find (address->transport_name);
596 cont (cont_cls, target, GNUNET_SYSERR);
597 return GNUNET_SYSERR;
601 papi->send (papi->cls, target, msgbuf, msgbuf_size, 0, timeout, session,
603 address->address_length, GNUNET_YES, cont, cont_cls);
608 cont (cont_cls, target, GNUNET_SYSERR);
614 * Task invoked to start a transmission to another peer.
616 * @param cls the 'struct NeighbourMapEntry'
617 * @param tc scheduler context
620 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
624 * We're done with our transmission attempt, continue processing.
626 * @param cls the 'struct MessageQueue' of the message
627 * @param receiver intended receiver
628 * @param success whether it worked or not
631 transmit_send_continuation (void *cls,
632 const struct GNUNET_PeerIdentity *receiver,
635 struct MessageQueue *mq;
636 struct NeighbourMapEntry *n;
642 GNUNET_assert (n->is_active == mq);
644 if (success == GNUNET_YES)
646 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
647 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
651 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
652 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
653 (success == GNUNET_OK) ? "successful" : "FAILED");
655 if (NULL != mq->cont)
656 mq->cont (mq->cont_cls, success);
662 * Check the ready list for the given neighbour and if a plugin is
663 * ready for transmission (and if we have a message), do so!
665 * @param n target peer for which to transmit
668 try_transmission_to_peer (struct NeighbourMapEntry *n)
670 struct MessageQueue *mq;
671 struct GNUNET_TIME_Relative timeout;
674 if (n->is_active != NULL)
677 return; /* transmission already pending */
679 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
682 return; /* currently waiting for bandwidth */
684 while (NULL != (mq = n->messages_head))
686 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
687 if (timeout.rel_value > 0)
689 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
692 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
695 return; /* no more messages */
697 if (GST_plugins_find (n->address->transport_name) == NULL)
702 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
706 if ( (n->session == NULL) && (NULL == n->address) )
708 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
709 "No address for peer `%s'\n",
710 GNUNET_i2s (&n->id));
711 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
712 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
713 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
718 send_with_plugin (&n->id, mq->message_buf, mq->message_buf_size, 0,
719 timeout, n->session, n->address,
720 GNUNET_YES, &transmit_send_continuation,
724 /* failure, but 'send' would not call continuation in this case,
725 * so we need to do it here! */
726 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
733 * Task invoked to start a transmission to another peer.
735 * @param cls the 'struct NeighbourMapEntry'
736 * @param tc scheduler context
739 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
741 struct NeighbourMapEntry *n = cls;
743 GNUNET_assert (NULL != lookup_neighbour (&n->id));
744 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
745 try_transmission_to_peer (n);
750 * Initialize the neighbours subsystem.
752 * @param cls closure for callbacks
753 * @param connect_cb function to call if we connect to a peer
754 * @param disconnect_cb function to call if we disconnect from a peer
757 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
758 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
761 connect_notify_cb = connect_cb;
762 disconnect_notify_cb = disconnect_cb;
763 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
768 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
772 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
773 "Sending DISCONNECT message to peer `%4s': %i\n",
774 GNUNET_i2s (target), result);
780 send_disconnect (const struct GNUNET_PeerIdentity *target,
781 const struct GNUNET_HELLO_Address *address,
782 struct Session *session)
785 struct SessionDisconnectMessage disconnect_msg;
788 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
789 "Sending DISCONNECT message to peer `%4s'\n",
790 GNUNET_i2s (target));
793 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
794 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
795 disconnect_msg.reserved = htonl (0);
796 disconnect_msg.purpose.size =
797 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
798 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
799 sizeof (struct GNUNET_TIME_AbsoluteNBO));
800 disconnect_msg.purpose.purpose =
801 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
802 disconnect_msg.timestamp =
803 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
804 disconnect_msg.public_key = GST_my_public_key;
805 GNUNET_assert (GNUNET_OK ==
806 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
807 &disconnect_msg.purpose,
808 &disconnect_msg.signature));
811 send_with_plugin (target, (const char *) &disconnect_msg,
812 sizeof (disconnect_msg), UINT32_MAX,
813 GNUNET_TIME_UNIT_FOREVER_REL, session, address,
815 &send_disconnect_cont, NULL);
817 if (ret == GNUNET_SYSERR)
818 return GNUNET_SYSERR;
820 GNUNET_STATISTICS_update (GST_stats,
822 ("# peers disconnected due to external request"), 1,
828 * Disconnect from the given neighbour, clean up the record.
830 * @param n neighbour to disconnect from
833 disconnect_neighbour (struct NeighbourMapEntry *n)
835 struct MessageQueue *mq;
838 previous_state = n->state;
840 if (is_disconnecting (n))
843 /* send DISCONNECT MESSAGE */
844 if ((previous_state == S_CONNECTED)|| is_connecting (n))
847 send_disconnect (&n->id, n->address,
849 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
850 GNUNET_i2s (&n->id));
852 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
853 "Could not send DISCONNECT_MSG to `%s'\n",
854 GNUNET_i2s (&n->id));
857 change_state (n, S_DISCONNECT);
858 GST_validation_set_address_use (&n->id,
863 if (n->state == S_CONNECTED)
865 GNUNET_assert (NULL != n->address);
866 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
869 if (n->address != NULL)
871 struct GNUNET_TRANSPORT_PluginFunctions *papi;
872 papi = GST_plugins_find (n->address->transport_name);
874 papi->disconnect (papi->cls, &n->id);
876 while (NULL != (mq = n->messages_head))
878 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
879 if (NULL != mq->cont)
880 mq->cont (mq->cont_cls, GNUNET_SYSERR);
883 if (NULL != n->is_active)
885 n->is_active->n = NULL;
889 switch (previous_state) {
891 GNUNET_assert (neighbours_connected > 0);
892 neighbours_connected--;
893 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
894 GNUNET_SCHEDULER_cancel (n->keepalive_task);
895 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
896 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
898 disconnect_notify_cb (callback_cls, &n->id);
900 case S_FAST_RECONNECT:
901 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
903 disconnect_notify_cb (callback_cls, &n->id);
908 GNUNET_ATS_suggest_address_cancel (GST_ats, &n->id);
910 GNUNET_assert (GNUNET_YES ==
911 GNUNET_CONTAINER_multihashmap_remove (neighbours,
912 &n->id.hashPubKey, n));
913 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
915 GNUNET_SCHEDULER_cancel (n->ats_suggest);
916 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
918 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
920 GNUNET_SCHEDULER_cancel (n->timeout_task);
921 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
923 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
925 GNUNET_SCHEDULER_cancel (n->transmission_task);
926 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
928 if (NULL != n->address)
930 GNUNET_HELLO_address_free (n->address);
934 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
935 GNUNET_i2s (&n->id), n);
941 * Peer has been idle for too long. Disconnect.
943 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
944 * @param tc scheduler context
947 neighbour_timeout_task (void *cls,
948 const struct GNUNET_SCHEDULER_TaskContext *tc)
950 struct NeighbourMapEntry *n = cls;
952 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
954 GNUNET_STATISTICS_update (GST_stats,
956 ("# peers disconnected due to timeout"), 1,
958 disconnect_neighbour (n);
963 * Send another keepalive message.
965 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
966 * @param tc scheduler context
969 neighbour_keepalive_task (void *cls,
970 const struct GNUNET_SCHEDULER_TaskContext *tc)
972 struct NeighbourMapEntry *n = cls;
973 struct GNUNET_MessageHeader m;
976 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
977 &neighbour_keepalive_task, n);
978 GNUNET_assert (S_CONNECTED == n->state);
979 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
981 m.size = htons (sizeof (struct GNUNET_MessageHeader));
982 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
984 send_with_plugin (&n->id, (const void *) &m, sizeof (m),
985 UINT32_MAX /* priority */ ,
986 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
987 GNUNET_YES, NULL, NULL);
992 * Disconnect from the given neighbour.
995 * @param key hash of neighbour's public key (not used)
996 * @param value the 'struct NeighbourMapEntry' of the neighbour
999 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
1001 struct NeighbourMapEntry *n = value;
1004 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
1005 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
1007 if (S_CONNECTED == n->state)
1008 GNUNET_STATISTICS_update (GST_stats,
1010 ("# peers disconnected due to global disconnect"),
1012 disconnect_neighbour (n);
1018 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1020 struct NeighbourMapEntry *n = cls;
1022 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1024 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1025 " ATS did not suggested address to connect to peer `%s'\n",
1026 GNUNET_i2s (&n->id));
1028 disconnect_neighbour (n);
1032 * Cleanup the neighbours subsystem.
1035 GST_neighbours_stop ()
1037 // This can happen during shutdown
1038 if (neighbours == NULL)
1043 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1045 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1046 GNUNET_assert (neighbours_connected == 0);
1048 callback_cls = NULL;
1049 connect_notify_cb = NULL;
1050 disconnect_notify_cb = NULL;
1055 * We tried to send a SESSION_CONNECT message to another peer. If this
1056 * succeeded, we change the state. If it failed, we should tell
1057 * ATS to not use this address anymore (until it is re-validated).
1059 * @param cls the 'struct GNUNET_HELLO_Address' of the address that was tried
1060 * @param success GNUNET_OK on success
1063 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1066 struct GNUNET_HELLO_Address *address = cls;
1067 struct NeighbourMapEntry *n;
1069 if (GNUNET_YES != success)
1070 GNUNET_ATS_address_destroyed (GST_ats, address, NULL);
1071 if ( (NULL == neighbours) ||
1072 (NULL == (n = lookup_neighbour (&address->peer))) ||
1073 (n->state == S_DISCONNECT) ||
1074 (GNUNET_YES == success) )
1076 GNUNET_HELLO_address_free (address);
1080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1081 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %p, asking ATS for new address \n",
1082 GNUNET_i2s (&n->id),
1083 GST_plugins_a2s (n->address),
1086 change_state (n, S_NOT_CONNECTED);
1087 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1088 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1090 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1092 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1093 GNUNET_HELLO_address_free (address);
1098 * We tried to switch addresses with an peer already connected. If it failed,
1099 * we should tell ATS to not use this address anymore (until it is re-validated).
1101 * @param cls the 'struct NeighbourMapEntry'
1102 * @param success GNUNET_OK on success
1105 send_switch_address_continuation (void *cls,
1106 const struct GNUNET_PeerIdentity *target,
1109 struct NeighbourMapEntry *n = cls;
1111 GNUNET_assert (n != NULL);
1112 if (is_disconnecting (n))
1113 return; /* neighbour is going away */
1115 GNUNET_assert (n->state == S_CONNECTED);
1116 if (GNUNET_YES != success)
1119 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1120 "Failed to switch connected peer `%s' to address '%s' session %X, asking ATS for new address \n",
1121 GNUNET_i2s (&n->id),
1122 GST_plugins_a2s (n->address), n->session);
1125 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1127 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1128 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1130 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1132 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1135 /* Tell ATS that switching addresses was successful */
1136 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
1140 * We tried to send a SESSION_CONNECT message to another peer. If this
1141 * succeeded, we change the state. If it failed, we should tell
1142 * ATS to not use this address anymore (until it is re-validated).
1144 * @param cls the 'struct NeighbourMapEntry'
1145 * @param success GNUNET_OK on success
1148 send_connect_ack_continuation (void *cls,
1149 const struct GNUNET_PeerIdentity *target,
1152 struct NeighbourMapEntry *n = cls;
1154 GNUNET_assert (n != NULL);
1156 if (GNUNET_YES == success)
1157 return; /* sending successful */
1159 /* sending failed, ask for next address */
1161 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1162 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1163 GNUNET_i2s (&n->id),
1164 GST_plugins_a2s (n->address),
1167 change_state (n, S_NOT_CONNECTED);
1169 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1171 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1172 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1174 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1176 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1181 * For an existing neighbour record, set the active connection to
1182 * the given address.
1184 * @param peer identity of the peer to switch the address for
1185 * @param address address of the other peer, NULL if other peer
1187 * @param session session to use (or NULL)
1188 * @param ats performance data
1189 * @param ats_count number of entries in ats
1190 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1191 * connection is not up (yet)
1194 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1195 const struct GNUNET_HELLO_Address *address,
1196 struct Session *session,
1197 const struct GNUNET_ATS_Information *ats,
1199 struct GNUNET_BANDWIDTH_Value32NBO
1201 struct GNUNET_BANDWIDTH_Value32NBO
1204 struct NeighbourMapEntry *n;
1205 struct SessionConnectMessage connect_msg;
1209 if (neighbours == NULL)
1211 /* This can happen during shutdown */
1214 n = lookup_neighbour (peer);
1217 if (n->state == S_DISCONNECT)
1219 /* We are disconnecting, nothing to do here */
1222 GNUNET_assert (address->transport_name != NULL);
1223 if ( (session == NULL) && (0 == address->address_length) )
1225 GNUNET_break_op (0);
1226 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1227 GNUNET_ATS_suggest_address (GST_ats, peer);
1231 /* checks successful and neighbour != NULL */
1233 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1234 "ATS tells us to switch to address '%s' session %p for %s peer `%s'\n",
1235 GST_plugins_a2s (address),
1236 session, (S_CONNECTED == n->state) ? "CONNECTED" : "NOT CONNECTED",
1239 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1241 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1242 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1244 /* do not switch addresses just update quotas */
1245 if ( (n->state == S_CONNECTED) &&
1246 (NULL != n->address) &&
1247 (0 == GNUNET_HELLO_address_cmp (address,
1249 (n->session == session) )
1251 struct QuotaSetMessage q_msg;
1254 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1255 "Sending outbound quota of %u Bps and inbound quota of %u Bps for peer `%s' to all clients\n",
1256 ntohl (n->bandwidth_out.value__),
1257 ntohl (n->bandwidth_in.value__), GNUNET_i2s (peer));
1260 n->bandwidth_in = bandwidth_in;
1261 n->bandwidth_out = bandwidth_out;
1262 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1264 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1265 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1266 q_msg.quota = n->bandwidth_out;
1267 q_msg.peer = (*peer);
1268 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1271 if (n->state == S_CONNECTED)
1273 /* mark old address as no longer used */
1274 GNUNET_assert (NULL != n->address);
1275 GST_validation_set_address_use (&n->id,
1279 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1282 /* set new address */
1283 if (NULL != n->address)
1284 GNUNET_HELLO_address_free (n->address);
1285 n->address = GNUNET_HELLO_address_copy (address);
1286 n->session = session;
1287 n->bandwidth_in = bandwidth_in;
1288 n->bandwidth_out = bandwidth_out;
1289 GNUNET_SCHEDULER_cancel (n->timeout_task);
1291 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1292 &neighbour_timeout_task, n);
1295 case S_NOT_CONNECTED:
1296 case S_CONNECT_SENT:
1297 case S_FAST_RECONNECT:
1298 msg_len = sizeof (struct SessionConnectMessage);
1299 connect_msg.header.size = htons (msg_len);
1300 connect_msg.header.type =
1301 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1302 connect_msg.reserved = htonl (0);
1303 connect_msg.timestamp =
1304 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1305 change_state (n, S_CONNECT_SENT);
1307 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1308 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1309 address, GNUNET_YES,
1310 &send_connect_continuation,
1311 GNUNET_HELLO_address_copy (address));
1313 case S_CONNECT_RECV:
1314 /* We received a CONNECT message and asked ATS for an address */
1315 msg_len = sizeof (struct SessionConnectMessage);
1316 connect_msg.header.size = htons (msg_len);
1317 connect_msg.header.type =
1318 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1319 connect_msg.reserved = htonl (0);
1320 connect_msg.timestamp =
1321 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1323 send_with_plugin (&n->id, (const void *) &connect_msg, msg_len,
1324 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1325 address, GNUNET_YES,
1326 &send_connect_ack_continuation, n);
1329 /* connected peer is switching addresses */
1330 GST_validation_set_address_use (&n->id,
1334 msg_len = sizeof (struct SessionConnectMessage);
1335 connect_msg.header.size = htons (msg_len);
1336 connect_msg.header.type =
1337 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1338 connect_msg.reserved = htonl (0);
1339 connect_msg.timestamp =
1340 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1342 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1343 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1344 address, GNUNET_YES,
1345 &send_switch_address_continuation, n);
1346 if (ret == GNUNET_SYSERR)
1348 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1349 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1351 GST_plugins_a2s (address), session);
1355 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1356 "Invalid connection state to switch addresses %u \n", n->state);
1357 GNUNET_break_op (0);
1364 * Obtain current latency information for the given neighbour.
1367 * @return observed latency of the address, FOREVER if the address was
1368 * never successfully validated
1370 struct GNUNET_TIME_Relative
1371 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1373 struct NeighbourMapEntry *n;
1375 n = lookup_neighbour (peer);
1377 ( (n->address == NULL) && (n->session == NULL) ) )
1378 return GNUNET_TIME_UNIT_FOREVER_REL;
1379 return GST_validation_get_address_latency (peer,
1386 * Create an entry in the neighbour map for the given peer
1388 * @param peer peer to create an entry for
1389 * @return new neighbour map entry
1391 static struct NeighbourMapEntry *
1392 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1394 struct NeighbourMapEntry *n;
1397 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1398 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1400 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1402 n->state = S_NOT_CONNECTED;
1403 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1404 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1405 MAX_BANDWIDTH_CARRY_S);
1407 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1408 &neighbour_timeout_task, n);
1409 GNUNET_assert (GNUNET_OK ==
1410 GNUNET_CONTAINER_multihashmap_put (neighbours,
1411 &n->id.hashPubKey, n,
1412 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1418 * Try to create a connection to the given target (eventually).
1420 * @param target peer to try to connect to
1423 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1425 struct NeighbourMapEntry *n;
1427 // This can happen during shutdown
1428 if (neighbours == NULL)
1433 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1434 GNUNET_i2s (target));
1437 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1442 n = lookup_neighbour (target);
1446 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1447 return; /* already connecting or connected */
1448 if (is_disconnecting (n))
1449 change_state (n, S_NOT_CONNECTED);
1454 n = setup_neighbour (target);
1456 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1457 "Asking ATS for suggested address to connect to peer `%s'\n",
1458 GNUNET_i2s (&n->id));
1461 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1465 * Test if we're connected to the given peer.
1467 * @param target peer to test
1468 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1471 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1473 struct NeighbourMapEntry *n;
1475 // This can happen during shutdown
1476 if (neighbours == NULL)
1481 n = lookup_neighbour (target);
1483 if ((NULL == n) || (S_CONNECTED != n->state))
1484 return GNUNET_NO; /* not connected */
1490 * A session was terminated. Take note.
1492 * @param peer identity of the peer where the session died
1493 * @param session session that is gone
1496 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1497 struct Session *session)
1499 struct NeighbourMapEntry *n;
1501 if (neighbours == NULL)
1503 /* This can happen during shutdown */
1508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1509 session, GNUNET_i2s (peer));
1512 n = lookup_neighbour (peer);
1515 if (session != n->session)
1516 return; /* doesn't affect us */
1517 if (n->state == S_CONNECTED)
1518 GST_validation_set_address_use (&n->id,
1523 if (NULL != n->address)
1525 GNUNET_HELLO_address_free (n->address);
1529 /* not connected anymore anyway, shouldn't matter */
1530 if ((S_CONNECTED != n->state) && (!is_connecting (n)))
1532 change_state (n, S_FAST_RECONNECT);
1534 GNUNET_assert (neighbours_connected > 0);
1535 neighbours_connected--;
1537 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1539 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1540 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1543 /* We are connected, so ask ATS to switch addresses */
1544 GNUNET_SCHEDULER_cancel (n->timeout_task);
1546 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1547 &neighbour_timeout_task, n);
1548 /* try QUICKLY to re-establish a connection, reduce timeout! */
1549 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1550 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1552 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1554 GNUNET_ATS_suggest_address (GST_ats, peer);
1559 * Transmit a message to the given target using the active connection.
1561 * @param target destination
1562 * @param msg message to send
1563 * @param msg_size number of bytes in msg
1564 * @param timeout when to fail with timeout
1565 * @param cont function to call when done
1566 * @param cont_cls closure for 'cont'
1569 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1570 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1571 GST_NeighbourSendContinuation cont, void *cont_cls)
1573 struct NeighbourMapEntry *n;
1574 struct MessageQueue *mq;
1576 // This can happen during shutdown
1577 if (neighbours == NULL)
1582 n = lookup_neighbour (target);
1583 if ((n == NULL) || (!is_connected (n)))
1585 GNUNET_STATISTICS_update (GST_stats,
1587 ("# messages not sent (no such peer or not connected)"),
1591 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1592 "Could not send message to peer `%s': unknown neighbour",
1593 GNUNET_i2s (target));
1594 else if (!is_connected (n))
1595 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1596 "Could not send message to peer `%s': not connected\n",
1597 GNUNET_i2s (target));
1600 cont (cont_cls, GNUNET_SYSERR);
1604 if ((n->session == NULL) && (n->address == NULL) )
1606 GNUNET_STATISTICS_update (GST_stats,
1608 ("# messages not sent (no such peer or not connected)"),
1611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1612 "Could not send message to peer `%s': no address available\n",
1613 GNUNET_i2s (target));
1617 cont (cont_cls, GNUNET_SYSERR);
1621 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1622 GNUNET_STATISTICS_update (GST_stats,
1624 ("# bytes in message queue for other peers"),
1625 msg_size, GNUNET_NO);
1626 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1628 mq->cont_cls = cont_cls;
1629 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1630 memcpy (&mq[1], msg, msg_size);
1631 mq->message_buf = (const char *) &mq[1];
1632 mq->message_buf_size = msg_size;
1633 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1634 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1636 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1637 (NULL == n->is_active))
1638 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1643 * We have received a message from the given sender. How long should
1644 * we delay before receiving more? (Also used to keep the peer marked
1647 * @param sender sender of the message
1648 * @param size size of the message
1649 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1650 * GNUNET_NO if the neighbour is not connected or violates the quota,
1651 * GNUNET_SYSERR if the connection is not fully up yet
1652 * @return how long to wait before reading more from this sender
1654 struct GNUNET_TIME_Relative
1655 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1656 *sender, ssize_t size, int *do_forward)
1658 struct NeighbourMapEntry *n;
1659 struct GNUNET_TIME_Relative ret;
1661 // This can happen during shutdown
1662 if (neighbours == NULL)
1664 return GNUNET_TIME_UNIT_FOREVER_REL;
1667 n = lookup_neighbour (sender);
1670 GST_neighbours_try_connect (sender);
1671 n = lookup_neighbour (sender);
1674 GNUNET_STATISTICS_update (GST_stats,
1676 ("# messages discarded due to lack of neighbour record"),
1678 *do_forward = GNUNET_NO;
1679 return GNUNET_TIME_UNIT_ZERO;
1682 if (!is_connected (n))
1684 *do_forward = GNUNET_SYSERR;
1685 return GNUNET_TIME_UNIT_ZERO;
1687 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1689 n->quota_violation_count++;
1691 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1692 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1693 n->in_tracker.available_bytes_per_s__,
1694 n->quota_violation_count);
1696 /* Discount 32k per violation */
1697 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1701 if (n->quota_violation_count > 0)
1703 /* try to add 32k back */
1704 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1705 n->quota_violation_count--;
1708 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1710 GNUNET_STATISTICS_update (GST_stats,
1712 ("# bandwidth quota violations by other peers"),
1714 *do_forward = GNUNET_NO;
1715 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1717 *do_forward = GNUNET_YES;
1718 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1719 if (ret.rel_value > 0)
1722 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1723 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1724 (unsigned long long) n->in_tracker.
1725 consumption_since_last_update__,
1726 (unsigned int) n->in_tracker.available_bytes_per_s__,
1727 (unsigned long long) ret.rel_value);
1729 GNUNET_STATISTICS_update (GST_stats,
1730 gettext_noop ("# ms throttling suggested"),
1731 (int64_t) ret.rel_value, GNUNET_NO);
1738 * Keep the connection to the given neighbour alive longer,
1739 * we received a KEEPALIVE (or equivalent).
1741 * @param neighbour neighbour to keep alive
1744 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1746 struct NeighbourMapEntry *n;
1748 // This can happen during shutdown
1749 if (neighbours == NULL)
1754 n = lookup_neighbour (neighbour);
1757 GNUNET_STATISTICS_update (GST_stats,
1759 ("# KEEPALIVE messages discarded (not connected)"),
1763 GNUNET_SCHEDULER_cancel (n->timeout_task);
1765 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1766 &neighbour_timeout_task, n);
1771 * Change the incoming quota for the given peer.
1773 * @param neighbour identity of peer to change qutoa for
1774 * @param quota new quota
1777 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1778 struct GNUNET_BANDWIDTH_Value32NBO quota)
1780 struct NeighbourMapEntry *n;
1782 // This can happen during shutdown
1783 if (neighbours == NULL)
1788 n = lookup_neighbour (neighbour);
1791 GNUNET_STATISTICS_update (GST_stats,
1793 ("# SET QUOTA messages ignored (no such peer)"),
1797 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1798 if (0 != ntohl (quota.value__))
1801 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1802 GNUNET_i2s (&n->id), "SET_QUOTA");
1804 if (is_connected (n))
1805 GNUNET_STATISTICS_update (GST_stats,
1806 gettext_noop ("# disconnects due to quota of 0"),
1808 disconnect_neighbour (n);
1813 * Closure for the neighbours_iterate function.
1815 struct IteratorContext
1818 * Function to call on each connected neighbour.
1820 GST_NeighbourIterator cb;
1830 * Call the callback from the closure for each connected neighbour.
1832 * @param cls the 'struct IteratorContext'
1833 * @param key the hash of the public key of the neighbour
1834 * @param value the 'struct NeighbourMapEntry'
1835 * @return GNUNET_OK (continue to iterate)
1838 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1840 struct IteratorContext *ic = cls;
1841 struct NeighbourMapEntry *n = value;
1843 if (!is_connected (n))
1846 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
1852 * Iterate over all connected neighbours.
1854 * @param cb function to call
1855 * @param cb_cls closure for cb
1858 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1860 struct IteratorContext ic;
1862 // This can happen during shutdown
1863 if (neighbours == NULL)
1870 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1874 * If we have an active connection to the given target, it must be shutdown.
1876 * @param target peer to disconnect from
1879 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1881 struct NeighbourMapEntry *n;
1883 // This can happen during shutdown
1884 if (neighbours == NULL)
1889 n = lookup_neighbour (target);
1891 return; /* not active */
1892 disconnect_neighbour (n);
1897 * We received a disconnect message from the given peer,
1898 * validate and process.
1900 * @param peer sender of the message
1901 * @param msg the disconnect message
1904 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
1906 const struct GNUNET_MessageHeader
1909 struct NeighbourMapEntry *n;
1910 const struct SessionDisconnectMessage *sdm;
1914 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1915 "Received DISCONNECT message from peer `%s'\n",
1919 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1921 // GNUNET_break_op (0);
1922 GNUNET_STATISTICS_update (GST_stats,
1924 ("# disconnect messages ignored (old format)"), 1,
1928 sdm = (const struct SessionDisconnectMessage *) msg;
1929 n = lookup_neighbour (peer);
1931 return; /* gone already */
1932 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1933 n->connect_ts.abs_value)
1935 GNUNET_STATISTICS_update (GST_stats,
1937 ("# disconnect messages ignored (timestamp)"), 1,
1941 GNUNET_CRYPTO_hash (&sdm->public_key,
1942 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1944 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
1946 GNUNET_break_op (0);
1949 if (ntohl (sdm->purpose.size) !=
1950 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1951 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1952 sizeof (struct GNUNET_TIME_AbsoluteNBO))
1954 GNUNET_break_op (0);
1958 GNUNET_CRYPTO_rsa_verify
1959 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
1960 &sdm->signature, &sdm->public_key))
1962 GNUNET_break_op (0);
1965 GST_neighbours_force_disconnect (peer);
1970 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
1971 * Consider switching to it.
1973 * @param message possibly a 'struct SessionConnectMessage' (check format)
1974 * @param peer identity of the peer to switch the address for
1975 * @param address address of the other peer, NULL if other peer
1977 * @param session session to use (or NULL)
1978 * @param ats performance data
1979 * @param ats_count number of entries in ats
1982 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
1983 const struct GNUNET_PeerIdentity *peer,
1984 const struct GNUNET_HELLO_Address *address,
1985 struct Session *session,
1986 const struct GNUNET_ATS_Information *ats,
1989 const struct SessionConnectMessage *scm;
1990 struct QuotaSetMessage q_msg;
1991 struct GNUNET_MessageHeader msg;
1992 struct NeighbourMapEntry *n;
1997 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1998 "Received CONNECT_ACK message from peer `%s'\n",
2001 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2003 GNUNET_break_op (0);
2006 scm = (const struct SessionConnectMessage *) message;
2007 GNUNET_break_op (ntohl (scm->reserved) == 0);
2008 n = lookup_neighbour (peer);
2011 /* we did not send 'CONNECT' (how could we? no record for this peer!) */
2012 GNUNET_break_op (0);
2015 if (n->state != S_CONNECT_SENT)
2017 GNUNET_STATISTICS_update (GST_stats,
2018 gettext_noop ("# unexpected CONNECT_ACK messages"), 1,
2022 if (NULL != session)
2023 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2025 "Giving ATS session %p of plugin %s for peer %s\n",
2026 session, address->transport_name, GNUNET_i2s (peer));
2027 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2028 GNUNET_assert (NULL != n->address);
2029 change_state (n, S_CONNECTED);
2030 GST_validation_set_address_use (&n->id,
2034 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2036 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2037 "Setting inbound quota of %u for peer `%s' to \n",
2038 ntohl (n->bandwidth_in.value__), GNUNET_i2s (&n->id));
2040 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2042 /* send ACK (ACK) */
2043 msg_len = sizeof (msg);
2044 msg.size = htons (msg_len);
2045 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2048 send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2049 GNUNET_TIME_UNIT_FOREVER_REL, n->session,
2050 n->address, GNUNET_YES, NULL,
2053 if (ret == GNUNET_SYSERR)
2054 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2055 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2056 GNUNET_i2s (&n->id),
2057 GST_plugins_a2s (n->address), n->session);
2060 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2062 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2063 &neighbour_keepalive_task, n);
2065 neighbours_connected++;
2066 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2069 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2070 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2071 GNUNET_i2s (&n->id),
2072 GST_plugins_a2s (n->address), n->session,
2075 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2078 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2079 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2080 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2082 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2083 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2084 q_msg.quota = n->bandwidth_out;
2085 q_msg.peer = (*peer);
2086 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2092 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2093 const struct GNUNET_PeerIdentity *peer,
2094 const struct GNUNET_HELLO_Address *address,
2095 struct Session *session,
2096 const struct GNUNET_ATS_Information *ats,
2099 struct NeighbourMapEntry *n;
2100 struct QuotaSetMessage q_msg;
2103 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2104 "Received ACK message from peer `%s'\n",
2108 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2110 GNUNET_break_op (0);
2113 n = lookup_neighbour (peer);
2116 send_disconnect (peer, address,
2121 if (S_CONNECTED == n->state)
2123 if (!is_connecting(n))
2125 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# unexpected ACK messages"), 1,
2129 if (NULL != session)
2130 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2132 "Giving ATS session %p of plugin %s for peer %s\n",
2133 session, address->transport_name, GNUNET_i2s (peer));
2134 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2135 GNUNET_assert (n->address != NULL);
2136 change_state (n, S_CONNECTED);
2137 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2139 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2141 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2143 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2144 &neighbour_keepalive_task, n);
2145 GST_validation_set_address_use (&n->id,
2149 neighbours_connected++;
2150 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2154 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2155 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2156 GNUNET_i2s (&n->id),
2157 GST_plugins_a2s (n->address), n->session,
2160 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2162 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2163 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2164 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2166 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2167 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2168 q_msg.quota = n->bandwidth_out;
2169 q_msg.peer = (*peer);
2170 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2173 struct BlackListCheckContext
2175 struct GNUNET_ATS_Information *ats;
2179 struct Session *session;
2181 struct GNUNET_HELLO_Address *address;
2183 struct GNUNET_TIME_Absolute ts;
2188 handle_connect_blacklist_cont (void *cls,
2189 const struct GNUNET_PeerIdentity *peer,
2192 struct NeighbourMapEntry *n;
2193 struct BlackListCheckContext *bcc = cls;
2196 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2197 "Blacklist check due to CONNECT message: `%s'\n",
2199 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2203 if (GNUNET_OK != result)
2205 GNUNET_HELLO_address_free (bcc->address);
2210 n = lookup_neighbour (peer);
2212 n = setup_neighbour (peer);
2214 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2216 if (NULL != bcc->session)
2217 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2219 "Giving ATS session %p of address `%s' for peer %s\n",
2221 GST_plugins_a2s (bcc->address),
2223 GNUNET_ATS_address_update (GST_ats, bcc->address,
2224 bcc->session, bcc->ats, bcc->ats_count);
2225 n->connect_ts = bcc->ts;
2230 if (n->state != S_CONNECT_RECV)
2231 change_state (n, S_CONNECT_RECV);
2233 /* Ask ATS for an address to connect via that address */
2234 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2235 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2237 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2239 GNUNET_ATS_suggest_address (GST_ats, peer);
2243 * We received a 'SESSION_CONNECT' message from the other peer.
2244 * Consider switching to it.
2246 * @param message possibly a 'struct SessionConnectMessage' (check format)
2247 * @param peer identity of the peer to switch the address for
2248 * @param address address of the other peer, NULL if other peer
2250 * @param session session to use (or NULL)
2251 * @param ats performance data
2252 * @param ats_count number of entries in ats (excluding 0-termination)
2255 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2256 const struct GNUNET_PeerIdentity *peer,
2257 const struct GNUNET_HELLO_Address *address,
2258 struct Session *session,
2259 const struct GNUNET_ATS_Information *ats,
2262 const struct SessionConnectMessage *scm;
2263 struct NeighbourMapEntry *n;
2264 struct BlackListCheckContext *bcc = NULL;
2267 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2268 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2271 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2273 GNUNET_break_op (0);
2277 scm = (const struct SessionConnectMessage *) message;
2278 GNUNET_break_op (ntohl (scm->reserved) == 0);
2280 n = lookup_neighbour (peer);
2282 (S_CONNECTED == n->state) )
2284 /* connected peer switches addresses */
2285 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2289 /* we are not connected to this peer */
2290 /* do blacklist check */
2292 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2293 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2294 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2295 bcc->ats_count = ats_count + 1;
2296 bcc->address = GNUNET_HELLO_address_copy (address);
2297 bcc->session = session;
2298 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2299 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2300 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2301 bcc->ats[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2302 GST_blacklist_test_allowed (peer, address->transport_name, handle_connect_blacklist_cont,
2307 /* end of file gnunet-service-transport_neighbours.c */