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;
189 /* fresh peer or completely disconnected */
191 /* sent CONNECT message to other peer, waiting for CONNECT_ACK */
193 /* received CONNECT message to other peer, sending CONNECT_ACK */
195 /* sent CONNECT_ACK message to other peer, wait for ACK or payload */
196 S_CONNECT_RECV_ACK_SENT = 8,
197 /* received ACK or payload */
199 /* Disconnect in progress */
204 * Entry in neighbours.
206 struct NeighbourMapEntry
210 * Head of list of messages we would like to send to this peer;
211 * must contain at most one message per client.
213 struct MessageQueue *messages_head;
216 * Tail of list of messages we would like to send to this peer; must
217 * contain at most one message per client.
219 struct MessageQueue *messages_tail;
222 * Performance data for the peer.
224 //struct GNUNET_ATS_Information *ats;
227 * Are we currently trying to send a message? If so, which one?
229 struct MessageQueue *is_active;
232 * Active session for communicating with the peer.
234 struct Session *session;
237 * Address we currently use.
239 struct GNUNET_HELLO_Address *address;
242 * Identity of this neighbour.
244 struct GNUNET_PeerIdentity id;
247 * ID of task scheduled to run when this peer is about to
248 * time out (will free resources associated with the peer).
250 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
253 * ID of task scheduled to send keepalives.
255 GNUNET_SCHEDULER_TaskIdentifier keepalive_task;
258 * ID of task scheduled to run when we should try transmitting
259 * the head of the message queue.
261 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
264 * Tracker for inbound bandwidth.
266 struct GNUNET_BANDWIDTH_Tracker in_tracker;
269 * Inbound bandwidth from ATS, activated when connection is up
271 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_in;
274 * Inbound bandwidth from ATS, activated when connection is up
276 struct GNUNET_BANDWIDTH_Value32NBO bandwidth_out;
279 * Timestamp of the 'SESSION_CONNECT' message we got from the other peer
281 struct GNUNET_TIME_Absolute connect_ts;
285 * We asked ATS for a new address for this peer
287 GNUNET_SCHEDULER_TaskIdentifier ats_suggest;
290 * Task the resets the peer state after due to an pending
291 * unsuccessful connection setup
293 GNUNET_SCHEDULER_TaskIdentifier state_reset;
296 * How often has the other peer (recently) violated the inbound
297 * traffic limit? Incremented by 10 per violation, decremented by 1
298 * per non-violation (for each time interval).
300 unsigned int quota_violation_count;
304 * The current state of the peer
305 * Element of enum State
313 * All known neighbours and their HELLOs.
315 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
318 * Closure for connect_notify_cb and disconnect_notify_cb
320 static void *callback_cls;
323 * Function to call when we connected to a neighbour.
325 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
328 * Function to call when we disconnected from a neighbour.
330 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
333 * counter for connected neighbours
335 static int neighbours_connected;
338 * Lookup a neighbour entry in the neighbours hash map.
340 * @param pid identity of the peer to look up
341 * @return the entry, NULL if there is no existing record
343 static struct NeighbourMapEntry *
344 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
346 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
349 #define change_state(n, state, ...) change (n, state, __LINE__)
352 is_connecting (struct NeighbourMapEntry *n)
354 if ((n->state > S_NOT_CONNECTED) && (n->state < S_CONNECTED))
360 is_connected (struct NeighbourMapEntry *n)
362 if (n->state == S_CONNECTED)
368 is_disconnecting (struct NeighbourMapEntry *n)
370 if (n->state == S_DISCONNECT)
376 print_state (int state)
381 return "S_CONNECTED";
384 return "S_CONNECT_RECV";
386 case S_CONNECT_RECV_ACK_SENT:
387 return "S_CONNECT_RECV_ACK_SENT";
390 return "S_CONNECT_SENT";
393 return "S_DISCONNECT";
395 case S_NOT_CONNECTED:
396 return "S_NOT_CONNECTED";
406 change (struct NeighbourMapEntry *n, int state, int line);
409 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
412 reset_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
414 struct NeighbourMapEntry *n = cls;
416 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
420 /* This jut a temporary debug message to check if a the value
421 * SETUP_CONNECTION_TIMEOUT was choosen to small for slow machines
423 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
424 "Information for developers: Connection to peer `%s' %s failed in state `%s', resetting connection attempt \n",
425 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address),
426 print_state (n->state));
428 GNUNET_STATISTICS_update (GST_stats,
430 ("# failed connection attempts due to timeout"), 1,
433 /* resetting state */
434 n->state = S_NOT_CONNECTED;
436 /* destroying address */
437 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
439 /* request new address */
440 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
441 GNUNET_SCHEDULER_cancel (n->ats_suggest);
443 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
445 GNUNET_ATS_suggest_address (GST_ats, &n->id);
449 change (struct NeighbourMapEntry *n, int state, int line)
451 char *old = strdup (print_state (n->state));
452 char *new = strdup (print_state (state));
454 /* allowed transitions */
455 int allowed = GNUNET_NO;
459 case S_NOT_CONNECTED:
460 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT) ||
461 (state == S_DISCONNECT))
463 allowed = GNUNET_YES;
465 /* Schedule reset task */
466 if ((state == S_CONNECT_RECV) || (state == S_CONNECT_SENT))
468 GNUNET_assert (n->state_reset == GNUNET_SCHEDULER_NO_TASK);
470 GNUNET_SCHEDULER_add_delayed (SETUP_CONNECTION_TIMEOUT, &reset_task,
477 if ((state == S_NOT_CONNECTED) || (state == S_DISCONNECT) ||
478 (state == S_CONNECTED) ||
479 /* FIXME SENT -> RECV ISSUE! */ (state == S_CONNECT_SENT))
481 if ((state == S_CONNECTED) || (state == S_DISCONNECT) ||
482 (state == S_NOT_CONNECTED))
485 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
486 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
487 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address),
488 print_state (n->state), print_state (state));
490 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
491 GNUNET_SCHEDULER_cancel (n->state_reset);
492 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
495 allowed = GNUNET_YES;
500 if ((state == S_NOT_CONNECTED) || (state == S_CONNECTED) ||
501 (state == S_DISCONNECT) ||
502 /* FIXME SENT -> RECV ISSUE! */ (state == S_CONNECT_RECV))
504 if ((state == S_CONNECTED) || (state == S_DISCONNECT) ||
505 (state == S_NOT_CONNECTED))
508 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
509 "Removed reset task for peer `%s' %s failed in state transition `%s' -> `%s' \n",
510 GNUNET_i2s (&n->id), GST_plugins_a2s (n->address),
511 print_state (n->state), print_state (state));
513 GNUNET_assert (n->state_reset != GNUNET_SCHEDULER_NO_TASK);
514 GNUNET_SCHEDULER_cancel (n->state_reset);
515 n->state_reset = GNUNET_SCHEDULER_NO_TASK;
518 allowed = GNUNET_YES;
523 if (state == S_DISCONNECT)
525 allowed = GNUNET_YES;
531 * if (state == S_NOT_CONNECTED)
533 * allowed = GNUNET_YES;
543 if (allowed == GNUNET_NO)
545 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
546 "Illegal state transition from `%s' to `%s' in line %u \n", old,
551 return GNUNET_SYSERR;
556 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
557 "State for neighbour `%s' %X changed from `%s' to `%s' in line %u\n",
558 GNUNET_i2s (&n->id), n, old, new, line);
566 send_with_plugin (const struct GNUNET_PeerIdentity *target, const char *msgbuf,
567 size_t msgbuf_size, uint32_t priority,
568 struct GNUNET_TIME_Relative timeout, struct Session *session,
569 const struct GNUNET_HELLO_Address *address,
570 int force_address, GNUNET_TRANSPORT_TransmitContinuation cont,
573 struct GNUNET_TRANSPORT_PluginFunctions *papi;
574 size_t ret = GNUNET_SYSERR;
576 /* FIXME : ats returns an address with all values 0 */
580 cont (cont_cls, target, GNUNET_SYSERR);
581 return GNUNET_SYSERR;
584 if ((session == NULL) && (address == NULL))
587 cont (cont_cls, target, GNUNET_SYSERR);
588 return GNUNET_SYSERR;
591 papi = GST_plugins_find (address->transport_name);
595 cont (cont_cls, target, GNUNET_SYSERR);
596 return GNUNET_SYSERR;
600 papi->send (papi->cls, target, msgbuf, msgbuf_size, 0, timeout, session,
602 address->address_length, GNUNET_YES, cont, cont_cls);
607 cont (cont_cls, target, GNUNET_SYSERR);
613 * Task invoked to start a transmission to another peer.
615 * @param cls the 'struct NeighbourMapEntry'
616 * @param tc scheduler context
619 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
623 * We're done with our transmission attempt, continue processing.
625 * @param cls the 'struct MessageQueue' of the message
626 * @param receiver intended receiver
627 * @param success whether it worked or not
630 transmit_send_continuation (void *cls,
631 const struct GNUNET_PeerIdentity *receiver,
634 struct MessageQueue *mq;
635 struct NeighbourMapEntry *n;
641 GNUNET_assert (n->is_active == mq);
643 if (success == GNUNET_YES)
645 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
646 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
650 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sending message of type %u was %s\n",
651 ntohs (((struct GNUNET_MessageHeader *) mq->message_buf)->type),
652 (success == GNUNET_OK) ? "successful" : "FAILED");
654 if (NULL != mq->cont)
655 mq->cont (mq->cont_cls, success);
661 * Check the ready list for the given neighbour and if a plugin is
662 * ready for transmission (and if we have a message), do so!
664 * @param n target peer for which to transmit
667 try_transmission_to_peer (struct NeighbourMapEntry *n)
669 struct MessageQueue *mq;
670 struct GNUNET_TIME_Relative timeout;
673 if (n->is_active != NULL)
676 return; /* transmission already pending */
678 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
681 return; /* currently waiting for bandwidth */
683 while (NULL != (mq = n->messages_head))
685 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
686 if (timeout.rel_value > 0)
688 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
691 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
694 return; /* no more messages */
696 if (GST_plugins_find (n->address->transport_name) == NULL)
701 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
705 if ( (n->session == NULL) && (NULL == n->address) )
707 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
708 "No address for peer `%s'\n",
709 GNUNET_i2s (&n->id));
710 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
711 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
712 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
717 send_with_plugin (&n->id, mq->message_buf, mq->message_buf_size, 0,
718 timeout, n->session, n->address,
719 GNUNET_YES, &transmit_send_continuation,
723 /* failure, but 'send' would not call continuation in this case,
724 * so we need to do it here! */
725 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
732 * Task invoked to start a transmission to another peer.
734 * @param cls the 'struct NeighbourMapEntry'
735 * @param tc scheduler context
738 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
740 struct NeighbourMapEntry *n = cls;
742 GNUNET_assert (NULL != lookup_neighbour (&n->id));
743 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
744 try_transmission_to_peer (n);
749 * Initialize the neighbours subsystem.
751 * @param cls closure for callbacks
752 * @param connect_cb function to call if we connect to a peer
753 * @param disconnect_cb function to call if we disconnect from a peer
756 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
757 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
760 connect_notify_cb = connect_cb;
761 disconnect_notify_cb = disconnect_cb;
762 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
767 send_disconnect_cont (void *cls, const struct GNUNET_PeerIdentity *target,
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "Sending DISCONNECT message to peer `%4s': %i\n",
773 GNUNET_i2s (target), result);
779 send_disconnect (const struct GNUNET_PeerIdentity *target,
780 const struct GNUNET_HELLO_Address *address,
781 struct Session *session)
784 struct SessionDisconnectMessage disconnect_msg;
787 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
788 "Sending DISCONNECT message to peer `%4s'\n",
789 GNUNET_i2s (target));
792 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
793 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
794 disconnect_msg.reserved = htonl (0);
795 disconnect_msg.purpose.size =
796 htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
797 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
798 sizeof (struct GNUNET_TIME_AbsoluteNBO));
799 disconnect_msg.purpose.purpose =
800 htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
801 disconnect_msg.timestamp =
802 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
803 disconnect_msg.public_key = GST_my_public_key;
804 GNUNET_assert (GNUNET_OK ==
805 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
806 &disconnect_msg.purpose,
807 &disconnect_msg.signature));
810 send_with_plugin (target, (const char *) &disconnect_msg,
811 sizeof (disconnect_msg), UINT32_MAX,
812 GNUNET_TIME_UNIT_FOREVER_REL, session, address,
814 &send_disconnect_cont, NULL);
816 if (ret == GNUNET_SYSERR)
817 return GNUNET_SYSERR;
819 GNUNET_STATISTICS_update (GST_stats,
821 ("# peers disconnected due to external request"), 1,
827 * Disconnect from the given neighbour, clean up the record.
829 * @param n neighbour to disconnect from
832 disconnect_neighbour (struct NeighbourMapEntry *n)
834 struct MessageQueue *mq;
835 int was_connected = is_connected (n);
837 /* send DISCONNECT MESSAGE */
838 if (is_connected (n) || is_connecting (n))
841 send_disconnect (&n->id, n->address,
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Sent DISCONNECT_MSG to `%s'\n",
844 GNUNET_i2s (&n->id));
846 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
847 "Could not send DISCONNECT_MSG to `%s'\n",
848 GNUNET_i2s (&n->id));
853 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
857 if (is_disconnecting (n))
859 change_state (n, S_DISCONNECT);
860 GST_validation_set_address_use (&n->id,
865 if (n->address != NULL)
867 struct GNUNET_TRANSPORT_PluginFunctions *papi;
868 papi = GST_plugins_find (n->address->transport_name);
870 papi->disconnect (papi->cls, &n->id);
873 while (NULL != (mq = n->messages_head))
875 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
876 if (NULL != mq->cont)
877 mq->cont (mq->cont_cls, GNUNET_SYSERR);
880 if (NULL != n->is_active)
882 n->is_active->n = NULL;
887 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != n->keepalive_task);
888 GNUNET_SCHEDULER_cancel (n->keepalive_task);
889 n->keepalive_task = GNUNET_SCHEDULER_NO_TASK;
890 GNUNET_assert (neighbours_connected > 0);
891 neighbours_connected--;
892 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
894 disconnect_notify_cb (callback_cls, &n->id);
896 GNUNET_assert (GNUNET_YES ==
897 GNUNET_CONTAINER_multihashmap_remove (neighbours,
898 &n->id.hashPubKey, n));
899 if (GNUNET_SCHEDULER_NO_TASK != n->ats_suggest)
901 GNUNET_SCHEDULER_cancel (n->ats_suggest);
902 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
904 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
906 GNUNET_SCHEDULER_cancel (n->timeout_task);
907 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
909 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
911 GNUNET_SCHEDULER_cancel (n->transmission_task);
912 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
914 if (NULL != n->address)
916 GNUNET_HELLO_address_free (n->address);
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Deleting peer `%4s', %X\n",
921 GNUNET_i2s (&n->id), n);
927 * Peer has been idle for too long. Disconnect.
929 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
930 * @param tc scheduler context
933 neighbour_timeout_task (void *cls,
934 const struct GNUNET_SCHEDULER_TaskContext *tc)
936 struct NeighbourMapEntry *n = cls;
938 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
940 GNUNET_STATISTICS_update (GST_stats,
942 ("# peers disconnected due to timeout"), 1,
944 disconnect_neighbour (n);
949 * Send another keepalive message.
951 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
952 * @param tc scheduler context
955 neighbour_keepalive_task (void *cls,
956 const struct GNUNET_SCHEDULER_TaskContext *tc)
958 struct NeighbourMapEntry *n = cls;
959 struct GNUNET_MessageHeader m;
962 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
963 &neighbour_keepalive_task, n);
964 GNUNET_assert (is_connected (n));
965 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# keepalives sent"), 1,
967 m.size = htons (sizeof (struct GNUNET_MessageHeader));
968 m.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_KEEPALIVE);
970 send_with_plugin (&n->id, (const void *) &m, sizeof (m),
971 UINT32_MAX /* priority */ ,
972 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->address,
973 GNUNET_YES, NULL, NULL);
978 * Disconnect from the given neighbour.
981 * @param key hash of neighbour's public key (not used)
982 * @param value the 'struct NeighbourMapEntry' of the neighbour
985 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
987 struct NeighbourMapEntry *n = value;
990 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
991 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
993 if (is_connected (n))
994 GNUNET_STATISTICS_update (GST_stats,
996 ("# peers disconnected due to global disconnect"),
998 disconnect_neighbour (n);
1004 ats_suggest_cancel (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
1006 struct NeighbourMapEntry *n = cls;
1008 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1010 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1011 " ATS did not suggested address to connect to peer `%s'\n",
1012 GNUNET_i2s (&n->id));
1014 disconnect_neighbour (n);
1018 * Cleanup the neighbours subsystem.
1021 GST_neighbours_stop ()
1023 // This can happen during shutdown
1024 if (neighbours == NULL)
1029 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
1031 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
1032 GNUNET_assert (neighbours_connected == 0);
1034 callback_cls = NULL;
1035 connect_notify_cb = NULL;
1036 disconnect_notify_cb = NULL;
1041 * We tried to send a SESSION_CONNECT message to another peer. If this
1042 * succeeded, we change the state. If it failed, we should tell
1043 * ATS to not use this address anymore (until it is re-validated).
1045 * @param cls the 'struct NeighbourMapEntry'
1046 * @param success GNUNET_OK on success
1049 send_connect_continuation (void *cls, const struct GNUNET_PeerIdentity *target,
1052 struct NeighbourMapEntry *n = cls;
1054 GNUNET_assert (n != NULL);
1055 GNUNET_assert (!is_connected (n));
1057 if (is_disconnecting (n))
1058 return; /* neighbour is going away */
1060 if (GNUNET_YES != success)
1063 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1064 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1065 GNUNET_i2s (&n->id), n->address->transport_name,
1067 0) ? "<inbound>" : GST_plugins_a2s (n->address), n->session);
1070 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1072 change_state (n, S_NOT_CONNECTED);
1074 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1075 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1077 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1079 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1088 * We tried to switch addresses with an peer already connected. If it failed,
1089 * we should tell ATS to not use this address anymore (until it is re-validated).
1091 * @param cls the 'struct NeighbourMapEntry'
1092 * @param success GNUNET_OK on success
1095 send_switch_address_continuation (void *cls,
1096 const struct GNUNET_PeerIdentity *target,
1099 struct NeighbourMapEntry *n = cls;
1101 GNUNET_assert (n != NULL);
1102 if (is_disconnecting (n))
1103 return; /* neighbour is going away */
1105 GNUNET_assert (n->state == S_CONNECTED);
1106 if (GNUNET_YES != success)
1109 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1110 "Failed to switch connected peer `%s' to plugin `%s' address '%s' session %X, asking ATS for new address \n",
1111 GNUNET_i2s (&n->id), n->address->transport_name,
1113 0) ? "<inbound>" : GST_plugins_a2s (n->address), n->session);
1116 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1118 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1119 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1121 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1123 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1126 /* Tell ATS that switching addresses was successful */
1127 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
1131 * We tried to send a SESSION_CONNECT message to another peer. If this
1132 * succeeded, we change the state. If it failed, we should tell
1133 * ATS to not use this address anymore (until it is re-validated).
1135 * @param cls the 'struct NeighbourMapEntry'
1136 * @param success GNUNET_OK on success
1139 send_connect_ack_continuation (void *cls,
1140 const struct GNUNET_PeerIdentity *target,
1143 struct NeighbourMapEntry *n = cls;
1145 GNUNET_assert (n != NULL);
1147 if (GNUNET_YES == success)
1148 return; /* sending successful */
1150 /* sending failed, ask for next address */
1152 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1153 "Failed to send CONNECT_MSG to peer `%4s' with plugin `%s' address '%s' session %X, asking ATS for new address \n",
1154 GNUNET_i2s (&n->id), n->address->transport_name,
1155 (n->addrlen == 0) ? "<inbound>" : GST_plugins_a2s (n->address),
1158 change_state (n, S_NOT_CONNECTED);
1160 GNUNET_ATS_address_destroyed (GST_ats, n->address, NULL);
1162 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1163 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1165 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1167 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1171 * For an existing neighbour record, set the active connection to
1172 * the given address.
1174 * @param peer identity of the peer to switch the address for
1175 * @param address address of the other peer, NULL if other peer
1177 * @param session session to use (or NULL)
1178 * @param ats performance data
1179 * @param ats_count number of entries in ats
1180 * @return GNUNET_YES if we are currently connected, GNUNET_NO if the
1181 * connection is not up (yet)
1184 GST_neighbours_switch_to_address_3way (const struct GNUNET_PeerIdentity *peer,
1185 const struct GNUNET_HELLO_Address *address,
1186 struct Session *session,
1187 const struct GNUNET_ATS_Information *ats,
1189 struct GNUNET_BANDWIDTH_Value32NBO
1191 struct GNUNET_BANDWIDTH_Value32NBO
1194 struct NeighbourMapEntry *n;
1195 struct SessionConnectMessage connect_msg;
1200 // This can happen during shutdown
1201 if (neighbours == NULL)
1206 checks_failed = GNUNET_NO;
1208 if (address == NULL)
1210 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1211 "ATS offered suggested us empty address: plugin NULL");
1212 GNUNET_break_op (0);
1213 checks_failed = GNUNET_YES;
1215 if ( (session == NULL) && (address == NULL) )
1217 GNUNET_log (GNUNET_ERROR_TYPE_WARNING,
1218 "ATS offered suggested us empty address: address NULL & session NULL");
1219 GNUNET_break_op (0);
1220 checks_failed = GNUNET_YES;
1223 n = lookup_neighbour (peer);
1225 checks_failed = GNUNET_YES;
1227 if (checks_failed == GNUNET_YES)
1229 GNUNET_ATS_address_destroyed (GST_ats, address, session);
1231 GNUNET_ATS_suggest_address (GST_ats, peer);
1235 /* checks successful and neighbour != NULL */
1237 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1238 "ATS tells us to switch to plugin `%s' address '%s' session %X for %s peer `%s'\n",
1239 address->transport_name,
1240 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (address),
1241 session, (is_connected (n) ? "CONNECTED" : "NOT CONNECTED"),
1245 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1247 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1248 n->ats_suggest = GNUNET_SCHEDULER_NO_TASK;
1251 // do not switch addresses just update quotas
1252 if ( (is_connected (n)) &&
1253 (NULL != n->address) &&
1254 (0 == GNUNET_HELLO_address_cmp (address,
1256 (n->session == session) )
1258 struct QuotaSetMessage q_msg;
1261 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1262 "Sending outbound quota of %u Bps and inbound quota of %u Bps for peer `%s' to all clients\n",
1263 ntohl (n->bandwidth_out.value__),
1264 ntohl (n->bandwidth_in.value__), GNUNET_i2s (peer));
1267 n->bandwidth_in = bandwidth_in;
1268 n->bandwidth_out = bandwidth_out;
1269 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
1271 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
1272 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
1273 q_msg.quota = n->bandwidth_out;
1274 q_msg.peer = (*peer);
1275 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
1278 if (n->state == S_CONNECTED)
1279 GST_validation_set_address_use (&n->id,
1283 /* This will be a connection switch, tell ATS about it */
1284 if (n->state == S_CONNECTED)
1286 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_NO);
1289 /* set new address */
1290 if (NULL != n->address)
1291 GNUNET_HELLO_address_free (n->address);
1292 n->address = GNUNET_HELLO_address_copy (address);
1293 n->bandwidth_in = bandwidth_in;
1294 n->bandwidth_out = bandwidth_out;
1295 n->session = session;
1296 GNUNET_SCHEDULER_cancel (n->timeout_task);
1298 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1299 &neighbour_timeout_task, n);
1300 if (n->state == S_CONNECTED)
1301 GST_validation_set_address_use (&n->id,
1307 if (n->state == S_DISCONNECT)
1309 /* We are disconnecting, nothing to do here */
1312 /* We are not connected/connecting and initiate a fresh connect */
1313 if (n->state == S_NOT_CONNECTED)
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);
1319 connect_msg.reserved = htonl (0);
1320 connect_msg.timestamp =
1321 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1323 change_state (n, S_CONNECT_SENT);
1326 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1327 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1328 address, GNUNET_YES,
1329 &send_connect_continuation, n);
1334 /* We received a CONNECT message and asked ATS for an address */
1335 else if (n->state == S_CONNECT_RECV)
1337 msg_len = sizeof (struct SessionConnectMessage);
1338 connect_msg.header.size = htons (msg_len);
1339 connect_msg.header.type =
1340 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT_ACK);
1341 connect_msg.reserved = htonl (0);
1342 connect_msg.timestamp =
1343 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1346 send_with_plugin (&n->id, (const void *) &connect_msg, msg_len,
1347 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1348 address, GNUNET_YES,
1349 &send_connect_ack_continuation, n);
1352 /* connected peer is switching addresses */
1353 else if (n->state == S_CONNECTED)
1355 msg_len = sizeof (struct SessionConnectMessage);
1356 connect_msg.header.size = htons (msg_len);
1357 connect_msg.header.type =
1358 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
1359 connect_msg.reserved = htonl (0);
1360 connect_msg.timestamp =
1361 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1364 send_with_plugin (peer, (const char *) &connect_msg, msg_len,
1365 UINT32_MAX, GNUNET_TIME_UNIT_FOREVER_REL, session,
1366 address, GNUNET_YES,
1367 &send_switch_address_continuation, n);
1368 if (ret == GNUNET_SYSERR)
1370 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1371 "Failed to send CONNECT_MESSAGE to `%4s' using address '%s' session %X\n",
1373 GST_plugins_a2s (address), session);
1377 else if (n->state == S_CONNECT_SENT)
1381 GNUNET_log (GNUNET_ERROR_TYPE_ERROR,
1382 "Invalid connection state to switch addresses %u \n", n->state);
1383 GNUNET_break_op (0);
1389 * Obtain current latency information for the given neighbour.
1392 * @return observed latency of the address, FOREVER if the address was
1393 * never successfully validated
1395 struct GNUNET_TIME_Relative
1396 GST_neighbour_get_latency (const struct GNUNET_PeerIdentity *peer)
1398 struct NeighbourMapEntry *n;
1400 n = lookup_neighbour (peer);
1402 ( (n->address == NULL) && (n->session == NULL) ) )
1403 return GNUNET_TIME_UNIT_FOREVER_REL;
1404 return GST_validation_get_address_latency (peer,
1411 * Create an entry in the neighbour map for the given peer
1413 * @param peer peer to create an entry for
1414 * @return new neighbour map entry
1416 static struct NeighbourMapEntry *
1417 setup_neighbour (const struct GNUNET_PeerIdentity *peer)
1419 struct NeighbourMapEntry *n;
1422 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1423 "Unknown peer `%s', creating new neighbour\n", GNUNET_i2s (peer));
1425 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
1427 n->state = S_NOT_CONNECTED;
1428 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
1429 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
1430 MAX_BANDWIDTH_CARRY_S);
1432 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1433 &neighbour_timeout_task, n);
1434 GNUNET_assert (GNUNET_OK ==
1435 GNUNET_CONTAINER_multihashmap_put (neighbours,
1436 &n->id.hashPubKey, n,
1437 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
1443 * Try to create a connection to the given target (eventually).
1445 * @param target peer to try to connect to
1448 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
1450 struct NeighbourMapEntry *n;
1452 // This can happen during shutdown
1453 if (neighbours == NULL)
1458 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
1459 GNUNET_i2s (target));
1462 memcmp (target, &GST_my_identity, sizeof (struct GNUNET_PeerIdentity)))
1467 n = lookup_neighbour (target);
1471 if ((is_connected (n)) || (is_connecting (n)))
1472 return; /* already connecting or connected */
1473 if (is_disconnecting (n))
1474 change_state (n, S_NOT_CONNECTED);
1479 n = setup_neighbour (target);
1481 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1482 "Asking ATS for suggested address to connect to peer `%s'\n",
1483 GNUNET_i2s (&n->id));
1486 GNUNET_ATS_suggest_address (GST_ats, &n->id);
1490 * Test if we're connected to the given peer.
1492 * @param target peer to test
1493 * @return GNUNET_YES if we are connected, GNUNET_NO if not
1496 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
1498 struct NeighbourMapEntry *n;
1500 // This can happen during shutdown
1501 if (neighbours == NULL)
1506 n = lookup_neighbour (target);
1508 if ((NULL == n) || (!is_connected (n)))
1509 return GNUNET_NO; /* not connected */
1515 * A session was terminated. Take note.
1517 * @param peer identity of the peer where the session died
1518 * @param session session that is gone
1521 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
1522 struct Session *session)
1524 struct NeighbourMapEntry *n;
1526 // This can happen during shutdown
1527 if (neighbours == NULL)
1533 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Session %X to peer `%s' ended \n",
1534 session, GNUNET_i2s (peer));
1537 n = lookup_neighbour (peer);
1540 if (session != n->session)
1541 return; /* doesn't affect us */
1544 if (NULL != n->address)
1546 GNUNET_HELLO_address_free (n->address);
1550 /* not connected anymore anyway, shouldn't matter */
1551 if ((!is_connected (n)) && (!is_connecting (n)))
1554 /* We are connected, so ask ATS to switch addresses */
1555 GNUNET_SCHEDULER_cancel (n->timeout_task);
1557 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_DISCONNECT_SESSION_TIMEOUT,
1558 &neighbour_timeout_task, n);
1559 /* try QUICKLY to re-establish a connection, reduce timeout! */
1560 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
1561 GNUNET_SCHEDULER_cancel (n->ats_suggest);
1563 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
1565 GNUNET_ATS_suggest_address (GST_ats, peer);
1570 * Transmit a message to the given target using the active connection.
1572 * @param target destination
1573 * @param msg message to send
1574 * @param msg_size number of bytes in msg
1575 * @param timeout when to fail with timeout
1576 * @param cont function to call when done
1577 * @param cont_cls closure for 'cont'
1580 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
1581 size_t msg_size, struct GNUNET_TIME_Relative timeout,
1582 GST_NeighbourSendContinuation cont, void *cont_cls)
1584 struct NeighbourMapEntry *n;
1585 struct MessageQueue *mq;
1587 // This can happen during shutdown
1588 if (neighbours == NULL)
1593 n = lookup_neighbour (target);
1594 if ((n == NULL) || (!is_connected (n)))
1596 GNUNET_STATISTICS_update (GST_stats,
1598 ("# messages not sent (no such peer or not connected)"),
1602 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1603 "Could not send message to peer `%s': unknown neighbour",
1604 GNUNET_i2s (target));
1605 else if (!is_connected (n))
1606 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1607 "Could not send message to peer `%s': not connected\n",
1608 GNUNET_i2s (target));
1611 cont (cont_cls, GNUNET_SYSERR);
1615 if ((n->session == NULL) && (n->address == NULL) )
1617 GNUNET_STATISTICS_update (GST_stats,
1619 ("# messages not sent (no such peer or not connected)"),
1622 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1623 "Could not send message to peer `%s': no address available\n",
1624 GNUNET_i2s (target));
1628 cont (cont_cls, GNUNET_SYSERR);
1632 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
1633 GNUNET_STATISTICS_update (GST_stats,
1635 ("# bytes in message queue for other peers"),
1636 msg_size, GNUNET_NO);
1637 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
1639 mq->cont_cls = cont_cls;
1640 /* FIXME: this memcpy can be up to 7% of our total runtime! */
1641 memcpy (&mq[1], msg, msg_size);
1642 mq->message_buf = (const char *) &mq[1];
1643 mq->message_buf_size = msg_size;
1644 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
1645 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
1647 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
1648 (NULL == n->is_active))
1649 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
1654 * We have received a message from the given sender. How long should
1655 * we delay before receiving more? (Also used to keep the peer marked
1658 * @param sender sender of the message
1659 * @param size size of the message
1660 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
1661 * GNUNET_NO if the neighbour is not connected or violates the quota,
1662 * GNUNET_SYSERR if the connection is not fully up yet
1663 * @return how long to wait before reading more from this sender
1665 struct GNUNET_TIME_Relative
1666 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
1667 *sender, ssize_t size, int *do_forward)
1669 struct NeighbourMapEntry *n;
1670 struct GNUNET_TIME_Relative ret;
1672 // This can happen during shutdown
1673 if (neighbours == NULL)
1675 return GNUNET_TIME_UNIT_FOREVER_REL;
1678 n = lookup_neighbour (sender);
1681 GST_neighbours_try_connect (sender);
1682 n = lookup_neighbour (sender);
1685 GNUNET_STATISTICS_update (GST_stats,
1687 ("# messages discarded due to lack of neighbour record"),
1689 *do_forward = GNUNET_NO;
1690 return GNUNET_TIME_UNIT_ZERO;
1693 if (!is_connected (n))
1695 *do_forward = GNUNET_SYSERR;
1696 return GNUNET_TIME_UNIT_ZERO;
1698 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
1700 n->quota_violation_count++;
1702 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1703 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
1704 n->in_tracker.available_bytes_per_s__,
1705 n->quota_violation_count);
1707 /* Discount 32k per violation */
1708 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
1712 if (n->quota_violation_count > 0)
1714 /* try to add 32k back */
1715 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
1716 n->quota_violation_count--;
1719 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
1721 GNUNET_STATISTICS_update (GST_stats,
1723 ("# bandwidth quota violations by other peers"),
1725 *do_forward = GNUNET_NO;
1726 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
1728 *do_forward = GNUNET_YES;
1729 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 32 * 1024);
1730 if (ret.rel_value > 0)
1733 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1734 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
1735 (unsigned long long) n->in_tracker.
1736 consumption_since_last_update__,
1737 (unsigned int) n->in_tracker.available_bytes_per_s__,
1738 (unsigned long long) ret.rel_value);
1740 GNUNET_STATISTICS_update (GST_stats,
1741 gettext_noop ("# ms throttling suggested"),
1742 (int64_t) ret.rel_value, GNUNET_NO);
1749 * Keep the connection to the given neighbour alive longer,
1750 * we received a KEEPALIVE (or equivalent).
1752 * @param neighbour neighbour to keep alive
1755 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
1757 struct NeighbourMapEntry *n;
1759 // This can happen during shutdown
1760 if (neighbours == NULL)
1765 n = lookup_neighbour (neighbour);
1768 GNUNET_STATISTICS_update (GST_stats,
1770 ("# KEEPALIVE messages discarded (not connected)"),
1774 GNUNET_SCHEDULER_cancel (n->timeout_task);
1776 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
1777 &neighbour_timeout_task, n);
1782 * Change the incoming quota for the given peer.
1784 * @param neighbour identity of peer to change qutoa for
1785 * @param quota new quota
1788 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1789 struct GNUNET_BANDWIDTH_Value32NBO quota)
1791 struct NeighbourMapEntry *n;
1793 // This can happen during shutdown
1794 if (neighbours == NULL)
1799 n = lookup_neighbour (neighbour);
1802 GNUNET_STATISTICS_update (GST_stats,
1804 ("# SET QUOTA messages ignored (no such peer)"),
1808 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1809 if (0 != ntohl (quota.value__))
1812 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1813 GNUNET_i2s (&n->id), "SET_QUOTA");
1815 if (is_connected (n))
1816 GNUNET_STATISTICS_update (GST_stats,
1817 gettext_noop ("# disconnects due to quota of 0"),
1819 disconnect_neighbour (n);
1824 * Closure for the neighbours_iterate function.
1826 struct IteratorContext
1829 * Function to call on each connected neighbour.
1831 GST_NeighbourIterator cb;
1841 * Call the callback from the closure for each connected neighbour.
1843 * @param cls the 'struct IteratorContext'
1844 * @param key the hash of the public key of the neighbour
1845 * @param value the 'struct NeighbourMapEntry'
1846 * @return GNUNET_OK (continue to iterate)
1849 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1851 struct IteratorContext *ic = cls;
1852 struct NeighbourMapEntry *n = value;
1854 if (!is_connected (n))
1857 ic->cb (ic->cb_cls, &n->id, NULL, 0, n->address);
1863 * Iterate over all connected neighbours.
1865 * @param cb function to call
1866 * @param cb_cls closure for cb
1869 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1871 struct IteratorContext ic;
1873 // This can happen during shutdown
1874 if (neighbours == NULL)
1881 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1885 * If we have an active connection to the given target, it must be shutdown.
1887 * @param target peer to disconnect from
1890 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1892 struct NeighbourMapEntry *n;
1894 // This can happen during shutdown
1895 if (neighbours == NULL)
1900 n = lookup_neighbour (target);
1902 return; /* not active */
1903 if (is_connected (n))
1905 send_disconnect (&n->id, n->address, n->session);
1907 n = lookup_neighbour (target);
1909 return; /* gone already */
1911 disconnect_neighbour (n);
1916 * We received a disconnect message from the given peer,
1917 * validate and process.
1919 * @param peer sender of the message
1920 * @param msg the disconnect message
1923 GST_neighbours_handle_disconnect_message (const struct GNUNET_PeerIdentity
1925 const struct GNUNET_MessageHeader
1928 struct NeighbourMapEntry *n;
1929 const struct SessionDisconnectMessage *sdm;
1933 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
1934 "Received DISCONNECT message from peer `%s'\n",
1938 if (ntohs (msg->size) != sizeof (struct SessionDisconnectMessage))
1940 // GNUNET_break_op (0);
1941 GNUNET_STATISTICS_update (GST_stats,
1943 ("# disconnect messages ignored (old format)"), 1,
1947 sdm = (const struct SessionDisconnectMessage *) msg;
1948 n = lookup_neighbour (peer);
1950 return; /* gone already */
1951 if (GNUNET_TIME_absolute_ntoh (sdm->timestamp).abs_value <=
1952 n->connect_ts.abs_value)
1954 GNUNET_STATISTICS_update (GST_stats,
1956 ("# disconnect messages ignored (timestamp)"), 1,
1960 GNUNET_CRYPTO_hash (&sdm->public_key,
1961 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded),
1963 if (0 != memcmp (peer, &hc, sizeof (struct GNUNET_PeerIdentity)))
1965 GNUNET_break_op (0);
1968 if (ntohl (sdm->purpose.size) !=
1969 sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1970 sizeof (struct GNUNET_CRYPTO_RsaPublicKeyBinaryEncoded) +
1971 sizeof (struct GNUNET_TIME_AbsoluteNBO))
1973 GNUNET_break_op (0);
1977 GNUNET_CRYPTO_rsa_verify
1978 (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT, &sdm->purpose,
1979 &sdm->signature, &sdm->public_key))
1981 GNUNET_break_op (0);
1984 GST_neighbours_force_disconnect (peer);
1989 * We received a 'SESSION_CONNECT_ACK' message from the other peer.
1990 * Consider switching to it.
1992 * @param message possibly a 'struct SessionConnectMessage' (check format)
1993 * @param peer identity of the peer to switch the address for
1994 * @param address address of the other peer, NULL if other peer
1996 * @param session session to use (or NULL)
1997 * @param ats performance data
1998 * @param ats_count number of entries in ats
2001 GST_neighbours_handle_connect_ack (const struct GNUNET_MessageHeader *message,
2002 const struct GNUNET_PeerIdentity *peer,
2003 const struct GNUNET_HELLO_Address *address,
2004 struct Session *session,
2005 const struct GNUNET_ATS_Information *ats,
2008 const struct SessionConnectMessage *scm;
2009 struct QuotaSetMessage q_msg;
2010 struct GNUNET_MessageHeader msg;
2011 struct NeighbourMapEntry *n;
2017 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2018 "Received CONNECT_ACK message from peer `%s'\n",
2022 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2024 GNUNET_break_op (0);
2028 scm = (const struct SessionConnectMessage *) message;
2029 GNUNET_break_op (ntohl (scm->reserved) == 0);
2030 n = lookup_neighbour (peer);
2032 n = setup_neighbour (peer);
2034 if (!is_connecting(n))
2036 GNUNET_STATISTICS_update (GST_stats,
2037 gettext_noop ("# unexpected CONNECT_ACK messages"), 1,
2042 if (NULL != session)
2043 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2045 "Giving ATS session %p of plugin %s for peer %s\n",
2046 session, address->transport_name, GNUNET_i2s (peer));
2047 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2049 was_connected = is_connected (n);
2050 if (!is_connected (n))
2052 change_state (n, S_CONNECTED);
2053 GST_validation_set_address_use (&n->id,
2059 GNUNET_ATS_address_in_use (GST_ats, n->address, NULL, GNUNET_YES);
2062 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2063 "Setting inbound quota of %u for peer `%s' to \n",
2064 ntohl (n->bandwidth_in.value__), GNUNET_i2s (&n->id));
2066 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2068 /* send ACK (ACK) */
2069 msg_len = sizeof (msg);
2070 msg.size = htons (msg_len);
2071 msg.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_ACK);
2074 send_with_plugin (&n->id, (const char *) &msg, msg_len, UINT32_MAX,
2075 GNUNET_TIME_UNIT_FOREVER_REL, n->session,
2076 n->address, GNUNET_YES, NULL,
2079 if (ret == GNUNET_SYSERR)
2080 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2081 "Failed to send SESSION_ACK to `%4s' using address '%s' session %X\n",
2082 GNUNET_i2s (&n->id),
2083 GST_plugins_a2s (n->address), n->session);
2088 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2090 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2091 &neighbour_keepalive_task, n);
2093 neighbours_connected++;
2094 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2097 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2098 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2099 GNUNET_i2s (&n->id),
2100 GST_plugins_a2s (n->address), n->session,
2103 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2107 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2108 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2109 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2111 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2112 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2113 q_msg.quota = n->bandwidth_out;
2114 q_msg.peer = (*peer);
2115 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2120 GST_neighbours_handle_ack (const struct GNUNET_MessageHeader *message,
2121 const struct GNUNET_PeerIdentity *peer,
2122 const struct GNUNET_HELLO_Address *address,
2123 struct Session *session,
2124 const struct GNUNET_ATS_Information *ats,
2127 struct NeighbourMapEntry *n;
2128 struct QuotaSetMessage q_msg;
2132 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Received ACK message from peer `%s'\n",
2136 if (ntohs (message->size) != sizeof (struct GNUNET_MessageHeader))
2138 GNUNET_break_op (0);
2142 n = lookup_neighbour (peer);
2145 send_disconnect (peer, address,
2151 if (is_connected (n))
2154 if (!is_connecting(n))
2156 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# unexpected ACK messages"), 1,
2161 if (NULL != session)
2162 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2164 "Giving ATS session %p of plugin %s for peer %s\n",
2165 session, address->transport_name, GNUNET_i2s (peer));
2166 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2168 was_connected = is_connected (n);
2169 change_state (n, S_CONNECTED);
2171 GNUNET_ATS_address_in_use (GST_ats, n->address, n->session, GNUNET_YES);
2173 GST_neighbours_set_incoming_quota (&n->id, n->bandwidth_in);
2175 if (n->keepalive_task == GNUNET_SCHEDULER_NO_TASK)
2177 GNUNET_SCHEDULER_add_delayed (KEEPALIVE_FREQUENCY,
2178 &neighbour_keepalive_task, n);
2182 GST_validation_set_address_use (&n->id,
2186 neighbours_connected++;
2187 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
2191 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2192 "Notify about connect of `%4s' using address '%s' session %X LINE %u\n",
2193 GNUNET_i2s (&n->id),
2194 GST_plugins_a2s (n->address), n->session,
2197 connect_notify_cb (callback_cls, &n->id, ats, ats_count);
2200 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2201 "Sending outbound quota of %u Bps for peer `%s' to all clients\n",
2202 ntohl (n->bandwidth_out.value__), GNUNET_i2s (peer));
2204 q_msg.header.size = htons (sizeof (struct QuotaSetMessage));
2205 q_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SET_QUOTA);
2206 q_msg.quota = n->bandwidth_out;
2207 q_msg.peer = (*peer);
2208 GST_clients_broadcast (&q_msg.header, GNUNET_NO);
2211 struct BlackListCheckContext
2213 struct GNUNET_ATS_Information *ats;
2217 struct Session *session;
2219 struct GNUNET_HELLO_Address *address;
2221 struct GNUNET_TIME_Absolute ts;
2226 handle_connect_blacklist_cont (void *cls,
2227 const struct GNUNET_PeerIdentity *peer,
2230 struct NeighbourMapEntry *n;
2231 struct BlackListCheckContext *bcc = cls;
2234 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2235 "Blacklist check due to CONNECT message: `%s'\n",
2237 (result == GNUNET_OK) ? "ALLOWED" : "FORBIDDEN");
2241 if (GNUNET_OK != result)
2243 GNUNET_HELLO_address_free (bcc->address);
2248 n = lookup_neighbour (peer);
2250 n = setup_neighbour (peer);
2252 if (bcc->ts.abs_value > n->connect_ts.abs_value)
2254 if (NULL != bcc->session)
2255 GNUNET_log_from (GNUNET_ERROR_TYPE_DEBUG | GNUNET_ERROR_TYPE_BULK,
2257 "Giving ATS session %p of address `%s' for peer %s\n",
2259 GST_plugins_a2s (bcc->address),
2261 GNUNET_ATS_address_update (GST_ats, bcc->address,
2262 bcc->session, bcc->ats, bcc->ats_count);
2263 n->connect_ts = bcc->ts;
2268 if (n->state != S_CONNECT_RECV)
2269 change_state (n, S_CONNECT_RECV);
2271 /* Ask ATS for an address to connect via that address */
2272 if (n->ats_suggest != GNUNET_SCHEDULER_NO_TASK)
2273 GNUNET_SCHEDULER_cancel (n->ats_suggest);
2275 GNUNET_SCHEDULER_add_delayed (ATS_RESPONSE_TIMEOUT, ats_suggest_cancel,
2277 GNUNET_ATS_suggest_address (GST_ats, peer);
2281 * We received a 'SESSION_CONNECT' message from the other peer.
2282 * Consider switching to it.
2284 * @param message possibly a 'struct SessionConnectMessage' (check format)
2285 * @param peer identity of the peer to switch the address for
2286 * @param address address of the other peer, NULL if other peer
2288 * @param session session to use (or NULL)
2289 * @param ats performance data
2290 * @param ats_count number of entries in ats (excluding 0-termination)
2293 GST_neighbours_handle_connect (const struct GNUNET_MessageHeader *message,
2294 const struct GNUNET_PeerIdentity *peer,
2295 const struct GNUNET_HELLO_Address *address,
2296 struct Session *session,
2297 const struct GNUNET_ATS_Information *ats,
2300 const struct SessionConnectMessage *scm;
2301 struct NeighbourMapEntry *n;
2302 struct BlackListCheckContext *bcc = NULL;
2305 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
2306 "Received CONNECT message from peer `%s'\n", GNUNET_i2s (peer));
2309 if (ntohs (message->size) != sizeof (struct SessionConnectMessage))
2311 GNUNET_break_op (0);
2315 scm = (const struct SessionConnectMessage *) message;
2316 GNUNET_break_op (ntohl (scm->reserved) == 0);
2318 n = lookup_neighbour (peer);
2321 /* connected peer switches addresses */
2322 if (is_connected (n))
2324 GNUNET_ATS_address_update (GST_ats, address, session, ats, ats_count);
2329 /* we are not connected to this peer */
2330 /* do blacklist check */
2332 GNUNET_malloc (sizeof (struct BlackListCheckContext) +
2333 sizeof (struct GNUNET_ATS_Information) * (ats_count + 1));
2334 bcc->ts = GNUNET_TIME_absolute_ntoh (scm->timestamp);
2335 bcc->ats_count = ats_count + 1;
2336 bcc->address = GNUNET_HELLO_address_copy (address);
2337 bcc->session = session;
2338 bcc->ats = (struct GNUNET_ATS_Information *) &bcc[1];
2339 memcpy (bcc->ats, ats, sizeof (struct GNUNET_ATS_Information) * ats_count);
2340 bcc->ats[ats_count].type = htonl (GNUNET_ATS_QUALITY_NET_DELAY);
2341 bcc->ats[ats_count].value = htonl ((uint32_t) GST_neighbour_get_latency (peer).rel_value);
2342 GST_blacklist_test_allowed (peer, address->transport_name, handle_connect_blacklist_cont,
2347 /* end of file gnunet-service-transport_neighbours.c */