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.h"
32 #include "gnunet_peerinfo_service.h"
33 #include "gnunet_constants.h"
34 #include "transport.h"
38 * Size of the neighbour hash map.
40 #define NEIGHBOUR_TABLE_SIZE 256
43 * How often must a peer violate bandwidth quotas before we start
44 * to simply drop its messages?
46 #define QUOTA_VIOLATION_DROP_THRESHOLD 10
50 * Entry in neighbours.
52 struct NeighbourMapEntry;
55 * Message a peer sends to another to indicate its
56 * preference for communicating via a particular
57 * session (and the desire to establish a real
60 struct SessionConnectMessage
63 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT'
65 struct GNUNET_MessageHeader header;
70 uint32_t reserved GNUNET_PACKED;
73 * Absolute time at the sender. Only the most recent connect
74 * message implies which session is preferred by the sender.
76 struct GNUNET_TIME_AbsoluteNBO timestamp;
81 struct SessionDisconnectMessage
84 * Header of type 'GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT'
86 struct GNUNET_MessageHeader header;
91 uint32_t reserved GNUNET_PACKED;
94 * Purpose of the signature. Extends over the timestamp.
95 * Purpose should be GNUNET_SIGNATURE_PURPOSE_TRANSPORT_DISCONNECT.
97 struct GNUNET_CRYPTO_RsaSignaturePurpose purpose;
100 * Absolute time at the sender. Only the most recent connect
101 * message implies which session is preferred by the sender.
103 struct GNUNET_TIME_AbsoluteNBO timestamp;
106 * Signature of the peer that sends us the disconnect. Only
107 * valid if the timestamp is AFTER the timestamp from the
108 * corresponding 'CONNECT' message.
110 struct GNUNET_CRYPTO_RsaSignature signature;
115 * For each neighbour we keep a list of messages
116 * that we still want to transmit to the neighbour.
122 * This is a doubly linked list.
124 struct MessageQueue *next;
127 * This is a doubly linked list.
129 struct MessageQueue *prev;
132 * Once this message is actively being transmitted, which
133 * neighbour is it associated with?
135 struct NeighbourMapEntry *n;
138 * Function to call once we're done.
140 GST_NeighbourSendContinuation cont;
148 * The message(s) we want to transmit, GNUNET_MessageHeader(s)
149 * stuck together in memory. Allocated at the end of this struct.
151 const char *message_buf;
154 * Size of the message buf
156 size_t message_buf_size;
159 * At what time should we fail?
161 struct GNUNET_TIME_Absolute timeout;
167 * Entry in neighbours.
169 struct NeighbourMapEntry
173 * Head of list of messages we would like to send to this peer;
174 * must contain at most one message per client.
176 struct MessageQueue *messages_head;
179 * Tail of list of messages we would like to send to this peer; must
180 * contain at most one message per client.
182 struct MessageQueue *messages_tail;
185 * Context for address suggestion.
186 * NULL after we are connected.
188 struct GNUNET_ATS_SuggestionContext *asc;
191 * Performance data for the peer.
193 struct GNUNET_TRANSPORT_ATS_Information *ats;
196 * Are we currently trying to send a message? If so, which one?
198 struct MessageQueue *is_active;
201 * Active session for communicating with the peer.
203 struct Session *session;
206 * Name of the plugin we currently use.
211 * Address used for communicating with the peer, NULL for inbound connections.
216 * Number of bytes in 'addr'.
221 * Identity of this neighbour.
223 struct GNUNET_PeerIdentity id;
226 * ID of task scheduled to run when this peer is about to
227 * time out (will free resources associated with the peer).
229 GNUNET_SCHEDULER_TaskIdentifier timeout_task;
232 * ID of task scheduled to run when we should try transmitting
233 * the head of the message queue.
235 GNUNET_SCHEDULER_TaskIdentifier transmission_task;
238 * Tracker for inbound bandwidth.
240 struct GNUNET_BANDWIDTH_Tracker in_tracker;
243 * How often has the other peer (recently) violated the inbound
244 * traffic limit? Incremented by 10 per violation, decremented by 1
245 * per non-violation (for each time interval).
247 unsigned int quota_violation_count;
250 * Number of values in 'ats' array.
252 unsigned int ats_count;
255 * Are we already in the process of disconnecting this neighbour?
260 * Do we currently consider this neighbour connected? (as far as
261 * the connect/disconnect callbacks are concerned)?
269 * All known neighbours and their HELLOs.
271 static struct GNUNET_CONTAINER_MultiHashMap *neighbours;
274 * Closure for connect_notify_cb and disconnect_notify_cb
276 static void *callback_cls;
279 * Function to call when we connected to a neighbour.
281 static GNUNET_TRANSPORT_NotifyConnect connect_notify_cb;
284 * Function to call when we disconnected from a neighbour.
286 static GNUNET_TRANSPORT_NotifyDisconnect disconnect_notify_cb;
289 * counter for connected neighbours
291 static int neighbours_connected;
294 * Lookup a neighbour entry in the neighbours hash map.
296 * @param pid identity of the peer to look up
297 * @return the entry, NULL if there is no existing record
299 static struct NeighbourMapEntry *
300 lookup_neighbour (const struct GNUNET_PeerIdentity *pid)
302 return GNUNET_CONTAINER_multihashmap_get (neighbours, &pid->hashPubKey);
307 * Task invoked to start a transmission to another peer.
309 * @param cls the 'struct NeighbourMapEntry'
310 * @param tc scheduler context
313 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc);
317 * We're done with our transmission attempt, continue processing.
319 * @param cls the 'struct MessageQueue' of the message
320 * @param receiver intended receiver
321 * @param success whether it worked or not
324 transmit_send_continuation (void *cls,
325 const struct GNUNET_PeerIdentity *receiver,
328 struct MessageQueue *mq;
329 struct NeighbourMapEntry *n;
335 GNUNET_assert (n->is_active == mq);
337 GNUNET_assert (n->transmission_task == GNUNET_SCHEDULER_NO_TASK);
338 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
340 if (NULL != mq->cont)
341 mq->cont (mq->cont_cls, success);
347 * Check the ready list for the given neighbour and if a plugin is
348 * ready for transmission (and if we have a message), do so!
350 * @param n target peer for which to transmit
353 try_transmission_to_peer (struct NeighbourMapEntry *n)
355 struct MessageQueue *mq;
356 struct GNUNET_TIME_Relative timeout;
358 struct GNUNET_TRANSPORT_PluginFunctions *papi;
360 if (n->is_active != NULL)
361 return; /* transmission already pending */
362 if (n->transmission_task != GNUNET_SCHEDULER_NO_TASK)
363 return; /* currently waiting for bandwidth */
364 mq = n->messages_head;
365 while (NULL != (mq = n->messages_head))
367 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
368 if (timeout.rel_value > 0)
370 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR); /* timeout */
373 return; /* no more messages */
375 papi = GST_plugins_find (n->plugin_name);
381 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
385 if (((n->session == NULL) && (n->addr == NULL) && (n->addrlen == 0)))
387 GNUNET_log (GNUNET_ERROR_TYPE_ERROR, "No address peer for peer `%s'\n",
388 GNUNET_i2s (&n->id));
389 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
390 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
395 papi->send (papi->cls, &n->id, mq->message_buf, mq->message_buf_size,
396 0 /* priority -- remove from plugin API? */ ,
397 timeout, n->session, n->addr, n->addrlen, GNUNET_YES,
398 &transmit_send_continuation, mq);
401 /* failure, but 'send' would not call continuation in this case,
402 * so we need to do it here! */
403 transmit_send_continuation (mq, &n->id, GNUNET_SYSERR);
404 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
410 * Task invoked to start a transmission to another peer.
412 * @param cls the 'struct NeighbourMapEntry'
413 * @param tc scheduler context
416 transmission_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
418 struct NeighbourMapEntry *n = cls;
420 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
421 try_transmission_to_peer (n);
426 * Initialize the neighbours subsystem.
428 * @param cls closure for callbacks
429 * @param connect_cb function to call if we connect to a peer
430 * @param disconnect_cb function to call if we disconnect from a peer
433 GST_neighbours_start (void *cls, GNUNET_TRANSPORT_NotifyConnect connect_cb,
434 GNUNET_TRANSPORT_NotifyDisconnect disconnect_cb)
437 connect_notify_cb = connect_cb;
438 disconnect_notify_cb = disconnect_cb;
439 neighbours = GNUNET_CONTAINER_multihashmap_create (NEIGHBOUR_TABLE_SIZE);
444 * Disconnect from the given neighbour, clean up the record.
446 * @param n neighbour to disconnect from
449 disconnect_neighbour (struct NeighbourMapEntry *n)
451 struct MessageQueue *mq;
453 if (GNUNET_YES == n->in_disconnect)
455 n->in_disconnect = GNUNET_YES;
456 while (NULL != (mq = n->messages_head))
458 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
459 mq->cont (mq->cont_cls, GNUNET_SYSERR);
462 if (NULL != n->is_active)
464 n->is_active->n = NULL;
467 if (GNUNET_YES == n->is_connected)
469 n->is_connected = GNUNET_NO;
471 GNUNET_assert (neighbours_connected > 0);
472 neighbours_connected--;
474 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), -1,
476 disconnect_notify_cb (callback_cls, &n->id);
478 GNUNET_assert (GNUNET_YES ==
479 GNUNET_CONTAINER_multihashmap_remove (neighbours,
480 &n->id.hashPubKey, n));
481 if (GNUNET_SCHEDULER_NO_TASK != n->timeout_task)
483 GNUNET_SCHEDULER_cancel (n->timeout_task);
484 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
486 if (GNUNET_SCHEDULER_NO_TASK != n->transmission_task)
488 GNUNET_SCHEDULER_cancel (n->transmission_task);
489 n->transmission_task = GNUNET_SCHEDULER_NO_TASK;
493 GNUNET_ATS_suggest_address_cancel (n->asc);
496 GNUNET_array_grow (n->ats, n->ats_count, 0);
497 if (NULL != n->plugin_name)
499 GNUNET_free (n->plugin_name);
500 n->plugin_name = NULL;
504 GNUNET_free (n->addr);
514 * Peer has been idle for too long. Disconnect.
516 * @param cls the 'struct NeighbourMapEntry' of the neighbour that went idle
517 * @param tc scheduler context
520 neighbour_timeout_task (void *cls,
521 const struct GNUNET_SCHEDULER_TaskContext *tc)
523 struct NeighbourMapEntry *n = cls;
525 n->timeout_task = GNUNET_SCHEDULER_NO_TASK;
526 disconnect_neighbour (n);
531 * Disconnect from the given neighbour.
534 * @param key hash of neighbour's public key (not used)
535 * @param value the 'struct NeighbourMapEntry' of the neighbour
538 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
540 struct NeighbourMapEntry *n = value;
543 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
544 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
546 disconnect_neighbour (n);
552 * Cleanup the neighbours subsystem.
555 GST_neighbours_stop ()
557 GNUNET_assert (neighbours != NULL);
559 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
561 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
562 GNUNET_assert (neighbours_connected == 0);
565 connect_notify_cb = NULL;
566 disconnect_notify_cb = NULL;
571 * For an existing neighbour record, set the active connection to
574 * @param peer identity of the peer to switch the address for
575 * @param plugin_name name of transport that delivered the PONG
576 * @param address address of the other peer, NULL if other peer
578 * @param address_len number of bytes in address
579 * @param session session to use (or NULL)
580 * @param ats performance data
581 * @param ats_count number of entries in ats (excluding 0-termination)
584 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
585 const char *plugin_name, const void *address,
586 size_t address_len, struct Session *session,
587 const struct GNUNET_TRANSPORT_ATS_Information
588 *ats, uint32_t ats_count)
590 struct NeighbourMapEntry *n;
591 struct SessionConnectMessage connect_msg;
593 GNUNET_assert (neighbours != NULL);
595 n = lookup_neighbour (peer);
603 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
604 "SWITCH! Peer `%4s' switches to plugin `%s' address '%s' session %X\n",
605 GNUNET_i2s (peer), plugin_name,
606 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
612 GNUNET_free_non_null (n->addr);
613 n->addr = GNUNET_malloc (address_len);
614 memcpy (n->addr, address, address_len);
615 n->addrlen = address_len;
616 n->session = session;
617 GNUNET_array_grow (n->ats, n->ats_count, ats_count);
619 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
620 GNUNET_free_non_null (n->plugin_name);
621 n->plugin_name = GNUNET_strdup (plugin_name);
622 GNUNET_SCHEDULER_cancel (n->timeout_task);
624 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
625 &neighbour_timeout_task, n);
626 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
627 connect_msg.header.type =
628 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
629 connect_msg.reserved = htonl (0);
630 connect_msg.timestamp =
631 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
632 GST_neighbours_send (peer, &connect_msg, sizeof (connect_msg),
633 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
638 * Try to connect to the target peer using the given address
640 * @param cls the 'struct NeighbourMapEntry' of the target
641 * @param target identity of the target peer
642 * @param plugin_name name of the plugin
643 * @param plugin_address binary address
644 * @param plugin_address_len length of address
645 * @param session session to use
646 * @param bandwidth available bandwidth
647 * @param ats performance data for the address (as far as known)
648 * @param ats_count number of performance records in 'ats'
651 try_connect_using_address (void *cls, const struct GNUNET_PeerIdentity *target,
652 const char *plugin_name, const void *plugin_address,
653 size_t plugin_address_len, struct Session *session,
654 struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
655 const struct GNUNET_TRANSPORT_ATS_Information *ats,
658 struct NeighbourMapEntry *n = cls;
662 was_connected = n->is_connected;
663 n->is_connected = GNUNET_YES;
664 GST_neighbours_switch_to_address (target, plugin_name, plugin_address,
665 plugin_address_len, session, ats,
667 if (GNUNET_YES == was_connected)
670 neighbours_connected++;
671 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
673 connect_notify_cb (callback_cls, target, n->ats, n->ats_count);
678 * Try to create a connection to the given target (eventually).
680 * @param target peer to try to connect to
683 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
685 struct NeighbourMapEntry *n;
687 GNUNET_assert (neighbours != NULL);
690 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
691 GNUNET_i2s (target));
695 memcmp (target, &GST_my_identity,
696 sizeof (struct GNUNET_PeerIdentity)));
697 n = lookup_neighbour (target);
698 if ((NULL != n) && (GNUNET_YES == n->is_connected))
699 return; /* already connected */
703 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
704 "Unknown peer `%s', creating new neighbour\n",
705 GNUNET_i2s (target));
707 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
709 GNUNET_array_grow (n->ats, n->ats_count, 1);
710 n->ats[0].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);;
711 n->ats[0].value = htonl (0);
712 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
713 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
714 MAX_BANDWIDTH_CARRY_S);
716 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
717 &neighbour_timeout_task, n);
718 GNUNET_assert (GNUNET_OK ==
719 GNUNET_CONTAINER_multihashmap_put (neighbours,
720 &n->id.hashPubKey, n,
721 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
724 return; /* already trying */
726 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
727 "Asking ATS for suggested address to connect to peer `%s'\n",
728 GNUNET_i2s (target));
731 GNUNET_ATS_suggest_address (GST_ats, target, &try_connect_using_address,
737 * Test if we're connected to the given peer.
739 * @param target peer to test
740 * @return GNUNET_YES if we are connected, GNUNET_NO if not
743 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
745 struct NeighbourMapEntry *n;
747 GNUNET_assert (neighbours != NULL);
749 n = lookup_neighbour (target);
750 if ((NULL == n) || (n->is_connected != GNUNET_YES))
751 return GNUNET_NO; /* not connected */
757 * A session was terminated. Take note.
759 * @param peer identity of the peer where the session died
760 * @param session session that is gone
763 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
764 struct Session *session)
766 struct NeighbourMapEntry *n;
768 GNUNET_assert (neighbours != NULL);
771 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
772 "Session %X to peer `%s' ended \n",
773 session, GNUNET_i2s (peer));
776 n = lookup_neighbour (peer);
779 if (session != n->session)
780 return; /* doesn't affect us */
783 GNUNET_free (n->addr);
788 if (GNUNET_YES != n->is_connected)
789 return; /* not connected anymore anyway, shouldn't matter */
791 /* we are not connected until ATS suggests a new address */
792 //n->is_connected = GNUNET_NO;
794 GNUNET_SCHEDULER_cancel (n->timeout_task);
796 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
797 &neighbour_timeout_task, n);
798 /* try QUICKLY to re-establish a connection, reduce timeout! */
801 /* how can this be!? */
806 GNUNET_ATS_suggest_address (GST_ats, peer, &try_connect_using_address, n);
811 * Transmit a message to the given target using the active connection.
813 * @param target destination
814 * @param msg message to send
815 * @param msg_size number of bytes in msg
816 * @param timeout when to fail with timeout
817 * @param cont function to call when done
818 * @param cont_cls closure for 'cont'
821 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
822 size_t msg_size, struct GNUNET_TIME_Relative timeout,
823 GST_NeighbourSendContinuation cont, void *cont_cls)
825 struct NeighbourMapEntry *n;
826 struct MessageQueue *mq;
828 GNUNET_assert (neighbours != NULL);
830 n = lookup_neighbour (target);
831 if ((n == NULL) || (GNUNET_YES != n->is_connected))
833 GNUNET_STATISTICS_update (GST_stats,
835 ("# messages not sent (no such peer or not connected)"),
839 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
840 "Could not send message to peer `%s': unknown neighbor",
841 GNUNET_i2s (target));
842 else if (GNUNET_YES != n->is_connected)
843 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
844 "Could not send message to peer `%s': not connected\n",
845 GNUNET_i2s (target));
848 cont (cont_cls, GNUNET_SYSERR);
852 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
854 GNUNET_STATISTICS_update (GST_stats,
856 ("# messages not sent (no such peer or not connected)"),
859 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
860 "Could not send message to peer `%s': no address available\n",
861 GNUNET_i2s (target));
865 cont (cont_cls, GNUNET_SYSERR);
870 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
871 GNUNET_STATISTICS_update (GST_stats,
873 ("# bytes in message queue for other peers"),
874 msg_size, GNUNET_NO);
875 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
877 mq->cont_cls = cont_cls;
878 /* FIXME: this memcpy can be up to 7% of our total runtime! */
879 memcpy (&mq[1], msg, msg_size);
880 mq->message_buf = (const char *) &mq[1];
881 mq->message_buf_size = msg_size;
882 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
883 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
884 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
885 (NULL == n->is_active))
886 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
891 * We have received a message from the given sender. How long should
892 * we delay before receiving more? (Also used to keep the peer marked
895 * @param sender sender of the message
896 * @param size size of the message
897 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
898 * GNUNET_NO if the neighbour is not connected or violates the quota
899 * @return how long to wait before reading more from this sender
901 struct GNUNET_TIME_Relative
902 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
903 *sender, ssize_t size, int *do_forward)
905 struct NeighbourMapEntry *n;
906 struct GNUNET_TIME_Relative ret;
908 GNUNET_assert (neighbours != NULL);
910 n = lookup_neighbour (sender);
913 *do_forward = GNUNET_NO;
914 return GNUNET_TIME_UNIT_ZERO;
916 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
918 n->quota_violation_count++;
920 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
921 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
922 n->in_tracker.available_bytes_per_s__,
923 n->quota_violation_count);
925 /* Discount 32k per violation */
926 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
930 if (n->quota_violation_count > 0)
932 /* try to add 32k back */
933 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
934 n->quota_violation_count--;
937 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
939 GNUNET_STATISTICS_update (GST_stats,
941 ("# bandwidth quota violations by other peers"),
943 *do_forward = GNUNET_NO;
944 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
946 *do_forward = GNUNET_YES;
947 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
948 if (ret.rel_value > 0)
951 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
952 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
953 (unsigned long long) n->in_tracker.
954 consumption_since_last_update__,
955 (unsigned int) n->in_tracker.available_bytes_per_s__,
956 (unsigned long long) ret.rel_value);
958 GNUNET_STATISTICS_update (GST_stats,
959 gettext_noop ("# ms throttling suggested"),
960 (int64_t) ret.rel_value, GNUNET_NO);
967 * Keep the connection to the given neighbour alive longer,
968 * we received a KEEPALIVE (or equivalent).
970 * @param neighbour neighbour to keep alive
973 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
975 struct NeighbourMapEntry *n;
977 GNUNET_assert (neighbours != NULL);
979 n = lookup_neighbour (neighbour);
982 GNUNET_STATISTICS_update (GST_stats,
984 ("# KEEPALIVE messages discarded (not connected)"),
988 GNUNET_SCHEDULER_cancel (n->timeout_task);
990 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
991 &neighbour_timeout_task, n);
996 * Change the incoming quota for the given peer.
998 * @param neighbour identity of peer to change qutoa for
999 * @param quota new quota
1002 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1003 struct GNUNET_BANDWIDTH_Value32NBO quota)
1005 struct NeighbourMapEntry *n;
1007 GNUNET_assert (neighbours != NULL);
1009 n = lookup_neighbour (neighbour);
1012 GNUNET_STATISTICS_update (GST_stats,
1014 ("# SET QUOTA messages ignored (no such peer)"),
1018 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1019 if (0 != ntohl (quota.value__))
1022 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1023 GNUNET_i2s (&n->id), "SET_QUOTA");
1025 GNUNET_STATISTICS_update (GST_stats,
1026 gettext_noop ("# disconnects due to quota of 0"), 1,
1028 disconnect_neighbour (n);
1033 * Closure for the neighbours_iterate function.
1035 struct IteratorContext
1038 * Function to call on each connected neighbour.
1040 GST_NeighbourIterator cb;
1050 * Call the callback from the closure for each connected neighbour.
1052 * @param cls the 'struct IteratorContext'
1053 * @param key the hash of the public key of the neighbour
1054 * @param value the 'struct NeighbourMapEntry'
1055 * @return GNUNET_OK (continue to iterate)
1058 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1060 struct IteratorContext *ic = cls;
1061 struct NeighbourMapEntry *n = value;
1063 if (GNUNET_YES != n->is_connected)
1066 GNUNET_assert (n->ats_count > 0);
1067 ic->cb (ic->cb_cls, &n->id, n->ats, n->ats_count);
1073 * Iterate over all connected neighbours.
1075 * @param cb function to call
1076 * @param cb_cls closure for cb
1079 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1081 struct IteratorContext ic;
1083 GNUNET_assert (neighbours != NULL);
1087 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1092 * If we have an active connection to the given target, it must be shutdown.
1094 * @param target peer to disconnect from
1097 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1099 struct NeighbourMapEntry *n;
1100 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1101 struct SessionDisconnectMessage disconnect_msg;
1103 GNUNET_assert (neighbours != NULL);
1105 n = lookup_neighbour (target);
1107 return; /* not active */
1108 if (GNUNET_YES == n->is_connected)
1110 /* we're actually connected, send DISCONNECT message */
1111 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1112 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1113 disconnect_msg.reserved = htonl (0);
1114 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1115 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1116 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1117 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1118 GNUNET_assert (GNUNET_OK ==
1119 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1120 &disconnect_msg.purpose,
1121 &disconnect_msg.signature));
1122 papi = GST_plugins_find (n->plugin_name);
1124 papi->send (papi->cls, target, (const void *) &disconnect_msg,
1125 sizeof (struct GNUNET_MessageHeader),
1126 UINT32_MAX /* priority */ ,
1127 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
1128 GNUNET_YES, NULL, NULL);
1130 disconnect_neighbour (n);
1134 /* end of file gnunet-service-transport_neighbours.c */