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);
1155 if (GNUNET_YES == success)
1156 return; /* sending successful */
1158 /* sending failed, ask for next address */
1160 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1161 "Failed to send CONNECT_MSG to peer `%4s' with address '%s' session %X, asking ATS for new address \n",
1162 GNUNET_i2s (&n->id),
1163 GST_plugins_a2s (n->address),
1166 change_state (n, S_NOT_CONNECTED);
1168 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1170 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1171 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1173 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1175 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1180 * For an existing neighbour record, set the active connection to
1181 * the given address.
1183 * @param peer identity of the peer to switch the address for
1184 * @param address address of the other peer, NULL if other peer
1186 * @param session session to use (or NULL)
1187 * @param ats performance data
1188 * @param ats_count number of entries in ats
1189 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1190 * connection is not up (yet)
1193 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1194 const struct GNUNET_HELLO_Address *address,
1195 struct Session *session,
1196 const struct GNUNET_ATS_Information *ats,
1198 struct GNUNET_BANDWIDTH_Value32NBO
1200 struct GNUNET_BANDWIDTH_Value32NBO
1203 struct NeighbourMapEntry *n;
1204 struct SessionConnectMessage connect_msg;
1208 if (neighbours == NULL)
1210 /* This can happen during shutdown */
1213 n = lookup_neighbour (peer);
1216 if (n->state == S_DISCONNECT)
1218 /* We are disconnecting, nothing to do here */
1221 GNUNET_assert (address->transport_name != NULL);
1222 if ( (session == NULL) && (0 == address->address_length) )
1224 GNUNET_break_op (0);
1225 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1226 GNUNET_ATS_suggest_address (GST_ats, peer);
1230 /* checks successful and neighbour != NULL */
1232 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1233 "ATS tells us to switch to address '%s' session %p for peer `%s' in state `%s'\n",
1234 GST_plugins_a2s (address),
1237 print_state(n->state));
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;
1253 n->bandwidth_in = bandwidth_in;
1254 n->bandwidth_out = bandwidth_out;
1255 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1258 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1259 "Sending outbound quota of %u Bps and inbound quota of %u Bps for peer `%s' to all clients\n",
1260 ntohl (n->bandwidth_out.value__),
1261 ntohl (n->bandwidth_in.value__), GNUNET_i2s (peer));
1263 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1264 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1265 q_msg.quota = n->bandwidth_out;
1266 q_msg.peer = (*peer);
1267 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1270 if (n->state == S_CONNECTED)
1272 /* mark old address as no longer used */
1273 GNUNET_assert (NULL != n->address);
1274 GST_validation_set_address_use (&n->id,
1278 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1281 /* set new address */
1282 if (NULL != n->address)
1283 GNUNET_HELLO_address_free (n->address);
1284 n->address = GNUNET_HELLO_address_copy (address);
1285 n->session = session;
1286 n->bandwidth_in = bandwidth_in;
1287 n->bandwidth_out = bandwidth_out;
1288 GNUNET_SCHEDULER_cancel (n->timeout_task);
1290 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1291 &neighbour_timeout_task, n);
1294 case S_NOT_CONNECTED:
1295 case S_CONNECT_SENT:
1296 case S_FAST_RECONNECT:
1297 msg_len = sizeof (struct SessionConnectMessage);
1298 connect_msg.header.size = htons (msg_len);
1299 connect_msg.header.type =
1300 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1301 connect_msg.reserved = htonl (0);
1302 connect_msg.timestamp =
1303 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1304 change_state (n, S_CONNECT_SENT);
1306 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1307 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1308 address, GNUNET_YES,
1309 &send_connect_continuation,
1310 GNUNET_HELLO_address_copy (address));
1312 case S_CONNECT_RECV:
1313 /* We received a CONNECT message and asked ATS for an address */
1314 msg_len = sizeof (struct SessionConnectMessage);
1315 connect_msg.header.size = htons (msg_len);
1316 connect_msg.header.type =
1317 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1318 connect_msg.reserved = htonl (0);
1319 connect_msg.timestamp =
1320 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1322 send_with_plugin (&n->id, (const void *) &connect_msg, msg_len,
1323 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1324 address, GNUNET_YES,
1325 &send_connect_ack_continuation, n);
1328 /* connected peer is switching addresses */
1329 GST_validation_set_address_use (&n->id,
1333 msg_len = sizeof (struct SessionConnectMessage);
1334 connect_msg.header.size = htons (msg_len);
1335 connect_msg.header.type =
1336 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1337 connect_msg.reserved = htonl (0);
1338 connect_msg.timestamp =
1339 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1341 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1342 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1343 address, GNUNET_YES,
1344 &send_switch_address_continuation, n);
1345 if (ret == GNUNET_SYSERR)
1347 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1348 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1350 GST_plugins_a2s (address), session);
1354 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1355 "Invalid connection state to switch addresses %u \n", n->state);
1356 GNUNET_break_op (0);
1363 * Obtain current latency information for the given neighbour.
1366 * @return observed latency of the address, FOREVER if the address was
1367 * never successfully validated
1369 struct GNUNET_TIME_Relative
1370 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1372 struct NeighbourMapEntry *n;
1374 n = lookup_neighbour (peer);
1376 ( (n->address == NULL) && (n->session == NULL) ) )
1377 return GNUNET_TIME_UNIT_FOREVER_REL;
1378 return GST_validation_get_address_latency (peer,
1385 * Create an entry in the neighbour map for the given peer
1387 * @param peer peer to create an entry for
1388 * @return new neighbour map entry
1390 static struct NeighbourMapEntry *
1391 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1393 struct NeighbourMapEntry *n;
1396 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1397 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1399 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1401 n->state = S_NOT_CONNECTED;
1402 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1403 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1404 MAX_BANDWIDTH_CARRY_S);
1406 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1407 &neighbour_timeout_task, n);
1408 GNUNET_assert (GNUNET_OK ==
1409 GNUNET_CONTAINER_multihashmap_put (neighbours,
1410 &n->id.hashPubKey, n,
1411 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1417 * Try to create a connection to the given target (eventually).
1419 * @param target peer to try to connect to
1422 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1424 struct NeighbourMapEntry *n;
1426 // This can happen during shutdown
1427 if (neighbours == NULL)
1432 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1433 GNUNET_i2s (target));
1436 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1441 n = lookup_neighbour (target);
1445 if ((S_CONNECTED == n->state) || (is_connecting (n)))
1446 return; /* already connecting or connected */
1447 if (is_disconnecting (n))
1448 change_state (n, S_NOT_CONNECTED);
1453 n = setup_neighbour (target);
1455 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1456 "Asking ATS for suggested address to connect to peer `%s'\n",
1457 GNUNET_i2s (&n->id));
1460 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1464 * Test if we're connected to the given peer.
1466 * @param target peer to test
1467 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1470 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1472 struct NeighbourMapEntry *n;
1474 // This can happen during shutdown
1475 if (neighbours == NULL)
1480 n = lookup_neighbour (target);
1482 if ((NULL == n) || (S_CONNECTED != n->state))
1483 return GNUNET_NO; /* not connected */
1489 * A session was terminated. Take note.
1491 * @param peer identity of the peer where the session died
1492 * @param session session that is gone
1495 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1496 struct Session *session)
1498 struct NeighbourMapEntry *n;
1500 if (neighbours == NULL)
1502 /* This can happen during shutdown */
1507 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1508 session, GNUNET_i2s (peer));
1511 n = lookup_neighbour (peer);
1514 if (session != n->session)
1515 return; /* doesn't affect us */
1516 if (n->state == S_CONNECTED)
1517 GST_validation_set_address_use (&n->id,
1522 if (NULL != n->address)
1524 GNUNET_HELLO_address_free (n->address);
1528 /* not connected anymore anyway, shouldn't matter */
1529 if ((S_CONNECTED != n->state) && (!is_connecting (n)))
1531 change_state (n, S_FAST_RECONNECT);
1533 GNUNET_assert (neighbours_connected > 0);
1534 neighbours_connected--;
1536 if (n->keepalive_task != GNUNET_SCHEDULER_NO_TASK)
1538 GNUNET_SCHEDULER_cancel (n->keepalive_task);
1539 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
1542 /* We are connected, so ask ATS to switch addresses */
1543 GNUNET_SCHEDULER_cancel (n->timeout_task);
1545 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1546 &neighbour_timeout_task, n);
1547 /* try QUICKLY to re-establish a connection, reduce timeout! */
1548 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1549 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1551 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, &ats_suggest_cancel,
1553 GNUNET_ATS_suggest_address (GST_ats, peer);
1558 * Transmit a message to the given target using the active connection.
1560 * @param target destination
1561 * @param msg message to send
1562 * @param msg_size number of bytes in msg
1563 * @param timeout when to fail with timeout
1564 * @param cont function to call when done
1565 * @param cont_cls closure for 'cont'
1568 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1569 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1570 GST_NeighbourSendContinuation cont, void *cont_cls)
1572 struct NeighbourMapEntry *n;
1573 struct MessageQueue *mq;
1575 // This can happen during shutdown
1576 if (neighbours == NULL)
1581 n = lookup_neighbour (target);
1582 if ((n == NULL) || (!is_connected (n)))
1584 GNUNET_STATISTICS_update (GST_stats,
1586 ("# messages not sent (no such peer or not connected)"),
1590 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1591 "Could not send message to peer `%s': unknown neighbour",
1592 GNUNET_i2s (target));
1593 else if (!is_connected (n))
1594 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1595 "Could not send message to peer `%s': not connected\n",
1596 GNUNET_i2s (target));
1599 cont (cont_cls, GNUNET_SYSERR);
1603 if ((n->session == NULL) && (n->address == NULL) )
1605 GNUNET_STATISTICS_update (GST_stats,
1607 ("# messages not sent (no such peer or not connected)"),
1610 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1611 "Could not send message to peer `%s': no address available\n",
1612 GNUNET_i2s (target));
1616 cont (cont_cls, GNUNET_SYSERR);
1620 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1621 GNUNET_STATISTICS_update (GST_stats,
1623 ("# bytes in message queue for other peers"),
1624 msg_size, GNUNET_NO);
1625 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1627 mq->cont_cls = cont_cls;
1628 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1629 memcpy (&mq[1], msg, msg_size);
1630 mq->message_buf = (const char *) &mq[1];
1631 mq->message_buf_size = msg_size;
1632 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1633 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1635 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1636 (NULL == n->is_active))
1637 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1642 * We have received a message from the given sender. How long should
1643 * we delay before receiving more? (Also used to keep the peer marked
1646 * @param sender sender of the message
1647 * @param size size of the message
1648 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1649 * GNUNET_NO if the neighbour is not connected or violates the quota,
1650 * GNUNET_SYSERR if the connection is not fully up yet
1651 * @return how long to wait before reading more from this sender
1653 struct GNUNET_TIME_Relative
1654 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1655 *sender, ssize_t size, int *do_forward)
1657 struct NeighbourMapEntry *n;
1658 struct GNUNET_TIME_Relative ret;
1660 // This can happen during shutdown
1661 if (neighbours == NULL)
1663 return GNUNET_TIME_UNIT_FOREVER_REL;
1666 n = lookup_neighbour (sender);
1669 GST_neighbours_try_connect (sender);
1670 n = lookup_neighbour (sender);
1673 GNUNET_STATISTICS_update (GST_stats,
1675 ("# messages discarded due to lack of neighbour record"),
1677 *do_forward = GNUNET_NO;
1678 return GNUNET_TIME_UNIT_ZERO;
1681 if (!is_connected (n))
1683 *do_forward = GNUNET_SYSERR;
1684 return GNUNET_TIME_UNIT_ZERO;
1686 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1688 n->quota_violation_count++;
1690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1691 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1692 n->in_tracker.available_bytes_per_s__,
1693 n->quota_violation_count);
1695 /* Discount 32k per violation */
1696 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1700 if (n->quota_violation_count > 0)
1702 /* try to add 32k back */
1703 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1704 n->quota_violation_count--;
1707 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1709 GNUNET_STATISTICS_update (GST_stats,
1711 ("# bandwidth quota violations by other peers"),
1713 *do_forward = GNUNET_NO;
1714 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1716 *do_forward = GNUNET_YES;
1717 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1718 if (ret.rel_value > 0)
1721 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1722 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1723 (unsigned long long) n->in_tracker.
1724 consumption_since_last_update__,
1725 (unsigned int) n->in_tracker.available_bytes_per_s__,
1726 (unsigned long long) ret.rel_value);
1728 GNUNET_STATISTICS_update (GST_stats,
1729 gettext_noop ("# ms throttling suggested"),
1730 (int64_t) ret.rel_value, GNUNET_NO);
1737 * Keep the connection to the given neighbour alive longer,
1738 * we received a KEEPALIVE (or equivalent).
1740 * @param neighbour neighbour to keep alive
1743 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1745 struct NeighbourMapEntry *n;
1747 // This can happen during shutdown
1748 if (neighbours == NULL)
1753 n = lookup_neighbour (neighbour);
1756 GNUNET_STATISTICS_update (GST_stats,
1758 ("# KEEPALIVE messages discarded (not connected)"),
1762 GNUNET_SCHEDULER_cancel (n->timeout_task);
1764 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1765 &neighbour_timeout_task, n);
1770 * Change the incoming quota for the given peer.
1772 * @param neighbour identity of peer to change qutoa for
1773 * @param quota new quota
1776 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1777 struct GNUNET_BANDWIDTH_Value32NBO quota)
1779 struct NeighbourMapEntry *n;
1781 // This can happen during shutdown
1782 if (neighbours == NULL)
1787 n = lookup_neighbour (neighbour);
1790 GNUNET_STATISTICS_update (GST_stats,
1792 ("# SET QUOTA messages ignored (no such peer)"),
1797 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1798 "Setting inbound quota of %u Bps for peer `%s' to all clients\n",
1799 ntohl (quota.value__), GNUNET_i2s (&n->id));
1801 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1802 if (0 != ntohl (quota.value__))
1805 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1806 GNUNET_i2s (&n->id), "SET_QUOTA");
1808 if (is_connected (n))
1809 GNUNET_STATISTICS_update (GST_stats,
1810 gettext_noop ("# disconnects due to quota of 0"),
1812 disconnect_neighbour (n);
1817 * Closure for the neighbours_iterate function.
1819 struct IteratorContext
1822 * Function to call on each connected neighbour.
1824 GST_NeighbourIterator cb;
1834 * Call the callback from the closure for each connected neighbour.
1836 * @param cls the 'struct IteratorContext'
1837 * @param key the hash of the public key of the neighbour
1838 * @param value the 'struct NeighbourMapEntry'
1839 * @return GNUNET_OK (continue to iterate)
1842 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1844 struct IteratorContext *ic = cls;
1845 struct NeighbourMapEntry *n = value;
1847 if (!is_connected (n))
1850 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
1856 * Iterate over all connected neighbours.
1858 * @param cb function to call
1859 * @param cb_cls closure for cb
1862 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1864 struct IteratorContext ic;
1866 // This can happen during shutdown
1867 if (neighbours == NULL)
1874 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1878 * If we have an active connection to the given target, it must be shutdown.
1880 * @param target peer to disconnect from
1883 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1885 struct NeighbourMapEntry *n;
1887 // This can happen during shutdown
1888 if (neighbours == NULL)
1893 n = lookup_neighbour (target);
1895 return; /* not active */
1896 disconnect_neighbour (n);
1901 * We received a disconnect message from the given peer,
1902 * validate and process.
1904 * @param peer sender of the message
1905 * @param msg the disconnect message
1908 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
1910 const struct GNUNET_MessageHeader
1913 struct NeighbourMapEntry *n;
1914 const struct SessionDisconnectMessage *sdm;
1918 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1919 "Received DISCONNECT message from peer `%s'\n",
1923 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1925 // GNUNET_break_op (0);
1926 GNUNET_STATISTICS_update (GST_stats,
1928 ("# disconnect messages ignored (old format)"), 1,
1932 sdm = (const struct SessionDisconnectMessage *) msg;
1933 n = lookup_neighbour (peer);
1935 return; /* gone already */
1936 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1937 n->connect_ts.abs_value)
1939 GNUNET_STATISTICS_update (GST_stats,
1941 ("# disconnect messages ignored (timestamp)"), 1,
1945 GNUNET_CRYPTO_hash (&sdm->public_key,
1946 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1948 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
1950 GNUNET_break_op (0);
1953 if (ntohl (sdm->purpose.size) !=
1954 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1955 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1956 sizeof (struct GNUNET_TIME_AbsoluteNBO))
1958 GNUNET_break_op (0);
1962 GNUNET_CRYPTO_rsa_verify
1963 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
1964 &sdm->signature, &sdm->public_key))
1966 GNUNET_break_op (0);
1969 GST_neighbours_force_disconnect (peer);
1974 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
1975 * Consider switching to it.
1977 * @param message possibly a 'struct SessionConnectMessage' (check format)
1978 * @param peer identity of the peer to switch the address for
1979 * @param address address of the other peer, NULL if other peer
1981 * @param session session to use (or NULL)
1982 * @param ats performance data
1983 * @param ats_count number of entries in ats
1986 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
1987 const struct GNUNET_PeerIdentity *peer,
1988 const struct GNUNET_HELLO_Address *address,
1989 struct Session *session,
1990 const struct GNUNET_ATS_Information *ats,
1993 const struct SessionConnectMessage *scm;
1994 struct QuotaSetMessage q_msg;
1995 struct GNUNET_MessageHeader msg;
1996 struct NeighbourMapEntry *n;
2001 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
2002 "Received CONNECT_ACK message from peer `%s'\n",
2006 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2008 GNUNET_break_op (0);
2011 scm = (const struct SessionConnectMessage *) message;
2012 GNUNET_break_op (ntohl (scm->reserved) == 0);
2013 n = lookup_neighbour (peer);
2016 /* we did not send 'CONNECT' (how could we? no record for this peer!) */
2017 GNUNET_break_op (0);
2023 * ((n->state != S_CONNECT_RECV) && (n->address != NULL)):
2025 * We also received an CONNECT message, switched from SENDT to RECV and
2026 * ATS already suggested us an address after a successful blacklist check
2028 if ((n->state != S_CONNECT_SENT) && ((n->state != S_CONNECT_RECV) && (n->address != NULL)))
2030 GNUNET_STATISTICS_update (GST_stats,
2031 gettext_noop ("# unexpected CONNECT_ACK messages"), 1,
2036 if (NULL != session)
2037 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2039 "Giving ATS session %p of plugin %s for peer %s\n",
2040 session, address->transport_name, GNUNET_i2s (peer));
2041 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2042 GNUNET_assert (NULL != n->address);
2044 change_state (n, S_CONNECTED);
2045 GST_validation_set_address_use (&n->id,
2049 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2051 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2053 /* send ACK (ACK) */
2054 msg_len = sizeof (msg);
2055 msg.size = htons (msg_len);
2056 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2059 send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2060 GNUNET_TIME_UNIT_FOREVER_REL, n->session,
2061 n->address, GNUNET_YES, NULL,
2064 if (ret == GNUNET_SYSERR)
2065 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2066 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2067 GNUNET_i2s (&n->id),
2068 GST_plugins_a2s (n->address), n->session);
2071 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2073 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2074 &neighbour_keepalive_task, n);
2076 neighbours_connected++;
2077 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2081 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2082 GNUNET_i2s (&n->id),
2083 GST_plugins_a2s (n->address), n->session,
2086 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2089 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2090 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2091 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2093 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2094 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2095 q_msg.quota = n->bandwidth_out;
2096 q_msg.peer = (*peer);
2097 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2103 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2104 const struct GNUNET_PeerIdentity *peer,
2105 const struct GNUNET_HELLO_Address *address,
2106 struct Session *session,
2107 const struct GNUNET_ATS_Information *ats,
2110 struct NeighbourMapEntry *n;
2111 struct QuotaSetMessage q_msg;
2114 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2115 "Received ACK message from peer `%s'\n",
2119 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2121 GNUNET_break_op (0);
2124 n = lookup_neighbour (peer);
2127 send_disconnect (peer, address,
2132 if (S_CONNECTED == n->state)
2134 if (!is_connecting(n))
2136 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# unexpected ACK messages"), 1,
2140 if (NULL != session)
2141 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2143 "Giving ATS session %p of plugin %s for peer %s\n",
2144 session, address->transport_name, GNUNET_i2s (peer));
2145 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2146 GNUNET_assert (n->address != NULL);
2147 change_state (n, S_CONNECTED);
2148 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2150 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2152 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2154 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2155 &neighbour_keepalive_task, n);
2156 GST_validation_set_address_use (&n->id,
2160 neighbours_connected++;
2161 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2165 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2166 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2167 GNUNET_i2s (&n->id),
2168 GST_plugins_a2s (n->address), n->session,
2171 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2173 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2174 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2175 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2177 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2178 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2179 q_msg.quota = n->bandwidth_out;
2180 q_msg.peer = (*peer);
2181 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2184 struct BlackListCheckContext
2186 struct GNUNET_ATS_Information *ats;
2190 struct Session *session;
2192 struct GNUNET_HELLO_Address *address;
2194 struct GNUNET_TIME_Absolute ts;
2199 handle_connect_blacklist_cont (void *cls,
2200 const struct GNUNET_PeerIdentity *peer,
2203 struct NeighbourMapEntry *n;
2204 struct BlackListCheckContext *bcc = cls;
2207 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2208 "Blacklist check due to CONNECT message: `%s'\n",
2210 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2214 if (GNUNET_OK != result)
2216 GNUNET_HELLO_address_free (bcc->address);
2221 n = lookup_neighbour (peer);
2223 n = setup_neighbour (peer);
2225 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2227 if (NULL != bcc->session)
2228 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2230 "Giving ATS session %p of address `%s' for peer %s\n",
2232 GST_plugins_a2s (bcc->address),
2234 GNUNET_ATS_address_update (GST_ats, bcc->address,
2235 bcc->session, bcc->ats, bcc->ats_count);
2236 n->connect_ts = bcc->ts;
2241 if (n->state != S_CONNECT_RECV)
2242 change_state (n, S_CONNECT_RECV);
2244 /* Ask ATS for an address to connect via that address */
2245 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2246 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2248 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2250 GNUNET_ATS_suggest_address (GST_ats, peer);
2254 * We received a 'SESSION_CONNECT' message from the other peer.
2255 * Consider switching to it.
2257 * @param message possibly a 'struct SessionConnectMessage' (check format)
2258 * @param peer identity of the peer to switch the address for
2259 * @param address address of the other peer, NULL if other peer
2261 * @param session session to use (or NULL)
2262 * @param ats performance data
2263 * @param ats_count number of entries in ats (excluding 0-termination)
2266 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2267 const struct GNUNET_PeerIdentity *peer,
2268 const struct GNUNET_HELLO_Address *address,
2269 struct Session *session,
2270 const struct GNUNET_ATS_Information *ats,
2273 const struct SessionConnectMessage *scm;
2274 struct NeighbourMapEntry *n;
2275 struct BlackListCheckContext *bcc = NULL;
2278 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2279 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2282 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2284 GNUNET_break_op (0);
2288 scm = (const struct SessionConnectMessage *) message;
2289 GNUNET_break_op (ntohl (scm->reserved) == 0);
2291 n = lookup_neighbour (peer);
2293 (S_CONNECTED == n->state) )
2295 /* connected peer switches addresses */
2296 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2300 /* we are not connected to this peer */
2301 /* do blacklist check */
2303 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2304 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2305 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2306 bcc->ats_count = ats_count + 1;
2307 bcc->address = GNUNET_HELLO_address_copy (address);
2308 bcc->session = session;
2309 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2310 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2311 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2312 bcc->ats[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2313 GST_blacklist_test_allowed (peer, address->transport_name, handle_connect_blacklist_cont,
2318 /* end of file gnunet-service-transport_neighbours.c */