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 while (NULL != (mq = n->messages_head))
366 timeout = GNUNET_TIME_absolute_get_remaining (mq->timeout);
367 if (timeout.rel_value > 0)
369 GNUNET_CONTAINER_DLL_remove (n->messages_head, n->messages_tail, mq);
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 if (GNUNET_YES == n->is_connected)
527 GNUNET_STATISTICS_update (GST_stats,
528 gettext_noop ("# peers disconnected due to timeout"), 1,
530 disconnect_neighbour (n);
535 * Disconnect from the given neighbour.
538 * @param key hash of neighbour's public key (not used)
539 * @param value the 'struct NeighbourMapEntry' of the neighbour
542 disconnect_all_neighbours (void *cls, const GNUNET_HashCode * key, void *value)
544 struct NeighbourMapEntry *n = value;
547 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s', %s\n",
548 GNUNET_i2s (&n->id), "SHUTDOWN_TASK");
550 if (GNUNET_YES == n->is_connected)
551 GNUNET_STATISTICS_update (GST_stats,
552 gettext_noop ("# peers disconnected due to global disconnect"), 1,
554 disconnect_neighbour (n);
560 * Cleanup the neighbours subsystem.
563 GST_neighbours_stop ()
565 GNUNET_assert (neighbours != NULL);
567 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &disconnect_all_neighbours,
569 GNUNET_CONTAINER_multihashmap_destroy (neighbours);
570 GNUNET_assert (neighbours_connected == 0);
573 connect_notify_cb = NULL;
574 disconnect_notify_cb = NULL;
579 * For an existing neighbour record, set the active connection to
582 * @param peer identity of the peer to switch the address for
583 * @param plugin_name name of transport that delivered the PONG
584 * @param address address of the other peer, NULL if other peer
586 * @param address_len number of bytes in address
587 * @param session session to use (or NULL)
588 * @param ats performance data
589 * @param ats_count number of entries in ats (excluding 0-termination)
592 GST_neighbours_switch_to_address (const struct GNUNET_PeerIdentity *peer,
593 const char *plugin_name, const void *address,
594 size_t address_len, struct Session *session,
595 const struct GNUNET_TRANSPORT_ATS_Information
596 *ats, uint32_t ats_count)
598 struct NeighbourMapEntry *n;
599 struct SessionConnectMessage connect_msg;
601 GNUNET_assert (neighbours != NULL);
603 n = lookup_neighbour (peer);
611 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
612 "SWITCH! Peer `%4s' switches to plugin `%s' address '%s' session %X\n",
613 GNUNET_i2s (peer), plugin_name,
614 (address_len == 0) ? "<inbound>" : GST_plugins_a2s (plugin_name,
620 GNUNET_free_non_null (n->addr);
621 n->addr = GNUNET_malloc (address_len);
622 memcpy (n->addr, address, address_len);
623 n->addrlen = address_len;
624 n->session = session;
625 GNUNET_array_grow (n->ats, n->ats_count, ats_count);
627 ats_count * sizeof (struct GNUNET_TRANSPORT_ATS_Information));
628 GNUNET_free_non_null (n->plugin_name);
629 n->plugin_name = GNUNET_strdup (plugin_name);
630 GNUNET_SCHEDULER_cancel (n->timeout_task);
632 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
633 &neighbour_timeout_task, n);
634 connect_msg.header.size = htons (sizeof (struct SessionConnectMessage));
635 connect_msg.header.type =
636 htons (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_CONNECT);
637 connect_msg.reserved = htonl (0);
638 connect_msg.timestamp =
639 GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
640 GST_neighbours_send (peer, &connect_msg, sizeof (connect_msg),
641 GNUNET_TIME_UNIT_FOREVER_REL, NULL, NULL);
646 * Try to connect to the target peer using the given address
648 * @param cls the 'struct NeighbourMapEntry' of the target
649 * @param target identity of the target peer
650 * @param plugin_name name of the plugin
651 * @param plugin_address binary address
652 * @param plugin_address_len length of address
653 * @param session session to use
654 * @param bandwidth available bandwidth
655 * @param ats performance data for the address (as far as known)
656 * @param ats_count number of performance records in 'ats'
659 try_connect_using_address (void *cls, const struct GNUNET_PeerIdentity *target,
660 const char *plugin_name, const void *plugin_address,
661 size_t plugin_address_len, struct Session *session,
662 struct GNUNET_BANDWIDTH_Value32NBO bandwidth,
663 const struct GNUNET_TRANSPORT_ATS_Information *ats,
666 struct NeighbourMapEntry *n = cls;
670 was_connected = n->is_connected;
671 n->is_connected = GNUNET_YES;
672 GST_neighbours_switch_to_address (target, plugin_name, plugin_address,
673 plugin_address_len, session, ats,
675 if (GNUNET_YES == was_connected)
678 neighbours_connected++;
679 GNUNET_STATISTICS_update (GST_stats, gettext_noop ("# peers connected"), 1,
681 connect_notify_cb (callback_cls, target, n->ats, n->ats_count);
686 * Try to create a connection to the given target (eventually).
688 * @param target peer to try to connect to
691 GST_neighbours_try_connect (const struct GNUNET_PeerIdentity *target)
693 struct NeighbourMapEntry *n;
695 GNUNET_assert (neighbours != NULL);
698 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Trying to connect to peer `%s'\n",
699 GNUNET_i2s (target));
703 memcmp (target, &GST_my_identity,
704 sizeof (struct GNUNET_PeerIdentity)));
705 n = lookup_neighbour (target);
706 if ((NULL != n) && (GNUNET_YES == n->is_connected))
707 return; /* already connected */
711 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
712 "Unknown peer `%s', creating new neighbour\n",
713 GNUNET_i2s (target));
715 n = GNUNET_malloc (sizeof (struct NeighbourMapEntry));
717 GNUNET_array_grow (n->ats, n->ats_count, 1);
718 n->ats[0].type = htonl (GNUNET_TRANSPORT_ATS_ARRAY_TERMINATOR);;
719 n->ats[0].value = htonl (0);
720 GNUNET_BANDWIDTH_tracker_init (&n->in_tracker,
721 GNUNET_CONSTANTS_DEFAULT_BW_IN_OUT,
722 MAX_BANDWIDTH_CARRY_S);
724 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
725 &neighbour_timeout_task, n);
726 GNUNET_assert (GNUNET_OK ==
727 GNUNET_CONTAINER_multihashmap_put (neighbours,
728 &n->id.hashPubKey, n,
729 GNUNET_CONTAINER_MULTIHASHMAPOPTION_UNIQUE_ONLY));
732 return; /* already trying */
734 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
735 "Asking ATS for suggested address to connect to peer `%s'\n",
736 GNUNET_i2s (target));
739 GNUNET_ATS_suggest_address (GST_ats, target, &try_connect_using_address,
745 * Test if we're connected to the given peer.
747 * @param target peer to test
748 * @return GNUNET_YES if we are connected, GNUNET_NO if not
751 GST_neighbours_test_connected (const struct GNUNET_PeerIdentity *target)
753 struct NeighbourMapEntry *n;
755 GNUNET_assert (neighbours != NULL);
757 n = lookup_neighbour (target);
758 if ((NULL == n) || (n->is_connected != GNUNET_YES))
759 return GNUNET_NO; /* not connected */
765 * A session was terminated. Take note.
767 * @param peer identity of the peer where the session died
768 * @param session session that is gone
771 GST_neighbours_session_terminated (const struct GNUNET_PeerIdentity *peer,
772 struct Session *session)
774 struct NeighbourMapEntry *n;
776 GNUNET_assert (neighbours != NULL);
779 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
780 "Session %X to peer `%s' ended \n",
781 session, GNUNET_i2s (peer));
784 n = lookup_neighbour (peer);
787 if (session != n->session)
788 return; /* doesn't affect us */
791 GNUNET_free (n->addr);
796 if (GNUNET_YES != n->is_connected)
797 return; /* not connected anymore anyway, shouldn't matter */
799 /* we are not connected until ATS suggests a new address */
800 //n->is_connected = GNUNET_NO;
802 GNUNET_SCHEDULER_cancel (n->timeout_task);
804 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
805 &neighbour_timeout_task, n);
806 /* try QUICKLY to re-establish a connection, reduce timeout! */
809 /* how can this be!? */
814 GNUNET_ATS_suggest_address (GST_ats, peer, &try_connect_using_address, n);
819 * Transmit a message to the given target using the active connection.
821 * @param target destination
822 * @param msg message to send
823 * @param msg_size number of bytes in msg
824 * @param timeout when to fail with timeout
825 * @param cont function to call when done
826 * @param cont_cls closure for 'cont'
829 GST_neighbours_send (const struct GNUNET_PeerIdentity *target, const void *msg,
830 size_t msg_size, struct GNUNET_TIME_Relative timeout,
831 GST_NeighbourSendContinuation cont, void *cont_cls)
833 struct NeighbourMapEntry *n;
834 struct MessageQueue *mq;
836 GNUNET_assert (neighbours != NULL);
838 n = lookup_neighbour (target);
839 if ((n == NULL) || (GNUNET_YES != n->is_connected))
841 GNUNET_STATISTICS_update (GST_stats,
843 ("# messages not sent (no such peer or not connected)"),
847 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
848 "Could not send message to peer `%s': unknown neighbor",
849 GNUNET_i2s (target));
850 else if (GNUNET_YES != n->is_connected)
851 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
852 "Could not send message to peer `%s': not connected\n",
853 GNUNET_i2s (target));
856 cont (cont_cls, GNUNET_SYSERR);
860 if ((n->session == NULL) && (n->addr == NULL) && (n->addrlen ==0))
862 GNUNET_STATISTICS_update (GST_stats,
864 ("# messages not sent (no such peer or not connected)"),
867 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
868 "Could not send message to peer `%s': no address available\n",
869 GNUNET_i2s (target));
873 cont (cont_cls, GNUNET_SYSERR);
878 GNUNET_assert (msg_size >= sizeof (struct GNUNET_MessageHeader));
879 GNUNET_STATISTICS_update (GST_stats,
881 ("# bytes in message queue for other peers"),
882 msg_size, GNUNET_NO);
883 mq = GNUNET_malloc (sizeof (struct MessageQueue) + msg_size);
885 mq->cont_cls = cont_cls;
886 /* FIXME: this memcpy can be up to 7% of our total runtime! */
887 memcpy (&mq[1], msg, msg_size);
888 mq->message_buf = (const char *) &mq[1];
889 mq->message_buf_size = msg_size;
890 mq->timeout = GNUNET_TIME_relative_to_absolute (timeout);
891 GNUNET_CONTAINER_DLL_insert_tail (n->messages_head, n->messages_tail, mq);
892 if ((GNUNET_SCHEDULER_NO_TASK == n->transmission_task) &&
893 (NULL == n->is_active))
894 n->transmission_task = GNUNET_SCHEDULER_add_now (&transmission_task, n);
899 * We have received a message from the given sender. How long should
900 * we delay before receiving more? (Also used to keep the peer marked
903 * @param sender sender of the message
904 * @param size size of the message
905 * @param do_forward set to GNUNET_YES if the message should be forwarded to clients
906 * GNUNET_NO if the neighbour is not connected or violates the quota
907 * @return how long to wait before reading more from this sender
909 struct GNUNET_TIME_Relative
910 GST_neighbours_calculate_receive_delay (const struct GNUNET_PeerIdentity
911 *sender, ssize_t size, int *do_forward)
913 struct NeighbourMapEntry *n;
914 struct GNUNET_TIME_Relative ret;
916 GNUNET_assert (neighbours != NULL);
918 n = lookup_neighbour (sender);
921 *do_forward = GNUNET_NO;
922 return GNUNET_TIME_UNIT_ZERO;
924 if (GNUNET_YES == GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, size))
926 n->quota_violation_count++;
928 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
929 "Bandwidth quota (%u b/s) violation detected (total of %u).\n",
930 n->in_tracker.available_bytes_per_s__,
931 n->quota_violation_count);
933 /* Discount 32k per violation */
934 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, -32 * 1024);
938 if (n->quota_violation_count > 0)
940 /* try to add 32k back */
941 GNUNET_BANDWIDTH_tracker_consume (&n->in_tracker, 32 * 1024);
942 n->quota_violation_count--;
945 if (n->quota_violation_count > QUOTA_VIOLATION_DROP_THRESHOLD)
947 GNUNET_STATISTICS_update (GST_stats,
949 ("# bandwidth quota violations by other peers"),
951 *do_forward = GNUNET_NO;
952 return GNUNET_CONSTANTS_QUOTA_VIOLATION_TIMEOUT;
954 *do_forward = GNUNET_YES;
955 ret = GNUNET_BANDWIDTH_tracker_get_delay (&n->in_tracker, 0);
956 if (ret.rel_value > 0)
959 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG,
960 "Throttling read (%llu bytes excess at %u b/s), waiting %llu ms before reading more.\n",
961 (unsigned long long) n->in_tracker.
962 consumption_since_last_update__,
963 (unsigned int) n->in_tracker.available_bytes_per_s__,
964 (unsigned long long) ret.rel_value);
966 GNUNET_STATISTICS_update (GST_stats,
967 gettext_noop ("# ms throttling suggested"),
968 (int64_t) ret.rel_value, GNUNET_NO);
975 * Keep the connection to the given neighbour alive longer,
976 * we received a KEEPALIVE (or equivalent).
978 * @param neighbour neighbour to keep alive
981 GST_neighbours_keepalive (const struct GNUNET_PeerIdentity *neighbour)
983 struct NeighbourMapEntry *n;
985 GNUNET_assert (neighbours != NULL);
987 n = lookup_neighbour (neighbour);
990 GNUNET_STATISTICS_update (GST_stats,
992 ("# KEEPALIVE messages discarded (not connected)"),
996 GNUNET_SCHEDULER_cancel (n->timeout_task);
998 GNUNET_SCHEDULER_add_delayed (GNUNET_CONSTANTS_IDLE_CONNECTION_TIMEOUT,
999 &neighbour_timeout_task, n);
1004 * Change the incoming quota for the given peer.
1006 * @param neighbour identity of peer to change qutoa for
1007 * @param quota new quota
1010 GST_neighbours_set_incoming_quota (const struct GNUNET_PeerIdentity *neighbour,
1011 struct GNUNET_BANDWIDTH_Value32NBO quota)
1013 struct NeighbourMapEntry *n;
1015 GNUNET_assert (neighbours != NULL);
1017 n = lookup_neighbour (neighbour);
1020 GNUNET_STATISTICS_update (GST_stats,
1022 ("# SET QUOTA messages ignored (no such peer)"),
1026 GNUNET_BANDWIDTH_tracker_update_quota (&n->in_tracker, quota);
1027 if (0 != ntohl (quota.value__))
1030 GNUNET_log (GNUNET_ERROR_TYPE_DEBUG, "Disconnecting peer `%4s' due to `%s'\n",
1031 GNUNET_i2s (&n->id), "SET_QUOTA");
1033 if (GNUNET_YES == n->is_connected)
1034 GNUNET_STATISTICS_update (GST_stats,
1035 gettext_noop ("# disconnects due to quota of 0"), 1,
1037 disconnect_neighbour (n);
1042 * Closure for the neighbours_iterate function.
1044 struct IteratorContext
1047 * Function to call on each connected neighbour.
1049 GST_NeighbourIterator cb;
1059 * Call the callback from the closure for each connected neighbour.
1061 * @param cls the 'struct IteratorContext'
1062 * @param key the hash of the public key of the neighbour
1063 * @param value the 'struct NeighbourMapEntry'
1064 * @return GNUNET_OK (continue to iterate)
1067 neighbours_iterate (void *cls, const GNUNET_HashCode * key, void *value)
1069 struct IteratorContext *ic = cls;
1070 struct NeighbourMapEntry *n = value;
1072 if (GNUNET_YES != n->is_connected)
1075 GNUNET_assert (n->ats_count > 0);
1076 ic->cb (ic->cb_cls, &n->id, n->ats, n->ats_count, n->plugin_name, n->addr, n->addrlen);
1082 * Iterate over all connected neighbours.
1084 * @param cb function to call
1085 * @param cb_cls closure for cb
1088 GST_neighbours_iterate (GST_NeighbourIterator cb, void *cb_cls)
1090 struct IteratorContext ic;
1092 GNUNET_assert (neighbours != NULL);
1096 GNUNET_CONTAINER_multihashmap_iterate (neighbours, &neighbours_iterate, &ic);
1101 * If we have an active connection to the given target, it must be shutdown.
1103 * @param target peer to disconnect from
1106 GST_neighbours_force_disconnect (const struct GNUNET_PeerIdentity *target)
1108 struct NeighbourMapEntry *n;
1109 struct GNUNET_TRANSPORT_PluginFunctions *papi;
1110 struct SessionDisconnectMessage disconnect_msg;
1112 GNUNET_assert (neighbours != NULL);
1114 n = lookup_neighbour (target);
1116 return; /* not active */
1117 if (GNUNET_YES == n->is_connected)
1119 /* we're actually connected, send DISCONNECT message */
1120 disconnect_msg.header.size = htons (sizeof (struct SessionDisconnectMessage));
1121 disconnect_msg.header.type = htons (GNUNET_MESSAGE_TYPE_TRANSPORT_DISCONNECT);
1122 disconnect_msg.reserved = htonl (0);
1123 disconnect_msg.purpose.size = htonl (sizeof (struct GNUNET_CRYPTO_RsaSignaturePurpose) +
1124 sizeof (struct GNUNET_TIME_AbsoluteNBO));
1125 disconnect_msg.purpose.purpose = htonl (GNUNET_MESSAGE_TYPE_TRANSPORT_SESSION_DISCONNECT);
1126 disconnect_msg.timestamp = GNUNET_TIME_absolute_hton (GNUNET_TIME_absolute_get ());
1127 GNUNET_assert (GNUNET_OK ==
1128 GNUNET_CRYPTO_rsa_sign (GST_my_private_key,
1129 &disconnect_msg.purpose,
1130 &disconnect_msg.signature));
1131 papi = GST_plugins_find (n->plugin_name);
1133 papi->send (papi->cls, target, (const void *) &disconnect_msg,
1134 sizeof (struct GNUNET_MessageHeader),
1135 UINT32_MAX /* priority */ ,
1136 GNUNET_TIME_UNIT_FOREVER_REL, n->session, n->addr, n->addrlen,
1137 GNUNET_YES, NULL, NULL);
1138 GNUNET_STATISTICS_update (GST_stats,
1139 gettext_noop ("# peers disconnected due to external request"), 1,
1142 disconnect_neighbour (n);
1146 /* end of file gnunet-service-transport_neighbours.c */